File:  [mozdev] / chimera / MainController.mm
Revision 1.32: download - view: text, annotated - select for diffs - revision graph
Sun Mar 10 19:26:16 2002 UTC (17 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Full implementation of window autosaving (including correct ignoring of windows opened as popups by web pages.

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

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