File: nsMacMain.cpp

package info (click to toggle)
iceweasel 2.0.0.19-0etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 298,784 kB
  • ctags: 317,912
  • sloc: cpp: 1,796,902; ansic: 987,677; xml: 109,036; makefile: 47,777; asm: 35,201; perl: 26,983; sh: 20,879; cs: 6,232; java: 5,513; python: 3,249; pascal: 459; lex: 306; php: 244; csh: 132; objc: 97; yacc: 79; ada: 49; awk: 14; sql: 4; sed: 4
file content (485 lines) | stat: -rw-r--r-- 16,279 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include <stdlib.h>

#include "nsViewerApp.h"
#include "nsBrowserWindow.h"
#include "nsIWidget.h"
#include "nsIServiceManager.h"
#include "resources.h"

#include <ToolUtils.h>          // MacOS includes
#include <Menus.h>
#include <Windows.h>
#include <Devices.h>
#include <Resources.h>
#include <Dialogs.h>

#include "nsMacMessagePump.h"   // for the windowless menu event handler
#ifdef GC_LEAK_DETECTOR
#include "nsILeakDetector.h"
#endif
#ifndef XP_MACOSX
#include "macstdlibextras.h"
#endif

typedef     SInt32          MessageT;
typedef   PRUint32    Uint32;
const MessageT  cmd_Undo            = 11;   // nil
const MessageT  cmd_Cut             = 12;   // nil
const MessageT  cmd_Copy            = 13;   // nil
const MessageT  cmd_Paste           = 14;   // nil
const MessageT  cmd_Clear           = 15;   // nil
const MessageT  cmd_SelectAll       = 16;   // nil

const MessageT  cmd_About           = 1;    // nil

                    // File Menu
const MessageT  cmd_New             = 2;    // nil
const MessageT  cmd_Open            = 3;    // nil
const MessageT  cmd_Close           = 4;    // nil
const MessageT  cmd_Save            = 5;    // nil
const MessageT  cmd_SaveAs          = 6;    // nil
const MessageT  cmd_Revert          = 7;    // nil
const MessageT  cmd_PageSetup       = 8;    // nil
const MessageT  cmd_Print           = 9;    // nil
const MessageT  cmd_PrintOne        = 17;   // nil
const MessageT  cmd_Quit            = 10;   // nil
const MessageT  cmd_Preferences     = 27;   // nil


enum
{
    menu_First = 128,
    menu_Apple = menu_First,
    menu_File,
    menu_Edit,
    menu_Sample,
    menu_Debug,
    menu_Tools,
    menu_URLS,
    menu_Last = menu_URLS,

    submenu_Print = 16,
    submenu_CompatibilityMode = 32,
    
    cmd_Sample0                 = 1000,
    cmd_FirstXPToolkitSample    = 1100,
    cmd_PrintOneColumn  = 2000,
    cmd_Find                        = 3000,

    cmd_ViewSource          = 2200,
    cmd_PrintSetup,

    cmd_DebugMode               = 4000,
    cmd_ReflowTest,
    cmd_DumpContents,
    cmd_DumpFrames,
    cmd_DumpViews,
    cmd_DumpStyleSheets,
    cmd_DumpStyleContexts,
    cmd_DebugSave,
    cmd_DebugOutputText,
    cmd_DebugOutputHTML,    
    cmd_DebugToggleSelection,
    cmd_DebugRobot,
    cmd_GFXWidgetMode,
    cmd_NativeWidgetMode,
    cmd_GFXScrollBars,
    cmd_NativeScrollBars,
    cmd_DumpLeaks,

    item_GFXWidgetMode = 24,
    item_NativeWidgetMode,

    cmd_Compatibility_UseDTD    = 4200,
    cmd_Compatibility_NavQuirks,
    cmd_Compatibility_Standard,
    
    cmd_JSConsole               = 5000,
    cmd_EditorMode,
    cmd_Top100,
    cmd_TableInspector,
    cmd_ImageInspector,
    
