Back to index

lightning-sunbird  0.9+nobinonly
nsMacMain.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.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdlib.h>
00039 
00040 #include "nsViewerApp.h"
00041 #include "nsBrowserWindow.h"
00042 #include "nsIWidget.h"
00043 #include "nsIServiceManager.h"
00044 #include "resources.h"
00045 
00046 #include <ToolUtils.h>          // MacOS includes
00047 #include <Menus.h>
00048 #include <Windows.h>
00049 #include <Devices.h>
00050 #include <Resources.h>
00051 #include <Dialogs.h>
00052 
00053 #include "nsMacMessagePump.h"   // for the windowless menu event handler
00054 #ifdef GC_LEAK_DETECTOR
00055 #include "nsILeakDetector.h"
00056 #endif
00057 #ifndef XP_MACOSX
00058 #include "macstdlibextras.h"
00059 #endif
00060 
00061 typedef     SInt32          MessageT;
00062 typedef   PRUint32    Uint32;
00063 const MessageT  cmd_Undo            = 11;   // nil
00064 const MessageT  cmd_Cut             = 12;   // nil
00065 const MessageT  cmd_Copy            = 13;   // nil
00066 const MessageT  cmd_Paste           = 14;   // nil
00067 const MessageT  cmd_Clear           = 15;   // nil
00068 const MessageT  cmd_SelectAll       = 16;   // nil
00069 
00070 const MessageT  cmd_About           = 1;    // nil
00071 
00072                     // File Menu
00073 const MessageT  cmd_New             = 2;    // nil
00074 const MessageT  cmd_Open            = 3;    // nil
00075 const MessageT  cmd_Close           = 4;    // nil
00076 const MessageT  cmd_Save            = 5;    // nil
00077 const MessageT  cmd_SaveAs          = 6;    // nil
00078 const MessageT  cmd_Revert          = 7;    // nil
00079 const MessageT  cmd_PageSetup       = 8;    // nil
00080 const MessageT  cmd_Print           = 9;    // nil
00081 const MessageT  cmd_PrintOne        = 17;   // nil
00082 const MessageT  cmd_Quit            = 10;   // nil
00083 const MessageT  cmd_Preferences     = 27;   // nil
00084 
00085 
00086 enum
00087 {
00088     menu_First = 128,
00089     menu_Apple = menu_First,
00090     menu_File,
00091     menu_Edit,
00092     menu_Sample,
00093     menu_Debug,
00094     menu_Tools,
00095     menu_URLS,
00096     menu_Last = menu_URLS,
00097 
00098     submenu_Print = 16,
00099     submenu_CompatibilityMode = 32,
00100     
00101     cmd_Sample0                 = 1000,
00102     cmd_FirstXPToolkitSample    = 1100,
00103     cmd_PrintOneColumn  = 2000,
00104     cmd_Find                        = 3000,
00105 
00106     cmd_ViewSource          = 2200,
00107     cmd_PrintSetup,
00108 
00109     cmd_DebugMode               = 4000,
00110     cmd_ReflowTest,
00111     cmd_DumpContents,
00112     cmd_DumpFrames,
00113     cmd_DumpViews,
00114     cmd_DumpStyleSheets,
00115     cmd_DumpStyleContexts,
00116     cmd_DebugSave,
00117     cmd_DebugOutputText,
00118     cmd_DebugOutputHTML,    
00119     cmd_DebugToggleSelection,
00120     cmd_DebugRobot,
00121     cmd_GFXWidgetMode,
00122     cmd_NativeWidgetMode,
00123     cmd_GFXScrollBars,
00124     cmd_NativeScrollBars,
00125     cmd_DumpLeaks,
00126 
00127     item_GFXWidgetMode = 24,
00128     item_NativeWidgetMode,
00129 
00130     cmd_Compatibility_UseDTD    = 4200,
00131     cmd_Compatibility_NavQuirks,
00132     cmd_Compatibility_Standard,
00133     
00134     cmd_JSConsole               = 5000,
00135     cmd_EditorMode,
00136     cmd_Top100,
00137     cmd_TableInspector,
00138     cmd_ImageInspector,
00139     
00140     cmd_SaveURL1 = 6000,
00141     cmd_SaveURL2,
00142     cmd_LoadURL1,
00143     cmd_LoadURL2
00144 
00145 };
00146 
00147 static nsNativeViewerApp* gTheApp;
00148 
00149 
00150 //----------------------------------------------------------------------
00151 
00152 nsNativeViewerApp::nsNativeViewerApp()
00153 {
00154     //nsMacMessagePump::SetWindowlessMenuEventHandler(DispatchMenuItemWithoutWindow);
00155 }
00156 
00157 nsNativeViewerApp::~nsNativeViewerApp()
00158 {
00159 }
00160 
00161 int
00162 nsNativeViewerApp::Run()
00163 {
00164   OpenWindow();
00165   mAppShell->Run();
00166   return 0;
00167 }
00168 
00169 void nsNativeViewerApp::DispatchMenuItemWithoutWindow(PRInt32 menuResult)
00170 {
00171     long menuID = HiWord(menuResult);
00172     long menuItem = LoWord(menuResult);
00173     switch (menuID)
00174     {
00175         case menu_Apple:
00176             switch (menuItem)
00177             {
00178                 case cmd_About:
00179                     ::Alert(128, nil);
00180                     break;
00181                 default:
00182                     Str255 daName;
00183                     GetMenuItemText(GetMenuHandle(menu_Apple), menuItem, daName);
00184                     #if !TARGET_CARBON
00185                     OpenDeskAcc(daName);
00186                     #endif
00187                     break;
00188             }
00189             break;
00190 
00191         case menu_File:
00192             
00193             switch (menuItem)
00194             {
00195                 case cmd_New:
00196                     gTheApp->OpenWindow();
00197                     break;
00198                 case cmd_Open:
00199                     nsBrowserWindow * newWindow;
00200                     gTheApp->OpenWindow((PRUint32)0, newWindow);
00201                     newWindow->DoFileOpen();
00202                     break;
00203                 case cmd_Quit:
00204                     gTheApp->Exit();
00205                     break;
00206             }
00207             break;
00208         }
00209 }
00210 
00211 #pragma mark -
00212 //----------------------------------------------------------------------
00213 
00214 nsNativeBrowserWindow::nsNativeBrowserWindow()
00215 {
00216 }
00217 
00218 nsNativeBrowserWindow::~nsNativeBrowserWindow()
00219 {
00220 }
00221 
00222 
00223 nsresult
00224 nsNativeBrowserWindow::InitNativeWindow()
00225 {
00226     // this is where we get a chance to set up the window refCon
00227     NS_PRECONDITION(nsnull != mWindow, "Null window in InitNativeWindow");
00228     
00229     WindowPtr       wind = (WindowPtr)mWindow->GetNativeData(NS_NATIVE_DISPLAY);
00230     if (!wind) return NS_ERROR_NULL_POINTER;
00231 
00232     SetPortWindowPort(wind);
00233     ::SetWRefCon(wind, (long)this);
00234     return NS_OK;
00235 }
00236 
00237 static void CloseFrontWindow()
00238 {
00239     WindowPtr       wind = ::FrontWindow();
00240     if (!wind) return;
00241     
00242     nsBrowserWindow     *browserWindow = (nsBrowserWindow *)GetWRefCon(wind);
00243     if (!browserWindow) return;
00244     
00245     browserWindow->Destroy();
00246 }
00247 
00248 
00249 nsresult
00250 nsNativeBrowserWindow::CreateMenuBar(PRInt32 aWidth)
00251 {
00252     for (int i = menu_First; i <= menu_Last; i++)
00253     {
00254     MenuHandle menu = GetMenu(i);
00255     NS_ASSERTION(menu, "menu failed to load");
00256     if (menu) InsertMenu(menu, 0);
00257     }
00258     InsertMenu(GetMenu(submenu_Print), -1);
00259     InsertMenu(GetMenu(submenu_CompatibilityMode), -1);
00260     AppendResMenu(GetMenuHandle(menu_Apple), 'DRVR');
00261     DrawMenuBar();
00262     return NS_OK;
00263 }
00264 
00265 nsresult
00266 nsNativeBrowserWindow::GetMenuBarHeight(PRInt32 * aHeightOut)
00267 {
00268   NS_ASSERTION(nsnull != aHeightOut,"null out param.");
00269 
00270   *aHeightOut = 0;
00271 
00272   return NS_OK;
00273 }
00274 
00275 nsEventStatus
00276 nsNativeBrowserWindow::DispatchMenuItem(PRInt32 aID)
00277 {
00278     nsEventStatus status = nsEventStatus_eIgnore;
00279     PRInt32 xpID = 0;
00280     long menuID = HiWord(aID);
00281     long menuItem = LoWord(aID);
00282     
00283     switch (menuID)
00284     {
00285         case menu_Apple:
00286             switch (menuItem)
00287             {
00288                 case cmd_About:
00289                     ::Alert(128, nil);
00290                     break;
00291                 default:
00292                     Str255 daName;
00293                     GetMenuItemText(GetMenuHandle(menu_Apple), menuItem, daName);
00294                     #if !TARGET_CARBON
00295                     OpenDeskAcc(daName);
00296                     #endif
00297                     break;
00298             }
00299             break;
00300 
00301         case menu_File:
00302             switch (menuItem)
00303             {
00304                 case cmd_New:           xpID = VIEWER_WINDOW_OPEN;      break;
00305                 case cmd_Open:      xpID = VIEWER_FILE_OPEN;            break;
00306                 case cmd_Close:
00307                     CloseFrontWindow();
00308                     status = nsEventStatus_eConsumeNoDefault;
00309                     break;
00310 
00311                 case cmd_ViewSource:            xpID = VIEW_SOURCE;             break;
00312                 case cmd_Save:          /*n.a.*/                        break;
00313                 case cmd_SaveAs:        /*n.a.*/                        break;
00314                 case cmd_Revert:        /*n.a.*/                        break;
00315                 case cmd_PageSetup: /*n.a.*/                        break;
00316                 case cmd_Print:                 xpID = VIEWER_PRINT;                break;
00317                 case cmd_PrintSetup:        xpID = VIEWER_PRINT_SETUP;  break;
00318                 case cmd_Quit:                  xpID = VIEWER_EXIT;                 break;
00319             }
00320             break;
00321 
00322         case menu_Edit:
00323             switch (menuItem)
00324             {
00325                 case cmd_Undo:      /*n.a.*/                                                        break;
00326                 case cmd_Cut:                   xpID = VIEWER_EDIT_CUT;                 break;
00327                 case cmd_Copy:              xpID = VIEWER_EDIT_COPY;                break;
00328                 case cmd_Paste:             xpID = VIEWER_EDIT_PASTE;               break;
00329                 case cmd_Clear:     /*n.a.*/                                                        break;
00330                 case cmd_SelectAll:     xpID = VIEWER_EDIT_SELECTALL;       break;
00331                 case cmd_Find:              xpID = VIEWER_EDIT_FINDINPAGE;  break;
00332                 case cmd_Preferences:   xpID = VIEWER_PREFS;                        break;
00333             }
00334             break;
00335 
00336         case menu_Sample:
00337             if ( menuItem < cmd_FirstXPToolkitSample )
00338                 xpID = VIEWER_DEMO0 + menuItem - cmd_Sample0;
00339             else
00340                 xpID = VIEWER_XPTOOLKITDEMOBASE + (menuItem - cmd_FirstXPToolkitSample);
00341             break;
00342 
00343         case menu_Debug:
00344             switch (menuItem)
00345             {
00346                 case cmd_DebugMode:                 xpID = VIEWER_VISUAL_DEBUGGING;         break;
00347                 case cmd_ReflowTest:                xpID = VIEWER_REFLOW_TEST;                  break;
00348 
00349                 case cmd_DumpContents:          xpID = VIEWER_DUMP_CONTENT;                 break;
00350                 case cmd_DumpFrames:                xpID = VIEWER_DUMP_FRAMES;                  break;
00351                 case cmd_DumpViews:                 xpID = VIEWER_DUMP_VIEWS;                       break;
00352 
00353                 case cmd_DumpStyleSheets:       xpID = VIEWER_DUMP_STYLE_SHEETS;        break;
00354                 case cmd_DumpStyleContexts: xpID = VIEWER_DUMP_STYLE_CONTEXTS;  break;
00355 
00356                 case cmd_DebugSave:                         xpID = VIEWER_DEBUGSAVE;                    break;
00357                 case cmd_DebugOutputText:               xpID = VIEWER_DISPLAYTEXT;              break;
00358                 case cmd_DebugOutputHTML:               xpID = VIEWER_DISPLAYHTML;              break;
00359                 case cmd_DebugToggleSelection:  xpID = VIEWER_TOGGLE_SELECTION;         break;
00360                 case cmd_DebugRobot:                        xpID = VIEWER_DEBUGROBOT;                       break;
00361 #ifdef GC_LEAK_DETECTOR
00362                 case cmd_DumpLeaks:
00363                     {
00364                         nsresult rv;
00365                         nsCOMPtr<nsILeakDetector> leakDetector = 
00366                                  do_GetService("@mozilla.org/xpcom/leakdetector;1", &rv);
00367                         if (NS_SUCCEEDED(rv))
00368                             leakDetector->DumpLeaks();
00369                     }
00370                     break;
00371 #endif
00372                 case cmd_GFXScrollBars:     xpID =VIEWER_GFX_SCROLLBARS_ON; break;
00373                 case cmd_NativeScrollBars: xpID =VIEWER_GFX_SCROLLBARS_OFF; break;
00374             }
00375             break;
00376             
00377         case menu_Tools:
00378             switch (menuItem)
00379             {
00380                 case cmd_JSConsole:                 xpID = JS_CONSOLE;                              break;
00381                 case cmd_EditorMode:                xpID = EDITOR_MODE;                             break;
00382                 case cmd_Top100:                        xpID = VIEWER_TOP100;                           break;
00383                 case cmd_TableInspector:        xpID = VIEWER_TABLE_INSPECTOR;      break;
00384                 case cmd_ImageInspector:        xpID = VIEWER_IMAGE_INSPECTOR;      break;
00385             }
00386             break;
00387             
00388     case menu_URLS:
00389             switch (menuItem)
00390             {
00391                 case cmd_SaveURL1:                  xpID = VIEWER_SAVE_TEST_URL1;                               break;
00392                 case cmd_SaveURL2:                  xpID = VIEWER_SAVE_TEST_URL2;                               break;
00393                 case cmd_LoadURL1:                  xpID = VIEWER_GOTO_TEST_URL1;                               break;
00394                 case cmd_LoadURL2:                  xpID = VIEWER_GOTO_TEST_URL2;                               break;  
00395             }   
00396       break;    
00397             
00398         case submenu_Print:
00399             xpID = VIEWER_ONE_COLUMN + menuItem - cmd_PrintOneColumn;
00400             break;
00401             
00402         case submenu_CompatibilityMode:
00403             switch (menuItem)
00404             {
00405                 case cmd_Compatibility_UseDTD:        xpID = VIEWER_USE_DTD_MODE;   break;
00406                 case cmd_Compatibility_NavQuirks:       xpID = VIEWER_NAV_QUIRKS_MODE;  break;
00407                 case cmd_Compatibility_Standard:        xpID = VIEWER_STANDARD_MODE;        break;
00408             }
00409             break;
00410     }
00411 
00412     // Dispatch xp menu items
00413     if (xpID != 0) {
00414         // beard: nsBrowserWindow::DispatchMenuItem almost always returns nsEventStatus_eIgnore.
00415         // this causes double menu item dispatching for most items except for VIEWER_EXIT!
00416         nsBrowserWindow::DispatchMenuItem(xpID);
00417         status = nsEventStatus_eConsumeNoDefault;
00418     }
00419     return status;
00420 }
00421 
00425 static pascal OSErr handleQuitApplication(const AppleEvent*, AppleEvent*, long)
00426 {
00427     if (gTheApp != nsnull) {
00428         gTheApp->Exit();
00429     } else {
00430         ExitToShell();
00431     }
00432     return noErr;
00433 }
00434 
00435 #pragma mark -
00436 //----------------------------------------------------------------------
00437 int main(int argc, char **argv)
00438 {
00439 #if !TARGET_CARBON
00440     // Set up the toolbox and (if DEBUG) the console
00441     InitializeMacToolbox();
00442 #endif
00443 
00444     // Install a Quit AppleEvent handler.
00445 #ifdef DEBUG
00446   OSErr err =
00447 #endif
00448     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
00449                           NewAEEventHandlerUPP(handleQuitApplication), 0,
00450                           false);
00451     NS_ASSERTION((err==noErr), "AEInstallEventHandler failed");
00452 
00453 #ifdef XP_MACOSX
00454     // use the location of the executable to learn where everything is, this
00455     // is because the current working directory is ill-defined when the
00456     // application is double-clicked from the Finder.
00457     {
00458         char* path = strdup(argv[0]);
00459         char* lastSlash = strrchr(path, '/');
00460         if (lastSlash) {
00461             *lastSlash = '\0';
00462             setenv("MOZILLA_FIVE_HOME", path, 1);
00463         }
00464         free(path);
00465     }
00466 #endif
00467 
00468     // Start up XPCOM?
00469     nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00470     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_InitXPCOM failed");
00471 
00472     gTheApp = new nsNativeViewerApp();
00473     if (gTheApp != nsnull) {
00474         NS_ADDREF(gTheApp);
00475         if (gTheApp->Initialize(argc, argv) == NS_OK)
00476             gTheApp->Run();
00477         NS_RELEASE(gTheApp);
00478     }
00479 
00480     // Shutdown XPCOM?
00481     rv = NS_ShutdownXPCOM(nsnull);
00482     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00483 
00484     return 0;
00485 }