Annotation of chimera/BrowserWindowController.mm, revision 1.25

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

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