File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.21: download - view: text, annotated - select for diffs - revision graph
Sat Feb 9 08:12:33 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Adding bookmarks works.

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

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