Annotation of chimera/BrowserWindowController.mm, revision 1.61

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.51      macserv    47: #include "nsCocoaBrowserService.h"
1.61    ! hyatt      48: #include "nsString.h"
        !            49: #include "GeckoUtils.h"
1.37      hyatt      50: 
1.1       hyatt      51: static NSString *BrowserToolbarIdentifier  = @"Browser Window Toolbar";
                     52: static NSString *BackToolbarItemIdentifier = @"Back Toolbar Item";
                     53: static NSString *ForwardToolbarItemIdentifier  = @"Forward Toolbar Item";
                     54: static NSString *ReloadToolbarItemIdentifier = @"Reload Toolbar Item";
                     55: static NSString *StopToolbarItemIdentifier = @"Stop Toolbar Item";
                     56: static NSString *HomeToolbarItemIdentifier = @"Home Toolbar Item";
                     57: static NSString *LocationToolbarItemIdentifier = @"Location Toolbar Item";
                     58: static NSString *SidebarToolbarItemIdentifier  = @"Sidebar Toolbar Item";
1.11      macserv    59: static NSString *PrintToolbarItemIdentifier  = @"Print Toolbar Item";
1.1       hyatt      60: 
                     61: @interface BrowserWindowController(Private)
                     62: - (void)setupToolbar;
1.52      macserv    63: - (void)setupSidebarTabs;
1.1       hyatt      64: @end
                     65: 
                     66: @implementation BrowserWindowController
                     67: 
1.39      hyatt      68: -(void)enterModalSession
                     69: {
                     70:     mModalSession = [NSApp beginModalSessionForWindow: [self window]];
                     71:     [NSApp runModalSession: mModalSession];
                     72:     [NSApp endModalSession: mModalSession];
                     73:     mModalSession = nil;
                     74: }
                     75: 
1.58      hyatt      76: -(void)windowDidBecomeKey: (NSNotification*)aNotification
                     77: {
                     78:   // May become necessary later.
                     79: }
                     80: 
                     81: -(void)windowDidResignKey: (NSNotification*)aNotification
                     82: {
                     83:   // May be needed later.
                     84: }
                     85: 
1.35      hyatt      86: -(void)mouseMoved:(NSEvent*)aEvent
                     87: {
                     88:     if (mMoveReentrant)
                     89:         return;
                     90:         
                     91:     mMoveReentrant = YES;
                     92:     NSView* view = [[[self window] contentView] hitTest: [aEvent locationInWindow]];
                     93:     [view mouseMoved: aEvent];
                     94:     [super mouseMoved: aEvent];
                     95:     mMoveReentrant = NO;
                     96: }
                     97: 
1.41      macserv    98: - (id)initWithWindowNibName:(NSString *)windowNibName
1.4       hyatt      99: {
1.41      macserv   100:     if ( (self = [super initWithWindowNibName:(NSString *)windowNibName]) ) {
1.51      macserv   101:         if ( nsCocoaBrowserService::sNumBrowsers == 0 ) {
                    102:             [self setShouldCascadeWindows:NO];
                    103:         } else {
                    104:             [self setShouldCascadeWindows:YES];
                    105:         }
1.41      macserv   106:         mInitialized = NO;
                    107:         mMoveReentrant = NO;
1.48      hyatt     108:         mShouldAutosave = YES;
1.58      hyatt     109:         mChromeMask = 0;
1.59      hyatt     110:         mContextMenuFlags = 0;
                    111:         mContextMenuEvent = nsnull;
                    112:         mContextMenuNode = nsnull;
1.41      macserv   113:     }
                    114:     return self;
1.4       hyatt     115: }
                    116: 
1.48      hyatt     117: -(void)autosaveWindowFrame
                    118: {
                    119:   if (mShouldAutosave)
                    120:     [[self window] saveFrameUsingName: @"NavigatorWindow"];
                    121: }
                    122: 
                    123: -(void)disableAutosave
                    124: {
                    125:   mShouldAutosave = NO;
                    126: }
                    127: 
