File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.73: download - view: text, annotated - select for diffs - revision graph
Sat Apr 20 00:02:55 2002 UTC (17 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Make hidden tabs actually able to work with Gecko (for background tab loading and bookmark groups).  Requires an update of the Mozilla tree.

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

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