File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.31: download - view: text, annotated - select for diffs - revision graph
Tue Feb 12 01:47:57 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Lots of tweaks and cleanup.

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

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