1.17      hyatt     128: - (void)windowWillClose:(NSNotification *)notification
                    129: {
1.49      hyatt     130:   printf("Window will close notification.\n");
                    131:   [mSidebarBookmarksDataSource windowClosing];
1.48      hyatt     132: 
1.49      hyatt     133:   [self autosaveWindowFrame];
                    134:   [self autorelease];
1.17      hyatt     135: }
                    136: 
1.7       hyatt     137: - (void)dealloc
                    138: {
1.49      hyatt     139:   printf("Browser controller died.\n");
1.40      hyatt     140: 
1.49      hyatt     141:   // Loop over all tabs, and tell them that the window is closed.
                    142:   int numTabs = [mTabBrowser numberOfTabViewItems];
                    143:   for (int i = 0; i < numTabs; i++) {
                    144:     NSTabViewItem* item = [mTabBrowser tabViewItemAtIndex: i];
                    145:     [[item view] windowClosed];
                    146:   }
                    147:   [mSidebarBrowserView windowClosed];
                    148: 
                    149:   [mProgress release];
                    150:   
                    151:   [super dealloc];
1.7       hyatt     152: }
                    153: 
1.1       hyatt     154: - (void)windowDidLoad
                    155: {
1.35      hyatt     156:     [super windowDidLoad];
1.48      hyatt     157: 
                    158:     // Get our saved dimensions.
                    159:     [[self window] setFrameUsingName: @"NavigatorWindow"];
1.35      hyatt     160:     
1.39      hyatt     161:     if (mModalSession)
                    162:       [NSApp stopModal: mModalSession];
                    163:       
1.4       hyatt     164:     mInitialized = YES;
1.57      hyatt     165: 
                    166:     mDrawerCachedFrame = NO;
1.38      hyatt     167:     
                    168:     // Retain with a single extra refcount.  This allows the MyBrowserViews
                    169:     // to remove the progress meter from its superview without having to 
                    170:     // worry about retaining and releasing it.
                    171:     [mProgress retain];
                    172:     
1.34      hyatt     173:     [[self window] setAcceptsMouseMovedEvents: YES];
                    174:     
1.1       hyatt     175:     [self setupToolbar];
1.30      hyatt     176: 
1.41      macserv   177: //  03/03/2002 mlj Changing strategy a bit here.  The addTab: method was
                    178: // duplicating a lot of the code found here.  I have moved it to that method.
                    179: // We now remove the IB tab, then add one of our own.
                    180: 
                    181:     [mTabBrowser removeTabViewItem:[mTabBrowser tabViewItemAtIndex:0]];
                    182:     [self newTab];
1.4       hyatt     183:     
                    184:     if (mURL) {
                    185:       [self loadURL: mURL];
                    186:       [mURL release];
                    187:     }
1.18      hyatt     188:     
                    189:     [mSidebarDrawer setDelegate: self];
1.50      macserv   190: 
1.52      macserv   191:     [self setupSidebarTabs];
1.18      hyatt     192: }
                    193: 
