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"
11 #include "nsReadableUtils.h"
12 #include "nsIPrintSession.h"
13 #include "mozilla/ArrayUtils.h"
14 #include "mozilla/DebugOnly.h"
15 #include "mozilla/RefPtr.h"
17 using namespace mozilla
;
19 #define DEFAULT_MARGIN_WIDTH 0.5
21 NS_IMPL_ISUPPORTS(nsPrintSettings
, nsIPrintSettings
)
23 nsPrintSettings::nsPrintSettings()
25 mPrintBGColors(false),
26 mPrintBGImages(false),
31 mShowMarginGuides(false),
32 mHonorPageRuleMargins(true),
33 mIsPrintSelectionRBEnabled(false),
34 mPrintSelectionOnly(false),
38 mPaperSizeUnit(kPaperSizeInches
),
39 mPrintReversed(false),
41 mOrientation(kPortraitOrientation
),
47 mOutputFormat(kOutputFormatNative
),
48 mIsInitedFromPrinter(false),
49 mIsInitedFromPrefs(false) {
50 /* member initializers and constructor code */
51 int32_t marginWidth
= NS_INCHES_TO_INT_TWIPS(DEFAULT_MARGIN_WIDTH
);
52 mMargin
.SizeTo(marginWidth
, marginWidth
, marginWidth
, marginWidth
);
53 mEdge
.SizeTo(0, 0, 0, 0);
54 mUnwriteableMargin
.SizeTo(0, 0, 0, 0);
56 mHeaderStrs
[0].AssignLiteral("&T");
57 mHeaderStrs
[2].AssignLiteral("&U");
59 mFooterStrs
[0].AssignLiteral(
60 "&PT"); // Use &P (Page Num Only) or &PT (Page Num of Page Total)
61 mFooterStrs
[2].AssignLiteral("&D");
64 void nsPrintSettings::InitWithInitializer(
65 const PrintSettingsInitializer
& aSettings
) {
66 const double kInchesPerPoint
= 1.0 / 72.0;
68 SetPrinterName(aSettings
.mPrinter
);
69 SetPrintInColor(aSettings
.mPrintInColor
);
70 SetResolution(aSettings
.mResolution
);
71 SetNumCopies(aSettings
.mNumCopies
);
72 SetDuplex(aSettings
.mDuplex
);
73 // The paper ID used by nsPrintSettings is the non-localizable identifier
74 // exposed as "id" by the paper, not the potentially localized human-friendly
75 // "name", which could change, e.g. if the user changes their system locale.
76 SetPaperId(aSettings
.mPaperInfo
.mId
);
78 // Set the paper sizes to match the unit.
79 SetPaperSizeUnit(aSettings
.mPaperSizeUnit
);
80 double sizeUnitsPerPoint
=
81 aSettings
.mPaperSizeUnit
== kPaperSizeInches
? 1.0 / 72.0 : 25.4 / 72.0;
82 SetPaperWidth(aSettings
.mPaperInfo
.mSize
.width
* sizeUnitsPerPoint
);
83 SetPaperHeight(aSettings
.mPaperInfo
.mSize
.height
* sizeUnitsPerPoint
);
85 // If our initializer says that we're producing portrait-mode sheets of
86 // paper, then our page format must also be portrait-mode; unless we've got
87 // a pages-per-sheet value with orthogonal pages/sheets, in which case it's
89 const bool areSheetsOfPaperPortraitMode
=
90 (aSettings
.mSheetOrientation
== kPortraitOrientation
);
91 const bool arePagesPortraitMode
=
92 (areSheetsOfPaperPortraitMode
!= HasOrthogonalSheetsAndPages());
93 SetOrientation(arePagesPortraitMode
? kPortraitOrientation
94 : kLandscapeOrientation
);
96 if (aSettings
.mPaperInfo
.mUnwriteableMargin
) {
97 const auto& margin
= aSettings
.mPaperInfo
.mUnwriteableMargin
.value();
98 // Margins are stored internally in TWIPS, but the setters expect inches.
99 SetUnwriteableMarginTop(margin
.top
* kInchesPerPoint
);
100 SetUnwriteableMarginRight(margin
.right
* kInchesPerPoint
);
101 SetUnwriteableMarginBottom(margin
.bottom
* kInchesPerPoint
);
102 SetUnwriteableMarginLeft(margin
.left
* kInchesPerPoint
);
105 // Set this last because other setters may overwrite its value.
106 SetIsInitializedFromPrinter(true);
109 nsPrintSettings::nsPrintSettings(const nsPrintSettings
& aPS
) { *this = aPS
; }
111 nsPrintSettings::~nsPrintSettings() = default;
113 NS_IMETHODIMP
nsPrintSettings::GetPrintSession(
114 nsIPrintSession
** aPrintSession
) {
115 NS_ENSURE_ARG_POINTER(aPrintSession
);
116 *aPrintSession
= nullptr;
118 nsCOMPtr
<nsIPrintSession
> session
= do_QueryReferent(mSession
);
119 if (!session
) return NS_ERROR_NOT_INITIALIZED
;
120 *aPrintSession
= session
;
121 NS_ADDREF(*aPrintSession
);
125 NS_IMETHODIMP
nsPrintSettings::SetPrintSession(nsIPrintSession
* aPrintSession
) {
126 // Clearing it by passing nullptr is not allowed. That's why we
127 // use a weak ref so that it doesn't have to be cleared.
128 NS_ENSURE_ARG(aPrintSession
);
130 mSession
= do_GetWeakReference(aPrintSession
);
132 // This may happen if the implementation of this object does
133 // not support weak references - programmer error.
134 NS_ERROR("Could not get a weak reference from aPrintSession");
135 return NS_ERROR_FAILURE
;
140 NS_IMETHODIMP
nsPrintSettings::GetPrintReversed(bool* aPrintReversed
) {
141 *aPrintReversed
= mPrintReversed
;
144 NS_IMETHODIMP
nsPrintSettings::SetPrintReversed(bool aPrintReversed
) {
145 mPrintReversed
= aPrintReversed
;
149 NS_IMETHODIMP
nsPrintSettings::GetPrintInColor(bool* aPrintInColor
) {
150 *aPrintInColor
= mPrintInColor
;
153 NS_IMETHODIMP
nsPrintSettings::SetPrintInColor(bool aPrintInColor
) {
154 mPrintInColor
= aPrintInColor
;
158 NS_IMETHODIMP
nsPrintSettings::GetOrientation(int32_t* aOrientation
) {
159 *aOrientation
= mOrientation
;
162 NS_IMETHODIMP
nsPrintSettings::SetOrientation(int32_t aOrientation
) {
163 mOrientation
= aOrientation
;
167 NS_IMETHODIMP
nsPrintSettings::GetResolution(int32_t* aResolution
) {
168 NS_ENSURE_ARG_POINTER(aResolution
);
169 *aResolution
= mResolution
;
172 NS_IMETHODIMP
nsPrintSettings::SetResolution(const int32_t aResolution
) {
173 mResolution
= aResolution
;
177 NS_IMETHODIMP
nsPrintSettings::GetDuplex(int32_t* aDuplex
) {
178 NS_ENSURE_ARG_POINTER(aDuplex
);
182 NS_IMETHODIMP
nsPrintSettings::SetDuplex(const int32_t aDuplex
) {
187 NS_IMETHODIMP
nsPrintSettings::GetPrinterName(nsAString
& aPrinter
) {
192 NS_IMETHODIMP
nsPrintSettings::SetPrinterName(const nsAString
& aPrinter
) {
193 if (!mPrinter
.Equals(aPrinter
)) {
194 mIsInitedFromPrinter
= false;
195 mIsInitedFromPrefs
= false;
198 mPrinter
.Assign(aPrinter
);
202 NS_IMETHODIMP
nsPrintSettings::GetNumCopies(int32_t* aNumCopies
) {
203 NS_ENSURE_ARG_POINTER(aNumCopies
);
204 *aNumCopies
= mNumCopies
;
207 NS_IMETHODIMP
nsPrintSettings::SetNumCopies(int32_t aNumCopies
) {
208 mNumCopies
= aNumCopies
;
212 NS_IMETHODIMP
nsPrintSettings::GetNumPagesPerSheet(int32_t* aNumPagesPerSheet
) {
213 NS_ENSURE_ARG_POINTER(aNumPagesPerSheet
);
214 *aNumPagesPerSheet
= mNumPagesPerSheet
;
217 NS_IMETHODIMP
nsPrintSettings::SetNumPagesPerSheet(int32_t aNumPagesPerSheet
) {
218 mNumPagesPerSheet
= aNumPagesPerSheet
;
222 NS_IMETHODIMP
nsPrintSettings::GetPrintToFile(bool* aPrintToFile
) {
223 NS_ENSURE_ARG_POINTER(aPrintToFile
);
224 *aPrintToFile
= mPrintToFile
;
227 NS_IMETHODIMP
nsPrintSettings::SetPrintToFile(bool aPrintToFile
) {
228 mPrintToFile
= aPrintToFile
;
232 NS_IMETHODIMP
nsPrintSettings::GetToFileName(nsAString
& aToFileName
) {
233 aToFileName
= mToFileName
;
236 NS_IMETHODIMP
nsPrintSettings::SetToFileName(const nsAString
& aToFileName
) {
237 mToFileName
= aToFileName
;
241 NS_IMETHODIMP
nsPrintSettings::GetOutputFormat(int16_t* aOutputFormat
) {
242 NS_ENSURE_ARG_POINTER(aOutputFormat
);
243 *aOutputFormat
= mOutputFormat
;
246 NS_IMETHODIMP
nsPrintSettings::SetOutputFormat(int16_t aOutputFormat
) {
247 mOutputFormat
= aOutputFormat
;
251 NS_IMETHODIMP
nsPrintSettings::GetPrintPageDelay(int32_t* aPrintPageDelay
) {
252 *aPrintPageDelay
= mPrintPageDelay
;
255 NS_IMETHODIMP
nsPrintSettings::SetPrintPageDelay(int32_t aPrintPageDelay
) {
256 mPrintPageDelay
= aPrintPageDelay
;
260 NS_IMETHODIMP
nsPrintSettings::GetIsInitializedFromPrinter(
261 bool* aIsInitializedFromPrinter
) {
262 NS_ENSURE_ARG_POINTER(aIsInitializedFromPrinter
);
263 *aIsInitializedFromPrinter
= mIsInitedFromPrinter
;
266 NS_IMETHODIMP
nsPrintSettings::SetIsInitializedFromPrinter(
267 bool aIsInitializedFromPrinter
) {
268 mIsInitedFromPrinter
= aIsInitializedFromPrinter
;
272 NS_IMETHODIMP
nsPrintSettings::GetIsInitializedFromPrefs(
273 bool* aInitializedFromPrefs
) {
274 NS_ENSURE_ARG_POINTER(aInitializedFromPrefs
);
275 *aInitializedFromPrefs
= mIsInitedFromPrefs
;
278 NS_IMETHODIMP
nsPrintSettings::SetIsInitializedFromPrefs(
279 bool aInitializedFromPrefs
) {
280 mIsInitedFromPrefs
= aInitializedFromPrefs
;
284 NS_IMETHODIMP
nsPrintSettings::GetMarginTop(double* aMarginTop
) {
285 NS_ENSURE_ARG_POINTER(aMarginTop
);
286 *aMarginTop
= NS_TWIPS_TO_INCHES(mMargin
.top
);
289 NS_IMETHODIMP
nsPrintSettings::SetMarginTop(double aMarginTop
) {
290 mMargin
.top
= NS_INCHES_TO_INT_TWIPS(float(aMarginTop
));
294 NS_IMETHODIMP
nsPrintSettings::GetMarginLeft(double* aMarginLeft
) {
295 NS_ENSURE_ARG_POINTER(aMarginLeft
);
296 *aMarginLeft
= NS_TWIPS_TO_INCHES(mMargin
.left
);
299 NS_IMETHODIMP
nsPrintSettings::SetMarginLeft(double aMarginLeft
) {
300 mMargin
.left
= NS_INCHES_TO_INT_TWIPS(float(aMarginLeft
));
304 NS_IMETHODIMP
nsPrintSettings::GetMarginBottom(double* aMarginBottom
) {
305 NS_ENSURE_ARG_POINTER(aMarginBottom
);
306 *aMarginBottom
= NS_TWIPS_TO_INCHES(mMargin
.bottom
);
309 NS_IMETHODIMP
nsPrintSettings::SetMarginBottom(double aMarginBottom
) {
310 mMargin
.bottom
= NS_INCHES_TO_INT_TWIPS(float(aMarginBottom
));
314 NS_IMETHODIMP
nsPrintSettings::GetMarginRight(double* aMarginRight
) {
315 NS_ENSURE_ARG_POINTER(aMarginRight
);
316 *aMarginRight
= NS_TWIPS_TO_INCHES(mMargin
.right
);
319 NS_IMETHODIMP
nsPrintSettings::SetMarginRight(double aMarginRight
) {
320 mMargin
.right
= NS_INCHES_TO_INT_TWIPS(float(aMarginRight
));
324 NS_IMETHODIMP
nsPrintSettings::GetEdgeTop(double* aEdgeTop
) {
325 NS_ENSURE_ARG_POINTER(aEdgeTop
);
326 *aEdgeTop
= NS_TWIPS_TO_INCHES(mEdge
.top
);
329 NS_IMETHODIMP
nsPrintSettings::SetEdgeTop(double aEdgeTop
) {
330 mEdge
.top
= NS_INCHES_TO_INT_TWIPS(float(aEdgeTop
));
334 NS_IMETHODIMP
nsPrintSettings::GetEdgeLeft(double* aEdgeLeft
) {
335 NS_ENSURE_ARG_POINTER(aEdgeLeft
);
336 *aEdgeLeft
= NS_TWIPS_TO_INCHES(mEdge
.left
);
339 NS_IMETHODIMP
nsPrintSettings::SetEdgeLeft(double aEdgeLeft
) {
340 mEdge
.left
= NS_INCHES_TO_INT_TWIPS(float(aEdgeLeft
));
344 NS_IMETHODIMP
nsPrintSettings::GetEdgeBottom(double* aEdgeBottom
) {
345 NS_ENSURE_ARG_POINTER(aEdgeBottom
);
346 *aEdgeBottom
= NS_TWIPS_TO_INCHES(mEdge
.bottom
);
349 NS_IMETHODIMP
nsPrintSettings::SetEdgeBottom(double aEdgeBottom
) {
350 mEdge
.bottom
= NS_INCHES_TO_INT_TWIPS(float(aEdgeBottom
));
354 NS_IMETHODIMP
nsPrintSettings::GetEdgeRight(double* aEdgeRight
) {
355 NS_ENSURE_ARG_POINTER(aEdgeRight
);
356 *aEdgeRight
= NS_TWIPS_TO_INCHES(mEdge
.right
);
359 NS_IMETHODIMP
nsPrintSettings::SetEdgeRight(double aEdgeRight
) {
360 mEdge
.right
= NS_INCHES_TO_INT_TWIPS(float(aEdgeRight
));
364 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginTop(
365 double* aUnwriteableMarginTop
) {
366 NS_ENSURE_ARG_POINTER(aUnwriteableMarginTop
);
367 *aUnwriteableMarginTop
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.top
);
370 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginTop(
371 double aUnwriteableMarginTop
) {
372 if (aUnwriteableMarginTop
>= 0.0) {
373 mUnwriteableMargin
.top
= NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginTop
);
378 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginLeft(
379 double* aUnwriteableMarginLeft
) {
380 NS_ENSURE_ARG_POINTER(aUnwriteableMarginLeft
);
381 *aUnwriteableMarginLeft
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.left
);
384 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginLeft(
385 double aUnwriteableMarginLeft
) {
386 if (aUnwriteableMarginLeft
>= 0.0) {
387 mUnwriteableMargin
.left
= NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginLeft
);
392 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginBottom(
393 double* aUnwriteableMarginBottom
) {
394 NS_ENSURE_ARG_POINTER(aUnwriteableMarginBottom
);
395 *aUnwriteableMarginBottom
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.bottom
);
398 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginBottom(
399 double aUnwriteableMarginBottom
) {
400 if (aUnwriteableMarginBottom
>= 0.0) {
401 mUnwriteableMargin
.bottom
=
402 NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginBottom
);
407 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginRight(
408 double* aUnwriteableMarginRight
) {
409 NS_ENSURE_ARG_POINTER(aUnwriteableMarginRight
);
410 *aUnwriteableMarginRight
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.right
);
413 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginRight(
414 double aUnwriteableMarginRight
) {
415 if (aUnwriteableMarginRight
>= 0.0) {
416 mUnwriteableMargin
.right
= NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginRight
);
421 NS_IMETHODIMP
nsPrintSettings::GetScaling(double* aScaling
) {
422 NS_ENSURE_ARG_POINTER(aScaling
);
423 *aScaling
= mScaling
;
427 NS_IMETHODIMP
nsPrintSettings::SetScaling(double aScaling
) {
432 NS_IMETHODIMP
nsPrintSettings::GetPrintBGColors(bool* aPrintBGColors
) {
433 NS_ENSURE_ARG_POINTER(aPrintBGColors
);
434 *aPrintBGColors
= mPrintBGColors
;
437 NS_IMETHODIMP
nsPrintSettings::SetPrintBGColors(bool aPrintBGColors
) {
438 mPrintBGColors
= aPrintBGColors
;
442 NS_IMETHODIMP
nsPrintSettings::GetPrintBGImages(bool* aPrintBGImages
) {
443 NS_ENSURE_ARG_POINTER(aPrintBGImages
);
444 *aPrintBGImages
= mPrintBGImages
;
447 NS_IMETHODIMP
nsPrintSettings::SetPrintBGImages(bool aPrintBGImages
) {
448 mPrintBGImages
= aPrintBGImages
;
452 NS_IMETHODIMP
nsPrintSettings::GetTitle(nsAString
& aTitle
) {
456 NS_IMETHODIMP
nsPrintSettings::SetTitle(const nsAString
& aTitle
) {
461 NS_IMETHODIMP
nsPrintSettings::GetDocURL(nsAString
& aDocURL
) {
465 NS_IMETHODIMP
nsPrintSettings::SetDocURL(const nsAString
& aDocURL
) {
470 NS_IMETHODIMP
nsPrintSettings::GetHeaderStrLeft(nsAString
& aTitle
) {
471 aTitle
= mHeaderStrs
[0];
474 NS_IMETHODIMP
nsPrintSettings::SetHeaderStrLeft(const nsAString
& aTitle
) {
475 mHeaderStrs
[0] = aTitle
;
479 NS_IMETHODIMP
nsPrintSettings::GetHeaderStrCenter(nsAString
& aTitle
) {
480 aTitle
= mHeaderStrs
[1];
483 NS_IMETHODIMP
nsPrintSettings::SetHeaderStrCenter(const nsAString
& aTitle
) {
484 mHeaderStrs
[1] = aTitle
;
488 NS_IMETHODIMP
nsPrintSettings::GetHeaderStrRight(nsAString
& aTitle
) {
489 aTitle
= mHeaderStrs
[2];
492 NS_IMETHODIMP
nsPrintSettings::SetHeaderStrRight(const nsAString
& aTitle
) {
493 mHeaderStrs
[2] = aTitle
;
497 NS_IMETHODIMP
nsPrintSettings::GetFooterStrLeft(nsAString
& aTitle
) {
498 aTitle
= mFooterStrs
[0];
501 NS_IMETHODIMP
nsPrintSettings::SetFooterStrLeft(const nsAString
& aTitle
) {
502 mFooterStrs
[0] = aTitle
;
506 NS_IMETHODIMP
nsPrintSettings::GetFooterStrCenter(nsAString
& aTitle
) {
507 aTitle
= mFooterStrs
[1];
510 NS_IMETHODIMP
nsPrintSettings::SetFooterStrCenter(const nsAString
& aTitle
) {
511 mFooterStrs
[1] = aTitle
;
515 NS_IMETHODIMP
nsPrintSettings::GetFooterStrRight(nsAString
& aTitle
) {
516 aTitle
= mFooterStrs
[2];
519 NS_IMETHODIMP
nsPrintSettings::SetFooterStrRight(const nsAString
& aTitle
) {
520 mFooterStrs
[2] = aTitle
;
524 NS_IMETHODIMP
nsPrintSettings::GetPrintSilent(bool* aPrintSilent
) {
525 NS_ENSURE_ARG_POINTER(aPrintSilent
);
526 *aPrintSilent
= mPrintSilent
;
529 NS_IMETHODIMP
nsPrintSettings::SetPrintSilent(bool aPrintSilent
) {
530 mPrintSilent
= aPrintSilent
;
534 NS_IMETHODIMP
nsPrintSettings::GetShrinkToFit(bool* aShrinkToFit
) {
535 NS_ENSURE_ARG_POINTER(aShrinkToFit
);
536 *aShrinkToFit
= mShrinkToFit
;
539 NS_IMETHODIMP
nsPrintSettings::SetShrinkToFit(bool aShrinkToFit
) {
540 mShrinkToFit
= aShrinkToFit
;
544 NS_IMETHODIMP
nsPrintSettings::GetShowMarginGuides(bool* aShowMarginGuides
) {
545 *aShowMarginGuides
= mShowMarginGuides
;
549 NS_IMETHODIMP
nsPrintSettings::SetShowMarginGuides(bool aShowMarginGuides
) {
550 mShowMarginGuides
= aShowMarginGuides
;
554 NS_IMETHODIMP
nsPrintSettings::GetHonorPageRuleMargins(bool* aResult
) {
555 *aResult
= mHonorPageRuleMargins
;
559 NS_IMETHODIMP
nsPrintSettings::SetHonorPageRuleMargins(bool aHonor
) {
560 mHonorPageRuleMargins
= aHonor
;
564 NS_IMETHODIMP
nsPrintSettings::GetIsPrintSelectionRBEnabled(
565 bool* aIsPrintSelectionRBEnabled
) {
566 *aIsPrintSelectionRBEnabled
= mIsPrintSelectionRBEnabled
;
569 NS_IMETHODIMP
nsPrintSettings::SetIsPrintSelectionRBEnabled(
570 bool aIsPrintSelectionRBEnabled
) {
571 mIsPrintSelectionRBEnabled
= aIsPrintSelectionRBEnabled
;
575 NS_IMETHODIMP
nsPrintSettings::GetPrintSelectionOnly(bool* aResult
) {
576 *aResult
= mPrintSelectionOnly
;
580 NS_IMETHODIMP
nsPrintSettings::SetPrintSelectionOnly(bool aSelectionOnly
) {
581 mPrintSelectionOnly
= aSelectionOnly
;
585 NS_IMETHODIMP
nsPrintSettings::GetPaperId(nsAString
& aPaperId
) {
589 NS_IMETHODIMP
nsPrintSettings::SetPaperId(const nsAString
& aPaperId
) {
594 NS_IMETHODIMP
nsPrintSettings::GetIsCancelled(bool* aIsCancelled
) {
595 NS_ENSURE_ARG_POINTER(aIsCancelled
);
596 *aIsCancelled
= mIsCancelled
;
599 NS_IMETHODIMP
nsPrintSettings::SetIsCancelled(bool aIsCancelled
) {
600 mIsCancelled
= aIsCancelled
;
604 NS_IMETHODIMP
nsPrintSettings::GetSaveOnCancel(bool* aSaveOnCancel
) {
605 *aSaveOnCancel
= mSaveOnCancel
;
609 NS_IMETHODIMP
nsPrintSettings::GetPaperWidth(double* aPaperWidth
) {
610 NS_ENSURE_ARG_POINTER(aPaperWidth
);
611 *aPaperWidth
= mPaperWidth
;
614 NS_IMETHODIMP
nsPrintSettings::SetPaperWidth(double aPaperWidth
) {
615 mPaperWidth
= aPaperWidth
;
619 NS_IMETHODIMP
nsPrintSettings::GetPaperHeight(double* aPaperHeight
) {
620 NS_ENSURE_ARG_POINTER(aPaperHeight
);
621 *aPaperHeight
= mPaperHeight
;
624 NS_IMETHODIMP
nsPrintSettings::SetPaperHeight(double aPaperHeight
) {
625 mPaperHeight
= aPaperHeight
;
629 NS_IMETHODIMP
nsPrintSettings::GetPaperSizeUnit(int16_t* aPaperSizeUnit
) {
630 NS_ENSURE_ARG_POINTER(aPaperSizeUnit
);
631 *aPaperSizeUnit
= mPaperSizeUnit
;
634 NS_IMETHODIMP
nsPrintSettings::SetPaperSizeUnit(int16_t aPaperSizeUnit
) {
635 mPaperSizeUnit
= aPaperSizeUnit
;
639 /** ---------------------------------------------------
640 * See documentation in nsPrintSettingsService.h
641 * @update 6/21/00 dwc
642 * @update 1/12/01 rods
645 nsPrintSettings::SetMarginInTwips(nsIntMargin
& aMargin
) {
651 nsPrintSettings::SetEdgeInTwips(nsIntMargin
& aEdge
) {
656 // NOTE: Any subclass implementation of this function should make sure
657 // to check for negative margin values in aUnwriteableMargin (which
658 // would indicate that we should use the system default unwriteable margin.)
660 nsPrintSettings::SetUnwriteableMarginInTwips(nsIntMargin
& aUnwriteableMargin
) {
661 if (aUnwriteableMargin
.top
>= 0) {
662 mUnwriteableMargin
.top
= aUnwriteableMargin
.top
;
664 if (aUnwriteableMargin
.left
>= 0) {
665 mUnwriteableMargin
.left
= aUnwriteableMargin
.left
;
667 if (aUnwriteableMargin
.bottom
>= 0) {
668 mUnwriteableMargin
.bottom
= aUnwriteableMargin
.bottom
;
670 if (aUnwriteableMargin
.right
>= 0) {
671 mUnwriteableMargin
.right
= aUnwriteableMargin
.right
;
676 nsIntMargin
nsPrintSettings::GetMarginInTwips() { return mMargin
; }
678 nsIntMargin
nsPrintSettings::GetEdgeInTwips() { return mEdge
; }
680 nsIntMargin
nsPrintSettings::GetUnwriteableMarginInTwips() {
681 return mUnwriteableMargin
;
684 /** ---------------------------------------------------
685 * See documentation in nsPrintSettingsService.h
688 nsPrintSettings::GetEffectivePageSize(double* aWidth
, double* aHeight
) {
689 if (mPaperSizeUnit
== kPaperSizeInches
) {
690 *aWidth
= NS_INCHES_TO_TWIPS(float(mPaperWidth
));
691 *aHeight
= NS_INCHES_TO_TWIPS(float(mPaperHeight
));
693 MOZ_ASSERT(mPaperSizeUnit
== kPaperSizeMillimeters
,
694 "unexpected paper size unit");
695 *aWidth
= NS_MILLIMETERS_TO_TWIPS(float(mPaperWidth
));
696 *aHeight
= NS_MILLIMETERS_TO_TWIPS(float(mPaperHeight
));
698 if (kLandscapeOrientation
== mOrientation
) {
699 double temp
= *aWidth
;
706 bool nsPrintSettings::HasOrthogonalSheetsAndPages() {
707 return mNumPagesPerSheet
== 2 || mNumPagesPerSheet
== 6;
710 void nsPrintSettings::GetEffectiveSheetSize(double* aWidth
, double* aHeight
) {
711 mozilla::DebugOnly
<nsresult
> rv
= GetEffectivePageSize(aWidth
, aHeight
);
713 // Our GetEffectivePageSize impls only return NS_OK, so this should hold:
714 MOZ_ASSERT(NS_SUCCEEDED(rv
), "Uh oh, GetEffectivePageSize failed");
716 if (HasOrthogonalSheetsAndPages()) {
717 std::swap(*aWidth
, *aHeight
);
721 int32_t nsPrintSettings::GetSheetOrientation() {
722 if (HasOrthogonalSheetsAndPages()) {
723 // Sheet orientation is rotated with respect to the page orientation.
724 return kLandscapeOrientation
== mOrientation
? kPortraitOrientation
725 : kLandscapeOrientation
;
728 // Sheet orientation is the same as the page orientation.
733 nsPrintSettings::SetPageRanges(const nsTArray
<int32_t>& aPages
) {
734 // Needs to be a set of (start, end) pairs.
735 if (aPages
.Length() % 2 != 0) {
736 return NS_ERROR_FAILURE
;
738 mPageRanges
= aPages
.Clone();
743 nsPrintSettings::GetPageRanges(nsTArray
<int32_t>& aPages
) {
744 aPages
= mPageRanges
.Clone();
748 bool nsIPrintSettings::IsPageSkipped(int32_t aPageNum
,
749 const nsTArray
<int32_t>& aRanges
) {
750 MOZ_RELEASE_ASSERT(aRanges
.Length() % 2 == 0);
751 if (aRanges
.IsEmpty()) {
754 for (size_t i
= 0; i
< aRanges
.Length(); i
+= 2) {
755 if (aRanges
[i
] <= aPageNum
&& aPageNum
<= aRanges
[i
+ 1]) {
756 // The page is included in this piece of the custom range,
757 // so it's not skipped.
764 nsresult
nsPrintSettings::_Clone(nsIPrintSettings
** _retval
) {
765 RefPtr
<nsPrintSettings
> printSettings
= new nsPrintSettings(*this);
766 printSettings
.forget(_retval
);
771 nsPrintSettings::Clone(nsIPrintSettings
** _retval
) {
772 NS_ENSURE_ARG_POINTER(_retval
);
773 return _Clone(_retval
);
776 nsresult
nsPrintSettings::_Assign(nsIPrintSettings
* aPS
) {
777 nsPrintSettings
* ps
= static_cast<nsPrintSettings
*>(aPS
);
782 nsresult
nsPrintSettings::EquivalentTo(nsIPrintSettings
* aPrintSettings
,
784 MOZ_ASSERT(aPrintSettings
);
786 auto* other
= static_cast<nsPrintSettings
*>(aPrintSettings
);
787 if (GetMarginInTwips() != aPrintSettings
->GetMarginInTwips()) {
790 if (GetEdgeInTwips() != aPrintSettings
->GetEdgeInTwips()) {
793 if (GetUnwriteableMarginInTwips() !=
794 aPrintSettings
->GetUnwriteableMarginInTwips()) {
797 nsTArray
<int32_t> ourPageRanges
, otherPageRanges
;
798 if (NS_FAILED(GetPageRanges(ourPageRanges
)) ||
799 NS_FAILED(aPrintSettings
->GetPageRanges(otherPageRanges
)) ||
800 ourPageRanges
!= otherPageRanges
) {
803 double ourScaling
, otherScaling
;
804 if (NS_FAILED(GetScaling(&ourScaling
)) ||
805 NS_FAILED(aPrintSettings
->GetScaling(&otherScaling
)) ||
806 ourScaling
!= otherScaling
) {
809 if (GetPrintBGColors() != aPrintSettings
->GetPrintBGColors()) {
812 if (GetPrintBGImages() != aPrintSettings
->GetPrintBGImages()) {
815 if (GetPrintSelectionOnly() != aPrintSettings
->GetPrintSelectionOnly()) {
818 if (GetShrinkToFit() != aPrintSettings
->GetShrinkToFit()) {
821 if (GetShowMarginGuides() != aPrintSettings
->GetShowMarginGuides()) {
824 if (GetHonorPageRuleMargins() != aPrintSettings
->GetHonorPageRuleMargins()) {
827 nsAutoString ourTitle
, otherTitle
;
828 if (NS_FAILED(GetTitle(ourTitle
)) ||
829 NS_FAILED(aPrintSettings
->GetTitle(otherTitle
)) ||
830 ourTitle
!= otherTitle
) {
833 nsAutoString ourUrl
, otherUrl
;
834 if (NS_FAILED(GetDocURL(ourUrl
)) ||
835 NS_FAILED(aPrintSettings
->GetDocURL(otherUrl
)) || ourUrl
!= otherUrl
) {
838 if (!mozilla::ArrayEqual(mHeaderStrs
, other
->mHeaderStrs
) ||
839 !mozilla::ArrayEqual(mFooterStrs
, other
->mFooterStrs
)) {
842 nsAutoString ourPaperId
, otherPaperId
;
843 if (NS_FAILED(GetPaperId(ourPaperId
)) ||
844 NS_FAILED(aPrintSettings
->GetPaperId(otherPaperId
)) ||
845 ourPaperId
!= otherPaperId
) {
848 double ourWidth
, ourHeight
, otherWidth
, otherHeight
;
849 if (NS_FAILED(GetEffectivePageSize(&ourWidth
, &ourHeight
)) ||
850 NS_FAILED(other
->GetEffectivePageSize(&otherWidth
, &otherHeight
)) ||
851 std::abs(ourWidth
- otherWidth
) >= 1 ||
852 std::abs(ourHeight
- otherHeight
) >= 1) {
855 int32_t ourOrientation
, otherOrientation
;
856 if (NS_FAILED(GetOrientation(&ourOrientation
)) ||
857 NS_FAILED(aPrintSettings
->GetOrientation(&otherOrientation
)) ||
858 ourOrientation
!= otherOrientation
) {
861 int32_t ourResolution
, otherResolution
;
862 if (NS_FAILED(GetResolution(&ourResolution
)) ||
863 NS_FAILED(aPrintSettings
->GetResolution(&otherResolution
)) ||
864 ourResolution
!= otherResolution
) {
867 int32_t ourNumPagesPerSheet
, otherNumPagesPerSheet
;
868 if (NS_FAILED(GetNumPagesPerSheet(&ourNumPagesPerSheet
)) ||
869 NS_FAILED(aPrintSettings
->GetNumPagesPerSheet(&otherNumPagesPerSheet
)) ||
870 ourNumPagesPerSheet
!= otherNumPagesPerSheet
) {
878 mozilla::PrintSettingsInitializer
nsPrintSettings::GetSettingsInitializer() {
879 mozilla::PrintSettingsInitializer settingsInitializer
;
880 settingsInitializer
.mPrinter
.Assign(mPrinter
);
881 settingsInitializer
.mPaperInfo
.mId
= mPaperId
;
883 double pointsPerSizeUnit
=
884 mPaperSizeUnit
== kPaperSizeInches
? 72.0 : 72.0 / 25.4;
885 settingsInitializer
.mPaperInfo
.mSize
= {mPaperWidth
* pointsPerSizeUnit
,
886 mPaperHeight
* pointsPerSizeUnit
};
888 // Unwritable margins are stored in TWIPS here and points in PaperInfo.
889 settingsInitializer
.mPaperInfo
.mUnwriteableMargin
=
890 Some(mozilla::gfx::MarginDouble(
891 mUnwriteableMargin
.top
/ 20.0, mUnwriteableMargin
.right
/ 20.0,
892 mUnwriteableMargin
.bottom
/ 20.0, mUnwriteableMargin
.left
/ 20.0));
894 settingsInitializer
.mPrintInColor
= mPrintInColor
;
895 settingsInitializer
.mResolution
= mResolution
;
896 settingsInitializer
.mSheetOrientation
= GetSheetOrientation();
897 settingsInitializer
.mNumCopies
= mNumCopies
;
898 settingsInitializer
.mDuplex
= mDuplex
;
899 RefPtr
<nsIPrintSettings
> settingsToInitialize
;
900 MOZ_ALWAYS_SUCCEEDS(Clone(getter_AddRefs(settingsToInitialize
)));
901 settingsInitializer
.mPrintSettings
=
902 new nsMainThreadPtrHolder
<nsPrintSettings
>(
903 "PrintSettingsInitializer::mPrintSettings",
904 settingsToInitialize
.forget().downcast
<nsPrintSettings
>());
905 return settingsInitializer
;
909 nsPrintSettings::Assign(nsIPrintSettings
* aPS
) {
914 //-------------------------------------------
915 nsPrintSettings
& nsPrintSettings::operator=(const nsPrintSettings
& rhs
) {
920 mPageRanges
= rhs
.mPageRanges
.Clone();
921 mMargin
= rhs
.mMargin
;
923 mUnwriteableMargin
= rhs
.mUnwriteableMargin
;
924 mScaling
= rhs
.mScaling
;
925 mPrintBGColors
= rhs
.mPrintBGColors
;
926 mPrintBGImages
= rhs
.mPrintBGImages
;
929 mIsCancelled
= rhs
.mIsCancelled
;
930 mSaveOnCancel
= rhs
.mSaveOnCancel
;
931 mPrintSilent
= rhs
.mPrintSilent
;
932 mShrinkToFit
= rhs
.mShrinkToFit
;
933 mShowMarginGuides
= rhs
.mShowMarginGuides
;
934 mHonorPageRuleMargins
= rhs
.mHonorPageRuleMargins
;
935 mIsPrintSelectionRBEnabled
= rhs
.mIsPrintSelectionRBEnabled
;
936 mPrintSelectionOnly
= rhs
.mPrintSelectionOnly
;
937 mPaperId
= rhs
.mPaperId
;
938 mPaperWidth
= rhs
.mPaperWidth
;
939 mPaperHeight
= rhs
.mPaperHeight
;
940 mPaperSizeUnit
= rhs
.mPaperSizeUnit
;
941 mPrintReversed
= rhs
.mPrintReversed
;
942 mPrintInColor
= rhs
.mPrintInColor
;
943 mOrientation
= rhs
.mOrientation
;
944 mResolution
= rhs
.mResolution
;
945 mDuplex
= rhs
.mDuplex
;
946 mNumCopies
= rhs
.mNumCopies
;
947 mNumPagesPerSheet
= rhs
.mNumPagesPerSheet
;
948 mPrinter
= rhs
.mPrinter
;
949 mPrintToFile
= rhs
.mPrintToFile
;
950 mToFileName
= rhs
.mToFileName
;
951 mOutputFormat
= rhs
.mOutputFormat
;
952 mIsInitedFromPrinter
= rhs
.mIsInitedFromPrinter
;
953 mIsInitedFromPrefs
= rhs
.mIsInitedFromPrefs
;
954 mPrintPageDelay
= rhs
.mPrintPageDelay
;
956 for (int32_t i
= 0; i
< NUM_HEAD_FOOT
; i
++) {
957 mHeaderStrs
[i
] = rhs
.mHeaderStrs
[i
];
958 mFooterStrs
[i
] = rhs
.mFooterStrs
[i
];
964 void nsPrintSettings::SetDefaultFileName() {
965 nsAutoString filename
;
966 nsresult rv
= GetToFileName(filename
);
967 if (NS_FAILED(rv
) || filename
.IsEmpty()) {
968 const char* path
= PR_GetEnv("PWD");
970 path
= PR_GetEnv("HOME");
974 CopyUTF8toUTF16(mozilla::MakeStringSpan(path
), filename
);
975 filename
.AppendLiteral("/mozilla.pdf");
977 filename
.AssignLiteral("mozilla.pdf");
980 SetToFileName(filename
);