home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / chrome / mail.jar / content / global / globalOverlay.js < prev    next >
Encoding:
Text File  |  2003-12-01  |  5.2 KB  |  194 lines

  1. function closeWindow(aClose)
  2. {
  3.   var windowCount = 0;
  4.   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  5.   var e = wm.getEnumerator(null);
  6.   
  7.   while (e.hasMoreElements()) {
  8.     var w = e.getNext();
  9.     ++windowCount;
  10.     if (windowCount == 2) 
  11.       break;
  12.   }
  13.  
  14.   // If we're down to the last window and someone tries to shut down, check to make sure we can!
  15.   if (windowCount == 1 && !canQuitApplication()) 
  16.     return false;
  17.  
  18.   if (aClose)    
  19.     window.close();
  20.   
  21.   return true;
  22. }
  23.  
  24. function canQuitApplication()
  25. {
  26.   var os = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  27.   if (os) {
  28.     try {
  29.       var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"].createInstance(Components.interfaces.nsISupportsPRBool);
  30.       os.notifyObservers(cancelQuit, "quit-application-requested", null);
  31.       
  32.       // Something aborted the quit process. 
  33.       if (cancelQuit.data)
  34.         return false;
  35.     }
  36.     catch (ex) {
  37.     }
  38.   }
  39.   return true;
  40. }
  41.  
  42. function goQuitApplication()
  43. {
  44.   if (!canQuitApplication())
  45.     return;
  46.     
  47.   var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  48.   var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  49.   var enumerator = windowManagerInterface.getEnumerator( null );
  50.   var appShell = Components.classes['@mozilla.org/appshell/appShellService;1'].getService();
  51.   appShell = appShell.QueryInterface( Components.interfaces.nsIAppShellService );
  52.  
  53.   var nativeAppSupport = null;
  54.   try {
  55.     nativeAppSupport = appShell.nativeAppSupport;
  56.   }
  57.   catch ( ex ) {
  58.   }
  59.  
  60.   while ( enumerator.hasMoreElements()  )
  61.   {
  62.      var domWindow = enumerator.getNext();
  63.      if (("tryToClose" in domWindow) && !domWindow.tryToClose())
  64.        return false;          
  65.      domWindow.close();
  66.   };
  67.   if (!nativeAppSupport || !nativeAppSupport.isServerMode)
  68.     appShell.quit(Components.interfaces.nsIAppShellService.eAttemptQuit);
  69.   return true;
  70. }
  71.  
  72. //
  73. // Command Updater functions
  74. //
  75. function goUpdateCommand(command)
  76. {
  77.   try {
  78.     var controller = top.document.commandDispatcher.getControllerForCommand(command);
  79.  
  80.     var enabled = false;
  81.  
  82.     if ( controller )
  83.       enabled = controller.isCommandEnabled(command);
  84.  
  85.     goSetCommandEnabled(command, enabled);
  86.   }
  87.   catch (e) {
  88.     dump("An error occurred updating the "+command+" command\n");
  89.   }
  90. }
  91.  
  92. function goDoCommand(command)
  93. {
  94.   try {
  95.     var controller = top.document.commandDispatcher.getControllerForCommand(command);
  96.     if ( controller && controller.isCommandEnabled(command))
  97.       controller.doCommand(command);
  98.   }
  99.   catch (e) {
  100.     dump("An error occurred executing the "+command+" command\n");
  101.     dump(e+"\n")
  102.   }
  103. }
  104.  
  105.  
  106. function goSetCommandEnabled(id, enabled)
  107. {
  108.   var node = document.getElementById(id);
  109.  
  110.   if ( node )
  111.   {
  112.     if ( enabled )
  113.       node.removeAttribute("disabled");
  114.     else
  115.       node.setAttribute('disabled', 'true');
  116.   }
  117. }
  118.  
  119. function goSetMenuValue(command, labelAttribute)
  120. {
  121.   var commandNode = top.document.getElementById(command);
  122.   if ( commandNode )
  123.   {
  124.     var label = commandNode.getAttribute(labelAttribute);
  125.     if ( label )
  126.       commandNode.setAttribute('label', label);
  127.   }
  128. }
  129.  
  130. function goSetAccessKey(command, valueAttribute)
  131. {
  132.   var commandNode = top.document.getElementById(command);
  133.   if ( commandNode )
  134.   {
  135.     var value = commandNode.getAttribute(valueAttribute);
  136.     if ( value )
  137.       commandNode.setAttribute('accesskey', value);
  138.   }
  139. }
  140.  
  141. // this function is used to inform all the controllers attached to a node that an event has occurred
  142. // (e.g. the tree controllers need to be informed of blur events so that they can change some of the
  143. // menu items back to their default values)
  144. function goOnEvent(node, event)
  145. {
  146.   var numControllers = node.controllers.getControllerCount();
  147.   var controller;
  148.  
  149.   for ( var controllerIndex = 0; controllerIndex < numControllers; controllerIndex++ )
  150.   {
  151.     controller = node.controllers.getControllerAt(controllerIndex);
  152.     if ( controller )
  153.       controller.onEvent(event);
  154.   }
  155. }
  156.  
  157. function visitLink(aEvent) {
  158.   var node = aEvent.target;
  159.   while (node.nodeType != Node.ELEMENT_NODE)
  160.     node = node.parentNode;
  161.   var url = node.getAttribute("link");
  162.   if (url != "")
  163.     window.opener.openNewWindowWith(url, null, false);
  164. }
  165.  
  166. function isValidLeftClick(aEvent, aName)
  167. {
  168.   return (aEvent.button == 0 && aEvent.originalTarget.localName == aName);
  169. }
  170.  
  171. function setTooltipText(aID, aTooltipText)
  172. {
  173.   var element = document.getElementById(aID);
  174.   if (element)
  175.     element.setAttribute("tooltiptext", aTooltipText);
  176. }
  177.  
  178. function FillInTooltip ( tipElement )
  179. {
  180.   var retVal = false;
  181.   var textNode = document.getElementById("TOOLTIP-tooltipText");
  182.   if (textNode) {
  183.     while (textNode.hasChildNodes())
  184.       textNode.removeChild(textNode.firstChild);
  185.     var tipText = tipElement.getAttribute("tooltiptext");
  186.     if (tipText) {
  187.       var node = document.createTextNode(tipText);
  188.       textNode.appendChild(node);
  189.       retVal = true;
  190.     }
  191.   }
  192.   return retVal;
  193. }
  194.