1.22      hyatt     194: - (void)drawerWillOpen: (NSNotification*)aNotification
                    195: {
1.57      hyatt     196:   [mSidebarBookmarksDataSource ensureBookmarks];
                    197: 
                    198:   // Force the window to shrink and move if necessary in order to accommodate the sidebar.
                    199:   NSRect screenFrame = [[[self window] screen] frame];
                    200:   NSRect windowFrame = [[self window] frame];
                    201:   NSSize drawerSize = [mSidebarDrawer contentSize];
                    202:   int fudgeFactor = 12; // Not sure how to get the drawer's border info, so we fudge it for now.
                    203:   drawerSize.width += fudgeFactor;
                    204:   if (windowFrame.origin.x + windowFrame.size.width + drawerSize.width >
                    205:       screenFrame.size.width) {
                    206:     // We need to adjust the window so that it can fit.
                    207:     int shrinkDelta = (windowFrame.size.width + drawerSize.width) - screenFrame.size.width;
                    208:     if (shrinkDelta < 0) shrinkDelta = 0;
                    209:     int newWidth = (windowFrame.size.width - shrinkDelta);
                    210:     int newPosition = screenFrame.size.width - newWidth - drawerSize.width;
                    211:     if (newPosition < 0) newPosition = 0;
                    212:     mCachedFrameBeforeDrawerOpen = windowFrame;
                    213:     windowFrame.origin.x = newPosition;
                    214:     windowFrame.size.width = newWidth;
                    215:     mCachedFrameAfterDrawerOpen = windowFrame;
                    216:     [[self window] setFrame: windowFrame display: YES];
                    217:     mDrawerCachedFrame = YES;
                    218:   }
1.22      hyatt     219: }
                    220: 
1.18      hyatt     221: - (void)drawerDidOpen:(NSNotification *)aNotification
                    222: {
1.57      hyatt     223:   // XXXdwh This is temporary.
1.56      hyatt     224:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
1.57      hyatt     225: 
                    226:   // Toggle the sidebar icon.
                    227:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarOpened"]];
1.1       hyatt     228: }
                    229: 
