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