Line data Source code
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 : #ifndef nsPrintEngine_h___
6 : #define nsPrintEngine_h___
7 :
8 : #include "mozilla/Attributes.h"
9 : #include "mozilla/UniquePtr.h"
10 :
11 : #include "nsCOMPtr.h"
12 :
13 : #include "nsPrintObject.h"
14 : #include "nsPrintData.h"
15 : #include "nsFrameList.h"
16 : #include "nsIFrame.h"
17 : #include "nsIWebProgress.h"
18 : #include "mozilla/dom/HTMLCanvasElement.h"
19 : #include "nsIWebProgressListener.h"
20 : #include "nsWeakReference.h"
21 :
22 : // Interfaces
23 : #include "nsIObserver.h"
24 :
25 : // Classes
26 : class nsPagePrintTimer;
27 : class nsIDocShell;
28 : class nsIDocument;
29 : class nsIDocumentViewerPrint;
30 : class nsPrintObject;
31 : class nsIDocShell;
32 : class nsIPageSequenceFrame;
33 :
34 : //------------------------------------------------------------------------
35 : // nsPrintEngine Class
36 : //
37 : //------------------------------------------------------------------------
38 : class nsPrintEngine final : public nsIObserver,
39 : public nsIWebProgressListener,
40 : public nsSupportsWeakReference
41 : {
42 : public:
43 : // nsISupports interface...
44 : NS_DECL_ISUPPORTS
45 :
46 : // nsIObserver
47 : NS_DECL_NSIOBSERVER
48 :
49 : NS_DECL_NSIWEBPROGRESSLISTENER
50 :
51 : // Old nsIWebBrowserPrint methods; not cleaned up yet
52 : NS_IMETHOD Print(nsIPrintSettings* aPrintSettings,
53 : nsIWebProgressListener* aWebProgressListener);
54 : NS_IMETHOD PrintPreview(nsIPrintSettings* aPrintSettings,
55 : mozIDOMWindowProxy* aChildDOMWin,
56 : nsIWebProgressListener* aWebProgressListener);
57 : NS_IMETHOD GetIsFramesetDocument(bool *aIsFramesetDocument);
58 : NS_IMETHOD GetIsIFrameSelected(bool *aIsIFrameSelected);
59 : NS_IMETHOD GetIsRangeSelection(bool *aIsRangeSelection);
60 : NS_IMETHOD GetIsFramesetFrameSelected(bool *aIsFramesetFrameSelected);
61 : NS_IMETHOD GetPrintPreviewNumPages(int32_t *aPrintPreviewNumPages);
62 : NS_IMETHOD EnumerateDocumentNames(uint32_t* aCount, char16_t*** aResult);
63 : static nsresult GetGlobalPrintSettings(nsIPrintSettings** aPrintSettings);
64 : NS_IMETHOD GetDoingPrint(bool *aDoingPrint);
65 : NS_IMETHOD GetDoingPrintPreview(bool *aDoingPrintPreview);
66 : NS_IMETHOD GetCurrentPrintSettings(nsIPrintSettings **aCurrentPrintSettings);
67 :
68 :
69 : // This enum tells indicates what the default should be for the title
70 : // if the title from the document is null
71 : enum eDocTitleDefault {
72 : eDocTitleDefBlank,
73 : eDocTitleDefURLDoc
74 : };
75 :
76 : nsPrintEngine();
77 :
78 : void Destroy();
79 : void DestroyPrintingData();
80 :
81 : nsresult Initialize(nsIDocumentViewerPrint* aDocViewerPrint,
82 : nsIDocShell* aContainer,
83 : nsIDocument* aDocument,
84 : float aScreenDPI,
85 : FILE* aDebugFile);
86 :
87 : nsresult GetSeqFrameAndCountPages(nsIFrame*& aSeqFrame, int32_t& aCount);
88 :
89 : //
90 : // The following three methods are used for printing...
91 : //
92 : nsresult DocumentReadyForPrinting();
93 : nsresult GetSelectionDocument(nsIDeviceContextSpec * aDevSpec,
94 : nsIDocument ** aNewDoc);
95 :
96 : nsresult SetupToPrintContent();
97 : nsresult EnablePOsForPrinting();
98 : nsPrintObject* FindSmallestSTF();
99 :
100 : bool PrintDocContent(const mozilla::UniquePtr<nsPrintObject>& aPO,
101 : nsresult& aStatus);
102 : nsresult DoPrint(const mozilla::UniquePtr<nsPrintObject>& aPO);
103 :
104 : void SetPrintPO(nsPrintObject* aPO, bool aPrint);
105 :
106 : void TurnScriptingOn(bool aDoTurnOn);
107 : bool CheckDocumentForPPCaching();
108 : void InstallPrintPreviewListener();
109 :
110 : // nsIDocumentViewerPrint Printing Methods
111 : bool HasPrintCallbackCanvas();
112 : bool PrePrintPage();
113 : bool PrintPage(nsPrintObject* aPOect, bool& aInRange);
114 : bool DonePrintingPages(nsPrintObject* aPO, nsresult aResult);
115 :
116 : //---------------------------------------------------------------------
117 : void BuildDocTree(nsIDocShell * aParentNode,
118 : nsTArray<nsPrintObject*> * aDocList,
119 : const mozilla::UniquePtr<nsPrintObject>& aPO);
120 : nsresult ReflowDocList(const mozilla::UniquePtr<nsPrintObject>& aPO,
121 : bool aSetPixelScale);
122 :
123 : nsresult ReflowPrintObject(const mozilla::UniquePtr<nsPrintObject>& aPO);
124 :
125 : void CheckForChildFrameSets(const mozilla::UniquePtr<nsPrintObject>& aPO);
126 :
127 : void CalcNumPrintablePages(int32_t& aNumPages);
128 : void ShowPrintProgress(bool aIsForPrinting, bool& aDoNotify);
129 : nsresult CleanupOnFailure(nsresult aResult, bool aIsPrinting);
130 : // If FinishPrintPreview() fails, caller may need to reset the state of the
131 : // object, for example by calling CleanupOnFailure().
132 : nsresult FinishPrintPreview();
133 : static void CloseProgressDialog(nsIWebProgressListener* aWebProgressListener);
134 : void SetDocAndURLIntoProgress(const mozilla::UniquePtr<nsPrintObject>& aPO,
135 : nsIPrintProgressParams* aParams);
136 : void EllipseLongString(nsAString& aStr, const uint32_t aLen, bool aDoFront);
137 : nsresult CheckForPrinters(nsIPrintSettings* aPrintSettings);
138 : void CleanupDocTitleArray(char16_t**& aArray, int32_t& aCount);
139 :
140 : bool IsThereARangeSelection(nsPIDOMWindowOuter* aDOMWin);
141 :
142 : void FirePrintingErrorEvent(nsresult aPrintError);
143 : //---------------------------------------------------------------------
144 :
145 :
146 : // Timer Methods
147 : nsresult StartPagePrintTimer(const mozilla::UniquePtr<nsPrintObject>& aPO);
148 :
149 : bool IsWindowsInOurSubTree(nsPIDOMWindowOuter* aDOMWindow);
150 : static bool IsParentAFrameSet(nsIDocShell * aParent);
151 : bool IsThereAnIFrameSelected(nsIDocShell* aDocShell,
152 : nsPIDOMWindowOuter* aDOMWin,
153 : bool& aIsParentFrameSet);
154 :
155 : static nsPrintObject* FindPrintObjectByDOMWin(nsPrintObject* aParentObject,
156 : nsPIDOMWindowOuter* aDOMWin);
157 :
158 : // get the currently infocus frame for the document viewer
159 : already_AddRefed<nsPIDOMWindowOuter> FindFocusedDOMWindow();
160 :
161 : //---------------------------------------------------------------------
162 : // Static Methods
163 : //---------------------------------------------------------------------
164 : static void GetDocumentTitleAndURL(nsIDocument* aDoc,
165 : nsAString& aTitle,
166 : nsAString& aURLStr);
167 : void GetDisplayTitleAndURL(const mozilla::UniquePtr<nsPrintObject>& aPO,
168 : nsAString& aTitle,
169 : nsAString& aURLStr,
170 : eDocTitleDefault aDefType);
171 :
172 : static bool HasFramesetChild(nsIContent* aContent);
173 :
174 : bool CheckBeforeDestroy();
175 : nsresult Cancelled();
176 :
177 0 : nsIPresShell* GetPrintPreviewPresShell() {return mPrtPreview->mPrintObject->mPresShell;}
178 :
179 0 : float GetPrintPreviewScale() { return mPrtPreview->mPrintObject->
180 0 : mPresContext->GetPrintPreviewScale(); }
181 :
182 : static nsIPresShell* GetPresShellFor(nsIDocShell* aDocShell);
183 :
184 : // These calls also update the DocViewer
185 : void SetIsPrinting(bool aIsPrinting);
186 0 : bool GetIsPrinting()
187 : {
188 0 : return mIsDoingPrinting;
189 : }
190 : void SetIsPrintPreview(bool aIsPrintPreview);
191 0 : bool GetIsPrintPreview()
192 : {
193 0 : return mIsDoingPrintPreview;
194 : }
195 0 : void SetIsCreatingPrintPreview(bool aIsCreatingPrintPreview)
196 : {
197 0 : mIsCreatingPrintPreview = aIsCreatingPrintPreview;
198 0 : }
199 0 : bool GetIsCreatingPrintPreview()
200 : {
201 0 : return mIsCreatingPrintPreview;
202 : }
203 :
204 0 : void SetDisallowSelectionPrint(bool aDisallowSelectionPrint)
205 : {
206 0 : mDisallowSelectionPrint = aDisallowSelectionPrint;
207 0 : }
208 :
209 : protected:
210 : ~nsPrintEngine();
211 :
212 : nsresult CommonPrint(bool aIsPrintPreview, nsIPrintSettings* aPrintSettings,
213 : nsIWebProgressListener* aWebProgressListener,
214 : nsIDOMDocument* aDoc);
215 :
216 : nsresult DoCommonPrint(bool aIsPrintPreview, nsIPrintSettings* aPrintSettings,
217 : nsIWebProgressListener* aWebProgressListener,
218 : nsIDOMDocument* aDoc);
219 :
220 : void FirePrintCompletionEvent();
221 : static nsresult GetSeqFrameAndCountPagesInternal(const mozilla::UniquePtr<nsPrintObject>& aPO,
222 : nsIFrame*& aSeqFrame,
223 : int32_t& aCount);
224 :
225 : static nsresult FindSelectionBoundsWithList(nsFrameList::Enumerator& aChildFrames,
226 : nsIFrame * aParentFrame,
227 : nsRect& aRect,
228 : nsIFrame *& aStartFrame,
229 : nsRect& aStartRect,
230 : nsIFrame *& aEndFrame,
231 : nsRect& aEndRect);
232 :
233 : static nsresult FindSelectionBounds(nsIFrame * aParentFrame,
234 : nsRect& aRect,
235 : nsIFrame *& aStartFrame,
236 : nsRect& aStartRect,
237 : nsIFrame *& aEndFrame,
238 : nsRect& aEndRect);
239 :
240 : static nsresult GetPageRangeForSelection(nsIPageSequenceFrame* aPageSeqFrame,
241 : nsIFrame** aStartFrame,
242 : int32_t& aStartPageNum,
243 : nsRect& aStartRect,
244 : nsIFrame** aEndFrame,
245 : int32_t& aEndPageNum,
246 : nsRect& aEndRect);
247 :
248 : static void MapContentForPO(const mozilla::UniquePtr<nsPrintObject>& aPO,
249 : nsIContent* aContent);
250 :
251 : static void MapContentToWebShells(const mozilla::UniquePtr<nsPrintObject>& aRootPO,
252 : const mozilla::UniquePtr<nsPrintObject>& aPO);
253 :
254 : static void SetPrintAsIs(nsPrintObject* aPO, bool aAsIs = true);
255 :
256 : void DisconnectPagePrintTimer();
257 :
258 : // Static member variables
259 : bool mIsCreatingPrintPreview;
260 : bool mIsDoingPrinting;
261 : bool mIsDoingPrintPreview; // per DocumentViewer
262 : bool mProgressDialogIsShown;
263 :
264 : nsCOMPtr<nsIDocumentViewerPrint> mDocViewerPrint;
265 : nsWeakPtr mContainer;
266 : float mScreenDPI;
267 :
268 : // We are the primary owner of our nsPrintData member vars. These vars
269 : // are refcounted so that functions (e.g. nsPrintData methods) can create
270 : // temporary owning references when they need to fire a callback that
271 : // could conceivably destroy this nsPrintEngine owner object and all its
272 : // member-data.
273 : RefPtr<nsPrintData> mPrt;
274 :
275 : nsPagePrintTimer* mPagePrintTimer;
276 : WeakFrame mPageSeqFrame;
277 :
278 : // Print Preview
279 : RefPtr<nsPrintData> mPrtPreview;
280 : RefPtr<nsPrintData> mOldPrtPreview;
281 :
282 : nsCOMPtr<nsIDocument> mDocument;
283 :
284 : FILE* mDebugFile;
285 :
286 : int32_t mLoadCounter;
287 : bool mDidLoadDataForPrinting;
288 : bool mIsDestroying;
289 : bool mDisallowSelectionPrint;
290 :
291 : nsresult AfterNetworkPrint(bool aHandleError);
292 :
293 : nsresult SetRootView(nsPrintObject* aPO,
294 : bool& aDoReturn,
295 : bool& aDocumentIsTopLevel,
296 : nsSize& aAdjSize);
297 : nsView* GetParentViewForRoot();
298 : bool DoSetPixelScale();
299 : void UpdateZoomRatio(nsPrintObject* aPO, bool aSetPixelScale);
300 : nsresult ReconstructAndReflow(bool aDoSetPixelScale);
301 : nsresult UpdateSelectionAndShrinkPrintObject(nsPrintObject* aPO,
302 : bool aDocumentIsTopLevel);
303 : nsresult InitPrintDocConstruction(bool aHandleError);
304 : void FirePrintPreviewUpdateEvent();
305 : private:
306 : nsPrintEngine& operator=(const nsPrintEngine& aOther) = delete;
307 : };
308 :
309 : #endif /* nsPrintEngine_h___ */
|