Annotation of chimera/BrowserWindowController.mm, revision 1.68

1.44      hyatt       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 ***** */
1.1       hyatt      37: 
                     38: #import "BrowserWindowController.h"
                     39: #import "MyBrowserView.h"
1.50      macserv    40: #import "IconTabViewItem.h"
1.1       hyatt      41: 
1.37      hyatt      42: #include "nsIWebNavigation.h"
1.61      hyatt      43: #include "nsIDOMElement.h"
1.59      hyatt      44: #include "nsIDOMEvent.h"
1.61      hyatt      45: #include "nsIPrefBranch.h"
1.59      hyatt      46: #include "nsIContextMenuListener.h"
1.64      hyatt      47: #include "nsIDOMWindow.h"
                     48: #include "nsIScriptGlobalObject.h"
                     49: #include "nsIDocShell.h"
                     50: #include "nsIMarkupDocumentViewer.h"
                     51: #include "nsIContentViewer.h"
1.51      macserv    52: #include "nsCocoaBrowserService.h"
1.61      hyatt      53: #include "nsString.h"
                     54: #include "GeckoUtils.h"
1.37      hyatt      55: 
1.1       hyatt      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";
1.11      macserv    64: static NSString *PrintToolbarItemIdentifier  = @"Print Toolbar Item";
1.1       hyatt      65: 
                     66: @interface BrowserWindowController(Private)
                     67: - (void)setupToolbar;
1.52      macserv    68: - (void)setupSidebarTabs;
1.1       hyatt      69: @end
                     70: 
                     71: @implementation BrowserWindowController
                     72: 
1.39      hyatt      73: -(void)enterModalSession
                     74: {
                     75:     mModalSession = [NSApp beginModalSessionForWindow: [self window]];
                     76:     [NSApp runModalSession: mModalSession];
                     77:     [NSApp endModalSession: mModalSession];
                     78:     mModalSession = nil;
                     79: }
                     80: 
1.58      hyatt      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: 
1.35      hyatt      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: 
1.41      macserv   103: - (id)initWithWindowNibName:(NSString *)windowNibName
1.4       hyatt     104: {
1.41      macserv   105:     if ( (self = [super initWithWindowNibName:(NSString *)windowNibName]) ) {
1.51      macserv   106:         if ( nsCocoaBrowserService::sNumBrowsers == 0 ) {
                    107:             [self setShouldCascadeWindows:NO];
                    108:         } else {
                    109:             [self setShouldCascadeWindows:YES];
                    110:         }
1.41      macserv   111:         mInitialized = NO;
                    112:         mMoveReentrant = NO;
1.48      hyatt     113:         mShouldAutosave = YES;
1.58      hyatt     114:         mChromeMask = 0;
1.59      hyatt     115:         mContextMenuFlags = 0;
                    116:         mContextMenuEvent = nsnull;
                    117:         mContextMenuNode = nsnull;
1.41      macserv   118:     }
                    119:     return self;
1.4       hyatt     120: }
                    121: 
1.48      hyatt     122: -(void)autosaveWindowFrame
                    123: {
                    124:   if (mShouldAutosave)
                    125:     [[self window] saveFrameUsingName: @"NavigatorWindow"];
                    126: }
                    127: 
                    128: -(void)disableAutosave
                    129: {
                    130:   mShouldAutosave = NO;
                    131: }
                    132: 
1.17      hyatt     133: - (void)windowWillClose:(NSNotification *)notification
                    134: {
1.49      hyatt     135:   printf("Window will close notification.\n");
                    136:   [mSidebarBookmarksDataSource windowClosing];
1.48      hyatt     137: 
1.49      hyatt     138:   [self autosaveWindowFrame];
                    139:   [self autorelease];
1.17      hyatt     140: }
                    141: 
1.7       hyatt     142: - (void)dealloc
                    143: {
1.49      hyatt     144:   printf("Browser controller died.\n");
1.40      hyatt     145: 
1.49      hyatt     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];
1.7       hyatt     157: }
                    158: 
