toolkit/components/printing/content/printdialog.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 var dialog;
michael@0 8 var printService = null;
michael@0 9 var gOriginalNumCopies = 1;
michael@0 10
michael@0 11 var paramBlock;
michael@0 12 var gPrefs = null;
michael@0 13 var gPrintSettings = null;
michael@0 14 var gWebBrowserPrint = null;
michael@0 15 var gPrintSetInterface = Components.interfaces.nsIPrintSettings;
michael@0 16 var doDebug = false;
michael@0 17
michael@0 18 //---------------------------------------------------
michael@0 19 function initDialog()
michael@0 20 {
michael@0 21 dialog = new Object;
michael@0 22
michael@0 23 dialog.propertiesButton = document.getElementById("properties");
michael@0 24 dialog.descText = document.getElementById("descText");
michael@0 25
michael@0 26 dialog.printrangeGroup = document.getElementById("printrangeGroup");
michael@0 27 dialog.allpagesRadio = document.getElementById("allpagesRadio");
michael@0 28 dialog.rangeRadio = document.getElementById("rangeRadio");
michael@0 29 dialog.selectionRadio = document.getElementById("selectionRadio");
michael@0 30 dialog.frompageInput = document.getElementById("frompageInput");
michael@0 31 dialog.frompageLabel = document.getElementById("frompageLabel");
michael@0 32 dialog.topageInput = document.getElementById("topageInput");
michael@0 33 dialog.topageLabel = document.getElementById("topageLabel");
michael@0 34
michael@0 35 dialog.numCopiesInput = document.getElementById("numCopiesInput");
michael@0 36
michael@0 37 dialog.printframeGroup = document.getElementById("printframeGroup");
michael@0 38 dialog.aslaidoutRadio = document.getElementById("aslaidoutRadio");
michael@0 39 dialog.selectedframeRadio = document.getElementById("selectedframeRadio");
michael@0 40 dialog.eachframesepRadio = document.getElementById("eachframesepRadio");
michael@0 41 dialog.printframeGroupLabel = document.getElementById("printframeGroupLabel");
michael@0 42
michael@0 43 dialog.fileCheck = document.getElementById("fileCheck");
michael@0 44 dialog.printerLabel = document.getElementById("printerLabel");
michael@0 45 dialog.printerList = document.getElementById("printerList");
michael@0 46
michael@0 47 dialog.printButton = document.documentElement.getButton("accept");
michael@0 48
michael@0 49 // <data> elements
michael@0 50 dialog.printName = document.getElementById("printButton");
michael@0 51 dialog.fpDialog = document.getElementById("fpDialog");
michael@0 52
michael@0 53 dialog.enabled = false;
michael@0 54 }
michael@0 55
michael@0 56 //---------------------------------------------------
michael@0 57 function checkInteger(element)
michael@0 58 {
michael@0 59 var value = element.value;
michael@0 60 if (value && value.length > 0) {
michael@0 61 value = value.replace(/[^0-9]/g,"");
michael@0 62 if (!value) value = "";
michael@0 63 element.value = value;
michael@0 64 }
michael@0 65 if (!value || value < 1 || value > 999)
michael@0 66 dialog.printButton.setAttribute("disabled","true");
michael@0 67 else
michael@0 68 dialog.printButton.removeAttribute("disabled");
michael@0 69 }
michael@0 70
michael@0 71 //---------------------------------------------------
michael@0 72 function stripTrailingWhitespace(element)
michael@0 73 {
michael@0 74 var value = element.value;
michael@0 75 value = value.replace(/\s+$/,"");
michael@0 76 element.value = value;
michael@0 77 }
michael@0 78
michael@0 79 //---------------------------------------------------
michael@0 80 function getPrinterDescription(printerName)
michael@0 81 {
michael@0 82 var s = "";
michael@0 83
michael@0 84 try {
michael@0 85 /* This may not work with non-ASCII test (see bug 235763 comment #16) */
michael@0 86 s = gPrefs.getCharPref("print.printer_" + printerName + ".printer_description")
michael@0 87 } catch(e) {
michael@0 88 }
michael@0 89
michael@0 90 return s;
michael@0 91 }
michael@0 92
michael@0 93 //---------------------------------------------------
michael@0 94 function listElement(aListElement)
michael@0 95 {
michael@0 96 this.listElement = aListElement;
michael@0 97 }
michael@0 98
michael@0 99 listElement.prototype =
michael@0 100 {
michael@0 101 clearList:
michael@0 102 function ()
michael@0 103 {
michael@0 104 // remove the menupopup node child of the menulist.
michael@0 105 var popup = this.listElement.firstChild;
michael@0 106 if (popup) {
michael@0 107 this.listElement.removeChild(popup);
michael@0 108 }
michael@0 109 },
michael@0 110
michael@0 111 appendPrinterNames:
michael@0 112 function (aDataObject)
michael@0 113 {
michael@0 114 if ((null == aDataObject) || !aDataObject.hasMore()) {
michael@0 115 // disable dialog
michael@0 116 this.listElement.setAttribute("value", "");
michael@0 117 this.listElement.setAttribute("label",
michael@0 118 document.getElementById("printingBundle")
michael@0 119 .getString("noprinter"));
michael@0 120
michael@0 121 this.listElement.setAttribute("disabled", "true");
michael@0 122 dialog.printerLabel.setAttribute("disabled","true");
michael@0 123 dialog.propertiesButton.setAttribute("disabled","true");
michael@0 124 dialog.fileCheck.setAttribute("disabled","true");
michael@0 125 dialog.printButton.setAttribute("disabled","true");
michael@0 126 }
michael@0 127 else {
michael@0 128 // build popup menu from printer names
michael@0 129 var list = document.getElementById("printerList");
michael@0 130 do {
michael@0 131 printerNameStr = aDataObject.getNext();
michael@0 132 list.appendItem(printerNameStr, printerNameStr, getPrinterDescription(printerNameStr));
michael@0 133 } while (aDataObject.hasMore());
michael@0 134 this.listElement.removeAttribute("disabled");
michael@0 135 }
michael@0 136 }
michael@0 137 };
michael@0 138
michael@0 139 //---------------------------------------------------
michael@0 140 function getPrinters()
michael@0 141 {
michael@0 142 var selectElement = new listElement(dialog.printerList);
michael@0 143 selectElement.clearList();
michael@0 144
michael@0 145 var printerEnumerator;
michael@0 146 try {
michael@0 147 printerEnumerator =
michael@0 148 Components.classes["@mozilla.org/gfx/printerenumerator;1"]
michael@0 149 .getService(Components.interfaces.nsIPrinterEnumerator)
michael@0 150 .printerNameList;
michael@0 151 } catch(e) { printerEnumerator = null; }
michael@0 152
michael@0 153 selectElement.appendPrinterNames(printerEnumerator);
michael@0 154 selectElement.listElement.value = printService.defaultPrinterName;
michael@0 155
michael@0 156 // make sure we load the prefs for the initially selected printer
michael@0 157 setPrinterDefaultsForSelectedPrinter();
michael@0 158 }
michael@0 159
michael@0 160
michael@0 161 //---------------------------------------------------
michael@0 162 // update gPrintSettings with the defaults for the selected printer
michael@0 163 function setPrinterDefaultsForSelectedPrinter()
michael@0 164 {
michael@0 165 gPrintSettings.printerName = dialog.printerList.value;
michael@0 166
michael@0 167 dialog.descText.value = getPrinterDescription(gPrintSettings.printerName);
michael@0 168
michael@0 169 // First get any defaults from the printer
michael@0 170 printService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
michael@0 171
michael@0 172 // now augment them with any values from last time
michael@0 173 printService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSetInterface.kInitSaveAll);
michael@0 174
michael@0 175 if (doDebug) {
michael@0 176 dump("setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', paperName='"+gPrintSettings.paperName+"'\n");
michael@0 177 }
michael@0 178 }
michael@0 179
michael@0 180 //---------------------------------------------------
michael@0 181 function displayPropertiesDialog()
michael@0 182 {
michael@0 183 gPrintSettings.numCopies = dialog.numCopiesInput.value;
michael@0 184 try {
michael@0 185 var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
michael@0 186 .getService(Components.interfaces.nsIPrintingPromptService);
michael@0 187 if (printingPromptService) {
michael@0 188 printingPromptService.showPrinterProperties(null, dialog.printerList.value, gPrintSettings);
michael@0 189 dialog.numCopiesInput.value = gPrintSettings.numCopies;
michael@0 190 }
michael@0 191 } catch(e) {
michael@0 192 dump("problems getting printingPromptService\n");
michael@0 193 }
michael@0 194 }
michael@0 195
michael@0 196 //---------------------------------------------------
michael@0 197 function doPrintRange(inx)
michael@0 198 {
michael@0 199 if (inx == 1) {
michael@0 200 dialog.frompageInput.removeAttribute("disabled");
michael@0 201 dialog.frompageLabel.removeAttribute("disabled");
michael@0 202 dialog.topageInput.removeAttribute("disabled");
michael@0 203 dialog.topageLabel.removeAttribute("disabled");
michael@0 204 } else {
michael@0 205 dialog.frompageInput.setAttribute("disabled","true");
michael@0 206 dialog.frompageLabel.setAttribute("disabled","true");
michael@0 207 dialog.topageInput.setAttribute("disabled","true");
michael@0 208 dialog.topageLabel.setAttribute("disabled","true");
michael@0 209 }
michael@0 210 }
michael@0 211
michael@0 212 //---------------------------------------------------
michael@0 213 function loadDialog()
michael@0 214 {
michael@0 215 var print_copies = 1;
michael@0 216 var print_selection_radio_enabled = false;
michael@0 217 var print_frametype = gPrintSetInterface.kSelectedFrame;
michael@0 218 var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
michael@0 219 var print_tofile = "";
michael@0 220
michael@0 221 try {
michael@0 222 gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
michael@0 223
michael@0 224 printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"];
michael@0 225 if (printService) {
michael@0 226 printService = printService.getService();
michael@0 227 if (printService) {
michael@0 228 printService = printService.QueryInterface(Components.interfaces.nsIPrintSettingsService);
michael@0 229 }
michael@0 230 }
michael@0 231 } catch(e) {}
michael@0 232
michael@0 233 // Note: getPrinters sets up the PrintToFile control
michael@0 234 getPrinters();
michael@0 235
michael@0 236 if (gPrintSettings) {
michael@0 237 print_tofile = gPrintSettings.printToFile;
michael@0 238 gOriginalNumCopies = gPrintSettings.numCopies;
michael@0 239
michael@0 240 print_copies = gPrintSettings.numCopies;
michael@0 241 print_frametype = gPrintSettings.printFrameType;
michael@0 242 print_howToEnableUI = gPrintSettings.howToEnableFrameUI;
michael@0 243 print_selection_radio_enabled = gPrintSettings.GetPrintOptions(gPrintSetInterface.kEnableSelectionRB);
michael@0 244 }
michael@0 245
michael@0 246 if (doDebug) {
michael@0 247 dump("loadDialog*********************************************\n");
michael@0 248 dump("print_tofile "+print_tofile+"\n");
michael@0 249 dump("print_frame "+print_frametype+"\n");
michael@0 250 dump("print_howToEnableUI "+print_howToEnableUI+"\n");
michael@0 251 dump("selection_radio_enabled "+print_selection_radio_enabled+"\n");
michael@0 252 }
michael@0 253
michael@0 254 dialog.printrangeGroup.selectedItem = dialog.allpagesRadio;
michael@0 255 if (print_selection_radio_enabled) {
michael@0 256 dialog.selectionRadio.removeAttribute("disabled");
michael@0 257 } else {
michael@0 258 dialog.selectionRadio.setAttribute("disabled","true");
michael@0 259 }
michael@0 260 doPrintRange(dialog.rangeRadio.selected);
michael@0 261 dialog.frompageInput.value = 1;
michael@0 262 dialog.topageInput.value = 1;
michael@0 263 dialog.numCopiesInput.value = print_copies;
michael@0 264
michael@0 265 if (doDebug) {
michael@0 266 dump("print_howToEnableUI: "+print_howToEnableUI+"\n");
michael@0 267 }
michael@0 268
michael@0 269 // print frame
michael@0 270 if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAll) {
michael@0 271 dialog.aslaidoutRadio.removeAttribute("disabled");
michael@0 272
michael@0 273 dialog.selectedframeRadio.removeAttribute("disabled");
michael@0 274 dialog.eachframesepRadio.removeAttribute("disabled");
michael@0 275 dialog.printframeGroupLabel.removeAttribute("disabled");
michael@0 276
michael@0 277 // initialize radio group
michael@0 278 dialog.printframeGroup.selectedItem = dialog.selectedframeRadio;
michael@0 279
michael@0 280 } else if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAsIsAndEach) {
michael@0 281 dialog.aslaidoutRadio.removeAttribute("disabled"); //enable
michael@0 282
michael@0 283 dialog.selectedframeRadio.setAttribute("disabled","true"); // disable
michael@0 284 dialog.eachframesepRadio.removeAttribute("disabled"); // enable
michael@0 285 dialog.printframeGroupLabel.removeAttribute("disabled"); // enable
michael@0 286
michael@0 287 // initialize
michael@0 288 dialog.printframeGroup.selectedItem = dialog.eachframesepRadio;
michael@0 289
michael@0 290 } else {
michael@0 291 dialog.aslaidoutRadio.setAttribute("disabled","true");
michael@0 292 dialog.selectedframeRadio.setAttribute("disabled","true");
michael@0 293 dialog.eachframesepRadio.setAttribute("disabled","true");
michael@0 294 dialog.printframeGroupLabel.setAttribute("disabled","true");
michael@0 295 }
michael@0 296
michael@0 297 dialog.printButton.label = dialog.printName.getAttribute("label");
michael@0 298 }
michael@0 299
michael@0 300 //---------------------------------------------------
michael@0 301 function onLoad()
michael@0 302 {
michael@0 303 // Init dialog.
michael@0 304 initDialog();
michael@0 305
michael@0 306 // param[0]: nsIPrintSettings object
michael@0 307 // param[1]: container for return value (1 = print, 0 = cancel)
michael@0 308
michael@0 309 gPrintSettings = window.arguments[0].QueryInterface(gPrintSetInterface);
michael@0 310 gWebBrowserPrint = window.arguments[1].QueryInterface(Components.interfaces.nsIWebBrowserPrint);
michael@0 311 paramBlock = window.arguments[2].QueryInterface(Components.interfaces.nsIDialogParamBlock);
michael@0 312
michael@0 313 // default return value is "cancel"
michael@0 314 paramBlock.SetInt(0, 0);
michael@0 315
michael@0 316 loadDialog();
michael@0 317 }
michael@0 318
michael@0 319 //---------------------------------------------------
michael@0 320 function onAccept()
michael@0 321 {
michael@0 322 if (gPrintSettings != null) {
michael@0 323 var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
michael@0 324
michael@0 325 // save these out so they can be picked up by the device spec
michael@0 326 gPrintSettings.printerName = dialog.printerList.value;
michael@0 327 print_howToEnableUI = gPrintSettings.howToEnableFrameUI;
michael@0 328 gPrintSettings.printToFile = dialog.fileCheck.checked;
michael@0 329
michael@0 330 if (gPrintSettings.printToFile && !chooseFile())
michael@0 331 return false;
michael@0 332
michael@0 333 if (dialog.allpagesRadio.selected) {
michael@0 334 gPrintSettings.printRange = gPrintSetInterface.kRangeAllPages;
michael@0 335 } else if (dialog.rangeRadio.selected) {
michael@0 336 gPrintSettings.printRange = gPrintSetInterface.kRangeSpecifiedPageRange;
michael@0 337 } else if (dialog.selectionRadio.selected) {
michael@0 338 gPrintSettings.printRange = gPrintSetInterface.kRangeSelection;
michael@0 339 }
michael@0 340 gPrintSettings.startPageRange = dialog.frompageInput.value;
michael@0 341 gPrintSettings.endPageRange = dialog.topageInput.value;
michael@0 342 gPrintSettings.numCopies = dialog.numCopiesInput.value;
michael@0 343
michael@0 344 var frametype = gPrintSetInterface.kNoFrames;
michael@0 345 if (print_howToEnableUI != gPrintSetInterface.kFrameEnableNone) {
michael@0 346 if (dialog.aslaidoutRadio.selected) {
michael@0 347 frametype = gPrintSetInterface.kFramesAsIs;
michael@0 348 } else if (dialog.selectedframeRadio.selected) {
michael@0 349 frametype = gPrintSetInterface.kSelectedFrame;
michael@0 350 } else if (dialog.eachframesepRadio.selected) {
michael@0 351 frametype = gPrintSetInterface.kEachFrameSep;
michael@0 352 } else {
michael@0 353 frametype = gPrintSetInterface.kSelectedFrame;
michael@0 354 }
michael@0 355 }
michael@0 356 gPrintSettings.printFrameType = frametype;
michael@0 357 if (doDebug) {
michael@0 358 dump("onAccept*********************************************\n");
michael@0 359 dump("frametype "+frametype+"\n");
michael@0 360 dump("numCopies "+gPrintSettings.numCopies+"\n");
michael@0 361 dump("printRange "+gPrintSettings.printRange+"\n");
michael@0 362 dump("printerName "+gPrintSettings.printerName+"\n");
michael@0 363 dump("startPageRange "+gPrintSettings.startPageRange+"\n");
michael@0 364 dump("endPageRange "+gPrintSettings.endPageRange+"\n");
michael@0 365 dump("printToFile "+gPrintSettings.printToFile+"\n");
michael@0 366 }
michael@0 367 }
michael@0 368
michael@0 369 var saveToPrefs = false;
michael@0 370
michael@0 371 saveToPrefs = gPrefs.getBoolPref("print.save_print_settings");
michael@0 372
michael@0 373 if (saveToPrefs && printService != null) {
michael@0 374 var flags = gPrintSetInterface.kInitSavePaperSize |
michael@0 375 gPrintSetInterface.kInitSaveColorSpace |
michael@0 376 gPrintSetInterface.kInitSaveEdges |
michael@0 377 gPrintSetInterface.kInitSaveInColor |
michael@0 378 gPrintSetInterface.kInitSaveResolutionName |
michael@0 379 gPrintSetInterface.kInitSaveDownloadFonts |
michael@0 380 gPrintSetInterface.kInitSavePrintCommand |
michael@0 381 gPrintSetInterface.kInitSaveShrinkToFit |
michael@0 382 gPrintSetInterface.kInitSaveScaling;
michael@0 383 printService.savePrintSettingsToPrefs(gPrintSettings, true, flags);
michael@0 384 }
michael@0 385
michael@0 386 // set return value to "print"
michael@0 387 if (paramBlock) {
michael@0 388 paramBlock.SetInt(0, 1);
michael@0 389 } else {
michael@0 390 dump("*** FATAL ERROR: No paramBlock\n");
michael@0 391 }
michael@0 392
michael@0 393 return true;
michael@0 394 }
michael@0 395
michael@0 396 //---------------------------------------------------
michael@0 397 function onCancel()
michael@0 398 {
michael@0 399 // set return value to "cancel"
michael@0 400 if (paramBlock) {
michael@0 401 paramBlock.SetInt(0, 0);
michael@0 402 } else {
michael@0 403 dump("*** FATAL ERROR: No paramBlock\n");
michael@0 404 }
michael@0 405
michael@0 406 return true;
michael@0 407 }
michael@0 408
michael@0 409 //---------------------------------------------------
michael@0 410 const nsIFilePicker = Components.interfaces.nsIFilePicker;
michael@0 411 function chooseFile()
michael@0 412 {
michael@0 413 try {
michael@0 414 var fp = Components.classes["@mozilla.org/filepicker;1"]
michael@0 415 .createInstance(nsIFilePicker);
michael@0 416 fp.init(window, dialog.fpDialog.getAttribute("label"), nsIFilePicker.modeSave);
michael@0 417 fp.appendFilters(nsIFilePicker.filterAll);
michael@0 418 if (fp.show() != Components.interfaces.nsIFilePicker.returnCancel &&
michael@0 419 fp.file && fp.file.path) {
michael@0 420 gPrintSettings.toFileName = fp.file.path;
michael@0 421 return true;
michael@0 422 }
michael@0 423 } catch(ex) {
michael@0 424 dump(ex);
michael@0 425 }
michael@0 426
michael@0 427 return false;
michael@0 428 }
michael@0 429

mercurial