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

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