File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.62: download - view: text, annotated - select for diffs - revision graph
Sun Apr 14 08:43:03 2002 UTC (17 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
So many changes I can't even go into it. :)

    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)saveURL: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
  454:             url: (NSURL*)aURL suggestedFilename: (NSString*)aFilename
  455: {
  456:   [[mBrowserView getBrowserView] saveURL: aFilterView filterList: aFilterList
  457:                                      url: aURL suggestedFilename: aFilename];
  458: }
  459: 
  460: - (void)printDocument
  461: {
  462:     [[mBrowserView getBrowserView] printDocument];
  463: }
  464: 
  465: - (void)printPreview
  466: {
  467:     [[mBrowserView getBrowserView] printPreview];
  468: }
  469: 
  470: - (BOOL)findInPage:(NSString*)text
  471: {
  472:     return [[mBrowserView getBrowserView] findInPage:text];
  473: }
  474: 
  475: - (void)addBookmarkExtended: (BOOL)aIsFromMenu
  476: {
  477:   [mSidebarBookmarksDataSource ensureBookmarks];
  478:   [mSidebarBookmarksDataSource addBookmark: self useSelection: !aIsFromMenu];
  479: }
  480: 
  481: - (IBAction)back:(id)aSender
  482: {
  483:   [[mBrowserView getBrowserView] goBack];
  484: }
  485: 
  486: - (IBAction)forward:(id)aSender
  487: {
  488:   [[mBrowserView getBrowserView] goForward];
  489: }
  490: 
  491: - (IBAction)reload:(id)aSender
  492: {
  493:   [[mBrowserView getBrowserView] reload: 0];
  494: }
  495: 
  496: - (IBAction)stop:(id)aSender
  497: {
  498:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
  499: }
  500: 
  501: - (IBAction)home:(id)aSender
  502: {
  503:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  504: }
  505: 
  506: - (IBAction)toggleSidebar:(id)aSender
  507: {
  508:     NSResponder* resp = [[self window] firstResponder];
  509:     [[self window] makeFirstResponder: nil];
  510:     
  511:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) 	{
  512:         // XXXHack to bypass sidebar crashes.
  513:         [mSidebarDrawer openOnEdge: NSMaxXEdge];
  514:     } else {
  515:         [mSidebarDrawer close];
  516:     }
  517:     
  518:     [[self window] makeFirstResponder: resp];
  519: }
  520: 
  521: 
  522: -(void)loadURL:(NSURL*)aURL
  523: {
  524:     if (mInitialized) {
  525:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  526:     }
  527:     else {
  528:         mURL = aURL;
  529:         [mURL retain];
  530:     }
  531: }
  532: 
  533: - (void)updateLocationFields:(NSString *)locationString
  534: {
  535: /* //commenting this out because it doesn't work right yet.
  536:     if ( [locationString length] > 30 ) {
  537:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  538:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
  539:     } else {
  540:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
  541:             [NSString stringWithFormat:@"Location: %@", locationString]];
  542:     }
  543: */
  544: 
  545:     [mURLBar setStringValue:locationString];
  546:     [mLocationSheetURLField setStringValue:locationString];
  547: 
  548:     [[self window] update];
  549:     [[self window] display];
  550: }
  551: 
  552: -(void)newTab
  553: {
  554:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  555:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  556:     [newView setTab: newTab];
  557: 
  558:     [newTab setLabel: @"Untitled"];
  559:     [newTab setView: newView];
  560: 
  561:     [mTabBrowser addTabViewItem: newTab];
  562: 
  563:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
  564: 
  565:     [mTabBrowser selectLastTabViewItem: self];
  566: 
  567:     if ( [[[self window] toolbar] isVisible] ) {
  568:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
  569:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
  570:           if ([mTabBrowser numberOfTabViewItems] > 1) {
  571:             [self focusURLBar];
  572:             [[mBrowserView getBrowserView] setActive: NO];
  573:           }
  574:           else if ([[self window] isKeyWindow])
  575:             [[mBrowserView getBrowserView] setActive: YES];
  576:           else
  577:             [[mBrowserView getBrowserView] setActive: NO];
  578:         }
  579:     }
  580: }
  581: 
  582: -(void)closeTab
  583: {
  584:   if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
  585:     [[[mTabBrowser selectedTabViewItem] view] windowClosed];
  586:     [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
  587:   }
  588: }
  589: 
  590: - (void)previousTab
  591: {
  592:     [mTabBrowser selectPreviousTabViewItem:self];
  593: }
  594: 
  595: - (void)nextTab
  596: {
  597:     [mTabBrowser selectNextTabViewItem:self];
  598: }
  599: 
  600: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
  601: {
  602:     // Disconnect the old view, if one has been designated.
  603:     // If the window has just been opened, none has been.
  604:     if ( mBrowserView ) {
  605:         [mBrowserView disconnectView];
  606:     }
  607:     // Connect up the new view
  608:     mBrowserView = [aTabViewItem view];
  609:        
  610:     // Make the new view the primary content area.
  611:     [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
  612:         progress: mProgress windowController: self];
  613: }
  614: 
  615: -(MyBrowserView*)getMyBrowserView
  616: {
  617:     return mBrowserView;
  618: }
  619: 
  620: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
  621: {
  622:   // Autosave our dimensions before we open a new window.  That ensures the size ends up matching.
  623:   [self autosaveWindowFrame];
  624: 
  625:   BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  626:   [browser loadURL: aURL];
  627:   if (aLoadInBG)
  628:     [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
  629:   else {
  630:     // Focus the content area and show the window.
  631:     [browser enterModalSession];
  632:     [[[browser getMyBrowserView] getBrowserView] setActive: YES];
  633:   }
  634: }
  635: 
  636: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
  637: {
  638:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
  639:     
  640:     NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
  641:     int index = [mTabBrowser indexOfTabViewItem: selectedTab];
  642:     [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
  643:     
  644:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
  645:     [newView setTab: newTab];
  646:     
  647:     [newTab setLabel: @"Loading..."];
  648:     [newTab setView: newView];
  649: 
  650:     [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
  651: 
  652:     if (!aLoadInBG) {
  653:       [mTabBrowser selectTabViewItem: newTab];
  654:       // Focus the content area.
  655:       [[newView getBrowserView] setActive: YES];
  656:     }
  657: }
  658: 
  659: -(void)setupSidebarTabs
  660: {
  661:     IconTabViewItem   *bookItem = [[IconTabViewItem alloc] initWithIdentifier:@"bookmarkSidebarIconTabViewItem"
  662:                                   withTabIcon:[NSImage imageNamed:@"bookicon"]];
  663:     IconTabViewItem   *histItem = [[IconTabViewItem alloc] initWithIdentifier:@"historySidebarIconTabViewItem"
  664:                                   withTabIcon:[NSImage imageNamed:@"historyicon"]];
  665:     IconTabViewItem *searchItem = [[IconTabViewItem alloc] initWithIdentifier:@"searchSidebarIconTabViewItem"
  666:                                   withTabIcon:[NSImage imageNamed:@"searchicon"]];
  667: 
  668:     [bookItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:0] view]];
  669:     [histItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:1] view]];
  670:     [searchItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:2] view]];
  671: 
  672:     [mSidebarTabView insertTabViewItem:bookItem   atIndex:0];
  673:     [mSidebarTabView insertTabViewItem:histItem   atIndex:1];
  674:     [mSidebarTabView insertTabViewItem:searchItem atIndex:2];
  675: 
  676:     [mSidebarTabView selectFirstTabViewItem:self];
  677: }
  678: 
  679: -(void)setChromeMask:(int)aMask
  680: {
  681:   mChromeMask = aMask;
  682: }
  683: 
  684: // Called when a context menu should be shown.
  685: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
  686: {
  687:   mContextMenuFlags = flags;
  688:   mContextMenuNode = aNode;
  689:   mContextMenuEvent = aEvent;
  690: }
  691: 
  692: - (NSMenu*)getContextMenu
  693: {
  694:   NSMenu* result = nil;
  695:   if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_LINK) != 0) {
  696:     if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
  697:       result = mImageLinkMenu;
  698:     }
  699:     else
  700:       result = mLinkMenu;
  701:   }
  702:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_INPUT) != 0 ||
  703:            (mContextMenuFlags & nsIContextMenuListener::CONTEXT_TEXT) != 0) {
  704:     result = mInputMenu;
  705:   }
  706:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
  707:     result = mImageMenu;
  708:   }
  709:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_DOCUMENT) != 0) {
  710:     result = mPageMenu;
  711:   }
  712:   
  713:   return result;
  714: }
  715: 
  716: // Context menu methods
  717: - (IBAction)openLinkInNewWindow:(id)aSender
  718: {
  719:   [self openLinkInNewWindowOrTab: YES];
  720: }
  721: 
  722: - (IBAction)openLinkInNewTab:(id)aSender
  723: {
  724:   [self openLinkInNewWindowOrTab: NO];
  725: }
  726: 
  727: -(void)openLinkInNewWindowOrTab: (BOOL)aUseWindow
  728: {
  729:   nsCOMPtr<nsIDOMElement> linkContent;
  730:   nsAutoString href;
  731:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
  732: 
  733:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
  734:   // really uses these anyway? :)
  735:   if (!linkContent || href.IsEmpty())
  736:     return;
  737: 
  738:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
  739:   if (!pref)
  740:     return; // Something bad happened if we can't get prefs.
  741: 
  742:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
  743:   NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
  744: 
  745:   PRBool loadInBackground;
  746:   pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
  747: 
  748:   if (aUseWindow)
  749:     [self openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
  750:   else
  751:     [self openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
  752: }
  753: 
  754: - (IBAction)savePageAs:(id)aSender
  755: {
  756:   [self saveDocument: nil filterList: nil];
  757: }
  758: 
  759: - (IBAction)saveLinkAs:(id)aSender
  760: {
  761:   nsCOMPtr<nsIDOMElement> linkContent;
  762:   nsAutoString href;
  763:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
  764: 
  765:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
  766:   // really uses these anyway? :)
  767:   if (!linkContent || href.IsEmpty())
  768:     return;
  769: 
  770:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
  771:   NSURL* urlToSave = [NSURL URLWithString: hrefStr];
  772: 
  773:   // The user wants to save this link.
  774:   nsAutoString text;
  775:   GeckoUtils::GatherTextUnder(mContextMenuNode, text);
  776: 
  777:   [self saveURL: nil filterList: nil
  778:             url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
  779:                                                                       length:nsCRT::strlen(text.get())]];
  780: }
  781: 
  782: - (IBAction)saveImageAs:(id)aSender
  783: {
  784:   nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
  785:   if (imgElement) {
  786:       nsAutoString text;
  787:       imgElement->GetAttribute(NS_LITERAL_STRING("src"), text);
  788:       nsAutoString url;
  789:       imgElement->GetSrc(url);
  790: 
  791:       NSString* hrefStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
  792:       NSURL* urlToSave = [NSURL URLWithString: hrefStr];
  793: 
  794:       [self saveURL: nil filterList: nil
  795:                 url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
  796:                                                                           length:nsCRT::strlen(text.get())]];
  797:   }
  798: }
  799: 
  800: - (IBAction)viewOnlyThisImage:(id)aSender
  801: {
  802:   nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
  803:   if (imgElement) {
  804:     nsAutoString url;
  805:     imgElement->GetSrc(url);
  806: 
  807:     NSString* urlStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
  808:     NSURL* urlToView = [NSURL URLWithString: urlStr];
  809: 
  810:     [self loadURL: urlToView];
  811: 
  812:     // Focus and activate our content area.
  813:     [[mBrowserView getBrowserView] setActive: YES];
  814:   }  
  815: }
  816: 
  817: @end
  818: 

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