/* * BrowserWindowController.mm */ #import "BrowserWindowController.h" #import "MyBrowserView.h" static NSString *BrowserToolbarIdentifier = @"Browser Window Toolbar"; static NSString *BackToolbarItemIdentifier = @"Back Toolbar Item"; static NSString *ForwardToolbarItemIdentifier = @"Forward Toolbar Item"; static NSString *ReloadToolbarItemIdentifier = @"Reload Toolbar Item"; static NSString *StopToolbarItemIdentifier = @"Stop Toolbar Item"; static NSString *HomeToolbarItemIdentifier = @"Home Toolbar Item"; static NSString *LocationToolbarItemIdentifier = @"Location Toolbar Item"; static NSString *SidebarToolbarItemIdentifier = @"Sidebar Toolbar Item"; @interface BrowserWindowController(Private) - (void)setupToolbar; @end @implementation BrowserWindowController - (void)windowDidLoad { [self setupToolbar]; } - (void)setupToolbar { NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease]; [toolbar setDisplayMode:NSToolbarDisplayModeDefault]; [toolbar setAllowsUserCustomization:YES]; [toolbar setAutosavesConfiguration:YES]; [toolbar setDelegate:self]; [[self window] setToolbar:toolbar]; } - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar { NSLog(@"toolbarAllowedItemIdentifiers"); return [NSArray arrayWithObjects: BackToolbarItemIdentifier, ForwardToolbarItemIdentifier, ReloadToolbarItemIdentifier, StopToolbarItemIdentifier, HomeToolbarItemIdentifier, LocationToolbarItemIdentifier, SidebarToolbarItemIdentifier, NSToolbarCustomizeToolbarItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier, NSToolbarSpaceItemIdentifier, NSToolbarSeparatorItemIdentifier, nil]; } - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar { return [NSArray arrayWithObjects: BackToolbarItemIdentifier, ForwardToolbarItemIdentifier, ReloadToolbarItemIdentifier, StopToolbarItemIdentifier, HomeToolbarItemIdentifier, LocationToolbarItemIdentifier, SidebarToolbarItemIdentifier, nil]; } - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdent willBeInsertedIntoToolbar:(BOOL)willBeInserted { NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease]; if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) { [toolbarItem setLabel:@"Back"]; [toolbarItem setPaletteLabel:@"Go Back"]; [toolbarItem setToolTip:@"Go back one page"]; [toolbarItem setImage:[NSImage imageNamed:@"back"]]; [toolbarItem setTarget:self]; [toolbarItem setAction:@selector(back:)]; } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) { [toolbarItem setLabel:@"Forward"]; [toolbarItem setPaletteLabel:@"Go Forward"]; [toolbarItem setToolTip:@"Go forward one page"]; [toolbarItem setImage:[NSImage imageNamed:@"forward"]]; [toolbarItem setTarget:self]; [toolbarItem setAction:@selector(forward:)]; } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) { [toolbarItem setLabel:@"Reload"]; [toolbarItem setPaletteLabel:@"Reload Page"]; [toolbarItem setToolTip:@"Reload current page"]; [toolbarItem setImage:[NSImage imageNamed:@"reload"]]; [toolbarItem setTarget:self]; [toolbarItem setAction:@selector(reload:)]; } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) { [toolbarItem setLabel:@"Stop"]; [toolbarItem setPaletteLabel:@"Stop Loading"]; [toolbarItem setToolTip:@"Stop loading this page"]; [toolbarItem setImage:[NSImage imageNamed:@"stop"]]; [toolbarItem setTarget:self]; [toolbarItem setAction:@selector(stop:)]; } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) { [toolbarItem setLabel:@"Home"]; [toolbarItem setPaletteLabel:@"Go Home"]; [toolbarItem setToolTip:@"Go to home page"]; [toolbarItem setImage:[NSImage imageNamed:@"home"]]; [toolbarItem setTarget:self]; [toolbarItem setAction:@selector(home:)]; } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) { [toolbarItem setLabel:@"Sidebar"]; [toolbarItem setPaletteLabel:@"Toggle Sidebar"]; [toolbarItem setToolTip:@"Show or hide the Sidebar"]; [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]]; [toolbarItem setTarget:self]; [toolbarItem setAction:@selector(toggleSidebar:)]; } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) { NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease]; [toolbarItem setLabel:@"Location"]; [toolbarItem setPaletteLabel:@"Location"]; [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]]; [toolbarItem setView:mLocationToolbarView]; [toolbarItem setMinSize:NSMakeSize(128,32)]; [toolbarItem setMaxSize:NSMakeSize(2560,32)]; [menuFormRep setTarget:self]; [menuFormRep setAction:@selector(displayLocationSheet:)]; [menuFormRep setTitle:[toolbarItem label]]; [toolbarItem setMenuFormRepresentation:menuFormRep]; } else { toolbarItem = nil; } return toolbarItem; } // This method handles the enabling/disabling of the toolbar buttons. - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem { // Check the action and see if it matches. SEL action = [theItem action]; if (action == @selector(back:)) return [[mBrowserView getBrowserView] canGoBack]; else if (action == @selector(forward:)) return [[mBrowserView getBrowserView] canGoForward]; else if (action == @selector(reload:)) return [mBrowserView isBusy] == NO; else if (action == @selector(stop:)) return [mBrowserView isBusy]; else return YES; } -(void)updateToolbarItems { [[[self window] toolbar] validateVisibleItems]; } -(IBAction) back:(id)aSender { [[mBrowserView getBrowserView] goBack]; } -(IBAction) forward:(id)aSender { [[mBrowserView getBrowserView] goForward]; } -(IBAction) reload:(id)aSender { [[mBrowserView getBrowserView] reload: 0]; } -(IBAction) stop:(id)aSender { [[mBrowserView getBrowserView] stop: 0]; } -(IBAction) home:(id)aSender { NSURL* url = [NSURL URLWithString:@"about:blank"]; [[mBrowserView getBrowserView] loadURI:url flags:NSLoadFlagsNone]; } -(IBAction) toggleSidebar:(id)aSender { if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) { [mSidebarDrawer open]; } else { [mSidebarDrawer close]; } } -(void)loadURL:(NSString*)aURL { NSURL* url = [NSURL URLWithString:aURL]; [[mBrowserView getBrowserView] loadURI:url flags:NSLoadFlagsNone]; } @end