nsPrintEngine.h 11 KB

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