<?xml version="1.0"?>

<!DOCTYPE window [
  <!ENTITY % textcontextDTD SYSTEM "chrome://global/locale/textcontext.dtd" >
  %textcontextDTD;
]>

<bindings id="textboxBindings"
   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 id="textbox" extends="xul:box">
    <resources>
      <stylesheet src="chrome://global/skin/textbox.css"/>
    </resources>
    
    <content context="_child">
      <xul:hbox class="textbox-internal-box" flex="1">
        <html:input class="textbox-input" flex="1" inherits="onfocus,onblur,value,type,maxlength,disabled,size,readonly"/>
      </xul:hbox>
        <xul:menupopup onpopupshowing="this.parentNode.focus(); this.parentNode.doPopupItemEnabling(this);">
          <xul:menuitem label="&undoCmd.label;" accesskey="&undoCmd.accesskey;" cmd="cmd_undo" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_undo');              
            controller.doCommand('cmd_undo');"/>
          <xul:menuseparator/>
          <xul:menuitem label="&cutCmd.label;" accesskey="&cutCmd.accesskey;" cmd="cmd_cut" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_cut');
            controller.doCommand('cmd_cut');"/>
          <xul:menuitem label="&copyCmd.label;" accesskey="&copyCmd.accesskey;" cmd="cmd_copy" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_copy');
            controller.doCommand('cmd_copy');"/>
          <xul:menuitem label="&pasteCmd.label;" accesskey="&pasteCmd.accesskey;" cmd="cmd_paste" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_paste');
            controller.doCommand('cmd_paste');"/>
          <xul:menuitem label="&deleteCmd.label;" accesskey="&deleteCmd.accesskey;" cmd="cmd_delete" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_delete');
            controller.doCommand('cmd_delete');"/>
           <xul:menuseparator/>
          <xul:menuitem label="&selectAllCmd.label;" accesskey="&selectAllCmd.accesskey;" cmd="cmd_selectAll" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_selectAll');
            controller.doCommand('cmd_selectAll');"/>
        </xul:menupopup>
    </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;"/>
      <property name="type"       onset="this.inputField.type = val; return val;"
                                  onget="return this.inputField.type;"/>
      <property name="maxlength"  onset="this.inputField.maxlength = val; return val;"
                                  onget="return this.inputField.maxlength;"/>
      <property name="disabled"   onset="this.inputField.disabled = val;
                                         if (val) this.setAttribute('disabled', 'true');
                                         else this.removeAttribute('disabled'); return val;"
                                  onget="return this.inputField.disabled;"/>
      <property name="size"       onset="this.inputField.size = val; return val;"
                                  onget="return this.inputField.size;"/>
      <property name="readonly"   onset="this.inputField.readonly = val;
                                         if (val) this.setAttribute('disabled', 'true');
                                         else this.removeAttribute('disabled'); return val;"
                                  onget="return this.inputField.readonly;"/>
                                  

      <method name="select">
        <body>
          this.inputField.select();
        </body>
      </method>

      <method name="doPopupItemEnabling">
        <parameter name="popupNode"/> 
        <body>
          <![CDATA[
            var children = popupNode.childNodes;
            for (var i = 0; i < children.length; i++) {              
              var command = children[i].getAttribute("cmd");
              if (command) {
                var controller = document.commandDispatcher.getControllerForCommand(command);
                var enabled = controller.isCommandEnabled(command);
                if (enabled)
                  children[i].removeAttribute('disabled');
                else
                  children[i].setAttribute('disabled','true');               
              }
            }
          ]]>
        </body>
      </method>
                             
      
      <property name="controllers"    readonly="true" onget="return this.inputField.controllers"/>
      <property name="textLength"     readonly="true" onget="return this.inputField.textLength;"/>
      <property name="selectionStart" onset="this.inputField.selectionStart = val; return val;"
                                      onget="return this.inputField.selectionStart;"/>
      <property name="selectionEnd"   onset="this.inputField.selectionEnd = val; return val;"
                                      onget="return this.inputField.selectionEnd;"/>
     
      <property name="suppressFocusBlur">
        false
      </property>

      <method name="setSelectionRange">
        <parameter name="aSelectionStart"/>
        <parameter name="aSelectionEnd"/>
        <body>
          this.inputField.setSelectionRange( aSelectionStart, aSelectionEnd );
        </body>
      </method>

      <constructor action="var str = this.boxObject.getProperty('value'); 
                                                if (str) {
                                                  this.inputField.value=str;
                                                  this.boxObject.removeProperty('value');
                                                }"/>
      <destructor action="if (this.inputField.value) this.boxObject.setProperty('value', this.inputField.value);"/>
   
    </implementation>
    
    <handlers>
      <handler event="focus" phase="capturing">
      <![CDATA[
        if (this.getAttribute('focused') != 'true') {
          this.setAttribute('focused','true');
          this.suppressFocusBlur = true;
          if (document.commandDispatcher.focusedElement != this.inputField) 
            this.inputField.focus();
          this.suppressFocusBlur = false;
        }
      ]]>
      </handler>
      
      <handler event="blur" phase="capturing">
      <![CDATA[
        if (!this.suppressFocusBlur && this.getAttribute('focused') == 'true') {
          this.removeAttribute('focused');
        }
      ]]>
      </handler>
    </handlers>    
  </binding>

  <binding id="textarea" extends="chrome://global/content/bindings/textbox.xml#textbox">
    <content context="_child">
      <xul:hbox class="textarea-internal-box" flex="1">
        <html:textarea class="textbox-textarea" flex="1" inherits="onfocus,onblur,value,disabled,rows,cols,readonly"/>
      </xul:hbox>
      <xul:popupset>
        <xul:popup onpopupshowing="this.parentNode.parentNode.doPopupItemEnabling(this); this.focus();">
          <xul:menuitem label="&undoCmd.label;" accesskey="&undoCmd.accesskey;" cmd="cmd_undo" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_undo');              
            controller.doCommand('cmd_undo');"/>
          <xul:menuseparator/>
          <xul:menuitem label="&cutCmd.label;" accesskey="&cutCmd.accesskey;" cmd="cmd_cut" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_cut');
            controller.doCommand('cmd_cut');"/>
          <xul:menuitem label="&copyCmd.label;" accesskey="&copyCmd.accesskey;" cmd="cmd_copy" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_copy');
            controller.doCommand('cmd_copy');"/>
          <xul:menuitem label="&pasteCmd.label;" accesskey="&pasteCmd.accesskey;" cmd="cmd_paste" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_paste');
            controller.doCommand('cmd_paste');"/>
          <xul:menuitem label="&deleteCmd.label;" accesskey="&deleteCmd.accesskey;" cmd="cmd_delete" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_delete');
            controller.doCommand('cmd_delete');"/>
           <xul:menuseparator/>
          <xul:menuitem label="&selectAllCmd.label;" accesskey="&selectAllCmd.accesskey;" cmd="cmd_selectAll" oncommand="
            var controller = document.commandDispatcher.getControllerForCommand('cmd_selectAll');
            controller.doCommand('cmd_selectAll');"/>
        </xul:popup>
      </xul:popupset>
    </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", "textarea");
            if (l.length > 0) {
              input = l[0];
              break;
            }
          } catch (e) {} 
        }
        input;
      ]]>
      </property>
    </implementation>
  </binding>

</bindings>