<?xml version="1.0"?>

<bindings id="menulistBindings"
   xmlns="http://www.mozilla.org/xbl"
   xmlns:html="http://www.w3.org/1999/xhtml"
   xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<!-- 
  BINDING : menulist
  XUL REPRESENTATION : <menulist>
  -->
  <binding id="menulist" extends="xul:menu">
    <content includes="menupopup">
      <xul:image class="menu-icon" inherits="src"/>
      <xul:text class="menu-text" flex="1" inherits="value,accesskey,crop" crop="right"/>
      <xul:image class="menu-dropmarker"/>
    </content>
    <handlers>
      <handler event="command" phase="capturing" action="if (event.target.localName == 'menuitem') this.selectedItem = event.target;"/>
      <handler event="create">
        <![CDATA[
          if (event.target.parentNode == this && this.selectedItem) {
            // Not ready for auto-setting the active child in hierarchies yet.
            // For now, only do this when the outermost menupopup opens.
            var menuBox = this.boxObject.QueryInterface(Components.interfaces.nsIMenuBoxObject);
            menuBox.activeChild = this.selectedItem;
          }
        ]]>
      </handler>
      <handler event="bindingattached" action="this.setInitialSelection()"/>
    </handlers>
    <implementation>
      <method name="setInitialSelection">
        <body>
          <![CDATA[
            this.setAttribute('sizetopopup', 'true');
            if (!this.value && this.childNodes.length) {
              var arr = this.firstChild.getElementsByAttribute('selected', 'true');

              if (!arr.length && this.data)
                arr = this.firstChild.getElementsByAttribute('data', this.data);

              if (arr.length)
                this.selectedItem = arr[0];
              else
                this.selectedIndex = 0;
            }
          ]]>
        </body>
      </method>
      <property name="data" onget="return this.getAttribute('data');">
        <setter>
          <![CDATA[
            var arr;

            if (this.childNodes.length)
              arr = this.firstChild.getElementsByAttribute('data', val);

            if (arr && arr.length)
              this.selectedItem = arr[0];
            else
              this.setAttribute('data', val);

            return val;
          ]]>
        </setter>
      </property>
      <property name="crop" onset="this.setAttribute('crop',val); return val;"
                            onget="return this.getAttribute('crop');"/>
      <property name="src"  onset="this.setAttribute('src',val); return val;"
                            onget="return this.getAttribute('src');"/>
      <property name="value" onset="this.setAttribute('value',val); return val;"
                             onget="return this.getAttribute('value');"/>
      <property name="disabled" onset="if (val) this.setAttribute('disabled',true); 
                                       else this.removeAttribute('disabled');
                                       return val;"
                                onget="return (this.getAttribute('disabled')=='true');"/>
      <property name="selectedInternal">
        null
      </property>

      <property name="selectedIndex">
        <getter>
          <![CDATA[
            // Quick and dirty. We won't deal with hierarchical menulists yet.
            if (!this.selectedItem)
              return -1;

            var children = this.selectedItem.parentNode.childNodes;
            for (var i = 0; i < children.length; i++) {
              if (children[i] == this.selectedItem)
                return i;
            }

            return -1;
          ]]>
        </getter>
        <setter>
          <![CDATA[
            if (val < 0)
              this.selectedItem = null;
            else {
              var curr = this.firstChild;
              while (curr && curr.localName != 'menupopup')
                curr = curr.nextSibling;
              if (curr && val < curr.childNodes.length)
                this.selectedItem = curr.childNodes[val];
            }
            return val;    
          ]]>
        </setter>
      </property>

      <property name="selectedItem">
        <getter>
          <![CDATA[
            return this.selectedInternal;
          ]]>
        </getter>
        <setter>
          <![CDATA[
            if (this.selectedInternal == val)
              return val;
            
            if (this.selectedInternal)
              this.selectedInternal.removeAttribute('selected');

            this.selectedInternal = val;
            if (!this.selectedInternal) {
              this.removeAttribute('data');
              this.removeAttribute('src');
              this.removeAttribute('value');
              return val;
            }

            val.setAttribute('selected', 'true');

            this.setAttribute('data', val.getAttribute('data'));
            this.setAttribute('src', val.getAttribute('src'));
            this.setAttribute('value', val.getAttribute('value'));

            return val;

          ]]>
        </setter>
      </property>
    </implementation>  
  </binding>

<!-- 
  BINDING : menulist-editable
  XUL REPRESENTATION : <menulist editable="true">
  -->
  <binding id="menulist-editable" extends="chrome://global/content/menulistBindings.xml#menulist">
    <content includes="menupopup">
      <xul:box flex="1" class="menulist-editable-box">
        <xul:image class="menu-icon" inherits="src"/>
        <html:input type="text" flex="1" class="menulist-editable-text" allowevents="true" inherits="value,disabled"/>
        <xul:image class="menu-dropmarker"/>
      </xul:box>
    </content>
    <implementation>
      <property name="inputField" readonly="true">
       <![CDATA[
        var v = document.getAnonymousNodes(this);
        var input = null;
        for (var i = 0; i < v.length; i++) {
          try {
            var l = v[i].getElementsByTagNameNS("http://www.w3.org/1999/xhtml", "input");
            if (l.length > 0) {
              input = l[0];
              break;
            }
          } catch (e) {} 
        }
        input;
      ]]>
      </property>
      <property name="value"      onset="this.inputField.value = val; return val;"
                                  onget="return this.inputField.value;"/>
    </implementation>
  </binding>
  
