Annotation of chimera/BrowserWindowController.mm, revision 1.63

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: 
1.63    ! hyatt     440: -(IBAction)cancelAddBookmarkSheet:(id)sender
        !           441: {
        !           442:   [mAddBookmarkSheetWindow orderOut:self];
        !           443:   [NSApp endSheet:mAddBookmarkSheetWindow returnCode:0];
        !           444:   [mCachedBMDS endAddBookmark: 0];
        !           445: }
        !           446: 
        !           447: -(IBAction)endAddBookmarkSheet:(id)sender
        !           448: {
        !           449:   [mAddBookmarkSheetWindow orderOut:self];
        !           450:   [NSApp endSheet:mAddBookmarkSheetWindow returnCode:0];
        !           451:   [mCachedBMDS endAddBookmark: 1];
        !           452: }
        !           453: 
        !           454: - (void)cacheBookmarkDS: (id)aDS
        !           455: {
        !           456:   mCachedBMDS = aDS;
        !           457: }
        !           458: 
        !           459: -(IBAction)manageBookmarks: (id)aSender
        !           460: {
        !           461:   if (![mSidebarDrawer isOpen])
        !           462:     [self toggleSidebar: self];
        !           463: 
        !           464:   [mSidebarTabView selectFirstTabViewItem:self];
        !           465: }
        !           466: 
1.5       macserv   467: - (IBAction)goToLocationFromToolbarURLField:(id)sender
                    468: {
                    469:     [self loadURL:[NSURL URLWithString:[sender stringValue]]];
1.32      hyatt     470:     
                    471:     // Focus and activate our content area.
                    472:     [[mBrowserView getBrowserView] setActive: YES];
1.5       macserv   473: }
                    474: 
