Annotation of chimera/BrowserWindowController.mm, revision 1.41

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

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