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