Annotation of chimera/BrowserWindowController.mm, revision 1.40

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

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