1.1       hyatt     159: - (void)windowDidLoad
                    160: {
1.35      hyatt     161:     [super windowDidLoad];
1.48      hyatt     162: 
1.67      hyatt     163:   [[mURLBar cell] setImage: [NSImage imageNamed:@"smallbookmark"]];
                    164:   
1.48      hyatt     165:     // Get our saved dimensions.
                    166:     [[self window] setFrameUsingName: @"NavigatorWindow"];
1.35      hyatt     167:     
1.39      hyatt     168:     if (mModalSession)
                    169:       [NSApp stopModal: mModalSession];
                    170:       
1.4       hyatt     171:     mInitialized = YES;
1.57      hyatt     172: 
                    173:     mDrawerCachedFrame = NO;
1.38      hyatt     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:     
1.34      hyatt     180:     [[self window] setAcceptsMouseMovedEvents: YES];
                    181:     
1.1       hyatt     182:     [self setupToolbar];
1.66      hyatt     183:     
1.41      macserv   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];
1.4       hyatt     190:     
                    191:     if (mURL) {
                    192:       [self loadURL: mURL];
                    193:       [mURL release];
                    194:     }
1.18      hyatt     195:     
                    196:     [mSidebarDrawer setDelegate: self];
1.50      macserv   197: 
1.52      macserv   198:     [self setupSidebarTabs];
1.66      hyatt     199: 
                    200:     [mPersonalToolbar initializeToolbar];
1.18      hyatt     201: }
                    202: 
1.22      hyatt     203: - (void)drawerWillOpen: (NSNotification*)aNotification
                    204: {
1.57      hyatt     205:   [mSidebarBookmarksDataSource ensureBookmarks];
                    206: 
                    207:   // Force the window to shrink and move if necessary in order to accommodate the sidebar.
1.65      hyatt     208:   NSRect screenFrame = [[[self window] screen] visibleFrame];
1.57      hyatt     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:     int shrinkDelta = (windowFrame.size.width + drawerSize.width) - screenFrame.size.width;
                    217:     if (shrinkDelta < 0) shrinkDelta = 0;
                    218:     int newWidth = (windowFrame.size.width - shrinkDelta);
                    219:     int 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:   }
1.22      hyatt     228: }
                    229: 
1.18      hyatt     230: - (void)drawerDidOpen:(NSNotification *)aNotification
                    231: {
1.57      hyatt     232:   // XXXdwh This is temporary.
1.56      hyatt     233:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
1.57      hyatt     234: 
                    235:   // Toggle the sidebar icon.
                    236:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarOpened"]];
1.1       hyatt     237: }
                    238: 
1.19      hyatt     239: - (void)drawerDidClose:(NSNotification *)aNotification
                    240: {
1.57      hyatt     241:   // Unload the Gecko web page in "My Panels" to save memory.
                    242:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
                    243: 
                    244:   // XXXdwh ignore for now.
1.56      hyatt     245:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
1.57      hyatt     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 %d %d\n", frame.origin.x, mCachedFrameAfterDrawerOpen.origin.x);
                    257:       printf("Widths are %d %d\n", frame.size.width, mCachedFrameAfterDrawerOpen.size.width);
                    258:       // Restore the original frame.
                    259:       [[self window] setFrame: mCachedFrameBeforeDrawerOpen display: YES];
                    260:     }
                    261:   }
1.19      hyatt     262: }
                    263: 
1.1       hyatt     264: - (void)setupToolbar
                    265: {
1.58      hyatt     266:   if (mChromeMask) {
                    267:     printf("Uh-oh. %d\n", mChromeMask);
                    268:   }
                    269:   
1.1       hyatt     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,
1.11      macserv   289:                                         PrintToolbarItemIdentifier,
1.1       hyatt     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"];
1.54      macserv   353:         [toolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
1.1       hyatt     354:         [toolbarItem setTarget:self];
                    355:         [toolbarItem setAction:@selector(toggleSidebar:)];
1.54      macserv   356:         mSidebarToolbarItem = toolbarItem;
1.1       hyatt     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];
1.5       macserv   369:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
1.1       hyatt     370:         [menuFormRep setTitle:[toolbarItem label]];
                    371:         
                    372:         [toolbarItem setMenuFormRepresentation:menuFormRep];
1.5       macserv   373:         mLocationToolbarItem = toolbarItem;
1.11      macserv   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)];
1.1       hyatt     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:    
1.5       macserv   406: - (void)updateToolbarItems
1.1       hyatt     407: {
                    408:   [[[self window] toolbar] validateVisibleItems];
                    409: }
                    410: 
1.5       macserv   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
1.3       hyatt     426: {
                    427:     [mURLBar selectText: self];
                    428: }
                    429: 
1.5       macserv   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]]];
1.32      hyatt     444:     
                    445:     // Focus and activate our content area.
                    446:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   447: }
                    448: 
1.63      hyatt     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 isOpen])
                    471:     [self toggleSidebar: self];
                    472: 
                    473:   [mSidebarTabView selectFirstTabViewItem:self];
                    474: }
                    475: 
1.5       macserv   476: - (IBAction)goToLocationFromToolbarURLField:(id)sender
                    477: {
                    478:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
1.32      hyatt     479:     
                    480:     // Focus and activate our content area.
                    481:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   482: }
                    483: 
1.7       hyatt     484: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
1.6       hyatt     485: {
1.62      hyatt     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];
1.6       hyatt     494: }
                    495: 
1.12      hyatt     496: - (void)printDocument
                    497: {
                    498:     [[mBrowserView getBrowserView] printDocument];
                    499: }
                    500: 
1.15      hyatt     501: - (void)printPreview
                    502: {
                    503:     [[mBrowserView getBrowserView] printPreview];
                    504: }
                    505: 
1.60      pinkerto  506: - (BOOL)findInPage:(NSString*)text
1.16      hyatt     507: {
1.60      pinkerto  508:     return [[mBrowserView getBrowserView] findInPage:text];
1.22      hyatt     509: }
                    510: 
1.68    ! hyatt     511: - (void)addBookmarkExtended: (BOOL)aIsFromMenu isFolder:(BOOL)aIsFolder
1.22      hyatt     512: {
1.62      hyatt     513:   [mSidebarBookmarksDataSource ensureBookmarks];
1.68    ! hyatt     514:   [mSidebarBookmarksDataSource addBookmark: self useSelection: !aIsFromMenu isFolder: aIsFolder];
1.16      hyatt     515: }
                    516: 
1.5       macserv   517: - (IBAction)back:(id)aSender
1.1       hyatt     518: {
                    519:   [[mBrowserView getBrowserView] goBack];
                    520: }
                    521: 
1.5       macserv   522: - (IBAction)forward:(id)aSender
1.1       hyatt     523: {
                    524:   [[mBrowserView getBrowserView] goForward];
                    525: }
                    526: 
1.5       macserv   527: - (IBAction)reload:(id)aSender
1.1       hyatt     528: {
                    529:   [[mBrowserView getBrowserView] reload: 0];
                    530: }
                    531: 
1.5       macserv   532: - (IBAction)stop:(id)aSender
1.1       hyatt     533: {
1.37      hyatt     534:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
1.1       hyatt     535: }
                    536: 
1.5       macserv   537: - (IBAction)home:(id)aSender
1.1       hyatt     538: {
1.5       macserv   539:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.1       hyatt     540: }
                    541: 
1.5       macserv   542: - (IBAction)toggleSidebar:(id)aSender
1.1       hyatt     543: {
1.36      hyatt     544:     NSResponder* resp = [[self window] firstResponder];
                    545:     [[self window] makeFirstResponder: nil];
                    546:     
1.31      hyatt     547:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) )   {
1.34      hyatt     548:         // XXXHack to bypass sidebar crashes.
1.57      hyatt     549:         [mSidebarDrawer openOnEdge: NSMaxXEdge];
1.1       hyatt     550:     } else {
                    551:         [mSidebarDrawer close];
                    552:     }
1.36      hyatt     553:     
                    554:     [[self window] makeFirstResponder: resp];
