<?xml version="1.0"?>

<bindings id="menubuttonBindings"
   xmlns="http://www.mozilla.org/xbl"
   xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="menubutton-base">
    <resources>
      <stylesheet src="chrome://global/skin/menubutton.css"/>
    </resources>
  </binding>

  <binding id="menubutton-single-right" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="menubutton-internal-box" autostretch="never">
        <xul:text class="menubutton-single-text" inherits="value=label,crop,accesskey"/>
        <xul:image class="menubutton-single-icon"/>
      </xul:box>
      <children includes="menupopup|template"/>
    </content>
  </binding>

  <binding id="menubutton-single-left" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="menubutton-internal-box" autostretch="never">
        <xul:image class="menubutton-single-icon"/>
        <xul:text class="menubutton-single-text" inherits="value=label,crop,accesskey"/>
      </xul:box>
      <children includes="menupopup|template"/>
    </content>
  </binding>
  
  <binding id="menubutton-single-top" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="button-internal-box" autostretch="never" orient="vertical" flex="1">
        <xul:image class="menubutton-single-icon"/>
        <xul:text class="menubutton-single-text" inherits="value=label,crop,accesskey"/>
      </xul:box>
      <children includes="menupopup"/>
    </content>
  </binding>

  <binding id="menubutton-single-bottom" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="button-internal-box" autostretch="never" orient="vertical" flex="1">
        <xul:text class="menubutton-single-text" inherits="value=label,crop,accesskey"/>
        <xul:image class="menubutton-single-icon"/>
      </xul:box>
      <children includes="menupopup"/>
    </content>
  </binding>

  <binding id="menubutton-icon" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="menubutton-icon-internal-box" autostretch="never">
        <xul:image class="menubutton-icon-icon" inherits="src"/>
      </xul:box>
      <children includes="menupopup"/>
    </content>
  </binding>
  
  <binding id="menubutton-dual-top" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button top" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,label,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
      <children includes="menupopup"/>
    </content>
  </binding>

  <binding id="menubutton-dual-left" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button left" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,label,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
      <children includes="menupopup"/>
    </content>
  </binding>

  <binding id="menubutton-dual-right" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button right" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,label,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
      <children includes="menupopup"/>
    </content>
  </binding>

  <binding id="menubutton-dual-bottom" extends="chrome://global/content/bindings/menubutton.xml#menubutton-base">
    <content>
      <xul:box class="menubutton-internal-box">
        <xul:button class="menubutton-dual-button bottom" allowevents="true" 
                    inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,label,crop,accesskey"/>
        <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle">
          <xul:image class="menubutton-dropmarker"/>
        </xul:box>
      </xul:box>
      <children includes="menupopup"/>
    </content>
  </binding>

  <binding id="menubutton-dual-ex" extends="chrome://global/content/bindings/menubutton.xml#menubutton-dual-top">
    <content>
      <xul:button class="menubutton-dual-ex-button top" allowevents="true"
                  inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,label,crop,accesskey,disabled"
                  onmouseover="this.parentNode.buttonover = true;"
                  onmouseout="this.parentNode.buttonover = false;"
                  onmousedown="this.parentNode.buttondown = true; this.parentNode.captureMouseUp();"/>
      <xul:box class="menubutton-dual-dropmarker-box" autostretch="never" valign="middle" inherits="disabled">
        <xul:image class="menubutton-dropmarker" inherits="disabled"/>
      </xul:box>
      <children includes="menupopup"/>
    </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.setAttribute("buttondown", "true");
              this._pendingActive = false;
            }
          }
          this.setAttribute("buttonover", val);
          return val;
        ]]>
        </setter>
      </property>
      
      <property name="buttondown">
        <getter>
          return this.getAttribute("buttondown");
        </getter>
        <setter>
          this.setAttribute("buttondown", val);
          return val;
        </setter>
      </property>
      
      <property name="_pendingActive">false</property>
      <property name="_buttonIsDown">false</property>
      
      <property name="_globalMouseUpHandler" readonly="true">
      <![CDATA[
          var fn = function(aEvent)
          {
            var btn = document.__MenubuttonDualExMouseDown__;
            btn.onMouseReallyUp();
            document.removeEventListener("mouseup", btn._globalMouseUpHandler, true);
            document.__MenubuttonDualExMouseDown__ = null;
          };
          fn;
      ]]>
      </property>
      
      <method name="captureMouseUp">
        <body>
        <![CDATA[
          document.__MenubuttonDualExMouseDown__ = this;
          document.addEventListener("mouseup", this._globalMouseUpHandler, true);
        ]]>
        </body>
      </method>
      
      <method name="onMouseReallyUp">
        <body>
        <![CDATA[
          this._pendingActive = false;
          this.buttondown = false;
        ]]>
        </body>
      </method>
      
    </implementation>
  </binding>
  
  <binding id="menubutton-dual-ex-left" extends="chrome://global/content/bindings/menubutton.xml#menubutton-dual-ex">
    <content>
      <xul:button class="menubutton-dual-ex-button left" allowevents="true"
                  inherits="tooltiptext=buttontooltiptext,oncommand=buttonaction,src,label,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>
      <children includes="menupopup"/>
    </content>
  </binding>

</bindings>
