File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.45: download - view: text, annotated - select for diffs - revision graph
Fri Mar 8 01:43:01 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Getting command clicking working.

    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: //    [mBrowserView makePrimaryBrowserView: tab urlbar: mURLBar status: mStatus
  140:   //      progress: mProgress windowController: self];
  141: }
  142: 
  143: - (void)drawerWillOpen: (NSNotification*)aNotification
  144: {
  145:     [mSidebarBookmarksDataSource ensureBookmarks];
  146: }
  147: 
  148: - (void)drawerDidOpen:(NSNotification *)aNotification
  149: {
  150:     // XXXdwh This is temporary.
  151:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
  152: }
  153: 
  154: - (void)drawerDidClose:(NSNotification *)aNotification
  155: {
  156:     // Unload the Gecko web page in "My Panels" to save memory.
  157:     [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
  158: }
  159: 
  160: - (void)setupToolbar
  161: {
  162:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
  163:     
  164:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
  165:     [toolbar setAllowsUserCustomization:YES];
  166:     [toolbar setAutosavesConfiguration:YES];
  167:     [toolbar setDelegate:self];
  168:     [[self window] setToolbar:toolbar];
  169: }
  170: 
  171: 
  172: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  173: {
  174:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  175:                                         ForwardToolbarItemIdentifier,
  176:                                         ReloadToolbarItemIdentifier,
  177:                                         StopToolbarItemIdentifier,
  178:                                         HomeToolbarItemIdentifier,
  179:                                         LocationToolbarItemIdentifier,
  180:                                         SidebarToolbarItemIdentifier,
  181:                                         PrintToolbarItemIdentifier,
  182:                                         NSToolbarCustomizeToolbarItemIdentifier,
  183:                                         NSToolbarFlexibleSpaceItemIdentifier,
  184:                                         NSToolbarSpaceItemIdentifier,
  185:                                         NSToolbarSeparatorItemIdentifier,
  186:                                         nil];
  187: }
  188: 
  189: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  190: {
  191:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  192:                                         ForwardToolbarItemIdentifier,
  193:                                         ReloadToolbarItemIdentifier,
  194:                                         StopToolbarItemIdentifier,
  195:                                         HomeToolbarItemIdentifier,
  196:                                         LocationToolbarItemIdentifier,
  197:                                         SidebarToolbarItemIdentifier,
  198:                                         nil];
  199: }
  200: 
  201: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  202:       itemForItemIdentifier:(NSString *)itemIdent
  203:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  204: {
  205:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  206:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
  207:         [toolbarItem setLabel:@"Back"];
  208:         [toolbarItem setPaletteLabel:@"Go Back"];
  209:         [toolbarItem setToolTip:@"Go back one page"];
  210:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
  211:         [toolbarItem setTarget:self];
  212:         [toolbarItem setAction:@selector(back:)];
  213:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
  214:         [toolbarItem setLabel:@"Forward"];
  215:         [toolbarItem setPaletteLabel:@"Go Forward"];
  216:         [toolbarItem setToolTip:@"Go forward one page"];
  217:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
  218:         [toolbarItem setTarget:self];
  219:         [toolbarItem setAction:@selector(forward:)];
  220:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  221:         [toolbarItem setLabel:@"Reload"];
  222:         [toolbarItem setPaletteLabel:@"Reload Page"];
  223:         [toolbarItem setToolTip:@"Reload current page"];
  224:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  225:         [toolbarItem setTarget:self];
  226:         [toolbarItem setAction:@selector(reload:)];
  227:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  228:         [toolbarItem setLabel:@"Stop"];
  229:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  230:         [toolbarItem setToolTip:@"Stop loading this page"];
  231:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  232:         [toolbarItem setTarget:self];
  233:         [toolbarItem setAction:@selector(stop:)];
  234:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  235:         [toolbarItem setLabel:@"Home"];
  236:         [toolbarItem setPaletteLabel:@"Go Home"];
  237:         [toolbarItem setToolTip:@"Go to home page"];
  238:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  239:         [toolbarItem setTarget:self];
  240:         [toolbarItem setAction:@selector(home:)];
  241:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  242:         [toolbarItem setLabel:@"Sidebar"];
  243:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  244:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  245:         [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
  246:         [toolbarItem setTarget:self];
  247:         [toolbarItem setAction:@selector(toggleSidebar:)];
  248:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  249:         
  250:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  251:         
  252:         [toolbarItem setLabel:@"Location"];
  253:         [toolbarItem setPaletteLabel:@"Location"];
  254:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  255:         [toolbarItem setView:mLocationToolbarView];
  256:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  257:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  258:         
  259:         [menuFormRep setTarget:self];
  260:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  261:         [menuFormRep setTitle:[toolbarItem label]];
  262:         
  263:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  264:         mLocationToolbarItem = toolbarItem;
  265: 
  266:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
  267:         [toolbarItem setLabel:@"Print"];
  268:         [toolbarItem setPaletteLabel:@"Print"];
  269:         [toolbarItem setToolTip:@"Print this page"];
  270:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
  271:         [toolbarItem setTarget:self];
  272:         [toolbarItem setAction:@selector(printDocument)];
  273:     } else {
  274:         toolbarItem = nil;
  275:     }
  276:         
  277:     return toolbarItem;
  278: }
  279: 
  280: // This method handles the enabling/disabling of the toolbar buttons.
  281: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  282: {
  283:   // Check the action and see if it matches.
  284:   SEL action = [theItem action];
  285:   if (action == @selector(back:))
  286:     return [[mBrowserView getBrowserView] canGoBack];
  287:   else if (action == @selector(forward:))
  288:     return [[mBrowserView getBrowserView] canGoForward];
  289:   else if (action == @selector(reload:))
  290:     return [mBrowserView isBusy] == NO;
  291:   else if (action == @selector(stop:))
  292:     return [mBrowserView isBusy];
  293:   else
  294:     return YES;
  295: }
  296:    
  297: - (void)updateToolbarItems
  298: {
  299:   [[[self window] toolbar] validateVisibleItems];
  300: }
  301: 
  302: - (void)performAppropriateLocationAction
  303: {
  304:     if ( [[[self window] toolbar] isVisible] ) {
  305:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  306:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  307:             [self focusURLBar];
  308:         } else {
  309:             [self beginLocationSheet];
  310:         }
  311:     } else {
  312:         [self beginLocationSheet];
  313:     }
  314: }
  315: 
  316: - (void)focusURLBar
  317: {
  318:     [mURLBar selectText: self];
  319: }
  320: 
  321: - (void)beginLocationSheet
  322: {
  323:     [NSApp beginSheet:	mLocationSheetWindow
  324:        modalForWindow:	[self window]
  325:         modalDelegate:	nil
  326:        didEndSelector:	nil
  327:           contextInfo:	nil];
  328: }
  329: 
  330: - (IBAction)endLocationSheet:(id)sender
  331: {
  332:     [mLocationSheetWindow orderOut:self];
  333:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  334:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  335:     
  336:     // Focus and activate our content area.
  337:     [[mBrowserView getBrowserView] setActive: YES];
  338: }
  339: 
  340: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  341: {
  342:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  343:     
  344:     // Focus and activate our content area.
  345:     [[mBrowserView getBrowserView] setActive: YES];
  346: }
  347: 
  348: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  349: {
  350:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
  351: }
  352: 
  353: - (void)printDocument
  354: {
  355:     [[mBrowserView getBrowserView] printDocument];
  356: }
  357: 
  358: - (void)printPreview
  359: {
  360:     [[mBrowserView getBrowserView] printPreview];
  361: }
  362: 
  363: - (void)findInPage
  364: {
  365:     [[mBrowserView getBrowserView] findInPage];
  366: }
  367: 
  368: - (void)findAgain
  369: {
  370:     [[mBrowserView getBrowserView] findAgain];
  371: }
  372: 
  373: - (void)addBookmark
  374: {
  375:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
  376:     // menu data source going.
  377:     [mSidebarBookmarksDataSource ensureBookmarks];
  378:     [mSidebarBookmarksDataSource addBookmark: self];
  379: }
  380: 
  381: - (IBAction)back:(id)aSender
  382: {
  383:   [[mBrowserView getBrowserView] goBack];
  384: }
  385: 
  386: - (IBAction)forward:(id)aSender
  387: {
  388:   [[mBrowserView getBrowserView] goForward];
  389: }
  390: 
  391: - (IBAction)reload:(id)aSender
  392: {
  393:   [[mBrowserView getBrowserView] reload: 0];
  394: }
  395: 
  396: - (IBAction)stop:(id)aSender
  397: {
  398:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
  399: }
  400: 
  401: - (IBAction)home:(id)aSender
  402: {
  403:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  404: }
  405: 
  406: - (IBAction)toggleSidebar:(id)aSender
  407: {
  408:     NSResponder* resp = [[self window] firstResponder];
  409:     [[self window] makeFirstResponder: nil];
  410:     
  411:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) 	{
  412:         // XXXHack to bypass sidebar crashes.
  413:         [mSidebarDrawer open];
  414:     } else {
  415:         [mSidebarDrawer close];
  416:     }
  417:     
  418:     [[self window] makeFirstResponder: resp];
  419: }
  420: 
  421: -(void)loadURL:(NSURL*)aURL
  422: {
  423:     if (mInitialized) {
  424:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  425:     }
  426:     else {
  427:         mURL = aURL;
  428:         [mURL retain];
  429:     }
  430: }
  431: 
  432: - (void)updateLocationFields:(NSString *)locationString
  433: {
  434: /* //commenting this out because it doesn't work right yet.
  435:     if ( [locationString length] > 30 ) {
  436:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  437:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  438:     } else {
  439:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  440:             [NSString stringWithFormat:@"Location: %@", locationString]];
  441:     }
  442: */
  443: 
  444:     [mURLBar setStringValue:locationString];
  445:     [mLocationSheetURLField setStringValue:locationString];
  446: 
  447:     [[self window] update];
  448:     [[self window] display];
  449: }
  450: 
  451: -(void)newTab
  452: {
  453:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  454:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [[newTab view] frame]] autorelease];
  455: 
  456:     [newTab setLabel: @"Untitled"];
  457:     [newTab setView: newView];
  458: 
  459:     [mTabBrowser addTabViewItem: newTab];
  460: 
  461:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  462: 
  463:     [mTabBrowser selectLastTabViewItem: self];
  464: 
  465:     if ( [[[self window] toolbar] isVisible] ) {
  466:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  467:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  468:             [self focusURLBar];
  469:         }
  470:     }
  471: }
  472: 
  473: -(void)closeTab
  474: {
  475:     if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
  476:         [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
  477:     }
  478: }
  479: 
  480: - (void)previousTab
  481: {
  482:     [mTabBrowser selectPreviousTabViewItem:self];
  483: }
  484: 
  485: - (void)nextTab
  486: {
  487:     [mTabBrowser selectNextTabViewItem:self];
  488: }
  489: 
  490: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
  491: {
  492:     // Disconnect the old view, if one has been designated.
  493:     // If the window has just been opened, none has been.
  494:     if ( mBrowserView ) {
  495:         [mBrowserView disconnectView];
  496:     }
  497:     // Connect up the new view
  498:     mBrowserView = [aTabViewItem view];
  499:        
  500:     // Make the new view the primary content area.
  501:     [mBrowserView makePrimaryBrowserView: aTabViewItem urlbar: mURLBar status: mStatus
  502:         progress: mProgress windowController: self];
  503: }
  504: 
  505: -(MyBrowserView*)getMyBrowserView
  506: {
  507:     return mBrowserView;
  508: }
  509: 
  510: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
  511: {
  512:     BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  513:     [browser loadURL: aURL];
  514: 	[browser showWindow: self];
  515: }
  516: @end
  517: 

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