File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.18: download - view: text, annotated - select for diffs - revision graph
Thu Feb 7 05:11:38 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Checking in a prototype sidebar.

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

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