1.7       hyatt     475: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
1.6       hyatt     476: {
1.62      hyatt     477:   [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
                    478: }
                    479: 
                    480: - (void)saveURL: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
                    481:             url: (NSURL*)aURL suggestedFilename: (NSString*)aFilename
                    482: {
                    483:   [[mBrowserView getBrowserView] saveURL: aFilterView filterList: aFilterList
                    484:                                      url: aURL suggestedFilename: aFilename];
1.6       hyatt     485: }
                    486: 
1.12      hyatt     487: - (void)printDocument
                    488: {
                    489:     [[mBrowserView getBrowserView] printDocument];
                    490: }
                    491: 
1.15      hyatt     492: - (void)printPreview
                    493: {
                    494:     [[mBrowserView getBrowserView] printPreview];
                    495: }
                    496: 
1.60      pinkerto  497: - (BOOL)findInPage:(NSString*)text
1.16      hyatt     498: {
1.60      pinkerto  499:     return [[mBrowserView getBrowserView] findInPage:text];
1.22      hyatt     500: }
                    501: 
1.62      hyatt     502: - (void)addBookmarkExtended: (BOOL)aIsFromMenu
1.22      hyatt     503: {
1.62      hyatt     504:   [mSidebarBookmarksDataSource ensureBookmarks];
                    505:   [mSidebarBookmarksDataSource addBookmark: self useSelection: !aIsFromMenu];
1.16      hyatt     506: }
                    507: 
1.5       macserv   508: - (IBAction)back:(id)aSender
1.1       hyatt     509: {
                    510:   [[mBrowserView getBrowserView] goBack];
                    511: }
                    512: 
1.5       macserv   513: - (IBAction)forward:(id)aSender
1.1       hyatt     514: {
                    515:   [[mBrowserView getBrowserView] goForward];
                    516: }
                    517: 
1.5       macserv   518: - (IBAction)reload:(id)aSender
1.1       hyatt     519: {
                    520:   [[mBrowserView getBrowserView] reload: 0];
                    521: }
                    522: 
1.5       macserv   523: - (IBAction)stop:(id)aSender
1.1       hyatt     524: {
1.37      hyatt     525:   [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
1.1       hyatt     526: }
                    527: 
1.5       macserv   528: - (IBAction)home:(id)aSender
1.1       hyatt     529: {
1.5       macserv   530:   [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.1       hyatt     531: }
                    532: 
1.5       macserv   533: - (IBAction)toggleSidebar:(id)aSender
1.1       hyatt     534: {
1.36      hyatt     535:     NSResponder* resp = [[self window] firstResponder];
                    536:     [[self window] makeFirstResponder: nil];
                    537:     
1.31      hyatt     538:     if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) )   {
1.34      hyatt     539:         // XXXHack to bypass sidebar crashes.
1.57      hyatt     540:         [mSidebarDrawer openOnEdge: NSMaxXEdge];
1.1       hyatt     541:     } else {
                    542:         [mSidebarDrawer close];
                    543:     }
1.36      hyatt     544:     
                    545:     [[self window] makeFirstResponder: resp];
1.1       hyatt     546: }
1.54      macserv   547: 
1.1       hyatt     548: 
1.4       hyatt     549: -(void)loadURL:(NSURL*)aURL
1.1       hyatt     550: {
1.32      hyatt     551:     if (mInitialized) {
1.4       hyatt     552:         [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
1.32      hyatt     553:     }
1.4       hyatt     554:     else {
                    555:         mURL = aURL;
                    556:         [mURL retain];
                    557:     }
1.1       hyatt     558: }
1.5       macserv   559: 
                    560: - (void)updateLocationFields:(NSString *)locationString
                    561: {
                    562: /* //commenting this out because it doesn't work right yet.
                    563:     if ( [locationString length] > 30 ) {
                    564:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    565:             [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
                    566:     } else {
                    567:         [[mLocationToolbarItem menuFormRepresentation] setTitle:
                    568:             [NSString stringWithFormat:@"Location: %@", locationString]];
                    569:     }
                    570: */
                    571: 
                    572:     [mURLBar setStringValue:locationString];
                    573:     [mLocationSheetURLField setStringValue:locationString];
                    574: 
                    575:     [[self window] update];
                    576:     [[self window] display];
1.8       hyatt     577: }
1.27      hyatt     578: 
                    579: -(void)newTab
                    580: {
1.28      hyatt     581:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
1.47      hyatt     582:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    583:     [newView setTab: newTab];
1.41      macserv   584: 
                    585:     [newTab setLabel: @"Untitled"];
                    586:     [newTab setView: newView];
                    587: 
1.28      hyatt     588:     [mTabBrowser addTabViewItem: newTab];
1.42      macserv   589: 
1.28      hyatt     590:     [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
1.42      macserv   591: 
1.28      hyatt     592:     [mTabBrowser selectLastTabViewItem: self];
1.42      macserv   593: 
1.29      hyatt     594:     if ( [[[self window] toolbar] isVisible] ) {
                    595:         if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
                    596:              ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
1.61      hyatt     597:           if ([mTabBrowser numberOfTabViewItems] > 1) {
                    598:             [self focusURLBar];
                    599:             [[mBrowserView getBrowserView] setActive: NO];
                    600:           }
                    601:           else if ([[self window] isKeyWindow])
                    602:             [[mBrowserView getBrowserView] setActive: YES];
                    603:           else
                    604:             [[mBrowserView getBrowserView] setActive: NO];
1.42      macserv   605:         }
1.29      hyatt     606:     }
1.42      macserv   607: }
                    608: 
                    609: -(void)closeTab
                    610: {
1.49      hyatt     611:   if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
                    612:     [[[mTabBrowser selectedTabViewItem] view] windowClosed];
                    613:     [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
                    614:   }
1.42      macserv   615: }
                    616: 
                    617: - (void)previousTab
                    618: {
                    619:     [mTabBrowser selectPreviousTabViewItem:self];
                    620: }
                    621: 
                    622: - (void)nextTab
                    623: {
                    624:     [mTabBrowser selectNextTabViewItem:self];
1.28      hyatt     625: }
1.27      hyatt     626: 
1.28      hyatt     627: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
                    628: {
1.41      macserv   629:     // Disconnect the old view, if one has been designated.
                    630:     // If the window has just been opened, none has been.
                    631:     if ( mBrowserView ) {
                    632:         [mBrowserView disconnectView];
                    633:     }
1.28      hyatt     634:     // Connect up the new view
                    635:     mBrowserView = [aTabViewItem view];
1.30      hyatt     636:        
1.28      hyatt     637:     // Make the new view the primary content area.
1.47      hyatt     638:     [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
1.28      hyatt     639:         progress: mProgress windowController: self];
1.27      hyatt     640: }
1.28      hyatt     641: 
1.31      hyatt     642: -(MyBrowserView*)getMyBrowserView
                    643: {
                    644:     return mBrowserView;
                    645: }
1.45      hyatt     646: 
                    647: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
                    648: {
1.48      hyatt     649:   // Autosave our dimensions before we open a new window.  That ensures the size ends up matching.
                    650:   [self autosaveWindowFrame];
                    651: 
                    652:   BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
                    653:   [browser loadURL: aURL];
                    654:   if (aLoadInBG)
                    655:     [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
1.62      hyatt     656:   else {
1.61      hyatt     657:     // Focus the content area and show the window.
1.62      hyatt     658:     [browser enterModalSession];
                    659:     [[[browser getMyBrowserView] getBrowserView] setActive: YES];
                    660:   }
1.47      hyatt     661: }
                    662: 
                    663: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
                    664: {
                    665:     NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
                    666:     
                    667:     NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
                    668:     int index = [mTabBrowser indexOfTabViewItem: selectedTab];
                    669:     [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
                    670:     
                    671:     MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
                    672:     [newView setTab: newTab];
                    673:     
                    674:     [newTab setLabel: @"Loading..."];
                    675:     [newTab setView: newView];
                    676: 
                    677:     [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
                    678: 
1.61      hyatt     679:     if (!aLoadInBG) {
                    680:       [mTabBrowser selectTabViewItem: newTab];
                    681:       // Focus the content area.
                    682:       [[newView getBrowserView] setActive: YES];
                    683:     }
1.45      hyatt     684: }
1.52      macserv   685: 
                    686: -(void)setupSidebarTabs
                    687: {
                    688:     IconTabViewItem   *bookItem = [[IconTabViewItem alloc] initWithIdentifier:@"bookmarkSidebarIconTabViewItem"
                    689:                                   withTabIcon:[NSImage imageNamed:@"bookicon"]];
                    690:     IconTabViewItem   *histItem = [[IconTabViewItem alloc] initWithIdentifier:@"historySidebarIconTabViewItem"
                    691:                                   withTabIcon:[NSImage imageNamed:@"historyicon"]];
                    692:     IconTabViewItem *searchItem = [[IconTabViewItem alloc] initWithIdentifier:@"searchSidebarIconTabViewItem"
                    693:                                   withTabIcon:[NSImage imageNamed:@"searchicon"]];
1.63    ! hyatt     694:     IconTabViewItem *panelsItem = [[IconTabViewItem alloc] initWithIdentifier:@"myPanelsIconTabViewItem"
        !           695:                                   withTabIcon:[NSImage imageNamed:@"panel_icon"]];
        !           696:     
1.52      macserv   697:     [bookItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:0] view]];
                    698:     [histItem   setView:[[mSidebarSourceTabView tabViewItemAtIndex:1] view]];
                    699:     [searchItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:2] view]];
1.63    ! hyatt     700:     [panelsItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:3] view]];
1.52      macserv   701: 
1.63    ! hyatt     702:     [mSidebarTabView removeTabViewItem:[mSidebarTabView tabViewItemAtIndex:0]];
        !           703:     
