home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / comm.jar / content / communicator / printing.js < prev    next >
Encoding:
JavaScript  |  2005-07-29  |  5.6 KB  |  153 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. var gPrintSettings = null;
  42. var gPrintSettingsAreGlobal = false;
  43. var gSavePrintSettings = false;
  44.  
  45. function setPrinterDefaultsForSelectedPrinter(aPrintService)
  46. {
  47.   if (gPrintSettings.printerName == "") {
  48.     gPrintSettings.printerName = aPrintService.defaultPrinterName;
  49.   }
  50.  
  51.   // First get any defaults from the printer 
  52.   aPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
  53.  
  54.   // now augment them with any values from last time
  55.   aPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettings.kInitSaveAll);
  56. }
  57.  
  58. function GetPrintSettings()
  59. {
  60.   try {
  61.     if (gPrintSettings == null) {
  62.       var pref = Components.classes["@mozilla.org/preferences-service;1"]
  63.                            .getService(Components.interfaces.nsIPrefBranch);
  64.       if (pref) {
  65.         gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
  66.         gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
  67.       }
  68.  
  69.       var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  70.                                         .getService(Components.interfaces.nsIPrintSettingsService);
  71.       if (gPrintSettingsAreGlobal) {
  72.         gPrintSettings = printService.globalPrintSettings;
  73.         setPrinterDefaultsForSelectedPrinter(printService);
  74.       } else {
  75.         gPrintSettings = printService.newPrintSettings;
  76.       }
  77.     }
  78.   } catch (e) {
  79.     dump("GetPrintSettings() "+e+"\n");
  80.   }
  81.  
  82.   return gPrintSettings;
  83. }
  84.  
  85. function goPageSetup(domwin, printSettings)
  86. {
  87.   try {
  88.     if (printSettings == null) {
  89.       dump("***************** PrintSettings arg is null!");
  90.       return false;
  91.     }
  92.  
  93.     // This code calls the printoptions service to bring up the printoptions
  94.     // dialog.  This will be an xp dialog if the platform did not override
  95.     // the ShowPrintSetupDialog method.
  96.     var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  97.                                              .getService(Components.interfaces.nsIPrintingPromptService);
  98.     printingPromptService.showPageSetup(domwin, printSettings, null);
  99.   } catch(e) {
  100.     // Note: Pressing Cancel gets here too.
  101.  
  102.     return false; 
  103.   }
  104.   return true;
  105. }
  106.  
  107. function NSPrintSetup()
  108. {
  109.   var didOK = false;
  110.   try {
  111.     gPrintSettings = GetPrintSettings();
  112.  
  113.     var webBrowserPrint = null;
  114.     if (_content) {
  115.       webBrowserPrint = _content
  116.         .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  117.         .getInterface(Components.interfaces.nsIWebBrowserPrint);
  118.     }
  119.  
  120.     didOK = goPageSetup(window, gPrintSettings);
  121.     if (didOK) {
  122.       if (webBrowserPrint) {
  123.         if (gPrintSettingsAreGlobal && gSavePrintSettings) {
  124.           var psService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  125.                                             .getService(Components.interfaces.nsIPrintSettingsService);
  126.           psService.savePrintSettingsToPrefs(gPrintSettings, false, gPrintSettings.kInitSaveNativeData);
  127.         }
  128.       }
  129.     }
  130.   } catch (e) {
  131.     dump("NSPrintSetup() " + e + "\n");
  132.   }
  133.   return didOK;
  134. }
  135.  
  136. function NSPrint()
  137. {
  138.   try {
  139.     var webBrowserPrint = _content
  140.           .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  141.           .getInterface(Components.interfaces.nsIWebBrowserPrint);
  142.     if (webBrowserPrint) {
  143.       gPrintSettings = GetPrintSettings();
  144.       webBrowserPrint.print(gPrintSettings, null);
  145.     }
  146.   } catch (e) {
  147.     // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  148.     // causing an exception to be thrown which we catch here.
  149.     // Unfortunately this will also consume helpful failures, so add a
  150.     // dump(e); // if you need to debug
  151.   }
  152. }
  153.