/* * 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"; static NSString *PrintToolbarItemIdentifier = @"Print Toolbar Item"; @interface BrowserWindowController(Private) - (void)setupToolbar; @end @implementation BrowserWindowController - (id)init { mInitialized = NO; return [super init]; } - (void)windowWillClose:(NSNotification *)notification { [self autorelease]; [mSidebarBookmarksDataSource windowClosing]; } - (void)dealloc { printf("Browser controller died.\n"); [mBrowserView windowClosed]; [mSidebarBrowserView windowClosed]; [super dealloc]; } - (void)windowDidLoad { mInitialized = YES; [self setupToolbar]; if (mURL) { [self loadURL: mURL]; [mURL release]; } [mSidebarDrawer setDelegate: self]; // Our initial view is our primary view. Connect it up. NSTabViewItem* tab = [mTabBrowser tabViewItemAtIndex: 0]; [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus progress: mProgress windowController: self]; } - (void)drawerWillOpen: (NSNotification*)aNotification { [mSidebarBookmarksDataSource ensureBookmarks]; } - (void)drawerDidOpen:(NSNotification *)aNotification { // XXXdwh This is temporary. [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone]; } - (void)drawerDidClose:(NSNotification *)aNotification { // Unload the Gecko web page in "My Panels" to save memory. [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone]; } - (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 { return [NSArray arrayWithObjects: BackToolbarItemIdentifier, ForwardToolbarItemIdentifier, ReloadToolbarItemIdentifier, StopToolbarItemIdentifier, HomeToolbarItemIdentifier, LocationToolbarItemIdentifier, SidebarToolbarItemIdentifier, PrintToolbarItemIdentifier, 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(performAppropriateLocationAction)]; [menuFormRep setTitle:[toolbarItem label]]; [toolbarItem setMenuFormRepresentation:menuFormRep]; mLocationToolbarItem = toolbarItem; } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) { [toolbarItem setLabel:@"Print"]; [toolbarItem setPaletteLabel:@"Print"]; [toolbarItem setToolTip:@"Print this page"]; [toolbarItem setImage:[NSImage imageNamed:@"print"]]; [toolbarItem setTarget:self]; [toolbarItem setAction:@selector(printDocument)]; } 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]; } - (void)performAppropriateLocationAction { if ( [[[self window] toolbar] isVisible] ) { if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) || ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) { [self focusURLBar]; } else { [self beginLocationSheet]; } } else { [self beginLocationSheet]; } } - (void)focusURLBar { [mURLBar selectText: self]; } - (void)beginLocationSheet { [NSApp beginSheet: mLocationSheetWindow modalForWindow: [self window] modalDelegate: nil didEndSelector: nil contextInfo: nil]; } - (IBAction)endLocationSheet:(id)sender { [mLocationSheetWindow orderOut:self]; [NSApp endSheet:mLocationSheetWindow returnCode:1]; [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]]; } - (IBAction)goToLocationFromToolbarURLField:(id)sender { [self loadURL:[NSURL URLWithString:[sender stringValue]]]; } - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList { [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList]; } - (void)printDocument { [[mBrowserView getBrowserView] printDocument]; } - (void)printPreview { [[mBrowserView getBrowserView] printPreview]; } - (void)findInPage { [[mBrowserView getBrowserView] findInPage]; } - (void)findAgain { [[mBrowserView getBrowserView] findAgain]; } - (void)addBookmark { // XXXdwh Hack. Just go to the sidebar for now until we get our // menu data source going. [mSidebarBookmarksDataSource ensureBookmarks]; [mSidebarBookmarksDataSource addBookmark: self]; } - (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 { [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone]; } - (IBAction)toggleSidebar:(id)aSender { if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) { [mSidebarDrawer open]; } else { [mSidebarDrawer close]; } } -(void)loadURL:(NSURL*)aURL { if (mInitialized) [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone]; else { mURL = aURL; [mURL retain]; } } - (void)updateLocationFields:(NSString *)locationString { /* //commenting this out because it doesn't work right yet. if ( [locationString length] > 30 ) { [[mLocationToolbarItem menuFormRepresentation] setTitle: [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]]; } else { [[mLocationToolbarItem menuFormRepresentation] setTitle: [NSString stringWithFormat:@"Location: %@", locationString]]; } */ [mURLBar setStringValue:locationString]; [mLocationSheetURLField setStringValue:locationString]; [[self window] update]; [[self window] display]; } -(void)newTab { NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease]; [mTabBrowser addTabViewItem: newTab]; MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease]; [newTab setView: newView]; [newTab setLabel: @"Untitled"]; [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone]; [mTabBrowser selectLastTabViewItem: self]; if ( [[[self window] toolbar] isVisible] ) { if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) || ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) { [self focusURLBar]; } } } - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem { // Disconnect the old view. [mBrowserView disconnectView]; // Connect up the new view mBrowserView = [aTabViewItem view]; // Make the new view the primary content area. [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus progress: mProgress windowController: self]; } @end