File:  [mozdev] / chimera / MainController.mm
Revision 1.48: download - view: text, annotated - select for diffs - revision graph
Sat Apr 20 00:44:56 2002 UTC (17 years, 5 months ago) by macserv
Branches: MAIN
CVS tags: HEAD
A bunch of tweaks here... no changes in functionality.  Updated trilicense header on all files that were missing it.  Fixed initialization code syntax errors all over the place.  Put back fade code, and switched fading off for now...

    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:NO withStatusRect:NSMakeRect(0,0,0,0)];
   56:         mFindDialog = nil;
   57:         mMenuBookmarks = nil;
   58:     }
   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((BookmarksDataSource*)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* mainWindow = [mApplication mainWindow];
  102:   if ( mainWindow && [[mainWindow windowController] respondsToSelector:@selector(autosaveWindowFrame)] )
  103:     [[mainWindow 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:   [[[mApplication mainWindow] windowController] newTab];
  113: }
  114: 
  115: -(IBAction)closeTab:(id)aSender
  116: {
  117:   [[[mApplication mainWindow] windowController] closeTab];
  118: }
  119: 
  120: -(IBAction) previousTab:(id)aSender
  121: {
  122:   [[[mApplication mainWindow] windowController] previousTab];
  123: }
  124: 
  125: -(IBAction) nextTab:(id)aSender;
  126: {
  127: 	[[[mApplication mainWindow] windowController] nextTab];
  128: }
  129: 
  130: -(IBAction) openFile:(id)aSender
  131: {
  132:     NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
  133:     [openPanel setCanChooseFiles: YES];
  134:     [openPanel setCanChooseDirectories: NO];
  135:     [openPanel setAllowsMultipleSelection: NO];
  136:     NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
  137:                                                 @"txt",@"text",
  138:                                                 @"gif",@"jpg",@"jpeg",@"png",@"bmp",
  139:                                                 nil];
  140:     int result = [openPanel runModalForTypes: array];
  141:     if (result == NSOKButton) {
  142:         NSArray* urlArray = [openPanel URLs];
  143:         if ([urlArray count] == 0)
  144:             return;
  145:         NSURL* url = [urlArray objectAtIndex: 0];
  146:         // ----------------------
  147:         [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:url];
  148:         // ----------------------
  149:         NSWindow* mainWindow = [mApplication mainWindow];
  150:         if (mainWindow) {
  151:           [[mainWindow windowController] loadURL: url];
  152:           [[[[mainWindow windowController] getMyBrowserView] getBrowserView] setActive: YES];
  153:         }
  154:         else
  155:           [self openBrowserWindowWithURL: url];
  156:     }
  157: }
  158: 
  159: -(IBAction) openLocation:(id)aSender
  160: {
  161:     NSWindow* mainWindow = [mApplication mainWindow];
  162:     if (!mainWindow) {
  163:       [self openBrowserWindowWithURLString: @"about:blank"];
  164:       mainWindow = [mApplication mainWindow];
  165:     }
  166:     
  167:     [[mainWindow windowController] performAppropriateLocationAction];
  168: }
  169: 
  170: -(IBAction) savePage:(id)aSender
  171: {
  172:   [[[mApplication mainWindow] windowController] saveDocument: mFilterView filterList: mFilterList];
  173: }
  174: 
  175: -(IBAction) printPage:(id)aSender
  176: {
  177:   [[[mApplication mainWindow] windowController] printDocument];
  178: }
  179: 
  180: -(IBAction) printPreview:(id)aSender
  181: {
  182:   [[[mApplication mainWindow] windowController] printPreview];
  183: }
  184: 
  185: -(IBAction) toggleOfflineMode:(id)aSender
  186: {
  187:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  188:     if (!ioService)
  189:         return;
  190:     PRBool offline = PR_FALSE;
  191:     ioService->GetOffline(&offline);
  192:     ioService->SetOffline(!offline);
  193:     mOffline = !offline;
  194:     
  195:     // Update the menu item text.
  196:     // Set the menu item's text to "Go Online" if we're currently
  197:     // offline.
  198:     if (mOffline)
  199:         [mOfflineMenuItem setTitle: @"Go Online"];
  200:     else
  201:         [mOfflineMenuItem setTitle: @"Work Offline"];
  202:         
  203:     // Indicate that we are working offline.
  204:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
  205: }
  206: 
  207: // Edit menu actions.
  208: 
  209: 
  210: //
  211: // -findInPage
  212: //
  213: // Called in response to "Find" in edit menu. Opens the find dialog. We only keep
  214: // one around for the whole app to use, showing/hiding as we see fit.
  215: //
  216: -(IBAction) findInPage:(id)aSender
  217: {
  218:   if ( !mFindDialog )
  219:      mFindDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
  220:   [mFindDialog showWindow:self];
  221: }
  222: 
  223: 
  224: //
  225: // -findAgain
  226: //
  227: // Called in response to "Find Again" in edit menu. Tells the find controller
  228: // to find the next occurrance of what's already been found.
  229: //
  230: -(IBAction) findAgain:(id)aSender
  231: {
  232:   if ( mFindDialog )
  233:     [mFindDialog findAgain:aSender];
  234:   else
  235:     NSBeep();
  236: }
  237: 
  238: -(IBAction) goBack:(id)aSender
  239: {
  240:     [[[mApplication mainWindow] windowController] back: aSender]; 
  241: }
  242: 
  243: -(IBAction) goForward:(id)aSender
  244: {
  245:     [[[mApplication mainWindow] windowController] forward: aSender]; 
  246: }
  247: 
  248: -(IBAction) doReload:(id)aSender
  249: {
  250:     [(BrowserWindowController*)([[mApplication mainWindow] windowController]) reload: aSender]; 
  251: }
  252: 
  253: -(IBAction) doStop:(id)aSender
  254: {
  255:     [(BrowserWindowController*)([[mApplication mainWindow] windowController]) stop: aSender]; 
  256: }
  257: 
  258: -(IBAction) goHome:(id)aSender
  259: {
  260:     [[[mApplication mainWindow] windowController] home: aSender];
  261: }
  262: 
  263: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
  264: {
  265:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
  266: }
  267: 
  268: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
  269: {
  270: 	BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  271:   [browser loadURL: aURL];
  272: 	[browser showWindow: self];
  273:   return browser;
  274: }
  275: 
  276: -(void)applicationWillTerminate: (NSNotification*)aNotification
  277: {
  278:     printf("Termination notification.\n");
  279: 
  280:     // Autosave one of the windows.
  281:     [[[mApplication mainWindow] windowController] autosaveWindowFrame];
  282:     
  283:     mMenuBookmarks->RemoveObserver();
  284:     delete mMenuBookmarks;
  285:     mMenuBookmarks = nsnull;
  286:     nsCocoaBrowserService::TermEmbedding();
  287: }
  288: 
  289: // Bookmarks menu actions.
  290: -(IBAction) addBookmark:(id)aSender
  291: {
  292:   [[[mApplication mainWindow] windowController] addBookmarkExtended: YES isFolder: NO];
  293: }
  294: 
  295: -(IBAction) addFolder:(id)aSender
  296: {
  297:   [[[mApplication mainWindow] windowController] addBookmarkExtended: YES isFolder: YES];
  298: }
  299: 
  300: -(IBAction) addSeparator:(id)aSender
  301: {
  302: }
  303: 
  304: -(IBAction) openMenuBookmark:(id)aSender
  305: {
  306:     NSWindow* mainWind = [mApplication mainWindow];
  307:     if (!mainWind) {
  308:         [self openBrowserWindowWithURLString: @"about:blank"];
  309:         mainWind = [mApplication mainWindow];
  310:     }
  311: 
  312:     BookmarksService::OpenMenuBookmark([mainWind windowController], aSender);
  313: }
  314: 
  315: -(IBAction)manageBookmarks: (id)aSender
  316: {
  317:   NSWindow* mainWindow = [mApplication mainWindow];
  318:   if (!mainWindow) {
  319:     [self openBrowserWindowWithURLString: @"about:blank"];
  320:     mainWindow = [mApplication mainWindow];
  321:   }
  322: 
  323:   [[mainWindow windowController] manageBookmarks: aSender];
  324: }
  325: 
  326: - (MVPreferencesController *)preferencesController
  327: {
  328:     if (!preferencesController) {
  329:         preferencesController = [[MVPreferencesController sharedInstance] retain];
  330:     }
  331:     return preferencesController;
  332: }
  333: 
  334: - (void)displayPreferencesWindow:sender
  335: {
  336:     [[self preferencesController] showPreferences:nil] ;
  337: }
  338: 
  339: - (IBAction)showAboutBox:(id)sender
  340: {
  341:     [[AboutBox sharedInstance] showPanel:sender];
  342: }
  343: 
  344: - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
  345: {
  346:     NSWindow* mainWindow = [mApplication mainWindow];
  347: 
  348:     if (mainWindow) {
  349:         [[mainWindow windowController] loadURL:[NSURL fileURLWithPath:filename]];
  350:     } else {
  351:         [self openBrowserWindowWithURL:[NSURL fileURLWithPath:filename]];
  352:     }
  353:     
  354:     return YES;
  355:     
  356: }
  357: 
  358: - (IBAction)biggerTextSize:(id)aSender
  359: {
  360:   [[[mApplication mainWindow] windowController] biggerTextSize];
  361: }
  362: 
  363: - (IBAction)smallerTextSize:(id)aSender
  364: {
  365:   [[[mApplication mainWindow] windowController] smallerTextSize];
  366: }
  367: 
  368: - (IBAction)toggleSmoothText:(id)aSender
  369: {
  370:   // XXXdwh Grab the prefs service and just set the pref directly.
  371:   
  372: }
  373: 
  374: 
  375: @end

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