File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.38: download - view: text, annotated - select for diffs - revision graph
Sun Feb 24 06:30:48 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Add support for security.

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

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