<?xml version="1.0"?>

<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (the "License"); you may not use this file except in compliance with
   - the License. You may obtain a copy of the License at
   - http://www.mozilla.org/MPL/
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is Mozilla XForms support.
   -
   - The Initial Developer of the Original Code is
   - Alexander Surkov.
   - Portions created by the Initial Developer are Copyright (C) 2006
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -  Alexander Surkov <surkov@dc.baikal.ru>
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the GPL or the LGPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   -
   - ***** END LICENSE BLOCK ***** -->


<!--
  This file contains xforms controls implementation for XUL. All controls
  are inherited from interface bindings realized in xforms.xml.
-->

<!DOCTYPE bindings [
<!ENTITY % xformsDTD SYSTEM "chrome://xforms/locale/xforms.dtd">
  %xformsDTD;
]>

<bindings id="xformsBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xbl="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
          xmlns:mozType="http://www.mozilla.org/projects/xforms/2005/type">

  <!-- OUTPUT: <DEFAULT>
    @note - xf:output can support tab navigation
  -->
  <binding id="xformswidget-output"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <resources>
      <stylesheet src="chrome://xforms/skin/xforms-xul.css"/>
    </resources>

    <content>
      <children includes="label"/>
      <xul:description class="xf-value" anonid="control"
                       xbl:inherits="tabindex">
        <children/>
      </xul:description>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'control'),

            set value(val) {
              this.firstChild.data = val ? val : "";
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- OUTPUT: <DATE, APPEARANCE='FULL'> -->
  <binding id="xformswidget-output-date-full"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <xul:box mozType:calendar="true" anonid="control" readonly="true"
               xbl:inherits="tabindex"/>
      <children/>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return this.ownerDocument.
            getAnonymousElementByAttribute(this, "anonid", "control");
        </body>
      </method>
    </implementation>
  </binding>


  <!-- OUTPUT: <MEDIATYPE="image/*", TYPE="xsd:anyURI"> -->
  <binding id="xformswidget-output-mediatype-anyURI"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <xul:image class="xf-value" anonid="control"
                 xbl:inherits="tabindex"/>
      <children/>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "control"),

            set value(val) {
              // We have to use a timeout since set value can be hit during the
              // output's constructor.  There are always potential problems
              // changing the DOM during the constructor.  For example, if we
              // don't use a timeout then setting the img's @src won't work on
              // FF 1.5 or FF 2.  So we'll wait a tick before changing it here.
              setTimeout(function(img, val) {img.src = val;}, 0, this, val);
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- OUTPUT: <MEDIATYPE="image/*", TYPE="xsd:base64Binary"> -->
  <binding id="xformswidget-output-mediatype-base64Binary"
           extends="chrome://xforms/content/xforms.xml#xformswidget-output-base">
    <content>
      <children includes="label"/>
      <xul:image class="xf-value" anonid="control"
                 xbl:inherits="tabindex"/>
      <children/>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "control"),

            set value(val) {
              // We have to use a timeout since set value can be hit during the
              // output's constructor.  There are always potential problems
              // changing the DOM during the constructor.  For example, if we
              // don't use a timeout then setting the img's @src won't work on
              // FF 1.5 or FF 2.  So we'll wait a tick before changing it here.
              setTimeout(function(img, val) {img.src = "data:[image/*][;base64]," + val;}, 0, this, val);
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- LABEL: <DEFAULT>
    @note - xf:label can support tab navigation
  -->
  <binding id="xformswidget-label"
           extends="chrome://xforms/content/xforms.xml#xformswidget-label-base">
    <resources>
      <stylesheet src="chrome://xforms/skin/xforms-xul.css"/>
    </resources>

    <content>
      <xul:label anonid="implicitcontent"
                 xbl:inherits="tabindex"/>
      <xul:label anonid="explicitcontent"
                 xbl:inherits="tabindex"><children/></xul:label>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            _implicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'implicitcontent'),
            _explicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'explicitcontent'),
            __proto__: this,

            set value(val) {
              if (val != null) {
                this._implicitContent.hidden = false;
                this._explicitContent.hidden = true;
                this._implicitContent.textContent = val;
              } else {
                this._implicitContent.hidden = true;
                this._explicitContent.hidden = false;
                this._implicitContent.textContent = '';
              }
            },

            get textValue() {
              if (!this._implicitContent.hidden)
                return this._implicitContent.textContent;
              return this.textContent;
            },
            get nodeValue() {
              var fragment = this.ownerDocument.createDocumentFragment();

              var container = null;
              if (!this._implicitContent.hidden)
                container = this._implicitContent;
              else
                container = this;

              for (var node = container.firstChild; node; node = node.nextSibling) {
                fragment.appendChild(node.cloneNode(true));
              }
              var rep = new XMLSerializer().serializeToString(fragment);
              return fragment;
            }
          };
        </body>
      </method>

    </implementation>
  </binding>


  <!-- LABEL: <ACCESKEY SUPPORT> -->
  <binding id="xformswidget-label-accesskey"
           extends="chrome://xforms/content/xforms.xml#xformswidget-label-accesskey-base">
    <resources>
      <stylesheet src="chrome://xforms/skin/xforms-xul.css"/>
    </resources>

    <content>
      <xul:label anonid="implicitcontent"
                 xbl:inherits="tabindex"/>
      <xul:label anonid="explicitcontent"
                 xbl:inherits="tabindex"><children/></xul:label>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
        <![CDATA[
          return {
            _labelControl: this,
            _implicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'implicitcontent'),
            _explicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'explicitcontent'),

            set value(val) {
              var textnode = null;

              if (val != null) {
                this._explicitContent.hidden = true;
                this._implicitContent.hidden = false;
                this._implicitContent.textContent = val;
                textnode = this._implicitContent.firstChild;
              } else {
                // XXX: How should we handle underlining if label contains
                // non-text nodes (see bug 370483)?
                var underline = true;
                for (var node = this._labelControl.firstChild; node; node = node.nextSibling) {
                  if (node.nodeType == Node.ELEMENT_NODE) {
                    underline = false;
                    break;
                  }
                }
                if (!underline) {
                  this._explicitContent.hidden = false;
                  this._implicitContent.hidden = true;
                  this._implicitContent.textContent = '';
                  return;
                }

                this._explicitContent.hidden = true;
                this._implicitContent.hidden = false;
                this._implicitContent.textContent = this._labelControl.textContent;
                textnode = this._implicitContent.firstChild;
              }

              var accesskey = this._labelControl.parentNode.getAttribute("accesskey");
              if (accesskey.length == 1 && textnode)
                this._labelControl.formatWithAccesskey(accesskey, textnode);
            }
          };
        ]]>
        </body>
      </method>
    </implementation>
  </binding>


  <!-- TRIGGER: base widget for triggers -->
  <binding id="xformswidget-trigger-base"
           extends="chrome://xforms/content/xforms.xml#xformswidget-trigger-base">

    <handlers>
      <handler event="focus" phase="capturing">
        if (event.originalTarget == this.control) {
          this.dispatchDOMUIEvent("DOMFocusIn");
        }
      </handler>

      <handler event="blur" phase="capturing">
        if (event.originalTarget == this.control) {
          this.dispatchDOMUIEvent("DOMFocusOut");
        }
      </handler>
    </handlers>

  </binding>


  <!-- TRIGGER: <DEFAULT> -->
  <binding id="xformswidget-trigger"
           extends="#xformswidget-trigger-base">
    <content>
      <xul:button anonid="control" flex="1"
                  xbl:inherits="tabindex">
        <children/>
      </xul:button>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'control'),

            activate: function triggerControlElmActivate() {
              this.focus();
              this.click();
            }
          };
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="command">
        // XXX: we need to fire 'DOMActivate' event to get xforms:submit to
        // work, since xul:button do not do it (see a bug 323005
        // https://bugzilla.mozilla.org/show_bug.cgi?id=323005).
        this.dispatchDOMUIEvent("DOMActivate");
      </handler>
    </handlers>
  </binding>


  <!-- TRIGGER: MINIMAL -->
  <binding id="xformswidget-trigger-minimal"
           extends="#xformswidget-trigger-base">
    <content>
      <xul:box tabindex="0" anonid="control" flex="1"
               xbl:inherits="tabindex, orient">
        <children/>
      </xul:box>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            __proto__: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'control'),
            trigger: this,

            set disabled(val) {
              this.isDisabled = val;
            },
            activate: function triggerControlElmActivate(aNoFocus) {
              if (!this.isDisabled) {
                if (!aNoFocus)
                  this.trigger.focus();
                this.trigger.dispatchDOMUIEvent("DOMActivate");
              }
            },

            isDisabled: false
          }
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="click" action="this.control.activate(true)"/>
    </handlers>
  </binding>


  <!-- SWITCH -->
  <binding id="xformswidget-switch"
           extends="chrome://xforms/content/xforms.xml#xformswidget-accessors">
    <content>
      <xul:deck flex="1" anonid="deck">
        <children/>
      </xul:deck>
    </content>

    <implementation>
      <property name="selectedCase"
        onget="return this.deck.selectedPanel;"
        onset="this.deck.selectedPanel = val;"/>

      <property name="deck">
        <getter>
          if (!this._deck) {
            this._deck = this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "deck");
          }
          return this._deck;
        </getter>
      </property>
      <field name="_deck">null</field>
    </implementation>
  </binding>


  <!-- CASE -->
  <binding id="xformswidget-case"
           extends="chrome://xforms/content/xforms.xml#xformswidget-case-base">
    <content>
      <xul:box flex="1">
        <children/>
      </xul:box>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
        <![CDATA[
          var switchElm = this.parentNode;
          while (switchElm && switchElm.namespaceURI != this.XFORMS_NS &&
                 switchElm.nodeName != "switch") {
            switchElm = switchElm.parentNode;
          }

          return {
            caseElm: this,
            switchElm: switchElm,

            set selected(val) {
              this.switchElm.selectedCase = this.caseElm;
            }
          };
        ]]>
        </body>
      </method>
    </implementation>
  </binding>


  <!-- ALERT: <DEFAULT> -->
  <binding id="xformswidget-alert"
           extends="chrome://xforms/content/xforms.xml#xformswidget-alert-base">

    <content role="alert">
      <xul:deck anonid="contentswitcher" flex="1" selectedIndex="1">
        <xul:label anonid="implicitcontent" xbl:inherits="orient"/>
        <xul:label xbl:inherits="orient"><children/></xul:label>
      </xul:deck>
    </content>

    <implementation>
      <method name="getControlElement">
        <body>
          return {
            _contentSwitcher: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'contentswitcher'),
            _implicitContent: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'implicitcontent'),

            setValue: function setValue(aUseInlineValue, aValue) {
              if (aUseInlineValue) {
                this._contentSwitcher.selectedIndex = 1;
              } else {
                this._implicitContent.textContent = aValue;
                this._contentSwitcher.selectedIndex = 0;
              }
            }
          };
        </body>
      </method>
    </implementation>
  </binding>


  <!-- MESSAGE: EPHEMERAL, HINT: <DEFAULT> -->
  <binding id="xformswidget-ephemeral-message"
           extends="#xformswidget-alert">

    <content>
      <xul:tooltip anonid="popup">
        <xul:deck anonid="contentswitcher" flex="1" selectedIndex="1">
          <xul:label anonid="implicitcontent" xbl:inherits="orient"/>
          <xul:label xbl:inherits="orient"><children/></xul:label>
        </xul:deck>
      </xul:tooltip>
    </content>

    <implementation implements="nsIXFormsEphemeralMessageUI">
      <method name="show">
        <parameter name="aPosX"/>
        <parameter name="aPosY"/>
        <body>
          // Since the method gets coordinates relative document but tooltip can
          // be shown relative screen then we should do the following magic.
          // We use passed coordinates to save the logic of xtf part of
          // message. Also we substract 21 from y coordinate to revoke tooltip
          // logic.

          var boxObject = this.ownerDocument.getBoxObjectFor(this.parentNode);
          var deltaX = aPosX - boxObject.x;
          var deltaY = aPosY - boxObject.y;

          aPosX = boxObject.screenX + deltaX;
          aPosY = boxObject.screenY + deltaY - 21;

          this.popup.showPopup(this.ownerDocument.documentElement,
                               aPosX, aPosY, "tooltip", null, null);
        </body>
      </method>

      <method name="hide">
        <body>
          this.popup.hidePopup();
        </body>
      </method>

    <!-- internal -->
      <property name="popup" readonly="true">
        <getter>
          if (!this._popup) {
            this._popup = this.ownerDocument.
              getAnonymousElementByAttribute(this, "anonid", "popup");
          }
          return this._popup;
        </getter>
      </property>
      <field name="_popup"/>
    </implementation>
  </binding>


  <!-- UPLOAD: <DEFAULT> -->
  <binding id="xformswidget-upload"
           extends="chrome://xforms/content/xforms.xml#xformswidget-upload-base">
    <content>
      <children includes="label"/>
      <xul:textbox anonid="text_control"
                   class="xf-value"
                   readonly="readonly"
                   tabindex="-1"/>
      <xul:button anonid="browse_button"
                   xbl:inherits="tabindex"
                   label="&xforms.upload.browsetext;"/>
      <xul:button anonid="clear_button"
                   xbl:inherits="tabindex"
                   label="&xforms.upload.cleartext;"/>
      <children/>
    </content>

    <implementation implements="nsIXFormsUIWidget, nsIXFormsUploadUIElement">
      <method name="getControlElement">
        <body>
          return {
            get value() {
              return this._textControl.value;
            },
            set value(val) {
              this._textControl.value = val;
            },
            set readonly(val) {
              if (val) {
                this._browseButton.setAttribute('disabled', 'disabled');
                this._clearButton.setAttribute('disabled', 'disabled');
              } else {
                this._browseButton.removeAttribute('disabled');
                this._clearButton.removeAttribute('disabled');
              }
            },
            focus: function() {
              this._browseButton.focus();
            },

            _browseButton: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'browse_button'),
            _clearButton: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'clear_button'),
            _textControl: this.ownerDocument.
              getAnonymousElementByAttribute(this, 'anonid', 'text_control'),
          };
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="command">
        switch (event.originalTarget.getAttribute("anonid")) {
          case "browse_button":
            this.uploadElement.pickFile();
            this.dispatchDOMUIEvent('DOMActivate');
            break;
          case "clear_button":
            this.uploadElement.clearFile();
            this.dispatchDOMUIEvent('DOMActivate');
            break;
        }
      </handler>
    </handlers>
  </binding>


  <!-- UPLOAD: DISABLED -->
  <binding id="xformswidget-upload-disabled"
           extends="chrome://xforms/content/xforms.xml#xformswidget-upload-disabled-base">
    <content>
      <children includes="label"/>
      <xul:textbox anonid="text_control"
                   class="xf-value"
                   readonly="readonly"
                   tabindex="-1"/>
      <xul:button anonid="browse_button"
                   xbl:inherits="tabindex"
                   label="&xforms.upload.browsetext;"
                   disabled="true"/>
      <xul:button anonid="clear_button"
                   xbl:inherits="tabindex"
                   label="&xforms.upload.cleartext;"
                   disabled="true"/>
      <children/>
    </content>
  </binding>


  <!-- REPEAT -->
  <binding id="xformswidget-repeat"
           extends="chrome://xforms/content/xforms.xml#xformswidget-repeat-base">
    <content>
      <xul:box hidden="true">
        <children/>
      </xul:box>
      <xul:vbox anonid="insertion" flex="1"/>
    </content>
  </binding>

</bindings>
