|
1 /* -*- Mode: C++; tab-width: 2; 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 "nsPrintData.h" |
|
7 |
|
8 #include "nsIStringBundle.h" |
|
9 #include "nsIServiceManager.h" |
|
10 #include "nsPrintObject.h" |
|
11 #include "nsPrintPreviewListener.h" |
|
12 #include "nsIWebProgressListener.h" |
|
13 #include "mozilla/Services.h" |
|
14 |
|
15 //----------------------------------------------------- |
|
16 // PR LOGGING |
|
17 #ifdef MOZ_LOGGING |
|
18 #define FORCE_PR_LOG /* Allow logging in the release build */ |
|
19 #endif |
|
20 |
|
21 #include "prlog.h" |
|
22 |
|
23 #ifdef PR_LOGGING |
|
24 #define DUMP_LAYOUT_LEVEL 9 // this turns on the dumping of each doucment's layout info |
|
25 static PRLogModuleInfo * |
|
26 GetPrintingLog() |
|
27 { |
|
28 static PRLogModuleInfo *sLog; |
|
29 if (!sLog) |
|
30 sLog = PR_NewLogModule("printing"); |
|
31 return sLog; |
|
32 } |
|
33 #define PR_PL(_p1) PR_LOG(GetPrintingLog(), PR_LOG_DEBUG, _p1); |
|
34 #else |
|
35 #define PRT_YESNO(_p) |
|
36 #define PR_PL(_p1) |
|
37 #endif |
|
38 |
|
39 //--------------------------------------------------- |
|
40 //-- nsPrintData Class Impl |
|
41 //--------------------------------------------------- |
|
42 nsPrintData::nsPrintData(ePrintDataType aType) : |
|
43 mType(aType), mDebugFilePtr(nullptr), mPrintObject(nullptr), mSelectedPO(nullptr), |
|
44 mPrintDocList(0), mIsIFrameSelected(false), |
|
45 mIsParentAFrameSet(false), mOnStartSent(false), |
|
46 mIsAborted(false), mPreparingForPrint(false), mDocWasToBeDestroyed(false), |
|
47 mShrinkToFit(false), mPrintFrameType(nsIPrintSettings::kFramesAsIs), |
|
48 mNumPrintablePages(0), mNumPagesPrinted(0), |
|
49 mShrinkRatio(1.0), mOrigDCScale(1.0), mPPEventListeners(nullptr), |
|
50 mBrandName(nullptr) |
|
51 { |
|
52 MOZ_COUNT_CTOR(nsPrintData); |
|
53 nsCOMPtr<nsIStringBundle> brandBundle; |
|
54 nsCOMPtr<nsIStringBundleService> svc = |
|
55 mozilla::services::GetStringBundleService(); |
|
56 if (svc) { |
|
57 svc->CreateBundle( "chrome://branding/locale/brand.properties", getter_AddRefs( brandBundle ) ); |
|
58 if (brandBundle) { |
|
59 brandBundle->GetStringFromName(MOZ_UTF16("brandShortName"), &mBrandName ); |
|
60 } |
|
61 } |
|
62 |
|
63 if (!mBrandName) { |
|
64 mBrandName = ToNewUnicode(NS_LITERAL_STRING("Mozilla Document")); |
|
65 } |
|
66 |
|
67 } |
|
68 |
|
69 nsPrintData::~nsPrintData() |
|
70 { |
|
71 MOZ_COUNT_DTOR(nsPrintData); |
|
72 // remove the event listeners |
|
73 if (mPPEventListeners) { |
|
74 mPPEventListeners->RemoveListeners(); |
|
75 NS_RELEASE(mPPEventListeners); |
|
76 } |
|
77 |
|
78 // Only Send an OnEndPrinting if we have started printing |
|
79 if (mOnStartSent && mType != eIsPrintPreview) { |
|
80 OnEndPrinting(); |
|
81 } |
|
82 |
|
83 if (mPrintDC && !mDebugFilePtr) { |
|
84 PR_PL(("****************** End Document ************************\n")); |
|
85 PR_PL(("\n")); |
|
86 bool isCancelled = false; |
|
87 mPrintSettings->GetIsCancelled(&isCancelled); |
|
88 |
|
89 nsresult rv = NS_OK; |
|
90 if (mType == eIsPrinting) { |
|
91 if (!isCancelled && !mIsAborted) { |
|
92 rv = mPrintDC->EndDocument(); |
|
93 } else { |
|
94 rv = mPrintDC->AbortDocument(); |
|
95 } |
|
96 if (NS_FAILED(rv)) { |
|
97 // XXX nsPrintData::ShowPrintErrorDialog(rv); |
|
98 } |
|
99 } |
|
100 } |
|
101 |
|
102 delete mPrintObject; |
|
103 |
|
104 if (mBrandName) { |
|
105 NS_Free(mBrandName); |
|
106 } |
|
107 } |
|
108 |
|
109 void nsPrintData::OnStartPrinting() |
|
110 { |
|
111 if (!mOnStartSent) { |
|
112 DoOnProgressChange(0, 0, true, nsIWebProgressListener::STATE_START|nsIWebProgressListener::STATE_IS_DOCUMENT|nsIWebProgressListener::STATE_IS_NETWORK); |
|
113 mOnStartSent = true; |
|
114 } |
|
115 } |
|
116 |
|
117 void nsPrintData::OnEndPrinting() |
|
118 { |
|
119 DoOnProgressChange(100, 100, true, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT); |
|
120 DoOnProgressChange(100, 100, true, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_NETWORK); |
|
121 } |
|
122 |
|
123 void |
|
124 nsPrintData::DoOnProgressChange(int32_t aProgress, |
|
125 int32_t aMaxProgress, |
|
126 bool aDoStartStop, |
|
127 int32_t aFlag) |
|
128 { |
|
129 for (int32_t i=0;i<mPrintProgressListeners.Count();i++) { |
|
130 nsIWebProgressListener* wpl = mPrintProgressListeners.ObjectAt(i); |
|
131 wpl->OnProgressChange(nullptr, nullptr, aProgress, aMaxProgress, aProgress, aMaxProgress); |
|
132 if (aDoStartStop) { |
|
133 wpl->OnStateChange(nullptr, nullptr, aFlag, NS_OK); |
|
134 } |
|
135 } |
|
136 } |
|
137 |