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 "mfcembed.h"
00006 #include "PrintProgressDialog.h"
00007 //#include "BrowserView.h"
00008 #include "nsIWebBrowser.h"
00009 #include "nsIWebBrowserPrint.h"
00010 #include "nsIInterfaceRequestorUtils.h"
00011 
00012 #include "nsMemory.h"
00013 
00014 #ifdef _DEBUG
00015 #define new DEBUG_NEW
00016 #undef THIS_FILE
00017 static char THIS_FILE[] = __FILE__;
00018 #endif
00019 
00021 // CPrintProgressDialog dialog
00022 
00023 class CDlgPrintListener : public nsIWebProgressListener
00024 {
00025 // Construction
00026 public:
00027        CDlgPrintListener(CPrintProgressDialog* aDlg); 
00028 
00029   NS_DECL_ISUPPORTS
00030   NS_DECL_NSIWEBPROGRESSLISTENER
00031 
00032   void ClearDlg() { m_PrintDlg = NULL; } // weak reference
00033 
00034 // Implementation
00035 protected:
00036        CPrintProgressDialog* m_PrintDlg;
00037 };
00038 
00039 NS_IMPL_ADDREF(CDlgPrintListener)
00040 NS_IMPL_RELEASE(CDlgPrintListener)
00041 
00042 NS_INTERFACE_MAP_BEGIN(CDlgPrintListener)
00043     NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebProgressListener)
00044     NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
00045 NS_INTERFACE_MAP_END
00046 
00047 
00048 CDlgPrintListener::CDlgPrintListener(CPrintProgressDialog* aDlg) :
00049   m_PrintDlg(aDlg)
00050 {
00051 }
00052 
00053 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
00054 NS_IMETHODIMP 
00055 CDlgPrintListener::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00056 {
00057   if (m_PrintDlg) {
00058     if (aStateFlags == (nsIWebProgressListener::STATE_START|nsIWebProgressListener::STATE_IS_DOCUMENT)) {
00059       return m_PrintDlg->OnStartPrinting();
00060 
00061     } else if (aStateFlags == (nsIWebProgressListener::STATE_STOP|nsIWebProgressListener::STATE_IS_DOCUMENT)) {
00062       return m_PrintDlg->OnEndPrinting(aStatus);
00063     }
00064   }
00065   return NS_OK;
00066 }
00067 
00068 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00069 NS_IMETHODIMP 
00070 CDlgPrintListener::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00071 {
00072   if (m_PrintDlg) {
00073     return m_PrintDlg->OnProgressPrinting(aCurSelfProgress, aMaxSelfProgress);
00074   }
00075   return NS_OK;
00076 }
00077 
00078 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00079 NS_IMETHODIMP 
00080 CDlgPrintListener::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00081 {
00082     return NS_OK;
00083 }
00084 
00085 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00086 NS_IMETHODIMP 
00087 CDlgPrintListener::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00088 {
00089     return NS_OK;
00090 }
00091 
00092 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00093 NS_IMETHODIMP 
00094 CDlgPrintListener::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00095 {
00096     return NS_OK;
00097 }
00098 
00099 
00101 // CPrintProgressDialog dialog
00102 
00103 
00104 CPrintProgressDialog::CPrintProgressDialog(CWnd*               pParent,
00105                                            BOOL                aIsForPrinting,
00106                                            nsIPrintProgressParams* aPPParams,
00107                                            nsIWebBrowserPrint* aWebBrowserPrint,
00108                                            nsIPrintSettings*   aPrintSettings)
00109        : CDialog(CPrintProgressDialog::IDD, pParent),
00110   m_WebBrowserPrint(aWebBrowserPrint),
00111   m_PPParams(aPPParams),
00112   m_PrintListener(nsnull),
00113   m_PrintSettings(aPrintSettings),
00114   m_HasStarted(FALSE),
00115   m_IsForPrinting(aIsForPrinting)
00116 {
00117        //{{AFX_DATA_INIT(CPrintProgressDialog)
00118               // NOTE: the ClassWizard will add member initialization here
00119        //}}AFX_DATA_INIT
00120 }
00121 
00122 CPrintProgressDialog::~CPrintProgressDialog()
00123 {
00124   CDlgPrintListener * pl = (CDlgPrintListener*)m_PrintListener.get();
00125   if (pl) {
00126     pl->ClearDlg();
00127   }
00128 }
00129 
00130 
00131 void CPrintProgressDialog::DoDataExchange(CDataExchange* pDX)
00132 {
00133        CDialog::DoDataExchange(pDX);
00134        //{{AFX_DATA_MAP(CPrintProgressDialog)
00135               // NOTE: the ClassWizard will add DDX and DDV calls here
00136        //}}AFX_DATA_MAP
00137 }
00138 
00139 
00140 BEGIN_MESSAGE_MAP(CPrintProgressDialog, CDialog)
00141        //{{AFX_MSG_MAP(CPrintProgressDialog)
00142        //}}AFX_MSG_MAP
00143 END_MESSAGE_MAP()
00144 
00146 // CPrintProgressDialog message handlers
00147 
00148 BOOL CPrintProgressDialog::OnInitDialog() 
00149 {
00150        CDialog::OnInitDialog();
00151 
00152   CWnd* cancelBtn = (CWnd*)GetDlgItem(IDCANCEL);
00153   if (cancelBtn)
00154   {
00155     if (m_IsForPrinting)
00156     {
00157       cancelBtn->EnableWindow(FALSE);
00158     } else {
00159       cancelBtn->ShowWindow(SW_HIDE);
00160       SetWindowText(_T("Print Preview"));
00161     }
00162   }
00163        
00164   SetDocAndURL();
00165 
00166        return TRUE;  // return TRUE unless you set the focus to a control
00167                      // EXCEPTION: OCX Property Pages should return FALSE
00168 }
00169 
00170 
00171 /* void OnStartPrinting (); */
00172 NS_IMETHODIMP 
00173 CPrintProgressDialog::OnStartPrinting()
00174 {
00175        CProgressCtrl* progressCtrl = (CProgressCtrl *)GetDlgItem(IDC_PRINT_PROGRESS_PRG);
00176        CWnd* progressText = (CWnd*)GetDlgItem(IDC_PRINT_PROGRESS_TXT);
00177   if (!progressCtrl || !progressText) return NS_OK;
00178 
00179   m_HasStarted = TRUE;
00180        progressCtrl->ShowWindow(SW_HIDE);
00181 
00182   SetDocAndURL();
00183 
00184   return NS_OK;
00185 }
00186 
00187 /* void OnProgressPrinting (in PRUint32 aProgress, in PRUint32 aProgressMax); */
00188 NS_IMETHODIMP 
00189 CPrintProgressDialog::OnProgressPrinting(PRUint32 aProgress, PRUint32 aProgressMax)
00190 {
00191        CProgressCtrl* progressCtrl = (CProgressCtrl *)GetDlgItem(IDC_PRINT_PROGRESS_PRG);
00192        CWnd* progressText = (CWnd*)GetDlgItem(IDC_PRINT_PROGRESS_TXT);
00193   if (!progressCtrl || !progressText) return NS_OK;
00194 
00195   if (m_HasStarted && aProgress != 100) 
00196   {
00197          progressText->ShowWindow(SW_HIDE);
00198          progressCtrl->ShowWindow(SW_SHOW);
00199          progressCtrl->SetRange(0, aProgressMax);
00200     m_HasStarted = FALSE;
00201          progressText->UpdateWindow();
00202   }
00203 
00204   SetDocAndURL();
00205 
00206   // Initialize the progress meter we we get the "zero" progress
00207   // which also tells us the max progress
00208   if (aProgress == 0) {
00209          progressCtrl->SetRange(0, aProgressMax);
00210     progressCtrl->SetPos(0);
00211 
00212   }
00213   CWnd* cancelBtn = (CWnd*)GetDlgItem(IDCANCEL);
00214   if (cancelBtn && m_IsForPrinting)
00215   {
00216     cancelBtn->EnableWindow(TRUE);
00217   }
00218        progressCtrl->SetPos(aProgress);
00219   RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
00220 
00221   return NS_OK;
00222 }
00223 
00224 /* void OnEndPrinting (in PRUint32 aStatus); */
00225 NS_IMETHODIMP 
00226 CPrintProgressDialog::OnEndPrinting(PRUint32 aStatus)
00227 {
00228   return NS_OK;
00229 }
00230 
00231 void CPrintProgressDialog::OnCancel() 
00232 {
00233   if (m_WebBrowserPrint) {
00234     m_WebBrowserPrint->Cancel();
00235   }
00236 
00237        CDialog::OnCancel();
00238 }
00239 
00240 void CPrintProgressDialog::SetDocAndURL()
00241 {
00242        USES_CONVERSION;
00243 
00244   if (m_PPParams) 
00245   {
00246     PRUnichar* docTitle = nsnull;
00247     PRUnichar* urlStr   = nsnull;
00248     m_PPParams->GetDocTitle(&docTitle);
00249     m_PPParams->GetDocURL(&urlStr);
00250 
00251          if (docTitle)
00252     {
00253       if (*docTitle && W2T(docTitle))
00254            {
00255                   SetWindowText(W2T(docTitle));
00256            }
00257       nsMemory::Free(docTitle);
00258     }
00259 
00260          if (urlStr)
00261     {
00262       if (*urlStr && W2T(urlStr))
00263            {
00264              CWnd *pWnd = GetDlgItem(IDC_PRINT_PROGRESS_URL_TXT);
00265              if (pWnd)
00266         {
00267                     pWnd->SetWindowText(W2T(urlStr));
00268              }
00269       }
00270       nsMemory::Free(urlStr);
00271     }
00272   }
00273   Invalidate();
00274   UpdateWindow();
00275 }