File:  [mozdev] / chimera / MainController.mm
Revision 1.30: download - view: text, annotated - select for diffs - revision graph
Sun Mar 10 07:36:38 2002 UTC (17 years, 9 months ago) by macserv
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

    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] splash];
   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:     [mBookmarksMenu setAutoenablesItems: NO];
   70:     
   71:     mMenuBookmarks = new BookmarksService(nil);
   72:     mMenuBookmarks->AddObserver();
   73:     mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
   74:     BookmarksService::gMainController = self;
   75:     
   76:     // Initialize offline mode.
   77:     mOffline = NO;
   78:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
   79:     if (!ioService)
   80:         return;
   81:     PRBool offline = PR_FALSE;
   82:     ioService->GetOffline(&offline);
   83:     mOffline = offline;
   84:     
   85:     // Set the menu item's text to "Go Online" if we're currently
   86:     // offline.
   87:     if (mOffline)
   88:         [mOfflineMenuItem setTitle: @"Go Online"];
   89: 
   90:     [mSplashScreen close];
   91: }
   92: 
   93: -(IBAction)newWindow:(id)aSender
   94: {
   95:     BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
   96:     [controller focusURLBar];
   97: }
   98: 
   99: -(IBAction)newTab:(id)aSender
  100: {
  101:     NSWindow* keyWindow = [mApplication keyWindow];
  102:     if (keyWindow)
  103:         [[keyWindow windowController] newTab];
  104: }
  105: 
  106: -(IBAction)closeTab:(id)aSender
  107: {
  108:     NSWindow* keyWindow = [mApplication keyWindow];
  109:     if (keyWindow)
  110:         [[keyWindow windowController] closeTab];
  111: }
  112: 
  113: -(IBAction) previousTab:(id)aSender
  114: {
  115:     NSWindow* keyWindow = [mApplication keyWindow];
  116:     if (keyWindow)
  117:         [[keyWindow windowController] previousTab];
  118: }
  119: 
  120: -(IBAction) nextTab:(id)aSender;
  121: {
  122:     NSWindow* keyWindow = [mApplication keyWindow];
  123:     if (keyWindow)
  124:         [[keyWindow windowController] nextTab];
  125: }
  126: 
  127: -(IBAction) openFile:(id)aSender
  128: {
  129:     NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
  130:     [openPanel setCanChooseFiles: YES];
  131:     [openPanel setCanChooseDirectories: NO];
  132:     [openPanel setAllowsMultipleSelection: NO];
  133:     NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
  134:                                                 @"txt",@"text",
  135:                                                 @"gif",@"jpg",@"jpeg",@"png",@"bmp",
  136:                                                 nil];
  137:     int result = [openPanel runModalForTypes: array];
  138:     if (result == NSOKButton) {
  139:         NSArray* urlArray = [openPanel URLs];
  140:         if ([urlArray count] == 0)
  141:             return;
  142:         NSURL* url = [urlArray objectAtIndex: 0];
  143:         NSWindow* keyWindow = [mApplication keyWindow];
  144:         if (keyWindow)
  145:             return [[keyWindow windowController] loadURL: url];
  146:         else
  147:             [self openBrowserWindowWithURL: url];
  148:     }
  149: }
  150: 
  151: -(IBAction) openLocation:(id)aSender
  152: {
  153:     NSWindow* keyWindow = [mApplication keyWindow];
  154:     if (!keyWindow) {
  155:       [self openBrowserWindowWithURLString: @"about:blank"];
  156:       keyWindow = [mApplication keyWindow];
  157:     }
  158:     
  159:     [[keyWindow windowController] performAppropriateLocationAction];
  160: }
  161: 
  162: -(IBAction) savePage:(id)aSender
  163: {
  164:     NSWindow* keyWindow = [mApplication keyWindow];
  165:     if (keyWindow)
  166:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
  167: }
  168: 
  169: -(IBAction) printPage:(id)aSender
  170: {
  171:     NSWindow* keyWindow = [mApplication keyWindow];
  172:     if (keyWindow)
  173:       [[keyWindow windowController] printDocument];
  174: }
  175: 
  176: -(IBAction) printPreview:(id)aSender
  177: {
  178:     NSWindow* keyWindow = [mApplication keyWindow];
  179:     if (keyWindow)
  180:       [[keyWindow windowController] printPreview];
  181: }
  182: 
  183: -(IBAction) toggleOfflineMode:(id)aSender
  184: {
  185:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  186:     if (!ioService)
  187:         return;
  188:     PRBool offline = PR_FALSE;
  189:     ioService->GetOffline(&offline);
  190:     ioService->SetOffline(!offline);
  191:     mOffline = !offline;
  192:     
  193:     // Update the menu item text.
  194:     // Set the menu item's text to "Go Online" if we're currently
  195:     // offline.
  196:     if (mOffline)
  197:         [mOfflineMenuItem setTitle: @"Go Online"];
  198:     else
  199:         [mOfflineMenuItem setTitle: @"Work Offline"];
  200:         
  201:     // Indicate that we are working offline.
  202:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
  203: }
  204: 
  205: // Edit menu actions.
  206: -(IBAction) findInPage:(id)aSender
  207: {
  208:     NSWindow* keyWindow = [mApplication keyWindow];
  209:     if (keyWindow)
  210:       [[keyWindow windowController] findInPage];
  211: }
  212: 
  213: -(IBAction) findAgain:(id)aSender
  214: {
  215:     NSWindow* keyWindow = [mApplication keyWindow];
  216:     if (keyWindow)
  217:       [[keyWindow windowController] findAgain];
  218: }
  219: 
  220: -(IBAction) goBack:(id)aSender
  221: {
  222:     [[[mApplication keyWindow] windowController] back: aSender]; 
  223: }
  224: 
  225: -(IBAction) goForward:(id)aSender
  226: {
  227:     [[[mApplication keyWindow] windowController] forward: aSender]; 
  228: }
  229: 
  230: -(IBAction) doReload:(id)aSender
  231: {
  232:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
  233: }
  234: 
  235: -(IBAction) doStop:(id)aSender
  236: {
  237:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
  238: }
  239: 
  240: -(IBAction) goHome:(id)aSender
  241: {
  242:     [[[mApplication keyWindow] windowController] home: aSender];
  243: }
  244: 
  245: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
  246: {
  247:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
  248: }
  249: 
  250: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
  251: {
  252: 	BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  253:     [browser loadURL: aURL];
  254: 	[browser showWindow: self];
  255:     return browser;
  256: }
  257: 
  258: -(void)applicationWillTerminate: (NSNotification*)aNotification
  259: {
  260:     printf("Termination notification.\n");
  261:     mMenuBookmarks->RemoveObserver();
  262:     delete mMenuBookmarks;
  263:     mMenuBookmarks = nsnull;
  264:     nsCocoaBrowserService::TermEmbedding();
  265: }
  266: 
  267: // Bookmarks menu actions.
  268: -(IBAction) addBookmark:(id)aSender
  269: {
  270:     NSWindow* keyWindow = [mApplication keyWindow];
  271:     if (keyWindow)
  272:       [[keyWindow windowController] addBookmark];
  273: }
  274: 
  275: -(IBAction) openMenuBookmark:(id)aSender
  276: {
  277:     NSWindow* keyWindow = [mApplication keyWindow];
  278:     if (!keyWindow) {
  279:         [self openBrowserWindowWithURLString: @"about:blank"];
  280:         keyWindow = [mApplication keyWindow];
  281:     }
  282: 
  283:     BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
  284: }
  285: 
  286: - (MVPreferencesController *)preferencesController
  287: {
  288:     if (!preferencesController) {
  289:         preferencesController = [[MVPreferencesController sharedInstance] retain];
  290:     }
  291:     return preferencesController;
  292: }
  293: 
  294: - (void)displayPreferencesWindow:sender
  295: {
  296:     [[self preferencesController] showPreferences:nil] ;
  297: }
  298: 
  299: - (IBAction)showAboutBox:(id)sender
  300: {
  301:     [[AboutBox sharedInstance] showPanel:sender];
  302: }
  303: 
  304: @end

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