Back to index

lightning-sunbird  0.9+nobinonly
PrintProgressDialog.cpp
Go to the documentation of this file.
00001 // PrintProgressDialog.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "testembed.h"
00006 #include "PrintProgressDialog.h"
00007 #include "BrowserView.h"
00008 #include "nsIWebBrowser.h"
00009 #include "nsIWebBrowserPrint.h"
00010 
00011 #ifdef _DEBUG
00012 #define new DEBUG_NEW
00013 #undef THIS_FILE
00014 static char THIS_FILE[] = __FILE__;
00015 #endif
00016 
00018 // CPrintProgressDialog dialog
00019 
00020 class CDlgPrintListener : public nsIWebProgressListener
00021 {
00022 // Construction
00023 public:
00024        CDlgPrintListener(CPrintProgressDialog* aDlg); 
00025 
00026   NS_DECL_ISUPPORTS
00027   NS_DECL_NSIWEBPROGRESSLISTENER
00028 
00029   void ClearDlg() { m_PrintDlg = NULL; } // weak reference
00030 
00031 // Implementation
00032 protected:
00033        CPrintProgressDialog* m_PrintDlg;
00034 };
00035 
00036 NS_IMPL_ADDREF(CDlgPrintListener)
00037 NS_IMPL_RELEASE(CDlgPrintListener)
00038 
00039 NS_INTERFACE_MAP_BEGIN(CDlgPrintListener)
00040     NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebProgressListener)
00041     NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
00042 NS_INTERFACE_MAP_END
00043 
00044 
00045 CDlgPrintListener::CDlgPrintListener(CPrintProgressDialog* aDlg) :
00046   m_PrintDlg(aDlg)
00047 {
00048   //NS_ADDREF_THIS();
00049 }
00050 
00051 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
00052 NS_IMETHODIMP 
00053 CDlgPrintListener::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00054 {
00055   if (m_PrintDlg) {
00056     if (aStateFlags == (nsIWebProgressListener::STATE_START|nsIWebProgressListener::STATE_IS_DOCUMENT)) {
00057       return m_PrintDlg->OnStartPrinting();
00058 
00059     } else if (aStateFlags == (nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT)) {
00060       return m_PrintDlg->OnEndPrinting(aStatus);
00061     }
00062   }
00063   return NS_OK;
00064 }
00065 
00066 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00067 NS_IMETHODIMP 
00068 CDlgPrintListener::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00069 {
00070   if (m_PrintDlg) {
00071     return m_PrintDlg->OnProgressPrinting(aCurSelfProgress, aMaxSelfProgress);
00072   }
00073   return NS_OK;
00074 }
00075 
00076 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00077 NS_IMETHODIMP 
00078 CDlgPrintListener::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00079 {
00080     return NS_OK;
00081 }
00082 
00083 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00084 NS_IMETHODIMP 
00085 CDlgPrintListener::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00086 {
00087     return NS_OK;
00088 }
00089 
00090 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00091 NS_IMETHODIMP 
00092 CDlgPrintListener::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00093 {
00094     return NS_OK;
00095 }
00096 
00097 
00099 // CPrintProgressDialog dialog
00100 
00101 
00102 CPrintProgressDialog::CPrintProgressDialog(nsIWebBrowser* aWebBrowser,
00103                                            nsIPrintSettings* aPrintSettings,
00104                                            CWnd* pParent /*=NULL*/)
00105        : CDialog(CPrintProgressDialog::IDD, pParent),
00106   m_WebBrowser(aWebBrowser),
00107   m_PrintListener(nsnull),
00108   m_PrintSettings(aPrintSettings),
00109   m_InModalMode(PR_FALSE)
00110 {
00111        //{{AFX_DATA_INIT(CPrintProgressDialog)
00112               // NOTE: the ClassWizard will add member initialization here
00113        //}}AFX_DATA_INIT
00114 }
00115 
00116 CPrintProgressDialog::~CPrintProgressDialog()
00117 {
00118   CDlgPrintListener * pl = (CDlgPrintListener*)m_PrintListener.get();
00119   if (pl) {
00120     pl->ClearDlg();
00121   }
00122 }
00123 
00124 
00125 void CPrintProgressDialog::DoDataExchange(CDataExchange* pDX)
00126 {
00127        CDialog::DoDataExchange(pDX);
00128        //{{AFX_DATA_MAP(CPrintProgressDialog)
00129               // NOTE: the ClassWizard will add DDX and DDV calls here
00130        //}}AFX_DATA_MAP
00131 }
00132 
00133 
00134 BEGIN_MESSAGE_MAP(CPrintProgressDialog, CDialog)
00135        //{{AFX_MSG_MAP(CPrintProgressDialog)
00136        //}}AFX_MSG_MAP
00137 END_MESSAGE_MAP()
00138 
00140 // CPrintProgressDialog message handlers
00141 static void GetLocalRect(CWnd * aWnd, CRect& aRect, CWnd * aParent)
00142 {
00143   CRect wr;
00144   aParent->GetWindowRect(wr);
00145 
00146   CRect cr;
00147   aParent->GetClientRect(cr);
00148 
00149   aWnd->GetWindowRect(aRect);
00150 
00151   int borderH = wr.Height() - cr.Height();
00152   int borderW = (wr.Width() - cr.Width())/2;
00153   aRect.top    -= wr.top+borderH-borderW;
00154   aRect.left   -= wr.left+borderW;
00155   aRect.right  -= wr.left+borderW;
00156   aRect.bottom -= wr.top+borderH-borderW;
00157 
00158 }
00159 
00160 BOOL CPrintProgressDialog::OnInitDialog() 
00161 {
00162        CDialog::OnInitDialog();
00163        
00164        CRect clientRect;
00165        GetClientRect(&clientRect);
00166 
00167        CRect titleRect;
00168   GetLocalRect(GetDlgItem(IDC_PPD_DOC_TITLE_STATIC), titleRect, this);
00169 
00170        CRect itemRect;
00171   GetLocalRect(GetDlgItem(IDC_PPD_DOC_TXT), itemRect, this);
00172 
00173   CRect progRect;
00174   progRect.left   = titleRect.left;
00175   progRect.top    = itemRect.top+itemRect.Height()+5;
00176   progRect.right  = clientRect.Width()-(2*titleRect.left);
00177   progRect.bottom = progRect.top+titleRect.Height();
00178 
00179 
00180        m_wndProgress.Create (WS_CHILD | WS_VISIBLE, progRect, this, -1);
00181        m_wndProgress.SetPos (0);
00182        
00183        return TRUE;  // return TRUE unless you set the focus to a control
00184                      // EXCEPTION: OCX Property Pages should return FALSE
00185 }
00186 
00187 
00188 int CPrintProgressDialog::DoModal( )
00189 {
00190   PRBool doModal = PR_FALSE;
00191   nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(m_WebBrowser));
00192   if(print) 
00193   {
00194     m_PrintListener = new CDlgPrintListener(this); // constructor addrefs
00195     if (m_PrintListener) {
00196       // doModal will be set to false if the print job was cancelled
00197       nsIWebProgressListener * wpl = NS_STATIC_CAST(nsIWebProgressListener*, m_PrintListener);
00198       doModal = NS_SUCCEEDED(print->Print(m_PrintSettings, wpl)) == PR_TRUE;
00199     }
00200   }
00201 
00202   if (doModal) {
00203     m_InModalMode = PR_TRUE;
00204     return CDialog::DoModal();
00205   }
00206   return 0;
00207 }
00208 
00209 
00210 /* void OnStartPrinting (); */
00211 NS_IMETHODIMP 
00212 CPrintProgressDialog::OnStartPrinting()
00213 {
00214   return NS_OK;
00215 }
00216 
00217 /* void OnProgressPrinting (in PRUint32 aProgress, in PRUint32 aProgressMax); */
00218 NS_IMETHODIMP 
00219 CPrintProgressDialog::OnProgressPrinting(PRUint32 aProgress, PRUint32 aProgressMax)
00220 {
00221   if (m_wndProgress.m_hWnd == NULL) return NS_OK;
00222 
00223   // Initialize the progress meter we we get the "zero" progress
00224   // which also tells us the max progress
00225   if (aProgress == 0) {
00226          CWnd *pWnd = GetDlgItem(IDC_PPD_DOC_TXT);
00227          if(pWnd)
00228                 pWnd->SetWindowText(m_URL);
00229 
00230          m_wndProgress.SetRange(0, aProgressMax);
00231     m_wndProgress.SetPos(0);
00232   }
00233        m_wndProgress.SetPos(aProgress);
00234   RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
00235 
00236   return NS_OK;
00237 }
00238 
00239 /* void OnEndPrinting (in PRUint32 aStatus); */
00240 NS_IMETHODIMP 
00241 CPrintProgressDialog::OnEndPrinting(PRUint32 aStatus)
00242 {
00243   // Here we need to know whether we have gone "modal" 
00244   // because we could get notified here if the user cancels
00245   // before we ever get a chance to go into the modal loop
00246   if (m_InModalMode) {
00247     EndDialog(1);
00248   }
00249   return NS_OK;
00250 }
00251 
00252 void CPrintProgressDialog::OnCancel() 
00253 {
00254   nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(m_WebBrowser));
00255   if (print) {
00256     print->Cancel();
00257   }
00258 
00259        CDialog::OnCancel();
00260 }
00261 
00262 void CPrintProgressDialog::SetURI(const char* aTitle)
00263 {
00264   m_URL = _T(aTitle);
00265 }