1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/printing/content/printPageSetup.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,481 @@ 1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +var gDialog; 1.11 +var paramBlock; 1.12 +var gPrefs = null; 1.13 +var gPrintService = null; 1.14 +var gPrintSettings = null; 1.15 +var gStringBundle = null; 1.16 +var gDoingMetric = false; 1.17 + 1.18 +var gPrintSettingsInterface = Components.interfaces.nsIPrintSettings; 1.19 +var gDoDebug = false; 1.20 + 1.21 +//--------------------------------------------------- 1.22 +function initDialog() 1.23 +{ 1.24 + gDialog = new Object; 1.25 + 1.26 + gDialog.orientation = document.getElementById("orientation"); 1.27 + gDialog.portrait = document.getElementById("portrait"); 1.28 + gDialog.landscape = document.getElementById("landscape"); 1.29 + 1.30 + gDialog.printBG = document.getElementById("printBG"); 1.31 + 1.32 + gDialog.shrinkToFit = document.getElementById("shrinkToFit"); 1.33 + 1.34 + gDialog.marginGroup = document.getElementById("marginGroup"); 1.35 + 1.36 + gDialog.marginPage = document.getElementById("marginPage"); 1.37 + gDialog.marginTop = document.getElementById("marginTop"); 1.38 + gDialog.marginBottom = document.getElementById("marginBottom"); 1.39 + gDialog.marginLeft = document.getElementById("marginLeft"); 1.40 + gDialog.marginRight = document.getElementById("marginRight"); 1.41 + 1.42 + gDialog.topInput = document.getElementById("topInput"); 1.43 + gDialog.bottomInput = document.getElementById("bottomInput"); 1.44 + gDialog.leftInput = document.getElementById("leftInput"); 1.45 + gDialog.rightInput = document.getElementById("rightInput"); 1.46 + 1.47 + gDialog.hLeftOption = document.getElementById("hLeftOption"); 1.48 + gDialog.hCenterOption = document.getElementById("hCenterOption"); 1.49 + gDialog.hRightOption = document.getElementById("hRightOption"); 1.50 + 1.51 + gDialog.fLeftOption = document.getElementById("fLeftOption"); 1.52 + gDialog.fCenterOption = document.getElementById("fCenterOption"); 1.53 + gDialog.fRightOption = document.getElementById("fRightOption"); 1.54 + 1.55 + gDialog.scalingLabel = document.getElementById("scalingInput"); 1.56 + gDialog.scalingInput = document.getElementById("scalingInput"); 1.57 + 1.58 + gDialog.enabled = false; 1.59 + 1.60 + gDialog.strings = new Array; 1.61 + gDialog.strings[ "marginUnits.inches" ] = document.getElementById("marginUnits.inches").childNodes[0].nodeValue; 1.62 + gDialog.strings[ "marginUnits.metric" ] = document.getElementById("marginUnits.metric").childNodes[0].nodeValue; 1.63 + gDialog.strings[ "customPrompt.title" ] = document.getElementById("customPrompt.title").childNodes[0].nodeValue; 1.64 + gDialog.strings[ "customPrompt.prompt" ] = document.getElementById("customPrompt.prompt").childNodes[0].nodeValue; 1.65 + 1.66 +} 1.67 + 1.68 +//--------------------------------------------------- 1.69 +function isListOfPrinterFeaturesAvailable() 1.70 +{ 1.71 + var has_printerfeatures = false; 1.72 + 1.73 + try { 1.74 + has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures"); 1.75 + } catch(ex) { 1.76 + } 1.77 + 1.78 + return has_printerfeatures; 1.79 +} 1.80 + 1.81 +//--------------------------------------------------- 1.82 +function checkDouble(element) 1.83 +{ 1.84 + element.value = element.value.replace(/[^.0-9]/g, ""); 1.85 +} 1.86 + 1.87 +// Theoretical paper width/height. 1.88 +var gPageWidth = 8.5; 1.89 +var gPageHeight = 11.0; 1.90 + 1.91 +//--------------------------------------------------- 1.92 +function setOrientation() 1.93 +{ 1.94 + var selection = gDialog.orientation.selectedItem; 1.95 + 1.96 + var style = "background-color:white;"; 1.97 + if ((selection == gDialog.portrait && gPageWidth > gPageHeight) || 1.98 + (selection == gDialog.landscape && gPageWidth < gPageHeight)) { 1.99 + // Swap width/height. 1.100 + var temp = gPageHeight; 1.101 + gPageHeight = gPageWidth; 1.102 + gPageWidth = temp; 1.103 + } 1.104 + var div = gDoingMetric ? 100 : 10; 1.105 + style += "width:" + gPageWidth/div + unitString() + ";height:" + gPageHeight/div + unitString() + ";"; 1.106 + gDialog.marginPage.setAttribute( "style", style ); 1.107 +} 1.108 + 1.109 +//--------------------------------------------------- 1.110 +function unitString() 1.111 +{ 1.112 + return (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) ? "in" : "mm"; 1.113 +} 1.114 + 1.115 +//--------------------------------------------------- 1.116 +function checkMargin( value, max, other ) 1.117 +{ 1.118 + // Don't draw this margin bigger than permitted. 1.119 + return Math.min(value, max - other.value); 1.120 +} 1.121 + 1.122 +//--------------------------------------------------- 1.123 +function changeMargin( node ) 1.124 +{ 1.125 + // Correct invalid input. 1.126 + checkDouble(node); 1.127 + 1.128 + // Reset the margin height/width for this node. 1.129 + var val = node.value; 1.130 + var nodeToStyle; 1.131 + var attr="width"; 1.132 + if ( node == gDialog.topInput ) { 1.133 + nodeToStyle = gDialog.marginTop; 1.134 + val = checkMargin( val, gPageHeight, gDialog.bottomInput ); 1.135 + attr = "height"; 1.136 + } else if ( node == gDialog.bottomInput ) { 1.137 + nodeToStyle = gDialog.marginBottom; 1.138 + val = checkMargin( val, gPageHeight, gDialog.topInput ); 1.139 + attr = "height"; 1.140 + } else if ( node == gDialog.leftInput ) { 1.141 + nodeToStyle = gDialog.marginLeft; 1.142 + val = checkMargin( val, gPageWidth, gDialog.rightInput ); 1.143 + } else { 1.144 + nodeToStyle = gDialog.marginRight; 1.145 + val = checkMargin( val, gPageWidth, gDialog.leftInput ); 1.146 + } 1.147 + var style = attr + ":" + (val/10) + unitString() + ";"; 1.148 + nodeToStyle.setAttribute( "style", style ); 1.149 +} 1.150 + 1.151 +//--------------------------------------------------- 1.152 +function changeMargins() 1.153 +{ 1.154 + changeMargin( gDialog.topInput ); 1.155 + changeMargin( gDialog.bottomInput ); 1.156 + changeMargin( gDialog.leftInput ); 1.157 + changeMargin( gDialog.rightInput ); 1.158 +} 1.159 + 1.160 +//--------------------------------------------------- 1.161 +function customize( node ) 1.162 +{ 1.163 + // If selection is now "Custom..." then prompt user for custom setting. 1.164 + if ( node.value == 6 ) { 1.165 + var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ] 1.166 + .getService( Components.interfaces.nsIPromptService ); 1.167 + var title = gDialog.strings[ "customPrompt.title" ]; 1.168 + var promptText = gDialog.strings[ "customPrompt.prompt" ]; 1.169 + var result = { value: node.custom }; 1.170 + var ok = prompter.prompt(window, title, promptText, result, null, { value: false } ); 1.171 + if ( ok ) { 1.172 + node.custom = result.value; 1.173 + } 1.174 + } 1.175 +} 1.176 + 1.177 +//--------------------------------------------------- 1.178 +function setHeaderFooter( node, value ) 1.179 +{ 1.180 + node.value= hfValueToId(value); 1.181 + if (node.value == 6) { 1.182 + // Remember current Custom... value. 1.183 + node.custom = value; 1.184 + } else { 1.185 + // Start with empty Custom... value. 1.186 + node.custom = ""; 1.187 + } 1.188 +} 1.189 + 1.190 +var gHFValues = new Array; 1.191 +gHFValues[ "&T" ] = 1; 1.192 +gHFValues[ "&U" ] = 2; 1.193 +gHFValues[ "&D" ] = 3; 1.194 +gHFValues[ "&P" ] = 4; 1.195 +gHFValues[ "&PT" ] = 5; 1.196 + 1.197 +function hfValueToId(val) 1.198 +{ 1.199 + if ( val in gHFValues ) { 1.200 + return gHFValues[val]; 1.201 + } 1.202 + if ( val.length ) { 1.203 + return 6; // Custom... 1.204 + } else { 1.205 + return 0; // --blank-- 1.206 + } 1.207 +} 1.208 + 1.209 +function hfIdToValue(node) 1.210 +{ 1.211 + var result = ""; 1.212 + switch ( parseInt( node.value ) ) { 1.213 + case 0: 1.214 + break; 1.215 + case 1: 1.216 + result = "&T"; 1.217 + break; 1.218 + case 2: 1.219 + result = "&U"; 1.220 + break; 1.221 + case 3: 1.222 + result = "&D"; 1.223 + break; 1.224 + case 4: 1.225 + result = "&P"; 1.226 + break; 1.227 + case 5: 1.228 + result = "&PT"; 1.229 + break; 1.230 + case 6: 1.231 + result = node.custom; 1.232 + break; 1.233 + } 1.234 + return result; 1.235 +} 1.236 + 1.237 +function setPrinterDefaultsForSelectedPrinter() 1.238 +{ 1.239 + if (gPrintSettings.printerName == "") { 1.240 + gPrintSettings.printerName = gPrintService.defaultPrinterName; 1.241 + } 1.242 + 1.243 + // First get any defaults from the printer 1.244 + gPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings); 1.245 + 1.246 + // now augment them with any values from last time 1.247 + gPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettingsInterface.kInitSaveAll); 1.248 + 1.249 + if (gDoDebug) { 1.250 + dump("pagesetup/setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', orientation='"+gPrintSettings.orientation+"'\n"); 1.251 + } 1.252 +} 1.253 + 1.254 +//--------------------------------------------------- 1.255 +function loadDialog() 1.256 +{ 1.257 + var print_orientation = 0; 1.258 + var print_margin_top = 0.5; 1.259 + var print_margin_left = 0.5; 1.260 + var print_margin_bottom = 0.5; 1.261 + var print_margin_right = 0.5; 1.262 + 1.263 + try { 1.264 + gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); 1.265 + 1.266 + gPrintService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]; 1.267 + if (gPrintService) { 1.268 + gPrintService = gPrintService.getService(); 1.269 + if (gPrintService) { 1.270 + gPrintService = gPrintService.QueryInterface(Components.interfaces.nsIPrintSettingsService); 1.271 + } 1.272 + } 1.273 + } catch(ex) { 1.274 + dump("loadDialog: ex="+ex+"\n"); 1.275 + } 1.276 + 1.277 + setPrinterDefaultsForSelectedPrinter(); 1.278 + 1.279 + gDialog.printBG.checked = gPrintSettings.printBGColors || gPrintSettings.printBGImages; 1.280 + 1.281 + gDialog.shrinkToFit.checked = gPrintSettings.shrinkToFit; 1.282 + 1.283 + gDialog.scalingLabel.disabled = gDialog.scalingInput.disabled = gDialog.shrinkToFit.checked; 1.284 + 1.285 + var marginGroupLabel = gDialog.marginGroup.label; 1.286 + if (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) { 1.287 + marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.inches"]); 1.288 + gDoingMetric = false; 1.289 + } else { 1.290 + marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.metric"]); 1.291 + // Also, set global page dimensions for A4 paper, in millimeters (assumes portrait at this point). 1.292 + gPageWidth = 2100; 1.293 + gPageHeight = 2970; 1.294 + gDoingMetric = true; 1.295 + } 1.296 + gDialog.marginGroup.label = marginGroupLabel; 1.297 + 1.298 + print_orientation = gPrintSettings.orientation; 1.299 + print_margin_top = convertMarginInchesToUnits(gPrintSettings.marginTop, gDoingMetric); 1.300 + print_margin_left = convertMarginInchesToUnits(gPrintSettings.marginLeft, gDoingMetric); 1.301 + print_margin_right = convertMarginInchesToUnits(gPrintSettings.marginRight, gDoingMetric); 1.302 + print_margin_bottom = convertMarginInchesToUnits(gPrintSettings.marginBottom, gDoingMetric); 1.303 + 1.304 + if (gDoDebug) { 1.305 + dump("print_orientation "+print_orientation+"\n"); 1.306 + 1.307 + dump("print_margin_top "+print_margin_top+"\n"); 1.308 + dump("print_margin_left "+print_margin_left+"\n"); 1.309 + dump("print_margin_right "+print_margin_right+"\n"); 1.310 + dump("print_margin_bottom "+print_margin_bottom+"\n"); 1.311 + } 1.312 + 1.313 + if (print_orientation == gPrintSettingsInterface.kPortraitOrientation) { 1.314 + gDialog.orientation.selectedItem = gDialog.portrait; 1.315 + } else if (print_orientation == gPrintSettingsInterface.kLandscapeOrientation) { 1.316 + gDialog.orientation.selectedItem = gDialog.landscape; 1.317 + } 1.318 + 1.319 + // Set orientation the first time on a timeout so the dialog sizes to the 1.320 + // maximum height specified in the .xul file. Otherwise, if the user switches 1.321 + // from landscape to portrait, the content grows and the buttons are clipped. 1.322 + setTimeout( setOrientation, 0 ); 1.323 + 1.324 + gDialog.topInput.value = print_margin_top.toFixed(1); 1.325 + gDialog.bottomInput.value = print_margin_bottom.toFixed(1); 1.326 + gDialog.leftInput.value = print_margin_left.toFixed(1); 1.327 + gDialog.rightInput.value = print_margin_right.toFixed(1); 1.328 + changeMargins(); 1.329 + 1.330 + setHeaderFooter( gDialog.hLeftOption, gPrintSettings.headerStrLeft ); 1.331 + setHeaderFooter( gDialog.hCenterOption, gPrintSettings.headerStrCenter ); 1.332 + setHeaderFooter( gDialog.hRightOption, gPrintSettings.headerStrRight ); 1.333 + 1.334 + setHeaderFooter( gDialog.fLeftOption, gPrintSettings.footerStrLeft ); 1.335 + setHeaderFooter( gDialog.fCenterOption, gPrintSettings.footerStrCenter ); 1.336 + setHeaderFooter( gDialog.fRightOption, gPrintSettings.footerStrRight ); 1.337 + 1.338 + gDialog.scalingInput.value = (gPrintSettings.scaling * 100).toFixed(0); 1.339 + 1.340 + // Enable/disable widgets based in the information whether the selected 1.341 + // printer supports the matching feature or not 1.342 + if (isListOfPrinterFeaturesAvailable()) { 1.343 + if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_orientation")) 1.344 + gDialog.orientation.removeAttribute("disabled"); 1.345 + else 1.346 + gDialog.orientation.setAttribute("disabled","true"); 1.347 + } 1.348 + 1.349 + // Give initial focus to the orientation radio group. 1.350 + // Done on a timeout due to to bug 103197. 1.351 + setTimeout( function() { gDialog.orientation.focus(); }, 0 ); 1.352 +} 1.353 + 1.354 +//--------------------------------------------------- 1.355 +function onLoad() 1.356 +{ 1.357 + // Init gDialog. 1.358 + initDialog(); 1.359 + 1.360 + if (window.arguments[0] != null) { 1.361 + gPrintSettings = window.arguments[0].QueryInterface(Components.interfaces.nsIPrintSettings); 1.362 + paramBlock = window.arguments[1].QueryInterface(Components.interfaces.nsIDialogParamBlock); 1.363 + } else if (gDoDebug) { 1.364 + alert("window.arguments[0] == null!"); 1.365 + } 1.366 + 1.367 + // default return value is "cancel" 1.368 + paramBlock.SetInt(0, 0); 1.369 + 1.370 + if (gPrintSettings) { 1.371 + loadDialog(); 1.372 + } else if (gDoDebug) { 1.373 + alert("Could initialize gDialog, PrintSettings is null!"); 1.374 + } 1.375 +} 1.376 + 1.377 +function convertUnitsMarginToInches(aVal, aIsMetric) 1.378 +{ 1.379 + if (aIsMetric) { 1.380 + return aVal / 25.4; 1.381 + } else { 1.382 + return aVal; 1.383 + } 1.384 +} 1.385 + 1.386 +function convertMarginInchesToUnits(aVal, aIsMetric) 1.387 +{ 1.388 + if (aIsMetric) { 1.389 + return aVal * 25.4; 1.390 + } else { 1.391 + return aVal; 1.392 + } 1.393 +} 1.394 + 1.395 +//--------------------------------------------------- 1.396 +function onAccept() 1.397 +{ 1.398 + 1.399 + if (gPrintSettings) { 1.400 + if ( gDialog.orientation.selectedItem == gDialog.portrait ) { 1.401 + gPrintSettings.orientation = gPrintSettingsInterface.kPortraitOrientation; 1.402 + } else { 1.403 + gPrintSettings.orientation = gPrintSettingsInterface.kLandscapeOrientation; 1.404 + } 1.405 + 1.406 + // save these out so they can be picked up by the device spec 1.407 + gPrintSettings.marginTop = convertUnitsMarginToInches(gDialog.topInput.value, gDoingMetric); 1.408 + gPrintSettings.marginLeft = convertUnitsMarginToInches(gDialog.leftInput.value, gDoingMetric); 1.409 + gPrintSettings.marginBottom = convertUnitsMarginToInches(gDialog.bottomInput.value, gDoingMetric); 1.410 + gPrintSettings.marginRight = convertUnitsMarginToInches(gDialog.rightInput.value, gDoingMetric); 1.411 + 1.412 + gPrintSettings.headerStrLeft = hfIdToValue(gDialog.hLeftOption); 1.413 + gPrintSettings.headerStrCenter = hfIdToValue(gDialog.hCenterOption); 1.414 + gPrintSettings.headerStrRight = hfIdToValue(gDialog.hRightOption); 1.415 + 1.416 + gPrintSettings.footerStrLeft = hfIdToValue(gDialog.fLeftOption); 1.417 + gPrintSettings.footerStrCenter = hfIdToValue(gDialog.fCenterOption); 1.418 + gPrintSettings.footerStrRight = hfIdToValue(gDialog.fRightOption); 1.419 + 1.420 + gPrintSettings.printBGColors = gDialog.printBG.checked; 1.421 + gPrintSettings.printBGImages = gDialog.printBG.checked; 1.422 + 1.423 + gPrintSettings.shrinkToFit = gDialog.shrinkToFit.checked; 1.424 + 1.425 + var scaling = document.getElementById("scalingInput").value; 1.426 + if (scaling < 10.0) { 1.427 + scaling = 10.0; 1.428 + } 1.429 + if (scaling > 500.0) { 1.430 + scaling = 500.0; 1.431 + } 1.432 + scaling /= 100.0; 1.433 + gPrintSettings.scaling = scaling; 1.434 + 1.435 + if (gDoDebug) { 1.436 + dump("******* Page Setup Accepting ******\n"); 1.437 + dump("print_margin_top "+gDialog.topInput.value+"\n"); 1.438 + dump("print_margin_left "+gDialog.leftInput.value+"\n"); 1.439 + dump("print_margin_right "+gDialog.bottomInput.value+"\n"); 1.440 + dump("print_margin_bottom "+gDialog.rightInput.value+"\n"); 1.441 + } 1.442 + } 1.443 + 1.444 + // set return value to "ok" 1.445 + if (paramBlock) { 1.446 + paramBlock.SetInt(0, 1); 1.447 + } else { 1.448 + dump("*** FATAL ERROR: No paramBlock\n"); 1.449 + } 1.450 + 1.451 + var flags = gPrintSettingsInterface.kInitSaveMargins | 1.452 + gPrintSettingsInterface.kInitSaveHeaderLeft | 1.453 + gPrintSettingsInterface.kInitSaveHeaderCenter | 1.454 + gPrintSettingsInterface.kInitSaveHeaderRight | 1.455 + gPrintSettingsInterface.kInitSaveFooterLeft | 1.456 + gPrintSettingsInterface.kInitSaveFooterCenter | 1.457 + gPrintSettingsInterface.kInitSaveFooterRight | 1.458 + gPrintSettingsInterface.kInitSaveBGColors | 1.459 + gPrintSettingsInterface.kInitSaveBGImages | 1.460 + gPrintSettingsInterface.kInitSaveInColor | 1.461 + gPrintSettingsInterface.kInitSaveReversed | 1.462 + gPrintSettingsInterface.kInitSaveOrientation | 1.463 + gPrintSettingsInterface.kInitSaveOddEvenPages | 1.464 + gPrintSettingsInterface.kInitSaveShrinkToFit | 1.465 + gPrintSettingsInterface.kInitSaveScaling; 1.466 + 1.467 + gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags); 1.468 + 1.469 + return true; 1.470 +} 1.471 + 1.472 +//--------------------------------------------------- 1.473 +function onCancel() 1.474 +{ 1.475 + // set return value to "cancel" 1.476 + if (paramBlock) { 1.477 + paramBlock.SetInt(0, 0); 1.478 + } else { 1.479 + dump("*** FATAL ERROR: No paramBlock\n"); 1.480 + } 1.481 + 1.482 + return true; 1.483 +} 1.484 +