File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.47: download - view: text, annotated - select for diffs - revision graph
Fri Mar 8 03:18:00 2002 UTC (16 years, 6 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Make tab link loading work, although background loading is whacked.

    1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
    2: /* ***** BEGIN LICENSE BLOCK *****
    3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
    4:  *
    5:  * The contents of this file are subject to the Netscape Public License
    6:  * Version 1.1 (the "License"); you may not use this file except in
    7:  * compliance with the License. You may obtain a copy of the License at
    8:  * http://www.mozilla.org/NPL/
    9:  *
   10:  * Software distributed under the License is distributed on an "AS IS" basis,
   11:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   12:  * for the specific language governing rights and limitations under the
   13:  * License.
   14:  *
   15:  * The Original Code is mozilla.org code.
   16:  *
   17:  * The Initial Developer of the Original Code is 
   18:  * Netscape Communications Corporation.
   19:  * Portions created by the Initial Developer are Copyright (C) 2002
   20:  * the Initial Developer. All Rights Reserved.
   21:  *
   22:  * Contributor(s):
   23:  *
   24:  * Alternatively, the contents of this file may be used under the terms of
   25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
   26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   27:  * in which case the provisions of the GPL or the LGPL are applicable instead
   28:  * of those above. If you wish to allow use of your version of this file only
   29:  * under the terms of either the GPL or the LGPL, and not to allow others to
   30:  * use your version of this file under the terms of the NPL, indicate your
   31:  * decision by deleting the provisions above and replace them with the notice
   32:  * and other provisions required by the GPL or the LGPL. If you do not delete
   33:  * the provisions above, a recipient may use your version of this file under
   34:  * the terms of any one of the NPL, the GPL or the LGPL.
   35:  *
   36:  * ***** END LICENSE BLOCK ***** */
   37: 
   38: #import "BrowserWindowController.h"
   39: #import "MyBrowserView.h"
   40: 
   41: #include "nsIWebNavigation.h"
   42: 
   43: static NSString *BrowserToolbarIdentifier	= @"Browser Window Toolbar";
   44: static NSString *BackToolbarItemIdentifier	= @"Back Toolbar Item";
   45: static NSString *ForwardToolbarItemIdentifier	= @"Forward Toolbar Item";
   46: static NSString *ReloadToolbarItemIdentifier	= @"Reload Toolbar Item";
   47: static NSString *StopToolbarItemIdentifier	= @"Stop Toolbar Item";
   48: static NSString *HomeToolbarItemIdentifier	= @"Home Toolbar Item";
   49: static NSString *LocationToolbarItemIdentifier	= @"Location Toolbar Item";
   50: static NSString *SidebarToolbarItemIdentifier	= @"Sidebar Toolbar Item";
   51: static NSString *PrintToolbarItemIdentifier	= @"Print Toolbar Item";
   52: 
   53: @interface BrowserWindowController(Private)
   54: - (void)setupToolbar;
   55: @end
   56: 
   57: @implementation BrowserWindowController
   58: 
   59: -(void)enterModalSession
   60: {
   61:     mModalSession = [NSApp beginModalSessionForWindow: [self window]];
   62:     [NSApp runModalSession: mModalSession];
   63:     [NSApp endModalSession: mModalSession];
   64:     mModalSession = nil;
   65: }
   66: 
   67: -(void)mouseMoved:(NSEvent*)aEvent
   68: {
   69:     if (mMoveReentrant)
   70:         return;
   71:         
   72:     mMoveReentrant = YES;
   73:     NSView* view = [[[self window] contentView] hitTest: [aEvent locationInWindow]];
   74:     [view mouseMoved: aEvent];
   75:     [super mouseMoved: aEvent];
   76:     mMoveReentrant = NO;
   77: }
   78: 
   79: - (id)initWithWindowNibName:(NSString *)windowNibName
   80: {
   81:     if ( (self = [super initWithWindowNibName:(NSString *)windowNibName]) ) {
   82:         mInitialized = NO;
   83:         mMoveReentrant = NO;
   84:     }
   85:     return self;
   86: }
   87: 
   88: - (void)windowWillClose:(NSNotification *)notification
   89: {
   90:     printf("Window will close notification.\n");
   91:     [self autorelease];
   92:     [mSidebarBookmarksDataSource windowClosing];
   93: }
   94: 
   95: - (void)dealloc
   96: {
   97:     printf("Browser controller died.\n");
   98: 
   99:     [mBrowserView windowClosed];
  100:     [mSidebarBrowserView windowClosed];
  101:     
  102:     [mProgress release];
  103:       
  104:     [super dealloc];
  105: }
  106: 
  107: - (void)windowDidLoad
  108: {
  109:     [super windowDidLoad];
  110:     
  111:     if (mModalSession)
  112:       [NSApp stopModal: mModalSession];
  113:       
  114:     mInitialized = YES;
  115:     
  116:     // Retain with a single extra refcount.  This allows the MyBrowserViews
  117:     // to remove the progress meter from its superview without having to 
  118:     // worry about retaining and releasing it.
  119:     [mProgress retain];
  120:     
  121:     [[self window] setAcceptsMouseMovedEvents: YES];
  122:     
  123:     [self setupToolbar];
  124: 
  125: //  03/03/2002 mlj Changing strategy a bit here.  The addTab: method was
  126: //	duplicating a lot of the code found here.  I have moved it to that method.
  127: //	We now remove the IB tab, then add one of our own.
  128: 
  129:     [mTabBrowser removeTabViewItem:[mTabBrowser tabViewItemAtIndex:0]];
  130:     [self newTab];
  131:     
  132:     if (mURL) {
  133:       [self loadURL: mURL];
  134:       [mURL release];
  135:     }
  136:     
  137:     [mSidebarDrawer setDelegate: self];
  138: }
  139: 
  140: - (void)drawerWillOpen: (NSNotification*)aNotification
  141: {
  142:     [mSidebarBookmarksDataSource ensureBookmarks];
  143: }
  144: 
  145: - (void)drawerDidOpen:(NSNotification *)aNotification
  146: {
  147:     // XXXdwh This is temporary.
  148:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
  149: }
  150: 
  151: - (void)drawerDidClose:(NSNotification *)aNotification
  152: {
  153:     // Unload the Gecko web page in "My Panels" to save memory.
  154:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
  155: }
  156: 
  157: - (void)setupToolbar
  158: {
  159:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
  160:     
  161:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
  162:     [toolbar setAllowsUserCustomization:YES];
  163:     [toolbar setAutosavesConfiguration:YES];
  164:     [toolbar setDelegate:self];
  165:     [[self window] setToolbar:toolbar];
  166: }
  167: 
  168: 
  169: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  170: {
  171:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  172:                                         ForwardToolbarItemIdentifier,
  173:                                         ReloadToolbarItemIdentifier,
  174:                                         StopToolbarItemIdentifier,
  175:                                         HomeToolbarItemIdentifier,
  176:                                         LocationToolbarItemIdentifier,
  177:                                         SidebarToolbarItemIdentifier,
  178:                                         PrintToolbarItemIdentifier,
  179:                                         NSToolbarCustomizeToolbarItemIdentifier,
  180:                                         NSToolbarFlexibleSpaceItemIdentifier,
  181:                                         NSToolbarSpaceItemIdentifier,
  182:                                         NSToolbarSeparatorItemIdentifier,
  183:                                         nil];
  184: }
  185: 
  186: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  187: {
  188:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  189:                                         ForwardToolbarItemIdentifier,
  190:                                         ReloadToolbarItemIdentifier,
  191:                                         StopToolbarItemIdentifier,
  192:                                         HomeToolbarItemIdentifier,
  193:                                         LocationToolbarItemIdentifier,
  194:                                         SidebarToolbarItemIdentifier,
  195:                                         nil];
  196: }
  197: 
  198: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  199:       itemForItemIdentifier:(NSString *)itemIdent
  200:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  201: {
  202:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  203:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
  204:         [toolbarItem setLabel:@"Back"];
  205:         [toolbarItem setPaletteLabel:@"Go Back"];
  206:         [toolbarItem setToolTip:@"Go back one page"];
  207:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
  208:         [toolbarItem setTarget:self];
  209:         [toolbarItem setAction:@selector(back:)];
  210:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
  211:         [toolbarItem setLabel:@"Forward"];
  212:         [toolbarItem setPaletteLabel:@"Go Forward"];
  213:         [toolbarItem setToolTip:@"Go forward one page"];
  214:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
  215:         [toolbarItem setTarget:self];
  216:         [toolbarItem setAction:@selector(forward:)];
  217:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  218:         [toolbarItem setLabel:@"Reload"];
  219:         [toolbarItem setPaletteLabel:@"Reload Page"];
  220:         [toolbarItem setToolTip:@"Reload current page"];
  221:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  222:         [toolbarItem setTarget:self];
  223:         [toolbarItem setAction:@selector(reload:)];
  224:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  225:         [toolbarItem setLabel:@"Stop"];
  226:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  227:         [toolbarItem setToolTip:@"Stop loading this page"];
  228:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  229:         [toolbarItem setTarget:self];
  230:         [toolbarItem setAction:@selector(stop:)];
  231:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  232:         [toolbarItem setLabel:@"Home"];
  233:         [toolbarItem setPaletteLabel:@"Go Home"];
  234:         [toolbarItem setToolTip:@"Go to home page"];
  235:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  236:         [toolbarItem setTarget:self];
  237:         [toolbarItem setAction:@selector(home:)];
  238:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  239:         [toolbarItem setLabel:@"Sidebar"];
  240:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  241:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  242:         [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
  243:         [toolbarItem setTarget:self];
  244:         [toolbarItem setAction:@selector(toggleSidebar:)];
  245:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  246:         
  247:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  248:         
  249:         [toolbarItem setLabel:@"Location"];
  250:         [toolbarItem setPaletteLabel:@"Location"];
  251:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  252:         [toolbarItem setView:mLocationToolbarView];
  253:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  254:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  255:         
  256:         [menuFormRep setTarget:self];
  257:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  258:         [menuFormRep setTitle:[toolbarItem label]];
  259:         
  260:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  261:         mLocationToolbarItem = toolbarItem;
  262: 
  263:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
  264:         [toolbarItem setLabel:@"Print"];
  265:         [toolbarItem setPaletteLabel:@"Print"];
  266:         [toolbarItem setToolTip:@"Print this page"];
  267:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
  268:         [toolbarItem setTarget:self];
  269:         [toolbarItem setAction:@selector(printDocument)];
  270:     } else {
  271:         toolbarItem = nil;
  272:     }
  273:         
  274:     return toolbarItem;
  275: }
  276: 
  277: // This method handles the enabling/disabling of the toolbar buttons.
  278: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  279: {
  280:   // Check the action and see if it matches.
  281:   SEL action = [theItem action];
  282:   if (action == @selector(back:))
  283:     return [[mBrowserView getBrowserView] canGoBack];
  284:   else if (action == @selector(forward:))
  285:     return [[mBrowserView getBrowserView] canGoForward];
  286:   else if (action == @selector(reload:))
  287:     return [mBrowserView isBusy] == NO;
  288:   else if (action == @selector(stop:))
  289:     return [mBrowserView isBusy];
  290:   else
  291:     return YES;
  292: }
  293:    
  294: - (void)updateToolbarItems
  295: {
  296:   [[[self window] toolbar] validateVisibleItems];
  297: }
  298: 
  299: - (void)performAppropriateLocationAction
  300: {
  301:     if ( [[[self window] toolbar] isVisible] ) {
  302:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  303:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  304:             [self focusURLBar];
  305:         } else {
  306:             [self beginLocationSheet];
  307:         }
  308:     } else {
  309:         [self beginLocationSheet];
  310:     }
  311: }
  312: 
  313: - (void)focusURLBar
  314: {
  315:     [mURLBar selectText: self];
  316: }
  317: 
  318: - (void)beginLocationSheet
  319: {
  320:     [NSApp beginSheet:	mLocationSheetWindow
  321:        modalForWindow:	[self window]
  322:         modalDelegate:	nil
  323:        didEndSelector:	nil
  324:           contextInfo:	nil];
  325: }
  326: 
  327: - (IBAction)endLocationSheet:(id)sender
  328: {
  329:     [mLocationSheetWindow orderOut:self];
  330:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  331:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  332:     
  333:     // Focus and activate our content area.
  334:     [[mBrowserView getBrowserView] setActive: YES];
  335: }
  336: 
  337: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  338: {
  339:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  340:     
  341:     // Focus and activate our content area.
  342:     [[mBrowserView getBrowserView] setActive: YES];
  343: }
  344: 
  345: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  346: {
  347:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
  348: }
  349: 
  350: - (void)printDocument
  351: {
  352:     [[mBrowserView getBrowserView] printDocument];
  353: }
  354: 
  355: - (void)printPreview
  356: {
  357:     [[mBrowserView getBrowserView] printPreview];
  358: }
  359: 
  360: - (void)findInPage
  361: {
  362:     [[mBrowserView getBrowserView] findInPage];
  363: }
  364: 
  365: - (void)findAgain
  366: {
  367:     [[mBrowserView getBrowserView] findAgain];
  368: }
  369: 
  370: - (void)addBookmark
  371: {
  372:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
  373:     // menu data source going.
  374:     [mSidebarBookmarksDataSource ensureBookmarks];
  375:     [mSidebarBookmarksDataSource addBookmark: self];
  376: }
  377: 
  378: - (IBAction)back:(id)aSender
  379: {
  380:   [[mBrowserView getBrowserView] goBack];
  381: }
  382: 
  383: - (IBAction)forward:(id)aSender
  384: {
  385:   [[mBrowserView getBrowserView] goForward];
  386: }
  387: 
  388: - (IBAction)reload:(id)aSender
  389: {
  390:   [[mBrowserView getBrowserView] reload: 0];
  391: }
  392: 
  393: - (IBAction)stop:(id)aSender
  394: {
  395:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
  396: }
  397: 
  398: - (IBAction)home:(id)aSender
  399: {
  400:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  401: }
  402: 
  403: - (IBAction)toggleSidebar:(id)aSender
  404: {
  405:     NSResponder* resp = [[self window] firstResponder];
  406:     [[self window] makeFirstResponder: nil];
  407:     
  408:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) 	{
  409:         // XXXHack to bypass sidebar crashes.
  410:         [mSidebarDrawer open];
  411:     } else {
  412:         [mSidebarDrawer close];
  413:     }
  414:     
  415:     [[self window] makeFirstResponder: resp];
  416: }
  417: 
  418: -(void)loadURL:(NSURL*)aURL
  419: {
  420:     if (mInitialized) {
  421:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  422:     }
  423:     else {
  424:         mURL = aURL;
  425:         [mURL retain];
  426:     }
  427: }
  428: 
  429: - (void)updateLocationFields:(NSString *)locationString
  430: {
  431: /* //commenting this out because it doesn't work right yet.
  432:     if ( [locationString length] > 30 ) {
  433:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  434:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  435:     } else {
  436:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  437:             [NSString stringWithFormat:@"Location: %@", locationString]];
  438:     }
  439: */
  440: 
  441:     [mURLBar setStringValue:locationString];
  442:     [mLocationSheetURLField setStringValue:locationString];
  443: 
  444:     [[self window] update];
  445:     [[self window] display];
  446: }
  447: 
  448: -(void)newTab
  449: {
  450:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  451:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  452:     [newView setTab: newTab];
  453: 
  454:     [newTab setLabel: @"Untitled"];
  455:     [newTab setView: newView];
  456: 
  457:     [mTabBrowser addTabViewItem: newTab];
  458: 
  459:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  460: 
  461:     [mTabBrowser selectLastTabViewItem: self];
  462: 
  463:     if ( [[[self window] toolbar] isVisible] ) {
  464:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  465:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  466:             [self focusURLBar];
  467:         }
  468:     }
  469: }
  470: 
  471: -(void)closeTab
  472: {
  473:     if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
  474:         [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
  475:     }
  476: }
  477: 
  478: - (void)previousTab
  479: {
  480:     [mTabBrowser selectPreviousTabViewItem:self];
  481: }
  482: 
  483: - (void)nextTab
  484: {
  485:     [mTabBrowser selectNextTabViewItem:self];
  486: }
  487: 
  488: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
  489: {
  490:     // Disconnect the old view, if one has been designated.
  491:     // If the window has just been opened, none has been.
  492:     if ( mBrowserView ) {
  493:         [mBrowserView disconnectView];
  494:     }
  495:     // Connect up the new view
  496:     mBrowserView = [aTabViewItem view];
  497:        
  498:     // Make the new view the primary content area.
  499:     [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
  500:         progress: mProgress windowController: self];
  501: }
  502: 
  503: -(MyBrowserView*)getMyBrowserView
  504: {
  505:     return mBrowserView;
  506: }
  507: 
  508: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
  509: {
  510:     BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  511:     [browser loadURL: aURL];
  512:     if (aLoadInBG)
  513:       [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
  514: 	else
  515:       [browser showWindow: self];
  516:       
  517:     // XXXdwh Focus the content area.
  518: }
  519: 
  520: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
  521: {
  522:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  523:     
  524:     NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
  525:     int index = [mTabBrowser indexOfTabViewItem: selectedTab];
  526:     [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
  527:     
  528:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  529:     [newView setTab: newTab];
  530:     
  531:     [newTab setLabel: @"Loading..."];
  532:     [newTab setView: newView];
  533: 
  534:     [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  535: 
  536:     if (!aLoadInBG)
  537:         [mTabBrowser selectTabViewItem: newTab];
  538:         
  539:     // XXXdwh Focus the content area.
  540: }
  541: @end
  542: 

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