    cmd_SaveURL1 = 6000,
    cmd_SaveURL2,
    cmd_LoadURL1,
    cmd_LoadURL2

};

static nsNativeViewerApp* gTheApp;


//----------------------------------------------------------------------

nsNativeViewerApp::nsNativeViewerApp()
{
    //nsMacMessagePump::SetWindowlessMenuEventHandler(DispatchMenuItemWithoutWindow);
}

nsNativeViewerApp::~nsNativeViewerApp()
{
}

int
nsNativeViewerApp::Run()
{
  OpenWindow();
  mAppShell->Run();
  return 0;
}

void nsNativeViewerApp::DispatchMenuItemWithoutWindow(PRInt32 menuResult)
{
    long menuID = HiWord(menuResult);
    long menuItem = LoWord(menuResult);
    switch (menuID)
    {
        case menu_Apple:
            switch (menuItem)
            {
                case cmd_About:
                    ::Alert(128, nil);
                    break;
                default:
                    Str255 daName;
                    GetMenuItemText(GetMenuHandle(menu_Apple), menuItem, daName);
                    #if !TARGET_CARBON
                    OpenDeskAcc(daName);
                    #endif
                    break;
            }
            break;

        case menu_File:
            
            switch (menuItem)
            {
                case cmd_New:
                    gTheApp->OpenWindow();
                    break;
                case cmd_Open:
                    nsBrowserWindow * newWindow;
                    gTheApp->OpenWindow((PRUint32)0, newWindow);
                    newWindow->DoFileOpen();
                    break;
                case cmd_Quit:
                    gTheApp->Exit();
                    break;
            }
            break;
        }
}

#pragma mark -
//----------------------------------------------------------------------

nsNativeBrowserWindow::nsNativeBrowserWindow()
{
}

nsNativeBrowserWindow::~nsNativeBrowserWindow()
{
}


nsresult
nsNativeBrowserWindow::InitNativeWindow()
{
    // this is where we get a chance to set up the window refCon
    NS_PRECONDITION(nsnull != mWindow, "Null window in InitNativeWindow");
    
    WindowPtr       wind = (WindowPtr)mWindow->GetNativeData(NS_NATIVE_DISPLAY);
    if (!wind) return NS_ERROR_NULL_POINTER;

    SetPortWindowPort(wind);
    ::SetWRefCon(wind, (long)this);
    return NS_OK;
}

static void CloseFrontWindow()
{
    WindowPtr       wind = ::FrontWindow();
    if (!wind) return;
    
    nsBrowserWindow     *browserWindow = (nsBrowserWindow *)GetWRefCon(wind);
    if (!browserWindow) return;
    
    browserWindow->Destroy();
}


nsresult
nsNativeBrowserWindow::CreateMenuBar(PRInt32 aWidth)
{
    for (int i = menu_First; i <= menu_Last; i++)
    {
    MenuHandle menu = GetMenu(i);
    NS_ASSERTION(menu, "menu failed to load");
    if (menu) InsertMenu(menu, 0);
    }
    InsertMenu(GetMenu(submenu_Print), -1);
    InsertMenu(GetMenu(submenu_CompatibilityMode), -1);
    AppendResMenu(GetMenuHandle(menu_Apple), 'DRVR');
    DrawMenuBar();
    return NS_OK;
}

nsresult
nsNativeBrowserWindow::GetMenuBarHeight(PRInt32 * aHeightOut)
{
  NS_ASSERTION(nsnull != aHeightOut,"null out param.");

  *aHeightOut = 0;

  return NS_OK;
}

nsEventStatus
nsNativeBrowserWindow::DispatchMenuItem(PRInt32 aID)
{
    nsEventStatus status = nsEventStatus_eIgnore;
    PRInt32 xpID = 0;
    long menuID = HiWord(aID);
    long menuItem = LoWord(aID);
    
    switch (menuID)
    {
        case menu_Apple:
            switch (menuItem)
            {
                case cmd_About:
                    ::Alert(128, nil);
                    break;
                default:
                    Str255 daName;
                    GetMenuItemText(GetMenuHandle(menu_Apple), menuItem, daName);
                    #if !TARGET_CARBON
                    OpenDeskAcc(daName);
                    #endif
                    break;
            }
            break;

        case menu_File:
            switch (menuItem)
            {
                case cmd_New:           xpID = VIEWER_WINDOW_OPEN;      break;
                case cmd_Open:      xpID = VIEWER_FILE_OPEN;            break;
                case cmd_Close:
                    CloseFrontWindow();
                    status = nsEventStatus_eConsumeNoDefault;
                    break;

                case cmd_ViewSource:            xpID = VIEW_SOURCE;             break;
                case cmd_Save:          /*n.a.*/                        break;
                case cmd_SaveAs:        /*n.a.*/                        break;
                case cmd_Revert:        /*n.a.*/                        break;
                case cmd_PageSetup: /*n.a.*/                        break;
                case cmd_Print:                 xpID = VIEWER_PRINT;                break;
                case cmd_PrintSetup:        xpID = VIEWER_PRINT_SETUP;  break;
                case cmd_Quit:                  xpID = VIEWER_EXIT;                 break;
            }
            break;

        case menu_Edit:
            switch (menuItem)
            {
                case cmd_Undo:      /*n.a.*/                                                        break;
                case cmd_Cut:                   xpID = VIEWER_EDIT_CUT;                 break;
                case cmd_Copy:              xpID = VIEWER_EDIT_COPY;                break;
                case cmd_Paste:             xpID = VIEWER_EDIT_PASTE;               break;
                case cmd_Clear:     /*n.a.*/                                                        break;
                case cmd_SelectAll:     xpID = VIEWER_EDIT_SELECTALL;       break;
                case cmd_Find:              xpID = VIEWER_EDIT_FINDINPAGE;  break;
                case cmd_Preferences:   xpID = VIEWER_PREFS;                        break;
            }
            break;

        case menu_Sample:
            if ( menuItem < cmd_FirstXPToolkitSample )
                xpID = VIEWER_DEMO0 + menuItem - cmd_Sample0;
            else
                xpID = VIEWER_XPTOOLKITDEMOBASE + (menuItem - cmd_FirstXPToolkitSample);
            break;

        case menu_Debug:
            switch (menuItem)
            {
                case cmd_DebugMode:                 xpID = VIEWER_VISUAL_DEBUGGING;         break;
                case cmd_ReflowTest:                xpID = VIEWER_REFLOW_TEST;                  break;

                case cmd_DumpContents:          xpID = VIEWER_DUMP_CONTENT;                 break;
                case cmd_DumpFrames:                xpID = VIEWER_DUMP_FRAMES;                  break;
                case cmd_DumpViews:                 xpID = VIEWER_DUMP_VIEWS;                       break;

                case cmd_DumpStyleSheets:       xpID = VIEWER_DUMP_STYLE_SHEETS;        break;
                case cmd_DumpStyleContexts: xpID = VIEWER_DUMP_STYLE_CONTEXTS;  break;

                case cmd_DebugSave:                         xpID = VIEWER_DEBUGSAVE;                    break;
                case cmd_DebugOutputText:               xpID = VIEWER_DISPLAYTEXT;              break;
                case cmd_DebugOutputHTML:               xpID = VIEWER_DISPLAYHTML;              break;
                case cmd_DebugToggleSelection:  xpID = VIEWER_TOGGLE_SELECTION;         break;
                case cmd_DebugRobot:                        xpID = VIEWER_DEBUGROBOT;                       break;
#ifdef GC_LEAK_DETECTOR
                case cmd_DumpLeaks:
                    {
                        nsresult rv;
                        nsCOMPtr<nsILeakDetector> leakDetector = 
                                 do_GetService("@mozilla.org/xpcom/leakdetector;1", &rv);
                        if (NS_SUCCEEDED(rv))
                            leakDetector->DumpLeaks();
                    }
                    break;
#endif
                case cmd_GFXScrollBars:     xpID =VIEWER_GFX_SCROLLBARS_ON; break;
                case cmd_NativeScrollBars: xpID =VIEWER_GFX_SCROLLBARS_OFF; break;
            }
            break;
            
        case menu_Tools:
            switch (menuItem)
            {
                case cmd_JSConsole:                 xpID = JS_CONSOLE;                              break;
                case cmd_EditorMode:                xpID = EDITOR_MODE;                             break;
                case cmd_Top100:                        xpID = VIEWER_TOP100;                           break;
                case cmd_TableInspector:        xpID = VIEWER_TABLE_INSPECTOR;      break;
                case cmd_ImageInspector:        xpID = VIEWER_IMAGE_INSPECTOR;      break;
            }
            break;
            
    case menu_URLS:
            switch (menuItem)
            {
                case cmd_SaveURL1:                  xpID = VIEWER_SAVE_TEST_URL1;                               break;
                case cmd_SaveURL2:                  xpID = VIEWER_SAVE_TEST_URL2;                               break;
                case cmd_LoadURL1:                  xpID = VIEWER_GOTO_TEST_URL1;                               break;
                case cmd_LoadURL2:                  xpID = VIEWER_GOTO_TEST_URL2;                               break;  
            }   
      break;    
            
        case submenu_Print:
            xpID = VIEWER_ONE_COLUMN + menuItem - cmd_PrintOneColumn;
            break;
            
        case submenu_CompatibilityMode:
            switch (menuItem)
            {
                case cmd_Compatibility_UseDTD:        xpID = VIEWER_USE_DTD_MODE;   break;
                case cmd_Compatibility_NavQuirks:       xpID = VIEWER_NAV_QUIRKS_MODE;  break;
                case cmd_Compatibility_Standard:        xpID = VIEWER_STANDARD_MODE;        break;
            }
            break;
    }

    // Dispatch xp menu items
    if (xpID != 0) {
        // beard: nsBrowserWindow::DispatchMenuItem almost always returns nsEventStatus_eIgnore.
        // this causes double menu item dispatching for most items except for VIEWER_EXIT!
        nsBrowserWindow::DispatchMenuItem(xpID);
        status = nsEventStatus_eConsumeNoDefault;
    }
    return status;
}

