Back to index

lightning-sunbird  0.9+nobinonly
nsBrowserWindow.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 Communicator client 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  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or 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  * This Original Code has been modified by IBM Corporation. Modifications made by IBM 
00039  * described herein are Copyright (c) International Business Machines Corporation, 2000.
00040  * Modifications to Mozilla code or documentation identified per MPL Section 3.3
00041  *
00042  * Date             Modified by     Description of modification
00043  * 04/20/2000       IBM Corp.      OS/2 VisualAge build.
00044  */
00045 
00046 #include "nsIPref.h" 
00047 #include "prmem.h"
00048 
00049 #include "nsBrowserWindow.h"
00050 #include "nsIAppShell.h"
00051 #include "nsIWidget.h"
00052 #include "nsITextWidget.h"
00053 #include "nsIButton.h"
00054 #include "nsITimer.h"
00055 #include "nsIDOMDocument.h"
00056 #include "nsIURL.h"
00057 #include "nsIChannel.h"
00058 #include "nsNetUtil.h"
00059 #include "nsIDOMWindowInternal.h"
00060 #include "nsIFilePicker.h"
00061 #include "nsILookAndFeel.h"
00062 #include "nsIComponentManager.h"
00063 #include "nsIFactory.h"
00064 #include "nsCRT.h"
00065 #include "nsWidgetsCID.h"
00066 #include "nsViewerApp.h"
00067 #include "prprf.h"
00068 #include "nsIComponentManager.h"
00069 #include "nsParserCIID.h"
00070 #include "nsCOMPtr.h"
00071 #include "nsIServiceManager.h"
00072 #include "nsIStringBundle.h"
00073 #include "nsLayoutCID.h"
00074 #include "nsIDocumentViewer.h"
00075 #include "nsIContentViewer.h"
00076 #include "nsIContentViewerFile.h"
00077 #include "nsIContentViewerContainer.h"
00078 #include "nsIPresShell.h"
00079 #include "nsPresContext.h"
00080 #include "nsIDocument.h"
00081 #include "nsILayoutDebugger.h"
00082 #include "nsThrobber.h"
00083 #include "nsIDocShellTreeItem.h"
00084 #include "nsIDocShellTreeNode.h"
00085 #include "nsIWebNavigation.h"
00086 #include "nsIWebBrowserFocus.h"
00087 #include "nsIBaseWindow.h"
00088 #include "nsXPIDLString.h"
00089 #include "nsReadableUtils.h"
00090 #include "nsIViewManager.h"
00091 #include "nsGUIEvent.h"
00092 #include "nsIWebProgress.h"
00093 #include "nsIWebBrowserSetup.h"
00094 #include "nsIWebBrowserPrint.h"
00095 
00096 #include "nsCWebBrowser.h"
00097 #include "nsUnicharUtils.h"
00098 #include "nsInt64.h"
00099 
00100 #include "nsIParser.h"
00101 #include "nsEditorMode.h"
00102 
00103 // Needed for "Find" GUI
00104 #include "nsICheckButton.h"
00105 #include "nsILabel.h"
00106 #include "nsWidgetSupport.h"
00107 
00108 #include "nsXPBaseWindow.h"
00109 
00110 #include "resources.h"
00111 
00112 #if defined(WIN32)
00113 #include <windows.h>
00114 #endif
00115 
00116 #include <ctype.h> // tolower
00117 
00118 // For Copy
00119 #include "nsISelection.h"
00120 #include "nsISelectionController.h"
00121 
00122 // XXX For font setting below
00123 #include "nsFont.h"
00124 #include "nsUnitConversion.h"
00125 #include "nsIDeviceContext.h"
00126 
00127 #if defined(CookieManagement) || defined(SingleSignon) || defined(ClientWallet)
00128 #include "nsIServiceManager.h"
00129 #endif
00130 
00131 #ifdef CookieManagement
00132 
00133 #include "nsIURL.h"
00134 
00135 #endif
00136 #include "nsIIOService.h"
00137 #include "nsNetCID.h"
00138 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
00139 
00140 #if defined(ClientWallet) || defined(SingleSignon)
00141 #include "nsIWalletService.h"
00142 static NS_DEFINE_IID(kIWalletServiceIID, NS_IWALLETSERVICE_IID);
00143 static NS_DEFINE_CID(kWalletServiceCID, NS_WALLETSERVICE_CID);
00144 #endif
00145 
00146 #ifdef PURIFY
00147 #include "pure.h"
00148 #endif
00149 
00150 #define THROBBING_N
00151 
00152 #define CLEANUP_WIDGET(_widget, _txt) \
00153   if(_widget) {            \
00154   DestroyWidget((_widget)); \
00155   refCnt = (_widget)->Release(); \
00156   (_widget) = nsnull; \
00157   NS_ASSERTION(refCnt == 0, (_txt)); \
00158   }
00159 
00160 
00161 // XXX greasy constants
00162 #ifdef THROBBING_N
00163 #define THROBBER_WIDTH 32
00164 #define THROBBER_HEIGHT 32
00165 #define THROBBER_AT "resource:/res/throbber/anims%02d.gif"
00166 #define THROB_NUM 29
00167 #else
00168 #define THROBBER_WIDTH 42
00169 #define THROBBER_HEIGHT 42
00170 #define THROBBER_AT "resource:/res/throbber/LargeAnimation%02d.gif"
00171 #define THROB_NUM 38
00172 #endif
00173 #define BUTTON_WIDTH 90
00174 #define BUTTON_HEIGHT THROBBER_HEIGHT
00175 #define MAX_TEXT_LENGTH 30000
00176 
00177 #ifdef INSET_WEBSHELL
00178 #define WEBSHELL_LEFT_INSET 5
00179 #define WEBSHELL_RIGHT_INSET 5
00180 #define WEBSHELL_TOP_INSET 5
00181 #define WEBSHELL_BOTTOM_INSET 5
00182 #else
00183 #define WEBSHELL_LEFT_INSET 0
00184 #define WEBSHELL_RIGHT_INSET 0
00185 #define WEBSHELL_TOP_INSET 0
00186 #define WEBSHELL_BOTTOM_INSET 0
00187 #endif
00188 
00189 static NS_DEFINE_CID(kLookAndFeelCID, NS_LOOKANDFEEL_CID);
00190 static NS_DEFINE_CID(kButtonCID, NS_BUTTON_CID);
00191 static NS_DEFINE_CID(kTextFieldCID, NS_TEXTFIELD_CID);
00192 static NS_DEFINE_CID(kWindowCID, NS_WINDOW_CID);
00193 
00194 static NS_DEFINE_IID(kIXPBaseWindowIID, NS_IXPBASE_WINDOW_IID);
00195 static NS_DEFINE_IID(kIButtonIID, NS_IBUTTON_IID);
00196 static NS_DEFINE_IID(kIDOMDocumentIID, NS_IDOMDOCUMENT_IID);
00197 static NS_DEFINE_IID(kITextWidgetIID, NS_ITEXTWIDGET_IID);
00198 static NS_DEFINE_IID(kIWidgetIID, NS_IWIDGET_IID);
00199 static NS_DEFINE_IID(kIDocumentViewerIID, NS_IDOCUMENT_VIEWER_IID);
00200 static NS_DEFINE_CID(kXPBaseWindowCID, NS_XPBASE_WINDOW_CID);
00201 static NS_DEFINE_IID(kIStringBundleServiceIID, NS_ISTRINGBUNDLESERVICE_IID);
00202 
00203 static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
00204 
00205 static NS_DEFINE_IID(kILayoutDebuggerIID, NS_ILAYOUT_DEBUGGER_IID);
00206 static NS_DEFINE_CID(kLayoutDebuggerCID, NS_LAYOUT_DEBUGGER_CID);
00207 
00208 #define FILE_PROTOCOL "file://"
00209 
00210 #ifdef USE_LOCAL_WIDGETS
00211   extern nsresult NS_NewButton(nsIButton** aControl);
00212   extern nsresult NS_NewLabel(nsILabel** aControl);
00213   extern nsresult NS_NewTextWidget(nsITextWidget** aControl);
00214   extern nsresult NS_NewCheckButton(nsICheckButton** aControl);
00215 #endif
00216 
00217 //******* Cleanup Above here***********/
00218 
00219 //*****************************************************************************
00220 // nsBrowserWindow::nsIBaseWindow
00221 //*****************************************************************************   
00222 
00223 NS_IMETHODIMP nsBrowserWindow::InitWindow(nativeWindow aParentNativeWindow,
00224    nsIWidget* parentWidget, PRInt32 x, PRInt32 y, PRInt32 cx, PRInt32 cy)   
00225 {
00226    // Ignore wigdet parents for now.  Don't think those are a vaild thing to call.
00227    NS_ENSURE_SUCCESS(SetPositionAndSize(x, y, cx, cy, PR_FALSE), NS_ERROR_FAILURE);
00228 
00229    return NS_OK;
00230 }
00231 
00232 NS_IMETHODIMP nsBrowserWindow::Create()
00233 {
00234    NS_ASSERTION(PR_FALSE, "You can't call this");
00235    return NS_ERROR_UNEXPECTED;
00236 }
00237 
00238 NS_IMETHODIMP nsBrowserWindow::Destroy()
00239 {
00240   RemoveBrowser(this);
00241 
00242   nsCOMPtr<nsIBaseWindow> docShellWin(do_QueryInterface(mDocShell));
00243   docShellWin->Destroy();
00244   mDocShell = nsnull;
00245 
00246   nsrefcnt refCnt;
00247 
00248   CLEANUP_WIDGET(mBack,     "nsBrowserWindow::Destroy - mBack is being leaked.");
00249   CLEANUP_WIDGET(mForward,  "nsBrowserWindow::Destroy - mForward is being leaked.");
00250   CLEANUP_WIDGET(mLocation, "nsBrowserWindow::Destroy - mLocation is being leaked.");
00251   CLEANUP_WIDGET(mStatus,   "nsBrowserWindow::Destroy - mStatus is being leaked.");
00252 
00253   if (mThrobber) {
00254     mThrobber->Destroy();
00255     refCnt = mThrobber->Release();
00256     mThrobber = nsnull;
00257     NS_ASSERTION(refCnt == 0, "nsBrowserWindow::Destroy - mThrobber is being leaked.");
00258     mThrobber = nsnull;
00259   }
00260 
00261   // Others are holding refs to this, 
00262   // but it gets released OK.
00263   DestroyWidget(mWindow);
00264   mWindow = nsnull;
00265   // NS_RELEASE(mWindow);
00266 
00267   return NS_OK;
00268 }
00269 
00270 NS_IMETHODIMP nsBrowserWindow::SetPosition(PRInt32 aX, PRInt32 aY)
00271 {
00272    PRInt32 cx=0;
00273    PRInt32 cy=0;
00274 
00275    NS_ENSURE_SUCCESS(GetSize(&cx, &cy), NS_ERROR_FAILURE);
00276    NS_ENSURE_SUCCESS(SetPositionAndSize(aX, aY, cx, cy, PR_FALSE), 
00277       NS_ERROR_FAILURE);
00278    return NS_OK;
00279 }
00280 
00281 NS_IMETHODIMP nsBrowserWindow::GetPosition(PRInt32* aX, PRInt32* aY)
00282 {
00283    return GetPositionAndSize(aX, aY, nsnull, nsnull);
00284 }
00285 
00286 NS_IMETHODIMP nsBrowserWindow::SetSize(PRInt32 aCX, PRInt32 aCY, PRBool aRepaint)
00287 {
00288    PRInt32 x=0;
00289    PRInt32 y=0;
00290 
00291    NS_ENSURE_SUCCESS(GetPosition(&x, &y), NS_ERROR_FAILURE);
00292    NS_ENSURE_SUCCESS(SetPositionAndSize(x, y, aCX, aCY, aRepaint), 
00293       NS_ERROR_FAILURE);
00294 
00295    return NS_OK;
00296 }
00297 
00298 NS_IMETHODIMP nsBrowserWindow::GetSize(PRInt32* aCX, PRInt32* aCY)
00299 {
00300    return GetPositionAndSize(nsnull, nsnull, aCX, aCY);
00301 }
00302 
00303 NS_IMETHODIMP nsBrowserWindow::SetPositionAndSize(PRInt32 aX, PRInt32 aY, 
00304    PRInt32 aCX, PRInt32 aCY, PRBool aRepaint)
00305 {
00306    NS_ENSURE_SUCCESS(mWindow->Resize(aX, aY, aCX, aCY, aRepaint), 
00307       NS_ERROR_FAILURE);
00308 
00309    return NS_OK;
00310 }
00311 
00312 NS_IMETHODIMP nsBrowserWindow::GetPositionAndSize(PRInt32* aX, PRInt32* aY, 
00313    PRInt32* aCX, PRInt32* aCY)
00314 {
00315    nsRect bounds;
00316 
00317    NS_ENSURE_SUCCESS(mWindow->GetBounds(bounds), NS_ERROR_FAILURE);
00318 
00319    if(aX)
00320       *aX = bounds.x;
00321    if(aY)
00322       *aY = bounds.y;
00323    if(aCX)
00324       *aCX = bounds.width;
00325    if(aCY)
00326       *aCY = bounds.height;
00327 
00328    return NS_OK;
00329 }
00330 
00331 NS_IMETHODIMP nsBrowserWindow::Repaint(PRBool aForce)
00332 {
00333    //XXX First Check In
00334    NS_ASSERTION(PR_FALSE, "Not Yet Implemented");
00335    return NS_OK;
00336 }
00337 
00338 NS_IMETHODIMP nsBrowserWindow::GetParentWidget(nsIWidget** aParentWidget)
00339 {
00340    NS_ENSURE_ARG_POINTER(aParentWidget);
00341    //XXX First Check In
00342    NS_ASSERTION(PR_FALSE, "Not Yet Implemented");
00343    return NS_OK;
00344 }
00345 
00346 NS_IMETHODIMP nsBrowserWindow::SetParentWidget(nsIWidget* aParentWidget)
00347 {
00348    NS_ASSERTION(PR_FALSE, "You can't call this");
00349    return NS_ERROR_NOT_IMPLEMENTED;
00350 }
00351 
00352 NS_IMETHODIMP nsBrowserWindow::GetParentNativeWindow(nativeWindow* aParentNativeWindow)
00353 {
00354    NS_ENSURE_ARG_POINTER(aParentNativeWindow);
00355 
00356    //XXX First Check In
00357    NS_ASSERTION(PR_FALSE, "Not Yet Implemented");
00358    return NS_OK;
00359 }
00360 
00361 NS_IMETHODIMP nsBrowserWindow::SetParentNativeWindow(nativeWindow aParentNativeWindow)
00362 {
00363    NS_ASSERTION(PR_FALSE, "You can't call this");
00364    return NS_ERROR_NOT_IMPLEMENTED;
00365 }
00366 
00367 NS_IMETHODIMP nsBrowserWindow::GetVisibility(PRBool* aVisibility)
00368 {
00369    NS_ENSURE_ARG_POINTER(aVisibility);
00370 
00371    //XXX First Check In
00372    NS_ASSERTION(PR_FALSE, "Not Yet Implemented");
00373    return NS_OK;
00374 }
00375 
00376 NS_IMETHODIMP nsBrowserWindow::SetVisibility(PRBool aVisibility)
00377 {
00378    NS_ENSURE_STATE(mWindow);
00379 
00380    NS_ENSURE_SUCCESS(mWindow->Show(aVisibility), NS_ERROR_FAILURE);
00381 
00382    return NS_OK;
00383 }
00384 
00385 NS_IMETHODIMP nsBrowserWindow::GetEnabled(PRBool *aEnabled)
00386 {
00387   NS_ENSURE_ARG_POINTER(aEnabled);
00388   *aEnabled = PR_TRUE;
00389   if (mWindow)
00390     mWindow->IsEnabled(aEnabled);
00391   return NS_OK;
00392 }
00393 
00394 NS_IMETHODIMP nsBrowserWindow::SetEnabled(PRBool aEnabled)
00395 {
00396   if (mWindow)
00397     mWindow->Enable(aEnabled);
00398   return NS_OK;
00399 }
00400 
00401 NS_IMETHODIMP nsBrowserWindow::GetBlurSuppression(PRBool *aBlurSuppression)
00402 {
00403   NS_ENSURE_ARG_POINTER(aBlurSuppression);
00404   *aBlurSuppression = PR_FALSE;
00405   return NS_ERROR_NOT_IMPLEMENTED;
00406 }
00407 
00408 NS_IMETHODIMP nsBrowserWindow::SetBlurSuppression(PRBool aBlurSuppression)
00409 {
00410   return NS_ERROR_NOT_IMPLEMENTED;
00411 }
00412 
00413 NS_IMETHODIMP nsBrowserWindow::GetMainWidget(nsIWidget** aMainWidget)
00414 {
00415    NS_ENSURE_ARG_POINTER(aMainWidget);
00416 
00417    //XXX First Check In
00418    NS_ASSERTION(PR_FALSE, "Not Yet Implemented");
00419    return NS_OK;
00420 }
00421 
00422 NS_IMETHODIMP nsBrowserWindow::SetFocus()
00423 {
00424    //XXX First Check In
00425    //NS_WARNING("Not Yet Implemented");
00426    return NS_OK;
00427 }
00428 
00429 NS_IMETHODIMP nsBrowserWindow::GetTitle(PRUnichar** aTitle)
00430 {
00431    NS_ENSURE_ARG_POINTER(aTitle);
00432 
00433    *aTitle = ToNewUnicode(mTitle);
00434 
00435    return NS_OK;
00436 }
00437 
00438 NS_IMETHODIMP nsBrowserWindow::SetTitle(const PRUnichar* aTitle)
00439 {
00440    NS_ENSURE_STATE(mWindow);
00441 
00442    mTitle = aTitle;
00443 
00444    NS_ENSURE_SUCCESS(mWindow->SetTitle(nsAutoString(aTitle)), NS_ERROR_FAILURE);
00445 
00446    return NS_OK;
00447 }
00448 
00449 //*****************************************************************************
00450 // nsBrowserWindow: Helper Function
00451 //*****************************************************************************   
00452 
00453 void nsBrowserWindow::DestroyWidget(nsISupports* aWidget)
00454 {
00455   if(aWidget) {
00456     nsCOMPtr<nsIWidget> w(do_QueryInterface(aWidget));
00457     if (w) {
00458       w->Destroy();
00459     }
00460   }
00461 }
00462 
00463 //******* Cleanup below here *************/
00464 
00465 
00466 
00467 
00468 //----------------------------------------------------------------------
00469 
00470 static
00471 nsIPresShell*
00472 GetPresShellFor(nsIDocShell* aDocShell)
00473 {
00474   nsIPresShell* shell = nsnull;
00475   if (nsnull != aDocShell) {
00476     nsIContentViewer* cv = nsnull;
00477     aDocShell->GetContentViewer(&cv);
00478     if (nsnull != cv) {
00479       nsIDocumentViewer* docv = nsnull;
00480       cv->QueryInterface(kIDocumentViewerIID, (void**) &docv);
00481       if (nsnull != docv) {
00482         nsCOMPtr<nsPresContext> cx;
00483         docv->GetPresContext(getter_AddRefs(cx));
00484         if (nsnull != cx) {
00485           NS_IF_ADDREF(shell = cx->GetPresShell());
00486         }
00487         NS_RELEASE(docv);
00488       }
00489       NS_RELEASE(cv);
00490     }
00491   }
00492   return shell;
00493 }
00494 
00495 nsVoidArray* nsBrowserWindow::gBrowsers = nsnull;
00496 
00497 nsBrowserWindow*
00498 nsBrowserWindow::FindBrowserFor(nsIWidget* aWidget, PRIntn aWhich)
00499 {
00500   nsIWidget*        widget;
00501   nsBrowserWindow*  result = nsnull;
00502 
00503   if (!gBrowsers)
00504     return nsnull;
00505 
00506   PRInt32 i, n = gBrowsers->Count();
00507   for (i = 0; i < n; i++) {
00508     nsBrowserWindow* bw = (nsBrowserWindow*) gBrowsers->ElementAt(i);
00509     if (nsnull != bw) {
00510       switch (aWhich) {
00511       case FIND_WINDOW:
00512         if (bw->mWindow) {
00513           bw->mWindow->QueryInterface(kIWidgetIID, (void**) &widget);
00514           if (widget == aWidget) {
00515             result = bw;
00516           }
00517           NS_IF_RELEASE(widget);
00518         }
00519         break;
00520       case FIND_BACK:
00521         if (bw->mBack) {
00522           bw->mBack->QueryInterface(kIWidgetIID, (void**) &widget);
00523           if (widget == aWidget) {
00524             result = bw;
00525           }
00526           NS_IF_RELEASE(widget);
00527         }
00528         break;
00529       case FIND_FORWARD:
00530         if (bw->mForward) {
00531           bw->mForward->QueryInterface(kIWidgetIID, (void**) &widget);
00532           if (widget == aWidget) {
00533             result = bw;
00534           }
00535           NS_IF_RELEASE(widget);
00536         }
00537         break;
00538       case FIND_LOCATION:
00539         if (bw->mLocation) {
00540           bw->mLocation->QueryInterface(kIWidgetIID, (void**) &widget);
00541           if (widget == aWidget) {
00542             result = bw;
00543           }
00544           NS_IF_RELEASE(widget);
00545         }
00546         break;
00547       }
00548     }
00549   }
00550   if (nsnull != result) {
00551     NS_ADDREF(result);
00552   }
00553   return result;
00554 }
00555 
00556 void
00557 nsBrowserWindow::AddBrowser(nsBrowserWindow* aBrowser)
00558 {
00559   if (!gBrowsers) {
00560     gBrowsers = new nsVoidArray();
00561     if (!gBrowsers)
00562       return;
00563   }
00564   gBrowsers->AppendElement(aBrowser);
00565   NS_ADDREF(aBrowser);
00566 }
00567 
00568 void
00569 nsBrowserWindow::RemoveBrowser(nsBrowserWindow* aBrowser)
00570 {
00571   //nsViewerApp* app = aBrowser->mApp;
00572   if (!gBrowsers)
00573     return;
00574   gBrowsers->RemoveElement(aBrowser);
00575   if (!gBrowsers->Count()) {
00576     delete gBrowsers;
00577     gBrowsers = nsnull;
00578   }
00579   NS_RELEASE(aBrowser);
00580 }
00581 
00582 void
00583 nsBrowserWindow::CloseAllWindows()
00584 {
00585   while (gBrowsers && gBrowsers->Count()) {
00586     nsBrowserWindow* bw = (nsBrowserWindow*) gBrowsers->ElementAt(0);
00587     NS_ADDREF(bw);
00588     bw->Destroy();
00589     NS_RELEASE(bw);
00590   }
00591   if (gBrowsers) {
00592     delete gBrowsers;
00593     gBrowsers = nsnull;
00594   }
00595 }
00596 
00597 static nsEventStatus PR_CALLBACK
00598 HandleBrowserEvent(nsGUIEvent *aEvent)
00599 { 
00600   nsEventStatus result = nsEventStatus_eIgnore;
00601   nsBrowserWindow* bw =
00602     nsBrowserWindow::FindBrowserFor(aEvent->widget, FIND_WINDOW);
00603   if (nsnull != bw) {
00604     nsSizeEvent* sizeEvent;
00605     switch(aEvent->message) {
00606     case NS_SIZE:
00607       sizeEvent = (nsSizeEvent*)aEvent;  
00608       bw->Layout(sizeEvent->windowSize->width,
00609                  sizeEvent->windowSize->height);
00610       result = nsEventStatus_eConsumeNoDefault;
00611       break;
00612 
00613     case NS_XUL_CLOSE:
00614     case NS_DESTROY:
00615     {
00616       nsViewerApp* app = bw->mApp;
00617       app->CloseWindow(bw);
00618       result = nsEventStatus_eConsumeDoDefault;
00619 
00620 #ifndef XP_MAC
00621       // XXX Really shouldn't just exit, we should just notify somebody...
00622       if (!nsBrowserWindow::gBrowsers || !nsBrowserWindow::gBrowsers->Count()) {
00623         app->Exit();
00624       }
00625 #endif
00626     }
00627     return result;
00628 
00629     case NS_MENU_SELECTED:
00630       result = bw->DispatchMenuItem(((nsMenuEvent*)aEvent)->mCommand);
00631       break;
00632 
00633     case NS_ACTIVATE:
00634       {
00635         nsCOMPtr<nsIWebBrowserFocus> focus = do_GetInterface(bw->mWebBrowser);
00636         if (focus)
00637           focus->Activate();
00638       }
00639       break;
00640 
00641     case NS_DEACTIVATE:
00642       {
00643         nsCOMPtr<nsIWebBrowserFocus> focus = do_GetInterface(bw->mWebBrowser);
00644         if (focus)
00645           focus->Deactivate();
00646       }
00647       break;
00648 
00649     default:
00650       break;
00651     }
00652     NS_RELEASE(bw);
00653   }
00654   return result;
00655 }
00656 
00657 static nsEventStatus PR_CALLBACK
00658 HandleBackEvent(nsGUIEvent *aEvent)
00659 {
00660   nsEventStatus result = nsEventStatus_eIgnore;
00661   nsBrowserWindow* bw =
00662     nsBrowserWindow::FindBrowserFor(aEvent->widget, FIND_BACK);
00663   if (nsnull != bw) {
00664     switch(aEvent->message) {
00665     case NS_MOUSE_LEFT_BUTTON_UP:
00666       bw->Back();
00667       break;
00668     }
00669     NS_RELEASE(bw);
00670   }
00671   return result;
00672 }
00673 
00674 static nsEventStatus PR_CALLBACK
00675 HandleForwardEvent(nsGUIEvent *aEvent)
00676 {
00677   nsEventStatus result = nsEventStatus_eIgnore;
00678   nsBrowserWindow* bw =
00679     nsBrowserWindow::FindBrowserFor(aEvent->widget, FIND_FORWARD);
00680   if (nsnull != bw) {
00681     switch(aEvent->message) {
00682     case NS_MOUSE_LEFT_BUTTON_UP:
00683       bw->Forward();
00684       break;
00685     }
00686     NS_RELEASE(bw);
00687   }
00688   return result;
00689 }
00690 
00691 static nsEventStatus PR_CALLBACK
00692 HandleLocationEvent(nsGUIEvent *aEvent)
00693 {
00694   nsEventStatus result = nsEventStatus_eIgnore;
00695   nsBrowserWindow* bw =
00696     nsBrowserWindow::FindBrowserFor(aEvent->widget, FIND_LOCATION);
00697   if (nsnull != bw) {
00698     switch (aEvent->message) {
00699     case NS_KEY_UP:
00700       if (NS_VK_RETURN == ((nsKeyEvent*)aEvent)->keyCode) {
00701         nsAutoString text;
00702         PRUint32 size;
00703         bw->mLocation->GetText(text, 1000, size);
00704         bw->GoTo(text.get());
00705       }
00706       break;
00707 
00708     default:
00709       break;
00710     }
00711     NS_RELEASE(bw);
00712   }
00713   return result;
00714 }
00715 
00716 #ifdef PURIFY
00717 static void
00718 DispatchPurifyEvent(PRInt32 aID)
00719 {
00720   if (!PurifyIsRunning()) {
00721       printf("!!! Re-run viewer under Purify to use this menu item.\n");
00722       fflush(stdout);
00723       return;
00724   }
00725 
00726   switch (aID) {
00727   case VIEWER_PURIFY_SHOW_NEW_LEAKS:
00728     PurifyPrintf("viewer: new leaks");
00729     PurifyNewLeaks();
00730     break;
00731   case VIEWER_PURIFY_SHOW_ALL_LEAKS:
00732     PurifyPrintf("viewer: all leaks");
00733     PurifyAllLeaks();
00734     break;
00735   case VIEWER_PURIFY_CLEAR_ALL_LEAKS:
00736     PurifyPrintf("viewer: clear leaks");
00737     PurifyClearLeaks();
00738     break;
00739   case VIEWER_PURIFY_SHOW_ALL_HANDLES_IN_USE:
00740     PurifyPrintf("viewer: all handles");
00741     PurifyAllHandlesInuse();
00742     break;
00743   case VIEWER_PURIFY_SHOW_NEW_IN_USE:
00744     PurifyPrintf("viewer: new in-use");
00745     PurifyNewInuse();
00746     break;
00747   case VIEWER_PURIFY_SHOW_ALL_IN_USE:
00748     PurifyPrintf("viewer: all in-use");
00749     PurifyAllInuse();
00750     break;
00751   case VIEWER_PURIFY_CLEAR_ALL_IN_USE:
00752     PurifyPrintf("viewer: clear in-use");
00753     PurifyClearInuse();
00754     break;
00755   case VIEWER_PURIFY_HEAP_VALIDATE:
00756     PurifyPrintf("viewer: heap validate");
00757     PurifyHeapValidate(PURIFY_HEAP_ALL, PURIFY_HEAP_BLOCKS_ALL, NULL);
00758     break;
00759   }
00760 }
00761 #endif
00762 
00763 nsEventStatus
00764 nsBrowserWindow::DispatchMenuItem(PRInt32 aID)
00765 {
00766 #if defined(CookieManagement) || defined(SingleSignon) || defined(ClientWallet)
00767   nsresult res;
00768 #if defined(ClientWallet) || defined(SingleSignon)
00769   nsIWalletService *walletservice;
00770 #endif
00771 #ifdef ClientWallet
00772 #define WALLET_EDITOR_URL "file:///y|/walleted.html"
00773   nsAutoString urlString; urlString.AssignWithConversion(WALLET_EDITOR_URL);
00774 #endif
00775 #endif
00776 
00777   nsEventStatus result;
00778 #ifdef NS_DEBUG
00779   result = DispatchDebugMenu(aID);
00780   if (nsEventStatus_eIgnore != result) {
00781     return result;
00782   }
00783 #endif
00784   result = DispatchStyleMenu(aID);
00785   if (nsEventStatus_eIgnore != result) {
00786     return result;
00787   }
00788 
00789   switch (aID) {
00790   case VIEWER_EXIT:
00791     mApp->Exit();
00792     return nsEventStatus_eConsumeNoDefault;
00793 
00794   case VIEWER_WINDOW_OPEN:
00795     mApp->OpenWindow();
00796     break;
00797   
00798   case VIEWER_FILE_OPEN:
00799     DoFileOpen();
00800     break;
00801 
00802   case VIEW_SOURCE:
00803     {
00804       //XXX Find out how the string is allocated, and perhaps delete it...
00805     }
00806     break;
00807   
00808   case VIEWER_EDIT_COPY:
00809     DoCopy();
00810     break;
00811 
00812   case VIEWER_EDIT_PASTE:
00813     DoPaste();
00814     break;
00815 
00816   case VIEWER_EDIT_FINDINPAGE:
00817     DoFind();
00818     break;
00819 
00820   case VIEWER_DEMO0:
00821   case VIEWER_DEMO1:
00822   case VIEWER_DEMO2:
00823   case VIEWER_DEMO3:
00824   case VIEWER_DEMO4:
00825   case VIEWER_DEMO5:
00826   case VIEWER_DEMO6:
00827   case VIEWER_DEMO7:
00828   case VIEWER_DEMO8: 
00829   case VIEWER_DEMO9: 
00830   case VIEWER_DEMO10: 
00831   case VIEWER_DEMO11: 
00832   case VIEWER_DEMO12: 
00833   case VIEWER_DEMO13:
00834   case VIEWER_DEMO14:
00835   case VIEWER_DEMO15:
00836   case VIEWER_DEMO16:
00837   case VIEWER_DEMO17:
00838     {
00839       PRIntn ix = aID - VIEWER_DEMO0;
00840       nsAutoString url; url.AssignWithConversion(SAMPLES_BASE_URL);
00841       url.AppendLiteral("/test");
00842       url.AppendInt(ix, 10);
00843       url.AppendLiteral(".html");
00844       nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
00845       webNav->LoadURI(url.get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull);
00846     }
00847     break;
00848 
00849   case VIEWER_XPTOOLKITTOOLBAR1:
00850     {
00851       nsAutoString url; url.AssignWithConversion(SAMPLES_BASE_URL);
00852       url.AppendLiteral("/toolbarTest1.xul");
00853       nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
00854       webNav->LoadURI(url.get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull);
00855       break;
00856     }
00857   case VIEWER_XPTOOLKITTREE1:
00858     {
00859       nsAutoString url; url.AssignWithConversion(SAMPLES_BASE_URL);
00860       url.AppendLiteral("/treeTest1.xul");
00861       nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
00862       webNav->LoadURI(url.get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull);
00863       break;
00864     }
00865   
00866   case JS_CONSOLE:
00867     DoJSConsole();
00868     break;
00869 
00870   case VIEWER_PREFS:
00871     DoPrefs();
00872     break;
00873 
00874   case EDITOR_MODE:
00875     DoEditorMode(mDocShell);
00876     break;
00877 
00878   case VIEWER_ONE_COLUMN:
00879   case VIEWER_TWO_COLUMN:
00880   case VIEWER_THREE_COLUMN:
00881     ShowPrintPreview(aID);
00882     break;
00883 
00884   case VIEWER_PRINT:
00885     DoPrint();
00886     break;
00887 
00888   case VIEWER_TABLE_INSPECTOR:
00889     DoTableInspector();
00890     break;
00891 
00892   case VIEWER_IMAGE_INSPECTOR:
00893     DoImageInspector();
00894     break;
00895 
00896   case VIEWER_GFX_SCROLLBARS_ON: {
00897     SetBoolPref("nglayout.widget.gfxscrollbars", PR_TRUE);
00898     nsAutoString text;
00899     PRUint32 size;
00900     mLocation->GetText(text, 1000, size);
00901     GoTo(text.get());
00902     }
00903     break;
00904 
00905   case VIEWER_GFX_SCROLLBARS_OFF: {
00906     SetBoolPref("nglayout.widget.gfxscrollbars", PR_FALSE);
00907     nsAutoString text;
00908     PRUint32 size;
00909     mLocation->GetText(text, 1000, size);
00910     GoTo(text.get());
00911     }
00912     break;
00913 
00914   case VIEWER_GOTO_TEST_URL1: 
00915   case VIEWER_GOTO_TEST_URL2: {
00916     nsAutoString urlStr;
00917     const char * pref = (aID == VIEWER_GOTO_TEST_URL1) ? "nglayout.widget.testurl1" : "nglayout.widget.testurl2";
00918     GetStringPref(pref, urlStr);
00919     PRUint32 size;
00920     mLocation->SetText(urlStr, size);
00921     GoTo(urlStr.get());
00922 
00923     }
00924     break;
00925 
00926   case VIEWER_SAVE_TEST_URL1: 
00927   case VIEWER_SAVE_TEST_URL2: {
00928     nsAutoString text;
00929     PRUint32 size;
00930     mLocation->GetText(text, 1000, size);
00931     const char * pref = (aID == VIEWER_SAVE_TEST_URL1) ? "nglayout.widget.testurl1" : "nglayout.widget.testurl2";
00932     SetStringPref(pref, text);
00933     }
00934     break;
00935 
00936 #ifdef PURIFY
00937   case VIEWER_PURIFY_SHOW_NEW_LEAKS:
00938   case VIEWER_PURIFY_SHOW_ALL_LEAKS:
00939   case VIEWER_PURIFY_CLEAR_ALL_LEAKS:
00940   case VIEWER_PURIFY_SHOW_ALL_HANDLES_IN_USE:
00941   case VIEWER_PURIFY_SHOW_NEW_IN_USE:
00942   case VIEWER_PURIFY_SHOW_ALL_IN_USE:
00943   case VIEWER_PURIFY_CLEAR_ALL_IN_USE:
00944   case VIEWER_PURIFY_HEAP_VALIDATE:
00945     DispatchPurifyEvent(aID);
00946     break;
00947 #endif
00948 
00949   case VIEWER_ZOOM_500:
00950   case VIEWER_ZOOM_300:
00951   case VIEWER_ZOOM_200:
00952   case VIEWER_ZOOM_100:
00953   case VIEWER_ZOOM_070:
00954   case VIEWER_ZOOM_050:
00955   case VIEWER_ZOOM_030:
00956   case VIEWER_ZOOM_020:
00957     mDocShell->SetZoom((aID - VIEWER_ZOOM_BASE) / 10.0f);
00958     break;
00959 
00960 #ifdef ClientWallet
00961   case PRVCY_PREFILL:
00962   case PRVCY_QPREFILL:
00963   nsIPresShell* shell;
00964   shell = nsnull;
00965   shell = GetPresShell();
00966   res = CallGetService(kWalletServiceCID, &walletservice);
00967   if ((NS_OK == res) && (nsnull != walletservice)) {
00968     nsString urlString2;
00969 //    res = walletservice->WALLET_Prefill(shell, (PRVCY_QPREFILL == aID));
00970     NS_RELEASE(walletservice);
00971   }
00972 
00973 #ifndef HTMLDialogs 
00974   if (aID == PRVCY_PREFILL) {
00975     nsAutoString url(NS_LITERAL_STRING("file:///y|/htmldlgs.htm"));
00976     nsBrowserWindow* bw = nsnull;
00977     mApp->OpenWindow(PRUint32(~0), bw);
00978     bw->SetVisibility(PR_TRUE);
00979     bw->GoTo(url.get());
00980     NS_RELEASE(bw);
00981   }
00982 #endif
00983   break;
00984 
00985   case PRVCY_DISPLAY_WALLET:
00986   {
00987 
00988 
00989   /* set a cookie for the javascript wallet editor */
00990   res = CallGetService(kWalletServiceCID, &walletservice);
00991   if ((NS_OK == res) && (nsnull != walletservice)) {
00992     nsIURI * url;
00993     nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &res));
00994     if (NS_FAILED(res)) return nsEventStatus_eIgnore;
00995 
00996     nsIURI *uri = nsnull;
00997     res = service->NewURI(NS_LITERAL_CSTRING(WALLET_EDITOR_URL), nsnull, nsnull, &uri);
00998     if (NS_FAILED(res)) return nsEventStatus_eIgnore;
00999 
01000     res = uri->QueryInterface(NS_GET_IID(nsIURI), (void**)&url);
01001     NS_RELEASE(uri);
01002     if (NS_SUCCEEDED(res)) {
01003 //      res = walletservice->WALLET_PreEdit(url);
01004       NS_RELEASE(walletservice);
01005     }
01006   }
01007 
01008   /* invoke the javascript wallet editor */
01009   nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
01010   webNav->LoadURI(urlString.get(), nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull);
01011   }
01012   break;
01013 #endif
01014 
01015 #if defined(CookieManagement)
01016   case PRVCY_DISPLAY_COOKIES:
01017   {
01018       break;
01019   }
01020 #endif
01021 
01022 #if defined(SingleSignon)
01023   case PRVCY_DISPLAY_SIGNONS:
01024   res = CallGetService(kWalletServiceCID, &walletservice);
01025   if ((NS_OK == res) && (nsnull != walletservice)) {
01026 //    res = walletservice->SI_DisplaySignonInfoAsHTML();
01027     NS_RELEASE(walletservice);
01028   }
01029   break;
01030 #endif
01031 
01032   }
01033 
01034   // Any menu IDs that the editor uses will be processed here
01035   DoEditorTest(mDocShell, aID);
01036 
01037   return nsEventStatus_eIgnore;
01038 }
01039 
01040 void
01041 nsBrowserWindow::Back()
01042 {
01043   nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
01044   webNav->GoBack();
01045 }
01046 
01047 void
01048 nsBrowserWindow::Forward()
01049 {
01050   nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
01051   webNav->GoForward();
01052 }
01053 
01054 void
01055 nsBrowserWindow::GoTo(const PRUnichar* aURL)
01056 {
01057    nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
01058    webNav->LoadURI(aURL, nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull);
01059 }
01060 
01061 
01062 static PRBool GetFileFromFileSelector(nsIDOMWindowInternal* aParentWindow,
01063                                       nsILocalFile **aFile,
01064                                       nsILocalFile **aDisplayDirectory)
01065 {
01066   nsresult rv;
01067   nsCOMPtr<nsIFilePicker> filePicker = do_CreateInstance("@mozilla.org/filepicker;1");
01068 
01069   if (filePicker) {
01070     rv = filePicker->Init(aParentWindow, NS_LITERAL_STRING("Open HTML"),
01071                           nsIFilePicker::modeOpen);
01072     if (NS_SUCCEEDED(rv)) {
01073       filePicker->AppendFilters(nsIFilePicker::filterAll | nsIFilePicker::filterHTML |
01074                                 nsIFilePicker::filterXML | nsIFilePicker::filterImages);
01075       if (*aDisplayDirectory)
01076         filePicker->SetDisplayDirectory(*aDisplayDirectory);
01077       
01078       PRInt16 dialogResult;
01079       rv = filePicker->Show(&dialogResult);
01080       if (NS_FAILED(rv) || dialogResult == nsIFilePicker::returnCancel)
01081         return PR_FALSE;
01082 
01083       filePicker->GetFile(aFile);
01084       if (*aFile) {
01085         NS_IF_RELEASE(*aDisplayDirectory);
01086         filePicker->GetDisplayDirectory(aDisplayDirectory);
01087         return PR_TRUE;
01088       }
01089     }
01090   }
01091 
01092   return PR_FALSE;
01093 }
01094 
01095 void
01096 nsBrowserWindow::DoFileOpen()
01097 {
01098   nsCOMPtr<nsILocalFile> file;
01099   nsCOMPtr<nsIDOMWindow> domWindow;
01100   nsCOMPtr<nsIDOMWindowInternal> parentWindow;
01101   nsresult rv;
01102 
01103   // get nsIDOMWindowInternal interface for nsIFilePicker
01104   rv = mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
01105   if (NS_SUCCEEDED(rv))
01106     parentWindow = do_QueryInterface(domWindow);
01107 
01108   if (GetFileFromFileSelector(parentWindow, getter_AddRefs(file),
01109                               getter_AddRefs(mOpenFileDirectory))) {
01110     nsCOMPtr<nsIURI> uri;
01111     NS_NewFileURI(getter_AddRefs(uri), file);
01112     if (uri) {
01113       nsCAutoString spec;
01114       uri->GetSpec(spec);
01115       
01116       // Ask the Web widget to load the file URL
01117       nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(mWebBrowser));
01118       webNav->LoadURI(NS_ConvertUTF8toUCS2(spec).get(),
01119                       nsIWebNavigation::LOAD_FLAGS_NONE,
01120                       nsnull,
01121                       nsnull,
01122                       nsnull);
01123       SetVisibility(PR_TRUE);
01124     }
01125   }
01126 }
01127 
01128 #define DIALOG_FONT      "Helvetica"
01129 #define DIALOG_FONT_SIZE 10
01130 
01131 //---------------------------------------------------------------
01132 NS_IMETHODIMP nsBrowserWindow::FindNext(const nsString &aSearchStr, PRBool aMatchCase, PRBool aSearchDown, PRBool &aIsFound)
01133 {
01134   // Insert find code here.
01135 
01136   return NS_OK;
01137 }
01138 
01139 //---------------------------------------------------------------
01140 NS_IMETHODIMP nsBrowserWindow::ForceRefresh()
01141 {
01142   nsIPresShell* shell = GetPresShell();
01143   if (nsnull != shell) {
01144     nsIViewManager *vm = shell->GetViewManager();
01145     if (vm) {
01146       nsIView* root;
01147       vm->GetRootView(root);
01148       if (nsnull != root) {
01149         vm->UpdateView(root, NS_VMREFRESH_IMMEDIATE);
01150       }
01151     }
01152     NS_RELEASE(shell);
01153   }
01154   return NS_OK;
01155 }
01156 
01157 
01165 nsEventStatus nsBrowserWindow::ProcessDialogEvent(nsGUIEvent *aEvent)
01166 { 
01167   nsEventStatus result = nsEventStatus_eIgnore;
01168 
01169   //printf("aEvent->message %d\n", aEvent->message);
01170   switch(aEvent->message) {
01171 
01172     case NS_KEY_DOWN: {
01173     } break;
01174 
01175     case NS_MOUSE_LEFT_BUTTON_UP: {
01176     } break;
01177 
01178     case NS_PAINT: 
01179       break;
01180     default:
01181       result = nsEventStatus_eIgnore;
01182   }
01183     //printf("result: %d = %d\n", result, PR_FALSE);
01184 
01185     return result;
01186 }
01187 
01188 void
01189 nsBrowserWindow::DoFind()
01190 {
01191 }
01192 
01193 
01194 //----------------------------------------------------------------------
01195 
01196 #define VIEWER_BUNDLE_URL NS_LITERAL_CSTRING("resource:/res/viewer.properties")
01197 
01198 static nsString* gTitleSuffix = nsnull;
01199 
01200 #if XXX
01201 static nsString*
01202 GetTitleSuffix(void)
01203 {
01204   nsString* suffix = new nsString(" - Failed");
01205   nsIStringBundleService* service = nsnull;
01206   nsresult ret = CallGetService(kStringBundleServiceCID, &service);
01207   if (NS_FAILED(ret)) {
01208     return suffix;
01209   }
01210   nsIURI* url = nsnull;
01211     nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret));
01212     if (NS_FAILED(ret)) return ret;
01213 
01214     nsIURI *uri = nsnull;
01215     ret = service->NewURI(VIEWER_BUNDLE_URL, nsnull, nsnull, &uri);
01216     if (NS_FAILED(ret)) return ret;
01217 
01218     ret = uri->QueryInterface(NS_GET_IID(nsIURI), (void**)&url);
01219     NS_RELEASE(uri);
01220   if (NS_FAILED(ret)) {
01221     NS_RELEASE(service);
01222     return suffix;
01223   }
01224   nsIStringBundle* bundle = nsnull;
01225   ret = service->CreateBundle(url, &bundle);
01226   NS_RELEASE(url);
01227   if (NS_FAILED(ret)) {
01228     NS_RELEASE(service);
01229     return suffix;
01230   }
01231   ret = bundle->GetStringFromID(1, *suffix);
01232   NS_RELEASE(bundle);
01233   NS_RELEASE(service);
01234 
01235   return suffix;
01236 }
01237 #endif
01238 
01239 // Note: operator new zeros our memory
01240 nsBrowserWindow::nsBrowserWindow()
01241 {
01242   if (!gTitleSuffix) {
01243 #if XXX
01244     gTitleSuffix = GetTitleSuffix();
01245 #endif
01246     if ( (gTitleSuffix = new nsString) != 0 )
01247       gTitleSuffix->AssignLiteral(" - Raptor");
01248   }
01249   AddBrowser(this);
01250 }
01251 
01252 nsBrowserWindow::~nsBrowserWindow()
01253 {
01254   NS_IF_RELEASE(mAppShell);
01255   NS_IF_RELEASE(mTableInspectorDialog);
01256   NS_IF_RELEASE(mImageInspectorDialog);
01257   NS_IF_RELEASE(mWebCrawler);
01258 
01259   if (nsnull != mTableInspector) {
01260     delete mTableInspector;
01261   }
01262   if (nsnull != mImageInspector) {
01263     delete mImageInspector;
01264   }
01265   if (mWebBrowserChrome) {
01266     mWebBrowserChrome->BrowserWindow(nsnull);
01267     NS_RELEASE(mWebBrowserChrome);
01268   }
01269   if (nsnull != gTitleSuffix) {
01270     delete gTitleSuffix;
01271     gTitleSuffix = nsnull;
01272   }
01273 }
01274 
01275 NS_IMPL_ISUPPORTS3(nsBrowserWindow, nsIBaseWindow, nsIInterfaceRequestor, nsIProgressEventSink)
01276 
01277 nsresult
01278 nsBrowserWindow::GetInterface(const nsIID& aIID,
01279                               void** aInstancePtrResult)
01280 {
01281   nsresult rv;
01282 
01283   NS_PRECONDITION(aInstancePtrResult, "null pointer");
01284   if (!aInstancePtrResult)
01285     return NS_ERROR_NULL_POINTER;
01286 
01287   *aInstancePtrResult = NULL;
01288 
01289   if (aIID.Equals(NS_GET_IID(nsIWebBrowserChrome))) {
01290     rv = EnsureWebBrowserChrome();
01291     if (NS_SUCCEEDED(rv))
01292       return mWebBrowserChrome->QueryInterface(aIID, aInstancePtrResult);
01293   }
01294 
01295   return QueryInterface(aIID, aInstancePtrResult);
01296 }
01297 
01298 nsresult
01299 nsBrowserWindow::Init(nsIAppShell* aAppShell,
01300                       const nsRect& aBounds,
01301                       PRUint32 aChromeMask,
01302                       PRBool aAllowPlugins)
01303 {
01304   mChromeMask = aChromeMask;
01305   mAppShell = aAppShell;
01306   NS_IF_ADDREF(mAppShell);
01307   mAllowPlugins = aAllowPlugins;
01308 
01309   // Create top level window
01310   nsresult rv;
01311   mWindow = do_CreateInstance(kWindowCID, &rv);
01312   if (NS_OK != rv) {
01313     return rv;
01314   }
01315   nsWidgetInitData initData;
01316   initData.mWindowType = eWindowType_toplevel;
01317   initData.mBorderStyle = eBorderStyle_default;
01318 
01319   nsRect r(0, 0, aBounds.width, aBounds.height);
01320   mWindow->Create((nsIWidget*)NULL, r, HandleBrowserEvent,
01321                   nsnull, aAppShell, nsnull, &initData);
01322   mWindow->GetClientBounds(r);
01323 
01324   // Create web shell
01325   mWebBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID);
01326   NS_ENSURE_TRUE(mWebBrowser, NS_ERROR_FAILURE);
01327 
01328   r.x = r.y = 0;
01329   nsCOMPtr<nsIBaseWindow> webBrowserWin(do_QueryInterface(mWebBrowser));
01330   rv = webBrowserWin->InitWindow(mWindow->GetNativeData(NS_NATIVE_WIDGET), nsnull, r.x, r.y, r.width, r.height);
01331   NS_ENSURE_SUCCESS(EnsureWebBrowserChrome(), NS_ERROR_FAILURE);
01332   mWebBrowser->SetContainerWindow(mWebBrowserChrome);
01333   nsWeakPtr weakling(
01334       do_GetWeakReference((nsIWebProgressListener*)mWebBrowserChrome));
01335   mWebBrowser->AddWebBrowserListener(weakling, NS_GET_IID(nsIWebProgressListener));
01336 
01337   webBrowserWin->Create();
01338 
01339   // Give the embedding app a chance to do platforms-specific window setup
01340   InitNativeWindow();
01341 
01342   // Disable global history because we don't have profile-relative file locations
01343   nsCOMPtr<nsIWebBrowserSetup> setup(do_QueryInterface(mWebBrowser));
01344   if (setup)
01345     setup->SetProperty(nsIWebBrowserSetup::SETUP_USE_GLOBAL_HISTORY, PR_FALSE);
01346 
01347   mDocShell = do_GetInterface(mWebBrowser);
01348   mDocShell->SetAllowPlugins(aAllowPlugins);
01349   webBrowserWin->SetVisibility(PR_TRUE);
01350 
01351   if (nsIWebBrowserChrome::CHROME_MENUBAR & aChromeMask) {
01352     rv = CreateMenuBar(r.width);
01353     if (NS_OK != rv) {
01354       return rv;
01355     }
01356     mHaveMenuBar = PR_TRUE;
01357     mWindow->GetClientBounds(r);
01358     r.x = r.y = 0;
01359   }
01360 
01361   if (nsIWebBrowserChrome::CHROME_TOOLBAR & aChromeMask) {
01362     rv = CreateToolBar(r.width);
01363     if (NS_OK != rv) {
01364       return rv;
01365     }
01366   }
01367 
01368   if (nsIWebBrowserChrome::CHROME_STATUSBAR & aChromeMask) {
01369     rv = CreateStatusBar(r.width);
01370     if (NS_OK != rv) {
01371       return rv;
01372     }
01373   }
01374 
01375   // Now lay it all out
01376   Layout(r.width, r.height);
01377 
01378   return NS_OK;
01379 }
01380 
01381 nsresult
01382 nsBrowserWindow::Init(nsIAppShell* aAppShell,
01383                       const nsRect& aBounds,
01384                       PRUint32 aChromeMask,
01385                       PRBool aAllowPlugins,
01386                       nsIDocumentViewer* aDocumentViewer,
01387                       nsPresContext* aPresContext)
01388 {
01389   mChromeMask = aChromeMask;
01390   mAppShell = aAppShell;
01391   NS_IF_ADDREF(mAppShell);
01392   mAllowPlugins = aAllowPlugins;
01393   // Create top level window
01394   nsresult rv;
01395   mWindow = do_CreateInstance(kWindowCID, &rv);
01396   if (NS_FAILED(rv)) {
01397     return rv;
01398   }
01399   nsWidgetInitData initData;
01400   initData.mWindowType = eWindowType_toplevel;
01401   initData.mBorderStyle = eBorderStyle_default;
01402   nsRect r(0, 0, aBounds.width, aBounds.height);
01403   mWindow->Create((nsIWidget*)NULL, r, HandleBrowserEvent,
01404                  
01405 nsnull, aAppShell, nsnull, &initData);
01406   mWindow->GetClientBounds(r);
01407   // Create web shell
01408   mWebBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID);
01409   NS_ENSURE_TRUE(mWebBrowser, NS_ERROR_FAILURE);
01410   r.x = r.y = 0;
01411   nsCOMPtr<nsIBaseWindow> webBrowserWin(do_QueryInterface(mWebBrowser));
01412   rv = webBrowserWin->InitWindow(mWindow->GetNativeData(NS_NATIVE_WIDGET),
01413 nsnull, r.x, r.y, r.width, r.height);
01414   NS_ENSURE_SUCCESS(EnsureWebBrowserChrome(), NS_ERROR_FAILURE);
01415   mWebBrowser->SetContainerWindow(mWebBrowserChrome);
01416   webBrowserWin->Create();
01417   mDocShell = do_GetInterface(mWebBrowser);
01418   mDocShell->SetAllowPlugins(aAllowPlugins);
01419   webBrowserWin->SetVisibility(PR_TRUE);
01420   if (nsIWebBrowserChrome::CHROME_MENUBAR & aChromeMask) {
01421     rv = CreateMenuBar(r.width);
01422     if (NS_OK != rv) {
01423       return rv;
01424     }
01425     mWindow->GetClientBounds(r);
01426     r.x = r.y = 0;
01427   }
01428   if (nsIWebBrowserChrome::CHROME_TOOLBAR & aChromeMask) {
01429     rv = CreateToolBar(r.width);
01430     if (NS_OK != rv) {
01431       return rv;
01432     }
01433   }
01434   if (nsIWebBrowserChrome::CHROME_STATUSBAR & aChromeMask)
01435 {
01436     rv = CreateStatusBar(r.width);
01437     if (NS_OK != rv) {
01438       return rv;
01439     }
01440   }
01441   // Give the embedding app a chance to do platforms-specific window setup
01442   InitNativeWindow();
01443  
01444   // Now lay it all out
01445   Layout(r.width, r.height);
01446 
01447 
01448 
01449 
01450   // Create a document viewer and bind it to the webshell
01451   nsCOMPtr<nsIDocumentViewer> docv;
01452   aDocumentViewer->CreateDocumentViewerUsing(aPresContext,
01453                                              getter_AddRefs(docv));
01454   docv->SetContainer(mWebBrowser);
01455   nsCOMPtr<nsIContentViewerContainer> cvContainer = do_QueryInterface(mDocShell);
01456   cvContainer->Embed(docv, "duh", nsnull);
01457 
01458   webBrowserWin->SetVisibility(PR_TRUE);
01459 
01460   return NS_OK;
01461 }
01462 
01463 void
01464 nsBrowserWindow::SetWebCrawler(nsWebCrawler* aCrawler)
01465 {
01466   if (mWebCrawler) {
01467     if (mDocShell) {
01468       nsCOMPtr<nsIWebProgress> progress(do_GetInterface(mDocShell));
01469       NS_ASSERTION(progress, "no web progress avail");
01470 
01471       (void) progress->RemoveProgressListener(
01472                                 (nsIWebProgressListener*)mWebCrawler);
01473     }
01474     NS_RELEASE(mWebCrawler);
01475   }
01476   if (aCrawler) {
01477     mWebCrawler = aCrawler;
01478     NS_ADDREF(aCrawler);
01479   }
01480 }
01481 
01482 // XXX This sort of thing should be in a resource
01483 #define TOOL_BAR_FONT      "Helvetica"
01484 #define TOOL_BAR_FONT_SIZE 12
01485 #define STATUS_BAR_FONT      "Helvetica"
01486 #define STATUS_BAR_FONT_SIZE 10
01487 
01488 nsresult
01489 nsBrowserWindow::CreateToolBar(PRInt32 aWidth)
01490 {
01491   nsresult rv;
01492 
01493   nsIDeviceContext* dc = mWindow->GetDeviceContext();
01494   float t2d;
01495   t2d = dc->TwipsToDevUnits();
01496   float d2a;
01497   d2a = dc->DevUnitsToAppUnits();
01498   nsFont font(TOOL_BAR_FONT, NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
01499               NS_FONT_WEIGHT_NORMAL, 0,
01500               nscoord(NSIntPointsToTwips(TOOL_BAR_FONT_SIZE) * t2d * d2a));
01501   NS_RELEASE(dc);
01502 
01503 #ifdef USE_LOCAL_WIDGETS
01504   rv = NS_NewButton(&mBack);
01505 #else
01506   // Create and place back button
01507   rv = CallCreateInstance(kButtonCID, &mBack);
01508 #endif
01509 
01510   if (NS_OK != rv) {
01511     return rv;
01512   }
01513   nsRect r(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
01514 
01515   nsIWidget* widget = nsnull;
01516   NS_CreateButton(mWindow,mBack,r,HandleBackEvent,&font);
01517   if (NS_OK == mBack->QueryInterface(kIWidgetIID,(void**)&widget))
01518   {
01519     nsAutoString back(NS_LITERAL_STRING("Back"));
01520     mBack->SetLabel(back);
01521     NS_RELEASE(widget);
01522   }
01523 
01524 
01525   // Create and place forward button
01526   r.SetRect(BUTTON_WIDTH, 0, BUTTON_WIDTH, BUTTON_HEIGHT);
01527   
01528 #ifdef USE_LOCAL_WIDGETS
01529   rv = NS_NewButton(&mForward);
01530 #else
01531   rv = CallCreateInstance(kButtonCID, &mForward);
01532 #endif
01533   if (NS_OK != rv) {
01534     return rv;
01535   }
01536   if (NS_OK == mForward->QueryInterface(kIWidgetIID,(void**)&widget))
01537   {
01538     widget->Create(mWindow.get(), r, HandleForwardEvent, NULL);
01539     widget->SetFont(font);
01540     widget->Show(PR_TRUE);
01541     nsAutoString forward(NS_LITERAL_STRING("Forward"));
01542     mForward->SetLabel(forward);
01543     NS_RELEASE(widget);
01544   }
01545 
01546 
01547   // Create and place location bar
01548   r.SetRect(2*BUTTON_WIDTH, 0,
01549             aWidth - 2*BUTTON_WIDTH - THROBBER_WIDTH,
01550             BUTTON_HEIGHT);
01551 
01552 #ifdef USE_LOCAL_WIDGETS
01553   rv = NS_NewTextWidget(&mLocation);
01554 #else
01555   rv = CallCreateInstance(kTextFieldCID, &mLocation);
01556 #endif
01557 
01558   if (NS_OK != rv) {
01559     return rv;
01560   }
01561 
01562   NS_CreateTextWidget(mWindow,mLocation,r,HandleLocationEvent,&font);
01563   if (NS_OK == mLocation->QueryInterface(kIWidgetIID,(void**)&widget))
01564   { 
01565     widget->SetForegroundColor(NS_RGB(0, 0, 0));
01566     widget->SetBackgroundColor(NS_RGB(255, 255, 255));
01567     PRUint32 size;
01568     nsAutoString empty;
01569     mLocation->SetText(empty, size);
01570     mLocation->SetMaxTextLength(MAX_TEXT_LENGTH);
01571     NS_RELEASE(widget);
01572   }
01573 
01574   // Create and place throbber
01575   r.SetRect(aWidth - THROBBER_WIDTH, 0,
01576             THROBBER_WIDTH, THROBBER_HEIGHT);
01577   mThrobber = nsThrobber::NewThrobber();
01578   nsString throbberURL; throbberURL.AssignWithConversion(THROBBER_AT);
01579   mThrobber->Init(mWindow, r, throbberURL, THROB_NUM);
01580   mThrobber->Show();
01581   return NS_OK;
01582 }
01583 
01584 // Overload this method in your nsNativeBrowserWindow if you need to 
01585 // have the logic in nsBrowserWindow::Layout() offset the menu within
01586 // the parent window.
01587 nsresult
01588 nsBrowserWindow::GetMenuBarHeight(PRInt32 * aHeightOut)
01589 {
01590   NS_ASSERTION(nsnull != aHeightOut,"null out param.");
01591 
01592   *aHeightOut = 0;
01593 
01594   return NS_OK;
01595 }
01596 
01597 nsresult
01598 nsBrowserWindow::CreateStatusBar(PRInt32 aWidth)
01599 {
01600   nsresult rv;
01601 
01602   nsIDeviceContext* dc = mWindow->GetDeviceContext();
01603   float t2d;
01604   t2d = dc->TwipsToDevUnits();
01605   nsFont font(STATUS_BAR_FONT, NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
01606               NS_FONT_WEIGHT_NORMAL, 0,
01607               nscoord(t2d * NSIntPointsToTwips(STATUS_BAR_FONT_SIZE)));
01608   NS_RELEASE(dc);
01609 
01610   nsRect r(0, 0, aWidth, THROBBER_HEIGHT);
01611 
01612 #ifdef USE_LOCAL_WIDGETS
01613   rv = NS_NewTextWidget(&mStatus);
01614 #else
01615   rv = CallCreateInstance(kTextFieldCID, &mStatus);
01616 #endif
01617 
01618   if (NS_OK != rv) {
01619     return rv;
01620   }
01621 
01622   nsIWidget* widget = nsnull;
01623   NS_CreateTextWidget(mWindow,mStatus,r,HandleLocationEvent,&font);
01624   if (NS_OK == mStatus->QueryInterface(kIWidgetIID,(void**)&widget))
01625   {
01626     widget->SetForegroundColor(NS_RGB(0, 0, 0));
01627     PRUint32 size;
01628     mStatus->SetText(EmptyString(),size);
01629     mStatus->SetMaxTextLength(MAX_TEXT_LENGTH);
01630 
01631     nsITextWidget* textWidget = nsnull;
01632     if (NS_OK == mStatus->QueryInterface(kITextWidgetIID,(void**)&textWidget))
01633     {
01634       PRBool  wasReadOnly;
01635       textWidget->SetReadOnly(PR_TRUE, wasReadOnly);
01636       NS_RELEASE(textWidget);
01637     }
01638 
01639     NS_RELEASE(widget);
01640   }
01641 
01642   return NS_OK;
01643 }
01644 
01645 void
01646 nsBrowserWindow::Layout(PRInt32 aWidth, PRInt32 aHeight)
01647 {
01648   nscoord txtHeight;
01649   nscoord menuBarHeight;
01650   {
01651     nsCOMPtr<nsILookAndFeel> lookAndFeel = do_GetService(kLookAndFeelCID);
01652     if (lookAndFeel) {
01653       lookAndFeel->GetMetric(nsILookAndFeel::eMetric_TextFieldHeight, txtHeight);
01654     } else {
01655       txtHeight = 24;
01656     }
01657   }
01658 
01659   // Find out the menubar height
01660   GetMenuBarHeight(&menuBarHeight);
01661 
01662   nsRect rr(0, 0, aWidth, aHeight);
01663 
01664   // position location bar (it's stretchy)
01665   if (nsIWebBrowserChrome::CHROME_TOOLBAR & mChromeMask) {
01666     nsIWidget* locationWidget = nsnull;
01667     if (mLocation &&
01668         NS_SUCCEEDED(mLocation->QueryInterface(kIWidgetIID,
01669                                                (void**)&locationWidget))) {
01670       if (mThrobber) {
01671         PRInt32 width = PR_MAX(aWidth - (2*BUTTON_WIDTH + THROBBER_WIDTH), 0);
01672       
01673         locationWidget->Resize(2*BUTTON_WIDTH, menuBarHeight,
01674                                width,
01675                                BUTTON_HEIGHT,
01676                                PR_TRUE);
01677         mThrobber->MoveTo(aWidth - THROBBER_WIDTH, menuBarHeight);
01678       }
01679       else {
01680         PRInt32 width = PR_MAX(aWidth - 2*BUTTON_WIDTH, 0);
01681         locationWidget->Resize(2*BUTTON_WIDTH, menuBarHeight,
01682                                width,
01683                                BUTTON_HEIGHT,
01684                                PR_TRUE);
01685       }
01686 
01687       locationWidget->Show(PR_TRUE);
01688       NS_RELEASE(locationWidget);
01689 
01690       nsIWidget* w = nsnull;
01691       if (mBack &&
01692           NS_SUCCEEDED(mBack->QueryInterface(kIWidgetIID, (void**)&w))) {
01693         w->Move(0, menuBarHeight);
01694         w->Show(PR_TRUE);
01695         NS_RELEASE(w);
01696       }
01697       if (mForward &&
01698           NS_SUCCEEDED(mForward->QueryInterface(kIWidgetIID, (void**)&w))) {
01699         w->Move(BUTTON_WIDTH, menuBarHeight);
01700         w->Show(PR_TRUE);
01701         NS_RELEASE(w);
01702       }
01703     }
01704   }
01705   else {
01706     nsIWidget* w = nsnull;
01707     if (mLocation &&
01708         NS_SUCCEEDED(mLocation->QueryInterface(kIWidgetIID, (void**)&w))) {
01709       w->Show(PR_FALSE);
01710       NS_RELEASE(w);
01711     }
01712     if (mBack &&
01713         NS_SUCCEEDED(mBack->QueryInterface(kIWidgetIID, (void**)&w))) {
01714       w->Move(0, menuBarHeight);
01715       w->Show(PR_FALSE);
01716       NS_RELEASE(w);
01717     }
01718     if (mForward &&
01719         NS_SUCCEEDED(mForward->QueryInterface(kIWidgetIID, (void**)&w))) {
01720       w->Move(BUTTON_WIDTH, menuBarHeight);
01721       w->Show(PR_FALSE);
01722       NS_RELEASE(w);
01723     }
01724     if (mThrobber) {
01725       mThrobber->Hide();
01726     }
01727   }
01728   
01729   nsIWidget* statusWidget = nsnull;
01730 
01731   if (mStatus && NS_OK == mStatus->QueryInterface(kIWidgetIID,(void**)&statusWidget)) {
01732     if (mChromeMask & nsIWebBrowserChrome::CHROME_STATUSBAR) {
01733       statusWidget->Resize(0, aHeight - txtHeight,
01734                            aWidth, txtHeight,
01735                            PR_TRUE);
01736 
01737       //Since allowing a negative height is a bad idea, let's condition this...
01738       rr.height = PR_MAX(0,rr.height-txtHeight);
01739       statusWidget->Show(PR_TRUE);
01740     }
01741     else {
01742       statusWidget->Show(PR_FALSE);
01743     }
01744     NS_RELEASE(statusWidget);
01745   }
01746 
01747   // inset the web widget
01748 
01749   if (nsIWebBrowserChrome::CHROME_TOOLBAR & mChromeMask) {
01750     rr.height -= BUTTON_HEIGHT;
01751     rr.y += BUTTON_HEIGHT;
01752   }
01753 
01754   rr.x += WEBSHELL_LEFT_INSET;
01755   rr.y += WEBSHELL_TOP_INSET + menuBarHeight;
01756   rr.width -= WEBSHELL_LEFT_INSET + WEBSHELL_RIGHT_INSET;
01757   rr.height -= WEBSHELL_TOP_INSET + WEBSHELL_BOTTOM_INSET + menuBarHeight;
01758 
01759   //Since allowing a negative height is a bad idea, let's condition this...
01760   if(rr.height<0)
01761     rr.height=0;
01762 
01763   nsCOMPtr<nsIBaseWindow> webBrowserWin(do_QueryInterface(mWebBrowser));
01764   if (webBrowserWin) {
01765     webBrowserWin->SetPositionAndSize(rr.x, rr.y, rr.width, rr.height, PR_FALSE);
01766   }
01767 }
01768 
01769 NS_IMETHODIMP
01770 nsBrowserWindow::MoveTo(PRInt32 aX, PRInt32 aY)
01771 {
01772   NS_PRECONDITION(nsnull != mWindow, "null window");
01773   mWindow->Move(aX, aY);
01774   return NS_OK;
01775 }
01776 
01777 NS_IMETHODIMP
01778 nsBrowserWindow::SizeContentTo(PRInt32 aWidth, PRInt32 aHeight)
01779 {
01780   NS_PRECONDITION(nsnull != mWindow, "null window");
01781 
01782   // XXX We want to do this in one shot
01783   mWindow->Resize(aWidth, aHeight, PR_FALSE);
01784   Layout(aWidth, aHeight);
01785 
01786   return NS_OK;
01787 }
01788 
01789 NS_IMETHODIMP
01790 nsBrowserWindow::SizeWindowTo(PRInt32 aWidth, PRInt32 aHeight,
01791                               PRBool /*aWidthTransient*/,
01792                               PRBool /*aHeightTransient*/)
01793 {
01794   return SizeContentTo(aWidth, aHeight);
01795 }
01796 
01797 NS_IMETHODIMP
01798 nsBrowserWindow::GetContentBounds(nsRect& aBounds)
01799 {
01800   mWindow->GetClientBounds(aBounds);
01801   return NS_OK;
01802 }
01803 
01804 NS_IMETHODIMP
01805 nsBrowserWindow::GetWindowBounds(nsRect& aBounds)
01806 {
01807   mWindow->GetBounds(aBounds);
01808   return NS_OK;
01809 }
01810 
01811 NS_IMETHODIMP
01812 nsBrowserWindow::SetChrome(PRUint32 aChromeMask)
01813 {
01814   mChromeMask = aChromeMask;
01815   nsRect r;
01816   mWindow->GetClientBounds(r);
01817   Layout(r.width, r.height);
01818 
01819   return NS_OK;
01820 }
01821 
01822 NS_IMETHODIMP
01823 nsBrowserWindow::GetChrome(PRUint32& aChromeMaskResult)
01824 {
01825   aChromeMaskResult = mChromeMask;
01826   return NS_OK;
01827 }
01828 
01829 NS_IMETHODIMP
01830 nsBrowserWindow::GetDocShell(nsIDocShell*& aResult)
01831 {
01832   NS_IF_ADDREF(aResult = mDocShell);
01833   return NS_OK;
01834 }
01835 
01836 NS_IMETHODIMP
01837 nsBrowserWindow::GetContentDocShell(nsIDocShell **aResult)
01838 {
01839   NS_IF_ADDREF(*aResult = mDocShell);
01840   return NS_OK;
01841 }
01842 
01843 //----------------------------------------
01844 NS_IMETHODIMP
01845 nsBrowserWindow::SetProgress(PRInt32 aProgress, PRInt32 aProgressMax)
01846 {
01847   return NS_OK;
01848 }
01849 
01850 NS_IMETHODIMP
01851 nsBrowserWindow::ShowMenuBar(PRBool aShow)
01852 {
01853   return NS_OK;
01854 }
01855 
01856 //----------------------------------------
01857 
01858 NS_IMETHODIMP
01859 nsBrowserWindow::OnProgress(nsIRequest* request, nsISupports *ctxt,
01860                             PRUint64 aProgress, PRUint64 aProgressMax)
01861 {
01862   nsresult rv;
01863 
01864   nsCOMPtr<nsIURI> aURL;
01865   nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
01866   rv = channel->GetURI(getter_AddRefs(aURL));
01867   if (NS_FAILED(rv)) return rv;
01868   
01869   if (mStatus) {
01870     nsAutoString url;
01871     if (nsnull != aURL) {
01872       nsCAutoString str;
01873       aURL->GetSpec(str);
01874       AppendUTF8toUTF16(str, url);
01875     }
01876     url.AppendLiteral(": progress ");
01877     url.AppendInt(PRInt64(aProgress), 10);
01878     if (nsUint64(0) != aProgressMax) {
01879       url.AppendLiteral(" (out of ");
01880       url.AppendInt(PRInt64(aProgressMax), 10);
01881       url.Append(NS_LITERAL_STRING(")"));
01882     }
01883     PRUint32 size;
01884     mStatus->SetText(url,size);
01885   }
01886   return NS_OK;
01887 }
01888 
01889 NS_IMETHODIMP
01890 nsBrowserWindow::OnStatus(nsIRequest* request, nsISupports *ctxt,
01891                           nsresult aStatus, const PRUnichar *aStatusArg)
01892 {
01893   if (mStatus) {
01894     nsresult rv;
01895     nsCOMPtr<nsIStringBundleService> sbs = do_GetService(kStringBundleServiceCID, &rv);
01896     if (NS_FAILED(rv)) return rv;
01897     nsXPIDLString msg;
01898     rv = sbs->FormatStatusMessage(aStatus, aStatusArg, getter_Copies(msg));
01899     if (NS_FAILED(rv)) return rv;
01900     PRUint32 size;
01901     nsAutoString msg2( NS_STATIC_CAST(const PRUnichar*, msg));
01902     mStatus->SetText(msg2, size);
01903   }
01904   return NS_OK;
01905 }
01906 
01907 //----------------------------------------
01908 
01909 // Toolbar support
01910 
01911 void
01912 nsBrowserWindow::StartThrobber()
01913 {
01914 }
01915 
01916 void
01917 nsBrowserWindow::StopThrobber()
01918 {
01919 }
01920 
01921 void
01922 nsBrowserWindow::LoadThrobberImages()
01923 {
01924 }
01925 
01926 void
01927 nsBrowserWindow::DestroyThrobberImages()
01928 {
01929 }
01930 
01931 nsIPresShell*
01932 nsBrowserWindow::GetPresShell()
01933 {
01934   return GetPresShellFor(mDocShell);
01935 }
01936 
01937 
01938 void
01939 nsBrowserWindow::DoCopy()
01940 {
01941   nsIPresShell* shell = GetPresShell();
01942   if (nsnull != shell) {
01943     shell->DoCopy();
01944     NS_RELEASE(shell);
01945   }
01946 }
01947 
01948 void
01949 nsBrowserWindow::DoPaste()
01950 {
01951   nsIPresShell* shell = GetPresShell();
01952   if (nsnull != shell) {
01953 
01954     printf("nsBrowserWindow::DoPaste()\n");
01955 
01956     NS_RELEASE(shell);
01957   }
01958 }
01959 
01960 //----------------------------------------------------------------------
01961 
01962 void
01963 nsBrowserWindow::ShowPrintPreview(PRInt32 aID)
01964 {
01965   nsCOMPtr <nsIContentViewer> viewer;
01966 
01967   mDocShell->GetContentViewer(getter_AddRefs(viewer));
01968 
01969   if (viewer)
01970   {
01971     nsCOMPtr<nsIContentViewerFile> viewerFile = do_QueryInterface(viewer);
01972     if (viewerFile) {
01973       //viewerFile->PrintPreview(nsnull);
01974     }
01975   }
01976 }
01977 
01978 void nsBrowserWindow::DoPrint(void)
01979 {
01980   nsCOMPtr <nsIContentViewer> viewer;
01981 
01982   mDocShell->GetContentViewer(getter_AddRefs(viewer));
01983 
01984   if (viewer) {
01985     nsCOMPtr<nsIWebBrowserPrint> webBrowserPrint = do_QueryInterface(viewer);
01986     if (webBrowserPrint) {
01987       if (!mPrintSettings) {
01988         webBrowserPrint->GetGlobalPrintSettings(getter_AddRefs(mPrintSettings));
01989       }
01990       webBrowserPrint->Print(mPrintSettings, (nsIWebProgressListener*)nsnull);
01991     }
01992   }
01993 }
01994 
01995 //---------------------------------------------------------------
01996 nsIDOMDocument* nsBrowserWindow::GetDOMDocument(nsIDocShell *aDocShell)
01997 {
01998   nsIDOMDocument* domDoc = nsnull;
01999   if (nsnull != aDocShell) {
02000     nsIContentViewer* mCViewer;
02001     aDocShell->GetContentViewer(&mCViewer);
02002     if (nsnull != mCViewer) {
02003       nsIDocumentViewer* mDViewer;
02004       if (NS_OK == mCViewer->QueryInterface(kIDocumentViewerIID, (void**) &mDViewer)) {
02005         nsCOMPtr<nsIDocument> mDoc;
02006         mDViewer->GetDocument(getter_AddRefs(mDoc));
02007         if (nsnull != mDoc) {
02008           mDoc->QueryInterface(kIDOMDocumentIID, (void**) &domDoc);
02009         }
02010         NS_RELEASE(mDViewer);
02011       }
02012       NS_RELEASE(mCViewer);
02013     }
02014   }
02015   return domDoc;
02016 }
02017 
02018 //---------------------------------------------------------------
02019 
02020 void nsBrowserWindow::DoTableInspector()
02021 {
02022   if (mTableInspectorDialog) {
02023     mTableInspectorDialog->SetVisible(PR_TRUE);
02024     return;
02025   }
02026   nsIDOMDocument* domDoc = GetDOMDocument(mDocShell);
02027 
02028   if (nsnull != domDoc) {
02029     nsString printHTML(NS_LITERAL_STRING("resource:/res/samples/printsetup.html"));
02030     nsRect rect(0, 0, 375, 510);
02031     nsString title(NS_LITERAL_STRING("Table Inspector"));
02032 
02033     nsCOMPtr<nsIComponentManager> compMgr;
02034     NS_GetComponentManager(getter_AddRefs(compMgr));
02035 
02036     nsXPBaseWindow * xpWin = nsnull;
02037     nsresult rv = compMgr->CreateInstance(kXPBaseWindowCID, nsnull, kIXPBaseWindowIID, (void **) &xpWin);
02038     if (rv == NS_OK) {
02039       xpWin->Init(eXPBaseWindowType_dialog, mAppShell, printHTML, title, rect, PRUint32(~0), PR_FALSE);
02040       xpWin->SetVisible(PR_TRUE);
02041       if (NS_OK == xpWin->QueryInterface(kIXPBaseWindowIID, (void**) &mTableInspectorDialog)) {
02042         mTableInspector = new nsTableInspectorDialog(this, domDoc); // ref counts domDoc
02043         if (nsnull != mTableInspector) {
02044           xpWin->AddWindowListener(mTableInspector); 
02045         }
02046       }
02047       NS_RELEASE(xpWin);
02048     }
02049     NS_RELEASE(domDoc);
02050   }
02051 
02052 }
02053 void nsBrowserWindow::DoImageInspector()
02054 {
02055   if (mImageInspectorDialog) {
02056     mImageInspectorDialog->SetVisible(PR_TRUE);
02057     return;
02058   }
02059 
02060   nsIDOMDocument* domDoc = GetDOMDocument(mDocShell);
02061 
02062   if (nsnull != domDoc) {
02063     nsString printHTML(NS_LITERAL_STRING("resource:/res/samples/image_props.html"));
02064     nsRect rect(0, 0, 485, 124);
02065     nsString title(NS_LITERAL_STRING("Image Inspector"));
02066 
02067     nsCOMPtr<nsIComponentManager> compMgr;
02068     NS_GetComponentManager(getter_AddRefs(compMgr));
02069 
02070     nsXPBaseWindow * xpWin;
02071     nsresult rv = compMgr->CreateInstance(kXPBaseWindowCID, nsnull, kIXPBaseWindowIID, (void **) &xpWin);
02072     if (rv == NS_OK) {
02073       xpWin->Init(eXPBaseWindowType_dialog, mAppShell, printHTML, title, rect, PRUint32(~0), PR_FALSE);
02074       xpWin->SetVisible(PR_TRUE);
02075       if (NS_OK == xpWin->QueryInterface(kIXPBaseWindowIID, (void**) &mImageInspectorDialog)) {
02076         mImageInspector = new nsImageInspectorDialog(this, domDoc); // ref counts domDoc
02077         if (nsnull != mImageInspector) {
02078           xpWin->AddWindowListener(mImageInspector); 
02079         }
02080       }
02081       NS_RELEASE(xpWin);
02082     }
02083     NS_RELEASE(domDoc);
02084   }
02085 
02086 }
02087 
02088 //----------------------------------------------------------------------
02089 
02090 void
02091 nsBrowserWindow::DoJSConsole()
02092 {
02093   mApp->CreateJSConsole(this);
02094 }
02095 
02096 void
02097 nsBrowserWindow::DoPrefs()
02098 {
02099   mApp->DoPrefs(this);
02100 }
02101 
02102 void
02103 nsBrowserWindow::DoEditorMode(nsIDocShell *aDocShell)
02104 {
02105   PRInt32 i, n;
02106   if (nsnull != aDocShell) {
02107     nsIContentViewer* mCViewer;
02108     aDocShell->GetContentViewer(&mCViewer);
02109     if (nsnull != mCViewer) {
02110       nsIDocumentViewer* mDViewer;
02111       if (NS_OK == mCViewer->QueryInterface(kIDocumentViewerIID, (void**) &mDViewer)) 
02112       {
02113         nsCOMPtr<nsIDocument> mDoc;
02114         mDViewer->GetDocument(getter_AddRefs(mDoc));
02115         if (nsnull != mDoc) {
02116           nsIDOMDocument* mDOMDoc;
02117           if (NS_OK == mDoc->QueryInterface(kIDOMDocumentIID, (void**) &mDOMDoc)) 
02118           {
02119             nsIPresShell* shell = GetPresShellFor(aDocShell);
02120             NS_InitEditorMode(mDOMDoc, shell);
02121             NS_RELEASE(mDOMDoc);
02122             NS_IF_RELEASE(shell);
02123           }
02124         }
02125         NS_RELEASE(mDViewer);
02126       }
02127       NS_RELEASE(mCViewer);
02128     }
02129     
02130     nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
02131     docShellAsNode->GetChildCount(&n);
02132     for (i = 0; i < n; i++) {
02133       nsCOMPtr<nsIDocShellTreeItem> child;
02134       docShellAsNode->GetChildAt(i, getter_AddRefs(child));
02135       nsCOMPtr<nsIDocShell> childAsShell(do_QueryInterface(child));
02136       DoEditorMode(childAsShell);
02137     }
02138   }
02139 }
02140 
02141 // Same as above, but calls NS_DoEditorTest instead of starting an editor
02142 void
02143 nsBrowserWindow::DoEditorTest(nsIDocShell *aDocShell, PRInt32 aCommandID)
02144 {
02145   if (nsnull != aDocShell) {
02146     nsIContentViewer* mCViewer;
02147     aDocShell->GetContentViewer(&mCViewer);
02148     if (nsnull != mCViewer) {
02149       nsIDocumentViewer* mDViewer;
02150       if (NS_OK == mCViewer->QueryInterface(kIDocumentViewerIID, (void**) &mDViewer)) 
02151       {
02152         nsCOMPtr<nsIDocument> mDoc;
02153         mDViewer->GetDocument(getter_AddRefs(mDoc));
02154         if (nsnull != mDoc) {
02155           nsIDOMDocument* mDOMDoc;
02156           if (NS_OK == mDoc->QueryInterface(kIDOMDocumentIID, (void**) &mDOMDoc)) 
02157           {
02158             NS_DoEditorTest(aCommandID);
02159             NS_RELEASE(mDOMDoc);
02160           }
02161         }
02162         NS_RELEASE(mDViewer);
02163       }
02164       NS_RELEASE(mCViewer);
02165     }
02166     // Do we need to do all the children as in DoEditorMode?
02167     // Its seems to work if we don't do that
02168   }
02169 }
02170 
02171 
02172 
02173 
02174 #ifdef NS_DEBUG
02175 #include "nsIContent.h"
02176 #include "nsIFrame.h"
02177 #include "nsIFrameDebug.h"
02178 
02179 
02180 static void DumpAWebShell(nsIDocShellTreeItem* aShellItem, FILE* out, PRInt32 aIndent)
02181 {
02182   nsXPIDLString name;
02183   nsAutoString str;
02184   nsCOMPtr<nsIDocShellTreeItem> parent;
02185   PRInt32 i, n;
02186 
02187   for (i = aIndent; --i >= 0; ) fprintf(out, "  ");
02188 
02189   fprintf(out, "%p '", aShellItem);
02190   aShellItem->GetName(getter_Copies(name));
02191   aShellItem->GetSameTypeParent(getter_AddRefs(parent));
02192   str.Assign(name);
02193   fputs(NS_LossyConvertUCS2toASCII(str).get(), out);
02194   fprintf(out, "' parent=%p <\n", parent.get());
02195 
02196   aIndent++;
02197   nsCOMPtr<nsIDocShellTreeNode> shellAsNode(do_QueryInterface(aShellItem));
02198   shellAsNode->GetChildCount(&n);
02199   for (i = 0; i < n; i++) {
02200     nsCOMPtr<nsIDocShellTreeItem> child;
02201     shellAsNode->GetChildAt(i, getter_AddRefs(child));
02202     if (child) {
02203       DumpAWebShell(child, out, aIndent);
02204     }
02205   }
02206   aIndent--;
02207   for (i = aIndent; --i >= 0; ) fprintf(out, "  ");
02208   fputs(">\n", out);
02209 }
02210 
02211 void
02212 nsBrowserWindow::DumpWebShells(FILE* out)
02213 {
02214   nsCOMPtr<nsIDocShellTreeItem> shellAsItem(do_QueryInterface(mDocShell));
02215   DumpAWebShell(shellAsItem, out, 0);
02216 }
02217 
02218 static
02219 void 
02220 DumpMultipleWebShells(nsBrowserWindow& aBrowserWindow, nsIDocShell* aDocShell, FILE* aOut)
02221 { 
02222   PRInt32 count;
02223   nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
02224   if (docShellAsNode) {
02225     docShellAsNode->GetChildCount(&count);
02226     if (count > 0) {
02227       fprintf(aOut, "webshells= \n");
02228       aBrowserWindow.DumpWebShells(aOut);
02229     }
02230   }
02231 }
02232 
02233 static
02234 void
02235 DumpContentRecurse(nsIDocShell* aDocShell, FILE* out)
02236 {
02237   if (nsnull != aDocShell) {
02238     fprintf(out, "docshell=%p \n", aDocShell);
02239     nsIPresShell* shell = GetPresShellFor(aDocShell);
02240     if (nsnull != shell) {
02241       nsIDocument *doc = shell->GetDocument();
02242       if (doc) {
02243         nsIContent *root = doc->GetRootContent();
02244         if (nsnull != root) {
02245           root->List(out);
02246         }
02247       }
02248       NS_RELEASE(shell);
02249     }
02250     else {
02251       fputs("null pres shell\n", out);
02252     }
02253     // dump the frames of the sub documents
02254     PRInt32 i, n;
02255     nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
02256     docShellAsNode->GetChildCount(&n);
02257     for (i = 0; i < n; i++) {
02258       nsCOMPtr<nsIDocShellTreeItem> child;
02259       docShellAsNode->GetChildAt(i, getter_AddRefs(child));
02260       nsCOMPtr<nsIDocShell> childAsShell(do_QueryInterface(child));
02261       if (nsnull != child) {
02262         DumpContentRecurse(childAsShell, out);
02263       }
02264     }
02265   }
02266 }
02267 
02268 void
02269 nsBrowserWindow::DumpContent(FILE* out)
02270 {
02271   DumpContentRecurse(mDocShell, out);
02272   DumpMultipleWebShells(*this, mDocShell, out);
02273 }
02274 
02275 static void
02276 DumpFramesRecurse(nsIDocShell* aDocShell, FILE* out, nsString *aFilterName)
02277 {
02278   if (nsnull != aDocShell) {
02279     fprintf(out, "webshell=%p \n", aDocShell);
02280     nsIPresShell* shell = GetPresShellFor(aDocShell);
02281     if (nsnull != shell) {
02282       nsIFrame* root = shell->GetRootFrame();
02283       if (nsnull != root) {
02284         nsIFrameDebug* fdbg;
02285         if (NS_SUCCEEDED(CallQueryInterface(root, &fdbg))) {
02286           fdbg->List(shell->GetPresContext(), out, 0);
02287         }
02288       }
02289       NS_RELEASE(shell);
02290     }
02291     else {
02292       fputs("null pres shell\n", out);
02293     }
02294 
02295     // dump the frames of the sub documents
02296     PRInt32 i, n;
02297     nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
02298     docShellAsNode->GetChildCount(&n);
02299     for (i = 0; i < n; i++) {
02300       nsCOMPtr<nsIDocShellTreeItem> child;
02301       docShellAsNode->GetChildAt(i, getter_AddRefs(child));
02302       nsCOMPtr<nsIDocShell> childAsShell(do_QueryInterface(child));
02303       if (childAsShell) {
02304         DumpFramesRecurse(childAsShell, out, aFilterName);
02305       }
02306     }
02307   }
02308 }
02309 
02310 void
02311 nsBrowserWindow::DumpFrames(FILE* out, nsString *aFilterName)
02312 {
02313   DumpFramesRecurse(mDocShell, out, aFilterName);
02314   DumpMultipleWebShells(*this, mDocShell, out);
02315 }
02316 
02317 //----------------------------------------------------------------------
02318 
02319 static
02320 void
02321 DumpViewsRecurse(nsIDocShell* aDocShell, FILE* out)
02322 {
02323   if (nsnull != aDocShell) {
02324     fprintf(out, "docshell=%p \n", aDocShell);
02325     nsIPresShell* shell = GetPresShellFor(aDocShell);
02326     if (nsnull != shell) {
02327       nsIViewManager *vm = shell->GetViewManager();
02328       if (vm) {
02329         nsIView* root;
02330         vm->GetRootView(root);
02331         if (nsnull != root) {
02332           root->List(out);
02333         }
02334       }
02335       NS_RELEASE(shell);
02336     }
02337     else {
02338       fputs("null pres shell\n", out);
02339     }
02340 
02341     // dump the views of the sub documents
02342     PRInt32 i, n;
02343     nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
02344     docShellAsNode->GetChildCount(&n);
02345     for (i = 0; i < n; i++) {
02346       nsCOMPtr<nsIDocShellTreeItem> child;
02347       docShellAsNode->GetChildAt(i, getter_AddRefs(child));
02348       nsCOMPtr<nsIDocShell> childAsShell(do_QueryInterface(child));
02349       if (childAsShell) {
02350         DumpViewsRecurse(childAsShell, out);
02351       }
02352     }
02353   }
02354 }
02355 
02356 void
02357 nsBrowserWindow::DumpViews(FILE* out)
02358 {
02359   DumpViewsRecurse(mDocShell, out);
02360   DumpMultipleWebShells(*this, mDocShell, out);
02361 }
02362 
02363 void
02364 nsBrowserWindow::DumpStyleSheets(FILE* out)
02365 {
02366   nsCOMPtr<nsIPresShell> shell = dont_AddRef(GetPresShell());
02367   if (shell) {
02368     shell->ListStyleSheets(out);
02369   } else {
02370     fputs("null pres shell\n", out);
02371   }
02372 }
02373 
02374 void
02375 nsBrowserWindow::DumpStyleContexts(FILE* out)
02376 {
02377   nsCOMPtr<nsIPresShell> shell = dont_AddRef(GetPresShell());
02378   if (shell) {
02379     nsIFrame* root = shell->GetRootFrame();
02380     shell->ListStyleContexts(root, out);
02381   } else {
02382     fputs("null pres shell\n", out);
02383   }
02384 }
02385 
02386 void
02387 nsBrowserWindow::DumpReflowStats(FILE* out)
02388 {
02389   nsIPresShell* shell = GetPresShell();
02390   if (nsnull != shell) {
02391 #ifdef MOZ_REFLOW_PERF
02392     shell->DumpReflows();
02393 #else
02394     fprintf(out,"***********************************\n");
02395     fprintf(out, "Sorry, you have built with MOZ_REFLOW_PERF=1\n");
02396     fprintf(out,"***********************************\n");
02397 #endif
02398     NS_RELEASE(shell);
02399   } else {
02400     fputs("null pres shell\n", out);
02401   }
02402 }
02403 
02404 void
02405 nsBrowserWindow::DisplayReflowStats(PRBool aIsOn)
02406 {
02407   nsIPresShell* shell = GetPresShell();
02408   if (nsnull != shell) {
02409 #ifdef MOZ_REFLOW_PERF
02410     shell->SetPaintFrameCount(aIsOn);
02411     SetBoolPref("layout.reflow.showframecounts",aIsOn);
02412     ForceRefresh();
02413 #else
02414     printf("***********************************\n");
02415     printf("Sorry, you have built with MOZ_REFLOW_PERF=1\n");
02416     printf("***********************************\n");
02417 #endif
02418     NS_RELEASE(shell);
02419   }
02420 }
02421 
02422 void
02423 nsBrowserWindow::ToggleFrameBorders()
02424 {
02425   nsILayoutDebugger* ld;
02426   nsresult rv = CallCreateInstance(kLayoutDebuggerCID, &ld);
02427   if (NS_SUCCEEDED(rv)) {
02428     PRBool showing;
02429     ld->GetShowFrameBorders(&showing);
02430     ld->SetShowFrameBorders(!showing);
02431     ForceRefresh();
02432     NS_RELEASE(ld);
02433   }
02434 }
02435 
02436 void
02437 nsBrowserWindow::ToggleVisualEventDebugging()
02438 {
02439   nsILayoutDebugger* ld;
02440   nsresult rv = CallCreateInstance(kLayoutDebuggerCID, &ld);
02441   if (NS_SUCCEEDED(rv)) {
02442     PRBool showing;
02443     ld->GetShowEventTargetFrameBorder(&showing);
02444     ld->SetShowEventTargetFrameBorder(!showing);
02445     ForceRefresh();
02446     NS_RELEASE(ld);
02447   }
02448 }
02449 
02450 void
02451 nsBrowserWindow::ToggleBoolPrefAndRefresh(const char * aPrefName)
02452 {
02453   NS_ASSERTION(nsnull != aPrefName,"null pref name");
02454 
02455   nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID));
02456   if (prefs && nsnull != aPrefName)
02457   {
02458     PRBool value;
02459     prefs->GetBoolPref(aPrefName,&value);
02460     prefs->SetBoolPref(aPrefName,!value);
02461     prefs->SavePrefFile(nsnull);
02462     
02463     ForceRefresh();
02464   }
02465 }
02466 
02467 #endif
02468 
02469 void
02470 nsBrowserWindow::SetBoolPref(const char * aPrefName, PRBool aValue)
02471 {
02472   NS_ASSERTION(nsnull != aPrefName,"null pref name");
02473 
02474   nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID));
02475   if (prefs && nsnull != aPrefName)
02476   {
02477     prefs->SetBoolPref(aPrefName, aValue);
02478     prefs->SavePrefFile(nsnull);
02479   }
02480 }
02481 
02482 void
02483 nsBrowserWindow::SetStringPref(const char * aPrefName, const nsString& aValue)
02484 {
02485   NS_ASSERTION(nsnull != aPrefName, "null pref name");
02486 
02487   nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID));
02488   if (nsnull != prefs && nsnull != aPrefName)
02489   {
02490     char * prefStr = ToNewCString(aValue);
02491     prefs->SetCharPref(aPrefName, prefStr);
02492     prefs->SavePrefFile(nsnull);
02493     delete [] prefStr;
02494   }
02495 
02496 }
02497 
02498 void
02499 nsBrowserWindow::GetStringPref(const char * aPrefName, nsString& aValue)
02500 {
02501   NS_ASSERTION(nsnull != aPrefName, "null pref name");
02502 
02503   nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID));
02504   if (nsnull != prefs && nsnull != aPrefName)
02505   {
02506     char* prefCharVal;
02507     nsresult result = prefs->CopyCharPref(aPrefName, &prefCharVal);
02508     if (NS_SUCCEEDED(result)) {
02509       aValue.AssignWithConversion(prefCharVal);
02510       PL_strfree(prefCharVal);
02511     }
02512   }
02513 }
02514 
02515 #ifdef NS_DEBUG
02516 void
02517 nsBrowserWindow::DoDebugSave()
02518 {
02519   /* Removed 04/01/99 -- gpk */
02520 }
02521 
02522 void 
02523 nsBrowserWindow::DoToggleSelection()
02524 {
02525   nsIPresShell* shell = GetPresShell();
02526   if (nsnull != shell) {
02527     nsCOMPtr<nsISelectionController> selCon;
02528     selCon = do_QueryInterface(shell);
02529     if (selCon) {
02530       PRInt16  current;
02531       selCon->GetDisplaySelection(&current);
02532       selCon->SetDisplaySelection(!current);
02533       ForceRefresh();
02534     }
02535     NS_RELEASE(shell);
02536   }
02537 }
02538 
02539 void
02540 nsBrowserWindow::DoDebugRobot()
02541 {
02542   mApp->CreateRobot(this);
02543 }
02544 
02545 void
02546 nsBrowserWindow::DoSiteWalker()
02547 {
02548   mApp->CreateSiteWalker(this);
02549 }
02550 
02551 nsEventStatus
02552 nsBrowserWindow::DispatchDebugMenu(PRInt32 aID)
02553 {
02554   nsEventStatus result = nsEventStatus_eIgnore;
02555 
02556   switch(aID) {
02557     case VIEWER_VISUAL_DEBUGGING:
02558       ToggleFrameBorders();
02559       result = nsEventStatus_eConsumeNoDefault;
02560       break;
02561 
02562     case VIEWER_VISUAL_EVENT_DEBUGGING:
02563       ToggleVisualEventDebugging();
02564       result = nsEventStatus_eConsumeNoDefault;
02565       break;
02566 
02567     case VIEWER_TOGGLE_PAINT_FLASHING:
02568       ToggleBoolPrefAndRefresh("nglayout.debug.paint_flashing");
02569       result = nsEventStatus_eConsumeNoDefault;
02570       break;
02571 
02572     case VIEWER_TOGGLE_PAINT_DUMPING:
02573       ToggleBoolPrefAndRefresh("nglayout.debug.paint_dumping");
02574       result = nsEventStatus_eConsumeNoDefault;
02575       break;
02576 
02577     case VIEWER_TOGGLE_INVALIDATE_DUMPING:
02578       ToggleBoolPrefAndRefresh("nglayout.debug.invalidate_dumping");
02579       result = nsEventStatus_eConsumeNoDefault;
02580       break;
02581 
02582     case VIEWER_TOGGLE_EVENT_DUMPING:
02583       ToggleBoolPrefAndRefresh("nglayout.debug.event_dumping");
02584       result = nsEventStatus_eConsumeNoDefault;
02585       break;
02586 
02587     case VIEWER_TOGGLE_MOTION_EVENT_DUMPING:
02588       ToggleBoolPrefAndRefresh("nglayout.debug.motion_event_dumping");
02589       result = nsEventStatus_eConsumeNoDefault;
02590       break;
02591 
02592     case VIEWER_TOGGLE_CROSSING_EVENT_DUMPING:
02593       ToggleBoolPrefAndRefresh("nglayout.debug.crossing_event_dumping");
02594       result = nsEventStatus_eConsumeNoDefault;
02595       break;
02596 
02597     case VIEWER_DUMP_CONTENT:
02598       DumpContent();
02599       result = nsEventStatus_eConsumeNoDefault;
02600       break;
02601 
02602     case VIEWER_DUMP_FRAMES:
02603       DumpFrames();
02604       result = nsEventStatus_eConsumeNoDefault;
02605       break;
02606 
02607     case VIEWER_DUMP_VIEWS:
02608       DumpViews();
02609       result = nsEventStatus_eConsumeNoDefault;
02610       break;
02611 
02612     case VIEWER_DUMP_STYLE_SHEETS:
02613       DumpStyleSheets();
02614       result = nsEventStatus_eConsumeNoDefault;
02615       break;
02616 
02617     case VIEWER_DUMP_STYLE_CONTEXTS:
02618       DumpStyleContexts();
02619       result = nsEventStatus_eConsumeNoDefault;
02620       break;
02621 
02622     case VIEWER_DEBUG_DUMP_REFLOW_TOTS:
02623       DumpReflowStats();
02624       result = nsEventStatus_eConsumeNoDefault;
02625       break;
02626 
02627     case VIEWER_DSP_REFLOW_CNTS_ON:
02628       DisplayReflowStats(PR_TRUE);
02629       result = nsEventStatus_eConsumeNoDefault;
02630       break;
02631 
02632     case VIEWER_DSP_REFLOW_CNTS_OFF:
02633       DisplayReflowStats(PR_FALSE);
02634       result = nsEventStatus_eConsumeNoDefault;
02635       break;
02636 
02637     case VIEWER_DEBUGSAVE:
02638       DoDebugSave();
02639       break;
02640 
02641     case VIEWER_TOGGLE_SELECTION:
02642       DoToggleSelection();
02643       break;
02644 
02645 
02646     case VIEWER_DEBUGROBOT:
02647       DoDebugRobot();
02648       break;
02649 
02650     case VIEWER_TOP100:
02651       DoSiteWalker();
02652       break;
02653   }
02654   return(result);
02655 }
02656 
02657 #endif // NS_DEBUG
02658 
02659 #define USE_DTD  0
02660 #define STANDARD 1 
02661 #define QUIRKS   2 
02662 void 
02663 nsBrowserWindow::SetCompatibilityMode(PRUint32 aMode)
02664 {
02665   nsCOMPtr<nsIPref> pref(do_GetService(NS_PREF_CONTRACTID));
02666   if (pref) { 
02667     int32 prefInt = USE_DTD;
02668     if (STANDARD == aMode) {
02669       prefInt = eCompatibility_FullStandards;
02670     }
02671     else if (QUIRKS == aMode) {
02672       prefInt = eCompatibility_NavQuirks;
02673     }
02674     pref->SetIntPref("nglayout.compatibility.mode", prefInt);
02675     pref->SavePrefFile(nsnull);
02676   }
02677 }
02678 
02679 nsEventStatus
02680 nsBrowserWindow::DispatchStyleMenu(PRInt32 aID)
02681 {
02682   nsEventStatus result = nsEventStatus_eIgnore;
02683 
02684   switch(aID) {
02685   case VIEWER_SELECT_STYLE_LIST:
02686     {
02687       nsIPresShell* shell = GetPresShell();
02688       if (nsnull != shell) {
02689         nsAutoString  defaultStyle;
02690         nsIDocument *doc = shell->GetDocument();
02691         if (doc) {
02692           nsIAtom* defStyleAtom = NS_NewAtom("default-style");
02693           doc->GetHeaderData(defStyleAtom, defaultStyle);
02694           NS_RELEASE(defStyleAtom);
02695         }
02696 
02697         nsStringArray titles;
02698         shell->ListAlternateStyleSheets(titles);
02699         nsAutoString  current;
02700         shell->GetActiveAlternateStyleSheet(current);
02701 
02702         PRInt32 count = titles.Count();
02703         fprintf(stdout, "There %s %d alternate style sheet%s\n",  
02704                 ((1 == count) ? "is" : "are"),
02705                 count,
02706                 ((1 == count) ? "" : "s"));
02707         PRInt32 index;
02708         for (index = 0; index < count; index++) {
02709           fprintf(stdout, "%d: \"", index + 1);
02710           nsAutoString title;
02711           titles.StringAt(index, title);
02712           fputs(NS_LossyConvertUCS2toASCII(title).get(), stdout);
02713           fprintf(stdout, "\" %s%s\n", 
02714                   ((title.Equals(current, nsCaseInsensitiveStringComparator())) ? "<< current " : ""), 
02715                   ((title.Equals(defaultStyle, nsCaseInsensitiveStringComparator())) ? "** default" : ""));
02716         }
02717         NS_RELEASE(shell);
02718       }
02719     }
02720     result = nsEventStatus_eConsumeNoDefault;
02721     break;
02722 
02723   case VIEWER_SELECT_STYLE_DEFAULT:
02724     {
02725       nsIPresShell* shell = GetPresShell();
02726       if (nsnull != shell) {
02727         nsIDocument *doc = shell->GetDocument();
02728         if (doc) {
02729           nsAutoString  defaultStyle;
02730           nsIAtom* defStyleAtom = NS_NewAtom("default-style");
02731           doc->GetHeaderData(defStyleAtom, defaultStyle);
02732           NS_RELEASE(defStyleAtom);
02733           fputs("Selecting default style sheet \"", stdout);
02734           fputs(NS_LossyConvertUCS2toASCII(defaultStyle).get(), stdout);
02735           fputs("\"\n", stdout);
02736           shell->SelectAlternateStyleSheet(defaultStyle);
02737         }
02738         NS_RELEASE(shell);
02739       }
02740     }
02741     result = nsEventStatus_eConsumeNoDefault;
02742     break;
02743 
02744   case VIEWER_SELECT_STYLE_ONE:
02745   case VIEWER_SELECT_STYLE_TWO:
02746   case VIEWER_SELECT_STYLE_THREE:
02747   case VIEWER_SELECT_STYLE_FOUR:
02748     {
02749       nsIPresShell* shell = GetPresShell();
02750       if (nsnull != shell) {
02751         nsStringArray titles;
02752         shell->ListAlternateStyleSheets(titles);
02753         nsAutoString  title;
02754         titles.StringAt(aID - VIEWER_SELECT_STYLE_ONE, title);
02755         fputs("Selecting alternate style sheet \"", stdout);
02756         fputs(NS_LossyConvertUCS2toASCII(title).get(), stdout);
02757         fputs("\"\n", stdout);
02758         shell->SelectAlternateStyleSheet(title);
02759         NS_RELEASE(shell);
02760       }
02761     }
02762     result = nsEventStatus_eConsumeNoDefault;
02763     break;
02764 
02765   case VIEWER_USE_DTD_MODE:
02766   case VIEWER_NAV_QUIRKS_MODE:
02767   case VIEWER_STANDARD_MODE:
02768     SetCompatibilityMode(aID - VIEWER_USE_DTD_MODE);
02769     result = nsEventStatus_eConsumeNoDefault;
02770     break;
02771   }
02772   return result;
02773 }
02774 
02775 NS_IMETHODIMP nsBrowserWindow::EnsureWebBrowserChrome()
02776 {
02777    if(mWebBrowserChrome)
02778       return NS_OK;
02779 
02780    mWebBrowserChrome = new nsWebBrowserChrome();
02781    NS_ENSURE_TRUE(mWebBrowserChrome, NS_ERROR_OUT_OF_MEMORY);
02782 
02783    NS_ADDREF(mWebBrowserChrome);
02784    mWebBrowserChrome->BrowserWindow(this);
02785 
02786    return NS_OK;
02787 }
02788 
02789 nsresult nsBrowserWindow::GetWindow(nsIWidget** aWindow)
02790 {
02791   if (aWindow && mWindow) {
02792     *aWindow = mWindow.get();
02793     NS_IF_ADDREF(*aWindow);
02794     return NS_OK;
02795   }
02796   return NS_ERROR_FAILURE;
02797 }
02798