nsPrintData.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "nsPrintData.h"
  6. #include "nsIStringBundle.h"
  7. #include "nsIServiceManager.h"
  8. #include "nsPIDOMWindow.h"
  9. #include "nsPrintObject.h"
  10. #include "nsPrintPreviewListener.h"
  11. #include "nsIWebProgressListener.h"
  12. #include "nsIWidget.h"
  13. #include "mozilla/Services.h"
  14. //-----------------------------------------------------
  15. // PR LOGGING
  16. #include "mozilla/Logging.h"
  17. #define DUMP_LAYOUT_LEVEL 9 // this turns on the dumping of each doucment's layout info
  18. static mozilla::LazyLogModule gPrintingLog("printing");
  19. #define PR_PL(_p1) MOZ_LOG(gPrintingLog, mozilla::LogLevel::Debug, _p1);
  20. //---------------------------------------------------
  21. //-- nsPrintData Class Impl
  22. //---------------------------------------------------
  23. nsPrintData::nsPrintData(ePrintDataType aType) :
  24. mType(aType), mDebugFilePtr(nullptr), mPrintObject(nullptr), mSelectedPO(nullptr),
  25. mPrintDocList(0), mIsIFrameSelected(false),
  26. mIsParentAFrameSet(false), mOnStartSent(false),
  27. mIsAborted(false), mPreparingForPrint(false), mDocWasToBeDestroyed(false),
  28. mShrinkToFit(false), mPrintFrameType(nsIPrintSettings::kFramesAsIs),
  29. mNumPrintablePages(0), mNumPagesPrinted(0),
  30. mShrinkRatio(1.0), mOrigDCScale(1.0), mPPEventListeners(nullptr),
  31. mBrandName(nullptr)
  32. {
  33. nsCOMPtr<nsIStringBundle> brandBundle;
  34. nsCOMPtr<nsIStringBundleService> svc =
  35. mozilla::services::GetStringBundleService();
  36. if (svc) {
  37. svc->CreateBundle( "chrome://branding/locale/brand.properties", getter_AddRefs( brandBundle ) );
  38. if (brandBundle) {
  39. brandBundle->GetStringFromName(u"brandShortName", &mBrandName );
  40. }
  41. }
  42. if (!mBrandName) {
  43. mBrandName = ToNewUnicode(NS_LITERAL_STRING("Mozilla Document"));
  44. }
  45. }
  46. nsPrintData::~nsPrintData()
  47. {
  48. // remove the event listeners
  49. if (mPPEventListeners) {
  50. mPPEventListeners->RemoveListeners();
  51. NS_RELEASE(mPPEventListeners);
  52. }
  53. // Only Send an OnEndPrinting if we have started printing
  54. if (mOnStartSent && mType != eIsPrintPreview) {
  55. OnEndPrinting();
  56. }
  57. if (mPrintDC && !mDebugFilePtr) {
  58. PR_PL(("****************** End Document ************************\n"));
  59. PR_PL(("\n"));
  60. bool isCancelled = false;
  61. mPrintSettings->GetIsCancelled(&isCancelled);
  62. nsresult rv = NS_OK;
  63. if (mType == eIsPrinting) {
  64. if (!isCancelled && !mIsAborted) {
  65. rv = mPrintDC->EndDocument();
  66. } else {
  67. rv = mPrintDC->AbortDocument();
  68. }
  69. if (NS_FAILED(rv)) {
  70. // XXX nsPrintData::ShowPrintErrorDialog(rv);
  71. }
  72. }
  73. }
  74. delete mPrintObject;
  75. if (mBrandName) {
  76. free(mBrandName);
  77. }
  78. }
  79. void nsPrintData::OnStartPrinting()
  80. {
  81. if (!mOnStartSent) {
  82. DoOnProgressChange(0, 0, true, nsIWebProgressListener::STATE_START|nsIWebProgressListener::STATE_IS_DOCUMENT|nsIWebProgressListener::STATE_IS_NETWORK);
  83. mOnStartSent = true;
  84. }
  85. }
  86. void nsPrintData::OnEndPrinting()
  87. {
  88. DoOnProgressChange(100, 100, true, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT);
  89. DoOnProgressChange(100, 100, true, nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_NETWORK);
  90. }
  91. void
  92. nsPrintData::DoOnProgressChange(int32_t aProgress,
  93. int32_t aMaxProgress,
  94. bool aDoStartStop,
  95. int32_t aFlag)
  96. {
  97. size_t numberOfListeners = mPrintProgressListeners.Length();
  98. for (size_t i = 0; i < numberOfListeners; ++i) {
  99. nsCOMPtr<nsIWebProgressListener> listener =
  100. mPrintProgressListeners.SafeElementAt(i);
  101. if (NS_WARN_IF(!listener)) {
  102. continue;
  103. }
  104. listener->OnProgressChange(nullptr, nullptr, aProgress, aMaxProgress,
  105. aProgress, aMaxProgress);
  106. if (aDoStartStop) {
  107. listener->OnStateChange(nullptr, nullptr, aFlag, NS_OK);
  108. }
  109. }
  110. }
  111. void
  112. nsPrintData::DoOnStatusChange(nsresult aStatus)
  113. {
  114. size_t numberOfListeners = mPrintProgressListeners.Length();
  115. for (size_t i = 0; i < numberOfListeners; ++i) {
  116. nsCOMPtr<nsIWebProgressListener> listener =
  117. mPrintProgressListeners.SafeElementAt(i);
  118. if (NS_WARN_IF(!listener)) {
  119. continue;
  120. }
  121. listener->OnStatusChange(nullptr, nullptr, aStatus, nullptr);
  122. }
  123. }