Back to index

lightning-sunbird  0.9+nobinonly
nsPrintingPromptService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 
00041 #include "nsPrintingPromptService.h"
00042 #include "nsIPrintingPromptService.h"
00043 #include "nsIFactory.h"
00044 #include "nsIDOMWindow.h"
00045 #include "nsReadableUtils.h"
00046 #include "nsIEmbeddingSiteWindow.h"
00047 #include "nsIServiceManager.h"
00048 #include "nsIWebBrowserChrome.h"
00049 #include "nsIWindowWatcher.h"
00050 #include "nsPrintDialogUtil.h"
00051 
00052 // Printing Progress Includes
00053 #include "nsPrintProgress.h"
00054 #include "nsPrintProgressParams.h"
00055 #include "nsIWebProgressListener.h"
00056 
00057 // XP Dialog includes
00058 #include "nsIDialogParamBlock.h"
00059 #include "nsISupportsUtils.h"
00060 #include "nsISupportsArray.h"
00061 
00062 // Includes need to locate the native Window
00063 #include "nsIWidget.h"
00064 #include "nsIBaseWindow.h"
00065 #include "nsIWebBrowserChrome.h"
00066 #include "nsIDocShellTreeOwner.h"
00067 #include "nsIDocShellTreeItem.h"
00068 #include "nsIScriptGlobalObject.h"
00069 #include "nsIDocShell.h"
00070 #include "nsIInterfaceRequestorUtils.h"
00071 
00072 
00073 static const char *kPrintProgressDialogURL  = "chrome://global/content/printProgress.xul";
00074 static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
00075 static const char *kPageSetupDialogURL      = "chrome://global/content/printPageSetup.xul";
00076 
00077 // Static Data 
00078 static HINSTANCE gInstance;
00079 
00080 /****************************************************************
00081  ************************* ParamBlock ***************************
00082  ****************************************************************/
00083 
00084 class ParamBlock {
00085 
00086 public:
00087     ParamBlock() 
00088     {
00089         mBlock = 0;
00090     }
00091     ~ParamBlock() 
00092     {
00093         NS_IF_RELEASE(mBlock);
00094     }
00095     nsresult Init() {
00096       return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
00097     }
00098     nsIDialogParamBlock * operator->() const { return mBlock; }
00099     operator nsIDialogParamBlock * const ()  { return mBlock; }
00100 
00101 private:
00102     nsIDialogParamBlock *mBlock;
00103 };
00104 
00105 //*****************************************************************************   
00106 
00107 NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
00108 
00109 nsPrintingPromptService::nsPrintingPromptService() 
00110 {
00111 }
00112 
00113 //-----------------------------------------------------------
00114 nsPrintingPromptService::~nsPrintingPromptService()
00115 {
00116 }
00117 
00118 //-----------------------------------------------------------
00119 nsresult
00120 nsPrintingPromptService::Init()
00121 {
00122     nsresult rv;
00123     mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
00124     return rv;
00125 }
00126 
00127 //-----------------------------------------------------------
00128 HWND
00129 nsPrintingPromptService::GetHWNDForDOMWindow(nsIDOMWindow *aWindow)
00130 {
00131     nsCOMPtr<nsIWebBrowserChrome> chrome;
00132     HWND hWnd = NULL;
00133 
00134     // We might be embedded so check this path first
00135     if (mWatcher) {
00136         nsCOMPtr<nsIDOMWindow> fosterParent;
00137         if (!aWindow) 
00138         {   // it will be a dependent window. try to find a foster parent.
00139             mWatcher->GetActiveWindow(getter_AddRefs(fosterParent));
00140             aWindow = fosterParent;
00141         }
00142         mWatcher->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
00143     }
00144 
00145     if (chrome) {
00146         nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
00147         if (site) 
00148         {
00149             HWND w;
00150             site->GetSiteWindow(reinterpret_cast<void **>(&w));
00151             return w;
00152         }
00153     }
00154 
00155     // Now we might be the Browser so check this path
00156     nsCOMPtr<nsIScriptGlobalObject> scriptGlobal(do_QueryInterface(aWindow));
00157 
00158     nsCOMPtr<nsIDocShellTreeItem> treeItem =
00159         do_QueryInterface(scriptGlobal->GetDocShell());
00160     if (!treeItem) return nsnull;
00161 
00162     nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
00163     treeItem->GetTreeOwner(getter_AddRefs(treeOwner));
00164     if (!treeOwner) return nsnull;
00165 
00166     nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome(do_GetInterface(treeOwner));
00167     if (!webBrowserChrome) return nsnull;
00168 
00169     nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(webBrowserChrome));
00170     if (!baseWin) return nsnull;
00171 
00172     nsCOMPtr<nsIWidget> widget;
00173     baseWin->GetMainWidget(getter_AddRefs(widget));
00174     if (!widget) return nsnull;
00175 
00176     return (HWND)widget->GetNativeData(NS_NATIVE_WINDOW);
00177 
00178 }
00179 
00180 
00182 // nsIPrintingPrompt
00183 
00184 //-----------------------------------------------------------
00185 NS_IMETHODIMP 
00186 nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
00187 {
00188     NS_ENSURE_ARG(parent);
00189 
00190     HWND hWnd = GetHWNDForDOMWindow(parent);
00191     NS_ASSERTION(hWnd, "Couldn't get native window for PRint Dialog!");
00192 
00193     return NativeShowPrintDialog(hWnd, webBrowserPrint, printSettings);
00194 }
00195 
00196 
00197 /* 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); */
00198 NS_IMETHODIMP 
00199 nsPrintingPromptService::ShowProgress(nsIDOMWindow*            parent, 
00200                                       nsIWebBrowserPrint*      webBrowserPrint,    // ok to be null
00201                                       nsIPrintSettings*        printSettings,      // ok to be null
00202                                       nsIObserver*             openDialogObserver, // ok to be null
00203                                       PRBool                   isForPrinting,
00204                                       nsIWebProgressListener** webProgressListener,
00205                                       nsIPrintProgressParams** printProgressParams,
00206                                       PRBool*                  notifyOnOpen)
00207 {
00208     NS_ENSURE_ARG(webProgressListener);
00209     NS_ENSURE_ARG(printProgressParams);
00210     NS_ENSURE_ARG(notifyOnOpen);
00211 
00212     *notifyOnOpen = PR_FALSE;
00213     if (mPrintProgress) 
00214     {
00215         *webProgressListener = nsnull;
00216         *printProgressParams = nsnull;
00217         return NS_ERROR_FAILURE;
00218     }
00219 
00220     nsPrintProgress* prtProgress = new nsPrintProgress();
00221     nsresult rv = prtProgress->QueryInterface(NS_GET_IID(nsIPrintProgress), (void**)getter_AddRefs(mPrintProgress));
00222     NS_ENSURE_SUCCESS(rv, rv);
00223 
00224     rv = prtProgress->QueryInterface(NS_GET_IID(nsIWebProgressListener), (void**)getter_AddRefs(mWebProgressListener));
00225     NS_ENSURE_SUCCESS(rv, rv);
00226 
00227     nsPrintProgressParams* prtProgressParams = new nsPrintProgressParams();
00228     rv = prtProgressParams->QueryInterface(NS_GET_IID(nsIPrintProgressParams), (void**)printProgressParams);
00229     NS_ENSURE_SUCCESS(rv, rv);
00230 
00231     if (printProgressParams) 
00232     {
00233         nsCOMPtr<nsIDOMWindowInternal> parentDOMIntl(do_QueryInterface(parent));
00234 
00235         if (mWatcher && !parentDOMIntl) 
00236         {
00237             nsCOMPtr<nsIDOMWindow> active;
00238             mWatcher->GetActiveWindow(getter_AddRefs(active));
00239             parentDOMIntl = do_QueryInterface(active);
00240         }
00241 
00242         if (parentDOMIntl) 
00243         {
00244             mPrintProgress->OpenProgressDialog(parentDOMIntl, 
00245                                                isForPrinting?kPrintProgressDialogURL:kPrtPrvProgressDialogURL, 
00246                                                *printProgressParams, openDialogObserver, notifyOnOpen);
00247         }
00248     }
00249 
00250     *webProgressListener = NS_STATIC_CAST(nsIWebProgressListener*, this);
00251     NS_ADDREF(*webProgressListener);
00252 
00253     return rv;
00254 }
00255 
00256 /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
00257 NS_IMETHODIMP 
00258 nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
00259 {
00260     NS_ENSURE_ARG(printSettings);
00261 
00262     ParamBlock block;
00263     nsresult rv = block.Init();
00264     if (NS_FAILED(rv))
00265       return rv;
00266 
00267     block->SetInt(0, 0);
00268     rv = DoDialog(parent, block, printSettings, kPageSetupDialogURL);
00269 
00270     // if aWebBrowserPrint is not null then we are printing
00271     // so we want to pass back NS_ERROR_ABORT on cancel
00272     if (NS_SUCCEEDED(rv)) 
00273     {
00274       PRInt32 status;
00275       block->GetInt(0, &status);
00276       return status == 0?NS_ERROR_ABORT:NS_OK;
00277     }
00278 
00279     return rv;
00280 }
00281 
00282 /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
00283 NS_IMETHODIMP 
00284 nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const PRUnichar *printerName, nsIPrintSettings *printSettings)
00285 {
00286     return NS_ERROR_NOT_IMPLEMENTED;
00287 }
00288 
00289 //-----------------------------------------------------------
00290 // Helper to Fly XP Dialog
00291 nsresult
00292 nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
00293                                   nsIDialogParamBlock *aParamBlock, 
00294                                   nsIPrintSettings* aPS,
00295                                   const char *aChromeURL)
00296 {
00297     NS_ENSURE_ARG(aParamBlock);
00298     NS_ENSURE_ARG(aPS);
00299     NS_ENSURE_ARG(aChromeURL);
00300 
00301     if (!mWatcher)
00302         return NS_ERROR_FAILURE;
00303 
00304     nsresult rv = NS_OK;
00305 
00306     // get a parent, if at all possible
00307     // (though we'd rather this didn't fail, it's OK if it does. so there's
00308     // no failure or null check.)
00309     nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
00310     if (!aParent) 
00311     {
00312         mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
00313         aParent = activeParent;
00314     }
00315 
00316     // create a nsISupportsArray of the parameters 
00317     // being passed to the window
00318     nsCOMPtr<nsISupportsArray> array;
00319     NS_NewISupportsArray(getter_AddRefs(array));
00320     if (!array) return NS_ERROR_FAILURE;
00321 
00322     nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
00323     NS_ASSERTION(psSupports, "PrintSettings must be a supports");
00324     array->AppendElement(psSupports);
00325 
00326     nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
00327     NS_ASSERTION(blkSupps, "IOBlk must be a supports");
00328     array->AppendElement(blkSupps);
00329 
00330     nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
00331     NS_ASSERTION(array, "array must be a supports");
00332 
00333 
00334     nsCOMPtr<nsIDOMWindow> dialog;
00335     rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
00336                               "centerscreen,chrome,modal,titlebar", arguments,
00337                               getter_AddRefs(dialog));
00338 
00339     return rv;
00340 }
00341 
00343 // nsIWebProgressListener
00345 
00346 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in nsresult aStatus); */
00347 NS_IMETHODIMP 
00348 nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00349 {
00350     if ((aStateFlags & STATE_STOP) && mWebProgressListener) 
00351     {
00352         mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00353         if (mPrintProgress) 
00354         {
00355             mPrintProgress->CloseProgressDialog(PR_TRUE);
00356         }
00357         mPrintProgress       = nsnull;
00358         mWebProgressListener = nsnull;
00359     }
00360     return NS_OK;
00361 }
00362 
00363 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00364 NS_IMETHODIMP 
00365 nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00366 {
00367   if (mWebProgressListener) 
00368   {
00369       return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
00370   }
00371   return NS_ERROR_FAILURE;
00372 }
00373 
00374 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00375 NS_IMETHODIMP 
00376 nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00377 {
00378   if (mWebProgressListener) 
00379   {
00380       return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location);
00381   }
00382   return NS_ERROR_FAILURE;
00383 }
00384 
00385 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00386 NS_IMETHODIMP 
00387 nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00388 {
00389   if (mWebProgressListener) 
00390   {
00391       return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00392   }
00393   return NS_ERROR_FAILURE;
00394 }
00395 
00396 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00397 NS_IMETHODIMP 
00398 nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00399 {
00400   if (mWebProgressListener) 
00401   {
00402       return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
00403   }
00404   return NS_ERROR_FAILURE;
00405 }
00406 
00407