toolkit/components/printing/content/printPageSetup.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 var gDialog;
     8 var paramBlock;
     9 var gPrefs         = null;
    10 var gPrintService  = null;
    11 var gPrintSettings = null;
    12 var gStringBundle  = null;
    13 var gDoingMetric   = false;
    15 var gPrintSettingsInterface = Components.interfaces.nsIPrintSettings;
    16 var gDoDebug = false;
    18 //---------------------------------------------------
    19 function initDialog()
    20 {
    21   gDialog = new Object;
    23   gDialog.orientation     = document.getElementById("orientation");
    24   gDialog.portrait        = document.getElementById("portrait");
    25   gDialog.landscape       = document.getElementById("landscape");
    27   gDialog.printBG         = document.getElementById("printBG");
    29   gDialog.shrinkToFit     = document.getElementById("shrinkToFit");
    31   gDialog.marginGroup     = document.getElementById("marginGroup");
    33   gDialog.marginPage      = document.getElementById("marginPage");
    34   gDialog.marginTop       = document.getElementById("marginTop");
    35   gDialog.marginBottom    = document.getElementById("marginBottom");
    36   gDialog.marginLeft      = document.getElementById("marginLeft");
    37   gDialog.marginRight     = document.getElementById("marginRight");
    39   gDialog.topInput        = document.getElementById("topInput");
    40   gDialog.bottomInput     = document.getElementById("bottomInput");
    41   gDialog.leftInput       = document.getElementById("leftInput");
    42   gDialog.rightInput      = document.getElementById("rightInput");
    44   gDialog.hLeftOption     = document.getElementById("hLeftOption");
    45   gDialog.hCenterOption   = document.getElementById("hCenterOption");
    46   gDialog.hRightOption    = document.getElementById("hRightOption");
    48   gDialog.fLeftOption     = document.getElementById("fLeftOption");
    49   gDialog.fCenterOption   = document.getElementById("fCenterOption");
    50   gDialog.fRightOption    = document.getElementById("fRightOption");
    52   gDialog.scalingLabel    = document.getElementById("scalingInput");
    53   gDialog.scalingInput    = document.getElementById("scalingInput");
    55   gDialog.enabled         = false;
    57   gDialog.strings                          = new Array;
    58   gDialog.strings[ "marginUnits.inches" ]  = document.getElementById("marginUnits.inches").childNodes[0].nodeValue;
    59   gDialog.strings[ "marginUnits.metric" ]  = document.getElementById("marginUnits.metric").childNodes[0].nodeValue;
    60   gDialog.strings[ "customPrompt.title" ]  = document.getElementById("customPrompt.title").childNodes[0].nodeValue;
    61   gDialog.strings[ "customPrompt.prompt" ] = document.getElementById("customPrompt.prompt").childNodes[0].nodeValue;
    63 }
    65 //---------------------------------------------------
    66 function isListOfPrinterFeaturesAvailable()
    67 {
    68   var has_printerfeatures = false;
    70   try {
    71     has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures");
    72   } catch(ex) {
    73   }
    75   return has_printerfeatures;
    76 }
    78 //---------------------------------------------------
    79 function checkDouble(element)
    80 {
    81   element.value = element.value.replace(/[^.0-9]/g, "");
    82 }
    84 // Theoretical paper width/height.
    85 var gPageWidth  = 8.5;
    86 var gPageHeight = 11.0;
    88 //---------------------------------------------------
    89 function setOrientation()
    90 {
    91   var selection = gDialog.orientation.selectedItem;
    93   var style = "background-color:white;";
    94   if ((selection == gDialog.portrait && gPageWidth > gPageHeight) || 
    95       (selection == gDialog.landscape && gPageWidth < gPageHeight)) {
    96     // Swap width/height.
    97     var temp = gPageHeight;
    98     gPageHeight = gPageWidth;
    99     gPageWidth = temp;
   100   }
   101   var div = gDoingMetric ? 100 : 10;
   102   style += "width:" + gPageWidth/div + unitString() + ";height:" + gPageHeight/div + unitString() + ";";
   103   gDialog.marginPage.setAttribute( "style", style );
   104 }
   106 //---------------------------------------------------
   107 function unitString()
   108 {
   109   return (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) ? "in" : "mm";
   110 }
   112 //---------------------------------------------------
   113 function checkMargin( value, max, other )
   114 {
   115   // Don't draw this margin bigger than permitted.
   116   return Math.min(value, max - other.value);
   117 }
   119 //---------------------------------------------------
   120 function changeMargin( node )
   121 {
   122   // Correct invalid input.
   123   checkDouble(node);
   125   // Reset the margin height/width for this node.
   126   var val = node.value;
   127   var nodeToStyle;
   128   var attr="width";
   129   if ( node == gDialog.topInput ) {
   130     nodeToStyle = gDialog.marginTop;
   131     val = checkMargin( val, gPageHeight, gDialog.bottomInput );
   132     attr = "height";
   133   } else if ( node == gDialog.bottomInput ) {
   134     nodeToStyle = gDialog.marginBottom;
   135     val = checkMargin( val, gPageHeight, gDialog.topInput );
   136     attr = "height";
   137   } else if ( node == gDialog.leftInput ) {
   138     nodeToStyle = gDialog.marginLeft;
   139     val = checkMargin( val, gPageWidth, gDialog.rightInput );
   140   } else {
   141     nodeToStyle = gDialog.marginRight;
   142     val = checkMargin( val, gPageWidth, gDialog.leftInput );
   143   }
   144   var style = attr + ":" + (val/10) + unitString() + ";";
   145   nodeToStyle.setAttribute( "style", style );
   146 }
   148 //---------------------------------------------------
   149 function changeMargins()
   150 {
   151   changeMargin( gDialog.topInput );
   152   changeMargin( gDialog.bottomInput );
   153   changeMargin( gDialog.leftInput );
   154   changeMargin( gDialog.rightInput );
   155 }
   157 //---------------------------------------------------
   158 function customize( node )
   159 {
   160   // If selection is now "Custom..." then prompt user for custom setting.
   161   if ( node.value == 6 ) {
   162     var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ]
   163                      .getService( Components.interfaces.nsIPromptService );
   164     var title      = gDialog.strings[ "customPrompt.title" ];
   165     var promptText = gDialog.strings[ "customPrompt.prompt" ];
   166     var result = { value: node.custom };
   167     var ok = prompter.prompt(window, title, promptText, result, null, { value: false } );
   168     if ( ok ) {
   169         node.custom = result.value;
   170     }
   171   }
   172 }
   174 //---------------------------------------------------
   175 function setHeaderFooter( node, value )
   176 {
   177   node.value= hfValueToId(value);
   178   if (node.value == 6) {
   179     // Remember current Custom... value.
   180     node.custom = value;
   181   } else {
   182     // Start with empty Custom... value.
   183     node.custom = "";
   184   }
   185 }
   187 var gHFValues = new Array;
   188 gHFValues[ "&T" ] = 1;
   189 gHFValues[ "&U" ] = 2;
   190 gHFValues[ "&D" ] = 3;
   191 gHFValues[ "&P" ] = 4;
   192 gHFValues[ "&PT" ] = 5;
   194 function hfValueToId(val)
   195 {
   196   if ( val in gHFValues ) {
   197       return gHFValues[val];
   198   }
   199   if ( val.length ) {
   200       return 6; // Custom...
   201   } else {
   202       return 0; // --blank--
   203   }
   204 }
   206 function hfIdToValue(node)
   207 {
   208   var result = "";
   209   switch ( parseInt( node.value ) ) {
   210   case 0:
   211     break;
   212   case 1:
   213     result = "&T";
   214     break;
   215   case 2:
   216     result = "&U";
   217     break;
   218   case 3:
   219     result = "&D";
   220     break;
   221   case 4:
   222     result = "&P";
   223     break;
   224   case 5:
   225     result = "&PT";
   226     break;
   227   case 6:
   228     result = node.custom;
   229     break;
   230   }
   231   return result;
   232 }
   234 function setPrinterDefaultsForSelectedPrinter()
   235 {
   236   if (gPrintSettings.printerName == "") {
   237     gPrintSettings.printerName = gPrintService.defaultPrinterName;
   238   }
   240   // First get any defaults from the printer 
   241   gPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
   243   // now augment them with any values from last time
   244   gPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettingsInterface.kInitSaveAll);
   246   if (gDoDebug) {
   247     dump("pagesetup/setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', orientation='"+gPrintSettings.orientation+"'\n");
   248   }
   249 }
   251 //---------------------------------------------------
   252 function loadDialog()
   253 {
   254   var print_orientation   = 0;
   255   var print_margin_top    = 0.5;
   256   var print_margin_left   = 0.5;
   257   var print_margin_bottom = 0.5;
   258   var print_margin_right  = 0.5;
   260   try {
   261     gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
   263     gPrintService = Components.classes["@mozilla.org/gfx/printsettings-service;1"];
   264     if (gPrintService) {
   265       gPrintService = gPrintService.getService();
   266       if (gPrintService) {
   267         gPrintService = gPrintService.QueryInterface(Components.interfaces.nsIPrintSettingsService);
   268       }
   269     }
   270   } catch(ex) {
   271     dump("loadDialog: ex="+ex+"\n");
   272   }
   274   setPrinterDefaultsForSelectedPrinter();
   276   gDialog.printBG.checked = gPrintSettings.printBGColors || gPrintSettings.printBGImages;
   278   gDialog.shrinkToFit.checked   = gPrintSettings.shrinkToFit;
   280   gDialog.scalingLabel.disabled = gDialog.scalingInput.disabled = gDialog.shrinkToFit.checked;
   282   var marginGroupLabel = gDialog.marginGroup.label;
   283   if (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) {
   284     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.inches"]);
   285     gDoingMetric = false;
   286   } else {
   287     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.metric"]);
   288     // Also, set global page dimensions for A4 paper, in millimeters (assumes portrait at this point).
   289     gPageWidth = 2100;
   290     gPageHeight = 2970;
   291     gDoingMetric = true;
   292   }
   293   gDialog.marginGroup.label = marginGroupLabel;
   295   print_orientation   = gPrintSettings.orientation;
   296   print_margin_top    = convertMarginInchesToUnits(gPrintSettings.marginTop, gDoingMetric);
   297   print_margin_left   = convertMarginInchesToUnits(gPrintSettings.marginLeft, gDoingMetric);
   298   print_margin_right  = convertMarginInchesToUnits(gPrintSettings.marginRight, gDoingMetric);
   299   print_margin_bottom = convertMarginInchesToUnits(gPrintSettings.marginBottom, gDoingMetric);
   301   if (gDoDebug) {
   302     dump("print_orientation   "+print_orientation+"\n");
   304     dump("print_margin_top    "+print_margin_top+"\n");
   305     dump("print_margin_left   "+print_margin_left+"\n");
   306     dump("print_margin_right  "+print_margin_right+"\n");
   307     dump("print_margin_bottom "+print_margin_bottom+"\n");
   308   }
   310   if (print_orientation == gPrintSettingsInterface.kPortraitOrientation) {
   311     gDialog.orientation.selectedItem = gDialog.portrait;
   312   } else if (print_orientation == gPrintSettingsInterface.kLandscapeOrientation) {
   313     gDialog.orientation.selectedItem = gDialog.landscape;
   314   }
   316   // Set orientation the first time on a timeout so the dialog sizes to the
   317   // maximum height specified in the .xul file.  Otherwise, if the user switches
   318   // from landscape to portrait, the content grows and the buttons are clipped.
   319   setTimeout( setOrientation, 0 );
   321   gDialog.topInput.value    = print_margin_top.toFixed(1);
   322   gDialog.bottomInput.value = print_margin_bottom.toFixed(1);
   323   gDialog.leftInput.value   = print_margin_left.toFixed(1);
   324   gDialog.rightInput.value  = print_margin_right.toFixed(1);
   325   changeMargins();
   327   setHeaderFooter( gDialog.hLeftOption, gPrintSettings.headerStrLeft );
   328   setHeaderFooter( gDialog.hCenterOption, gPrintSettings.headerStrCenter );
   329   setHeaderFooter( gDialog.hRightOption, gPrintSettings.headerStrRight );
   331   setHeaderFooter( gDialog.fLeftOption, gPrintSettings.footerStrLeft );
   332   setHeaderFooter( gDialog.fCenterOption, gPrintSettings.footerStrCenter );
   333   setHeaderFooter( gDialog.fRightOption, gPrintSettings.footerStrRight );
   335   gDialog.scalingInput.value = (gPrintSettings.scaling * 100).toFixed(0);
   337   // Enable/disable widgets based in the information whether the selected
   338   // printer supports the matching feature or not
   339   if (isListOfPrinterFeaturesAvailable()) {
   340     if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_orientation"))
   341       gDialog.orientation.removeAttribute("disabled");
   342     else
   343       gDialog.orientation.setAttribute("disabled","true");
   344   }
   346   // Give initial focus to the orientation radio group.
   347   // Done on a timeout due to to bug 103197.
   348   setTimeout( function() { gDialog.orientation.focus(); }, 0 );
   349 }
   351 //---------------------------------------------------
   352 function onLoad()
   353 {
   354   // Init gDialog.
   355   initDialog();
   357   if (window.arguments[0] != null) {
   358     gPrintSettings = window.arguments[0].QueryInterface(Components.interfaces.nsIPrintSettings);
   359     paramBlock     = window.arguments[1].QueryInterface(Components.interfaces.nsIDialogParamBlock);
   360   } else if (gDoDebug) {
   361     alert("window.arguments[0] == null!");
   362   }
   364   // default return value is "cancel"
   365   paramBlock.SetInt(0, 0);
   367   if (gPrintSettings) {
   368     loadDialog();
   369   } else if (gDoDebug) {
   370     alert("Could initialize gDialog, PrintSettings is null!");
   371   }
   372 }
   374 function convertUnitsMarginToInches(aVal, aIsMetric)
   375 {
   376   if (aIsMetric) {
   377     return aVal / 25.4;
   378   } else {
   379     return aVal;
   380   }
   381 }
   383 function convertMarginInchesToUnits(aVal, aIsMetric)
   384 {
   385   if (aIsMetric) {
   386     return aVal * 25.4;
   387   } else {
   388     return aVal;
   389   }
   390 }
   392 //---------------------------------------------------
   393 function onAccept()
   394 {
   396   if (gPrintSettings) {
   397     if ( gDialog.orientation.selectedItem == gDialog.portrait ) {
   398       gPrintSettings.orientation = gPrintSettingsInterface.kPortraitOrientation;
   399     } else {
   400       gPrintSettings.orientation = gPrintSettingsInterface.kLandscapeOrientation;
   401     }
   403     // save these out so they can be picked up by the device spec
   404     gPrintSettings.marginTop    = convertUnitsMarginToInches(gDialog.topInput.value, gDoingMetric);
   405     gPrintSettings.marginLeft   = convertUnitsMarginToInches(gDialog.leftInput.value, gDoingMetric);
   406     gPrintSettings.marginBottom = convertUnitsMarginToInches(gDialog.bottomInput.value, gDoingMetric);
   407     gPrintSettings.marginRight  = convertUnitsMarginToInches(gDialog.rightInput.value, gDoingMetric);
   409     gPrintSettings.headerStrLeft   = hfIdToValue(gDialog.hLeftOption);
   410     gPrintSettings.headerStrCenter = hfIdToValue(gDialog.hCenterOption);
   411     gPrintSettings.headerStrRight  = hfIdToValue(gDialog.hRightOption);
   413     gPrintSettings.footerStrLeft   = hfIdToValue(gDialog.fLeftOption);
   414     gPrintSettings.footerStrCenter = hfIdToValue(gDialog.fCenterOption);
   415     gPrintSettings.footerStrRight  = hfIdToValue(gDialog.fRightOption);
   417     gPrintSettings.printBGColors = gDialog.printBG.checked;
   418     gPrintSettings.printBGImages = gDialog.printBG.checked;
   420     gPrintSettings.shrinkToFit   = gDialog.shrinkToFit.checked;
   422     var scaling = document.getElementById("scalingInput").value;
   423     if (scaling < 10.0) {
   424       scaling = 10.0;
   425     }
   426     if (scaling > 500.0) {
   427       scaling = 500.0;
   428     }
   429     scaling /= 100.0;
   430     gPrintSettings.scaling = scaling;
   432     if (gDoDebug) {
   433       dump("******* Page Setup Accepting ******\n");
   434       dump("print_margin_top    "+gDialog.topInput.value+"\n");
   435       dump("print_margin_left   "+gDialog.leftInput.value+"\n");
   436       dump("print_margin_right  "+gDialog.bottomInput.value+"\n");
   437       dump("print_margin_bottom "+gDialog.rightInput.value+"\n");
   438     }
   439   }
   441   // set return value to "ok"
   442   if (paramBlock) {
   443     paramBlock.SetInt(0, 1);
   444   } else {
   445     dump("*** FATAL ERROR: No paramBlock\n");
   446   }
   448   var flags = gPrintSettingsInterface.kInitSaveMargins |
   449               gPrintSettingsInterface.kInitSaveHeaderLeft |
   450               gPrintSettingsInterface.kInitSaveHeaderCenter |
   451               gPrintSettingsInterface.kInitSaveHeaderRight |
   452               gPrintSettingsInterface.kInitSaveFooterLeft |
   453               gPrintSettingsInterface.kInitSaveFooterCenter |
   454               gPrintSettingsInterface.kInitSaveFooterRight |
   455               gPrintSettingsInterface.kInitSaveBGColors |
   456               gPrintSettingsInterface.kInitSaveBGImages |
   457               gPrintSettingsInterface.kInitSaveInColor |
   458               gPrintSettingsInterface.kInitSaveReversed |
   459               gPrintSettingsInterface.kInitSaveOrientation |
   460               gPrintSettingsInterface.kInitSaveOddEvenPages |
   461               gPrintSettingsInterface.kInitSaveShrinkToFit |
   462               gPrintSettingsInterface.kInitSaveScaling;
   464   gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags);
   466   return true;
   467 }
   469 //---------------------------------------------------
   470 function onCancel()
   471 {
   472   // set return value to "cancel"
   473   if (paramBlock) {
   474     paramBlock.SetInt(0, 0);
   475   } else {
   476     dump("*** FATAL ERROR: No paramBlock\n");
   477   }
   479   return true;
   480 }

mercurial