/**
 * Quit AppleEvent handler.
 */
static pascal OSErr handleQuitApplication(const AppleEvent*, AppleEvent*, long)
{
    if (gTheApp != nsnull) {
        gTheApp->Exit();
    } else {
        ExitToShell();
    }
    return noErr;
}

#pragma mark -
//----------------------------------------------------------------------
int main(int argc, char **argv)
{
#if !TARGET_CARBON
    // Set up the toolbox and (if DEBUG) the console
    InitializeMacToolbox();
#endif

    // Install a Quit AppleEvent handler.
#ifdef DEBUG
  OSErr err =
#endif
    AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
                          NewAEEventHandlerUPP(handleQuitApplication), 0,
                          false);
    NS_ASSERTION((err==noErr), "AEInstallEventHandler failed");

#ifdef XP_MACOSX
    // use the location of the executable to learn where everything is, this
    // is because the current working directory is ill-defined when the
    // application is double-clicked from the Finder.
    {
        char* path = strdup(argv[0]);
        char* lastSlash = strrchr(path, '/');
        if (lastSlash) {
            *lastSlash = '\0';
            setenv("MOZILLA_FIVE_HOME", path, 1);
        }
        free(path);
    }
#endif

    // Start up XPCOM?
    nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_InitXPCOM failed");

    gTheApp = new nsNativeViewerApp();
    if (gTheApp != nsnull) {
        NS_ADDREF(gTheApp);
        if (gTheApp->Initialize(argc, argv) == NS_OK)
            gTheApp->Run();
        NS_RELEASE(gTheApp);
    }

    // Shutdown XPCOM?
    rv = NS_ShutdownXPCOM(nsnull);
    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");

    return 0;
}