File:  [mozdev] / chimera / MainController.mm
Revision 1.26: download - view: text, annotated - select for diffs - revision graph
Sun Mar 3 12:53:10 2002 UTC (17 years, 8 months ago) by macserv
Branches: MAIN
CVS tags: HEAD
Last checkin for the night.  This makes tabs fully usable.  What's left: Menu item validation code.  This will cover all menu items, but most importantly, it will switch the key shortcut for Close Window between cmd+w and shift+cmd+w and Close Tab between blank and cmd+w.

    1: /*
    2:  *  MainController.mm
    3:  */
    4: 
    5: #import "MainController.h"
    6: #import "BrowserWindowController.h"
    7: #include "BookmarksService.h"
    8: #include "nsCOMPtr.h"
    9: #include "nsIServiceManager.h"
   10: #include "nsIIOService.h"
   11: #include "nsCocoaBrowserService.h"
   12: 
   13: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
   14: 
   15: @implementation MainController
   16: 
   17: -(void)dealloc
   18: {
   19:     [super dealloc];
   20:     printf("Main controller died.\n");
   21: }
   22: 
   23: -(void)awakeFromNib
   24: {
   25:     [self newWindow: self];
   26:     
   27:     [mBookmarksMenu setAutoenablesItems: NO];
   28:     
   29:     mMenuBookmarks = new BookmarksService(nil);
   30:     mMenuBookmarks->AddObserver();
   31:     mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
   32:     BookmarksService::gMainController = self;
   33:     
   34:     // Initialize offline mode.
   35:     mOffline = NO;
   36:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
   37:     if (!ioService)
   38:         return;
   39:     PRBool offline = PR_FALSE;
   40:     ioService->GetOffline(&offline);
   41:     mOffline = offline;
   42:     
   43:     // Set the menu item's text to "Go Online" if we're currently
   44:     // offline.
   45:     if (mOffline)
   46:         [mOfflineMenuItem setTitle: @"Go Online"];
   47: }
   48: 
   49: -(IBAction)newWindow:(id)aSender
   50: {
   51:     BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
   52:     [controller focusURLBar];
   53: }
   54: 
   55: -(IBAction)newTab:(id)aSender
   56: {
   57:     NSWindow* keyWindow = [mApplication keyWindow];
   58:     if (keyWindow)
   59:         [[keyWindow windowController] newTab];
   60: }
   61: 
   62: -(IBAction)closeTab:(id)aSender
   63: {
   64:     NSWindow* keyWindow = [mApplication keyWindow];
   65:     if (keyWindow)
   66:         [[keyWindow windowController] closeTab];
   67: }
   68: 
   69: -(IBAction) previousTab:(id)aSender
   70: {
   71:     NSWindow* keyWindow = [mApplication keyWindow];
   72:     if (keyWindow)
   73:         [[keyWindow windowController] previousTab];
   74: }
   75: 
   76: -(IBAction) nextTab:(id)aSender;
   77: {
   78:     NSWindow* keyWindow = [mApplication keyWindow];
   79:     if (keyWindow)
   80:         [[keyWindow windowController] nextTab];
   81: }
   82: 
   83: -(IBAction) openFile:(id)aSender
   84: {
   85:     NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
   86:     [openPanel setCanChooseFiles: YES];
   87:     [openPanel setCanChooseDirectories: NO];
   88:     [openPanel setAllowsMultipleSelection: NO];
   89:     NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
   90:                                                 @"txt",@"text",
   91:                                                 @"gif",@"jpg",@"jpeg",@"png",@"bmp",
   92:                                                 nil];
   93:     int result = [openPanel runModalForTypes: array];
   94:     if (result == NSOKButton) {
   95:         NSArray* urlArray = [openPanel URLs];
   96:         if ([urlArray count] == 0)
   97:             return;
   98:         NSURL* url = [urlArray objectAtIndex: 0];
   99:         NSWindow* keyWindow = [mApplication keyWindow];
  100:         if (keyWindow)
  101:             return [[keyWindow windowController] loadURL: url];
  102:         else
  103:             [self openBrowserWindowWithURL: url];
  104:     }
  105: }
  106: 
  107: -(IBAction) openLocation:(id)aSender
  108: {
  109:     NSWindow* keyWindow = [mApplication keyWindow];
  110:     if (!keyWindow) {
  111:       [self openBrowserWindowWithURLString: @"about:blank"];
  112:       keyWindow = [mApplication keyWindow];
  113:     }
  114:     
  115:     [[keyWindow windowController] performAppropriateLocationAction];
  116: }
  117: 
  118: -(IBAction) savePage:(id)aSender
  119: {
  120:     NSWindow* keyWindow = [mApplication keyWindow];
  121:     if (keyWindow)
  122:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
  123: }
  124: 
  125: -(IBAction) printPage:(id)aSender
  126: {
  127:     NSWindow* keyWindow = [mApplication keyWindow];
  128:     if (keyWindow)
  129:       [[keyWindow windowController] printDocument];
  130: }
  131: 
  132: -(IBAction) printPreview:(id)aSender
  133: {
  134:     NSWindow* keyWindow = [mApplication keyWindow];
  135:     if (keyWindow)
  136:       [[keyWindow windowController] printPreview];
  137: }
  138: 
  139: -(IBAction) toggleOfflineMode:(id)aSender
  140: {
  141:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  142:     if (!ioService)
  143:         return;
  144:     PRBool offline = PR_FALSE;
  145:     ioService->GetOffline(&offline);
  146:     ioService->SetOffline(!offline);
  147:     mOffline = !offline;
  148:     
  149:     // Update the menu item text.
  150:     // Set the menu item's text to "Go Online" if we're currently
  151:     // offline.
  152:     if (mOffline)
  153:         [mOfflineMenuItem setTitle: @"Go Online"];
  154:     else
  155:         [mOfflineMenuItem setTitle: @"Work Offline"];
  156:         
  157:     // Indicate that we are working offline.
  158:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
  159: }
  160: 
  161: // Edit menu actions.
  162: -(IBAction) findInPage:(id)aSender
  163: {
  164:     NSWindow* keyWindow = [mApplication keyWindow];
  165:     if (keyWindow)
  166:       [[keyWindow windowController] findInPage];
  167: }
  168: 
  169: -(IBAction) findAgain:(id)aSender
  170: {
  171:     NSWindow* keyWindow = [mApplication keyWindow];
  172:     if (keyWindow)
  173:       [[keyWindow windowController] findAgain];
  174: }
  175: 
  176: -(IBAction) goBack:(id)aSender
  177: {
  178:     [[[mApplication keyWindow] windowController] back: aSender]; 
  179: }
  180: 
  181: -(IBAction) goForward:(id)aSender
  182: {
  183:     [[[mApplication keyWindow] windowController] forward: aSender]; 
  184: }
  185: 
  186: -(IBAction) doReload:(id)aSender
  187: {
  188:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender]; 
  189: }
  190: 
  191: -(IBAction) doStop:(id)aSender
  192: {
  193:     [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender]; 
  194: }
  195: 
  196: -(IBAction) goHome:(id)aSender
  197: {
  198:     [[[mApplication keyWindow] windowController] home: aSender];
  199: }
  200: 
  201: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
  202: {
  203:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
  204: }
  205: 
  206: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
  207: {
  208: 	BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  209:     [browser loadURL: aURL];
  210: 	[browser showWindow: self];
  211:     return browser;
  212: }
  213: 
  214: -(void)applicationWillTerminate: (NSNotification*)aNotification
  215: {
  216:     printf("Termination notification.\n");
  217:     mMenuBookmarks->RemoveObserver();
  218:     delete mMenuBookmarks;
  219:     mMenuBookmarks = nsnull;
  220:     nsCocoaBrowserService::TermEmbedding();
  221: }
  222: 
  223: // Bookmarks menu actions.
  224: -(IBAction) addBookmark:(id)aSender
  225: {
  226:     NSWindow* keyWindow = [mApplication keyWindow];
  227:     if (keyWindow)
  228:       [[keyWindow windowController] addBookmark];
  229: }
  230: 
  231: -(IBAction) openMenuBookmark:(id)aSender
  232: {
  233:     NSWindow* keyWindow = [mApplication keyWindow];
  234:     if (!keyWindow) {
  235:         [self openBrowserWindowWithURLString: @"about:blank"];
  236:         keyWindow = [mApplication keyWindow];
  237:     }
  238: 
  239:     BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
  240: }
  241: 
  242: - (MVPreferencesController *)preferencesController
  243: {
  244:     if (!preferencesController) {
  245:         preferencesController = [[MVPreferencesController sharedInstance] retain];
  246:     }
  247:     return preferencesController;
  248: }
  249: 
  250: - (void)displayPreferencesWindow:sender
  251: {
  252:     [[self preferencesController] showPreferences:nil] ;
  253: }
  254: 
  255: 
  256: @end

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