File:  [mozdev] / chimera / BrowserWindowController.mm
Revision 1.66: download - view: text, annotated - select for diffs - revision graph
Wed Apr 17 07:38:31 2002 UTC (17 years ago) by hyatt
Branches: MAIN
CVS tags: HEAD
New bookmark icon.  WOrking on the personal toolbar.

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

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