Annotation of chimera/BrowserWindowController.mm, revision 1.37

1.2       macserv     1: /*
                      2:  *  BrowserWindowController.mm
                      3:  */
1.1       hyatt       4: 
                      5: #import "BrowserWindowController.h"
                      6: #import "MyBrowserView.h"
                      7: 
1.37    ! hyatt       8: #include "nsIWebNavigation.h"
        !             9: 
1.1       hyatt      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";
1.11      macserv    18: static NSString *PrintToolbarItemIdentifier    = @"Print Toolbar Item";
1.1       hyatt      19: 
                     20: @interface BrowserWindowController(Private)
                     21: - (void)setupToolbar;
                     22: @end
                     23: 
                     24: @implementation BrowserWindowController
                     25: 
1.35      hyatt      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: 
1.4       hyatt      38: - (id)init 
                     39: {
                     40:     mInitialized = NO;
1.35      hyatt      41:     mMoveReentrant = NO;
1.4       hyatt      42:     return [super init];
                     43: }
                     44: 
1.17      hyatt      45: - (void)windowWillClose:(NSNotification *)notification
                     46: {
                     47:     [self autorelease];
1.21      hyatt      48:     [mSidebarBookmarksDataSource windowClosing];
1.17      hyatt      49: }
                     50: 
1.7       hyatt      51: - (void)dealloc
                     52: {
1.24      hyatt      53:     printf("Browser controller died.\n");
                     54:     
1.10      hyatt      55:     [mBrowserView windowClosed];
1.25      hyatt      56:     [mSidebarBrowserView windowClosed];
                     57:     
1.7       hyatt      58:     [super dealloc];
                     59: }
                     60: 
1.1       hyatt      61: - (void)windowDidLoad
                     62: {
1.35      hyatt      63:     [super windowDidLoad];
                     64:     
1.4       hyatt      65:     mInitialized = YES;
1.34      hyatt      66:     [[self window] setAcceptsMouseMovedEvents: YES];
                     67:     
1.1       hyatt      68:     [self setupToolbar];
1.30      hyatt      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;
1.4       hyatt      77:     
                     78:     if (mURL) {
                     79:       [self loadURL: mURL];
                     80:       [mURL release];
                     81:     }
1.18      hyatt      82:     
                     83:     [mSidebarDrawer setDelegate: self];
1.32      hyatt      84:     
                     85:     [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus
                     86:         progress: mProgress windowController: self];
1.18      hyatt      87: }
                     88: 
1.22      hyatt      89: - (void)drawerWillOpen: (NSNotification*)aNotification
                     90: {
                     91:     [mSidebarBookmarksDataSource ensureBookmarks];
                     92: }
                     93: 
1.18      hyatt      94: - (void)drawerDidOpen:(NSNotification *)aNotification
                     95: {
1.23      hyatt      96:     // XXXdwh This is temporary.
                     97:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
1.1       hyatt      98: }
                     99: 
1.19      hyatt     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: 
1.1       hyatt     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,
1.11      macserv   127:                                         PrintToolbarItemIdentifier,
1.1       hyatt     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];
1.5       macserv   206:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
1.1       hyatt     207:         [menuFormRep setTitle:[toolbarItem label]];
                    208:         
                    209:         [toolbarItem setMenuFormRepresentation:menuFormRep];
1.5       macserv   210:         mLocationToolbarItem = toolbarItem;
1.11      macserv   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)];
1.1       hyatt     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:    
1.5       macserv   243: - (void)updateToolbarItems
1.1       hyatt     244: {
                    245:   [[[self window] toolbar] validateVisibleItems];
                    246: }
                    247: 
1.5       macserv   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
1.3       hyatt     263: {
                    264:     [mURLBar selectText: self];
                    265: }
                    266: 
1.5       macserv   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]]];
1.32      hyatt     281:     
                    282:     // Focus and activate our content area.
                    283:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   284: }
                    285: 
                    286: - (IBAction)goToLocationFromToolbarURLField:(id)sender
                    287: {
                    288:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
1.32      hyatt     289:     
                    290:     // Focus and activate our content area.
                    291:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   292: }
                    293: 
