/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. #include "nsCOMPtr.h"
  39. #include "nsPrintingPromptService.h"
  40. #include "nsIPrintingPromptService.h"
  41. #include "nsIFactory.h"
  42. #include "nsPIDOMWindow.h"
  43. #include "nsReadableUtils.h"
  44. #include "nsIEmbeddingSiteWindow.h"
  45. #include "nsIServiceManager.h"
  46. #include "nsIWebBrowserChrome.h"
  47. #include "nsIWindowWatcher.h"
  48. #include "nsPrintDialogUtil.h"
  49. // Printing Progress Includes
  50. #include "nsPrintProgress.h"
  51. #include "nsPrintProgressParams.h"
  52. #include "nsIWebProgressListener.h"
  53. // XP Dialog includes
  54. #include "nsIDialogParamBlock.h"
  55. #include "nsISupportsUtils.h"
  56. #include "nsISupportsArray.h"
  57. // Includes need to locate the native Window
  58. #include "nsIWidget.h"
  59. #include "nsIBaseWindow.h"
  60. #include "nsIWebBrowserChrome.h"
  61. #include "nsIDocShellTreeOwner.h"
  62. #include "nsIDocShellTreeItem.h"
  63. #include "nsIDocShell.h"
  64. #include "nsIInterfaceRequestorUtils.h"
  65. static const char *kPrintProgressDialogURL = "chrome://global/content/printProgress.xul";
  66. static const char *kPrtPrvProgressDialogURL = "chrome://global/content/printPreviewProgress.xul";
  67. static const char *kPageSetupDialogURL = "chrome://global/content/printPageSetup.xul";
  68. // Static Data
  69. static HINSTANCE gInstance;
  70. /****************************************************************
  71. ************************* ParamBlock ***************************
  72. ****************************************************************/
  73. class ParamBlock {
  74. public:
  75. ParamBlock()
  76. {
  77. mBlock = 0;
  78. }
  79. ~ParamBlock()
  80. {
  81. NS_IF_RELEASE(mBlock);
  82. }
  83. nsresult Init() {
  84. return CallCreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID, &mBlock);
  85. }
  86. nsIDialogParamBlock * operator->() const { return mBlock; }
  87. operator nsIDialogParamBlock * const () { return mBlock; }
  88. private:
  89. nsIDialogParamBlock *mBlock;
  90. };
  91. //*****************************************************************************
  92. NS_IMPL_ISUPPORTS2(nsPrintingPromptService, nsIPrintingPromptService, nsIWebProgressListener)
  93. nsPrintingPromptService::nsPrintingPromptService()
  94. {
  95. }
  96. //-----------------------------------------------------------
  97. nsPrintingPromptService::~nsPrintingPromptService()
  98. {
  99. }
  100. //-----------------------------------------------------------
  101. nsresult
  102. nsPrintingPromptService::Init()
  103. {
  104. nsresult rv;
  105. mWatcher = do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
  106. return rv;
  107. }
  108. //-----------------------------------------------------------
  109. HWND
  110. nsPrintingPromptService::GetHWNDForDOMWindow(nsIDOMWindow *aWindow)
  111. {
  112. nsCOMPtr<nsIWebBrowserChrome> chrome;
  113. HWND hWnd = NULL;
  114. // We might be embedded so check this path first
  115. if (mWatcher) {
  116. nsCOMPtr<nsIDOMWindow> fosterParent;
  117. if (!aWindow)
  118. { // it will be a dependent window. try to find a foster parent.
  119. mWatcher->GetActiveWindow(getter_AddRefs(fosterParent));
  120. aWindow = fosterParent;
  121. }
  122. mWatcher->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
  123. }
  124. if (chrome) {
  125. nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
  126. if (site)
  127. {
  128. HWND w;
  129. site->GetSiteWindow(reinterpret_cast<void **>(&w));
  130. return w;
  131. }
  132. }
  133. // Now we might be the Browser so check this path
  134. nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
  135. nsCOMPtr<nsIDocShellTreeItem> treeItem =
  136. do_QueryInterface(window->GetDocShell());
  137. if (!treeItem) return nsnull;
  138. nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
  139. treeItem->GetTreeOwner(getter_AddRefs(treeOwner));
  140. if (!treeOwner) return nsnull;
  141. nsCOMPtr<nsIWebBrowserChrome> webBrowserChrome(do_GetInterface(treeOwner));
  142. if (!webBrowserChrome) return nsnull;
  143. nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(webBrowserChrome));
  144. if (!baseWin) return nsnull;
  145. nsCOMPtr<nsIWidget> widget;
  146. baseWin->GetMainWidget(getter_AddRefs(widget));
  147. if (!widget) return nsnull;
  148. return (HWND)widget->GetNativeData(NS_NATIVE_TMP_WINDOW);
  149. }
  150. ///////////////////////////////////////////////////////////////////////////////
  151. // nsIPrintingPrompt
  152. //-----------------------------------------------------------
  153. NS_IMETHODIMP
  154. nsPrintingPromptService::ShowPrintDialog(nsIDOMWindow *parent, nsIWebBrowserPrint *webBrowserPrint, nsIPrintSettings *printSettings)
  155. {
  156. NS_ENSURE_ARG(parent);
  157. HWND hWnd = GetHWNDForDOMWindow(parent);
  158. NS_ASSERTION(hWnd, "Couldn't get native window for PRint Dialog!");
  159. return NativeShowPrintDialog(hWnd, webBrowserPrint, printSettings);
  160. }
  161. /* 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); */
  162. NS_IMETHODIMP
  163. nsPrintingPromptService::ShowProgress(nsIDOMWindow* parent,
  164. nsIWebBrowserPrint* webBrowserPrint, // ok to be null
  165. nsIPrintSettings* printSettings, // ok to be null
  166. nsIObserver* openDialogObserver, // ok to be null
  167. bool isForPrinting,
  168. nsIWebProgressListener** webProgressListener,
  169. nsIPrintProgressParams** printProgressParams,
  170. bool* notifyOnOpen)
  171. {
  172. NS_ENSURE_ARG(webProgressListener);
  173. NS_ENSURE_ARG(printProgressParams);
  174. NS_ENSURE_ARG(notifyOnOpen);
  175. *notifyOnOpen = false;
  176. if (mPrintProgress) {
  177. *webProgressListener = nsnull;
  178. *printProgressParams = nsnull;
  179. return NS_ERROR_FAILURE;
  180. }
  181. nsPrintProgress* prtProgress = new nsPrintProgress();
  182. mPrintProgress = prtProgress;
  183. mWebProgressListener = prtProgress;
  184. nsCOMPtr<nsIPrintProgressParams> prtProgressParams = new nsPrintProgressParams();
  185. nsCOMPtr<nsIDOMWindow> parentWindow = parent;
  186. if (mWatcher && !parentWindow) {
  187. mWatcher->GetActiveWindow(getter_AddRefs(parentWindow));
  188. }
  189. if (parentWindow) {
  190. mPrintProgress->OpenProgressDialog(parentWindow,
  191. isForPrinting ? kPrintProgressDialogURL : kPrtPrvProgressDialogURL,
  192. prtProgressParams, openDialogObserver, notifyOnOpen);
  193. }
  194. prtProgressParams.forget(printProgressParams);
  195. NS_ADDREF(*webProgressListener = this);
  196. return NS_OK;
  197. }
  198. /* void showPageSetup (in nsIDOMWindow parent, in nsIPrintSettings printSettings); */
  199. NS_IMETHODIMP
  200. nsPrintingPromptService::ShowPageSetup(nsIDOMWindow *parent, nsIPrintSettings *printSettings, nsIObserver *aObs)
  201. {
  202. NS_ENSURE_ARG(printSettings);
  203. ParamBlock block;
  204. nsresult rv = block.Init();
  205. if (NS_FAILED(rv))
  206. return rv;
  207. block->SetInt(0, 0);
  208. rv = DoDialog(parent, block, printSettings, kPageSetupDialogURL);
  209. // if aWebBrowserPrint is not null then we are printing
  210. // so we want to pass back NS_ERROR_ABORT on cancel
  211. if (NS_SUCCEEDED(rv))
  212. {
  213. PRInt32 status;
  214. block->GetInt(0, &status);
  215. return status == 0?NS_ERROR_ABORT:NS_OK;
  216. }
  217. return rv;
  218. }
  219. /* void showPrinterProperties (in nsIDOMWindow parent, in wstring printerName, in nsIPrintSettings printSettings); */
  220. NS_IMETHODIMP
  221. nsPrintingPromptService::ShowPrinterProperties(nsIDOMWindow *parent, const PRUnichar *printerName, nsIPrintSettings *printSettings)
  222. {
  223. return NS_ERROR_NOT_IMPLEMENTED;
  224. }
  225. //-----------------------------------------------------------
  226. // Helper to Fly XP Dialog
  227. nsresult
  228. nsPrintingPromptService::DoDialog(nsIDOMWindow *aParent,
  229. nsIDialogParamBlock *aParamBlock,
  230. nsIPrintSettings* aPS,
  231. const char *aChromeURL)
  232. {
  233. NS_ENSURE_ARG(aParamBlock);
  234. NS_ENSURE_ARG(aPS);
  235. NS_ENSURE_ARG(aChromeURL);
  236. if (!mWatcher)
  237. return NS_ERROR_FAILURE;
  238. nsresult rv = NS_OK;
  239. // get a parent, if at all possible
  240. // (though we'd rather this didn't fail, it's OK if it does. so there's
  241. // no failure or null check.)
  242. nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
  243. if (!aParent)
  244. {
  245. mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
  246. aParent = activeParent;
  247. }
  248. // create a nsISupportsArray of the parameters
  249. // being passed to the window
  250. nsCOMPtr<nsISupportsArray> array;
  251. NS_NewISupportsArray(getter_AddRefs(array));
  252. if (!array) return NS_ERROR_FAILURE;
  253. nsCOMPtr<nsISupports> psSupports(do_QueryInterface(aPS));
  254. NS_ASSERTION(psSupports, "PrintSettings must be a supports");
  255. array->AppendElement(psSupports);
  256. nsCOMPtr<nsISupports> blkSupps(do_QueryInterface(aParamBlock));
  257. NS_ASSERTION(blkSupps, "IOBlk must be a supports");
  258. array->AppendElement(blkSupps);
  259. nsCOMPtr<nsISupports> arguments(do_QueryInterface(array));
  260. NS_ASSERTION(array, "array must be a supports");
  261. nsCOMPtr<nsIDOMWindow> dialog;
  262. rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
  263. "centerscreen,chrome,modal,titlebar", arguments,
  264. getter_AddRefs(dialog));
  265. return rv;
  266. }
  267. //////////////////////////////////////////////////////////////////////
  268. // nsIWebProgressListener
  269. //////////////////////////////////////////////////////////////////////
  270. /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in nsresult aStatus); */
  271. NS_IMETHODIMP
  272. nsPrintingPromptService::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
  273. {
  274. if ((aStateFlags & STATE_STOP) && mWebProgressListener)
  275. {
  276. mWebProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
  277. if (mPrintProgress)
  278. {
  279. mPrintProgress->CloseProgressDialog(true);
  280. }
  281. mPrintProgress = nsnull;
  282. mWebProgressListener = nsnull;
  283. }
  284. return NS_OK;
  285. }
  286. /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
  287. NS_IMETHODIMP
  288. nsPrintingPromptService::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
  289. {
  290. if (mWebProgressListener)
  291. {
  292. return mWebProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
  293. }
  294. return NS_ERROR_FAILURE;
  295. }
  296. /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */
  297. NS_IMETHODIMP
  298. nsPrintingPromptService::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, PRUint32 aFlags)
  299. {
  300. if (mWebProgressListener)
  301. {
  302. return mWebProgressListener->OnLocationChange(aWebProgress, aRequest, location, aFlags);
  303. }
  304. return NS_ERROR_FAILURE;
  305. }
  306. /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
  307. NS_IMETHODIMP
  308. nsPrintingPromptService::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
  309. {
  310. if (mWebProgressListener)
  311. {
  312. return mWebProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
  313. }
  314. return NS_ERROR_FAILURE;
  315. }
  316. /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
  317. NS_IMETHODIMP
  318. nsPrintingPromptService::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
  319. {
  320. if (mWebProgressListener)
  321. {
  322. return mWebProgressListener->OnSecurityChange(aWebProgress, aRequest, state);
  323. }
  324. return NS_ERROR_FAILURE;
  325. }