File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.17: download - view: text, annotated - select for diffs - revision graph
Tue Feb 5 09:51:56 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Fix leaks.

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

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