2017-10-19 05:04:13 +03:00
|
|
|
/* -*- 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 "nsPrintDialogWin.h"
|
|
|
|
|
2017-10-19 05:04:30 +03:00
|
|
|
#include "nsArray.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2017-10-19 05:04:13 +03:00
|
|
|
#include "nsIBaseWindow.h"
|
2017-10-19 05:04:30 +03:00
|
|
|
#include "nsIDialogParamBlock.h"
|
|
|
|
#include "nsIDocShellTreeOwner.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIEmbeddingSiteWindow.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2017-10-19 05:04:13 +03:00
|
|
|
#include "nsIPrintSettings.h"
|
|
|
|
#include "nsIWebBrowserChrome.h"
|
2017-10-19 05:04:30 +03:00
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsPrintDialogUtil.h"
|
|
|
|
#include "nsIPrintSettings.h"
|
|
|
|
#include "nsIWebBrowserChrome.h"
|
|
|
|
#include "nsQueryObject.h"
|
|
|
|
|
2017-10-19 05:04:50 +03:00
|
|
|
static const char* kPageSetupDialogURL =
|
|
|
|
"chrome://global/content/printPageSetup.xul";
|
2017-10-19 05:04:13 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2017-10-19 05:04:30 +03:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ParamBlock
|
|
|
|
*/
|
|
|
|
|
2017-10-19 05:04:50 +03:00
|
|
|
class ParamBlock {
|
2017-10-19 05:04:30 +03:00
|
|
|
public:
|
2017-10-19 05:04:50 +03:00
|
|
|
ParamBlock() { mBlock = 0; }
|
|
|
|
~ParamBlock() { NS_IF_RELEASE(mBlock); }
|
|
|
|
nsresult Init() {
|
|
|
|
return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
|
2017-10-19 05:04:30 +03:00
|
|
|
}
|
2017-10-19 05:04:50 +03:00
|
|
|
nsIDialogParamBlock* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
|
|
|
|
return mBlock;
|
2017-10-19 05:04:30 +03:00
|
|
|
}
|
2017-10-19 05:04:50 +03:00
|
|
|
operator nsIDialogParamBlock* const() { return mBlock; }
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
private:
|
2017-10-19 05:04:50 +03:00
|
|
|
nsIDialogParamBlock* mBlock;
|
2017-10-19 05:04:30 +03:00
|
|
|
};
|
2017-10-19 05:04:13 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsPrintDialogServiceWin, nsIPrintDialogService)
|
|
|
|
|
2017-10-19 05:04:50 +03:00
|
|
|
nsPrintDialogServiceWin::nsPrintDialogServiceWin() {}
|
2017-10-19 05:04:13 +03:00
|
|
|
|
2017-10-19 05:04:50 +03:00
|
|
|
nsPrintDialogServiceWin::~nsPrintDialogServiceWin() {}
|
2017-10-19 05:04:13 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrintDialogServiceWin::Init() {
|
2017-10-19 05:04:30 +03:00
|
|
|
nsresult rv;
|
|
|
|
mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
|
|
|
|
return rv;
|
2017-10-19 05:04:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-19 05:04:50 +03:00
|
|
|
nsPrintDialogServiceWin::Show(nsPIDOMWindowOuter* aParent,
|
|
|
|
nsIPrintSettings* aSettings,
|
|
|
|
nsIWebBrowserPrint* aWebBrowserPrint) {
|
2017-10-19 05:04:30 +03:00
|
|
|
NS_ENSURE_ARG(aParent);
|
|
|
|
HWND hWnd = GetHWNDForDOMWindow(aParent);
|
|
|
|
NS_ASSERTION(hWnd, "Couldn't get native window for PRint Dialog!");
|
|
|
|
|
|
|
|
return NativeShowPrintDialog(hWnd, aWebBrowserPrint, aSettings);
|
2017-10-19 05:04:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-10-19 05:04:50 +03:00
|
|
|
nsPrintDialogServiceWin::ShowPageSetup(nsPIDOMWindowOuter* aParent,
|
|
|
|
nsIPrintSettings* aNSSettings) {
|
2017-10-19 05:04:30 +03:00
|
|
|
NS_ENSURE_ARG(aParent);
|
|
|
|
NS_ENSURE_ARG(aNSSettings);
|
|
|
|
|
|
|
|
ParamBlock block;
|
|
|
|
nsresult rv = block.Init();
|
2017-10-19 05:04:50 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
block->SetInt(0, 0);
|
|
|
|
rv = DoDialog(aParent, block, aNSSettings, kPageSetupDialogURL);
|
|
|
|
|
|
|
|
// if aWebBrowserPrint is not null then we are printing
|
|
|
|
// so we want to pass back NS_ERROR_ABORT on cancel
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
int32_t status;
|
|
|
|
block->GetInt(0, &status);
|
2017-10-19 05:04:50 +03:00
|
|
|
return status == 0 ? NS_ERROR_ABORT : NS_OK;
|
2017-10-19 05:04:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-19 05:04:50 +03:00
|
|
|
nsresult nsPrintDialogServiceWin::DoDialog(mozIDOMWindowProxy* aParent,
|
|
|
|
nsIDialogParamBlock* aParamBlock,
|
2017-10-19 05:04:30 +03:00
|
|
|
nsIPrintSettings* aPS,
|
2017-10-19 05:04:50 +03:00
|
|
|
const char* aChromeURL) {
|
2017-10-19 05:04:30 +03:00
|
|
|
NS_ENSURE_ARG(aParamBlock);
|
|
|
|
NS_ENSURE_ARG(aPS);
|
|
|
|
NS_ENSURE_ARG(aChromeURL);
|
|
|
|
|
|
|
|
if (!mWatcher) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// get a parent, if at all possible
|
|
|
|
// (though we'd rather this didn't fail, it's OK if it does. so there's
|
|
|
|
// no failure or null check.)
|
|
|
|
// retain ownership for method lifetime
|
|
|
|
nsCOMPtr<mozIDOMWindowProxy> activeParent;
|
|
|
|
if (!aParent) {
|
|
|
|
mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
|
|
|
|
aParent = activeParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a nsIMutableArray of the parameters
|
|
|
|
// being passed to the window
|
|
|
|
nsCOMPtr<nsIMutableArray> array = nsArray::Create();
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
|
|
|
|
NS_ASSERTION(psSupports, "PrintSettings must be a supports");
|
2017-10-21 17:53:02 +03:00
|
|
|
array->AppendElement(psSupports);
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
|
|
|
|
NS_ASSERTION(blkSupps, "IOBlk must be a supports");
|
2017-10-21 17:53:02 +03:00
|
|
|
array->AppendElement(blkSupps);
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
nsCOMPtr<mozIDOMWindowProxy> dialog;
|
|
|
|
nsresult rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
|
|
|
|
"centerscreen,chrome,modal,titlebar",
|
|
|
|
array, getter_AddRefs(dialog));
|
|
|
|
|
2017-10-19 05:04:50 +03:00
|
|
|
return rv;
|
2017-10-19 05:04:30 +03:00
|
|
|
}
|
|
|
|
|
2017-10-19 05:04:50 +03:00
|
|
|
HWND nsPrintDialogServiceWin::GetHWNDForDOMWindow(mozIDOMWindowProxy* aWindow) {
|
2017-10-19 05:04:30 +03:00
|
|
|
nsCOMPtr<nsIWebBrowserChrome> chrome;
|
|
|
|
|
|
|
|
// We might be embedded so check this path first
|
|
|
|
if (mWatcher) {
|
|
|
|
nsCOMPtr<mozIDOMWindowProxy> fosterParent;
|
|
|
|
// it will be a dependent window. try to find a foster parent.
|
|
|
|
if (!aWindow) {
|
2017-10-19 05:04:50 +03:00
|
|
|
mWatcher->GetActiveWindow(getter_AddRefs(fosterParent));
|
|
|
|
aWindow = fosterParent;
|
2017-10-19 05:04:30 +03:00
|
|
|
}
|
|
|
|
mWatcher->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chrome) {
|
|
|
|
nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
|
|
|
|
if (site) {
|
|
|
|
HWND w;
|
2017-10-19 05:04:50 +03:00
|
|
|
site->GetSiteWindow(reinterpret_cast<void**>(&w));
|
2017-10-19 05:04:30 +03:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we might be the Browser so check this path
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = nsPIDOMWindowOuter::From(aWindow);
|
|
|
|
|
2018-10-03 22:52:01 +03:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> treeItem = window->GetDocShell();
|
2017-10-19 05:04:50 +03:00
|
|
|
if (!treeItem) return nullptr;
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
|
|
|
|
treeItem->GetTreeOwner(getter_AddRefs(treeOwner));
|
2017-10-19 05:04:50 +03:00
|
|
|
if (!treeOwner) return nullptr;
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome(do_GetInterface(treeOwner));
|
2017-10-19 05:04:50 +03:00
|
|
|
if (!webBrowserChrome) return nullptr;
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(webBrowserChrome));
|
2017-10-19 05:04:50 +03:00
|
|
|
if (!baseWin) return nullptr;
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWidget> widget;
|
|
|
|
baseWin->GetMainWidget(getter_AddRefs(widget));
|
2017-10-19 05:04:50 +03:00
|
|
|
if (!widget) return nullptr;
|
2017-10-19 05:04:30 +03:00
|
|
|
|
|
|
|
return (HWND)widget->GetNativeData(NS_NATIVE_TMP_WINDOW);
|
2017-10-19 05:04:13 +03:00
|
|
|
}
|