Bug 1793629 - Implement attention indicator for the unified extensions button, r...
[gecko.git] / widget / windows / nsPrintDialogWin.cpp
blob941cc91774626497813a5f916be0a060f4a05938
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"
8 #include "nsArray.h"
9 #include "nsCOMPtr.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;
31 /**
32 * ParamBlock
35 class ParamBlock {
36 public:
37 ParamBlock() { mBlock = 0; }
38 ~ParamBlock() { NS_IF_RELEASE(mBlock); }
39 nsresult Init() {
40 return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
42 nsIDialogParamBlock* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
43 return mBlock;
45 operator nsIDialogParamBlock* const() { return mBlock; }
47 private:
48 nsIDialogParamBlock* mBlock;
51 NS_IMPL_ISUPPORTS(nsPrintDialogServiceWin, nsIPrintDialogService)
53 nsPrintDialogServiceWin::nsPrintDialogServiceWin() {}
55 nsPrintDialogServiceWin::~nsPrintDialogServiceWin() {}
57 NS_IMETHODIMP
58 nsPrintDialogServiceWin::Init() {
59 nsresult rv;
60 mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
61 return rv;
64 NS_IMETHODIMP
65 nsPrintDialogServiceWin::ShowPrintDialog(mozIDOMWindowProxy* aParent,
66 bool aHaveSelection,
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);
75 NS_IMETHODIMP
76 nsPrintDialogServiceWin::ShowPageSetupDialog(mozIDOMWindowProxy* aParent,
77 nsIPrintSettings* aNSSettings) {
78 NS_ENSURE_ARG(aParent);
79 NS_ENSURE_ARG(aNSSettings);
81 ParamBlock block;
82 nsresult rv = block.Init();
83 if (NS_FAILED(rv)) return rv;
85 block->SetInt(0, 0);
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)) {
91 int32_t status;
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?
100 return rv;
103 nsresult nsPrintDialogServiceWin::DoDialog(mozIDOMWindowProxy* aParent,
104 nsIDialogParamBlock* aParamBlock,
105 nsIPrintSettings* aPS,
106 const char* aChromeURL) {
107 NS_ENSURE_ARG(aParamBlock);
108 NS_ENSURE_ARG(aPS);
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;
118 if (!aParent) {
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));
140 return rv;
143 HWND nsPrintDialogServiceWin::GetHWNDForDOMWindow(mozIDOMWindowProxy* aWindow) {
144 nsCOMPtr<nsIWebBrowserChrome> chrome;
146 // We might be embedded so check this path first
147 if (mWatcher) {
148 nsCOMPtr<mozIDOMWindowProxy> fosterParent;
149 // it will be a dependent window. try to find a foster parent.
150 if (!aWindow) {
151 mWatcher->GetActiveWindow(getter_AddRefs(fosterParent));
152 aWindow = fosterParent;
154 mWatcher->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
157 if (chrome) {
158 nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
159 if (site) {
160 HWND w;
161 site->GetSiteWindow(reinterpret_cast<void**>(&w));
162 return 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);