File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.34: download - view: text, annotated - select for diffs - revision graph
Thu Feb 21 08:59:58 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Make sure Gecko windows get mouse moves.

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

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