michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCOMPtr.h" michael@0: michael@0: #include "nsPrintingPromptService.h" michael@0: #include "nsIPrintingPromptService.h" michael@0: #include "nsIFactory.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsIEmbeddingSiteWindow.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIWebBrowserChrome.h" michael@0: #include "nsIWindowWatcher.h" michael@0: #include "nsPrintDialogUtil.h" michael@0: michael@0: // Printing Progress Includes michael@0: #include "nsPrintProgress.h" michael@0: #include "nsPrintProgressParams.h" michael@0: #include "nsIWebProgressListener.h" michael@0: michael@0: // XP Dialog includes michael@0: #include "nsIDialogParamBlock.h" michael@0: #include "nsISupportsUtils.h" michael@0: #include "nsISupportsArray.h" michael@0: michael@0: // Includes need to locate the native Window michael@0: #include "nsIWidget.h" michael@0: #include "nsIBaseWindow.h" michael@0: #include "nsIWebBrowserChrome.h" michael@0: #include "nsIDocShellTreeOwner.h" michael@0: #include "nsIDocShellTreeItem.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: michael@0: michael@0: static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul"; michael@0: static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul"; michael@0: static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul"; michael@0: michael@0: /**************************************************************** michael@0: ************************* ParamBlock *************************** michael@0: ****************************************************************/ michael@0: michael@0: class ParamBlock { michael@0: michael@0: public: michael@0: ParamBlock() michael@0: { michael@0: mBlock = 0; michael@0: } michael@0: ~ParamBlock() michael@0: { michael@0: NS_IF_RELEASE(mBlock); michael@0: } michael@0: nsresult Init() { michael@0: return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock); michael@0: } michael@0: nsIDialogParamBlock * operator->() const { return mBlock; } michael@0: operator nsIDialogParamBlock * const () { return mBlock; } michael@0: michael@0: private: michael@0: nsIDialogParamBlock *mBlock; michael@0: }; michael@0: michael@0: //***************************************************************************** michael@0: michael@0: NS_IMPL_ISUPPORTS(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener) michael@0: michael@0: nsPrintingPromptService::nsPrintingPromptService() michael@0: { michael@0: } michael@0: michael@0: //----------------------------------------------------------- michael@0: nsPrintingPromptService::~nsPrintingPromptService() michael@0: { michael@0: } michael@0: michael@0: //----------------------------------------------------------- michael@0: nsresult michael@0: nsPrintingPromptService::Init() michael@0: { michael@0: nsresult rv; michael@0: mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv); michael@0: return rv; michael@0: } michael@0: michael@0: //----------------------------------------------------------- michael@0: HWND michael@0: nsPrintingPromptService::GetHWNDForDOMWindow(nsIDOMWindow *aWindow) michael@0: { michael@0: nsCOMPtr chrome; michael@0: michael@0: // We might be embedded so check this path first michael@0: if (mWatcher) { michael@0: nsCOMPtr fosterParent; michael@0: if (!aWindow) michael@0: { // it will be a dependent window. try to find a foster parent. michael@0: mWatcher->GetActiveWindow(getter_AddRefs(fosterParent)); michael@0: aWindow = fosterParent; michael@0: } michael@0: mWatcher->GetChromeForWindow(aWindow, getter_AddRefs(chrome)); michael@0: } michael@0: michael@0: if (chrome) { michael@0: nsCOMPtr site(do_QueryInterface(chrome)); michael@0: if (site) michael@0: { michael@0: HWND w; michael@0: site->GetSiteWindow(reinterpret_cast(&w)); michael@0: return w; michael@0: } michael@0: } michael@0: michael@0: // Now we might be the Browser so check this path michael@0: nsCOMPtr window(do_QueryInterface(aWindow)); michael@0: michael@0: nsCOMPtr treeItem = michael@0: do_QueryInterface(window->GetDocShell()); michael@0: if (!treeItem) return nullptr; michael@0: michael@0: nsCOMPtr treeOwner; michael@0: treeItem->GetTreeOwner(getter_AddRefs(treeOwner)); michael@0: if (!treeOwner) return nullptr; michael@0: michael@0: nsCOMPtr webBrowserChrome(do_GetInterface(treeOwner)); michael@0: if (!webBrowserChrome) return nullptr; michael@0: michael@0: nsCOMPtr baseWin(do_QueryInterface(webBrowserChrome)); michael@0: if (!baseWin) return nullptr; michael@0: michael@0: nsCOMPtr widget; michael@0: baseWin->GetMainWidget(getter_AddRefs(widget)); michael@0: if (!widget) return nullptr; michael@0: michael@0: return (HWND)widget->GetNativeData(NS_NATIVE_TMP_WINDOW); michael@0: michael@0: } michael@0: michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // nsIPrintingPrompt michael@0: michael@0: //----------------------------------------------------------- michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings) michael@0: { michael@0: NS_ENSURE_ARG(parent); michael@0: michael@0: HWND hWnd = GetHWNDForDOMWindow(parent); michael@0: NS_ASSERTION(hWnd, "Couldn't get native window for PRint Dialog!"); michael@0: michael@0: return NativeShowPrintDialog(hWnd, webBrowserPrint, printSettings); michael@0: } michael@0: michael@0: michael@0: /* void showProgress (in nsIDOMWindow parent, in nsIWebBrowserPrint webBrowserPrint, in nsIPrintSettings printSettings, in nsIObserver openDialogObserver, in boolean isForPrinting, out nsIWebProgressListener webProgressListener, out nsIPrintProgressParams printProgressParams, out boolean notifyOnOpen); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowProgress(nsIDOMWindow* parent, michael@0: nsIWebBrowserPrint* webBrowserPrint, // ok to be null michael@0: nsIPrintSettings* printSettings, // ok to be null michael@0: nsIObserver* openDialogObserver, // ok to be null michael@0: bool isForPrinting, michael@0: nsIWebProgressListener** webProgressListener, michael@0: nsIPrintProgressParams** printProgressParams, michael@0: bool* notifyOnOpen) michael@0: { michael@0: NS_ENSURE_ARG(webProgressListener); michael@0: NS_ENSURE_ARG(printProgressParams); michael@0: NS_ENSURE_ARG(notifyOnOpen); michael@0: michael@0: *notifyOnOpen = false; michael@0: if (mPrintProgress) { michael@0: *webProgressListener = nullptr; michael@0: *printProgressParams = nullptr; michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsPrintProgress* prtProgress = new nsPrintProgress(); michael@0: mPrintProgress = prtProgress; michael@0: mWebProgressListener = prtProgress; michael@0: michael@0: nsCOMPtr prtProgressParams = new nsPrintProgressParams(); michael@0: michael@0: nsCOMPtr parentWindow = parent; michael@0: michael@0: if (mWatcher && !parentWindow) { michael@0: mWatcher->GetActiveWindow(getter_AddRefs(parentWindow)); michael@0: } michael@0: michael@0: if (parentWindow) { michael@0: mPrintProgress->OpenProgressDialog(parentWindow, michael@0: isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL, michael@0: prtProgressParams, openDialogObserver, notifyOnOpen); michael@0: } michael@0: michael@0: prtProgressParams.forget(printProgressParams); michael@0: NS_ADDREF(*webProgressListener = this); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs) michael@0: { michael@0: NS_ENSURE_ARG(printSettings); michael@0: michael@0: ParamBlock block; michael@0: nsresult rv = block.Init(); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: block->SetInt(0, 0); michael@0: rv = DoDialog(parent, block, printSettings, kPageSetupDialogURL); michael@0: michael@0: // if aWebBrowserPrint is not null then we are printing michael@0: // so we want to pass back NS_ERROR_ABORT on cancel michael@0: if (NS_SUCCEEDED(rv)) michael@0: { michael@0: int32_t status; michael@0: block->GetInt(0, &status); michael@0: return status == 0?NS_ERROR_ABORT:NS_OK; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const char16_t *printerName, nsIPrintSettings *printSettings) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: //----------------------------------------------------------- michael@0: // Helper to Fly XP Dialog michael@0: nsresult michael@0: nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent, michael@0: nsIDialogParamBlock *aParamBlock, michael@0: nsIPrintSettings* aPS, michael@0: const char *aChromeURL) michael@0: { michael@0: NS_ENSURE_ARG(aParamBlock); michael@0: NS_ENSURE_ARG(aPS); michael@0: NS_ENSURE_ARG(aChromeURL); michael@0: michael@0: if (!mWatcher) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv = NS_OK; michael@0: michael@0: // get a parent, if at all possible michael@0: // (though we'd rather this didn't fail, it's OK if it does. so there's michael@0: // no failure or null check.) michael@0: nsCOMPtr activeParent; // retain ownership for method lifetime michael@0: if (!aParent) michael@0: { michael@0: mWatcher->GetActiveWindow(getter_AddRefs(activeParent)); michael@0: aParent = activeParent; michael@0: } michael@0: michael@0: // create a nsISupportsArray of the parameters michael@0: // being passed to the window michael@0: nsCOMPtr array; michael@0: NS_NewISupportsArray(getter_AddRefs(array)); michael@0: if (!array) return NS_ERROR_FAILURE; michael@0: michael@0: nsCOMPtr psSupports(do_QueryInterface(aPS)); michael@0: NS_ASSERTION(psSupports, "PrintSettings must be a supports"); michael@0: array->AppendElement(psSupports); michael@0: michael@0: nsCOMPtr blkSupps(do_QueryInterface(aParamBlock)); michael@0: NS_ASSERTION(blkSupps, "IOBlk must be a supports"); michael@0: array->AppendElement(blkSupps); michael@0: michael@0: nsCOMPtr arguments(do_QueryInterface(array)); michael@0: NS_ASSERTION(array, "array must be a supports"); michael@0: michael@0: michael@0: nsCOMPtr dialog; michael@0: rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank", michael@0: "centerscreen,chrome,modal,titlebar", arguments, michael@0: getter_AddRefs(dialog)); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////// michael@0: // nsIWebProgressListener michael@0: ////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in nsresult aStatus); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t aStateFlags, nsresult aStatus) michael@0: { michael@0: if ((aStateFlags & STATE_STOP) && mWebProgressListener) michael@0: { michael@0: mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); michael@0: if (mPrintProgress) michael@0: { michael@0: mPrintProgress->CloseProgressDialog(true); michael@0: } michael@0: mPrintProgress = nullptr; michael@0: mWebProgressListener = nullptr; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, int32_t aCurSelfProgress, int32_t aMaxSelfProgress, int32_t aCurTotalProgress, int32_t aMaxTotalProgress) michael@0: { michael@0: if (mWebProgressListener) michael@0: { michael@0: return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress); michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, uint32_t aFlags) michael@0: { michael@0: if (mWebProgressListener) michael@0: { michael@0: return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags); michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const char16_t *aMessage) michael@0: { michael@0: if (mWebProgressListener) michael@0: { michael@0: return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */ michael@0: NS_IMETHODIMP michael@0: nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, uint32_t state) michael@0: { michael@0: if (mWebProgressListener) michael@0: { michael@0: return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state); michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: