home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / communicator / printPageSetup.js < prev    next >
Encoding:
JavaScript  |  2002-05-17  |  14.2 KB  |  421 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 gPrintSettings = null;
  44. var gStringBundle  = null;
  45.  
  46. var gPrintSettingsInterface = Components.interfaces.nsIPrintSettings;
  47. var gDoDebug = false;
  48.  
  49. //---------------------------------------------------
  50. function initDialog()
  51. {
  52.   gDialog = new Object;
  53.  
  54.   gDialog.orientation     = document.getElementById("orientation");
  55.   gDialog.portrait        = document.getElementById("portrait");
  56.   gDialog.landscape       = document.getElementById("landscape");
  57.  
  58.   gDialog.printBG         = document.getElementById("printBG");
  59.  
  60.   gDialog.shrinkToFit     = document.getElementById("shrinkToFit");
  61.  
  62.   gDialog.marginGroup     = document.getElementById("marginGroup");
  63.  
  64.   gDialog.marginPage      = document.getElementById("marginPage");
  65.   gDialog.marginTop       = document.getElementById("marginTop");
  66.   gDialog.marginBottom    = document.getElementById("marginBottom");
  67.   gDialog.marginLeft      = document.getElementById("marginLeft");
  68.   gDialog.marginRight     = document.getElementById("marginRight");
  69.  
  70.   gDialog.topInput        = document.getElementById("topInput");
  71.   gDialog.bottomInput     = document.getElementById("bottomInput");
  72.   gDialog.leftInput       = document.getElementById("leftInput");
  73.   gDialog.rightInput      = document.getElementById("rightInput");
  74.  
  75.   gDialog.hLeftOption     = document.getElementById("hLeftOption");
  76.   gDialog.hCenterOption   = document.getElementById("hCenterOption");
  77.   gDialog.hRightOption    = document.getElementById("hRightOption");
  78.  
  79.   gDialog.fLeftOption     = document.getElementById("fLeftOption");
  80.   gDialog.fCenterOption   = document.getElementById("fCenterOption");
  81.   gDialog.fRightOption    = document.getElementById("fRightOption");
  82.  
  83.   gDialog.scalingLabel    = document.getElementById("scalingInput");
  84.   gDialog.scalingInput    = document.getElementById("scalingInput");
  85.  
  86.   gDialog.enabled         = false;
  87.  
  88.   gDialog.strings                          = new Array;
  89.   gDialog.strings[ "marginUnits.inches" ]  = document.getElementById("marginUnits.inches").childNodes[0].nodeValue;
  90.   gDialog.strings[ "marginUnits.metric" ]  = document.getElementById("marginUnits.metric").childNodes[0].nodeValue;
  91.   gDialog.strings[ "customPrompt.title" ]  = document.getElementById("customPrompt.title").childNodes[0].nodeValue;
  92.   gDialog.strings[ "customPrompt.prompt" ] = document.getElementById("customPrompt.prompt").childNodes[0].nodeValue;
  93.  
  94. }
  95.  
  96. //---------------------------------------------------
  97. function checkDouble(element)
  98. {
  99.   var value = element.value;
  100.   if (value && value.length > 0) {
  101.     value = value.replace(/[^\.|^0-9]/g,"");
  102.     if (!value) value = "";
  103.     element.value = value;
  104.   }
  105. }
  106.  
  107. // Theoretical paper width/height.
  108. var gPageWidth  = 8.5;
  109. var gPageHeight = 11.0;
  110.  
  111. //---------------------------------------------------
  112. function setOrientation()
  113. {
  114.   var selection = gDialog.orientation.selectedItem;
  115.  
  116.   var style = "background-color:white;";
  117.   if ((selection == gDialog.portrait && gPageWidth > gPageHeight) || 
  118.       (selection == gDialog.landscape && gPageWidth < gPageHeight)) {
  119.     // Swap width/height.
  120.     var temp = gPageHeight;
  121.     gPageHeight = gPageWidth;
  122.     gPageWidth = temp;
  123.   }
  124.   style += "width:" + gPageWidth/10 + unitString() + ";height:" + gPageHeight/10 + unitString() + ";";
  125.   gDialog.marginPage.setAttribute( "style", style );
  126. }
  127.  
  128. //---------------------------------------------------
  129. function unitString()
  130. {
  131.   return (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) ? "in" : "mm";
  132. }
  133.  
  134. //---------------------------------------------------
  135. function checkMargin( value, max, other )
  136. {
  137.   // Don't draw this margin bigger than permitted.
  138.   return Math.min(value, max - other.value);
  139. }
  140.  
  141. //---------------------------------------------------
  142. function changeMargin( node )
  143. {
  144.   // Correct invalid input.
  145.   checkDouble(node);
  146.  
  147.   // Reset the margin height/width for this node.
  148.   var val = node.value;
  149.   var nodeToStyle;
  150.   var attr="width";
  151.   if ( node == gDialog.topInput ) {
  152.     nodeToStyle = gDialog.marginTop;
  153.     val = checkMargin( val, gPageHeight, gDialog.bottomInput );
  154.     attr = "height";
  155.   } else if ( node == gDialog.bottomInput ) {
  156.     nodeToStyle = gDialog.marginBottom;
  157.     val = checkMargin( val, gPageHeight, gDialog.topInput );
  158.     attr = "height";
  159.   } else if ( node == gDialog.leftInput ) {
  160.     nodeToStyle = gDialog.marginLeft;
  161.     val = checkMargin( val, gPageWidth, gDialog.rightInput );
  162.   } else {
  163.     nodeToStyle = gDialog.marginRight;
  164.     val = checkMargin( val, gPageWidth, gDialog.leftInput );
  165.   }
  166.   var style = attr + ":" + val/10 + unitString() + ";";
  167.   nodeToStyle.setAttribute( "style", style );
  168. }
  169.  
  170. //---------------------------------------------------
  171. function changeMargins()
  172. {
  173.   changeMargin( gDialog.topInput );
  174.   changeMargin( gDialog.bottomInput );
  175.   changeMargin( gDialog.leftInput );
  176.   changeMargin( gDialog.rightInput );
  177. }
  178.  
  179. //---------------------------------------------------
  180. function customize( node )
  181. {
  182.   // If selection is now "Custom..." then prompt user for custom setting.
  183.   if ( node.value == 6 ) {
  184.     var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ]
  185.                      .getService( Components.interfaces.nsIPromptService );
  186.     var title      = gDialog.strings[ "customPrompt.title" ];
  187.     var promptText = gDialog.strings[ "customPrompt.prompt" ];
  188.     var result = { value: node.custom };
  189.     var ok = prompter.prompt(window, title, promptText, result, null, { value: false } );
  190.     if ( ok ) {
  191.         node.custom = result.value;
  192.     }
  193.   }
  194. }
  195.  
  196. //---------------------------------------------------
  197. function setHeaderFooter( node, value )
  198. {
  199.   node.value= hfValueToId(value);
  200.   if (node.value == 6) {
  201.     // Remember current Custom... value.
  202.     node.custom = value;
  203.   } else {
  204.     // Start with empty Custom... value.
  205.     node.custom = "";
  206.   }
  207. }
  208.  
  209. //---------------------------------------------------
  210. function getDoubleStr(val, dec)
  211. {
  212.   var str = val.toString();
  213.   var inx = str.indexOf(".");
  214.   return str.substring(0, inx+dec+1);
  215. }
  216.  
  217. var gHFValues = new Array;
  218. gHFValues[ "&T" ] = 1;
  219. gHFValues[ "&U" ] = 2;
  220. gHFValues[ "&D" ] = 3;
  221. gHFValues[ "&P" ] = 4;
  222. gHFValues[ "&PT" ] = 5;
  223.  
  224. function hfValueToId(val)
  225. {
  226.   if ( val in gHFValues ) {
  227.       return gHFValues[val];
  228.   }
  229.   if ( val.length ) {
  230.       return 6; // Custom...
  231.   } else {
  232.       return 0; // --blank--
  233.   }
  234. }
  235.  
  236. function hfIdToValue(node)
  237. {
  238.   var result = "";
  239.   switch ( parseInt( node.value ) ) {
  240.   case 0:
  241.     break;
  242.   case 1:
  243.     result = "&T";
  244.     break;
  245.   case 2:
  246.     result = "&U";
  247.     break;
  248.   case 3:
  249.     result = "&D";
  250.     break;
  251.   case 4:
  252.     result = "&P";
  253.     break;
  254.   case 5:
  255.     result = "&PT";
  256.     break;
  257.   case 6:
  258.     result = node.custom;
  259.     break;
  260.   }
  261.   return result;
  262. }
  263.  
  264. //---------------------------------------------------
  265. function loadDialog()
  266. {
  267.   var print_orientation   = 0;
  268.   var print_margin_top    = 0.5;
  269.   var print_margin_left   = 0.5;
  270.   var print_margin_bottom = 0.5;
  271.   var print_margin_right  = 0.5;
  272.  
  273.   if (gPrintSettings) {
  274.     print_orientation  = gPrintSettings.orientation;
  275.  
  276.     print_margin_top    = gPrintSettings.marginTop;
  277.     print_margin_left   = gPrintSettings.marginLeft;
  278.     print_margin_right  = gPrintSettings.marginRight;
  279.     print_margin_bottom = gPrintSettings.marginBottom;
  280.   }
  281.  
  282.   if (gDoDebug) {
  283.     dump("print_orientation   "+print_orientation+"\n");
  284.  
  285.     dump("print_margin_top    "+print_margin_top+"\n");
  286.     dump("print_margin_left   "+print_margin_left+"\n");
  287.     dump("print_margin_right  "+print_margin_right+"\n");
  288.     dump("print_margin_bottom "+print_margin_bottom+"\n");
  289.   }
  290.  
  291.   gDialog.printBG.checked = gPrintSettings.printBGColors || gPrintSettings.printBGImages;
  292.  
  293.   gDialog.shrinkToFit.checked   = gPrintSettings.shrinkToFit;
  294.  
  295.   gDialog.scalingLabel.disabled = gDialog.scalingInput.disabled = gDialog.shrinkToFit.checked;
  296.  
  297.   if (print_orientation == gPrintSettingsInterface.kPortraitOrientation) {
  298.     gDialog.orientation.selectedItem = gDialog.portrait;
  299.   } else if (print_orientation == gPrintSettingsInterface.kLandscapeOrientation) {
  300.     gDialog.orientation.selectedItem = gDialog.landscape;
  301.   }
  302.  
  303.   var marginGroupLabel = gDialog.marginGroup.label;
  304.   if (gPrintSettings.paperSizeUnit == gPrintSettingsInterface.kPaperSizeInches) {
  305.     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.inches"]);
  306.   } else {
  307.     marginGroupLabel = marginGroupLabel.replace(/#1/, gDialog.strings["marginUnits.metric"]);
  308.     // Also, set global page dimensions for A4 paper, in millimeters (assumes portrait at this point).
  309.     gPageWidth = 210;
  310.     gPageHeight = 297;
  311.   }
  312.   gDialog.marginGroup.label = marginGroupLabel;
  313.  
  314.   // Set orientation the first time on a timeout so the dialog sizes to the
  315.   // maximum height specified in the .xul file.  Otherwise, if the user switches
  316.   // from landscape to portrait, the content grows and the buttons are clipped.
  317.   setTimeout( setOrientation, 0 );
  318.  
  319.   gDialog.topInput.value    = getDoubleStr(print_margin_top, 1);
  320.   gDialog.bottomInput.value = getDoubleStr(print_margin_bottom, 1);
  321.   gDialog.leftInput.value   = getDoubleStr(print_margin_left, 1);
  322.   gDialog.rightInput.value  = getDoubleStr(print_margin_right, 1);
  323.   changeMargins();
  324.  
  325.   setHeaderFooter( gDialog.hLeftOption, gPrintSettings.headerStrLeft );
  326.   setHeaderFooter( gDialog.hCenterOption, gPrintSettings.headerStrCenter );
  327.   setHeaderFooter( gDialog.hRightOption, gPrintSettings.headerStrRight );
  328.  
  329.   setHeaderFooter( gDialog.fLeftOption, gPrintSettings.footerStrLeft );
  330.   setHeaderFooter( gDialog.fCenterOption, gPrintSettings.footerStrCenter );
  331.   setHeaderFooter( gDialog.fRightOption, gPrintSettings.footerStrRight );
  332.  
  333.   gDialog.scalingInput.value  = getDoubleStr(gPrintSettings.scaling * 100.0, 3);
  334.  
  335.   // Give initial focus to the orientation radio group.
  336.   // Done on a timeout due to to bug 103197.
  337.   setTimeout( function() { gDialog.orientation.focus(); }, 0 );
  338. }
  339.  
  340. //---------------------------------------------------
  341. function onLoad()
  342. {
  343.   // Init gDialog.
  344.   initDialog();
  345.  
  346.   if (window.arguments[0] != null) {
  347.     gPrintSettings = window.arguments[0].QueryInterface(Components.interfaces.nsIPrintSettings);
  348.     paramBlock     = window.arguments[1].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  349.   } else if (gDoDebug) {
  350.     alert("window.arguments[0] == null!");
  351.   }
  352.  
  353.   // default return value is "cancel"
  354.   paramBlock.SetInt(0, 0);
  355.  
  356.   if (gPrintSettings) {
  357.     loadDialog();
  358.   } else if (gDoDebug) {
  359.     alert("Could initialize gDialog, PrintSettings is null!");
  360.   }
  361. }
  362.  
  363. //---------------------------------------------------
  364. function onAccept()
  365. {
  366.  
  367.   if (gPrintSettings) {
  368.     if ( gDialog.orientation.selectedItem == gDialog.portrait ) {
  369.       gPrintSettings.orientation = gPrintSettingsInterface.kPortraitOrientation;
  370.     } else {
  371.       gPrintSettings.orientation = gPrintSettingsInterface.kLandscapeOrientation;
  372.     }
  373.  
  374.     // save these out so they can be picked up by the device spec
  375.     gPrintSettings.marginTop    = gDialog.topInput.value;
  376.     gPrintSettings.marginLeft   = gDialog.leftInput.value;
  377.     gPrintSettings.marginBottom = gDialog.bottomInput.value;
  378.     gPrintSettings.marginRight  = gDialog.rightInput.value;
  379.  
  380.     gPrintSettings.headerStrLeft   = hfIdToValue(gDialog.hLeftOption);
  381.     gPrintSettings.headerStrCenter = hfIdToValue(gDialog.hCenterOption);
  382.     gPrintSettings.headerStrRight  = hfIdToValue(gDialog.hRightOption);
  383.  
  384.     gPrintSettings.footerStrLeft   = hfIdToValue(gDialog.fLeftOption);
  385.     gPrintSettings.footerStrCenter = hfIdToValue(gDialog.fCenterOption);
  386.     gPrintSettings.footerStrRight  = hfIdToValue(gDialog.fRightOption);
  387.  
  388.     gPrintSettings.printBGColors = gDialog.printBG.checked;
  389.     gPrintSettings.printBGImages = gDialog.printBG.checked;
  390.  
  391.     gPrintSettings.shrinkToFit   = gDialog.shrinkToFit.checked;
  392.  
  393.     var scaling = document.getElementById("scalingInput").value;
  394.     if (scaling < 10.0) {
  395.       scaling = 10.0;
  396.     }
  397.     if (scaling > 500.0) {
  398.       scaling = 500.0;
  399.     }
  400.     scaling /= 100.0;
  401.     gPrintSettings.scaling = scaling;
  402.  
  403.     if (gDoDebug) {
  404.       dump("******* Page Setup Accepting ******\n");
  405.       dump("print_margin_top    "+gDialog.topInput.value+"\n");
  406.       dump("print_margin_left   "+gDialog.leftInput.value+"\n");
  407.       dump("print_margin_right  "+gDialog.bottomInput.value+"\n");
  408.       dump("print_margin_bottom "+gDialog.rightInput.value+"\n");
  409.     }
  410.   }
  411.  
  412.   // set return value to "ok"
  413.   if (paramBlock) {
  414.     paramBlock.SetInt(0, 1);
  415.   } else {
  416.     dump("*** FATAL ERROR: No paramBlock\n");
  417.   }
  418.  
  419.   return true;
  420. }
  421.