Annotation of chimera/BrowserWindowController.mm, revision 1.65

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: 
                    163:     // Get our saved dimensions.
                    164:     [[self window] setFrameUsingName: @"NavigatorWindow"];
1.35      hyatt     165:     
1.39      hyatt     166:     if (mModalSession)
                    167:       [NSApp stopModal: mModalSession];
                    168:       
1.4       hyatt     169:     mInitialized = YES;
1.57      hyatt     170: 
                    171:     mDrawerCachedFrame = NO;
1.38      hyatt     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:     
1.34      hyatt     178:     [[self window] setAcceptsMouseMovedEvents: YES];
                    179:     
1.1       hyatt     180:     [self setupToolbar];
1.30      hyatt     181: 
1.41      macserv   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];
1.4       hyatt     188:     
                    189:     if (mURL) {
                    190:       [self loadURL: mURL];
                    191:       [mURL release];
                    192:     }
1.18      hyatt     193:     
                    194:     [mSidebarDrawer setDelegate: self];
1.50      macserv   195: 
1.52      macserv   196:     [self setupSidebarTabs];
1.18      hyatt     197: }
                    198: 
1.22      hyatt     199: - (void)drawerWillOpen: (NSNotification*)aNotification
                    200: {
1.57      hyatt     201:   [mSidebarBookmarksDataSource ensureBookmarks];
                    202: 
                    203:   // Force the window to shrink and move if necessary in order to accommodate the sidebar.
1.65    ! hyatt     204:   NSRect screenFrame = [[[self window] screen] visibleFrame];
1.57      hyatt     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:   }
1.22      hyatt     224: }
                    225: 
1.18      hyatt     226: - (void)drawerDidOpen:(NSNotification *)aNotification
                    227: {
1.57      hyatt     228:   // XXXdwh This is temporary.
1.56      hyatt     229:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
1.57      hyatt     230: 
                    231:   // Toggle the sidebar icon.
                    232:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarOpened"]];
1.1       hyatt     233: }
                    234: 
1.19      hyatt     235: - (void)drawerDidClose:(NSNotification *)aNotification
                    236: {
1.57      hyatt     237:   // Unload the Gecko web page in "My Panels" to save memory.
                    238:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
                    239: 
                    240:   // XXXdwh ignore for now.
1.56      hyatt     241:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
1.57      hyatt     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:   }
1.19      hyatt     258: }
                    259: 
1.1       hyatt     260: - (void)setupToolbar
                    261: {
1.58      hyatt     262:   if (mChromeMask) {
                    263:     printf("Uh-oh. %d\n", mChromeMask);
                    264:   }
                    265:   
1.1       hyatt     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,
1.11      macserv   285:                                         PrintToolbarItemIdentifier,
1.1       hyatt     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"];
1.54      macserv   349:         [toolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
1.1       hyatt     350:         [toolbarItem setTarget:self];
                    351:         [toolbarItem setAction:@selector(toggleSidebar:)];
1.54      macserv   352:         mSidebarToolbarItem = toolbarItem;
1.1       hyatt     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];
1.5       macserv   365:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
1.1       hyatt     366:         [menuFormRep setTitle:[toolbarItem label]];
                    367:         
                    368:         [toolbarItem setMenuFormRepresentation:menuFormRep];
1.5       macserv   369:         mLocationToolbarItem = toolbarItem;
1.11      macserv   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)];
1.1       hyatt     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:    
1.5       macserv   402: - (void)updateToolbarItems
1.1       hyatt     403: {
                    404:   [[[self window] toolbar] validateVisibleItems];
                    405: }
                    406: 
1.5       macserv   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
1.3       hyatt     422: {
                    423:     [mURLBar selectText: self];
                    424: }
                    425: 
1.5       macserv   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]]];
1.32      hyatt     440:     
                    441:     // Focus and activate our content area.
                    442:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   443: }
                    444: 
1.63      hyatt     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: 
1.5       macserv   472: - (IBAction)goToLocationFromToolbarURLField:(id)sender
                    473: {
                    474:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
1.32      hyatt     475:     
                    476:     // Focus and activate our content area.
                    477:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   478: }
                    479: 
