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___
8 #include "mozilla/Attributes.h"
12 #include "nsPrintObject.h"
13 #include "nsPrintData.h"
14 #include "nsFrameList.h"
15 #include "mozilla/Attributes.h"
16 #include "nsIWebProgress.h"
17 #include "mozilla/dom/HTMLCanvasElement.h"
18 #include "nsIWebProgressListener.h"
19 #include "nsWeakReference.h"
22 #include "nsIDOMWindow.h"
23 #include "nsIObserver.h"
26 class nsPagePrintTimer
;
28 class nsDeviceContext
;
30 class nsIDocumentViewerPrint
;
33 class nsIPageSequenceFrame
;
34 class nsIWeakReference
;
36 //------------------------------------------------------------------------
37 // nsPrintEngine Class
39 //------------------------------------------------------------------------
40 class nsPrintEngine MOZ_FINAL
: public nsIObserver
,
41 public nsIWebProgressListener
,
42 public nsSupportsWeakReference
45 // nsISupports interface...
51 NS_DECL_NSIWEBPROGRESSLISTENER
53 // Old nsIWebBrowserPrint methods; not cleaned up yet
54 NS_IMETHOD
Print(nsIPrintSettings
* aPrintSettings
,
55 nsIWebProgressListener
* aWebProgressListener
);
56 NS_IMETHOD
PrintPreview(nsIPrintSettings
* aPrintSettings
,
57 nsIDOMWindow
*aChildDOMWin
,
58 nsIWebProgressListener
* aWebProgressListener
);
59 NS_IMETHOD
GetIsFramesetDocument(bool *aIsFramesetDocument
);
60 NS_IMETHOD
GetIsIFrameSelected(bool *aIsIFrameSelected
);
61 NS_IMETHOD
GetIsRangeSelection(bool *aIsRangeSelection
);
62 NS_IMETHOD
GetIsFramesetFrameSelected(bool *aIsFramesetFrameSelected
);
63 NS_IMETHOD
GetPrintPreviewNumPages(int32_t *aPrintPreviewNumPages
);
64 NS_IMETHOD
EnumerateDocumentNames(uint32_t* aCount
, char16_t
*** aResult
);
65 static nsresult
GetGlobalPrintSettings(nsIPrintSettings
** aPrintSettings
);
66 NS_IMETHOD
GetDoingPrint(bool *aDoingPrint
);
67 NS_IMETHOD
GetDoingPrintPreview(bool *aDoingPrintPreview
);
68 NS_IMETHOD
GetCurrentPrintSettings(nsIPrintSettings
**aCurrentPrintSettings
);
71 // This enum tells indicates what the default should be for the title
72 // if the title from the document is null
73 enum eDocTitleDefault
{
81 void DestroyPrintingData();
83 nsresult
Initialize(nsIDocumentViewerPrint
* aDocViewerPrint
,
84 nsIDocShell
* aContainer
,
85 nsIDocument
* aDocument
,
89 nsresult
GetSeqFrameAndCountPages(nsIFrame
*& aSeqFrame
, int32_t& aCount
);
92 // The following three methods are used for printing...
94 nsresult
DocumentReadyForPrinting();
95 nsresult
GetSelectionDocument(nsIDeviceContextSpec
* aDevSpec
,
96 nsIDocument
** aNewDoc
);
98 nsresult
SetupToPrintContent();
99 nsresult
EnablePOsForPrinting();
100 nsPrintObject
* FindSmallestSTF();
102 bool PrintDocContent(nsPrintObject
* aPO
, nsresult
& aStatus
);
103 nsresult
DoPrint(nsPrintObject
* aPO
);
105 void SetPrintPO(nsPrintObject
* aPO
, bool aPrint
);
107 void TurnScriptingOn(bool aDoTurnOn
);
108 bool CheckDocumentForPPCaching();
109 void InstallPrintPreviewListener();
111 // nsIDocumentViewerPrint Printing Methods
112 bool HasPrintCallbackCanvas();
114 bool PrintPage(nsPrintObject
* aPOect
, bool& aInRange
);
115 bool DonePrintingPages(nsPrintObject
* aPO
, nsresult aResult
);
117 //---------------------------------------------------------------------
118 void BuildDocTree(nsIDocShell
* aParentNode
,
119 nsTArray
<nsPrintObject
*> * aDocList
,
120 nsPrintObject
* aPO
);
121 nsresult
ReflowDocList(nsPrintObject
* aPO
, bool aSetPixelScale
);
123 nsresult
ReflowPrintObject(nsPrintObject
* aPO
);
125 void CheckForChildFrameSets(nsPrintObject
* aPO
);
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(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
);
140 bool IsThereARangeSelection(nsIDOMWindow
* aDOMWin
);
142 //---------------------------------------------------------------------
146 nsresult
StartPagePrintTimer(nsPrintObject
* aPO
);
148 bool IsWindowsInOurSubTree(nsPIDOMWindow
* aDOMWindow
);
149 static bool IsParentAFrameSet(nsIDocShell
* aParent
);
150 bool IsThereAnIFrameSelected(nsIDocShell
* aDocShell
,
151 nsIDOMWindow
* aDOMWin
,
152 bool& aIsParentFrameSet
);
154 static nsPrintObject
* FindPrintObjectByDOMWin(nsPrintObject
* aParentObject
,
155 nsIDOMWindow
* aDOMWin
);
157 // get the currently infocus frame for the document viewer
158 already_AddRefed
<nsIDOMWindow
> FindFocusedDOMWindow();
160 //---------------------------------------------------------------------
162 //---------------------------------------------------------------------
163 static void GetDocumentTitleAndURL(nsIDocument
* aDoc
,
166 void GetDisplayTitleAndURL(nsPrintObject
* aPO
,
169 eDocTitleDefault aDefType
);
170 static void ShowPrintErrorDialog(nsresult printerror
,
171 bool aIsPrinting
= true);
173 static bool HasFramesetChild(nsIContent
* aContent
);
175 bool CheckBeforeDestroy();
176 nsresult
Cancelled();
178 nsIPresShell
* GetPrintPreviewPresShell() {return mPrtPreview
->mPrintObject
->mPresShell
;}
180 float GetPrintPreviewScale() { return mPrtPreview
->mPrintObject
->
181 mPresContext
->GetPrintPreviewScale(); }
183 static nsIPresShell
* GetPresShellFor(nsIDocShell
* aDocShell
);
185 // These calls also update the DocViewer
186 void SetIsPrinting(bool aIsPrinting
);
189 return mIsDoingPrinting
;
191 void SetIsPrintPreview(bool aIsPrintPreview
);
192 bool GetIsPrintPreview()
194 return mIsDoingPrintPreview
;
196 void SetIsCreatingPrintPreview(bool aIsCreatingPrintPreview
)
198 mIsCreatingPrintPreview
= aIsCreatingPrintPreview
;
200 bool GetIsCreatingPrintPreview()
202 return mIsCreatingPrintPreview
;
205 void SetDisallowSelectionPrint(bool aDisallowSelectionPrint
)
207 mDisallowSelectionPrint
= aDisallowSelectionPrint
;
210 void SetNoMarginBoxes(bool aNoMarginBoxes
) {
211 mNoMarginBoxes
= aNoMarginBoxes
;
217 nsresult
CommonPrint(bool aIsPrintPreview
, nsIPrintSettings
* aPrintSettings
,
218 nsIWebProgressListener
* aWebProgressListener
,
219 nsIDOMDocument
* aDoc
);
221 nsresult
DoCommonPrint(bool aIsPrintPreview
, nsIPrintSettings
* aPrintSettings
,
222 nsIWebProgressListener
* aWebProgressListener
,
223 nsIDOMDocument
* aDoc
);
225 void FirePrintCompletionEvent();
226 static nsresult
GetSeqFrameAndCountPagesInternal(nsPrintObject
* aPO
,
227 nsIFrame
*& aSeqFrame
,
230 static nsresult
FindSelectionBoundsWithList(nsPresContext
* aPresContext
,
231 nsRenderingContext
& aRC
,
232 nsFrameList::Enumerator
& aChildFrames
,
233 nsIFrame
* aParentFrame
,
235 nsIFrame
*& aStartFrame
,
237 nsIFrame
*& aEndFrame
,
240 static nsresult
FindSelectionBounds(nsPresContext
* aPresContext
,
241 nsRenderingContext
& aRC
,
242 nsIFrame
* aParentFrame
,
244 nsIFrame
*& aStartFrame
,
246 nsIFrame
*& aEndFrame
,
249 static nsresult
GetPageRangeForSelection(nsIPresShell
* aPresShell
,
250 nsPresContext
* aPresContext
,
251 nsRenderingContext
& aRC
,
252 nsISelection
* aSelection
,
253 nsIPageSequenceFrame
* aPageSeqFrame
,
254 nsIFrame
** aStartFrame
,
255 int32_t& aStartPageNum
,
257 nsIFrame
** aEndFrame
,
258 int32_t& aEndPageNum
,
261 static void MapContentForPO(nsPrintObject
* aPO
, nsIContent
* aContent
);
263 static void MapContentToWebShells(nsPrintObject
* aRootPO
, nsPrintObject
* aPO
);
265 static void SetPrintAsIs(nsPrintObject
* aPO
, bool aAsIs
= true);
267 // Static member variables
268 bool mIsCreatingPrintPreview
;
269 bool mIsDoingPrinting
;
270 bool mIsDoingPrintPreview
; // per DocumentViewer
271 bool mProgressDialogIsShown
;
273 nsCOMPtr
<nsIDocumentViewerPrint
> mDocViewerPrint
;
274 nsWeakPtr mContainer
;
278 nsPagePrintTimer
* mPagePrintTimer
;
279 nsIPageSequenceFrame
* mPageSeqFrame
;
282 nsPrintData
* mPrtPreview
;
283 nsPrintData
* mOldPrtPreview
;
285 nsCOMPtr
<nsIDocument
> mDocument
;
289 int32_t mLoadCounter
;
290 bool mDidLoadDataForPrinting
;
292 bool mDisallowSelectionPrint
;
295 nsresult
AfterNetworkPrint(bool aHandleError
);
297 nsresult
SetRootView(nsPrintObject
* aPO
,
299 bool& aDocumentIsTopLevel
,
301 nsView
* GetParentViewForRoot();
302 bool DoSetPixelScale();
303 void UpdateZoomRatio(nsPrintObject
* aPO
, bool aSetPixelScale
);
304 nsresult
ReconstructAndReflow(bool aDoSetPixelScale
);
305 nsresult
UpdateSelectionAndShrinkPrintObject(nsPrintObject
* aPO
,
306 bool aDocumentIsTopLevel
);
307 nsresult
InitPrintDocConstruction(bool aHandleError
);
308 void FirePrintPreviewUpdateEvent();
310 nsPrintEngine
& operator=(const nsPrintEngine
& aOther
) = delete;
313 #endif /* nsPrintEngine_h___ */