michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var gDialog; michael@0: var paramBlock; michael@0: var gPrefs = null; michael@0: var gPrintService = null; michael@0: var gPrintSettings = null; michael@0: var gStringBundle = null; michael@0: var gDoingMetric = false; michael@0: michael@0: var gPrintSettingsInterface = Components.interfaces.nsIPrintSettings; michael@0: var gDoDebug = false; michael@0: michael@0: //--------------------------------------------------- michael@0: function initDialog() michael@0: { michael@0: gDialog = new Object; michael@0: michael@0: gDialog.orientation = document.getElementById("orientation"); michael@0: gDialog.portrait = document.getElementById("portrait"); michael@0: gDialog.landscape = document.getElementById("landscape"); michael@0: michael@0: gDialog.printBG = document.getElementById("printBG"); michael@0: michael@0: gDialog.shrinkToFit = document.getElementById("shrinkToFit"); michael@0: michael@0: gDialog.marginGroup = document.getElementById("marginGroup"); michael@0: michael@0: gDialog.marginPage = document.getElementById("marginPage"); michael@0: gDialog.marginTop = document.getElementById("marginTop"); michael@0: gDialog.marginBottom = document.getElementById("marginBottom"); michael@0: gDialog.marginLeft = document.getElementById("marginLeft"); michael@0: gDialog.marginRight = document.getElementById("marginRight"); michael@0: michael@0: gDialog.topInput = document.getElementById("topInput"); michael@0: gDialog.bottomInput = document.getElementById("bottomInput"); michael@0: gDialog.leftInput = document.getElementById("leftInput"); michael@0: gDialog.rightInput = document.getElementById("rightInput"); michael@0: michael@0: gDialog.hLeftOption = document.getElementById("hLeftOption"); michael@0: gDialog.hCenterOption = document.getElementById("hCenterOption"); michael@0: gDialog.hRightOption = document.getElementById("hRightOption"); michael@0: michael@0: gDialog.fLeftOption = document.getElementById("fLeftOption"); michael@0: gDialog.fCenterOption = document.getElementById("fCenterOption"); michael@0: gDialog.fRightOption = document.getElementById("fRightOption"); michael@0: michael@0: gDialog.scalingLabel = document.getElementById("scalingInput"); michael@0: gDialog.scalingInput = document.getElementById("scalingInput"); michael@0: michael@0: gDialog.enabled = false; michael@0: michael@0: gDialog.strings = new Array; michael@0: gDialog.strings[ "marginUnits.inches" ] = document.getElementById("marginUnits.inches").childNodes[0].nodeValue; michael@0: gDialog.strings[ "marginUnits.metric" ] = document.getElementById("marginUnits.metric").childNodes[0].nodeValue; michael@0: gDialog.strings[ "customPrompt.title" ] = document.getElementById("customPrompt.title").childNodes[0].nodeValue; michael@0: gDialog.strings[ "customPrompt.prompt" ] = document.getElementById("customPrompt.prompt").childNodes[0].nodeValue; michael@0: michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function isListOfPrinterFeaturesAvailable() michael@0: { michael@0: var has_printerfeatures = false; michael@0: michael@0: try { michael@0: has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures"); michael@0: } catch(ex) { michael@0: } michael@0: michael@0: return has_printerfeatures; michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function checkDouble(element) michael@0: { michael@0: element.value = element.value.replace(/[^.0-9]/g, ""); michael@0: } michael@0: michael@0: // Theoretical paper width/height. michael@0: var gPageWidth = 8.5; michael@0: var gPageHeight = 11.0; michael@0: michael@0: //--------------------------------------------------- michael@0: function setOrientation() michael@0: { michael@0: var selection = gDialog.orientation.selectedItem; michael@0: michael@0: var style = "background-color:white;"; michael@0: if ((selection == gDialog.portrait && gPageWidth > gPageHeight) || michael@0: (selection == gDialog.landscape && gPageWidth < gPageHeight)) { michael@0: // Swap width/height. michael@0: var temp = gPageHeight; michael@0: gPageHeight = gPageWidth; michael@0: gPageWidth = temp; michael@0: } michael@0: var div = gDoingMetric ? 100 : 10; michael@0: style += "width:" + gPageWidth/div + unitString() + ";height:" + gPageHeight/div + unitString() + ";"; michael@0: gDialog.marginPage.setAttribute( "style", style ); michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function unitString() michael@0: { michael@0: return (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) ? "in" : "mm"; michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function checkMargin( value, max, other ) michael@0: { michael@0: // Don't draw this margin bigger than permitted. michael@0: return Math.min(value, max - other.value); michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function changeMargin( node ) michael@0: { michael@0: // Correct invalid input. michael@0: checkDouble(node); michael@0: michael@0: // Reset the margin height/width for this node. michael@0: var val = node.value; michael@0: var nodeToStyle; michael@0: var attr="width"; michael@0: if ( node == gDialog.topInput ) { michael@0: nodeToStyle = gDialog.marginTop; michael@0: val = checkMargin( val, gPageHeight, gDialog.bottomInput ); michael@0: attr = "height"; michael@0: } else if ( node == gDialog.bottomInput ) { michael@0: nodeToStyle = gDialog.marginBottom; michael@0: val = checkMargin( val, gPageHeight, gDialog.topInput ); michael@0: attr = "height"; michael@0: } else if ( node == gDialog.leftInput ) { michael@0: nodeToStyle = gDialog.marginLeft; michael@0: val = checkMargin( val, gPageWidth, gDialog.rightInput ); michael@0: } else { michael@0: nodeToStyle = gDialog.marginRight; michael@0: val = checkMargin( val, gPageWidth, gDialog.leftInput ); michael@0: } michael@0: var style = attr + ":" + (val/10) + unitString() + ";"; michael@0: nodeToStyle.setAttribute( "style", style ); michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function changeMargins() michael@0: { michael@0: changeMargin( gDialog.topInput ); michael@0: changeMargin( gDialog.bottomInput ); michael@0: changeMargin( gDialog.leftInput ); michael@0: changeMargin( gDialog.rightInput ); michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function customize( node ) michael@0: { michael@0: // If selection is now "Custom..." then prompt user for custom setting. michael@0: if ( node.value == 6 ) { michael@0: var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ] michael@0: .getService( Components.interfaces.nsIPromptService ); michael@0: var title = gDialog.strings[ "customPrompt.title" ]; michael@0: var promptText = gDialog.strings[ "customPrompt.prompt" ]; michael@0: var result = { value: node.custom }; michael@0: var ok = prompter.prompt(window, title, promptText, result, null, { value: false } ); michael@0: if ( ok ) { michael@0: node.custom = result.value; michael@0: } michael@0: } michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function setHeaderFooter( node, value ) michael@0: { michael@0: node.value= hfValueToId(value); michael@0: if (node.value == 6) { michael@0: // Remember current Custom... value. michael@0: node.custom = value; michael@0: } else { michael@0: // Start with empty Custom... value. michael@0: node.custom = ""; michael@0: } michael@0: } michael@0: michael@0: var gHFValues = new Array; michael@0: gHFValues[ "&T" ] = 1; michael@0: gHFValues[ "&U" ] = 2; michael@0: gHFValues[ "&D" ] = 3; michael@0: gHFValues[ "&P" ] = 4; michael@0: gHFValues[ "&PT" ] = 5; michael@0: michael@0: function hfValueToId(val) michael@0: { michael@0: if ( val in gHFValues ) { michael@0: return gHFValues[val]; michael@0: } michael@0: if ( val.length ) { michael@0: return 6; // Custom... michael@0: } else { michael@0: return 0; // --blank-- michael@0: } michael@0: } michael@0: michael@0: function hfIdToValue(node) michael@0: { michael@0: var result = ""; michael@0: switch ( parseInt( node.value ) ) { michael@0: case 0: michael@0: break; michael@0: case 1: michael@0: result = "&T"; michael@0: break; michael@0: case 2: michael@0: result = "&U"; michael@0: break; michael@0: case 3: michael@0: result = "&D"; michael@0: break; michael@0: case 4: michael@0: result = "&P"; michael@0: break; michael@0: case 5: michael@0: result = "&PT"; michael@0: break; michael@0: case 6: michael@0: result = node.custom; michael@0: break; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: function setPrinterDefaultsForSelectedPrinter() michael@0: { michael@0: if (gPrintSettings.printerName == "") { michael@0: gPrintSettings.printerName = gPrintService.defaultPrinterName; michael@0: } michael@0: michael@0: // First get any defaults from the printer michael@0: gPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings); michael@0: michael@0: // now augment them with any values from last time michael@0: gPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettingsInterface.kInitSaveAll); michael@0: michael@0: if (gDoDebug) { michael@0: dump("pagesetup/setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', orientation='"+gPrintSettings.orientation+"'\n"); michael@0: } michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function loadDialog() michael@0: { michael@0: var print_orientation = 0; michael@0: var print_margin_top = 0.5; michael@0: var print_margin_left = 0.5; michael@0: var print_margin_bottom = 0.5; michael@0: var print_margin_right = 0.5; michael@0: michael@0: try { michael@0: gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); michael@0: michael@0: gPrintService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]; michael@0: if (gPrintService) { michael@0: gPrintService = gPrintService.getService(); michael@0: if (gPrintService) { michael@0: gPrintService = gPrintService.QueryInterface(Components.interfaces.nsIPrintSettingsService); michael@0: } michael@0: } michael@0: } catch(ex) { michael@0: dump("loadDialog: ex="+ex+"\n"); michael@0: } michael@0: michael@0: setPrinterDefaultsForSelectedPrinter(); michael@0: michael@0: gDialog.printBG.checked = gPrintSettings.printBGColors || gPrintSettings.printBGImages; michael@0: michael@0: gDialog.shrinkToFit.checked = gPrintSettings.shrinkToFit; michael@0: michael@0: gDialog.scalingLabel.disabled = gDialog.scalingInput.disabled = gDialog.shrinkToFit.checked; michael@0: michael@0: var marginGroupLabel = gDialog.marginGroup.label; michael@0: if (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) { michael@0: marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.inches"]); michael@0: gDoingMetric = false; michael@0: } else { michael@0: marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.metric"]); michael@0: // Also, set global page dimensions for A4 paper, in millimeters (assumes portrait at this point). michael@0: gPageWidth = 2100; michael@0: gPageHeight = 2970; michael@0: gDoingMetric = true; michael@0: } michael@0: gDialog.marginGroup.label = marginGroupLabel; michael@0: michael@0: print_orientation = gPrintSettings.orientation; michael@0: print_margin_top = convertMarginInchesToUnits(gPrintSettings.marginTop, gDoingMetric); michael@0: print_margin_left = convertMarginInchesToUnits(gPrintSettings.marginLeft, gDoingMetric); michael@0: print_margin_right = convertMarginInchesToUnits(gPrintSettings.marginRight, gDoingMetric); michael@0: print_margin_bottom = convertMarginInchesToUnits(gPrintSettings.marginBottom, gDoingMetric); michael@0: michael@0: if (gDoDebug) { michael@0: dump("print_orientation "+print_orientation+"\n"); michael@0: michael@0: dump("print_margin_top "+print_margin_top+"\n"); michael@0: dump("print_margin_left "+print_margin_left+"\n"); michael@0: dump("print_margin_right "+print_margin_right+"\n"); michael@0: dump("print_margin_bottom "+print_margin_bottom+"\n"); michael@0: } michael@0: michael@0: if (print_orientation == gPrintSettingsInterface.kPortraitOrientation) { michael@0: gDialog.orientation.selectedItem = gDialog.portrait; michael@0: } else if (print_orientation == gPrintSettingsInterface.kLandscapeOrientation) { michael@0: gDialog.orientation.selectedItem = gDialog.landscape; michael@0: } michael@0: michael@0: // Set orientation the first time on a timeout so the dialog sizes to the michael@0: // maximum height specified in the .xul file. Otherwise, if the user switches michael@0: // from landscape to portrait, the content grows and the buttons are clipped. michael@0: setTimeout( setOrientation, 0 ); michael@0: michael@0: gDialog.topInput.value = print_margin_top.toFixed(1); michael@0: gDialog.bottomInput.value = print_margin_bottom.toFixed(1); michael@0: gDialog.leftInput.value = print_margin_left.toFixed(1); michael@0: gDialog.rightInput.value = print_margin_right.toFixed(1); michael@0: changeMargins(); michael@0: michael@0: setHeaderFooter( gDialog.hLeftOption, gPrintSettings.headerStrLeft ); michael@0: setHeaderFooter( gDialog.hCenterOption, gPrintSettings.headerStrCenter ); michael@0: setHeaderFooter( gDialog.hRightOption, gPrintSettings.headerStrRight ); michael@0: michael@0: setHeaderFooter( gDialog.fLeftOption, gPrintSettings.footerStrLeft ); michael@0: setHeaderFooter( gDialog.fCenterOption, gPrintSettings.footerStrCenter ); michael@0: setHeaderFooter( gDialog.fRightOption, gPrintSettings.footerStrRight ); michael@0: michael@0: gDialog.scalingInput.value = (gPrintSettings.scaling * 100).toFixed(0); michael@0: michael@0: // Enable/disable widgets based in the information whether the selected michael@0: // printer supports the matching feature or not michael@0: if (isListOfPrinterFeaturesAvailable()) { michael@0: if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_orientation")) michael@0: gDialog.orientation.removeAttribute("disabled"); michael@0: else michael@0: gDialog.orientation.setAttribute("disabled","true"); michael@0: } michael@0: michael@0: // Give initial focus to the orientation radio group. michael@0: // Done on a timeout due to to bug 103197. michael@0: setTimeout( function() { gDialog.orientation.focus(); }, 0 ); michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function onLoad() michael@0: { michael@0: // Init gDialog. michael@0: initDialog(); michael@0: michael@0: if (window.arguments[0] != null) { michael@0: gPrintSettings = window.arguments[0].QueryInterface(Components.interfaces.nsIPrintSettings); michael@0: paramBlock = window.arguments[1].QueryInterface(Components.interfaces.nsIDialogParamBlock); michael@0: } else if (gDoDebug) { michael@0: alert("window.arguments[0] == null!"); michael@0: } michael@0: michael@0: // default return value is "cancel" michael@0: paramBlock.SetInt(0, 0); michael@0: michael@0: if (gPrintSettings) { michael@0: loadDialog(); michael@0: } else if (gDoDebug) { michael@0: alert("Could initialize gDialog, PrintSettings is null!"); michael@0: } michael@0: } michael@0: michael@0: function convertUnitsMarginToInches(aVal, aIsMetric) michael@0: { michael@0: if (aIsMetric) { michael@0: return aVal / 25.4; michael@0: } else { michael@0: return aVal; michael@0: } michael@0: } michael@0: michael@0: function convertMarginInchesToUnits(aVal, aIsMetric) michael@0: { michael@0: if (aIsMetric) { michael@0: return aVal * 25.4; michael@0: } else { michael@0: return aVal; michael@0: } michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function onAccept() michael@0: { michael@0: michael@0: if (gPrintSettings) { michael@0: if ( gDialog.orientation.selectedItem == gDialog.portrait ) { michael@0: gPrintSettings.orientation = gPrintSettingsInterface.kPortraitOrientation; michael@0: } else { michael@0: gPrintSettings.orientation = gPrintSettingsInterface.kLandscapeOrientation; michael@0: } michael@0: michael@0: // save these out so they can be picked up by the device spec michael@0: gPrintSettings.marginTop = convertUnitsMarginToInches(gDialog.topInput.value, gDoingMetric); michael@0: gPrintSettings.marginLeft = convertUnitsMarginToInches(gDialog.leftInput.value, gDoingMetric); michael@0: gPrintSettings.marginBottom = convertUnitsMarginToInches(gDialog.bottomInput.value, gDoingMetric); michael@0: gPrintSettings.marginRight = convertUnitsMarginToInches(gDialog.rightInput.value, gDoingMetric); michael@0: michael@0: gPrintSettings.headerStrLeft = hfIdToValue(gDialog.hLeftOption); michael@0: gPrintSettings.headerStrCenter = hfIdToValue(gDialog.hCenterOption); michael@0: gPrintSettings.headerStrRight = hfIdToValue(gDialog.hRightOption); michael@0: michael@0: gPrintSettings.footerStrLeft = hfIdToValue(gDialog.fLeftOption); michael@0: gPrintSettings.footerStrCenter = hfIdToValue(gDialog.fCenterOption); michael@0: gPrintSettings.footerStrRight = hfIdToValue(gDialog.fRightOption); michael@0: michael@0: gPrintSettings.printBGColors = gDialog.printBG.checked; michael@0: gPrintSettings.printBGImages = gDialog.printBG.checked; michael@0: michael@0: gPrintSettings.shrinkToFit = gDialog.shrinkToFit.checked; michael@0: michael@0: var scaling = document.getElementById("scalingInput").value; michael@0: if (scaling < 10.0) { michael@0: scaling = 10.0; michael@0: } michael@0: if (scaling > 500.0) { michael@0: scaling = 500.0; michael@0: } michael@0: scaling /= 100.0; michael@0: gPrintSettings.scaling = scaling; michael@0: michael@0: if (gDoDebug) { michael@0: dump("******* Page Setup Accepting ******\n"); michael@0: dump("print_margin_top "+gDialog.topInput.value+"\n"); michael@0: dump("print_margin_left "+gDialog.leftInput.value+"\n"); michael@0: dump("print_margin_right "+gDialog.bottomInput.value+"\n"); michael@0: dump("print_margin_bottom "+gDialog.rightInput.value+"\n"); michael@0: } michael@0: } michael@0: michael@0: // set return value to "ok" michael@0: if (paramBlock) { michael@0: paramBlock.SetInt(0, 1); michael@0: } else { michael@0: dump("*** FATAL ERROR: No paramBlock\n"); michael@0: } michael@0: michael@0: var flags = gPrintSettingsInterface.kInitSaveMargins | michael@0: gPrintSettingsInterface.kInitSaveHeaderLeft | michael@0: gPrintSettingsInterface.kInitSaveHeaderCenter | michael@0: gPrintSettingsInterface.kInitSaveHeaderRight | michael@0: gPrintSettingsInterface.kInitSaveFooterLeft | michael@0: gPrintSettingsInterface.kInitSaveFooterCenter | michael@0: gPrintSettingsInterface.kInitSaveFooterRight | michael@0: gPrintSettingsInterface.kInitSaveBGColors | michael@0: gPrintSettingsInterface.kInitSaveBGImages | michael@0: gPrintSettingsInterface.kInitSaveInColor | michael@0: gPrintSettingsInterface.kInitSaveReversed | michael@0: gPrintSettingsInterface.kInitSaveOrientation | michael@0: gPrintSettingsInterface.kInitSaveOddEvenPages | michael@0: gPrintSettingsInterface.kInitSaveShrinkToFit | michael@0: gPrintSettingsInterface.kInitSaveScaling; michael@0: michael@0: gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: //--------------------------------------------------- michael@0: function onCancel() michael@0: { michael@0: // set return value to "cancel" michael@0: if (paramBlock) { michael@0: paramBlock.SetInt(0, 0); michael@0: } else { michael@0: dump("*** FATAL ERROR: No paramBlock\n"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: