home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 July & August / PCWorld_2005-07-08_cd.bin / komunikace / netscape / nsb-install-8-0.exe / chrome / toolkit.jar / content / global / printdialog.js < prev    next >
Text File  |  2004-11-25  |  17KB  |  478 lines

  1.  
  2. var dialog;
  3. var printService       = null;
  4. var printOptions       = null;
  5. var gOriginalNumCopies = 1;
  6.  
  7. var paramBlock;
  8. var gPrintSettings     = null;
  9. var gWebBrowserPrint   = null;
  10. var default_file       = "mozilla.ps";
  11. var gPrintSetInterface = Components.interfaces.nsIPrintSettings;
  12. var doDebug            = false;
  13. var gFileFromPicker    = "";
  14.  
  15. //---------------------------------------------------
  16. function initDialog()
  17. {
  18.   dialog = new Object;
  19.  
  20.   dialog.propertiesButton = document.getElementById("properties");
  21.  
  22.   dialog.destGroup       = document.getElementById("destGroup");
  23.   dialog.fileRadio       = document.getElementById("fileRadio");
  24.   dialog.printerRadio    = document.getElementById("printerRadio");
  25.  
  26.   dialog.printrangeGroup = document.getElementById("printrangeGroup");
  27.   dialog.allpagesRadio   = document.getElementById("allpagesRadio");
  28.   dialog.rangeRadio      = document.getElementById("rangeRadio");
  29.   dialog.selectionRadio  = document.getElementById("selectionRadio");
  30.   dialog.frompageInput   = document.getElementById("frompageInput");
  31.   dialog.frompageLabel   = document.getElementById("frompageLabel");
  32.   dialog.topageInput     = document.getElementById("topageInput");
  33.   dialog.topageLabel     = document.getElementById("topageLabel");
  34.  
  35.   dialog.numCopiesInput  = document.getElementById("numCopiesInput");  
  36.  
  37.   dialog.printframeGroup      = document.getElementById("printframeGroup");
  38.   dialog.aslaidoutRadio       = document.getElementById("aslaidoutRadio");
  39.   dialog.selectedframeRadio   = document.getElementById("selectedframeRadio");
  40.   dialog.eachframesepRadio    = document.getElementById("eachframesepRadio");
  41.   dialog.printframeGroupLabel = document.getElementById("printframeGroupLabel");
  42.  
  43.   dialog.fileInput       = document.getElementById("fileInput");
  44.   dialog.fileLabel       = document.getElementById("fileLabel");
  45.   dialog.printerLabel    = document.getElementById("printerLabel");
  46.   dialog.chooseButton    = document.getElementById("chooseFile");
  47.   dialog.printerList     = document.getElementById("printerList");
  48.  
  49.   dialog.printButton     = document.documentElement.getButton("accept");
  50.  
  51.   // <data> elements
  52.   dialog.printName       = document.getElementById("printButton");
  53.   dialog.fpDialog        = document.getElementById("fpDialog");
  54.  
  55.   dialog.enabled         = false;
  56. }
  57.  
  58. //---------------------------------------------------
  59. function checkInteger(element)
  60. {
  61.   var value = element.value;
  62.   if (value && value.length > 0) {
  63.     value = value.replace(/[^0-9]/g,"");
  64.     if (!value) value = "";
  65.     element.value = value;
  66.   }
  67.   if (!value || value < 1 || value > 999)
  68.     dialog.printButton.setAttribute("disabled","true");
  69.   else
  70.     dialog.printButton.removeAttribute("disabled");
  71. }
  72.  
  73. //---------------------------------------------------
  74. function stripTrailingWhitespace(element)
  75. {
  76.   var value = element.value;
  77.   value = value.replace(/\s+$/,"");
  78.   element.value = value;
  79. }
  80.  
  81. //---------------------------------------------------
  82. function doEnablePrintToFile(value)
  83. {
  84.   if (value) {
  85.     dialog.fileLabel.removeAttribute("disabled");
  86.     dialog.fileInput.removeAttribute("disabled");
  87.     dialog.chooseButton.removeAttribute("disabled");
  88.   } else {
  89.     dialog.fileLabel.setAttribute("disabled","true");
  90.     dialog.fileInput.setAttribute("disabled","true");
  91.     dialog.chooseButton.setAttribute("disabled","true");
  92.   }
  93. }
  94.  
  95. //---------------------------------------------------
  96. function listElement(aListElement)
  97.   {
  98.     this.listElement = aListElement;
  99.   }
  100.  
  101. listElement.prototype =
  102.   {
  103.     clearList:
  104.       function ()
  105.         {
  106.           // remove the menupopup node child of the menulist.
  107.           this.listElement.removeChild(this.listElement.firstChild);
  108.         },
  109.  
  110.     appendPrinterNames: 
  111.       function (aDataObject) 
  112.         { 
  113.           var popupNode = document.createElement("menupopup"); 
  114.           var strDefaultPrinterName = "";
  115.           var printerName;
  116.  
  117.           // build popup menu from printer names
  118.           while (aDataObject.hasMoreElements()) {
  119.             printerName = aDataObject.getNext();
  120.             printerName = printerName.QueryInterface(Components.interfaces.nsISupportsString);
  121.             var printerNameStr = printerName.toString();
  122.             if (strDefaultPrinterName == "")
  123.                strDefaultPrinterName = printerNameStr;
  124.             var itemNode = document.createElement("menuitem");
  125.             itemNode.setAttribute("value", printerNameStr);
  126.             itemNode.setAttribute("label", printerNameStr);
  127.             popupNode.appendChild(itemNode);
  128.           }
  129.           if (strDefaultPrinterName != "") {
  130.             this.listElement.removeAttribute("disabled");
  131.           } else {
  132.             var stringBundle = srGetStrBundle("chrome://global/locale/printing.properties");
  133.             this.listElement.setAttribute("value", strDefaultPrinterName);
  134.             this.listElement.setAttribute("label", stringBundle.GetStringFromName("noprinter"));
  135.  
  136.             // disable dialog
  137.             this.listElement.setAttribute("disabled", "true");
  138.             dialog.destGroup.setAttribute("disabled","true");
  139.             dialog.printerRadio.setAttribute("disabled","true");
  140.             dialog.printerLabel.setAttribute("disabled","true");
  141.             dialog.propertiesButton.setAttribute("disabled","true");
  142.             dialog.fileRadio.setAttribute("disabled","true");
  143.             dialog.printButton.setAttribute("disabled","true");
  144.             doEnablePrintToFile(false);
  145.           }
  146.  
  147.           this.listElement.appendChild(popupNode); 
  148.           return strDefaultPrinterName;
  149.         } 
  150.   };
  151.  
  152. //---------------------------------------------------
  153. function getPrinters()
  154. {
  155.   var printerEnumerator = printOptions.availablePrinters();
  156.  
  157.   var selectElement = new listElement(dialog.printerList);
  158.   selectElement.clearList();
  159.   var strDefaultPrinterName = selectElement.appendPrinterNames(printerEnumerator);
  160.  
  161.   selectElement.listElement.value = strDefaultPrinterName;
  162.  
  163.   // make sure we load the prefs for the initially selected printer
  164.   setPrinterDefaultsForSelectedPrinter();
  165. }
  166.  
  167.  
  168. //---------------------------------------------------
  169. // update gPrintSettings with the defaults for the selected printer
  170. function setPrinterDefaultsForSelectedPrinter()
  171. {
  172.   gPrintSettings.printerName = dialog.printerList.value;
  173.   
  174.   // First get any defaults from the printer 
  175.   printService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
  176.   
  177.   // now augment them with any values from last time
  178.   printService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSetInterface.kInitSaveAll);
  179.   
  180.   if (doDebug) {
  181.     dump("setPrinterDefaultsForSelectedPrinter: printerName='"+gPrintSettings.printerName+"', paperName='"+gPrintSettings.paperName+"'\n");
  182.   }
  183. }
  184.  
  185. //---------------------------------------------------
  186. function displayPropertiesDialog()
  187. {
  188.   gPrintSettings.numCopies = dialog.numCopiesInput.value;
  189.   try {
  190.     var printingPromptService = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  191.                                                  .getService(Components.interfaces.nsIPrintingPromptService);
  192.     if (printingPromptService) {
  193.       printingPromptService.showPrinterProperties(null, dialog.printerList.value, gPrintSettings);
  194.       dialog.numCopiesInput.value = gPrintSettings.numCopies;
  195.     }
  196.   } catch(e) {
  197.     dump("problems getting printingPromptService\n");
  198.   }
  199. }
  200.  
  201. //---------------------------------------------------
  202. function doPrintRange(inx)
  203. {
  204.   if (inx == 1) {
  205.     dialog.frompageInput.removeAttribute("disabled");
  206.     dialog.frompageLabel.removeAttribute("disabled");
  207.     dialog.topageInput.removeAttribute("disabled");
  208.     dialog.topageLabel.removeAttribute("disabled");
  209.   } else {
  210.     dialog.frompageInput.setAttribute("disabled","true");
  211.     dialog.frompageLabel.setAttribute("disabled","true");
  212.     dialog.topageInput.setAttribute("disabled","true");
  213.     dialog.topageLabel.setAttribute("disabled","true");
  214.   }
  215. }
  216.  
  217. //---------------------------------------------------
  218. function loadDialog()
  219. {
  220.   var print_copies        = 1;
  221.   var print_file          = default_file;
  222.   var print_selection_radio_enabled = false;
  223.   var print_frametype     = gPrintSetInterface.kSelectedFrame;
  224.   var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
  225.   var print_tofile        = "";
  226.  
  227.   try {
  228.     printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"];
  229.     if (printService) {
  230.       printService = printService.getService();
  231.       if (printService) {
  232.         printService = printService.QueryInterface(Components.interfaces.nsIPrintSettingsService);
  233.         printOptions = printService.QueryInterface(Components.interfaces.nsIPrintOptions);
  234.       }
  235.     }
  236.   } catch(e) {}
  237.  
  238.   // Note: getPrinters sets up the PrintToFile radio buttons and initalises gPrintSettings
  239.   getPrinters();
  240.  
  241.   if (gPrintSettings) {
  242.     print_tofile        = gPrintSettings.printToFile;
  243.     gOriginalNumCopies  = gPrintSettings.numCopies;
  244.  
  245.     print_copies        = gPrintSettings.numCopies;
  246.     print_file          = gPrintSettings.toFileName;
  247.     print_frametype     = gPrintSettings.printFrameType;
  248.     print_howToEnableUI = gPrintSettings.howToEnableFrameUI;
  249.     print_selection_radio_enabled = gPrintSettings.GetPrintOptions(gPrintSetInterface.kEnableSelectionRB);
  250.   }
  251.  
  252.   if (print_tofile) {
  253.     dialog.destGroup.selectedItem = dialog.fileRadio;
  254.     doEnablePrintToFile(true);
  255.   } else {
  256.     dialog.destGroup.selectedItem = dialog.printerRadio;
  257.     doEnablePrintToFile(false);
  258.   }
  259.  
  260.   if (doDebug) {
  261.     dump("loadDialog*********************************************\n");
  262.     dump("toFileName              ["+print_file+"]\n");
  263.     dump("print_tofile            "+print_tofile+"\n");
  264.     dump("print_frame             "+print_frametype+"\n");
  265.     dump("print_howToEnableUI     "+print_howToEnableUI+"\n");
  266.     dump("selection_radio_enabled "+print_selection_radio_enabled+"\n");
  267.   }
  268.  
  269.   if (print_file == "") {
  270.     print_file = default_file;
  271.   }
  272.  
  273.   dialog.printrangeGroup.selectedItem = dialog.allpagesRadio;
  274.   if (print_selection_radio_enabled) {
  275.     dialog.selectionRadio.removeAttribute("disabled");
  276.   } else {
  277.     dialog.selectionRadio.setAttribute("disabled","true");
  278.   }
  279.   doPrintRange(dialog.rangeRadio.selected);
  280.   dialog.frompageInput.value  = 1;
  281.   dialog.topageInput.value    = 1;
  282.   dialog.numCopiesInput.value = print_copies;
  283.  
  284.   dialog.fileInput.value   = print_file;
  285.  
  286.   if (gPrintSettings.toFileName != "") {
  287.     dialog.fileInput.value = gPrintSettings.toFileName;
  288.   }
  289.  
  290.   if (doDebug) {
  291.     dump("print_howToEnableUI: "+print_howToEnableUI+"\n");
  292.   }
  293.  
  294.   // print frame
  295.   if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAll) {
  296.     dialog.aslaidoutRadio.removeAttribute("disabled");
  297.  
  298.     dialog.selectedframeRadio.removeAttribute("disabled");
  299.     dialog.eachframesepRadio.removeAttribute("disabled");
  300.     dialog.printframeGroupLabel.removeAttribute("disabled");
  301.  
  302.     // initialize radio group
  303.     dialog.printframeGroup.selectedItem = dialog.selectedframeRadio;
  304.  
  305.   } else if (print_howToEnableUI == gPrintSetInterface.kFrameEnableAsIsAndEach) {
  306.     dialog.aslaidoutRadio.removeAttribute("disabled");       //enable
  307.  
  308.     dialog.selectedframeRadio.setAttribute("disabled","true"); // disable
  309.     dialog.eachframesepRadio.removeAttribute("disabled");       // enable
  310.     dialog.printframeGroupLabel.removeAttribute("disabled");    // enable
  311.  
  312.     // initialize
  313.     dialog.printframeGroup.selectedItem = dialog.eachframesepRadio;
  314.  
  315.   } else {
  316.     dialog.aslaidoutRadio.setAttribute("disabled","true");
  317.     dialog.selectedframeRadio.setAttribute("disabled","true");
  318.     dialog.eachframesepRadio.setAttribute("disabled","true");
  319.     dialog.printframeGroupLabel.setAttribute("disabled","true");
  320.   }
  321.  
  322.   dialog.printButton.label = dialog.printName.getAttribute("label");
  323. }
  324.  
  325. //---------------------------------------------------
  326. function onLoad()
  327. {
  328.   // Init dialog.
  329.   initDialog();
  330.  
  331.   // param[0]: nsIPrintSettings object
  332.   // param[1]: container for return value (1 = print, 0 = cancel)
  333.  
  334.   gPrintSettings   = window.arguments[0].QueryInterface(gPrintSetInterface);
  335.   gWebBrowserPrint = window.arguments[1].QueryInterface(Components.interfaces.nsIWebBrowserPrint);
  336.   paramBlock       = window.arguments[2].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  337.  
  338.   // default return value is "cancel"
  339.   paramBlock.SetInt(0, 0);
  340.  
  341.   loadDialog();
  342. }
  343.  
  344. //---------------------------------------------------
  345. function onAccept()
  346. {
  347.  
  348.   if (gPrintSettings) {
  349.     var print_howToEnableUI = gPrintSetInterface.kFrameEnableNone;
  350.     var stringBundle = srGetStrBundle("chrome://global/locale/printing.properties");
  351.     var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  352.                         .getService(Components.interfaces.nsIPromptService);
  353.  
  354.     if (dialog.fileRadio.selected && dialog.fileInput.value == "") {
  355.       var titleText = stringBundle.GetStringFromName("noPrintFilename.title");
  356.       var alertText = stringBundle.GetStringFromName("noPrintFilename.alert");
  357.       promptService.alert(this.window, titleText, alertText);
  358.       return false;
  359.     }
  360.  
  361.     // save these out so they can be picked up by the device spec
  362.     gPrintSettings.printToFile = dialog.fileRadio.selected;
  363.     print_howToEnableUI        = gPrintSettings.howToEnableFrameUI;
  364.  
  365.     // save these out so they can be picked up by the device spec
  366.     gPrintSettings.toFileName   = dialog.fileInput.value;
  367.     gPrintSettings.printerName  = dialog.printerList.value;
  368.  
  369.     if (gPrintSettings.printToFile) {
  370.       if (gPrintSettings.toFileName == "")
  371.         return false;
  372.       var sfile = Components.classes["@mozilla.org/file/local;1"]
  373.                   .createInstance(Components.interfaces.nsILocalFile);
  374.       sfile.initWithPath(gPrintSettings.toFileName);
  375.       if (sfile.exists() &&
  376.         gPrintSettings.toFileName != gFileFromPicker) {
  377.         var desc = stringBundle.formatStringFromName("fileConfirm.exists",
  378.                                                      [gPrintSettings.toFileName],
  379.                                                      1);
  380.         if (!promptService.confirm(this.window, null, desc)) {
  381.           return false;
  382.         }
  383.       }
  384.     }
  385.  
  386.     if (dialog.allpagesRadio.selected) {
  387.       gPrintSettings.printRange = gPrintSetInterface.kRangeAllPages;
  388.     } else if (dialog.rangeRadio.selected) {
  389.       gPrintSettings.printRange = gPrintSetInterface.kRangeSpecifiedPageRange;
  390.     } else if (dialog.selectionRadio.selected) {
  391.       gPrintSettings.printRange = gPrintSetInterface.kRangeSelection;
  392.     }
  393.     gPrintSettings.startPageRange = dialog.frompageInput.value;
  394.     gPrintSettings.endPageRange   = dialog.topageInput.value;
  395.     gPrintSettings.numCopies      = dialog.numCopiesInput.value;
  396.  
  397.     var frametype = gPrintSetInterface.kNoFrames;
  398.     if (print_howToEnableUI != gPrintSetInterface.kFrameEnableNone) {
  399.       if (dialog.aslaidoutRadio.selected) {
  400.         frametype = gPrintSetInterface.kFramesAsIs;
  401.       } else if (dialog.selectedframeRadio.selected) {
  402.         frametype = gPrintSetInterface.kSelectedFrame;
  403.       } else if (dialog.eachframesepRadio.selected) {
  404.         frametype = gPrintSetInterface.kEachFrameSep;
  405.       } else {
  406.         frametype = gPrintSetInterface.kSelectedFrame;
  407.       }
  408.     }
  409.     gPrintSettings.printFrameType = frametype;
  410.     if (doDebug) {
  411.       dump("onAccept*********************************************\n");
  412.       dump("frametype      "+frametype+"\n");
  413.       dump("numCopies      "+gPrintSettings.numCopies+"\n");
  414.       dump("printRange     "+gPrintSettings.printRange+"\n");
  415.       dump("printerName    "+gPrintSettings.printerName+"\n");
  416.       dump("startPageRange "+gPrintSettings.startPageRange+"\n");
  417.       dump("endPageRange   "+gPrintSettings.endPageRange+"\n");
  418.       dump("printToFile    "+gPrintSettings.printToFile+"\n");
  419.     }
  420.   }
  421.  
  422.   if (printService) {
  423.     var flags = gPrintSetInterface.kInitSavePaperSizeType | 
  424.                 gPrintSetInterface.kInitSavePaperSizeUnit |
  425.                 gPrintSetInterface.kInitSavePaperWidth    | 
  426.                 gPrintSetInterface.kInitSavePaperHeight   |
  427.                 gPrintSetInterface.kInitSavePaperName     | 
  428.                 gPrintSetInterface.kInitSaveInColor       |
  429.                 gPrintSetInterface.kInitSavePrintCommand;
  430.     printService.savePrintSettingsToPrefs(gPrintSettings, true, flags);
  431.   }
  432.  
  433.   // set return value to "print"
  434.   if (paramBlock) {
  435.     paramBlock.SetInt(0, 1);
  436.   } else {
  437.     dump("*** FATAL ERROR: No paramBlock\n");
  438.   }
  439.  
  440.   return true;
  441.  
  442. }
  443.  
  444. //---------------------------------------------------
  445. function onCancel()
  446. {
  447.   // set return value to "cancel"
  448.   if (paramBlock) {
  449.     paramBlock.SetInt(0, 0);
  450.   } else {
  451.     dump("*** FATAL ERROR: No paramBlock\n");
  452.   }
  453.  
  454.   return true;
  455. }
  456.  
  457. //---------------------------------------------------
  458. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  459. function onChooseFile()
  460. {
  461.   if (dialog.fileRadio.selected == false)
  462.     return;
  463.  
  464.   try {
  465.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  466.     fp.init(window, dialog.fpDialog.getAttribute("label"), nsIFilePicker.modeSave);
  467.     fp.appendFilters(nsIFilePicker.filterAll);
  468.     fp.show();
  469.     if (fp.file && fp.file.path.length > 0) {
  470.       dialog.fileInput.value = fp.file.path;
  471.       gFileFromPicker = dialog.fileInput.value;
  472.     }
  473.   } catch(ex) {
  474.     dump(ex);
  475.   }
  476. }
  477.  
  478.