forked from mozilla/mozilla-central
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsPrintEngine.cpp
4058 lines (3484 loc) · 132 KB
/
nsPrintEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsPrintEngine.h"
#include "nsIStringBundle.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
#include "mozilla/Selection.h"
#include "nsIScriptGlobalObject.h"
#include "nsPIDOMWindow.h"
#include "nsIDocShell.h"
#include "nsIFrame.h"
#include "nsIURI.h"
#include "nsITextToSubURI.h"
#include "nsError.h"
#include "nsView.h"
#include "nsAsyncDOMEvent.h"
#include <algorithm>
// Print Options
#include "nsIPrintSettings.h"
#include "nsIPrintSettingsService.h"
#include "nsIPrintOptions.h"
#include "nsIPrintSession.h"
#include "nsGfxCIID.h"
#include "nsIServiceManager.h"
#include "nsGkAtoms.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
static const char sPrintSettingsServiceContractID[] = "@mozilla.org/gfx/printsettings-service;1";
// Printing Events
#include "nsPrintPreviewListener.h"
#include "nsThreadUtils.h"
// Printing
#include "nsIWebBrowserPrint.h"
#include "nsIDOMHTMLFrameElement.h"
#include "nsIDOMHTMLFrameSetElement.h"
#include "nsIDOMHTMLIFrameElement.h"
#include "nsIDOMHTMLObjectElement.h"
#include "nsIDOMHTMLEmbedElement.h"
// Print Preview
#include "imgIContainer.h" // image animation mode constants
#include "nsIWebBrowserPrint.h" // needed for PrintPreview Navigation constants
// Print Progress
#include "nsIPrintProgress.h"
#include "nsIPrintProgressParams.h"
#include "nsIObserver.h"
// Print error dialog
#include "nsIPrompt.h"
#include "nsIWindowWatcher.h"
// Printing Prompts
#include "nsIPrintingPromptService.h"
static const char kPrintingPromptService[] = "@mozilla.org/embedcomp/printingprompt-service;1";
// Printing Timer
#include "nsPagePrintTimer.h"
// FrameSet
#include "nsIDocument.h"
// Focus
#include "nsISelectionController.h"
// Misc
#include "nsISupportsUtils.h"
#include "nsIScriptContext.h"
#include "nsIDOMDocument.h"
#include "nsISelectionListener.h"
#include "nsISelectionPrivate.h"
#include "nsIDOMRange.h"
#include "nsContentCID.h"
#include "nsLayoutCID.h"
#include "nsContentUtils.h"
#include "nsIPresShell.h"
#include "nsLayoutUtils.h"
#include "mozilla/Preferences.h"
#include "nsWidgetsCID.h"
#include "nsIDeviceContextSpec.h"
#include "nsViewManager.h"
#include "nsView.h"
#include "nsRenderingContext.h"
#include "nsIPageSequenceFrame.h"
#include "nsIURL.h"
#include "nsIContentViewerEdit.h"
#include "nsIContentViewerFile.h"
#include "nsIMarkupDocumentViewer.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDocShellTreeOwner.h"
#include "nsIWebBrowserChrome.h"
#include "nsIBaseWindow.h"
#include "nsILayoutHistoryState.h"
#include "nsFrameManager.h"
#include "nsGUIEvent.h"
#include "nsHTMLReflowState.h"
#include "nsIDOMHTMLAnchorElement.h"
#include "nsIDOMHTMLAreaElement.h"
#include "nsIDOMHTMLLinkElement.h"
#include "nsIDOMHTMLImageElement.h"
#include "nsIContentViewerContainer.h"
#include "nsIContentViewer.h"
#include "nsIDocumentViewerPrint.h"
#include "nsCSSFrameConstructor.h"
#include "nsFocusManager.h"
#include "nsRange.h"
#include "nsCDefaultURIFixup.h"
#include "nsIURIFixup.h"
#include "mozilla/dom/Element.h"
#include "nsContentList.h"
using namespace mozilla;
using namespace mozilla::dom;
//-----------------------------------------------------
// PR LOGGING
#ifdef MOZ_LOGGING
#define FORCE_PR_LOG /* Allow logging in the release build */
#endif
#include "prlog.h"
#ifdef PR_LOGGING
#ifdef DEBUG
// PR_LOGGING is force to always be on (even in release builds)
// but we only want some of it on,
//#define EXTENDED_DEBUG_PRINTING
#endif
#define DUMP_LAYOUT_LEVEL 9 // this turns on the dumping of each doucment's layout info
static PRLogModuleInfo *
GetPrintingLog()
{
static PRLogModuleInfo *sLog;
if (!sLog)
sLog = PR_NewLogModule("printing");
return sLog;
}
#define PR_PL(_p1) PR_LOG(GetPrintingLog(), PR_LOG_DEBUG, _p1);
#ifdef EXTENDED_DEBUG_PRINTING
static uint32_t gDumpFileNameCnt = 0;
static uint32_t gDumpLOFileNameCnt = 0;
#endif
#define PRT_YESNO(_p) ((_p)?"YES":"NO")
static const char * gFrameTypesStr[] = {"eDoc", "eFrame", "eIFrame", "eFrameSet"};
static const char * gPrintFrameTypeStr[] = {"kNoFrames", "kFramesAsIs", "kSelectedFrame", "kEachFrameSep"};
static const char * gFrameHowToEnableStr[] = {"kFrameEnableNone", "kFrameEnableAll", "kFrameEnableAsIsAndEach"};
static const char * gPrintRangeStr[] = {"kRangeAllPages", "kRangeSpecifiedPageRange", "kRangeSelection", "kRangeFocusFrame"};
#else
#define PRT_YESNO(_p)
#define PR_PL(_p1)
#endif
#ifdef EXTENDED_DEBUG_PRINTING
// Forward Declarations
static void DumpPrintObjectsListStart(const char * aStr, nsTArray<nsPrintObject*> * aDocList);
static void DumpPrintObjectsTree(nsPrintObject * aPO, int aLevel= 0, FILE* aFD = nullptr);
static void DumpPrintObjectsTreeLayout(nsPrintObject * aPO,nsDeviceContext * aDC, int aLevel= 0, FILE * aFD = nullptr);
#define DUMP_DOC_LIST(_title) DumpPrintObjectsListStart((_title), mPrt->mPrintDocList);
#define DUMP_DOC_TREE DumpPrintObjectsTree(mPrt->mPrintObject);
#define DUMP_DOC_TREELAYOUT DumpPrintObjectsTreeLayout(mPrt->mPrintObject, mPrt->mPrintDC);
#else
#define DUMP_DOC_LIST(_title)
#define DUMP_DOC_TREE
#define DUMP_DOC_TREELAYOUT
#endif
class nsScriptSuppressor
{
public:
nsScriptSuppressor(nsPrintEngine* aPrintEngine)
: mPrintEngine(aPrintEngine), mSuppressed(false) {}
~nsScriptSuppressor() { Unsuppress(); }
void Suppress()
{
if (mPrintEngine) {
mSuppressed = true;
mPrintEngine->TurnScriptingOn(false);
}
}
void Unsuppress()
{
if (mPrintEngine && mSuppressed) {
mPrintEngine->TurnScriptingOn(true);
}
mSuppressed = false;
}
void Disconnect() { mPrintEngine = nullptr; }
protected:
nsRefPtr<nsPrintEngine> mPrintEngine;
bool mSuppressed;
};
NS_IMPL_ISUPPORTS3(nsPrintEngine, nsIWebProgressListener,
nsISupportsWeakReference, nsIObserver)
//---------------------------------------------------
//-- nsPrintEngine Class Impl
//---------------------------------------------------
nsPrintEngine::nsPrintEngine() :
mIsCreatingPrintPreview(false),
mIsDoingPrinting(false),
mIsDoingPrintPreview(false),
mProgressDialogIsShown(false),
mScreenDPI(115.0f),
mPrt(nullptr),
mPagePrintTimer(nullptr),
mPageSeqFrame(nullptr),
mPrtPreview(nullptr),
mOldPrtPreview(nullptr),
mDebugFile(nullptr),
mLoadCounter(0),
mDidLoadDataForPrinting(false),
mIsDestroying(false),
mDisallowSelectionPrint(false),
mNoMarginBoxes(false)
{
}
//-------------------------------------------------------
nsPrintEngine::~nsPrintEngine()
{
Destroy(); // for insurance
}
//-------------------------------------------------------
void nsPrintEngine::Destroy()
{
if (mIsDestroying) {
return;
}
mIsDestroying = true;
if (mPrt) {
delete mPrt;
mPrt = nullptr;
}
#ifdef NS_PRINT_PREVIEW
if (mPrtPreview) {
delete mPrtPreview;
mPrtPreview = nullptr;
}
// This is insruance
if (mOldPrtPreview) {
delete mOldPrtPreview;
mOldPrtPreview = nullptr;
}
#endif
mDocViewerPrint = nullptr;
}
//-------------------------------------------------------
void nsPrintEngine::DestroyPrintingData()
{
if (mPrt) {
nsPrintData* data = mPrt;
mPrt = nullptr;
delete data;
}
}
//---------------------------------------------------------------------------------
//-- Section: Methods needed by the DocViewer
//---------------------------------------------------------------------------------
//--------------------------------------------------------
nsresult nsPrintEngine::Initialize(nsIDocumentViewerPrint* aDocViewerPrint,
nsIWeakReference* aContainer,
nsIDocument* aDocument,
float aScreenDPI,
FILE* aDebugFile)
{
NS_ENSURE_ARG_POINTER(aDocViewerPrint);
NS_ENSURE_ARG_POINTER(aContainer);
NS_ENSURE_ARG_POINTER(aDocument);
mDocViewerPrint = aDocViewerPrint;
mContainer = aContainer;
mDocument = aDocument;
mScreenDPI = aScreenDPI;
mDebugFile = aDebugFile; // ok to be NULL
return NS_OK;
}
//-------------------------------------------------------
bool
nsPrintEngine::CheckBeforeDestroy()
{
if (mPrt && mPrt->mPreparingForPrint) {
mPrt->mDocWasToBeDestroyed = true;
return true;
}
return false;
}
//-------------------------------------------------------
nsresult
nsPrintEngine::Cancelled()
{
if (mPrt && mPrt->mPrintSettings) {
return mPrt->mPrintSettings->SetIsCancelled(true);
}
return NS_ERROR_FAILURE;
}
//-------------------------------------------------------
// Install our event listeners on the document to prevent
// some events from being processed while in PrintPreview
//
// No return code - if this fails, there isn't much we can do
void
nsPrintEngine::InstallPrintPreviewListener()
{
if (!mPrt->mPPEventListeners) {
nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mContainer);
nsCOMPtr<nsPIDOMWindow> win(do_GetInterface(docShell));
if (win) {
nsCOMPtr<EventTarget> target = do_QueryInterface(win->GetFrameElementInternal());
mPrt->mPPEventListeners = new nsPrintPreviewListener(target);
mPrt->mPPEventListeners->AddListeners();
}
}
}
//----------------------------------------------------------------------
nsresult
nsPrintEngine::GetSeqFrameAndCountPagesInternal(nsPrintObject* aPO,
nsIFrame*& aSeqFrame,
int32_t& aCount)
{
NS_ENSURE_ARG_POINTER(aPO);
// Finds the SimplePageSequencer frame
nsIPageSequenceFrame* seqFrame = aPO->mPresShell->GetPageSequenceFrame();
if (seqFrame) {
aSeqFrame = do_QueryFrame(seqFrame);
} else {
aSeqFrame = nullptr;
}
if (aSeqFrame == nullptr) return NS_ERROR_FAILURE;
// first count the total number of pages
aCount = 0;
nsIFrame* pageFrame = aSeqFrame->GetFirstPrincipalChild();
while (pageFrame != nullptr) {
aCount++;
pageFrame = pageFrame->GetNextSibling();
}
return NS_OK;
}
//-----------------------------------------------------------------
nsresult nsPrintEngine::GetSeqFrameAndCountPages(nsIFrame*& aSeqFrame, int32_t& aCount)
{
NS_ASSERTION(mPrtPreview, "mPrtPreview can't be null!");
return GetSeqFrameAndCountPagesInternal(mPrtPreview->mPrintObject, aSeqFrame, aCount);
}
//---------------------------------------------------------------------------------
//-- Done: Methods needed by the DocViewer
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//-- Section: nsIWebBrowserPrint
//---------------------------------------------------------------------------------
// Foward decl for Debug Helper Functions
#ifdef EXTENDED_DEBUG_PRINTING
static int RemoveFilesInDir(const char * aDir);
static void GetDocTitleAndURL(nsPrintObject* aPO, char *& aDocStr, char *& aURLStr);
static void DumpPrintObjectsTree(nsPrintObject * aPO, int aLevel, FILE* aFD);
static void DumpPrintObjectsList(nsTArray<nsPrintObject*> * aDocList);
static void RootFrameList(nsPresContext* aPresContext, FILE* out, int32_t aIndent);
static void DumpViews(nsIDocShell* aDocShell, FILE* out);
static void DumpLayoutData(char* aTitleStr, char* aURLStr,
nsPresContext* aPresContext,
nsDeviceContext * aDC, nsIFrame * aRootFrame,
nsIDocShell * aDocShell, FILE* aFD);
#endif
//--------------------------------------------------------------------------------
nsresult
nsPrintEngine::CommonPrint(bool aIsPrintPreview,
nsIPrintSettings* aPrintSettings,
nsIWebProgressListener* aWebProgressListener,
nsIDOMDocument* aDoc) {
nsRefPtr<nsPrintEngine> kungfuDeathGrip = this;
nsresult rv = DoCommonPrint(aIsPrintPreview, aPrintSettings,
aWebProgressListener, aDoc);
if (NS_FAILED(rv)) {
if (aIsPrintPreview) {
SetIsCreatingPrintPreview(false);
SetIsPrintPreview(false);
} else {
SetIsPrinting(false);
}
if (mProgressDialogIsShown)
CloseProgressDialog(aWebProgressListener);
if (rv != NS_ERROR_ABORT && rv != NS_ERROR_OUT_OF_MEMORY)
ShowPrintErrorDialog(rv, !aIsPrintPreview);
delete mPrt;
mPrt = nullptr;
}
return rv;
}
nsresult
nsPrintEngine::DoCommonPrint(bool aIsPrintPreview,
nsIPrintSettings* aPrintSettings,
nsIWebProgressListener* aWebProgressListener,
nsIDOMDocument* aDoc)
{
nsresult rv;
if (aIsPrintPreview) {
// The WebProgressListener can be QI'ed to nsIPrintingPromptService
// then that means the progress dialog is already being shown.
nsCOMPtr<nsIPrintingPromptService> pps(do_QueryInterface(aWebProgressListener));
mProgressDialogIsShown = pps != nullptr;
if (mIsDoingPrintPreview) {
mOldPrtPreview = mPrtPreview;
mPrtPreview = nullptr;
}
} else {
mProgressDialogIsShown = false;
}
mPrt = new nsPrintData(aIsPrintPreview ? nsPrintData::eIsPrintPreview :
nsPrintData::eIsPrinting);
NS_ENSURE_TRUE(mPrt, NS_ERROR_OUT_OF_MEMORY);
// if they don't pass in a PrintSettings, then get the Global PS
mPrt->mPrintSettings = aPrintSettings;
if (!mPrt->mPrintSettings) {
rv = GetGlobalPrintSettings(getter_AddRefs(mPrt->mPrintSettings));
NS_ENSURE_SUCCESS(rv, rv);
}
rv = CheckForPrinters(mPrt->mPrintSettings);
NS_ENSURE_SUCCESS(rv, rv);
mPrt->mPrintSettings->SetIsCancelled(false);
mPrt->mPrintSettings->GetShrinkToFit(&mPrt->mShrinkToFit);
// In the case the margin boxes are not printed store the print settings for
// the footer/header to be used as default print setting for follow up prints.
mPrt->mPrintSettings->SetPersistMarginBoxSettings(!mNoMarginBoxes);
if (mNoMarginBoxes) {
// Set the footer/header to blank.
const PRUnichar* emptyString = EmptyString().get();
mPrt->mPrintSettings->SetHeaderStrLeft(emptyString);
mPrt->mPrintSettings->SetHeaderStrCenter(emptyString);
mPrt->mPrintSettings->SetHeaderStrRight(emptyString);
mPrt->mPrintSettings->SetFooterStrLeft(emptyString);
mPrt->mPrintSettings->SetFooterStrCenter(emptyString);
mPrt->mPrintSettings->SetFooterStrRight(emptyString);
}
if (aIsPrintPreview) {
SetIsCreatingPrintPreview(true);
SetIsPrintPreview(true);
nsCOMPtr<nsIMarkupDocumentViewer> viewer =
do_QueryInterface(mDocViewerPrint);
if (viewer) {
viewer->SetTextZoom(1.0f);
viewer->SetFullZoom(1.0f);
viewer->SetMinFontSize(0);
}
}
// Create a print session and let the print settings know about it.
// The print settings hold an nsWeakPtr to the session so it does not
// need to be cleared from the settings at the end of the job.
// XXX What lifetime does the printSession need to have?
nsCOMPtr<nsIPrintSession> printSession;
if (!aIsPrintPreview) {
printSession = do_CreateInstance("@mozilla.org/gfx/printsession;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
mPrt->mPrintSettings->SetPrintSession(printSession);
}
if (aWebProgressListener != nullptr) {
mPrt->mPrintProgressListeners.AppendObject(aWebProgressListener);
}
// Get the currently focused window and cache it
// because the Print Dialog will "steal" focus and later when you try
// to get the currently focused windows it will be NULL
mPrt->mCurrentFocusWin = FindFocusedDOMWindow();
// Check to see if there is a "regular" selection
bool isSelection = IsThereARangeSelection(mPrt->mCurrentFocusWin);
// Get the docshell for this documentviewer
nsCOMPtr<nsIDocShell> webContainer(do_QueryReferent(mContainer, &rv));
NS_ENSURE_SUCCESS(rv, rv);
{
if (aIsPrintPreview) {
nsCOMPtr<nsIContentViewer> viewer;
webContainer->GetContentViewer(getter_AddRefs(viewer));
if (viewer && viewer->GetDocument() && viewer->GetDocument()->IsShowing()) {
viewer->GetDocument()->OnPageHide(false, nullptr);
}
}
nsAutoScriptBlocker scriptBlocker;
mPrt->mPrintObject = new nsPrintObject();
NS_ENSURE_TRUE(mPrt->mPrintObject, NS_ERROR_OUT_OF_MEMORY);
rv = mPrt->mPrintObject->Init(webContainer, aDoc, aIsPrintPreview);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(mPrt->mPrintDocList.AppendElement(mPrt->mPrintObject),
NS_ERROR_OUT_OF_MEMORY);
mPrt->mIsParentAFrameSet = IsParentAFrameSet(webContainer);
mPrt->mPrintObject->mFrameType = mPrt->mIsParentAFrameSet ? eFrameSet : eDoc;
// Build the "tree" of PrintObjects
BuildDocTree(mPrt->mPrintObject->mDocShell, &mPrt->mPrintDocList,
mPrt->mPrintObject);
}
if (!aIsPrintPreview) {
SetIsPrinting(true);
}
// XXX This isn't really correct...
if (!mPrt->mPrintObject->mDocument ||
!mPrt->mPrintObject->mDocument->GetRootElement())
return NS_ERROR_GFX_PRINTER_STARTDOC;
// Create the linkage from the sub-docs back to the content element
// in the parent document
MapContentToWebShells(mPrt->mPrintObject, mPrt->mPrintObject);
mPrt->mIsIFrameSelected = IsThereAnIFrameSelected(webContainer, mPrt->mCurrentFocusWin, mPrt->mIsParentAFrameSet);
// Setup print options for UI
if (mPrt->mIsParentAFrameSet) {
if (mPrt->mCurrentFocusWin) {
mPrt->mPrintSettings->SetHowToEnableFrameUI(nsIPrintSettings::kFrameEnableAll);
} else {
mPrt->mPrintSettings->SetHowToEnableFrameUI(nsIPrintSettings::kFrameEnableAsIsAndEach);
}
} else {
mPrt->mPrintSettings->SetHowToEnableFrameUI(nsIPrintSettings::kFrameEnableNone);
}
// Now determine how to set up the Frame print UI
mPrt->mPrintSettings->SetPrintOptions(nsIPrintSettings::kEnableSelectionRB,
isSelection || mPrt->mIsIFrameSelected);
nsCOMPtr<nsIDeviceContextSpec> devspec
(do_CreateInstance("@mozilla.org/gfx/devicecontextspec;1", &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsScriptSuppressor scriptSuppressor(this);
if (!aIsPrintPreview) {
#ifdef DEBUG
mPrt->mDebugFilePtr = mDebugFile;
#endif
scriptSuppressor.Suppress();
bool printSilently;
mPrt->mPrintSettings->GetPrintSilent(&printSilently);
// Check prefs for a default setting as to whether we should print silently
printSilently =
Preferences::GetBool("print.always_print_silent", printSilently);
// Ask dialog to be Print Shown via the Plugable Printing Dialog Service
// This service is for the Print Dialog and the Print Progress Dialog
// If printing silently or you can't get the service continue on
if (!printSilently) {
nsCOMPtr<nsIPrintingPromptService> printPromptService(do_GetService(kPrintingPromptService));
if (printPromptService) {
nsIDOMWindow *domWin = mDocument->GetWindow();
NS_ENSURE_TRUE(domWin, NS_ERROR_FAILURE);
// Platforms not implementing a given dialog for the service may
// return NS_ERROR_NOT_IMPLEMENTED or an error code.
//
// NS_ERROR_NOT_IMPLEMENTED indicates they want default behavior
// Any other error code means we must bail out
//
nsCOMPtr<nsIWebBrowserPrint> wbp(do_QueryInterface(mDocViewerPrint));
rv = printPromptService->ShowPrintDialog(domWin, wbp,
mPrt->mPrintSettings);
//
// ShowPrintDialog triggers an event loop which means we can't assume
// that the state of this->{anything} matches the state we've checked
// above. Including that a given {thing} is non null.
if (!mPrt) {
return NS_ERROR_FAILURE;
}
if (NS_SUCCEEDED(rv)) {
// since we got the dialog and it worked then make sure we
// are telling GFX we want to print silent
printSilently = true;
if (mPrt->mPrintSettings) {
// The user might have changed shrink-to-fit in the print dialog, so update our copy of its state
mPrt->mPrintSettings->GetShrinkToFit(&mPrt->mShrinkToFit);
}
} else if (rv == NS_ERROR_NOT_IMPLEMENTED) {
// This means the Dialog service was there,
// but they choose not to implement this dialog and
// are looking for default behavior from the toolkit
rv = NS_OK;
}
} else {
// No dialog service available
rv = NS_ERROR_NOT_IMPLEMENTED;
}
} else {
// Call any code that requires a run of the event loop.
rv = mPrt->mPrintSettings->SetupSilentPrinting();
}
// Check explicitly for abort because it's expected
if (rv == NS_ERROR_ABORT)
return rv;
NS_ENSURE_SUCCESS(rv, rv);
}
rv = devspec->Init(nullptr, mPrt->mPrintSettings, aIsPrintPreview);
NS_ENSURE_SUCCESS(rv, rv);
mPrt->mPrintDC = new nsDeviceContext();
rv = mPrt->mPrintDC->InitForPrinting(devspec);
NS_ENSURE_SUCCESS(rv, rv);
if (aIsPrintPreview) {
mPrt->mPrintSettings->SetPrintFrameType(nsIPrintSettings::kFramesAsIs);
// override any UI that wants to PrintPreview any selection or page range
// we want to view every page in PrintPreview each time
mPrt->mPrintSettings->SetPrintRange(nsIPrintSettings::kRangeAllPages);
} else {
// Always check and set the print settings first and then fall back
// onto the PrintService if there isn't a PrintSettings
//
// Posiible Usage values:
// nsIPrintSettings::kUseInternalDefault
// nsIPrintSettings::kUseSettingWhenPossible
//
// NOTE: The consts are the same for PrintSettings and PrintSettings
int16_t printFrameTypeUsage = nsIPrintSettings::kUseSettingWhenPossible;
mPrt->mPrintSettings->GetPrintFrameTypeUsage(&printFrameTypeUsage);
// Ok, see if we are going to use our value and override the default
if (printFrameTypeUsage == nsIPrintSettings::kUseSettingWhenPossible) {
// Get the Print Options/Settings PrintFrameType to see what is preferred
int16_t printFrameType = nsIPrintSettings::kEachFrameSep;
mPrt->mPrintSettings->GetPrintFrameType(&printFrameType);
// Don't let anybody do something stupid like try to set it to
// kNoFrames when we are printing a FrameSet
if (printFrameType == nsIPrintSettings::kNoFrames) {
mPrt->mPrintFrameType = nsIPrintSettings::kEachFrameSep;
mPrt->mPrintSettings->SetPrintFrameType(mPrt->mPrintFrameType);
} else {
// First find out from the PrinService what options are available
// to us for Printing FrameSets
int16_t howToEnableFrameUI;
mPrt->mPrintSettings->GetHowToEnableFrameUI(&howToEnableFrameUI);
if (howToEnableFrameUI != nsIPrintSettings::kFrameEnableNone) {
switch (howToEnableFrameUI) {
case nsIPrintSettings::kFrameEnableAll:
mPrt->mPrintFrameType = printFrameType;
break;
case nsIPrintSettings::kFrameEnableAsIsAndEach:
if (printFrameType != nsIPrintSettings::kSelectedFrame) {
mPrt->mPrintFrameType = printFrameType;
} else { // revert back to a good value
mPrt->mPrintFrameType = nsIPrintSettings::kEachFrameSep;
}
break;
} // switch
mPrt->mPrintSettings->SetPrintFrameType(mPrt->mPrintFrameType);
}
}
} else {
mPrt->mPrintSettings->GetPrintFrameType(&mPrt->mPrintFrameType);
}
}
if (mPrt->mPrintFrameType == nsIPrintSettings::kEachFrameSep) {
CheckForChildFrameSets(mPrt->mPrintObject);
}
if (NS_FAILED(EnablePOsForPrinting())) {
return NS_ERROR_FAILURE;
}
// Attach progressListener to catch network requests.
nsCOMPtr<nsIWebProgress> webProgress = do_QueryInterface(mPrt->mPrintObject->mDocShell);
webProgress->AddProgressListener(
static_cast<nsIWebProgressListener*>(this),
nsIWebProgress::NOTIFY_STATE_REQUEST);
mLoadCounter = 0;
mDidLoadDataForPrinting = false;
if (aIsPrintPreview) {
bool notifyOnInit = false;
ShowPrintProgress(false, notifyOnInit);
// Very important! Turn Off scripting
TurnScriptingOn(false);
if (!notifyOnInit) {
InstallPrintPreviewListener();
rv = InitPrintDocConstruction(false);
} else {
rv = NS_OK;
}
} else {
bool doNotify;
ShowPrintProgress(true, doNotify);
if (!doNotify) {
// Print listener setup...
mPrt->OnStartPrinting();
rv = InitPrintDocConstruction(false);
}
}
// We will enable scripting later after printing has finished.
scriptSuppressor.Disconnect();
return NS_OK;
}
//---------------------------------------------------------------------------------
NS_IMETHODIMP
nsPrintEngine::Print(nsIPrintSettings* aPrintSettings,
nsIWebProgressListener* aWebProgressListener)
{
// If we have a print preview document, use that instead of the original
// mDocument. That way animated images etc. get printed using the same state
// as in print preview.
nsCOMPtr<nsIDOMDocument> doc =
do_QueryInterface(mPrtPreview && mPrtPreview->mPrintObject ?
mPrtPreview->mPrintObject->mDocument : mDocument);
return CommonPrint(false, aPrintSettings, aWebProgressListener, doc);
}
NS_IMETHODIMP
nsPrintEngine::PrintPreview(nsIPrintSettings* aPrintSettings,
nsIDOMWindow *aChildDOMWin,
nsIWebProgressListener* aWebProgressListener)
{
// Get the DocShell and see if it is busy
// (We can't Print Preview this document if it is still busy)
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mContainer));
NS_ENSURE_STATE(docShell);
uint32_t busyFlags = nsIDocShell::BUSY_FLAGS_NONE;
if (NS_FAILED(docShell->GetBusyFlags(&busyFlags)) ||
busyFlags != nsIDocShell::BUSY_FLAGS_NONE) {
CloseProgressDialog(aWebProgressListener);
ShowPrintErrorDialog(NS_ERROR_GFX_PRINTER_DOC_IS_BUSY, false);
return NS_ERROR_FAILURE;
}
NS_ENSURE_STATE(aChildDOMWin);
nsCOMPtr<nsIDOMDocument> doc;
aChildDOMWin->GetDocument(getter_AddRefs(doc));
NS_ENSURE_STATE(doc);
// Document is not busy -- go ahead with the Print Preview
return CommonPrint(true, aPrintSettings, aWebProgressListener, doc);
}
//----------------------------------------------------------------------------------
/* readonly attribute boolean isFramesetDocument; */
NS_IMETHODIMP
nsPrintEngine::GetIsFramesetDocument(bool *aIsFramesetDocument)
{
nsCOMPtr<nsIDocShell> webContainer(do_QueryReferent(mContainer));
*aIsFramesetDocument = IsParentAFrameSet(webContainer);
return NS_OK;
}
//----------------------------------------------------------------------------------
/* readonly attribute boolean isIFrameSelected; */
NS_IMETHODIMP
nsPrintEngine::GetIsIFrameSelected(bool *aIsIFrameSelected)
{
*aIsIFrameSelected = false;
// Get the docshell for this documentviewer
nsCOMPtr<nsIDocShell> webContainer(do_QueryReferent(mContainer));
// Get the currently focused window
nsCOMPtr<nsIDOMWindow> currentFocusWin = FindFocusedDOMWindow();
if (currentFocusWin && webContainer) {
// Get whether the doc contains a frameset
// Also, check to see if the currently focus docshell
// is a child of this docshell
bool isParentFrameSet;
*aIsIFrameSelected = IsThereAnIFrameSelected(webContainer, currentFocusWin, isParentFrameSet);
}
return NS_OK;
}
//----------------------------------------------------------------------------------
/* readonly attribute boolean isRangeSelection; */
NS_IMETHODIMP
nsPrintEngine::GetIsRangeSelection(bool *aIsRangeSelection)
{
// Get the currently focused window
nsCOMPtr<nsIDOMWindow> currentFocusWin = FindFocusedDOMWindow();
*aIsRangeSelection = IsThereARangeSelection(currentFocusWin);
return NS_OK;
}
//----------------------------------------------------------------------------------
/* readonly attribute boolean isFramesetFrameSelected; */
NS_IMETHODIMP
nsPrintEngine::GetIsFramesetFrameSelected(bool *aIsFramesetFrameSelected)
{
// Get the currently focused window
nsCOMPtr<nsIDOMWindow> currentFocusWin = FindFocusedDOMWindow();
*aIsFramesetFrameSelected = currentFocusWin != nullptr;
return NS_OK;
}
//----------------------------------------------------------------------------------
/* readonly attribute long printPreviewNumPages; */
NS_IMETHODIMP
nsPrintEngine::GetPrintPreviewNumPages(int32_t *aPrintPreviewNumPages)
{
NS_ENSURE_ARG_POINTER(aPrintPreviewNumPages);
nsPrintData* prt = nullptr;
nsIFrame* seqFrame = nullptr;
*aPrintPreviewNumPages = 0;
// When calling this function, the FinishPrintPreview() function might not
// been called as there are still some
if (mPrtPreview) {
prt = mPrtPreview;
} else {
prt = mPrt;
}
if ((!prt) ||
NS_FAILED(GetSeqFrameAndCountPagesInternal(prt->mPrintObject, seqFrame, *aPrintPreviewNumPages))) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
//----------------------------------------------------------------------------------
// Enumerate all the documents for their titles
NS_IMETHODIMP
nsPrintEngine::EnumerateDocumentNames(uint32_t* aCount,
PRUnichar*** aResult)
{
NS_ENSURE_ARG(aCount);
NS_ENSURE_ARG_POINTER(aResult);
*aCount = 0;
*aResult = nullptr;
int32_t numDocs = mPrt->mPrintDocList.Length();
PRUnichar** array = (PRUnichar**) nsMemory::Alloc(numDocs * sizeof(PRUnichar*));
if (!array)
return NS_ERROR_OUT_OF_MEMORY;
for (int32_t i=0;i<numDocs;i++) {
nsPrintObject* po = mPrt->mPrintDocList.ElementAt(i);
NS_ASSERTION(po, "nsPrintObject can't be null!");
PRUnichar * docTitleStr;
PRUnichar * docURLStr;
GetDocumentTitleAndURL(po->mDocument, &docTitleStr, &docURLStr);
// Use the URL if the doc is empty
if (!docTitleStr || !*docTitleStr) {
if (docURLStr && *docURLStr) {
nsMemory::Free(docTitleStr);
docTitleStr = docURLStr;
} else {
nsMemory::Free(docURLStr);
}
docURLStr = nullptr;
if (!docTitleStr || !*docTitleStr) {
CleanupDocTitleArray(array, i);
return NS_ERROR_OUT_OF_MEMORY;
}
}
array[i] = docTitleStr;
if (docURLStr) nsMemory::Free(docURLStr);
}
*aCount = numDocs;
*aResult = array;
return NS_OK;
}
//----------------------------------------------------------------------------------
/* readonly attribute nsIPrintSettings globalPrintSettings; */
nsresult
nsPrintEngine::GetGlobalPrintSettings(nsIPrintSettings **aGlobalPrintSettings)
{
NS_ENSURE_ARG_POINTER(aGlobalPrintSettings);
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsIPrintSettingsService> printSettingsService =
do_GetService(sPrintSettingsServiceContractID, &rv);
if (NS_SUCCEEDED(rv)) {
rv = printSettingsService->GetGlobalPrintSettings(aGlobalPrintSettings);
}
return rv;
}
//----------------------------------------------------------------------------------
/* readonly attribute boolean doingPrint; */
NS_IMETHODIMP
nsPrintEngine::GetDoingPrint(bool *aDoingPrint)
{
NS_ENSURE_ARG_POINTER(aDoingPrint);
*aDoingPrint = mIsDoingPrinting;
return NS_OK;
}
//----------------------------------------------------------------------------------
/* readonly attribute boolean doingPrintPreview; */
NS_IMETHODIMP
nsPrintEngine::GetDoingPrintPreview(bool *aDoingPrintPreview)
{
NS_ENSURE_ARG_POINTER(aDoingPrintPreview);
*aDoingPrintPreview = mIsDoingPrintPreview;
return NS_OK;
}
//----------------------------------------------------------------------------------
/* readonly attribute nsIPrintSettings currentPrintSettings; */
NS_IMETHODIMP
nsPrintEngine::GetCurrentPrintSettings(nsIPrintSettings * *aCurrentPrintSettings)
{
NS_ENSURE_ARG_POINTER(aCurrentPrintSettings);
if (mPrt) {
*aCurrentPrintSettings = mPrt->mPrintSettings;
} else if (mPrtPreview) {
*aCurrentPrintSettings = mPrtPreview->mPrintSettings;
} else {
*aCurrentPrintSettings = nullptr;
}
NS_IF_ADDREF(*aCurrentPrintSettings);
return NS_OK;
}
//-----------------------------------------------------------------
//-- Section: Pre-Reflow Methods
//-----------------------------------------------------------------
//---------------------------------------------------------------------
// This method checks to see if there is at least one printer defined
// and if so, it sets the first printer in the list as the default name