Annotation of chimera/MainController.mm, revision 1.40

1.27      hyatt       1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
                      2: /* ***** BEGIN LICENSE BLOCK *****
                      3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
                      4:  *
                      5:  * The contents of this file are subject to the Netscape Public License
                      6:  * Version 1.1 (the "License"); you may not use this file except in
                      7:  * compliance with the License. You may obtain a copy of the License at
                      8:  * http://www.mozilla.org/NPL/
                      9:  *
                     10:  * Software distributed under the License is distributed on an "AS IS" basis,
                     11:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
                     12:  * for the specific language governing rights and limitations under the
                     13:  * License.
                     14:  *
                     15:  * The Original Code is mozilla.org code.
                     16:  *
                     17:  * The Initial Developer of the Original Code is 
                     18:  * Netscape Communications Corporation.
                     19:  * Portions created by the Initial Developer are Copyright (C) 2002
                     20:  * the Initial Developer. All Rights Reserved.
                     21:  *
                     22:  * Contributor(s):
                     23:  *
                     24:  * Alternatively, the contents of this file may be used under the terms of
                     25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
                     26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
                     27:  * in which case the provisions of the GPL or the LGPL are applicable instead
                     28:  * of those above. If you wish to allow use of your version of this file only
                     29:  * under the terms of either the GPL or the LGPL, and not to allow others to
                     30:  * use your version of this file under the terms of the NPL, indicate your
                     31:  * decision by deleting the provisions above and replace them with the notice
                     32:  * and other provisions required by the GPL or the LGPL. If you do not delete
                     33:  * the provisions above, a recipient may use your version of this file under
                     34:  * the terms of any one of the NPL, the GPL or the LGPL.
                     35:  *
                     36:  * ***** END LICENSE BLOCK ***** */
1.1       hyatt      37: 
                     38: #import "MainController.h"
                     39: #import "BrowserWindowController.h"
1.20      hyatt      40: #include "BookmarksService.h"
1.10      hyatt      41: #include "nsCOMPtr.h"
                     42: #include "nsIServiceManager.h"
                     43: #include "nsIIOService.h"
1.12      hyatt      44: #include "nsCocoaBrowserService.h"
1.30      macserv    45: #import  "AboutBox.h"
1.10      hyatt      46: 
1.36      pinkerto   47: 
1.10      hyatt      48: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
                     49: 
1.1       hyatt      50: @implementation MainController
                     51: 
1.30      macserv    52: -(id)init
                     53: {
                     54:     if ( (self = [super init]) ) {
1.35      macserv    55:         mSplashScreen = [[SplashScreenWindow alloc] splashImage:nil withFade:YES withStatusRect:NSMakeRect(0,0,0,0)];
1.30      macserv    56:     }
1.36      pinkerto   57:     mFindDialog = nil;
                     58:     mMenuBookmarks = nil;
1.30      macserv    59:     return self;
                     60: }
                     61: 
1.21      hyatt      62: -(void)dealloc
                     63: {
                     64:     [super dealloc];
1.36      pinkerto   65:     [mFindDialog release];
1.21      hyatt      66:     printf("Main controller died.\n");
                     67: }
                     68: 
1.1       hyatt      69: -(void)awakeFromNib
1.35      macserv    70: {    
1.4       hyatt      71:     [self newWindow: self];
1.35      macserv    72:     
                     73:     [mSplashScreen close];
1.31      macserv    74: 
1.19      hyatt      75:     [mBookmarksMenu setAutoenablesItems: NO];
                     76:     mMenuBookmarks = new BookmarksService(nil);
                     77:     mMenuBookmarks->AddObserver();
                     78:     mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
1.20      hyatt      79:     BookmarksService::gMainController = self;
1.19      hyatt      80:     
1.10      hyatt      81:     // Initialize offline mode.
                     82:     mOffline = NO;
                     83:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                     84:     if (!ioService)
                     85:         return;
                     86:     PRBool offline = PR_FALSE;
                     87:     ioService->GetOffline(&offline);
                     88:     mOffline = offline;
                     89:     
                     90:     // Set the menu item's text to "Go Online" if we're currently
                     91:     // offline.
                     92:     if (mOffline)
                     93:         [mOfflineMenuItem setTitle: @"Go Online"];
1.30      macserv    94: 
1.1       hyatt      95: }
                     96: 
                     97: -(IBAction)newWindow:(id)aSender
                     98: {
1.32      hyatt      99:   // If we have a key window, have it autosave its dimensions before
                    100:   // we open a new window.  That ensures the size ends up matching.
                    101:   NSWindow* keyWindow = [mApplication keyWindow];
1.38      pinkerto  102:   if ( keyWindow && [keyWindow respondsToSelector:@selector(autosaveWindowFrame)] )
1.32      hyatt     103:     [[keyWindow windowController] autosaveWindowFrame];
                    104: 
                    105:   // Now open the new window.
                    106:   BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
                    107:   [controller focusURLBar];
                    108: }  