1.19      hyatt     230: - (void)drawerDidClose:(NSNotification *)aNotification
                    231: {
1.57      hyatt     232:   // Unload the Gecko web page in "My Panels" to save memory.
                    233:   [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
                    234: 
                    235:   // XXXdwh ignore for now.
1.56      hyatt     236:   //  [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
1.57      hyatt     237: 
                    238:   if (mDrawerCachedFrame) {
                    239:     printf("Got here.\n");
                    240:     mDrawerCachedFrame = NO;
                    241:     NSRect frame = [[self window] frame];
                    242:     if (frame.origin.x == mCachedFrameAfterDrawerOpen.origin.x &&
                    243:         frame.origin.y == mCachedFrameAfterDrawerOpen.origin.y &&
                    244:         frame.size.width == mCachedFrameAfterDrawerOpen.size.width &&
                    245:         frame.size.height == mCachedFrameAfterDrawerOpen.size.height) {
                    246:       printf("Got here too.\n");
                    247:       printf("Xes are %d %d\n", frame.origin.x, mCachedFrameAfterDrawerOpen.origin.x);
                    248:       printf("Widths are %d %d\n", frame.size.width, mCachedFrameAfterDrawerOpen.size.width);
                    249:       // Restore the original frame.
                    250:       [[self window] setFrame: mCachedFrameBeforeDrawerOpen display: YES];
                    251:     }
                    252:   }
1.19      hyatt     253: }
                    254: 
1.1       hyatt     255: - (void)setupToolbar
                    256: {
1.58      hyatt     257:   if (mChromeMask) {
                    258:     printf("Uh-oh. %d\n", mChromeMask);
                    259:   }
                    260:   
1.1       hyatt     261:     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
                    262:     
                    263:     [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
                    264:     [toolbar setAllowsUserCustomization:YES];
                    265:     [toolbar setAutosavesConfiguration:YES];
                    266:     [toolbar setDelegate:self];
                    267:     [[self window] setToolbar:toolbar];
                    268: }
                    269: 
                    270: 
                    271: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
                    272: {
                    273:     return [NSArray arrayWithObjects:  BackToolbarItemIdentifier,
                    274:                                         ForwardToolbarItemIdentifier,
                    275:                                         ReloadToolbarItemIdentifier,
                    276:                                         StopToolbarItemIdentifier,
                    277:                                         HomeToolbarItemIdentifier,
                    278:                                         LocationToolbarItemIdentifier,
                    279:                                         SidebarToolbarItemIdentifier,
1.11      macserv   280:                                         PrintToolbarItemIdentifier,
1.1       hyatt     281:                                         NSToolbarCustomizeToolbarItemIdentifier,
                    282:                                         NSToolbarFlexibleSpaceItemIdentifier,
                    283:                                         NSToolbarSpaceItemIdentifier,
                    284:                                         NSToolbarSeparatorItemIdentifier,
                    285:                                         nil];
                    286: }
                    287: 
                    288: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
                    289: {
                    290:     return [NSArray arrayWithObjects:  BackToolbarItemIdentifier,
                    291:                                         ForwardToolbarItemIdentifier,
                    292:                                         ReloadToolbarItemIdentifier,
                    293:                                         StopToolbarItemIdentifier,
                    294:                                         HomeToolbarItemIdentifier,
                    295:                                         LocationToolbarItemIdentifier,
                    296:                                         SidebarToolbarItemIdentifier,
                    297:                                         nil];
                    298: }
                    299: 
                    300: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
                    301:       itemForItemIdentifier:(NSString *)itemIdent
                    302:   willBeInsertedIntoToolbar:(BOOL)willBeInserted
                    303: {
                    304:     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
                    305:     if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
                    306:         [toolbarItem setLabel:@"Back"];
                    307:         [toolbarItem setPaletteLabel:@"Go Back"];
                    308:         [toolbarItem setToolTip:@"Go back one page"];
                    309:         [toolbarItem setImage:[NSImage imageNamed:@"back"]];
                    310:         [toolbarItem setTarget:self];
                    311:         [toolbarItem setAction:@selector(back:)];
                    312:     } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
                    313:         [toolbarItem setLabel:@"Forward"];
                    314:         [toolbarItem setPaletteLabel:@"Go Forward"];
                    315:         [toolbarItem setToolTip:@"Go forward one page"];
                    316:         [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
                    317:         [toolbarItem setTarget:self];
                    318:         [toolbarItem setAction:@selector(forward:)];
                    319:     } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
                    320:         [toolbarItem setLabel:@"Reload"];
                    321:         [toolbarItem setPaletteLabel:@"Reload Page"];
                    322:         [toolbarItem setToolTip:@"Reload current page"];
                    323:         [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
                    324:         [toolbarItem setTarget:self];
                    325:         [toolbarItem setAction:@selector(reload:)];
                    326:     } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
                    327:         [toolbarItem setLabel:@"Stop"];
                    328:         [toolbarItem setPaletteLabel:@"Stop Loading"];
                    329:         [toolbarItem setToolTip:@"Stop loading this page"];
                    330:         [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
                    331:         [toolbarItem setTarget:self];
                    332:         [toolbarItem setAction:@selector(stop:)];
                    333:     } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
                    334:         [toolbarItem setLabel:@"Home"];
                    335:         [toolbarItem setPaletteLabel:@"Go Home"];
                    336:         [toolbarItem setToolTip:@"Go to home page"];
                    337:         [toolbarItem setImage:[NSImage imageNamed:@"home"]];
                    338:         [toolbarItem setTarget:self];
                    339:         [toolbarItem setAction:@selector(home:)];
                    340:     } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
                    341:         [toolbarItem setLabel:@"Sidebar"];
                    342:         [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
                    343:         [toolbarItem setToolTip:@"Show or hide the Sidebar"];
1.54      macserv   344:         [toolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
1.1       hyatt     345:         [toolbarItem setTarget:self];
                    346:         [toolbarItem setAction:@selector(toggleSidebar:)];
1.54      macserv   347:         mSidebarToolbarItem = toolbarItem;
1.1       hyatt     348:     } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
                    349:         
                    350:         NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
                    351:         
                    352:         [toolbarItem setLabel:@"Location"];
                    353:         [toolbarItem setPaletteLabel:@"Location"];
                    354:         [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
                    355:         [toolbarItem setView:mLocationToolbarView];
                    356:         [toolbarItem setMinSize:NSMakeSize(128,32)];
                    357:         [toolbarItem setMaxSize:NSMakeSize(2560,32)];
                    358:         
                    359:         [menuFormRep setTarget:self];
1.5       macserv   360:         [menuFormRep setAction:@selector(performAppropriateLocationAction)];
1.1       hyatt     361:         [menuFormRep setTitle:[toolbarItem label]];
                    362:         
                    363:         [toolbarItem setMenuFormRepresentation:menuFormRep];
1.5       macserv   364:         mLocationToolbarItem = toolbarItem;
1.11      macserv   365: 
                    366:     } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
                    367:         [toolbarItem setLabel:@"Print"];
                    368:         [toolbarItem setPaletteLabel:@"Print"];
                    369:         [toolbarItem setToolTip:@"Print this page"];
                    370:         [toolbarItem setImage:[NSImage imageNamed:@"print"]];
                    371:         [toolbarItem setTarget:self];
                    372:         [toolbarItem setAction:@selector(printDocument)];
1.1       hyatt     373:     } else {
                    374:         toolbarItem = nil;
                    375:     }
                    376:         
                    377:     return toolbarItem;
                    378: }
                    379: 
                    380: // This method handles the enabling/disabling of the toolbar buttons.
                    381: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
                    382: {
                    383:   // Check the action and see if it matches.
                    384:   SEL action = [theItem action];
                    385:   if (action == @selector(back:))
                    386:     return [[mBrowserView getBrowserView] canGoBack];
                    387:   else if (action == @selector(forward:))
                    388:     return [[mBrowserView getBrowserView] canGoForward];
                    389:   else if (action == @selector(reload:))
                    390:     return [mBrowserView isBusy] == NO;
                    391:   else if (action == @selector(stop:))
                    392:     return [mBrowserView isBusy];
                    393:   else
                    394:     return YES;
                    395: }
                    396:    
