1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2000
20 * the Initial Developer. All Rights Reserved.
23 * Jessica Blanco <jblanco@us.ibm.com>
24 * Bastiaan Jacques <b.jacques@planet.nl>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsPrintOptionsImpl.h"
42 #include "nsReadableUtils.h"
43 #include "nsPrintSettingsImpl.h"
45 #include "nsIDOMWindow.h"
46 #include "nsIServiceManager.h"
47 #include "nsIDialogParamBlock.h"
49 #include "nsISupportsPrimitives.h"
50 #include "nsIWindowWatcher.h"
51 #include "nsIDOMWindowInternal.h"
52 #include "nsVoidArray.h"
53 #include "nsSupportsArray.h"
57 #include "nsIPrefBranch.h"
58 #include "nsIPrefService.h"
59 #include "nsIServiceManager.h"
61 #include "nsIStringEnumerator.h"
62 #include "nsISupportsPrimitives.h"
64 #include "nsAutoPtr.h"
66 NS_IMPL_ISUPPORTS2(nsPrintOptions
, nsIPrintOptions
, nsIPrintSettingsService
)
69 static const char kMarginTop
[] = "print_margin_top";
70 static const char kMarginLeft
[] = "print_margin_left";
71 static const char kMarginBottom
[] = "print_margin_bottom";
72 static const char kMarginRight
[] = "print_margin_right";
73 static const char kEdgeTop
[] = "print_edge_top";
74 static const char kEdgeLeft
[] = "print_edge_left";
75 static const char kEdgeBottom
[] = "print_edge_bottom";
76 static const char kEdgeRight
[] = "print_edge_right";
77 static const char kUnwriteableMarginTop
[] = "print_unwriteable_margin_top";
78 static const char kUnwriteableMarginLeft
[] = "print_unwriteable_margin_left";
79 static const char kUnwriteableMarginBottom
[] = "print_unwriteable_margin_bottom";
80 static const char kUnwriteableMarginRight
[] = "print_unwriteable_margin_right";
82 // Prefs for Print Options
83 static const char kPrintEvenPages
[] = "print_evenpages";
84 static const char kPrintOddPages
[] = "print_oddpages";
85 static const char kPrintHeaderStrLeft
[] = "print_headerleft";
86 static const char kPrintHeaderStrCenter
[] = "print_headercenter";
87 static const char kPrintHeaderStrRight
[] = "print_headerright";
88 static const char kPrintFooterStrLeft
[] = "print_footerleft";
89 static const char kPrintFooterStrCenter
[] = "print_footercenter";
90 static const char kPrintFooterStrRight
[] = "print_footerright";
93 static const char kPrintReversed
[] = "print_reversed";
94 static const char kPrintInColor
[] = "print_in_color";
95 static const char kPrintPaperName
[] = "print_paper_name";
96 static const char kPrintPlexName
[] = "print_plex_name";
97 static const char kPrintPaperSizeType
[] = "print_paper_size_type";
98 static const char kPrintPaperData
[] = "print_paper_data";
99 static const char kPrintPaperSizeUnit
[] = "print_paper_size_unit";
100 static const char kPrintPaperWidth
[] = "print_paper_width";
101 static const char kPrintPaperHeight
[] = "print_paper_height";
102 static const char kPrintColorspace
[] = "print_colorspace";
103 static const char kPrintResolutionName
[]= "print_resolution_name";
104 static const char kPrintDownloadFonts
[] = "print_downloadfonts";
105 static const char kPrintOrientation
[] = "print_orientation";
106 static const char kPrintCommand
[] = "print_command";
107 static const char kPrinterName
[] = "print_printer";
108 static const char kPrintToFile
[] = "print_to_file";
109 static const char kPrintToFileName
[] = "print_to_filename";
110 static const char kPrintPageDelay
[] = "print_pagedelay";
111 static const char kPrintBGColors
[] = "print_bgcolor";
112 static const char kPrintBGImages
[] = "print_bgimages";
113 static const char kPrintShrinkToFit
[] = "print_shrink_to_fit";
114 static const char kPrintScaling
[] = "print_scaling";
116 static const char kJustLeft
[] = "left";
117 static const char kJustCenter
[] = "center";
118 static const char kJustRight
[] = "right";
120 #define NS_PRINTER_ENUMERATOR_CONTRACTID "@mozilla.org/gfx/printerenumerator;1"
122 nsPrintOptions::nsPrintOptions()
126 nsPrintOptions::~nsPrintOptions()
131 nsPrintOptions::Init()
134 nsCOMPtr
<nsIPrefService
> prefService
=
135 do_GetService(NS_PREFSERVICE_CONTRACTID
, &rv
);
136 NS_ENSURE_SUCCESS(rv
, rv
);
138 return prefService
->GetBranch("print.", getter_AddRefs(mPrefBranch
));
142 nsPrintOptions::ShowPrintSetupDialog(nsIPrintSettings
*aPS
)
144 NS_ENSURE_ARG_POINTER(aPS
);
147 // create a nsISupportsArray of the parameters
148 // being passed to the window
149 nsCOMPtr
<nsISupportsArray
> array
;
150 rv
= NS_NewISupportsArray(getter_AddRefs(array
));
151 NS_ENSURE_SUCCESS(rv
, rv
);
153 nsCOMPtr
<nsISupports
> psSupports
= do_QueryInterface(aPS
);
154 NS_ASSERTION(psSupports
, "PrintSettings must be a supports");
155 array
->AppendElement(psSupports
);
157 nsCOMPtr
<nsIDialogParamBlock
> ioParamBlock
=
158 do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID
, &rv
);
159 NS_ENSURE_SUCCESS(rv
, rv
);
161 ioParamBlock
->SetInt(0, 0);
163 nsCOMPtr
<nsISupports
> blkSupps
= do_QueryInterface(ioParamBlock
);
164 NS_ASSERTION(blkSupps
, "IOBlk must be a supports");
165 array
->AppendElement(blkSupps
);
167 nsCOMPtr
<nsIWindowWatcher
> wwatch
=
168 do_GetService(NS_WINDOWWATCHER_CONTRACTID
, &rv
);
169 NS_ENSURE_SUCCESS(rv
, rv
);
171 nsCOMPtr
<nsIDOMWindow
> active
;
172 wwatch
->GetActiveWindow(getter_AddRefs(active
));
173 nsCOMPtr
<nsIDOMWindowInternal
> parent
= do_QueryInterface(active
);
174 // null |parent| is non-fatal
176 nsCOMPtr
<nsIDOMWindow
> newWindow
;
178 return wwatch
->OpenWindow(parent
,
179 "chrome://global/content/printPageSetup.xul",
180 "_blank","chrome,modal,centerscreen", array
,
181 getter_AddRefs(newWindow
));
184 /** ---------------------------------------------------
185 * Helper function - Creates the "prefix" for the pref
186 * It is either "print."
187 * or "print.printer_<print name>."
190 nsPrintOptions::GetPrefName(const char * aPrefName
,
191 const nsAString
& aPrinterName
)
193 if (!aPrefName
|| !*aPrefName
) {
194 NS_ERROR("Must have a valid pref name!");
198 mPrefName
.Truncate(); /* mPrefName = ""; */
200 if (aPrinterName
.Length()) {
201 mPrefName
.Append("printer_");
202 AppendUTF16toUTF8(aPrinterName
, mPrefName
);
203 mPrefName
.Append(".");
205 mPrefName
+= aPrefName
;
207 return mPrefName
.get();
210 //----------------------------------------------------------------------
211 // Testing of read/write prefs
212 // This define controls debug output
214 static void WriteDebugStr(const char* aArg1
, const char* aArg2
,
215 const PRUnichar
* aStr
)
220 str
.ReplaceChar(s
, r
);
222 printf("%s %s = %s \n", aArg1
, aArg2
, ToNewUTF8String(str
));
224 const char* kWriteStr
= "Write Pref:";
225 const char* kReadStr
= "Read Pref:";
226 #define DUMP_STR(_a1, _a2, _a3) WriteDebugStr((_a1), GetPrefName((_a2), \
228 #define DUMP_BOOL(_a1, _a2, _a3) printf("%s %s = %s \n", (_a1), \
229 GetPrefName((_a2), aPrefName), (_a3)?"T":"F");
230 #define DUMP_INT(_a1, _a2, _a3) printf("%s %s = %d \n", (_a1), \
231 GetPrefName((_a2), aPrefName), (_a3));
232 #define DUMP_DBL(_a1, _a2, _a3) printf("%s %s = %10.5f \n", (_a1), \
233 GetPrefName((_a2), aPrefName), (_a3));
235 #define DUMP_STR(_a1, _a2, _a3)
236 #define DUMP_BOOL(_a1, _a2, _a3)
237 #define DUMP_INT(_a1, _a2, _a3)
238 #define DUMP_DBL(_a1, _a2, _a3)
239 #endif /* DEBUG_rods_X */
240 //----------------------------------------------------------------------
243 * This will either read in the generic prefs (not specific to a printer)
244 * or read the prefs in using the printer name to qualify.
245 * It is either "print.attr_name" or "print.printer_HPLasr5.attr_name"
248 nsPrintOptions::ReadPrefs(nsIPrintSettings
* aPS
, const nsAString
& aPrinterName
,
251 NS_ENSURE_STATE(mPrefBranch
);
252 NS_ENSURE_ARG_POINTER(aPS
);
254 if (aFlags
& nsIPrintSettings::kInitSaveMargins
) {
255 nscoord halfInch
= NS_INCHES_TO_TWIPS(0.5);
257 margin
.SizeTo(halfInch
, halfInch
, halfInch
, halfInch
);
258 ReadInchesToTwipsPref(GetPrefName(kMarginTop
, aPrinterName
), margin
.top
,
260 DUMP_INT(kReadStr
, kMarginTop
, margin
.top
);
261 ReadInchesToTwipsPref(GetPrefName(kMarginLeft
, aPrinterName
), margin
.left
,
263 DUMP_INT(kReadStr
, kMarginLeft
, margin
.left
);
264 ReadInchesToTwipsPref(GetPrefName(kMarginBottom
, aPrinterName
),
265 margin
.bottom
, kMarginBottom
);
266 DUMP_INT(kReadStr
, kMarginBottom
, margin
.bottom
);
267 ReadInchesToTwipsPref(GetPrefName(kMarginRight
, aPrinterName
), margin
.right
,
269 DUMP_INT(kReadStr
, kMarginRight
, margin
.right
);
270 aPS
->SetMarginInTwips(margin
);
273 if (aFlags
& nsIPrintSettings::kInitSaveEdges
) {
275 margin
.SizeTo(0,0,0,0);
276 ReadInchesIntToTwipsPref(GetPrefName(kEdgeTop
, aPrinterName
), margin
.top
,
278 DUMP_INT(kReadStr
, kEdgeTop
, margin
.top
);
279 ReadInchesIntToTwipsPref(GetPrefName(kEdgeLeft
, aPrinterName
), margin
.left
,
281 DUMP_INT(kReadStr
, kEdgeLeft
, margin
.left
);
282 ReadInchesIntToTwipsPref(GetPrefName(kEdgeBottom
, aPrinterName
),
283 margin
.bottom
, kEdgeBottom
);
284 DUMP_INT(kReadStr
, kEdgeBottom
, margin
.bottom
);
285 ReadInchesIntToTwipsPref(GetPrefName(kEdgeRight
, aPrinterName
), margin
.right
,
287 DUMP_INT(kReadStr
, kEdgeRight
, margin
.right
);
288 aPS
->SetEdgeInTwips(margin
);
291 if (aFlags
& nsIPrintSettings::kInitSaveUnwriteableMargins
) {
293 margin
.SizeTo(0,0,0,0);
294 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginTop
, aPrinterName
), margin
.top
,
295 kUnwriteableMarginTop
);
296 DUMP_INT(kReadStr
, kUnwriteableMarginTop
, margin
.top
);
297 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginLeft
, aPrinterName
), margin
.left
,
298 kUnwriteableMarginLeft
);
299 DUMP_INT(kReadStr
, kUnwriteableMarginLeft
, margin
.left
);
300 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginBottom
, aPrinterName
),
301 margin
.bottom
, kUnwriteableMarginBottom
);
302 DUMP_INT(kReadStr
, kUnwriteableMarginBottom
, margin
.bottom
);
303 ReadInchesIntToTwipsPref(GetPrefName(kUnwriteableMarginRight
, aPrinterName
), margin
.right
,
304 kUnwriteableMarginRight
);
305 DUMP_INT(kReadStr
, kUnwriteableMarginRight
, margin
.right
);
306 aPS
->SetUnwriteableMarginInTwips(margin
);
314 #define GETBOOLPREF(_prefname, _retval) \
316 mPrefBranch->GetBoolPref( \
317 GetPrefName(_prefname, aPrinterName), _retval \
321 #define GETSTRPREF(_prefname, _retval) \
324 GetPrefName(_prefname, aPrinterName), _retval \
328 #define GETINTPREF(_prefname, _retval) \
330 mPrefBranch->GetIntPref( \
331 GetPrefName(_prefname, aPrinterName), _retval \
335 #define GETDBLPREF(_prefname, _retval) \
338 GetPrefName(_prefname, aPrinterName), _retval \
342 // Paper size prefs are read as a group
343 if (aFlags
& nsIPrintSettings::kInitSavePaperSize
) {
344 PRInt32 sizeUnit
, sizeType
;
345 double width
, height
;
347 PRBool success
= GETINTPREF(kPrintPaperSizeUnit
, &sizeUnit
)
348 && GETINTPREF(kPrintPaperSizeType
, &sizeType
)
349 && GETDBLPREF(kPrintPaperWidth
, width
)
350 && GETDBLPREF(kPrintPaperHeight
, height
)
351 && GETSTRPREF(kPrintPaperName
, str
);
353 // Bug 315687: Sanity check paper size to avoid paper size values in
354 // mm when the size unit flag is inches. The value 100 is arbitrary
355 // and can be changed.
357 success
= (sizeUnit
!= nsIPrintSettings::kPaperSizeInches
)
363 aPS
->SetPaperSizeUnit(sizeUnit
);
364 DUMP_INT(kReadStr
, kPrintPaperSizeUnit
, sizeUnit
);
365 aPS
->SetPaperSizeType(sizeType
);
366 DUMP_INT(kReadStr
, kPrintPaperSizeType
, sizeType
);
367 aPS
->SetPaperWidth(width
);
368 DUMP_DBL(kReadStr
, kPrintPaperWidth
, width
);
369 aPS
->SetPaperHeight(height
);
370 DUMP_DBL(kReadStr
, kPrintPaperHeight
, height
);
371 aPS
->SetPaperName(str
.get());
372 DUMP_STR(kReadStr
, kPrintPaperName
, str
.get());
376 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
377 if (GETBOOLPREF(kPrintEvenPages
, &b
)) {
378 aPS
->SetPrintOptions(nsIPrintSettings::kPrintEvenPages
, b
);
379 DUMP_BOOL(kReadStr
, kPrintEvenPages
, b
);
383 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
384 if (GETBOOLPREF(kPrintOddPages
, &b
)) {
385 aPS
->SetPrintOptions(nsIPrintSettings::kPrintOddPages
, b
);
386 DUMP_BOOL(kReadStr
, kPrintOddPages
, b
);
390 if (aFlags
& nsIPrintSettings::kInitSaveHeaderLeft
) {
391 if (GETSTRPREF(kPrintHeaderStrLeft
, str
)) {
392 aPS
->SetHeaderStrLeft(str
.get());
393 DUMP_STR(kReadStr
, kPrintHeaderStrLeft
, str
.get());
397 if (aFlags
& nsIPrintSettings::kInitSaveHeaderCenter
) {
398 if (GETSTRPREF(kPrintHeaderStrCenter
, str
)) {
399 aPS
->SetHeaderStrCenter(str
.get());
400 DUMP_STR(kReadStr
, kPrintHeaderStrCenter
, str
.get());
404 if (aFlags
& nsIPrintSettings::kInitSaveHeaderRight
) {
405 if (GETSTRPREF(kPrintHeaderStrRight
, str
)) {
406 aPS
->SetHeaderStrRight(str
.get());
407 DUMP_STR(kReadStr
, kPrintHeaderStrRight
, str
.get());
411 if (aFlags
& nsIPrintSettings::kInitSaveFooterLeft
) {
412 if (GETSTRPREF(kPrintFooterStrLeft
, str
)) {
413 aPS
->SetFooterStrLeft(str
.get());
414 DUMP_STR(kReadStr
, kPrintFooterStrLeft
, str
.get());
418 if (aFlags
& nsIPrintSettings::kInitSaveFooterCenter
) {
419 if (GETSTRPREF(kPrintFooterStrCenter
, str
)) {
420 aPS
->SetFooterStrCenter(str
.get());
421 DUMP_STR(kReadStr
, kPrintFooterStrCenter
, str
.get());
425 if (aFlags
& nsIPrintSettings::kInitSaveFooterRight
) {
426 if (GETSTRPREF(kPrintFooterStrRight
, str
)) {
427 aPS
->SetFooterStrRight(str
.get());
428 DUMP_STR(kReadStr
, kPrintFooterStrRight
, str
.get());
432 if (aFlags
& nsIPrintSettings::kInitSaveBGColors
) {
433 if (GETBOOLPREF(kPrintBGColors
, &b
)) {
434 aPS
->SetPrintBGColors(b
);
435 DUMP_BOOL(kReadStr
, kPrintBGColors
, b
);
439 if (aFlags
& nsIPrintSettings::kInitSaveBGImages
) {
440 if (GETBOOLPREF(kPrintBGImages
, &b
)) {
441 aPS
->SetPrintBGImages(b
);
442 DUMP_BOOL(kReadStr
, kPrintBGImages
, b
);
446 if (aFlags
& nsIPrintSettings::kInitSaveReversed
) {
447 if (GETBOOLPREF(kPrintReversed
, &b
)) {
448 aPS
->SetPrintReversed(b
);
449 DUMP_BOOL(kReadStr
, kPrintReversed
, b
);
453 if (aFlags
& nsIPrintSettings::kInitSaveInColor
) {
454 if (GETBOOLPREF(kPrintInColor
, &b
)) {
455 aPS
->SetPrintInColor(b
);
456 DUMP_BOOL(kReadStr
, kPrintInColor
, b
);
460 if (aFlags
& nsIPrintSettings::kInitSavePlexName
) {
461 if (GETSTRPREF(kPrintPlexName
, str
)) {
462 aPS
->SetPlexName(str
.get());
463 DUMP_STR(kReadStr
, kPrintPlexName
, str
.get());
467 if (aFlags
& nsIPrintSettings::kInitSavePaperData
) {
468 if (GETINTPREF(kPrintPaperData
, &iVal
)) {
469 aPS
->SetPaperData(iVal
);
470 DUMP_INT(kReadStr
, kPrintPaperData
, iVal
);
474 if (aFlags
& nsIPrintSettings::kInitSaveColorspace
) {
475 if (GETSTRPREF(kPrintColorspace
, str
)) {
476 aPS
->SetColorspace(str
.get());
477 DUMP_STR(kReadStr
, kPrintColorspace
, str
.get());
481 if (aFlags
& nsIPrintSettings::kInitSaveResolutionName
) {
482 if (GETSTRPREF(kPrintResolutionName
, str
)) {
483 aPS
->SetResolutionName(str
.get());
484 DUMP_STR(kReadStr
, kPrintResolutionName
, str
.get());
488 if (aFlags
& nsIPrintSettings::kInitSaveDownloadFonts
) {
489 if (GETBOOLPREF(kPrintDownloadFonts
, &b
)) {
490 aPS
->SetDownloadFonts(b
);
491 DUMP_BOOL(kReadStr
, kPrintDownloadFonts
, b
);
495 if (aFlags
& nsIPrintSettings::kInitSaveOrientation
) {
496 if (GETINTPREF(kPrintOrientation
, &iVal
)) {
497 aPS
->SetOrientation(iVal
);
498 DUMP_INT(kReadStr
, kPrintOrientation
, iVal
);
502 if (aFlags
& nsIPrintSettings::kInitSavePrintCommand
) {
503 if (GETSTRPREF(kPrintCommand
, str
)) {
504 aPS
->SetPrintCommand(str
.get());
505 DUMP_STR(kReadStr
, kPrintCommand
, str
.get());
509 if (aFlags
& nsIPrintSettings::kInitSavePrintToFile
) {
510 if (GETBOOLPREF(kPrintToFile
, &b
)) {
511 aPS
->SetPrintToFile(b
);
512 DUMP_BOOL(kReadStr
, kPrintToFile
, b
);
516 if (aFlags
& nsIPrintSettings::kInitSaveToFileName
) {
517 if (GETSTRPREF(kPrintToFileName
, str
)) {
518 aPS
->SetToFileName(str
.get());
519 DUMP_STR(kReadStr
, kPrintToFileName
, str
.get());
523 if (aFlags
& nsIPrintSettings::kInitSavePageDelay
) {
524 if (GETINTPREF(kPrintPageDelay
, &iVal
)) {
525 aPS
->SetPrintPageDelay(iVal
);
526 DUMP_INT(kReadStr
, kPrintPageDelay
, iVal
);
530 if (aFlags
& nsIPrintSettings::kInitSaveShrinkToFit
) {
531 if (GETBOOLPREF(kPrintShrinkToFit
, &b
)) {
532 aPS
->SetShrinkToFit(b
);
533 DUMP_BOOL(kReadStr
, kPrintShrinkToFit
, b
);
537 if (aFlags
& nsIPrintSettings::kInitSaveScaling
) {
538 if (GETDBLPREF(kPrintScaling
, dbl
)) {
539 aPS
->SetScaling(dbl
);
540 DUMP_DBL(kReadStr
, kPrintScaling
, dbl
);
550 /** ---------------------------------------------------
551 * See documentation in nsPrintOptionsImpl.h
552 * @update 1/12/01 rods
555 nsPrintOptions::WritePrefs(nsIPrintSettings
*aPS
, const nsAString
& aPrinterName
,
558 NS_ENSURE_ARG_POINTER(aPS
);
559 NS_ENSURE_STATE(mPrefBranch
);
562 if (aFlags
& nsIPrintSettings::kInitSaveMargins
) {
563 if (NS_SUCCEEDED(aPS
->GetMarginInTwips(margin
))) {
564 WriteInchesFromTwipsPref(GetPrefName(kMarginTop
, aPrinterName
),
566 DUMP_INT(kWriteStr
, kMarginTop
, margin
.top
);
567 WriteInchesFromTwipsPref(GetPrefName(kMarginLeft
, aPrinterName
),
569 DUMP_INT(kWriteStr
, kMarginLeft
, margin
.top
);
570 WriteInchesFromTwipsPref(GetPrefName(kMarginBottom
, aPrinterName
),
572 DUMP_INT(kWriteStr
, kMarginBottom
, margin
.top
);
573 WriteInchesFromTwipsPref(GetPrefName(kMarginRight
, aPrinterName
),
575 DUMP_INT(kWriteStr
, kMarginRight
, margin
.top
);
580 if (aFlags
& nsIPrintSettings::kInitSaveEdges
) {
581 if (NS_SUCCEEDED(aPS
->GetEdgeInTwips(edge
))) {
582 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeTop
, aPrinterName
),
584 DUMP_INT(kWriteStr
, kEdgeTop
, edge
.top
);
585 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeLeft
, aPrinterName
),
587 DUMP_INT(kWriteStr
, kEdgeLeft
, edge
.top
);
588 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeBottom
, aPrinterName
),
590 DUMP_INT(kWriteStr
, kEdgeBottom
, edge
.top
);
591 WriteInchesIntFromTwipsPref(GetPrefName(kEdgeRight
, aPrinterName
),
593 DUMP_INT(kWriteStr
, kEdgeRight
, edge
.top
);
597 nsMargin unwriteableMargin
;
598 if (aFlags
& nsIPrintSettings::kInitSaveUnwriteableMargins
) {
599 if (NS_SUCCEEDED(aPS
->GetUnwriteableMarginInTwips(unwriteableMargin
))) {
600 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginTop
, aPrinterName
),
601 unwriteableMargin
.top
);
602 DUMP_INT(kWriteStr
, kUnwriteableMarginTop
, unwriteableMargin
.top
);
603 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginLeft
, aPrinterName
),
604 unwriteableMargin
.left
);
605 DUMP_INT(kWriteStr
, kUnwriteableMarginLeft
, unwriteableMargin
.top
);
606 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginBottom
, aPrinterName
),
607 unwriteableMargin
.bottom
);
608 DUMP_INT(kWriteStr
, kUnwriteableMarginBottom
, unwriteableMargin
.top
);
609 WriteInchesIntFromTwipsPref(GetPrefName(kUnwriteableMarginRight
, aPrinterName
),
610 unwriteableMargin
.right
);
611 DUMP_INT(kWriteStr
, kUnwriteableMarginRight
, unwriteableMargin
.top
);
615 // Paper size prefs are saved as a group
616 if (aFlags
& nsIPrintSettings::kInitSavePaperSize
) {
617 PRInt16 sizeUnit
, sizeType
;
618 double width
, height
;
622 NS_SUCCEEDED(aPS
->GetPaperSizeUnit(&sizeUnit
)) &&
623 NS_SUCCEEDED(aPS
->GetPaperSizeType(&sizeType
)) &&
624 NS_SUCCEEDED(aPS
->GetPaperWidth(&width
)) &&
625 NS_SUCCEEDED(aPS
->GetPaperHeight(&height
)) &&
626 NS_SUCCEEDED(aPS
->GetPaperName(&name
))
628 DUMP_INT(kWriteStr
, kPrintPaperSizeUnit
, sizeUnit
);
629 mPrefBranch
->SetIntPref(GetPrefName(kPrintPaperSizeUnit
, aPrinterName
),
631 DUMP_INT(kWriteStr
, kPrintPaperSizeType
, sizeType
);
632 mPrefBranch
->SetIntPref(GetPrefName(kPrintPaperSizeType
, aPrinterName
),
634 DUMP_DBL(kWriteStr
, kPrintPaperWidth
, width
);
635 WritePrefDouble(GetPrefName(kPrintPaperWidth
, aPrinterName
), width
);
636 DUMP_DBL(kWriteStr
, kPrintPaperHeight
, height
);
637 WritePrefDouble(GetPrefName(kPrintPaperHeight
, aPrinterName
), height
);
638 DUMP_STR(kWriteStr
, kPrintPaperName
, name
);
639 WritePrefString(name
, GetPrefName(kPrintPaperName
, aPrinterName
));
649 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
650 if (NS_SUCCEEDED(aPS
->GetPrintOptions(nsIPrintSettings::kPrintEvenPages
,
652 DUMP_BOOL(kWriteStr
, kPrintEvenPages
, b
);
653 mPrefBranch
->SetBoolPref(GetPrefName(kPrintEvenPages
, aPrinterName
),
658 if (aFlags
& nsIPrintSettings::kInitSaveOddEvenPages
) {
659 if (NS_SUCCEEDED(aPS
->GetPrintOptions(nsIPrintSettings::kPrintOddPages
,
661 DUMP_BOOL(kWriteStr
, kPrintOddPages
, b
);
662 mPrefBranch
->SetBoolPref(GetPrefName(kPrintOddPages
, aPrinterName
),
667 if (aFlags
& nsIPrintSettings::kInitSaveHeaderLeft
) {
668 if (NS_SUCCEEDED(aPS
->GetHeaderStrLeft(&uStr
))) {
669 DUMP_STR(kWriteStr
, kPrintHeaderStrLeft
, uStr
);
670 WritePrefString(uStr
, GetPrefName(kPrintHeaderStrLeft
, aPrinterName
));
674 if (aFlags
& nsIPrintSettings::kInitSaveHeaderCenter
) {
675 if (NS_SUCCEEDED(aPS
->GetHeaderStrCenter(&uStr
))) {
676 DUMP_STR(kWriteStr
, kPrintHeaderStrCenter
, uStr
);
677 WritePrefString(uStr
, GetPrefName(kPrintHeaderStrCenter
, aPrinterName
));
681 if (aFlags
& nsIPrintSettings::kInitSaveHeaderRight
) {
682 if (NS_SUCCEEDED(aPS
->GetHeaderStrRight(&uStr
))) {
683 DUMP_STR(kWriteStr
, kPrintHeaderStrRight
, uStr
);
684 WritePrefString(uStr
, GetPrefName(kPrintHeaderStrRight
, aPrinterName
));
688 if (aFlags
& nsIPrintSettings::kInitSaveFooterLeft
) {
689 if (NS_SUCCEEDED(aPS
->GetFooterStrLeft(&uStr
))) {
690 DUMP_STR(kWriteStr
, kPrintFooterStrLeft
, uStr
);
691 WritePrefString(uStr
, GetPrefName(kPrintFooterStrLeft
, aPrinterName
));
695 if (aFlags
& nsIPrintSettings::kInitSaveFooterCenter
) {
696 if (NS_SUCCEEDED(aPS
->GetFooterStrCenter(&uStr
))) {
697 DUMP_STR(kWriteStr
, kPrintFooterStrCenter
, uStr
);
698 WritePrefString(uStr
, GetPrefName(kPrintFooterStrCenter
, aPrinterName
));
702 if (aFlags
& nsIPrintSettings::kInitSaveFooterRight
) {
703 if (NS_SUCCEEDED(aPS
->GetFooterStrRight(&uStr
))) {
704 DUMP_STR(kWriteStr
, kPrintFooterStrRight
, uStr
);
705 WritePrefString(uStr
, GetPrefName(kPrintFooterStrRight
, aPrinterName
));
709 if (aFlags
& nsIPrintSettings::kInitSaveBGColors
) {
710 if (NS_SUCCEEDED(aPS
->GetPrintBGColors(&b
))) {
711 DUMP_BOOL(kWriteStr
, kPrintBGColors
, b
);
712 mPrefBranch
->SetBoolPref(GetPrefName(kPrintBGColors
, aPrinterName
), b
);
716 if (aFlags
& nsIPrintSettings::kInitSaveBGImages
) {
717 if (NS_SUCCEEDED(aPS
->GetPrintBGImages(&b
))) {
718 DUMP_BOOL(kWriteStr
, kPrintBGImages
, b
);
719 mPrefBranch
->SetBoolPref(GetPrefName(kPrintBGImages
, aPrinterName
), b
);
723 if (aFlags
& nsIPrintSettings::kInitSaveReversed
) {
724 if (NS_SUCCEEDED(aPS
->GetPrintReversed(&b
))) {
725 DUMP_BOOL(kWriteStr
, kPrintReversed
, b
);
726 mPrefBranch
->SetBoolPref(GetPrefName(kPrintReversed
, aPrinterName
), b
);
730 if (aFlags
& nsIPrintSettings::kInitSaveInColor
) {
731 if (NS_SUCCEEDED(aPS
->GetPrintInColor(&b
))) {
732 DUMP_BOOL(kWriteStr
, kPrintInColor
, b
);
733 mPrefBranch
->SetBoolPref(GetPrefName(kPrintInColor
, aPrinterName
), b
);
737 if (aFlags
& nsIPrintSettings::kInitSavePlexName
) {
738 if (NS_SUCCEEDED(aPS
->GetPlexName(&uStr
))) {
739 DUMP_STR(kWriteStr
, kPrintPlexName
, uStr
);
740 WritePrefString(uStr
, GetPrefName(kPrintPlexName
, aPrinterName
));
744 if (aFlags
& nsIPrintSettings::kInitSavePaperData
) {
745 if (NS_SUCCEEDED(aPS
->GetPaperData(&iVal16
))) {
746 DUMP_INT(kWriteStr
, kPrintPaperData
, iVal16
);
747 mPrefBranch
->SetIntPref(GetPrefName(kPrintPaperData
, aPrinterName
),
752 if (aFlags
& nsIPrintSettings::kInitSaveColorspace
) {
753 if (NS_SUCCEEDED(aPS
->GetColorspace(&uStr
))) {
754 DUMP_STR(kWriteStr
, kPrintColorspace
, uStr
);
755 WritePrefString(uStr
, GetPrefName(kPrintColorspace
, aPrinterName
));
759 if (aFlags
& nsIPrintSettings::kInitSaveResolutionName
) {
760 if (NS_SUCCEEDED(aPS
->GetResolutionName(&uStr
))) {
761 DUMP_STR(kWriteStr
, kPrintResolutionName
, uStr
);
762 WritePrefString(uStr
, GetPrefName(kPrintResolutionName
, aPrinterName
));
766 if (aFlags
& nsIPrintSettings::kInitSaveDownloadFonts
) {
767 if (NS_SUCCEEDED(aPS
->GetDownloadFonts(&b
))) {
768 DUMP_BOOL(kWriteStr
, kPrintDownloadFonts
, b
);
769 mPrefBranch
->SetBoolPref(GetPrefName(kPrintDownloadFonts
, aPrinterName
),
774 if (aFlags
& nsIPrintSettings::kInitSaveOrientation
) {
775 if (NS_SUCCEEDED(aPS
->GetOrientation(&iVal
))) {
776 DUMP_INT(kWriteStr
, kPrintOrientation
, iVal
);
777 mPrefBranch
->SetIntPref(GetPrefName(kPrintOrientation
, aPrinterName
),
782 if (aFlags
& nsIPrintSettings::kInitSavePrintCommand
) {
783 if (NS_SUCCEEDED(aPS
->GetPrintCommand(&uStr
))) {
784 DUMP_STR(kWriteStr
, kPrintCommand
, uStr
);
785 WritePrefString(uStr
, GetPrefName(kPrintCommand
, aPrinterName
));
789 // Only the general version of this pref is saved
790 if ((aFlags
& nsIPrintSettings::kInitSavePrinterName
)
791 && aPrinterName
.IsEmpty()) {
792 if (NS_SUCCEEDED(aPS
->GetPrinterName(&uStr
))) {
793 DUMP_STR(kWriteStr
, kPrinterName
, uStr
);
794 WritePrefString(uStr
, kPrinterName
);
798 if (aFlags
& nsIPrintSettings::kInitSavePrintToFile
) {
799 if (NS_SUCCEEDED(aPS
->GetPrintToFile(&b
))) {
800 DUMP_BOOL(kWriteStr
, kPrintToFile
, b
);
801 mPrefBranch
->SetBoolPref(GetPrefName(kPrintToFile
, aPrinterName
), b
);
805 if (aFlags
& nsIPrintSettings::kInitSaveToFileName
) {
806 if (NS_SUCCEEDED(aPS
->GetToFileName(&uStr
))) {
807 DUMP_STR(kWriteStr
, kPrintToFileName
, uStr
);
808 WritePrefString(uStr
, GetPrefName(kPrintToFileName
, aPrinterName
));
812 if (aFlags
& nsIPrintSettings::kInitSavePageDelay
) {
813 if (NS_SUCCEEDED(aPS
->GetPrintPageDelay(&iVal
))) {
814 DUMP_INT(kWriteStr
, kPrintPageDelay
, iVal
);
815 mPrefBranch
->SetIntPref(GetPrefName(kPrintPageDelay
, aPrinterName
), iVal
);
819 if (aFlags
& nsIPrintSettings::kInitSaveShrinkToFit
) {
820 if (NS_SUCCEEDED(aPS
->GetShrinkToFit(&b
))) {
821 DUMP_BOOL(kWriteStr
, kPrintShrinkToFit
, b
);
822 mPrefBranch
->SetBoolPref(GetPrefName(kPrintShrinkToFit
, aPrinterName
), b
);
826 if (aFlags
& nsIPrintSettings::kInitSaveScaling
) {
827 if (NS_SUCCEEDED(aPS
->GetScaling(&dbl
))) {
828 DUMP_DBL(kWriteStr
, kPrintScaling
, dbl
);
829 WritePrefDouble(GetPrefName(kPrintScaling
, aPrinterName
), dbl
);
840 nsPrintOptions::DisplayJobProperties(const PRUnichar
*aPrinter
,
841 nsIPrintSettings
* aPrintSettings
,
844 NS_ENSURE_ARG_POINTER(aPrinter
);
845 *aDisplayed
= PR_FALSE
;
848 nsCOMPtr
<nsIPrinterEnumerator
> propDlg
=
849 do_CreateInstance(NS_PRINTER_ENUMERATOR_CONTRACTID
, &rv
);
850 NS_ENSURE_SUCCESS(rv
, rv
);
852 NS_ENSURE_ARG_POINTER(aPrintSettings
);
853 rv
= propDlg
->DisplayPropertiesDlg(aPrinter
, aPrintSettings
);
854 NS_ENSURE_SUCCESS(rv
, rv
);
856 *aDisplayed
= PR_TRUE
;
861 NS_IMETHODIMP
nsPrintOptions::GetNativeData(PRInt16 aDataType
, void * *_retval
)
863 return NS_ERROR_NOT_IMPLEMENTED
;
866 nsresult
nsPrintOptions::_CreatePrintSettings(nsIPrintSettings
**_retval
)
868 // does not initially ref count
869 nsPrintSettings
* printSettings
= new nsPrintSettings();
870 NS_ENSURE_TRUE(printSettings
, NS_ERROR_OUT_OF_MEMORY
);
872 NS_ADDREF(*_retval
= printSettings
); // ref count
874 nsXPIDLString printerName
;
875 nsresult rv
= GetDefaultPrinterName(getter_Copies(printerName
));
876 NS_ENSURE_SUCCESS(rv
, rv
);
877 (*_retval
)->SetPrinterName(printerName
.get());
879 (void)InitPrintSettingsFromPrefs(*_retval
, PR_FALSE
,
880 nsIPrintSettings::kInitSaveAll
);
885 NS_IMETHODIMP
nsPrintOptions::CreatePrintSettings(nsIPrintSettings
**_retval
)
887 return _CreatePrintSettings(_retval
);
891 nsPrintOptions::GetGlobalPrintSettings(nsIPrintSettings
**aGlobalPrintSettings
)
895 rv
= CreatePrintSettings(getter_AddRefs(mGlobalPrintSettings
));
896 NS_ENSURE_SUCCESS(rv
, rv
);
898 NS_ADDREF(*aGlobalPrintSettings
= mGlobalPrintSettings
.get());
904 nsPrintOptions::GetNewPrintSettings(nsIPrintSettings
* *aNewPrintSettings
)
906 return CreatePrintSettings(aNewPrintSettings
);
910 nsPrintOptions::GetDefaultPrinterName(PRUnichar
* *aDefaultPrinterName
)
913 nsCOMPtr
<nsIPrinterEnumerator
> prtEnum
=
914 do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID
, &rv
);
915 NS_ENSURE_SUCCESS(rv
, rv
);
917 // Look up the printer from the last print job
918 nsAutoString lastPrinterName
;
919 ReadPrefString(kPrinterName
, lastPrinterName
);
920 if (!lastPrinterName
.IsEmpty()) {
921 // Verify it's still a valid printer
922 nsCOMPtr
<nsIStringEnumerator
> printers
;
923 rv
= prtEnum
->GetPrinterNameList(getter_AddRefs(printers
));
924 if (NS_SUCCEEDED(rv
)) {
925 PRBool isValid
= PR_FALSE
;
927 while (NS_SUCCEEDED(printers
->HasMore(&hasMore
)) && hasMore
) {
928 nsAutoString printer
;
929 if (NS_SUCCEEDED(printers
->GetNext(printer
)) && lastPrinterName
.Equals(printer
)) {
935 *aDefaultPrinterName
= ToNewUnicode(lastPrinterName
);
941 // There is no last printer preference, or it doesn't name a valid printer.
942 // Return the default from the printer enumeration.
943 return prtEnum
->GetDefaultPrinterName(aDefaultPrinterName
);
947 nsPrintOptions::InitPrintSettingsFromPrinter(const PRUnichar
*aPrinterName
,
948 nsIPrintSettings
*aPrintSettings
)
950 NS_ENSURE_ARG_POINTER(aPrintSettings
);
951 NS_ENSURE_ARG_POINTER(aPrinterName
);
954 nsXPIDLString printerName
;
955 aPrintSettings
->GetPrinterName(getter_Copies(printerName
));
956 if (!printerName
.Equals(aPrinterName
)) {
957 NS_WARNING("Printer names should match!");
961 PRBool isInitialized
;
962 aPrintSettings
->GetIsInitializedFromPrinter(&isInitialized
);
967 nsCOMPtr
<nsIPrinterEnumerator
> prtEnum
=
968 do_GetService(NS_PRINTER_ENUMERATOR_CONTRACTID
, &rv
);
969 NS_ENSURE_SUCCESS(rv
, rv
);
971 rv
= prtEnum
->InitPrintSettingsFromPrinter(aPrinterName
, aPrintSettings
);
972 NS_ENSURE_SUCCESS(rv
, rv
);
974 aPrintSettings
->SetIsInitializedFromPrinter(PR_TRUE
);
978 /** ---------------------------------------------------
979 * Helper function - Returns either the name or sets the length to zero
982 GetAdjustedPrinterName(nsIPrintSettings
* aPS
, PRBool aUsePNP
,
983 nsAString
& aPrinterName
)
985 NS_ENSURE_ARG_POINTER(aPS
);
987 aPrinterName
.Truncate();
991 // Get the Printer Name from the PrintSettings
992 // to use as a prefix for Pref Names
993 PRUnichar
* prtName
= nsnull
;
995 nsresult rv
= aPS
->GetPrinterName(&prtName
);
996 NS_ENSURE_SUCCESS(rv
, rv
);
998 aPrinterName
= nsDependentString(prtName
);
1000 // Convert any whitespaces, carriage returns or newlines to _
1001 // The below algorithm is supposedly faster than using iterators
1002 NS_NAMED_LITERAL_STRING(replSubstr
, "_");
1003 const char* replaceStr
= " \n\r";
1006 for (x
=0; x
< (PRInt32
)strlen(replaceStr
); x
++) {
1007 PRUnichar uChar
= replaceStr
[x
];
1010 while ((i
= aPrinterName
.FindChar(uChar
, i
)) != kNotFound
) {
1011 aPrinterName
.Replace(i
, 1, replSubstr
);
1019 nsPrintOptions::GetPrinterPrefInt(nsIPrintSettings
*aPrintSettings
,
1020 const PRUnichar
*aPrefName
, PRInt32
*_retval
)
1022 NS_ENSURE_STATE(mPrefBranch
);
1023 NS_ENSURE_ARG_POINTER(aPrintSettings
);
1024 NS_ENSURE_ARG_POINTER(aPrefName
);
1026 nsAutoString prtName
;
1027 // Get the Printer Name from the PrintSettings
1028 // to use as a prefix for Pref Names
1029 GetAdjustedPrinterName(aPrintSettings
, PR_TRUE
, prtName
);
1031 const char* prefName
=
1032 GetPrefName(NS_LossyConvertUTF16toASCII(aPrefName
).get(), prtName
);
1034 NS_ENSURE_TRUE(prefName
, NS_ERROR_FAILURE
);
1037 nsresult rv
= mPrefBranch
->GetIntPref(prefName
, &iVal
);
1038 NS_ENSURE_SUCCESS(rv
, rv
);
1045 nsPrintOptions::InitPrintSettingsFromPrefs(nsIPrintSettings
* aPS
,
1046 PRBool aUsePNP
, PRUint32 aFlags
)
1048 NS_ENSURE_ARG_POINTER(aPS
);
1050 PRBool isInitialized
;
1051 aPS
->GetIsInitializedFromPrefs(&isInitialized
);
1056 nsAutoString prtName
;
1057 // read any non printer specific prefs
1058 // with empty printer name
1059 nsresult rv
= ReadPrefs(aPS
, prtName
, aFlags
);
1060 NS_ENSURE_SUCCESS(rv
, rv
);
1062 // Get the Printer Name from the PrintSettings
1063 // to use as a prefix for Pref Names
1064 rv
= GetAdjustedPrinterName(aPS
, aUsePNP
, prtName
);
1065 NS_ENSURE_SUCCESS(rv
, rv
);
1067 if (prtName
.IsEmpty()) {
1068 NS_WARNING("Caller should supply a printer name.");
1072 // Now read any printer specific prefs
1073 rv
= ReadPrefs(aPS
, prtName
, aFlags
);
1074 if (NS_SUCCEEDED(rv
))
1075 aPS
->SetIsInitializedFromPrefs(PR_TRUE
);
1081 * Save all of the printer settings; if we can find a printer name, save
1082 * printer-specific preferences. Otherwise, save generic ones.
1085 nsPrintOptions::SavePrintSettingsToPrefs(nsIPrintSettings
*aPS
,
1086 PRBool aUsePrinterNamePrefix
,
1089 NS_ENSURE_ARG_POINTER(aPS
);
1090 nsAutoString prtName
;
1092 // Get the printer name from the PrinterSettings for an optional prefix.
1093 nsresult rv
= GetAdjustedPrinterName(aPS
, aUsePrinterNamePrefix
, prtName
);
1094 NS_ENSURE_SUCCESS(rv
, rv
);
1096 // Write the prefs, with or without a printer name prefix.
1097 return WritePrefs(aPS
, prtName
, aFlags
);
1101 //-----------------------------------------------------
1102 //-- Protected Methods --------------------------------
1103 //-----------------------------------------------------
1105 nsPrintOptions::ReadPrefString(const char * aPrefId
, nsAString
& aString
)
1107 NS_ENSURE_STATE(mPrefBranch
);
1108 NS_ENSURE_ARG_POINTER(aPrefId
);
1111 nsresult rv
= mPrefBranch
->GetCharPref(aPrefId
, getter_Copies(str
));
1112 NS_ENSURE_SUCCESS(rv
, rv
);
1114 CopyUTF8toUTF16(str
.get(), aString
);
1120 nsPrintOptions::WritePrefString(PRUnichar
*& aStr
, const char* aPrefId
)
1122 NS_ENSURE_STATE(mPrefBranch
);
1123 NS_ENSURE_ARG_POINTER(aStr
);
1124 NS_ENSURE_ARG_POINTER(aPrefId
);
1126 nsresult rv
= mPrefBranch
->SetCharPref(aPrefId
,
1127 NS_ConvertUTF16toUTF8(aStr
).get());
1129 nsMemory::Free(aStr
);
1135 nsPrintOptions::WritePrefString(const char * aPrefId
, const nsAString
& aString
)
1137 NS_ENSURE_STATE(mPrefBranch
);
1138 NS_ENSURE_ARG_POINTER(aPrefId
);
1140 return mPrefBranch
->SetCharPref(aPrefId
,
1141 NS_ConvertUTF16toUTF8(aString
).get());
1145 nsPrintOptions::ReadPrefDouble(const char * aPrefId
, double& aVal
)
1147 NS_ENSURE_STATE(mPrefBranch
);
1148 NS_ENSURE_ARG_POINTER(aPrefId
);
1151 nsresult rv
= mPrefBranch
->GetCharPref(aPrefId
, &str
);
1152 if (NS_SUCCEEDED(rv
) && str
) {
1154 nsMemory::Free(str
);
1160 nsPrintOptions::WritePrefDouble(const char * aPrefId
, double aVal
)
1162 NS_ENSURE_STATE(mPrefBranch
);
1163 NS_ENSURE_ARG_POINTER(aPrefId
);
1165 char str
[16]; // max 9 chars in below snprintf(), 16 will do nicely
1166 int ret
= PR_snprintf(str
, sizeof(str
), "%6.2f", aVal
);
1167 NS_ENSURE_TRUE(ret
>= 0, NS_ERROR_FAILURE
);
1169 return mPrefBranch
->SetCharPref(aPrefId
, str
);
1173 nsPrintOptions::ReadInchesToTwipsPref(const char * aPrefId
, nscoord
& aTwips
,
1174 const char * aMarginPref
)
1180 char * str
= nsnull
;
1181 nsresult rv
= mPrefBranch
->GetCharPref(aPrefId
, &str
);
1182 if (NS_FAILED(rv
) || !str
)
1183 rv
= mPrefBranch
->GetCharPref(aMarginPref
, &str
);
1184 if (NS_SUCCEEDED(rv
) && str
) {
1185 nsAutoString justStr
;
1186 justStr
.AssignWithConversion(str
);
1188 float inches
= justStr
.ToFloat(&errCode
);
1189 if (NS_SUCCEEDED(errCode
)) {
1190 aTwips
= NS_INCHES_TO_TWIPS(inches
);
1194 nsMemory::Free(str
);
1199 nsPrintOptions::WriteInchesFromTwipsPref(const char * aPrefId
, nscoord aTwips
)
1205 double inches
= NS_TWIPS_TO_INCHES(aTwips
);
1206 nsCAutoString inchesStr
;
1207 inchesStr
.AppendFloat(inches
);
1209 mPrefBranch
->SetCharPref(aPrefId
, inchesStr
.get());
1213 nsPrintOptions::ReadInchesIntToTwipsPref(const char * aPrefId
, nscoord
& aTwips
,
1214 const char * aMarginPref
)
1221 nsresult rv
= mPrefBranch
->GetIntPref(aPrefId
, &value
);
1222 if (NS_FAILED(rv
)) {
1223 rv
= mPrefBranch
->GetIntPref(aMarginPref
, &value
);
1225 if (NS_SUCCEEDED(rv
)) {
1226 aTwips
= NS_INCHES_TO_TWIPS(float(value
)/100.0f
);
1233 nsPrintOptions::WriteInchesIntFromTwipsPref(const char * aPrefId
, nscoord aTwips
)
1239 mPrefBranch
->SetIntPref(aPrefId
, PRInt32(NS_TWIPS_TO_INCHES(aTwips
)*100.0f
+ 0.5f
));
1243 nsPrintOptions::ReadJustification(const char * aPrefId
, PRInt16
& aJust
,
1247 nsAutoString justStr
;
1248 if (NS_SUCCEEDED(ReadPrefString(aPrefId
, justStr
))) {
1249 if (justStr
.EqualsASCII(kJustRight
)) {
1250 aJust
= nsIPrintSettings::kJustRight
;
1252 } else if (justStr
.EqualsASCII(kJustCenter
)) {
1253 aJust
= nsIPrintSettings::kJustCenter
;
1256 aJust
= nsIPrintSettings::kJustLeft
;
1261 //---------------------------------------------------
1263 nsPrintOptions::WriteJustification(const char * aPrefId
, PRInt16 aJust
)
1266 case nsIPrintSettings::kJustLeft
:
1267 mPrefBranch
->SetCharPref(aPrefId
, kJustLeft
);
1270 case nsIPrintSettings::kJustCenter
:
1271 mPrefBranch
->SetCharPref(aPrefId
, kJustCenter
);
1274 case nsIPrintSettings::kJustRight
:
1275 mPrefBranch
->SetCharPref(aPrefId
, kJustRight
);
1280 //----------------------------------------------------------------------
1281 // Testing of read/write prefs
1282 // This define turns on the testing module below
1283 // so at start up it writes and reads the prefs.
1291 nsCOMPtr
<nsIPrintSettings
> ps
;
1293 nsCOMPtr
<nsIPrintOptions
> printService
=
1294 do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv
);
1295 if (NS_SUCCEEDED(rv
)) {
1296 rv
= printService
->CreatePrintSettings(getter_AddRefs(ps
));
1300 ps
->SetPrintOptions(nsIPrintSettings::kPrintOddPages
, PR_TRUE
);
1301 ps
->SetPrintOptions(nsIPrintSettings::kPrintEvenPages
, PR_FALSE
);
1302 ps
->SetMarginTop(1.0);
1303 ps
->SetMarginLeft(1.0);
1304 ps
->SetMarginBottom(1.0);
1305 ps
->SetMarginRight(1.0);
1306 ps
->SetScaling(0.5);
1307 ps
->SetPrintBGColors(PR_TRUE
);
1308 ps
->SetPrintBGImages(PR_TRUE
);
1309 ps
->SetPrintRange(15);
1310 ps
->SetHeaderStrLeft(NS_ConvertUTF8toUTF16("Left").get());
1311 ps
->SetHeaderStrCenter(NS_ConvertUTF8toUTF16("Center").get());
1312 ps
->SetHeaderStrRight(NS_ConvertUTF8toUTF16("Right").get());
1313 ps
->SetFooterStrLeft(NS_ConvertUTF8toUTF16("Left").get());
1314 ps
->SetFooterStrCenter(NS_ConvertUTF8toUTF16("Center").get());
1315 ps
->SetFooterStrRight(NS_ConvertUTF8toUTF16("Right").get());
1316 ps
->SetPaperName(NS_ConvertUTF8toUTF16("Paper Name").get());
1317 ps
->SetPlexName(NS_ConvertUTF8toUTF16("Plex Name").get());
1318 ps
->SetPaperSizeType(10);
1319 ps
->SetPaperData(1);
1320 ps
->SetPaperWidth(100.0);
1321 ps
->SetPaperHeight(50.0);
1322 ps
->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters
);
1323 ps
->SetPrintReversed(PR_TRUE
);
1324 ps
->SetPrintInColor(PR_TRUE
);
1325 ps
->SetOrientation(nsIPrintSettings::kLandscapeOrientation
);
1326 ps
->SetPrintCommand(NS_ConvertUTF8toUTF16("Command").get());
1327 ps
->SetNumCopies(2);
1328 ps
->SetPrinterName(NS_ConvertUTF8toUTF16("Printer Name").get());
1329 ps
->SetPrintToFile(PR_TRUE
);
1330 ps
->SetToFileName(NS_ConvertUTF8toUTF16("File Name").get());
1331 ps
->SetPrintPageDelay(1000);
1332 ps
->SetShrinkToFit(PR_TRUE
);
1334 struct SettingsType
{
1338 SettingsType gSettings
[] = {
1339 {"OddEven", nsIPrintSettings::kInitSaveOddEvenPages
},
1340 {kPrintHeaderStrLeft
, nsIPrintSettings::kInitSaveHeaderLeft
},
1341 {kPrintHeaderStrCenter
, nsIPrintSettings::kInitSaveHeaderCenter
},
1342 {kPrintHeaderStrRight
, nsIPrintSettings::kInitSaveHeaderRight
},
1343 {kPrintFooterStrLeft
, nsIPrintSettings::kInitSaveFooterLeft
},
1344 {kPrintFooterStrCenter
, nsIPrintSettings::kInitSaveFooterCenter
},
1345 {kPrintFooterStrRight
, nsIPrintSettings::kInitSaveFooterRight
},
1346 {kPrintBGColors
, nsIPrintSettings::kInitSaveBGColors
},
1347 {kPrintBGImages
, nsIPrintSettings::kInitSaveBGImages
},
1348 {kPrintShrinkToFit
, nsIPrintSettings::kInitSaveShrinkToFit
},
1349 {kPrintPaperSize
, nsIPrintSettings::kInitSavePaperSize
},
1350 {kPrintPlexName
, nsIPrintSettings::kInitSavePlexName
},
1351 {kPrintPaperData
, nsIPrintSettings::kInitSavePaperData
},
1352 {kPrintReversed
, nsIPrintSettings::kInitSaveReversed
},
1353 {kPrintInColor
, nsIPrintSettings::kInitSaveInColor
},
1354 {kPrintColorspace
, nsIPrintSettings::kInitSaveColorspace
},
1355 {kPrintResolutionName
, nsIPrintSettings::kInitSaveResolutionName
},
1356 {kPrintDownloadFonts
, nsIPrintSettings::kInitSaveDownloadFonts
},
1357 {kPrintOrientation
, nsIPrintSettings::kInitSaveOrientation
},
1358 {kPrintCommand
, nsIPrintSettings::kInitSavePrintCommand
},
1359 {kPrinterName
, nsIPrintSettings::kInitSavePrinterName
},
1360 {kPrintToFile
, nsIPrintSettings::kInitSavePrintToFile
},
1361 {kPrintToFileName
, nsIPrintSettings::kInitSaveToFileName
},
1362 {kPrintPageDelay
, nsIPrintSettings::kInitSavePageDelay
},
1363 {"Margins", nsIPrintSettings::kInitSaveMargins
},
1364 {"All", nsIPrintSettings::kInitSaveAll
},
1367 nsString prefix
; prefix
.AssignLiteral("Printer Name");
1369 while (gSettings
[i
].mName
!= nsnull
) {
1370 printf("------------------------------------------------\n");
1371 printf("%d) %s -> 0x%X\n", i
, gSettings
[i
].mName
, gSettings
[i
].mFlag
);
1372 printService
->SavePrintSettingsToPrefs(ps
, PR_TRUE
, gSettings
[i
].mFlag
);
1373 printService
->InitPrintSettingsFromPrefs(ps
, PR_TRUE
,
1374 gSettings
[i
].mFlag
);