michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsPrintData.h" michael@0: michael@0: #include "nsIStringBundle.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsPrintObject.h" michael@0: #include "nsPrintPreviewListener.h" michael@0: #include "nsIWebProgressListener.h" michael@0: #include "mozilla/Services.h" michael@0: michael@0: //----------------------------------------------------- michael@0: // PR LOGGING michael@0: #ifdef MOZ_LOGGING michael@0: #define FORCE_PR_LOG /* Allow logging in the release build */ michael@0: #endif michael@0: michael@0: #include "prlog.h" michael@0: michael@0: #ifdef PR_LOGGING michael@0: #define DUMP_LAYOUT_LEVEL 9 // this turns on the dumping of each doucment's layout info michael@0: static PRLogModuleInfo * michael@0: GetPrintingLog() michael@0: { michael@0: static PRLogModuleInfo *sLog; michael@0: if (!sLog) michael@0: sLog = PR_NewLogModule("printing"); michael@0: return sLog; michael@0: } michael@0: #define PR_PL(_p1) PR_LOG(GetPrintingLog(), PR_LOG_DEBUG, _p1); michael@0: #else michael@0: #define PRT_YESNO(_p) michael@0: #define PR_PL(_p1) michael@0: #endif michael@0: michael@0: //--------------------------------------------------- michael@0: //-- nsPrintData Class Impl michael@0: //--------------------------------------------------- michael@0: nsPrintData::nsPrintData(ePrintDataType aType) : michael@0: mType(aType), mDebugFilePtr(nullptr), mPrintObject(nullptr), mSelectedPO(nullptr), michael@0: mPrintDocList(0), mIsIFrameSelected(false), michael@0: mIsParentAFrameSet(false), mOnStartSent(false), michael@0: mIsAborted(false), mPreparingForPrint(false), mDocWasToBeDestroyed(false), michael@0: mShrinkToFit(false), mPrintFrameType(nsIPrintSettings::kFramesAsIs), michael@0: mNumPrintablePages(0), mNumPagesPrinted(0), michael@0: mShrinkRatio(1.0), mOrigDCScale(1.0), mPPEventListeners(nullptr), michael@0: mBrandName(nullptr) michael@0: { michael@0: MOZ_COUNT_CTOR(nsPrintData); michael@0: nsCOMPtr brandBundle; michael@0: nsCOMPtr svc = michael@0: mozilla::services::GetStringBundleService(); michael@0: if (svc) { michael@0: svc->CreateBundle( "chrome://branding/locale/brand.properties", getter_AddRefs( brandBundle ) ); michael@0: if (brandBundle) { michael@0: brandBundle->GetStringFromName(MOZ_UTF16("brandShortName"), &mBrandName ); michael@0: } michael@0: } michael@0: michael@0: if (!mBrandName) { michael@0: mBrandName = ToNewUnicode(NS_LITERAL_STRING("Mozilla Document")); michael@0: } michael@0: michael@0: } michael@0: michael@0: nsPrintData::~nsPrintData() michael@0: { michael@0: MOZ_COUNT_DTOR(nsPrintData); michael@0: // remove the event listeners michael@0: if (mPPEventListeners) { michael@0: mPPEventListeners->RemoveListeners(); michael@0: NS_RELEASE(mPPEventListeners); michael@0: } michael@0: michael@0: // Only Send an OnEndPrinting if we have started printing michael@0: if (mOnStartSent && mType != eIsPrintPreview) { michael@0: OnEndPrinting(); michael@0: } michael@0: michael@0: if (mPrintDC && !mDebugFilePtr) { michael@0: PR_PL(("****************** End Document ************************\n")); michael@0: PR_PL(("\n")); michael@0: bool isCancelled = false; michael@0: mPrintSettings->GetIsCancelled(&isCancelled); michael@0: michael@0: nsresult rv = NS_OK; michael@0: if (mType == eIsPrinting) { michael@0: if (!isCancelled && !mIsAborted) { michael@0: rv = mPrintDC->EndDocument(); michael@0: } else { michael@0: rv = mPrintDC->AbortDocument(); michael@0: } michael@0: if (NS_FAILED(rv)) { michael@0: // XXX nsPrintData::ShowPrintErrorDialog(rv); michael@0: } michael@0: } michael@0: } michael@0: michael@0: delete mPrintObject; michael@0: michael@0: if (mBrandName) { michael@0: NS_Free(mBrandName); michael@0: } michael@0: } michael@0: michael@0: void nsPrintData::OnStartPrinting() michael@0: { michael@0: if (!mOnStartSent) { michael@0: DoOnProgressChange(0, 0, true, nsIWebProgressListener::STATE_START|nsIWebProgressListener::STATE_IS_DOCUMENT|nsIWebProgressListener::STATE_IS_NETWORK); michael@0: mOnStartSent = true; michael@0: } michael@0: } michael@0: michael@0: void nsPrintData::OnEndPrinting() michael@0: { michael@0: DoOnProgressChange(100, 100, true, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT); michael@0: DoOnProgressChange(100, 100, true, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_NETWORK); michael@0: } michael@0: michael@0: void michael@0: nsPrintData::DoOnProgressChange(int32_t aProgress, michael@0: int32_t aMaxProgress, michael@0: bool aDoStartStop, michael@0: int32_t aFlag) michael@0: { michael@0: for (int32_t i=0;iOnProgressChange(nullptr, nullptr, aProgress, aMaxProgress, aProgress, aMaxProgress); michael@0: if (aDoStartStop) { michael@0: wpl->OnStateChange(nullptr, nullptr, aFlag, NS_OK); michael@0: } michael@0: } michael@0: } michael@0: