home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Theme / 8GadgetPack / 8GadgetPackSetup.msi / Gadgets.7z / Gadgets / HUD-Time.gadget / js / font-picker.js < prev    next >
Text File  |  2012-11-01  |  7KB  |  245 lines

  1. /*
  2. Based on Jonathan Abbott's Flip Calendar gadget
  3. http://vistagadgets.spaces.live.com/
  4. */
  5.  
  6. console.log('loaded: font-picker.js');
  7.  
  8. var fontPicker = {
  9.     supportedFontTypes: ' (truetype) ',
  10.     fontsToSkip: ':Bookshelf Symbol 7:'+':MS Outlook:'+':MS Reference Specialty:'+':MT Extra:'+':Symbol:'+':Webdings:'+':Wingdings:'+':Wingdings 2:'+':Wingdings 3:',
  11.  
  12.     fillYield: 50,    // Yield back to IE every X ms
  13.  
  14.     fontSizer: null,
  15.     fonttypeIndex: 0,
  16.     currentFont: System.Gadget.document.parentWindow.font,
  17.     selectingFont: -1,
  18.  
  19.     init: function() {
  20.         console.log('[init]');
  21.  
  22.         // System.Gadget.onSettingsClosing = this.settingsClosing;
  23.  
  24.         divFonttype.style.fontFamily = divFonttype.innerText = this.currentFont;
  25.         divFonttype.style.fontSize = '16px';
  26.  
  27.         this.fontSizer = document.createElement('<g:background />').addTextObject(this.currentFont, this.currentFont, 16, 'black', 0, 0);
  28.  
  29.         //remove focus from the 1st checkbox
  30.         document.body.focus();
  31.         
  32.         //read the fonts from the registry and build the table
  33.         this.buildList(this.readFonts());
  34.  
  35.         // setFont(currentFont, 0);
  36.         // console.log(tdFonts);
  37.         // console.log(this.fonttypeIndex);
  38.         tdFonts.rows(this.fonttypeIndex).className = 'menuItemSelected';
  39.     },
  40.  
  41.     settingsClosing: function(event) {
  42.         console.log('[settingsClosing]');
  43.         
  44.         if (event.closeAction == event.Action.commit) {
  45.             System.Gadget.Settings.writeString('font', divFonttype.innerText);
  46.         }
  47.         // event.cancel = false;
  48.     },
  49.  
  50.     readFonts: function() {
  51.         console.log('[readFonts]');
  52.  
  53.         var HKLM = 2147483650;
  54.         var rPath = 'SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\\';
  55.  
  56.         // connect to the registry
  57.         var oSwbem = new ActiveXObject('WbemScripting.SwbemLocator');
  58.         var oSvc = oSwbem.ConnectServer(null, 'root\\default');
  59.         var oReg = oSvc.Get('StdRegProv');
  60.  
  61.         // enumerate the values 
  62.         var oMethod = oReg.Methods_.Item('EnumValues');
  63.         var oInParam = oMethod.InParameters.SpawnInstance_();
  64.         oInParam.hDefKey = HKLM;
  65.         oInParam.sSubKeyName = rPath;
  66.         var oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);
  67.  
  68.         // get the values into an array
  69.         var sNames = oOutParam.sNames.toArray();
  70.  
  71.         // sort it
  72.         sNames.sort();
  73.  
  74.         return sNames;
  75.     },
  76.  
  77.     buildList: function(listItems) {
  78.         console.log('[buildList]');
  79.  
  80.         // try{
  81.             var counter = new Date();
  82.  
  83.             while (listItems.length > 0 && (new Date())-counter<this.fillYield) {
  84.                 // is the font one we can use
  85.                 var fontType = ' ' + listItems[0].substring(listItems[0].lastIndexOf('(')).toLowerCase() + ' ';
  86.                 var fontName = listItems[0].substring(0, listItems[0].length - fontType.length + 1);
  87.  
  88.                 if(this.supportedFontTypes.indexOf(fontType) > -1 && this.fontsToSkip.indexOf(':' + fontName + ':') == -1) {
  89.                     //check the height, to see if it's a font we can use
  90.                     this.fontSizer.font = this.fontSizer.value = fontName;
  91.                     this.fontSizer.height = 0;
  92.  
  93.                     if (this.fontSizer.height != 0) {
  94.                         // console.log('Using font: '+fontName);
  95.  
  96.                         var tRow = tdFonts.insertRow(tdFonts.rows.length);
  97.                         tRow.className = 'menuItem';
  98.                         
  99.                         var tCell = tRow.insertCell(0);
  100.                         tCell.innerText = fontName;
  101.                         tCell.style.fontFamily = fontName;
  102.                         tCell.style.fontSize = divFonttype.style.fontSize;
  103.  
  104.                         if (fontName == this.currentFont) {
  105.                             this.fonttypeIndex = tdFonts.rows.length - 1;
  106.                             // tRow.className = 'menuItemSelected';
  107.                         }
  108.  
  109.                     } else {
  110.                         // console.log('Cant use font: '+fontName);
  111.                     }
  112.                 }
  113.                 listItems.shift();
  114.             }
  115.  
  116.             if (listItems.length > 0) {
  117.                 var context = this;
  118.                 setTimeout(function() { context.buildList(listItems); }, 20);
  119.             }
  120.         // } catch(err) {
  121.             // console.log('buildList: '+err.name+' - '+err.message);
  122.         // }
  123.     },
  124.  
  125.     showFontMenu: function(id, x, y) {
  126.         console.log('[showFontMenu]');
  127.  
  128.         if (id == -1) {
  129.             if (fontMenu.style.display != 'none') {
  130.                 id = this.selectingFont;
  131.             } else {
  132.                 return;
  133.             }
  134.         }
  135.             
  136.         fontMenu.style.display = (fontMenu.style.display == 'none') ? 'inline' : 'none';
  137.  
  138.         if (fontMenu.style.display == 'none') {
  139.             console.log('[showFontMenu] hidden');
  140.  
  141.             switch (this.selectingFont) {
  142.                 case 0:
  143.                     fontDropPNG.src = 'images/dropmenu.png';
  144.                     break;
  145.             }
  146.             this.selectingFont = -1;
  147.         } else {
  148.             console.log('[showFontMenu] displayed');
  149.  
  150.             fontMenu.style.top = y;
  151.             fontMenu.style.left = x;
  152.             this.selectingFont = id;
  153.             
  154.             // console.log(tdFonts.rows(fonttypeIndex).className);
  155.             // console.log(id);
  156.  
  157.             switch (id) {
  158.                 case 0:
  159.                     fontMenu.scrollTop = tdFonts.rows(this.fonttypeIndex).offsetTop - 65;
  160.                     tdFonts.rows(this.fonttypeIndex).className = 'menuItemSelected';
  161.                     break;
  162.             }
  163.  
  164.             // console.log(tdFonts.rows(fonttypeIndex).className);
  165.         }
  166.     },
  167.  
  168.     setFont: function(index, viaKey) {
  169.         console.log('[setFont] '+index+', '+viaKey);
  170.  
  171.         var sFont = tdFonts.rows(index).cells(0).innerText;
  172.  
  173.         var selecting = (viaKey != null) ? viaKey : this.selectingFont;
  174.         switch (selecting) {
  175.             case 0:
  176.                 divFonttype.style.fontFamily = sFont;
  177.                 divFonttype.innerText = sFont;
  178.                 tdFonts.rows(this.fonttypeIndex).className = 'menuItem';
  179.                 this.fonttypeIndex = index;
  180.                 tdFonts.rows(this.fonttypeIndex).className = 'menuItemSelected';
  181.                 break;
  182.         }
  183.  
  184.         // adjust the scrollbar if needed
  185.         if (fontMenu.scrollTop > tdFonts.rows(index).offsetTop) {
  186.             fontMenu.scrollTop = tdFonts.rows(index).offsetTop;
  187.         }
  188.  
  189.         if (fontMenu.scrollTop < tdFonts.rows(index).offsetTop+tdFonts.rows(index).offsetHeight-150) {
  190.             fontMenu.scrollTop = tdFonts.rows(index).offsetTop+tdFonts.rows(index).offsetHeight-150;
  191.         }
  192.     },
  193.  
  194.     setFontStyle: function(i) {
  195.         console.log('[setFontStyle]');
  196.  
  197.         switch (i) {
  198.             case 0:
  199.                 divFonttype.style.fontWeight = divFonttypeBold.checked ? 'bold' : '';
  200.                 divFonttype.style.fontStyle = divFonttypeItalic.checked ? 'italic' : '';
  201.                 break;
  202.         }
  203.     },
  204.  
  205.     keyPress: function(i) {
  206.         console.log('[keyPress]');
  207.  
  208.         switch (event.keyCode)
  209.         {
  210.             case 40:    //down arrow
  211.                 if (i == 0) {
  212.                     if (this.fonttypeIndex < tdFonts.rows.length-1) {
  213.                         this.setFont(this.fonttypeIndex+1, i);
  214.                     }
  215.                 }
  216.                 break;
  217.             case 38:    //up arrow
  218.                 if (i == 0) {
  219.                     if (this.fonttypeIndex > 0) {
  220.                         this.setFont(this.fonttypeIndex-1, i);
  221.                     }
  222.                 }
  223.                 break;
  224.             case 36:    //home
  225.                 this.setFont(0, i);
  226.                 break;
  227.             case 35:    //end
  228.                 this.setFont(tdFonts.rows.length - 1, i);
  229.                 break;
  230.         }
  231.     },
  232.  
  233.     mouseOver: function(mouseEntered, id) {
  234.         // console.log('[mouseOver]');
  235.  
  236.         var selected = (this.selectingFont == 0) ? this.fonttypeIndex : 0;
  237.  
  238.         if (mouseEntered) {
  239.             id.className = (id.rowIndex == selected) ? 'menuItemHoverSelected' : 'menuItemHover';
  240.         } else {
  241.             id.className = (id.rowIndex == selected) ? 'menuItemSelected' : 'menuItem';
  242.         }
  243.     }
  244. }
  245.