File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.63: download - view: text, annotated - select for diffs - revision graph
Sun Apr 14 21:41:21 2002 UTC (17 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
MOre stuff.

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

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