Back to index

lightning-sunbird  0.9+nobinonly
nsPrintData.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsPrintData.h"
00039 
00040 #include "nsIStringBundle.h"
00041 #include "nsIServiceManager.h"
00042 #include "nsReadableUtils.h"
00043 #include "nsCRT.h"
00044 
00045 #include "nsISelection.h"
00046 #include "nsIScriptGlobalObject.h"
00047 #include "nsIDocShell.h"
00048 #include "nsIURI.h"
00049 #include "nsINodeInfo.h"
00050 
00051 //-----------------------------------------------------
00052 // PR LOGGING
00053 #ifdef MOZ_LOGGING
00054 #define FORCE_PR_LOG /* Allow logging in the release build */
00055 #endif
00056 
00057 #include "prlog.h"
00058 
00059 #ifdef PR_LOGGING
00060 
00061 #ifdef NS_DEBUG
00062 // PR_LOGGING is force to always be on (even in release builds)
00063 // but we only want some of it on,
00064 //#define EXTENDED_DEBUG_PRINTING 
00065 #endif
00066 
00067 #define DUMP_LAYOUT_LEVEL 9 // this turns on the dumping of each doucment's layout info
00068 static PRLogModuleInfo * kPrintingLogMod = PR_NewLogModule("printing");
00069 #define PR_PL(_p1)  PR_LOG(kPrintingLogMod, PR_LOG_DEBUG, _p1);
00070 #else
00071 #define PRT_YESNO(_p)
00072 #define PR_PL(_p1)
00073 #endif
00074 
00075 //---------------------------------------------------
00076 //-- nsPrintData Class Impl
00077 //---------------------------------------------------
00078 nsPrintData::nsPrintData(ePrintDataType aType) :
00079   mType(aType), mPrintView(nsnull), mDebugFilePtr(nsnull), mPrintObject(nsnull), mSelectedPO(nsnull),
00080   mShowProgressDialog(PR_TRUE), mProgressDialogIsShown(PR_FALSE), mPrintDocList(nsnull), mIsIFrameSelected(PR_FALSE),
00081   mIsParentAFrameSet(PR_FALSE), mPrintingAsIsSubDoc(PR_FALSE), mOnStartSent(PR_FALSE),
00082   mIsAborted(PR_FALSE), mPreparingForPrint(PR_FALSE), mDocWasToBeDestroyed(PR_FALSE),
00083   mShrinkToFit(PR_FALSE), mPrintFrameType(nsIPrintSettings::kFramesAsIs), 
00084   mNumPrintableDocs(0), mNumDocsPrinted(0), mNumPrintablePages(0), mNumPagesPrinted(0),
00085   mShrinkRatio(1.0), mOrigDCScale(1.0), mOrigZoom(1.0), mPPEventListeners(NULL), 
00086   mBrandName(nsnull)
00087 {
00088 
00089   nsCOMPtr<nsIStringBundle> brandBundle;
00090   nsCOMPtr<nsIStringBundleService> svc( do_GetService( NS_STRINGBUNDLE_CONTRACTID ) );
00091   if (svc) {
00092     svc->CreateBundle( "chrome://branding/locale/brand.properties", getter_AddRefs( brandBundle ) );
00093     if (brandBundle) {
00094       brandBundle->GetStringFromName(NS_LITERAL_STRING("brandShortName").get(), &mBrandName );
00095     }
00096   }
00097 
00098   if (!mBrandName) {
00099     mBrandName = ToNewUnicode(NS_LITERAL_STRING("Mozilla Document"));
00100   }
00101 
00102 }
00103 
00104 nsPrintData::~nsPrintData()
00105 {
00106 
00107   // Set the cached Zoom value back into the DC
00108   if (mPrintDC) {
00109     mPrintDC->SetZoom(mOrigZoom);
00110   }
00111 
00112   // remove the event listeners
00113   if (mPPEventListeners) {
00114     mPPEventListeners->RemoveListeners();
00115     NS_RELEASE(mPPEventListeners);
00116   }
00117 
00118   // Only Send an OnEndPrinting if we have started printing
00119   if (mOnStartSent && mType != eIsPrintPreview) {
00120     OnEndPrinting();
00121   }
00122 
00123   if (mPrintDC && !mDebugFilePtr) {
00124     PR_PL(("****************** End Document ************************\n"));
00125     PR_PL(("\n"));
00126     PRBool isCancelled = PR_FALSE;
00127     mPrintSettings->GetIsCancelled(&isCancelled);
00128 
00129     nsresult rv = NS_OK;
00130     if (mType == eIsPrinting) {
00131       if (!isCancelled && !mIsAborted) {
00132         rv = mPrintDC->EndDocument();
00133       } else {
00134         rv = mPrintDC->AbortDocument();  
00135       }
00136       if (NS_FAILED(rv)) {
00137         // XXX nsPrintData::ShowPrintErrorDialog(rv);
00138       }
00139     }
00140   }
00141 
00142   delete mPrintObject;
00143 
00144   if (mPrintDocList != nsnull) {
00145     mPrintDocList->Clear();
00146     delete mPrintDocList;
00147   }
00148 
00149   if (mBrandName) {
00150     NS_Free(mBrandName);
00151   }
00152 
00153   for (PRInt32 i=0;i<mPrintProgressListeners.Count();i++) {
00154     nsIWebProgressListener* wpl = NS_STATIC_CAST(nsIWebProgressListener*, mPrintProgressListeners.ElementAt(i));
00155     NS_ASSERTION(wpl, "nsIWebProgressListener is NULL!");
00156     NS_RELEASE(wpl);
00157   }
00158 
00159 }
00160 
00161 void nsPrintData::OnStartPrinting()
00162 {
00163   if (!mOnStartSent) {
00164     DoOnProgressChange(mPrintProgressListeners, 0, 0, PR_TRUE, nsIWebProgressListener::STATE_START|nsIWebProgressListener::STATE_IS_DOCUMENT);
00165     mOnStartSent = PR_TRUE;
00166   }
00167 }
00168 
00169 void nsPrintData::OnEndPrinting()
00170 {
00171   DoOnProgressChange(mPrintProgressListeners, 100, 100, PR_TRUE, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT);
00172   if (mPrintProgress && mShowProgressDialog) {
00173     mPrintProgress->CloseProgressDialog(PR_TRUE);
00174   }
00175 }
00176 
00177 void
00178 nsPrintData::DoOnProgressChange(nsVoidArray& aListeners,
00179                               PRInt32      aProgess,
00180                               PRInt32      aMaxProgress,
00181                               PRBool       aDoStartStop,
00182                               PRInt32      aFlag)
00183 {
00184   if (aProgess == 0) return;
00185 
00186   for (PRInt32 i=0;i<aListeners.Count();i++) {
00187     nsIWebProgressListener* wpl = NS_STATIC_CAST(nsIWebProgressListener*, aListeners.ElementAt(i));
00188     NS_ASSERTION(wpl, "nsIWebProgressListener is NULL!");
00189     wpl->OnProgressChange(nsnull, nsnull, aProgess, aMaxProgress, aProgess, aMaxProgress);
00190     if (aDoStartStop) {
00191       wpl->OnStateChange(nsnull, nsnull, aFlag, 0);
00192     }
00193   }
00194 }
00195 
00196 
00197 
00198