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:
41: #include "nsIWebNavigation.h"
42:
43: static NSString *BrowserToolbarIdentifier = @"Browser Window Toolbar";
44: static NSString *BackToolbarItemIdentifier = @"Back Toolbar Item";
45: static NSString *ForwardToolbarItemIdentifier = @"Forward Toolbar Item";
46: static NSString *ReloadToolbarItemIdentifier = @"Reload Toolbar Item";
47: static NSString *StopToolbarItemIdentifier = @"Stop Toolbar Item";
48: static NSString *HomeToolbarItemIdentifier = @"Home Toolbar Item";
49: static NSString *LocationToolbarItemIdentifier = @"Location Toolbar Item";
50: static NSString *SidebarToolbarItemIdentifier = @"Sidebar Toolbar Item";
51: static NSString *PrintToolbarItemIdentifier = @"Print Toolbar Item";
52:
53: @interface BrowserWindowController(Private)
54: - (void)setupToolbar;
55: @end
56:
57: @implementation BrowserWindowController
58:
59: -(void)enterModalSession
60: {
61: mModalSession = [NSApp beginModalSessionForWindow: [self window]];
62: [NSApp runModalSession: mModalSession];
63: [NSApp endModalSession: mModalSession];
64: mModalSession = nil;
65: }
66:
67: -(void)mouseMoved:(NSEvent*)aEvent
68: {
69: if (mMoveReentrant)
70: return;
71:
72: mMoveReentrant = YES;
73: NSView* view = [[[self window] contentView] hitTest: [aEvent locationInWindow]];
74: [view mouseMoved: aEvent];
75: [super mouseMoved: aEvent];
76: mMoveReentrant = NO;
77: }
78:
79: - (id)initWithWindowNibName:(NSString *)windowNibName
80: {
81: if ( (self = [super initWithWindowNibName:(NSString *)windowNibName]) ) {
82: mInitialized = NO;
83: mMoveReentrant = NO;
84: mShouldAutosave = YES;
85: }
86: return self;
87: }
88:
89: -(void)autosaveWindowFrame
90: {
91: if (mShouldAutosave)
92: [[self window] saveFrameUsingName: @"NavigatorWindow"];
93: }
94:
95: -(void)disableAutosave
96: {
97: mShouldAutosave = NO;
98: }
99:
100: - (void)windowWillClose:(NSNotification *)notification
101: {
102: printf("Window will close notification.\n");
103: [mSidebarBookmarksDataSource windowClosing];
104:
105: [self autosaveWindowFrame];
106: [self autorelease];
107: }
108:
109: - (void)dealloc
110: {
111: printf("Browser controller died.\n");
112:
113: // Loop over all tabs, and tell them that the window is closed.
114: int numTabs = [mTabBrowser numberOfTabViewItems];
115: for (int i = 0; i < numTabs; i++) {
116: NSTabViewItem* item = [mTabBrowser tabViewItemAtIndex: i];
117: [[item view] windowClosed];
118: }
119: [mSidebarBrowserView windowClosed];
120:
121: [mProgress release];
122:
123: [super dealloc];
124: }
125:
126: - (void)windowDidLoad
127: {
128: [super windowDidLoad];
129:
130: // Get our saved dimensions.
131: [[self window] setFrameUsingName: @"NavigatorWindow"];
132:
133: if (mModalSession)
134: [NSApp stopModal: mModalSession];
135:
136: mInitialized = YES;
137:
138: // Retain with a single extra refcount. This allows the MyBrowserViews
139: // to remove the progress meter from its superview without having to
140: // worry about retaining and releasing it.
141: [mProgress retain];
142:
143: [[self window] setAcceptsMouseMovedEvents: YES];
144:
145: [self setupToolbar];
146:
147: // 03/03/2002 mlj Changing strategy a bit here. The addTab: method was
148: // duplicating a lot of the code found here. I have moved it to that method.
149: // We now remove the IB tab, then add one of our own.
150:
151: [mTabBrowser removeTabViewItem:[mTabBrowser tabViewItemAtIndex:0]];
152: [self newTab];
153:
154: if (mURL) {
155: [self loadURL: mURL];
156: [mURL release];
157: }
158:
159: [mSidebarDrawer setDelegate: self];
160: }
161:
162: - (void)drawerWillOpen: (NSNotification*)aNotification
163: {
164: [mSidebarBookmarksDataSource ensureBookmarks];
165: }
166:
167: - (void)drawerDidOpen:(NSNotification *)aNotification
168: {
169: // XXXdwh This is temporary.
170: [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
171: }
172:
173: - (void)drawerDidClose:(NSNotification *)aNotification
174: {
175: // Unload the Gecko web page in "My Panels" to save memory.
176: [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
177: }
178:
179: - (void)setupToolbar
180: {
181: NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
182:
183: [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
184: [toolbar setAllowsUserCustomization:YES];
185: [toolbar setAutosavesConfiguration:YES];
186: [toolbar setDelegate:self];
187: [[self window] setToolbar:toolbar];
188: }
189:
190:
191: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
192: {
193: return [NSArray arrayWithObjects: BackToolbarItemIdentifier,
194: ForwardToolbarItemIdentifier,
195: ReloadToolbarItemIdentifier,
196: StopToolbarItemIdentifier,
197: HomeToolbarItemIdentifier,
198: LocationToolbarItemIdentifier,
199: SidebarToolbarItemIdentifier,
200: PrintToolbarItemIdentifier,
201: NSToolbarCustomizeToolbarItemIdentifier,
202: NSToolbarFlexibleSpaceItemIdentifier,
203: NSToolbarSpaceItemIdentifier,
204: NSToolbarSeparatorItemIdentifier,
205: nil];
206: }
207:
208: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
209: {
210: return [NSArray arrayWithObjects: BackToolbarItemIdentifier,
211: ForwardToolbarItemIdentifier,
212: ReloadToolbarItemIdentifier,
213: StopToolbarItemIdentifier,
214: HomeToolbarItemIdentifier,
215: LocationToolbarItemIdentifier,
216: SidebarToolbarItemIdentifier,
217: nil];
218: }
219:
220: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
221: itemForItemIdentifier:(NSString *)itemIdent
222: willBeInsertedIntoToolbar:(BOOL)willBeInserted
223: {
224: NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
225: if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
226: [toolbarItem setLabel:@"Back"];
227: [toolbarItem setPaletteLabel:@"Go Back"];
228: [toolbarItem setToolTip:@"Go back one page"];
229: [toolbarItem setImage:[NSImage imageNamed:@"back"]];
230: [toolbarItem setTarget:self];
231: [toolbarItem setAction:@selector(back:)];
232: } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
233: [toolbarItem setLabel:@"Forward"];
234: [toolbarItem setPaletteLabel:@"Go Forward"];
235: [toolbarItem setToolTip:@"Go forward one page"];
236: [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
237: [toolbarItem setTarget:self];
238: [toolbarItem setAction:@selector(forward:)];
239: } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
240: [toolbarItem setLabel:@"Reload"];
241: [toolbarItem setPaletteLabel:@"Reload Page"];
242: [toolbarItem setToolTip:@"Reload current page"];
243: [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
244: [toolbarItem setTarget:self];
245: [toolbarItem setAction:@selector(reload:)];
246: } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
247: [toolbarItem setLabel:@"Stop"];
248: [toolbarItem setPaletteLabel:@"Stop Loading"];
249: [toolbarItem setToolTip:@"Stop loading this page"];
250: [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
251: [toolbarItem setTarget:self];
252: [toolbarItem setAction:@selector(stop:)];
253: } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
254: [toolbarItem setLabel:@"Home"];
255: [toolbarItem setPaletteLabel:@"Go Home"];
256: [toolbarItem setToolTip:@"Go to home page"];
257: [toolbarItem setImage:[NSImage imageNamed:@"home"]];
258: [toolbarItem setTarget:self];
259: [toolbarItem setAction:@selector(home:)];
260: } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
261: [toolbarItem setLabel:@"Sidebar"];
262: [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
263: [toolbarItem setToolTip:@"Show or hide the Sidebar"];
264: [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
265: [toolbarItem setTarget:self];
266: [toolbarItem setAction:@selector(toggleSidebar:)];
267: } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
268:
269: NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
270:
271: [toolbarItem setLabel:@"Location"];
272: [toolbarItem setPaletteLabel:@"Location"];
273: [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
274: [toolbarItem setView:mLocationToolbarView];
275: [toolbarItem setMinSize:NSMakeSize(128,32)];
276: [toolbarItem setMaxSize:NSMakeSize(2560,32)];
277:
278: [menuFormRep setTarget:self];
279: [menuFormRep setAction:@selector(performAppropriateLocationAction)];
280: [menuFormRep setTitle:[toolbarItem label]];
281:
282: [toolbarItem setMenuFormRepresentation:menuFormRep];
283: mLocationToolbarItem = toolbarItem;
284:
285: } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
286: [toolbarItem setLabel:@"Print"];
287: [toolbarItem setPaletteLabel:@"Print"];
288: [toolbarItem setToolTip:@"Print this page"];
289: [toolbarItem setImage:[NSImage imageNamed:@"print"]];
290: [toolbarItem setTarget:self];
291: [toolbarItem setAction:@selector(printDocument)];
292: } else {
293: toolbarItem = nil;
294: }
295:
296: return toolbarItem;
297: }
298:
299: // This method handles the enabling/disabling of the toolbar buttons.
300: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
301: {
302: // Check the action and see if it matches.
303: SEL action = [theItem action];
304: if (action == @selector(back:))
305: return [[mBrowserView getBrowserView] canGoBack];
306: else if (action == @selector(forward:))
307: return [[mBrowserView getBrowserView] canGoForward];
308: else if (action == @selector(reload:))
309: return [mBrowserView isBusy] == NO;
310: else if (action == @selector(stop:))
311: return [mBrowserView isBusy];
312: else
313: return YES;
314: }
315:
316: - (void)updateToolbarItems
317: {
318: [[[self window] toolbar] validateVisibleItems];
319: }
320:
321: - (void)performAppropriateLocationAction
322: {
323: if ( [[[self window] toolbar] isVisible] ) {
324: if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
325: ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
326: [self focusURLBar];
327: } else {
328: [self beginLocationSheet];
329: }
330: } else {
331: [self beginLocationSheet];
332: }
333: }
334:
335: - (void)focusURLBar
336: {
337: [mURLBar selectText: self];
338: }
339:
340: - (void)beginLocationSheet
341: {
342: [NSApp beginSheet: mLocationSheetWindow
343: modalForWindow: [self window]
344: modalDelegate: nil
345: didEndSelector: nil
346: contextInfo: nil];
347: }
348:
349: - (IBAction)endLocationSheet:(id)sender
350: {
351: [mLocationSheetWindow orderOut:self];
352: [NSApp endSheet:mLocationSheetWindow returnCode:1];
353: [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
354:
355: // Focus and activate our content area.
356: [[mBrowserView getBrowserView] setActive: YES];
357: }
358:
359: - (IBAction)goToLocationFromToolbarURLField:(id)sender
360: {
361: [self loadURL:[NSURL URLWithString:[sender stringValue]]];
362:
363: // Focus and activate our content area.
364: [[mBrowserView getBrowserView] setActive: YES];
365: }
366:
367: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
368: {
369: [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
370: }
371:
372: - (void)printDocument
373: {
374: [[mBrowserView getBrowserView] printDocument];
375: }
376:
377: - (void)printPreview
378: {
379: [[mBrowserView getBrowserView] printPreview];
380: }
381:
382: - (void)findInPage
383: {
384: [[mBrowserView getBrowserView] findInPage];
385: }
386:
387: - (void)findAgain
388: {
389: [[mBrowserView getBrowserView] findAgain];
390: }
391:
392: - (void)addBookmark
393: {
394: // XXXdwh Hack. Just go to the sidebar for now until we get our
395: // menu data source going.
396: [mSidebarBookmarksDataSource ensureBookmarks];
397: [mSidebarBookmarksDataSource addBookmark: self];
398: }
399:
400: - (IBAction)back:(id)aSender
401: {
402: [[mBrowserView getBrowserView] goBack];
403: }
404:
405: - (IBAction)forward:(id)aSender
406: {
407: [[mBrowserView getBrowserView] goForward];
408: }
409:
410: - (IBAction)reload:(id)aSender
411: {
412: [[mBrowserView getBrowserView] reload: 0];
413: }
414:
415: - (IBAction)stop:(id)aSender
416: {
417: [[mBrowserView getBrowserView] stop: nsIWebNavigation::STOP_ALL];
418: }
419:
420: - (IBAction)home:(id)aSender
421: {
422: [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
423: }
424:
425: - (IBAction)toggleSidebar:(id)aSender
426: {
427: NSResponder* resp = [[self window] firstResponder];
428: [[self window] makeFirstResponder: nil];
429:
430: if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) {
431: // XXXHack to bypass sidebar crashes.
432: [mSidebarDrawer open];
433: } else {
434: [mSidebarDrawer close];
435: }
436:
437: [[self window] makeFirstResponder: resp];
438: }
439:
440: -(void)loadURL:(NSURL*)aURL
441: {
442: if (mInitialized) {
443: [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
444: }
445: else {
446: mURL = aURL;
447: [mURL retain];
448: }
449: }
450:
451: - (void)updateLocationFields:(NSString *)locationString
452: {
453: /* //commenting this out because it doesn't work right yet.
454: if ( [locationString length] > 30 ) {
455: [[mLocationToolbarItem menuFormRepresentation] setTitle:
456: [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
457: } else {
458: [[mLocationToolbarItem menuFormRepresentation] setTitle:
459: [NSString stringWithFormat:@"Location: %@", locationString]];
460: }
461: */
462:
463: [mURLBar setStringValue:locationString];
464: [mLocationSheetURLField setStringValue:locationString];
465:
466: [[self window] update];
467: [[self window] display];
468: }
469:
470: -(void)newTab
471: {
472: NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
473: MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
474: [newView setTab: newTab];
475:
476: [newTab setLabel: @"Untitled"];
477: [newTab setView: newView];
478:
479: [mTabBrowser addTabViewItem: newTab];
480:
481: [[newView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
482:
483: [mTabBrowser selectLastTabViewItem: self];
484:
485: if ( [[[self window] toolbar] isVisible] ) {
486: if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
487: ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
488: [self focusURLBar];
489: }
490: }
491: }
492:
493: -(void)closeTab
494: {
495: if ( [mTabBrowser numberOfTabViewItems] > 1 ) {
496: [[[mTabBrowser selectedTabViewItem] view] windowClosed];
497: [mTabBrowser removeTabViewItem:[mTabBrowser selectedTabViewItem]];
498: }
499: }
500:
501: - (void)previousTab
502: {
503: [mTabBrowser selectPreviousTabViewItem:self];
504: }
505:
506: - (void)nextTab
507: {
508: [mTabBrowser selectNextTabViewItem:self];
509: }
510:
511: - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)aTabViewItem
512: {
513: // Disconnect the old view, if one has been designated.
514: // If the window has just been opened, none has been.
515: if ( mBrowserView ) {
516: [mBrowserView disconnectView];
517: }
518: // Connect up the new view
519: mBrowserView = [aTabViewItem view];
520:
521: // Make the new view the primary content area.
522: [mBrowserView makePrimaryBrowserView: mURLBar status: mStatus
523: progress: mProgress windowController: self];
524: }
525:
526: -(MyBrowserView*)getMyBrowserView
527: {
528: return mBrowserView;
529: }
530:
531: -(void)openNewWindowWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
532: {
533: // Autosave our dimensions before we open a new window. That ensures the size ends up matching.
534: [self autosaveWindowFrame];
535:
536: BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
537: [browser loadURL: aURL];
538: if (aLoadInBG)
539: [[browser window] orderWindow: NSWindowBelow relativeTo: [[self window] windowNumber]];
540: else
541: [browser showWindow: self];
542:
543: // XXXdwh Focus the content area.
544: }
545:
546: -(void)openNewTabWithURL: (NSURL*)aURL loadInBackground: (BOOL)aLoadInBG
547: {
548: NSTabViewItem* newTab = [[[NSTabViewItem alloc] initWithIdentifier: nil] autorelease];
549:
550: NSTabViewItem* selectedTab = [mTabBrowser selectedTabViewItem];
551: int index = [mTabBrowser indexOfTabViewItem: selectedTab];
552: [mTabBrowser insertTabViewItem: newTab atIndex: index+1];
553:
554: MyBrowserView* newView = [[[MyBrowserView alloc] initWithFrame: [mBrowserView frame]] autorelease];
555: [newView setTab: newTab];
556:
557: [newTab setLabel: @"Loading..."];
558: [newTab setView: newView];
559:
560: [[newView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
561:
562: if (!aLoadInBG)
563: [mTabBrowser selectTabViewItem: newTab];
564:
565: // XXXdwh Focus the content area.
566: }
567: @end
568:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>