Annotation of chimera/MainController.mm, revision 1.36

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];
                    102:   if (keyWindow)
                    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];
                    158:         if (keyWindow)
                    159:             return [[keyWindow windowController] loadURL: url];
                    160:         else
                    161:             [self openBrowserWindowWithURL: url];
                    162:     }
1.4       hyatt     163: }
                    164: 
                    165: -(IBAction) openLocation:(id)aSender
                    166: {
                    167:     NSWindow* keyWindow = [mApplication keyWindow];
                    168:     if (!keyWindow) {
1.5       hyatt     169:       [self openBrowserWindowWithURLString: @"about:blank"];
1.4       hyatt     170:       keyWindow = [mApplication keyWindow];
                    171:     }
                    172:     
1.6       macserv   173:     [[keyWindow windowController] performAppropriateLocationAction];
1.1       hyatt     174: }
                    175: 
1.7       hyatt     176: -(IBAction) savePage:(id)aSender
                    177: {
                    178:     NSWindow* keyWindow = [mApplication keyWindow];
                    179:     if (keyWindow)
1.8       hyatt     180:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
1.7       hyatt     181: }
                    182: 
1.9       hyatt     183: -(IBAction) printPage:(id)aSender
                    184: {
                    185:     NSWindow* keyWindow = [mApplication keyWindow];
                    186:     if (keyWindow)
                    187:       [[keyWindow windowController] printDocument];
                    188: }
                    189: 
1.16      hyatt     190: -(IBAction) printPreview:(id)aSender
                    191: {
                    192:     NSWindow* keyWindow = [mApplication keyWindow];
                    193:     if (keyWindow)
                    194:       [[keyWindow windowController] printPreview];
                    195: }
                    196: 
1.7       hyatt     197: -(IBAction) toggleOfflineMode:(id)aSender
                    198: {
1.10      hyatt     199:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    200:     if (!ioService)
                    201:         return;
                    202:     PRBool offline = PR_FALSE;
                    203:     ioService->GetOffline(&offline);
                    204:     ioService->SetOffline(!offline);
                    205:     mOffline = !offline;
                    206:     
                    207:     // Update the menu item text.
                    208:     // Set the menu item's text to "Go Online" if we're currently
                    209:     // offline.
                    210:     if (mOffline)
                    211:         [mOfflineMenuItem setTitle: @"Go Online"];
                    212:     else
                    213:         [mOfflineMenuItem setTitle: @"Work Offline"];
                    214:         
                    215:     // Indicate that we are working offline.
1.11      hyatt     216:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
1.7       hyatt     217: }
                    218: 
1.13      hyatt     219: // Edit menu actions.
1.36    ! pinkerto  220: 
        !           221: 
        !           222: //
        !           223: // -findInPage
        !           224: //
        !           225: // Called in response to "Find" in edit menu. Opens the find dialog. We only keep
        !           226: // one around for the whole app to use, showing/hiding as we see fit.
        !           227: //
1.13      hyatt     228: -(IBAction) findInPage:(id)aSender
1.16      hyatt     229: {
1.36    ! pinkerto  230:   if ( !mFindDialog )
        !           231:      mFindDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
        !           232:   [mFindDialog showWindow:self];
1.16      hyatt     233: }
                    234: 
                    235: -(IBAction) findAgain:(id)aSender
1.13      hyatt     236: {
1.17      hyatt     237:     NSWindow* keyWindow = [mApplication keyWindow];
                    238:     if (keyWindow)
                    239:       [[keyWindow windowController] findAgain];
1.13      hyatt     240: }
                    241: 
1.23      hyatt     242: -(IBAction) goBack:(id)aSender
1.2       hyatt     243: {
                    244:     [[[mApplication keyWindow] windowController] back: aSender]; 
                    245: }
                    246: 
1.23      hyatt     247: -(IBAction) goForward:(id)aSender
1.2       hyatt     248: {
                    249:     [[[mApplication keyWindow] windowController] forward: aSender]; 
                    250: }
                    251: 
1.23      hyatt     252: -(IBAction) doReload:(id)aSender
1.2       hyatt     253: {
1.23      hyatt     254:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
1.2       hyatt     255: }
                    256: 
1.23      hyatt     257: -(IBAction) doStop:(id)aSender
1.2       hyatt     258: {
1.23      hyatt     259:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
1.2       hyatt     260: }
                    261: 
1.23      hyatt     262: -(IBAction) goHome:(id)aSender
1.2       hyatt     263: {
                    264:     [[[mApplication keyWindow] windowController] home: aSender];
                    265: }
                    266: 
1.5       hyatt     267: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
                    268: {
                    269:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
                    270: }
                    271: 
                    272: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
1.1       hyatt     273: {
1.3       macserv   274:  BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
1.34      hyatt     275:   [browser loadURL: aURL];
1.3       macserv   276:  [browser showWindow: self];
1.34      hyatt     277:   return browser;
1.1       hyatt     278: }
                    279: 
1.12      hyatt     280: -(void)applicationWillTerminate: (NSNotification*)aNotification
                    281: {
1.24      hyatt     282:     printf("Termination notification.\n");
1.19      hyatt     283:     mMenuBookmarks->RemoveObserver();
                    284:     delete mMenuBookmarks;
1.24      hyatt     285:     mMenuBookmarks = nsnull;
1.12      hyatt     286:     nsCocoaBrowserService::TermEmbedding();
                    287: }
1.18      hyatt     288: 
                    289: // Bookmarks menu actions.
                    290: -(IBAction) addBookmark:(id)aSender
                    291: {
                    292:     NSWindow* keyWindow = [mApplication keyWindow];
                    293:     if (keyWindow)
                    294:       [[keyWindow windowController] addBookmark];
                    295: }
                    296: 
1.20      hyatt     297: -(IBAction) openMenuBookmark:(id)aSender
                    298: {
                    299:     NSWindow* keyWindow = [mApplication keyWindow];
                    300:     if (!keyWindow) {
                    301:         [self openBrowserWindowWithURLString: @"about:blank"];
                    302:         keyWindow = [mApplication keyWindow];
                    303:     }
                    304: 
                    305:     BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
                    306: }
1.25      macserv   307: 
                    308: - (MVPreferencesController *)preferencesController
                    309: {
                    310:     if (!preferencesController) {
                    311:         preferencesController = [[MVPreferencesController sharedInstance] retain];
                    312:     }
                    313:     return preferencesController;
                    314: }
                    315: 
                    316: - (void)displayPreferencesWindow:sender
                    317: {
                    318:     [[self preferencesController] showPreferences:nil] ;
                    319: }
                    320: 
1.30      macserv   321: - (IBAction)showAboutBox:(id)sender
                    322: {
                    323:     [[AboutBox sharedInstance] showPanel:sender];
                    324: }
1.33      macserv   325: 
                    326: - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
                    327: {
                    328:     NSWindow* keyWindow = [mApplication keyWindow];
                    329: 
                    330:     if (keyWindow) {
                    331:         [[keyWindow windowController] loadURL:[NSURL fileURLWithPath:filename]];
                    332:     } else {
                    333:         [self openBrowserWindowWithURL:[NSURL fileURLWithPath:filename]];
                    334:     }
                    335:     
                    336:     return YES;
                    337:     
                    338: }
                    339: 
1.25      macserv   340: 
1.1       hyatt     341: @end

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