File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.24: download - view: text, annotated - select for diffs - revision graph
Sun Feb 10 01:12:05 2002 UTC (17 years, 10 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
The status bar is hooked up now.  Also adding debugging printfs.

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

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