File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.33: download - view: text, annotated - select for diffs - revision graph
Wed Feb 20 01:33:48 2002 UTC (17 years, 4 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Stop the sidebar crash.

    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:     // Our initial view is our primary view.  Connect it up.
   52:     NSTabViewItem* tab = [mTabBrowser tabViewItemAtIndex: 0];
   53:     NSView* view = [tab view];
   54:     
   55:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [view frame]] autorelease];
   56:     [tab setView: newView];
   57:     mBrowserView = newView;
   58:     
   59:     if (mURL) {
   60:       [self loadURL: mURL];
   61:       [mURL release];
   62:     }
   63:     
   64:     [mSidebarDrawer setDelegate: self];
   65:     
   66:     [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus
   67:         progress: mProgress windowController: self];
   68: }
   69: 
   70: - (void)drawerWillOpen: (NSNotification*)aNotification
   71: {
   72:     [mSidebarBookmarksDataSource ensureBookmarks];
   73: }
   74: 
   75: - (void)drawerDidOpen:(NSNotification *)aNotification
   76: {
   77:     // XXXdwh This is temporary.
   78:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
   79: }
   80: 
   81: - (void)drawerDidClose:(NSNotification *)aNotification
   82: {
   83:     // Unload the Gecko web page in "My Panels" to save memory.
   84:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
   85: }
   86: 
   87: - (void)setupToolbar
   88: {
   89:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
   90:     
   91:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
   92:     [toolbar setAllowsUserCustomization:YES];
   93:     [toolbar setAutosavesConfiguration:YES];
   94:     [toolbar setDelegate:self];
   95:     [[self window] setToolbar:toolbar];
   96: }
   97: 
   98: 
   99: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  100: {
  101:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  102:                                         ForwardToolbarItemIdentifier,
  103:                                         ReloadToolbarItemIdentifier,
  104:                                         StopToolbarItemIdentifier,
  105:                                         HomeToolbarItemIdentifier,
  106:                                         LocationToolbarItemIdentifier,
  107:                                         SidebarToolbarItemIdentifier,
  108:                                         PrintToolbarItemIdentifier,
  109:                                         NSToolbarCustomizeToolbarItemIdentifier,
  110:                                         NSToolbarFlexibleSpaceItemIdentifier,
  111:                                         NSToolbarSpaceItemIdentifier,
  112:                                         NSToolbarSeparatorItemIdentifier,
  113:                                         nil];
  114: }
  115: 
  116: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  117: {
  118:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  119:                                         ForwardToolbarItemIdentifier,
  120:                                         ReloadToolbarItemIdentifier,
  121:                                         StopToolbarItemIdentifier,
  122:                                         HomeToolbarItemIdentifier,
  123:                                         LocationToolbarItemIdentifier,
  124:                                         SidebarToolbarItemIdentifier,
  125:                                         nil];
  126: }
  127: 
  128: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  129:       itemForItemIdentifier:(NSString *)itemIdent
  130:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  131: {
  132:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  133:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
  134:         [toolbarItem setLabel:@"Back"];
  135:         [toolbarItem setPaletteLabel:@"Go Back"];
  136:         [toolbarItem setToolTip:@"Go back one page"];
  137:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
  138:         [toolbarItem setTarget:self];
  139:         [toolbarItem setAction:@selector(back:)];
  140:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
  141:         [toolbarItem setLabel:@"Forward"];
  142:         [toolbarItem setPaletteLabel:@"Go Forward"];
  143:         [toolbarItem setToolTip:@"Go forward one page"];
  144:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
  145:         [toolbarItem setTarget:self];
  146:         [toolbarItem setAction:@selector(forward:)];
  147:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  148:         [toolbarItem setLabel:@"Reload"];
  149:         [toolbarItem setPaletteLabel:@"Reload Page"];
  150:         [toolbarItem setToolTip:@"Reload current page"];
  151:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  152:         [toolbarItem setTarget:self];
  153:         [toolbarItem setAction:@selector(reload:)];
  154:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  155:         [toolbarItem setLabel:@"Stop"];
  156:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  157:         [toolbarItem setToolTip:@"Stop loading this page"];
  158:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  159:         [toolbarItem setTarget:self];
  160:         [toolbarItem setAction:@selector(stop:)];
  161:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  162:         [toolbarItem setLabel:@"Home"];
  163:         [toolbarItem setPaletteLabel:@"Go Home"];
  164:         [toolbarItem setToolTip:@"Go to home page"];
  165:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  166:         [toolbarItem setTarget:self];
  167:         [toolbarItem setAction:@selector(home:)];
  168:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  169:         [toolbarItem setLabel:@"Sidebar"];
  170:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  171:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  172:         [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
  173:         [toolbarItem setTarget:self];
  174:         [toolbarItem setAction:@selector(toggleSidebar:)];
  175:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  176:         
  177:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  178:         
  179:         [toolbarItem setLabel:@"Location"];
  180:         [toolbarItem setPaletteLabel:@"Location"];
  181:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  182:         [toolbarItem setView:mLocationToolbarView];
  183:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  184:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  185:         
  186:         [menuFormRep setTarget:self];
  187:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  188:         [menuFormRep setTitle:[toolbarItem label]];
  189:         
  190:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  191:         mLocationToolbarItem = toolbarItem;
  192: 
  193:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
  194:         [toolbarItem setLabel:@"Print"];
  195:         [toolbarItem setPaletteLabel:@"Print"];
  196:         [toolbarItem setToolTip:@"Print this page"];
  197:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
  198:         [toolbarItem setTarget:self];
  199:         [toolbarItem setAction:@selector(printDocument)];
  200:     } else {
  201:         toolbarItem = nil;
  202:     }
  203:         
  204:     return toolbarItem;
  205: }
  206: 
  207: // This method handles the enabling/disabling of the toolbar buttons.
  208: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  209: {
  210:   // Check the action and see if it matches.
  211:   SEL action = [theItem action];
  212:   if (action == @selector(back:))
  213:     return [[mBrowserView getBrowserView] canGoBack];
  214:   else if (action == @selector(forward:))
  215:     return [[mBrowserView getBrowserView] canGoForward];
  216:   else if (action == @selector(reload:))
  217:     return [mBrowserView isBusy] == NO;
  218:   else if (action == @selector(stop:))
  219:     return [mBrowserView isBusy];
  220:   else
  221:     return YES;
  222: }
  223:    
  224: - (void)updateToolbarItems
  225: {
  226:   [[[self window] toolbar] validateVisibleItems];
  227: }
  228: 
  229: - (void)performAppropriateLocationAction
  230: {
  231:     if ( [[[self window] toolbar] isVisible] ) {
  232:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  233:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  234:             [self focusURLBar];
  235:         } else {
  236:             [self beginLocationSheet];
  237:         }
  238:     } else {
  239:         [self beginLocationSheet];
  240:     }
  241: }
  242: 
  243: - (void)focusURLBar
  244: {
  245:     [mURLBar selectText: self];
  246: }
  247: 
  248: - (void)beginLocationSheet
  249: {
  250:     [NSApp beginSheet:	mLocationSheetWindow
  251:        modalForWindow:	[self window]
  252:         modalDelegate:	nil
  253:        didEndSelector:	nil
  254:           contextInfo:	nil];
  255: }
  256: 
  257: - (IBAction)endLocationSheet:(id)sender
  258: {
  259:     [mLocationSheetWindow orderOut:self];
  260:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  261:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  262:     
  263:     // Focus and activate our content area.
  264:     [[mBrowserView getBrowserView] setActive: YES];
  265: }
  266: 
  267: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  268: {
  269:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  270:     
  271:     // Focus and activate our content area.
  272:     [[mBrowserView getBrowserView] setActive: YES];
  273: }
  274: 
  275: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  276: {
  277:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
  278: }
  279: 
  280: - (void)printDocument
  281: {
  282:     [[mBrowserView getBrowserView] printDocument];
  283: }
  284: 
  285: - (void)printPreview
  286: {
  287:     [[mBrowserView getBrowserView] printPreview];
  288: }
  289: 
  290: - (void)findInPage
  291: {
  292:     [[mBrowserView getBrowserView] findInPage];
  293: }
  294: 
  295: - (void)findAgain
  296: {
  297:     [[mBrowserView getBrowserView] findAgain];
  298: }
  299: 
  300: - (void)addBookmark
  301: {
  302:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
  303:     // menu data source going.
  304:     [mSidebarBookmarksDataSource ensureBookmarks];
  305:     [mSidebarBookmarksDataSource addBookmark: self];
  306: }
  307: 
  308: - (IBAction)back:(id)aSender
  309: {
  310:   [[mBrowserView getBrowserView] goBack];
  311: }
  312: 
  313: - (IBAction)forward:(id)aSender
  314: {
  315:   [[mBrowserView getBrowserView] goForward];
  316: }
  317: 
  318: - (IBAction)reload:(id)aSender
  319: {
  320:   [[mBrowserView getBrowserView] reload: 0];
  321: }
  322: 
  323: - (IBAction)stop:(id)aSender
  324: {
  325:   [[mBrowserView getBrowserView] stop: 0];
  326: }
  327: 
  328: - (IBAction)home:(id)aSender
  329: {
  330:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  331: }
  332: 
  333: - (IBAction)toggleSidebar:(id)aSender
  334: {
  335:     // XXXHack to bypass sidebar crashes.
  336:     [[self window] makeFirstResponder: mURLBar];
  337:     
  338:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) 	{
  339:         [mSidebarDrawer open];
  340:     } else {
  341:         [mSidebarDrawer close];
  342:     }
  343: }
  344: 
  345: -(void)loadURL:(NSURL*)aURL
  346: {
  347:     if (mInitialized) {
  348:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  349:     }
  350:     else {
  351:         mURL = aURL;
  352:         [mURL retain];
  353:     }
  354: }
  355: 
  356: - (void)updateLocationFields:(NSString *)locationString
  357: {
  358: /* //commenting this out because it doesn't work right yet.
  359:     if ( [locationString length] > 30 ) {
  360:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  361:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  362:     } else {
  363:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  364:             [NSString stringWithFormat:@"Location: %@", locationString]];
  365:     }
  366: */
  367: 
  368:     [mURLBar setStringValue:locationString];
  369:     [mLocationSheetURLField setStringValue:locationString];
  370: 
  371:     [[self window] update];
  372:     [[self window] display];
  373: }
  374: 
  375: -(void)newTab
  376: {
  377:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  378:     [mTabBrowser addTabViewItem: newTab];
  379:     
  380:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  381:     [newTab setView: newView];
  382:     [newTab setLabel: @"Untitled"];
  383:     
  384:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  385:     
  386:     [mTabBrowser selectLastTabViewItem: self];
  387:     
  388:     if ( [[[self window] toolbar] isVisible] ) {
  389:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  390:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  391:             [self focusURLBar];
  392:         } 
  393:     }
  394:     
  395:     //XXXdwh Do this if we ever support auto-hide.
  396:     //if ([mTabBrowser numberOfTabViewItems] == 2)
  397:     //    [mTabBrowser setTabViewType: NSTopTabsBezelBorder];
  398: }
  399: 
  400: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
  401: {
  402:     // Disconnect the old view.
  403:     [mBrowserView disconnectView];
  404:     
  405:     // Connect up the new view
  406:     mBrowserView = [aTabViewItem view];
  407:        
  408:     // Make the new view the primary content area.
  409:     [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus
  410:         progress: mProgress windowController: self];
  411: }
  412: 
  413: -(MyBrowserView*)getMyBrowserView
  414: {
  415:     return mBrowserView;
  416: }
  417: @end
  418: 

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