File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.39: download - view: text, annotated - select for diffs - revision graph
Mon Feb 25 08:44:17 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
A bunch of new stuff.  window.open now works, and security is getting closer.

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

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