1.7       hyatt     480: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
1.6       hyatt     481: {
1.62      hyatt     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];
1.6       hyatt     490: }
                    491: 
1.12      hyatt     492: - (void)printDocument
                    493: {
                    494:     [[mBrowserView getBrowserView] printDocument];
                    495: }
                    496: 
1.15      hyatt     497: - (void)printPreview
                    498: {
                    499:     [[mBrowserView getBrowserView] printPreview];
                    500: }
                    501: 
1.60      pinkerto  502: - (BOOL)findInPage:(NSString*)text
1.16      hyatt     503: {
1.60      pinkerto  504:     return [[mBrowserView getBrowserView] findInPage:text];
1.22      hyatt     505: }
                    506: 
1.62      hyatt     507: - (void)addBookmarkExtended: (BOOL)aIsFromMenu
1.22      hyatt     508: {
1.62      hyatt     509:   [mSidebarBookmarksDataSource ensureBookmarks];
                    510:   [mSidebarBookmarksDataSource addBookmark: self useSelection: !aIsFromMenu];
1.16      hyatt     511: }
                    512: 
1.5       macserv   513: - (IBAction)back:(id)aSender
1.1       hyatt     514: {
                    515:   [[mBrowserView getBrowserView] goBack];
                    516: }
                    517: 
1.5       macserv   518: - (IBAction)forward:(id)aSender
1.1       hyatt     519: {
                    520:   [[mBrowserView getBrowserView] goForward];
                    521: }
                    522: 
1.5       macserv   523: - (IBAction)reload:(id)aSender
1.1       hyatt     524: {
                    525:   [[mBrowserView getBrowserView] reload: 0];
                    526: }
                    527: 
1.5       macserv   528: - (IBAction)stop:(id)aSender
1.1       hyatt     529: {
1.37      hyatt     530:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
1.1       hyatt     531: }
                    532: 
1.5       macserv   533: - (IBAction)home:(id)aSender
1.1       hyatt     534: {
1.5       macserv   535:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.1       hyatt     536: }
                    537: 
1.5       macserv   538: - (IBAction)toggleSidebar:(id)aSender
1.1       hyatt     539: {
1.36      hyatt     540:     NSResponder* resp = [[self window] firstResponder];
                    541:     [[self window] makeFirstResponder: nil];
                    542:     
1.31      hyatt     543:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) )   {
1.34      hyatt     544:         // XXXHack to bypass sidebar crashes.
1.57      hyatt     545:         [mSidebarDrawer openOnEdge: NSMaxXEdge];
1.1       hyatt     546:     } else {
                    547:         [mSidebarDrawer close];
                    548:     }
1.36      hyatt     549:     
                    550:     [[self window] makeFirstResponder: resp];
1.1       hyatt     551: }
1.54      macserv   552: 
1.1       hyatt     553: 
1.4       hyatt     554: -(void)loadURL:(NSURL*)aURL
1.1       hyatt     555: {
1.32      hyatt     556:     if (mInitialized) {
1.4       hyatt     557:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
1.32      hyatt     558:     }
1.4       hyatt     559:     else {
                    560:         mURL = aURL;
                    561:         [mURL retain];
                    562:     }
1.1       hyatt     563: }
1.5       macserv   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];
1.8       hyatt     582: }
1.27      hyatt     583: 
                    584: -(void)newTab
                    585: {
1.28      hyatt     586:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
1.47      hyatt     587:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    588:     [newView setTab: newTab];
1.41      macserv   589: 
                    590:     [newTab setLabel: @"Untitled"];
                    591:     [newTab setView: newView];
                    592: 
1.28      hyatt     593:     [mTabBrowser addTabViewItem: newTab];
1.42      macserv   594: 
1.28      hyatt     595:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.42      macserv   596: 
1.28      hyatt     597:     [mTabBrowser selectLastTabViewItem: self];
1.42      macserv   598: 
1.29      hyatt     599:     if ( [[[self window] toolbar] isVisible] ) {
                    600:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
                    601:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
1.61      hyatt     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];
1.42      macserv   610:         }
1.29      hyatt     611:     }
1.42      macserv   612: }
                    613: 
                    614: -(void)closeTab
                    615: {
1.49      hyatt     616:   if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
                    617:     [[[mTabBrowser selectedTabViewItem] view] windowClosed];
                    618:     [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
                    619:   }
1.42      macserv   620: }
                    621: 
                    622: - (void)previousTab
                    623: {
                    624:     [mTabBrowser selectPreviousTabViewItem:self];
                    625: }
                    626: 
                    627: - (void)nextTab
                    628: {
                    629:     [mTabBrowser selectNextTabViewItem:self];
1.28      hyatt     630: }
1.27      hyatt     631: 
1.28      hyatt     632: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
                    633: {
1.41      macserv   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:     }
1.28      hyatt     639:     // Connect up the new view
                    640:     mBrowserView = [aTabViewItem view];