1.7       hyatt     294: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
1.6       hyatt     295: {
1.7       hyatt     296:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
1.6       hyatt     297: }
                    298: 
1.12      hyatt     299: - (void)printDocument
                    300: {
                    301:     [[mBrowserView getBrowserView] printDocument];
                    302: }
                    303: 
1.15      hyatt     304: - (void)printPreview
                    305: {
                    306:     [[mBrowserView getBrowserView] printPreview];
                    307: }
                    308: 
1.16      hyatt     309: - (void)findInPage
                    310: {
                    311:     [[mBrowserView getBrowserView] findInPage];
                    312: }
                    313: 
                    314: - (void)findAgain
                    315: {
                    316:     [[mBrowserView getBrowserView] findAgain];
1.22      hyatt     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];
1.16      hyatt     325: }
                    326: 
1.5       macserv   327: - (IBAction)back:(id)aSender
1.1       hyatt     328: {
                    329:   [[mBrowserView getBrowserView] goBack];
                    330: }
                    331: 
1.5       macserv   332: - (IBAction)forward:(id)aSender
1.1       hyatt     333: {
                    334:   [[mBrowserView getBrowserView] goForward];
                    335: }
                    336: 
1.5       macserv   337: - (IBAction)reload:(id)aSender
1.1       hyatt     338: {
                    339:   [[mBrowserView getBrowserView] reload: 0];
                    340: }
                    341: 
1.5       macserv   342: - (IBAction)stop:(id)aSender
1.1       hyatt     343: {
1.37    ! hyatt     344:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
1.1       hyatt     345: }
                    346: 
1.5       macserv   347: - (IBAction)home:(id)aSender
1.1       hyatt     348: {
1.5       macserv   349:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.1       hyatt     350: }
                    351: 
1.5       macserv   352: - (IBAction)toggleSidebar:(id)aSender
1.1       hyatt     353: {
1.36      hyatt     354:     NSResponder* resp = [[self window] firstResponder];
                    355:     [[self window] makeFirstResponder: nil];
                    356:     
1.31      hyatt     357:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) )         {
1.34      hyatt     358:         // XXXHack to bypass sidebar crashes.
1.1       hyatt     359:         [mSidebarDrawer open];
                    360:     } else {
                    361:         [mSidebarDrawer close];
                    362:     }
1.36      hyatt     363:     
                    364:     [[self window] makeFirstResponder: resp];
1.1       hyatt     365: }
                    366: 
1.4       hyatt     367: -(void)loadURL:(NSURL*)aURL
1.1       hyatt     368: {
1.32      hyatt     369:     if (mInitialized) {
1.4       hyatt     370:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
1.32      hyatt     371:     }
1.4       hyatt     372:     else {
                    373:         mURL = aURL;
                    374:         [mURL retain];
                    375:     }
1.1       hyatt     376: }
1.5       macserv   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];
1.8       hyatt     395: }
1.27      hyatt     396: 
                    397: -(void)newTab
                    398: {
1.28      hyatt     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];
1.29      hyatt     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:     }
1.31      hyatt     416:     
                    417:     //XXXdwh Do this if we ever support auto-hide.
                    418:     //if ([mTabBrowser numberOfTabViewItems] == 2)
                    419:     //    [mTabBrowser setTabViewType: NSTopTabsBezelBorder];
1.28      hyatt     420: }
1.27      hyatt     421: 
1.28      hyatt     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];
1.30      hyatt     429:        
1.28      hyatt     430:     // Make the new view the primary content area.
                    431:     [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus
                    432:         progress: mProgress windowController: self];
1.27      hyatt     433: }
1.28      hyatt     434: 
1.31      hyatt     435: -(MyBrowserView*)getMyBrowserView
                    436: {
                    437:     return mBrowserView;
                    438: }
1.1       hyatt     439: @end
                    440: 

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