home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / printPageSetup.js < prev    next >
Text File  |  2003-06-08  |  18KB  |  527 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  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
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla Communicator client code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s): 
  23.  *   Masaki Katakai <katakai@japan.sun.com>
  24.  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
  25.  *   Asko Tontti <atontti@cc.hut.fi>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the NPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the NPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. var gDialog;
  42. var paramBlock;
  43. var gPrefs         = null;
  44. var gPrintService  = null;
  45. var gPrintSettings = null;
  46. var gStringBundle  = null;
  47. var gDoingMetric   = false;
  48.  
  49. var gPrintSettingsInterface = Components.interfaces.nsIPrintSettings;
  50. var gDoDebug = false;
  51.  
  52. //---------------------------------------------------
  53. function initDialog()
  54. {
  55.   gDialog = new Object;
  56.  
  57.   gDialog.orientation     = document.getElementById("orientation");
  58.   gDialog.portrait        = document.getElementById("portrait");
  59.   gDialog.landscape       = document.getElementById("landscape");
  60.  
  61.   gDialog.printBG         = document.getElementById("printBG");
  62.  
  63.   gDialog.shrinkToFit     = document.getElementById("shrinkToFit");
  64.  
  65.   gDialog.marginGroup     = document.getElementById("marginGroup");
  66.  
  67.   gDialog.marginPage      = document.getElementById("marginPage");
  68.   gDialog.marginTop       = document.getElementById("marginTop");
  69.   gDialog.marginBottom    = document.getElementById("marginBottom");
  70.   gDialog.marginLeft      = document.getElementById("marginLeft");
  71.   gDialog.marginRight     = document.getElementById("marginRight");
  72.  
  73.   gDialog.topInput        = document.getElementById("topInput");
  74.   gDialog.bottomInput     = document.getElementById("bottomInput");
  75.   gDialog.leftInput       = document.getElementById("leftInput");
  76.   gDialog.rightInput      = document.getElementById("rightInput");
  77.  
  78.   gDialog.hLeftOption     = document.getElementById("hLeftOption");
  79.   gDialog.hCenterOption   = document.getElementById("hCenterOption");
  80.   gDialog.hRightOption    = document.getElementById("hRightOption");
  81.  
  82.   gDialog.fLeftOption     = document.getElementById("fLeftOption");
  83.   gDialog.fCenterOption   = document.getElementById("fCenterOption");
  84.   gDialog.fRightOption    = document.getElementById("fRightOption");
  85.  
  86.   gDialog.scalingLabel    = document.getElementById("scalingInput");
  87.   gDialog.scalingInput    = document.getElementById("scalingInput");
  88.  
  89.   gDialog.enabled         = false;
  90.  
  91.   gDialog.strings                          = new Array;
  92.   gDialog.strings[ "marginUnits.inches" ]  = document.getElementById("marginUnits.inches").childNodes[0].nodeValue;
  93.   gDialog.strings[ "marginUnits.metric" ]  = document.getElementById("marginUnits.metric").childNodes[0].nodeValue;
  94.   gDialog.strings[ "customPrompt.title" ]  = document.getElementById("customPrompt.title").childNodes[0].nodeValue;
  95.   gDialog.strings[ "customPrompt.prompt" ] = document.getElementById("customPrompt.prompt").childNodes[0].nodeValue;
  96.  
  97. }
  98.  
  99. //---------------------------------------------------
  100. function isListOfPrinterFeaturesAvailable()
  101. {
  102.   var has_printerfeatures = false;
  103.   
  104.   try {
  105.     has_printerfeatures = gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".has_special_printerfeatures");
  106.   } catch(ex) {
  107.   }
  108.   
  109.   return has_printerfeatures;
  110. }
  111.  
  112. //---------------------------------------------------
  113. function checkDouble(element)
  114. {
  115.   var value = element.value;
  116.   if (value && value.length > 0) {
  117.     value = value.replace(/[^\.|^0-9]/g,"");
  118.     if (!value) value = "";
  119.     element.value = value;
  120.   }
  121. }
  122.  
  123. // Theoretical paper width/height.
  124. var gPageWidth  = 8.5;
  125. var gPageHeight = 11.0;
  126.  
  127. //---------------------------------------------------
  128. function setOrientation()
  129. {
  130.   var selection = gDialog.orientation.selectedItem;
  131.  
  132.   var style = "background-color:white;";
  133.   if ((selection == gDialog.portrait && gPageWidth > gPageHeight) || 
  134.       (selection == gDialog.landscape && gPageWidth < gPageHeight)) {
  135.     // Swap width/height.
  136.     var temp = gPageHeight;
  137.     gPageHeight = gPageWidth;
  138.     gPageWidth = temp;
  139.   }
  140.   var div = gDoingMetric ? 100 : 10;
  141.   style += "width:" + gPageWidth/div + unitString() + ";height:" + gPageHeight/div + unitString() + ";";
  142.   gDialog.marginPage.setAttribute( "style", style );
  143. }
  144.  
  145. //---------------------------------------------------
  146. function unitString()
  147. {
  148.   return (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) ? "in" : "mm";
  149. }
  150.  
  151. //---------------------------------------------------
  152. function checkMargin( value, max, other )
  153. {
  154.   // Don't draw this margin bigger than permitted.
  155.   return Math.min(value, max - other.value);
  156. }
  157.  
  158. //---------------------------------------------------
  159. function changeMargin( node )
  160. {
  161.   // Correct invalid input.
  162.   checkDouble(node);
  163.  
  164.   // Reset the margin height/width for this node.
  165.   var val = node.value;
  166.   var nodeToStyle;
  167.   var attr="width";
  168.   if ( node == gDialog.topInput ) {
  169.     nodeToStyle = gDialog.marginTop;
  170.     val = checkMargin( val, gPageHeight, gDialog.bottomInput );
  171.     attr = "height";
  172.   } else if ( node == gDialog.bottomInput ) {
  173.     nodeToStyle = gDialog.marginBottom;
  174.     val = checkMargin( val, gPageHeight, gDialog.topInput );
  175.     attr = "height";
  176.   } else if ( node == gDialog.leftInput ) {
  177.     nodeToStyle = gDialog.marginLeft;
  178.     val = checkMargin( val, gPageWidth, gDialog.rightInput );
  179.   } else {
  180.     nodeToStyle = gDialog.marginRight;
  181.     val = checkMargin( val, gPageWidth, gDialog.leftInput );
  182.   }
  183.   var style = attr + ":" + (val/10) + unitString() + ";";
  184.   nodeToStyle.setAttribute( "style", style );
  185. }
  186.  
  187. //---------------------------------------------------
  188. function changeMargins()
  189. {
  190.   changeMargin( gDialog.topInput );
  191.   changeMargin( gDialog.bottomInput );
  192.   changeMargin( gDialog.leftInput );
  193.   changeMargin( gDialog.rightInput );
  194. }
  195.  
  196. //---------------------------------------------------
  197. function customize( node )
  198. {
  199.   // If selection is now "Custom..." then prompt user for custom setting.
  200.   if ( node.value == 6 ) {
  201.     var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ]
  202.                      .getService( Components.interfaces.nsIPromptService );
  203.     var title      = gDialog.strings[ "customPrompt.title" ];
  204.     var promptText = gDialog.strings[ "customPrompt.prompt" ];
  205.     var result = { value: node.custom };
  206.     var ok = prompter.prompt(window, title, promptText, result, null, { value: false } );
  207.     if ( ok ) {
  208.         node.custom = result.value;
  209.     }
  210.   }
  211. }
  212.  
  213. //---------------------------------------------------
  214. function setHeaderFooter( node, value )
  215. {
  216.   node.value= hfValueToId(value);
  217.   if (node.value == 6) {
  218.     // Remember current Custom... value.
  219.     node.custom = value;
  220.   } else {
  221.     // Start with empty Custom... value.
  222.     node.custom = "";
  223.   }
  224. }
  225.  
  226. //---------------------------------------------------
  227. function getDoubleStr(val, dec)
  228. {
  229.   var str = val.toString();
  230.   var inx = str.indexOf(".");
  231.   return str.substring(0, inx+dec+1);
  232. }
  233.  
  234. var gHFValues = new Array;
  235. gHFValues[ "&T" ] = 1;
  236. gHFValues[ "&U" ] = 2;
  237. gHFValues[ "&D" ] = 3;
  238. gHFValues[ "&P" ] = 4;
  239. gHFValues[ "&PT" ] = 5;
  240.  
  241. function hfValueToId(val)
  242. {
  243.   if ( val in gHFValues ) {
  244.       return gHFValues[val];
  245.   }
  246.   if ( val.length ) {
  247.       return 6; // Custom...
  248.   } else {
  249.       return 0; // --blank--
  250.   }
  251. }
  252.  
  253. function hfIdToValue(node)
  254. {
  255.   var result = "";
  256.   switch ( parseInt( node.value ) ) {
  257.   case 0:
  258.     break;
  259.   case 1:
  260.     result = "&T";
  261.     break;
  262.   case 2:
  263.     result = "&U";
  264.     break;
  265.   case 3:
  266.     result = "&D";
  267.     break;
  268.   case 4:
  269.     result = "&P";
  270.     break;
  271.   case 5:
  272.     result = "&PT";
  273.     break;
  274.   case 6:
  275.     result = node.custom;
  276.     break;
  277.   }
  278.   return result;
  279. }
  280.  
  281. function setPrinterDefaultsForSelectedPrinter()
  282. {
  283.   if (gPrintSettings.printerName == "") {
  284.     gPrintSettings.printerName = gPrintService.defaultPrinterName;
  285.   }
  286.   
  287.   // First get any defaults from the printer 
  288.   gPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
  289.  
  290.   // now augment them with any values from last time
  291.   gPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettingsInterface.kInitSaveAll);
  292.   
  293.   if (gDoDebug) {
  294.     dump("pagesetup/setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', orientation='"+gPrintSettings.orientation+"'\n");
  295.   }
  296. }
  297.  
  298. //---------------------------------------------------
  299. function loadDialog()
  300. {
  301.   var print_orientation   = 0;
  302.   var print_margin_top    = 0.5;
  303.   var print_margin_left   = 0.5;
  304.   var print_margin_bottom = 0.5;
  305.   var print_margin_right  = 0.5;
  306.  
  307.   try {
  308.     gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  309.   
  310.     gPrintService = Components.classes["@mozilla.org/gfx/printsettings-service;1"];
  311.     if (gPrintService) {
  312.       gPrintService = gPrintService.getService();
  313.       if (gPrintService) {
  314.         gPrintService = gPrintService.QueryInterface(Components.interfaces.nsIPrintSettingsService);
  315.       }
  316.     }
  317.   } catch(ex) {
  318.     dump("loadDialog: ex="+ex+"\n");
  319.   }
  320.  
  321.   setPrinterDefaultsForSelectedPrinter();
  322.  
  323.   gDialog.printBG.checked = gPrintSettings.printBGColors || gPrintSettings.printBGImages;
  324.  
  325.   gDialog.shrinkToFit.checked   = gPrintSettings.shrinkToFit;
  326.  
  327.   gDialog.scalingLabel.disabled = gDialog.scalingInput.disabled = gDialog.shrinkToFit.checked;
  328.  
  329.   var marginGroupLabel = gDialog.marginGroup.label;
  330.   if (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) {
  331.     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.inches"]);
  332.     gDoingMetric = false;
  333.   } else {
  334.     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.metric"]);
  335.     // Also, set global page dimensions for A4 paper, in millimeters (assumes portrait at this point).
  336.     gPageWidth = 2100;
  337.     gPageHeight = 2970;
  338.     gDoingMetric = true;
  339.   }
  340.   gDialog.marginGroup.label = marginGroupLabel;
  341.  
  342.   print_orientation   = gPrintSettings.orientation;
  343.   print_margin_top    = convertMarginInchesToUnits(gPrintSettings.marginTop, gDoingMetric);
  344.   print_margin_left   = convertMarginInchesToUnits(gPrintSettings.marginLeft, gDoingMetric);
  345.   print_margin_right  = convertMarginInchesToUnits(gPrintSettings.marginRight, gDoingMetric);
  346.   print_margin_bottom = convertMarginInchesToUnits(gPrintSettings.marginBottom, gDoingMetric);
  347.  
  348.   if (gDoDebug) {
  349.     dump("print_orientation   "+print_orientation+"\n");
  350.  
  351.     dump("print_margin_top    "+print_margin_top+"\n");
  352.     dump("print_margin_left   "+print_margin_left+"\n");
  353.     dump("print_margin_right  "+print_margin_right+"\n");
  354.     dump("print_margin_bottom "+print_margin_bottom+"\n");
  355.   }
  356.  
  357.   if (print_orientation == gPrintSettingsInterface.kPortraitOrientation) {
  358.     gDialog.orientation.selectedItem = gDialog.portrait;
  359.   } else if (print_orientation == gPrintSettingsInterface.kLandscapeOrientation) {
  360.     gDialog.orientation.selectedItem = gDialog.landscape;
  361.   }
  362.  
  363.   // Set orientation the first time on a timeout so the dialog sizes to the
  364.   // maximum height specified in the .xul file.  Otherwise, if the user switches
  365.   // from landscape to portrait, the content grows and the buttons are clipped.
  366.   setTimeout( setOrientation, 0 );
  367.  
  368.   gDialog.topInput.value    = getDoubleStr(print_margin_top, 1);
  369.   gDialog.bottomInput.value = getDoubleStr(print_margin_bottom, 1);
  370.   gDialog.leftInput.value   = getDoubleStr(print_margin_left, 1);
  371.   gDialog.rightInput.value  = getDoubleStr(print_margin_right, 1);
  372.   changeMargins();
  373.  
  374.   setHeaderFooter( gDialog.hLeftOption, gPrintSettings.headerStrLeft );
  375.   setHeaderFooter( gDialog.hCenterOption, gPrintSettings.headerStrCenter );
  376.   setHeaderFooter( gDialog.hRightOption, gPrintSettings.headerStrRight );
  377.  
  378.   setHeaderFooter( gDialog.fLeftOption, gPrintSettings.footerStrLeft );
  379.   setHeaderFooter( gDialog.fCenterOption, gPrintSettings.footerStrCenter );
  380.   setHeaderFooter( gDialog.fRightOption, gPrintSettings.footerStrRight );
  381.  
  382.   gDialog.scalingInput.value  = getDoubleStr(gPrintSettings.scaling * 100.0, 3);
  383.  
  384.   // Enable/disable widgets based in the information whether the selected
  385.   // printer supports the matching feature or not
  386.   if (isListOfPrinterFeaturesAvailable()) {
  387.     if (gPrefs.getBoolPref("print.tmp.printerfeatures." + gPrintSettings.printerName + ".can_change_orientation"))
  388.       gDialog.orientation.removeAttribute("disabled");
  389.     else
  390.       gDialog.orientation.setAttribute("disabled","true");
  391.   }
  392.  
  393.   // Give initial focus to the orientation radio group.
  394.   // Done on a timeout due to to bug 103197.
  395.   setTimeout( function() { gDialog.orientation.focus(); }, 0 );
  396. }
  397.  
  398. //---------------------------------------------------
  399. function onLoad()
  400. {
  401.   // Init gDialog.
  402.   initDialog();
  403.  
  404.   if (window.arguments[0] != null) {
  405.     gPrintSettings = window.arguments[0].QueryInterface(Components.interfaces.nsIPrintSettings);
  406.     paramBlock     = window.arguments[1].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  407.   } else if (gDoDebug) {
  408.     alert("window.arguments[0] == null!");
  409.   }
  410.  
  411.   // default return value is "cancel"
  412.   paramBlock.SetInt(0, 0);
  413.  
  414.   if (gPrintSettings) {
  415.     loadDialog();
  416.   } else if (gDoDebug) {
  417.     alert("Could initialize gDialog, PrintSettings is null!");
  418.   }
  419. }
  420.  
  421. function convertUnitsMarginToInches(aVal, aIsMetric)
  422. {
  423.   if (aIsMetric) {
  424.     return aVal / 25.4;
  425.   } else {
  426.     return aVal;
  427.   }
  428. }
  429.  
  430. function convertMarginInchesToUnits(aVal, aIsMetric)
  431. {
  432.   if (aIsMetric) {
  433.     return aVal * 25.4;
  434.   } else {
  435.     return aVal;
  436.   }
  437. }
  438.  
  439. //---------------------------------------------------
  440. function onAccept()
  441. {
  442.  
  443.   if (gPrintSettings) {
  444.     if ( gDialog.orientation.selectedItem == gDialog.portrait ) {
  445.       gPrintSettings.orientation = gPrintSettingsInterface.kPortraitOrientation;
  446.     } else {
  447.       gPrintSettings.orientation = gPrintSettingsInterface.kLandscapeOrientation;
  448.     }
  449.  
  450.     // save these out so they can be picked up by the device spec
  451.     gPrintSettings.marginTop    = convertUnitsMarginToInches(gDialog.topInput.value, gDoingMetric);
  452.     gPrintSettings.marginLeft   = convertUnitsMarginToInches(gDialog.leftInput.value, gDoingMetric);
  453.     gPrintSettings.marginBottom = convertUnitsMarginToInches(gDialog.bottomInput.value, gDoingMetric);
  454.     gPrintSettings.marginRight  = convertUnitsMarginToInches(gDialog.rightInput.value, gDoingMetric);
  455.  
  456.     gPrintSettings.headerStrLeft   = hfIdToValue(gDialog.hLeftOption);
  457.     gPrintSettings.headerStrCenter = hfIdToValue(gDialog.hCenterOption);
  458.     gPrintSettings.headerStrRight  = hfIdToValue(gDialog.hRightOption);
  459.  
  460.     gPrintSettings.footerStrLeft   = hfIdToValue(gDialog.fLeftOption);
  461.     gPrintSettings.footerStrCenter = hfIdToValue(gDialog.fCenterOption);
  462.     gPrintSettings.footerStrRight  = hfIdToValue(gDialog.fRightOption);
  463.  
  464.     gPrintSettings.printBGColors = gDialog.printBG.checked;
  465.     gPrintSettings.printBGImages = gDialog.printBG.checked;
  466.  
  467.     gPrintSettings.shrinkToFit   = gDialog.shrinkToFit.checked;
  468.  
  469.     var scaling = document.getElementById("scalingInput").value;
  470.     if (scaling < 10.0) {
  471.       scaling = 10.0;
  472.     }
  473.     if (scaling > 500.0) {
  474.       scaling = 500.0;
  475.     }
  476.     scaling /= 100.0;
  477.     gPrintSettings.scaling = scaling;
  478.  
  479.     if (gDoDebug) {
  480.       dump("******* Page Setup Accepting ******\n");
  481.       dump("print_margin_top    "+gDialog.topInput.value+"\n");
  482.       dump("print_margin_left   "+gDialog.leftInput.value+"\n");
  483.       dump("print_margin_right  "+gDialog.bottomInput.value+"\n");
  484.       dump("print_margin_bottom "+gDialog.rightInput.value+"\n");
  485.     }
  486.   }
  487.  
  488.   // set return value to "ok"
  489.   if (paramBlock) {
  490.     paramBlock.SetInt(0, 1);
  491.   } else {
  492.     dump("*** FATAL ERROR: No paramBlock\n");
  493.   }
  494.  
  495.   var flags = gPrintSettingsInterface.kInitSaveMargins |
  496.               gPrintSettingsInterface.kInitSaveHeaderLeft |
  497.               gPrintSettingsInterface.kInitSaveHeaderCenter |
  498.               gPrintSettingsInterface.kInitSaveHeaderRight |
  499.               gPrintSettingsInterface.kInitSaveFooterLeft |
  500.               gPrintSettingsInterface.kInitSaveFooterCenter |
  501.               gPrintSettingsInterface.kInitSaveFooterRight |
  502.               gPrintSettingsInterface.kInitSaveBGColors |
  503.               gPrintSettingsInterface.kInitSaveBGImages |
  504.               gPrintSettingsInterface.kInitSaveInColor |
  505.               gPrintSettingsInterface.kInitSaveReversed |
  506.               gPrintSettingsInterface.kInitSaveOrientation |
  507.               gPrintSettingsInterface.kInitSaveOddEvenPages;
  508.  
  509.   gPrintService.savePrintSettingsToPrefs(gPrintSettings, true, flags);
  510.  
  511.   return true;
  512. }
  513.  
  514. //---------------------------------------------------
  515. function onCancel()
  516. {
  517.   // set return value to "cancel"
  518.   if (paramBlock) {
  519.     paramBlock.SetInt(0, 0);
  520.   } else {
  521.     dump("*** FATAL ERROR: No paramBlock\n");
  522.   }
  523.  
  524.   return true;
  525. }
  526.  
  527.