1.30      hyatt     641:        
1.28      hyatt     642:     // Make the new view the primary content area.
1.47      hyatt     643:     [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
1.28      hyatt     644:         progress: mProgress windowController: self];
1.27      hyatt     645: }
1.28      hyatt     646: 
1.31      hyatt     647: -(MyBrowserView*)getMyBrowserView
                    648: {
                    649:     return mBrowserView;
                    650: }
1.45      hyatt     651: 
                    652: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
                    653: {
1.48      hyatt     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]];
1.62      hyatt     661:   else {
1.61      hyatt     662:     // Focus the content area and show the window.
1.62      hyatt     663:     [browser enterModalSession];
                    664:     [[[browser getMyBrowserView] getBrowserView] setActive: YES];
                    665:   }
1.47      hyatt     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: 
1.61      hyatt     684:     if (!aLoadInBG) {
                    685:       [mTabBrowser selectTabViewItem: newTab];
                    686:       // Focus the content area.
                    687:       [[newView getBrowserView] setActive: YES];
                    688:     }
1.45      hyatt     689: }
1.52      macserv   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"]];
1.63      hyatt     699:     IconTabViewItem *panelsItem = [[IconTabViewItem alloc] initWithIdentifier:@"myPanelsIconTabViewItem"
                    700:                                   withTabIcon:[NSImage imageNamed:@"panel_icon"]];
                    701:     
1.52      macserv   702:     [bookItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:0] view]];
                    703:     [histItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:1] view]];
                    704:     [searchItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:2] view]];
1.63      hyatt     705:     [panelsItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:3] view]];
1.52      macserv   706: 
1.63      hyatt     707:     [mSidebarTabView removeTabViewItem:[mSidebarTabView tabViewItemAtIndex:0]];
                    708:     
1.52      macserv   709:     [mSidebarTabView insertTabViewItem:bookItem   atIndex:0];
                    710:     [mSidebarTabView insertTabViewItem:histItem   atIndex:1];
                    711:     [mSidebarTabView insertTabViewItem:searchItem atIndex:2];
1.63      hyatt     712:     [mSidebarTabView insertTabViewItem:panelsItem atIndex:3];
                    713:     
1.53      macserv   714:     [mSidebarTabView selectFirstTabViewItem:self];
1.52      macserv   715: }
                    716: 
1.58      hyatt     717: -(void)setChromeMask:(int)aMask
                    718: {
                    719:   mChromeMask = aMask;
1.64      hyatt     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);
1.63      hyatt     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;
1.59      hyatt     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;
1.61      hyatt     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: 
1.62      hyatt     861: - (IBAction)savePageAs:(id)aSender
                    862: {
                    863:   [self saveDocument: nil filterList: nil];
                    864: }
                    865: 
1.61      hyatt     866: - (IBAction)saveLinkAs:(id)aSender
                    867: {
1.62      hyatt     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())]];
1.61      hyatt     887: }
                    888: 
                    889: - (IBAction)saveImageAs:(id)aSender
                    890: {
1.62      hyatt     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:   }  
1.58      hyatt     922: }
1.52      macserv   923: 
1.1       hyatt     924: @end
                    925: 

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