home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / toolkit.jar / content / global / autocomplete.xml < prev    next >
Encoding:
Extensible Markup Language  |  2001-09-30  |  58.8 KB  |  1,593 lines

  1. <?xml version="1.0"?>
  2.  
  3. <!DOCTYPE window [
  4.   <!ENTITY % textcontextDTD SYSTEM "chrome://global/locale/textcontext.dtd" >
  5.   %textcontextDTD;
  6. ]>
  7.  
  8. <bindings id="autocompleteBindings"
  9.           xmlns="http://www.mozilla.org/xbl"
  10.           xmlns:html="http://www.w3.org/1999/xhtml"
  11.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  12.  
  13.   <binding id="autocomplete" display="xul:menu"
  14.            extends="chrome://global/content/bindings/textbox.xml#textbox">
  15.     <resources>
  16.       <stylesheet src="chrome://global/content/autocomplete.css"/>
  17.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  18.     </resources>
  19.  
  20.     <content>
  21.       <xul:hbox class="autocomplete-internal-box textbox-internal-box" flex="1">
  22.         <xul:hbox class="autocomplete-icon-box" align="center" inherits="userAction">
  23.           <children includes="image">
  24.             <xul:image class="autocomplete-icon" allowevents="true"/>
  25.           </children>
  26.         </xul:hbox>
  27.  
  28.         <xul:hbox class="autocomplete-textbox-container" align="center" flex="1" allowevents="true">
  29.           <html:input anonid="textbox" class="autocomplete-textbox textbox-input" flex="1"
  30.                       inherits="onfocus,onblur,value,type,maxlength,disabled,size,readonly,userAction"/>
  31.         </xul:hbox>
  32.                             
  33.         <xul:hbox class="autocomplete-history-button" inherits="open,hidden=hideHistory">
  34.           <xul:image class="autocomplete-history-image"/>
  35.         </xul:hbox>
  36.       </xul:hbox>
  37.  
  38.       <xul:popupset anonid="popupset">
  39.         <xul:popup ignorekeys="true" anonid="popup" class="autocomplete-result-popup" inherits="for=id,nomatch"/>
  40.       </xul:popupset>
  41.  
  42.       <children includes="menupopup"/>
  43.     </content>
  44.  
  45.     <implementation>
  46.  
  47.       <constructor><![CDATA[
  48.         // set default property values
  49.         this.userAction = "none";
  50.         this.noMatch = true;
  51.         this.ifSetAttribute("timeout", 50);
  52.         this.ifSetAttribute("maxrows", 5);
  53.         this.ifSetAttribute("autoFill", false);
  54.         this.ifSetAttribute("autoFillAfterMatch", false);
  55.         this.ifSetAttribute("showPopup", true);
  56.         this.ifSetAttribute("minResultsForPopup", 0);
  57.         this.ifSetAttribute("flexPopup", true);
  58.         this.ifSetAttribute("alwaysOpenPopup", false);
  59.         this.ifSetAttribute("disableAutocomplete", false);
  60.         this.ifSetAttribute("forceComplete", false);
  61.         this.ifSetAttribute("tabScrolling", false);
  62.         this.ifSetAttribute("showCommentColumn", false);
  63.         
  64.         // initialize the search sessions
  65.         this.searchSessions = this.getAttribute("searchSessions");
  66.         
  67.         // assure that the history popup flexes as well as the results popup
  68.         this.setAttribute("sizetopopup", this.flexPopup);
  69.         
  70.         var mps = this.getElementsByTagName("menupopup");
  71.         if (mps.length == 0)
  72.           this.ifSetAttribute("hideHistory", true);
  73.         
  74.         // turn js handler attributes into functions
  75.         var attr = this.getAttribute("ontextcommand");
  76.         if (attr) this.ontextcommand = new Function("userAction", attr);
  77.         attr = this.getAttribute("onerrorcommand");
  78.         if (attr) this.onerrorcommand = new Function("errItem", attr);
  79.         attr = this.getAttribute("ontextrevert");
  80.         if (attr) this.ontextrevert = new Function(attr);
  81.         attr = this.getAttribute("oninit");
  82.         if (attr) this.oninit = new Function(attr);
  83.  
  84.         // hack to work around lack of bottom-up constructor calling
  85.         if ("initialize" in this.resultsPopup)
  86.           this.resultsPopup.initialize();
  87.         
  88.         this.fireInit();
  89.       ]]></constructor>
  90.  
  91.       <destructor><![CDATA[
  92.       ]]></destructor>
  93.       
  94.       <!-- =================== PUBLIC PROPERTIES =================== -->
  95.  
  96.       <property name="value"
  97.                 onset="this.ignoreInputEvent = true;
  98.                        this.mInputElt.value = val;
  99.                        this.ignoreInputEvent = false;
  100.                        return val;"
  101.                 onget="return this.mInputElt ? this.mInputElt.value : null;"/>
  102.   
  103.       <property name="focused"
  104.                 onget="return this.getAttribute('focused') == 'true';"/>
  105.  
  106.       <!-- space-delimited string of search session types to use -->
  107.       <property name="searchSessions" onget="return this.getAttribute('searchSessions')">
  108.         <setter><![CDATA[
  109.           val = val ? val : "";
  110.           var list = val.split(" ");
  111.           this.mSessions = {};
  112.           this.mListeners = {};
  113.           this.mLastResults = {};
  114.           this.mLastStatus = {};
  115.  
  116.           for (var i in list) {
  117.             var name = list[i];
  118.             if (name != "") {
  119.               var contractid = "@mozilla.org/autocompleteSession;1?type=" + name;
  120.               try {
  121.                 var session =
  122.                   Components.classes[contractid].getService(Components.interfaces.nsIAutoCompleteSession);
  123.               } catch (e) {
  124.                 dump("### ERROR - unable to create search session \"" + session + "\".\n");
  125.                 break;
  126.               }
  127.               this.mSessions[name] = session;
  128.               this.mListeners[name] = new (this.mAutoCompleteListener)(name);
  129.               this.mLastResults[name] = null;
  130.               this.mLastStatus[name] = null;
  131.               ++this.sessionCount;
  132.             }
  133.           }
  134.         ]]></setter>
  135.       </property>
  136.  
  137.       <!-- the number of sessions currently in use -->
  138.       <property name="sessionCount">0</property>
  139.  
  140.       <!-- number of milliseconds after a keystroke before a search begins -->
  141.       <property name="timeout"
  142.                 onset="return this.setAttribute('timeout', val);"
  143.                 onget="var t = parseInt(this.getAttribute('timeout')); return t ? t : 0;"/>
  144.  
  145.       <!-- number of milliseconds after a keystroke before a search begins -->
  146.       <property name="maxRows"
  147.                 onset="return this.setAttribute('maxrows', val);"
  148.                 onget="var t = parseInt(this.getAttribute('maxrows')); return t ? t : 0;"/>
  149.  
  150.       <!-- option for filling the textbox with the best match while typing 
  151.            and selecting the difference -->
  152.       <property name="autoFill"
  153.                 onset="return this.setAttribute('autoFill', val);"
  154.                 onget="return this.getAttribute('autoFill') == 'true';"/>
  155.  
  156.       <!-- if the resulting match string is not at the beginning of the typed string,
  157.            this will optionally autofill like this "bar |>> foobar|" -->
  158.       <property name="autoFillAfterMatch"
  159.                 onset="return this.setAttribute('autoFillAfterMatch', val);"
  160.                 onget="return this.getAttribute('autoFillAfterMatch') == 'true';"/>
  161.                 
  162.       <!-- toggles a second column in the results list which contains
  163.            the string in the comment field of each autocomplete result -->
  164.       <property name="showCommentColumn"
  165.                 onget=
  166.           "return this.getAttribute('showCommentColumn') == 'true';">
  167.         <setter><![CDATA[
  168.  
  169.           var currentState = this.getAttribute('showCommentColumn');          
  170.  
  171.           // if comment column has been switched from off to on
  172.           //
  173.           if (val && (currentState == 'false')) { 
  174.  
  175.             // reset the flex on the value column and add the comment column
  176.             //
  177.             document.getElementById("value").setAttribute("flex", 2);
  178.             this.resultsPopup.addColumn({id: "comment", flex: 1});
  179.  
  180.           // if comment column has been switched from on to off
  181.           //
  182.           } else if (!val && (currentState == 'true')) {
  183.  
  184.             // reset the flex on the value column and add the comment column
  185.             //
  186.             document.getElementById("value").setAttribute("flex", 1);
  187.             this.resultsPopup.removeColumn('comment');
  188.           }
  189.  
  190.           // save and return the current state
  191.           //
  192.           return this.setAttribute('showCommentColumn', val);
  193.         ]]></setter>
  194.       </property>
  195.  
  196.                 
  197.       <!-- option for completing to the default result whenever the user hits
  198.            enter or the textbox loses focus -->
  199.       <property name="forceComplete"
  200.                 onset="return this.setAttribute('forceComplete', val);"
  201.                 onget="return this.getAttribute('forceComplete') == 'true';"/>
  202.  
  203.       <!-- option to show the popup containing the results -->
  204.       <property name="showPopup"
  205.                 onset="return this.setAttribute('showPopup', val);"
  206.                 onget="return this.getAttribute('showPopup') == 'true';"/>
  207.  
  208.       <!-- option to keep the popup open while typing, even when there are no matches -->
  209.       <property name="alwaysOpenPopup"
  210.                 onset="return this.setAttribute('alwaysOpenPopup', val);"
  211.                 onget="return this.getAttribute('alwaysOpenPopup') == 'true';"/>
  212.  
  213.       <!-- option to size the popup to fit the width of the entire widget -->
  214.       <property name="flexPopup"
  215.                 onset="this.setAttribute('sizetopopup', val); return this.setAttribute('flexPopup', val);"
  216.                 onget="return this.getAttribute('flexPopup') == 'true';"/>
  217.  
  218.       <!-- option to allow scrolling through the list via the tab key, rather than
  219.            tab moving focus out of the textbox -->
  220.       <property name="tabScrolling"
  221.                 onset="return this.setAttribute('tabScrolling', val);"
  222.                 onget="return this.getAttribute('tabScrolling') == 'true';"/>
  223.  
  224.       <!-- option to turn off autocomplete -->
  225.       <property name="disableAutocomplete"
  226.                 onset="return this.setAttribute('disableAutocomplete', val);"
  227.                 onget="return this.getAttribute('disableAutocomplete') == 'true';"/>
  228.  
  229.       <!-- option to hide the history menubutton -->
  230.       <property name="hideHistory"
  231.                 onset="this.setAttribute('hideHistory', val);"
  232.                 onget="return this.getAttribute('hideHistory') == 'true';"/>
  233.       
  234.       <property name="minResultsForPopup"
  235.                 onset="return this.setAttribute('minResultsForPopup', val);"
  236.                 onget="var t = parseInt(this.getAttribute('minResultsForPopup')); return t ? t : 0;"/>
  237.       
  238.       <!-- state which indicates the current action being performed by the user.
  239.            Possible values are : none, typing, scrolling -->
  240.       <property name="userAction"
  241.                 onset="return this.setAttribute('userAction', val);"
  242.                 onget="return this.getAttribute('userAction');"/>
  243.       
  244.       <!-- state which indicates if the last search had no matches -->
  245.       <property name="noMatch">true</property>
  246.  
  247.       <!-- state which indicates a search is currently happening -->
  248.       <property name="isSearching">false</property>
  249.  
  250.       <!-- state which indicates a search timeout is current waiting -->
  251.       <property name="isWaiting" 
  252.                 onget="return this.mAutoCompleteTimer != 0;"/>
  253.  
  254.       <!-- reference to the results popup element -->
  255.       <property name="resultsPopup"><![CDATA[
  256.         var elt = document.getAnonymousElementByAttribute(this, "anonid", "popup");
  257.         elt.__AUTOCOMPLETE_BOX__ = this;
  258.         elt;
  259.       ]]></property>
  260.  
  261.       <!-- =================== PRIVATE PROPERTIES =================== -->
  262.  
  263.       <property name="mSessions">null</property>
  264.       <property name="mListeners">null</property>
  265.       <property name="mLastResults">null</property>
  266.       <property name="mLastStatus">null</property>
  267.       <property name="mLastKeyCode">null</property>
  268.       <property name="mAutoCompleteTimer">0</property>
  269.       <property name="mMenuOpen">false</property>
  270.       <property name="mFireAfterSearch">false</property>
  271.       <property name="mFinishAfterSearch">false</property>
  272.       <property name="mNeedToFinish">false</property>
  273.       <property name="mNeedToComplete">false</property>
  274.       <property name="mTransientValue">false</property>
  275.       <property name="mView">null</property>
  276.       <property name="currentSearchString">null</property>
  277.       <property name="ignoreInputEvent">false</property>
  278.       <property name="oninit">null</property>
  279.       <property name="ontextcommand">null</property>
  280.       <property name="ontextrevert">null</property>
  281.       <property name="onerrorcommand">null</property>
  282.       <field name="mDefaultMatchFilled">false</field>
  283.  
  284.       <property name="mAutoCompleteListener"><![CDATA[
  285.         var listener = function(aSession) { this.sessionName = aSession };
  286.         listener.prototype = {
  287.           param: this,
  288.           sessionName: null,
  289.           onAutoComplete: function(aResults, aStatus) 
  290.           {
  291.             this.param.processResults(this.sessionName, aResults, aStatus);
  292.           }
  293.         };
  294.         listener;
  295.       ]]></property>
  296.  
  297.       <property name="mPopupSetElt"><![CDATA[
  298.         var elt = document.getAnonymousElementByAttribute(this, "anonid", "popupset");
  299.         elt.__AUTOCOMPLETE_BOX__ = this;
  300.         elt;
  301.       ]]></property>
  302.  
  303.       <property name="mInputElt"><![CDATA[
  304.         document.getAnonymousElementByAttribute(this, "anonid", "textbox");
  305.       ]]></property>
  306.  
  307.       <!-- =================== PUBLIC METHODS =================== -->
  308.  
  309.       <!-- get the result object from the autocomplete results from a specific session -->
  310.       <method name="getResultAt">
  311.         <parameter name="aIndex"/>
  312.         <body><![CDATA[
  313.           var obj = this.convertIndexToSession(aIndex);
  314.           if (obj && this.mLastResults[obj.session]) {
  315.             var nsIAutoCompleteItem = Components.interfaces.nsIAutoCompleteItem;
  316.             if (obj.index >= 0) {
  317.               var item = this.mLastResults[obj.session].items.QueryElementAt(obj.index, nsIAutoCompleteItem);
  318.               return item;
  319.             }
  320.           }
  321.           return null;
  322.         ]]></body>
  323.       </method>
  324.  
  325.       <!-- get the autocomplete session status returned by the session
  326.            that a given item came from -->
  327.       <method name="getSessionStatusAt">
  328.         <parameter name="aIndex"/>
  329.         <body><![CDATA[
  330.           var obj = this.convertIndexToSession(aIndex);
  331.           return obj ? this.mLastStatus[obj.session] : null;
  332.         ]]></body>
  333.       </method>
  334.  
  335.  
  336.       <!-- get a value from the autocomplete results as a string via an absolute index-->
  337.       <method name="getResultValueAt">
  338.         <parameter name="aIndex"/>
  339.         <body><![CDATA[
  340.           var obj = this.convertIndexToSession(aIndex);
  341.           return obj ? this.getSessionValueAt(obj.session, obj.index) : null;
  342.         ]]></body>
  343.       </method>
  344.  
  345.       <!-- get the result object from the autocomplete results from a specific session -->
  346.       <method name="getSessionResultAt">
  347.         <parameter name="aSession"/>
  348.         <parameter name="aIndex"/>
  349.         <body><![CDATA[
  350.           var session = this.mLastResults[aSession];
  351.           if (session) {
  352.             var item = session.items.QueryElementAt(aIndex, Components.interfaces.nsIAutoCompleteItem);
  353.             return item;
  354.           }
  355.           return null;
  356.         ]]></body>
  357.       </method>
  358.  
  359.       <!-- get a value from the autocomplete results as a string from a specific session -->
  360.       <method name="getSessionValueAt">
  361.         <parameter name="aSession"/>
  362.         <parameter name="aIndex"/>
  363.         <body><![CDATA[
  364.           var result = this.getSessionResultAt(aSession, aIndex);
  365.           if (result)
  366.             return result.value;
  367.           return null;
  368.         ]]></body>
  369.       </method>
  370.  
  371.       <!-- get the total number of results in a specific session or overall if session is null-->
  372.       <method name="getResultCount">
  373.         <parameter name="aSession"/>
  374.         <body><![CDATA[
  375.           return this.view.rowCount;
  376.         ]]></body>
  377.       </method>
  378.       
  379.       <!-- get a session object by index -->
  380.       <method name="getSession">
  381.         <parameter name="aIndex"/>
  382.         <body><![CDATA[
  383.           var idx = 0;
  384.           for (var name in this.mSessions) {
  385.             if (idx == aIndex)
  386.               return this.mSessions[name];
  387.             ++idx;
  388.           }
  389.           return null;
  390.         ]]></body>
  391.       </method>
  392.  
  393.       <!-- get a session object by name -->
  394.       <method name="getSessionByName">
  395.         <parameter name="aSessionName"/>
  396.         <body><![CDATA[
  397.           return this.mSessions[aSessionName];
  398.         ]]></body>
  399.       </method>
  400.  
  401.       <!-- add a session by reference -->
  402.       <method name="addSession">
  403.         <parameter name="aSession"/>
  404.         <body><![CDATA[
  405.           ++this.sessionCount;
  406.           var name = "anon_"+this.sessionCount;
  407.           this.mSessions[name] = aSession;
  408.           this.mListeners[name] = new (this.mAutoCompleteListener)(name);
  409.           this.mLastResults[name] = null;
  410.           this.mLastStatus[name] = null;
  411.         ]]></body>
  412.       </method>
  413.  
  414.       <!-- remove a session by reference -->
  415.       <method name="removeSession">
  416.         <parameter name="aSession"/>
  417.         <body><![CDATA[
  418.           for (var name in this.mSessions) {
  419.             if (this.mSessions[name] == aSession) {
  420.               delete this.mSessions[name];
  421.               delete this.mListeners[name];
  422.               delete this.mLastResults[name];
  423.               delete this.mLastStatus[name];
  424.               --this.sessionCount;
  425.               break;
  426.             }
  427.           }
  428.         ]]></body>
  429.       </method>
  430.  
  431.       <!-- make this widget listen to all of the same autocomplete sessions 
  432.            from another autocomplete widget -->
  433.       <method name="syncSessions">
  434.         <parameter name="aCopyFrom"/>
  435.         <body><![CDATA[
  436.           this.sessionCount = aCopyFrom.sessionCount;
  437.           this.mSessions = {};
  438.           this.mListeners = {};
  439.           this.mLastResults = {};
  440.           this.mLastStatus = {};
  441.           for (var name in aCopyFrom.mSessions) {
  442.             this.mSessions[name] = aCopyFrom.mSessions[name];
  443.             this.mListeners[name] = new (this.mAutoCompleteListener)(name);
  444.             this.mLastResults[name] = null;
  445.             this.mLastStatus[name] = null;
  446.           }
  447.         ]]></body>
  448.       </method>
  449.  
  450.       <!-- get the first session that has results -->
  451.       <method name="getDefaultSession">
  452.         <body><![CDATA[
  453.           for (var name in this.mLastResults) {
  454.             var results = this.mLastResults[name];
  455.             if (results && results.items.Count() > 0)
  456.               return name;
  457.           }
  458.           return null;
  459.         ]]></body>
  460.       </method>
  461.  
  462.       <!-- empty the cached result data and empty the results popup -->
  463.       <method name="clearResults">
  464.         <parameter name="aInvalidate"/>
  465.         <body><![CDATA[
  466.           this.clearResultData();
  467.           this.clearResultElements(aInvalidate);
  468.         ]]></body>
  469.       </method>
  470.  
  471.       <!-- =================== PRIVATE METHODS =================== -->
  472.   
  473.       <!-- ::::::::::::: session searching ::::::::::::: -->
  474.  
  475.       <!--  -->
  476.       <method name="callListener">
  477.         <parameter name="me"/>
  478.         <parameter name="aAction"/>
  479.         <body><![CDATA[
  480.           // bail if the binding was detached or the element removed from
  481.           // document during the timeout
  482.           if (!("startLookup" in me) || !me.ownerDocument || !me.parentNode)
  483.             return;
  484.  
  485.           me.clearTimer();
  486.             
  487.           if (me.disableAutocomplete)
  488.             return;
  489.  
  490.           switch (aAction) {
  491.             case "startLookup":
  492.               me.startLookup();
  493.               break;
  494.  
  495.             case "stopLookup":
  496.               me.stopLookup();
  497.               break;
  498.  
  499.             case "autoComplete":
  500.               me.autoComplete();
  501.               break;
  502.           }
  503.         ]]></body>
  504.       </method>
  505.  
  506.       <!--  -->
  507.       <method name="startLookup">
  508.         <body><![CDATA[
  509.           var str = this.value;
  510.           this.lastSearchString = this.currentSearchString;
  511.           this.currentSearchString = str;
  512.           
  513.           this.isSearching = true;
  514.           this.mSessionReturns = this.sessionCount;
  515.           this.mFailureCount = 0;
  516.           this.mFailureItems = 0;
  517.           this.mDefaultMatchFilled = false; // clear out our prefill state.
  518.  
  519.           // tell each session to start searching...
  520.           for (var name in this.mSessions)
  521.             this.mSessions[name].onStartLookup(str, this.mLastResults[name], this.mListeners[name]);
  522.         ]]></body>
  523.       </method>
  524.  
  525.       <!--  -->
  526.       <method name="stopLookup">
  527.         <body><![CDATA[
  528.           for (var name in this.mSessions)
  529.             this.mSessions[name].onStopLookup();
  530.         ]]></body>
  531.       </method>
  532.  
  533.       <!--  -->
  534.       <method name="autoComplete">
  535.         <body><![CDATA[
  536.           for (var name in this.mSessions)
  537.             this.mSessions[name].onAutoComplete(this.value, 
  538.                                                 this.mLastResults[name],
  539.                                                 this.mListeners[name]);
  540.         ]]></body>
  541.       </method>
  542.  
  543.       <!--  -->
  544.       <method name="processResults">
  545.         <parameter name="aSessionName"/>
  546.         <parameter name="aResults"/>
  547.         <parameter name="aStatus"/>
  548.         <body><![CDATA[
  549.           if (this.disableAutocomplete)
  550.             return;
  551.  
  552.           --this.mSessionReturns;
  553.  
  554.           var firstReturn = this.mSessionReturns == (this.sessionCount-1) - this.mFailureCount;
  555.           
  556.           if (firstReturn)
  557.             this.clearResults(false); // clear results, but don't repaint yet
  558.  
  559.           this.mLastStatus[aSessionName] = aStatus;
  560.  
  561.           // check the many criteria for failure
  562.           if (aStatus == Components.interfaces.nsIAutoCompleteStatus.failed ||
  563.               aStatus == Components.interfaces.nsIAutoCompleteStatus.ignored || 
  564.               aStatus == Components.interfaces.nsIAutoCompleteStatus.noMatch ||
  565.               aResults == null ||
  566.               aResults.items.Count() == 0 ||
  567.               aResults.searchString != this.currentSearchString)
  568.           {
  569.             this.mLastResults[aSessionName] = null;
  570.             this.searchFailed();
  571.             return;
  572.           } else if (aStatus == 
  573.                      Components.interfaces.nsIAutoCompleteStatus.failureItems){
  574.             ++this.mFailureItems;
  575.           }
  576.           
  577.           this.mLastResults[aSessionName] = aResults;
  578.  
  579.           this.autoFillInput(aSessionName, aResults, false);
  580.          
  581.           // always call openResultPopup...we may not have opened it
  582.           // if a previous search session didn't return enough search results.
  583.           // it's smart and doesn't try to open itself multiple times...
  584.           // be sure to add our result elements before calling openResultPopuup as we need
  585.           // to know the total # of results found so far.
  586.           this.addResultElements(aSessionName, aResults);      
  587.           this.openResultPopup();
  588.          
  589.           // if this is the last session to return... 
  590.           if (this.mSessionReturns == 0) 
  591.             this.postSearchCleanup();
  592.           
  593.         ]]></body>
  594.       </method>
  595.  
  596.       <!-- called each time a search fails, except when failure items need
  597.            to be displayed. If all searches have failed, clear the list
  598.            and close the popup -->
  599.       <method name="searchFailed">
  600.         <body><![CDATA[
  601.           // if it's the last session to return, time to clean up...
  602.           if (this.mSessionReturns == 0)
  603.             this.postSearchCleanup();
  604.  
  605.           ++this.mFailureCount;
  606.           
  607.           // if all searches are done and they all failed...
  608.           if (this.mSessionReturns == 0 && this.mFailureCount == this.sessionCount) {
  609.             if (this.alwaysOpenPopup) {
  610.               this.clearResults(true); // clear data and repaint empty
  611.               
  612.               if (this.value) {
  613.                 this.openResultPopup();
  614.               } else
  615.                 this.closeResultPopup(); 
  616.             } else
  617.               this.closeResultPopup(); 
  618.           }
  619.         ]]></body>
  620.       </method>
  621.  
  622.       <!-- does some stuff after a search is done (success or failure) -->
  623.       <method name="postSearchCleanup">
  624.         <body><![CDATA[
  625.           this.isSearching = false;
  626.  
  627.           // figure out if there are no matches in all search sessions
  628.           var failed = true;
  629.           for (var name in this.mSessions) {
  630.             if (this.mLastResults[name])
  631.               failed = this.mLastResults[name].items.Count() < 1;
  632.             if (!failed)
  633.               break;
  634.           }
  635.           this.noMatch = failed;
  636.           this.setAttribute("nomatch", this.noMatch);
  637.           
  638.           // if we have processed all of our searches, and none of them gave us a default index,
  639.           // then we should try to auto fill the input field with the first match. 
  640.           // note: autoFillInput is smart enough to kick out if we've already prefilled something...
  641.           if (!this.noMatch) {
  642.              var defaultSession = this.getDefaultSession();
  643.              if (defaultSession)
  644.                 this.autoFillInput(defaultSession, this.mLastResults[defaultSession], true);         
  645.           }
  646.           
  647.           if (this.mFinishAfterSearch)
  648.             this.finishAutoComplete(false, this.mFireAfterSearch);
  649.         ]]></body>
  650.       </method>
  651.  
  652.       <!-- when the focus exits the widget or user hits return, 
  653.            determine what value to leave in the textbox -->
  654.       <method name="finishAutoComplete">
  655.         <parameter name="aForceComplete"/>
  656.         <parameter name="aFireTextCommand"/>
  657.         <body><![CDATA[
  658.           this.mFinishAfterSearch = false;
  659.           this.mFireAfterSearch = false;
  660.           if (this.mNeedToFinish && !this.disableAutocomplete) {
  661.             // if a search is happening at this juncture, bail out of this function
  662.             // and let the search finish, and tell it to come back here when it's done
  663.             if (this.isSearching || this.isWaiting) {
  664.               this.mFinishAfterSearch = true;
  665.               this.mFireAfterSearch = aFireTextCommand;
  666.               return;
  667.             }
  668.             
  669.             this.mNeedToFinish = false;
  670.  
  671.             // set textbox value to either override value, or default search result 
  672.             var val = this.resultsPopup.getOverrideValue();
  673.             if (val) {
  674.               this.value = val;
  675.             } else if (this.mTransientValue) {
  676.               // do nothing
  677.             } else if (this.forceComplete && (this.mNeedToComplete || aForceComplete)) {
  678.               var defaultSession = this.getDefaultSession();
  679.  
  680.               // we want to use the default item index for the first session which gave us a valid
  681.               // default item index...
  682.  
  683.               for (var name in this.mLastResults) {
  684.                var results = this.mLastResults[name];
  685.                if (results && results.items.Count() > 0 && results.defaultItemIndex != -1)
  686.                {
  687.                  defaultSession = name;
  688.                  break;
  689.                }
  690.               }
  691.  
  692.               if (defaultSession) {
  693.                 var results = this.mLastResults[defaultSession];
  694.                 if (results && !this.noMatch)
  695.                   if (results.defaultItemIndex != -1)
  696.                   this.value = this.getSessionValueAt(defaultSession, results.defaultItemIndex);
  697.                   else
  698.                     this.value = this.getSessionValueAt(defaultSession, 0); // preselect the first one...
  699.               }
  700.             }
  701.  
  702.             this.closeResultPopup();
  703.           }
  704.           
  705.           this.mNeedToComplete = false;
  706.           this.currentSearchString = "";
  707.           this.clearTimer();
  708.  
  709.           if (aFireTextCommand)
  710.             this.fireTextCommand(this.userAction);
  711.         ]]></body>
  712.       </method>
  713.  
  714.       <!--  when the user clicks an entry in the autocomplete popup -->
  715.       <method name="onResultClick">
  716.         <body><![CDATA[
  717.          // set textbox value to either override value, or the clicked result
  718.           var errItem=null;
  719.           var val = this.resultsPopup.getOverrideValue();
  720.             if (val)
  721.               this.value = val;
  722.           else if (this.resultsPopup.selectedIndex != null &&
  723.                    !this.noMatch) {
  724.             if (this.getSessionStatusAt(this.resultsPopup.selectedIndex) ==
  725.                 Components.interfaces.nsIAutoCompleteStatus.failureItems) {
  726.               this.value = this.currentSearchString;
  727.               this.mTransientValue = true;
  728.               errItem = this.getResultAt(this.resultsPopup.selectedIndex);
  729.             } else { 
  730.               this.value = this.getResultValueAt(
  731.                                              this.resultsPopup.selectedIndex);
  732.             }
  733.           }
  734.  
  735.           this.mNeedToFinish = false;
  736.           this.mNeedToComplete = false;
  737.           
  738.           this.closeResultPopup();
  739.  
  740.           this.currentSearchString = "";
  741.  
  742.           if (errItem) {
  743.             this.fireErrorCommand(errItem);
  744.           }
  745.           this.fireTextCommand("clicking");
  746.         ]]></body>
  747.       </method>
  748.  
  749.       <!-- when the user hits escape, revert the previously typed value in the textbox -->
  750.       <method name="undoAutoComplete">
  751.         <body><![CDATA[
  752.           var val = this.currentSearchString;
  753.  
  754.           var ok = this.fireTextRevert();
  755.           if ((ok || ok == undefined) && val)
  756.             this.value = val;
  757.  
  758.           this.userAction = "typing";
  759.  
  760.           this.currentSearchString = "";
  761.           this.mNeedToComplete = false;
  762.         ]]></body>
  763.       </method>
  764.  
  765.       <!-- convert an absolute result index into a session name/index pair -->
  766.       <method name="convertIndexToSession">
  767.         <parameter name="aIndex"/>
  768.         <body><![CDATA[
  769.           var idx = 0;
  770.           for (var name in this.mLastResults) {
  771.             if (this.mLastResults[name]) {
  772.               if ((idx+this.mLastResults[name].items.Count())-1 >= aIndex) {
  773.                 return {session: name, index: aIndex-idx};
  774.               }
  775.               idx += this.mLastResults[name].items.Count();
  776.             }
  777.           }
  778.           return null;
  779.         ]]></body>
  780.       </method>
  781.  
  782.       <!-- ::::::::::::: user input handling ::::::::::::: -->
  783.  
  784.       <!--  -->
  785.       <method name="processInput">
  786.         <body><![CDATA[
  787.           // stop current lookup in case it's async.
  788.           this.stopLookup();
  789.           // stop the queued up lookup on a timer
  790.           this.clearTimer();
  791.  
  792.           if (this.ignoreInputEvent)
  793.             return;
  794.           
  795.           if (this.disableAutocomplete)
  796.             return;
  797.  
  798.           this.userAction = "typing";
  799.           this.mNeedToFinish = true;
  800.           this.mTransientValue = false;
  801.           this.mNeedToComplete = true;
  802.  
  803.           this.resultsPopup.selectedIndex = null;
  804.           
  805.           // We want to autocomplete only if the user is editing at the end of the text
  806.           if (this.mInputElt.selectionEnd >= this.value.length)
  807.             this.mAutoCompleteTimer = setTimeout(this.callListener, this.timeout, this, "startLookup");
  808.           else
  809.             this.noMatch = true;
  810.         ]]></body>
  811.       </method>
  812.  
  813.       <!--  -->
  814.       <method name="processKeyPress">
  815.         <parameter name="aEvent"/>
  816.         <body><![CDATA[
  817.           this.mLastKeyCode = aEvent.keyCode;
  818.           
  819.           var killEvent = false;
  820.           
  821.           switch (aEvent.keyCode) {
  822.             case KeyEvent.DOM_VK_TAB:
  823.               if (this.tabScrolling) {
  824.                 // don't kill this event if alt-tab or ctrl-tab is hit
  825.                 if (!aEvent.altKey && !aEvent.ctrlKey) {
  826.                   killEvent = this.mMenuOpen;
  827.                   if (killEvent)
  828.                     this.keyNavigation(aEvent);
  829.                 }
  830.               } 
  831.               break;              
  832.               
  833.             case KeyEvent.DOM_VK_RETURN:
  834.  
  835.               // if this is a failure item, save it for fireErrorCommand
  836.               var errItem = null;
  837.               if (this.resultsPopup.selectedIndex != null && 
  838.                   this.getSessionStatusAt(this.resultsPopup.selectedIndex) ==
  839.                   Components.interfaces.nsIAutoCompleteStatus.failureItems) {
  840.                 errItem = this.getResultAt(this.resultsPopup.selectedIndex);
  841.               }
  842.  
  843.               killEvent = this.mMenuOpen;
  844.               this.finishAutoComplete(true, true);
  845.               this.closeResultPopup();
  846.               if (errItem) {
  847.                   this.fireErrorCommand(errItem);
  848.               }
  849.               break;
  850.  
  851.             case KeyEvent.DOM_VK_ESCAPE:
  852.               this.clearTimer();
  853.               killEvent = this.mMenuOpen;
  854.               this.undoAutoComplete();
  855.               this.closeResultPopup();
  856.               break;
  857.   
  858.             case KeyEvent.DOM_VK_LEFT:
  859.             case KeyEvent.DOM_VK_RIGHT:
  860.               this.clearTimer();
  861.               this.closeResultPopup();
  862.               break;
  863.  
  864.             case KeyEvent.DOM_VK_PAGE_UP:
  865.             case KeyEvent.DOM_VK_PAGE_DOWN:
  866.             case KeyEvent.DOM_VK_UP:
  867.             case KeyEvent.DOM_VK_DOWN:
  868.               this.clearTimer();
  869.               killEvent = true;
  870.               this.keyNavigation(aEvent);
  871.               break;
  872.           }
  873.           
  874.           if (killEvent) {
  875.             aEvent.preventDefault();
  876.             aEvent.preventBubble();
  877.           }
  878.           
  879.           return true;
  880.         ]]></body>
  881.       </method>
  882.  
  883.       <!--  -->
  884.       <method name="keyNavigation">
  885.         <parameter name="aEvent"/>
  886.         <body><![CDATA[
  887.           var k = aEvent.keyCode;
  888.           if (k == KeyEvent.DOM_VK_TAB ||
  889.               k == KeyEvent.DOM_VK_UP || k == KeyEvent.DOM_VK_DOWN ||
  890.               k == KeyEvent.DOM_VK_PAGE_UP || k == KeyEvent.DOM_VK_PAGE_DOWN)
  891.           {
  892.             // up/down keys while menu is closed will open menu
  893.             if (!this.mMenuOpen && (this.view.rowCount > 0 || this.alwaysOpenPopup)) {
  894.               this.openResultPopup();
  895.               return;
  896.             }
  897.             
  898.             this.userAction = "scrolling";
  899.             this.mNeedToComplete = false;
  900.             
  901.             var dir = k == KeyEvent.DOM_VK_DOWN ||
  902.                       k == KeyEvent.DOM_VK_PAGE_DOWN ||
  903.                      (k == KeyEvent.DOM_VK_TAB && !aEvent.shiftKey) ? 1 : -1;
  904.             var amt = k == KeyEvent.DOM_VK_PAGE_UP ||
  905.                       k == KeyEvent.DOM_VK_PAGE_DOWN ? this.resultsPopup.pageCount-1 : 1;
  906.             var selected = this.resultsPopup.selectBy(dir, amt);
  907.           
  908.             // determine which value to place in the textbox
  909.             this.ignoreInputEvent = true;
  910.             if (selected != null) {
  911.               if (this.getSessionStatusAt(selected) == 
  912.                   Components.interfaces.nsIAutoCompleteStatus.failureItems) { 
  913.                 this.value = this.currentSearchString;
  914.               } else {
  915.                 this.value = this.getResultValueAt(selected);
  916.               }
  917.               this.mTransientValue = true;
  918.             } else {
  919.               this.value = this.currentSearchString;
  920.               this.mTransientValue = false;
  921.             }
  922.             
  923.             // move cursor to the end
  924.             this.mInputElt.setSelectionRange(this.value.length, this.value.length);
  925.             this.ignoreInputEvent = false;
  926.           }
  927.         ]]></body>
  928.       </method>
  929.  
  930.       <!-- while the user is typing, fill the textbox with the "default" value
  931.            if one can be assumed, and select the end of the text -->
  932.       <method name="autoFillInput">
  933.         <parameter name="aSessionName"/>
  934.         <parameter name="aResults"/>
  935.         <parameter name="aUseFirstMatchIfNoDefault"/>
  936.         <body><![CDATA[
  937.           if (this.mDefaultMatchFilled) return;
  938.             
  939.           if (!this.mFinishAfterSearch && this.autoFill && 
  940.               this.mLastKeyCode != KeyEvent.DOM_VK_BACK_SPACE &&
  941.               this.mLastKeyCode != KeyEvent.DOM_VK_DELETE) {
  942.             var indexToUse = aResults.defaultItemIndex;
  943.             if (aUseFirstMatchIfNoDefault && indexToUse == -1)
  944.               indexToUse = 0;      
  945.               
  946.             if (indexToUse != -1) {
  947.               var resultValue = this.getSessionValueAt(aSessionName, indexToUse);
  948.               var match = resultValue.toLowerCase();
  949.               var entry = this.currentSearchString.toLowerCase();
  950.               this.ignoreInputEvent = true;
  951.               if (match.indexOf(entry) == 0) {
  952.                 var endPoint = this.value.length;
  953.                 this.value = this.value + resultValue.substr(endPoint);
  954.                 this.mInputElt.setSelectionRange(endPoint, this.value.length);
  955.               } else {
  956.                 if (this.autoFillAfterMatch) {
  957.                   this.value = this.value + " >> " + resultValue;
  958.                   this.mInputElt.setSelectionRange(entry.length, this.value.length);
  959.                 } else {
  960.                   var postIndex = resultValue.indexOf(this.value);
  961.                   if (postIndex >= 0) {
  962.                     var startPt = this.value.length;
  963.                     this.value = this.value + resultValue.substr(startPt+postIndex);
  964.                     this.mInputElt.setSelectionRange(startPt, this.value.length);
  965.                   }
  966.                 }
  967.               }
  968.               this.mNeedToComplete = true;
  969.               this.ignoreInputEvent = false;
  970.               this.mDefaultMatchFilled = true;
  971.             }
  972.           } 
  973.         ]]></body>
  974.       </method>
  975.  
  976.       <!-- ::::::::::::: popup and outliner ::::::::::::: -->
  977.  
  978.       <!--  -->
  979.       <method name="openResultPopup">
  980.         <body><![CDATA[
  981.           if (!this.mMenuOpen && this.showPopup && this.focused &&
  982.                (this.getResultCount("") >= this.minResultsForPopup
  983.                 || this.mFailureItems)) {
  984.             if (this.flexPopup) {
  985.               var w = this.boxObject.width;
  986.               if (w != this.resultsPopup.boxObject.width)
  987.                 this.resultsPopup.setAttribute("width", w);
  988.             }
  989.             
  990.             this.resultsPopup.openPopup(this, -1, -1, "popup", "bottomleft", "topleft");
  991.             this.mMenuOpen = true;
  992.           }
  993.         ]]></body>
  994.       </method>
  995.  
  996.       <!--  -->
  997.       <method name="closeResultPopup">
  998.         <body><![CDATA[
  999.           if (this.resultsPopup && this.mMenuOpen) {
  1000.             this.resultsPopup.closePopup();
  1001.             this.mMenuOpen = false;
  1002.           }
  1003.         ]]></body>
  1004.       </method>
  1005.  
  1006.       <!--  -->
  1007.       <method name="addResultElements">
  1008.         <parameter name="aSessionName"/>
  1009.         <parameter name="aResults"/>
  1010.         <body><![CDATA[
  1011.           if (this.focused) {
  1012.             this.view.addResults(aResults);
  1013.             this.resultsPopup.adjustHeight();
  1014.           }
  1015.         ]]></body>
  1016.       </method>
  1017.  
  1018.       <!--  -->
  1019.       <method name="clearResultElements">
  1020.         <parameter name="aInvalidate"/>
  1021.         <body><![CDATA[
  1022.           this.view.clearResults(aInvalidate);
  1023.           if (aInvalidate)
  1024.             this.resultsPopup.adjustHeight();
  1025.  
  1026.           this.noMatch = true;
  1027.         ]]></body>
  1028.       </method>
  1029.  
  1030.       <!--  -->
  1031.       <method name="clearResultData">
  1032.         <body><![CDATA[
  1033.           for (var name in this.mSessions) {
  1034.             this.mLastResults[name] = null;
  1035.             this.mLastStatus[name] = null;
  1036.           }
  1037.         ]]></body>
  1038.       </method>
  1039.  
  1040.       <!-- ::::::::::::: miscellaneous ::::::::::::: -->
  1041.  
  1042.       <!--  -->
  1043.       <method name="ifSetAttribute">
  1044.         <parameter name="aAttr"/>
  1045.         <parameter name="aVal"/>
  1046.         <body><![CDATA[
  1047.           if (this.getAttribute(aAttr) == "")
  1048.             this.setAttribute(aAttr, aVal);
  1049.         ]]></body>
  1050.       </method>
  1051.  
  1052.       <!--  -->
  1053.       <method name="clearTimer">
  1054.         <parameter name=""/>
  1055.         <body><![CDATA[
  1056.           if (this.mAutoCompleteTimer) {
  1057.             clearTimeout(this.mAutoCompleteTimer);
  1058.             this.mAutoCompleteTimer = 0;
  1059.           }
  1060.         ]]></body>
  1061.       </method>
  1062.  
  1063.       <!-- ::::::::::::: event dispatching ::::::::::::: -->
  1064.  
  1065.      <!-- execute the external init handler -->
  1066.       <method name="fireInit">
  1067.         <body><![CDATA[
  1068.           return this.oninit ? this.oninit() : null;
  1069.         ]]></body>
  1070.       </method>
  1071.  
  1072.       <!-- execute the external command handler -->
  1073.       <method name="fireTextCommand">
  1074.         <parameter name="aUserAction"/>
  1075.         <body><![CDATA[
  1076.           return this.ontextcommand ? this.ontextcommand(aUserAction) : null;
  1077.         ]]></body>
  1078.       </method>
  1079.  
  1080.       <!-- execute the external command handler -->
  1081.       <method name="fireErrorCommand">
  1082.         <parameter name="aErrorItem"/>
  1083.         <body><![CDATA[
  1084.           return this.onerrorcommand ? this.onerrorcommand(aErrorItem) : null;
  1085.         ]]></body>
  1086.       </method>
  1087.  
  1088.      <!-- execute the external command handler -->
  1089.       <method name="fireTextRevert">
  1090.         <body><![CDATA[
  1091.           return this.ontextrevert ? this.ontextrevert() : null;
  1092.         ]]></body>
  1093.       </method>
  1094.  
  1095.       <!-- =================== OUTLINER VIEW =================== -->
  1096.  
  1097.       <property name="view"><![CDATA[
  1098.         ({
  1099.           mTextbox: this,
  1100.           mOutliner: null,
  1101.           mBoxObject: null,
  1102.           mResults: [],
  1103.           mRowCount: 0,
  1104.           mSelectedIndex: null,
  1105.           
  1106.           get outlinerBoxObject()
  1107.           {
  1108.             return this.mOutliner;
  1109.           },
  1110.           
  1111.           set selectedIndex(aRow)
  1112.           {
  1113.             if (!this.mBoxObject)
  1114.               return;
  1115.               
  1116.             if (this.mSelectedIndex != null)
  1117.               this.mBoxObject.invalidateRow(this.mSelectedIndex);
  1118.               
  1119.             this.mSelectedIndex = aRow;
  1120.             
  1121.             this.mBoxObject.invalidateRow(aRow);
  1122.             
  1123.             if (aRow != null)
  1124.               this.mBoxObject.ensureRowIsVisible(aRow);
  1125.           },
  1126.           
  1127.           get selectedIndex()
  1128.           {
  1129.             return this.mSelectedIndex;
  1130.           },
  1131.           
  1132.           clearResults: function(aInvalidate)
  1133.           {
  1134.             this.mRowCount = 0;  
  1135.             this.mResults = [];
  1136.             
  1137.             if (aInvalidate && this.mOutliner)
  1138.               this.mOutliner.invalidate();
  1139.           },
  1140.           
  1141.           addResults: function(aResults)
  1142.           {
  1143.             this.mResults.push(aResults);
  1144.             var oldCount = this.mRowCount;
  1145.             this.mRowCount += aResults.items.Count();
  1146.             
  1147.             if (this.mOutliner)
  1148.               this.mOutliner.rowCountChanged(oldCount, this.mRowCount);
  1149.           },
  1150.           
  1151.           createAtom: function(aVal)
  1152.           {
  1153.             try {
  1154.               var i = Components.interfaces.nsIAtomService;
  1155.               var svc = Components.classes["@mozilla.org/atom-service;1"].getService(i);
  1156.               return svc.getAtom(aVal);
  1157.             } catch(ex) { }
  1158.             return null; // XXX equivalent to falling off the end?
  1159.           },
  1160.  
  1161.           //////////////////////////////////////////////////////////
  1162.           // nsIOutlinerView interface
  1163.           
  1164.           get rowCount() {
  1165.             return this.mRowCount;
  1166.           },
  1167.           
  1168.           // implementing these results in a crash
  1169.           // get selection() {},
  1170.           // set selection(aVal) { },
  1171.           
  1172.           setOutliner: function(aOutliner)
  1173.           {
  1174.             this.mOutliner = aOutliner;
  1175.             
  1176.             if (aOutliner) {
  1177.               this.mBoxObject = this.mTextbox.resultsPopup.outliner.outlinerBoxObject;
  1178.             
  1179.               // cache atoms for pseudoelement properties
  1180.               this.mAtomSelected = this.createAtom("menuactive")
  1181.             }
  1182.           },
  1183.           
  1184.           getCellText: function(aRow, aColId)
  1185.           {
  1186.             var result = this.mTextbox.getResultAt(aRow);
  1187.             if (!result) return "";
  1188.             return aColId == "value" ? result.value : (aColId == "comment" ? result.comment : "");
  1189.           },
  1190.  
  1191.           getRowProperties: function(aIndex, aProperties) 
  1192.           {
  1193.             if (aIndex == this.mSelectedIndex)
  1194.               aProperties.AppendElement(this.mAtomSelected);
  1195.           },
  1196.  
  1197.           getCellProperties: function(aIndex, aColId, aProperties)
  1198.           {
  1199.             this.getRowProperties(aIndex, aProperties);
  1200.  
  1201.             // for the value column, append nsIAutoCompleteItem::className
  1202.             // to the property list so that we can style this column
  1203.             // using that property
  1204.             try { 
  1205.               if (aColId == "value") {
  1206.                 var className = this.mTextbox.getResultAt(aIndex).className;
  1207.                   if ( className != "" ) {
  1208.                     aProperties.AppendElement(this.createAtom(className));
  1209.                   }
  1210.               }
  1211.             } catch (ex) {
  1212.               // the ability to style here is a frill, so don't abort
  1213.               // if there's a problem
  1214.             } 
  1215.           },
  1216.  
  1217.           getColumnProperties: function(aColId, aColElt, aProperties) 
  1218.           {
  1219.           },
  1220.           
  1221.           getParentIndex: function(aRowIndex) { },
  1222.           hasNextSibling: function(aRowIndex, aAfterIndex) { },
  1223.           getLevel: function(aIndex) {},
  1224.           isContainer: function(aIndex) {},
  1225.           isContainerOpen: function(aIndex) {},
  1226.           isContainerEmpty: function(aIndex) {},
  1227.           toggleOpenState: function(aIndex) {},
  1228.           selectionChanged: function() {},
  1229.           cycleHeader: function(aColId, aElt) {},
  1230.           cycleCell: function(aRow, aColId) {},
  1231.           isEditable: function(aRow, aColId) {},
  1232.           setCellText: function(aRow, aColId, aValue) {},
  1233.           performAction: function(aAction) {},
  1234.           performActionOnRow: function(aAction, aRow) {},
  1235.           performActionOnCell: function(aAction, aRow, aColId) {}
  1236.         });
  1237.       ]]></property>
  1238.  
  1239.     </implementation>
  1240.  
  1241.     <handlers>
  1242.       <handler event="input"
  1243.                action="this.processInput();"/>
  1244.  
  1245.       <handler event="keypress" phase="capturing"
  1246.                 action="return this.processKeyPress(event);"/>
  1247.  
  1248.       <handler event="focus" phase="capturing"
  1249.                action="this.userAction = 'typing';"/>
  1250.  
  1251.       <handler event="blur" phase="capturing"
  1252.                action="this.userAction = 'none'; this.finishAutoComplete(false, false);"/>
  1253.     </handlers>
  1254.   </binding> 
  1255.  
  1256.   <binding id="autocomplete-result-popup" extends="chrome://global/content/bindings/popup.xml#popup">
  1257.     <resources>
  1258.       <stylesheet src="chrome://global/content/autocomplete.css"/>
  1259.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  1260.     </resources>
  1261.  
  1262.     <content>
  1263.       <xul:hbox class="autocomplete-result-box" flex="1">
  1264.         <xul:outliner anonid="outliner" class="autocomplete-outliner" flex="1">
  1265.           <xul:outlinerbody anonid="outlinerbody" class="autocomplete-outlinerbody" flex="1"/>
  1266.         </xul:outliner>
  1267.       </xul:hbox>
  1268.     </content>
  1269.     
  1270.     <implementation>
  1271.       <constructor><![CDATA[
  1272.         if (this.textbox && this.textbox.view)
  1273.           this.initialize();
  1274.       ]]></constructor>
  1275.  
  1276.      <property name="textbox"
  1277.                 onget="return '__AUTOCOMPLETE_BOX__' in this ? this.__AUTOCOMPLETE_BOX__ : null;"/>
  1278.       
  1279.       <property name="outliner">
  1280.         document.getAnonymousElementByAttribute(this, "anonid", "outliner");
  1281.       </property>
  1282.  
  1283.       <property name="outlinerbody">
  1284.         document.getAnonymousElementByAttribute(this, "anonid", "outlinerbody");
  1285.       </property>
  1286.  
  1287.       <property name="view" onget="return this.mView;">
  1288.         <setter><![CDATA[
  1289.           this.mView = val;
  1290.           var bx = this.outliner.boxObject;
  1291.           bx = bx.QueryInterface(Components.interfaces.nsIOutlinerBoxObject);
  1292.           bx.view = val;
  1293.         ]]></setter>
  1294.       </property>
  1295.  
  1296.      <property name="pageCount"
  1297.                onget="return this.outliner.outlinerBoxObject.getPageCount();"/>
  1298.  
  1299.       <property name="selectedIndex"
  1300.                 onget="return this.textbox.view.selectedIndex"
  1301.                 onset="this.textbox.view.selectedIndex = val; return val;"/>
  1302.  
  1303.      <property name="mLastRows">0</property>
  1304.  
  1305.       <method name="initialize">
  1306.         <body><![CDATA[
  1307.         this.outliner.__AUTOCOMPLETE_BOX__ = this.textbox;
  1308.         this.outlinerbody.__AUTOCOMPLETE_BOX__ = this.textbox;
  1309.  
  1310.         this._selectedIndex = null;
  1311.  
  1312.         this.initColumns();
  1313.         this.view = this.textbox.view;
  1314.         ]]></body>
  1315.       </method>
  1316.  
  1317.       <!-- initialize the columns in the outliner -->
  1318.       <method name="initColumns">
  1319.         <body><![CDATA[
  1320.           if (this.textbox.showCommentColumn) {
  1321.             this.addColumn({id: "value", flex: 2});
  1322.             this.addColumn({id: "comment", flex: 1});
  1323.           } else
  1324.             this.addColumn({id: "value", flex: 1});
  1325.         ]]></body>
  1326.       </method>
  1327.  
  1328.       <method name="addColumn">
  1329.         <parameter name="aAttrs"/>
  1330.         <body><![CDATA[
  1331.           var col = document.createElement("outlinercol");
  1332.           col.setAttribute("class", "autocomplete-outlinercol");
  1333.           for (var name in aAttrs)
  1334.             col.setAttribute(name, aAttrs[name]);
  1335.           this.outliner.appendChild(col);
  1336.           return col;
  1337.         ]]></body>
  1338.       </method>
  1339.  
  1340.       <!-- remove a single column from the outliner, specified by
  1341.            element id -->
  1342.       <method name="removeColumn">
  1343.         <parameter name="aColId"/>
  1344.         <body><![CDATA[
  1345.           return this.outliner.removeChild(document.getElementById(aColId));
  1346.         ]]></body>
  1347.       </method>
  1348.  
  1349.       <method name="adjustHeight">
  1350.         <body><![CDATA[
  1351.           // detect the desired height of the outliner
  1352.           var bx = this.outliner.outlinerBoxObject;
  1353.           var view = this.textbox.view;
  1354.           var rows = this.textbox.maxRows;
  1355.           if (!view.rowCount || (rows && view.rowCount < rows))
  1356.             rows = view.rowCount;
  1357.           
  1358.           var height = rows * bx.rowHeight;
  1359.           
  1360.           this.outliner.setAttribute("height", height);
  1361.           this.outliner.setAttribute("hidescrollbar", view.rowCount <= rows);
  1362.           
  1363.           // XXX horrible hack to workaround for bug 86551
  1364.           // outliner will refuse to recognize initial height of 0 unless
  1365.           // we force frame reconstruction via setting the style attribute
  1366.           if (!("__firstPaint__" in this)) {
  1367.             this.__firstPaint__ = true;
  1368.             this.outliner.setAttribute("style", this.outliner.getAttribute("style"));
  1369.           }
  1370.         ]]></body>
  1371.       </method>
  1372.  
  1373.       <method name="selectBy">
  1374.         <parameter name="aDir"/>
  1375.         <parameter name="aAmount"/>
  1376.         <body><![CDATA[
  1377.           try {
  1378.             var bx = this.outliner.outlinerBoxObject;
  1379.             var view = bx.view;
  1380.             this.selectedIndex = this.getNextIndex(aDir, aAmount, this.selectedIndex, view.rowCount-1);
  1381.   
  1382.             return this.selectedIndex;
  1383.           } catch (ex) {
  1384.             // do nothing - occasionally timer-related js errors happen here
  1385.             // e.g. "this.selectedIndex has no properties", when you type fast and hit a
  1386.             // navigation key before this popup has opened
  1387.             return null;
  1388.           }
  1389.         ]]></body>
  1390.       </method>
  1391.  
  1392.       <method name="getNextIndex">
  1393.         <parameter name="aDir"/>
  1394.         <parameter name="aAmount"/>
  1395.         <parameter name="aIndex"/>
  1396.         <parameter name="aMaxRow"/>
  1397.         <body><![CDATA[
  1398.           if (aMaxRow < 0)
  1399.             return null;
  1400.             
  1401.           var newIdx = aIndex + aDir*aAmount;
  1402.           if (aDir < 0 && aIndex == null || newIdx > aMaxRow && aIndex != aMaxRow)
  1403.             newIdx = aMaxRow;
  1404.           else if (aDir > 0 && aIndex == null || newIdx < 0 && aIndex != 0)
  1405.             newIdx = 0;
  1406.           
  1407.           if (newIdx < 0 && aIndex == 0 || newIdx > aMaxRow && aIndex == aMaxRow)
  1408.             aIndex = null;
  1409.           else
  1410.             aIndex = newIdx;
  1411.           
  1412.           return aIndex;
  1413.         ]]></body>
  1414.       </method>
  1415.  
  1416.       <!-- This method is meant to be overriden by bindings extending this one.  When the 
  1417.            user selects an item from the list by hitting enter or clicking, this method
  1418.            can set the value of the textbox to a different value if it wants to. -->
  1419.       <method name="getOverrideValue">
  1420.         <body><![CDATA[
  1421.           return null;
  1422.         ]]></body>
  1423.       </method>
  1424.  
  1425.     </implementation>
  1426.  
  1427.     <handlers>
  1428.       <handler event="popupshowing">
  1429.         this.textbox.mMenuOpen = true;
  1430.       </handler>
  1431.  
  1432.       <handler event="popuphiding">
  1433.         this.textbox.mMenuOpen = false;
  1434.         this.selectedIndex = null;
  1435.       </handler>
  1436.     </handlers>
  1437.   </binding>
  1438.  
  1439.   <binding id="autocomplete-outliner" extends="chrome://global/content/bindings/outliner.xml#outliner">
  1440.     <resources>
  1441.       <stylesheet src="chrome://global/content/autocomplete.css"/>
  1442.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  1443.     </resources>
  1444.  
  1445.     <content>
  1446.       <xul:vbox flex="1">
  1447.         <xul:hbox class="autocomplete-outliner-cols">
  1448.           <children includes="outlinercol"/>
  1449.         </xul:hbox>
  1450.         <xul:outlinerrows class="autocomplete-outlinerrows outliner-rows" inherits="hidescrollbar" flex="1">
  1451.           <children/>
  1452.         </xul:outlinerrows>
  1453.       </xul:vbox>
  1454.     </content>
  1455.  
  1456.     <implementation>
  1457.       <property name="textbox"
  1458.                 onget="return this.__AUTOCOMPLETE_BOX__;"/>
  1459.     </implementation>
  1460.   </binding>
  1461.  
  1462.   <binding id="autocomplete-outlinerbody">
  1463.     <resources>
  1464.       <stylesheet src="chrome://global/content/autocomplete.css"/>
  1465.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  1466.     </resources>
  1467.  
  1468.     <implementation>
  1469.       <property name="textbox"
  1470.                 onget="return this.__AUTOCOMPLETE_BOX__;"/>
  1471.  
  1472.       <property name="mLastMoveTime">new Date()</property>
  1473.  
  1474.       <method name="getHoverCell">
  1475.         <parameter name="aEvent"/>
  1476.         <body><![CDATA[
  1477.            var obox = this.textbox.view.outlinerBoxObject;
  1478.            var pbox = this.textbox.resultsPopup.boxObject;
  1479.  
  1480.            var row = {}; var col = {}; var obj = {};
  1481.            var x = aEvent.screenX-pbox.screenX + this.boxObject.x;
  1482.            var y = aEvent.screenY-pbox.screenY + this.boxObject.y;
  1483.            obox.getCellAt(x-1, y-1, row, col, obj);
  1484.            if (row.value >= 0 && row.value < this.textbox.view.rowCount)
  1485.              return {row: row.value, column: col.value};
  1486.            else
  1487.             return null;
  1488.         ]]></body>
  1489.       </method>
  1490.     </implementation>
  1491.     
  1492.     <handlers>
  1493.       <handler event="mouseout" action="this.textbox.view.selectedIndex = null;"/>
  1494.       <handler event="mouseup" action="this.textbox.onResultClick();"/>
  1495.  
  1496.       <handler event="mousemove"><![CDATA[
  1497.         if (new Date() - this.mLastMoveTime > 30) {
  1498.          var rc = this.getHoverCell(event);
  1499.          if (rc && rc.row != this.textbox.view.selectedIndex) 
  1500.             this.textbox.view.selectedIndex = rc.row;
  1501.           this.mLastMoveTime = new Date();
  1502.         }
  1503.       ]]></handler>
  1504.     </handlers>
  1505.   </binding>
  1506.  
  1507.   <binding id="autocomplete-outlinerrows">
  1508.     <resources>
  1509.       <stylesheet src="chrome://global/content/autocomplete.css"/>
  1510.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  1511.     </resources>
  1512.  
  1513.     <content onmousedown="event.preventDefault()">
  1514.       <xul:hbox flex="1" class="outliner-bodybox">
  1515.         <children/>
  1516.       </xul:hbox>
  1517.       <xul:scrollbar inherits="hidescrollbar" orient="vertical" class="outliner-scrollbar"/>
  1518.     </content>
  1519.   </binding>
  1520.  
  1521.   <binding id="autocomplete-outlinercol">
  1522.     <resources>
  1523.       <stylesheet src="chrome://global/content/autocomplete.css"/>
  1524.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  1525.     </resources>
  1526.   </binding>
  1527.  
  1528.   <binding id="autocomplete-history-popup" extends="chrome://global/content/bindings/popup.xml#popup">
  1529.     <resources>
  1530.       <stylesheet src="chrome://global/content/autocomplete.css"/>
  1531.       <stylesheet src="chrome://global/skin/autocomplete.css"/>
  1532.     </resources>
  1533.  
  1534.     <content>
  1535.       <xul:vbox class="autocomplete-result-box autocomplete-history-box" flex="1">
  1536.         <children/>
  1537.       </xul:vbox>
  1538.     </content>
  1539.   </binding>
  1540.  
  1541.   <binding id="autocomplete-internal-box">
  1542.     <content context="_child">
  1543.       <children/>
  1544.         <xul:menupopup onpopupshowing="this.parentNode.focus(); this.parentNode.doPopupItemEnabling(this);">
  1545.           <xul:menuitem label="&undoCmd.label;" accesskey="&undoCmd.accesskey;" cmd="cmd_undo"
  1546.                         oncommand="var controller = document.commandDispatcher.getControllerForCommand('cmd_undo');              
  1547.                                    controller.doCommand('cmd_undo');"/>
  1548.           <xul:menuseparator/>
  1549.           <xul:menuitem label="&cutCmd.label;" accesskey="&cutCmd.accesskey;" cmd="cmd_cut"
  1550.                         oncommand="var controller = document.commandDispatcher.getControllerForCommand('cmd_cut');
  1551.                                    controller.doCommand('cmd_cut');"/>
  1552.           <xul:menuitem label="©Cmd.label;" accesskey="©Cmd.accesskey;" cmd="cmd_copy"
  1553.                         oncommand="var controller = document.commandDispatcher.getControllerForCommand('cmd_copy');
  1554.                                    controller.doCommand('cmd_copy');"/>
  1555.           <xul:menuitem label="&pasteCmd.label;" accesskey="&pasteCmd.accesskey;" cmd="cmd_paste"
  1556.                         oncommand="var controller = document.commandDispatcher.getControllerForCommand('cmd_paste');
  1557.                         controller.doCommand('cmd_paste');"/>
  1558.           <xul:menuitem label="&deleteCmd.label;" accesskey="&deleteCmd.accesskey;" cmd="cmd_delete"
  1559.                         oncommand="var controller = document.commandDispatcher.getControllerForCommand('cmd_delete');
  1560.                                    controller.doCommand('cmd_delete');"/>
  1561.           <xul:menuseparator/>
  1562.           <xul:menuitem label="&selectAllCmd.label;" accesskey="&selectAllCmd.accesskey;" cmd="cmd_selectAll"
  1563.                         oncommand="var controller = document.commandDispatcher.getControllerForCommand('cmd_selectAll');
  1564.                                    controller.doCommand('cmd_selectAll');"/>
  1565.         </xul:menupopup>
  1566.     </content>
  1567.  
  1568.     <implementation>
  1569.       <method name="doPopupItemEnabling">
  1570.         <parameter name="popupNode"/> 
  1571.         <body>
  1572.           <![CDATA[
  1573.             var children = popupNode.childNodes;
  1574.             for (var i = 0; i < children.length; i++) {              
  1575.               var command = children[i].getAttribute("cmd");
  1576.               if (command) {
  1577.                 var controller = document.commandDispatcher.getControllerForCommand(command);
  1578.                 var enabled = controller.isCommandEnabled(command);
  1579.                 if (enabled)
  1580.                   children[i].removeAttribute("disabled");
  1581.                 else
  1582.                   children[i].setAttribute("disabled", "true");               
  1583.               }
  1584.             }
  1585.           ]]>
  1586.         </body>
  1587.       </method>
  1588.     </implementation>
  1589.   </binding>
  1590.   
  1591. </bindings>
  1592.  
  1593.