Annotation of chimera/BrowserWindowController.mm, revision 1.34

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

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