File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Thu Jan 31 05:45:26 2002 UTC (17 years, 10 months ago) by macserv
Branches: MAIN
CVS tags: HEAD
Chimera: Tons of updates.  Have fun merging this.

    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: 
   17: @interface BrowserWindowController(Private)
   18: - (void)setupToolbar;
   19: @end
   20: 
   21: @implementation BrowserWindowController
   22: 
   23: - (id)init 
   24: {
   25:     mInitialized = NO;
   26:     return [super init];
   27: }
   28: 
   29: - (void)windowDidLoad
   30: {
   31:     mInitialized = YES;
   32:     [self setupToolbar];
   33:     
   34:     if (mURL) {
   35:       [self loadURL: mURL];
   36:       [mURL release];
   37:     }
   38: }
   39: 
   40: - (void)setupToolbar
   41: {
   42:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
   43:     
   44:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
   45:     [toolbar setAllowsUserCustomization:YES];
   46:     [toolbar setAutosavesConfiguration:YES];
   47:     [toolbar setDelegate:self];
   48:     [[self window] setToolbar:toolbar];
   49: }
   50: 
   51: 
   52: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
   53: {
   54:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
   55:                                         ForwardToolbarItemIdentifier,
   56:                                         ReloadToolbarItemIdentifier,
   57:                                         StopToolbarItemIdentifier,
   58:                                         HomeToolbarItemIdentifier,
   59:                                         LocationToolbarItemIdentifier,
   60:                                         SidebarToolbarItemIdentifier,
   61:                                         NSToolbarCustomizeToolbarItemIdentifier,
   62:                                         NSToolbarFlexibleSpaceItemIdentifier,
   63:                                         NSToolbarSpaceItemIdentifier,
   64:                                         NSToolbarSeparatorItemIdentifier,
   65:                                         nil];
   66: }
   67: 
   68: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
   69: {
   70:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
   71:                                         ForwardToolbarItemIdentifier,
   72:                                         ReloadToolbarItemIdentifier,
   73:                                         StopToolbarItemIdentifier,
   74:                                         HomeToolbarItemIdentifier,
   75:                                         LocationToolbarItemIdentifier,
   76:                                         SidebarToolbarItemIdentifier,
   77:                                         nil];
   78: }
   79: 
   80: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
   81:       itemForItemIdentifier:(NSString *)itemIdent
   82:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
   83: {
   84:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
   85:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
   86:         [toolbarItem setLabel:@"Back"];
   87:         [toolbarItem setPaletteLabel:@"Go Back"];
   88:         [toolbarItem setToolTip:@"Go back one page"];
   89:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
   90:         [toolbarItem setTarget:self];
   91:         [toolbarItem setAction:@selector(back:)];
   92:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
   93:         [toolbarItem setLabel:@"Forward"];
   94:         [toolbarItem setPaletteLabel:@"Go Forward"];
   95:         [toolbarItem setToolTip:@"Go forward one page"];
   96:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
   97:         [toolbarItem setTarget:self];
   98:         [toolbarItem setAction:@selector(forward:)];
   99:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  100:         [toolbarItem setLabel:@"Reload"];
  101:         [toolbarItem setPaletteLabel:@"Reload Page"];
  102:         [toolbarItem setToolTip:@"Reload current page"];
  103:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  104:         [toolbarItem setTarget:self];
  105:         [toolbarItem setAction:@selector(reload:)];
  106:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  107:         [toolbarItem setLabel:@"Stop"];
  108:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  109:         [toolbarItem setToolTip:@"Stop loading this page"];
  110:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  111:         [toolbarItem setTarget:self];
  112:         [toolbarItem setAction:@selector(stop:)];
  113:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  114:         [toolbarItem setLabel:@"Home"];
  115:         [toolbarItem setPaletteLabel:@"Go Home"];
  116:         [toolbarItem setToolTip:@"Go to home page"];
  117:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  118:         [toolbarItem setTarget:self];
  119:         [toolbarItem setAction:@selector(home:)];
  120:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  121:         [toolbarItem setLabel:@"Sidebar"];
  122:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  123:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  124:         [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
  125:         [toolbarItem setTarget:self];
  126:         [toolbarItem setAction:@selector(toggleSidebar:)];
  127:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  128:         
  129:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  130:         
  131:         [toolbarItem setLabel:@"Location"];
  132:         [toolbarItem setPaletteLabel:@"Location"];
  133:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  134:         [toolbarItem setView:mLocationToolbarView];
  135:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  136:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  137:         
  138:         [menuFormRep setTarget:self];
  139:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  140:         [menuFormRep setTitle:[toolbarItem label]];
  141:         
  142:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  143:         mLocationToolbarItem = toolbarItem;
  144:         
  145:     } else {
  146:         toolbarItem = nil;
  147:     }
  148:         
  149:     return toolbarItem;
  150: }
  151: 
  152: // This method handles the enabling/disabling of the toolbar buttons.
  153: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  154: {
  155:   // Check the action and see if it matches.
  156:   SEL action = [theItem action];
  157:   if (action == @selector(back:))
  158:     return [[mBrowserView getBrowserView] canGoBack];
  159:   else if (action == @selector(forward:))
  160:     return [[mBrowserView getBrowserView] canGoForward];
  161:   else if (action == @selector(reload:))
  162:     return [mBrowserView isBusy] == NO;
  163:   else if (action == @selector(stop:))
  164:     return [mBrowserView isBusy];
  165:   else
  166:     return YES;
  167: }
  168:    
  169: - (void)updateToolbarItems
  170: {
  171:   [[[self window] toolbar] validateVisibleItems];
  172: }
  173: 
  174: - (void)performAppropriateLocationAction
  175: {
  176:     if ( [[[self window] toolbar] isVisible] ) {
  177:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  178:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  179:             [self focusURLBar];
  180:         } else {
  181:             [self beginLocationSheet];
  182:         }
  183:     } else {
  184:         [self beginLocationSheet];
  185:     }
  186: }
  187: 
  188: - (void)focusURLBar
  189: {
  190:     [mURLBar selectText: self];
  191: }
  192: 
  193: - (void)beginLocationSheet
  194: {
  195:     [NSApp beginSheet:	mLocationSheetWindow
  196:        modalForWindow:	[self window]
  197:         modalDelegate:	nil
  198:        didEndSelector:	nil
  199:           contextInfo:	nil];
  200: }
  201: 
  202: - (IBAction)endLocationSheet:(id)sender
  203: {
  204:     [mLocationSheetWindow orderOut:self];
  205:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  206:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  207: }
  208: 
  209: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  210: {
  211:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  212: }
  213: 
  214: - (IBAction)back:(id)aSender
  215: {
  216:   [[mBrowserView getBrowserView] goBack];
  217: }
  218: 
  219: - (IBAction)forward:(id)aSender
  220: {
  221:   [[mBrowserView getBrowserView] goForward];
  222: }
  223: 
  224: - (IBAction)reload:(id)aSender
  225: {
  226:   [[mBrowserView getBrowserView] reload: 0];
  227: }
  228: 
  229: - (IBAction)stop:(id)aSender
  230: {
  231:   [[mBrowserView getBrowserView] stop: 0];
  232: }
  233: 
  234: - (IBAction)home:(id)aSender
  235: {
  236:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  237: }
  238: 
  239: - (IBAction)toggleSidebar:(id)aSender
  240: {
  241:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) {
  242:         [mSidebarDrawer open];
  243:     } else {
  244:         [mSidebarDrawer close];
  245:     }
  246: }
  247: 
  248: -(void)loadURL:(NSURL*)aURL
  249: {
  250:     if (mInitialized)
  251:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  252:     else {
  253:         mURL = aURL;
  254:         [mURL retain];
  255:     }
  256: }
  257: 
  258: - (void)updateLocationFields:(NSString *)locationString
  259: {
  260: /* //commenting this out because it doesn't work right yet.
  261:     if ( [locationString length] > 30 ) {
  262:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  263:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  264:     } else {
  265:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  266:             [NSString stringWithFormat:@"Location: %@", locationString]];
  267:     }
  268: */
  269: 
  270:     [mURLBar setStringValue:locationString];
  271:     [mLocationSheetURLField setStringValue:locationString];
  272: 
  273:     [[self window] update];
  274:     [[self window] display];
  275: }
  276: 
  277: @end
  278: 

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