Backed out changeset a5ff95602119 (bug 1905021) as requested for causing accessibilit...
[gecko.git] / widget / windows / nsPrintDialogWin.cpp
blob35ea52b17b55d379db3e0c3f524ce58bfa4ccc15
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 "nsComponentManagerUtils.h"
10 #include "nsCOMPtr.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"
26 #include "WinUtils.h"
28 static const char* kPageSetupDialogURL =
29 "chrome://global/content/printPageSetup.xhtml";
31 using namespace mozilla;
32 using namespace mozilla::widget;
34 /**
35 * ParamBlock
38 class ParamBlock {
39 public:
40 ParamBlock() { mBlock = 0; }
41 ~ParamBlock() { NS_IF_RELEASE(mBlock); }
42 nsresult Init() {
43 return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
45 nsIDialogParamBlock* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
46 return mBlock;
48 operator nsIDialogParamBlock* const() { return mBlock; }
50 private:
51 nsIDialogParamBlock* mBlock;
54 NS_IMPL_ISUPPORTS(nsPrintDialogServiceWin, nsIPrintDialogService)
56 nsPrintDialogServiceWin::nsPrintDialogServiceWin() {}
58 nsPrintDialogServiceWin::~nsPrintDialogServiceWin() {}
60 NS_IMETHODIMP
61 nsPrintDialogServiceWin::Init() {
62 nsresult rv;
63 mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
64 return rv;
67 NS_IMETHODIMP
68 nsPrintDialogServiceWin::ShowPrintDialog(mozIDOMWindowProxy* aParent,
69 bool aHaveSelection,
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);
81 NS_IMETHODIMP
82 nsPrintDialogServiceWin::ShowPageSetupDialog(mozIDOMWindowProxy* aParent,
83 nsIPrintSettings* aNSSettings) {
84 NS_ENSURE_ARG(aParent);
85 NS_ENSURE_ARG(aNSSettings);
87 ParamBlock block;
88 nsresult rv = block.Init();
89 if (NS_FAILED(rv)) return rv;
91 block->SetInt(0, 0);
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)) {
97 int32_t status;
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?
106 return rv;
109 nsresult nsPrintDialogServiceWin::DoDialog(mozIDOMWindowProxy* aParent,
110 nsIDialogParamBlock* aParamBlock,
111 nsIPrintSettings* aPS,
112 const char* aChromeURL) {
113 NS_ENSURE_ARG(aParamBlock);
114 NS_ENSURE_ARG(aPS);
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;
124 if (!aParent) {
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));
146 return rv;