Annotation of chimera/BrowserWindowController.mm, revision 1.32

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

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