1.5       macserv   397: - (void)updateToolbarItems
1.1       hyatt     398: {
                    399:   [[[self window] toolbar] validateVisibleItems];
                    400: }
                    401: 
1.5       macserv   402: - (void)performAppropriateLocationAction
                    403: {
                    404:     if ( [[[self window] toolbar] isVisible] ) {
                    405:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
                    406:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
                    407:             [self focusURLBar];
                    408:         } else {
                    409:             [self beginLocationSheet];
                    410:         }
                    411:     } else {
                    412:         [self beginLocationSheet];
                    413:     }
                    414: }
                    415: 
                    416: - (void)focusURLBar
1.3       hyatt     417: {
                    418:     [mURLBar selectText: self];
                    419: }
                    420: 
1.5       macserv   421: - (void)beginLocationSheet
                    422: {
                    423:     [NSApp beginSheet: mLocationSheetWindow
                    424:        modalForWindow: [self window]
                    425:         modalDelegate: nil
                    426:        didEndSelector: nil
                    427:           contextInfo: nil];
                    428: }
                    429: 
                    430: - (IBAction)endLocationSheet:(id)sender
                    431: {
                    432:     [mLocationSheetWindow orderOut:self];
                    433:     [NSApp endSheet:mLocationSheetWindow returnCode:1];
                    434:     [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
1.32      hyatt     435:     
                    436:     // Focus and activate our content area.
                    437:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   438: }
                    439: 
                    440: - (IBAction)goToLocationFromToolbarURLField:(id)sender
                    441: {
                    442:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
1.32      hyatt     443:     
                    444:     // Focus and activate our content area.
                    445:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   446: }
                    447: 
1.7       hyatt     448: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
1.6       hyatt     449: {
1.7       hyatt     450:     [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
1.6       hyatt     451: }
                    452: 
1.12      hyatt     453: - (void)printDocument
                    454: {
                    455:     [[mBrowserView getBrowserView] printDocument];
                    456: }
                    457: 
1.15      hyatt     458: - (void)printPreview
                    459: {
                    460:     [[mBrowserView getBrowserView] printPreview];
                    461: }
                    462: 
1.60      pinkerto  463: - (BOOL)findInPage:(NSString*)text
1.16      hyatt     464: {
1.60      pinkerto  465:     return [[mBrowserView getBrowserView] findInPage:text];
1.22      hyatt     466: }
                    467: 
                    468: - (void)addBookmark
                    469: {
                    470:     // XXXdwh Hack.  Just go to the sidebar for now until we get our
                    471:     // menu data source going.
                    472:     [mSidebarBookmarksDataSource ensureBookmarks];
                    473:     [mSidebarBookmarksDataSource addBookmark: self];
1.16      hyatt     474: }
                    475: 
1.5       macserv   476: - (IBAction)back:(id)aSender
1.1       hyatt     477: {
                    478:   [[mBrowserView getBrowserView] goBack];
                    479: }
                    480: 
1.5       macserv   481: - (IBAction)forward:(id)aSender
1.1       hyatt     482: {
                    483:   [[mBrowserView getBrowserView] goForward];
                    484: }
                    485: 
1.5       macserv   486: - (IBAction)reload:(id)aSender
1.1       hyatt     487: {
                    488:   [[mBrowserView getBrowserView] reload: 0];
                    489: }
                    490: 
1.5       macserv   491: - (IBAction)stop:(id)aSender
1.1       hyatt     492: {
1.37      hyatt     493:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
1.1       hyatt     494: }
                    495: 
1.5       macserv   496: - (IBAction)home:(id)aSender
1.1       hyatt     497: {
1.5       macserv   498:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.1       hyatt     499: }
                    500: 
1.5       macserv   501: - (IBAction)toggleSidebar:(id)aSender
1.1       hyatt     502: {
1.36      hyatt     503:     NSResponder* resp = [[self window] firstResponder];
                    504:     [[self window] makeFirstResponder: nil];
                    505:     
1.31      hyatt     506:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) )   {
1.34      hyatt     507:         // XXXHack to bypass sidebar crashes.
1.57      hyatt     508:         [mSidebarDrawer openOnEdge: NSMaxXEdge];
1.1       hyatt     509:     } else {
                    510:         [mSidebarDrawer close];
                    511:     }
1.36      hyatt     512:     
                    513:     [[self window] makeFirstResponder: resp];
1.1       hyatt     514: }
1.54      macserv   515: 
1.1       hyatt     516: 
1.4       hyatt     517: -(void)loadURL:(NSURL*)aURL
1.1       hyatt     518: {
1.32      hyatt     519:     if (mInitialized) {
1.4       hyatt     520:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
1.32      hyatt     521:     }
1.4       hyatt     522:     else {
                    523:         mURL = aURL;
                    524:         [mURL retain];
                    525:     }
1.1       hyatt     526: }
1.5       macserv   527: 
                    528: - (void)updateLocationFields:(NSString *)locationString
                    529: {
                    530: /* //commenting this out because it doesn't work right yet.
                    531:     if ( [locationString length] > 30 ) {
                    532:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    533:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
                    534:     } else {
                    535:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    536:             [NSString stringWithFormat:@"Location: %@", locationString]];
                    537:     }
                    538: */
                    539: 
                    540:     [mURLBar setStringValue:locationString];
                    541:     [mLocationSheetURLField setStringValue:locationString];
                    542: 
                    543:     [[self window] update];
                    544:     [[self window] display];
1.8       hyatt     545: }
1.27      hyatt     546: 
                    547: -(void)newTab
                    548: {
1.28      hyatt     549:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
1.47      hyatt     550:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    551:     [newView setTab: newTab];
1.41      macserv   552: 
                    553:     [newTab setLabel: @"Untitled"];
                    554:     [newTab setView: newView];
                    555: 
1.28      hyatt     556:     [mTabBrowser addTabViewItem: newTab];
1.42      macserv   557: 
1.28      hyatt     558:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.42      macserv   559: 
1.28      hyatt     560:     [mTabBrowser selectLastTabViewItem: self];
1.42      macserv   561: 
1.29      hyatt     562:     if ( [[[self window] toolbar] isVisible] ) {
                    563:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
                    564:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
1.61    ! hyatt     565:           if ([mTabBrowser numberOfTabViewItems] > 1) {
        !           566:             [self focusURLBar];
        !           567:             [[mBrowserView getBrowserView] setActive: NO];
        !           568:           }
        !           569:           else if ([[self window] isKeyWindow])
        !           570:             [[mBrowserView getBrowserView] setActive: YES];
        !           571:           else
        !           572:             [[mBrowserView getBrowserView] setActive: NO];
1.42      macserv   573:         }
1.29      hyatt     574:     }
1.42      macserv   575: }
                    576: 
                    577: -(void)closeTab
                    578: {
1.49      hyatt     579:   if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
                    580:     [[[mTabBrowser selectedTabViewItem] view] windowClosed];
                    581:     [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
                    582:   }
1.42      macserv   583: }
                    584: 
                    585: - (void)previousTab
                    586: {
                    587:     [mTabBrowser selectPreviousTabViewItem:self];
                    588: }
                    589: 
                    590: - (void)nextTab
                    591: {
                    592:     [mTabBrowser selectNextTabViewItem:self];
1.28      hyatt     593: }
1.27      hyatt     594: 
1.28      hyatt     595: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
                    596: {
1.41      macserv   597:     // Disconnect the old view, if one has been designated.
                    598:     // If the window has just been opened, none has been.
                    599:     if ( mBrowserView ) {
                    600:         [mBrowserView disconnectView];
                    601:     }
1.28      hyatt     602:     // Connect up the new view
                    603:     mBrowserView = [aTabViewItem view];
1.30      hyatt     604:        
1.28      hyatt     605:     // Make the new view the primary content area.
1.47      hyatt     606:     [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
1.28      hyatt     607:         progress: mProgress windowController: self];
1.27      hyatt     608: }
1.28      hyatt     609: 
1.31      hyatt     610: -(MyBrowserView*)getMyBrowserView
                    611: {
                    612:     return mBrowserView;
                    613: }
