home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / toolkit.jar / content / global / selectDialog.js < prev    next >
Text File  |  2001-02-14  |  4KB  |  141 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape Communications
  16.  * Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  * Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. var elements = [];
  25. var numItems;
  26. var list;
  27.  
  28. function selectDialogOnLoad() {
  29.   dump("selectDialogOnLoad \n");
  30.   doSetOKCancel( commonDialogOnOK, commonDialogOnCancel );
  31.  
  32.   param = window.arguments[0].QueryInterface( Components.interfaces.nsIDialogParamBlock  );
  33.   if( !param )
  34.   dump( " error getting param block interface\n" );
  35.  
  36.   var messageText = param.GetString( 1 );
  37.   dump("message: "+ messageText +"\n");
  38.   {
  39.     var messageFragment;
  40.  
  41.     // Let the caller use "\n" to cause breaks
  42.     // Translate these into <br> tags
  43.     var messageParent = (document.getElementById("info.txt"));
  44.     done = false;
  45.     while (!done) {
  46.     breakIndex =   messageText.indexOf('\n');
  47.       if (breakIndex == 0) {
  48.         // Ignore break at the first character
  49.         messageText = messageText.slice(1);
  50.         dump("Found break at begining\n");
  51.         messageFragment = "";
  52.       } else if (breakIndex > 0) {
  53.         // The fragment up to the break
  54.         messageFragment = messageText.slice(0, breakIndex);
  55.  
  56.         // Chop off fragment we just found from remaining string
  57.         messageText = messageText.slice(breakIndex+1);
  58.       } else {
  59.         // "\n" not found. We're done
  60.         done = true;
  61.         messageFragment = messageText;
  62.       }
  63.       messageParent.setAttribute("value", messageFragment);
  64.     }
  65.   }
  66.  
  67.   var windowTitle = param.GetString( 0 );
  68.   dump("title: "+ windowTitle +"\n");
  69.   window.title = windowTitle;
  70.  
  71.   list = document.getElementById("list");
  72.   numItems = param.GetInt( 2 )
  73.  
  74.   for ( i = 2; i <= numItems+1; i++ ) {
  75.     var newString = param.GetString( i );
  76.     if (newString == "") {
  77.       newString = "<>";
  78.     }
  79.     dump("setting string "+newString+"\n");
  80.     elements[i-2] = AppendStringToTreelist(list, newString);
  81.   }
  82.   list.selectItem(elements[0]);
  83.  
  84.   // resize the window to the content
  85.   window.sizeToContent();
  86.  
  87.   // Move to the right location
  88.   moveToAlertPosition();
  89.   param.SetInt(0, 1 );
  90.   centerWindowOnScreen();
  91. }
  92.  
  93. function commonDialogOnOK() {
  94.   dump("commonDialogOnOK \n");
  95.   for (var i=0; i<numItems; i++) {
  96.     if (elements[i] == list.selectedItems[0]) {
  97.       param.SetInt(2, i );
  98.       break;
  99.     }
  100.   }
  101.   param.SetInt(0, 0 );
  102.   return true;
  103. }
  104.  
  105. function commonDialogOnCancel() {
  106.   dump("commonDialogOnCancel \n");
  107.   for (var i=0; i<numItems; i++) {
  108.     if (elements[i]) {
  109.       param.SetInt(2, i );
  110.       break;
  111.     }
  112.   }
  113.   param.SetInt(0, 1 );
  114.   return true;
  115. }
  116.  
  117. // following routine should really be in a global utilities package
  118.  
  119. function AppendStringToTreelist(tree, string)
  120. {
  121.   if (tree)
  122.   {
  123.     var treechildren = document.getElementById('child');
  124.     
  125.     var treeitem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treeitem");
  126.     var treerow = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treerow");
  127.     var treecell = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treecell");
  128.     if (treeitem && treerow && treecell)
  129.     {
  130.       treecell.setAttribute("value", string);
  131.       treerow.appendChild(treecell);
  132.       treeitem.appendChild(treerow);
  133.       treechildren.appendChild(treeitem)
  134.       var len = Number(tree.getAttribute("length"));
  135.       if (!len) len = -1;
  136.       tree.setAttribute("length",len+1);
  137.       return treeitem;
  138.     }
  139.   }
  140.   return null;
  141. }