Annotation of chimera/BrowserWindowController.mm, revision 1.38

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.38    ! hyatt      58:     [mProgress release];
        !            59:     
1.7       hyatt      60:     [super dealloc];
                     61: }
                     62: 
1.1       hyatt      63: - (void)windowDidLoad
                     64: {
1.35      hyatt      65:     [super windowDidLoad];
                     66:     
1.4       hyatt      67:     mInitialized = YES;
1.38    ! hyatt      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:     
1.34      hyatt      74:     [[self window] setAcceptsMouseMovedEvents: YES];
                     75:     
1.1       hyatt      76:     [self setupToolbar];
1.30      hyatt      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;
1.4       hyatt      85:     
                     86:     if (mURL) {
                     87:       [self loadURL: mURL];
                     88:       [mURL release];
                     89:     }
1.18      hyatt      90:     
                     91:     [mSidebarDrawer setDelegate: self];
1.32      hyatt      92:     
                     93:     [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus
                     94:         progress: mProgress windowController: self];
1.18      hyatt      95: }
                     96: 
1.22      hyatt      97: - (void)drawerWillOpen: (NSNotification*)aNotification
                     98: {
                     99:     [mSidebarBookmarksDataSource ensureBookmarks];
                    100: }
                    101: 
1.18      hyatt     102: - (void)drawerDidOpen:(NSNotification *)aNotification
                    103: {
1.23      hyatt     104:     // XXXdwh This is temporary.
                    105:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
1.1       hyatt     106: }
                    107: 
1.19      hyatt     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: 
1.1       hyatt     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,
1.11      macserv   135:                                         PrintToolbarItemIdentifier,
1.1       hyatt     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];
1.5       macserv   214:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
1.1       hyatt     215:         [menuFormRep setTitle:[toolbarItem label]];
                    216:         
                    217:         [toolbarItem setMenuFormRepresentation:menuFormRep];
1.5       macserv   218:         mLocationToolbarItem = toolbarItem;
1.11      macserv   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)];
1.1       hyatt     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:    
1.5       macserv   251: - (void)updateToolbarItems
1.1       hyatt     252: {
                    253:   [[[self window] toolbar] validateVisibleItems];
                    254: }
                    255: 
1.5       macserv   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
1.3       hyatt     271: {
                    272:     [mURLBar selectText: self];
                    273: }
                    274: 
1.5       macserv   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]]];
1.32      hyatt     289:     
                    290:     // Focus and activate our content area.
                    291:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   292: }
                    293: 
                    294: - (IBAction)goToLocationFromToolbarURLField:(id)sender
                    295: {
                    296:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
1.32      hyatt     297:     
                    298:     // Focus and activate our content area.
                    299:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   300: }
                    301: 
1.7       hyatt     302: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
1.6       hyatt     303: {
1.7       hyatt     304:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
1.6       hyatt     305: }
                    306: 
1.12      hyatt     307: - (void)printDocument
                    308: {
                    309:     [[mBrowserView getBrowserView] printDocument];
                    310: }
                    311: 
1.15      hyatt     312: - (void)printPreview
                    313: {
                    314:     [[mBrowserView getBrowserView] printPreview];
                    315: }
                    316: 
1.16      hyatt     317: - (void)findInPage
                    318: {
                    319:     [[mBrowserView getBrowserView] findInPage];
                    320: }
                    321: 
                    322: - (void)findAgain
                    323: {
                    324:     [[mBrowserView getBrowserView] findAgain];
1.22      hyatt     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];
1.16      hyatt     333: }
                    334: 
1.5       macserv   335: - (IBAction)back:(id)aSender
1.1       hyatt     336: {
                    337:   [[mBrowserView getBrowserView] goBack];
                    338: }
                    339: 
1.5       macserv   340: - (IBAction)forward:(id)aSender
1.1       hyatt     341: {
                    342:   [[mBrowserView getBrowserView] goForward];
                    343: }
                    344: 
1.5       macserv   345: - (IBAction)reload:(id)aSender
1.1       hyatt     346: {
                    347:   [[mBrowserView getBrowserView] reload: 0];
                    348: }
                    349: 
1.5       macserv   350: - (IBAction)stop:(id)aSender
1.1       hyatt     351: {
1.37      hyatt     352:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
1.1       hyatt     353: }
                    354: 
1.5       macserv   355: - (IBAction)home:(id)aSender
1.1       hyatt     356: {
1.5       macserv   357:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.1       hyatt     358: }
                    359: 
1.5       macserv   360: - (IBAction)toggleSidebar:(id)aSender
1.1       hyatt     361: {
1.36      hyatt     362:     NSResponder* resp = [[self window] firstResponder];
                    363:     [[self window] makeFirstResponder: nil];
                    364:     
1.31      hyatt     365:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) )         {
1.34      hyatt     366:         // XXXHack to bypass sidebar crashes.
1.1       hyatt     367:         [mSidebarDrawer open];
                    368:     } else {
                    369:         [mSidebarDrawer close];
                    370:     }
1.36      hyatt     371:     
                    372:     [[self window] makeFirstResponder: resp];
1.1       hyatt     373: }
                    374: 
1.4       hyatt     375: -(void)loadURL:(NSURL*)aURL
1.1       hyatt     376: {
1.32      hyatt     377:     if (mInitialized) {
1.4       hyatt     378:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
1.32      hyatt     379:     }
1.4       hyatt     380:     else {
                    381:         mURL = aURL;
                    382:         [mURL retain];
                    383:     }
1.1       hyatt     384: }
1.5       macserv   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];
1.8       hyatt     403: }
1.27      hyatt     404: 
                    405: -(void)newTab
                    406: {
1.28      hyatt     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];
1.29      hyatt     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:     }
1.31      hyatt     424:     
                    425:     //XXXdwh Do this if we ever support auto-hide.
                    426:     //if ([mTabBrowser numberOfTabViewItems] == 2)
                    427:     //    [mTabBrowser setTabViewType: NSTopTabsBezelBorder];
1.28      hyatt     428: }
1.27      hyatt     429: 
1.28      hyatt     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];
1.30      hyatt     437:        
1.28      hyatt     438:     // Make the new view the primary content area.
                    439:     [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus
                    440:         progress: mProgress windowController: self];
1.27      hyatt     441: }
1.28      hyatt     442: 
1.31      hyatt     443: -(MyBrowserView*)getMyBrowserView
                    444: {
                    445:     return mBrowserView;
                    446: }
1.1       hyatt     447: @end
                    448: 

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