1.52      macserv   704:     [mSidebarTabView insertTabViewItem:bookItem   atIndex:0];
                    705:     [mSidebarTabView insertTabViewItem:histItem   atIndex:1];
                    706:     [mSidebarTabView insertTabViewItem:searchItem atIndex:2];
1.63    ! hyatt     707:     [mSidebarTabView insertTabViewItem:panelsItem atIndex:3];
        !           708:     
1.53      macserv   709:     [mSidebarTabView selectFirstTabViewItem:self];
1.52      macserv   710: }
                    711: 
1.58      hyatt     712: -(void)setChromeMask:(int)aMask
                    713: {
                    714:   mChromeMask = aMask;
1.63    ! hyatt     715: }
        !           716: 
        !           717: -(id)getAddBookmarkSheetWindow
        !           718: {
        !           719:   return mAddBookmarkSheetWindow;
        !           720: }
        !           721: 
        !           722: -(id)getAddBookmarkTitle
        !           723: {
        !           724:   return mAddBookmarkTitleField;
        !           725: }
        !           726: 
        !           727: -(id)getAddBookmarkFolder
        !           728: {
        !           729:   return mAddBookmarkFolderField;
1.59      hyatt     730: }
                    731: 
                    732: // Called when a context menu should be shown.
                    733: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
                    734: {
                    735:   mContextMenuFlags = flags;
                    736:   mContextMenuNode = aNode;
                    737:   mContextMenuEvent = aEvent;
                    738: }
                    739: 
                    740: - (NSMenu*)getContextMenu
                    741: {
                    742:   NSMenu* result = nil;
                    743:   if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_LINK) != 0) {
                    744:     if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
                    745:       result = mImageLinkMenu;
                    746:     }
                    747:     else
                    748:       result = mLinkMenu;
                    749:   }
                    750:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_INPUT) != 0 ||
                    751:            (mContextMenuFlags & nsIContextMenuListener::CONTEXT_TEXT) != 0) {
                    752:     result = mInputMenu;
                    753:   }
                    754:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
                    755:     result = mImageMenu;
                    756:   }
                    757:   else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_DOCUMENT) != 0) {
                    758:     result = mPageMenu;
                    759:   }
                    760:   
                    761:   return result;
