Back to index

lightning-sunbird  0.9+nobinonly
CWebBrowserChrome.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Travis Bogard <travis@netscape.com>
00025  *   Conrad Carlen <ccarlen@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 // Local Includes
00042 #include "CWebBrowserChrome.h"
00043 #include "CBrowserWindow.h"
00044 #include "CBrowserShell.h"
00045 
00046 #include "nsIGenericFactory.h"
00047 #include "nsString.h"
00048 #include "nsXPIDLString.h"
00049 #include "nsReadableUtils.h"
00050 #include "nsIURI.h"
00051 #include "nsIWebProgress.h"
00052 #include "nsIDocShellTreeItem.h"
00053 #include "nsIRequest.h"
00054 #include "nsIChannel.h"
00055 #include "nsIDOMWindow.h"
00056 
00057 #include "UMacUnicode.h"
00058 #include "ApplIDs.h"
00059 
00060 #include <LStaticText.h>
00061 #include <LCheckBox.h>
00062 #include <LEditText.h>
00063 #include <UModalDialogs.h>
00064 #include <LPushButton.h>
00065 
00066 // Interfaces needed to be included
00067 
00068 // Defines
00069 #define USE_BALLOONS_FOR_TOOL_TIPS 0 // Using balloons for this is really obnoxious
00070 
00071 // Constants
00072 const PRInt32     kGrowIconSize = 15;
00073 
00074 //*****************************************************************************
00075 //***    CWebBrowserChrome: Object Management
00076 //*****************************************************************************
00077 
00078 CWebBrowserChrome::CWebBrowserChrome() :
00079    mBrowserWindow(nsnull), mBrowserShell(nsnull),
00080    mPreviousBalloonState(false), mInModalLoop(false)
00081 {
00082 }
00083 
00084 CWebBrowserChrome::~CWebBrowserChrome()
00085 {
00086 }
00087 
00088 //*****************************************************************************
00089 // CWebBrowserChrome::nsISupports
00090 //*****************************************************************************   
00091 
00092 NS_IMPL_ADDREF(CWebBrowserChrome)
00093 NS_IMPL_RELEASE(CWebBrowserChrome)
00094 
00095 NS_INTERFACE_MAP_BEGIN(CWebBrowserChrome)
00096    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebBrowserChrome)
00097    NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
00098    NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome)
00099    NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChromeFocus)
00100    NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
00101    NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow)
00102    NS_INTERFACE_MAP_ENTRY(nsIContextMenuListener)
00103    NS_INTERFACE_MAP_ENTRY(nsITooltipListener)
00104    NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
00105 NS_INTERFACE_MAP_END
00106 
00107 //*****************************************************************************
00108 // CWebBrowserChrome::nsIInterfaceRequestor
00109 //*****************************************************************************   
00110 
00111 NS_IMETHODIMP CWebBrowserChrome::GetInterface(const nsIID &aIID, void** aInstancePtr)
00112 {
00113    if (aIID.Equals(NS_GET_IID(nsIDOMWindow)))
00114    {
00115       nsCOMPtr<nsIWebBrowser> browser;
00116       GetWebBrowser(getter_AddRefs(browser));
00117       if (browser)
00118          return browser->GetContentDOMWindow((nsIDOMWindow **) aInstancePtr);
00119       return NS_ERROR_NOT_INITIALIZED;
00120    }
00121 
00122    return QueryInterface(aIID, aInstancePtr);
00123 }
00124 
00125 //*****************************************************************************
00126 // CWebBrowserChrome::nsIWebBrowserChrome
00127 //*****************************************************************************   
00128 
00129 NS_IMETHODIMP CWebBrowserChrome::SetStatus(PRUint32 statusType, const PRUnichar *status)
00130 {
00131    NS_ENSURE_TRUE(mBrowserWindow, NS_ERROR_NOT_INITIALIZED);
00132 
00133    if (statusType == STATUS_SCRIPT) 
00134       mBrowserWindow->SetStatus(status);
00135    else if (statusType == STATUS_LINK)
00136       mBrowserWindow->SetOverLink(status);
00137   
00138    return NS_OK;
00139 }
00140 
00141 NS_IMETHODIMP CWebBrowserChrome::GetWebBrowser(nsIWebBrowser** aWebBrowser)
00142 {
00143    NS_ENSURE_ARG_POINTER(aWebBrowser);
00144    NS_ENSURE_TRUE(mBrowserShell, NS_ERROR_NOT_INITIALIZED);
00145 
00146    mBrowserShell->GetWebBrowser(aWebBrowser);
00147    return NS_OK;
00148 }
00149 
00150 NS_IMETHODIMP CWebBrowserChrome::SetWebBrowser(nsIWebBrowser* aWebBrowser)
00151 {
00152    NS_ENSURE_ARG(aWebBrowser);   // Passing nsnull is NOT OK
00153    NS_ENSURE_TRUE(mBrowserShell, NS_ERROR_NOT_INITIALIZED);
00154 
00155    mBrowserShell->SetWebBrowser(aWebBrowser);
00156    return NS_OK;
00157 }
00158 
00159 NS_IMETHODIMP CWebBrowserChrome::GetChromeFlags(PRUint32* aChromeMask)
00160 {
00161    NS_ERROR("Haven't Implemented this yet");
00162    return NS_ERROR_FAILURE;
00163 }
00164 
00165 NS_IMETHODIMP CWebBrowserChrome::SetChromeFlags(PRUint32 aChromeMask)
00166 {
00167    NS_ERROR("Haven't Implemented this yet");
00168    return NS_ERROR_FAILURE;
00169 }
00170 
00171 NS_IMETHODIMP CWebBrowserChrome::DestroyBrowserWindow()
00172 {
00173     mInModalLoop = false;
00174     delete mBrowserWindow;
00175     return NS_OK;
00176 }
00177 
00178 NS_IMETHODIMP CWebBrowserChrome::IsWindowModal(PRBool *_retval)
00179 {
00180     *_retval = PR_FALSE;
00181     return NS_OK;
00182 }
00183 
00184 
00185 NS_IMETHODIMP CWebBrowserChrome::SizeBrowserTo(PRInt32 aCX, PRInt32 aCY)
00186 {
00187    CBrowserShell *browserShell = mBrowserWindow->GetBrowserShell();
00188    NS_ENSURE_TRUE(browserShell, NS_ERROR_NULL_POINTER);
00189    
00190    SDimension16 curSize;
00191    browserShell->GetFrameSize(curSize);
00192    mBrowserWindow->ResizeWindowBy(aCX - curSize.width, aCY - curSize.height);
00193    mBrowserWindow->SetSizeToContent(false);
00194    return NS_OK;
00195 }
00196 
00197 
00198 NS_IMETHODIMP CWebBrowserChrome::ShowAsModal(void)
00199 {
00200     // We need this override because StDialogHandler deletes
00201     // its window in its destructor. We don't want that here. 
00202     class CChromeDialogHandler : public StDialogHandler
00203     {
00204         public:
00205                                           CChromeDialogHandler(LWindow*             inWindow,
00206                                                                      LCommander*    inSuper) :
00207                                            StDialogHandler(inWindow, inSuper)
00208                                        { }
00209                                            
00210            virtual         ~CChromeDialogHandler()
00211                            { mDialog = nil; }
00212     };
00213     
00214     CChromeDialogHandler     theHandler(mBrowserWindow, mBrowserWindow->GetSuperCommander());
00215        
00216        // Set to false by ExitModalEventLoop or DestroyBrowserWindow
00217        mInModalLoop = true;
00218        while (mInModalLoop) 
00219            theHandler.DoDialog();
00220 
00221     return NS_OK;
00222 
00223 }
00224 
00225 NS_IMETHODIMP CWebBrowserChrome::ExitModalEventLoop(nsresult aStatus)
00226 {
00227    mInModalLoop = false;
00228    return NS_OK;
00229 }
00230 
00231 //*****************************************************************************
00232 // CWebBrowserChrome::nsIWebBrowserChromeFocus
00233 //*****************************************************************************
00234 
00235 NS_IMETHODIMP CWebBrowserChrome::FocusNextElement()
00236 {
00237   return NS_ERROR_NOT_IMPLEMENTED;
00238 }
00239 
00240 NS_IMETHODIMP CWebBrowserChrome::FocusPrevElement()
00241 {
00242   return NS_ERROR_NOT_IMPLEMENTED;
00243 }
00244 
00245 //*****************************************************************************
00246 // CWebBrowserChrome::nsIWebProgressListener
00247 //*****************************************************************************   
00248 
00249 NS_IMETHODIMP CWebBrowserChrome::OnProgressChange(nsIWebProgress *progress, nsIRequest *request,
00250                                                   PRInt32 curSelfProgress, PRInt32 maxSelfProgress,
00251                                                   PRInt32 curTotalProgress, PRInt32 maxTotalProgress)
00252 {
00253        NS_ENSURE_TRUE(mBrowserWindow, NS_OK);
00254        
00255    return mBrowserWindow->OnProgressChange(progress, request,
00256                                            curSelfProgress, maxSelfProgress,
00257                                            curTotalProgress, maxTotalProgress);
00258 }
00259 
00260 NS_IMETHODIMP CWebBrowserChrome::OnStateChange(nsIWebProgress *progress, nsIRequest *request,
00261                                                PRInt32 progressStateFlags, PRUint32 status)
00262 {
00263        NS_ENSURE_TRUE(mBrowserWindow, NS_OK);
00264        
00265     if (progressStateFlags & STATE_IS_NETWORK) {
00266       if (progressStateFlags & STATE_START)
00267          mBrowserWindow->OnStatusNetStart(progress, request, progressStateFlags, status);
00268       else if (progressStateFlags & STATE_STOP)
00269              mBrowserWindow->OnStatusNetStop(progress, request, progressStateFlags, status);
00270     }
00271 
00272    return NS_OK;
00273 }
00274 
00275 
00276 NS_IMETHODIMP CWebBrowserChrome::OnLocationChange(nsIWebProgress* aWebProgress,
00277                                                   nsIRequest* aRequest,
00278                                                   nsIURI *location)
00279 {
00280        NS_ENSURE_TRUE(mBrowserWindow, NS_OK);
00281 
00282  
00283        nsCAutoString buf;
00284        if (location)
00285               location->GetSpec(buf);
00286 
00287        nsAutoString tmp;
00288        CopyUTF8toUTF16(buf, tmp);
00289        mBrowserWindow->SetLocation(tmp);
00290 
00291        return NS_OK;
00292 }
00293 
00294 NS_IMETHODIMP 
00295 CWebBrowserChrome::OnStatusChange(nsIWebProgress* aWebProgress,
00296                                   nsIRequest* aRequest,
00297                                   nsresult aStatus,
00298                                   const PRUnichar* aMessage)
00299 {
00300     return NS_OK;
00301 }
00302 
00303 
00304 
00305 NS_IMETHODIMP 
00306 CWebBrowserChrome::OnSecurityChange(nsIWebProgress *aWebProgress, 
00307                                     nsIRequest *aRequest, 
00308                                     PRUint32 state)
00309 {
00310     return NS_OK;
00311 }
00312 
00313 
00314 //*****************************************************************************
00315 // CWebBrowserChrome::nsIEmbeddingSiteWindow
00316 //*****************************************************************************   
00317 
00318 NS_IMETHODIMP CWebBrowserChrome::SetDimensions(PRUint32 flags, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)
00319 {
00320     NS_ENSURE_STATE(mBrowserWindow);
00321 
00322     nsresult rv = NS_OK;
00323     CBrowserShell *browserShell;
00324         
00325     if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION)    // setting position
00326     {
00327         if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER)
00328         {
00329             // Don't allow setting the position of the embedded area
00330             rv = NS_ERROR_UNEXPECTED;
00331         }
00332         else // (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER)
00333         {
00334             mBrowserWindow->MoveWindowTo(x, y);
00335         }
00336     }
00337     else                                                        // setting size
00338     {
00339         mBrowserWindow->SetSizeToContent(false);
00340         if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER)
00341         {
00342             browserShell = mBrowserWindow->GetBrowserShell();
00343             NS_ENSURE_TRUE(browserShell, NS_ERROR_NULL_POINTER);
00344             SDimension16 curSize;
00345             browserShell->GetFrameSize(curSize);
00346             mBrowserWindow->ResizeWindowBy(cx - curSize.width, cy - curSize.height);
00347         }
00348         else // (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER)
00349         {
00350             if (mBrowserWindow->HasAttribute(windAttr_Resizable /*windAttr_SizeBox*/))
00351                 cy += 15;
00352             mBrowserWindow->ResizeWindowTo(cx, cy);
00353         }
00354     }
00355     return rv;
00356 }
00357 
00358 NS_IMETHODIMP CWebBrowserChrome::GetDimensions(PRUint32 flags, PRInt32 *x, PRInt32 *y, PRInt32 *cx, PRInt32 *cy)
00359 {
00360     NS_ENSURE_STATE(mBrowserWindow);
00361 
00362     nsresult rv = NS_OK;
00363     CBrowserShell *browserShell;
00364     Rect bounds;
00365         
00366     if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION)    // getting position
00367     {
00368         if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER)
00369         {
00370             browserShell = mBrowserWindow->GetBrowserShell();
00371             NS_ENSURE_TRUE(browserShell, NS_ERROR_NULL_POINTER);
00372             SPoint32 curPos;
00373             browserShell->GetFrameLocation(curPos);
00374             if (x)
00375                 *x = curPos.h;
00376             if (y)
00377                 *y = curPos.v;
00378         }
00379         else // (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER)
00380         {
00381             mBrowserWindow->GetGlobalBounds(bounds);
00382             if (x)
00383                 *x = bounds.left;
00384             if (y)
00385                 *y = bounds.top;
00386         }
00387     }
00388     else                                                        // getting size
00389     {
00390         if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER)
00391         {
00392             browserShell = mBrowserWindow->GetBrowserShell();
00393             NS_ENSURE_TRUE(browserShell, NS_ERROR_NULL_POINTER);
00394             SDimension16 curSize;
00395             browserShell->GetFrameSize(curSize);
00396             if (cx)
00397                 *cx = curSize.width;
00398             if (cy)
00399                 *cy = curSize.height;
00400         }
00401         else // (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER)
00402         {
00403             mBrowserWindow->GetGlobalBounds(bounds);
00404             if (cx)
00405                 *cx = bounds.right - bounds.left;
00406             if (cy)
00407             {
00408                 *cy = bounds.bottom - bounds.top;
00409                 if (mBrowserWindow->HasAttribute(windAttr_Resizable /*windAttr_SizeBox*/))
00410                     *cy -= 15;
00411             }
00412         }
00413     }
00414     return rv;
00415 }
00416 
00417 NS_IMETHODIMP CWebBrowserChrome::SetFocus()
00418 {
00419     NS_ASSERTION(PR_FALSE, "Not Yet Implemented");
00420     return NS_ERROR_NOT_IMPLEMENTED;
00421 }
00422 
00423 NS_IMETHODIMP CWebBrowserChrome::GetVisibility(PRBool *aVisibility)
00424 {
00425     NS_ENSURE_STATE(mBrowserWindow);
00426     NS_ENSURE_ARG_POINTER(aVisibility);
00427     
00428     mBrowserWindow->GetVisibility(aVisibility);
00429     return NS_OK;
00430 }
00431 
00432 NS_IMETHODIMP CWebBrowserChrome::SetVisibility(PRBool aVisibility)
00433 {
00434     NS_ENSURE_STATE(mBrowserWindow);
00435     
00436     mBrowserWindow->SetVisibility(aVisibility);
00437     return NS_OK;
00438 }
00439 
00440 NS_IMETHODIMP CWebBrowserChrome::GetTitle(PRUnichar * *aTitle)
00441 {
00442    NS_ENSURE_STATE(mBrowserWindow);
00443    NS_ENSURE_ARG_POINTER(aTitle);
00444 
00445    Str255         pStr;
00446    nsAutoString   titleStr;
00447    
00448    mBrowserWindow->GetDescriptor(pStr);
00449    CPlatformUCSConversion::GetInstance()->PlatformToUCS(pStr, titleStr);
00450    *aTitle = ToNewUnicode(titleStr);
00451    
00452    return NS_OK;
00453 }
00454 
00455 NS_IMETHODIMP CWebBrowserChrome::SetTitle(const PRUnichar * aTitle)
00456 {
00457     NS_ENSURE_STATE(mBrowserWindow);
00458     NS_ENSURE_ARG(aTitle);
00459     
00460     Str255          pStr;
00461        
00462     CPlatformUCSConversion::GetInstance()->UCSToPlatform(nsDependentString(aTitle), pStr);
00463     mBrowserWindow->SetDescriptor(pStr);
00464     return NS_OK;
00465 }
00466 
00467 NS_IMETHODIMP CWebBrowserChrome::GetSiteWindow(void * *aSiteWindow)
00468 {
00469     NS_ENSURE_ARG(aSiteWindow);
00470     NS_ENSURE_STATE(mBrowserWindow);
00471 
00472     *aSiteWindow = mBrowserWindow->Compat_GetMacWindow();
00473     
00474     return NS_OK;
00475 }
00476 
00477 //*****************************************************************************
00478 // CWebBrowserChrome::nsIContextMenuListener
00479 //*****************************************************************************   
00480 
00481 NS_IMETHODIMP CWebBrowserChrome::OnShowContextMenu(PRUint32 aContextFlags, nsIDOMEvent *aEvent, nsIDOMNode *aNode)
00482 {
00483     nsresult rv;
00484        NS_ENSURE_TRUE(mBrowserWindow, NS_ERROR_NOT_INITIALIZED);
00485     
00486     try
00487     {
00488         rv = mBrowserWindow->OnShowContextMenu(aContextFlags, aEvent, aNode);
00489     }
00490     catch (...)
00491     {
00492         rv = NS_ERROR_FAILURE;
00493     }
00494     return rv;
00495 }
00496 
00497 
00498 //*****************************************************************************
00499 // CWebBrowserChrome: Helpers
00500 //*****************************************************************************   
00501 
00502 //*****************************************************************************
00503 // CWebBrowserChrome: Accessors
00504 //*****************************************************************************   
00505 
00506 CBrowserWindow*& CWebBrowserChrome::BrowserWindow()
00507 {
00508    return mBrowserWindow;
00509 }
00510 
00511 CBrowserShell*& CWebBrowserChrome::BrowserShell()
00512 {
00513    return mBrowserShell;
00514 }
00515 
00516 NS_IMETHODIMP
00517 CWebBrowserChrome::OnShowTooltip(PRInt32 aXCoords, PRInt32 aYCoords, const PRUnichar *aTipText)
00518 {
00519   nsCAutoString printable;
00520   CPlatformUCSConversion::GetInstance()->UCSToPlatform(nsDependentString(aTipText), printable);
00521 
00522 #ifdef DEBUG
00523   printf("--------- SHOW TOOLTIP AT %ld %ld, |%s|\n", aXCoords, aYCoords, printable.get() );
00524 #endif
00525 
00526 #if USE_BALLOONS_FOR_TOOL_TIPS  
00527   Point where;
00528   ::GetMouse ( &where );
00529   ::LocalToGlobal ( &where );
00530   
00531   HMMessageRecord helpRec;
00532   helpRec.hmmHelpType = khmmString;
00533   helpRec.u.hmmString[0] = strlen(printable);
00534   memcpy ( &helpRec.u.hmmString[1], printable, strlen(printable) );
00535   
00536   mPreviousBalloonState = ::HMGetBalloons();
00537   ::HMSetBalloons ( true );
00538   OSErr err = ::HMShowBalloon ( &helpRec, where, NULL, NULL, 0, 0, 0 );
00539 #endif
00540     
00541   return NS_OK;
00542 }
00543 
00544 NS_IMETHODIMP
00545 CWebBrowserChrome::OnHideTooltip()
00546 {
00547 #ifdef DEBUG
00548   printf("--------- HIDE TOOLTIP\n");
00549 #endif
00550 
00551 #if USE_BALLOONS_FOR_TOOL_TIPS
00552   ::HMRemoveBalloon();
00553   ::HMSetBalloons ( mPreviousBalloonState );
00554 #endif
00555 
00556   return NS_OK;
00557 }