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