File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.61: download - view: text, annotated - select for diffs - revision graph
Sun Apr 14 00:08:17 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Open link in new window/tab now works from the context menu.

    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: #import "IconTabViewItem.h"
   41: 
   42: #include "nsIWebNavigation.h"
   43: #include "nsIDOMElement.h"
   44: #include "nsIDOMEvent.h"
   45: #include "nsIPrefBranch.h"
   46: #include "nsIContextMenuListener.h"
   47: #include "nsCocoaBrowserService.h"
   48: #include "nsString.h"
   49: #include "GeckoUtils.h"
   50: 
   51: static NSString *BrowserToolbarIdentifier	= @"Browser Window Toolbar";
   52: static NSString *BackToolbarItemIdentifier	= @"Back Toolbar Item";
   53: static NSString *ForwardToolbarItemIdentifier	= @"Forward Toolbar Item";
   54: static NSString *ReloadToolbarItemIdentifier	= @"Reload Toolbar Item";
   55: static NSString *StopToolbarItemIdentifier	= @"Stop Toolbar Item";
   56: static NSString *HomeToolbarItemIdentifier	= @"Home Toolbar Item";
   57: static NSString *LocationToolbarItemIdentifier	= @"Location Toolbar Item";
   58: static NSString *SidebarToolbarItemIdentifier	= @"Sidebar Toolbar Item";
   59: static NSString *PrintToolbarItemIdentifier	= @"Print Toolbar Item";
   60: 
   61: @interface BrowserWindowController(Private)
   62: - (void)setupToolbar;
   63: - (void)setupSidebarTabs;
   64: @end
   65: 
   66: @implementation BrowserWindowController
   67: 
   68: -(void)enterModalSession
   69: {
   70:     mModalSession = [NSApp beginModalSessionForWindow: [self window]];
   71:     [NSApp runModalSession: mModalSession];
   72:     [NSApp endModalSession: mModalSession];
   73:     mModalSession = nil;
   74: }
   75: 
   76: -(void)windowDidBecomeKey: (NSNotification*)aNotification
   77: {
   78:   // May become necessary later.
   79: }
   80: 
   81: -(void)windowDidResignKey: (NSNotification*)aNotification
   82: {
   83:   // May be needed later.
   84: }
   85: 
   86: -(void)mouseMoved:(NSEvent*)aEvent
   87: {
   88:     if (mMoveReentrant)
   89:         return;
   90:         
   91:     mMoveReentrant = YES;
   92:     NSView* view = [[[self window] contentView] hitTest: [aEvent locationInWindow]];
   93:     [view mouseMoved: aEvent];
   94:     [super mouseMoved: aEvent];
   95:     mMoveReentrant = NO;
   96: }
   97: 
   98: - (id)initWithWindowNibName:(NSString *)windowNibName
   99: {
  100:     if ( (self = [super initWithWindowNibName:(NSString *)windowNibName]) ) {
  101:         if ( nsCocoaBrowserService::sNumBrowsers == 0 ) {
  102:             [self setShouldCascadeWindows:NO];
  103:         } else {
  104:             [self setShouldCascadeWindows:YES];
  105:         }
  106:         mInitialized = NO;
  107:         mMoveReentrant = NO;
  108:         mShouldAutosave = YES;
  109:         mChromeMask = 0;
  110:         mContextMenuFlags = 0;
  111:         mContextMenuEvent = nsnull;
  112:         mContextMenuNode = nsnull;
  113:     }
  114:     return self;
  115: }
  116: 
  117: -(void)autosaveWindowFrame
  118: {
  119:   if (mShouldAutosave)
  120:     [[self window] saveFrameUsingName: @"NavigatorWindow"];
  121: }
  122: 
  123: -(void)disableAutosave
  124: {
  125:   mShouldAutosave = NO;
  126: }
  127: 
  128: - (void)windowWillClose:(NSNotification *)notification
  129: {
  130:   printf("Window will close notification.\n");
  131:   [mSidebarBookmarksDataSource windowClosing];
  132: 
  133:   [self autosaveWindowFrame];
  134:   [self autorelease];
  135: }
  136: 
  137: - (void)dealloc
  138: {
  139:   printf("Browser controller died.\n");
  140: 
  141:   // Loop over all tabs, and tell them that the window is closed.
  142:   int numTabs = [mTabBrowser numberOfTabViewItems];
  143:   for (int i = 0; i < numTabs; i++) {
  144:     NSTabViewItem* item = [mTabBrowser tabViewItemAtIndex: i];
  145:     [[item view] windowClosed];
  146:   }
  147:   [mSidebarBrowserView windowClosed];
  148: 
  149:   [mProgress release];
  150:   
  151:   [super dealloc];
  152: }
  153: 
  154: - (void)windowDidLoad
  155: {
  156:     [super windowDidLoad];
  157: 
  158:     // Get our saved dimensions.
  159:     [[self window] setFrameUsingName: @"NavigatorWindow"];
  160:     
  161:     if (mModalSession)
  162:       [NSApp stopModal: mModalSession];
  163:       
  164:     mInitialized = YES;
  165: 
  166:     mDrawerCachedFrame = NO;
  167:     
  168:     // Retain with a single extra refcount.  This allows the MyBrowserViews
  169:     // to remove the progress meter from its superview without having to 
  170:     // worry about retaining and releasing it.
  171:     [mProgress retain];
  172:     
  173:     [[self window] setAcceptsMouseMovedEvents: YES];
  174:     
  175:     [self setupToolbar];
  176: 
  177: //  03/03/2002 mlj Changing strategy a bit here.  The addTab: method was
  178: //	duplicating a lot of the code found here.  I have moved it to that method.
  179: //	We now remove the IB tab, then add one of our own.
  180: 
  181:     [mTabBrowser removeTabViewItem:[mTabBrowser tabViewItemAtIndex:0]];
  182:     [self newTab];
  183:     
  184:     if (mURL) {
  185:       [self loadURL: mURL];
  186:       [mURL release];
  187:     }
  188:     
  189:     [mSidebarDrawer setDelegate: self];
  190: 
  191:     [self setupSidebarTabs];
  192: }
  193: 
  194: - (void)drawerWillOpen: (NSNotification*)aNotification
  195: {
  196:   [mSidebarBookmarksDataSource ensureBookmarks];
  197: 
  198:   // Force the window to shrink and move if necessary in order to accommodate the sidebar.
  199:   NSRect screenFrame = [[[self window] screen] frame];
  200:   NSRect windowFrame = [[self window] frame];
  201:   NSSize drawerSize = [mSidebarDrawer contentSize];
  202:   int fudgeFactor = 12; // Not sure how to get the drawer's border info, so we fudge it for now.
  203:   drawerSize.width += fudgeFactor;
  204:   if (windowFrame.origin.x + windowFrame.size.width + drawerSize.width >
  205:       screenFrame.size.width) {
  206:     // We need to adjust the window so that it can fit.
  207:     int shrinkDelta = (windowFrame.size.width + drawerSize.width) - screenFrame.size.width;
  208:     if (shrinkDelta < 0) shrinkDelta = 0;
  209:     int newWidth = (windowFrame.size.width - shrinkDelta);
  210:     int newPosition = screenFrame.size.width - newWidth - drawerSize.width;
  211:     if (newPosition < 0) newPosition = 0;
  212:     mCachedFrameBeforeDrawerOpen = windowFrame;
  213:     windowFrame.origin.x = newPosition;
  214:     windowFrame.size.width = newWidth;
  215:     mCachedFrameAfterDrawerOpen = windowFrame;
  216:     [[self window] setFrame: windowFrame display: YES];
  217:     mDrawerCachedFrame = YES;
  218:   }
  219: }
  220: 
  221: - (void)drawerDidOpen:(NSNotification *)aNotification
  222: {
  223:   // XXXdwh This is temporary.
  224:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
  225: 
  226:   // Toggle the sidebar icon.
  227:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarOpened"]];
  228: }
  229: 
  230: - (void)drawerDidClose:(NSNotification *)aNotification
  231: {
  232:   // Unload the Gecko web page in "My Panels" to save memory.
  233:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
  234: 
  235:   // XXXdwh ignore for now.
  236:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
  237: 
  238:   if (mDrawerCachedFrame) {
  239:     printf("Got here.\n");
  240:     mDrawerCachedFrame = NO;
  241:     NSRect frame = [[self window] frame];
  242:     if (frame.origin.x == mCachedFrameAfterDrawerOpen.origin.x &&
  243:         frame.origin.y == mCachedFrameAfterDrawerOpen.origin.y &&
  244:         frame.size.width == mCachedFrameAfterDrawerOpen.size.width &&
  245:         frame.size.height == mCachedFrameAfterDrawerOpen.size.height) {
  246:       printf("Got here too.\n");
  247:       printf("Xes are %d %d\n", frame.origin.x, mCachedFrameAfterDrawerOpen.origin.x);
  248:       printf("Widths are %d %d\n", frame.size.width, mCachedFrameAfterDrawerOpen.size.width);
  249:       // Restore the original frame.
  250:       [[self window] setFrame: mCachedFrameBeforeDrawerOpen display: YES];
  251:     }
  252:   }
  253: }
  254: 
  255: - (void)setupToolbar
  256: {
  257:   if (mChromeMask) {
  258:     printf("Uh-oh. %d\n", mChromeMask);
  259:   }
  260:   
  261:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
  262:     
  263:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
  264:     [toolbar setAllowsUserCustomization:YES];
  265:     [toolbar setAutosavesConfiguration:YES];
  266:     [toolbar setDelegate:self];
  267:     [[self window] setToolbar:toolbar];
  268: }
  269: 
  270: 
  271: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
  272: {
  273:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  274:                                         ForwardToolbarItemIdentifier,
  275:                                         ReloadToolbarItemIdentifier,
  276:                                         StopToolbarItemIdentifier,
  277:                                         HomeToolbarItemIdentifier,
  278:                                         LocationToolbarItemIdentifier,
  279:                                         SidebarToolbarItemIdentifier,
  280:                                         PrintToolbarItemIdentifier,
  281:                                         NSToolbarCustomizeToolbarItemIdentifier,
  282:                                         NSToolbarFlexibleSpaceItemIdentifier,
  283:                                         NSToolbarSpaceItemIdentifier,
  284:                                         NSToolbarSeparatorItemIdentifier,
  285:                                         nil];
  286: }
  287: 
  288: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
  289: {
  290:     return [NSArray arrayWithObjects:	BackToolbarItemIdentifier,
  291:                                         ForwardToolbarItemIdentifier,
  292:                                         ReloadToolbarItemIdentifier,
  293:                                         StopToolbarItemIdentifier,
  294:                                         HomeToolbarItemIdentifier,
  295:                                         LocationToolbarItemIdentifier,
  296:                                         SidebarToolbarItemIdentifier,
  297:                                         nil];
  298: }
  299: 
  300: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
  301:       itemForItemIdentifier:(NSString *)itemIdent
  302:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
  303: {
  304:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
  305:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
  306:         [toolbarItem setLabel:@"Back"];
  307:         [toolbarItem setPaletteLabel:@"Go Back"];
  308:         [toolbarItem setToolTip:@"Go back one page"];
  309:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
  310:         [toolbarItem setTarget:self];
  311:         [toolbarItem setAction:@selector(back:)];
  312:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
  313:         [toolbarItem setLabel:@"Forward"];
  314:         [toolbarItem setPaletteLabel:@"Go Forward"];
  315:         [toolbarItem setToolTip:@"Go forward one page"];
  316:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
  317:         [toolbarItem setTarget:self];
  318:         [toolbarItem setAction:@selector(forward:)];
  319:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
  320:         [toolbarItem setLabel:@"Reload"];
  321:         [toolbarItem setPaletteLabel:@"Reload Page"];
  322:         [toolbarItem setToolTip:@"Reload current page"];
  323:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
  324:         [toolbarItem setTarget:self];
  325:         [toolbarItem setAction:@selector(reload:)];
  326:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
  327:         [toolbarItem setLabel:@"Stop"];
  328:         [toolbarItem setPaletteLabel:@"Stop Loading"];
  329:         [toolbarItem setToolTip:@"Stop loading this page"];
  330:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
  331:         [toolbarItem setTarget:self];
  332:         [toolbarItem setAction:@selector(stop:)];
  333:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
  334:         [toolbarItem setLabel:@"Home"];
  335:         [toolbarItem setPaletteLabel:@"Go Home"];
  336:         [toolbarItem setToolTip:@"Go to home page"];
  337:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
  338:         [toolbarItem setTarget:self];
  339:         [toolbarItem setAction:@selector(home:)];
  340:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
  341:         [toolbarItem setLabel:@"Sidebar"];
  342:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
  343:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
  344:         [toolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
  345:         [toolbarItem setTarget:self];
  346:         [toolbarItem setAction:@selector(toggleSidebar:)];
  347:         mSidebarToolbarItem = toolbarItem;
  348:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
  349:         
  350:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
  351:         
  352:         [toolbarItem setLabel:@"Location"];
  353:         [toolbarItem setPaletteLabel:@"Location"];
  354:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
  355:         [toolbarItem setView:mLocationToolbarView];
  356:         [toolbarItem setMinSize:NSMakeSize(128,32)];
  357:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
  358:         
  359:         [menuFormRep setTarget:self];
  360:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
  361:         [menuFormRep setTitle:[toolbarItem label]];
  362:         
  363:         [toolbarItem setMenuFormRepresentation:menuFormRep];
  364:         mLocationToolbarItem = toolbarItem;
  365: 
  366:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
  367:         [toolbarItem setLabel:@"Print"];
  368:         [toolbarItem setPaletteLabel:@"Print"];
  369:         [toolbarItem setToolTip:@"Print this page"];
  370:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
  371:         [toolbarItem setTarget:self];
  372:         [toolbarItem setAction:@selector(printDocument)];
  373:     } else {
  374:         toolbarItem = nil;
  375:     }
  376:         
  377:     return toolbarItem;
  378: }
  379: 
  380: // This method handles the enabling/disabling of the toolbar buttons.
  381: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
  382: {
  383:   // Check the action and see if it matches.
  384:   SEL action = [theItem action];
  385:   if (action == @selector(back:))
  386:     return [[mBrowserView getBrowserView] canGoBack];
  387:   else if (action == @selector(forward:))
  388:     return [[mBrowserView getBrowserView] canGoForward];
  389:   else if (action == @selector(reload:))
  390:     return [mBrowserView isBusy] == NO;
  391:   else if (action == @selector(stop:))
  392:     return [mBrowserView isBusy];
  393:   else
  394:     return YES;
  395: }
  396:    
  397: - (void)updateToolbarItems
  398: {
  399:   [[[self window] toolbar] validateVisibleItems];
  400: }
  401: 
  402: - (void)performAppropriateLocationAction
  403: {
  404:     if ( [[[self window] toolbar] isVisible] ) {
  405:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  406:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  407:             [self focusURLBar];
  408:         } else {
  409:             [self beginLocationSheet];
  410:         }
  411:     } else {
  412:         [self beginLocationSheet];
  413:     }
  414: }
  415: 
  416: - (void)focusURLBar
  417: {
  418:     [mURLBar selectText: self];
  419: }
  420: 
  421: - (void)beginLocationSheet
  422: {
  423:     [NSApp beginSheet:	mLocationSheetWindow
  424:        modalForWindow:	[self window]
  425:         modalDelegate:	nil
  426:        didEndSelector:	nil
  427:           contextInfo:	nil];
  428: }
  429: 
  430: - (IBAction)endLocationSheet:(id)sender
  431: {
  432:     [mLocationSheetWindow orderOut:self];
  433:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
  434:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
  435:     
  436:     // Focus and activate our content area.
  437:     [[mBrowserView getBrowserView] setActive: YES];
  438: }
  439: 
  440: - (IBAction)goToLocationFromToolbarURLField:(id)sender
  441: {
  442:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
  443:     
  444:     // Focus and activate our content area.
  445:     [[mBrowserView getBrowserView] setActive: YES];
  446: }
  447: 
  448: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  449: {
  450:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
  451: }
  452: 
  453: - (void)printDocument
  454: {
  455:     [[mBrowserView getBrowserView] printDocument];
  456: }
  457: 
  458: - (void)printPreview
  459: {
  460:     [[mBrowserView getBrowserView] printPreview];
  461: }
  462: 
  463: - (BOOL)findInPage:(NSString*)text
  464: {
  465:     return [[mBrowserView getBrowserView] findInPage:text];
  466: }
  467: 
  468: - (void)addBookmark
  469: {
  470:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
  471:     // menu data source going.
  472:     [mSidebarBookmarksDataSource ensureBookmarks];
  473:     [mSidebarBookmarksDataSource addBookmark: self];
  474: }
  475: 
  476: - (IBAction)back:(id)aSender
  477: {
  478:   [[mBrowserView getBrowserView] goBack];
  479: }
  480: 
  481: - (IBAction)forward:(id)aSender
  482: {
  483:   [[mBrowserView getBrowserView] goForward];
  484: }
  485: 
  486: - (IBAction)reload:(id)aSender
  487: {
  488:   [[mBrowserView getBrowserView] reload: 0];
  489: }
  490: 
  491: - (IBAction)stop:(id)aSender
  492: {
  493:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
  494: }
  495: 
  496: - (IBAction)home:(id)aSender
  497: {
  498:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  499: }
  500: 
  501: - (IBAction)toggleSidebar:(id)aSender
  502: {
  503:     NSResponder* resp = [[self window] firstResponder];
  504:     [[self window] makeFirstResponder: nil];
  505:     
  506:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) 	{
  507:         // XXXHack to bypass sidebar crashes.
  508:         [mSidebarDrawer openOnEdge: NSMaxXEdge];
  509:     } else {
  510:         [mSidebarDrawer close];
  511:     }
  512:     
  513:     [[self window] makeFirstResponder: resp];
  514: }
  515: 
  516: 
  517: -(void)loadURL:(NSURL*)aURL
  518: {
  519:     if (mInitialized) {
  520:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  521:     }
  522:     else {
  523:         mURL = aURL;
  524:         [mURL retain];
  525:     }
  526: }
  527: 
  528: - (void)updateLocationFields:(NSString *)locationString
  529: {
  530: /* //commenting this out because it doesn't work right yet.
  531:     if ( [locationString length] > 30 ) {
  532:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  533:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  534:     } else {
  535:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  536:             [NSString stringWithFormat:@"Location: %@", locationString]];
  537:     }
  538: */
  539: 
  540:     [mURLBar setStringValue:locationString];
  541:     [mLocationSheetURLField setStringValue:locationString];
  542: 
  543:     [[self window] update];
  544:     [[self window] display];
  545: }
  546: 
  547: -(void)newTab
  548: {
  549:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  550:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  551:     [newView setTab: newTab];
  552: 
  553:     [newTab setLabel: @"Untitled"];
  554:     [newTab setView: newView];
  555: 
  556:     [mTabBrowser addTabViewItem: newTab];
  557: 
  558:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  559: 
  560:     [mTabBrowser selectLastTabViewItem: self];
  561: 
  562:     if ( [[[self window] toolbar] isVisible] ) {
  563:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  564:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  565:           if ([mTabBrowser numberOfTabViewItems] > 1) {
  566:             [self focusURLBar];
  567:             [[mBrowserView getBrowserView] setActive: NO];
  568:           }
  569:           else if ([[self window] isKeyWindow])
  570:             [[mBrowserView getBrowserView] setActive: YES];
  571:           else
  572:             [[mBrowserView getBrowserView] setActive: NO];
  573:         }
  574:     }
  575: }
  576: 
  577: -(void)closeTab
  578: {
  579:   if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
  580:     [[[mTabBrowser selectedTabViewItem] view] windowClosed];
  581:     [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
  582:   }
  583: }
  584: 
  585: - (void)previousTab
  586: {
  587:     [mTabBrowser selectPreviousTabViewItem:self];
  588: }
  589: 
  590: - (void)nextTab
  591: {
  592:     [mTabBrowser selectNextTabViewItem:self];
  593: }
  594: 
  595: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
  596: {
  597:     // Disconnect the old view, if one has been designated.
  598:     // If the window has just been opened, none has been.
  599:     if ( mBrowserView ) {
  600:         [mBrowserView disconnectView];
  601:     }
  602:     // Connect up the new view
  603:     mBrowserView = [aTabViewItem view];
  604:        
  605:     // Make the new view the primary content area.
  606:     [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
  607:         progress: mProgress windowController: self];
  608: }
  609: 
  610: -(MyBrowserView*)getMyBrowserView
  611: {
  612:     return mBrowserView;
  613: }
  614: 
  615: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
  616: {
  617:   // Autosave our dimensions before we open a new window.  That ensures the size ends up matching.
  618:   [self autosaveWindowFrame];
  619: 
  620:   BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  621:   [browser loadURL: aURL];
  622:   if (aLoadInBG)
  623:     [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
  624:   else
  625:     // Focus the content area and show the window.
  626:     [browser showWindow: self];
  627: }
  628: 
  629: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
  630: {
  631:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  632:     
  633:     NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
  634:     int index = [mTabBrowser indexOfTabViewItem: selectedTab];
  635:     [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
  636:     
  637:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  638:     [newView setTab: newTab];
  639:     
  640:     [newTab setLabel: @"Loading..."];
  641:     [newTab setView: newView];
  642: 
  643:     [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  644: 
  645:     if (!aLoadInBG) {
  646:       [mTabBrowser selectTabViewItem: newTab];
  647:       // Focus the content area.
  648:       [[newView getBrowserView] setActive: YES];
  649:     }
  650: }
  651: 
  652: -(void)setupSidebarTabs
  653: {
  654:     IconTabViewItem   *bookItem = [[IconTabViewItem alloc] initWithIdentifier:@"bookmarkSidebarIconTabViewItem"
  655:                                   withTabIcon:[NSImage imageNamed:@"bookicon"]];
  656:     IconTabViewItem   *histItem = [[IconTabViewItem alloc] initWithIdentifier:@"historySidebarIconTabViewItem"
  657:                                   withTabIcon:[NSImage imageNamed:@"historyicon"]];
  658:     IconTabViewItem *searchItem = [[IconTabViewItem alloc] initWithIdentifier:@"searchSidebarIconTabViewItem"
  659:                                   withTabIcon:[NSImage imageNamed:@"searchicon"]];
  660: 
  661:     [bookItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:0] view]];
  662:     [histItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:1] view]];
  663:     [searchItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:2] view]];
  664: 
  665:     [mSidebarTabView insertTabViewItem:bookItem   atIndex:0];
  666:     [mSidebarTabView insertTabViewItem:histItem   atIndex:1];
  667:     [mSidebarTabView insertTabViewItem:searchItem atIndex:2];
  668: 
  669:     [mSidebarTabView selectFirstTabViewItem:self];
  670: }
  671: 
  672: -(void)setChromeMask:(int)aMask
  673: {
  674:   mChromeMask = aMask;
  675: }
  676: 
  677: // Called when a context menu should be shown.
  678: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
  679: {
  680:   mContextMenuFlags = flags;
  681:   mContextMenuNode = aNode;
  682:   mContextMenuEvent = aEvent;
  683: }
  684: 
  685: - (NSMenu*)getContextMenu
  686: {
  687:   NSMenu* result = nil;
  688:   if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_LINK) != 0) {
  689:     if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
  690:       result = mImageLinkMenu;
  691:     }
  692:     else
  693:       result = mLinkMenu;
  694:   }
  695:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_INPUT) != 0 ||
  696:            (mContextMenuFlags & nsIContextMenuListener::CONTEXT_TEXT) != 0) {
  697:     result = mInputMenu;
  698:   }
  699:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
  700:     result = mImageMenu;
  701:   }
  702:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_DOCUMENT) != 0) {
  703:     result = mPageMenu;
  704:   }
  705:   
  706:   return result;
  707: }
  708: 
  709: // Context menu methods
  710: - (IBAction)openLinkInNewWindow:(id)aSender
  711: {
  712:   [self openLinkInNewWindowOrTab: YES];
  713: }
  714: 
  715: - (IBAction)openLinkInNewTab:(id)aSender
  716: {
  717:   [self openLinkInNewWindowOrTab: NO];
  718: }
  719: 
  720: -(void)openLinkInNewWindowOrTab: (BOOL)aUseWindow
  721: {
  722:   nsCOMPtr<nsIDOMElement> linkContent;
  723:   nsAutoString href;
  724:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
  725: 
  726:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
  727:   // really uses these anyway? :)
  728:   if (!linkContent || href.IsEmpty())
  729:     return;
  730: 
  731:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
  732:   if (!pref)
  733:     return; // Something bad happened if we can't get prefs.
  734: 
  735:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
  736:   NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
  737: 
  738:   PRBool loadInBackground;
  739:   pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
  740: 
  741:   if (aUseWindow)
  742:     [self openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
  743:   else
  744:     [self openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
  745: }
  746: 
  747: - (IBAction)saveLinkAs:(id)aSender
  748: {
  749: }
  750: 
  751: - (IBAction)saveImageAs:(id)aSender
  752: {
  753: }
  754: 
  755: @end
  756: 

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