Bug 1889091 - Part 4: Remove extra stack pointer move. r=jandem
[gecko.git] / widget / nsPrintSettingsImpl.cpp
blob45623af88aece6bbed9776cb65fee2c68a352986
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/. */
6 #include "nsPrintSettingsImpl.h"
8 #include "prenv.h"
9 #include "nsCoord.h"
10 #include "nsPaper.h"
11 #include "nsReadableUtils.h"
12 #include "mozilla/DebugOnly.h"
13 #include "mozilla/RefPtr.h"
15 using namespace mozilla;
17 #define DEFAULT_MARGIN_WIDTH 0.5
19 NS_IMPL_ISUPPORTS(nsPrintSettings, nsIPrintSettings)
21 nsPrintSettings::nsPrintSettings() {
22 /* member initializers and constructor code */
23 int32_t marginWidth = NS_INCHES_TO_INT_TWIPS(DEFAULT_MARGIN_WIDTH);
24 mMargin.SizeTo(marginWidth, marginWidth, marginWidth, marginWidth);
25 mEdge.SizeTo(0, 0, 0, 0);
26 mUnwriteableMargin.SizeTo(0, 0, 0, 0);
28 mHeaderStrs[0].AssignLiteral("&T");
29 mHeaderStrs[2].AssignLiteral("&U");
31 mFooterStrs[0].AssignLiteral(
32 "&PT"); // Use &P (Page Num Only) or &PT (Page Num of Page Total)
33 mFooterStrs[2].AssignLiteral("&D");
36 void nsPrintSettings::InitWithInitializer(
37 const PrintSettingsInitializer& aSettings) {
38 const double kInchesPerPoint = 1.0 / 72.0;
40 SetPrinterName(aSettings.mPrinter);
41 SetPrintInColor(aSettings.mPrintInColor);
42 SetResolution(aSettings.mResolution);
43 SetNumCopies(aSettings.mNumCopies);
44 SetDuplex(aSettings.mDuplex);
45 // The paper ID used by nsPrintSettings is the non-localizable identifier
46 // exposed as "id" by the paper, not the potentially localized human-friendly
47 // "name", which could change, e.g. if the user changes their system locale.
48 SetPaperId(aSettings.mPaperInfo.mId);
50 // Set the paper sizes to match the unit.
51 SetPaperSizeUnit(aSettings.mPaperSizeUnit);
52 double sizeUnitsPerPoint =
53 aSettings.mPaperSizeUnit == kPaperSizeInches ? 1.0 / 72.0 : 25.4 / 72.0;
54 SetPaperWidth(aSettings.mPaperInfo.mSize.width * sizeUnitsPerPoint);
55 SetPaperHeight(aSettings.mPaperInfo.mSize.height * sizeUnitsPerPoint);
57 // If our initializer says that we're producing portrait-mode sheets of
58 // paper, then our page format must also be portrait-mode; unless we've got
59 // a pages-per-sheet value with orthogonal pages/sheets, in which case it's
60 // reversed.
61 const bool areSheetsOfPaperPortraitMode =
62 (aSettings.mSheetOrientation == kPortraitOrientation);
63 const bool arePagesPortraitMode =
64 (areSheetsOfPaperPortraitMode != HasOrthogonalPagesPerSheet());
65 SetOrientation(arePagesPortraitMode ? kPortraitOrientation
66 : kLandscapeOrientation);
68 if (aSettings.mPaperInfo.mUnwriteableMargin) {
69 const auto& margin = aSettings.mPaperInfo.mUnwriteableMargin.value();
70 // Margins are stored internally in TWIPS, but the setters expect inches.
71 SetUnwriteableMarginTop(margin.top * kInchesPerPoint);
72 SetUnwriteableMarginRight(margin.right * kInchesPerPoint);
73 SetUnwriteableMarginBottom(margin.bottom * kInchesPerPoint);
74 SetUnwriteableMarginLeft(margin.left * kInchesPerPoint);
77 // Set this last because other setters may overwrite its value.
78 SetIsInitializedFromPrinter(true);
81 nsPrintSettings::nsPrintSettings(const nsPrintSettings& aPS) { *this = aPS; }
83 nsPrintSettings::~nsPrintSettings() = default;
85 NS_IMETHODIMP nsPrintSettings::GetPrintReversed(bool* aPrintReversed) {
86 *aPrintReversed = mPrintReversed;
87 return NS_OK;
89 NS_IMETHODIMP nsPrintSettings::SetPrintReversed(bool aPrintReversed) {
90 mPrintReversed = aPrintReversed;
91 return NS_OK;
94 NS_IMETHODIMP nsPrintSettings::GetPrintInColor(bool* aPrintInColor) {
95 *aPrintInColor = mPrintInColor;
96 return NS_OK;
98 NS_IMETHODIMP nsPrintSettings::SetPrintInColor(bool aPrintInColor) {
99 mPrintInColor = aPrintInColor;
100 return NS_OK;
103 NS_IMETHODIMP nsPrintSettings::GetOrientation(int32_t* aOrientation) {
104 *aOrientation = mOrientation;
105 return NS_OK;
107 NS_IMETHODIMP nsPrintSettings::SetOrientation(int32_t aOrientation) {
108 mOrientation = aOrientation;
109 return NS_OK;
112 NS_IMETHODIMP nsPrintSettings::GetResolution(int32_t* aResolution) {
113 NS_ENSURE_ARG_POINTER(aResolution);
114 *aResolution = mResolution;
115 return NS_OK;
117 NS_IMETHODIMP nsPrintSettings::SetResolution(const int32_t aResolution) {
118 mResolution = aResolution;
119 return NS_OK;
122 NS_IMETHODIMP nsPrintSettings::GetDuplex(int32_t* aDuplex) {
123 NS_ENSURE_ARG_POINTER(aDuplex);
124 *aDuplex = mDuplex;
125 return NS_OK;
127 NS_IMETHODIMP nsPrintSettings::SetDuplex(const int32_t aDuplex) {
128 mDuplex = aDuplex;
129 return NS_OK;
132 NS_IMETHODIMP nsPrintSettings::GetPrinterName(nsAString& aPrinter) {
133 aPrinter = mPrinter;
134 return NS_OK;
137 NS_IMETHODIMP nsPrintSettings::SetPrinterName(const nsAString& aPrinter) {
138 if (!mPrinter.Equals(aPrinter)) {
139 mIsInitedFromPrinter = false;
140 mIsInitedFromPrefs = false;
143 mPrinter.Assign(aPrinter);
144 return NS_OK;
147 NS_IMETHODIMP nsPrintSettings::GetNumCopies(int32_t* aNumCopies) {
148 NS_ENSURE_ARG_POINTER(aNumCopies);
149 *aNumCopies = mNumCopies;
150 return NS_OK;
152 NS_IMETHODIMP nsPrintSettings::SetNumCopies(int32_t aNumCopies) {
153 mNumCopies = aNumCopies;
154 return NS_OK;
157 NS_IMETHODIMP nsPrintSettings::GetNumPagesPerSheet(int32_t* aNumPagesPerSheet) {
158 NS_ENSURE_ARG_POINTER(aNumPagesPerSheet);
159 *aNumPagesPerSheet = mNumPagesPerSheet;
160 return NS_OK;
162 NS_IMETHODIMP nsPrintSettings::SetNumPagesPerSheet(int32_t aNumPagesPerSheet) {
163 mNumPagesPerSheet = aNumPagesPerSheet;
164 return NS_OK;
167 NS_IMETHODIMP nsPrintSettings::GetOutputDestination(
168 OutputDestinationType* aDestination) {
169 *aDestination = mOutputDestination;
170 return NS_OK;
173 NS_IMETHODIMP nsPrintSettings::SetOutputDestination(
174 OutputDestinationType aDestination) {
175 mOutputDestination = aDestination;
176 return NS_OK;
179 NS_IMETHODIMP nsPrintSettings::SetOutputStream(nsIOutputStream* aStream) {
180 mOutputStream = aStream;
181 return NS_OK;
184 NS_IMETHODIMP nsPrintSettings::GetOutputStream(nsIOutputStream** aStream) {
185 NS_IF_ADDREF(*aStream = mOutputStream.get());
186 return NS_OK;
189 NS_IMETHODIMP nsPrintSettings::GetToFileName(nsAString& aToFileName) {
190 aToFileName = mToFileName;
191 return NS_OK;
193 NS_IMETHODIMP nsPrintSettings::SetToFileName(const nsAString& aToFileName) {
194 mToFileName = aToFileName;
195 return NS_OK;
198 NS_IMETHODIMP nsPrintSettings::GetOutputFormat(int16_t* aOutputFormat) {
199 NS_ENSURE_ARG_POINTER(aOutputFormat);
200 *aOutputFormat = mOutputFormat;
201 return NS_OK;
203 NS_IMETHODIMP nsPrintSettings::SetOutputFormat(int16_t aOutputFormat) {
204 mOutputFormat = aOutputFormat;
205 return NS_OK;
208 NS_IMETHODIMP nsPrintSettings::GetPrintPageDelay(int32_t* aPrintPageDelay) {
209 *aPrintPageDelay = mPrintPageDelay;
210 return NS_OK;
212 NS_IMETHODIMP nsPrintSettings::SetPrintPageDelay(int32_t aPrintPageDelay) {
213 mPrintPageDelay = aPrintPageDelay;
214 return NS_OK;
217 NS_IMETHODIMP nsPrintSettings::GetIsInitializedFromPrinter(
218 bool* aIsInitializedFromPrinter) {
219 NS_ENSURE_ARG_POINTER(aIsInitializedFromPrinter);
220 *aIsInitializedFromPrinter = mIsInitedFromPrinter;
221 return NS_OK;
223 NS_IMETHODIMP nsPrintSettings::SetIsInitializedFromPrinter(
224 bool aIsInitializedFromPrinter) {
225 mIsInitedFromPrinter = aIsInitializedFromPrinter;
226 return NS_OK;
229 NS_IMETHODIMP nsPrintSettings::GetIsInitializedFromPrefs(
230 bool* aInitializedFromPrefs) {
231 NS_ENSURE_ARG_POINTER(aInitializedFromPrefs);
232 *aInitializedFromPrefs = mIsInitedFromPrefs;
233 return NS_OK;
235 NS_IMETHODIMP nsPrintSettings::SetIsInitializedFromPrefs(
236 bool aInitializedFromPrefs) {
237 mIsInitedFromPrefs = aInitializedFromPrefs;
238 return NS_OK;
241 NS_IMETHODIMP nsPrintSettings::GetMarginTop(double* aMarginTop) {
242 NS_ENSURE_ARG_POINTER(aMarginTop);
243 *aMarginTop = NS_TWIPS_TO_INCHES(mMargin.top);
244 return NS_OK;
246 NS_IMETHODIMP nsPrintSettings::SetMarginTop(double aMarginTop) {
247 mMargin.top = NS_INCHES_TO_INT_TWIPS(float(aMarginTop));
248 return NS_OK;
251 NS_IMETHODIMP nsPrintSettings::GetMarginLeft(double* aMarginLeft) {
252 NS_ENSURE_ARG_POINTER(aMarginLeft);
253 *aMarginLeft = NS_TWIPS_TO_INCHES(mMargin.left);
254 return NS_OK;
256 NS_IMETHODIMP nsPrintSettings::SetMarginLeft(double aMarginLeft) {
257 mMargin.left = NS_INCHES_TO_INT_TWIPS(float(aMarginLeft));
258 return NS_OK;
261 NS_IMETHODIMP nsPrintSettings::GetMarginBottom(double* aMarginBottom) {
262 NS_ENSURE_ARG_POINTER(aMarginBottom);
263 *aMarginBottom = NS_TWIPS_TO_INCHES(mMargin.bottom);
264 return NS_OK;
266 NS_IMETHODIMP nsPrintSettings::SetMarginBottom(double aMarginBottom) {
267 mMargin.bottom = NS_INCHES_TO_INT_TWIPS(float(aMarginBottom));
268 return NS_OK;
271 NS_IMETHODIMP nsPrintSettings::GetMarginRight(double* aMarginRight) {
272 NS_ENSURE_ARG_POINTER(aMarginRight);
273 *aMarginRight = NS_TWIPS_TO_INCHES(mMargin.right);
274 return NS_OK;
276 NS_IMETHODIMP nsPrintSettings::SetMarginRight(double aMarginRight) {
277 mMargin.right = NS_INCHES_TO_INT_TWIPS(float(aMarginRight));
278 return NS_OK;
281 NS_IMETHODIMP nsPrintSettings::GetEdgeTop(double* aEdgeTop) {
282 NS_ENSURE_ARG_POINTER(aEdgeTop);
283 *aEdgeTop = NS_TWIPS_TO_INCHES(mEdge.top);
284 return NS_OK;
286 NS_IMETHODIMP nsPrintSettings::SetEdgeTop(double aEdgeTop) {
287 mEdge.top = NS_INCHES_TO_INT_TWIPS(float(aEdgeTop));
288 return NS_OK;
291 NS_IMETHODIMP nsPrintSettings::GetEdgeLeft(double* aEdgeLeft) {
292 NS_ENSURE_ARG_POINTER(aEdgeLeft);
293 *aEdgeLeft = NS_TWIPS_TO_INCHES(mEdge.left);
294 return NS_OK;
296 NS_IMETHODIMP nsPrintSettings::SetEdgeLeft(double aEdgeLeft) {
297 mEdge.left = NS_INCHES_TO_INT_TWIPS(float(aEdgeLeft));
298 return NS_OK;
301 NS_IMETHODIMP nsPrintSettings::GetEdgeBottom(double* aEdgeBottom) {
302 NS_ENSURE_ARG_POINTER(aEdgeBottom);
303 *aEdgeBottom = NS_TWIPS_TO_INCHES(mEdge.bottom);
304 return NS_OK;
306 NS_IMETHODIMP nsPrintSettings::SetEdgeBottom(double aEdgeBottom) {
307 mEdge.bottom = NS_INCHES_TO_INT_TWIPS(float(aEdgeBottom));
308 return NS_OK;
311 NS_IMETHODIMP nsPrintSettings::GetEdgeRight(double* aEdgeRight) {
312 NS_ENSURE_ARG_POINTER(aEdgeRight);
313 *aEdgeRight = NS_TWIPS_TO_INCHES(mEdge.right);
314 return NS_OK;
316 NS_IMETHODIMP nsPrintSettings::SetEdgeRight(double aEdgeRight) {
317 mEdge.right = NS_INCHES_TO_INT_TWIPS(float(aEdgeRight));
318 return NS_OK;
321 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginTop(
322 double* aUnwriteableMarginTop) {
323 NS_ENSURE_ARG_POINTER(aUnwriteableMarginTop);
324 *aUnwriteableMarginTop = NS_TWIPS_TO_INCHES(mUnwriteableMargin.top);
325 return NS_OK;
327 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginTop(
328 double aUnwriteableMarginTop) {
329 if (aUnwriteableMarginTop >= 0.0) {
330 mUnwriteableMargin.top = NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginTop);
332 return NS_OK;
335 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginLeft(
336 double* aUnwriteableMarginLeft) {
337 NS_ENSURE_ARG_POINTER(aUnwriteableMarginLeft);
338 *aUnwriteableMarginLeft = NS_TWIPS_TO_INCHES(mUnwriteableMargin.left);
339 return NS_OK;
341 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginLeft(
342 double aUnwriteableMarginLeft) {
343 if (aUnwriteableMarginLeft >= 0.0) {
344 mUnwriteableMargin.left = NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginLeft);
346 return NS_OK;
349 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginBottom(
350 double* aUnwriteableMarginBottom) {
351 NS_ENSURE_ARG_POINTER(aUnwriteableMarginBottom);
352 *aUnwriteableMarginBottom = NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom);
353 return NS_OK;
355 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginBottom(
356 double aUnwriteableMarginBottom) {
357 if (aUnwriteableMarginBottom >= 0.0) {
358 mUnwriteableMargin.bottom =
359 NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginBottom);
361 return NS_OK;
364 NS_IMETHODIMP nsPrintSettings::GetUnwriteableMarginRight(
365 double* aUnwriteableMarginRight) {
366 NS_ENSURE_ARG_POINTER(aUnwriteableMarginRight);
367 *aUnwriteableMarginRight = NS_TWIPS_TO_INCHES(mUnwriteableMargin.right);
368 return NS_OK;
370 NS_IMETHODIMP nsPrintSettings::SetUnwriteableMarginRight(
371 double aUnwriteableMarginRight) {
372 if (aUnwriteableMarginRight >= 0.0) {
373 mUnwriteableMargin.right = NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginRight);
375 return NS_OK;
378 NS_IMETHODIMP nsPrintSettings::GetScaling(double* aScaling) {
379 NS_ENSURE_ARG_POINTER(aScaling);
380 *aScaling = mScaling;
381 return NS_OK;
384 NS_IMETHODIMP nsPrintSettings::SetScaling(double aScaling) {
385 mScaling = aScaling;
386 return NS_OK;
389 NS_IMETHODIMP nsPrintSettings::GetPrintBGColors(bool* aPrintBGColors) {
390 NS_ENSURE_ARG_POINTER(aPrintBGColors);
391 *aPrintBGColors = mPrintBGColors;
392 return NS_OK;
394 NS_IMETHODIMP nsPrintSettings::SetPrintBGColors(bool aPrintBGColors) {
395 mPrintBGColors = aPrintBGColors;
396 return NS_OK;
399 NS_IMETHODIMP nsPrintSettings::GetPrintBGImages(bool* aPrintBGImages) {
400 NS_ENSURE_ARG_POINTER(aPrintBGImages);
401 *aPrintBGImages = mPrintBGImages;
402 return NS_OK;
404 NS_IMETHODIMP nsPrintSettings::SetPrintBGImages(bool aPrintBGImages) {
405 mPrintBGImages = aPrintBGImages;
406 return NS_OK;
409 NS_IMETHODIMP nsPrintSettings::GetTitle(nsAString& aTitle) {
410 aTitle = mTitle;
411 return NS_OK;
413 NS_IMETHODIMP nsPrintSettings::SetTitle(const nsAString& aTitle) {
414 mTitle = aTitle;
415 return NS_OK;
418 NS_IMETHODIMP nsPrintSettings::GetDocURL(nsAString& aDocURL) {
419 aDocURL = mURL;
420 return NS_OK;
422 NS_IMETHODIMP nsPrintSettings::SetDocURL(const nsAString& aDocURL) {
423 mURL = aDocURL;
424 return NS_OK;
427 NS_IMETHODIMP nsPrintSettings::GetHeaderStrLeft(nsAString& aTitle) {
428 aTitle = mHeaderStrs[0];
429 return NS_OK;
431 NS_IMETHODIMP nsPrintSettings::SetHeaderStrLeft(const nsAString& aTitle) {
432 mHeaderStrs[0] = aTitle;
433 return NS_OK;
436 NS_IMETHODIMP nsPrintSettings::GetHeaderStrCenter(nsAString& aTitle) {
437 aTitle = mHeaderStrs[1];
438 return NS_OK;
440 NS_IMETHODIMP nsPrintSettings::SetHeaderStrCenter(const nsAString& aTitle) {
441 mHeaderStrs[1] = aTitle;
442 return NS_OK;
445 NS_IMETHODIMP nsPrintSettings::GetHeaderStrRight(nsAString& aTitle) {
446 aTitle = mHeaderStrs[2];
447 return NS_OK;
449 NS_IMETHODIMP nsPrintSettings::SetHeaderStrRight(const nsAString& aTitle) {
450 mHeaderStrs[2] = aTitle;
451 return NS_OK;
454 NS_IMETHODIMP nsPrintSettings::GetFooterStrLeft(nsAString& aTitle) {
455 aTitle = mFooterStrs[0];
456 return NS_OK;
458 NS_IMETHODIMP nsPrintSettings::SetFooterStrLeft(const nsAString& aTitle) {
459 mFooterStrs[0] = aTitle;
460 return NS_OK;
463 NS_IMETHODIMP nsPrintSettings::GetFooterStrCenter(nsAString& aTitle) {
464 aTitle = mFooterStrs[1];
465 return NS_OK;
467 NS_IMETHODIMP nsPrintSettings::SetFooterStrCenter(const nsAString& aTitle) {
468 mFooterStrs[1] = aTitle;
469 return NS_OK;
472 NS_IMETHODIMP nsPrintSettings::GetFooterStrRight(nsAString& aTitle) {
473 aTitle = mFooterStrs[2];
474 return NS_OK;
476 NS_IMETHODIMP nsPrintSettings::SetFooterStrRight(const nsAString& aTitle) {
477 mFooterStrs[2] = aTitle;
478 return NS_OK;
481 NS_IMETHODIMP nsPrintSettings::GetPrintSilent(bool* aPrintSilent) {
482 NS_ENSURE_ARG_POINTER(aPrintSilent);
483 *aPrintSilent = mPrintSilent;
484 return NS_OK;
486 NS_IMETHODIMP nsPrintSettings::SetPrintSilent(bool aPrintSilent) {
487 mPrintSilent = aPrintSilent;
488 return NS_OK;
491 NS_IMETHODIMP nsPrintSettings::GetShrinkToFit(bool* aShrinkToFit) {
492 NS_ENSURE_ARG_POINTER(aShrinkToFit);
493 *aShrinkToFit = mShrinkToFit;
494 return NS_OK;
496 NS_IMETHODIMP nsPrintSettings::SetShrinkToFit(bool aShrinkToFit) {
497 mShrinkToFit = aShrinkToFit;
498 return NS_OK;
501 NS_IMETHODIMP nsPrintSettings::GetShowMarginGuides(bool* aShowMarginGuides) {
502 *aShowMarginGuides = mShowMarginGuides;
503 return NS_OK;
506 NS_IMETHODIMP nsPrintSettings::SetShowMarginGuides(bool aShowMarginGuides) {
507 mShowMarginGuides = aShowMarginGuides;
508 return NS_OK;
511 NS_IMETHODIMP nsPrintSettings::GetHonorPageRuleMargins(bool* aResult) {
512 *aResult = mHonorPageRuleMargins;
513 return NS_OK;
516 NS_IMETHODIMP nsPrintSettings::SetHonorPageRuleMargins(bool aHonor) {
517 mHonorPageRuleMargins = aHonor;
518 return NS_OK;
521 NS_IMETHODIMP nsPrintSettings::GetUsePageRuleSizeAsPaperSize(bool* aResult) {
522 *aResult = mUsePageRuleSizeAsPaperSize;
523 return NS_OK;
526 NS_IMETHODIMP nsPrintSettings::SetUsePageRuleSizeAsPaperSize(bool aHonor) {
527 mUsePageRuleSizeAsPaperSize = aHonor;
528 return NS_OK;
531 NS_IMETHODIMP nsPrintSettings::GetIgnoreUnwriteableMargins(bool* aResult) {
532 *aResult = mIgnoreUnwriteableMargins;
533 return NS_OK;
536 NS_IMETHODIMP nsPrintSettings::SetIgnoreUnwriteableMargins(bool aIgnore) {
537 mIgnoreUnwriteableMargins = aIgnore;
538 return NS_OK;
541 NS_IMETHODIMP nsPrintSettings::GetPrintSelectionOnly(bool* aResult) {
542 *aResult = mPrintSelectionOnly;
543 return NS_OK;
546 NS_IMETHODIMP nsPrintSettings::SetPrintSelectionOnly(bool aSelectionOnly) {
547 mPrintSelectionOnly = aSelectionOnly;
548 return NS_OK;
551 NS_IMETHODIMP nsPrintSettings::GetPaperId(nsAString& aPaperId) {
552 aPaperId = mPaperId;
553 return NS_OK;
555 NS_IMETHODIMP nsPrintSettings::SetPaperId(const nsAString& aPaperId) {
556 mPaperId = aPaperId;
557 return NS_OK;
560 NS_IMETHODIMP nsPrintSettings::GetPaperWidth(double* aPaperWidth) {
561 NS_ENSURE_ARG_POINTER(aPaperWidth);
562 *aPaperWidth = mPaperWidth;
563 return NS_OK;
565 NS_IMETHODIMP nsPrintSettings::SetPaperWidth(double aPaperWidth) {
566 mPaperWidth = aPaperWidth;
567 return NS_OK;
570 NS_IMETHODIMP nsPrintSettings::GetPaperHeight(double* aPaperHeight) {
571 NS_ENSURE_ARG_POINTER(aPaperHeight);
572 *aPaperHeight = mPaperHeight;
573 return NS_OK;
575 NS_IMETHODIMP nsPrintSettings::SetPaperHeight(double aPaperHeight) {
576 mPaperHeight = aPaperHeight;
577 return NS_OK;
580 NS_IMETHODIMP nsPrintSettings::GetPaperSizeUnit(int16_t* aPaperSizeUnit) {
581 NS_ENSURE_ARG_POINTER(aPaperSizeUnit);
582 *aPaperSizeUnit = mPaperSizeUnit;
583 return NS_OK;
585 NS_IMETHODIMP nsPrintSettings::SetPaperSizeUnit(int16_t aPaperSizeUnit) {
586 mPaperSizeUnit = aPaperSizeUnit;
587 return NS_OK;
590 /** ---------------------------------------------------
591 * See documentation in nsPrintSettingsService.h
592 * @update 6/21/00 dwc
593 * @update 1/12/01 rods
595 NS_IMETHODIMP
596 nsPrintSettings::SetMarginInTwips(nsIntMargin& aMargin) {
597 mMargin = aMargin;
598 return NS_OK;
601 NS_IMETHODIMP
602 nsPrintSettings::SetEdgeInTwips(nsIntMargin& aEdge) {
603 mEdge = aEdge;
604 return NS_OK;
607 // NOTE: Any subclass implementation of this function should make sure
608 // to check for negative margin values in aUnwriteableMargin (which
609 // would indicate that we should use the system default unwriteable margin.)
610 NS_IMETHODIMP
611 nsPrintSettings::SetUnwriteableMarginInTwips(nsIntMargin& aUnwriteableMargin) {
612 if (aUnwriteableMargin.top >= 0) {
613 mUnwriteableMargin.top = aUnwriteableMargin.top;
615 if (aUnwriteableMargin.left >= 0) {
616 mUnwriteableMargin.left = aUnwriteableMargin.left;
618 if (aUnwriteableMargin.bottom >= 0) {
619 mUnwriteableMargin.bottom = aUnwriteableMargin.bottom;
621 if (aUnwriteableMargin.right >= 0) {
622 mUnwriteableMargin.right = aUnwriteableMargin.right;
624 return NS_OK;
627 nsIntMargin nsPrintSettings::GetMarginInTwips() { return mMargin; }
629 nsIntMargin nsPrintSettings::GetEdgeInTwips() { return mEdge; }
631 nsIntMargin nsPrintSettings::GetUnwriteableMarginInTwips() {
632 return mUnwriteableMargin;
635 /** ---------------------------------------------------
636 * See documentation in nsPrintSettingsService.h
638 NS_IMETHODIMP
639 nsPrintSettings::GetEffectivePageSize(double* aWidth, double* aHeight) {
640 if (mPaperSizeUnit == kPaperSizeInches) {
641 *aWidth = NS_INCHES_TO_TWIPS(float(mPaperWidth));
642 *aHeight = NS_INCHES_TO_TWIPS(float(mPaperHeight));
643 } else {
644 MOZ_ASSERT(mPaperSizeUnit == kPaperSizeMillimeters,
645 "unexpected paper size unit");
646 *aWidth = NS_MILLIMETERS_TO_TWIPS(float(mPaperWidth));
647 *aHeight = NS_MILLIMETERS_TO_TWIPS(float(mPaperHeight));
649 if (kLandscapeOrientation == mOrientation) {
650 double temp = *aWidth;
651 *aWidth = *aHeight;
652 *aHeight = temp;
654 return NS_OK;
657 bool nsPrintSettings::HasOrthogonalPagesPerSheet() {
658 return mNumPagesPerSheet == 2 || mNumPagesPerSheet == 6;
661 void nsPrintSettings::GetEffectiveSheetSize(double* aWidth, double* aHeight) {
662 mozilla::DebugOnly<nsresult> rv = GetEffectivePageSize(aWidth, aHeight);
664 // Our GetEffectivePageSize impls only return NS_OK, so this should hold:
665 MOZ_ASSERT(NS_SUCCEEDED(rv), "Uh oh, GetEffectivePageSize failed");
667 if (HasOrthogonalPagesPerSheet()) {
668 std::swap(*aWidth, *aHeight);
672 int32_t nsPrintSettings::GetSheetOrientation() {
673 if (HasOrthogonalPagesPerSheet()) {
674 // Sheet orientation is rotated with respect to the page orientation.
675 return kLandscapeOrientation == mOrientation ? kPortraitOrientation
676 : kLandscapeOrientation;
679 // Sheet orientation is the same as the page orientation.
680 return mOrientation;
683 NS_IMETHODIMP
684 nsPrintSettings::SetPageRanges(const nsTArray<int32_t>& aPages) {
685 // Needs to be a set of (start, end) pairs.
686 if (aPages.Length() % 2 != 0) {
687 return NS_ERROR_FAILURE;
689 mPageRanges = aPages.Clone();
690 return NS_OK;
693 NS_IMETHODIMP
694 nsPrintSettings::GetPageRanges(nsTArray<int32_t>& aPages) {
695 aPages = mPageRanges.Clone();
696 return NS_OK;
699 bool nsIPrintSettings::IsPageSkipped(int32_t aPageNum,
700 const nsTArray<int32_t>& aRanges) {
701 MOZ_RELEASE_ASSERT(aRanges.Length() % 2 == 0);
702 if (aRanges.IsEmpty()) {
703 return false;
705 for (size_t i = 0; i < aRanges.Length(); i += 2) {
706 if (aRanges[i] <= aPageNum && aPageNum <= aRanges[i + 1]) {
707 // The page is included in this piece of the custom range,
708 // so it's not skipped.
709 return false;
712 return true;
715 nsresult nsPrintSettings::_Clone(nsIPrintSettings** _retval) {
716 RefPtr<nsPrintSettings> printSettings = new nsPrintSettings(*this);
717 printSettings.forget(_retval);
718 return NS_OK;
721 NS_IMETHODIMP
722 nsPrintSettings::Clone(nsIPrintSettings** _retval) {
723 NS_ENSURE_ARG_POINTER(_retval);
724 return _Clone(_retval);
727 nsresult nsPrintSettings::_Assign(nsIPrintSettings* aPS) {
728 nsPrintSettings* ps = static_cast<nsPrintSettings*>(aPS);
729 *this = *ps;
730 return NS_OK;
733 nsresult nsPrintSettings::EquivalentTo(nsIPrintSettings* aPrintSettings,
734 bool* _retval) {
735 MOZ_ASSERT(aPrintSettings);
736 *_retval = false;
737 auto* other = static_cast<nsPrintSettings*>(aPrintSettings);
738 if (GetMarginInTwips() != aPrintSettings->GetMarginInTwips()) {
739 return NS_OK;
741 if (GetEdgeInTwips() != aPrintSettings->GetEdgeInTwips()) {
742 return NS_OK;
744 if (GetUnwriteableMarginInTwips() !=
745 aPrintSettings->GetUnwriteableMarginInTwips()) {
746 return NS_OK;
748 nsTArray<int32_t> ourPageRanges, otherPageRanges;
749 if (NS_FAILED(GetPageRanges(ourPageRanges)) ||
750 NS_FAILED(aPrintSettings->GetPageRanges(otherPageRanges)) ||
751 ourPageRanges != otherPageRanges) {
752 return NS_OK;
754 double ourScaling, otherScaling;
755 if (NS_FAILED(GetScaling(&ourScaling)) ||
756 NS_FAILED(aPrintSettings->GetScaling(&otherScaling)) ||
757 ourScaling != otherScaling) {
758 return NS_OK;
760 if (GetPrintBGColors() != aPrintSettings->GetPrintBGColors()) {
761 return NS_OK;
763 if (GetPrintBGImages() != aPrintSettings->GetPrintBGImages()) {
764 return NS_OK;
766 if (GetPrintSelectionOnly() != aPrintSettings->GetPrintSelectionOnly()) {
767 return NS_OK;
769 if (GetShrinkToFit() != aPrintSettings->GetShrinkToFit()) {
770 return NS_OK;
772 if (GetShowMarginGuides() != aPrintSettings->GetShowMarginGuides()) {
773 return NS_OK;
775 if (GetHonorPageRuleMargins() != aPrintSettings->GetHonorPageRuleMargins()) {
776 return NS_OK;
778 if (GetUsePageRuleSizeAsPaperSize() !=
779 aPrintSettings->GetUsePageRuleSizeAsPaperSize()) {
780 return NS_OK;
782 if (GetIgnoreUnwriteableMargins() !=
783 aPrintSettings->GetIgnoreUnwriteableMargins()) {
784 return NS_OK;
786 nsAutoString ourTitle, otherTitle;
787 if (NS_FAILED(GetTitle(ourTitle)) ||
788 NS_FAILED(aPrintSettings->GetTitle(otherTitle)) ||
789 ourTitle != otherTitle) {
790 return NS_OK;
792 nsAutoString ourUrl, otherUrl;
793 if (NS_FAILED(GetDocURL(ourUrl)) ||
794 NS_FAILED(aPrintSettings->GetDocURL(otherUrl)) || ourUrl != otherUrl) {
795 return NS_OK;
797 if (!mozilla::ArrayEqual(mHeaderStrs, other->mHeaderStrs) ||
798 !mozilla::ArrayEqual(mFooterStrs, other->mFooterStrs)) {
799 return NS_OK;
801 nsAutoString ourPaperId, otherPaperId;
802 if (NS_FAILED(GetPaperId(ourPaperId)) ||
803 NS_FAILED(aPrintSettings->GetPaperId(otherPaperId)) ||
804 ourPaperId != otherPaperId) {
805 return NS_OK;
807 double ourWidth, ourHeight, otherWidth, otherHeight;
808 if (NS_FAILED(GetEffectivePageSize(&ourWidth, &ourHeight)) ||
809 NS_FAILED(other->GetEffectivePageSize(&otherWidth, &otherHeight)) ||
810 std::abs(ourWidth - otherWidth) >= 1 ||
811 std::abs(ourHeight - otherHeight) >= 1) {
812 return NS_OK;
814 int32_t ourOrientation, otherOrientation;
815 if (NS_FAILED(GetOrientation(&ourOrientation)) ||
816 NS_FAILED(aPrintSettings->GetOrientation(&otherOrientation)) ||
817 ourOrientation != otherOrientation) {
818 return NS_OK;
820 int32_t ourResolution, otherResolution;
821 if (NS_FAILED(GetResolution(&ourResolution)) ||
822 NS_FAILED(aPrintSettings->GetResolution(&otherResolution)) ||
823 ourResolution != otherResolution) {
824 return NS_OK;
826 int32_t ourNumPagesPerSheet, otherNumPagesPerSheet;
827 if (NS_FAILED(GetNumPagesPerSheet(&ourNumPagesPerSheet)) ||
828 NS_FAILED(aPrintSettings->GetNumPagesPerSheet(&otherNumPagesPerSheet)) ||
829 ourNumPagesPerSheet != otherNumPagesPerSheet) {
830 return NS_OK;
833 *_retval = true;
834 return NS_OK;
837 mozilla::PrintSettingsInitializer nsPrintSettings::GetSettingsInitializer() {
838 mozilla::PrintSettingsInitializer settingsInitializer;
839 settingsInitializer.mPrinter.Assign(mPrinter);
840 settingsInitializer.mPaperInfo.mId = mPaperId;
842 double pointsPerSizeUnit =
843 mPaperSizeUnit == kPaperSizeInches ? 72.0 : 72.0 / 25.4;
844 settingsInitializer.mPaperInfo.mSize = {mPaperWidth * pointsPerSizeUnit,
845 mPaperHeight * pointsPerSizeUnit};
847 // Unwritable margins are stored in TWIPS here and points in PaperInfo.
848 settingsInitializer.mPaperInfo.mUnwriteableMargin =
849 Some(mozilla::gfx::MarginDouble(
850 mUnwriteableMargin.top / 20.0, mUnwriteableMargin.right / 20.0,
851 mUnwriteableMargin.bottom / 20.0, mUnwriteableMargin.left / 20.0));
853 settingsInitializer.mPrintInColor = mPrintInColor;
854 settingsInitializer.mResolution = mResolution;
855 settingsInitializer.mSheetOrientation = GetSheetOrientation();
856 settingsInitializer.mNumCopies = mNumCopies;
857 settingsInitializer.mDuplex = mDuplex;
858 RefPtr<nsIPrintSettings> settingsToInitialize;
859 MOZ_ALWAYS_SUCCEEDS(Clone(getter_AddRefs(settingsToInitialize)));
860 settingsInitializer.mPrintSettings =
861 new nsMainThreadPtrHolder<nsPrintSettings>(
862 "PrintSettingsInitializer::mPrintSettings",
863 settingsToInitialize.forget().downcast<nsPrintSettings>());
864 return settingsInitializer;
867 NS_IMETHODIMP
868 nsPrintSettings::Assign(nsIPrintSettings* aPS) {
869 NS_ENSURE_ARG(aPS);
870 return _Assign(aPS);
873 //-------------------------------------------
874 nsPrintSettings& nsPrintSettings::operator=(const nsPrintSettings& rhs) {
875 if (this == &rhs) {
876 return *this;
879 mPageRanges = rhs.mPageRanges.Clone();
880 mMargin = rhs.mMargin;
881 mEdge = rhs.mEdge;
882 mUnwriteableMargin = rhs.mUnwriteableMargin;
883 mScaling = rhs.mScaling;
884 mPrintBGColors = rhs.mPrintBGColors;
885 mPrintBGImages = rhs.mPrintBGImages;
886 mTitle = rhs.mTitle;
887 mURL = rhs.mURL;
888 mPrintSilent = rhs.mPrintSilent;
889 mShrinkToFit = rhs.mShrinkToFit;
890 mShowMarginGuides = rhs.mShowMarginGuides;
891 mHonorPageRuleMargins = rhs.mHonorPageRuleMargins;
892 mUsePageRuleSizeAsPaperSize = rhs.mUsePageRuleSizeAsPaperSize;
893 mIgnoreUnwriteableMargins = rhs.mIgnoreUnwriteableMargins;
894 mPrintSelectionOnly = rhs.mPrintSelectionOnly;
895 mPaperId = rhs.mPaperId;
896 mPaperWidth = rhs.mPaperWidth;
897 mPaperHeight = rhs.mPaperHeight;
898 mPaperSizeUnit = rhs.mPaperSizeUnit;
899 mPrintReversed = rhs.mPrintReversed;
900 mPrintInColor = rhs.mPrintInColor;
901 mOrientation = rhs.mOrientation;
902 mResolution = rhs.mResolution;
903 mDuplex = rhs.mDuplex;
904 mNumCopies = rhs.mNumCopies;
905 mNumPagesPerSheet = rhs.mNumPagesPerSheet;
906 mPrinter = rhs.mPrinter;
907 mOutputDestination = rhs.mOutputDestination;
908 mOutputStream = rhs.mOutputStream;
909 mToFileName = rhs.mToFileName;
910 mOutputFormat = rhs.mOutputFormat;
911 mIsInitedFromPrinter = rhs.mIsInitedFromPrinter;
912 mIsInitedFromPrefs = rhs.mIsInitedFromPrefs;
913 mPrintPageDelay = rhs.mPrintPageDelay;
915 for (int32_t i = 0; i < NUM_HEAD_FOOT; i++) {
916 mHeaderStrs[i] = rhs.mHeaderStrs[i];
917 mFooterStrs[i] = rhs.mFooterStrs[i];
920 return *this;
923 void nsPrintSettings::SetDefaultFileName() {
924 nsAutoString filename;
925 nsresult rv = GetToFileName(filename);
926 if (NS_FAILED(rv) || filename.IsEmpty()) {
927 const char* path = PR_GetEnv("PWD");
928 if (!path) {
929 path = PR_GetEnv("HOME");
932 if (path) {
933 CopyUTF8toUTF16(mozilla::MakeStringSpan(path), filename);
934 filename.AppendLiteral("/mozilla.pdf");
935 } else {
936 filename.AssignLiteral("mozilla.pdf");
939 SetToFileName(filename);