1 # -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
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
15 # The Original Code is mozilla.org printing front-end.
17 # The Initial Developer of the Original Code is
18 # Netscape Communications Corporation.
19 # Portions created by the Initial Developer are Copyright (C) 2002
20 # the Initial Developer. All Rights Reserved.
23 # Masaki Katakai <katakai@japan.sun.com>
24 # Jessica Blanco <jblanco@us.ibm.com>
25 # Asko Tontti <atontti@cc.hut.fi>
26 # Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
27 # Peter Weilbacher <mozilla@weilbacher.org>
29 # Alternatively, the contents of this file may be used under the terms of
30 # either the GNU General Public License Version 2 or later (the "GPL"), or
31 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
32 # in which case the provisions of the GPL or the LGPL are applicable instead
33 # of those above. If you wish to allow use of your version of this file only
34 # under the terms of either the GPL or the LGPL, and not to allow others to
35 # use your version of this file under the terms of the MPL, indicate your
36 # decision by deleting the provisions above and replace them with the notice
37 # and other provisions required by the GPL or the LGPL. If you do not delete
38 # the provisions above, a recipient may use your version of this file under
39 # the terms of any one of the MPL, the GPL or the LGPL.
41 # ***** END LICENSE BLOCK *****
44 var printService = null;
45 var gOriginalNumCopies = 1;
49 var gPrintSettings = null;
50 var gWebBrowserPrint = null;
51 var gPrintSetInterface = Components.interfaces.nsIPrintSettings;
54 //---------------------------------------------------
59 dialog.propertiesButton = document.getElementById("properties");
60 dialog.descText = document.getElementById("descText");
62 dialog.printrangeGroup = document.getElementById("printrangeGroup");
63 dialog.allpagesRadio = document.getElementById("allpagesRadio");
64 dialog.rangeRadio = document.getElementById("rangeRadio");
65 dialog.selectionRadio = document.getElementById("selectionRadio");
66 dialog.frompageInput = document.getElementById("frompageInput");
67 dialog.frompageLabel = document.getElementById("frompageLabel");
68 dialog.topageInput = document.getElementById("topageInput");
69 dialog.topageLabel = document.getElementById("topageLabel");
71 dialog.numCopiesInput = document.getElementById("numCopiesInput");
73 dialog.printframeGroup = document.getElementById("printframeGroup");
74 dialog.aslaidoutRadio = document.getElementById("aslaidoutRadio");
75 dialog.selectedframeRadio = document.getElementById("selectedframeRadio");
76 dialog.eachframesepRadio = document.getElementById("eachframesepRadio");
77 dialog.printframeGroupLabel = document.getElementById("printframeGroupLabel");
79 dialog.fileCheck = document.getElementById("fileCheck");
80 dialog.printerLabel = document.getElementById("printerLabel");
81 dialog.printerList = document.getElementById("printerList");
83 dialog.printButton = document.documentElement.getButton("accept");
86 dialog.printName = document.getElementById("printButton");
87 dialog.fpDialog = document.getElementById("fpDialog");
89 dialog.enabled = false;
92 //---------------------------------------------------
93 function checkInteger(element)
95 var value = element.value;
96 if (value && value.length > 0) {
97 value = value.replace(/[^0-9]/g,"");
98 if (!value) value = "";
99 element.value = value;
101 if (!value || value < 1 || value > 999)
102 dialog.printButton.setAttribute("disabled","true");
104 dialog.printButton.removeAttribute("disabled");
107 //---------------------------------------------------
108 function stripTrailingWhitespace(element)
110 var value = element.value;
111 value = value.replace(/\s+$/,"");
112 element.value = value;
115 //---------------------------------------------------
116 function getPrinterDescription(printerName)
121 /* This may not work with non-ASCII test (see bug 235763 comment #16) */
122 s = gPrefs.getCharPref("print.printer_" + printerName + ".printer_description")
129 //---------------------------------------------------
130 function listElement(aListElement)
132 this.listElement = aListElement;
135 listElement.prototype =
140 // remove the menupopup node child of the menulist.
141 var popup = this.listElement.firstChild;
143 this.listElement.removeChild(popup);
148 function (aDataObject)
150 if ((null == aDataObject) || !aDataObject.hasMore()) {
152 this.listElement.setAttribute("value", "");
153 this.listElement.setAttribute("label",
154 document.getElementById("printingBundle")
155 .getString("noprinter"));
157 this.listElement.setAttribute("disabled", "true");
158 dialog.printerLabel.setAttribute("disabled","true");
159 dialog.propertiesButton.setAttribute("disabled","true");
160 dialog.fileCheck.setAttribute("disabled","true");
161 dialog.printButton.setAttribute("disabled","true");
164 // build popup menu from printer names
165 var list = document.getElementById("printerList");
167 printerNameStr = aDataObject.getNext();
168 list.appendItem(printerNameStr, printerNameStr, getPrinterDescription(printerNameStr));
169 } while (aDataObject.hasMore());
170 this.listElement.removeAttribute("disabled");
175 //---------------------------------------------------
176 function getPrinters()
178 var selectElement = new listElement(dialog.printerList);
179 selectElement.clearList();
181 var printerEnumerator;
184 Components.classes["@mozilla.org/gfx/printerenumerator;1"]
185 .getService(Components.interfaces.nsIPrinterEnumerator)
187 } catch(e) { printerEnumerator = null; }
189 selectElement.appendPrinterNames(printerEnumerator);
190 selectElement.listElement.value = printService.defaultPrinterName;
192 // make sure we load the prefs for the initially selected printer
193 setPrinterDefaultsForSelectedPrinter();
197 //---------------------------------------------------
198 // update gPrintSettings with the defaults for the selected printer
199 function setPrinterDefaultsForSelectedPrinter()
201 gPrintSettings.printerName = dialog.printerList.value;
203 dialog.descText.value = getPrinterDescription(gPrintSettings.printerName);
205 // First get any defaults from the printer
206 printService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
208 // now augment them with any values from last time
209 printService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSetInterface.kInitSaveAll);
212 dump("setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', paperName='"+gPrintSettings.paperName+"'\n");
216 //---------------------------------------------------
217 function displayPropertiesDialog()
219 gPrintSettings.numCopies = dialog.numCopiesInput.value;
221 var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
222 .getService(Components.interfaces.nsIPrintingPromptService);
223 if (printingPromptService) {
224 printingPromptService.showPrinterProperties(null, dialog.printerList.value, gPrintSettings);
225 dialog.numCopiesInput.value = gPrintSettings.numCopies;
228 dump("problems getting printingPromptService\n");
232 //---------------------------------------------------
233 function doPrintRange(inx)
236 dialog.frompageInput.removeAttribute("disabled");
237 dialog.frompageLabel.removeAttribute("disabled");
238 dialog.topageInput.removeAttribute("disabled");
239 dialog.topageLabel.removeAttribute("disabled");
241 dialog.frompageInput.setAttribute("disabled","true");
242 dialog.frompageLabel.setAttribute("disabled","true");
243 dialog.topageInput.setAttribute("disabled","true");
244 dialog.topageLabel.setAttribute("disabled","true");
248 //---------------------------------------------------
249 function loadDialog()
251 var print_copies = 1;
252 var print_selection_radio_enabled = false;
253 var print_frametype = gPrintSetInterface.kSelectedFrame;
254 var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
255 var print_tofile = "";
258 gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
260 printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"];
262 printService = printService.getService();
264 printService = printService.QueryInterface(Components.interfaces.nsIPrintSettingsService);
269 // Note: getPrinters sets up the PrintToFile control
272 if (gPrintSettings) {
273 print_tofile = gPrintSettings.printToFile;
274 gOriginalNumCopies = gPrintSettings.numCopies;
276 print_copies = gPrintSettings.numCopies;
277 print_frametype = gPrintSettings.printFrameType;
278 print_howToEnableUI = gPrintSettings.howToEnableFrameUI;
279 print_selection_radio_enabled = gPrintSettings.GetPrintOptions(gPrintSetInterface.kEnableSelectionRB);
283 dump("loadDialog*********************************************\n");
284 dump("print_tofile "+print_tofile+"\n");
285 dump("print_frame "+print_frametype+"\n");
286 dump("print_howToEnableUI "+print_howToEnableUI+"\n");
287 dump("selection_radio_enabled "+print_selection_radio_enabled+"\n");
290 dialog.printrangeGroup.selectedItem = dialog.allpagesRadio;
291 if (print_selection_radio_enabled) {
292 dialog.selectionRadio.removeAttribute("disabled");
294 dialog.selectionRadio.setAttribute("disabled","true");
296 doPrintRange(dialog.rangeRadio.selected);
297 dialog.frompageInput.value = 1;
298 dialog.topageInput.value = 1;
299 dialog.numCopiesInput.value = print_copies;
302 dump("print_howToEnableUI: "+print_howToEnableUI+"\n");
306 if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAll) {
307 dialog.aslaidoutRadio.removeAttribute("disabled");
309 dialog.selectedframeRadio.removeAttribute("disabled");
310 dialog.eachframesepRadio.removeAttribute("disabled");
311 dialog.printframeGroupLabel.removeAttribute("disabled");
313 // initialize radio group
314 dialog.printframeGroup.selectedItem = dialog.selectedframeRadio;
316 } else if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAsIsAndEach) {
317 dialog.aslaidoutRadio.removeAttribute("disabled"); //enable
319 dialog.selectedframeRadio.setAttribute("disabled","true"); // disable
320 dialog.eachframesepRadio.removeAttribute("disabled"); // enable
321 dialog.printframeGroupLabel.removeAttribute("disabled"); // enable
324 dialog.printframeGroup.selectedItem = dialog.eachframesepRadio;
327 dialog.aslaidoutRadio.setAttribute("disabled","true");
328 dialog.selectedframeRadio.setAttribute("disabled","true");
329 dialog.eachframesepRadio.setAttribute("disabled","true");
330 dialog.printframeGroupLabel.setAttribute("disabled","true");
333 dialog.printButton.label = dialog.printName.getAttribute("label");
336 //---------------------------------------------------
342 // param[0]: nsIPrintSettings object
343 // param[1]: container for return value (1 = print, 0 = cancel)
345 gPrintSettings = window.arguments[0].QueryInterface(gPrintSetInterface);
346 gWebBrowserPrint = window.arguments[1].QueryInterface(Components.interfaces.nsIWebBrowserPrint);
347 paramBlock = window.arguments[2].QueryInterface(Components.interfaces.nsIDialogParamBlock);
349 // default return value is "cancel"
350 paramBlock.SetInt(0, 0);
355 //---------------------------------------------------
358 if (gPrintSettings != null) {
359 var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
361 // save these out so they can be picked up by the device spec
362 gPrintSettings.printerName = dialog.printerList.value;
363 print_howToEnableUI = gPrintSettings.howToEnableFrameUI;
364 gPrintSettings.printToFile = dialog.fileCheck.checked;
366 if (gPrintSettings.printToFile && !chooseFile())
369 if (dialog.allpagesRadio.selected) {
370 gPrintSettings.printRange = gPrintSetInterface.kRangeAllPages;
371 } else if (dialog.rangeRadio.selected) {
372 gPrintSettings.printRange = gPrintSetInterface.kRangeSpecifiedPageRange;
373 } else if (dialog.selectionRadio.selected) {
374 gPrintSettings.printRange = gPrintSetInterface.kRangeSelection;
376 gPrintSettings.startPageRange = dialog.frompageInput.value;
377 gPrintSettings.endPageRange = dialog.topageInput.value;
378 gPrintSettings.numCopies = dialog.numCopiesInput.value;
380 var frametype = gPrintSetInterface.kNoFrames;
381 if (print_howToEnableUI != gPrintSetInterface.kFrameEnableNone) {
382 if (dialog.aslaidoutRadio.selected) {
383 frametype = gPrintSetInterface.kFramesAsIs;
384 } else if (dialog.selectedframeRadio.selected) {
385 frametype = gPrintSetInterface.kSelectedFrame;
386 } else if (dialog.eachframesepRadio.selected) {
387 frametype = gPrintSetInterface.kEachFrameSep;
389 frametype = gPrintSetInterface.kSelectedFrame;
392 gPrintSettings.printFrameType = frametype;
394 dump("onAccept*********************************************\n");
395 dump("frametype "+frametype+"\n");
396 dump("numCopies "+gPrintSettings.numCopies+"\n");
397 dump("printRange "+gPrintSettings.printRange+"\n");
398 dump("printerName "+gPrintSettings.printerName+"\n");
399 dump("startPageRange "+gPrintSettings.startPageRange+"\n");
400 dump("endPageRange "+gPrintSettings.endPageRange+"\n");
401 dump("printToFile "+gPrintSettings.printToFile+"\n");
405 var saveToPrefs = false;
407 saveToPrefs = gPrefs.getBoolPref("print.save_print_settings");
409 if (saveToPrefs && printService != null) {
410 var flags = gPrintSetInterface.kInitSavePaperSize |
411 gPrintSetInterface.kInitSaveColorSpace |
412 gPrintSetInterface.kInitSaveEdges |
413 gPrintSetInterface.kInitSaveInColor |
414 gPrintSetInterface.kInitSaveResolutionName |
415 gPrintSetInterface.kInitSaveDownloadFonts |
416 gPrintSetInterface.kInitSavePrintCommand |
417 gPrintSetInterface.kInitSaveShrinkToFit |
418 gPrintSetInterface.kInitSaveScaling;
419 printService.savePrintSettingsToPrefs(gPrintSettings, true, flags);
422 // set return value to "print"
424 paramBlock.SetInt(0, 1);
426 dump("*** FATAL ERROR: No paramBlock\n");
432 //---------------------------------------------------
435 // set return value to "cancel"
437 paramBlock.SetInt(0, 0);
439 dump("*** FATAL ERROR: No paramBlock\n");
445 //---------------------------------------------------
446 const nsIFilePicker = Components.interfaces.nsIFilePicker;
447 function chooseFile()
450 var fp = Components.classes["@mozilla.org/filepicker;1"]
451 .createInstance(nsIFilePicker);
452 fp.init(window, dialog.fpDialog.getAttribute("label"), nsIFilePicker.modeSave);
453 fp.appendFilters(nsIFilePicker.filterAll);
454 if (fp.show() != Components.interfaces.nsIFilePicker.returnCancel &&
455 fp.file && fp.file.path) {
456 gPrintSettings.toFileName = fp.file.path;