File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.9: download - view: text, annotated - select for diffs - revision graph
Mon Feb 4 03:53:59 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Plug browser controller leak.

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

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