Annotation of chimera/BrowserWindowController.mm, revision 1.39

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

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