File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.36: download - view: text, annotated - select for diffs - revision graph
Fri Feb 22 09:16:48 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Fix sidebar crash some more. :)

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

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