1.1       hyatt     555: }
1.54      macserv   556: 
1.1       hyatt     557: 
1.4       hyatt     558: -(void)loadURL:(NSURL*)aURL
1.1       hyatt     559: {
1.32      hyatt     560:     if (mInitialized) {
1.4       hyatt     561:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
1.32      hyatt     562:     }
1.4       hyatt     563:     else {
                    564:         mURL = aURL;
                    565:         [mURL retain];
                    566:     }
1.1       hyatt     567: }
1.5       macserv   568: 
                    569: - (void)updateLocationFields:(NSString *)locationString
                    570: {
                    571: /* //commenting this out because it doesn't work right yet.
                    572:     if ( [locationString length] > 30 ) {
                    573:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    574:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
                    575:     } else {
                    576:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    577:             [NSString stringWithFormat:@"Location: %@", locationString]];
                    578:     }
                    579: */
                    580: 
                    581:     [mURLBar setStringValue:locationString];
                    582:     [mLocationSheetURLField setStringValue:locationString];
                    583: 
                    584:     [[self window] update];
                    585:     [[self window] display];
1.8       hyatt     586: }
1.27      hyatt     587: 
                    588: -(void)newTab
                    589: {
1.28      hyatt     590:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
1.47      hyatt     591:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    592:     [newView setTab: newTab];
1.41      macserv   593: 
                    594:     [newTab setLabel: @"Untitled"];
                    595:     [newTab setView: newView];
                    596: 
1.28      hyatt     597:     [mTabBrowser addTabViewItem: newTab];
1.42      macserv   598: 
1.28      hyatt     599:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.42      macserv   600: 
1.28      hyatt     601:     [mTabBrowser selectLastTabViewItem: self];
1.42      macserv   602: 
1.29      hyatt     603:     if ( [[[self window] toolbar] isVisible] ) {
                    604:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
                    605:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
1.61      hyatt     606:           if ([mTabBrowser numberOfTabViewItems] > 1) {
                    607:             [self focusURLBar];
                    608:             [[mBrowserView getBrowserView] setActive: NO];
                    609:           }
                    610:           else if ([[self window] isKeyWindow])
                    611:             [[mBrowserView getBrowserView] setActive: YES];
                    612:           else
                    613:             [[mBrowserView getBrowserView] setActive: NO];
1.42      macserv   614:         }
1.29      hyatt     615:     }
1.42      macserv   616: }
                    617: 
                    618: -(void)closeTab
                    619: {
1.49      hyatt     620:   if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
                    621:     [[[mTabBrowser selectedTabViewItem] view] windowClosed];
                    622:     [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
                    623:   }
1.42      macserv   624: }
                    625: 
                    626: - (void)previousTab
                    627: {
                    628:     [mTabBrowser selectPreviousTabViewItem:self];
                    629: }
                    630: 
                    631: - (void)nextTab
                    632: {
                    633:     [mTabBrowser selectNextTabViewItem:self];
1.28      hyatt     634: }
1.27      hyatt     635: 
1.28      hyatt     636: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
                    637: {
1.41      macserv   638:     // Disconnect the old view, if one has been designated.
                    639:     // If the window has just been opened, none has been.
                    640:     if ( mBrowserView ) {
                    641:         [mBrowserView disconnectView];
                    642:     }
1.28      hyatt     643:     // Connect up the new view
                    644:     mBrowserView = [aTabViewItem view];
1.30      hyatt     645:        
1.28      hyatt     646:     // Make the new view the primary content area.
1.47      hyatt     647:     [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
1.28      hyatt     648:         progress: mProgress windowController: self];
1.27      hyatt     649: }
1.28      hyatt     650: 
1.31      hyatt     651: -(MyBrowserView*)getMyBrowserView
                    652: {
                    653:     return mBrowserView;
                    654: }
1.45      hyatt     655: 
                    656: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
                    657: {
1.48      hyatt     658:   // Autosave our dimensions before we open a new window.  That ensures the size ends up matching.
                    659:   [self autosaveWindowFrame];
                    660: 
                    661:   BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
                    662:   [browser loadURL: aURL];
                    663:   if (aLoadInBG)
                    664:     [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
1.62      hyatt     665:   else {
1.61      hyatt     666:     // Focus the content area and show the window.
1.62      hyatt     667:     [browser enterModalSession];
                    668:     [[[browser getMyBrowserView] getBrowserView] setActive: YES];
                    669:   }
1.47      hyatt     670: }
                    671: 
                    672: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
                    673: {
                    674:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
                    675:     
                    676:     NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
                    677:     int index = [mTabBrowser indexOfTabViewItem: selectedTab];
                    678:     [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
                    679:     
                    680:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    681:     [newView setTab: newTab];
                    682:     
                    683:     [newTab setLabel: @"Loading..."];
                    684:     [newTab setView: newView];
                    685: 
                    686:     [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
                    687: 
1.61      hyatt     688:     if (!aLoadInBG) {
                    689:       [mTabBrowser selectTabViewItem: newTab];
                    690:       // Focus the content area.
                    691:       [[newView getBrowserView] setActive: YES];
                    692:     }
1.45      hyatt     693: }
1.52      macserv   694: 
                    695: -(void)setupSidebarTabs
                    696: {
                    697:     IconTabViewItem   *bookItem = [[IconTabViewItem alloc] initWithIdentifier:@"bookmarkSidebarIconTabViewItem"
                    698:                                   withTabIcon:[NSImage imageNamed:@"bookicon"]];
                    699:     IconTabViewItem   *histItem = [[IconTabViewItem alloc] initWithIdentifier:@"historySidebarIconTabViewItem"
                    700:                                   withTabIcon:[NSImage imageNamed:@"historyicon"]];
                    701:     IconTabViewItem *searchItem = [[IconTabViewItem alloc] initWithIdentifier:@"searchSidebarIconTabViewItem"
                    702:                                   withTabIcon:[NSImage imageNamed:@"searchicon"]];
1.63      hyatt     703:     IconTabViewItem *panelsItem = [[IconTabViewItem alloc] initWithIdentifier:@"myPanelsIconTabViewItem"
                    704:                                   withTabIcon:[NSImage imageNamed:@"panel_icon"]];
                    705:     
1.52      macserv   706:     [bookItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:0] view]];
                    707:     [histItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:1] view]];
                    708:     [searchItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:2] view]];
