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/. */
6 #include "nsPrintDialogWin.h"
9 #include "nsComponentManagerUtils.h"
11 #include "nsIBaseWindow.h"
12 #include "nsIBrowserChild.h"
13 #include "nsIDialogParamBlock.h"
14 #include "nsIDocShell.h"
15 #include "nsIInterfaceRequestorUtils.h"
16 #include "nsIPrintSettings.h"
17 #include "nsIWebBrowserChrome.h"
18 #include "nsIWidget.h"
19 #include "nsPrintDialogUtil.h"
20 #include "nsIPrintSettings.h"
21 #include "nsIWebBrowserChrome.h"
22 #include "nsServiceManagerUtils.h"
23 #include "nsPIDOMWindow.h"
24 #include "nsQueryObject.h"
25 #include "WidgetUtils.h"
28 static const char* kPageSetupDialogURL
=
29 "chrome://global/content/printPageSetup.xhtml";
31 using namespace mozilla
;
32 using namespace mozilla::widget
;
40 ParamBlock() { mBlock
= 0; }
41 ~ParamBlock() { NS_IF_RELEASE(mBlock
); }
43 return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID
, &mBlock
);
45 nsIDialogParamBlock
* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
{
48 operator nsIDialogParamBlock
* const() { return mBlock
; }
51 nsIDialogParamBlock
* mBlock
;
54 NS_IMPL_ISUPPORTS(nsPrintDialogServiceWin
, nsIPrintDialogService
)
56 nsPrintDialogServiceWin::nsPrintDialogServiceWin() {}
58 nsPrintDialogServiceWin::~nsPrintDialogServiceWin() {}
61 nsPrintDialogServiceWin::Init() {
63 mWatcher
= do_GetService(NS_WINDOWWATCHER_CONTRACTID
, &rv
);
68 nsPrintDialogServiceWin::ShowPrintDialog(mozIDOMWindowProxy
* aParent
,
70 nsIPrintSettings
* aSettings
) {
71 NS_ENSURE_ARG(aParent
);
72 RefPtr
<nsIWidget
> parentWidget
=
73 WidgetUtils::DOMWindowToWidget(nsPIDOMWindowOuter::From(aParent
));
75 ScopedRtlShimWindow
shim(parentWidget
.get());
76 NS_ASSERTION(shim
.get(), "Couldn't get native window for PRint Dialog!");
78 return NativeShowPrintDialog(shim
.get(), aHaveSelection
, aSettings
);
82 nsPrintDialogServiceWin::ShowPageSetupDialog(mozIDOMWindowProxy
* aParent
,
83 nsIPrintSettings
* aNSSettings
) {
84 NS_ENSURE_ARG(aParent
);
85 NS_ENSURE_ARG(aNSSettings
);
88 nsresult rv
= block
.Init();
89 if (NS_FAILED(rv
)) return rv
;
92 rv
= DoDialog(aParent
, block
, aNSSettings
, kPageSetupDialogURL
);
94 // if aWebBrowserPrint is not null then we are printing
95 // so we want to pass back NS_ERROR_ABORT on cancel
96 if (NS_SUCCEEDED(rv
)) {
98 block
->GetInt(0, &status
);
99 return status
== 0 ? NS_ERROR_ABORT
: NS_OK
;
102 // We don't call nsPrintSettingsService::MaybeSavePrintSettingsToPrefs here
103 // since it's called for us in printPageSetup.js. Maybe we should move that
104 // call here for consistency with the other platforms though?
109 nsresult
nsPrintDialogServiceWin::DoDialog(mozIDOMWindowProxy
* aParent
,
110 nsIDialogParamBlock
* aParamBlock
,
111 nsIPrintSettings
* aPS
,
112 const char* aChromeURL
) {
113 NS_ENSURE_ARG(aParamBlock
);
115 NS_ENSURE_ARG(aChromeURL
);
117 if (!mWatcher
) return NS_ERROR_FAILURE
;
119 // get a parent, if at all possible
120 // (though we'd rather this didn't fail, it's OK if it does. so there's
121 // no failure or null check.)
122 // retain ownership for method lifetime
123 nsCOMPtr
<mozIDOMWindowProxy
> activeParent
;
125 mWatcher
->GetActiveWindow(getter_AddRefs(activeParent
));
126 aParent
= activeParent
;
129 // create a nsIMutableArray of the parameters
130 // being passed to the window
131 nsCOMPtr
<nsIMutableArray
> array
= nsArray::Create();
133 nsCOMPtr
<nsISupports
> psSupports(do_QueryInterface(aPS
));
134 NS_ASSERTION(psSupports
, "PrintSettings must be a supports");
135 array
->AppendElement(psSupports
);
137 nsCOMPtr
<nsISupports
> blkSupps(do_QueryInterface(aParamBlock
));
138 NS_ASSERTION(blkSupps
, "IOBlk must be a supports");
139 array
->AppendElement(blkSupps
);
141 nsCOMPtr
<mozIDOMWindowProxy
> dialog
;
142 nsresult rv
= mWatcher
->OpenWindow(
143 aParent
, nsDependentCString(aChromeURL
), "_blank"_ns
,
144 "centerscreen,chrome,modal,titlebar"_ns
, array
, getter_AddRefs(dialog
));