Annotation of chimera/BrowserWindowController.mm, revision 1.31

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:     
1.30      hyatt      59:     [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus
                     60:         progress: mProgress windowController: self];
                     61: 
1.4       hyatt      62:     if (mURL) {
                     63:       [self loadURL: mURL];
                     64:       [mURL release];
                     65:     }
1.18      hyatt      66:     
                     67:     [mSidebarDrawer setDelegate: self];
                     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]]];
                    262: }
                    263: 
                    264: - (IBAction)goToLocationFromToolbarURLField:(id)sender
                    265: {
                    266:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
                    267: }
                    268: 
1.7       hyatt     269: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
1.6       hyatt     270: {
1.7       hyatt     271:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
1.6       hyatt     272: }
                    273: 
1.12      hyatt     274: - (void)printDocument
                    275: {
                    276:     [[mBrowserView getBrowserView] printDocument];
                    277: }
                    278: 
1.15      hyatt     279: - (void)printPreview
                    280: {
                    281:     [[mBrowserView getBrowserView] printPreview];
                    282: }
                    283: 
1.16      hyatt     284: - (void)findInPage
                    285: {
                    286:     [[mBrowserView getBrowserView] findInPage];
                    287: }
                    288: 
                    289: - (void)findAgain
                    290: {
                    291:     [[mBrowserView getBrowserView] findAgain];
1.22      hyatt     292: }
                    293: 
                    294: - (void)addBookmark
                    295: {
                    296:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
                    297:     // menu data source going.
                    298:     [mSidebarBookmarksDataSource ensureBookmarks];
                    299:     [mSidebarBookmarksDataSource addBookmark: self];
1.16      hyatt     300: }
                    301: 
1.5       macserv   302: - (IBAction)back:(id)aSender
1.1       hyatt     303: {
                    304:   [[mBrowserView getBrowserView] goBack];
                    305: }
                    306: 
1.5       macserv   307: - (IBAction)forward:(id)aSender
1.1       hyatt     308: {
                    309:   [[mBrowserView getBrowserView] goForward];
                    310: }
                    311: 
1.5       macserv   312: - (IBAction)reload:(id)aSender
1.1       hyatt     313: {
                    314:   [[mBrowserView getBrowserView] reload: 0];
                    315: }
                    316: 
1.5       macserv   317: - (IBAction)stop:(id)aSender
1.1       hyatt     318: {
                    319:   [[mBrowserView getBrowserView] stop: 0];
                    320: }
                    321: 
1.5       macserv   322: - (IBAction)home:(id)aSender
1.1       hyatt     323: {
1.5       macserv   324:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.1       hyatt     325: }
                    326: 
1.5       macserv   327: - (IBAction)toggleSidebar:(id)aSender
1.1       hyatt     328: {
1.31    ! hyatt     329:     NSLog(@"The sidebar drawer is: %@", mSidebarDrawer);
        !           330:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) )         {
        !           331:         NSLog(@"opening drawer");
1.1       hyatt     332:         [mSidebarDrawer open];
                    333:     } else {
1.31    ! hyatt     334:         NSLog(@"closing drawer");
1.1       hyatt     335:         [mSidebarDrawer close];
                    336:     }
                    337: }
                    338: 
1.4       hyatt     339: -(void)loadURL:(NSURL*)aURL
1.1       hyatt     340: {
1.4       hyatt     341:     if (mInitialized)
                    342:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
                    343:     else {
                    344:         mURL = aURL;
                    345:         [mURL retain];
                    346:     }
1.1       hyatt     347: }
1.5       macserv   348: 
                    349: - (void)updateLocationFields:(NSString *)locationString
                    350: {
                    351: /* //commenting this out because it doesn't work right yet.
                    352:     if ( [locationString length] > 30 ) {
                    353:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    354:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
                    355:     } else {
                    356:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    357:             [NSString stringWithFormat:@"Location: %@", locationString]];
                    358:     }
                    359: */
                    360: 
                    361:     [mURLBar setStringValue:locationString];
                    362:     [mLocationSheetURLField setStringValue:locationString];
                    363: 
                    364:     [[self window] update];
                    365:     [[self window] display];
1.8       hyatt     366: }
1.27      hyatt     367: 
                    368: -(void)newTab
                    369: {
1.28      hyatt     370:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
                    371:     [mTabBrowser addTabViewItem: newTab];
                    372:     
                    373:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    374:     [newTab setView: newView];
                    375:     [newTab setLabel: @"Untitled"];
                    376:     
                    377:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
                    378:     
                    379:     [mTabBrowser selectLastTabViewItem: self];
1.29      hyatt     380:     
                    381:     if ( [[[self window] toolbar] isVisible] ) {
                    382:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
                    383:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
                    384:             [self focusURLBar];
                    385:         } 
                    386:     }
1.31    ! hyatt     387:     
        !           388:     //XXXdwh Do this if we ever support auto-hide.
        !           389:     //if ([mTabBrowser numberOfTabViewItems] == 2)
        !           390:     //    [mTabBrowser setTabViewType: NSTopTabsBezelBorder];
1.28      hyatt     391: }
1.27      hyatt     392: 
1.28      hyatt     393: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
                    394: {
                    395:     // Disconnect the old view.
                    396:     [mBrowserView disconnectView];
                    397:     
                    398:     // Connect up the new view
                    399:     mBrowserView = [aTabViewItem view];
1.30      hyatt     400:        
1.28      hyatt     401:     // Make the new view the primary content area.
                    402:     [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus
                    403:         progress: mProgress windowController: self];
1.27      hyatt     404: }
1.28      hyatt     405: 
1.31    ! hyatt     406: -(MyBrowserView*)getMyBrowserView
        !           407: {
        !           408:     return mBrowserView;
        !           409: }
1.1       hyatt     410: @end
                    411: 

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