File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.40: download - view: text, annotated - select for diffs - revision graph
Mon Feb 25 10:13:04 2002 UTC (17 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Adding new build.

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

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