1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/embedding/PPrinting.h"
7 #include "nsPrintOptionsImpl.h"
8 #include "nsReadableUtils.h"
9 #include "nsPrintSettingsImpl.h"
11 #include "nsIDOMWindow.h"
12 #include "nsIServiceManager.h"
13 #include "nsIDialogParamBlock.h"
15 #include "nsISupportsPrimitives.h"
16 #include "nsIWindowWatcher.h"
17 #include "nsISupportsArray.h"
20 #include "nsIStringEnumerator.h"
21 #include "nsISupportsPrimitives.h"
23 #include "nsAutoPtr.h"
24 #include "mozilla/Preferences.h"
25 #include "nsPrintfCString.h"
26 #include "nsIWebBrowserPrint.h"
28 using namespace mozilla
;
29 using namespace mozilla::embedding
;
31 NS_IMPL_ISUPPORTS(nsPrintOptions
, nsIPrintOptions
, nsIPrintSettingsService
)
34 static const char kMarginTop
[] = "print_margin_top";
35 static const char kMarginLeft
[] = "print_margin_left";
36 static const char kMarginBottom
[] = "print_margin_bottom";
37 static const char kMarginRight
[] = "print_margin_right";
38 static const char kEdgeTop
[] = "print_edge_top";
39 static const char kEdgeLeft
[] = "print_edge_left";
40 static const char kEdgeBottom
[] = "print_edge_bottom";
41 static const char kEdgeRight
[] = "print_edge_right";
42 static const char kUnwriteableMarginTop
[] = "print_unwriteable_margin_top";
43 static const char kUnwriteableMarginLeft
[] = "print_unwriteable_margin_left";
44 static const char kUnwriteableMarginBottom
[] = "print_unwriteable_margin_bottom";
45 static const char kUnwriteableMarginRight
[] = "print_unwriteable_margin_right";
47 // Prefs for Print Options
48 static const char kPrintEvenPages
[] = "print_evenpages";
49 static const char kPrintOddPages
[] = "print_oddpages";
50 static const char kPrintHeaderStrLeft
[] = "print_headerleft";
51 static const char kPrintHeaderStrCenter
[] = "print_headercenter";
52 static const char kPrintHeaderStrRight
[] = "print_headerright";
53 static const char kPrintFooterStrLeft
[] = "print_footerleft";
54 static const char kPrintFooterStrCenter
[] = "print_footercenter";
55 static const char kPrintFooterStrRight
[] = "print_footerright";
58 static const char kPrintReversed
[] = "print_reversed";
59 static const char kPrintInColor
[] = "print_in_color";
60 static const char kPrintPaperName
[] = "print_paper_name";
61 static const char kPrintPlexName
[] = "print_plex_name";
62 static const char kPrintPaperSizeType
[] = "print_paper_size_type";
63 static const char kPrintPaperData
[] = "print_paper_data";
64 static const char kPrintPaperSizeUnit
[] = "print_paper_size_unit";
65 static const char kPrintPaperWidth
[] = "print_paper_width";
66 static const char kPrintPaperHeight
[] = "print_paper_height";
67 static const char kPrintColorspace
[] = "print_colorspace";
68 static const char kPrintResolutionName
[]= "print_resolution_name";
69 static const char kPrintDownloadFonts
[] = "print_downloadfonts";
70 static const char kPrintOrientation
[] = "print_orientation";
71 static const char kPrintCommand
[] = "print_command";
72 static const char kPrinterName
[] = "print_printer";
73 static const char kPrintToFile
[] = "print_to_file";
74 static const char kPrintToFileName
[] = "print_to_filename";
75 static const char kPrintPageDelay
[] = "print_page_delay";
76 static const char kPrintBGColors
[] = "print_bgcolor";
77 static const char kPrintBGImages
[] = "print_bgimages";
78 static const char kPrintShrinkToFit
[] = "print_shrink_to_fit";
79 static const char kPrintScaling
[] = "print_scaling";
80 static const char kPrintResolution
[] = "print_resolution";
81 static const char kPrintDuplex
[] = "print_duplex";
83 static const char kJustLeft
[] = "left";
84 static const char kJustCenter
[] = "center";
85 static const char kJustRight
[] = "right";
87 #define NS_PRINTER_ENUMERATOR_CONTRACTID "@mozilla.org/gfx/printerenumerator;1"
89 nsPrintOptions::nsPrintOptions()
93 nsPrintOptions::~nsPrintOptions()
98 nsPrintOptions::Init()
104 nsPrintOptions::SerializeToPrintData(nsIPrintSettings
* aSettings
,
105 nsIWebBrowserPrint
* aWBP
,
108 aSettings
->GetStartPageRange(&data
->startPageRange());
109 aSettings
->GetEndPageRange(&data
->endPageRange());
111 aSettings
->GetEdgeTop(&data
->edgeTop());
112 aSettings
->GetEdgeLeft(&data
->edgeLeft());
113 aSettings
->GetEdgeBottom(&data
->edgeBottom());
114 aSettings
->GetEdgeRight(&data
->edgeRight());
116 aSettings
->GetMarginTop(&data
->marginTop());
117 aSettings
->GetMarginLeft(&data
->marginLeft());
118 aSettings
->GetMarginBottom(&data
->marginBottom());
119 aSettings
->GetMarginRight(&data
->marginRight());
120 aSettings
->GetUnwriteableMarginTop(&data
->unwriteableMarginTop());
121 aSettings
->GetUnwriteableMarginLeft(&data
->unwriteableMarginLeft());
122 aSettings
->GetUnwriteableMarginBottom(&data
->unwriteableMarginBottom());
123 aSettings
->GetUnwriteableMarginRight(&data
->unwriteableMarginRight());
125 aSettings
->GetScaling(&data
->scaling());
127 aSettings
->GetPrintBGColors(&data
->printBGColors());
128 aSettings
->GetPrintBGImages(&data
->printBGImages());
129 aSettings
->GetPrintRange(&data
->printRange());
131 // I have no idea if I'm doing this string copying correctly...
133 aSettings
->GetTitle(getter_Copies(title
));
134 data
->title() = title
;
136 nsXPIDLString docURL
;
137 aSettings
->GetDocURL(getter_Copies(docURL
));
138 data
->docURL() = docURL
;
141 nsXPIDLString headerStrLeft
;
142 aSettings
->GetHeaderStrLeft(getter_Copies(headerStrLeft
));
143 data
->headerStrLeft() = headerStrLeft
;
145 nsXPIDLString headerStrCenter
;
146 aSettings
->GetHeaderStrCenter(getter_Copies(headerStrCenter
));
147 data
->headerStrCenter() = headerStrCenter
;
149 nsXPIDLString headerStrRight
;
150 aSettings
->GetHeaderStrRight(getter_Copies(headerStrRight
));
151 data
->headerStrRight() = headerStrRight
;
154 nsXPIDLString footerStrLeft
;
155 aSettings
->GetFooterStrLeft(getter_Copies(footerStrLeft
));
156 data
->footerStrLeft() = footerStrLeft
;
158 nsXPIDLString footerStrCenter
;
159 aSettings
->GetFooterStrCenter(getter_Copies(footerStrCenter
));
160 data
->footerStrCenter() = footerStrCenter
;
162 nsXPIDLString footerStrRight
;
163 aSettings
->GetFooterStrRight(getter_Copies(footerStrRight
));
164 data
->footerStrRight() = footerStrRight
;
166 aSettings
->GetHowToEnableFrameUI(&data
->howToEnableFrameUI());
167 aSettings
->GetIsCancelled(&data
->isCancelled());
168 aSettings
->GetPrintFrameTypeUsage(&data
->printFrameTypeUsage());
169 aSettings
->GetPrintFrameType(&data
->printFrameType());
170 aSettings
->GetPrintSilent(&data
->printSilent());
171 aSettings
->GetShrinkToFit(&data
->shrinkToFit());
172 aSettings
->GetShowPrintProgress(&data
->showPrintProgress());
174 nsXPIDLString paperName
;
175 aSettings
->GetPaperName(getter_Copies(paperName
));
176 data
->paperName() = paperName
;
178 aSettings
->GetPaperSizeType(&data
->paperSizeType());
179 aSettings
->GetPaperData(&data
->paperData());
180 aSettings
->GetPaperWidth(&data
->paperWidth());
181 aSettings
->GetPaperHeight(&data
->paperHeight());
182 aSettings
->GetPaperSizeUnit(&data
->paperSizeUnit());
184 nsXPIDLString plexName
;
185 aSettings
->GetPlexName(getter_Copies(plexName
));
186 data
->plexName() = plexName
;
188 nsXPIDLString colorspace
;
189 aSettings
->GetColorspace(getter_Copies(colorspace
));
190 data
->colorspace() = colorspace
;
192 nsXPIDLString resolutionName
;
193 aSettings
->GetResolutionName(getter_Copies(resolutionName
));
194 data
->resolutionName() = resolutionName
;
196 aSettings
->GetDownloadFonts(&data
->downloadFonts());
197 aSettings
->GetPrintReversed(&data
->printReversed());
198 aSettings
->GetPrintInColor(&data
->printInColor());
199 aSettings
->GetOrientation(&data
->orientation());
201 nsXPIDLString printCommand
;
202 aSettings
->GetPrintCommand(getter_Copies(printCommand
));
203 data
->printCommand() = printCommand
;
205 aSettings
->GetNumCopies(&data
->numCopies());
207 nsXPIDLString printerName
;
208 aSettings
->GetPrinterName(getter_Copies(printerName
));
209 data
->printerName() = printerName
;
211 aSettings
->GetPrintToFile(&data
->printToFile());
213 nsXPIDLString toFileName
;
214 aSettings
->GetToFileName(getter_Copies(toFileName
));
215 data
->toFileName() = toFileName
;
217 aSettings
->GetOutputFormat(&data
->outputFormat());
218 aSettings
->GetPrintPageDelay(&data
->printPageDelay());
219 aSettings
->GetResolution(&data
->resolution());
220 aSettings
->GetDuplex(&data
->duplex());
221 aSettings
->GetIsInitializedFromPrinter(&data
->isInitializedFromPrinter());
222 aSettings
->GetIsInitializedFromPrefs(&data
->isInitializedFromPrefs());
223 aSettings
->GetPersistMarginBoxSettings(&data
->persistMarginBoxSettings());
229 nsPrintOptions::DeserializeToPrintSettings(const PrintData
& data
,
230 nsIPrintSettings
* settings
)
232 settings
->SetStartPageRange(data
.startPageRange());
233 settings
->SetEndPageRange(data
.endPageRange());
235 settings
->SetEdgeTop(data
.edgeTop());
236 settings
->SetEdgeLeft(data
.edgeLeft());
237 settings
->SetEdgeBottom(data
.edgeBottom());
238 settings
->SetEdgeRight(data
.edgeRight());
240 settings
->SetMarginTop(data
.marginTop());
241 settings
->SetMarginLeft(data
.marginLeft());
242 settings
->SetMarginBottom(data
.marginBottom());
243 settings
->SetMarginRight(data
.marginRight());
244 settings
->SetUnwriteableMarginTop(data
.unwriteableMarginTop());
245 settings
->SetUnwriteableMarginLeft(data
.unwriteableMarginLeft());
246 settings
->SetUnwriteableMarginBottom(data
.unwriteableMarginBottom());
247 settings
->SetUnwriteableMarginRight(data
.unwriteableMarginRight());
249 settings
->SetScaling(data
.scaling());
251 settings
->SetPrintBGColors(data
.printBGColors());
252 settings
->SetPrintBGImages(data
.printBGImages());
253 settings
->SetPrintRange(data
.printRange());
255 // I have no idea if I'm doing this string copying correctly...
256 settings
->SetTitle(data
.title().get());
257 settings
->SetDocURL(data
.docURL().get());
260 settings
->SetHeaderStrLeft(data
.headerStrLeft().get());
261 settings
->SetHeaderStrCenter(data
.headerStrCenter().get());
262 settings
->SetHeaderStrRight(data
.headerStrRight().get());
265 settings
->SetFooterStrLeft(data
.footerStrLeft().get());
266 settings
->SetFooterStrCenter(data
.footerStrCenter().get());
267 settings
->SetFooterStrRight(data
.footerStrRight().get());
269 settings
->SetHowToEnableFrameUI(data
.howToEnableFrameUI());
270 settings
->SetIsCancelled(data
.isCancelled());
271 settings
->SetPrintFrameTypeUsage(data
.printFrameTypeUsage());
272 settings
->SetPrintFrameType(data
.printFrameType());
273 settings
->SetPrintSilent(data
.printSilent());
274 settings
->SetShrinkToFit(data
.shrinkToFit());
275 settings
->SetShowPrintProgress(data
.showPrintProgress());
277 settings
->SetPaperName(data
.paperName().get());
279 settings
->SetPaperSizeType(data
.paperSizeType());
280 settings
->SetPaperData(data
.paperData());
281 settings
->SetPaperWidth(data
.paperWidth());
282 settings
->SetPaperHeight(data
.paperHeight());
283 settings
->SetPaperSizeUnit(data
.paperSizeUnit());
285 settings
->SetPlexName(data
.plexName().get());
287 settings
->SetColorspace(data
.colorspace().get());
289 settings
->SetResolutionName(data
.resolutionName().get());
291 settings
->SetDownloadFonts(data
.downloadFonts());
292 settings
->SetPrintReversed(data
.printReversed());
293 settings
->SetPrintInColor(data
.printInColor());
294 settings
->SetOrientation(data
.orientation());
296 settings
->SetPrintCommand(data
.printCommand().get());
298 settings
->SetNumCopies(data
.numCopies());
300 settings
->SetPrinterName(data
.printerName().get());
302 settings
->SetPrintToFile(data
.printToFile());
304 settings
->SetToFileName(data
.toFileName().get());
306 settings
->SetOutputFormat(data
.outputFormat());
307 settings
->SetPrintPageDelay(data
.printPageDelay());
308 settings
->SetResolution(data
.resolution());
309 settings
->SetDuplex(data
.duplex());
310 settings
->SetIsInitializedFromPrinter(data
.isInitializedFromPrinter());
311 settings
->SetIsInitializedFromPrefs(data
.isInitializedFromPrefs());
312 settings
->SetPersistMarginBoxSettings(data
.persistMarginBoxSettings());
319 nsPrintOptions::ShowPrintSetupDialog(nsIPrintSettings
*aPS
)
321 NS_ENSURE_ARG_POINTER(aPS
);
324 // create a nsISupportsArray of the parameters
325 // being passed to the window
326 nsCOMPtr
<nsISupportsArray
> array
;
327 rv
= NS_NewISupportsArray(getter_AddRefs(array
));
328 NS_ENSURE_SUCCESS(rv
, rv
);
330 nsCOMPtr
<nsISupports
> psSupports
= do_QueryInterface(aPS
);
331 NS_ASSERTION(psSupports
, "PrintSettings must be a supports");
332 array
->AppendElement(psSupports
);
334 nsCOMPtr
<nsIDialogParamBlock
> ioParamBlock
=
335 do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID
, &rv
);
336 NS_ENSURE_SUCCESS(rv
, rv
);
338 ioParamBlock
->SetInt(0, 0);
340 nsCOMPtr
<nsISupports
> blkSupps
= do_QueryInterface(ioParamBlock
);
341 NS_ASSERTION(blkSupps
, "IOBlk must be a supports");
342 array
->AppendElement(blkSupps
);
344 nsCOMPtr
<nsIWindowWatcher
> wwatch
=
345 do_GetService(NS_WINDOWWATCHER_CONTRACTID
, &rv
);
346 NS_ENSURE_SUCCESS(rv
, rv
);
348 nsCOMPtr
<nsIDOMWindow
> parent
;
349 wwatch
->GetActiveWindow(getter_AddRefs(parent
));
350 // null |parent| is non-fatal
352 nsCOMPtr
<nsIDOMWindow
> newWindow
;
354 return wwatch
->OpenWindow(parent
,
355 "chrome://global/content/printPageSetup.xul",
356 "_blank","chrome,modal,centerscreen", array
,
357 getter_AddRefs(newWindow
));
360 /** ---------------------------------------------------
361 * Helper function - Creates the "prefix" for the pref
362 * It is either "print."
363 * or "print.printer_<print name>."
366 nsPrintOptions::GetPrefName(const char * aPrefName
,
367 const nsAString
& aPrinterName
)
369 if (!aPrefName
|| !*aPrefName
) {
370 NS_ERROR("Must have a valid pref name!");
374 mPrefName
.AssignLiteral("print.");
376 if (aPrinterName
.Length()) {
377 mPrefName
.AppendLiteral("printer_");
378 AppendUTF16toUTF8(aPrinterName
, mPrefName
);
379 mPrefName
.Append('.');
381 mPrefName
+= aPrefName
;
383 return mPrefName
.get();
386 //----------------------------------------------------------------------
387 // Testing of read/write prefs
388 // This define controls debug output
390 static void WriteDebugStr(const char* aArg1
, const char* aArg2
,
391 const char16_t
* aStr
)
396 str
.ReplaceChar(s
, r
);
398 printf("%s %s = %s \n", aArg1
, aArg2
, ToNewUTF8String(str
));
400 const char* kWriteStr
= "Write Pref:";
401 const char* kReadStr
= "Read Pref:";
402 #define DUMP_STR(_a1, _a2, _a3) WriteDebugStr((_a1), GetPrefName((_a2), \
404 #define DUMP_BOOL(_a1, _a2, _a3) printf("%s %s = %s \n", (_a1), \
405 GetPrefName((_a2), aPrefName), (_a3)?"T":"F");
406 #define DUMP_INT(_a1, _a2, _a3) printf("%s %s = %d \n", (_a1), \
407 GetPrefName((_a2), aPrefName), (_a3));
408 #define DUMP_DBL(_a1, _a2, _a3) printf("%s %s = %10.5f \n", (_a1), \
409 GetPrefName((_a2), aPrefName), (_a3));
411 #define DUMP_STR(_a1, _a2, _a3)
412 #define DUMP_BOOL(_a1, _a2, _a3)
413 #define DUMP_INT(_a1, _a2, _a3)
414 #define DUMP_DBL(_a1, _a2, _a3)
415 #endif /* DEBUG_rods_X */
416 //----------------------------------------------------------------------
419 * This will either read in the generic prefs (not specific to a printer)
420 * or read the prefs in using the printer name to qualify.
421 * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
424 nsPrintOptions::ReadPrefs(nsIPrintSettings
* aPS
, const nsAString
& aPrinterName
,
427 NS_ENSURE_ARG_POINTER(aPS
);
429 if (aFlags
& nsIPrintSettings::kInitSaveMargins
) {
430 int32_t halfInch
= NS_INCHES_TO_INT_TWIPS(0.5);
431 nsIntMargin
margin(halfInch
, halfInch
, halfInch
, halfInch
);
432 ReadInchesToTwipsPref(GetPrefName(kMarginTop
, aPrinterName
), margin
.top
,
434 DUMP_INT(kReadStr
, kMarginTop
, margin
.top
);
435 ReadInchesToTwipsPref(GetPrefName(kMarginLeft
, aPrinterName
), margin
.left
,
437 DUMP_INT(kReadStr
, kMarginLeft
, margin
.left
);
438 ReadInchesToTwipsPref(GetPrefName(kMarginBottom
, aPrinterName
),
439 margin
.bottom
, kMarginBottom
);
440 DUMP_INT(kReadStr
, kMarginBottom
, margin
.bottom
);
441 ReadInchesToTwipsPref(GetPrefName(kMarginRight
, aPrinterName
), margin
.right
,
443 DUMP_INT(kReadStr
, kMarginRight
, margin
.right
);
444 aPS
->SetMarginInTwips(margin
);
447 if (aFlags
& nsIPrintSettings::kInitSaveEdges
) {
448 nsIntMargin
margin(0,0,0,0);
449 ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop
, aPrinterName
), margin
.top
,
451 DUMP_INT(kReadStr
, kEdgeTop
, margin
.top
);
452 ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft
, aPrinterName
), margin
.left
,
454 DUMP_INT(kReadStr
, kEdgeLeft
, margin
.left
);
455 ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom
, aPrinterName
),
456 margin
.bottom
, kEdgeBottom
);
457 DUMP_INT(kReadStr
, kEdgeBottom
, margin
.bottom
);
458 ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight
, aPrinterName
), margin
.right
,
460 DUMP_INT(kReadStr
, kEdgeRight
, margin
.right
);
461 aPS
->SetEdgeInTwips(margin
);
464 if (aFlags
& nsIPrintSettings::kInitSaveUnwriteableMargins
) {
466 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop
, aPrinterName
), margin
.top
,
467 kUnwriteableMarginTop
);
468 DUMP_INT(kReadStr
, kUnwriteableMarginTop
, margin
.top
);
469 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginLeft
, aPrinterName
), margin
.left
,
470 kUnwriteableMarginLeft
);
471 DUMP_INT(kReadStr
, kUnwriteableMarginLeft
, margin
.left
);
472 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginBottom
, aPrinterName
),
473 margin
.bottom
, kUnwriteableMarginBottom
);
474 DUMP_INT(kReadStr
, kUnwriteableMarginBottom
, margin
.bottom
);
475 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginRight
, aPrinterName
), margin
.right
,
476 kUnwriteableMarginRight
);
477 DUMP_INT(kReadStr
, kUnwriteableMarginRight
, margin
.right
);
478 aPS
->SetUnwriteableMarginInTwips(margin
);
486 #define GETBOOLPREF(_prefname, _retval) \
488 Preferences::GetBool( \
489 GetPrefName(_prefname, aPrinterName), _retval \
493 #define GETSTRPREF(_prefname, _retval) \
495 Preferences::GetString( \
496 GetPrefName(_prefname, aPrinterName), _retval \
500 #define GETINTPREF(_prefname, _retval) \
502 Preferences::GetInt( \
503 GetPrefName(_prefname, aPrinterName), _retval \
507 #define GETDBLPREF(_prefname, _retval) \
510 GetPrefName(_prefname, aPrinterName), _retval \
514 // Paper size prefs are read as a group
515 if (aFlags
& nsIPrintSettings::kInitSavePaperSize
) {
516 int32_t sizeUnit
, sizeType
;
517 double width
, height
;
519 bool success
= GETINTPREF(kPrintPaperSizeUnit
, &sizeUnit
)
520 && GETINTPREF(kPrintPaperSizeType
, &sizeType
)
521 && GETDBLPREF(kPrintPaperWidth
, width
)
522 && GETDBLPREF(kPrintPaperHeight
, height
)
523 && GETSTRPREF(kPrintPaperName
, &str
);
525 // Bug 315687: Sanity check paper size to avoid paper size values in
526 // mm when the size unit flag is inches. The value 100 is arbitrary
527 // and can be changed.
529 success
= (sizeUnit
!= nsIPrintSettings::kPaperSizeInches
)
535 aPS
->SetPaperSizeUnit(sizeUnit
);
536 DUMP_INT(kReadStr
, kPrintPaperSizeUnit
, sizeUnit
);
537 aPS
->SetPaperSizeType(sizeType
);
538 DUMP_INT(kReadStr
, kPrintPaperSizeType
, sizeType
);
539 aPS
->SetPaperWidth(width
);
540 DUMP_DBL(kReadStr
, kPrintPaperWidth
, width
);
541 aPS
->SetPaperHeight(height
);
542 DUMP_DBL(kReadStr
, kPrintPaperHeight
, height
);
543 aPS
->SetPaperName(str
.get());
544 DUMP_STR(kReadStr
, kPrintPaperName
, str
.get());
548 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
549 if (GETBOOLPREF(kPrintEvenPages
, &b
)) {
550 aPS
->SetPrintOptions(nsIPrintSettings::kPrintEvenPages
, b
);
551 DUMP_BOOL(kReadStr
, kPrintEvenPages
, b
);
555 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
556 if (GETBOOLPREF(kPrintOddPages
, &b
)) {
557 aPS
->SetPrintOptions(nsIPrintSettings::kPrintOddPages
, b
);
558 DUMP_BOOL(kReadStr
, kPrintOddPages
, b
);
562 if (aFlags
& nsIPrintSettings::kInitSaveHeaderLeft
) {
563 if (GETSTRPREF(kPrintHeaderStrLeft
, &str
)) {
564 aPS
->SetHeaderStrLeft(str
.get());
565 DUMP_STR(kReadStr
, kPrintHeaderStrLeft
, str
.get());
569 if (aFlags
& nsIPrintSettings::kInitSaveHeaderCenter
) {
570 if (GETSTRPREF(kPrintHeaderStrCenter
, &str
)) {
571 aPS
->SetHeaderStrCenter(str
.get());
572 DUMP_STR(kReadStr
, kPrintHeaderStrCenter
, str
.get());
576 if (aFlags
& nsIPrintSettings::kInitSaveHeaderRight
) {
577 if (GETSTRPREF(kPrintHeaderStrRight
, &str
)) {
578 aPS
->SetHeaderStrRight(str
.get());
579 DUMP_STR(kReadStr
, kPrintHeaderStrRight
, str
.get());
583 if (aFlags
& nsIPrintSettings::kInitSaveFooterLeft
) {
584 if (GETSTRPREF(kPrintFooterStrLeft
, &str
)) {
585 aPS
->SetFooterStrLeft(str
.get());
586 DUMP_STR(kReadStr
, kPrintFooterStrLeft
, str
.get());
590 if (aFlags
& nsIPrintSettings::kInitSaveFooterCenter
) {
591 if (GETSTRPREF(kPrintFooterStrCenter
, &str
)) {
592 aPS
->SetFooterStrCenter(str
.get());
593 DUMP_STR(kReadStr
, kPrintFooterStrCenter
, str
.get());
597 if (aFlags
& nsIPrintSettings::kInitSaveFooterRight
) {
598 if (GETSTRPREF(kPrintFooterStrRight
, &str
)) {
599 aPS
->SetFooterStrRight(str
.get());
600 DUMP_STR(kReadStr
, kPrintFooterStrRight
, str
.get());
604 if (aFlags
& nsIPrintSettings::kInitSaveBGColors
) {
605 if (GETBOOLPREF(kPrintBGColors
, &b
)) {
606 aPS
->SetPrintBGColors(b
);
607 DUMP_BOOL(kReadStr
, kPrintBGColors
, b
);
611 if (aFlags
& nsIPrintSettings::kInitSaveBGImages
) {
612 if (GETBOOLPREF(kPrintBGImages
, &b
)) {
613 aPS
->SetPrintBGImages(b
);
614 DUMP_BOOL(kReadStr
, kPrintBGImages
, b
);
618 if (aFlags
& nsIPrintSettings::kInitSaveReversed
) {
619 if (GETBOOLPREF(kPrintReversed
, &b
)) {
620 aPS
->SetPrintReversed(b
);
621 DUMP_BOOL(kReadStr
, kPrintReversed
, b
);
625 if (aFlags
& nsIPrintSettings::kInitSaveInColor
) {
626 if (GETBOOLPREF(kPrintInColor
, &b
)) {
627 aPS
->SetPrintInColor(b
);
628 DUMP_BOOL(kReadStr
, kPrintInColor
, b
);
632 if (aFlags
& nsIPrintSettings::kInitSavePlexName
) {
633 if (GETSTRPREF(kPrintPlexName
, &str
)) {
634 aPS
->SetPlexName(str
.get());
635 DUMP_STR(kReadStr
, kPrintPlexName
, str
.get());
639 if (aFlags
& nsIPrintSettings::kInitSavePaperData
) {
640 if (GETINTPREF(kPrintPaperData
, &iVal
)) {
641 aPS
->SetPaperData(iVal
);
642 DUMP_INT(kReadStr
, kPrintPaperData
, iVal
);
646 if (aFlags
& nsIPrintSettings::kInitSaveColorspace
) {
647 if (GETSTRPREF(kPrintColorspace
, &str
)) {
648 aPS
->SetColorspace(str
.get());
649 DUMP_STR(kReadStr
, kPrintColorspace
, str
.get());
653 if (aFlags
& nsIPrintSettings::kInitSaveResolutionName
) {
654 if (GETSTRPREF(kPrintResolutionName
, &str
)) {
655 aPS
->SetResolutionName(str
.get());
656 DUMP_STR(kReadStr
, kPrintResolutionName
, str
.get());
660 if (aFlags
& nsIPrintSettings::kInitSaveDownloadFonts
) {
661 if (GETBOOLPREF(kPrintDownloadFonts
, &b
)) {
662 aPS
->SetDownloadFonts(b
);
663 DUMP_BOOL(kReadStr
, kPrintDownloadFonts
, b
);
667 if (aFlags
& nsIPrintSettings::kInitSaveOrientation
) {
668 if (GETINTPREF(kPrintOrientation
, &iVal
)) {
669 aPS
->SetOrientation(iVal
);
670 DUMP_INT(kReadStr
, kPrintOrientation
, iVal
);
674 if (aFlags
& nsIPrintSettings::kInitSavePrintCommand
) {
675 if (GETSTRPREF(kPrintCommand
, &str
)) {
676 aPS
->SetPrintCommand(str
.get());
677 DUMP_STR(kReadStr
, kPrintCommand
, str
.get());
681 if (aFlags
& nsIPrintSettings::kInitSavePrintToFile
) {
682 if (GETBOOLPREF(kPrintToFile
, &b
)) {
683 aPS
->SetPrintToFile(b
);
684 DUMP_BOOL(kReadStr
, kPrintToFile
, b
);
688 if (aFlags
& nsIPrintSettings::kInitSaveToFileName
) {
689 if (GETSTRPREF(kPrintToFileName
, &str
)) {
690 aPS
->SetToFileName(str
.get());
691 DUMP_STR(kReadStr
, kPrintToFileName
, str
.get());
695 if (aFlags
& nsIPrintSettings::kInitSavePageDelay
) {
696 if (GETINTPREF(kPrintPageDelay
, &iVal
)) {
697 aPS
->SetPrintPageDelay(iVal
);
698 DUMP_INT(kReadStr
, kPrintPageDelay
, iVal
);
702 if (aFlags
& nsIPrintSettings::kInitSaveShrinkToFit
) {
703 if (GETBOOLPREF(kPrintShrinkToFit
, &b
)) {
704 aPS
->SetShrinkToFit(b
);
705 DUMP_BOOL(kReadStr
, kPrintShrinkToFit
, b
);
709 if (aFlags
& nsIPrintSettings::kInitSaveScaling
) {
710 if (GETDBLPREF(kPrintScaling
, dbl
)) {
711 aPS
->SetScaling(dbl
);
712 DUMP_DBL(kReadStr
, kPrintScaling
, dbl
);
716 if (aFlags
& nsIPrintSettings::kInitSaveResolution
) {
717 if (GETINTPREF(kPrintResolution
, &iVal
)) {
718 aPS
->SetResolution(iVal
);
719 DUMP_INT(kReadStr
, kPrintResolution
, iVal
);
723 if (aFlags
& nsIPrintSettings::kInitSaveDuplex
) {
724 if (GETINTPREF(kPrintDuplex
, &iVal
)) {
725 aPS
->SetDuplex(iVal
);
726 DUMP_INT(kReadStr
, kPrintDuplex
, iVal
);
736 /** ---------------------------------------------------
737 * See documentation in nsPrintOptionsImpl.h
738 * @update 1/12/01 rods
741 nsPrintOptions::WritePrefs(nsIPrintSettings
*aPS
, const nsAString
& aPrinterName
,
744 NS_ENSURE_ARG_POINTER(aPS
);
746 bool persistMarginBoxSettings
;
747 aPS
->GetPersistMarginBoxSettings(&persistMarginBoxSettings
);
750 if (aFlags
& nsIPrintSettings::kInitSaveMargins
) {
751 if (NS_SUCCEEDED(aPS
->GetMarginInTwips(margin
))) {
752 WriteInchesFromTwipsPref(GetPrefName(kMarginTop
, aPrinterName
),
754 DUMP_INT(kWriteStr
, kMarginTop
, margin
.top
);
755 WriteInchesFromTwipsPref(GetPrefName(kMarginLeft
, aPrinterName
),
757 DUMP_INT(kWriteStr
, kMarginLeft
, margin
.top
);
758 WriteInchesFromTwipsPref(GetPrefName(kMarginBottom
, aPrinterName
),
760 DUMP_INT(kWriteStr
, kMarginBottom
, margin
.top
);
761 WriteInchesFromTwipsPref(GetPrefName(kMarginRight
, aPrinterName
),
763 DUMP_INT(kWriteStr
, kMarginRight
, margin
.top
);
768 if (aFlags
& nsIPrintSettings::kInitSaveEdges
) {
769 if (NS_SUCCEEDED(aPS
->GetEdgeInTwips(edge
))) {
770 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop
, aPrinterName
),
772 DUMP_INT(kWriteStr
, kEdgeTop
, edge
.top
);
773 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft
, aPrinterName
),
775 DUMP_INT(kWriteStr
, kEdgeLeft
, edge
.top
);
776 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom
, aPrinterName
),
778 DUMP_INT(kWriteStr
, kEdgeBottom
, edge
.top
);
779 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight
, aPrinterName
),
781 DUMP_INT(kWriteStr
, kEdgeRight
, edge
.top
);
785 nsIntMargin unwriteableMargin
;
786 if (aFlags
& nsIPrintSettings::kInitSaveUnwriteableMargins
) {
787 if (NS_SUCCEEDED(aPS
->GetUnwriteableMarginInTwips(unwriteableMargin
))) {
788 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginTop
, aPrinterName
),
789 unwriteableMargin
.top
);
790 DUMP_INT(kWriteStr
, kUnwriteableMarginTop
, unwriteableMargin
.top
);
791 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginLeft
, aPrinterName
),
792 unwriteableMargin
.left
);
793 DUMP_INT(kWriteStr
, kUnwriteableMarginLeft
, unwriteableMargin
.top
);
794 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginBottom
, aPrinterName
),
795 unwriteableMargin
.bottom
);
796 DUMP_INT(kWriteStr
, kUnwriteableMarginBottom
, unwriteableMargin
.top
);
797 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginRight
, aPrinterName
),
798 unwriteableMargin
.right
);
799 DUMP_INT(kWriteStr
, kUnwriteableMarginRight
, unwriteableMargin
.top
);
803 // Paper size prefs are saved as a group
804 if (aFlags
& nsIPrintSettings::kInitSavePaperSize
) {
805 int16_t sizeUnit
, sizeType
;
806 double width
, height
;
810 NS_SUCCEEDED(aPS
->GetPaperSizeUnit(&sizeUnit
)) &&
811 NS_SUCCEEDED(aPS
->GetPaperSizeType(&sizeType
)) &&
812 NS_SUCCEEDED(aPS
->GetPaperWidth(&width
)) &&
813 NS_SUCCEEDED(aPS
->GetPaperHeight(&height
)) &&
814 NS_SUCCEEDED(aPS
->GetPaperName(&name
))
816 DUMP_INT(kWriteStr
, kPrintPaperSizeUnit
, sizeUnit
);
817 Preferences::SetInt(GetPrefName(kPrintPaperSizeUnit
, aPrinterName
),
819 DUMP_INT(kWriteStr
, kPrintPaperSizeType
, sizeType
);
820 Preferences::SetInt(GetPrefName(kPrintPaperSizeType
, aPrinterName
),
822 DUMP_DBL(kWriteStr
, kPrintPaperWidth
, width
);
823 WritePrefDouble(GetPrefName(kPrintPaperWidth
, aPrinterName
), width
);
824 DUMP_DBL(kWriteStr
, kPrintPaperHeight
, height
);
825 WritePrefDouble(GetPrefName(kPrintPaperHeight
, aPrinterName
), height
);
826 DUMP_STR(kWriteStr
, kPrintPaperName
, name
);
827 Preferences::SetString(GetPrefName(kPrintPaperName
, aPrinterName
), name
);
837 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
838 if (NS_SUCCEEDED(aPS
->GetPrintOptions(nsIPrintSettings::kPrintEvenPages
,
840 DUMP_BOOL(kWriteStr
, kPrintEvenPages
, b
);
841 Preferences::SetBool(GetPrefName(kPrintEvenPages
, aPrinterName
), b
);
845 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
846 if (NS_SUCCEEDED(aPS
->GetPrintOptions(nsIPrintSettings::kPrintOddPages
,
848 DUMP_BOOL(kWriteStr
, kPrintOddPages
, b
);
849 Preferences::SetBool(GetPrefName(kPrintOddPages
, aPrinterName
), b
);
853 if (persistMarginBoxSettings
) {
854 if (aFlags
& nsIPrintSettings::kInitSaveHeaderLeft
) {
855 if (NS_SUCCEEDED(aPS
->GetHeaderStrLeft(&uStr
))) {
856 DUMP_STR(kWriteStr
, kPrintHeaderStrLeft
, uStr
);
857 Preferences::SetString(GetPrefName(kPrintHeaderStrLeft
, aPrinterName
),
862 if (aFlags
& nsIPrintSettings::kInitSaveHeaderCenter
) {
863 if (NS_SUCCEEDED(aPS
->GetHeaderStrCenter(&uStr
))) {
864 DUMP_STR(kWriteStr
, kPrintHeaderStrCenter
, uStr
);
865 Preferences::SetString(GetPrefName(kPrintHeaderStrCenter
, aPrinterName
),
870 if (aFlags
& nsIPrintSettings::kInitSaveHeaderRight
) {
871 if (NS_SUCCEEDED(aPS
->GetHeaderStrRight(&uStr
))) {
872 DUMP_STR(kWriteStr
, kPrintHeaderStrRight
, uStr
);
873 Preferences::SetString(GetPrefName(kPrintHeaderStrRight
, aPrinterName
),
878 if (aFlags
& nsIPrintSettings::kInitSaveFooterLeft
) {
879 if (NS_SUCCEEDED(aPS
->GetFooterStrLeft(&uStr
))) {
880 DUMP_STR(kWriteStr
, kPrintFooterStrLeft
, uStr
);
881 Preferences::SetString(GetPrefName(kPrintFooterStrLeft
, aPrinterName
),
886 if (aFlags
& nsIPrintSettings::kInitSaveFooterCenter
) {
887 if (NS_SUCCEEDED(aPS
->GetFooterStrCenter(&uStr
))) {
888 DUMP_STR(kWriteStr
, kPrintFooterStrCenter
, uStr
);
889 Preferences::SetString(GetPrefName(kPrintFooterStrCenter
, aPrinterName
),
894 if (aFlags
& nsIPrintSettings::kInitSaveFooterRight
) {
895 if (NS_SUCCEEDED(aPS
->GetFooterStrRight(&uStr
))) {
896 DUMP_STR(kWriteStr
, kPrintFooterStrRight
, uStr
);
897 Preferences::SetString(GetPrefName(kPrintFooterStrRight
, aPrinterName
),
903 if (aFlags
& nsIPrintSettings::kInitSaveBGColors
) {
904 if (NS_SUCCEEDED(aPS
->GetPrintBGColors(&b
))) {
905 DUMP_BOOL(kWriteStr
, kPrintBGColors
, b
);
906 Preferences::SetBool(GetPrefName(kPrintBGColors
, aPrinterName
), b
);
910 if (aFlags
& nsIPrintSettings::kInitSaveBGImages
) {
911 if (NS_SUCCEEDED(aPS
->GetPrintBGImages(&b
))) {
912 DUMP_BOOL(kWriteStr
, kPrintBGImages
, b
);
913 Preferences::SetBool(GetPrefName(kPrintBGImages
, aPrinterName
), b
);
917 if (aFlags
& nsIPrintSettings::kInitSaveReversed
) {
918 if (NS_SUCCEEDED(aPS
->GetPrintReversed(&b
))) {
919 DUMP_BOOL(kWriteStr
, kPrintReversed
, b
);
920 Preferences::SetBool(GetPrefName(kPrintReversed
, aPrinterName
), b
);
924 if (aFlags
& nsIPrintSettings::kInitSaveInColor
) {
925 if (NS_SUCCEEDED(aPS
->GetPrintInColor(&b
))) {
926 DUMP_BOOL(kWriteStr
, kPrintInColor
, b
);
927 Preferences::SetBool(GetPrefName(kPrintInColor
, aPrinterName
), b
);
931 if (aFlags
& nsIPrintSettings::kInitSavePlexName
) {
932 if (NS_SUCCEEDED(aPS
->GetPlexName(&uStr
))) {
933 DUMP_STR(kWriteStr
, kPrintPlexName
, uStr
);
934 Preferences::SetString(GetPrefName(kPrintPlexName
, aPrinterName
), uStr
);
938 if (aFlags
& nsIPrintSettings::kInitSavePaperData
) {
939 if (NS_SUCCEEDED(aPS
->GetPaperData(&iVal16
))) {
940 DUMP_INT(kWriteStr
, kPrintPaperData
, iVal16
);
941 Preferences::SetInt(GetPrefName(kPrintPaperData
, aPrinterName
),
946 if (aFlags
& nsIPrintSettings::kInitSaveColorspace
) {
947 if (NS_SUCCEEDED(aPS
->GetColorspace(&uStr
))) {
948 DUMP_STR(kWriteStr
, kPrintColorspace
, uStr
);
949 Preferences::SetString(GetPrefName(kPrintColorspace
, aPrinterName
), uStr
);
953 if (aFlags
& nsIPrintSettings::kInitSaveResolutionName
) {
954 if (NS_SUCCEEDED(aPS
->GetResolutionName(&uStr
))) {
955 DUMP_STR(kWriteStr
, kPrintResolutionName
, uStr
);
956 Preferences::SetString(GetPrefName(kPrintResolutionName
, aPrinterName
),
961 if (aFlags
& nsIPrintSettings::kInitSaveDownloadFonts
) {
962 if (NS_SUCCEEDED(aPS
->GetDownloadFonts(&b
))) {
963 DUMP_BOOL(kWriteStr
, kPrintDownloadFonts
, b
);
964 Preferences::SetBool(GetPrefName(kPrintDownloadFonts
, aPrinterName
), b
);
968 if (aFlags
& nsIPrintSettings::kInitSaveOrientation
) {
969 if (NS_SUCCEEDED(aPS
->GetOrientation(&iVal
))) {
970 DUMP_INT(kWriteStr
, kPrintOrientation
, iVal
);
971 Preferences::SetInt(GetPrefName(kPrintOrientation
, aPrinterName
), iVal
);
975 if (aFlags
& nsIPrintSettings::kInitSavePrintCommand
) {
976 if (NS_SUCCEEDED(aPS
->GetPrintCommand(&uStr
))) {
977 DUMP_STR(kWriteStr
, kPrintCommand
, uStr
);
978 Preferences::SetString(GetPrefName(kPrintCommand
, aPrinterName
), uStr
);
982 // Only the general version of this pref is saved
983 if ((aFlags
& nsIPrintSettings::kInitSavePrinterName
)
984 && aPrinterName
.IsEmpty()) {
985 if (NS_SUCCEEDED(aPS
->GetPrinterName(&uStr
))) {
986 DUMP_STR(kWriteStr
, kPrinterName
, uStr
);
987 Preferences::SetString(kPrinterName
, uStr
);
991 if (aFlags
& nsIPrintSettings::kInitSavePrintToFile
) {
992 if (NS_SUCCEEDED(aPS
->GetPrintToFile(&b
))) {
993 DUMP_BOOL(kWriteStr
, kPrintToFile
, b
);
994 Preferences::SetBool(GetPrefName(kPrintToFile
, aPrinterName
), b
);
998 if (aFlags
& nsIPrintSettings::kInitSaveToFileName
) {
999 if (NS_SUCCEEDED(aPS
->GetToFileName(&uStr
))) {
1000 DUMP_STR(kWriteStr
, kPrintToFileName
, uStr
);
1001 Preferences::SetString(GetPrefName(kPrintToFileName
, aPrinterName
), uStr
);
1005 if (aFlags
& nsIPrintSettings::kInitSavePageDelay
) {
1006 if (NS_SUCCEEDED(aPS
->GetPrintPageDelay(&iVal
))) {
1007 DUMP_INT(kWriteStr
, kPrintPageDelay
, iVal
);
1008 Preferences::SetInt(GetPrefName(kPrintPageDelay
, aPrinterName
), iVal
);
1012 if (aFlags
& nsIPrintSettings::kInitSaveShrinkToFit
) {
1013 if (NS_SUCCEEDED(aPS
->GetShrinkToFit(&b
))) {
1014 DUMP_BOOL(kWriteStr
, kPrintShrinkToFit
, b
);
1015 Preferences::SetBool(GetPrefName(kPrintShrinkToFit
, aPrinterName
), b
);
1019 if (aFlags
& nsIPrintSettings::kInitSaveScaling
) {
1020 if (NS_SUCCEEDED(aPS
->GetScaling(&dbl
))) {
1021 DUMP_DBL(kWriteStr
, kPrintScaling
, dbl
);
1022 WritePrefDouble(GetPrefName(kPrintScaling
, aPrinterName
), dbl
);
1026 if (aFlags
& nsIPrintSettings::kInitSaveResolution
) {
1027 if (NS_SUCCEEDED(aPS
->GetResolution(&iVal
))) {
1028 DUMP_INT(kWriteStr
, kPrintResolution
, iVal
);
1029 Preferences::SetInt(GetPrefName(kPrintResolution
, aPrinterName
), iVal
);
1033 if (aFlags
& nsIPrintSettings::kInitSaveDuplex
) {
1034 if (NS_SUCCEEDED(aPS
->GetDuplex(&iVal
))) {
1035 DUMP_INT(kWriteStr
, kPrintDuplex
, iVal
);
1036 Preferences::SetInt(GetPrefName(kPrintDuplex
, aPrinterName
), iVal
);
1047 nsPrintOptions::DisplayJobProperties(const char16_t
*aPrinter
,
1048 nsIPrintSettings
* aPrintSettings
,
1051 NS_ENSURE_ARG_POINTER(aPrinter
);
1052 *aDisplayed
= false;
1055 nsCOMPtr
<nsIPrinterEnumerator
> propDlg
=
1056 do_CreateInstance(NS_PRINTER_ENUMERATOR_CONTRACTID
, &rv
);
1057 NS_ENSURE_SUCCESS(rv
, rv
);
1059 NS_ENSURE_ARG_POINTER(aPrintSettings
);
1060 rv
= propDlg
->DisplayPropertiesDlg(aPrinter
, aPrintSettings
);
1061 NS_ENSURE_SUCCESS(rv
, rv
);
1068 NS_IMETHODIMP
nsPrintOptions::GetNativeData(int16_t aDataType
, void * *_retval
)
1070 return NS_ERROR_NOT_IMPLEMENTED
;
1073 nsresult
nsPrintOptions::_CreatePrintSettings(nsIPrintSettings
**_retval
)
1075 // does not initially ref count
1076 nsPrintSettings
* printSettings
= new nsPrintSettings();
1077 NS_ENSURE_TRUE(printSettings
, NS_ERROR_OUT_OF_MEMORY
);
1079 NS_ADDREF(*_retval
= printSettings
); // ref count
1081 nsXPIDLString printerName
;
1082 nsresult rv
= GetDefaultPrinterName(getter_Copies(printerName
));
1083 NS_ENSURE_SUCCESS(rv
, rv
);
1084 (*_retval
)->SetPrinterName(printerName
.get());
1086 (void)InitPrintSettingsFromPrefs(*_retval
, false,
1087 nsIPrintSettings::kInitSaveAll
);
1092 NS_IMETHODIMP
nsPrintOptions::CreatePrintSettings(nsIPrintSettings
**_retval
)
1094 return _CreatePrintSettings(_retval
);
1098 nsPrintOptions::GetGlobalPrintSettings(nsIPrintSettings
**aGlobalPrintSettings
)
1102 rv
= CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings
));
1103 NS_ENSURE_SUCCESS(rv
, rv
);
1105 NS_ADDREF(*aGlobalPrintSettings
= mGlobalPrintSettings
.get());
1111 nsPrintOptions::GetNewPrintSettings(nsIPrintSettings
* *aNewPrintSettings
)
1113 return CreatePrintSettings(aNewPrintSettings
);
1117 nsPrintOptions::GetDefaultPrinterName(char16_t
* *aDefaultPrinterName
)
1120 nsCOMPtr
<nsIPrinterEnumerator
> prtEnum
=
1121 do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID
, &rv
);
1122 NS_ENSURE_SUCCESS(rv
, rv
);
1124 // Look up the printer from the last print job
1125 nsAutoString lastPrinterName
;
1126 Preferences::GetString(kPrinterName
, &lastPrinterName
);
1127 if (!lastPrinterName
.IsEmpty()) {
1128 // Verify it's still a valid printer
1129 nsCOMPtr
<nsIStringEnumerator
> printers
;
1130 rv
= prtEnum
->GetPrinterNameList(getter_AddRefs(printers
));
1131 if (NS_SUCCEEDED(rv
)) {
1132 bool isValid
= false;
1134 while (NS_SUCCEEDED(printers
->HasMore(&hasMore
)) && hasMore
) {
1135 nsAutoString printer
;
1136 if (NS_SUCCEEDED(printers
->GetNext(printer
)) && lastPrinterName
.Equals(printer
)) {
1142 *aDefaultPrinterName
= ToNewUnicode(lastPrinterName
);
1148 // There is no last printer preference, or it doesn't name a valid printer.
1149 // Return the default from the printer enumeration.
1150 return prtEnum
->GetDefaultPrinterName(aDefaultPrinterName
);
1154 nsPrintOptions::InitPrintSettingsFromPrinter(const char16_t
*aPrinterName
,
1155 nsIPrintSettings
*aPrintSettings
)
1157 NS_ENSURE_ARG_POINTER(aPrintSettings
);
1158 NS_ENSURE_ARG_POINTER(aPrinterName
);
1161 nsXPIDLString printerName
;
1162 aPrintSettings
->GetPrinterName(getter_Copies(printerName
));
1163 if (!printerName
.Equals(aPrinterName
)) {
1164 NS_WARNING("Printer names should match!");
1169 aPrintSettings
->GetIsInitializedFromPrinter(&isInitialized
);
1174 nsCOMPtr
<nsIPrinterEnumerator
> prtEnum
=
1175 do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID
, &rv
);
1176 NS_ENSURE_SUCCESS(rv
, rv
);
1178 rv
= prtEnum
->InitPrintSettingsFromPrinter(aPrinterName
, aPrintSettings
);
1179 NS_ENSURE_SUCCESS(rv
, rv
);
1181 aPrintSettings
->SetIsInitializedFromPrinter(true);
1185 /** ---------------------------------------------------
1186 * Helper function - Returns either the name or sets the length to zero
1189 GetAdjustedPrinterName(nsIPrintSettings
* aPS
, bool aUsePNP
,
1190 nsAString
& aPrinterName
)
1192 NS_ENSURE_ARG_POINTER(aPS
);
1194 aPrinterName
.Truncate();
1198 // Get the Printer Name from the PrintSettings
1199 // to use as a prefix for Pref Names
1200 char16_t
* prtName
= nullptr;
1202 nsresult rv
= aPS
->GetPrinterName(&prtName
);
1203 NS_ENSURE_SUCCESS(rv
, rv
);
1205 aPrinterName
= nsDependentString(prtName
);
1207 // Convert any whitespaces, carriage returns or newlines to _
1208 // The below algorithm is supposedly faster than using iterators
1209 NS_NAMED_LITERAL_STRING(replSubstr
, "_");
1210 const char* replaceStr
= " \n\r";
1213 for (x
=0; x
< (int32_t)strlen(replaceStr
); x
++) {
1214 char16_t uChar
= replaceStr
[x
];
1217 while ((i
= aPrinterName
.FindChar(uChar
, i
)) != kNotFound
) {
1218 aPrinterName
.Replace(i
, 1, replSubstr
);
1226 nsPrintOptions::GetPrinterPrefInt(nsIPrintSettings
*aPrintSettings
,
1227 const char16_t
*aPrefName
, int32_t *_retval
)
1229 NS_ENSURE_ARG_POINTER(aPrintSettings
);
1230 NS_ENSURE_ARG_POINTER(aPrefName
);
1232 nsAutoString prtName
;
1233 // Get the Printer Name from the PrintSettings
1234 // to use as a prefix for Pref Names
1235 GetAdjustedPrinterName(aPrintSettings
, true, prtName
);
1237 const char* prefName
=
1238 GetPrefName(NS_LossyConvertUTF16toASCII(aPrefName
).get(), prtName
);
1240 NS_ENSURE_TRUE(prefName
, NS_ERROR_FAILURE
);
1243 nsresult rv
= Preferences::GetInt(prefName
, &iVal
);
1244 NS_ENSURE_SUCCESS(rv
, rv
);
1251 nsPrintOptions::InitPrintSettingsFromPrefs(nsIPrintSettings
* aPS
,
1252 bool aUsePNP
, uint32_t aFlags
)
1254 NS_ENSURE_ARG_POINTER(aPS
);
1257 aPS
->GetIsInitializedFromPrefs(&isInitialized
);
1262 nsAutoString prtName
;
1263 // read any non printer specific prefs
1264 // with empty printer name
1265 nsresult rv
= ReadPrefs(aPS
, prtName
, aFlags
);
1266 NS_ENSURE_SUCCESS(rv
, rv
);
1268 // Do not use printer name in Linux because GTK backend does not support
1269 // per printer settings.
1271 // Get the Printer Name from the PrintSettings
1272 // to use as a prefix for Pref Names
1273 rv
= GetAdjustedPrinterName(aPS
, aUsePNP
, prtName
);
1274 NS_ENSURE_SUCCESS(rv
, rv
);
1276 if (prtName
.IsEmpty()) {
1277 NS_WARNING("Caller should supply a printer name.");
1281 // Now read any printer specific prefs
1282 rv
= ReadPrefs(aPS
, prtName
, aFlags
);
1283 if (NS_SUCCEEDED(rv
))
1284 aPS
->SetIsInitializedFromPrefs(true);
1291 * Save all of the printer settings; if we can find a printer name, save
1292 * printer-specific preferences. Otherwise, save generic ones.
1295 nsPrintOptions::SavePrintSettingsToPrefs(nsIPrintSettings
*aPS
,
1296 bool aUsePrinterNamePrefix
,
1299 NS_ENSURE_ARG_POINTER(aPS
);
1300 nsAutoString prtName
;
1302 // Do not use printer name in Linux because GTK backend does not support
1303 // per printer settings.
1305 // Get the printer name from the PrinterSettings for an optional prefix.
1306 nsresult rv
= GetAdjustedPrinterName(aPS
, aUsePrinterNamePrefix
, prtName
);
1307 NS_ENSURE_SUCCESS(rv
, rv
);
1310 // Write the prefs, with or without a printer name prefix.
1311 return WritePrefs(aPS
, prtName
, aFlags
);
1315 //-----------------------------------------------------
1316 //-- Protected Methods --------------------------------
1317 //-----------------------------------------------------
1319 nsPrintOptions::ReadPrefDouble(const char * aPrefId
, double& aVal
)
1321 NS_ENSURE_ARG_POINTER(aPrefId
);
1324 nsresult rv
= Preferences::GetCString(aPrefId
, &str
);
1325 if (NS_SUCCEEDED(rv
) && !str
.IsEmpty()) {
1326 aVal
= atof(str
.get());
1332 nsPrintOptions::WritePrefDouble(const char * aPrefId
, double aVal
)
1334 NS_ENSURE_ARG_POINTER(aPrefId
);
1336 nsPrintfCString
str("%6.2f", aVal
);
1337 NS_ENSURE_TRUE(!str
.IsEmpty(), NS_ERROR_FAILURE
);
1339 return Preferences::SetCString(aPrefId
, str
);
1343 nsPrintOptions::ReadInchesToTwipsPref(const char * aPrefId
, int32_t& aTwips
,
1344 const char * aMarginPref
)
1347 nsresult rv
= Preferences::GetString(aPrefId
, &str
);
1348 if (NS_FAILED(rv
) || str
.IsEmpty()) {
1349 rv
= Preferences::GetString(aMarginPref
, &str
);
1351 if (NS_SUCCEEDED(rv
) && !str
.IsEmpty()) {
1353 float inches
= str
.ToFloat(&errCode
);
1354 if (NS_SUCCEEDED(errCode
)) {
1355 aTwips
= NS_INCHES_TO_INT_TWIPS(inches
);
1363 nsPrintOptions::WriteInchesFromTwipsPref(const char * aPrefId
, int32_t aTwips
)
1365 double inches
= NS_TWIPS_TO_INCHES(aTwips
);
1366 nsAutoCString inchesStr
;
1367 inchesStr
.AppendFloat(inches
);
1369 Preferences::SetCString(aPrefId
, inchesStr
);
1373 nsPrintOptions::ReadInchesIntToTwipsPref(const char * aPrefId
, int32_t& aTwips
,
1374 const char * aMarginPref
)
1377 nsresult rv
= Preferences::GetInt(aPrefId
, &value
);
1378 if (NS_FAILED(rv
)) {
1379 rv
= Preferences::GetInt(aMarginPref
, &value
);
1381 if (NS_SUCCEEDED(rv
)) {
1382 aTwips
= NS_INCHES_TO_INT_TWIPS(float(value
)/100.0f
);
1389 nsPrintOptions::WriteInchesIntFromTwipsPref(const char * aPrefId
, int32_t aTwips
)
1391 Preferences::SetInt(aPrefId
,
1392 int32_t(NS_TWIPS_TO_INCHES(aTwips
) * 100.0f
+ 0.5f
));
1396 nsPrintOptions::ReadJustification(const char * aPrefId
, int16_t& aJust
,
1400 nsAutoString justStr
;
1401 if (NS_SUCCEEDED(Preferences::GetString(aPrefId
, &justStr
))) {
1402 if (justStr
.EqualsASCII(kJustRight
)) {
1403 aJust
= nsIPrintSettings::kJustRight
;
1404 } else if (justStr
.EqualsASCII(kJustCenter
)) {
1405 aJust
= nsIPrintSettings::kJustCenter
;
1407 aJust
= nsIPrintSettings::kJustLeft
;
1412 //---------------------------------------------------
1414 nsPrintOptions::WriteJustification(const char * aPrefId
, int16_t aJust
)
1417 case nsIPrintSettings::kJustLeft
:
1418 Preferences::SetCString(aPrefId
, kJustLeft
);
1421 case nsIPrintSettings::kJustCenter
:
1422 Preferences::SetCString(aPrefId
, kJustCenter
);
1425 case nsIPrintSettings::kJustRight
:
1426 Preferences::SetCString(aPrefId
, kJustRight
);
1431 //----------------------------------------------------------------------
1432 // Testing of read/write prefs
1433 // This define turns on the testing module below
1434 // so at start up it writes and reads the prefs.
1442 nsCOMPtr
<nsIPrintSettings
> ps
;
1444 nsCOMPtr
<nsIPrintOptions
> printService
=
1445 do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv
);
1446 if (NS_SUCCEEDED(rv
)) {
1447 rv
= printService
->CreatePrintSettings(getter_AddRefs(ps
));
1451 ps
->SetPrintOptions(nsIPrintSettings::kPrintOddPages
, true);
1452 ps
->SetPrintOptions(nsIPrintSettings::kPrintEvenPages
, false);
1453 ps
->SetMarginTop(1.0);
1454 ps
->SetMarginLeft(1.0);
1455 ps
->SetMarginBottom(1.0);
1456 ps
->SetMarginRight(1.0);
1457 ps
->SetScaling(0.5);
1458 ps
->SetPrintBGColors(true);
1459 ps
->SetPrintBGImages(true);
1460 ps
->SetPrintRange(15);
1461 ps
->SetHeaderStrLeft(NS_ConvertUTF8toUTF16("Left").get());
1462 ps
->SetHeaderStrCenter(NS_ConvertUTF8toUTF16("Center").get());
1463 ps
->SetHeaderStrRight(NS_ConvertUTF8toUTF16("Right").get());
1464 ps
->SetFooterStrLeft(NS_ConvertUTF8toUTF16("Left").get());
1465 ps
->SetFooterStrCenter(NS_ConvertUTF8toUTF16("Center").get());
1466 ps
->SetFooterStrRight(NS_ConvertUTF8toUTF16("Right").get());
1467 ps
->SetPaperName(NS_ConvertUTF8toUTF16("Paper Name").get());
1468 ps
->SetPlexName(NS_ConvertUTF8toUTF16("Plex Name").get());
1469 ps
->SetPaperSizeType(10);
1470 ps
->SetPaperData(1);
1471 ps
->SetPaperWidth(100.0);
1472 ps
->SetPaperHeight(50.0);
1473 ps
->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters
);
1474 ps
->SetPrintReversed(true);
1475 ps
->SetPrintInColor(true);
1476 ps
->SetOrientation(nsIPrintSettings::kLandscapeOrientation
);
1477 ps
->SetPrintCommand(NS_ConvertUTF8toUTF16("Command").get());
1478 ps
->SetNumCopies(2);
1479 ps
->SetPrinterName(NS_ConvertUTF8toUTF16("Printer Name").get());
1480 ps
->SetPrintToFile(true);
1481 ps
->SetToFileName(NS_ConvertUTF8toUTF16("File Name").get());
1482 ps
->SetPrintPageDelay(1000);
1483 ps
->SetShrinkToFit(true);
1485 struct SettingsType
{
1489 SettingsType gSettings
[] = {
1490 {"OddEven", nsIPrintSettings::kInitSaveOddEvenPages
},
1491 {kPrintHeaderStrLeft
, nsIPrintSettings::kInitSaveHeaderLeft
},
1492 {kPrintHeaderStrCenter
, nsIPrintSettings::kInitSaveHeaderCenter
},
1493 {kPrintHeaderStrRight
, nsIPrintSettings::kInitSaveHeaderRight
},
1494 {kPrintFooterStrLeft
, nsIPrintSettings::kInitSaveFooterLeft
},
1495 {kPrintFooterStrCenter
, nsIPrintSettings::kInitSaveFooterCenter
},
1496 {kPrintFooterStrRight
, nsIPrintSettings::kInitSaveFooterRight
},
1497 {kPrintBGColors
, nsIPrintSettings::kInitSaveBGColors
},
1498 {kPrintBGImages
, nsIPrintSettings::kInitSaveBGImages
},
1499 {kPrintShrinkToFit
, nsIPrintSettings::kInitSaveShrinkToFit
},
1500 {kPrintPaperSize
, nsIPrintSettings::kInitSavePaperSize
},
1501 {kPrintPlexName
, nsIPrintSettings::kInitSavePlexName
},
1502 {kPrintPaperData
, nsIPrintSettings::kInitSavePaperData
},
1503 {kPrintReversed
, nsIPrintSettings::kInitSaveReversed
},
1504 {kPrintInColor
, nsIPrintSettings::kInitSaveInColor
},
1505 {kPrintColorspace
, nsIPrintSettings::kInitSaveColorspace
},
1506 {kPrintResolutionName
, nsIPrintSettings::kInitSaveResolutionName
},
1507 {kPrintDownloadFonts
, nsIPrintSettings::kInitSaveDownloadFonts
},
1508 {kPrintOrientation
, nsIPrintSettings::kInitSaveOrientation
},
1509 {kPrintCommand
, nsIPrintSettings::kInitSavePrintCommand
},
1510 {kPrinterName
, nsIPrintSettings::kInitSavePrinterName
},
1511 {kPrintToFile
, nsIPrintSettings::kInitSavePrintToFile
},
1512 {kPrintToFileName
, nsIPrintSettings::kInitSaveToFileName
},
1513 {kPrintPageDelay
, nsIPrintSettings::kInitSavePageDelay
},
1514 {"Margins", nsIPrintSettings::kInitSaveMargins
},
1515 {"All", nsIPrintSettings::kInitSaveAll
},
1518 nsString prefix
; prefix
.AssignLiteral("Printer Name");
1520 while (gSettings
[i
].mName
!= nullptr) {
1521 printf("------------------------------------------------\n");
1522 printf("%d) %s -> 0x%X\n", i
, gSettings
[i
].mName
, gSettings
[i
].mFlag
);
1523 printService
->SavePrintSettingsToPrefs(ps
, true, gSettings
[i
].mFlag
);
1524 printService
->InitPrintSettingsFromPrefs(ps
, true,
1525 gSettings
[i
].mFlag
);