Back to index

lightning-sunbird  0.9+nobinonly
BrowserFrm.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: Mozilla-sample-code 1.0
00004  *
00005  * Copyright (c) 2002 Netscape Communications Corporation and
00006  * other contributors
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a
00009  * copy of this Mozilla sample software and associated documentation files
00010  * (the "Software"), to deal in the Software without restriction, including
00011  * without limitation the rights to use, copy, modify, merge, publish,
00012  * distribute, sublicense, and/or sell copies of the Software, and to permit
00013  * persons to whom the Software is furnished to do so, subject to the
00014  * following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included
00017  * in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  *
00027  * Contributor(s):
00028  *   Chak Nanga <chak@netscape.com>
00029  *
00030  * ***** END LICENSE BLOCK ***** */
00031 
00032 // File Overview....
00033 //
00034 // The typical MFC View, toolbar, statusbar creation done 
00035 // in CBrowserFrame::OnCreate()
00036 //
00037 // Code to update the Status/Tool bars in response to the
00038 // Web page loading progress(called from methods in CBrowserImpl)
00039 //
00040 // SetupFrameChrome() determines what, if any, UI elements this Frame
00041 // will sport based on the current "chromeMask" 
00042 //
00043 // Also take a look at OnClose() which gets used when you close a browser
00044 // window. This needs to be overrided mainly to handle supporting multiple
00045 // browser frame windows via the "New Browser Window" menu item
00046 // Without this being overridden the MFC framework handles the OnClose and
00047 // shutsdown the complete application when a frame window is closed.
00048 // In our case, we want the app to shutdown when the File/Exit menu is chosen
00049 //
00050 // Another key functionality this object implements is the IBrowserFrameGlue
00051 // interface - that's the interface the Gecko embedding interfaces call
00052 // upong to update the status bar etc.
00053 // (Take a look at IBrowserFrameGlue.h for the interface definition and
00054 // the BrowserFrm.h to see how we implement this interface - as a nested
00055 // class)
00056 // We pass this Glue object pointer to the CBrowserView object via the 
00057 // SetBrowserFrameGlue() method. The CBrowserView passes this on to the
00058 // embedding interface implementaion
00059 //
00060 // Please note the use of the macro METHOD_PROLOGUE in the implementation
00061 // of the nested BrowserFrameGlue object. Essentially what this macro does
00062 // is to get you access to the outer (or the object which is containing the
00063 // nested object) object via the pThis pointer.
00064 // Refer to the AFXDISP.H file in VC++ include dirs
00065 //
00066 // Next suggested file to look at : BrowserView.cpp
00067 
00068 #include "stdafx.h"
00069 #include "MfcEmbed.h"
00070 #include "BrowserFrm.h"
00071 #include "EditorFrm.h"
00072 #include "BrowserImpl.h"
00073 
00074 #ifdef _DEBUG
00075 #define new DEBUG_NEW
00076 #undef THIS_FILE
00077 static char THIS_FILE[] = __FILE__;
00078 #endif
00079 
00081 // CBrowserFrame
00082 
00083 IMPLEMENT_DYNAMIC(CBrowserFrame, CFrameWnd)
00084 
00085 BEGIN_MESSAGE_MAP(CBrowserFrame, CFrameWnd)
00086     //{{AFX_MSG_MAP(CBrowserFrame)
00087     ON_WM_CREATE()
00088     ON_WM_SETFOCUS()
00089     ON_WM_SIZE()
00090     ON_WM_CLOSE()
00091     ON_WM_ACTIVATE()
00092     //}}AFX_MSG_MAP
00093 END_MESSAGE_MAP()
00094 
00095 static UINT indicators[] =
00096 {
00097     ID_SEPARATOR,           // For the Status line
00098     ID_SEPARATOR,           // For the Progress Bar
00099     ID_SEPARATOR,           // For the padlock image
00100 };
00101 
00103 // CBrowserFrame construction/destruction
00104 
00105 CBrowserFrame::CBrowserFrame()
00106 {
00107     mIsEditor = FALSE;
00108 }
00109 
00110 CBrowserFrame::CBrowserFrame(PRUint32 chromeMask)
00111 {
00112     // Save the chromeMask off. It'll be used
00113     // later to determine whether this browser frame
00114     // will have menubar, toolbar, statusbar etc.
00115 
00116     m_chromeMask = chromeMask;
00117     mIsEditor = FALSE;
00118 }
00119 
00120 CBrowserFrame::~CBrowserFrame()
00121 {
00122 }
00123 
00124 void CBrowserFrame::OnClose()
00125 {
00126     CMfcEmbedApp *pApp = (CMfcEmbedApp *)AfxGetApp();
00127     pApp->RemoveFrameFromList(this);
00128 
00129     DestroyWindow();
00130 }
00131 
00132 // This is where the UrlBar, ToolBar, StatusBar, ProgressBar
00133 // get created
00134 //
00135 int CBrowserFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
00136 {
00137     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
00138         return -1;
00139 
00140     // Pass "this" to the View for later callbacks
00141     // and/or access to any public data members, if needed
00142     //
00143     m_wndBrowserView.SetBrowserFrame(this);
00144 
00145     // Pass on the BrowserFrameGlue also to the View which
00146     // it will use during the Init() process after creation
00147     // of the BrowserImpl obj. Essentially, the View object
00148     // hooks up the Embedded browser's callbacks to the BrowserFrame
00149     // via this BrowserFrameGlue object
00150     m_wndBrowserView.SetBrowserFrameGlue((PBROWSERFRAMEGLUE)&m_xBrowserFrameGlueObj);
00151 
00152     // create a view to occupy the client area of the frame
00153     // This will be the view in which the embedded browser will
00154     // be displayed in
00155     //
00156     if (!m_wndBrowserView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
00157         CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
00158     {
00159         TRACE0("Failed to create view window\n");
00160         return -1;
00161     }
00162 
00163     // create the URL bar (essentially a ComboBoxEx object)
00164     if (!m_wndUrlBar.Create(CBS_DROPDOWN | WS_CHILD, CRect(0, 0, 200, 150), this, ID_URL_BAR))
00165     {
00166         TRACE0("Failed to create URL Bar\n");
00167         return -1;      // fail to create
00168     }
00169     
00170     // Load the Most Recently Used(MRU) Urls into the UrlBar
00171     m_wndUrlBar.LoadMRUList();
00172 
00173     UINT resID = mIsEditor ? IDR_EDITOR : IDR_MAINFRAME;
00174 
00175     // Create the toolbar with Back, Fwd, Stop, etc. buttons..
00176     //                     or
00177     // Create a toolbar with the Editor toolbar buttons - Bold, Italic etc.
00178     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
00179         | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
00180         !m_wndToolBar.LoadToolBar(resID))
00181     {
00182         TRACE0("Failed to create toolbar\n");
00183         return -1;      // fail to create
00184     }
00185 
00186     // Create a ReBar window to which the toolbar and UrlBar 
00187     // will be added
00188     if (!m_wndReBar.Create(this))
00189     {
00190         TRACE0("Failed to create ReBar\n");
00191         return -1;      // fail to create
00192     }
00193     
00194     //Add the ToolBar and UrlBar windows to the rebar
00195     m_wndReBar.AddBar(&m_wndToolBar);
00196 
00197     if (!mIsEditor)
00198           m_wndReBar.AddBar(&m_wndUrlBar, _T("Enter URL:"));
00199 
00200     // Create the status bar with two panes - one pane for actual status
00201     // text msgs. and the other for the progress control
00202     if (!m_wndStatusBar.CreateEx(this) ||
00203         !m_wndStatusBar.SetIndicators(indicators,
00204           sizeof(indicators)/sizeof(UINT)))
00205     {
00206         TRACE0("Failed to create status bar\n");
00207         return -1;      // fail to create
00208     }
00209 
00210     // Create the progress bar as a child of the status bar.
00211     // Note that the ItemRect which we'll get at this stage
00212     // is bogus since the status bar panes are not fully
00213     // positioned yet i.e. we'll be passing in an invalid rect
00214     // to the Create function below
00215     // The actual positioning of the progress bar will be done
00216     // in response to OnSize()
00217     RECT rc;
00218     m_wndStatusBar.GetItemRect (1, &rc);
00219     if (!m_wndProgressBar.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH, rc, &m_wndStatusBar, ID_PROG_BAR))
00220     {
00221         TRACE0("Failed to create progress bar\n");
00222         return -1;      // fail to create
00223     }
00224 
00225     // The third pane(i.e. at index 2) of the status bar will have 
00226     // the security lock icon displayed in it. Set up it's size(16) 
00227     // and style(no border)so that the padlock icons can be properly drawn
00228     m_wndStatusBar.SetPaneInfo(2, -1, SBPS_NORMAL|SBPS_NOBORDERS, 16);
00229 
00230     // Create a tooltip window. (the MFC tooltip is not really suitable)
00231     m_wndTooltip.Create(CWnd::GetDesktopWindow());
00232 
00233     // Also, set the padlock icon to be the insecure icon to begin with
00234     UpdateSecurityStatus(nsIWebProgressListener::STATE_IS_INSECURE);
00235 
00236     // Based on the "chromeMask" we were supplied during construction
00237     // hide any requested UI elements - statusbar, menubar etc...
00238     // Note that the window styles (WM_RESIZE etc) are set inside
00239     // of PreCreateWindow()
00240 
00241     SetupFrameChrome(); 
00242 
00243     return 0;
00244 }
00245 
00246 void CBrowserFrame::SetupFrameChrome()
00247 {
00248     if(m_chromeMask == nsIWebBrowserChrome::CHROME_ALL)
00249         return;
00250 
00251     if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_MENUBAR) )
00252         SetMenu(NULL); // Hide the MenuBar
00253 
00254     if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_TOOLBAR) )
00255         m_wndReBar.ShowWindow(SW_HIDE); // Hide the ToolBar
00256 
00257     if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_STATUSBAR) )
00258         m_wndStatusBar.ShowWindow(SW_HIDE); // Hide the StatusBar
00259 }
00260 
00261 BOOL CBrowserFrame::PreCreateWindow(CREATESTRUCT& cs)
00262 {
00263     if( !CFrameWnd::PreCreateWindow(cs) )
00264         return FALSE;
00265 
00266     cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
00267 
00268     // Change window style based on the chromeMask
00269 
00270     if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_TITLEBAR) )
00271         cs.style &= ~WS_CAPTION; // No caption        
00272 
00273     if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_WINDOW_RESIZE) )
00274     {
00275         // Can't resize this window
00276         cs.style &= ~WS_SIZEBOX;
00277         cs.style &= ~WS_THICKFRAME;
00278         cs.style &= ~WS_MINIMIZEBOX;
00279         cs.style &= ~WS_MAXIMIZEBOX;
00280     }
00281 
00282     cs.lpszClass = AfxRegisterWndClass(0);
00283 
00284     return TRUE;
00285 }
00286 
00288 // CBrowserFrame message handlers
00289 void CBrowserFrame::OnSetFocus(CWnd* pOldWnd)
00290 {
00291     // forward focus to the view window
00292     m_wndBrowserView.SetFocus();
00293 }
00294 
00295 BOOL CBrowserFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
00296 {
00297     // let the view have first crack at the command
00298     if (m_wndBrowserView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
00299         return TRUE;
00300 
00301     // otherwise, do default handling
00302     return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
00303 }
00304 
00305 // Needed to properly position/resize the progress bar
00306 //
00307 void CBrowserFrame::OnSize(UINT nType, int cx, int cy) 
00308 {
00309        CFrameWnd::OnSize(nType, cx, cy);
00310     
00311        // Get the ItemRect of the status bar's Pane 1
00312        // That's where the progress bar will be located
00313        RECT rc;
00314        m_wndStatusBar.GetItemRect(1, &rc);
00315 
00316        // Move the progress bar into it's correct location
00317        //
00318        m_wndProgressBar.MoveWindow(&rc);
00319 }
00320 
00321 #ifdef _DEBUG
00322 void CBrowserFrame::AssertValid() const
00323 {
00324     CFrameWnd::AssertValid();
00325 }
00326 
00327 void CBrowserFrame::Dump(CDumpContext& dc) const
00328 {
00329     CFrameWnd::Dump(dc);
00330 }
00331 
00332 #endif //_DEBUG
00333 
00334 
00335 void CBrowserFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
00336 {
00337     CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
00338     
00339     m_wndBrowserView.Activate(nState, pWndOther, bMinimized);
00340 }
00341 
00342 #define IS_SECURE(state) ((state & 0xFFFF) == nsIWebProgressListener::STATE_IS_SECURE)
00343 void CBrowserFrame::UpdateSecurityStatus(PRInt32 aState)
00344 {
00345     int iResID = nsIWebProgressListener::STATE_IS_INSECURE;
00346     
00347     if(IS_SECURE(aState)){
00348         iResID = IDR_SECURITY_LOCK;
00349         m_wndBrowserView.m_SecurityState = CBrowserView::SECURITY_STATE_SECURE;
00350     }
00351     else if(aState == nsIWebProgressListener::STATE_IS_INSECURE) {
00352         iResID = IDR_SECURITY_UNLOCK;
00353         m_wndBrowserView.m_SecurityState = CBrowserView::SECURITY_STATE_INSECURE;
00354     }
00355     else if(aState == nsIWebProgressListener::STATE_IS_BROKEN) {
00356         iResID = IDR_SECURITY_BROKEN;
00357         m_wndBrowserView.m_SecurityState = CBrowserView::SECURITY_STATE_BROKEN;
00358     }
00359 
00360     CStatusBarCtrl& sb = m_wndStatusBar.GetStatusBarCtrl();
00361     sb.SetIcon(2, //2 is the pane index of the status bar where the lock icon will be shown
00362         (HICON)::LoadImage(AfxGetResourceHandle(),
00363         MAKEINTRESOURCE(iResID), IMAGE_ICON, 16,16,0));       
00364 }
00365 
00366 void CBrowserFrame::ShowSecurityInfo()
00367 {   
00368     m_wndBrowserView.ShowSecurityInfo();
00369 }
00370 
00371 // CMyStatusBar Class
00372 CMyStatusBar::CMyStatusBar()
00373 {
00374 }
00375 
00376 CMyStatusBar::~CMyStatusBar()
00377 {
00378 }
00379 
00380 BEGIN_MESSAGE_MAP(CMyStatusBar, CStatusBar)
00381     //{{AFX_MSG_MAP(CMyStatusBar)
00382     ON_WM_LBUTTONDOWN()
00383     //}}AFX_MSG_MAP
00384 END_MESSAGE_MAP()
00385 
00386 void CMyStatusBar::OnLButtonDown(UINT nFlags, CPoint point) 
00387 {
00388     // Check to see if the mouse click was within the
00389     // padlock icon pane(at pane index 2) of the status bar...
00390 
00391     RECT rc;
00392     GetItemRect(2, &rc );
00393 
00394     if(PtInRect(&rc, point)) 
00395     {
00396         CBrowserFrame *pFrame = (CBrowserFrame *)GetParent();
00397         if(pFrame != NULL)
00398             pFrame->ShowSecurityInfo();
00399     }
00400         
00401     CStatusBar::OnLButtonDown(nFlags, point);
00402 }