Annotation of chimera/MainController.mm, revision 1.35

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: 
                     47: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
                     48: 
1.1       hyatt      49: @implementation MainController
                     50: 
1.30      macserv    51: -(id)init
                     52: {
                     53:     if ( (self = [super init]) ) {
1.35    ! macserv    54:         mSplashScreen = [[SplashScreenWindow alloc] splashImage:nil withFade:YES withStatusRect:NSMakeRect(0,0,0,0)];
1.30      macserv    55:     }
                     56:     return self;
                     57: }
                     58: 
1.21      hyatt      59: -(void)dealloc
                     60: {
                     61:     [super dealloc];
                     62:     printf("Main controller died.\n");
                     63: }
                     64: 
1.1       hyatt      65: -(void)awakeFromNib
1.35    ! macserv    66: {    
1.4       hyatt      67:     [self newWindow: self];
1.35    ! macserv    68:     
        !            69:     [mSplashScreen close];
1.31      macserv    70: 
1.19      hyatt      71:     [mBookmarksMenu setAutoenablesItems: NO];
                     72:     mMenuBookmarks = new BookmarksService(nil);
                     73:     mMenuBookmarks->AddObserver();
                     74:     mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
1.20      hyatt      75:     BookmarksService::gMainController = self;
1.19      hyatt      76:     
1.10      hyatt      77:     // Initialize offline mode.
                     78:     mOffline = NO;
                     79:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                     80:     if (!ioService)
                     81:         return;
                     82:     PRBool offline = PR_FALSE;
                     83:     ioService->GetOffline(&offline);
                     84:     mOffline = offline;
                     85:     
                     86:     // Set the menu item's text to "Go Online" if we're currently
                     87:     // offline.
                     88:     if (mOffline)
                     89:         [mOfflineMenuItem setTitle: @"Go Online"];
1.30      macserv    90: 
1.1       hyatt      91: }
                     92: 
                     93: -(IBAction)newWindow:(id)aSender
                     94: {
1.32      hyatt      95:   // If we have a key window, have it autosave its dimensions before
                     96:   // we open a new window.  That ensures the size ends up matching.
                     97:   NSWindow* keyWindow = [mApplication keyWindow];
                     98:   if (keyWindow)
                     99:     [[keyWindow windowController] autosaveWindowFrame];
                    100: 
                    101:   // Now open the new window.
                    102:   BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
                    103:   [controller focusURLBar];
                    104: }  
1.4       hyatt     105: 
1.22      hyatt     106: -(IBAction)newTab:(id)aSender
                    107: {
                    108:     NSWindow* keyWindow = [mApplication keyWindow];
                    109:     if (keyWindow)
1.26      macserv   110:         [[keyWindow windowController] newTab];
                    111: }
                    112: 
                    113: -(IBAction)closeTab:(id)aSender
                    114: {
                    115:     NSWindow* keyWindow = [mApplication keyWindow];
                    116:     if (keyWindow)
                    117:         [[keyWindow windowController] closeTab];
                    118: }
                    119: 
                    120: -(IBAction) previousTab:(id)aSender
                    121: {
                    122:     NSWindow* keyWindow = [mApplication keyWindow];
                    123:     if (keyWindow)
                    124:         [[keyWindow windowController] previousTab];
                    125: }
                    126: 
                    127: -(IBAction) nextTab:(id)aSender;
                    128: {
                    129:     NSWindow* keyWindow = [mApplication keyWindow];
                    130:     if (keyWindow)
                    131:         [[keyWindow windowController] nextTab];
1.22      hyatt     132: }
                    133: 
1.4       hyatt     134: -(IBAction) openFile:(id)aSender
                    135: {
1.5       hyatt     136:     NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
                    137:     [openPanel setCanChooseFiles: YES];
                    138:     [openPanel setCanChooseDirectories: NO];
                    139:     [openPanel setAllowsMultipleSelection: NO];
                    140:     NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
                    141:                                                 @"txt",@"text",
                    142:                                                 @"gif",@"jpg",@"jpeg",@"png",@"bmp",
                    143:                                                 nil];
                    144:     int result = [openPanel runModalForTypes: array];
                    145:     if (result == NSOKButton) {
                    146:         NSArray* urlArray = [openPanel URLs];
                    147:         if ([urlArray count] == 0)
                    148:             return;
                    149:         NSURL* url = [urlArray objectAtIndex: 0];
1.33      macserv   150:         // ----------------------
                    151:         [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:url];
                    152:         // ----------------------
1.5       hyatt     153:         NSWindow* keyWindow = [mApplication keyWindow];
                    154:         if (keyWindow)
                    155:             return [[keyWindow windowController] loadURL: url];
                    156:         else
                    157:             [self openBrowserWindowWithURL: url];
                    158:     }
1.4       hyatt     159: }
                    160: 
                    161: -(IBAction) openLocation:(id)aSender
                    162: {
                    163:     NSWindow* keyWindow = [mApplication keyWindow];
                    164:     if (!keyWindow) {
1.5       hyatt     165:       [self openBrowserWindowWithURLString: @"about:blank"];
1.4       hyatt     166:       keyWindow = [mApplication keyWindow];
                    167:     }
                    168:     
1.6       macserv   169:     [[keyWindow windowController] performAppropriateLocationAction];
1.1       hyatt     170: }
                    171: 
1.7       hyatt     172: -(IBAction) savePage:(id)aSender
                    173: {
                    174:     NSWindow* keyWindow = [mApplication keyWindow];
                    175:     if (keyWindow)
1.8       hyatt     176:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
1.7       hyatt     177: }
                    178: 
1.9       hyatt     179: -(IBAction) printPage:(id)aSender
                    180: {
                    181:     NSWindow* keyWindow = [mApplication keyWindow];
                    182:     if (keyWindow)
                    183:       [[keyWindow windowController] printDocument];
                    184: }
                    185: 
