1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2: /* ***** BEGIN LICENSE BLOCK *****
3: * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4: *
5: * The contents of this file are subject to the Netscape Public License
6: * Version 1.1 (the "License"); you may not use this file except in
7: * compliance with the License. You may obtain a copy of the License at
8: * http://www.mozilla.org/NPL/
9: *
10: * Software distributed under the License is distributed on an "AS IS" basis,
11: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12: * for the specific language governing rights and limitations under the
13: * License.
14: *
15: * The Original Code is mozilla.org code.
16: *
17: * The Initial Developer of the Original Code is
18: * Netscape Communications Corporation.
19: * Portions created by the Initial Developer are Copyright (C) 2002
20: * the Initial Developer. All Rights Reserved.
21: *
22: * Contributor(s):
23: *
24: * Alternatively, the contents of this file may be used under the terms of
25: * either the GNU General Public License Version 2 or later (the "GPL"), or
26: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27: * in which case the provisions of the GPL or the LGPL are applicable instead
28: * of those above. If you wish to allow use of your version of this file only
29: * under the terms of either the GPL or the LGPL, and not to allow others to
30: * use your version of this file under the terms of the NPL, indicate your
31: * decision by deleting the provisions above and replace them with the notice
32: * and other provisions required by the GPL or the LGPL. If you do not delete
33: * the provisions above, a recipient may use your version of this file under
34: * the terms of any one of the NPL, the GPL or the LGPL.
35: *
36: * ***** END LICENSE BLOCK ***** */
37:
38: #import "BrowserWindowController.h"
39: #import "MyBrowserView.h"
40: #import "IconTabViewItem.h"
41:
42: #include "nsIWebNavigation.h"
43: #include "nsIDOMElement.h"
44: #include "nsIDOMEvent.h"
45: #include "nsIPrefBranch.h"
46: #include "nsIContextMenuListener.h"
47: #include "nsIDOMWindow.h"
48: #include "nsIScriptGlobalObject.h"
49: #include "nsIDocShell.h"
50: #include "nsIMarkupDocumentViewer.h"
51: #include "nsIContentViewer.h"
52: #include "nsCocoaBrowserService.h"
53: #include "nsString.h"
54: #include "GeckoUtils.h"
55:
56: static NSString *BrowserToolbarIdentifier = @"Browser Window Toolbar";
57: static NSString *BackToolbarItemIdentifier = @"Back Toolbar Item";
58: static NSString *ForwardToolbarItemIdentifier = @"Forward Toolbar Item";
59: static NSString *ReloadToolbarItemIdentifier = @"Reload Toolbar Item";
60: static NSString *StopToolbarItemIdentifier = @"Stop Toolbar Item";
61: static NSString *HomeToolbarItemIdentifier = @"Home Toolbar Item";
62: static NSString *LocationToolbarItemIdentifier = @"Location Toolbar Item";
63: static NSString *SidebarToolbarItemIdentifier = @"Sidebar Toolbar Item";
64: static NSString *PrintToolbarItemIdentifier = @"Print Toolbar Item";
65:
66: @interface BrowserWindowController(Private)
67: - (void)setupToolbar;
68: - (void)setupSidebarTabs;
69: @end
70:
71: @implementation BrowserWindowController
72:
73: -(void)enterModalSession
74: {
75: mModalSession = [NSApp beginModalSessionForWindow: [self window]];
76: [NSApp runModalSession: mModalSession];
77: [NSApp endModalSession: mModalSession];
78: mModalSession = nil;
79: }
80:
81: -(void)windowDidBecomeKey: (NSNotification*)aNotification
82: {
83: // May become necessary later.
84: }
85:
86: -(void)windowDidResignKey: (NSNotification*)aNotification
87: {
88: // May be needed later.
89: }
90:
91: -(void)mouseMoved:(NSEvent*)aEvent
92: {
93: if (mMoveReentrant)
94: return;
95:
96: mMoveReentrant = YES;
97: NSView* view = [[[self window] contentView] hitTest: [aEvent locationInWindow]];
98: [view mouseMoved: aEvent];
99: [super mouseMoved: aEvent];
100: mMoveReentrant = NO;
101: }
102:
103: - (id)initWithWindowNibName:(NSString *)windowNibName
104: {
105: if ( (self = [super initWithWindowNibName:(NSString *)windowNibName]) ) {
106: if ( nsCocoaBrowserService::sNumBrowsers == 0 ) {
107: [self setShouldCascadeWindows:NO];
108: } else {
109: [self setShouldCascadeWindows:YES];
110: }
111: mInitialized = NO;
112: mMoveReentrant = NO;
113: mShouldAutosave = YES;
114: mChromeMask = 0;
115: mContextMenuFlags = 0;
116: mContextMenuEvent = nsnull;
117: mContextMenuNode = nsnull;
118: }
119: return self;
120: }
121:
122: -(void)autosaveWindowFrame
123: {
124: if (mShouldAutosave)
125: [[self window] saveFrameUsingName: @"NavigatorWindow"];
126: }
127:
128: -(void)disableAutosave
129: {
130: mShouldAutosave = NO;
131: }
132:
133: - (void)windowWillClose:(NSNotification *)notification
134: {
135: printf("Window will close notification.\n");
136: [mSidebarBookmarksDataSource windowClosing];
137:
138: [self autosaveWindowFrame];
139: [self autorelease];
140: }
141:
142: - (void)dealloc
143: {
144: printf("Browser controller died.\n");
145:
146: // Loop over all tabs, and tell them that the window is closed.
147: int numTabs = [mTabBrowser numberOfTabViewItems];
148: for (int i = 0; i < numTabs; i++) {
149: NSTabViewItem* item = [mTabBrowser tabViewItemAtIndex: i];
150: [[item view] windowClosed];
151: }
152: [mSidebarBrowserView windowClosed];
153:
154: [mProgress release];
155:
156: [super dealloc];
157: }
158:
159: - (void)windowDidLoad
160: {
161: [super windowDidLoad];
162:
163: [[mURLBar cell] setImage: [NSImage imageNamed:@"smallbookmark"]];
164:
165: // Get our saved dimensions.
166: [[self window] setFrameUsingName: @"NavigatorWindow"];
167:
168: if (mModalSession)
169: [NSApp stopModal: mModalSession];
170:
171: mInitialized = YES;
172:
173: mDrawerCachedFrame = NO;
174:
175: // Retain with a single extra refcount. This allows the MyBrowserViews
176: // to remove the progress meter from its superview without having to
177: // worry about retaining and releasing it.
178: [mProgress retain];
179:
180: [[self window] setAcceptsMouseMovedEvents: YES];
181:
182: [self setupToolbar];
183:
184: // 03/03/2002 mlj Changing strategy a bit here. The addTab: method was
185: // duplicating a lot of the code found here. I have moved it to that method.
186: // We now remove the IB tab, then add one of our own.
187:
188: [mTabBrowser removeTabViewItem:[mTabBrowser tabViewItemAtIndex:0]];
189: [self newTab];
190:
191: if (mURL) {
192: [self loadURL: mURL];
193: [mURL release];
194: }
195:
196: [mSidebarDrawer setDelegate: self];
197:
198: [self setupSidebarTabs];
199:
200: [mPersonalToolbar initializeToolbar];
201: }
202:
203: - (void)drawerWillOpen: (NSNotification*)aNotification
204: {
205: [mSidebarBookmarksDataSource ensureBookmarks];
206:
207: // Force the window to shrink and move if necessary in order to accommodate the sidebar.
208: NSRect screenFrame = [[[self window] screen] visibleFrame];
209: NSRect windowFrame = [[self window] frame];
210: NSSize drawerSize = [mSidebarDrawer contentSize];
211: int fudgeFactor = 12; // Not sure how to get the drawer's border info, so we fudge it for now.
212: drawerSize.width += fudgeFactor;
213: if (windowFrame.origin.x + windowFrame.size.width + drawerSize.width >
214: screenFrame.size.width) {
215: // We need to adjust the window so that it can fit.
216: int shrinkDelta = (windowFrame.size.width + drawerSize.width) - screenFrame.size.width;
217: if (shrinkDelta < 0) shrinkDelta = 0;
218: int newWidth = (windowFrame.size.width - shrinkDelta);
219: int newPosition = screenFrame.size.width - newWidth - drawerSize.width;
220: if (newPosition < 0) newPosition = 0;
221: mCachedFrameBeforeDrawerOpen = windowFrame;
222: windowFrame.origin.x = newPosition;
223: windowFrame.size.width = newWidth;
224: mCachedFrameAfterDrawerOpen = windowFrame;
225: [[self window] setFrame: windowFrame display: YES];
226: mDrawerCachedFrame = YES;
227: }
228: }
229:
230: - (void)drawerDidOpen:(NSNotification *)aNotification
231: {
232: // XXXdwh This is temporary.
233: // [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
234:
235: // Toggle the sidebar icon.
236: [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarOpened"]];
237: }
238:
239: - (void)drawerDidClose:(NSNotification *)aNotification
240: {
241: // Unload the Gecko web page in "My Panels" to save memory.
242: [mSidebarToolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
243:
244: // XXXdwh ignore for now.
245: // [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
246:
247: if (mDrawerCachedFrame) {
248: printf("Got here.\n");
249: mDrawerCachedFrame = NO;
250: NSRect frame = [[self window] frame];
251: if (frame.origin.x == mCachedFrameAfterDrawerOpen.origin.x &&
252: frame.origin.y == mCachedFrameAfterDrawerOpen.origin.y &&
253: frame.size.width == mCachedFrameAfterDrawerOpen.size.width &&
254: frame.size.height == mCachedFrameAfterDrawerOpen.size.height) {
255: printf("Got here too.\n");
256: printf("Xes are %d %d\n", frame.origin.x, mCachedFrameAfterDrawerOpen.origin.x);
257: printf("Widths are %d %d\n", frame.size.width, mCachedFrameAfterDrawerOpen.size.width);
258: // Restore the original frame.
259: [[self window] setFrame: mCachedFrameBeforeDrawerOpen display: YES];
260: }
261: }
262: }
263:
264: - (void)setupToolbar
265: {
266: if (mChromeMask) {
267: printf("Uh-oh. %d\n", mChromeMask);
268: }
269:
270: NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
271:
272: [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
273: [toolbar setAllowsUserCustomization:YES];
274: [toolbar setAutosavesConfiguration:YES];
275: [toolbar setDelegate:self];
276: [[self window] setToolbar:toolbar];
277: }
278:
279:
280: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
281: {
282: return [NSArray arrayWithObjects: BackToolbarItemIdentifier,
283: ForwardToolbarItemIdentifier,
284: ReloadToolbarItemIdentifier,
285: StopToolbarItemIdentifier,
286: HomeToolbarItemIdentifier,
287: LocationToolbarItemIdentifier,
288: SidebarToolbarItemIdentifier,
289: PrintToolbarItemIdentifier,
290: NSToolbarCustomizeToolbarItemIdentifier,
291: NSToolbarFlexibleSpaceItemIdentifier,
292: NSToolbarSpaceItemIdentifier,
293: NSToolbarSeparatorItemIdentifier,
294: nil];
295: }
296:
297: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
298: {
299: return [NSArray arrayWithObjects: BackToolbarItemIdentifier,
300: ForwardToolbarItemIdentifier,
301: ReloadToolbarItemIdentifier,
302: StopToolbarItemIdentifier,
303: HomeToolbarItemIdentifier,
304: LocationToolbarItemIdentifier,
305: SidebarToolbarItemIdentifier,
306: nil];
307: }
308:
309: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
310: itemForItemIdentifier:(NSString *)itemIdent
311: willBeInsertedIntoToolbar:(BOOL)willBeInserted
312: {
313: NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
314: if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
315: [toolbarItem setLabel:@"Back"];
316: [toolbarItem setPaletteLabel:@"Go Back"];
317: [toolbarItem setToolTip:@"Go back one page"];
318: [toolbarItem setImage:[NSImage imageNamed:@"back"]];
319: [toolbarItem setTarget:self];
320: [toolbarItem setAction:@selector(back:)];
321: } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
322: [toolbarItem setLabel:@"Forward"];
323: [toolbarItem setPaletteLabel:@"Go Forward"];
324: [toolbarItem setToolTip:@"Go forward one page"];
325: [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
326: [toolbarItem setTarget:self];
327: [toolbarItem setAction:@selector(forward:)];
328: } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
329: [toolbarItem setLabel:@"Reload"];
330: [toolbarItem setPaletteLabel:@"Reload Page"];
331: [toolbarItem setToolTip:@"Reload current page"];
332: [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
333: [toolbarItem setTarget:self];
334: [toolbarItem setAction:@selector(reload:)];
335: } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
336: [toolbarItem setLabel:@"Stop"];
337: [toolbarItem setPaletteLabel:@"Stop Loading"];
338: [toolbarItem setToolTip:@"Stop loading this page"];
339: [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
340: [toolbarItem setTarget:self];
341: [toolbarItem setAction:@selector(stop:)];
342: } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
343: [toolbarItem setLabel:@"Home"];
344: [toolbarItem setPaletteLabel:@"Go Home"];
345: [toolbarItem setToolTip:@"Go to home page"];
346: [toolbarItem setImage:[NSImage imageNamed:@"home"]];
347: [toolbarItem setTarget:self];
348: [toolbarItem setAction:@selector(home:)];
349: } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
350: [toolbarItem setLabel:@"Sidebar"];
351: [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
352: [toolbarItem setToolTip:@"Show or hide the Sidebar"];
353: [toolbarItem setImage:[NSImage imageNamed:@"sidebarClosed"]];
354: [toolbarItem setTarget:self];
355: [toolbarItem setAction:@selector(toggleSidebar:)];
356: mSidebarToolbarItem = toolbarItem;
357: } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
358:
359: NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
360:
361: [toolbarItem setLabel:@"Location"];
362: [toolbarItem setPaletteLabel:@"Location"];
363: [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
364: [toolbarItem setView:mLocationToolbarView];
365: [toolbarItem setMinSize:NSMakeSize(128,32)];
366: [toolbarItem setMaxSize:NSMakeSize(2560,32)];
367:
368: [menuFormRep setTarget:self];
369: [menuFormRep setAction:@selector(performAppropriateLocationAction)];
370: [menuFormRep setTitle:[toolbarItem label]];
371:
372: [toolbarItem setMenuFormRepresentation:menuFormRep];
373: mLocationToolbarItem = toolbarItem;
374:
375: } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
376: [toolbarItem setLabel:@"Print"];
377: [toolbarItem setPaletteLabel:@"Print"];
378: [toolbarItem setToolTip:@"Print this page"];
379: [toolbarItem setImage:[NSImage imageNamed:@"print"]];
380: [toolbarItem setTarget:self];
381: [toolbarItem setAction:@selector(printDocument)];
382: } else {
383: toolbarItem = nil;
384: }
385:
386: return toolbarItem;
387: }
388:
389: // This method handles the enabling/disabling of the toolbar buttons.
390: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
391: {
392: // Check the action and see if it matches.
393: SEL action = [theItem action];
394: if (action == @selector(back:))
395: return [[mBrowserView getBrowserView] canGoBack];
396: else if (action == @selector(forward:))
397: return [[mBrowserView getBrowserView] canGoForward];
398: else if (action == @selector(reload:))
399: return [mBrowserView isBusy] == NO;
400: else if (action == @selector(stop:))
401: return [mBrowserView isBusy];
402: else
403: return YES;
404: }
405:
406: - (void)updateToolbarItems
407: {
408: [[[self window] toolbar] validateVisibleItems];
409: }
410:
411: - (void)performAppropriateLocationAction
412: {
413: if ( [[[self window] toolbar] isVisible] ) {
414: if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
415: ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
416: [self focusURLBar];
417: } else {
418: [self beginLocationSheet];
419: }
420: } else {
421: [self beginLocationSheet];
422: }
423: }
424:
425: - (void)focusURLBar
426: {
427: [mURLBar selectText: self];
428: }
429:
430: - (void)beginLocationSheet
431: {
432: [NSApp beginSheet: mLocationSheetWindow
433: modalForWindow: [self window]
434: modalDelegate: nil
435: didEndSelector: nil
436: contextInfo: nil];
437: }
438:
439: - (IBAction)endLocationSheet:(id)sender
440: {
441: [mLocationSheetWindow orderOut:self];
442: [NSApp endSheet:mLocationSheetWindow returnCode:1];
443: [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
444:
445: // Focus and activate our content area.
446: [[mBrowserView getBrowserView] setActive: YES];
447: }
448:
449: -(IBAction)cancelAddBookmarkSheet:(id)sender
450: {
451: [mAddBookmarkSheetWindow orderOut:self];
452: [NSApp endSheet:mAddBookmarkSheetWindow returnCode:0];
453: [mCachedBMDS endAddBookmark: 0];
454: }
455:
456: -(IBAction)endAddBookmarkSheet:(id)sender
457: {
458: [mAddBookmarkSheetWindow orderOut:self];
459: [NSApp endSheet:mAddBookmarkSheetWindow returnCode:0];
460: [mCachedBMDS endAddBookmark: 1];
461: }
462:
463: - (void)cacheBookmarkDS: (id)aDS
464: {
465: mCachedBMDS = aDS;
466: }
467:
468: -(IBAction)manageBookmarks: (id)aSender
469: {
470: if (![mSidebarDrawer isOpen])
471: [self toggleSidebar: self];
472:
473: [mSidebarTabView selectFirstTabViewItem:self];
474: }
475:
476: - (IBAction)goToLocationFromToolbarURLField:(id)sender
477: {
478: [self loadURL:[NSURL URLWithString:[sender stringValue]]];
479:
480: // Focus and activate our content area.
481: [[mBrowserView getBrowserView] setActive: YES];
482: }
483:
484: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
485: {
486: [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
487: }
488:
489: - (void)saveURL: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
490: url: (NSURL*)aURL suggestedFilename: (NSString*)aFilename
491: {
492: [[mBrowserView getBrowserView] saveURL: aFilterView filterList: aFilterList
493: url: aURL suggestedFilename: aFilename];
494: }
495:
496: - (void)printDocument
497: {
498: [[mBrowserView getBrowserView] printDocument];
499: }
500:
501: - (void)printPreview
502: {
503: [[mBrowserView getBrowserView] printPreview];
504: }
505:
506: - (BOOL)findInPage:(NSString*)text
507: {
508: return [[mBrowserView getBrowserView] findInPage:text];
509: }
510:
511: - (void)addBookmarkExtended: (BOOL)aIsFromMenu
512: {
513: [mSidebarBookmarksDataSource ensureBookmarks];
514: [mSidebarBookmarksDataSource addBookmark: self useSelection: !aIsFromMenu];
515: }
516:
517: - (IBAction)back:(id)aSender
518: {
519: [[mBrowserView getBrowserView] goBack];
520: }
521:
522: - (IBAction)forward:(id)aSender
523: {
524: [[mBrowserView getBrowserView] goForward];
525: }
526:
527: - (IBAction)reload:(id)aSender
528: {
529: [[mBrowserView getBrowserView] reload: 0];
530: }
531:
532: - (IBAction)stop:(id)aSender
533: {
534: [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
535: }
536:
537: - (IBAction)home:(id)aSender
538: {
539: [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
540: }
541:
542: - (IBAction)toggleSidebar:(id)aSender
543: {
544: NSResponder* resp = [[self window] firstResponder];
545: [[self window] makeFirstResponder: nil];
546:
547: if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) {
548: // XXXHack to bypass sidebar crashes.
549: [mSidebarDrawer openOnEdge: NSMaxXEdge];
550: } else {
551: [mSidebarDrawer close];
552: }
553:
554: [[self window] makeFirstResponder: resp];
555: }
556:
557:
558: -(void)loadURL:(NSURL*)aURL
559: {
560: if (mInitialized) {
561: [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
562: }
563: else {
564: mURL = aURL;
565: [mURL retain];
566: }
567: }
568:
569: - (void)updateLocationFields:(NSString *)locationString
570: {
571: /* //commenting this out because it doesn't work right yet.
572: if ( [locationString length] > 30 ) {
573: [[mLocationToolbarItem menuFormRepresentation] setTitle:
574: [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
575: } else {
576: [[mLocationToolbarItem menuFormRepresentation] setTitle:
577: [NSString stringWithFormat:@"Location: %@", locationString]];
578: }
579: */
580:
581: [mURLBar setStringValue:locationString];
582: [mLocationSheetURLField setStringValue:locationString];
583:
584: [[self window] update];
585: [[self window] display];
586: }
587:
588: -(void)newTab
589: {
590: NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
591: MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
592: [newView setTab: newTab];
593:
594: [newTab setLabel: @"Untitled"];
595: [newTab setView: newView];
596:
597: [mTabBrowser addTabViewItem: newTab];
598:
599: [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
600:
601: [mTabBrowser selectLastTabViewItem: self];
602:
603: if ( [[[self window] toolbar] isVisible] ) {
604: if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
605: ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
606: if ([mTabBrowser numberOfTabViewItems] > 1) {
607: [self focusURLBar];
608: [[mBrowserView getBrowserView] setActive: NO];
609: }
610: else if ([[self window] isKeyWindow])
611: [[mBrowserView getBrowserView] setActive: YES];
612: else
613: [[mBrowserView getBrowserView] setActive: NO];
614: }
615: }
616: }
617:
618: -(void)closeTab
619: {
620: if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
621: [[[mTabBrowser selectedTabViewItem] view] windowClosed];
622: [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
623: }
624: }
625:
626: - (void)previousTab
627: {
628: [mTabBrowser selectPreviousTabViewItem:self];
629: }
630:
631: - (void)nextTab
632: {
633: [mTabBrowser selectNextTabViewItem:self];
634: }
635:
636: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
637: {
638: // Disconnect the old view, if one has been designated.
639: // If the window has just been opened, none has been.
640: if ( mBrowserView ) {
641: [mBrowserView disconnectView];
642: }
643: // Connect up the new view
644: mBrowserView = [aTabViewItem view];
645:
646: // Make the new view the primary content area.
647: [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
648: progress: mProgress windowController: self];
649: }
650:
651: -(MyBrowserView*)getMyBrowserView
652: {
653: return mBrowserView;
654: }
655:
656: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
657: {
658: // Autosave our dimensions before we open a new window. That ensures the size ends up matching.
659: [self autosaveWindowFrame];
660:
661: BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
662: [browser loadURL: aURL];
663: if (aLoadInBG)
664: [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
665: else {
666: // Focus the content area and show the window.
667: [browser enterModalSession];
668: [[[browser getMyBrowserView] getBrowserView] setActive: YES];
669: }
670: }
671:
672: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
673: {
674: NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
675:
676: NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
677: int index = [mTabBrowser indexOfTabViewItem: selectedTab];
678: [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
679:
680: MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
681: [newView setTab: newTab];
682:
683: [newTab setLabel: @"Loading..."];
684: [newTab setView: newView];
685:
686: [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
687:
688: if (!aLoadInBG) {
689: [mTabBrowser selectTabViewItem: newTab];
690: // Focus the content area.
691: [[newView getBrowserView] setActive: YES];
692: }
693: }
694:
695: -(void)setupSidebarTabs
696: {
697: IconTabViewItem *bookItem = [[IconTabViewItem alloc] initWithIdentifier:@"bookmarkSidebarIconTabViewItem"
698: withTabIcon:[NSImage imageNamed:@"bookicon"]];
699: IconTabViewItem *histItem = [[IconTabViewItem alloc] initWithIdentifier:@"historySidebarIconTabViewItem"
700: withTabIcon:[NSImage imageNamed:@"historyicon"]];
701: IconTabViewItem *searchItem = [[IconTabViewItem alloc] initWithIdentifier:@"searchSidebarIconTabViewItem"
702: withTabIcon:[NSImage imageNamed:@"searchicon"]];
703: IconTabViewItem *panelsItem = [[IconTabViewItem alloc] initWithIdentifier:@"myPanelsIconTabViewItem"
704: withTabIcon:[NSImage imageNamed:@"panel_icon"]];
705:
706: [bookItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:0] view]];
707: [histItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:1] view]];
708: [searchItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:2] view]];
709: [panelsItem setView:[[mSidebarSourceTabView tabViewItemAtIndex:3] view]];
710:
711: [mSidebarTabView removeTabViewItem:[mSidebarTabView tabViewItemAtIndex:0]];
712:
713: [mSidebarTabView insertTabViewItem:bookItem atIndex:0];
714: [mSidebarTabView insertTabViewItem:histItem atIndex:1];
715: [mSidebarTabView insertTabViewItem:searchItem atIndex:2];
716: [mSidebarTabView insertTabViewItem:panelsItem atIndex:3];
717:
718: [mSidebarTabView selectFirstTabViewItem:self];
719: }
720:
721: -(void)setChromeMask:(int)aMask
722: {
723: mChromeMask = aMask;
724: }
725:
726: -(void) biggerTextSize
727: {
728: nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[mBrowserView getBrowserView] getContentWindow]);
729: nsCOMPtr<nsIScriptGlobalObject> global(do_QueryInterface(contentWindow));
730: if (!global)
731: return;
732: nsCOMPtr<nsIDocShell> docShell;
733: global->GetDocShell(getter_AddRefs(docShell));
734: if (!docShell)
735: return;
736: nsCOMPtr<nsIContentViewer> cv;
737: docShell->GetContentViewer(getter_AddRefs(cv));
738: nsCOMPtr<nsIMarkupDocumentViewer> markupViewer(do_QueryInterface(cv));
739: if (!markupViewer)
740: return;
741: float zoom;
742: markupViewer->GetTextZoom(&zoom);
743: if (zoom >= 20)
744: return;
745:
746: zoom += 0.25;
747: if (zoom > 20)
748: zoom = 20;
749:
750: markupViewer->SetTextZoom(zoom);
751: }
752:
753: -(void) smallerTextSize
754: {
755: nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[mBrowserView getBrowserView] getContentWindow]);
756: nsCOMPtr<nsIScriptGlobalObject> global(do_QueryInterface(contentWindow));
757: if (!global)
758: return;
759: nsCOMPtr<nsIDocShell> docShell;
760: global->GetDocShell(getter_AddRefs(docShell));
761: if (!docShell)
762: return;
763: nsCOMPtr<nsIContentViewer> cv;
764: docShell->GetContentViewer(getter_AddRefs(cv));
765: nsCOMPtr<nsIMarkupDocumentViewer> markupViewer(do_QueryInterface(cv));
766: if (!markupViewer)
767: return;
768: float zoom;
769: markupViewer->GetTextZoom(&zoom);
770: if (zoom <= 0.01)
771: return;
772:
773: zoom -= 0.25;
774: if (zoom < 0.01)
775: zoom = 0.01;
776:
777: markupViewer->SetTextZoom(zoom);
778: }
779:
780: -(id)getAddBookmarkSheetWindow
781: {
782: return mAddBookmarkSheetWindow;
783: }
784:
785: -(id)getAddBookmarkTitle
786: {
787: return mAddBookmarkTitleField;
788: }
789:
790: -(id)getAddBookmarkFolder
791: {
792: return mAddBookmarkFolderField;
793: }
794:
795: // Called when a context menu should be shown.
796: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
797: {
798: mContextMenuFlags = flags;
799: mContextMenuNode = aNode;
800: mContextMenuEvent = aEvent;
801: }
802:
803: - (NSMenu*)getContextMenu
804: {
805: NSMenu* result = nil;
806: if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_LINK) != 0) {
807: if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
808: result = mImageLinkMenu;
809: }
810: else
811: result = mLinkMenu;
812: }
813: else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_INPUT) != 0 ||
814: (mContextMenuFlags & nsIContextMenuListener::CONTEXT_TEXT) != 0) {
815: result = mInputMenu;
816: }
817: else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_IMAGE) != 0) {
818: result = mImageMenu;
819: }
820: else if ((mContextMenuFlags & nsIContextMenuListener::CONTEXT_DOCUMENT) != 0) {
821: result = mPageMenu;
822: }
823:
824: return result;
825: }
826:
827: // Context menu methods
828: - (IBAction)openLinkInNewWindow:(id)aSender
829: {
830: [self openLinkInNewWindowOrTab: YES];
831: }
832:
833: - (IBAction)openLinkInNewTab:(id)aSender
834: {
835: [self openLinkInNewWindowOrTab: NO];
836: }
837:
838: -(void)openLinkInNewWindowOrTab: (BOOL)aUseWindow
839: {
840: nsCOMPtr<nsIDOMElement> linkContent;
841: nsAutoString href;
842: GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
843:
844: // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
845: // really uses these anyway? :)
846: if (!linkContent || href.IsEmpty())
847: return;
848:
849: nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
850: if (!pref)
851: return; // Something bad happened if we can't get prefs.
852:
853: NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
854: NSURL* urlToLoad = [NSURL URLWithString: hrefStr];
855:
856: PRBool loadInBackground;
857: pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBackground);
858:
859: if (aUseWindow)
860: [self openNewWindowWithURL: urlToLoad loadInBackground: loadInBackground];
861: else
862: [self openNewTabWithURL: urlToLoad loadInBackground: loadInBackground];
863: }
864:
865: - (IBAction)savePageAs:(id)aSender
866: {
867: [self saveDocument: nil filterList: nil];
868: }
869:
870: - (IBAction)saveLinkAs:(id)aSender
871: {
872: nsCOMPtr<nsIDOMElement> linkContent;
873: nsAutoString href;
874: GeckoUtils::GetEnclosingLinkElementAndHref(mContextMenuNode, getter_AddRefs(linkContent), href);
875:
876: // XXXdwh Handle simple XLINKs if we want to be compatible with Mozilla, but who
877: // really uses these anyway? :)
878: if (!linkContent || href.IsEmpty())
879: return;
880:
881: NSString* hrefStr = [NSString stringWithCharacters: href.get() length:nsCRT::strlen(href.get())];
882: NSURL* urlToSave = [NSURL URLWithString: hrefStr];
883:
884: // The user wants to save this link.
885: nsAutoString text;
886: GeckoUtils::GatherTextUnder(mContextMenuNode, text);
887:
888: [self saveURL: nil filterList: nil
889: url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
890: length:nsCRT::strlen(text.get())]];
891: }
892:
893: - (IBAction)saveImageAs:(id)aSender
894: {
895: nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
896: if (imgElement) {
897: nsAutoString text;
898: imgElement->GetAttribute(NS_LITERAL_STRING("src"), text);
899: nsAutoString url;
900: imgElement->GetSrc(url);
901:
902: NSString* hrefStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
903: NSURL* urlToSave = [NSURL URLWithString: hrefStr];
904:
905: [self saveURL: nil filterList: nil
906: url: urlToSave suggestedFilename: [NSString stringWithCharacters: text.get()
907: length:nsCRT::strlen(text.get())]];
908: }
909: }
910:
911: - (IBAction)viewOnlyThisImage:(id)aSender
912: {
913: nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(mContextMenuNode));
914: if (imgElement) {
915: nsAutoString url;
916: imgElement->GetSrc(url);
917:
918: NSString* urlStr = [NSString stringWithCharacters: url.get() length:nsCRT::strlen(url.get())];
919: NSURL* urlToView = [NSURL URLWithString: urlStr];
920:
921: [self loadURL: urlToView];
922:
923: // Focus and activate our content area.
924: [[mBrowserView getBrowserView] setActive: YES];
925: }
926: }
927:
928: @end
929:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>