File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.27: download - view: text, annotated - select for diffs - revision graph
Mon Feb 11 05:47:44 2002 UTC (17 years, 4 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Stubbing out New Tab

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

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