Back to index

lightning-sunbird  0.9+nobinonly
BrowserFrameGlue.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 // This file has the IBrowserFrameGlueObj implementation
00042 // This frame glue object is nested inside of the BrowserFrame
00043 // object(See BrowserFrm.h for more info)
00044 //
00045 // This is the place where all the platform specific interaction
00046 // with the browser frame window takes place in response to 
00047 // callbacks from Gecko interface methods
00048 // 
00049 // The main purpose of this interface is to separate the cross 
00050 // platform code in BrowserImpl*.cpp from the platform specific
00051 // code(which is in this file)
00052 //
00053 // You'll also notice the use of a macro named "METHOD_PROLOGUE"
00054 // through out this file. This macro essentially makes the pointer
00055 // to a "containing" class available inside of the class which is
00056 // being contained via a var named "pThis". In our case, the 
00057 // BrowserFrameGlue object is contained inside of the BrowserFrame
00058 // object so "pThis" will be a pointer to a BrowserFrame object
00059 // Refer to MFC docs for more info on METHOD_PROLOGUE macro
00060 
00061 
00062 #include "stdafx.h"
00063 #include "TestEmbed.h"
00064 #include "BrowserFrm.h"
00065 #include "Dialogs.h"
00066 #include "nsReadableUtils.h"
00067 
00069 // IBrowserFrameGlue implementation
00070 
00071 void CBrowserFrame::BrowserFrameGlueObj::UpdateStatusBarText(const PRUnichar *aMessage)
00072 {
00073        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00074 
00075        nsCString strStatus; 
00076 
00077     if(aMessage)
00078         strStatus.AssignWithConversion(aMessage);
00079 
00080     pThis->m_wndStatusBar.SetPaneText(0, strStatus.get());
00081 }
00082 
00083 void CBrowserFrame::BrowserFrameGlueObj::UpdateProgress(PRInt32 aCurrent, PRInt32 aMax)
00084 {
00085        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00086 
00087     pThis->m_wndProgressBar.SetRange32(0, aMax);
00088     pThis->m_wndProgressBar.SetPos(aCurrent);
00089 }
00090 
00091 void CBrowserFrame::BrowserFrameGlueObj::UpdateBusyState(PRBool aBusy)
00092 {      
00093        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00094 
00095        // Just notify the view of the busy state
00096        // There's code in there which will take care of
00097        // updating the STOP toolbar btn. etc
00098 
00099        pThis->m_wndBrowserView.UpdateBusyState(aBusy);
00100 }
00101 
00102 // Called from the OnLocationChange() method in the nsIWebProgressListener 
00103 // interface implementation in CBrowserImpl to update the current URI
00104 // Will get called after a URI is successfully loaded in the browser
00105 // We use this info to update the URL bar's edit box
00106 //
00107 void CBrowserFrame::BrowserFrameGlueObj::UpdateCurrentURI(nsIURI *aLocation)
00108 {
00109        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00110 
00111        if(aLocation)
00112        {
00113               nsCAutoString uriString; 
00114 //            nsXPIDLCString uriString;
00115               aLocation->GetSpec(uriString);
00116 
00117               pThis->m_wndUrlBar.SetCurrentURL(uriString.get());
00118        }
00119 }
00120 
00121 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameTitle(PRUnichar **aTitle)
00122 {
00123        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00124 
00125        CString title;
00126        pThis->GetWindowText(title);
00127 
00128        if(!title.IsEmpty())
00129        {
00130               nsString nsTitle;
00131               nsTitle.AssignWithConversion(title.GetBuffer(0));
00132 
00133               *aTitle = ToNewUnicode(nsTitle);
00134        }
00135 }
00136 
00137 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFrameTitle(const PRUnichar *aTitle)
00138 {
00139        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00140 
00141        USES_CONVERSION;
00142 
00143        if(W2T(aTitle))
00144        {
00145               pThis->SetWindowText(W2T(aTitle));
00146        }
00147        else
00148        {
00149               // Use the AppName i.e. testembed as the title if we
00150               // do not get one from GetBrowserWindowTitle()
00151               //
00152               CString cs;
00153               cs.LoadString(AFX_IDS_APP_TITLE);
00154               pThis->SetWindowText(cs);
00155        }
00156 }
00157 
00158 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFrameSize(PRInt32 aCX, PRInt32 aCY)
00159 {
00160        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00161        
00162        pThis->SetWindowPos(NULL, 0, 0, aCX, aCY, 
00163                             SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER
00164                      );
00165 }
00166 
00167 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameSize(PRInt32 *aCX, PRInt32 *aCY)
00168 {
00169        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00170 
00171        RECT wndRect;
00172        pThis->GetWindowRect(&wndRect);
00173 
00174        if (aCX)
00175               *aCX = wndRect.right - wndRect.left;
00176 
00177        if (aCY)
00178               *aCY = wndRect.bottom - wndRect.top;
00179 }
00180 
00181 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFramePosition(PRInt32 aX, PRInt32 aY)
00182 {
00183        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)     
00184 
00185        pThis->SetWindowPos(NULL, aX, aY, 0, 0, 
00186                             SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
00187 }
00188 
00189 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFramePosition(PRInt32 *aX, PRInt32 *aY)
00190 {
00191        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00192 
00193        RECT wndRect;
00194        pThis->GetWindowRect(&wndRect);
00195 
00196        if (aX)
00197               *aX = wndRect.left;
00198 
00199        if (aY)
00200               *aY = wndRect.top;
00201 }
00202 
00203 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFramePositionAndSize(PRInt32 *aX, PRInt32 *aY, PRInt32 *aCX, PRInt32 *aCY)
00204 {
00205        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00206 
00207        RECT wndRect;
00208        pThis->GetWindowRect(&wndRect);
00209 
00210        if (aX)
00211               *aX = wndRect.left;
00212 
00213        if (aY)
00214               *aY = wndRect.top;
00215 
00216        if (aCX)
00217               *aCX = wndRect.right - wndRect.left;
00218 
00219        if (aCY)
00220               *aCY = wndRect.bottom - wndRect.top;
00221 }
00222 
00223 void CBrowserFrame::BrowserFrameGlueObj::SetBrowserFramePositionAndSize(PRInt32 aX, PRInt32 aY, PRInt32 aCX, PRInt32 aCY, PRBool fRepaint)
00224 {
00225        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00226 
00227        pThis->SetWindowPos(NULL, aX, aY, aCX, aCY, 
00228                             SWP_NOACTIVATE | SWP_NOZORDER);
00229 }
00230 
00231 void CBrowserFrame::BrowserFrameGlueObj::SetFocus()
00232 {
00233        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00234 
00235        pThis->SetFocus();
00236 }
00237 
00238 void CBrowserFrame::BrowserFrameGlueObj::FocusAvailable(PRBool *aFocusAvail)
00239 {
00240        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00241 
00242        HWND focusWnd = GetFocus()->m_hWnd;
00243 
00244        if ((focusWnd == pThis->m_hWnd) || ::IsChild(pThis->m_hWnd, focusWnd))
00245               *aFocusAvail = PR_TRUE;
00246        else
00247               *aFocusAvail = PR_FALSE;
00248 }
00249 
00250 void CBrowserFrame::BrowserFrameGlueObj::ShowBrowserFrame(PRBool aShow)
00251 {
00252        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00253 
00254        if(aShow)
00255        {
00256               pThis->ShowWindow(SW_SHOW);
00257               pThis->SetActiveWindow();
00258               pThis->UpdateWindow();
00259        }
00260        else
00261        {
00262               pThis->ShowWindow(SW_HIDE);
00263        }
00264 }
00265 
00266 void CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameVisibility(PRBool *aVisible)
00267 {
00268        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00269 
00270        // Is the current BrowserFrame the active one?
00271        if (GetActiveWindow()->m_hWnd != pThis->m_hWnd)
00272        {
00273               *aVisible = PR_FALSE;
00274               return;
00275        }
00276 
00277        // We're the active one
00278        //Return FALSE if we're minimized
00279        WINDOWPLACEMENT wpl;
00280        pThis->GetWindowPlacement(&wpl);
00281 
00282        if ((wpl.showCmd == SW_RESTORE) || (wpl.showCmd == SW_MAXIMIZE))
00283               *aVisible = PR_TRUE;
00284        else
00285               *aVisible = PR_FALSE;
00286 }
00287 
00288 PRBool CBrowserFrame::BrowserFrameGlueObj::CreateNewBrowserFrame(PRUint32 chromeMask, 
00289                                                  PRInt32 x, PRInt32 y, 
00290                                                  PRInt32 cx, PRInt32 cy,
00291                                                  nsIWebBrowser** aWebBrowser)
00292 {
00293    NS_ENSURE_ARG_POINTER(aWebBrowser);
00294 
00295    *aWebBrowser = nsnull;
00296 
00297        CTestEmbedApp *pApp = (CTestEmbedApp *)AfxGetApp();
00298        if(!pApp)
00299               return PR_FALSE;
00300 
00301        // Note that we're calling with the last param set to "false" i.e.
00302        // this instructs not to show the frame window
00303        // This is mainly needed when the window size is specified in the window.open()
00304        // JS call. In those cases Gecko calls us to create the browser with a default
00305        // size (all are -1) and then it calls the SizeBrowserTo() method to set
00306        // the proper window size. If this window were to be visible then you'll see
00307        // the window size changes on the screen causing an unappealing flicker
00308        //
00309 
00310        CBrowserFrame* pFrm = pApp->CreateNewBrowserFrame(chromeMask, x, y, cx, cy, PR_FALSE);
00311     if(!pFrm)
00312               return PR_FALSE;
00313 
00314        // At this stage we have a new CBrowserFrame and a new CBrowserView
00315        // objects. The CBrowserView also would have an embedded browser
00316        // object created. Get the mWebBrowser member from the CBrowserView
00317        // and return it. (See CBrowserView's CreateBrowser() on how the
00318        // embedded browser gets created and how it's mWebBrowser member
00319        // gets initialized)
00320 
00321        NS_IF_ADDREF(*aWebBrowser = pFrm->m_wndBrowserView.mWebBrowser);
00322 
00323        return PR_TRUE;
00324 }
00325 
00326 void CBrowserFrame::BrowserFrameGlueObj::DestroyBrowserFrame()
00327 {
00328        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00329 
00330        pThis->PostMessage(WM_CLOSE);
00331 }
00332 
00333 #define GOTO_BUILD_CTX_MENU { bContentHasFrames = FALSE; goto BUILD_CTX_MENU; }
00334 
00335 void CBrowserFrame::BrowserFrameGlueObj::ShowContextMenu(PRUint32 aContextFlags, nsIDOMNode *aNode)
00336 {
00337        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00338 
00339        UINT nIDResource = IDR_CTXMENU_DOCUMENT;
00340 
00341        if(aContextFlags & nsIContextMenuListener::CONTEXT_DOCUMENT)
00342               nIDResource = IDR_CTXMENU_DOCUMENT;
00343        else if(aContextFlags & nsIContextMenuListener::CONTEXT_TEXT)         
00344               nIDResource = IDR_CTXMENU_TEXT;
00345        else if(aContextFlags & nsIContextMenuListener::CONTEXT_LINK)
00346        {
00347               nIDResource = IDR_CTXMENU_LINK;
00348 
00349               // Since we handle all the browser menu/toolbar commands
00350               // in the View, we basically setup the Link's URL in the
00351               // BrowserView object. When a menu selection in the context
00352               // menu is made, the appropriate command handler in the
00353               // BrowserView will be invoked and the value of the URL
00354               // will be accesible in the view
00355               
00356               // Reset the value from the last invocation
00357               // (A new value will be set after we determine it below)
00358               //
00359               nsAutoString strUrlUcs2;
00360               pThis->m_wndBrowserView.SetCtxMenuLinkUrl(strUrlUcs2);
00361 
00362               // Get the URL from the link. This is two step process
00363               // 1. We first get the nsIDOMHTMLAnchorElement
00364               // 2. We then get the URL associated with the link
00365               nsresult rv = NS_OK;
00366               nsCOMPtr<nsIDOMHTMLAnchorElement> linkElement(do_QueryInterface(aNode, &rv));
00367               if(NS_FAILED(rv))
00368                      return;
00369 
00370               rv = linkElement->GetHref(strUrlUcs2);
00371               if(NS_FAILED(rv))
00372                      return;
00373 
00374               // Update the view with the new LinkUrl
00375               // Note that this string is in UCS2 format
00376               pThis->m_wndBrowserView.SetCtxMenuLinkUrl(strUrlUcs2);
00377        }
00378        else if(aContextFlags & nsIContextMenuListener::CONTEXT_IMAGE)
00379        {
00380               nIDResource = IDR_CTXMENU_IMAGE;
00381 
00382               nsAutoString strImgSrcUcs2;
00383               pThis->m_wndBrowserView.SetCtxMenuImageSrc(strImgSrcUcs2); // Clear it
00384 
00385               // Get the IMG SRC
00386               nsresult rv = NS_OK;
00387               nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(aNode, &rv));
00388               if(NS_FAILED(rv))
00389                      return;
00390 
00391               rv = imgElement->GetSrc(strImgSrcUcs2);
00392               if(NS_FAILED(rv))
00393                      return;
00394 
00395               pThis->m_wndBrowserView.SetCtxMenuImageSrc(strImgSrcUcs2); // Set the new Img Src
00396        }
00397 
00398        CMenu ctxMenu;
00399        if(ctxMenu.LoadMenu(nIDResource))
00400        {
00401               POINT cursorPos;
00402               GetCursorPos(&cursorPos);
00403 
00404               (ctxMenu.GetSubMenu(0))->TrackPopupMenu(TPM_LEFTALIGN, cursorPos.x, cursorPos.y, pThis);
00405        }
00406 }
00407 
00408 HWND CBrowserFrame::BrowserFrameGlueObj::GetBrowserFrameNativeWnd()
00409 {
00410        METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00411        return pThis->m_hWnd;
00412 }
00413 
00414 void CBrowserFrame::BrowserFrameGlueObj::ShowTooltip(PRInt32 aXCoords, PRInt32 aYCoords, const PRUnichar *aTipText)
00415 {
00416     METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00417     pThis->m_wndTooltip.SetTipText(CString(aTipText));
00418     pThis->m_wndTooltip.Show(&pThis->m_wndBrowserView, aXCoords, aYCoords);
00419 }
00420 
00421 void CBrowserFrame::BrowserFrameGlueObj::HideTooltip()
00422 {
00423     METHOD_PROLOGUE(CBrowserFrame, BrowserFrameGlueObj)
00424     pThis->m_wndTooltip.Hide();
00425 }
00426