1.63      hyatt     709:     [panelsItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:3] view]];
1.52      macserv   710: 
1.63      hyatt     711:     [mSidebarTabView removeTabViewItem:[mSidebarTabView tabViewItemAtIndex:0]];
                    712:     
1.52      macserv   713:     [mSidebarTabView insertTabViewItem:bookItem   atIndex:0];
                    714:     [mSidebarTabView insertTabViewItem:histItem   atIndex:1];
                    715:     [mSidebarTabView insertTabViewItem:searchItem atIndex:2];
1.63      hyatt     716:     [mSidebarTabView insertTabViewItem:panelsItem atIndex:3];
                    717:     
1.53      macserv   718:     [mSidebarTabView selectFirstTabViewItem:self];
1.52      macserv   719: }
                    720: 
1.58      hyatt     721: -(void)setChromeMask:(int)aMask
                    722: {
                    723:   mChromeMask = aMask;
1.64      hyatt     724: }
                    725: 
                    726: -(void) biggerTextSize
                    727: {
                    728:   nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[mBrowserView getBrowserView] getContentWindow]);
                    729:   nsCOMPtr<nsIScriptGlobalObject> global(do_QueryInterface(contentWindow));
                    730:   if (!global)
                    731:     return;
                    732:   nsCOMPtr<nsIDocShell> docShell;
                    733:   global->GetDocShell(getter_AddRefs(docShell));
                    734:   if (!docShell)
                    735:     return;
                    736:   nsCOMPtr<nsIContentViewer> cv;
                    737:   docShell->GetContentViewer(getter_AddRefs(cv));
                    738:   nsCOMPtr<nsIMarkupDocumentViewer> markupViewer(do_QueryInterface(cv));
                    739:   if (!markupViewer)
                    740:     return;
                    741:   float zoom;
                    742:   markupViewer->GetTextZoom(&zoom);
                    743:   if (zoom >= 20)
                    744:     return;
                    745:   
                    746:   zoom += 0.25;
                    747:   if (zoom > 20)
                    748:     zoom = 20;
                    749:   
                    750:   markupViewer->SetTextZoom(zoom);
                    751: }
                    752: 
                    753: -(void) smallerTextSize
                    754: {
                    755:   nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[mBrowserView getBrowserView] getContentWindow]);
                    756:   nsCOMPtr<nsIScriptGlobalObject> global(do_QueryInterface(contentWindow));
                    757:   if (!global)
                    758:     return;
                    759:   nsCOMPtr<nsIDocShell> docShell;
                    760:   global->GetDocShell(getter_AddRefs(docShell));
                    761:   if (!docShell)
                    762:     return;
                    763:   nsCOMPtr<nsIContentViewer> cv;
                    764:   docShell->GetContentViewer(getter_AddRefs(cv));
                    765:   nsCOMPtr<nsIMarkupDocumentViewer> markupViewer(do_QueryInterface(cv));
                    766:   if (!markupViewer)
                    767:     return;
                    768:   float zoom;
                    769:   markupViewer->GetTextZoom(&zoom);
                    770:   if (zoom <= 0.01)
                    771:     return;
                    772: 
                    773:   zoom -= 0.25;
                    774:   if (zoom < 0.01)
                    775:     zoom = 0.01;
                    776: 
                    777:   markupViewer->SetTextZoom(zoom);
