File:  [mozdev] / chimera / MainController.mm
Revision 1.40: download - view: text, annotated - select for diffs - revision graph
Sun Apr 14 00:08:17 2002 UTC (17 years, 6 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Open link in new window/tab now works from the context menu.

    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: 
   48: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
   49: 
   50: @implementation MainController
   51: 
   52: -(id)init
   53: {
   54:     if ( (self = [super init]) ) {
   55:         mSplashScreen = [[SplashScreenWindow alloc] splashImage:nil withFade:YES withStatusRect:NSMakeRect(0,0,0,0)];
   56:     }
   57:     mFindDialog = nil;
   58:     mMenuBookmarks = nil;
   59:     return self;
   60: }
   61: 
   62: -(void)dealloc
   63: {
   64:     [super dealloc];
   65:     [mFindDialog release];
   66:     printf("Main controller died.\n");
   67: }
   68: 
   69: -(void)awakeFromNib
   70: {    
   71:     [self newWindow: self];
   72:     
   73:     [mSplashScreen close];
   74: 
   75:     [mBookmarksMenu setAutoenablesItems: NO];
   76:     mMenuBookmarks = new BookmarksService(nil);
   77:     mMenuBookmarks->AddObserver();
   78:     mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
   79:     BookmarksService::gMainController = self;
   80:     
   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"];
   94: 
   95: }
   96: 
   97: -(IBAction)newWindow:(id)aSender
   98: {
   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 && [keyWindow respondsToSelector:@selector(autosaveWindowFrame)] )
  103:     [[keyWindow windowController] autosaveWindowFrame];
  104: 
  105:   // Now open the new window.
  106:   BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
  107:   [controller focusURLBar];
  108: }	
  109: 
  110: -(IBAction)newTab:(id)aSender
  111: {
  112:     NSWindow* keyWindow = [mApplication keyWindow];
  113:     if (keyWindow)
  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];
  136: }
  137: 
  138: -(IBAction) openFile:(id)aSender
  139: {
  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];
  154:         // ----------------------
  155:         [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:url];
  156:         // ----------------------
  157:         NSWindow* keyWindow = [mApplication keyWindow];
  158:         if (keyWindow) {
  159:           [[keyWindow windowController] loadURL: url];
  160:           [[[[keyWindow windowController] getMyBrowserView] getBrowserView] setActive: YES];
  161:         }
  162:         else
  163:           [self openBrowserWindowWithURL: url];
  164:     }
  165: }
  166: 
  167: -(IBAction) openLocation:(id)aSender
  168: {
  169:     NSWindow* keyWindow = [mApplication keyWindow];
  170:     if (!keyWindow) {
  171:       [self openBrowserWindowWithURLString: @"about:blank"];
  172:       keyWindow = [mApplication keyWindow];
  173:     }
  174:     
  175:     [[keyWindow windowController] performAppropriateLocationAction];
  176: }
  177: 
  178: -(IBAction) savePage:(id)aSender
  179: {
  180:     NSWindow* keyWindow = [mApplication keyWindow];
  181:     if (keyWindow)
  182:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
  183: }
  184: 
  185: -(IBAction) printPage:(id)aSender
  186: {
  187:     NSWindow* keyWindow = [mApplication keyWindow];
  188:     if (keyWindow)
  189:       [[keyWindow windowController] printDocument];
  190: }
  191: 
  192: -(IBAction) printPreview:(id)aSender
  193: {
  194:     NSWindow* keyWindow = [mApplication keyWindow];
  195:     if (keyWindow)
  196:       [[keyWindow windowController] printPreview];
  197: }
  198: 
  199: -(IBAction) toggleOfflineMode:(id)aSender
  200: {
  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.
  218:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
  219: }
  220: 
  221: // Edit menu actions.
  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: //
  230: -(IBAction) findInPage:(id)aSender
  231: {
  232:   if ( !mFindDialog )
  233:      mFindDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
  234:   [mFindDialog showWindow:self];
  235: }
  236: 
  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: //
  244: -(IBAction) findAgain:(id)aSender
  245: {
  246:   if ( mFindDialog )
  247:     [mFindDialog findAgain:aSender];
  248:   else
  249:     NSBeep();
  250: }
  251: 
  252: -(IBAction) goBack:(id)aSender
  253: {
  254:     [[[mApplication keyWindow] windowController] back: aSender]; 
  255: }
  256: 
  257: -(IBAction) goForward:(id)aSender
  258: {
  259:     [[[mApplication keyWindow] windowController] forward: aSender]; 
  260: }
  261: 
  262: -(IBAction) doReload:(id)aSender
  263: {
  264:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
  265: }
  266: 
  267: -(IBAction) doStop:(id)aSender
  268: {
  269:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
  270: }
  271: 
  272: -(IBAction) goHome:(id)aSender
  273: {
  274:     [[[mApplication keyWindow] windowController] home: aSender];
  275: }
  276: 
  277: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
  278: {
  279:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
  280: }
  281: 
  282: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
  283: {
  284: 	BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  285:   [browser loadURL: aURL];
  286: 	[browser showWindow: self];
  287:   return browser;
  288: }
  289: 
  290: -(void)applicationWillTerminate: (NSNotification*)aNotification
  291: {
  292:     printf("Termination notification.\n");
  293: 
  294:     // Autosave one of the windows.
  295:     NSWindow* keyWindow = [mApplication keyWindow];
  296:     if (keyWindow)
  297:       [[keyWindow windowController] autosaveWindowFrame];
  298:     
  299:     mMenuBookmarks->RemoveObserver();
  300:     delete mMenuBookmarks;
  301:     mMenuBookmarks = nsnull;
  302:     nsCocoaBrowserService::TermEmbedding();
  303: }
  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: 
  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: }
  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: 
  337: - (IBAction)showAboutBox:(id)sender
  338: {
  339:     [[AboutBox sharedInstance] showPanel:sender];
  340: }
  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: 
  356: 
  357: @end

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