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 "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
61 const bool areSheetsOfPaperPortraitMode
=
62 (aSettings
.mSheetOrientation
== kPortraitOrientation
);
63 const bool arePagesPortraitMode
=
64 (areSheetsOfPaperPortraitMode
!= HasOrthogonalSheetsAndPages());
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
;
89 NS_IMETHODIMP
nsPrintSettings::SetPrintReversed(bool aPrintReversed
) {
90 mPrintReversed
= aPrintReversed
;
94 NS_IMETHODIMP
nsPrintSettings::GetPrintInColor(bool* aPrintInColor
) {
95 *aPrintInColor
= mPrintInColor
;
98 NS_IMETHODIMP
nsPrintSettings::SetPrintInColor(bool aPrintInColor
) {
99 mPrintInColor
= aPrintInColor
;
103 NS_IMETHODIMP
nsPrintSettings::GetOrientation(int32_t* aOrientation
) {
104 *aOrientation
= mOrientation
;
107 NS_IMETHODIMP
nsPrintSettings::SetOrientation(int32_t aOrientation
) {
108 mOrientation
= aOrientation
;
112 NS_IMETHODIMP
nsPrintSettings::GetResolution(int32_t* aResolution
) {
113 NS_ENSURE_ARG_POINTER(aResolution
);
114 *aResolution
= mResolution
;
117 NS_IMETHODIMP
nsPrintSettings::SetResolution(const int32_t aResolution
) {
118 mResolution
= aResolution
;
122 NS_IMETHODIMP
nsPrintSettings::GetDuplex(int32_t* aDuplex
) {
123 NS_ENSURE_ARG_POINTER(aDuplex
);
127 NS_IMETHODIMP
nsPrintSettings::SetDuplex(const int32_t aDuplex
) {
132 NS_IMETHODIMP
nsPrintSettings::GetPrinterName(nsAString
& aPrinter
) {
137 NS_IMETHODIMP
nsPrintSettings::SetPrinterName(const nsAString
& aPrinter
) {
138 if (!mPrinter
.Equals(aPrinter
)) {
139 mIsInitedFromPrinter
= false;
140 mIsInitedFromPrefs
= false;
143 mPrinter
.Assign(aPrinter
);
147 NS_IMETHODIMP
nsPrintSettings::GetNumCopies(int32_t* aNumCopies
) {
148 NS_ENSURE_ARG_POINTER(aNumCopies
);
149 *aNumCopies
= mNumCopies
;
152 NS_IMETHODIMP
nsPrintSettings::SetNumCopies(int32_t aNumCopies
) {
153 mNumCopies
= aNumCopies
;
157 NS_IMETHODIMP
nsPrintSettings::GetNumPagesPerSheet(int32_t* aNumPagesPerSheet
) {
158 NS_ENSURE_ARG_POINTER(aNumPagesPerSheet
);
159 *aNumPagesPerSheet
= mNumPagesPerSheet
;
162 NS_IMETHODIMP
nsPrintSettings::SetNumPagesPerSheet(int32_t aNumPagesPerSheet
) {
163 mNumPagesPerSheet
= aNumPagesPerSheet
;
167 NS_IMETHODIMP
nsPrintSettings::GetOutputDestination(
168 OutputDestinationType
* aDestination
) {
169 *aDestination
= mOutputDestination
;
173 NS_IMETHODIMP
nsPrintSettings::SetOutputDestination(
174 OutputDestinationType aDestination
) {
175 mOutputDestination
= aDestination
;
179 NS_IMETHODIMP
nsPrintSettings::SetOutputStream(nsIOutputStream
* aStream
) {
180 mOutputStream
= aStream
;
184 NS_IMETHODIMP
nsPrintSettings::GetOutputStream(nsIOutputStream
** aStream
) {
185 NS_IF_ADDREF(*aStream
= mOutputStream
.get());
189 NS_IMETHODIMP
nsPrintSettings::GetToFileName(nsAString
& aToFileName
) {
190 aToFileName
= mToFileName
;
193 NS_IMETHODIMP
nsPrintSettings::SetToFileName(const nsAString
& aToFileName
) {
194 mToFileName
= aToFileName
;
198 NS_IMETHODIMP
nsPrintSettings::GetOutputFormat(int16_t* aOutputFormat
) {
199 NS_ENSURE_ARG_POINTER(aOutputFormat
);
200 *aOutputFormat
= mOutputFormat
;
203 NS_IMETHODIMP
nsPrintSettings::SetOutputFormat(int16_t aOutputFormat
) {
204 mOutputFormat
= aOutputFormat
;
208 NS_IMETHODIMP
nsPrintSettings::GetPrintPageDelay(int32_t* aPrintPageDelay
) {
209 *aPrintPageDelay
= mPrintPageDelay
;
212 NS_IMETHODIMP
nsPrintSettings::SetPrintPageDelay(int32_t aPrintPageDelay
) {
213 mPrintPageDelay
= aPrintPageDelay
;
217 NS_IMETHODIMP
nsPrintSettings::GetIsInitializedFromPrinter(
218 bool* aIsInitializedFromPrinter
) {
219 NS_ENSURE_ARG_POINTER(aIsInitializedFromPrinter
);
220 *aIsInitializedFromPrinter
= mIsInitedFromPrinter
;
223 NS_IMETHODIMP
nsPrintSettings::SetIsInitializedFromPrinter(
224 bool aIsInitializedFromPrinter
) {
225 mIsInitedFromPrinter
= aIsInitializedFromPrinter
;
229 NS_IMETHODIMP
nsPrintSettings::GetIsInitializedFromPrefs(
230 bool* aInitializedFromPrefs
) {
231 NS_ENSURE_ARG_POINTER(aInitializedFromPrefs
);
232 *aInitializedFromPrefs
= mIsInitedFromPrefs
;
235 NS_IMETHODIMP
nsPrintSettings::SetIsInitializedFromPrefs(
236 bool aInitializedFromPrefs
) {
237 mIsInitedFromPrefs
= aInitializedFromPrefs
;
241 NS_IMETHODIMP
nsPrintSettings::GetMarginTop(double* aMarginTop
) {
242 NS_ENSURE_ARG_POINTER(aMarginTop
);
243 *aMarginTop
= NS_TWIPS_TO_INCHES(mMargin
.top
);
246 NS_IMETHODIMP
nsPrintSettings::SetMarginTop(double aMarginTop
) {
247 mMargin
.top
= NS_INCHES_TO_INT_TWIPS(float(aMarginTop
));
251 NS_IMETHODIMP
nsPrintSettings::GetMarginLeft(double* aMarginLeft
) {
252 NS_ENSURE_ARG_POINTER(aMarginLeft
);
253 *aMarginLeft
= NS_TWIPS_TO_INCHES(mMargin
.left
);
256 NS_IMETHODIMP
nsPrintSettings::SetMarginLeft(double aMarginLeft
) {
257 mMargin
.left
= NS_INCHES_TO_INT_TWIPS(float(aMarginLeft
));
261 NS_IMETHODIMP
nsPrintSettings::GetMarginBottom(double* aMarginBottom
) {
262 NS_ENSURE_ARG_POINTER(aMarginBottom
);
263 *aMarginBottom
= NS_TWIPS_TO_INCHES(mMargin
.bottom
);
266 NS_IMETHODIMP
nsPrintSettings::SetMarginBottom(double aMarginBottom
) {
267 mMargin
.bottom
= NS_INCHES_TO_INT_TWIPS(float(aMarginBottom
));
271 NS_IMETHODIMP
nsPrintSettings::GetMarginRight(double* aMarginRight
) {
272 NS_ENSURE_ARG_POINTER(aMarginRight
);
273 *aMarginRight
= NS_TWIPS_TO_INCHES(mMargin
.right
);
276 NS_IMETHODIMP
nsPrintSettings::SetMarginRight(double aMarginRight
) {
277 mMargin
.right
= NS_INCHES_TO_INT_TWIPS(float(aMarginRight
));
281 NS_IMETHODIMP
nsPrintSettings::GetEdgeTop(double* aEdgeTop
) {
282 NS_ENSURE_ARG_POINTER(aEdgeTop
);
283 *aEdgeTop
= NS_TWIPS_TO_INCHES(mEdge
.top
);
286 NS_IMETHODIMP
nsPrintSettings::SetEdgeTop(double aEdgeTop
) {
287 mEdge
.top
= NS_INCHES_TO_INT_TWIPS(float(aEdgeTop
));
291 NS_IMETHODIMP
nsPrintSettings::GetEdgeLeft(double* aEdgeLeft
) {
292 NS_ENSURE_ARG_POINTER(aEdgeLeft
);
293 *aEdgeLeft
= NS_TWIPS_TO_INCHES(mEdge
.left
);
296 NS_IMETHODIMP
nsPrintSettings::SetEdgeLeft(double aEdgeLeft
) {
297 mEdge
.left
= NS_INCHES_TO_INT_TWIPS(float(aEdgeLeft
));
301 NS_IMETHODIMP
nsPrintSettings::GetEdgeBottom(double* aEdgeBottom
) {
302 NS_ENSURE_ARG_POINTER(aEdgeBottom
);
303 *aEdgeBottom
= NS_TWIPS_TO_INCHES(mEdge
.bottom
);
306 NS_IMETHODIMP
nsPrintSettings::SetEdgeBottom(double aEdgeBottom
) {
307 mEdge
.bottom
= NS_INCHES_TO_INT_TWIPS(float(aEdgeBottom
));
311 NS_IMETHODIMP
nsPrintSettings::GetEdgeRight(double* aEdgeRight
) {
312 NS_ENSURE_ARG_POINTER(aEdgeRight
);
313 *aEdgeRight
= NS_TWIPS_TO_INCHES(mEdge
.right
);
316 NS_IMETHODIMP
nsPrintSettings::SetEdgeRight(double aEdgeRight
) {
317 mEdge
.right
= NS_INCHES_TO_INT_TWIPS(float(aEdgeRight
));
321 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginTop(
322 double* aUnwriteableMarginTop
) {
323 NS_ENSURE_ARG_POINTER(aUnwriteableMarginTop
);
324 *aUnwriteableMarginTop
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.top
);
327 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginTop(
328 double aUnwriteableMarginTop
) {
329 if (aUnwriteableMarginTop
>= 0.0) {
330 mUnwriteableMargin
.top
= NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginTop
);
335 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginLeft(
336 double* aUnwriteableMarginLeft
) {
337 NS_ENSURE_ARG_POINTER(aUnwriteableMarginLeft
);
338 *aUnwriteableMarginLeft
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.left
);
341 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginLeft(
342 double aUnwriteableMarginLeft
) {
343 if (aUnwriteableMarginLeft
>= 0.0) {
344 mUnwriteableMargin
.left
= NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginLeft
);
349 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginBottom(
350 double* aUnwriteableMarginBottom
) {
351 NS_ENSURE_ARG_POINTER(aUnwriteableMarginBottom
);
352 *aUnwriteableMarginBottom
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.bottom
);
355 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginBottom(
356 double aUnwriteableMarginBottom
) {
357 if (aUnwriteableMarginBottom
>= 0.0) {
358 mUnwriteableMargin
.bottom
=
359 NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginBottom
);
364 NS_IMETHODIMP
nsPrintSettings::GetUnwriteableMarginRight(
365 double* aUnwriteableMarginRight
) {
366 NS_ENSURE_ARG_POINTER(aUnwriteableMarginRight
);
367 *aUnwriteableMarginRight
= NS_TWIPS_TO_INCHES(mUnwriteableMargin
.right
);
370 NS_IMETHODIMP
nsPrintSettings::SetUnwriteableMarginRight(
371 double aUnwriteableMarginRight
) {
372 if (aUnwriteableMarginRight
>= 0.0) {
373 mUnwriteableMargin
.right
= NS_INCHES_TO_INT_TWIPS(aUnwriteableMarginRight
);
378 NS_IMETHODIMP
nsPrintSettings::GetScaling(double* aScaling
) {
379 NS_ENSURE_ARG_POINTER(aScaling
);
380 *aScaling
= mScaling
;
384 NS_IMETHODIMP
nsPrintSettings::SetScaling(double aScaling
) {
389 NS_IMETHODIMP
nsPrintSettings::GetPrintBGColors(bool* aPrintBGColors
) {
390 NS_ENSURE_ARG_POINTER(aPrintBGColors
);
391 *aPrintBGColors
= mPrintBGColors
;
394 NS_IMETHODIMP
nsPrintSettings::SetPrintBGColors(bool aPrintBGColors
) {
395 mPrintBGColors
= aPrintBGColors
;
399 NS_IMETHODIMP
nsPrintSettings::GetPrintBGImages(bool* aPrintBGImages
) {
400 NS_ENSURE_ARG_POINTER(aPrintBGImages
);
401 *aPrintBGImages
= mPrintBGImages
;
404 NS_IMETHODIMP
nsPrintSettings::SetPrintBGImages(bool aPrintBGImages
) {
405 mPrintBGImages
= aPrintBGImages
;
409 NS_IMETHODIMP
nsPrintSettings::GetTitle(nsAString
& aTitle
) {
413 NS_IMETHODIMP
nsPrintSettings::SetTitle(const nsAString
& aTitle
) {
418 NS_IMETHODIMP
nsPrintSettings::GetDocURL(nsAString
& aDocURL
) {
422 NS_IMETHODIMP
nsPrintSettings::SetDocURL(const nsAString
& aDocURL
) {
427 NS_IMETHODIMP
nsPrintSettings::GetHeaderStrLeft(nsAString
& aTitle
) {
428 aTitle
= mHeaderStrs
[0];
431 NS_IMETHODIMP
nsPrintSettings::SetHeaderStrLeft(const nsAString
& aTitle
) {
432 mHeaderStrs
[0] = aTitle
;
436 NS_IMETHODIMP
nsPrintSettings::GetHeaderStrCenter(nsAString
& aTitle
) {
437 aTitle
= mHeaderStrs
[1];
440 NS_IMETHODIMP
nsPrintSettings::SetHeaderStrCenter(const nsAString
& aTitle
) {
441 mHeaderStrs
[1] = aTitle
;
445 NS_IMETHODIMP
nsPrintSettings::GetHeaderStrRight(nsAString
& aTitle
) {
446 aTitle
= mHeaderStrs
[2];
449 NS_IMETHODIMP
nsPrintSettings::SetHeaderStrRight(const nsAString
& aTitle
) {
450 mHeaderStrs
[2] = aTitle
;
454 NS_IMETHODIMP
nsPrintSettings::GetFooterStrLeft(nsAString
& aTitle
) {
455 aTitle
= mFooterStrs
[0];
458 NS_IMETHODIMP
nsPrintSettings::SetFooterStrLeft(const nsAString
& aTitle
) {
459 mFooterStrs
[0] = aTitle
;
463 NS_IMETHODIMP
nsPrintSettings::GetFooterStrCenter(nsAString
& aTitle
) {
464 aTitle
= mFooterStrs
[1];
467 NS_IMETHODIMP
nsPrintSettings::SetFooterStrCenter(const nsAString
& aTitle
) {
468 mFooterStrs
[1] = aTitle
;
472 NS_IMETHODIMP
nsPrintSettings::GetFooterStrRight(nsAString
& aTitle
) {
473 aTitle
= mFooterStrs
[2];
476 NS_IMETHODIMP
nsPrintSettings::SetFooterStrRight(const nsAString
& aTitle
) {
477 mFooterStrs
[2] = aTitle
;
481 NS_IMETHODIMP
nsPrintSettings::GetPrintSilent(bool* aPrintSilent
) {
482 NS_ENSURE_ARG_POINTER(aPrintSilent
);
483 *aPrintSilent
= mPrintSilent
;
486 NS_IMETHODIMP
nsPrintSettings::SetPrintSilent(bool aPrintSilent
) {
487 mPrintSilent
= aPrintSilent
;
491 NS_IMETHODIMP
nsPrintSettings::GetShrinkToFit(bool* aShrinkToFit
) {
492 NS_ENSURE_ARG_POINTER(aShrinkToFit
);
493 *aShrinkToFit
= mShrinkToFit
;
496 NS_IMETHODIMP
nsPrintSettings::SetShrinkToFit(bool aShrinkToFit
) {
497 mShrinkToFit
= aShrinkToFit
;
501 NS_IMETHODIMP
nsPrintSettings::GetShowMarginGuides(bool* aShowMarginGuides
) {
502 *aShowMarginGuides
= mShowMarginGuides
;
506 NS_IMETHODIMP
nsPrintSettings::SetShowMarginGuides(bool aShowMarginGuides
) {
507 mShowMarginGuides
= aShowMarginGuides
;
511 NS_IMETHODIMP
nsPrintSettings::GetHonorPageRuleMargins(bool* aResult
) {
512 *aResult
= mHonorPageRuleMargins
;
516 NS_IMETHODIMP
nsPrintSettings::SetHonorPageRuleMargins(bool aHonor
) {
517 mHonorPageRuleMargins
= aHonor
;
521 NS_IMETHODIMP
nsPrintSettings::GetUsePageRuleSizeAsPaperSize(bool* aResult
) {
522 *aResult
= mUsePageRuleSizeAsPaperSize
;
526 NS_IMETHODIMP
nsPrintSettings::SetUsePageRuleSizeAsPaperSize(bool aHonor
) {
527 mUsePageRuleSizeAsPaperSize
= aHonor
;
531 NS_IMETHODIMP
nsPrintSettings::GetIgnoreUnwriteableMargins(bool* aResult
) {
532 *aResult
= mIgnoreUnwriteableMargins
;
536 NS_IMETHODIMP
nsPrintSettings::SetIgnoreUnwriteableMargins(bool aIgnore
) {
537 mIgnoreUnwriteableMargins
= aIgnore
;
541 NS_IMETHODIMP
nsPrintSettings::GetPrintSelectionOnly(bool* aResult
) {
542 *aResult
= mPrintSelectionOnly
;
546 NS_IMETHODIMP
nsPrintSettings::SetPrintSelectionOnly(bool aSelectionOnly
) {
547 mPrintSelectionOnly
= aSelectionOnly
;
551 NS_IMETHODIMP
nsPrintSettings::GetPaperId(nsAString
& aPaperId
) {
555 NS_IMETHODIMP
nsPrintSettings::SetPaperId(const nsAString
& aPaperId
) {
560 NS_IMETHODIMP
nsPrintSettings::GetPaperWidth(double* aPaperWidth
) {
561 NS_ENSURE_ARG_POINTER(aPaperWidth
);
562 *aPaperWidth
= mPaperWidth
;
565 NS_IMETHODIMP
nsPrintSettings::SetPaperWidth(double aPaperWidth
) {
566 mPaperWidth
= aPaperWidth
;
570 NS_IMETHODIMP
nsPrintSettings::GetPaperHeight(double* aPaperHeight
) {
571 NS_ENSURE_ARG_POINTER(aPaperHeight
);
572 *aPaperHeight
= mPaperHeight
;
575 NS_IMETHODIMP
nsPrintSettings::SetPaperHeight(double aPaperHeight
) {
576 mPaperHeight
= aPaperHeight
;
580 NS_IMETHODIMP
nsPrintSettings::GetPaperSizeUnit(int16_t* aPaperSizeUnit
) {
581 NS_ENSURE_ARG_POINTER(aPaperSizeUnit
);
582 *aPaperSizeUnit
= mPaperSizeUnit
;
585 NS_IMETHODIMP
nsPrintSettings::SetPaperSizeUnit(int16_t aPaperSizeUnit
) {
586 mPaperSizeUnit
= aPaperSizeUnit
;
590 /** ---------------------------------------------------
591 * See documentation in nsPrintSettingsService.h
592 * @update 6/21/00 dwc
593 * @update 1/12/01 rods
596 nsPrintSettings::SetMarginInTwips(nsIntMargin
& aMargin
) {
602 nsPrintSettings::SetEdgeInTwips(nsIntMargin
& aEdge
) {
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.)
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
;
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
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
));
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
;
657 bool nsPrintSettings::HasOrthogonalSheetsAndPages() {
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 (HasOrthogonalSheetsAndPages()) {
668 std::swap(*aWidth
, *aHeight
);
672 int32_t nsPrintSettings::GetSheetOrientation() {
673 if (HasOrthogonalSheetsAndPages()) {
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.
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();
694 nsPrintSettings::GetPageRanges(nsTArray
<int32_t>& aPages
) {
695 aPages
= mPageRanges
.Clone();
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()) {
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.
715 nsresult
nsPrintSettings::_Clone(nsIPrintSettings
** _retval
) {
716 RefPtr
<nsPrintSettings
> printSettings
= new nsPrintSettings(*this);
717 printSettings
.forget(_retval
);
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
);
733 nsresult
nsPrintSettings::EquivalentTo(nsIPrintSettings
* aPrintSettings
,
735 MOZ_ASSERT(aPrintSettings
);
737 auto* other
= static_cast<nsPrintSettings
*>(aPrintSettings
);
738 if (GetMarginInTwips() != aPrintSettings
->GetMarginInTwips()) {
741 if (GetEdgeInTwips() != aPrintSettings
->GetEdgeInTwips()) {
744 if (GetUnwriteableMarginInTwips() !=
745 aPrintSettings
->GetUnwriteableMarginInTwips()) {
748 nsTArray
<int32_t> ourPageRanges
, otherPageRanges
;
749 if (NS_FAILED(GetPageRanges(ourPageRanges
)) ||
750 NS_FAILED(aPrintSettings
->GetPageRanges(otherPageRanges
)) ||
751 ourPageRanges
!= otherPageRanges
) {
754 double ourScaling
, otherScaling
;
755 if (NS_FAILED(GetScaling(&ourScaling
)) ||
756 NS_FAILED(aPrintSettings
->GetScaling(&otherScaling
)) ||
757 ourScaling
!= otherScaling
) {
760 if (GetPrintBGColors() != aPrintSettings
->GetPrintBGColors()) {
763 if (GetPrintBGImages() != aPrintSettings
->GetPrintBGImages()) {
766 if (GetPrintSelectionOnly() != aPrintSettings
->GetPrintSelectionOnly()) {
769 if (GetShrinkToFit() != aPrintSettings
->GetShrinkToFit()) {
772 if (GetShowMarginGuides() != aPrintSettings
->GetShowMarginGuides()) {
775 if (GetHonorPageRuleMargins() != aPrintSettings
->GetHonorPageRuleMargins()) {
778 if (GetUsePageRuleSizeAsPaperSize() !=
779 aPrintSettings
->GetUsePageRuleSizeAsPaperSize()) {
782 if (GetIgnoreUnwriteableMargins() !=
783 aPrintSettings
->GetIgnoreUnwriteableMargins()) {
786 nsAutoString ourTitle
, otherTitle
;
787 if (NS_FAILED(GetTitle(ourTitle
)) ||
788 NS_FAILED(aPrintSettings
->GetTitle(otherTitle
)) ||
789 ourTitle
!= otherTitle
) {
792 nsAutoString ourUrl
, otherUrl
;
793 if (NS_FAILED(GetDocURL(ourUrl
)) ||
794 NS_FAILED(aPrintSettings
->GetDocURL(otherUrl
)) || ourUrl
!= otherUrl
) {
797 if (!mozilla::ArrayEqual(mHeaderStrs
, other
->mHeaderStrs
) ||
798 !mozilla::ArrayEqual(mFooterStrs
, other
->mFooterStrs
)) {
801 nsAutoString ourPaperId
, otherPaperId
;
802 if (NS_FAILED(GetPaperId(ourPaperId
)) ||
803 NS_FAILED(aPrintSettings
->GetPaperId(otherPaperId
)) ||
804 ourPaperId
!= otherPaperId
) {
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) {
814 int32_t ourOrientation
, otherOrientation
;
815 if (NS_FAILED(GetOrientation(&ourOrientation
)) ||
816 NS_FAILED(aPrintSettings
->GetOrientation(&otherOrientation
)) ||
817 ourOrientation
!= otherOrientation
) {
820 int32_t ourResolution
, otherResolution
;
821 if (NS_FAILED(GetResolution(&ourResolution
)) ||
822 NS_FAILED(aPrintSettings
->GetResolution(&otherResolution
)) ||
823 ourResolution
!= otherResolution
) {
826 int32_t ourNumPagesPerSheet
, otherNumPagesPerSheet
;
827 if (NS_FAILED(GetNumPagesPerSheet(&ourNumPagesPerSheet
)) ||
828 NS_FAILED(aPrintSettings
->GetNumPagesPerSheet(&otherNumPagesPerSheet
)) ||
829 ourNumPagesPerSheet
!= otherNumPagesPerSheet
) {
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
;
868 nsPrintSettings::Assign(nsIPrintSettings
* aPS
) {
873 //-------------------------------------------
874 nsPrintSettings
& nsPrintSettings::operator=(const nsPrintSettings
& rhs
) {
879 mPageRanges
= rhs
.mPageRanges
.Clone();
880 mMargin
= rhs
.mMargin
;
882 mUnwriteableMargin
= rhs
.mUnwriteableMargin
;
883 mScaling
= rhs
.mScaling
;
884 mPrintBGColors
= rhs
.mPrintBGColors
;
885 mPrintBGImages
= rhs
.mPrintBGImages
;
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
];
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");
929 path
= PR_GetEnv("HOME");
933 CopyUTF8toUTF16(mozilla::MakeStringSpan(path
), filename
);
934 filename
.AppendLiteral("/mozilla.pdf");
936 filename
.AssignLiteral("mozilla.pdf");
939 SetToFileName(filename
);