1.45      hyatt     614: 
                    615: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
                    616: {
1.48      hyatt     617:   // Autosave our dimensions before we open a new window.  That ensures the size ends up matching.
                    618:   [self autosaveWindowFrame];
                    619: 
                    620:   BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
                    621:   [browser loadURL: aURL];
                    622:   if (aLoadInBG)
                    623:     [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
                    624:   else
1.61    ! hyatt     625:     // Focus the content area and show the window.
1.48      hyatt     626:     [browser showWindow: self];
1.47      hyatt     627: }
                    628: 
                    629: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
                    630: {
                    631:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
                    632:     
                    633:     NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
                    634:     int index = [mTabBrowser indexOfTabViewItem: selectedTab];
                    635:     [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
                    636:     
                    637:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    638:     [newView setTab: newTab];
                    639:     
                    640:     [newTab setLabel: @"Loading..."];
                    641:     [newTab setView: newView];
                    642: 
                    643:     [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
                    644: 
1.61    ! hyatt     645:     if (!aLoadInBG) {
        !           646:       [mTabBrowser selectTabViewItem: newTab];
        !           647:       // Focus the content area.
        !           648:       [[newView getBrowserView] setActive: YES];
        !           649:     }
1.45      hyatt     650: }
1.52      macserv   651: 
                    652: -(void)setupSidebarTabs
                    653: {
                    654:     IconTabViewItem   *bookItem = [[IconTabViewItem alloc] initWithIdentifier:@"bookmarkSidebarIconTabViewItem"
                    655:                                   withTabIcon:[NSImage imageNamed:@"bookicon"]];
                    656:     IconTabViewItem   *histItem = [[IconTabViewItem alloc] initWithIdentifier:@"historySidebarIconTabViewItem"
                    657:                                   withTabIcon:[NSImage imageNamed:@"historyicon"]];
                    658:     IconTabViewItem *searchItem = [[IconTabViewItem alloc] initWithIdentifier:@"searchSidebarIconTabViewItem"
                    659:                                   withTabIcon:[NSImage imageNamed:@"searchicon"]];
                    660: 
                    661:     [bookItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:0] view]];
                    662:     [histItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:1] view]];
                    663:     [searchItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:2] view]];
                    664: 
                    665:     [mSidebarTabView insertTabViewItem:bookItem   atIndex:0];
                    666:     [mSidebarTabView insertTabViewItem:histItem   atIndex:1];
                    667:     [mSidebarTabView insertTabViewItem:searchItem atIndex:2];