1.63      hyatt     778: }
                    779: 
                    780: -(id)getAddBookmarkSheetWindow
                    781: {
                    782:   return mAddBookmarkSheetWindow;
                    783: }
                    784: 
                    785: -(id)getAddBookmarkTitle
                    786: {
                    787:   return mAddBookmarkTitleField;
                    788: }
                    789: 
                    790: -(id)getAddBookmarkFolder
                    791: {
                    792:   return mAddBookmarkFolderField;
1.59      hyatt     793: }
                    794: 
                    795: // Called when a context menu should be shown.
                    796: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
                    797: {
                    798:   mContextMenuFlags = flags;
                    799:   mContextMenuNode = aNode;
                    800:   mContextMenuEvent = aEvent;
                    801: }
                    802: 
                    803: - (NSMenu*)getContextMenu
                    804: {
                    805:   NSMenu* result = nil;
                    806:   if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_LINK) != 0) {
                    807:     if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
                    808:       result = mImageLinkMenu;
                    809:     }
                    810:     else
                    811:       result = mLinkMenu;
                    812:   }
                    813:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_INPUT) != 0 ||
                    814:            (mContextMenuFlags & nsIContextMenuListener::CONTEXT_TEXT) != 0) {
                    815:     result = mInputMenu;
                    816:   }
                    817:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
                    818:     result = mImageMenu;
                    819:   }
                    820:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_DOCUMENT) != 0) {
                    821:     result = mPageMenu;
                    822:   }
                    823:   
                    824:   return result;
1.61      hyatt     825: }
                    826: 
                    827: // Context menu methods
                    828: - (IBAction)openLinkInNewWindow:(id)aSender
                    829: {
                    830:   [self openLinkInNewWindowOrTab: YES];
                    831: }
                    832: 
                    833: - (IBAction)openLinkInNewTab:(id)aSender
                    834: {
                    835:   [self openLinkInNewWindowOrTab: NO];
                    836: }
                    837: 
                    838: -(void)openLinkInNewWindowOrTab: (BOOL)aUseWindow
                    839: {
                    840:   nsCOMPtr<nsIDOMElement> linkContent;
                    841:   nsAutoString href;
                    842:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
                    843: 
                    844:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
                    845:   // really uses these anyway? :)
                    846:   if (!linkContent || href.IsEmpty())
                    847:     return;
                    848: 
                    849:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
                    850:   if (!pref)
                    851:     return; // Something bad happened if we can't get prefs.
                    852: 
                    853:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
                    854:   NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
                    855: 
                    856:   PRBool loadInBackground;
                    857:   pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
                    858: 
                    859:   if (aUseWindow)
                    860:     [self openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
                    861:   else
                    862:     [self openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
                    863: }
                    864: 
1.62      hyatt     865: - (IBAction)savePageAs:(id)aSender
                    866: {
                    867:   [self saveDocument: nil filterList: nil];
                    868: }
                    869: 
1.61      hyatt     870: - (IBAction)saveLinkAs:(id)aSender
                    871: {
1.62      hyatt     872:   nsCOMPtr<nsIDOMElement> linkContent;
                    873:   nsAutoString href;
                    874:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
                    875: 
                    876:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
                    877:   // really uses these anyway? :)
                    878:   if (!linkContent || href.IsEmpty())
                    879:     return;
                    880: 
                    881:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
                    882:   NSURL* urlToSave = [NSURL URLWithString: hrefStr];
                    883: 
                    884:   // The user wants to save this link.
                    885:   nsAutoString text;
                    886:   GeckoUtils::GatherTextUnder(mContextMenuNode, text);
                    887: 
                    888:   [self saveURL: nil filterList: nil
                    889:             url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
                    890:                                                                       length:nsCRT::strlen(text.get())]];
1.61      hyatt     891: }
                    892: 
                    893: - (IBAction)saveImageAs:(id)aSender
                    894: {
1.62      hyatt     895:   nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
                    896:   if (imgElement) {
                    897:       nsAutoString text;
                    898:       imgElement->GetAttribute(NS_LITERAL_STRING("src"), text);
                    899:       nsAutoString url;
                    900:       imgElement->GetSrc(url);
                    901: 
                    902:       NSString* hrefStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
                    903:       NSURL* urlToSave = [NSURL URLWithString: hrefStr];
                    904: 
                    905:       [self saveURL: nil filterList: nil
                    906:                 url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
                    907:                                                                           length:nsCRT::strlen(text.get())]];
                    908:   }
                    909: }
                    910: 
                    911: - (IBAction)viewOnlyThisImage:(id)aSender
                    912: {
                    913:   nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
                    914:   if (imgElement) {
                    915:     nsAutoString url;
                    916:     imgElement->GetSrc(url);
                    917: 
                    918:     NSString* urlStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
                    919:     NSURL* urlToView = [NSURL URLWithString: urlStr];
                    920: 
                    921:     [self loadURL: urlToView];
                    922: 
                    923:     // Focus and activate our content area.
                    924:     [[mBrowserView getBrowserView] setActive: YES];
                    925:   }  
1.58      hyatt     926: }
1.52      macserv   927: 
1.1       hyatt     928: @end
                    929: 

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