Annotation of chimera/BrowserWindowController.mm, revision 1.28

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

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