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

  1. <?xml version="1.0"?>
  2.  
  3. <bindings id="radioBindings"
  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="radiogroup">
  9.     <resources>
  10.       <stylesheet src="chrome://global/skin/radio.css"/>
  11.     </resources>
  12.  
  13.     <implementation implements="nsIDOMXULSelectControlElement, nsIAccessibleProvider">
  14.       <property name="accessible">
  15.         <getter>
  16.           <![CDATA[
  17.             var accService = Components.classes["@mozilla.org/accessibilityService;1"].getService(Components.interfaces.nsIAccessibilityService);
  18.             return accService.createXULRadioGroupAccessible(this);
  19.           ]]>
  20.         </getter>
  21.       </property>
  22.  
  23.       <property name="value" onset="this.setAttribute('value',val); return val;"
  24.                              onget="return this.getAttribute('value');"/>
  25.       <property name="disabled">
  26.         <getter>
  27.         <![CDATA[
  28.             return this.getAttribute('disabled') == 'true';
  29.         ]]>
  30.         </getter>
  31.         <setter>
  32.         <![CDATA[
  33.           if (val)
  34.             this.setAttribute('disabled', 'true');
  35.           else
  36.             this.removeAttribute('disabled');
  37.           var children = this._getRadioChildren();
  38.           for (var i = 0; i < children.length; ++i) {
  39.             children[i].disabled = val;
  40.           }
  41.           return val;
  42.         ]]>
  43.         </setter>
  44.       </property>
  45.       
  46.       <property name="selectedItem">
  47.         <getter>
  48.         <![CDATA[
  49.           var children = this._getRadioChildren();
  50.           for (var i = 0; i < children.length; ++i) {
  51.             if (children[i].selected)
  52.               return children[i];
  53.           }
  54.           return null;
  55.         ]]>
  56.         </getter>
  57.         <setter>
  58.         <![CDATA[
  59.           var alreadySelected = val.getAttribute("selected") == "true";
  60.           var focused = false;
  61.           if (this.getAttribute("focused") == "true") {
  62.             val.setAttribute("focused", "true");
  63.             focused = true;
  64.           }
  65.  
  66.           val.setAttribute("selected", "true");
  67.           this.value = val.value;
  68.           
  69.           // uncheck all other group nodes
  70.           var children = this._getRadioChildren();
  71.           for (var i = 0; i < children.length; ++i) {
  72.             if (children[i] != val) {
  73.               children[i].removeAttribute("selected");
  74.               children[i].removeAttribute("focused");
  75.             }
  76.           }
  77.  
  78.           var event = document.createEvent("Events");
  79.           event.initEvent("select", false, true);
  80.           this.dispatchEvent(event);
  81.  
  82.           if (!alreadySelected && focused) {
  83.             // Only report if actual change
  84.             var myEvent = document.createEvent("Events");
  85.             myEvent.initEvent("RadioStateChange", true, true);
  86.             val.dispatchEvent(myEvent);
  87.           }
  88.  
  89.           return val;
  90.         ]]>
  91.         </setter>        
  92.       </property>
  93.       
  94.       <property name="focusedItem">
  95.         <getter>
  96.         <![CDATA[
  97.           var children = this._getRadioChildren();
  98.           for (var i = 0; i < children.length; ++i) {
  99.             if (children[i].getAttribute("focused") == "true")
  100.               return children[i];
  101.           }
  102.           return null;
  103.         ]]>
  104.         </getter>
  105.         <setter>
  106.         <![CDATA[
  107.           if (val) val.setAttribute("focused", "true");
  108.           
  109.           // unfocus all other group nodes
  110.           var children = this._getRadioChildren();
  111.           for (var i = 0; i < children.length; ++i) {
  112.             if (children[i] != val)
  113.               children[i].removeAttribute("focused");
  114.           }
  115.           return val;
  116.         ]]>
  117.         </setter>
  118.       </property>
  119.       
  120.       <method name="checkAdjacentElement">
  121.         <parameter name="aNextFlag"/>
  122.         <body>
  123.         <![CDATA[
  124.           var currentElement = this.focusedItem;
  125.           // If focused item isn't a radio button, do nothing!
  126.           // (This allows textbox to work inside a radiogroup)
  127.           if (!currentElement)
  128.             return;
  129.           var index, i;
  130.           var children = this._getRadioChildren();
  131.           for (i = 0; i < children.length; ++i ) {
  132.             if (children[i] == currentElement) 
  133.               break;
  134.           }
  135.           
  136.           if (aNextFlag) {
  137.             do {
  138.               index = ++i % children.length;             
  139.               if (i > children.length)
  140.                 break;
  141.             }
  142.             while (children[index].getAttribute("hidden") == "true" || children[index].disabled);
  143.             // XXX check for collapsed attr and display/visibility props too
  144.  
  145.             this.selectedItem = children[index];
  146.             children[index].doCommand();
  147.           }
  148.           else {              
  149.             do {
  150.               index = i > 0 ? --i : (i = children.length - 1);
  151.               if (i == children.length)
  152.                 break;
  153.             }
  154.             while (children[index].getAttribute("hidden") == "true" || children[index].disabled);
  155.             // XXX check for collapsed attr and display/visibility props too
  156.  
  157.             this.selectedItem = children[index];
  158.             children[index].doCommand();
  159.           }
  160.         ]]>
  161.         </body>
  162.       </method>
  163.       <field name="mRadioChildren">[]</field>
  164.       <method name="_getRadioChildren">
  165.         <parameter name="aNode">this</parameter>
  166.         <body>
  167.         <![CDATA[
  168.           if (aNode) {
  169.             if (aNode == this && this.mRadioChildren)
  170.               return this.mRadioChildren;
  171.             else if (aNode.localName == "radio") {
  172.               this.mRadioChildren[this.mRadioChildren.length++] = aNode;
  173.               return this.mRadioChildren;
  174.             }
  175.             else if (aNode.localName == "radiogroup" && aNode != this)
  176.               return this.mRadioChildren;
  177.             for (var i = 0; i < aNode.childNodes.length; ++i)
  178.               this._getRadioChildren(aNode.childNodes[i]);
  179.           }
  180.           return this.mRadioChildren;
  181.         ]]>
  182.         </body>
  183.       </method>
  184.  
  185.       <method name="appendItem">
  186.         <parameter name="label"/>
  187.         <parameter name="value"/>
  188.         <body>
  189.         <![CDATA[
  190.           var XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  191.           var radio = document.createElementNS(XULNS, "radio");
  192.           radio.setAttribute("label", label);
  193.           radio.setAttribute("value", value);
  194.           this.appendChild(radio);
  195.           return radio;
  196.         ]]>
  197.         </body>
  198.       </method>
  199.       
  200.       <method name="insertItemAt">
  201.         <parameter name="index"/>
  202.         <parameter name="label"/>
  203.         <parameter name="value"/>
  204.         <body>
  205.         <![CDATA[
  206.           var XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  207.           var radio = document.createElementNS(XULNS, "radio");
  208.           radio.setAttribute("label", label);
  209.           radio.setAttribute("value", value);
  210.           var before = this.childNodes[index];
  211.           if (before)
  212.             this.insertBefore(radio, before);
  213.           else
  214.             this.appendChild(radio);
  215.           return radio;
  216.         ]]>
  217.         </body>
  218.       </method>
  219.  
  220.       <method name="removeItemAt">
  221.         <parameter name="index"/>
  222.         <body>
  223.         <![CDATA[
  224.           var remove = this.childNodes[index];
  225.           if (remove)
  226.             this.removeChild(remove);
  227.           return remove;
  228.         ]]>
  229.         </body>
  230.       </method>
  231.     </implementation>
  232.     
  233.     <handlers>
  234.       <handler event="click" button="0">
  235.         <![CDATA[
  236.           if (event.target.localName == "radio" && !event.target.disabled) 
  237.             this.selectedItem = event.target;
  238.           event.preventBubble();
  239.          ]]>
  240.       </handler>
  241.       <handler event="select">
  242.         <![CDATA[
  243.           //XXXblake this should not be necessary
  244.           //         initEvent was supposed to prevent this from bubbling
  245.           event.preventBubble();
  246.         ]]>
  247.       </handler>
  248.       <handler event="mousedown" button="0">
  249.         <![CDATA[
  250.           if (event.target.localName == "radio" && !event.target.disabled)
  251.             this.focusedItem = event.target;
  252.           event.preventBubble();
  253.          ]]>
  254.       </handler>
  255.      
  256.       <!-- keyboard navigation -->
  257.       <!-- Here's how keyboard navigation works in radio groups on Windows:
  258.            The group takes 'focus'
  259.            The user is then free to navigate around inside the group
  260.            using the arrow keys. Accessing previous or following radio buttons
  261.            is done solely through the arrow keys and not the tab button. Tab
  262.            takes you to the next widget in the tab order -->
  263.       <handler event="keypress" key=" ">
  264.          this.selectedItem = this.focusedItem;
  265.          this.selectedItem.doCommand();
  266.       </handler>
  267.       <handler event="keypress" keycode="VK_UP" phase="target">
  268.         this.checkAdjacentElement(false);
  269.         event.preventBubble();
  270.       </handler>
  271.       <handler event="keypress" keycode="VK_LEFT" phase="target">
  272.         this.checkAdjacentElement(false);
  273.         event.preventBubble();
  274.       </handler>
  275.       <handler event="keypress" keycode="VK_DOWN" phase="target">
  276.         this.checkAdjacentElement(true);
  277.         event.preventBubble();
  278.       </handler>
  279.       <handler event="keypress" keycode="VK_RIGHT" phase="target">
  280.         this.checkAdjacentElement(true);
  281.         event.preventBubble();
  282.       </handler>
  283.  
  284.       <!-- set a focused attribute on the selected item when the group
  285.            receives focus so that we can style it as if it were focused even though
  286.            it is not (Windows platform behaviour is for the group to receive focus,
  287.            not the item -->
  288.       <handler event="focus" phase="target">
  289.         if (event.target == this) {
  290.           this.setAttribute("focused", "true");
  291.           if (!this.focusedItem)
  292.             this.focusedItem = this.selectedItem;
  293.         }
  294.       </handler>
  295.       <handler event="blur" phase="target">
  296.         this.removeAttribute("focused");
  297.         this.focusedItem = null;
  298.       </handler>
  299.     </handlers>
  300.   </binding>
  301.  
  302.   <binding id="radio" extends="chrome://global/content/bindings/general.xml#control-item">
  303.     <resources>
  304.       <stylesheet src="chrome://global/skin/radio.css"/>
  305.     </resources>
  306.  
  307.     <content>
  308.       <xul:image class="radio-check" xbl:inherits="disabled,selected"/>
  309.       <xul:hbox class="radio-label-box" flex="1">
  310.         <xul:image class="radio-icon" xbl:inherits="src"/>
  311.         <xul:label class="radio-label" xbl:inherits="xbl:text=label,accesskey,crop" flex="1"/>
  312.       </xul:hbox>
  313.     </content>
  314.  
  315.     <implementation implements="nsIDOMXULSelectControlItemElement, nsIAccessibleProvider">
  316.       <constructor>
  317.         <![CDATA[
  318.           var parent = this.radioGroup;
  319.           parent.mRadioChildren[parent.mRadioChildren.length++] = this;
  320.         ]]>
  321.       </constructor>
  322.       <destructor>
  323.         <![CDATA[
  324.           var parent = this.radioGroup;
  325.           for (var i = 0; i < parent.mRadioChildren.length; ++i) {
  326.             if (parent.mRadioChildren[i] == this) {
  327.               parent.mRadioChildren.slice(i, i);
  328.               return;
  329.             }
  330.           }
  331.         ]]>
  332.       </destructor>
  333.       <property name="accessible">
  334.         <getter>
  335.           <![CDATA[
  336.             var accService = Components.classes["@mozilla.org/accessibilityService;1"].getService(Components.interfaces.nsIAccessibilityService);
  337.             return accService.createXULRadioButtonAccessible(this);
  338.           ]]>
  339.         </getter>
  340.       </property>
  341.       <property name="selected" readonly="true">
  342.         <getter>
  343.           <![CDATA[
  344.             return this.hasAttribute('selected');
  345.           ]]>
  346.         </getter>
  347.       </property>
  348.       <property name="radioGroup">
  349.         <getter>
  350.         <![CDATA[
  351.           var parent = this.parentNode;
  352.           while (parent) {
  353.             if (parent.localName == "radiogroup")
  354.               return parent;
  355.             parent = parent.parentNode;
  356.           }
  357.           return null;
  358.         ]]>
  359.         </getter>
  360.       </property>
  361.     </implementation>
  362.     <handlers>
  363.       <handler event="click" button="0">
  364.         <![CDATA[
  365.           if (!this.disabled)
  366.             this.radioGroup.selectedItem = this;
  367.          ]]>
  368.       </handler>
  369.  
  370.       <handler event="mousedown" button="0">
  371.         <![CDATA[
  372.           if (!this.disabled)
  373.             this.radioGroup.focusedItem = this;
  374.          ]]>
  375.       </handler>
  376.     </handlers>
  377.   </binding>
  378. </bindings>