File:  [mozdev] / chimera / MainController.mm
Revision 1.35: download - view: text, annotated - select for diffs - revision graph
Fri Apr 5 00:28:47 2002 UTC (17 years, 7 months ago) by macserv
Branches: MAIN
CVS tags: HEAD
Splash fade in effect

    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 ***** */
   37: 
   38: #import "MainController.h"
   39: #import "BrowserWindowController.h"
   40: #include "BookmarksService.h"
   41: #include "nsCOMPtr.h"
   42: #include "nsIServiceManager.h"
   43: #include "nsIIOService.h"
   44: #include "nsCocoaBrowserService.h"
   45: #import	"AboutBox.h"
   46: 
   47: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
   48: 
   49: @implementation MainController
   50: 
   51: -(id)init
   52: {
   53:     if ( (self = [super init]) ) {
   54:         mSplashScreen = [[SplashScreenWindow alloc] splashImage:nil withFade:YES withStatusRect:NSMakeRect(0,0,0,0)];
   55:     }
   56:     return self;
   57: }
   58: 
   59: -(void)dealloc
   60: {
   61:     [super dealloc];
   62:     printf("Main controller died.\n");
   63: }
   64: 
   65: -(void)awakeFromNib
   66: {    
   67:     [self newWindow: self];
   68:     
   69:     [mSplashScreen close];
   70: 
   71:     [mBookmarksMenu setAutoenablesItems: NO];
   72:     mMenuBookmarks = new BookmarksService(nil);
   73:     mMenuBookmarks->AddObserver();
   74:     mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
   75:     BookmarksService::gMainController = self;
   76:     
   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"];
   90: 
   91: }
   92: 
   93: -(IBAction)newWindow:(id)aSender
   94: {
   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: }	
  105: 
  106: -(IBAction)newTab:(id)aSender
  107: {
  108:     NSWindow* keyWindow = [mApplication keyWindow];
  109:     if (keyWindow)
  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];
  132: }
  133: 
  134: -(IBAction) openFile:(id)aSender
  135: {
  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];
  150:         // ----------------------
  151:         [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:url];
  152:         // ----------------------
  153:         NSWindow* keyWindow = [mApplication keyWindow];
  154:         if (keyWindow)
  155:             return [[keyWindow windowController] loadURL: url];
  156:         else
  157:             [self openBrowserWindowWithURL: url];
  158:     }
  159: }
  160: 
  161: -(IBAction) openLocation:(id)aSender
  162: {
  163:     NSWindow* keyWindow = [mApplication keyWindow];
  164:     if (!keyWindow) {
  165:       [self openBrowserWindowWithURLString: @"about:blank"];
  166:       keyWindow = [mApplication keyWindow];
  167:     }
  168:     
  169:     [[keyWindow windowController] performAppropriateLocationAction];
  170: }
  171: 
  172: -(IBAction) savePage:(id)aSender
  173: {
  174:     NSWindow* keyWindow = [mApplication keyWindow];
  175:     if (keyWindow)
  176:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
  177: }
  178: 
  179: -(IBAction) printPage:(id)aSender
  180: {
  181:     NSWindow* keyWindow = [mApplication keyWindow];
  182:     if (keyWindow)
  183:       [[keyWindow windowController] printDocument];
  184: }
  185: 
  186: -(IBAction) printPreview:(id)aSender
  187: {
  188:     NSWindow* keyWindow = [mApplication keyWindow];
  189:     if (keyWindow)
  190:       [[keyWindow windowController] printPreview];
  191: }
  192: 
  193: -(IBAction) toggleOfflineMode:(id)aSender
  194: {
  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.
  212:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
  213: }
  214: 
  215: // Edit menu actions.
  216: -(IBAction) findInPage:(id)aSender
  217: {
  218:     NSWindow* keyWindow = [mApplication keyWindow];
  219:     if (keyWindow)
  220:       [[keyWindow windowController] findInPage];
  221: }
  222: 
  223: -(IBAction) findAgain:(id)aSender
  224: {
  225:     NSWindow* keyWindow = [mApplication keyWindow];
  226:     if (keyWindow)
  227:       [[keyWindow windowController] findAgain];
  228: }
  229: 
  230: -(IBAction) goBack:(id)aSender
  231: {
  232:     [[[mApplication keyWindow] windowController] back: aSender]; 
  233: }
  234: 
  235: -(IBAction) goForward:(id)aSender
  236: {
  237:     [[[mApplication keyWindow] windowController] forward: aSender]; 
  238: }
  239: 
  240: -(IBAction) doReload:(id)aSender
  241: {
  242:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
  243: }
  244: 
  245: -(IBAction) doStop:(id)aSender
  246: {
  247:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
  248: }
  249: 
  250: -(IBAction) goHome:(id)aSender
  251: {
  252:     [[[mApplication keyWindow] windowController] home: aSender];
  253: }
  254: 
  255: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
  256: {
  257:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
  258: }
  259: 
  260: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
  261: {
  262: 	BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  263:   [browser loadURL: aURL];
  264: 	[browser showWindow: self];
  265:   return browser;
  266: }
  267: 
  268: -(void)applicationWillTerminate: (NSNotification*)aNotification
  269: {
  270:     printf("Termination notification.\n");
  271:     mMenuBookmarks->RemoveObserver();
  272:     delete mMenuBookmarks;
  273:     mMenuBookmarks = nsnull;
  274:     nsCocoaBrowserService::TermEmbedding();
  275: }
  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: 
  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: }
  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: 
  309: - (IBAction)showAboutBox:(id)sender
  310: {
  311:     [[AboutBox sharedInstance] showPanel:sender];
  312: }
  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: 
  328: 
  329: @end

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