1.4       hyatt     109: 
1.22      hyatt     110: -(IBAction)newTab:(id)aSender
                    111: {
                    112:     NSWindow* keyWindow = [mApplication keyWindow];
                    113:     if (keyWindow)
1.26      macserv   114:         [[keyWindow windowController] newTab];
                    115: }
                    116: 
                    117: -(IBAction)closeTab:(id)aSender
                    118: {
                    119:     NSWindow* keyWindow = [mApplication keyWindow];
                    120:     if (keyWindow)
                    121:         [[keyWindow windowController] closeTab];
                    122: }
                    123: 
                    124: -(IBAction) previousTab:(id)aSender
                    125: {
                    126:     NSWindow* keyWindow = [mApplication keyWindow];
                    127:     if (keyWindow)
                    128:         [[keyWindow windowController] previousTab];
                    129: }
                    130: 
                    131: -(IBAction) nextTab:(id)aSender;
                    132: {
                    133:     NSWindow* keyWindow = [mApplication keyWindow];
                    134:     if (keyWindow)
                    135:         [[keyWindow windowController] nextTab];
1.22      hyatt     136: }
                    137: 
1.4       hyatt     138: -(IBAction) openFile:(id)aSender
                    139: {
1.5       hyatt     140:     NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
                    141:     [openPanel setCanChooseFiles: YES];
                    142:     [openPanel setCanChooseDirectories: NO];
                    143:     [openPanel setAllowsMultipleSelection: NO];
                    144:     NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
                    145:                                                 @"txt",@"text",
                    146:                                                 @"gif",@"jpg",@"jpeg",@"png",@"bmp",
                    147:                                                 nil];
                    148:     int result = [openPanel runModalForTypes: array];
                    149:     if (result == NSOKButton) {
                    150:         NSArray* urlArray = [openPanel URLs];
                    151:         if ([urlArray count] == 0)
                    152:             return;
                    153:         NSURL* url = [urlArray objectAtIndex: 0];
1.33      macserv   154:         // ----------------------
                    155:         [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:url];
                    156:         // ----------------------
1.5       hyatt     157:         NSWindow* keyWindow = [mApplication keyWindow];
1.40    ! hyatt     158:         if (keyWindow) {
        !           159:           [[keyWindow windowController] loadURL: url];
        !           160:           [[[[keyWindow windowController] getMyBrowserView] getBrowserView] setActive: YES];
        !           161:         }
1.5       hyatt     162:         else
1.40    ! hyatt     163:           [self openBrowserWindowWithURL: url];
1.5       hyatt     164:     }
1.4       hyatt     165: }
                    166: 
                    167: -(IBAction) openLocation:(id)aSender
                    168: {
                    169:     NSWindow* keyWindow = [mApplication keyWindow];
                    170:     if (!keyWindow) {
1.5       hyatt     171:       [self openBrowserWindowWithURLString: @"about:blank"];
1.4       hyatt     172:       keyWindow = [mApplication keyWindow];
                    173:     }
                    174:     
1.6       macserv   175:     [[keyWindow windowController] performAppropriateLocationAction];
1.1       hyatt     176: }
                    177: 
1.7       hyatt     178: -(IBAction) savePage:(id)aSender
                    179: {
                    180:     NSWindow* keyWindow = [mApplication keyWindow];
                    181:     if (keyWindow)
1.8       hyatt     182:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
1.7       hyatt     183: }
                    184: 
1.9       hyatt     185: -(IBAction) printPage:(id)aSender
                    186: {
                    187:     NSWindow* keyWindow = [mApplication keyWindow];
                    188:     if (keyWindow)
                    189:       [[keyWindow windowController] printDocument];
                    190: }
                    191: 
1.16      hyatt     192: -(IBAction) printPreview:(id)aSender
                    193: {
                    194:     NSWindow* keyWindow = [mApplication keyWindow];
                    195:     if (keyWindow)
                    196:       [[keyWindow windowController] printPreview];
                    197: }
                    198: 
1.7       hyatt     199: -(IBAction) toggleOfflineMode:(id)aSender
                    200: {
1.10      hyatt     201:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    202:     if (!ioService)
                    203:         return;
                    204:     PRBool offline = PR_FALSE;
                    205:     ioService->GetOffline(&offline);
                    206:     ioService->SetOffline(!offline);
                    207:     mOffline = !offline;
                    208:     
                    209:     // Update the menu item text.
                    210:     // Set the menu item's text to "Go Online" if we're currently
                    211:     // offline.
                    212:     if (mOffline)
                    213:         [mOfflineMenuItem setTitle: @"Go Online"];
                    214:     else
                    215:         [mOfflineMenuItem setTitle: @"Work Offline"];
                    216:         
                    217:     // Indicate that we are working offline.
1.11      hyatt     218:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
1.7       hyatt     219: }
                    220: 
1.13      hyatt     221: // Edit menu actions.
1.36      pinkerto  222: 
                    223: 
                    224: //
                    225: // -findInPage
                    226: //
                    227: // Called in response to "Find" in edit menu. Opens the find dialog. We only keep
                    228: // one around for the whole app to use, showing/hiding as we see fit.
                    229: //
