1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsPrintObject.h"
39 : #include "nsIContentViewer.h"
40 : #include "nsIDOMDocument.h"
41 : #include "nsContentUtils.h"
42 : #include "nsIInterfaceRequestorUtils.h"
43 : #include "nsPIDOMWindow.h"
44 : #include "nsGkAtoms.h"
45 : #include "nsComponentManagerUtils.h"
46 : #include "nsIDocShellTreeItem.h"
47 : #include "nsIBaseWindow.h"
48 :
49 : //---------------------------------------------------
50 : //-- nsPrintObject Class Impl
51 : //---------------------------------------------------
52 0 : nsPrintObject::nsPrintObject() :
53 : mContent(nsnull), mFrameType(eFrame), mParent(nsnull),
54 : mHasBeenPrinted(false), mDontPrint(true), mPrintAsIs(false),
55 : mSharedPresShell(false), mInvisible(false), mDidCreateDocShell(false),
56 0 : mShrinkRatio(1.0), mZoomRatio(1.0)
57 : {
58 0 : MOZ_COUNT_CTOR(nsPrintObject);
59 0 : }
60 :
61 :
62 0 : nsPrintObject::~nsPrintObject()
63 : {
64 0 : MOZ_COUNT_DTOR(nsPrintObject);
65 0 : for (PRUint32 i=0;i<mKids.Length();i++) {
66 0 : nsPrintObject* po = mKids[i];
67 0 : delete po;
68 : }
69 :
70 0 : DestroyPresentation();
71 0 : if (mDidCreateDocShell && mDocShell) {
72 0 : nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(mDocShell));
73 0 : if (baseWin) {
74 0 : baseWin->Destroy();
75 : }
76 : }
77 0 : mDocShell = nsnull;
78 0 : mTreeOwner = nsnull; // mTreeOwner must be released after mDocShell;
79 0 : }
80 :
81 : //------------------------------------------------------------------
82 : nsresult
83 0 : nsPrintObject::Init(nsIDocShell* aDocShell, nsIDOMDocument* aDoc,
84 : bool aPrintPreview)
85 : {
86 0 : mPrintPreview = aPrintPreview;
87 :
88 0 : if (mPrintPreview || mParent) {
89 0 : mDocShell = aDocShell;
90 : } else {
91 0 : mTreeOwner = do_GetInterface(aDocShell);
92 0 : nsCOMPtr<nsIDocShellTreeItem> item = do_QueryInterface(aDocShell);
93 0 : PRInt32 itemType = 0;
94 0 : item->GetItemType(&itemType);
95 : // Create a container docshell for printing.
96 0 : mDocShell = do_CreateInstance("@mozilla.org/docshell;1");
97 0 : NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY);
98 0 : mDidCreateDocShell = true;
99 0 : nsCOMPtr<nsIDocShellTreeItem> newItem = do_QueryInterface(mDocShell);
100 0 : newItem->SetItemType(itemType);
101 0 : newItem->SetTreeOwner(mTreeOwner);
102 : }
103 0 : NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
104 :
105 0 : nsCOMPtr<nsIDOMDocument> dummy = do_GetInterface(mDocShell);
106 0 : nsCOMPtr<nsIContentViewer> viewer;
107 0 : mDocShell->GetContentViewer(getter_AddRefs(viewer));
108 0 : NS_ENSURE_STATE(viewer);
109 :
110 0 : nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
111 0 : NS_ENSURE_STATE(doc);
112 :
113 0 : if (mParent) {
114 0 : nsCOMPtr<nsPIDOMWindow> window = doc->GetWindow();
115 0 : if (window) {
116 0 : mContent = do_QueryInterface(window->GetFrameElementInternal());
117 : }
118 0 : mDocument = doc;
119 0 : return NS_OK;
120 : }
121 :
122 0 : mDocument = doc->CreateStaticClone(mDocShell);
123 0 : nsCOMPtr<nsIDOMDocument> clonedDOMDoc = do_QueryInterface(mDocument);
124 0 : NS_ENSURE_STATE(clonedDOMDoc);
125 :
126 0 : viewer->SetDOMDocument(clonedDOMDoc);
127 0 : return NS_OK;
128 : }
129 :
130 : //------------------------------------------------------------------
131 : // Resets PO by destroying the presentation
132 : void
133 0 : nsPrintObject::DestroyPresentation()
134 : {
135 0 : mPresContext = nsnull;
136 0 : if (mPresShell) {
137 0 : mPresShell->EndObservingDocument();
138 0 : nsAutoScriptBlocker scriptBlocker;
139 0 : mPresShell->Destroy();
140 : }
141 0 : mPresShell = nsnull;
142 0 : mViewManager = nsnull;
143 0 : }
144 :
|