1.61      hyatt     762: }
                    763: 
                    764: // Context menu methods
                    765: - (IBAction)openLinkInNewWindow:(id)aSender
                    766: {
                    767:   [self openLinkInNewWindowOrTab: YES];
                    768: }
                    769: 
                    770: - (IBAction)openLinkInNewTab:(id)aSender
                    771: {
                    772:   [self openLinkInNewWindowOrTab: NO];
                    773: }
                    774: 
                    775: -(void)openLinkInNewWindowOrTab: (BOOL)aUseWindow
                    776: {
                    777:   nsCOMPtr<nsIDOMElement> linkContent;
                    778:   nsAutoString href;
                    779:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
                    780: 
                    781:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
                    782:   // really uses these anyway? :)
                    783:   if (!linkContent || href.IsEmpty())
                    784:     return;
                    785: 
                    786:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
                    787:   if (!pref)
                    788:     return; // Something bad happened if we can't get prefs.
                    789: 
                    790:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
                    791:   NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
                    792: 
                    793:   PRBool loadInBackground;
                    794:   pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
                    795: 
                    796:   if (aUseWindow)
                    797:     [self openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
                    798:   else
                    799:     [self openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
                    800: }
                    801: 
1.62      hyatt     802: - (IBAction)savePageAs:(id)aSender
                    803: {
                    804:   [self saveDocument: nil filterList: nil];
                    805: }
                    806: 
1.61      hyatt     807: - (IBAction)saveLinkAs:(id)aSender
                    808: {
1.62      hyatt     809:   nsCOMPtr<nsIDOMElement> linkContent;
                    810:   nsAutoString href;
                    811:   GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
                    812: 
                    813:   // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
                    814:   // really uses these anyway? :)
                    815:   if (!linkContent || href.IsEmpty())
                    816:     return;
                    817: 
                    818:   NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
                    819:   NSURL* urlToSave = [NSURL URLWithString: hrefStr];
                    820: 
                    821:   // The user wants to save this link.
                    822:   nsAutoString text;
                    823:   GeckoUtils::GatherTextUnder(mContextMenuNode, text);
                    824: 
                    825:   [self saveURL: nil filterList: nil
                    826:             url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
                    827:                                                                       length:nsCRT::strlen(text.get())]];
1.61      hyatt     828: }
                    829: 
                    830: - (IBAction)saveImageAs:(id)aSender
                    831: {
1.62      hyatt     832:   nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
                    833:   if (imgElement) {
                    834:       nsAutoString text;
                    835:       imgElement->GetAttribute(NS_LITERAL_STRING("src"), text);
                    836:       nsAutoString url;
                    837:       imgElement->GetSrc(url);
                    838: 
                    839:       NSString* hrefStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
                    840:       NSURL* urlToSave = [NSURL URLWithString: hrefStr];
                    841: 
                    842:       [self saveURL: nil filterList: nil
                    843:                 url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
                    844:                                                                           length:nsCRT::strlen(text.get())]];
                    845:   }
                    846: }
                    847: 
                    848: - (IBAction)viewOnlyThisImage:(id)aSender
                    849: {
                    850:   nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
                    851:   if (imgElement) {
                    852:     nsAutoString url;
                    853:     imgElement->GetSrc(url);
                    854: 
                    855:     NSString* urlStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
                    856:     NSURL* urlToView = [NSURL URLWithString: urlStr];
                    857: 
                    858:     [self loadURL: urlToView];
                    859: 
                    860:     // Focus and activate our content area.
                    861:     [[mBrowserView getBrowserView] setActive: YES];
                    862:   }  
1.58      hyatt     863: }
1.52      macserv   864: 
1.1       hyatt     865: @end
                    866: 

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