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