Back to index

lightning-sunbird  0.9+nobinonly
BrowserImpl.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 // This is the class which implements all the interfaces
00034 // required(and optional) by the mozilla embeddable browser engine
00035 //
00036 // Note that this obj gets passed in the IBrowserFrameGlue* using the 
00037 // Init() method. Many of the interface implementations use this
00038 // to get the actual task done. Ex: to update the status bar
00039 //
00040 // Look at the INTERFACE_MAP_ENTRY's below for the list of 
00041 // the currently implemented interfaces
00042 //
00043 // This file currently has the implementation for all the interfaces
00044 // which are required of an app embedding Gecko
00045 // Implementation of other optional interfaces are in separate files
00046 // to avoid cluttering this one and to demonstrate other embedding
00047 // principles. For example, nsIPrompt is implemented in a separate DLL.
00048 // 
00049 //    nsIWebBrowserChrome    - This is a required interface to be implemented
00050 //                          by embeddors
00051 //
00052 //    nsIEmbeddingSiteWindow - This is a required interface to be implemented
00053 //                         by embedders            
00054 //                       - SetTitle() gets called after a document
00055 //                         load giving us the chance to update our
00056 //                         titlebar
00057 //
00058 // Some points to note...
00059 //
00060 // nsIWebBrowserChrome interface's SetStatus() gets called when a user 
00061 // mouses over the links on a page
00062 //
00063 // nsIWebProgressListener interface's OnStatusChange() gets called
00064 // to indicate progress when a page is being loaded
00065 //
00066 // Next suggested file(s) to look at : 
00067 //            Any of the BrowserImpl*.cpp files for other interface
00068 //            implementation details
00069 //
00070 
00071 #ifdef _WINDOWS
00072   #include "stdafx.h"
00073 #endif
00074 
00075 #include "nsIDOMWindow.h"
00076 #include "BrowserImpl.h"
00077 
00078 CBrowserImpl::CBrowserImpl()
00079 {
00080   m_pBrowserFrameGlue = NULL;
00081   mWebBrowser = nsnull;
00082 }
00083 
00084 
00085 CBrowserImpl::~CBrowserImpl()
00086 {
00087 }
00088 
00089 // It's very important that the creator of this CBrowserImpl object
00090 // Call on this Init() method with proper values after creation
00091 //
00092 NS_METHOD CBrowserImpl::Init(PBROWSERFRAMEGLUE pBrowserFrameGlue, 
00093                              nsIWebBrowser* aWebBrowser)
00094 {
00095       m_pBrowserFrameGlue = pBrowserFrameGlue;
00096       
00097       SetWebBrowser(aWebBrowser);
00098 
00099       return NS_OK;
00100 }
00101 
00102 //*****************************************************************************
00103 // CBrowserImpl::nsISupports
00104 //*****************************************************************************   
00105 
00106 NS_IMPL_ADDREF(CBrowserImpl)
00107 NS_IMPL_RELEASE(CBrowserImpl)
00108 
00109 NS_INTERFACE_MAP_BEGIN(CBrowserImpl)
00110    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebBrowserChrome)
00111    NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
00112    NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome)
00113    NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChromeFocus)
00114    NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow)
00115    NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow2)
00116    NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
00117    NS_INTERFACE_MAP_ENTRY(nsIContextMenuListener2)
00118    NS_INTERFACE_MAP_ENTRY(nsITooltipListener)
00119    NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
00120 NS_INTERFACE_MAP_END
00121 
00122 //*****************************************************************************
00123 // CBrowserImpl::nsIInterfaceRequestor
00124 //*****************************************************************************   
00125 
00126 NS_IMETHODIMP CBrowserImpl::GetInterface(const nsIID &aIID, void** aInstancePtr)
00127 {
00128     if(aIID.Equals(NS_GET_IID(nsIDOMWindow)))
00129     {
00130         if (mWebBrowser)
00131             return mWebBrowser->GetContentDOMWindow((nsIDOMWindow **) aInstancePtr);
00132         return NS_ERROR_NOT_INITIALIZED;
00133     }
00134 
00135     return QueryInterface(aIID, aInstancePtr);
00136 }
00137 
00138 //*****************************************************************************
00139 // CBrowserImpl::nsIWebBrowserChrome
00140 //*****************************************************************************   
00141 
00142 //Gets called when you mouseover links etc. in a web page
00143 //
00144 NS_IMETHODIMP CBrowserImpl::SetStatus(PRUint32 aType, const PRUnichar* aStatus)
00145 {
00146     if(! m_pBrowserFrameGlue)
00147         return NS_ERROR_FAILURE;
00148 
00149     m_pBrowserFrameGlue->UpdateStatusBarText(aStatus);
00150 
00151     return NS_OK;
00152 }
00153 
00154 NS_IMETHODIMP CBrowserImpl::GetWebBrowser(nsIWebBrowser** aWebBrowser)
00155 {
00156    NS_ENSURE_ARG_POINTER(aWebBrowser);
00157 
00158    *aWebBrowser = mWebBrowser;
00159 
00160    NS_IF_ADDREF(*aWebBrowser);
00161 
00162    return NS_OK;
00163 }
00164 
00165 // Currently called from Init(). I'm not sure who else
00166 // calls this
00167 //
00168 NS_IMETHODIMP CBrowserImpl::SetWebBrowser(nsIWebBrowser* aWebBrowser)
00169 {
00170    NS_ENSURE_ARG_POINTER(aWebBrowser);
00171 
00172    mWebBrowser = aWebBrowser;
00173 
00174    return NS_OK;
00175 }
00176 
00177 NS_IMETHODIMP CBrowserImpl::GetChromeFlags(PRUint32* aChromeMask)
00178 {
00179    return NS_ERROR_NOT_IMPLEMENTED;
00180 }
00181 
00182 NS_IMETHODIMP CBrowserImpl::SetChromeFlags(PRUint32 aChromeMask)
00183 {
00184    return NS_ERROR_NOT_IMPLEMENTED;
00185 }
00186 
00187 // Will get called in response to JavaScript window.close()
00188 //
00189 NS_IMETHODIMP CBrowserImpl::DestroyBrowserWindow()
00190 {
00191     if(! m_pBrowserFrameGlue)
00192         return NS_ERROR_FAILURE;
00193 
00194     m_pBrowserFrameGlue->DestroyBrowserFrame();
00195 
00196     return NS_OK;
00197 }
00198 
00199 // Gets called in response to set the size of a window
00200 // Ex: In response to a JavaScript Window.Open() call of
00201 // the form 
00202 //
00203 //        window.open("http://www.mozilla.org", "theWin", "width=200, height=400");
00204 //
00205 // First the CreateBrowserWindow() call will be made followed by a 
00206 // call to this method to resize the window
00207 //
00208 NS_IMETHODIMP CBrowserImpl::SizeBrowserTo(PRInt32 aCX, PRInt32 aCY)
00209 {
00210     if(! m_pBrowserFrameGlue)
00211         return NS_ERROR_FAILURE;
00212 
00213     HWND w = m_pBrowserFrameGlue->GetBrowserFrameNativeWnd();
00214 
00215     CRect rcNewFrame(CPoint(0, 0), CSize(aCX, aCY));
00216     CRect rcFrame;
00217     CRect rcClient;
00218 
00219     // Adjust for 3D edge on client area
00220     AdjustWindowRectEx(&rcNewFrame, WS_VISIBLE, FALSE, WS_EX_CLIENTEDGE);
00221 
00222     GetClientRect(w, &rcClient);
00223     GetWindowRect(w, &rcFrame);
00224 
00225     rcNewFrame.right += rcFrame.Width() - rcClient.Width();
00226     rcNewFrame.bottom += rcFrame.Height() - rcClient.Height();
00227 
00228     m_pBrowserFrameGlue->SetBrowserFrameSize(rcNewFrame.Width(), rcNewFrame.Height());
00229 
00230     return NS_OK;
00231 }
00232 
00233 NS_IMETHODIMP CBrowserImpl::ShowAsModal(void)
00234 {
00235    return NS_ERROR_NOT_IMPLEMENTED;
00236 }
00237 
00238 NS_IMETHODIMP CBrowserImpl::IsWindowModal(PRBool *retval)
00239 {
00240   // We're not modal
00241   *retval = PR_FALSE;
00242 
00243   return NS_OK;
00244 }
00245 
00246 NS_IMETHODIMP CBrowserImpl::ExitModalEventLoop(nsresult aStatus)
00247 {
00248   return NS_OK;
00249 }
00250 
00251 //*****************************************************************************
00252 // CBrowserImpl::nsIWebBrowserChromeFocus
00253 //*****************************************************************************
00254 
00255 NS_IMETHODIMP CBrowserImpl::FocusNextElement()
00256 {
00257     return NS_ERROR_NOT_IMPLEMENTED;
00258 }
00259 
00260 NS_IMETHODIMP CBrowserImpl::FocusPrevElement()
00261 {
00262     return NS_ERROR_NOT_IMPLEMENTED;
00263 }
00264 
00265 //*****************************************************************************
00266 // CBrowserImpl::nsIEmbeddingSiteWindow
00267 //*****************************************************************************   
00268 
00269 NS_IMETHODIMP CBrowserImpl::SetDimensions(PRUint32 aFlags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
00270 {
00271     if(! m_pBrowserFrameGlue)
00272         return NS_ERROR_FAILURE;
00273 
00274     if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION &&
00275         (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER || 
00276          aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER))
00277     {
00278         m_pBrowserFrameGlue->SetBrowserFramePositionAndSize(x, y, cx, cy, PR_TRUE);
00279     }
00280     else
00281     {
00282         if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION)
00283         {
00284             m_pBrowserFrameGlue->SetBrowserFramePosition(x, y);
00285         }
00286         if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER || 
00287             aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER)
00288         {
00289             m_pBrowserFrameGlue->SetBrowserFrameSize(cx, cy);
00290         }
00291     }
00292 
00293     return NS_OK;
00294 }
00295 
00296 NS_IMETHODIMP CBrowserImpl::GetDimensions(PRUint32 aFlags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
00297 {
00298     if(! m_pBrowserFrameGlue)
00299         return NS_ERROR_FAILURE;
00300     
00301     if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION)
00302     {
00303         m_pBrowserFrameGlue->GetBrowserFramePosition(x, y);
00304     }
00305     if (aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER || 
00306         aFlags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER)
00307     {
00308         m_pBrowserFrameGlue->GetBrowserFrameSize(cx, cy);
00309     }
00310 
00311     return NS_OK;
00312 }
00313 
00314 NS_IMETHODIMP CBrowserImpl::GetSiteWindow(void** aSiteWindow)
00315 {
00316   if (!aSiteWindow)
00317     return NS_ERROR_NULL_POINTER;
00318 
00319   *aSiteWindow = 0;
00320   if (m_pBrowserFrameGlue) {
00321     HWND w = m_pBrowserFrameGlue->GetBrowserFrameNativeWnd();
00322     *aSiteWindow = NS_REINTERPRET_CAST(void *, w);
00323   }
00324   return NS_OK;
00325 }
00326 
00327 NS_IMETHODIMP CBrowserImpl::SetFocus()
00328 {
00329     if(! m_pBrowserFrameGlue)
00330         return NS_ERROR_FAILURE;
00331 
00332     m_pBrowserFrameGlue->SetFocus();
00333 
00334     return NS_OK;
00335 }
00336 
00337 NS_IMETHODIMP CBrowserImpl::GetTitle(PRUnichar** aTitle)
00338 {
00339     if(! m_pBrowserFrameGlue)
00340         return NS_ERROR_FAILURE;
00341 
00342     m_pBrowserFrameGlue->GetBrowserFrameTitle(aTitle);
00343     
00344     return NS_OK;
00345 }
00346 
00347 NS_IMETHODIMP CBrowserImpl::SetTitle(const PRUnichar* aTitle)
00348 {
00349     if(! m_pBrowserFrameGlue)
00350         return NS_ERROR_FAILURE;
00351 
00352     m_pBrowserFrameGlue->SetBrowserFrameTitle(aTitle);
00353     
00354     return NS_OK;
00355 }
00356 
00357 NS_IMETHODIMP CBrowserImpl::GetVisibility(PRBool *aVisibility)
00358 {
00359     if(! m_pBrowserFrameGlue)
00360         return NS_ERROR_FAILURE;
00361 
00362     m_pBrowserFrameGlue->GetBrowserFrameVisibility(aVisibility);
00363 
00364     return NS_OK;
00365 }
00366 
00367 NS_IMETHODIMP CBrowserImpl::SetVisibility(PRBool aVisibility)
00368 {
00369     if(! m_pBrowserFrameGlue)
00370         return NS_ERROR_FAILURE;
00371 
00372     m_pBrowserFrameGlue->ShowBrowserFrame(aVisibility);
00373 
00374     return NS_OK;
00375 }
00376 
00377 //*****************************************************************************
00378 // CBrowserImpl::nsIEmbeddingSiteWindow2
00379 //*****************************************************************************   
00380 
00381 NS_IMETHODIMP CBrowserImpl::Blur()
00382 {
00383     return NS_OK;
00384 }
00385 
00386 
00387 //*****************************************************************************
00388 // CBrowserImpl::nsITooltipListener
00389 //*****************************************************************************   
00390 
00391 /* void onShowTooltip (in long aXCoords, in long aYCoords, in wstring aTipText); */
00392 NS_IMETHODIMP CBrowserImpl::OnShowTooltip(PRInt32 aXCoords, PRInt32 aYCoords, const PRUnichar *aTipText)
00393 {
00394     if(! m_pBrowserFrameGlue)
00395         return NS_ERROR_FAILURE;
00396 
00397     m_pBrowserFrameGlue->ShowTooltip(aXCoords, aYCoords, aTipText);
00398 
00399     return NS_OK;
00400 }
00401 
00402 /* void onHideTooltip (); */
00403 NS_IMETHODIMP CBrowserImpl::OnHideTooltip()
00404 {
00405     if(! m_pBrowserFrameGlue)
00406         return NS_ERROR_FAILURE;
00407 
00408     m_pBrowserFrameGlue->HideTooltip();
00409 
00410     return NS_OK;
00411 }