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"
10 #include "nsIBaseWindow.h"
11 #include "nsIBrowserChild.h"
12 #include "nsIDialogParamBlock.h"
13 #include "nsIDocShell.h"
14 #include "nsIEmbeddingSiteWindow.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 "nsPIDOMWindow.h"
23 #include "nsQueryObject.h"
25 static const char* kPageSetupDialogURL
=
26 "chrome://global/content/printPageSetup.xhtml";
28 using namespace mozilla
;
29 using namespace mozilla::widget
;
37 ParamBlock() { mBlock
= 0; }
38 ~ParamBlock() { NS_IF_RELEASE(mBlock
); }
40 return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID
, &mBlock
);
42 nsIDialogParamBlock
* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN
{
45 operator nsIDialogParamBlock
* const() { return mBlock
; }
48 nsIDialogParamBlock
* mBlock
;
51 NS_IMPL_ISUPPORTS(nsPrintDialogServiceWin
, nsIPrintDialogService
)
53 nsPrintDialogServiceWin::nsPrintDialogServiceWin() {}
55 nsPrintDialogServiceWin::~nsPrintDialogServiceWin() {}
58 nsPrintDialogServiceWin::Init() {
60 mWatcher
= do_GetService(NS_WINDOWWATCHER_CONTRACTID
, &rv
);
65 nsPrintDialogServiceWin::ShowPrintDialog(mozIDOMWindowProxy
* aParent
,
67 nsIPrintSettings
* aSettings
) {
68 NS_ENSURE_ARG(aParent
);
69 HWND hWnd
= GetHWNDForDOMWindow(aParent
);
70 NS_ASSERTION(hWnd
, "Couldn't get native window for PRint Dialog!");
72 return NativeShowPrintDialog(hWnd
, aHaveSelection
, aSettings
);
76 nsPrintDialogServiceWin::ShowPageSetupDialog(mozIDOMWindowProxy
* aParent
,
77 nsIPrintSettings
* aNSSettings
) {
78 NS_ENSURE_ARG(aParent
);
79 NS_ENSURE_ARG(aNSSettings
);
82 nsresult rv
= block
.Init();
83 if (NS_FAILED(rv
)) return rv
;
86 rv
= DoDialog(aParent
, block
, aNSSettings
, kPageSetupDialogURL
);
88 // if aWebBrowserPrint is not null then we are printing
89 // so we want to pass back NS_ERROR_ABORT on cancel
90 if (NS_SUCCEEDED(rv
)) {
92 block
->GetInt(0, &status
);
93 return status
== 0 ? NS_ERROR_ABORT
: NS_OK
;
96 // We don't call nsPrintSettingsService::SavePrintSettingsToPrefs here since
97 // it's called for us in printPageSetup.js. Maybe we should move that call
98 // here for consistency with the other platforms though?
103 nsresult
nsPrintDialogServiceWin::DoDialog(mozIDOMWindowProxy
* aParent
,
104 nsIDialogParamBlock
* aParamBlock
,
105 nsIPrintSettings
* aPS
,
106 const char* aChromeURL
) {
107 NS_ENSURE_ARG(aParamBlock
);
109 NS_ENSURE_ARG(aChromeURL
);
111 if (!mWatcher
) return NS_ERROR_FAILURE
;
113 // get a parent, if at all possible
114 // (though we'd rather this didn't fail, it's OK if it does. so there's
115 // no failure or null check.)
116 // retain ownership for method lifetime
117 nsCOMPtr
<mozIDOMWindowProxy
> activeParent
;
119 mWatcher
->GetActiveWindow(getter_AddRefs(activeParent
));
120 aParent
= activeParent
;
123 // create a nsIMutableArray of the parameters
124 // being passed to the window
125 nsCOMPtr
<nsIMutableArray
> array
= nsArray::Create();
127 nsCOMPtr
<nsISupports
> psSupports(do_QueryInterface(aPS
));
128 NS_ASSERTION(psSupports
, "PrintSettings must be a supports");
129 array
->AppendElement(psSupports
);
131 nsCOMPtr
<nsISupports
> blkSupps(do_QueryInterface(aParamBlock
));
132 NS_ASSERTION(blkSupps
, "IOBlk must be a supports");
133 array
->AppendElement(blkSupps
);
135 nsCOMPtr
<mozIDOMWindowProxy
> dialog
;
136 nsresult rv
= mWatcher
->OpenWindow(
137 aParent
, nsDependentCString(aChromeURL
), "_blank"_ns
,
138 "centerscreen,chrome,modal,titlebar"_ns
, array
, getter_AddRefs(dialog
));
143 HWND
nsPrintDialogServiceWin::GetHWNDForDOMWindow(mozIDOMWindowProxy
* aWindow
) {
144 nsCOMPtr
<nsIWebBrowserChrome
> chrome
;
146 // We might be embedded so check this path first
148 nsCOMPtr
<mozIDOMWindowProxy
> fosterParent
;
149 // it will be a dependent window. try to find a foster parent.
151 mWatcher
->GetActiveWindow(getter_AddRefs(fosterParent
));
152 aWindow
= fosterParent
;
154 mWatcher
->GetChromeForWindow(aWindow
, getter_AddRefs(chrome
));
158 nsCOMPtr
<nsIEmbeddingSiteWindow
> site(do_QueryInterface(chrome
));
161 site
->GetSiteWindow(reinterpret_cast<void**>(&w
));
166 // Now we might be the Browser so check this path
167 nsCOMPtr
<nsPIDOMWindowOuter
> window
= nsPIDOMWindowOuter::From(aWindow
);
169 nsCOMPtr
<nsIWebBrowserChrome
> webBrowserChrome
=
170 window
->GetWebBrowserChrome();
171 if (!webBrowserChrome
) return nullptr;
173 nsCOMPtr
<nsIBaseWindow
> baseWin(do_QueryInterface(webBrowserChrome
));
174 if (!baseWin
) return nullptr;
176 nsCOMPtr
<nsIWidget
> widget
;
177 baseWin
->GetMainWidget(getter_AddRefs(widget
));
178 if (!widget
) return nullptr;
180 return (HWND
)widget
->GetNativeData(NS_NATIVE_TMP_WINDOW
);