Annotation of chimera/BrowserWindowController.mm, revision 1.36

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

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