Back to index

lightning-sunbird  0.9+nobinonly
BrowserView.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Chak Nanga <chak@netscape.com>
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 // File Overview....
00040 //
00041 // When the CBrowserFrm creates this View:
00042 //   - CreateBrowser() is called in OnCreate() to create the
00043 //        mozilla embeddable browser
00044 //
00045 // OnSize() method handles the window resizes and calls the approriate
00046 // interface method to resize the embedded browser properly
00047 //
00048 // Command handlers to handle browser navigation - OnNavBack(), 
00049 // OnNavForward() etc
00050 //
00051 // DestroyBrowser() called for cleaning up during object destruction
00052 //
00053 // Some important coding notes....
00054 //
00055 // 1. Make sure we do not have the CS_HREDRAW|CS_VREDRAW in the call
00056 //       to AfxRegisterWndClass() inside of PreCreateWindow() below
00057 //       If these flags are present then you'll see screen flicker when 
00058 //       you resize the frame window
00059 //
00060 // Next suggested file to look at : BrowserImpl.cpp
00061 //
00062 
00063 #include "stdafx.h"
00064 #include "TestEmbed.h"
00065 #include "BrowserView.h"
00066 #include "BrowserImpl.h"
00067 #include "BrowserFrm.h"
00068 #include "Dialogs.h"
00069 #include "ProfileMgr.h"
00070 #include "ProfilesDlg.h"
00071 #include "UrlDialog.h"
00072 #include "Tests.h"
00073 //#include "nsiRequest.h"
00074 #include "QaUtils.h"
00075 
00076 // Print Includes
00077 #include "PrintProgressDialog.h"
00078 //#include "PrintSetupDialog.h"
00079 
00080 // Mozilla Includes
00081 #include "nsIWidget.h"
00082 
00083 #ifdef _DEBUG
00084 #define new DEBUG_NEW
00085 #undef THIS_FILE
00086 static char THIS_FILE[] = __FILE__;
00087 #endif
00088 
00089 // Register message for FindDialog communication
00090 static UINT WM_FINDMSG = ::RegisterWindowMessage(FINDMSGSTRING);
00091 
00092 BEGIN_MESSAGE_MAP(CBrowserView, CWnd)
00093        //{{AFX_MSG_MAP(CBrowserView)
00094        ON_WM_CREATE()
00095        ON_WM_DESTROY()
00096        ON_WM_SIZE()
00097        ON_CBN_SELENDOK(ID_URL_BAR, OnUrlSelectedInUrlBar)
00098        ON_COMMAND(IDOK, OnNewUrlEnteredInUrlBar)
00099        ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
00100        ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
00101        ON_COMMAND(ID_VIEW_SOURCE, OnViewSource)
00102        ON_COMMAND(ID_VIEW_INFO, OnViewInfo)
00103        ON_COMMAND(ID_NAV_BACK, OnNavBack)
00104        ON_COMMAND(ID_NAV_FORWARD, OnNavForward)
00105        ON_COMMAND(ID_NAV_HOME, OnNavHome)
00106        ON_COMMAND(ID_NAV_RELOAD, OnNavReload)
00107        ON_COMMAND(ID_NAV_STOP, OnNavStop)
00108        ON_COMMAND(ID_EDIT_CUT, OnCut)
00109        ON_COMMAND(ID_EDIT_COPY, OnCopy)
00110        ON_COMMAND(ID_EDIT_PASTE, OnPaste)
00111     ON_COMMAND(ID_EDIT_UNDO, OnUndoUrlBarEditOp)
00112        ON_COMMAND(ID_EDIT_SELECT_ALL, OnSelectAll)
00113        ON_COMMAND(ID_EDIT_SELECT_NONE, OnSelectNone)
00114        ON_COMMAND(ID_COPY_LINK_LOCATION, OnCopyLinkLocation)
00115        ON_COMMAND(ID_OPEN_LINK_IN_NEW_WINDOW, OnOpenLinkInNewWindow)
00116        ON_COMMAND(ID_VIEW_IMAGE, OnViewImageInNewWindow)
00117        ON_COMMAND(ID_SAVE_LINK_AS, OnSaveLinkAs)
00118        ON_COMMAND(ID_SAVE_IMAGE_AS, OnSaveImageAs)
00119        ON_COMMAND(ID_EDIT_FIND, OnShowFindDlg)
00120        ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
00121        ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint)
00122        ON_REGISTERED_MESSAGE(WM_FINDMSG, OnFindMsg)
00123 
00124        // Menu/Toolbar UI update handlers
00125        ON_UPDATE_COMMAND_UI(ID_NAV_BACK, OnUpdateNavBack)
00126        ON_UPDATE_COMMAND_UI(ID_NAV_FORWARD, OnUpdateNavForward)
00127        ON_UPDATE_COMMAND_UI(ID_NAV_STOP, OnUpdateNavStop)
00128        ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateCut)
00129        ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateCopy)
00130        ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdatePaste)
00131 
00132        //}}AFX_MSG_MAP
00133 END_MESSAGE_MAP()
00134 
00135 
00136 CBrowserView::CBrowserView()
00137 {
00138        mWebBrowser = nsnull;
00139        mBaseWindow = nsnull;
00140        mWebNav = nsnull;
00141 
00142        mpBrowserImpl = nsnull;
00143        mpBrowserFrame = nsnull;
00144        mpBrowserFrameGlue = nsnull;
00145 
00146        mbDocumentLoading = PR_FALSE;
00147 
00148        m_pFindDlg = NULL;
00149   m_pPrintProgressDlg = NULL;
00150 
00151   m_bUrlBarClipOp = FALSE;
00152   m_bCurrentlyPrinting = FALSE;
00153 
00154   char *theUrl = "http://www.aol.com/";
00155 }
00156 
00157 CBrowserView::~CBrowserView()
00158 {
00159 }
00160 
00161 // This is a good place to create the embeddable browser
00162 // instance
00163 //
00164 int CBrowserView::OnCreate(LPCREATESTRUCT lpCreateStruct)
00165 {
00166        CreateBrowser();
00167 
00168        return 0;
00169 }
00170 
00171 void CBrowserView::OnDestroy()
00172 {
00173        DestroyBrowser();
00174 }
00175 
00176 // Create an instance of the Mozilla embeddable browser
00177 //
00178 HRESULT CBrowserView::CreateBrowser() 
00179 {         
00180        // Create web shell
00181        nsresult rv;
00182        mWebBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
00183        if(NS_FAILED(rv))
00184               return rv;
00185 
00186        // Save off the nsIWebNavigation interface pointer 
00187        // in the mWebNav member variable which we'll use 
00188        // later for web page navigation
00189        //
00190        rv = NS_OK;
00191        mWebNav = do_QueryInterface(mWebBrowser, &rv);
00192        if(NS_FAILED(rv))
00193               return rv;
00194 
00195 //     mSessionHistory = do_GetInterface(mWebBrowser, &rv); // de: added 5/11
00196 
00197        // Create the CBrowserImpl object - this is the object
00198        // which implements the interfaces which are required
00199        // by an app embedding mozilla i.e. these are the interfaces
00200        // thru' which the *embedded* browser communicates with the
00201        // *embedding* app
00202        //
00203        // The CBrowserImpl object will be passed in to the 
00204        // SetContainerWindow() call below
00205        //
00206        // Also note that we're passing the BrowserFrameGlue pointer 
00207        // and also the mWebBrowser interface pointer via CBrowserImpl::Init()
00208        // of CBrowserImpl object. 
00209        // These pointers will be saved by the CBrowserImpl object.
00210        // The CBrowserImpl object uses the BrowserFrameGlue pointer to 
00211        // call the methods on that interface to update the status/progress bars
00212        // etc.
00213        mpBrowserImpl = new CBrowserImpl();
00214        if(mpBrowserImpl == nsnull)
00215               return NS_ERROR_OUT_OF_MEMORY;
00216 
00217        // Pass along the mpBrowserFrameGlue pointer to the BrowserImpl object
00218        // This is the interface thru' which the XP BrowserImpl code communicates
00219        // with the platform specific code to update status bars etc.
00220        mpBrowserImpl->Init(mpBrowserFrameGlue, mWebBrowser);
00221        mpBrowserImpl->AddRef();
00222 
00223     mWebBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome*, mpBrowserImpl));
00224 
00225        rv = NS_OK;
00226     nsCOMPtr<nsIDocShellTreeItem> dsti = do_QueryInterface(mWebBrowser, &rv);
00227        if(NS_FAILED(rv))
00228               return rv;
00229     dsti->SetItemType(nsIDocShellTreeItem::typeContentWrapper);
00230 
00231     // Create the real webbrowser window
00232   
00233        // Note that we're passing the m_hWnd in the call below to InitWindow()
00234        // (CBrowserView inherits the m_hWnd from CWnd)
00235        // This m_hWnd will be used as the parent window by the embeddable browser
00236        //
00237        rv = NS_OK;
00238        mBaseWindow = do_QueryInterface(mWebBrowser, &rv);
00239        if(NS_FAILED(rv))
00240               return rv;
00241 
00242        // Get the view's ClientRect which to be passed in to the InitWindow()
00243        // call below
00244        RECT rcLocation;
00245        GetClientRect(&rcLocation);
00246        if(IsRectEmpty(&rcLocation))
00247        {
00248               rcLocation.bottom++;
00249               rcLocation.top++;
00250        }
00251 
00252        rv = mBaseWindow->InitWindow(nsNativeWidget(m_hWnd), nsnull,
00253               0, 0, rcLocation.right - rcLocation.left, rcLocation.bottom - rcLocation.top);
00254        rv = mBaseWindow->Create();
00255 
00256     // Register the BrowserImpl object to receive progress messages
00257        // These callbacks will be used to update the status/progress bars
00258 
00259 /*            // from WinEmbed.cpp
00260        nsCOMPtr<nsIWebProgressListener> listener(NS_STATIC_CAST(nsIWebProgressListener*, this));
00261     nsCOMPtr<nsIWeakReference> thisListener(do_GetWeakReference(listener));
00262     (void)mWebBrowser->AddWebBrowserListener(thisListener, 
00263        NS_GET_IID(nsIWebProgressListener));
00264 
00265     // The window has been created. Now register for history notifications
00266     rv = mWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsISHistoryListener));
00267 */
00268 
00269        // Finally, show the web browser window
00270        mBaseWindow->SetVisibility(PR_TRUE);
00271 
00272        qaTests = new CTests(mWebBrowser, mBaseWindow, mWebNav, mpBrowserImpl);
00273        //nsiRequest = new CNsIRequest(mWebBrowser,mpBrowserImpl);
00274        return S_OK;
00275 }
00276 
00277 HRESULT CBrowserView::DestroyBrowser() 
00278 {         
00279        if(mBaseWindow)
00280        {
00281               mBaseWindow->Destroy();
00282         mBaseWindow = nsnull;
00283        }
00284 
00285        if(mpBrowserImpl)
00286        {
00287               mpBrowserImpl->Release();
00288               mpBrowserImpl = nsnull;
00289        }
00290 /*
00291        nsWeakPtr weakling(
00292     do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, mpBrowserImpl)));
00293        nsresult rv;
00294     rv = mWebBrowser->RemoveWebBrowserListener(weakling, NS_GET_IID(nsIWebProgressListener));
00295        if (NS_FAILED(rv))
00296        {
00297               QAOutput("Web Progress Listener not removed.");
00298        }
00299        else
00300        {
00301               QAOutput("Web Progress Listener removed.");
00302        }
00303 */
00304        return NS_OK;
00305 }
00306 
00307 BOOL CBrowserView::PreCreateWindow(CREATESTRUCT& cs) 
00308 {
00309        if (!CWnd::PreCreateWindow(cs))
00310               return FALSE;
00311 
00312        cs.dwExStyle |= WS_EX_CLIENTEDGE;
00313        cs.style &= ~WS_BORDER;
00314        cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS, 
00315               ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);
00316 
00317        return TRUE;
00318 }
00319 
00320 // Adjust the size of the embedded browser
00321 // in response to any container size changes
00322 //
00323 void CBrowserView::OnSize( UINT nType, int cx, int cy)
00324 {
00325     mBaseWindow->SetPositionAndSize(0, 0, cx, cy, PR_TRUE);    
00326 }
00327 
00328 // Called by this object's creator i.e. the CBrowserFrame object
00329 // to pass it's pointer to us
00330 //
00331 void CBrowserView::SetBrowserFrame(CBrowserFrame* pBrowserFrame)
00332 {
00333        mpBrowserFrame = pBrowserFrame;
00334 }
00335 
00336 void CBrowserView::SetBrowserFrameGlue(PBROWSERFRAMEGLUE pBrowserFrameGlue)
00337 {
00338        mpBrowserFrameGlue = pBrowserFrameGlue;
00339 }
00340 
00341 // A new URL was entered in the URL bar
00342 // Get the URL's text from the Urlbar's (ComboBox's) EditControl 
00343 // and navigate to that URL
00344 //
00345 void CBrowserView::OnNewUrlEnteredInUrlBar()
00346 {
00347        // Get the currently entered URL
00348        CString strUrl;
00349        mpBrowserFrame->m_wndUrlBar.GetEnteredURL(strUrl);
00350 
00351     if(IsViewSourceUrl(strUrl))
00352         OpenViewSourceWindow(strUrl.GetBuffer(0));
00353     else
00354            // Navigate to that URL
00355            OpenURL(strUrl.GetBuffer(0));  
00356 
00357        // Add what was just entered into the UrlBar
00358        mpBrowserFrame->m_wndUrlBar.AddURLToList(strUrl);
00359 }
00360 
00361 // A URL has  been selected from the UrlBar's dropdown list
00362 //
00363 void CBrowserView::OnUrlSelectedInUrlBar()
00364 {
00365        CString strUrl;
00366        
00367        mpBrowserFrame->m_wndUrlBar.GetSelectedURL(strUrl);
00368 
00369     if(IsViewSourceUrl(strUrl))
00370         OpenViewSourceWindow(strUrl.GetBuffer(0));
00371     else
00372        OpenURL(strUrl.GetBuffer(0));
00373 }
00374 
00375 BOOL CBrowserView::IsViewSourceUrl(CString& strUrl)
00376 {
00377     return (strUrl.Find("view-source:", 0) != -1) ? TRUE : FALSE;
00378 }
00379 
00380 BOOL CBrowserView::OpenViewSourceWindow(const char* pUrl)
00381 {
00382        // Create a new browser frame in which we'll show the document source
00383        // Note that we're getting rid of the toolbars etc. by specifying
00384        // the appropriate chromeFlags
00385        PRUint32 chromeFlags =  nsIWebBrowserChrome::CHROME_WINDOW_BORDERS |
00386                                                  nsIWebBrowserChrome::CHROME_TITLEBAR |
00387                                                  nsIWebBrowserChrome::CHROME_WINDOW_RESIZE;
00388        CBrowserFrame* pFrm = CreateNewBrowserFrame(chromeFlags);
00389        if(!pFrm)
00390               return FALSE;
00391 
00392        // Finally, load this URI into the newly created frame
00393        pFrm->m_wndBrowserView.OpenURL(pUrl);
00394 
00395     pFrm->BringWindowToTop();
00396 
00397     return TRUE;
00398 }
00399 
00400 void CBrowserView::OnViewSource() 
00401 {
00402        if(! mWebNav)
00403               return;
00404 
00405        // Get the URI object whose source we want to view.
00406     nsresult rv = NS_OK;
00407        nsCOMPtr<nsIURI> currentURI;
00408        rv = mWebNav->GetCurrentURI(getter_AddRefs(currentURI));
00409        if(NS_FAILED(rv) || !currentURI)
00410               return;
00411 
00412        // Get the uri string associated with the nsIURI object
00413        nsXPIDLCString uriString;
00414        rv = currentURI->GetSpec(uriString);
00415        if(NS_FAILED(rv))
00416               return;
00417 
00418     // Build the view-source: url
00419     nsCAutoString viewSrcUrl;
00420     viewSrcUrl.Append("view-source:");
00421     viewSrcUrl.Append(uriString);
00422 
00423     OpenViewSourceWindow(viewSrcUrl.get());
00424 }
00425 
00426 void CBrowserView::OnViewInfo() 
00427 {
00428        AfxMessageBox("To Be Done...");
00429 }
00430 
00431 void CBrowserView::OnNavBack() 
00432 {
00433        if(mWebNav)
00434        {
00435               mWebNav->GoBack();
00436        }
00437 }
00438 
00439 void CBrowserView::OnUpdateNavBack(CCmdUI* pCmdUI)
00440 {
00441        PRBool canGoBack = PR_FALSE;
00442 
00443     if (mWebNav)
00444         mWebNav->GetCanGoBack(&canGoBack);
00445 
00446        pCmdUI->Enable(canGoBack);
00447 }
00448 
00449 void CBrowserView::OnNavForward() 
00450 {
00451        if(mWebNav)
00452        {
00453               mWebNav->GoForward();
00454        }
00455 }
00456 
00457 void CBrowserView::OnUpdateNavForward(CCmdUI* pCmdUI)
00458 {
00459        PRBool canGoFwd = PR_FALSE;
00460 
00461     if (mWebNav)
00462         mWebNav->GetCanGoForward(&canGoFwd);
00463 
00464        pCmdUI->Enable(canGoFwd);
00465 }
00466 
00467 void CBrowserView::OnNavHome() 
00468 {
00469     // Get the currently configured HomePage URL
00470     CString strHomeURL;
00471        CTestEmbedApp *pApp = (CTestEmbedApp *)AfxGetApp();
00472        if(pApp)
00473       pApp->GetHomePage(strHomeURL);
00474 
00475     if(strHomeURL.GetLength() > 0)
00476         OpenURL(strHomeURL);       
00477 }
00478 
00479 void CBrowserView::OnNavReload() 
00480 {
00481        if(mWebNav)
00482               mWebNav->Reload(nsIWebNavigation::LOAD_FLAGS_NONE);
00483 }
00484 
00485 void CBrowserView::OnNavStop() 
00486 {      
00487        if(mWebNav)
00488               mWebNav->Stop(nsIWebNavigation::STOP_ALL);
00489 }
00490 
00491 void CBrowserView::OnUpdateNavStop(CCmdUI* pCmdUI)
00492 {
00493        pCmdUI->Enable(mbDocumentLoading);
00494 }
00495 
00496 void CBrowserView::OnCut()
00497 {
00498     if(m_bUrlBarClipOp)
00499     {
00500         // We need to operate on the URLBar selection
00501         mpBrowserFrame->CutUrlBarSelToClipboard();
00502         m_bUrlBarClipOp = FALSE;
00503     }
00504     else
00505     {
00506            nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00507 
00508            if(clipCmds)
00509                   clipCmds->CutSelection();
00510     }
00511 }
00512 
00513 void CBrowserView::OnUpdateCut(CCmdUI* pCmdUI)
00514 {
00515        PRBool canCutSelection = PR_FALSE;
00516 
00517        nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00518        if (clipCmds)
00519         clipCmds->CanCutSelection(&canCutSelection);
00520 
00521     if(!canCutSelection)
00522     {
00523         // Check to see if the Cut cmd is to cut the URL 
00524         // selection in the UrlBar
00525         if(mpBrowserFrame->CanCutUrlBarSelection())
00526         {
00527             canCutSelection = TRUE;
00528             m_bUrlBarClipOp = TRUE;
00529         }
00530     }
00531 
00532        pCmdUI->Enable(canCutSelection);
00533 }
00534 
00535 void CBrowserView::OnCopy()
00536 {
00537     if(m_bUrlBarClipOp)
00538     {
00539         // We need to operate on the URLBar selection
00540         mpBrowserFrame->CopyUrlBarSelToClipboard();
00541         m_bUrlBarClipOp = FALSE;
00542     }
00543     else
00544     {
00545         // We need to operate on the web page content
00546            nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00547 
00548            if(clipCmds)
00549                   clipCmds->CopySelection();
00550     }
00551 }
00552 
00553 void CBrowserView::OnUpdateCopy(CCmdUI* pCmdUI)
00554 {
00555        PRBool canCopySelection = PR_FALSE;
00556 
00557        nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00558     if (clipCmds)
00559         clipCmds->CanCopySelection(&canCopySelection);
00560 
00561     if(!canCopySelection)
00562     {
00563         // Check to see if the Copy cmd is to copy the URL 
00564         // selection in the UrlBar
00565         if(mpBrowserFrame->CanCopyUrlBarSelection())
00566         {
00567             canCopySelection = TRUE;
00568             m_bUrlBarClipOp = TRUE;
00569         }
00570     }
00571 
00572        pCmdUI->Enable(canCopySelection);
00573 }
00574 
00575 void CBrowserView::OnPaste()
00576 {
00577     if(m_bUrlBarClipOp)
00578     {
00579         mpBrowserFrame->PasteFromClipboardToUrlBar();
00580         m_bUrlBarClipOp = FALSE;
00581     }
00582     else
00583     {
00584            nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00585 
00586            if(clipCmds)
00587                   clipCmds->Paste();
00588     }    
00589 }
00590 
00591 void CBrowserView::OnUndoUrlBarEditOp()
00592 {
00593     if(mpBrowserFrame->CanUndoUrlBarEditOp())
00594         mpBrowserFrame->UndoUrlBarEditOp();
00595 }
00596 
00597 void CBrowserView::OnUpdatePaste(CCmdUI* pCmdUI)
00598 {
00599        PRBool canPaste = PR_FALSE;
00600 
00601        nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00602     if (clipCmds)
00603         clipCmds->CanPaste(&canPaste);
00604 
00605     if(!canPaste)
00606     {
00607         if(mpBrowserFrame->CanPasteToUrlBar())
00608         {
00609             canPaste = TRUE;
00610             m_bUrlBarClipOp = TRUE;
00611         }
00612     }
00613 
00614        pCmdUI->Enable(canPaste);
00615 }
00616 
00617 void CBrowserView::OnSelectAll()
00618 {
00619        nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00620 
00621        if(clipCmds)
00622               clipCmds->SelectAll();
00623 }
00624 
00625 void CBrowserView::OnSelectNone()
00626 {
00627        nsCOMPtr<nsIClipboardCommands> clipCmds = do_GetInterface(mWebBrowser);
00628 
00629        if(clipCmds)
00630               clipCmds->SelectNone();
00631 }
00632 
00633 void CBrowserView::OnFileOpen()
00634 {
00635        char *lpszFilter =
00636         "HTML Files Only (*.htm;*.html)|*.htm;*.html|"
00637         "All Files (*.*)|*.*||";
00638 
00639        CFileDialog cf(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
00640                                    lpszFilter, this);
00641        if(cf.DoModal() == IDOK)
00642        {
00643               CString strFullPath = cf.GetPathName(); // Will be like: c:\tmp\junk.htm
00644               OpenURL(strFullPath);
00645        }
00646 }
00647 
00648 void CBrowserView::GetBrowserWindowTitle(nsCString& title)
00649 {
00650        nsXPIDLString idlStrTitle;
00651        if(mBaseWindow)
00652               mBaseWindow->GetTitle(getter_Copies(idlStrTitle));
00653 
00654        title.AssignWithConversion(idlStrTitle);
00655 
00656        // Sanitize the title of all illegal characters
00657     title.CompressWhitespace();     // Remove whitespace from the ends
00658     title.StripChars("\\*|:\"><?"); // Strip illegal characters
00659     title.ReplaceChar('.', L'_');   // Dots become underscores
00660     title.ReplaceChar('/', L'-');   // Forward slashes become hyphens
00661 }
00662 
00663 void CBrowserView::OnFileSaveAs()
00664 {
00665        nsCString fileName;
00666 
00667        GetBrowserWindowTitle(fileName); // Suggest the window title as the filename
00668 
00669        char *lpszFilter =
00670         "Web Page, HTML Only (*.htm;*.html)|*.htm;*.html|"
00671         "Web Page, Complete (*.htm;*.html)|*.htm;*.html|" 
00672         "Text File (*.txt)|*.txt||";
00673 
00674        CFileDialog cf(FALSE, "htm", fileName.get(), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
00675                                    lpszFilter, this);
00676 
00677        if(cf.DoModal() == IDOK)
00678        {
00679               CString strFullPath = cf.GetPathName(); // Will be like: c:\tmp\junk.htm
00680               char *pStrFullPath = strFullPath.GetBuffer(0); // Get char * for later use
00681               
00682               CString strDataPath; 
00683               char *pStrDataPath = NULL;
00684               if(cf.m_ofn.nFilterIndex == 2) 
00685               {
00686                      // cf.m_ofn.nFilterIndex == 2 indicates
00687                      // user want to save the complete document including
00688                      // all frames, images, scripts, stylesheets etc.
00689 
00690                      int idxPeriod = strFullPath.ReverseFind('.');
00691                      strDataPath = strFullPath.Mid(0, idxPeriod);
00692                      strDataPath += "_files";
00693 
00694                      // At this stage strDataPath will be of the form
00695                      // c:\tmp\junk_files - assuming we're saving to a file
00696                      // named junk.htm
00697                      // Any images etc in the doc will be saved to a dir
00698                      // with the name c:\tmp\junk_files
00699 
00700                      pStrDataPath = strDataPath.GetBuffer(0); //Get char * for later use
00701               }
00702 
00703         // Save the file
00704         nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(mWebBrowser));
00705 //            if(persist)
00706 //                   persist->SaveDocument(nsnull, pStrFullPath, pStrDataPath);
00707        }
00708 }
00709 
00710 void CBrowserView::OpenURL(const char* pUrl)
00711 {
00712     OpenURL(NS_ConvertASCIItoUCS2(pUrl).get());
00713 }
00714 
00715 void CBrowserView::OpenURL(const PRUnichar* pUrl)
00716 {
00717     if(mWebNav)
00718         mWebNav->LoadURI(pUrl,                              // URI string
00719                          nsIWebNavigation::LOAD_FLAGS_NONE, // Load flags
00720                          nsnull,                            // Refering URI
00721                          nsnull,                            // Post data
00722                          nsnull);                           // Headers stream
00723 }
00724 
00725 CBrowserFrame* CBrowserView::CreateNewBrowserFrame(PRUint32 chromeMask, 
00726                                                                PRInt32 x, PRInt32 y, 
00727                                                                PRInt32 cx, PRInt32 cy,
00728                                                                PRBool bShowWindow)
00729 {
00730        CTestEmbedApp *pApp = (CTestEmbedApp *)AfxGetApp();
00731        if(!pApp)
00732               return NULL;
00733 
00734        return pApp->CreateNewBrowserFrame(chromeMask, x, y, cx, cy, bShowWindow);
00735 }
00736 
00737 void CBrowserView::OpenURLInNewWindow(const PRUnichar* pUrl)
00738 {
00739        if(!pUrl)
00740               return;
00741 
00742        CBrowserFrame* pFrm = CreateNewBrowserFrame();
00743        if(!pFrm)
00744               return;
00745 
00746        // Load the URL into it...
00747 
00748        // Note that OpenURL() is overloaded - one takes a "char *"
00749        // and the other a "PRUniChar *". We're using the "PRUnichar *"
00750        // version here
00751 
00752        pFrm->m_wndBrowserView.OpenURL(pUrl);
00753 }
00754 
00755 void CBrowserView::LoadHomePage()
00756 {
00757        OnNavHome();
00758 }
00759 
00760 void CBrowserView::OnCopyLinkLocation()
00761 {
00762        if(! mCtxMenuLinkUrl.Length())
00763               return;
00764 
00765        if (! OpenClipboard())
00766               return;
00767 
00768        HGLOBAL hClipData = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, mCtxMenuLinkUrl.Length() + 1);
00769        if(! hClipData)
00770               return;
00771 
00772        char *pszClipData = (char*)::GlobalLock(hClipData);
00773        if(!pszClipData)
00774               return;
00775 
00776        mCtxMenuLinkUrl.ToCString(pszClipData, mCtxMenuLinkUrl.Length() + 1);
00777 
00778        GlobalUnlock(hClipData);
00779 
00780        EmptyClipboard();
00781        SetClipboardData(CF_TEXT, hClipData);
00782        CloseClipboard();
00783 }
00784 
00785 void CBrowserView::OnOpenLinkInNewWindow()
00786 {
00787        if(mCtxMenuLinkUrl.Length())
00788               OpenURLInNewWindow(mCtxMenuLinkUrl.get());
00789 }
00790 
00791 void CBrowserView::OnViewImageInNewWindow()
00792 {
00793        if(mCtxMenuImgSrc.Length())
00794               OpenURLInNewWindow(mCtxMenuImgSrc.get());
00795 }
00796 
00797 void CBrowserView::OnSaveLinkAs()
00798 {
00799        if(! mCtxMenuLinkUrl.Length())
00800               return;
00801 
00802        // Try to get the file name part from the URL
00803        // To do that we first construct an obj which supports 
00804        // nsIRUI interface. Makes it easy to extract portions
00805        // of a URL like the filename, scheme etc. + We'll also
00806        // use it while saving this link to a file
00807        nsresult rv   = NS_OK;
00808        nsCOMPtr<nsIURI> linkURI;
00809        rv = NS_NewURI(getter_AddRefs(linkURI), mCtxMenuLinkUrl);
00810        if (NS_FAILED(rv)) 
00811               return;
00812 
00813        // Get the "path" portion (see nsIURI.h for more info
00814        // on various parts of a URI)
00815        nsCAutoString fileName;
00816        linkURI->GetPath(fileName);
00817 
00818        // The path may have the "/" char in it - strip those
00819        fileName.StripChars("\\/");
00820 
00821        // Now, use this file name in a File Save As dlg...
00822 
00823        char *lpszFilter =
00824         "HTML Files (*.htm;*.html)|*.htm;*.html|"
00825               "Text Files (*.txt)|*.txt|" 
00826            "All Files (*.*)|*.*||";
00827 
00828        const char *pFileName = fileName.Length() ? fileName.get() : NULL;
00829 
00830        CFileDialog cf(FALSE, "htm", pFileName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
00831               lpszFilter, this);
00832        if(cf.DoModal() == IDOK)
00833        {
00834               CString strFullPath = cf.GetPathName();
00835 
00836         nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(mWebBrowser));
00837 //            if(persist)
00838 //                   persist->SaveURI(linkURI, nsnull, strFullPath.GetBuffer(0));
00839        }
00840 }
00841 
00842 void CBrowserView::OnSaveImageAs()
00843 {
00844        if(! mCtxMenuImgSrc.Length())
00845               return;
00846 
00847        // Try to get the file name part from the URL
00848        // To do that we first construct an obj which supports 
00849        // nsIRUI interface. Makes it easy to extract portions
00850        // of a URL like the filename, scheme etc. + We'll also
00851        // use it while saving this link to a file
00852        nsresult rv   = NS_OK;
00853        nsCOMPtr<nsIURI> linkURI;
00854        rv = NS_NewURI(getter_AddRefs(linkURI), mCtxMenuImgSrc);
00855        if (NS_FAILED(rv)) 
00856               return;
00857 
00858        // Get the "path" portion (see nsIURI.h for more info
00859        // on various parts of a URI)
00860        nsCAutoString path;
00861        linkURI->GetPath(path);
00862 
00863        // The path may have the "/" char in it - strip those
00864        nsCAutoString fileName(path);
00865        fileName.StripChars("\\/");
00866 
00867        // Now, use this file name in a File Save As dlg...
00868 
00869        char *lpszFilter = "All Files (*.*)|*.*||";
00870        const char *pFileName = fileName.Length() ? fileName.get() : NULL;
00871 
00872        CFileDialog cf(FALSE, NULL, pFileName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
00873               lpszFilter, this);
00874        if(cf.DoModal() == IDOK)
00875        {
00876               CString strFullPath = cf.GetPathName();
00877 
00878         nsCOMPtr<nsIWebBrowserPersist> persist(do_QueryInterface(mWebBrowser));
00879 //            if(persist)
00880 //                   persist->SaveURI(linkURI, nsnull, strFullPath.GetBuffer(0));
00881        }
00882 }
00883 
00884 void CBrowserView::OnShowFindDlg() 
00885 {
00886        // When the the user chooses the Find menu item
00887        // and if a Find dlg. is already being shown
00888        // just set focus to the existing dlg instead of
00889        // creating a new one
00890        if(m_pFindDlg)
00891        {
00892               m_pFindDlg->SetFocus();
00893               return;
00894        }
00895 
00896        CString csSearchStr;
00897        PRBool bMatchCase = PR_FALSE;
00898        PRBool bMatchWholeWord = PR_FALSE;
00899        PRBool bWrapAround = PR_FALSE;
00900        PRBool bSearchBackwards = PR_FALSE;
00901 
00902        // See if we can get and initialize the dlg box with
00903        // the values/settings the user specified in the previous search
00904        nsCOMPtr<nsIWebBrowserFind> finder(do_GetInterface(mWebBrowser));
00905        if(finder)
00906        {
00907               nsXPIDLString stringBuf;
00908               finder->GetSearchString(getter_Copies(stringBuf));
00909               csSearchStr = stringBuf.get();
00910 
00911               finder->GetMatchCase(&bMatchCase);
00912               finder->GetEntireWord(&bMatchWholeWord);
00913               finder->GetWrapFind(&bWrapAround);
00914               finder->GetFindBackwards(&bSearchBackwards);            
00915        }
00916 
00917        m_pFindDlg = new CFindDialog(csSearchStr, bMatchCase, bMatchWholeWord,
00918                                                  bWrapAround, bSearchBackwards, this);
00919        m_pFindDlg->Create(TRUE, NULL, NULL, 0, this);
00920 }
00921 
00922 // This will be called whenever the user pushes the Find
00923 // button in the Find dialog box
00924 // This method gets bound to the WM_FINDMSG windows msg via the 
00925 //
00926 //        ON_REGISTERED_MESSAGE(WM_FINDMSG, OnFindMsg) 
00927 //
00928 //     message map entry.
00929 //
00930 // WM_FINDMSG (which is registered towards the beginning of this file)
00931 // is the message via which the FindDialog communicates with this view
00932 //
00933 LRESULT CBrowserView::OnFindMsg(WPARAM wParam, LPARAM lParam)
00934 {
00935        nsCOMPtr<nsIWebBrowserFind> finder(do_GetInterface(mWebBrowser));
00936        if(!finder)
00937               return NULL;
00938 
00939        // Get the pointer to the current Find dialog box
00940        CFindDialog* dlg = (CFindDialog *) CFindReplaceDialog::GetNotifier(lParam);
00941        if(!dlg) 
00942               return NULL;
00943 
00944        // Has the user decided to terminate the dialog box?
00945        if(dlg->IsTerminating())
00946               return NULL;
00947 
00948        if(dlg->FindNext())
00949        {
00950               nsString searchString;
00951               searchString.AssignWithConversion(dlg->GetFindString().GetBuffer(0));
00952               finder->SetSearchString(searchString.get());
00953        
00954               finder->SetMatchCase(dlg->MatchCase() ? PR_TRUE : PR_FALSE);
00955               finder->SetEntireWord(dlg->MatchWholeWord() ? PR_TRUE : PR_FALSE);
00956               finder->SetWrapFind(dlg->WrapAround() ? PR_TRUE : PR_FALSE);
00957               finder->SetFindBackwards(dlg->SearchBackwards() ? PR_TRUE : PR_FALSE);
00958 
00959               PRBool didFind;
00960               nsresult rv = finder->FindNext(&didFind);
00961               
00962               return (NS_SUCCEEDED(rv) && didFind);
00963        }
00964 
00965     return 0;
00966 }
00967 
00968 void CBrowserView::OnFilePrint()
00969 {
00970   nsresult rv;
00971   nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID, &rv));
00972   if (NS_SUCCEEDED(rv)) 
00973   {
00974     prefs->SetBoolPref("print.use_native_print_dialog", PR_TRUE);
00975     prefs->SetBoolPref("print.show_print_progress", PR_FALSE);
00976   }
00977   else
00978        NS_ASSERTION(PR_FALSE, "Could not get preferences service");
00979 
00980   nsCOMPtr<nsIDOMWindow> domWindow;
00981        mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00982   if(domWindow) {
00983     nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(mWebBrowser));
00984          if(print)
00985          {
00986       CPrintProgressDialog  dlg(mWebBrowser, m_PrintSettings);
00987 
00988            nsCOMPtr<nsIURI> currentURI;
00989            nsresult rv = mWebNav->GetCurrentURI(getter_AddRefs(currentURI));
00990       if(NS_SUCCEEDED(rv) || currentURI) 
00991       {
00992            nsCAutoString path;
00993            currentURI->GetPath(path);
00994         dlg.SetURI(path.get());
00995       }
00996       m_bCurrentlyPrinting = TRUE;
00997       dlg.DoModal();
00998       m_bCurrentlyPrinting = FALSE;
00999     }
01000   }
01001 }
01002 
01004 void CBrowserView::OnUpdateFilePrint(CCmdUI* pCmdUI)
01005 {
01006        pCmdUI->Enable(!m_bCurrentlyPrinting);
01007 }
01008 
01009 
01010 
01011 // Called from the busy state related methods in the 
01012 // BrowserFrameGlue object
01013 //
01014 // When aBusy is TRUE it means that browser is busy loading a URL
01015 // When aBusy is FALSE, it's done loading
01016 // We use this to update our STOP tool bar button
01017 //
01018 // We basically note this state into a member variable
01019 // The actual toolbar state will be updated in response to the
01020 // ON_UPDATE_COMMAND_UI method - OnUpdateNavStop() being called
01021 //
01022 void CBrowserView::UpdateBusyState(PRBool aBusy)
01023 {
01024        mbDocumentLoading = aBusy;
01025 }
01026 
01027 void CBrowserView::SetCtxMenuLinkUrl(nsAutoString& strLinkUrl)
01028 {
01029        mCtxMenuLinkUrl = strLinkUrl;
01030 }
01031 
01032 void CBrowserView::SetCtxMenuImageSrc(nsAutoString& strImgSrc)
01033 {
01034        mCtxMenuImgSrc = strImgSrc;
01035 }
01036 
01037 void CBrowserView::Activate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
01038 {
01039        nsCOMPtr<nsIWebBrowserFocus> focus(do_GetInterface(mWebBrowser));
01040        if(!focus)
01041               return;
01042     
01043     switch(nState) {
01044         case WA_ACTIVE:
01045         case WA_CLICKACTIVE:
01046             focus->Activate();
01047             break;
01048         case WA_INACTIVE:
01049             focus->Deactivate();
01050             break;
01051         default:
01052             break;
01053     }
01054 }
01055 
01056 BOOL CBrowserView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
01057 {
01058    // Since CTests has all the test menu handlers try and route it to it first
01059    // We do not need to do this if all the menu handlers were in CBrowserView itself
01060 
01061    if ((qaTests != NULL) && qaTests->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
01062       return TRUE;
01063    //if ((nsiRequest != NULL) && nsiRequest->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
01064    //   return TRUE;
01065 
01066    return CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
01067 }
01068