File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.8: download - view: text, annotated - select for diffs - revision graph
Mon Feb 4 03:44:35 2002 UTC (17 years, 10 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Saving actually works.  Just need to wire up the progress meter and elapsed time stuff.

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

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