home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / navigator / browser.js next >
Text File  |  2003-06-08  |  13KB  |  376 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Blake Ross <blakeross@telocity.com>
  24.  *   Peter Annema <disttsc@bart.nl>
  25.  *   Samir Gehani <sgehani@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the NPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the NPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
  42. var gPrintSettingsAreGlobal = true;
  43. var gSavePrintSettings = true;
  44. var gChromeState = null; // chrome state before we went into print preview
  45. var gOldCloseHandler = null; // close handler before we went into print preview
  46. var gInPrintPreviewMode = false;
  47. var gWebProgress        = null;
  48.  
  49. function getWebNavigation()
  50. {
  51.   try {
  52.     return getBrowser().webNavigation;
  53.   } catch (e) {
  54.     return null;
  55.   }
  56. }
  57.  
  58. function BrowserReloadWithFlags(reloadFlags)
  59. {
  60.   /* First, we'll try to use the session history object to reload so 
  61.    * that framesets are handled properly. If we're in a special 
  62.    * window (such as view-source) that has no session history, fall 
  63.    * back on using the web navigation's reload method.
  64.    */
  65.  
  66.   var webNav = getWebNavigation();
  67.   try {
  68.     var sh = webNav.sessionHistory;
  69.     if (sh)
  70.       webNav = sh.QueryInterface(Components.interfaces.nsIWebNavigation);
  71.   } catch (e) {
  72.   }
  73.  
  74.   try {
  75.     webNav.reload(reloadFlags);
  76.   } catch (e) {
  77.   }
  78. }
  79.  
  80. function toggleAffectedChrome(aHide)
  81. {
  82.   // chrome to toggle includes:
  83.   //   (*) menubar
  84.   //   (*) navigation bar
  85.   //   (*) personal toolbar
  86.   //   (*) tab browser ``strip''
  87.   //   (*) sidebar
  88.  
  89.   if (!gChromeState)
  90.     gChromeState = new Object;
  91.   var navToolbox = document.getElementById("navigator-toolbox");
  92.   navToolbox.hidden = aHide;
  93.   var theTabbrowser = document.getElementById("content"); 
  94.  
  95.   // sidebar states map as follows:
  96.   //   was-hidden    => hide/show nothing
  97.   //   was-collapsed => hide/show only the splitter
  98.   //   was-shown     => hide/show the splitter and the box
  99.   if (aHide)
  100.   {
  101.     // going into print preview mode
  102.     if (sidebar_is_collapsed())
  103.     {
  104.       gChromeState.sidebar = "was-collapsed";
  105.     }
  106.     else if (sidebar_is_hidden())
  107.       gChromeState.sidebar = "was-hidden";
  108.     else 
  109.     {
  110.       gChromeState.sidebar = "was-visible";
  111.     }
  112.     document.getElementById("sidebar-box").hidden = true;
  113.     document.getElementById("sidebar-splitter").hidden = true;
  114.     //deal with tab browser
  115.     gChromeState.hadTabStrip = theTabbrowser.getStripVisibility();
  116.     theTabbrowser.setStripVisibilityTo(false);
  117.   }
  118.   else
  119.   {
  120.     // restoring normal mode (i.e., leaving print preview mode)
  121.     //restore tab browser
  122.     theTabbrowser.setStripVisibilityTo(gChromeState.hadTabStrip);
  123.     if (gChromeState.sidebar == "was-collapsed" ||
  124.         gChromeState.sidebar == "was-visible")
  125.       document.getElementById("sidebar-splitter").hidden = false;
  126.     if (gChromeState.sidebar == "was-visible")
  127.       document.getElementById("sidebar-box").hidden = false;
  128.   }
  129.  
  130.   // if we are unhiding and sidebar used to be there rebuild it
  131.   if (!aHide && gChromeState.sidebar == "was-visible")
  132.     SidebarRebuild();
  133. }
  134.  
  135. function showPrintPreviewToolbar()
  136. {
  137.   toggleAffectedChrome(true);
  138.   const kXULNS = 
  139.     "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  140.  
  141.   var printPreviewTB = document.createElementNS(kXULNS, "toolbar");
  142.   printPreviewTB.setAttribute("printpreview", true);
  143.   printPreviewTB.setAttribute("id", "print-preview-toolbar");
  144.  
  145.   var navToolbox = document.getElementById("navigator-toolbox");
  146.   navToolbox.parentNode.insertBefore(printPreviewTB, navToolbox);
  147. }
  148.  
  149. function BrowserExitPrintPreview()
  150. {
  151.   gInPrintPreviewMode = false;
  152.  
  153.   var browser = getBrowser();
  154.   browser.setAttribute("handleCtrlPageUpDown", "true");
  155.  
  156.   // remove the print preview toolbar
  157.   var navToolbox = document.getElementById("navigator-toolbox");
  158.   var printPreviewTB = document.getElementById("print-preview-toolbar");
  159.   navToolbox.parentNode.removeChild(printPreviewTB);
  160.  
  161.   // restore chrome to original state
  162.   toggleAffectedChrome(false);
  163.  
  164.   // restore old onclose handler if we found one before previewing
  165.   var mainWin = document.getElementById("main-window");
  166.   mainWin.setAttribute("onclose", gOldCloseHandler);
  167.  
  168.   // exit print preview galley mode in content area
  169.   var ifreq = _content.QueryInterface(
  170.     Components.interfaces.nsIInterfaceRequestor);
  171.   var webBrowserPrint = ifreq.getInterface(
  172.     Components.interfaces.nsIWebBrowserPrint);     
  173.   webBrowserPrint.exitPrintPreview(); 
  174.   _content.focus();
  175. }
  176.  
  177. // This observer is called once the progress dialog has been "opened"
  178. var gPrintPreviewObs = {
  179.     observe: function(aSubject, aTopic, aData)
  180.     {
  181.       setTimeout(FinishPrintPreview, 0);
  182.     },
  183.  
  184.     QueryInterface : function(iid)
  185.     {
  186.      if (iid.equals(Components.interfaces.nsIObserver) || iid.equals(Components.interfaces.nsISupportsWeakReference))
  187.       return this;
  188.      
  189.      throw Components.results.NS_NOINTERFACE;
  190.     }
  191. };
  192.  
  193. function BrowserPrintPreview()
  194. {
  195.   var ifreq;
  196.   var webBrowserPrint;  
  197.   try {
  198.     ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  199.     webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);     
  200.     gPrintSettings = GetPrintSettings();
  201.  
  202.   } catch (e) {
  203.     // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  204.     // causing an exception to be thrown which we catch here.
  205.     // Unfortunately this will also consume helpful failures, so add a
  206.     // dump(e); // if you need to debug
  207.   }
  208.  
  209.   // Here we get the PrintingPromptService tso we can display the PP Progress from script
  210.   // For the browser implemented via XUL with the PP toolbar we cannot let it be
  211.   // automatically opened from the print engine because the XUL scrollbars in the PP window
  212.   // will layout before the content window and a crash will occur.
  213.   //
  214.   // Doing it all from script, means it lays out before hand and we can let printing do it's own thing
  215.   gWebProgress = new Object();
  216.  
  217.   var printPreviewParams    = new Object();
  218.   var notifyOnOpen          = new Object();
  219.   var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  220.                                   .getService(Components.interfaces.nsIPrintingPromptService);
  221.   if (printingPromptService) {
  222.     // just in case we are already printing, 
  223.     // an error code could be returned if the Prgress Dialog is already displayed
  224.     try {
  225.       printingPromptService.showProgress(this, webBrowserPrint, gPrintSettings, gPrintPreviewObs, false, gWebProgress, 
  226.                                          printPreviewParams, notifyOnOpen);
  227.       if (printPreviewParams.value) {
  228.         var webNav = getWebNavigation();
  229.         printPreviewParams.value.docTitle = webNav.document.title;
  230.         printPreviewParams.value.docURL   = webNav.currentURI.spec;
  231.       }
  232.  
  233.       // this tells us whether we should continue on with PP or 
  234.       // wait for the callback via the observer
  235.       if (!notifyOnOpen.value.valueOf() || gWebProgress.value == null) {
  236.         FinishPrintPreview();
  237.       }
  238.     } catch (e) {
  239.       FinishPrintPreview();
  240.     }
  241.   }
  242. }
  243.  
  244. function FinishPrintPreview()
  245. {
  246.   gInPrintPreviewMode = true;
  247.  
  248.   var browser = getBrowser();
  249.   try {
  250.     var ifreq = _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  251.     var webBrowserPrint = ifreq.getInterface(Components.interfaces.nsIWebBrowserPrint);     
  252.     if (webBrowserPrint) {
  253.       gPrintSettings = GetPrintSettings();
  254.       webBrowserPrint.printPreview(gPrintSettings, null, gWebProgress.value);
  255.     }
  256.  
  257.     browser.setAttribute("handleCtrlPageUpDown", "false");
  258.  
  259.     var mainWin = document.getElementById("main-window");
  260.  
  261.     // save previous close handler to restoreon exiting print preview mode
  262.     if (mainWin.hasAttribute("onclose"))
  263.       gOldCloseHandler = mainWin.getAttribute("onclose");
  264.     else
  265.       gOldCloseHandler = null;
  266.     mainWin.setAttribute("onclose", "BrowserExitPrintPreview(); return false;");
  267.  
  268.     // show the toolbar after we go into print preview mode so
  269.     // that we can initialize the toolbar with total num pages
  270.     showPrintPreviewToolbar();
  271.  
  272.     _content.focus();
  273.   } catch (e) {
  274.     // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  275.     // causing an exception to be thrown which we catch here.
  276.     // Unfortunately this will also consume helpful failures, so add a
  277.     // dump(e); // if you need to debug
  278.   }
  279. }
  280.  
  281.  
  282. function BrowserSetDefaultCharacterSet(aCharset)
  283. {
  284.   // no longer needed; set when setting Force; see bug 79608
  285. }
  286.  
  287. function BrowserSetForcedCharacterSet(aCharset)
  288. {
  289.   var docCharset = getBrowser().docShell.QueryInterface(
  290.                             Components.interfaces.nsIDocCharset);
  291.   docCharset.charset = aCharset;
  292.   BrowserReloadWithFlags(nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
  293. }
  294.  
  295. function BrowserSetForcedDetector(doReload)
  296. {
  297.   getBrowser().documentCharsetInfo.forcedDetector = true;
  298.   if (doReload)
  299.     BrowserReloadWithFlags(nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
  300. }
  301.  
  302. function BrowserFind()
  303. {
  304.   var focusedWindow = document.commandDispatcher.focusedWindow;
  305.   if (!focusedWindow || focusedWindow == window)
  306.     focusedWindow = window._content;
  307.  
  308.   findInPage(getBrowser(), window._content, focusedWindow)
  309. }
  310.  
  311. function BrowserFindAgain(reverse)
  312. {
  313.     var focusedWindow = document.commandDispatcher.focusedWindow;
  314.     if (!focusedWindow || focusedWindow == window)
  315.       focusedWindow = window._content;
  316.  
  317.   findAgainInPage(getBrowser(), window._content, focusedWindow, reverse)
  318. }
  319.  
  320. function BrowserCanFindAgain()
  321. {
  322.   return canFindAgainInPage();
  323. }
  324.  
  325. function getMarkupDocumentViewer()
  326. {
  327.   return getBrowser().markupDocumentViewer;
  328. }
  329.  
  330. /**
  331.  * Content area tooltip.
  332.  * XXX - this must move into XBL binding/equiv! Do not want to pollute
  333.  *       navigator.js with functionality that can be encapsulated into
  334.  *       browser widget. TEMPORARY!
  335.  *
  336.  * NOTE: Any changes to this routine need to be mirrored in ChromeListener::FindTitleText()
  337.  *       (located in mozilla/embedding/browser/webBrowser/nsDocShellTreeOwner.cpp)
  338.  *       which performs the same function, but for embedded clients that
  339.  *       don't use a XUL/JS layer. It is important that the logic of
  340.  *       these two routines be kept more or less in sync.
  341.  *       (pinkerton)
  342.  **/
  343. function FillInHTMLTooltip(tipElement)
  344. {
  345.   var retVal = false;
  346.   if (tipElement.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
  347.     return retVal;
  348.  
  349.   const XLinkNS = "http://www.w3.org/1999/xlink";
  350.  
  351.  
  352.   var titleText = null;
  353.   var XLinkTitleText = null;
  354.   
  355.   while (!titleText && !XLinkTitleText && tipElement) {
  356.     if (tipElement.nodeType == Node.ELEMENT_NODE) {
  357.       titleText = tipElement.getAttribute("title");
  358.       XLinkTitleText = tipElement.getAttributeNS(XLinkNS, "title");
  359.     }
  360.     tipElement = tipElement.parentNode;
  361.   }
  362.  
  363.   var texts = [titleText, XLinkTitleText];
  364.   var tipNode = document.getElementById("aHTMLTooltip");
  365.  
  366.   for (var i = 0; i < texts.length; ++i) {
  367.     var t = texts[i];
  368.     if (t && t.search(/\S/) >= 0) {
  369.       tipNode.setAttribute("label", t);
  370.       retVal = true;
  371.     }
  372.   }
  373.  
  374.   return retVal;
  375. }
  376.