home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Komunik / phoenix / chrome / toolkit.jar / content / global / bindings / button.xml < prev    next >
Extensible Markup Language  |  2002-11-05  |  11KB  |  325 lines

  1. <?xml version="1.0"?>
  2.  
  3. <bindings id="buttonBindings"
  4.    xmlns="http://www.mozilla.org/xbl"
  5.    xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  6.    xmlns:xbl="http://www.mozilla.org/xbl">
  7.  
  8.   <binding id="button-base" extends="chrome://global/content/bindings/general.xml#basetext">
  9.     <implementation implements="nsIDOMXULButtonElement, nsIAccessibleProvider">
  10.       <property name="accessible">
  11.         <getter>
  12.           <![CDATA[
  13.             var accService = Components.classes["@mozilla.org/accessibilityService;1"].getService(Components.interfaces.nsIAccessibilityService);
  14.             return accService.createXULButtonAccessible(this);
  15.           ]]>
  16.         </getter>
  17.       </property>
  18.  
  19.       <property name="type"
  20.                 onget="return this.getAttribute('type');"
  21.                 onset="this.setAttribute('type', val); return val;"/>
  22.  
  23.       <property name="dlgType"
  24.                 onget="return this.getAttribute('dlgType');"
  25.                 onset="this.setAttribute('dlgType', val); return val;"/>
  26.  
  27.       <property name="group"
  28.                 onget="return this.getAttribute('group');"
  29.                 onset="this.setAttribute('group', val); return val;"/>
  30.  
  31.       <property name="open"
  32.                 onget="return this.hasAttribute('open');"
  33.                 onset="if (val) this.setAttribute('open', 'true');
  34.                        else this.removeAttribute('open'); return val;"/>
  35.  
  36.       <property name="checked" onget="return this.hasAttribute('checked');">
  37.         <setter><![CDATA[
  38.           if (this.type == "checkbox") {
  39.             this.checkState = val ? 1 : 0;
  40.           } else if (this.type == "radio" && val) {
  41.             var sibs = this.parentNode.getElementsByAttribute("group", this.group);
  42.             for (var i = 0; i < sibs.length; ++i)
  43.               if (sibs[i].checked)
  44.                 sibs[i].checked = false;
  45.           }
  46.  
  47.           if (val)
  48.             this.setAttribute("checked", "true");
  49.           else
  50.             this.removeAttribute("checked");
  51.           
  52.           return val;
  53.         ]]></setter>
  54.       </property>
  55.       
  56.       <property name="checkState">
  57.         <getter><![CDATA[
  58.           var state = this.getAttribute("checkState");
  59.           if (state == "")
  60.             return this.checked ? 1 : 0;
  61.           else
  62.             return state == "0" ? 0 : (state == "2" ? 2 : 1);
  63.         ]]></getter>
  64.         <setter><![CDATA[
  65.           this.setAttribute("checkState", val);
  66.           return val;
  67.         ]]></setter>
  68.       </property>
  69.       
  70.       <property name="autoCheck"
  71.                 onget="return this.getAttribute('autoCheck') == 'true';"
  72.                 onset="this.setAttribute('autoCheck', val); return val;"/>
  73.  
  74.       <method name ="filterButtons">
  75.         <parameter name="node"/>
  76.         <body>
  77.         <![CDATA[
  78.           if (node.localName == "button" && node.accessKey &&
  79.             !node.disabled && !node.collapsed && !node.hidden)
  80.             return NodeFilter.FILTER_ACCEPT;
  81.           return NodeFilter.FILTER_SKIP;
  82.         ]]>
  83.         </body>
  84.       </method>
  85.  
  86.       <method name="fireAccessKeyButton">
  87.         <parameter name="aSubtree"/>
  88.         <parameter name="aAccessKeyLower"/>
  89.         <body>
  90.         <![CDATA[
  91.           var iterator = aSubtree.ownerDocument.createTreeWalker(aSubtree, 
  92.                                                                  NodeFilter.SHOW_ELEMENT, 
  93.                                                                  this.filterButtons, false);
  94.           while (iterator.nextNode()) {
  95.             var test = iterator.currentNode;
  96.             if (test.accessKey.toLowerCase() == aAccessKeyLower && 
  97.                 !test.disabled && !test.collapsed && !test.hidden) {
  98.               test.focus();
  99.               test.click();
  100.               return true;
  101.             }
  102.           }
  103.           return false;
  104.         ]]>
  105.         </body>
  106.       </method>
  107.     </implementation>
  108.  
  109.     <handlers>
  110.       <handler event="command">
  111.       <![CDATA[
  112.         if (this.autoCheck || !this.hasAttribute("autoCheck")) {
  113.           if (this.type == "checkbox") {
  114.             this.checked = !this.checked;
  115.           } else if (this.type == "radio") {
  116.             this.checked = true;
  117.           }
  118.         }
  119.       ]]>
  120.       </handler>
  121.  
  122.       <handler event="keypress">
  123.       <![CDATA[
  124.         if (event.keyCode == KeyEvent.DOM_VK_UP ||
  125.             event.keyCode == KeyEvent.DOM_VK_LEFT)
  126.           return window.document.commandDispatcher.rewindFocus();
  127.  
  128.         if (event.keyCode == KeyEvent.DOM_VK_DOWN ||
  129.             event.keyCode == KeyEvent.DOM_VK_RIGHT)
  130.           return window.document.commandDispatcher.advanceFocus();
  131.  
  132.         if (event.keyCode || event.charCode <= ' ')
  133.           return;  // No arrow key or potential accesskey pressed
  134.  
  135.         // Possible accesskey pressed
  136.         var charPressedLower = String.fromCharCode(event.charCode).toLowerCase();
  137.  
  138.         // If the accesskey of the current button is pressed, just activate it
  139.         if (this.accessKey.toLowerCase() == charPressedLower) {
  140.           this.click();
  141.           return;
  142.         }
  143.  
  144.         // Search for accesskey in the list of buttons for this doc and each subdoc
  145.         // Get the buttons for the main document and all sub-frames
  146.         for (var frameCount = -1; frameCount < window.top.frames.length; frameCount++) {
  147.           var doc = (frameCount == -1)? window.top.document: 
  148.             window.top.frames[frameCount].document
  149.           if (this.fireAccessKeyButton(doc.documentElement, charPressedLower))
  150.             return;
  151.         }
  152.  
  153.         // Test anonymous buttons
  154.         var dlg = window.top.document;
  155.         var buttonBox = dlg.getAnonymousElementByAttribute(dlg.documentElement,
  156.                                                          "anonid", "buttons");
  157.         if (buttonBox)
  158.           this.fireAccessKeyButton(buttonBox, charPressedLower);
  159.       ]]>
  160.       </handler>
  161.  
  162.     </handlers>
  163.   </binding>
  164.  
  165.   <binding id="button" display="xul:button"
  166.            extends="chrome://global/content/bindings/button.xml#button-base">
  167.     <resources>
  168.       <stylesheet src="chrome://global/skin/button.css"/>
  169.     </resources>
  170.  
  171.     <content>
  172.       <children includes="observes|template|menupopup|tooltip"/>
  173.       <xul:hbox class="box-inherit button-box" xbl:inherits="align,dir,pack,orient"
  174.                 align="center" pack="center" flex="1">
  175.         <children>
  176.           <xul:image class="button-icon" xbl:inherits="src=image"/>
  177.           <xul:label class="button-text" xbl:inherits="value=label,accesskey,crop" flex="1"/>
  178.         </children>
  179.       </xul:hbox>
  180.     </content>
  181.   </binding>
  182.  
  183.   <binding id="menu" display="xul:menu"
  184.            extends="chrome://global/content/bindings/button.xml#button">
  185.     <content>
  186.       <children includes="observes|template|menupopup|tooltip"/>
  187.       <xul:hbox class="box-inherit button-box" xbl:inherits="align,dir,pack,orient"
  188.                 align="center" pack="center" flex="1">
  189.         <children>
  190.           <xul:image class="button-icon" xbl:inherits="src=image"/>
  191.           <xul:label class="button-text" xbl:inherits="value=label,accesskey,crop" flex="1"/>
  192.           <xul:dropmarker class="button-menu-dropmarker" xbl:inherits="open,disabled"/>
  193.         </children>
  194.       </xul:hbox>
  195.     </content>
  196.   </binding>           
  197.  
  198.   <binding id="menu-button-base"
  199.            extends="chrome://global/content/bindings/button.xml#button-base">
  200.     <implementation>
  201.       <constructor>
  202.         this.init();
  203.       </constructor>
  204.       
  205.       <method name="init">
  206.         <body>
  207.         <![CDATA[
  208.           var btn = document.getAnonymousElementByAttribute(this, "anonid", "button");
  209.           if (!btn)
  210.             throw "XBL binding for <button type=\"menu-button\"/> binding must contain an element with anonid=\"button\"";
  211.           
  212.           btn._menubuttonParent = this;
  213.           btn.addEventListener("mouseover", function() { 
  214.             if (!this.disabled)
  215.               this._menubuttonParent.buttonover = true; 
  216.           }, true);
  217.           btn.addEventListener("mouseout", function() {
  218.             this._menubuttonParent.buttonover = false;
  219.            }, true);
  220.           btn.addEventListener("mousedown", function() {
  221.             if (!this.disabled) {
  222.               this._menubuttonParent.buttondown = true;
  223.               this._menubuttonParent._captureMouseUp();
  224.             }
  225.           }, true);
  226.         ]]>
  227.         </body>
  228.       </method>
  229.       
  230.       <property name="buttonover" onget="return this.getAttribute('buttonover');">
  231.         <setter>
  232.         <![CDATA[
  233.           var v = val || val == "true";
  234.           if (!v && this.buttondown) { 
  235.             this.buttondown = false;
  236.             this._pendingActive = true;
  237.           } 
  238.           else {
  239.             if (this._pendingActive) {
  240.               this.buttondown = true;
  241.               this._pendingActive = false;
  242.             }
  243.           }
  244.  
  245.           if (v)
  246.             this.setAttribute("buttonover", "true");
  247.           else
  248.             this.removeAttribute("buttonover");
  249.           return val;
  250.         ]]>
  251.         </setter>
  252.       </property>
  253.       
  254.       <property name="buttondown" onget="return this.getAttribute('buttondown') == 'true';">
  255.         <setter>
  256.         <![CDATA[
  257.           if (val || val == "true")
  258.             this.setAttribute("buttondown", "true");
  259.           else
  260.             this.removeAttribute("buttondown");
  261.           return val;
  262.         ]]>
  263.         </setter>
  264.       </property>
  265.       
  266.       <field name="_pendingActive">false</field>
  267.  
  268.       <method name="_captureMouseUp">
  269.         <body>
  270.         <![CDATA[
  271.           document.__ButtonMenuMouseDown__ = this;
  272.           document.addEventListener("mouseup", this._onGlobalMouseUp, true);
  273.         ]]>
  274.         </body>
  275.       </method>
  276.  
  277.       <method name="_onGlobalMouseUp" readonly="true">
  278.         <body>
  279.         <![CDATA[
  280.           var btn = document.__ButtonMenuMouseDown__;
  281.           btn._onMouseReallyUp();
  282.           document.removeEventListener("mouseup", btn._onGlobalMouseUp, true);
  283.           delete document.__ButtonMenuMouseDown__;
  284.         ]]>
  285.         </body>
  286.       </method>
  287.  
  288.       <method name="_onMouseReallyUp">
  289.         <body>
  290.         <![CDATA[
  291.           this._pendingActive = false;
  292.           this.buttondown = false;
  293.         ]]>
  294.         </body>
  295.       </method>
  296.  
  297.     </implementation>
  298.   </binding>
  299.  
  300.   <binding id="menu-button" display="xul:menu"
  301.            extends="chrome://global/content/bindings/button.xml#menu-button-base">
  302.     <resources>
  303.       <stylesheet src="chrome://global/skin/button.css"/>
  304.     </resources>
  305.  
  306.     <content>
  307.       <children includes="observes|template|menupopup|tooltip"/>
  308.       <xul:button class="box-inherit button-menubutton-button"
  309.                   anonid="button" flex="1" allowevents="true"
  310.                   xbl:inherits="disabled,crop,image,label,accessKey,command,
  311.                                 buttonover,buttondown,align,dir,pack,orient">
  312.         <children/>
  313.       </xul:button>
  314.       <xul:dropmarker class="button-menubutton-dropmarker" xbl:inherits="open,disabled"/>
  315.     </content>
  316.   </binding>
  317.   
  318.   <binding id="button-image" display="xul:button"
  319.            extends="chrome://global/content/bindings/button.xml#button">
  320.     <content>
  321.       <xul:image class="button-image-icon" xbl:inherits="src=image"/>
  322.     </content>
  323.   </binding>
  324.   
  325. </bindings>