File:  [mozdev] / chimera / MainController.mm
Revision 1.20: download - view: text, annotated - select for diffs - revision graph
Sun Feb 10 00:29:08 2002 UTC (17 years, 8 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Bookmarks in the menu bar work!

    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)awakeFromNib
   18: {
   19:     [self newWindow: self];
   20:     
   21:     [mBookmarksMenu setAutoenablesItems: NO];
   22:     
   23:     mMenuBookmarks = new BookmarksService(nil);
   24:     mMenuBookmarks->AddObserver();
   25:     mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
   26:     BookmarksService::gMainController = self;
   27:     
   28:     // Initialize offline mode.
   29:     mOffline = NO;
   30:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
   31:     if (!ioService)
   32:         return;
   33:     PRBool offline = PR_FALSE;
   34:     ioService->GetOffline(&offline);
   35:     mOffline = offline;
   36:     
   37:     // Set the menu item's text to "Go Online" if we're currently
   38:     // offline.
   39:     if (mOffline)
   40:         [mOfflineMenuItem setTitle: @"Go Online"];
   41: }
   42: 
   43: -(IBAction)newWindow:(id)aSender
   44: {
   45:     BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
   46:     [controller focusURLBar];
   47: }
   48: 
   49: -(IBAction) openFile:(id)aSender
   50: {
   51:     NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
   52:     [openPanel setCanChooseFiles: YES];
   53:     [openPanel setCanChooseDirectories: NO];
   54:     [openPanel setAllowsMultipleSelection: NO];
   55:     NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
   56:                                                 @"txt",@"text",
   57:                                                 @"gif",@"jpg",@"jpeg",@"png",@"bmp",
   58:                                                 nil];
   59:     int result = [openPanel runModalForTypes: array];
   60:     if (result == NSOKButton) {
   61:         NSArray* urlArray = [openPanel URLs];
   62:         if ([urlArray count] == 0)
   63:             return;
   64:         NSURL* url = [urlArray objectAtIndex: 0];
   65:         NSWindow* keyWindow = [mApplication keyWindow];
   66:         if (keyWindow)
   67:             return [[keyWindow windowController] loadURL: url];
   68:         else
   69:             [self openBrowserWindowWithURL: url];
   70:     }
   71: }
   72: 
   73: -(IBAction) openLocation:(id)aSender
   74: {
   75:     NSWindow* keyWindow = [mApplication keyWindow];
   76:     if (!keyWindow) {
   77:       [self openBrowserWindowWithURLString: @"about:blank"];
   78:       keyWindow = [mApplication keyWindow];
   79:     }
   80:     
   81:     [[keyWindow windowController] performAppropriateLocationAction];
   82: }
   83: 
   84: -(IBAction) savePage:(id)aSender
   85: {
   86:     NSWindow* keyWindow = [mApplication keyWindow];
   87:     if (keyWindow)
   88:       [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
   89: }
   90: 
   91: -(IBAction) printPage:(id)aSender
   92: {
   93:     NSWindow* keyWindow = [mApplication keyWindow];
   94:     if (keyWindow)
   95:       [[keyWindow windowController] printDocument];
   96: }
   97: 
   98: -(IBAction) printPreview:(id)aSender
   99: {
  100:     NSWindow* keyWindow = [mApplication keyWindow];
  101:     if (keyWindow)
  102:       [[keyWindow windowController] printPreview];
  103: }
  104: 
  105: -(IBAction) toggleOfflineMode:(id)aSender
  106: {
  107:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  108:     if (!ioService)
  109:         return;
  110:     PRBool offline = PR_FALSE;
  111:     ioService->GetOffline(&offline);
  112:     ioService->SetOffline(!offline);
  113:     mOffline = !offline;
  114:     
  115:     // Update the menu item text.
  116:     // Set the menu item's text to "Go Online" if we're currently
  117:     // offline.
  118:     if (mOffline)
  119:         [mOfflineMenuItem setTitle: @"Go Online"];
  120:     else
  121:         [mOfflineMenuItem setTitle: @"Work Offline"];
  122:         
  123:     // Indicate that we are working offline.
  124:     [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
  125: }
  126: 
  127: // Edit menu actions.
  128: -(IBAction) findInPage:(id)aSender
  129: {
  130:     NSWindow* keyWindow = [mApplication keyWindow];
  131:     if (keyWindow)
  132:       [[keyWindow windowController] findInPage];
  133: }
  134: 
  135: -(IBAction) findAgain:(id)aSender
  136: {
  137:     NSWindow* keyWindow = [mApplication keyWindow];
  138:     if (keyWindow)
  139:       [[keyWindow windowController] findAgain];
  140: }
  141: 
  142: -(IBAction) back:(id)aSender
  143: {
  144:     [[[mApplication keyWindow] windowController] back: aSender]; 
  145: }
  146: 
  147: -(IBAction) forward:(id)aSender
  148: {
  149:     [[[mApplication keyWindow] windowController] forward: aSender]; 
  150: }
  151: 
  152: -(IBAction) reload:(id)aSender
  153: {
  154:     [[[mApplication keyWindow] windowController] reload: aSender]; 
  155: }
  156: 
  157: -(IBAction) stop:(id)aSender
  158: {
  159:     [[[mApplication keyWindow] windowController] stop: aSender]; 
  160: }
  161: 
  162: -(IBAction) home:(id)aSender
  163: {
  164:     [[[mApplication keyWindow] windowController] home: aSender];
  165: }
  166: 
  167: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
  168: {
  169:     return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
  170: }
  171: 
  172: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
  173: {
  174: 	BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  175:     [browser loadURL: aURL];
  176: 	[browser showWindow: self];
  177:     return browser;
  178: }
  179: 
  180: -(void)applicationWillTerminate: (NSNotification*)aNotification
  181: {
  182:     mMenuBookmarks->RemoveObserver();
  183:     delete mMenuBookmarks;
  184:     nsCocoaBrowserService::TermEmbedding();
  185: }
  186: 
  187: // Bookmarks menu actions.
  188: -(IBAction) addBookmark:(id)aSender
  189: {
  190:     NSWindow* keyWindow = [mApplication keyWindow];
  191:     if (keyWindow)
  192:       [[keyWindow windowController] addBookmark];
  193: }
  194: 
  195: -(IBAction) openMenuBookmark:(id)aSender
  196: {
  197:     NSWindow* keyWindow = [mApplication keyWindow];
  198:     if (!keyWindow) {
  199:         [self openBrowserWindowWithURLString: @"about:blank"];
  200:         keyWindow = [mApplication keyWindow];
  201:     }
  202: 
  203:     BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
  204: }
  205: @end

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