1.16      hyatt     186: -(IBAction) printPreview:(id)aSender
                    187: {
                    188:     NSWindow* keyWindow = [mApplication keyWindow];
                    189:     if (keyWindow)
                    190:       [[keyWindow windowController] printPreview];
                    191: }
                    192: 
1.7       hyatt     193: -(IBAction) toggleOfflineMode:(id)aSender
                    194: {
1.10      hyatt     195:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    196:     if (!ioService)
                    197:         return;
                    198:     PRBool offline = PR_FALSE;
                    199:     ioService->GetOffline(&offline);
                    200:     ioService->SetOffline(!offline);
                    201:     mOffline = !offline;
                    202:     
                    203:     // Update the menu item text.
                    204:     // Set the menu item's text to "Go Online" if we're currently
                    205:     // offline.
                    206:     if (mOffline)
                    207:         [mOfflineMenuItem setTitle: @"Go Online"];
                    208:     else
                    209:         [mOfflineMenuItem setTitle: @"Work Offline"];
                    210:         
                    211:     // Indicate that we are working offline.
1.11      hyatt     212:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
1.7       hyatt     213: }
                    214: 
1.13      hyatt     215: // Edit menu actions.
                    216: -(IBAction) findInPage:(id)aSender
1.16      hyatt     217: {
1.17      hyatt     218:     NSWindow* keyWindow = [mApplication keyWindow];
                    219:     if (keyWindow)
                    220:       [[keyWindow windowController] findInPage];
1.16      hyatt     221: }
                    222: 
                    223: -(IBAction) findAgain:(id)aSender
1.13      hyatt     224: {
1.17      hyatt     225:     NSWindow* keyWindow = [mApplication keyWindow];
                    226:     if (keyWindow)
                    227:       [[keyWindow windowController] findAgain];
1.13      hyatt     228: }
                    229: 
1.23      hyatt     230: -(IBAction) goBack:(id)aSender
1.2       hyatt     231: {
                    232:     [[[mApplication keyWindow] windowController] back: aSender]; 
                    233: }
                    234: 
1.23      hyatt     235: -(IBAction) goForward:(id)aSender
1.2       hyatt     236: {
                    237:     [[[mApplication keyWindow] windowController] forward: aSender]; 
                    238: }
                    239: 
1.23      hyatt     240: -(IBAction) doReload:(id)aSender
1.2       hyatt     241: {
1.23      hyatt     242:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
1.2       hyatt     243: }
                    244: 
1.23      hyatt     245: -(IBAction) doStop:(id)aSender
1.2       hyatt     246: {
1.23      hyatt     247:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
1.2       hyatt     248: }
                    249: 
1.23      hyatt     250: -(IBAction) goHome:(id)aSender
1.2       hyatt     251: {
                    252:     [[[mApplication keyWindow] windowController] home: aSender];
                    253: }
                    254: 
1.5       hyatt     255: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
                    256: {
                    257:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
                    258: }
                    259: 
                    260: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
1.1       hyatt     261: {
1.3       macserv   262:  BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
1.34      hyatt     263:   [browser loadURL: aURL];
1.3       macserv   264:  [browser showWindow: self];
1.34      hyatt     265:   return browser;
1.1       hyatt     266: }
                    267: 
1.12      hyatt     268: -(void)applicationWillTerminate: (NSNotification*)aNotification
                    269: {
1.24      hyatt     270:     printf("Termination notification.\n");
1.19      hyatt     271:     mMenuBookmarks->RemoveObserver();
                    272:     delete mMenuBookmarks;
1.24      hyatt     273:     mMenuBookmarks = nsnull;
1.12      hyatt     274:     nsCocoaBrowserService::TermEmbedding();
                    275: }
1.18      hyatt     276: 
                    277: // Bookmarks menu actions.
                    278: -(IBAction) addBookmark:(id)aSender
                    279: {
                    280:     NSWindow* keyWindow = [mApplication keyWindow];
                    281:     if (keyWindow)
                    282:       [[keyWindow windowController] addBookmark];
                    283: }
                    284: 
1.20      hyatt     285: -(IBAction) openMenuBookmark:(id)aSender
                    286: {
                    287:     NSWindow* keyWindow = [mApplication keyWindow];
                    288:     if (!keyWindow) {
                    289:         [self openBrowserWindowWithURLString: @"about:blank"];
                    290:         keyWindow = [mApplication keyWindow];
                    291:     }
                    292: 
                    293:     BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
                    294: }
1.25      macserv   295: 
                    296: - (MVPreferencesController *)preferencesController
                    297: {
                    298:     if (!preferencesController) {
                    299:         preferencesController = [[MVPreferencesController sharedInstance] retain];
                    300:     }
                    301:     return preferencesController;
                    302: }
                    303: 
                    304: - (void)displayPreferencesWindow:sender
                    305: {
                    306:     [[self preferencesController] showPreferences:nil] ;
                    307: }
                    308: 
1.30      macserv   309: - (IBAction)showAboutBox:(id)sender
                    310: {
                    311:     [[AboutBox sharedInstance] showPanel:sender];
                    312: }
1.33      macserv   313: 
                    314: - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
                    315: {
                    316:     NSWindow* keyWindow = [mApplication keyWindow];
                    317: 
                    318:     if (keyWindow) {
                    319:         [[keyWindow windowController] loadURL:[NSURL fileURLWithPath:filename]];
                    320:     } else {
                    321:         [self openBrowserWindowWithURL:[NSURL fileURLWithPath:filename]];
                    322:     }
                    323:     
                    324:     return YES;
                    325:     
                    326: }
                    327: 
1.25      macserv   328: 
1.1       hyatt     329: @end

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