1.53      macserv   668: 
                    669:     [mSidebarTabView selectFirstTabViewItem:self];
1.52      macserv   670: }
                    671: 
1.58      hyatt     672: -(void)setChromeMask:(int)aMask
                    673: {
                    674:   mChromeMask = aMask;
1.59      hyatt     675: }
                    676: 
                    677: // Called when a context menu should be shown.
                    678: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
                    679: {
                    680:   mContextMenuFlags = flags;
                    681:   mContextMenuNode = aNode;
                    682:   mContextMenuEvent = aEvent;
                    683: }
                    684: 
                    685: - (NSMenu*)getContextMenu
                    686: {
                    687:   NSMenu* result = nil;
                    688:   if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_LINK) != 0) {
                    689:     if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
                    690:       result = mImageLinkMenu;
                    691:     }
                    692:     else
                    693:       result = mLinkMenu;
                    694:   }
                    695:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_INPUT) != 0 ||
                    696:            (mContextMenuFlags & nsIContextMenuListener::CONTEXT_TEXT) != 0) {
                    697:     result = mInputMenu;
                    698:   }
                    699:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
                    700:     result = mImageMenu;
                    701:   }
                    702:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_DOCUMENT) != 0) {
                    703:     result = mPageMenu;
                    704:   }
                    705:   
                    706:   return result;
