File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.42: download - view: text, annotated - select for diffs - revision graph
Sun Mar 3 12:53:10 2002 UTC (17 years, 3 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:  *  BrowserWindowController.mm
    3:  */
    4: 
    5: #import "BrowserWindowController.h"
    6: #import "MyBrowserView.h"
    7: 
    8: #include "nsIWebNavigation.h"
    9: 
   10: static NSString *BrowserToolbarIdentifier	= @"Browser Window Toolbar";
   11: static NSString *BackToolbarItemIdentifier	= @"Back Toolbar Item";
   12: static NSString *ForwardToolbarItemIdentifier	= @"Forward Toolbar Item";
   13: static NSString *ReloadToolbarItemIdentifier	= @"Reload Toolbar Item";
   14: static NSString *StopToolbarItemIdentifier	= @"Stop Toolbar Item";
   15: static NSString *HomeToolbarItemIdentifier	= @"Home Toolbar Item";
   16: static NSString *LocationToolbarItemIdentifier	= @"Location Toolbar Item";
   17: static NSString *SidebarToolbarItemIdentifier	= @"Sidebar Toolbar Item";
   18: static NSString *PrintToolbarItemIdentifier	= @"Print Toolbar Item";
   19: 
   20: @interface BrowserWindowController(Private)
   21: - (void)setupToolbar;
   22: @end
   23: 
   24: @implementation BrowserWindowController
   25: 
   26: -(void)enterModalSession
   27: {
   28:     mModalSession = [NSApp beginModalSessionForWindow: [self window]];
   29:     [NSApp runModalSession: mModalSession];
   30:     [NSApp endModalSession: mModalSession];
   31:     mModalSession = nil;
   32: }
   33: 
   34: -(void)mouseMoved:(NSEvent*)aEvent
   35: {
   36:     if (mMoveReentrant)
   37:         return;
   38:         
   39:     mMoveReentrant = YES;
   40:     NSView* view = [[[self window] contentView] hitTest: [aEvent locationInWindow]];
   41:     [view mouseMoved: aEvent];
   42:     [super mouseMoved: aEvent];
   43:     mMoveReentrant = NO;
   44: }
   45: 
   46: - (id)initWithWindowNibName:(NSString *)windowNibName
   47: {
   48:     if ( (self = [super initWithWindowNibName:(NSString *)windowNibName]) ) {
   49:         mInitialized = NO;
   50:         mMoveReentrant = NO;
   51:     }
   52:     return self;
   53: }
   54: 
   55: - (void)windowWillClose:(NSNotification *)notification
   56: {
   57:     printf("Window will close notification.\n");
   58:     [self autorelease];
   59:     [mSidebarBookmarksDataSource windowClosing];
   60: }
   61: 
   62: - (void)dealloc
   63: {
   64:     printf("Browser controller died.\n");
   65: 
   66:     [mBrowserView windowClosed];
   67:     [mSidebarBrowserView windowClosed];
   68:     
   69:     [mProgress release];
   70:       
   71:     [super dealloc];
   72: }
   73: 
   74: - (void)windowDidLoad
   75: {
   76:     [super windowDidLoad];
   77:     
   78:     if (mModalSession)
   79:       [NSApp stopModal: mModalSession];
   80:       
   81:     mInitialized = YES;
   82:     
   83:     // Retain with a single extra refcount.  This allows the MyBrowserViews
   84:     // to remove the progress meter from its superview without having to 
   85:     // worry about retaining and releasing it.
   86:     [mProgress retain];
   87:     
   88:     [[self window] setAcceptsMouseMovedEvents: YES];
   89:     
   90:     [self setupToolbar];
   91: 
   92: //  03/03/2002 mlj Changing strategy a bit here.  The addTab: method was
   93: //	duplicating a lot of the code found here.  I have moved it to that method.
   94: //	We now remove the IB tab, then add one of our own.
   95: 
   96:     [mTabBrowser removeTabViewItem:[mTabBrowser tabViewItemAtIndex:0]];
   97:     [self newTab];
   98:     
   99:     if (mURL) {
  100:       [self loadURL: mURL];
  101:       [mURL release];
  102:     }
  103:     
  104:     [mSidebarDrawer setDelegate: self];
  105:     
  106: //    [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus
  107:   //      progress: mProgress windowController: self];
  108: }
  109: 
  110: - (void)drawerWillOpen: (NSNotification*)aNotification
  111: {
  112:     [mSidebarBookmarksDataSource ensureBookmarks];
  113: }
  114: 
  115: - (void)drawerDidOpen:(NSNotification *)aNotification
  116: {
  117:     // XXXdwh This is temporary.
  118:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
  119: }
  120: 
  121: - (void)drawerDidClose:(NSNotification *)aNotification
  122: {
  123:     // Unload the Gecko web page in "My Panels" to save memory.
  124:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
  125: }
  126: 
  127: - (void)setupToolbar
  128: {
  129:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
  130:     
  131:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
  132:     [toolbar setAllowsUserCustomization:YES];
  133:     [toolbar setAutosavesConfiguration:YES];
  134:     [toolbar setDelegate:self];
  135:     [[self window] setToolbar:toolbar];
  136: }
  137: 
  138: 
  139: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  140: {
  141:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  142:                                         ForwardToolbarItemIdentifier,
  143:                                         ReloadToolbarItemIdentifier,
  144:                                         StopToolbarItemIdentifier,
  145:                                         HomeToolbarItemIdentifier,
  146:                                         LocationToolbarItemIdentifier,
  147:                                         SidebarToolbarItemIdentifier,
  148:                                         PrintToolbarItemIdentifier,
  149:                                         NSToolbarCustomizeToolbarItemIdentifier,
  150:                                         NSToolbarFlexibleSpaceItemIdentifier,
  151:                                         NSToolbarSpaceItemIdentifier,
  152:                                         NSToolbarSeparatorItemIdentifier,
  153:                                         nil];
  154: }
  155: 
  156: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  157: {
  158:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  159:                                         ForwardToolbarItemIdentifier,
  160:                                         ReloadToolbarItemIdentifier,
  161:                                         StopToolbarItemIdentifier,
  162:                                         HomeToolbarItemIdentifier,
  163:                                         LocationToolbarItemIdentifier,
  164:                                         SidebarToolbarItemIdentifier,
  165:                                         nil];
  166: }
  167: 
  168: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  169:       itemForItemIdentifier:(NSString *)itemIdent
  170:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  171: {
  172:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  173:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
  174:         [toolbarItem setLabel:@"Back"];
  175:         [toolbarItem setPaletteLabel:@"Go Back"];
  176:         [toolbarItem setToolTip:@"Go back one page"];
  177:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
  178:         [toolbarItem setTarget:self];
  179:         [toolbarItem setAction:@selector(back:)];
  180:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
  181:         [toolbarItem setLabel:@"Forward"];
  182:         [toolbarItem setPaletteLabel:@"Go Forward"];
  183:         [toolbarItem setToolTip:@"Go forward one page"];
  184:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
  185:         [toolbarItem setTarget:self];
  186:         [toolbarItem setAction:@selector(forward:)];
  187:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  188:         [toolbarItem setLabel:@"Reload"];
  189:         [toolbarItem setPaletteLabel:@"Reload Page"];
  190:         [toolbarItem setToolTip:@"Reload current page"];
  191:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  192:         [toolbarItem setTarget:self];
  193:         [toolbarItem setAction:@selector(reload:)];
  194:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  195:         [toolbarItem setLabel:@"Stop"];
  196:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  197:         [toolbarItem setToolTip:@"Stop loading this page"];
  198:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  199:         [toolbarItem setTarget:self];
  200:         [toolbarItem setAction:@selector(stop:)];
  201:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  202:         [toolbarItem setLabel:@"Home"];
  203:         [toolbarItem setPaletteLabel:@"Go Home"];
  204:         [toolbarItem setToolTip:@"Go to home page"];
  205:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  206:         [toolbarItem setTarget:self];
  207:         [toolbarItem setAction:@selector(home:)];
  208:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  209:         [toolbarItem setLabel:@"Sidebar"];
  210:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  211:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  212:         [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
  213:         [toolbarItem setTarget:self];
  214:         [toolbarItem setAction:@selector(toggleSidebar:)];
  215:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  216:         
  217:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  218:         
  219:         [toolbarItem setLabel:@"Location"];
  220:         [toolbarItem setPaletteLabel:@"Location"];
  221:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  222:         [toolbarItem setView:mLocationToolbarView];
  223:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  224:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  225:         
  226:         [menuFormRep setTarget:self];
  227:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  228:         [menuFormRep setTitle:[toolbarItem label]];
  229:         
  230:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  231:         mLocationToolbarItem = toolbarItem;
  232: 
  233:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
  234:         [toolbarItem setLabel:@"Print"];
  235:         [toolbarItem setPaletteLabel:@"Print"];
  236:         [toolbarItem setToolTip:@"Print this page"];
  237:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
  238:         [toolbarItem setTarget:self];
  239:         [toolbarItem setAction:@selector(printDocument)];
  240:     } else {
  241:         toolbarItem = nil;
  242:     }
  243:         
  244:     return toolbarItem;
  245: }
  246: 
  247: // This method handles the enabling/disabling of the toolbar buttons.
  248: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  249: {
  250:   // Check the action and see if it matches.
  251:   SEL action = [theItem action];
  252:   if (action == @selector(back:))
  253:     return [[mBrowserView getBrowserView] canGoBack];
  254:   else if (action == @selector(forward:))
  255:     return [[mBrowserView getBrowserView] canGoForward];
  256:   else if (action == @selector(reload:))
  257:     return [mBrowserView isBusy] == NO;
  258:   else if (action == @selector(stop:))
  259:     return [mBrowserView isBusy];
  260:   else
  261:     return YES;
  262: }
  263:    
  264: - (void)updateToolbarItems
  265: {
  266:   [[[self window] toolbar] validateVisibleItems];
  267: }
  268: 
  269: - (void)performAppropriateLocationAction
  270: {
  271:     if ( [[[self window] toolbar] isVisible] ) {
  272:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  273:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  274:             [self focusURLBar];
  275:         } else {
  276:             [self beginLocationSheet];
  277:         }
  278:     } else {
  279:         [self beginLocationSheet];
  280:     }
  281: }
  282: 
  283: - (void)focusURLBar
  284: {
  285:     [mURLBar selectText: self];
  286: }
  287: 
  288: - (void)beginLocationSheet
  289: {
  290:     [NSApp beginSheet:	mLocationSheetWindow
  291:        modalForWindow:	[self window]
  292:         modalDelegate:	nil
  293:        didEndSelector:	nil
  294:           contextInfo:	nil];
  295: }
  296: 
  297: - (IBAction)endLocationSheet:(id)sender
  298: {
  299:     [mLocationSheetWindow orderOut:self];
  300:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  301:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  302:     
  303:     // Focus and activate our content area.
  304:     [[mBrowserView getBrowserView] setActive: YES];
  305: }
  306: 
  307: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  308: {
  309:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  310:     
  311:     // Focus and activate our content area.
  312:     [[mBrowserView getBrowserView] setActive: YES];
  313: }
  314: 
  315: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  316: {
  317:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
  318: }
  319: 
  320: - (void)printDocument
  321: {
  322:     [[mBrowserView getBrowserView] printDocument];
  323: }
  324: 
  325: - (void)printPreview
  326: {
  327:     [[mBrowserView getBrowserView] printPreview];
  328: }
  329: 
  330: - (void)findInPage
  331: {
  332:     [[mBrowserView getBrowserView] findInPage];
  333: }
  334: 
  335: - (void)findAgain
  336: {
  337:     [[mBrowserView getBrowserView] findAgain];
  338: }
  339: 
  340: - (void)addBookmark
  341: {
  342:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
  343:     // menu data source going.
  344:     [mSidebarBookmarksDataSource ensureBookmarks];
  345:     [mSidebarBookmarksDataSource addBookmark: self];
  346: }
  347: 
  348: - (IBAction)back:(id)aSender
  349: {
  350:   [[mBrowserView getBrowserView] goBack];
  351: }
  352: 
  353: - (IBAction)forward:(id)aSender
  354: {
  355:   [[mBrowserView getBrowserView] goForward];
  356: }
  357: 
  358: - (IBAction)reload:(id)aSender
  359: {
  360:   [[mBrowserView getBrowserView] reload: 0];
  361: }
  362: 
  363: - (IBAction)stop:(id)aSender
  364: {
  365:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
  366: }
  367: 
  368: - (IBAction)home:(id)aSender
  369: {
  370:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  371: }
  372: 
  373: - (IBAction)toggleSidebar:(id)aSender
  374: {
  375:     NSResponder* resp = [[self window] firstResponder];
  376:     [[self window] makeFirstResponder: nil];
  377:     
  378:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) 	{
  379:         // XXXHack to bypass sidebar crashes.
  380:         [mSidebarDrawer open];
  381:     } else {
  382:         [mSidebarDrawer close];
  383:     }
  384:     
  385:     [[self window] makeFirstResponder: resp];
  386: }
  387: 
  388: -(void)loadURL:(NSURL*)aURL
  389: {
  390:     if (mInitialized) {
  391:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  392:     }
  393:     else {
  394:         mURL = aURL;
  395:         [mURL retain];
  396:     }
  397: }
  398: 
  399: - (void)updateLocationFields:(NSString *)locationString
  400: {
  401: /* //commenting this out because it doesn't work right yet.
  402:     if ( [locationString length] > 30 ) {
  403:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  404:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  405:     } else {
  406:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  407:             [NSString stringWithFormat:@"Location: %@", locationString]];
  408:     }
  409: */
  410: 
  411:     [mURLBar setStringValue:locationString];
  412:     [mLocationSheetURLField setStringValue:locationString];
  413: 
  414:     [[self window] update];
  415:     [[self window] display];
  416: }
  417: 
  418: -(void)newTab
  419: {
  420:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  421:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [[newTab view] frame]] autorelease];
  422: //  MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  423: 
  424:     [newTab setLabel: @"Untitled"];
  425:     [newTab setView: newView];
  426: 
  427:     [mTabBrowser addTabViewItem: newTab];
  428: 
  429:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  430: 
  431:     [mTabBrowser selectLastTabViewItem: self];
  432: 
  433:     if ( [[[self window] toolbar] isVisible] ) {
  434:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  435:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  436:             [self focusURLBar];
  437:         }
  438:     }
  439: }
  440: 
  441: -(void)closeTab
  442: {
  443:     if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
  444:         [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
  445:     }
  446: }
  447: 
  448: - (void)previousTab
  449: {
  450:     [mTabBrowser selectPreviousTabViewItem:self];
  451: }
  452: 
  453: - (void)nextTab
  454: {
  455:     [mTabBrowser selectNextTabViewItem:self];
  456: }
  457: 
  458: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
  459: {
  460:     // Disconnect the old view, if one has been designated.
  461:     // If the window has just been opened, none has been.
  462:     if ( mBrowserView ) {
  463:         [mBrowserView disconnectView];
  464:     }
  465:     // Connect up the new view
  466:     mBrowserView = [aTabViewItem view];
  467:        
  468:     // Make the new view the primary content area.
  469:     [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus
  470:         progress: mProgress windowController: self];
  471: }
  472: 
  473: -(MyBrowserView*)getMyBrowserView
  474: {
  475:     return mBrowserView;
  476: }
  477: @end
  478: 

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