File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.64: download - view: text, annotated - select for diffs - revision graph
Tue Apr 16 00:28:33 2002 UTC (17 years, 6 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
implemented text zoom.

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

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