1.61    ! hyatt     707: }
        !           708: 
        !           709: // Context menu methods
        !           710: - (IBAction)openLinkInNewWindow:(id)aSender
        !           711: {
        !           712:   [self openLinkInNewWindowOrTab: YES];
        !           713: }
        !           714: 
        !           715: - (IBAction)openLinkInNewTab:(id)aSender
        !           716: {
        !           717:   [self openLinkInNewWindowOrTab: NO];
        !           718: }
        !           719: 
        !           720: -(void)openLinkInNewWindowOrTab: (BOOL)aUseWindow
        !           721: {
        !           722:   nsCOMPtr<nsIDOMElement> linkContent;
        !           723:   nsAutoString href;
        !           724:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
        !           725: 
        !           726:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
        !           727:   // really uses these anyway? :)
        !           728:   if (!linkContent || href.IsEmpty())
        !           729:     return;
        !           730: 
        !           731:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
        !           732:   if (!pref)
        !           733:     return; // Something bad happened if we can't get prefs.
        !           734: 
        !           735:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
        !           736:   NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
        !           737: 
        !           738:   PRBool loadInBackground;
        !           739:   pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
        !           740: 
        !           741:   if (aUseWindow)
        !           742:     [self openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
        !           743:   else
        !           744:     [self openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
        !           745: }
        !           746: 
        !           747: - (IBAction)saveLinkAs:(id)aSender
        !           748: {
        !           749: }
        !           750: 
        !           751: - (IBAction)saveImageAs:(id)aSender
        !           752: {
1.58      hyatt     753: }
1.52      macserv   754: 
1.1       hyatt     755: @end
                    756: 

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