Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / layout / printing / nsPrintData.cpp
blobd657aea6f209ee8929d7a3b36a48fa12dca38577
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsPrintData.h"
40 #include "nsIStringBundle.h"
41 #include "nsIServiceManager.h"
42 #include "nsPrintObject.h"
43 #include "nsPrintPreviewListener.h"
44 #include "nsIWebProgressListener.h"
46 //-----------------------------------------------------
47 // PR LOGGING
48 #ifdef MOZ_LOGGING
49 #define FORCE_PR_LOG /* Allow logging in the release build */
50 #endif
52 #include "prlog.h"
54 #ifdef PR_LOGGING
55 #define DUMP_LAYOUT_LEVEL 9 // this turns on the dumping of each doucment's layout info
56 static PRLogModuleInfo * kPrintingLogMod = PR_NewLogModule("printing");
57 #define PR_PL(_p1) PR_LOG(kPrintingLogMod, PR_LOG_DEBUG, _p1);
58 #else
59 #define PRT_YESNO(_p)
60 #define PR_PL(_p1)
61 #endif
63 //---------------------------------------------------
64 //-- nsPrintData Class Impl
65 //---------------------------------------------------
66 nsPrintData::nsPrintData(ePrintDataType aType) :
67 mType(aType), mDebugFilePtr(nsnull), mPrintObject(nsnull), mSelectedPO(nsnull),
68 mPrintDocList(nsnull), mIsIFrameSelected(PR_FALSE),
69 mIsParentAFrameSet(PR_FALSE), mOnStartSent(PR_FALSE),
70 mIsAborted(PR_FALSE), mPreparingForPrint(PR_FALSE), mDocWasToBeDestroyed(PR_FALSE),
71 mShrinkToFit(PR_FALSE), mPrintFrameType(nsIPrintSettings::kFramesAsIs),
72 mNumPrintablePages(0), mNumPagesPrinted(0),
73 mShrinkRatio(1.0), mOrigDCScale(1.0), mPPEventListeners(NULL),
74 mBrandName(nsnull)
77 nsCOMPtr<nsIStringBundle> brandBundle;
78 nsCOMPtr<nsIStringBundleService> svc( do_GetService( NS_STRINGBUNDLE_CONTRACTID ) );
79 if (svc) {
80 svc->CreateBundle( "chrome://branding/locale/brand.properties", getter_AddRefs( brandBundle ) );
81 if (brandBundle) {
82 brandBundle->GetStringFromName(NS_LITERAL_STRING("brandShortName").get(), &mBrandName );
86 if (!mBrandName) {
87 mBrandName = ToNewUnicode(NS_LITERAL_STRING("Mozilla Document"));
92 nsPrintData::~nsPrintData()
94 // remove the event listeners
95 if (mPPEventListeners) {
96 mPPEventListeners->RemoveListeners();
97 NS_RELEASE(mPPEventListeners);
100 // Only Send an OnEndPrinting if we have started printing
101 if (mOnStartSent && mType != eIsPrintPreview) {
102 OnEndPrinting();
105 if (mPrintDC && !mDebugFilePtr) {
106 PR_PL(("****************** End Document ************************\n"));
107 PR_PL(("\n"));
108 PRBool isCancelled = PR_FALSE;
109 mPrintSettings->GetIsCancelled(&isCancelled);
111 nsresult rv = NS_OK;
112 if (mType == eIsPrinting) {
113 if (!isCancelled && !mIsAborted) {
114 rv = mPrintDC->EndDocument();
115 } else {
116 rv = mPrintDC->AbortDocument();
118 if (NS_FAILED(rv)) {
119 // XXX nsPrintData::ShowPrintErrorDialog(rv);
124 delete mPrintObject;
126 if (mPrintDocList != nsnull) {
127 mPrintDocList->Clear();
128 delete mPrintDocList;
131 if (mBrandName) {
132 NS_Free(mBrandName);
136 void nsPrintData::OnStartPrinting()
138 if (!mOnStartSent) {
139 DoOnProgressChange(0, 0, PR_TRUE, nsIWebProgressListener::STATE_START|nsIWebProgressListener::STATE_IS_DOCUMENT);
140 mOnStartSent = PR_TRUE;
144 void nsPrintData::OnEndPrinting()
146 DoOnProgressChange(100, 100, PR_TRUE, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT);
149 void
150 nsPrintData::DoOnProgressChange(PRInt32 aProgess,
151 PRInt32 aMaxProgress,
152 PRBool aDoStartStop,
153 PRInt32 aFlag)
155 if (aProgess == 0) return;
157 for (PRInt32 i=0;i<mPrintProgressListeners.Count();i++) {
158 nsIWebProgressListener* wpl = mPrintProgressListeners.ObjectAt(i);
159 wpl->OnProgressChange(nsnull, nsnull, aProgess, aMaxProgress, aProgess, aMaxProgress);
160 if (aDoStartStop) {
161 wpl->OnStateChange(nsnull, nsnull, aFlag, 0);