File:  [mozdev] / chimera / MainController.mm
Revision 1.39: download - view: text, annotated - select for diffs - revision graph
Fri Apr 12 19:45:37 2002 UTC (17 years, 8 months ago) by pinkerton
Branches: MAIN
CVS tags: HEAD
make "find again" work, as well as across multiple windows. uses a new "Find" protocol and puts all the logic in the find controller.

    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:             return [[keyWindow windowController] loadURL: url];
  160:         else
  161:             [self openBrowserWindowWithURL: url];
  162:     }
  163: }
  164: 
  165: -(IBAction) openLocation:(id)aSender
  166: {
  167:     NSWindow* keyWindow = [mApplication keyWindow];
  168:     if (!keyWindow) {
  169:       [self openBrowserWindowWithURLString: @"about:blank"];
  170:       keyWindow = [mApplication keyWindow];
  171:     }
  172:     
  173:     [[keyWindow windowController] performAppropriateLocationAction];
  174: }
  175: 
  176: -(IBAction) savePage:(id)aSender
  177: {
  178:     NSWindow* keyWindow = [mApplication keyWindow];
  179:     if (keyWindow)
  180:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
  181: }
  182: 
  183: -(IBAction) printPage:(id)aSender
  184: {
  185:     NSWindow* keyWindow = [mApplication keyWindow];
  186:     if (keyWindow)
  187:       [[keyWindow windowController] printDocument];
  188: }
  189: 
  190: -(IBAction) printPreview:(id)aSender
  191: {
  192:     NSWindow* keyWindow = [mApplication keyWindow];
  193:     if (keyWindow)
  194:       [[keyWindow windowController] printPreview];
  195: }
  196: 
  197: -(IBAction) toggleOfflineMode:(id)aSender
  198: {
  199:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  200:     if (!ioService)
  201:         return;
  202:     PRBool offline = PR_FALSE;
  203:     ioService->GetOffline(&offline);
  204:     ioService->SetOffline(!offline);
  205:     mOffline = !offline;
  206:     
  207:     // Update the menu item text.
  208:     // Set the menu item's text to "Go Online" if we're currently
  209:     // offline.
  210:     if (mOffline)
  211:         [mOfflineMenuItem setTitle: @"Go Online"];
  212:     else
  213:         [mOfflineMenuItem setTitle: @"Work Offline"];
  214:         
  215:     // Indicate that we are working offline.
  216:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
  217: }
  218: 
  219: // Edit menu actions.
  220: 
  221: 
  222: //
  223: // -findInPage
  224: //
  225: // Called in response to "Find" in edit menu. Opens the find dialog. We only keep
  226: // one around for the whole app to use, showing/hiding as we see fit.
  227: //
  228: -(IBAction) findInPage:(id)aSender
  229: {
  230:   if ( !mFindDialog )
  231:      mFindDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
  232:   [mFindDialog showWindow:self];
  233: }
  234: 
  235: 
  236: //
  237: // -findAgain
  238: //
  239: // Called in response to "Find Again" in edit menu. Tells the find controller
  240: // to find the next occurrance of what's already been found.
  241: //
  242: -(IBAction) findAgain:(id)aSender
  243: {
  244:   if ( mFindDialog )
  245:     [mFindDialog findAgain:aSender];
  246:   else
  247:     NSBeep();
  248: }
  249: 
  250: -(IBAction) goBack:(id)aSender
  251: {
  252:     [[[mApplication keyWindow] windowController] back: aSender]; 
  253: }
  254: 
  255: -(IBAction) goForward:(id)aSender
  256: {
  257:     [[[mApplication keyWindow] windowController] forward: aSender]; 
  258: }
  259: 
  260: -(IBAction) doReload:(id)aSender
  261: {
  262:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
  263: }
  264: 
  265: -(IBAction) doStop:(id)aSender
  266: {
  267:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
  268: }
  269: 
  270: -(IBAction) goHome:(id)aSender
  271: {
  272:     [[[mApplication keyWindow] windowController] home: aSender];
  273: }
  274: 
  275: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
  276: {
  277:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
  278: }
  279: 
  280: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
  281: {
  282: 	BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  283:   [browser loadURL: aURL];
  284: 	[browser showWindow: self];
  285:   return browser;
  286: }
  287: 
  288: -(void)applicationWillTerminate: (NSNotification*)aNotification
  289: {
  290:     printf("Termination notification.\n");
  291: 
  292:     // Autosave one of the windows.
  293:     NSWindow* keyWindow = [mApplication keyWindow];
  294:     if (keyWindow)
  295:       [[keyWindow windowController] autosaveWindowFrame];
  296:     
  297:     mMenuBookmarks->RemoveObserver();
  298:     delete mMenuBookmarks;
  299:     mMenuBookmarks = nsnull;
  300:     nsCocoaBrowserService::TermEmbedding();
  301: }
  302: 
  303: // Bookmarks menu actions.
  304: -(IBAction) addBookmark:(id)aSender
  305: {
  306:     NSWindow* keyWindow = [mApplication keyWindow];
  307:     if (keyWindow)
  308:       [[keyWindow windowController] addBookmark];
  309: }
  310: 
  311: -(IBAction) openMenuBookmark:(id)aSender
  312: {
  313:     NSWindow* keyWindow = [mApplication keyWindow];
  314:     if (!keyWindow) {
  315:         [self openBrowserWindowWithURLString: @"about:blank"];
  316:         keyWindow = [mApplication keyWindow];
  317:     }
  318: 
  319:     BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
  320: }
  321: 
  322: - (MVPreferencesController *)preferencesController
  323: {
  324:     if (!preferencesController) {
  325:         preferencesController = [[MVPreferencesController sharedInstance] retain];
  326:     }
  327:     return preferencesController;
  328: }
  329: 
  330: - (void)displayPreferencesWindow:sender
  331: {
  332:     [[self preferencesController] showPreferences:nil] ;
  333: }
  334: 
  335: - (IBAction)showAboutBox:(id)sender
  336: {
  337:     [[AboutBox sharedInstance] showPanel:sender];
  338: }
  339: 
  340: - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
  341: {
  342:     NSWindow* keyWindow = [mApplication keyWindow];
  343: 
  344:     if (keyWindow) {
  345:         [[keyWindow windowController] loadURL:[NSURL fileURLWithPath:filename]];
  346:     } else {
  347:         [self openBrowserWindowWithURL:[NSURL fileURLWithPath:filename]];
  348:     }
  349:     
  350:     return YES;
  351:     
  352: }
  353: 
  354: 
  355: @end

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