1 /* -*- Mode: C++; tab-width: 4; 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/. */
5 
6 #include "nsPrintSettingsImpl.h"
7 
8 #include "prenv.h"
9 #include "nsCoord.h"
10 #include "nsPaper.h"
11 #include "nsReadableUtils.h"
12 #include "nsIPrintSession.h"
13 #include "mozilla/DebugOnly.h"
14 #include "mozilla/RefPtr.h"
15 
16 #define DEFAULT_MARGIN_WIDTH 0.5
17 
NS_IMPL_ISUPPORTS(nsPrintSettings,nsIPrintSettings)18 NS_IMPL_ISUPPORTS(nsPrintSettings, nsIPrintSettings)
19 
20 nsPrintSettings::nsPrintSettings()
21     : mScaling(1.0),
22       mPrintBGColors(false),
23       mPrintBGImages(false),
24       mIsCancelled(false),
25       mSaveOnCancel(true),
26       mPrintSilent(false),
27       mShrinkToFit(true),
28       mShowPrintProgress(true),
29       mShowMarginGuides(false),
30       mHonorPageRuleMargins(true),
31       mIsPrintSelectionRBEnabled(false),
32       mPrintSelectionOnly(false),
33       mPrintPageDelay(50),
34       mPaperWidth(8.5),
35       mPaperHeight(11.0),
36       mPaperSizeUnit(kPaperSizeInches),
37       mPrintReversed(false),
38       mPrintInColor(true),
39       mOrientation(kPortraitOrientation),
40       mResolution(0),
41       mDuplex(0),
42       mNumCopies(1),
43       mNumPagesPerSheet(1),
44       mPrintToFile(false),
45       mOutputFormat(kOutputFormatNative),
46       mIsInitedFromPrinter(false),
47       mIsInitedFromPrefs(false) {
48   /* member initializers and constructor code */
49   int32_t marginWidth = NS_INCHES_TO_INT_TWIPS(DEFAULT_MARGIN_WIDTH);
50   mMargin.SizeTo(marginWidth, marginWidth, marginWidth, marginWidth);
51   mEdge.SizeTo(0, 0, 0, 0);
52   mUnwriteableMargin.SizeTo(0, 0, 0, 0);
53 
54   mHeaderStrs[0].AssignLiteral("&T");
55   mHeaderStrs[2].AssignLiteral("&U");
56 
57   mFooterStrs[0].AssignLiteral(
58       "&PT");  // Use &P (Page Num Only) or &PT (Page Num of Page Total)
59   mFooterStrs[2].AssignLiteral("&D");
60 }
61 
InitWithInitializer(const PrintSettingsInitializer & aSettings)62 void nsPrintSettings::InitWithInitializer(
63     const PrintSettingsInitializer& aSettings) {
64   const double kInchesPerPoint = 1.0 / 72.0;
65 
66   SetPrinterName(aSettings.mPrinter);
67   SetPrintInColor(aSettings.mPrintInColor);
68   SetResolution(aSettings.mResolution);
69   // The paper ID used by nsPrintSettings is the non-localizable identifier
70   // exposed as "id" by the paper, not the potentially localized human-friendly
71   // "name", which could change, e.g. if the user changes their system locale.
72   SetPaperId(aSettings.mPaperInfo.mId);
73   SetPaperWidth(aSettings.mPaperInfo.mSize.Width() * kInchesPerPoint);
74   SetPaperHeight(aSettings.mPaperInfo.mSize.Height() * kInchesPerPoint);
75   SetPaperSizeUnit(nsIPrintSettings::kPaperSizeInches);
76 
77   if (aSettings.mPaperInfo.mUnwriteableMargin) {
78     const auto& margin = aSettings.mPaperInfo.mUnwriteableMargin.value();
79     // Margins are stored internally in TWIPS, but the setters expect inches.
80     SetUnwriteableMarginTop(margin.top * kInchesPerPoint);
81     SetUnwriteableMarginRight(margin.right * kInchesPerPoint);
82     SetUnwriteableMarginBottom(margin.bottom * kInchesPerPoint);
83     SetUnwriteableMarginLeft(margin.left * kInchesPerPoint);
84   }
85 
86   // Set this last because other setters may overwrite its value.
87   SetIsInitializedFromPrinter(true);
88 }
89 
nsPrintSettings(const nsPrintSettings & aPS)90 nsPrintSettings::nsPrintSettings(const nsPrintSettings& aPS) { *this = aPS; }
91 
92 nsPrintSettings::~nsPrintSettings() = default;
93 
GetPrintSession(nsIPrintSession ** aPrintSession)94 NS_IMETHODIMP nsPrintSettings::GetPrintSession(
95     nsIPrintSession** aPrintSession) {
96   NS_ENSURE_ARG_POINTER(aPrintSession);
97   *aPrintSession = nullptr;
98 
99   nsCOMPtr<nsIPrintSession> session = do_QueryReferent(mSession);
100   if (!session) return NS_ERROR_NOT_INITIALIZED;
101   *aPrintSession = session;
102   NS_ADDREF(*aPrintSession);
103   return NS_OK;
104 }
105 
SetPrintSession(nsIPrintSession * aPrintSession)106 NS_IMETHODIMP nsPrintSettings::SetPrintSession(nsIPrintSession* aPrintSession) {
107   // Clearing it by passing nullptr is not allowed. That's why we
108   // use a weak ref so that it doesn't have to be cleared.
109   NS_ENSURE_ARG(aPrintSession);
110 
111   mSession = do_GetWeakReference(aPrintSession);
112   if (!mSession) {
113     // This may happen if the implementation of this object does
114     // not support weak references - programmer error.
115     NS_ERROR("Could not get a weak reference from aPrintSession");
116     return NS_ERROR_FAILURE;
117   }
118   return NS_OK;
119 }
120 
GetPrintReversed(bool * aPrintReversed)121 NS_IMETHODIMP nsPrintSettings::GetPrintReversed(bool* aPrintReversed) {
122   *aPrintReversed = mPrintReversed;
123   return NS_OK;
124 }
SetPrintReversed(bool aPrintReversed)125 NS_IMETHODIMP nsPrintSettings::SetPrintReversed(bool aPrintReversed) {
126   mPrintReversed = aPrintReversed;
127   return NS_OK;
128 }
129 
GetPrintInColor(bool * aPrintInColor)130 NS_IMETHODIMP nsPrintSettings::GetPrintInColor(bool* aPrintInColor) {
131   *aPrintInColor = mPrintInColor;
132   return NS_OK;
133 }
SetPrintInColor(bool aPrintInColor)134 NS_IMETHODIMP nsPrintSettings::SetPrintInColor(bool aPrintInColor) {
135   mPrintInColor = aPrintInColor;
136   return NS_OK;
137 }
138 
GetOrientation(int32_t * aOrientation)139 NS_IMETHODIMP nsPrintSettings::GetOrientation(int32_t* aOrientation) {
140   *aOrientation = mOrientation;
141   return NS_OK;
142 }
SetOrientation(int32_t aOrientation)143 NS_IMETHODIMP nsPrintSettings::SetOrientation(int32_t aOrientation) {
144   mOrientation = aOrientation;
145   return NS_OK;
146 }
147 
GetResolution(int32_t * aResolution)148 NS_IMETHODIMP nsPrintSettings::GetResolution(int32_t* aResolution) {
149   NS_ENSURE_ARG_POINTER(aResolution);
150   *aResolution = mResolution;
151   return NS_OK;
152 }
SetResolution(const int32_t aResolution)153 NS_IMETHODIMP nsPrintSettings::SetResolution(const int32_t aResolution) {
154   mResolution = aResolution;
155   return NS_OK;
156 }
157 
GetDuplex(int32_t * aDuplex)158 NS_IMETHODIMP nsPrintSettings::GetDuplex(int32_t* aDuplex) {
159   NS_ENSURE_ARG_POINTER(aDuplex);
160   *aDuplex = mDuplex;
161   return NS_OK;
162 }
SetDuplex(const int32_t aDuplex)163 NS_IMETHODIMP nsPrintSettings::SetDuplex(const int32_t aDuplex) {
164   mDuplex = aDuplex;
165   return NS_OK;
166 }
167 
GetPrinterName(nsAString & aPrinter)168 NS_IMETHODIMP nsPrintSettings::GetPrinterName(nsAString& aPrinter) {
169   aPrinter = mPrinter;
170   return NS_OK;
171 }
172 
SetPrinterName(const nsAString & aPrinter)173 NS_IMETHODIMP nsPrintSettings::SetPrinterName(const nsAString& aPrinter) {
174   if (!mPrinter.Equals(aPrinter)) {
175     mIsInitedFromPrinter = false;
176     mIsInitedFromPrefs = false;
177   }
178 
179   mPrinter.Assign(aPrinter);
180   return NS_OK;
181 }
182 
GetNumCopies(int32_t * aNumCopies)183 NS_IMETHODIMP nsPrintSettings::GetNumCopies(int32_t* aNumCopies) {
184   NS_ENSURE_ARG_POINTER(aNumCopies);
185   *aNumCopies = mNumCopies;
186   return NS_OK;
187 }
SetNumCopies(int32_t aNumCopies)188 NS_IMETHODIMP nsPrintSettings::SetNumCopies(int32_t aNumCopies) {
189   mNumCopies = aNumCopies;
190   return NS_OK;
191 }
192 
GetNumPagesPerSheet(int32_t * aNumPagesPerSheet)193 NS_IMETHODIMP nsPrintSettings::GetNumPagesPerSheet(int32_t* aNumPagesPerSheet) {
194   NS_ENSURE_ARG_POINTER(aNumPagesPerSheet);
195   *aNumPagesPerSheet = mNumPagesPerSheet;
196   return NS_OK;
197 }
SetNumPagesPerSheet(int32_t aNumPagesPerSheet)198 NS_IMETHODIMP nsPrintSettings::SetNumPagesPerSheet(int32_t aNumPagesPerSheet) {
199   mNumPagesPerSheet = aNumPagesPerSheet;
200   return NS_OK;
201 }
202 
GetPrintToFile(bool * aPrintToFile)203 NS_IMETHODIMP nsPrintSettings::GetPrintToFile(bool* aPrintToFile) {
204   NS_ENSURE_ARG_POINTER(aPrintToFile);
205   *aPrintToFile = mPrintToFile;
206   return NS_OK;
207 }
SetPrintToFile(bool aPrintToFile)208 NS_IMETHODIMP nsPrintSettings::SetPrintToFile(bool aPrintToFile) {
209   mPrintToFile = aPrintToFile;
210   return NS_OK;
211 }
212 
GetToFileName(nsAString & aToFileName)213 NS_IMETHODIMP nsPrintSettings::GetToFileName(nsAString& aToFileName) {
214   aToFileName = mToFileName;
215   return NS_OK;
216 }
SetToFileName(const nsAString & aToFileName)217 NS_IMETHODIMP nsPrintSettings::SetToFileName(const nsAString& aToFileName) {
218   mToFileName = aToFileName;
219   return NS_OK;
220 }
221 
GetOutputFormat(int16_t * aOutputFormat)222 NS_IMETHODIMP nsPrintSettings::GetOutputFormat(int16_t* aOutputFormat) {
223   NS_ENSURE_ARG_POINTER(aOutputFormat);
224   *aOutputFormat = mOutputFormat;
225   return NS_OK;
226 }
SetOutputFormat(int16_t aOutputFormat)227 NS_IMETHODIMP nsPrintSettings::SetOutputFormat(int16_t aOutputFormat) {
228   mOutputFormat = aOutputFormat;
229   return NS_OK;
230 }
231 
GetPrintPageDelay(int32_t * aPrintPageDelay)232 NS_IMETHODIMP nsPrintSettings::GetPrintPageDelay(int32_t* aPrintPageDelay) {
233   *aPrintPageDelay = mPrintPageDelay;
234   return NS_OK;
235 }
SetPrintPageDelay(int32_t aPrintPageDelay)236 NS_IMETHODIMP nsPrintSettings::SetPrintPageDelay(int32_t aPrintPageDelay) {
237   mPrintPageDelay = aPrintPageDelay;
238   return NS_OK;
239 }
240 
GetIsInitializedFromPrinter(bool * aIsInitializedFromPrinter)241 NS_IMETHODIMP nsPrintSettings::GetIsInitializedFromPrinter(
242     bool* aIsInitializedFromPrinter) {
243   NS_ENSURE_ARG_POINTER(aIsInitializedFromPrinter);
244   *aIsInitializedFromPrinter = mIsInitedFromPrinter;
245   return NS_OK;
246 }
SetIsInitializedFromPrinter(bool aIsInitializedFromPrinter)247 NS_IMETHODIMP nsPrintSettings::SetIsInitializedFromPrinter(
248     bool aIsInitializedFromPrinter) {
249   mIsInitedFromPrinter = aIsInitializedFromPrinter;
250   return NS_OK;
251 }
252 
GetIsInitializedFromPrefs(bool * aInitializedFromPrefs)253 NS_IMETHODIMP nsPrintSettings::GetIsInitializedFromPrefs(
254     bool* aInitializedFromPrefs) {
255   NS_ENSURE_ARG_POINTER(aInitializedFromPrefs);
256   *aInitializedFromPrefs = mIsInitedFromPrefs;
257   return NS_OK;
258 }
SetIsInitializedFromPrefs(bool aInitializedFromPrefs)259 NS_IMETHODIMP nsPrintSettings::SetIsInitializedFromPrefs(
260     bool aInitializedFromPrefs) {
261   mIsInitedFromPrefs = aInitializedFromPrefs;
262   return NS_OK;
263 }
264 
GetMarginTop(double * aMarginTop)265 NS_IMETHODIMP nsPrintSettings::GetMarginTop(double* aMarginTop) {
266   NS_ENSURE_ARG_POINTER(aMarginTop);
267   *aMarginTop = NS_TWIPS_TO_INCHES(mMargin.top);
268   return NS_OK;
269 }
SetMarginTop(double aMarginTop)270 NS_IMETHODIMP nsPrintSettings::SetMarginTop(double aMarginTop) {
271   mMargin.top = NS_INCHES_TO_INT_TWIPS(float(aMarginTop));
272   return NS_OK;
273 }
274 
GetMarginLeft(double * aMarginLeft)275 NS_IMETHODIMP nsPrintSettings::GetMarginLeft(double* aMarginLeft) {
276   NS_ENSURE_ARG_POINTER(aMarginLeft);
277   *aMarginLeft = NS_TWIPS_TO_INCHES(mMargin.left);
278   return NS_OK;
279 }
SetMarginLeft(double aMarginLeft)280 NS_IMETHODIMP nsPrintSettings::SetMarginLeft(double aMarginLeft) {
281   mMargin.left = NS_INCHES_TO_INT_TWIPS(float(aMarginLeft));
282   return NS_OK;
283 }
284 
GetMarginBottom(double * aMarginBottom)285 NS_IMETHODIMP nsPrintSettings::GetMarginBottom(double* aMarginBottom) {
286   NS_ENSURE_ARG_POINTER(aMarginBottom);
287   *aMarginBottom = NS_TWIPS_TO_INCHES(mMargin.bottom);
288   return NS_OK;
289 }
SetMarginBottom(double aMarginBottom)290 NS_IMETHODIMP nsPrintSettings::SetMarginBottom(double aMarginBottom) {
291   mMargin.bottom = NS_INCHES_TO_INT_TWIPS(float(aMarginBottom));
292   return NS_OK;
293 }
294 
GetMarginRight(double * aMarginRight)295 NS_IMETHODIMP nsPrintSettings::GetMarginRight(double* aMarginRight) {
296   NS_ENSURE_ARG_POINTER(aMarginRight);
297   *aMarginRight = NS_TWIPS_TO_INCHES(mMargin.right);
298   return NS_OK;
299 }
SetMarginRight(double aMarginRight)300 NS_IMETHODIMP nsPrintSettings::SetMarginRight(double aMarginRight) {
301   mMargin.right = NS_INCHES_TO_INT_TWIPS(float(aMarginRight));
302   return NS_OK;
303 }
304 
GetEdgeTop(double * aEdgeTop)305 NS_IMETHODIMP nsPrintSettings::GetEdgeTop(double* aEdgeTop) {
306   NS_ENSURE_ARG_POINTER(aEdgeTop);
307   *aEdgeTop = NS_TWIPS_TO_INCHES(mEdge.top);
308   return NS_OK;
309 }
SetEdgeTop(double aEdgeTop)310 NS_IMETHODIMP nsPrintSettings::SetEdgeTop(double aEdgeTop) {
311   mEdge.top = NS_INCHES_TO_INT_TWIPS(float(aEdgeTop));
312   return NS_OK;
313 }
314 
GetEdgeLeft(double * aEdgeLeft)315 NS_IMETHODIMP nsPrintSettings::GetEdgeLeft(double* aEdgeLeft) {
316   NS_ENSURE_ARG_POINTER(aEdgeLeft);
317   *aEdgeLeft = NS_TWIPS_TO_INCHES(mEdge.left);
318   return NS_OK;
319 }
SetEdgeLeft(double aEdgeLeft)320 NS_IMETHODIMP nsPrintSettings::SetEdgeLeft(double aEdgeLeft) {
321   mEdge.left = NS_INCHES_TO_INT_TWIPS(float(aEdgeLeft));
322   return NS_OK;
323 }
324 
GetEdgeBottom(double * aEdgeBottom)325 NS_IMETHODIMP nsPrintSettings::GetEdgeBottom(double* aEdgeBottom) {
326   NS_ENSURE_ARG_POINTER(aEdgeBottom);
327   *aEdgeBottom = NS_TWIPS_TO_INCHES(mEdge.bottom);
328   return NS_OK;
329 }
SetEdgeBottom(double aEdgeBottom)330 NS_IMETHODIMP nsPrintSettings::SetEdgeBottom(double aEdgeBottom) {
331   mEdge.bottom = NS_INCHES_TO_INT_TWIPS(float(aEdgeBottom));
332   return NS_OK;
333 }
334 
GetEdgeRight(double * aEdgeRight)335 NS_IMETHODIMP nsPrintSettings::GetEdgeRight(double* aEdgeRight) {
336   NS_ENSURE_ARG_POINTER(aEdgeRight);
337   *aEdgeRight = NS_TWIPS_TO_INCHES(mEdge.right);
338   return NS_OK;
339 }
SetEdgeRight(double aEdgeRight)340 NS_IMETHODIMP nsPrintSettings::SetEdgeRight(double aEdgeRight) {
341   mEdge.right = NS_INCHES_TO_INT_TWIPS(float(aEdgeRight));
342   return NS_OK;
343 }
344 
GetUnwriteableMarginTop(double * aUnwriteableMarginTop)345 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginTop(
346     double* aUnwriteableMarginTop) {
347   NS_ENSURE_ARG_POINTER(aUnwriteableMarginTop);
348   *aUnwriteableMarginTop = NS_TWIPS_TO_INCHES(mUnwriteableMargin.top);
349   return NS_OK;
350 }
SetUnwriteableMarginTop(double aUnwriteableMarginTop)351 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginTop(
352     double aUnwriteableMarginTop) {
353   if (aUnwriteableMarginTop >= 0.0) {
354     mUnwriteableMargin.top = NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginTop);
355   }
356   return NS_OK;
357 }
358 
GetUnwriteableMarginLeft(double * aUnwriteableMarginLeft)359 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginLeft(
360     double* aUnwriteableMarginLeft) {
361   NS_ENSURE_ARG_POINTER(aUnwriteableMarginLeft);
362   *aUnwriteableMarginLeft = NS_TWIPS_TO_INCHES(mUnwriteableMargin.left);
363   return NS_OK;
364 }
SetUnwriteableMarginLeft(double aUnwriteableMarginLeft)365 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginLeft(
366     double aUnwriteableMarginLeft) {
367   if (aUnwriteableMarginLeft >= 0.0) {
368     mUnwriteableMargin.left = NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginLeft);
369   }
370   return NS_OK;
371 }
372 
GetUnwriteableMarginBottom(double * aUnwriteableMarginBottom)373 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginBottom(
374     double* aUnwriteableMarginBottom) {
375   NS_ENSURE_ARG_POINTER(aUnwriteableMarginBottom);
376   *aUnwriteableMarginBottom = NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom);
377   return NS_OK;
378 }
SetUnwriteableMarginBottom(double aUnwriteableMarginBottom)379 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginBottom(
380     double aUnwriteableMarginBottom) {
381   if (aUnwriteableMarginBottom >= 0.0) {
382     mUnwriteableMargin.bottom =
383         NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginBottom);
384   }
385   return NS_OK;
386 }
387 
GetUnwriteableMarginRight(double * aUnwriteableMarginRight)388 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginRight(
389     double* aUnwriteableMarginRight) {
390   NS_ENSURE_ARG_POINTER(aUnwriteableMarginRight);
391   *aUnwriteableMarginRight = NS_TWIPS_TO_INCHES(mUnwriteableMargin.right);
392   return NS_OK;
393 }
SetUnwriteableMarginRight(double aUnwriteableMarginRight)394 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginRight(
395     double aUnwriteableMarginRight) {
396   if (aUnwriteableMarginRight >= 0.0) {
397     mUnwriteableMargin.right = NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginRight);
398   }
399   return NS_OK;
400 }
401 
GetScaling(double * aScaling)402 NS_IMETHODIMP nsPrintSettings::GetScaling(double* aScaling) {
403   NS_ENSURE_ARG_POINTER(aScaling);
404   *aScaling = mScaling;
405   return NS_OK;
406 }
407 
SetScaling(double aScaling)408 NS_IMETHODIMP nsPrintSettings::SetScaling(double aScaling) {
409   mScaling = aScaling;
410   return NS_OK;
411 }
412 
GetPrintBGColors(bool * aPrintBGColors)413 NS_IMETHODIMP nsPrintSettings::GetPrintBGColors(bool* aPrintBGColors) {
414   NS_ENSURE_ARG_POINTER(aPrintBGColors);
415   *aPrintBGColors = mPrintBGColors;
416   return NS_OK;
417 }
SetPrintBGColors(bool aPrintBGColors)418 NS_IMETHODIMP nsPrintSettings::SetPrintBGColors(bool aPrintBGColors) {
419   mPrintBGColors = aPrintBGColors;
420   return NS_OK;
421 }
422 
GetPrintBGImages(bool * aPrintBGImages)423 NS_IMETHODIMP nsPrintSettings::GetPrintBGImages(bool* aPrintBGImages) {
424   NS_ENSURE_ARG_POINTER(aPrintBGImages);
425   *aPrintBGImages = mPrintBGImages;
426   return NS_OK;
427 }
SetPrintBGImages(bool aPrintBGImages)428 NS_IMETHODIMP nsPrintSettings::SetPrintBGImages(bool aPrintBGImages) {
429   mPrintBGImages = aPrintBGImages;
430   return NS_OK;
431 }
432 
GetTitle(nsAString & aTitle)433 NS_IMETHODIMP nsPrintSettings::GetTitle(nsAString& aTitle) {
434   aTitle = mTitle;
435   return NS_OK;
436 }
SetTitle(const nsAString & aTitle)437 NS_IMETHODIMP nsPrintSettings::SetTitle(const nsAString& aTitle) {
438   mTitle = aTitle;
439   return NS_OK;
440 }
441 
GetDocURL(nsAString & aDocURL)442 NS_IMETHODIMP nsPrintSettings::GetDocURL(nsAString& aDocURL) {
443   aDocURL = mURL;
444   return NS_OK;
445 }
SetDocURL(const nsAString & aDocURL)446 NS_IMETHODIMP nsPrintSettings::SetDocURL(const nsAString& aDocURL) {
447   mURL = aDocURL;
448   return NS_OK;
449 }
450 
GetHeaderStrLeft(nsAString & aTitle)451 NS_IMETHODIMP nsPrintSettings::GetHeaderStrLeft(nsAString& aTitle) {
452   aTitle = mHeaderStrs[0];
453   return NS_OK;
454 }
SetHeaderStrLeft(const nsAString & aTitle)455 NS_IMETHODIMP nsPrintSettings::SetHeaderStrLeft(const nsAString& aTitle) {
456   mHeaderStrs[0] = aTitle;
457   return NS_OK;
458 }
459 
GetHeaderStrCenter(nsAString & aTitle)460 NS_IMETHODIMP nsPrintSettings::GetHeaderStrCenter(nsAString& aTitle) {
461   aTitle = mHeaderStrs[1];
462   return NS_OK;
463 }
SetHeaderStrCenter(const nsAString & aTitle)464 NS_IMETHODIMP nsPrintSettings::SetHeaderStrCenter(const nsAString& aTitle) {
465   mHeaderStrs[1] = aTitle;
466   return NS_OK;
467 }
468 
GetHeaderStrRight(nsAString & aTitle)469 NS_IMETHODIMP nsPrintSettings::GetHeaderStrRight(nsAString& aTitle) {
470   aTitle = mHeaderStrs[2];
471   return NS_OK;
472 }
SetHeaderStrRight(const nsAString & aTitle)473 NS_IMETHODIMP nsPrintSettings::SetHeaderStrRight(const nsAString& aTitle) {
474   mHeaderStrs[2] = aTitle;
475   return NS_OK;
476 }
477 
GetFooterStrLeft(nsAString & aTitle)478 NS_IMETHODIMP nsPrintSettings::GetFooterStrLeft(nsAString& aTitle) {
479   aTitle = mFooterStrs[0];
480   return NS_OK;
481 }
SetFooterStrLeft(const nsAString & aTitle)482 NS_IMETHODIMP nsPrintSettings::SetFooterStrLeft(const nsAString& aTitle) {
483   mFooterStrs[0] = aTitle;
484   return NS_OK;
485 }
486 
GetFooterStrCenter(nsAString & aTitle)487 NS_IMETHODIMP nsPrintSettings::GetFooterStrCenter(nsAString& aTitle) {
488   aTitle = mFooterStrs[1];
489   return NS_OK;
490 }
SetFooterStrCenter(const nsAString & aTitle)491 NS_IMETHODIMP nsPrintSettings::SetFooterStrCenter(const nsAString& aTitle) {
492   mFooterStrs[1] = aTitle;
493   return NS_OK;
494 }
495 
GetFooterStrRight(nsAString & aTitle)496 NS_IMETHODIMP nsPrintSettings::GetFooterStrRight(nsAString& aTitle) {
497   aTitle = mFooterStrs[2];
498   return NS_OK;
499 }
SetFooterStrRight(const nsAString & aTitle)500 NS_IMETHODIMP nsPrintSettings::SetFooterStrRight(const nsAString& aTitle) {
501   mFooterStrs[2] = aTitle;
502   return NS_OK;
503 }
504 
GetPrintSilent(bool * aPrintSilent)505 NS_IMETHODIMP nsPrintSettings::GetPrintSilent(bool* aPrintSilent) {
506   NS_ENSURE_ARG_POINTER(aPrintSilent);
507   *aPrintSilent = mPrintSilent;
508   return NS_OK;
509 }
SetPrintSilent(bool aPrintSilent)510 NS_IMETHODIMP nsPrintSettings::SetPrintSilent(bool aPrintSilent) {
511   mPrintSilent = aPrintSilent;
512   return NS_OK;
513 }
514 
GetShrinkToFit(bool * aShrinkToFit)515 NS_IMETHODIMP nsPrintSettings::GetShrinkToFit(bool* aShrinkToFit) {
516   NS_ENSURE_ARG_POINTER(aShrinkToFit);
517   *aShrinkToFit = mShrinkToFit;
518   return NS_OK;
519 }
SetShrinkToFit(bool aShrinkToFit)520 NS_IMETHODIMP nsPrintSettings::SetShrinkToFit(bool aShrinkToFit) {
521   mShrinkToFit = aShrinkToFit;
522   return NS_OK;
523 }
524 
GetShowPrintProgress(bool * aShowPrintProgress)525 NS_IMETHODIMP nsPrintSettings::GetShowPrintProgress(bool* aShowPrintProgress) {
526   NS_ENSURE_ARG_POINTER(aShowPrintProgress);
527   *aShowPrintProgress = mShowPrintProgress;
528   return NS_OK;
529 }
SetShowPrintProgress(bool aShowPrintProgress)530 NS_IMETHODIMP nsPrintSettings::SetShowPrintProgress(bool aShowPrintProgress) {
531   mShowPrintProgress = aShowPrintProgress;
532   return NS_OK;
533 }
534 
GetShowMarginGuides(bool * aShowMarginGuides)535 NS_IMETHODIMP nsPrintSettings::GetShowMarginGuides(bool* aShowMarginGuides) {
536   *aShowMarginGuides = mShowMarginGuides;
537   return NS_OK;
538 }
539 
SetShowMarginGuides(bool aShowMarginGuides)540 NS_IMETHODIMP nsPrintSettings::SetShowMarginGuides(bool aShowMarginGuides) {
541   mShowMarginGuides = aShowMarginGuides;
542   return NS_OK;
543 }
544 
GetHonorPageRuleMargins(bool * aResult)545 NS_IMETHODIMP nsPrintSettings::GetHonorPageRuleMargins(bool* aResult) {
546   *aResult = mHonorPageRuleMargins;
547   return NS_OK;
548 }
549 
SetHonorPageRuleMargins(bool aHonor)550 NS_IMETHODIMP nsPrintSettings::SetHonorPageRuleMargins(bool aHonor) {
551   mHonorPageRuleMargins = aHonor;
552   return NS_OK;
553 }
554 
GetIsPrintSelectionRBEnabled(bool * aIsPrintSelectionRBEnabled)555 NS_IMETHODIMP nsPrintSettings::GetIsPrintSelectionRBEnabled(
556     bool* aIsPrintSelectionRBEnabled) {
557   *aIsPrintSelectionRBEnabled = mIsPrintSelectionRBEnabled;
558   return NS_OK;
559 }
SetIsPrintSelectionRBEnabled(bool aIsPrintSelectionRBEnabled)560 NS_IMETHODIMP nsPrintSettings::SetIsPrintSelectionRBEnabled(
561     bool aIsPrintSelectionRBEnabled) {
562   mIsPrintSelectionRBEnabled = aIsPrintSelectionRBEnabled;
563   return NS_OK;
564 }
565 
GetPrintSelectionOnly(bool * aResult)566 NS_IMETHODIMP nsPrintSettings::GetPrintSelectionOnly(bool* aResult) {
567   *aResult = mPrintSelectionOnly;
568   return NS_OK;
569 }
570 
SetPrintSelectionOnly(bool aSelectionOnly)571 NS_IMETHODIMP nsPrintSettings::SetPrintSelectionOnly(bool aSelectionOnly) {
572   mPrintSelectionOnly = aSelectionOnly;
573   return NS_OK;
574 }
575 
GetPaperId(nsAString & aPaperId)576 NS_IMETHODIMP nsPrintSettings::GetPaperId(nsAString& aPaperId) {
577   aPaperId = mPaperId;
578   return NS_OK;
579 }
SetPaperId(const nsAString & aPaperId)580 NS_IMETHODIMP nsPrintSettings::SetPaperId(const nsAString& aPaperId) {
581   mPaperId = aPaperId;
582   return NS_OK;
583 }
584 
GetIsCancelled(bool * aIsCancelled)585 NS_IMETHODIMP nsPrintSettings::GetIsCancelled(bool* aIsCancelled) {
586   NS_ENSURE_ARG_POINTER(aIsCancelled);
587   *aIsCancelled = mIsCancelled;
588   return NS_OK;
589 }
SetIsCancelled(bool aIsCancelled)590 NS_IMETHODIMP nsPrintSettings::SetIsCancelled(bool aIsCancelled) {
591   mIsCancelled = aIsCancelled;
592   return NS_OK;
593 }
594 
GetSaveOnCancel(bool * aSaveOnCancel)595 NS_IMETHODIMP nsPrintSettings::GetSaveOnCancel(bool* aSaveOnCancel) {
596   *aSaveOnCancel = mSaveOnCancel;
597   return NS_OK;
598 }
599 
GetPaperWidth(double * aPaperWidth)600 NS_IMETHODIMP nsPrintSettings::GetPaperWidth(double* aPaperWidth) {
601   NS_ENSURE_ARG_POINTER(aPaperWidth);
602   *aPaperWidth = mPaperWidth;
603   return NS_OK;
604 }
SetPaperWidth(double aPaperWidth)605 NS_IMETHODIMP nsPrintSettings::SetPaperWidth(double aPaperWidth) {
606   mPaperWidth = aPaperWidth;
607   return NS_OK;
608 }
609 
GetPaperHeight(double * aPaperHeight)610 NS_IMETHODIMP nsPrintSettings::GetPaperHeight(double* aPaperHeight) {
611   NS_ENSURE_ARG_POINTER(aPaperHeight);
612   *aPaperHeight = mPaperHeight;
613   return NS_OK;
614 }
SetPaperHeight(double aPaperHeight)615 NS_IMETHODIMP nsPrintSettings::SetPaperHeight(double aPaperHeight) {
616   mPaperHeight = aPaperHeight;
617   return NS_OK;
618 }
619 
GetPaperSizeUnit(int16_t * aPaperSizeUnit)620 NS_IMETHODIMP nsPrintSettings::GetPaperSizeUnit(int16_t* aPaperSizeUnit) {
621   NS_ENSURE_ARG_POINTER(aPaperSizeUnit);
622   *aPaperSizeUnit = mPaperSizeUnit;
623   return NS_OK;
624 }
SetPaperSizeUnit(int16_t aPaperSizeUnit)625 NS_IMETHODIMP nsPrintSettings::SetPaperSizeUnit(int16_t aPaperSizeUnit) {
626   mPaperSizeUnit = aPaperSizeUnit;
627   return NS_OK;
628 }
629 
630 /** ---------------------------------------------------
631  *  See documentation in nsPrintSettingsService.h
632  *	@update 6/21/00 dwc
633  *	@update 1/12/01 rods
634  */
635 NS_IMETHODIMP
SetMarginInTwips(nsIntMargin & aMargin)636 nsPrintSettings::SetMarginInTwips(nsIntMargin& aMargin) {
637   mMargin = aMargin;
638   return NS_OK;
639 }
640 
641 NS_IMETHODIMP
SetEdgeInTwips(nsIntMargin & aEdge)642 nsPrintSettings::SetEdgeInTwips(nsIntMargin& aEdge) {
643   mEdge = aEdge;
644   return NS_OK;
645 }
646 
647 // NOTE: Any subclass implementation of this function should make sure
648 // to check for negative margin values in aUnwriteableMargin (which
649 // would indicate that we should use the system default unwriteable margin.)
650 NS_IMETHODIMP
SetUnwriteableMarginInTwips(nsIntMargin & aUnwriteableMargin)651 nsPrintSettings::SetUnwriteableMarginInTwips(nsIntMargin& aUnwriteableMargin) {
652   if (aUnwriteableMargin.top >= 0) {
653     mUnwriteableMargin.top = aUnwriteableMargin.top;
654   }
655   if (aUnwriteableMargin.left >= 0) {
656     mUnwriteableMargin.left = aUnwriteableMargin.left;
657   }
658   if (aUnwriteableMargin.bottom >= 0) {
659     mUnwriteableMargin.bottom = aUnwriteableMargin.bottom;
660   }
661   if (aUnwriteableMargin.right >= 0) {
662     mUnwriteableMargin.right = aUnwriteableMargin.right;
663   }
664   return NS_OK;
665 }
666 
GetMarginInTwips()667 nsIntMargin nsPrintSettings::GetMarginInTwips() { return mMargin; }
668 
GetEdgeInTwips()669 nsIntMargin nsPrintSettings::GetEdgeInTwips() { return mEdge; }
670 
GetUnwriteableMarginInTwips()671 nsIntMargin nsPrintSettings::GetUnwriteableMarginInTwips() {
672   return mUnwriteableMargin;
673 }
674 
675 /** ---------------------------------------------------
676  * Stub - platform-specific implementations can use this function.
677  */
678 NS_IMETHODIMP
SetupSilentPrinting()679 nsPrintSettings::SetupSilentPrinting() { return NS_OK; }
680 
681 /** ---------------------------------------------------
682  *  See documentation in nsPrintSettingsService.h
683  */
684 NS_IMETHODIMP
GetEffectivePageSize(double * aWidth,double * aHeight)685 nsPrintSettings::GetEffectivePageSize(double* aWidth, double* aHeight) {
686   if (mPaperSizeUnit == kPaperSizeInches) {
687     *aWidth = NS_INCHES_TO_TWIPS(float(mPaperWidth));
688     *aHeight = NS_INCHES_TO_TWIPS(float(mPaperHeight));
689   } else {
690     MOZ_ASSERT(mPaperSizeUnit == kPaperSizeMillimeters,
691                "unexpected paper size unit");
692     *aWidth = NS_MILLIMETERS_TO_TWIPS(float(mPaperWidth));
693     *aHeight = NS_MILLIMETERS_TO_TWIPS(float(mPaperHeight));
694   }
695   if (kLandscapeOrientation == mOrientation) {
696     double temp = *aWidth;
697     *aWidth = *aHeight;
698     *aHeight = temp;
699   }
700   return NS_OK;
701 }
702 
HasOrthogonalSheetsAndPages()703 bool nsPrintSettings::HasOrthogonalSheetsAndPages() {
704   return mNumPagesPerSheet == 2 || mNumPagesPerSheet == 6;
705 }
706 
GetEffectiveSheetSize(double * aWidth,double * aHeight)707 void nsPrintSettings::GetEffectiveSheetSize(double* aWidth, double* aHeight) {
708   mozilla::DebugOnly<nsresult> rv = GetEffectivePageSize(aWidth, aHeight);
709 
710   // Our GetEffectivePageSize impls only return NS_OK, so this should hold:
711   MOZ_ASSERT(NS_SUCCEEDED(rv), "Uh oh, GetEffectivePageSize failed");
712 
713   if (HasOrthogonalSheetsAndPages()) {
714     std::swap(*aWidth, *aHeight);
715   }
716 }
717 
GetSheetOrientation()718 int32_t nsPrintSettings::GetSheetOrientation() {
719   if (HasOrthogonalSheetsAndPages()) {
720     // Sheet orientation is rotated with respect to the page orientation.
721     return kLandscapeOrientation == mOrientation ? kPortraitOrientation
722                                                  : kLandscapeOrientation;
723   }
724 
725   // Sheet orientation is the same as the page orientation.
726   return mOrientation;
727 }
728 
729 NS_IMETHODIMP
SetPageRanges(const nsTArray<int32_t> & aPages)730 nsPrintSettings::SetPageRanges(const nsTArray<int32_t>& aPages) {
731   // Needs to be a set of (start, end) pairs.
732   if (aPages.Length() % 2 != 0) {
733     return NS_ERROR_FAILURE;
734   }
735   mPageRanges = aPages.Clone();
736   return NS_OK;
737 }
738 
739 NS_IMETHODIMP
GetPageRanges(nsTArray<int32_t> & aPages)740 nsPrintSettings::GetPageRanges(nsTArray<int32_t>& aPages) {
741   aPages = mPageRanges.Clone();
742   return NS_OK;
743 }
744 
IsPageSkipped(int32_t aPageNum,const nsTArray<int32_t> & aRanges)745 bool nsIPrintSettings::IsPageSkipped(int32_t aPageNum,
746                                      const nsTArray<int32_t>& aRanges) {
747   MOZ_RELEASE_ASSERT(aRanges.Length() % 2 == 0);
748   if (aRanges.IsEmpty()) {
749     return false;
750   }
751   for (size_t i = 0; i < aRanges.Length(); i += 2) {
752     if (aRanges[i] <= aPageNum && aPageNum <= aRanges[i + 1]) {
753       // The page is included in this piece of the custom range,
754       // so it's not skipped.
755       return false;
756     }
757   }
758   return true;
759 }
760 
_Clone(nsIPrintSettings ** _retval)761 nsresult nsPrintSettings::_Clone(nsIPrintSettings** _retval) {
762   RefPtr<nsPrintSettings> printSettings = new nsPrintSettings(*this);
763   printSettings.forget(_retval);
764   return NS_OK;
765 }
766 
767 NS_IMETHODIMP
Clone(nsIPrintSettings ** _retval)768 nsPrintSettings::Clone(nsIPrintSettings** _retval) {
769   NS_ENSURE_ARG_POINTER(_retval);
770   return _Clone(_retval);
771 }
772 
_Assign(nsIPrintSettings * aPS)773 nsresult nsPrintSettings::_Assign(nsIPrintSettings* aPS) {
774   nsPrintSettings* ps = static_cast<nsPrintSettings*>(aPS);
775   *this = *ps;
776   return NS_OK;
777 }
778 
779 NS_IMETHODIMP
Assign(nsIPrintSettings * aPS)780 nsPrintSettings::Assign(nsIPrintSettings* aPS) {
781   NS_ENSURE_ARG(aPS);
782   return _Assign(aPS);
783 }
784 
785 //-------------------------------------------
operator =(const nsPrintSettings & rhs)786 nsPrintSettings& nsPrintSettings::operator=(const nsPrintSettings& rhs) {
787   if (this == &rhs) {
788     return *this;
789   }
790 
791   mPageRanges = rhs.mPageRanges.Clone();
792   mMargin = rhs.mMargin;
793   mEdge = rhs.mEdge;
794   mUnwriteableMargin = rhs.mUnwriteableMargin;
795   mScaling = rhs.mScaling;
796   mPrintBGColors = rhs.mPrintBGColors;
797   mPrintBGImages = rhs.mPrintBGImages;
798   mTitle = rhs.mTitle;
799   mURL = rhs.mURL;
800   mIsCancelled = rhs.mIsCancelled;
801   mSaveOnCancel = rhs.mSaveOnCancel;
802   mPrintSilent = rhs.mPrintSilent;
803   mShrinkToFit = rhs.mShrinkToFit;
804   mShowPrintProgress = rhs.mShowPrintProgress;
805   mShowMarginGuides = rhs.mShowMarginGuides;
806   mHonorPageRuleMargins = rhs.mHonorPageRuleMargins;
807   mIsPrintSelectionRBEnabled = rhs.mIsPrintSelectionRBEnabled;
808   mPrintSelectionOnly = rhs.mPrintSelectionOnly;
809   mPaperId = rhs.mPaperId;
810   mPaperWidth = rhs.mPaperWidth;
811   mPaperHeight = rhs.mPaperHeight;
812   mPaperSizeUnit = rhs.mPaperSizeUnit;
813   mPrintReversed = rhs.mPrintReversed;
814   mPrintInColor = rhs.mPrintInColor;
815   mOrientation = rhs.mOrientation;
816   mResolution = rhs.mResolution;
817   mDuplex = rhs.mDuplex;
818   mNumCopies = rhs.mNumCopies;
819   mNumPagesPerSheet = rhs.mNumPagesPerSheet;
820   mPrinter = rhs.mPrinter;
821   mPrintToFile = rhs.mPrintToFile;
822   mToFileName = rhs.mToFileName;
823   mOutputFormat = rhs.mOutputFormat;
824   mIsInitedFromPrinter = rhs.mIsInitedFromPrinter;
825   mIsInitedFromPrefs = rhs.mIsInitedFromPrefs;
826   mPrintPageDelay = rhs.mPrintPageDelay;
827 
828   for (int32_t i = 0; i < NUM_HEAD_FOOT; i++) {
829     mHeaderStrs[i] = rhs.mHeaderStrs[i];
830     mFooterStrs[i] = rhs.mFooterStrs[i];
831   }
832 
833   return *this;
834 }
835 
SetDefaultFileName()836 void nsPrintSettings::SetDefaultFileName() {
837   nsAutoString filename;
838   nsresult rv = GetToFileName(filename);
839   if (NS_FAILED(rv) || filename.IsEmpty()) {
840     const char* path = PR_GetEnv("PWD");
841     if (!path) {
842       path = PR_GetEnv("HOME");
843     }
844 
845     if (path) {
846       CopyUTF8toUTF16(mozilla::MakeStringSpan(path), filename);
847       filename.AppendLiteral("/mozilla.pdf");
848     } else {
849       filename.AssignLiteral("mozilla.pdf");
850     }
851 
852     SetToFileName(filename);
853   }
854 }
855