1: /*
2: * BrowserWindowController.mm
3: */
4:
5: #import "BrowserWindowController.h"
6: #import "MyBrowserView.h"
7:
8: static NSString *BrowserToolbarIdentifier = @"Browser Window Toolbar";
9: static NSString *BackToolbarItemIdentifier = @"Back Toolbar Item";
10: static NSString *ForwardToolbarItemIdentifier = @"Forward Toolbar Item";
11: static NSString *ReloadToolbarItemIdentifier = @"Reload Toolbar Item";
12: static NSString *StopToolbarItemIdentifier = @"Stop Toolbar Item";
13: static NSString *HomeToolbarItemIdentifier = @"Home Toolbar Item";
14: static NSString *LocationToolbarItemIdentifier = @"Location Toolbar Item";
15: static NSString *SidebarToolbarItemIdentifier = @"Sidebar Toolbar Item";
16: static NSString *PrintToolbarItemIdentifier = @"Print Toolbar Item";
17:
18: @interface BrowserWindowController(Private)
19: - (void)setupToolbar;
20: @end
21:
22: @implementation BrowserWindowController
23:
24: - (id)init
25: {
26: mInitialized = NO;
27: return [super init];
28: }
29:
30: - (void)windowWillClose:(NSNotification *)notification
31: {
32: [self autorelease];
33: [mSidebarBookmarksDataSource windowClosing];
34: }
35:
36: - (void)dealloc
37: {
38: [mBrowserView windowClosed];
39: [super dealloc];
40: }
41:
42: - (void)windowDidLoad
43: {
44: mInitialized = YES;
45: [self setupToolbar];
46:
47: if (mURL) {
48: [self loadURL: mURL];
49: [mURL release];
50: }
51:
52: [mSidebarDrawer setDelegate: self];
53: }
54:
55: - (void)drawerWillOpen: (NSNotification*)aNotification
56: {
57: [mSidebarBookmarksDataSource ensureBookmarks];
58:
59: [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"http://tinderbox.mozilla.org/SeaMonkey/panel.html"] flags:NSLoadFlagsNone];
60: }
61:
62: - (void)drawerDidOpen:(NSNotification *)aNotification
63: {
64: }
65:
66: - (void)drawerDidClose:(NSNotification *)aNotification
67: {
68: // Unload the Gecko web page in "My Panels" to save memory.
69: [[mSidebarBrowserView getBrowserView] loadURI: [NSURL URLWithString: @"about:blank"] flags:NSLoadFlagsNone];
70: }
71:
72: - (void)setupToolbar
73: {
74: NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:BrowserToolbarIdentifier] autorelease];
75:
76: [toolbar setDisplayMode:NSToolbarDisplayModeDefault];
77: [toolbar setAllowsUserCustomization:YES];
78: [toolbar setAutosavesConfiguration:YES];
79: [toolbar setDelegate:self];
80: [[self window] setToolbar:toolbar];
81: }
82:
83:
84: - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
85: {
86: return [NSArray arrayWithObjects: BackToolbarItemIdentifier,
87: ForwardToolbarItemIdentifier,
88: ReloadToolbarItemIdentifier,
89: StopToolbarItemIdentifier,
90: HomeToolbarItemIdentifier,
91: LocationToolbarItemIdentifier,
92: SidebarToolbarItemIdentifier,
93: PrintToolbarItemIdentifier,
94: NSToolbarCustomizeToolbarItemIdentifier,
95: NSToolbarFlexibleSpaceItemIdentifier,
96: NSToolbarSpaceItemIdentifier,
97: NSToolbarSeparatorItemIdentifier,
98: nil];
99: }
100:
101: - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
102: {
103: return [NSArray arrayWithObjects: BackToolbarItemIdentifier,
104: ForwardToolbarItemIdentifier,
105: ReloadToolbarItemIdentifier,
106: StopToolbarItemIdentifier,
107: HomeToolbarItemIdentifier,
108: LocationToolbarItemIdentifier,
109: SidebarToolbarItemIdentifier,
110: nil];
111: }
112:
113: - (NSToolbarItem *) toolbar:(NSToolbar *)toolbar
114: itemForItemIdentifier:(NSString *)itemIdent
115: willBeInsertedIntoToolbar:(BOOL)willBeInserted
116: {
117: NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdent] autorelease];
118: if ( [itemIdent isEqual:BackToolbarItemIdentifier] ) {
119: [toolbarItem setLabel:@"Back"];
120: [toolbarItem setPaletteLabel:@"Go Back"];
121: [toolbarItem setToolTip:@"Go back one page"];
122: [toolbarItem setImage:[NSImage imageNamed:@"back"]];
123: [toolbarItem setTarget:self];
124: [toolbarItem setAction:@selector(back:)];
125: } else if ( [itemIdent isEqual:ForwardToolbarItemIdentifier] ) {
126: [toolbarItem setLabel:@"Forward"];
127: [toolbarItem setPaletteLabel:@"Go Forward"];
128: [toolbarItem setToolTip:@"Go forward one page"];
129: [toolbarItem setImage:[NSImage imageNamed:@"forward"]];
130: [toolbarItem setTarget:self];
131: [toolbarItem setAction:@selector(forward:)];
132: } else if ( [itemIdent isEqual:ReloadToolbarItemIdentifier] ) {
133: [toolbarItem setLabel:@"Reload"];
134: [toolbarItem setPaletteLabel:@"Reload Page"];
135: [toolbarItem setToolTip:@"Reload current page"];
136: [toolbarItem setImage:[NSImage imageNamed:@"reload"]];
137: [toolbarItem setTarget:self];
138: [toolbarItem setAction:@selector(reload:)];
139: } else if ( [itemIdent isEqual:StopToolbarItemIdentifier] ) {
140: [toolbarItem setLabel:@"Stop"];
141: [toolbarItem setPaletteLabel:@"Stop Loading"];
142: [toolbarItem setToolTip:@"Stop loading this page"];
143: [toolbarItem setImage:[NSImage imageNamed:@"stop"]];
144: [toolbarItem setTarget:self];
145: [toolbarItem setAction:@selector(stop:)];
146: } else if ( [itemIdent isEqual:HomeToolbarItemIdentifier] ) {
147: [toolbarItem setLabel:@"Home"];
148: [toolbarItem setPaletteLabel:@"Go Home"];
149: [toolbarItem setToolTip:@"Go to home page"];
150: [toolbarItem setImage:[NSImage imageNamed:@"home"]];
151: [toolbarItem setTarget:self];
152: [toolbarItem setAction:@selector(home:)];
153: } else if ( [itemIdent isEqual:SidebarToolbarItemIdentifier] ) {
154: [toolbarItem setLabel:@"Sidebar"];
155: [toolbarItem setPaletteLabel:@"Toggle Sidebar"];
156: [toolbarItem setToolTip:@"Show or hide the Sidebar"];
157: [toolbarItem setImage:[NSImage imageNamed:@"sidebar"]];
158: [toolbarItem setTarget:self];
159: [toolbarItem setAction:@selector(toggleSidebar:)];
160: } else if ( [itemIdent isEqual:LocationToolbarItemIdentifier] ) {
161:
162: NSMenuItem *menuFormRep = [[[NSMenuItem alloc] init] autorelease];
163:
164: [toolbarItem setLabel:@"Location"];
165: [toolbarItem setPaletteLabel:@"Location"];
166: [toolbarItem setImage:[NSImage imageNamed:@"Enter a web location."]];
167: [toolbarItem setView:mLocationToolbarView];
168: [toolbarItem setMinSize:NSMakeSize(128,32)];
169: [toolbarItem setMaxSize:NSMakeSize(2560,32)];
170:
171: [menuFormRep setTarget:self];
172: [menuFormRep setAction:@selector(performAppropriateLocationAction)];
173: [menuFormRep setTitle:[toolbarItem label]];
174:
175: [toolbarItem setMenuFormRepresentation:menuFormRep];
176: mLocationToolbarItem = toolbarItem;
177:
178: } else if ( [itemIdent isEqual:PrintToolbarItemIdentifier] ) {
179: [toolbarItem setLabel:@"Print"];
180: [toolbarItem setPaletteLabel:@"Print"];
181: [toolbarItem setToolTip:@"Print this page"];
182: [toolbarItem setImage:[NSImage imageNamed:@"print"]];
183: [toolbarItem setTarget:self];
184: [toolbarItem setAction:@selector(printDocument)];
185: } else {
186: toolbarItem = nil;
187: }
188:
189: return toolbarItem;
190: }
191:
192: // This method handles the enabling/disabling of the toolbar buttons.
193: - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
194: {
195: // Check the action and see if it matches.
196: SEL action = [theItem action];
197: if (action == @selector(back:))
198: return [[mBrowserView getBrowserView] canGoBack];
199: else if (action == @selector(forward:))
200: return [[mBrowserView getBrowserView] canGoForward];
201: else if (action == @selector(reload:))
202: return [mBrowserView isBusy] == NO;
203: else if (action == @selector(stop:))
204: return [mBrowserView isBusy];
205: else
206: return YES;
207: }
208:
209: - (void)updateToolbarItems
210: {
211: [[[self window] toolbar] validateVisibleItems];
212: }
213:
214: - (void)performAppropriateLocationAction
215: {
216: if ( [[[self window] toolbar] isVisible] ) {
217: if ( ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconAndLabel) ||
218: ([[[self window] toolbar] displayMode] == NSToolbarDisplayModeIconOnly) ) {
219: [self focusURLBar];
220: } else {
221: [self beginLocationSheet];
222: }
223: } else {
224: [self beginLocationSheet];
225: }
226: }
227:
228: - (void)focusURLBar
229: {
230: [mURLBar selectText: self];
231: }
232:
233: - (void)beginLocationSheet
234: {
235: [NSApp beginSheet: mLocationSheetWindow
236: modalForWindow: [self window]
237: modalDelegate: nil
238: didEndSelector: nil
239: contextInfo: nil];
240: }
241:
242: - (IBAction)endLocationSheet:(id)sender
243: {
244: [mLocationSheetWindow orderOut:self];
245: [NSApp endSheet:mLocationSheetWindow returnCode:1];
246: [self loadURL:[NSURL URLWithString:[mLocationSheetURLField stringValue]]];
247: }
248:
249: - (IBAction)goToLocationFromToolbarURLField:(id)sender
250: {
251: [self loadURL:[NSURL URLWithString:[sender stringValue]]];
252: }
253:
254: - (void)saveDocument: (NSView*)aFilterView filterList: (NSPopUpButton*)aFilterList
255: {
256: [[mBrowserView getBrowserView] saveDocument: aFilterView filterList: aFilterList];
257: }
258:
259: - (void)printDocument
260: {
261: [[mBrowserView getBrowserView] printDocument];
262: }
263:
264: - (void)printPreview
265: {
266: [[mBrowserView getBrowserView] printPreview];
267: }
268:
269: - (void)findInPage
270: {
271: [[mBrowserView getBrowserView] findInPage];
272: }
273:
274: - (void)findAgain
275: {
276: [[mBrowserView getBrowserView] findAgain];
277: }
278:
279: - (void)addBookmark
280: {
281: // XXXdwh Hack. Just go to the sidebar for now until we get our
282: // menu data source going.
283: [mSidebarBookmarksDataSource ensureBookmarks];
284: [mSidebarBookmarksDataSource addBookmark: self];
285: }
286:
287: - (IBAction)back:(id)aSender
288: {
289: [[mBrowserView getBrowserView] goBack];
290: }
291:
292: - (IBAction)forward:(id)aSender
293: {
294: [[mBrowserView getBrowserView] goForward];
295: }
296:
297: - (IBAction)reload:(id)aSender
298: {
299: [[mBrowserView getBrowserView] reload: 0];
300: }
301:
302: - (IBAction)stop:(id)aSender
303: {
304: [[mBrowserView getBrowserView] stop: 0];
305: }
306:
307: - (IBAction)home:(id)aSender
308: {
309: [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString:@"about:blank"] flags:NSLoadFlagsNone];
310: }
311:
312: - (IBAction)toggleSidebar:(id)aSender
313: {
314: if ( ([mSidebarDrawer state] == NSDrawerClosedState) || ([mSidebarDrawer state] == NSDrawerClosingState) ) {
315: [mSidebarDrawer open];
316: } else {
317: [mSidebarDrawer close];
318: }
319: }
320:
321: -(void)loadURL:(NSURL*)aURL
322: {
323: if (mInitialized)
324: [[mBrowserView getBrowserView] loadURI:aURL flags:NSLoadFlagsNone];
325: else {
326: mURL = aURL;
327: [mURL retain];
328: }
329: }
330:
331: - (void)updateLocationFields:(NSString *)locationString
332: {
333: /* //commenting this out because it doesn't work right yet.
334: if ( [locationString length] > 30 ) {
335: [[mLocationToolbarItem menuFormRepresentation] setTitle:
336: [NSString stringWithFormat:@"Location: %@...", [locationString substringToIndex:31]]];
337: } else {
338: [[mLocationToolbarItem menuFormRepresentation] setTitle:
339: [NSString stringWithFormat:@"Location: %@", locationString]];
340: }
341: */
342:
343: [mURLBar setStringValue:locationString];
344: [mLocationSheetURLField setStringValue:locationString];
345:
346: [[self window] update];
347: [[self window] display];
348: }
349: @end
350:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>