File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.13: download - view: text, annotated - select for diffs - revision graph
Tue Feb 5 05:00:16 2002 UTC (17 years, 4 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Impl cut copy paste etc. Some focus issues plague this feature still.

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

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