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: 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 // The typical MFC View, toolbar, statusbar creation done 
00042 // in CBrowserFrame::OnCreate()
00043 //
00044 // Code to update the Status/Tool bars in response to the
00045 // Web page loading progress(called from methods in CBrowserImpl)
00046 //
00047 // SetupFrameChrome() determines what, if any, UI elements this Frame
00048 // will sport based on the current "chromeMask" 
00049 //
00050 // Also take a look at OnClose() which gets used when you close a browser
00051 // window. This needs to be overrided mainly to handle supporting multiple
00052 // browser frame windows via the "New Browser Window" menu item
00053 // Without this being overridden the MFC framework handles the OnClose and
00054 // shutsdown the complete application when a frame window is closed.
00055 // In our case, we want the app to shutdown when the File/Exit menu is chosen
00056 //
00057 // Another key functionality this object implements is the IBrowserFrameGlue
00058 // interface - that's the interface the Gecko embedding interfaces call
00059 // upong to update the status bar etc.
00060 // (Take a look at IBrowserFrameGlue.h for the interface definition and
00061 // the BrowserFrm.h to see how we implement this interface - as a nested
00062 // class)
00063 // We pass this Glue object pointer to the CBrowserView object via the 
00064 // SetBrowserFrameGlue() method. The CBrowserView passes this on to the
00065 // embedding interface implementaion
00066 //
00067 // Please note the use of the macro METHOD_PROLOGUE in the implementation
00068 // of the nested BrowserFrameGlue object. Essentially what this macro does
00069 // is to get you access to the outer (or the object which is containing the
00070 // nested object) object via the pThis pointer.
00071 // Refer to the AFXDISP.H file in VC++ include dirs
00072 //
00073 // Next suggested file to look at : BrowserView.cpp
00074 
00075 #include "stdafx.h"
00076 #include "TestEmbed.h"
00077 #include "BrowserFrm.h"
00078 
00079 #ifdef _DEBUG
00080 #define new DEBUG_NEW
00081 #undef THIS_FILE
00082 static char THIS_FILE[] = __FILE__;
00083 #endif
00084 
00086 // CBrowserFrame
00087 
00088 IMPLEMENT_DYNAMIC(CBrowserFrame, CFrameWnd)
00089 
00090 BEGIN_MESSAGE_MAP(CBrowserFrame, CFrameWnd)
00091        //{{AFX_MSG_MAP(CBrowserFrame)
00092        ON_WM_CREATE()
00093        ON_WM_SETFOCUS()
00094        ON_WM_SIZE()
00095        ON_WM_CLOSE()
00096        ON_WM_ACTIVATE()
00097        //}}AFX_MSG_MAP
00098 END_MESSAGE_MAP()
00099 
00100 static UINT indicators[] =
00101 {
00102        ID_SEPARATOR,           // For the Status line
00103        ID_SEPARATOR,           // For the Progress Bar
00104 };
00105 
00107 // CBrowserFrame construction/destruction
00108 
00109 CBrowserFrame::CBrowserFrame(PRUint32 chromeMask)
00110 {
00111        // Save the chromeMask off. It'll be used
00112        // later to determine whether this browser frame
00113        // will have menubar, toolbar, statusbar etc.
00114 
00115        m_chromeMask = chromeMask;
00116 }
00117 
00118 CBrowserFrame::~CBrowserFrame()
00119 {
00120 }
00121 
00122 void CBrowserFrame::OnClose()
00123 {
00124        CTestEmbedApp *pApp = (CTestEmbedApp *)AfxGetApp();
00125        pApp->RemoveFrameFromList(this);
00126 
00127        DestroyWindow();
00128 }
00129 
00130 // This is where the UrlBar, ToolBar, StatusBar, ProgressBar
00131 // get created
00132 //
00133 int CBrowserFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
00134 {
00135        if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
00136               return -1;
00137 
00138        // Pass "this" to the View for later callbacks
00139        // and/or access to any public data members, if needed
00140        //
00141        m_wndBrowserView.SetBrowserFrame(this);
00142 
00143        // Pass on the BrowserFrameGlue also to the View which
00144        // it will use during the Init() process after creation
00145        // of the BrowserImpl obj. Essentially, the View object
00146        // hooks up the Embedded browser's callbacks to the BrowserFrame
00147        // via this BrowserFrameGlue object
00148        m_wndBrowserView.SetBrowserFrameGlue((PBROWSERFRAMEGLUE)&m_xBrowserFrameGlueObj);
00149 
00150        // create a view to occupy the client area of the frame
00151        // This will be the view in which the embedded browser will
00152        // be displayed in
00153        //
00154        if (!m_wndBrowserView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
00155               CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
00156        {
00157               TRACE0("Failed to create view window\n");
00158               return -1;
00159        }
00160 
00161        // create the URL bar (essentially a ComboBoxEx object)
00162        if (!m_wndUrlBar.Create(CBS_DROPDOWN | WS_CHILD, CRect(0, 0, 200, 150), this, ID_URL_BAR))
00163        {
00164               TRACE0("Failed to create URL Bar\n");
00165               return -1;      // fail to create
00166        }
00167     
00168     // Load the Most Recently Used(MRU) Urls into the UrlBar
00169     m_wndUrlBar.LoadMRUList();
00170 
00171        // Create the toolbar with Back, Fwd, Stop, etc. buttons..
00172        if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
00173               | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
00174               !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
00175        {
00176               TRACE0("Failed to create toolbar\n");
00177               return -1;      // fail to create
00178        }
00179 
00180        // Create a ReBar window to which the toolbar and UrlBar 
00181        // will be added
00182        if (!m_wndReBar.Create(this))
00183        {
00184               TRACE0("Failed to create ReBar\n");
00185               return -1;      // fail to create
00186        }
00187        
00188        //Add the ToolBar and UrlBar windows to the rebar
00189        m_wndReBar.AddBar(&m_wndToolBar);
00190        m_wndReBar.AddBar(&m_wndUrlBar, "Enter URL:");
00191 
00192        // Create the status bar with two panes - one pane for actual status
00193        // text msgs. and the other for the progress control
00194        if (!m_wndStatusBar.Create(this) ||
00195               !m_wndStatusBar.SetIndicators(indicators,
00196                 sizeof(indicators)/sizeof(UINT)))
00197        {
00198               TRACE0("Failed to create status bar\n");
00199               return -1;      // fail to create
00200        }
00201 
00202        // Create the progress bar as a child of the status bar.
00203        // Note that the ItemRect which we'll get at this stage
00204        // is bogus since the status bar panes are not fully
00205        // positioned yet i.e. we'll be passing in an invalid rect
00206        // to the Create function below
00207        // The actual positioning of the progress bar will be done
00208        // in response to OnSize()
00209        RECT rc;
00210        m_wndStatusBar.GetItemRect (1, &rc);
00211        if (!m_wndProgressBar.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH, rc, &m_wndStatusBar, ID_PROG_BAR))
00212        {
00213               TRACE0("Failed to create progress bar\n");
00214               return -1;      // fail to create
00215        }
00216 
00217        // The third pane(i.e. at index 2) of the status bar will have 
00218        // the security lock icon displayed in it. Set up it's size(16) 
00219        // and style(no border)so that the padlock icons can be properly drawn
00220 //     m_wndStatusBar.SetPaneInfo(2, -1, SBPS_NORMAL|SBPS_NOBORDERS, 16);
00221 
00222        // Based on the "chromeMask" we were supplied during construction
00223        // hide any requested UI elements - statusbar, menubar etc...
00224        // Note that the window styles (WM_RESIZE etc) are set inside
00225        // of PreCreateWindow()
00226 
00227        SetupFrameChrome(); 
00228 
00229        return 0;
00230 }
00231 
00232 void CBrowserFrame::SetupFrameChrome()
00233 {
00234        if(m_chromeMask == nsIWebBrowserChrome::CHROME_ALL)
00235               return;
00236 
00237        if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_MENUBAR) )
00238               SetMenu(NULL); // Hide the MenuBar
00239 
00240        if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_TOOLBAR) )
00241               m_wndReBar.ShowWindow(SW_HIDE); // Hide the ToolBar
00242 
00243        if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_STATUSBAR) )
00244               m_wndStatusBar.ShowWindow(SW_HIDE); // Hide the StatusBar
00245 }
00246 
00247 BOOL CBrowserFrame::PreCreateWindow(CREATESTRUCT& cs)
00248 {
00249        if( !CFrameWnd::PreCreateWindow(cs) )
00250               return FALSE;
00251 
00252        cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
00253 
00254        // Change window style based on the chromeMask
00255 
00256        if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_TITLEBAR) )
00257               cs.style &= ~WS_CAPTION; // No caption           
00258 
00259        if(! (m_chromeMask & nsIWebBrowserChrome::CHROME_WINDOW_RESIZE) )
00260        {
00261               // Can't resize this window
00262               cs.style &= ~WS_SIZEBOX;
00263               cs.style &= ~WS_THICKFRAME;
00264               cs.style &= ~WS_MINIMIZEBOX;
00265               cs.style &= ~WS_MAXIMIZEBOX;
00266        }
00267 
00268        cs.lpszClass = AfxRegisterWndClass(0);
00269 
00270        return TRUE;
00271 }
00272 
00274 // CBrowserFrame message handlers
00275 void CBrowserFrame::OnSetFocus(CWnd* pOldWnd)
00276 {
00277        // forward focus to the view window
00278        m_wndBrowserView.SetFocus();
00279 }
00280 
00281 BOOL CBrowserFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
00282 {
00283        // let the view have first crack at the command
00284        if (m_wndBrowserView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
00285               return TRUE;
00286 
00287        // otherwise, do default handling
00288        return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
00289 }
00290 
00291 // Needed to properly position/resize the progress bar
00292 //
00293 void CBrowserFrame::OnSize(UINT nType, int cx, int cy) 
00294 {
00295        CFrameWnd::OnSize(nType, cx, cy);
00296        
00297        // Get the ItemRect of the status bar's Pane 1
00298        // That's where the progress bar will be located
00299        RECT rc;
00300        m_wndStatusBar.GetItemRect(1, &rc);
00301 
00302        // Move the progress bar into it's correct location
00303        //
00304        m_wndProgressBar.MoveWindow(&rc);
00305 }
00306 
00307 #ifdef _DEBUG
00308 void CBrowserFrame::AssertValid() const
00309 {
00310        CFrameWnd::AssertValid();
00311 }
00312 
00313 void CBrowserFrame::Dump(CDumpContext& dc) const
00314 {
00315        CFrameWnd::Dump(dc);
00316 }
00317 
00318 #endif //_DEBUG
00319 
00320 
00321 void CBrowserFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
00322 {
00323        CFrameWnd::OnActivate(nState, pWndOther, bMinimized);
00324        
00325     m_wndBrowserView.Activate(nState, pWndOther, bMinimized);
00326 }