File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.37: download - view: text, annotated - select for diffs - revision graph
Sun Feb 24 04:15:52 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Fixed alerts. Made stop work.  Fixed paste.

    1: /*
    2:  *  BrowserWindowController.mm
    3:  */
    4: 
    5: #import "BrowserWindowController.h"
    6: #import "MyBrowserView.h"
    7: 
    8: #include "nsIWebNavigation.h"
    9: 
   10: static NSString *BrowserToolbarIdentifier	= @"Browser Window Toolbar";
   11: static NSString *BackToolbarItemIdentifier	= @"Back Toolbar Item";
   12: static NSString *ForwardToolbarItemIdentifier	= @"Forward Toolbar Item";
   13: static NSString *ReloadToolbarItemIdentifier	= @"Reload Toolbar Item";
   14: static NSString *StopToolbarItemIdentifier	= @"Stop Toolbar Item";
   15: static NSString *HomeToolbarItemIdentifier	= @"Home Toolbar Item";
   16: static NSString *LocationToolbarItemIdentifier	= @"Location Toolbar Item";
   17: static NSString *SidebarToolbarItemIdentifier	= @"Sidebar Toolbar Item";
   18: static NSString *PrintToolbarItemIdentifier	= @"Print Toolbar Item";
   19: 
   20: @interface BrowserWindowController(Private)
   21: - (void)setupToolbar;
   22: @end
   23: 
   24: @implementation BrowserWindowController
   25: 
   26: -(void)mouseMoved:(NSEvent*)aEvent
   27: {
   28:     if (mMoveReentrant)
   29:         return;
   30:         
   31:     mMoveReentrant = YES;
   32:     NSView* view = [[[self window] contentView] hitTest: [aEvent locationInWindow]];
   33:     [view mouseMoved: aEvent];
   34:     [super mouseMoved: aEvent];
   35:     mMoveReentrant = NO;
   36: }
   37: 
   38: - (id)init 
   39: {
   40:     mInitialized = NO;
   41:     mMoveReentrant = NO;
   42:     return [super init];
   43: }
   44: 
   45: - (void)windowWillClose:(NSNotification *)notification
   46: {
   47:     [self autorelease];
   48:     [mSidebarBookmarksDataSource windowClosing];
   49: }
   50: 
   51: - (void)dealloc
   52: {
   53:     printf("Browser controller died.\n");
   54:     
   55:     [mBrowserView windowClosed];
   56:     [mSidebarBrowserView windowClosed];
   57:     
   58:     [super dealloc];
   59: }
   60: 
   61: - (void)windowDidLoad
   62: {
   63:     [super windowDidLoad];
   64:     
   65:     mInitialized = YES;
   66:     [[self window] setAcceptsMouseMovedEvents: YES];
   67:     
   68:     [self setupToolbar];
   69: 
   70:     // Our initial view is our primary view.  Connect it up.
   71:     NSTabViewItem* tab = [mTabBrowser tabViewItemAtIndex: 0];
   72:     NSView* view = [tab view];
   73:     
   74:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [view frame]] autorelease];
   75:     [tab setView: newView];
   76:     mBrowserView = newView;
   77:     
   78:     if (mURL) {
   79:       [self loadURL: mURL];
   80:       [mURL release];
   81:     }
   82:     
   83:     [mSidebarDrawer setDelegate: self];
   84:     
   85:     [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus
   86:         progress: mProgress windowController: self];
   87: }
   88: 
   89: - (void)drawerWillOpen: (NSNotification*)aNotification
   90: {
   91:     [mSidebarBookmarksDataSource ensureBookmarks];
   92: }
   93: 
   94: - (void)drawerDidOpen:(NSNotification *)aNotification
   95: {
   96:     // XXXdwh This is temporary.
   97:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
   98: }
   99: 
  100: - (void)drawerDidClose:(NSNotification *)aNotification
  101: {
  102:     // Unload the Gecko web page in "My Panels" to save memory.
  103:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
  104: }
  105: 
  106: - (void)setupToolbar
  107: {
  108:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
  109:     
  110:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
  111:     [toolbar setAllowsUserCustomization:YES];
  112:     [toolbar setAutosavesConfiguration:YES];
  113:     [toolbar setDelegate:self];
  114:     [[self window] setToolbar:toolbar];
  115: }
  116: 
  117: 
  118: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  119: {
  120:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  121:                                         ForwardToolbarItemIdentifier,
  122:                                         ReloadToolbarItemIdentifier,
  123:                                         StopToolbarItemIdentifier,
  124:                                         HomeToolbarItemIdentifier,
  125:                                         LocationToolbarItemIdentifier,
  126:                                         SidebarToolbarItemIdentifier,
  127:                                         PrintToolbarItemIdentifier,
  128:                                         NSToolbarCustomizeToolbarItemIdentifier,
  129:                                         NSToolbarFlexibleSpaceItemIdentifier,
  130:                                         NSToolbarSpaceItemIdentifier,
  131:                                         NSToolbarSeparatorItemIdentifier,
  132:                                         nil];
  133: }
  134: 
  135: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  136: {
  137:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  138:                                         ForwardToolbarItemIdentifier,
  139:                                         ReloadToolbarItemIdentifier,
  140:                                         StopToolbarItemIdentifier,
  141:                                         HomeToolbarItemIdentifier,
  142:                                         LocationToolbarItemIdentifier,
  143:                                         SidebarToolbarItemIdentifier,
  144:                                         nil];
  145: }
  146: 
  147: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  148:       itemForItemIdentifier:(NSString *)itemIdent
  149:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  150: {
  151:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  152:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
  153:         [toolbarItem setLabel:@"Back"];
  154:         [toolbarItem setPaletteLabel:@"Go Back"];
  155:         [toolbarItem setToolTip:@"Go back one page"];
  156:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
  157:         [toolbarItem setTarget:self];
  158:         [toolbarItem setAction:@selector(back:)];
  159:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
  160:         [toolbarItem setLabel:@"Forward"];
  161:         [toolbarItem setPaletteLabel:@"Go Forward"];
  162:         [toolbarItem setToolTip:@"Go forward one page"];
  163:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
  164:         [toolbarItem setTarget:self];
  165:         [toolbarItem setAction:@selector(forward:)];
  166:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  167:         [toolbarItem setLabel:@"Reload"];
  168:         [toolbarItem setPaletteLabel:@"Reload Page"];
  169:         [toolbarItem setToolTip:@"Reload current page"];
  170:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  171:         [toolbarItem setTarget:self];
  172:         [toolbarItem setAction:@selector(reload:)];
  173:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  174:         [toolbarItem setLabel:@"Stop"];
  175:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  176:         [toolbarItem setToolTip:@"Stop loading this page"];
  177:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  178:         [toolbarItem setTarget:self];
  179:         [toolbarItem setAction:@selector(stop:)];
  180:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  181:         [toolbarItem setLabel:@"Home"];
  182:         [toolbarItem setPaletteLabel:@"Go Home"];
  183:         [toolbarItem setToolTip:@"Go to home page"];
  184:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  185:         [toolbarItem setTarget:self];
  186:         [toolbarItem setAction:@selector(home:)];
  187:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  188:         [toolbarItem setLabel:@"Sidebar"];
  189:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  190:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  191:         [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
  192:         [toolbarItem setTarget:self];
  193:         [toolbarItem setAction:@selector(toggleSidebar:)];
  194:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  195:         
  196:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  197:         
  198:         [toolbarItem setLabel:@"Location"];
  199:         [toolbarItem setPaletteLabel:@"Location"];
  200:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  201:         [toolbarItem setView:mLocationToolbarView];
  202:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  203:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  204:         
  205:         [menuFormRep setTarget:self];
  206:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  207:         [menuFormRep setTitle:[toolbarItem label]];
  208:         
  209:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  210:         mLocationToolbarItem = toolbarItem;
  211: 
  212:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
  213:         [toolbarItem setLabel:@"Print"];
  214:         [toolbarItem setPaletteLabel:@"Print"];
  215:         [toolbarItem setToolTip:@"Print this page"];
  216:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
  217:         [toolbarItem setTarget:self];
  218:         [toolbarItem setAction:@selector(printDocument)];
  219:     } else {
  220:         toolbarItem = nil;
  221:     }
  222:         
  223:     return toolbarItem;
  224: }
  225: 
  226: // This method handles the enabling/disabling of the toolbar buttons.
  227: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  228: {
  229:   // Check the action and see if it matches.
  230:   SEL action = [theItem action];
  231:   if (action == @selector(back:))
  232:     return [[mBrowserView getBrowserView] canGoBack];
  233:   else if (action == @selector(forward:))
  234:     return [[mBrowserView getBrowserView] canGoForward];
  235:   else if (action == @selector(reload:))
  236:     return [mBrowserView isBusy] == NO;
  237:   else if (action == @selector(stop:))
  238:     return [mBrowserView isBusy];
  239:   else
  240:     return YES;
  241: }
  242:    
  243: - (void)updateToolbarItems
  244: {
  245:   [[[self window] toolbar] validateVisibleItems];
  246: }
  247: 
  248: - (void)performAppropriateLocationAction
  249: {
  250:     if ( [[[self window] toolbar] isVisible] ) {
  251:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  252:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  253:             [self focusURLBar];
  254:         } else {
  255:             [self beginLocationSheet];
  256:         }
  257:     } else {
  258:         [self beginLocationSheet];
  259:     }
  260: }
  261: 
  262: - (void)focusURLBar
  263: {
  264:     [mURLBar selectText: self];
  265: }
  266: 
  267: - (void)beginLocationSheet
  268: {
  269:     [NSApp beginSheet:	mLocationSheetWindow
  270:        modalForWindow:	[self window]
  271:         modalDelegate:	nil
  272:        didEndSelector:	nil
  273:           contextInfo:	nil];
  274: }
  275: 
  276: - (IBAction)endLocationSheet:(id)sender
  277: {
  278:     [mLocationSheetWindow orderOut:self];
  279:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  280:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  281:     
  282:     // Focus and activate our content area.
  283:     [[mBrowserView getBrowserView] setActive: YES];
  284: }
  285: 
  286: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  287: {
  288:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  289:     
  290:     // Focus and activate our content area.
  291:     [[mBrowserView getBrowserView] setActive: YES];
  292: }
  293: 
  294: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  295: {
  296:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
  297: }
  298: 
  299: - (void)printDocument
  300: {
  301:     [[mBrowserView getBrowserView] printDocument];
  302: }
  303: 
  304: - (void)printPreview
  305: {
  306:     [[mBrowserView getBrowserView] printPreview];
  307: }
  308: 
  309: - (void)findInPage
  310: {
  311:     [[mBrowserView getBrowserView] findInPage];
  312: }
  313: 
  314: - (void)findAgain
  315: {
  316:     [[mBrowserView getBrowserView] findAgain];
  317: }
  318: 
  319: - (void)addBookmark
  320: {
  321:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
  322:     // menu data source going.
  323:     [mSidebarBookmarksDataSource ensureBookmarks];
  324:     [mSidebarBookmarksDataSource addBookmark: self];
  325: }
  326: 
  327: - (IBAction)back:(id)aSender
  328: {
  329:   [[mBrowserView getBrowserView] goBack];
  330: }
  331: 
  332: - (IBAction)forward:(id)aSender
  333: {
  334:   [[mBrowserView getBrowserView] goForward];
  335: }
  336: 
  337: - (IBAction)reload:(id)aSender
  338: {
  339:   [[mBrowserView getBrowserView] reload: 0];
  340: }
  341: 
  342: - (IBAction)stop:(id)aSender
  343: {
  344:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
  345: }
  346: 
  347: - (IBAction)home:(id)aSender
  348: {
  349:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  350: }
  351: 
  352: - (IBAction)toggleSidebar:(id)aSender
  353: {
  354:     NSResponder* resp = [[self window] firstResponder];
  355:     [[self window] makeFirstResponder: nil];
  356:     
  357:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) 	{
  358:         // XXXHack to bypass sidebar crashes.
  359:         [mSidebarDrawer open];
  360:     } else {
  361:         [mSidebarDrawer close];
  362:     }
  363:     
  364:     [[self window] makeFirstResponder: resp];
  365: }
  366: 
  367: -(void)loadURL:(NSURL*)aURL
  368: {
  369:     if (mInitialized) {
  370:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  371:     }
  372:     else {
  373:         mURL = aURL;
  374:         [mURL retain];
  375:     }
  376: }
  377: 
  378: - (void)updateLocationFields:(NSString *)locationString
  379: {
  380: /* //commenting this out because it doesn't work right yet.
  381:     if ( [locationString length] > 30 ) {
  382:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  383:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  384:     } else {
  385:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  386:             [NSString stringWithFormat:@"Location: %@", locationString]];
  387:     }
  388: */
  389: 
  390:     [mURLBar setStringValue:locationString];
  391:     [mLocationSheetURLField setStringValue:locationString];
  392: 
  393:     [[self window] update];
  394:     [[self window] display];
  395: }
  396: 
  397: -(void)newTab
  398: {
  399:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  400:     [mTabBrowser addTabViewItem: newTab];
  401:     
  402:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  403:     [newTab setView: newView];
  404:     [newTab setLabel: @"Untitled"];
  405:     
  406:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  407:     
  408:     [mTabBrowser selectLastTabViewItem: self];
  409:     
  410:     if ( [[[self window] toolbar] isVisible] ) {
  411:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  412:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  413:             [self focusURLBar];
  414:         } 
  415:     }
  416:     
  417:     //XXXdwh Do this if we ever support auto-hide.
  418:     //if ([mTabBrowser numberOfTabViewItems] == 2)
  419:     //    [mTabBrowser setTabViewType: NSTopTabsBezelBorder];
  420: }
  421: 
  422: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
  423: {
  424:     // Disconnect the old view.
  425:     [mBrowserView disconnectView];
  426:     
  427:     // Connect up the new view
  428:     mBrowserView = [aTabViewItem view];
  429:        
  430:     // Make the new view the primary content area.
  431:     [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus
  432:         progress: mProgress windowController: self];
  433: }
  434: 
  435: -(MyBrowserView*)getMyBrowserView
  436: {
  437:     return mBrowserView;
  438: }
  439: @end
  440: 

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