1.13      hyatt     230: -(IBAction) findInPage:(id)aSender
1.16      hyatt     231: {
1.36      pinkerto  232:   if ( !mFindDialog )
                    233:      mFindDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
                    234:   [mFindDialog showWindow:self];
1.16      hyatt     235: }
                    236: 
1.39      pinkerto  237: 
                    238: //
                    239: // -findAgain
                    240: //
                    241: // Called in response to "Find Again" in edit menu. Tells the find controller
                    242: // to find the next occurrance of what's already been found.
                    243: //
1.16      hyatt     244: -(IBAction) findAgain:(id)aSender
1.13      hyatt     245: {
1.39      pinkerto  246:   if ( mFindDialog )
                    247:     [mFindDialog findAgain:aSender];
                    248:   else
                    249:     NSBeep();
1.13      hyatt     250: }
                    251: 
1.23      hyatt     252: -(IBAction) goBack:(id)aSender
1.2       hyatt     253: {
                    254:     [[[mApplication keyWindow] windowController] back: aSender]; 
                    255: }
                    256: 
1.23      hyatt     257: -(IBAction) goForward:(id)aSender
1.2       hyatt     258: {
                    259:     [[[mApplication keyWindow] windowController] forward: aSender]; 
                    260: }
                    261: 
1.23      hyatt     262: -(IBAction) doReload:(id)aSender
1.2       hyatt     263: {
1.23      hyatt     264:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
1.2       hyatt     265: }
                    266: 
1.23      hyatt     267: -(IBAction) doStop:(id)aSender
1.2       hyatt     268: {
1.23      hyatt     269:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
1.2       hyatt     270: }
                    271: 
1.23      hyatt     272: -(IBAction) goHome:(id)aSender
1.2       hyatt     273: {
                    274:     [[[mApplication keyWindow] windowController] home: aSender];
                    275: }
                    276: 
1.5       hyatt     277: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
                    278: {
                    279:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
                    280: }
                    281: 
                    282: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
1.1       hyatt     283: {
1.3       macserv   284:  BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
1.34      hyatt     285:   [browser loadURL: aURL];
1.3       macserv   286:  [browser showWindow: self];
1.34      hyatt     287:   return browser;
1.1       hyatt     288: }
                    289: 
1.12      hyatt     290: -(void)applicationWillTerminate: (NSNotification*)aNotification
                    291: {
1.24      hyatt     292:     printf("Termination notification.\n");
1.37      hyatt     293: 
                    294:     // Autosave one of the windows.
                    295:     NSWindow* keyWindow = [mApplication keyWindow];
                    296:     if (keyWindow)
                    297:       [[keyWindow windowController] autosaveWindowFrame];
                    298:     
1.19      hyatt     299:     mMenuBookmarks->RemoveObserver();
                    300:     delete mMenuBookmarks;
1.24      hyatt     301:     mMenuBookmarks = nsnull;
1.12      hyatt     302:     nsCocoaBrowserService::TermEmbedding();
                    303: }
1.18      hyatt     304: 
                    305: // Bookmarks menu actions.
                    306: -(IBAction) addBookmark:(id)aSender
                    307: {
                    308:     NSWindow* keyWindow = [mApplication keyWindow];
                    309:     if (keyWindow)
                    310:       [[keyWindow windowController] addBookmark];
                    311: }
                    312: 
1.20      hyatt     313: -(IBAction) openMenuBookmark:(id)aSender
                    314: {
                    315:     NSWindow* keyWindow = [mApplication keyWindow];
                    316:     if (!keyWindow) {
                    317:         [self openBrowserWindowWithURLString: @"about:blank"];
                    318:         keyWindow = [mApplication keyWindow];
                    319:     }
                    320: 
                    321:     BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
                    322: }
1.25      macserv   323: 
                    324: - (MVPreferencesController *)preferencesController
                    325: {
                    326:     if (!preferencesController) {
                    327:         preferencesController = [[MVPreferencesController sharedInstance] retain];
                    328:     }
                    329:     return preferencesController;
                    330: }
                    331: 
                    332: - (void)displayPreferencesWindow:sender
                    333: {
                    334:     [[self preferencesController] showPreferences:nil] ;
                    335: }
                    336: 
1.30      macserv   337: - (IBAction)showAboutBox:(id)sender
                    338: {
                    339:     [[AboutBox sharedInstance] showPanel:sender];
                    340: }
1.33      macserv   341: 
                    342: - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
                    343: {
                    344:     NSWindow* keyWindow = [mApplication keyWindow];
                    345: 
                    346:     if (keyWindow) {
                    347:         [[keyWindow windowController] loadURL:[NSURL fileURLWithPath:filename]];
                    348:     } else {
                    349:         [self openBrowserWindowWithURL:[NSURL fileURLWithPath:filename]];
                    350:     }
                    351:     
                    352:     return YES;
                    353:     
                    354: }
                    355: 
1.25      macserv   356: 
1.1       hyatt     357: @end

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>