PageRenderTime 62ms CodeModel.GetById 16ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/embedding/components/printingui/src/win/nsPrintingPromptService.cpp

http://github.com/zpao/v8monkey
C++ | 393 lines | 252 code | 68 blank | 73 comment | 26 complexity | f94c5498cd33f7b1ce8c70e8a4407cc1 MD5 | raw file
  1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2 *
  3 * ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is mozilla.org code.
 17 *
 18 * The Initial Developer of the Original Code is
 19 * Netscape Communications Corporation.
 20 * Portions created by the Initial Developer are Copyright (C) 1998
 21 * the Initial Developer. All Rights Reserved.
 22 *
 23 * Contributor(s):
 24 *
 25 * Alternatively, the contents of this file may be used under the terms of
 26 * either the GNU General Public License Version 2 or later (the "GPL"), or
 27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 28 * in which case the provisions of the GPL or the LGPL are applicable instead
 29 * of those above. If you wish to allow use of your version of this file only
 30 * under the terms of either the GPL or the LGPL, and not to allow others to
 31 * use your version of this file under the terms of the MPL, indicate your
 32 * decision by deleting the provisions above and replace them with the notice
 33 * and other provisions required by the GPL or the LGPL. If you do not delete
 34 * the provisions above, a recipient may use your version of this file under
 35 * the terms of any one of the MPL, the GPL or the LGPL.
 36 *
 37 * ***** END LICENSE BLOCK ***** */
 38
 39#include "nsCOMPtr.h"
 40
 41#include "nsPrintingPromptService.h"
 42#include "nsIPrintingPromptService.h"
 43#include "nsIFactory.h"
 44#include "nsPIDOMWindow.h"
 45#include "nsReadableUtils.h"
 46#include "nsIEmbeddingSiteWindow.h"
 47#include "nsIServiceManager.h"
 48#include "nsIWebBrowserChrome.h"
 49#include "nsIWindowWatcher.h"
 50#include "nsPrintDialogUtil.h"
 51
 52// Printing Progress Includes
 53#include "nsPrintProgress.h"
 54#include "nsPrintProgressParams.h"
 55#include "nsIWebProgressListener.h"
 56
 57// XP Dialog includes
 58#include "nsIDialogParamBlock.h"
 59#include "nsISupportsUtils.h"
 60#include "nsISupportsArray.h"
 61
 62// Includes need to locate the native Window
 63#include "nsIWidget.h"
 64#include "nsIBaseWindow.h"
 65#include "nsIWebBrowserChrome.h"
 66#include "nsIDocShellTreeOwner.h"
 67#include "nsIDocShellTreeItem.h"
 68#include "nsIDocShell.h"
 69#include "nsIInterfaceRequestorUtils.h"
 70
 71
 72static const char *kPrintProgressDialogURL  = "chrome://global/content/printProgress.xul";
 73static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
 74static const char *kPageSetupDialogURL      = "chrome://global/content/printPageSetup.xul";
 75
 76// Static Data 
 77static HINSTANCE gInstance;
 78
 79/****************************************************************
 80 ************************* ParamBlock ***************************
 81 ****************************************************************/
 82
 83class ParamBlock {
 84
 85public:
 86    ParamBlock() 
 87    {
 88        mBlock = 0;
 89    }
 90    ~ParamBlock() 
 91    {
 92        NS_IF_RELEASE(mBlock);
 93    }
 94    nsresult Init() {
 95      return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
 96    }
 97    nsIDialogParamBlock * operator->() const { return mBlock; }
 98    operator nsIDialogParamBlock * const ()  { return mBlock; }
 99
100private:
101    nsIDialogParamBlock *mBlock;
102};
103
104//*****************************************************************************   
105
106NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
107
108nsPrintingPromptService::nsPrintingPromptService() 
109{
110}
111
112//-----------------------------------------------------------
113nsPrintingPromptService::~nsPrintingPromptService()
114{
115}
116
117//-----------------------------------------------------------
118nsresult
119nsPrintingPromptService::Init()
120{
121    nsresult rv;
122    mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
123    return rv;
124}
125
126//-----------------------------------------------------------
127HWND
128nsPrintingPromptService::GetHWNDForDOMWindow(nsIDOMWindow *aWindow)
129{
130    nsCOMPtr<nsIWebBrowserChrome> chrome;
131    HWND hWnd = NULL;
132
133    // We might be embedded so check this path first
134    if (mWatcher) {
135        nsCOMPtr<nsIDOMWindow> fosterParent;
136        if (!aWindow) 
137        {   // it will be a dependent window. try to find a foster parent.
138            mWatcher->GetActiveWindow(getter_AddRefs(fosterParent));
139            aWindow = fosterParent;
140        }
141        mWatcher->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
142    }
143
144    if (chrome) {
145        nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
146        if (site) 
147        {
148            HWND w;
149            site->GetSiteWindow(reinterpret_cast<void **>(&w));
150            return w;
151        }
152    }
153
154    // Now we might be the Browser so check this path
155    nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
156
157    nsCOMPtr<nsIDocShellTreeItem> treeItem =
158        do_QueryInterface(window->GetDocShell());
159    if (!treeItem) return nsnull;
160
161    nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
162    treeItem->GetTreeOwner(getter_AddRefs(treeOwner));
163    if (!treeOwner) return nsnull;
164
165    nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome(do_GetInterface(treeOwner));
166    if (!webBrowserChrome) return nsnull;
167
168    nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(webBrowserChrome));
169    if (!baseWin) return nsnull;
170
171    nsCOMPtr<nsIWidget> widget;
172    baseWin->GetMainWidget(getter_AddRefs(widget));
173    if (!widget) return nsnull;
174
175    return (HWND)widget->GetNativeData(NS_NATIVE_TMP_WINDOW);
176
177}
178
179
180///////////////////////////////////////////////////////////////////////////////
181// nsIPrintingPrompt
182
183//-----------------------------------------------------------
184NS_IMETHODIMP 
185nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
186{
187    NS_ENSURE_ARG(parent);
188
189    HWND hWnd = GetHWNDForDOMWindow(parent);
190    NS_ASSERTION(hWnd, "Couldn't get native window for PRint Dialog!");
191
192    return NativeShowPrintDialog(hWnd, webBrowserPrint, printSettings);
193}
194
195
196/* 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); */
197NS_IMETHODIMP 
198nsPrintingPromptService::ShowProgress(nsIDOMWindow*            parent, 
199                                      nsIWebBrowserPrint*      webBrowserPrint,    // ok to be null
200                                      nsIPrintSettings*        printSettings,      // ok to be null
201                                      nsIObserver*             openDialogObserver, // ok to be null
202                                      bool                     isForPrinting,
203                                      nsIWebProgressListener** webProgressListener,
204                                      nsIPrintProgressParams** printProgressParams,
205                                      bool*                  notifyOnOpen)
206{
207    NS_ENSURE_ARG(webProgressListener);
208    NS_ENSURE_ARG(printProgressParams);
209    NS_ENSURE_ARG(notifyOnOpen);
210
211    *notifyOnOpen = false;
212    if (mPrintProgress) {
213        *webProgressListener = nsnull;
214        *printProgressParams = nsnull;
215        return NS_ERROR_FAILURE;
216    }
217
218    nsPrintProgress* prtProgress = new nsPrintProgress();
219    mPrintProgress = prtProgress;
220    mWebProgressListener = prtProgress;
221
222    nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams();
223
224    nsCOMPtr<nsIDOMWindow> parentWindow = parent;
225
226    if (mWatcher && !parentWindow) {
227        mWatcher->GetActiveWindow(getter_AddRefs(parentWindow));
228    }
229
230    if (parentWindow) {
231        mPrintProgress->OpenProgressDialog(parentWindow,
232                                           isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL,
233                                           prtProgressParams, openDialogObserver, notifyOnOpen);
234    }
235
236    prtProgressParams.forget(printProgressParams);
237    NS_ADDREF(*webProgressListener = this);
238
239    return NS_OK;
240}
241
242/* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
243NS_IMETHODIMP 
244nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
245{
246    NS_ENSURE_ARG(printSettings);
247
248    ParamBlock block;
249    nsresult rv = block.Init();
250    if (NS_FAILED(rv))
251      return rv;
252
253    block->SetInt(0, 0);
254    rv = DoDialog(parent, block, printSettings, kPageSetupDialogURL);
255
256    // if aWebBrowserPrint is not null then we are printing
257    // so we want to pass back NS_ERROR_ABORT on cancel
258    if (NS_SUCCEEDED(rv)) 
259    {
260      PRInt32 status;
261      block->GetInt(0, &status);
262      return status == 0?NS_ERROR_ABORT:NS_OK;
263    }
264
265    return rv;
266}
267
268/* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
269NS_IMETHODIMP 
270nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const PRUnichar *printerName, nsIPrintSettings *printSettings)
271{
272    return NS_ERROR_NOT_IMPLEMENTED;
273}
274
275//-----------------------------------------------------------
276// Helper to Fly XP Dialog
277nsresult
278nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
279                                  nsIDialogParamBlock *aParamBlock, 
280                                  nsIPrintSettings* aPS,
281                                  const char *aChromeURL)
282{
283    NS_ENSURE_ARG(aParamBlock);
284    NS_ENSURE_ARG(aPS);
285    NS_ENSURE_ARG(aChromeURL);
286
287    if (!mWatcher)
288        return NS_ERROR_FAILURE;
289
290    nsresult rv = NS_OK;
291
292    // get a parent, if at all possible
293    // (though we'd rather this didn't fail, it's OK if it does. so there's
294    // no failure or null check.)
295    nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
296    if (!aParent) 
297    {
298        mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
299        aParent = activeParent;
300    }
301
302    // create a nsISupportsArray of the parameters 
303    // being passed to the window
304    nsCOMPtr<nsISupportsArray> array;
305    NS_NewISupportsArray(getter_AddRefs(array));
306    if (!array) return NS_ERROR_FAILURE;
307
308    nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
309    NS_ASSERTION(psSupports, "PrintSettings must be a supports");
310    array->AppendElement(psSupports);
311
312    nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
313    NS_ASSERTION(blkSupps, "IOBlk must be a supports");
314    array->AppendElement(blkSupps);
315
316    nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
317    NS_ASSERTION(array, "array must be a supports");
318
319
320    nsCOMPtr<nsIDOMWindow> dialog;
321    rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
322                              "centerscreen,chrome,modal,titlebar", arguments,
323                              getter_AddRefs(dialog));
324
325    return rv;
326}
327
328//////////////////////////////////////////////////////////////////////
329// nsIWebProgressListener
330//////////////////////////////////////////////////////////////////////
331
332/* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in nsresult aStatus); */
333NS_IMETHODIMP 
334nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
335{
336    if ((aStateFlags & STATE_STOP) && mWebProgressListener) 
337    {
338        mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
339        if (mPrintProgress) 
340        {
341            mPrintProgress->CloseProgressDialog(true);
342        }
343        mPrintProgress       = nsnull;
344        mWebProgressListener = nsnull;
345    }
346    return NS_OK;
347}
348
349/* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
350NS_IMETHODIMP 
351nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
352{
353  if (mWebProgressListener) 
354  {
355      return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
356  }
357  return NS_ERROR_FAILURE;
358}
359
360/* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */
361NS_IMETHODIMP 
362nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, PRUint32 aFlags)
363{
364  if (mWebProgressListener) 
365  {
366      return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags);
367  }
368  return NS_ERROR_FAILURE;
369}
370
371/* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
372NS_IMETHODIMP 
373nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
374{
375  if (mWebProgressListener) 
376  {
377      return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
378  }
379  return NS_ERROR_FAILURE;
380}
381
382/* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
383NS_IMETHODIMP 
384nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
385{
386  if (mWebProgressListener) 
387  {
388      return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
389  }
390  return NS_ERROR_FAILURE;
391}
392
393