home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 July & August / PCWorld_2005-07-08_cd.bin / komunikace / netscape / nsb-install-8-0.exe / chrome / toolkit.jar / content / global / printUtils.js < prev    next >
Text File  |  2004-12-08  |  9KB  |  247 lines

  1.  
  2. var  XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  3.  
  4. var PrintUtils = {
  5.  
  6.   showPageSetup: function ()
  7.   {
  8.     try {
  9.       var printSettings = this.getPrintSettings();
  10.       var PRINTPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  11.                                      .getService(Components.interfaces.nsIPrintingPromptService);
  12.       PRINTPROMPTSVC.showPageSetup(window, printSettings, null);
  13.       this.savePrintSettings(printSettings);
  14.  
  15.     } catch (e) {
  16.       dump("showPageSetup "+e+"\n");
  17.       return false;
  18.     }
  19.     return true;
  20.   },
  21.  
  22.   print: function ()
  23.   {
  24.     var webBrowserPrint = this.getWebBrowserPrint();
  25.     var printSettings = this.getPrintSettings();
  26.     try {
  27.       webBrowserPrint.print(printSettings, null);
  28.     } catch (e) {
  29.       // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  30.       // causing an exception to be thrown which we catch here.
  31.       // Unfortunately this will also consume helpful failures, so add a
  32.       // dump("print: "+e+"\n"); // if you need to debug
  33.     }
  34.   },
  35.  
  36.   printPreview: function (aEnterPPCallback, aExitPPCallback)
  37.   {
  38.     // MERC: ccampbell: close sidebar if it's open
  39.     try {
  40.       if (!sidebar_is_hidden())
  41.         SidebarShowHide();
  42.     } catch(ex) { }
  43.  
  44.     // if we're in PP mode, don't copy the callbacks.
  45.     if (!document.getElementById("print-preview-toolbar")) {
  46.       this._onEnterPP = aEnterPPCallback;
  47.       this._onExitPP  = aExitPPCallback;
  48.     }
  49.     this._webProgressPP = {};
  50.     var ppParams        = {};
  51.     var notifyOnOpen    = {};
  52.     var webBrowserPrint = this.getWebBrowserPrint();
  53.     var printSettings   = this.getPrintSettings();
  54.     // Here we get the PrintingPromptService so we can display the PP Progress from script
  55.     // For the browser implemented via XUL with the PP toolbar we cannot let it be
  56.     // automatically opened from the print engine because the XUL scrollbars in the PP window
  57.     // will layout before the content window and a crash will occur.
  58.     // Doing it all from script, means it lays out before hand and we can let printing do it's own thing
  59.     var PPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  60.                                .getService(Components.interfaces.nsIPrintingPromptService);
  61.     // just in case we are already printing, 
  62.     // an error code could be returned if the Prgress Dialog is already displayed
  63.     try {
  64.       PPROMPTSVC.showProgress(this, webBrowserPrint, printSettings, this._obsPP, false,
  65.                               this._webProgressPP, ppParams, notifyOnOpen);
  66.       if (ppParams.value) {
  67.         var webNav = getBrowser().webNavigation;
  68.         ppParams.value.docTitle = webNav.document.title;
  69.         ppParams.value.docURL   = webNav.currentURI.spec;
  70.       }
  71.  
  72.       // this tells us whether we should continue on with PP or 
  73.       // wait for the callback via the observer
  74.       if (!notifyOnOpen.value.valueOf() || this._webProgressPP.value == null)
  75.         this.enterPrintPreview();
  76.     } catch (e) {
  77.       this.enterPrintPreview();
  78.     }
  79.   },
  80.  
  81.   getWebBrowserPrint: function ()
  82.   {
  83.     return _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  84.                    .getInterface(Components.interfaces.nsIWebBrowserPrint);
  85.   },
  86.  
  87.   savePrintSettings: function (aPrintSettings)
  88.   {
  89.     var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  90.                           .getService(Components.interfaces.nsIPrintSettingsService);
  91.     PSSVC.savePrintSettingsToPrefs(aPrintSettings, false, aPrintSettings.kInitSaveNativeData);
  92.   },
  93.  
  94.   ////////////////////////////////////////
  95.   // "private" methods. Don't use them. //
  96.   ////////////////////////////////////////
  97.  
  98.   setPrinterDefaultsForSelectedPrinter: function (aPSSVC, aPrintSettings)
  99.   {
  100.     if (!aPrintSettings.printerName)
  101.       aPrintSettings.printerName = aPSSVC.defaultPrinterName;
  102.  
  103.     // First get any defaults from the printer 
  104.     aPSSVC.initPrintSettingsFromPrinter(aPrintSettings.printerName, aPrintSettings);
  105.     // now augment them with any values from last time
  106.     aPSSVC.initPrintSettingsFromPrefs(aPrintSettings, true,  aPrintSettings.kInitSaveAll);
  107.   },
  108.  
  109.   getPrintSettings: function ()
  110.   {
  111.     var printSettings;
  112.     try {
  113.       var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  114.                             .getService(Components.interfaces.nsIPrintSettingsService);
  115.       printSettings = PSSVC.globalPrintSettings;
  116.       this.setPrinterDefaultsForSelectedPrinter(PSSVC, printSettings);
  117.     } catch (e) {
  118.       dump("getPrintSettings: "+e+"\n");
  119.     }
  120.     return printSettings;
  121.   },
  122.  
  123.   _chromeState: {},
  124.   _closeHandlerPP: null,
  125.   _webProgressPP: null,
  126.   _onEnterPP: null,
  127.   _onExitPP: null,
  128.  
  129.   // This observer is called once the progress dialog has been "opened"
  130.   _obsPP: 
  131.   {
  132.     observe: function(aSubject, aTopic, aData)
  133.     {
  134.       // delay the print preview to show the content of the progress dialog
  135.       setTimeout(function () { PrintUtils.enterPrintPreview(); }, 0);
  136.     },
  137.  
  138.     QueryInterface : function(iid)
  139.     {
  140.       if (iid.equals(Components.interfaces.nsIObserver) || iid.equals(Components.interfaces.nsISupportsWeakReference))
  141.         return this;   
  142.       throw Components.results.NS_NOINTERFACE;
  143.     }
  144.   },
  145.  
  146.   enterPrintPreview: function ()
  147.   {
  148.     var webBrowserPrint = this.getWebBrowserPrint();
  149.     var printSettings   = this.getPrintSettings();
  150.     try {
  151.       webBrowserPrint.printPreview(printSettings, null, this._webProgressPP.value);
  152.     } catch (e) {
  153.       // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  154.       // causing an exception to be thrown which we catch here.
  155.       // Unfortunately this will also consume helpful failures, so add a
  156.       // dump(e); // if you need to debug
  157.       return;
  158.     }
  159.  
  160.     var printPreviewTB = document.getElementById("print-preview-toolbar");
  161.     if (printPreviewTB) {
  162.       printPreviewTB.updateToolbar();
  163.       return;
  164.     }
  165.  
  166.     // show the toolbar after we go into print preview mode so
  167.     // that we can initialize the toolbar with total num pages
  168.     printPreviewTB = document.createElementNS(XUL_NS, "toolbar");
  169.     printPreviewTB.setAttribute("printpreview", true);
  170.     printPreviewTB.setAttribute("id", "print-preview-toolbar");
  171.  
  172.     getBrowser().parentNode.insertBefore(printPreviewTB, getBrowser());
  173.  
  174.     // Tab browser...
  175.     if ("getStripVisibility" in getBrowser()) {
  176.       this._chromeState.hadTabStrip = getBrowser().getStripVisibility();
  177.       getBrowser().setStripVisibilityTo(false);
  178.     }
  179.  
  180.     // copy the window close handler
  181.     if (document.documentElement.hasAttribute("onclose"))
  182.       this._closeHandlerPP = document.documentElement.getAttribute("onclose");
  183.     else
  184.       this._closeHandlerPP = null;
  185.     document.documentElement.setAttribute("onclose", "PrintUtils.exitPrintPreview(); return false;");
  186.  
  187.     // disable chrome shortcuts...
  188.     window.addEventListener("keypress", this.onKeyPressPP, true);
  189.  
  190.     _content.focus();
  191.     
  192.     // on Enter PP Call back
  193.     if (this._onEnterPP) {
  194.       this._onEnterPP();
  195.       this._onEnterPP = null;
  196.     }
  197.   },
  198.  
  199.   exitPrintPreview: function ()
  200.   {
  201.     window.removeEventListener("keypress", this.onKeyPressPP, true);
  202.  
  203.  
  204.     // restore the old close handler
  205.     document.documentElement.setAttribute("onclose", this._closeHandlerPP);
  206.     this._closeHandlerPP = null;
  207.  
  208.     if ("getStripVisibility" in getBrowser())
  209.       getBrowser().setStripVisibilityTo(this._chromeState.hadTabStrip);
  210.  
  211.     var webBrowserPrint = this.getWebBrowserPrint();
  212.     webBrowserPrint.exitPrintPreview(); 
  213.  
  214.     // remove the print preview toolbar
  215.     var printPreviewTB = document.getElementById("print-preview-toolbar");
  216.     getBrowser().parentNode.removeChild(printPreviewTB);
  217.  
  218.     _content.focus();
  219.  
  220.     // on Exit PP Call back
  221.     if (this._onExitPP) {
  222.       this._onExitPP();
  223.       this._onExitPP = null;
  224.     }
  225.   },
  226.  
  227.   onKeyPressPP: function (aEvent)
  228.   {
  229.     var closeKey;
  230.     try {
  231.       closeKey = document.getElementById("key_close")
  232.                          .getAttribute("key");
  233.       closeKey = aEvent["DOM_VK_"+closeKey];
  234.     } catch (e) {}
  235.     var isModif = aEvent.ctrlKey || aEvent.metaKey;
  236.     // ESC and Ctrl-W exits the PP
  237.     if (aEvent.keyCode == aEvent.DOM_VK_ESCAPE || isModif &&
  238.         (aEvent.charCode == closeKey || aEvent.charCode == closeKey + 32))
  239.       PrintUtils.exitPrintPreview();
  240.     // cancel shortkeys
  241.     if (isModif) {
  242.       aEvent.preventDefault();
  243.       aEvent.stopPropagation();
  244.     }
  245.   }
  246. }
  247.