<!-- 
  BINDING : menubutton-single
  XUL REPRESENTATION : <menubutton>
  -->
  <binding id="menubutton-single-right">
    <content includes="menupopup">
      <xul:box class="menubutton-internal-box" autostretch="never">
        <xul:text class="menubutton-single-text" inherits="value,crop,accesskey"/>
        <xul:image class="menubutton-single-icon"/>
      </xul:box>
    </content>
  </binding>

  <binding id="menubutton-single-left">
    <content includes="menupopup">
      <xul:box class="menubutton-internal-box" autostretch="never">
        <xul:image class="menubutton-single-icon"/>
        <xul:text class="menubutton-single-text" inherits="value,crop,accesskey"/>
      </xul:box>
    </content>
  </binding>
  
<!-- 
  BINDING : menubutton-icon
  XUL REPRESENTATION : <menubutton class="menubutton-icon">
  -->
  <binding id="menubutton-icon">
    <content includes="menupopup">
      <xul:box class="menubutton-icon-internal-box" autostretch="never">
        <xul:image class="menubutton-icon-icon" inherits="src"/>
      </xul:box>
    </content>
  </binding>
  
<!-- 
  BINDING : menubutton-dual
  XUL REPRESENTATION : <menubutton class="menubutton-dual">
  -->
  <binding id="menubutton-dual-top">
    <content includes="menupopup">
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button top" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,value,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
    </content>
  </binding>

  <binding id="menubutton-dual-left">
    <content includes="menupopup">
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button left" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,value,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
    </content>
  </binding>

  <binding id="menubutton-dual-right">
    <content includes="menupopup">
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button right" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,value,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
    </content>
  </binding>

  <binding id="menubutton-dual-bottom">
    <content includes="menupopup">
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button bottom" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,value,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
    </content>
  </binding>

  <binding id="menubutton-dual-ex" extends="chrome://global/content/menulistBindings.xml#menubutton-dual-top">
    <content includes="menupopup">
      <xul:button class="menubutton-dual-ex-button top" allowevents="true" autostretch="never" align="center"
                  inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,value,crop,accesskey,disabled"
                  onmouseover="this.parentNode.buttonover = true;"
                  onmouseout="this.parentNode.buttonover = false;"
                  onmousedown="this.parentNode.buttondown = true;"
                  onmouseup="this.parentNode.buttondown = false;"/>
      <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle" inherits="disabled">
        <xul:image class="menubutton-dropmarker" inherits="disabled"/>
      </xul:box>
    </content>
    <implementation>
      <property name="buttonover">
        <getter>
          return this.getAttribute("buttonover");
        </getter>
        <setter>
        <![CDATA[
          if (!val && this.getAttribute("buttondown") == "true") { 
            this.removeAttribute("buttondown");
            this._pendingActive = true;
          } 
          else {
            if (this._pendingActive && this._buttonIsDown) {
              this.setAttribute("buttondown", "true");
              this._pendingActive = false;
              this._buttonIsDown = false;
            }
          }
          this.setAttribute("buttonover", val);
          return val;
        ]]>
        </setter>
      </property>
      <property name="buttondown">
        <getter>
          return this.getAttribute("buttondown");
        </getter>
        <setter>
          if (val) this._buttonIsDown = true;
          this.setAttribute("buttondown", val);
          return val;
        </setter>
      </property>
      <property name="_pendingActive">false</property>
      <property name="_buttonIsDown">false</property>
    </implementation>
  </binding>
  
  <binding id="menubutton-dual-ex-left" extends="chrome://global/content/menulistBindings.xml#menubutton-dual-ex">
    <content excludes="menupopup">
      <xul:button class="menubutton-dual-ex-button left" allowevents="true" autostretch="never" 
                  inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,value,crop,accesskey,disabled"
                  onmouseover="this.parentNode.buttonover = true;"
                  onmouseout="this.parentNode.buttonover = false;"
                  onmousedown="this.parentNode.buttondown = true;"
                  onmouseup="this.parentNode.buttondown = false;"/>
      <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle" inherits="disabled">
        <xul:image class="menubutton-dropmarker" inherits="disabled"/>
      </xul:box>
    </content>
  </binding>

<!--
  BINDING : menubutton-item
  XUL REPRESENTATION : menuitem
  -->  
  <binding id="menubutton-item">
    <content autostretch="never" includes="menupopup">
      <xul:text class="menubutton-text" flex="1" inherits="value,accesskey,crop" crop="right"/>
    </content>
  </binding>  
  
  
</bindings>
