1//
2//  PXWelcomeController.m
3//  Pixen-XCode
4//
5//  Created by Andy Matuschak on Sat Jun 12 2004.
6//  Copyright (c) 2004 Open Sword Group. All rights reserved.
7//
8
9#import "PXWelcomeController.h"
10
11@implementation PXWelcomeController
12
13/* -----------------------------------------------------------------------------
14	Constructor:
15   -------------------------------------------------------------------------- */
16
17-(id) init
18{
19	if( self = [super initWithWindowNibName:@"PXDiscoverPixen"] )
20	{
21		tabView = nil;
22		itemsList = [[NSMutableDictionary alloc] init];
23		baseWindowName = [@"Welcome" retain];
24		autosaveName = [@"org.opensword" retain];
25	}
26
27	return self;
28}
29
30
31/* -----------------------------------------------------------------------------
32	Destructor:
33   -------------------------------------------------------------------------- */
34
35-(void)	dealloc
36{
37	[itemsList release];
38	[baseWindowName release];
39	[autosaveName release];
40	[super dealloc];
41}
42
43
44/* -----------------------------------------------------------------------------
45	awakeFromNib:
46		This object and all others in the NIB have been created and hooked up.
47		Fetch the window name so we can modify it to indicate the current
48		page, and add our toolbar to the window.
49
50		This method is the great obstacle to making UKPrefsPanel an NSTabView
51		subclass. When the tab view's awakeFromNib method is called, the
52		individual tabs aren't set up yet, meaning mapTabsToToolbar gives us an
53		empty toolbar. ... bummer.
54
55		If anybody knows how to fix this, you're welcome to tell me.
56   -------------------------------------------------------------------------- */
57
58-(void)	awakeFromNib
59{
60	NSString*		key;
61
62	[prev setEnabled:NO];
63
64	// Generate a string containing the window's title so we can display the original window title plus the selected pane:
65	/*wndTitle = [[tabView window] title];
66	if( [wndTitle length] > 0 )
67	{
68		[baseWindowName release];
69		baseWindowName = [[NSString stringWithFormat: @"%@ : ", wndTitle] retain];
70	}*/
71
72	// Make sure our autosave-name is based on the one of our prefs window:
73	[self setAutosaveName: [[tabView window] frameAutosaveName]];
74
75	// Select the preferences page the user last had selected when this window was opened:
76	key = [NSString stringWithFormat: @"%@.prefspanel.recentpage", autosaveName];
77	[tabView selectTabViewItemAtIndex: 0];
78	[tabView setTabViewType:NSNoTabsNoBorder];
79
80	// Actually hook up our toolbar and the tabs:
81	//[self mapTabsToToolbar];
82
83	/*id enumerator = [[[[tabView tabViewItemAtIndex:[tabView indexOfTabViewItem:[tabView selectedTabViewItem]]] view] subviews] objectEnumerator], current;
84	while (current = [enumerator nextObject])
85	{
86		if ([current isKindOfClass:[NSBox class]])
87		{
88			[[self window] setContentSize:NSMakeSize([[[self window] contentView] frame].size.width, [current frame].size.height + 42)];
89			[current setFrameOrigin:NSMakePoint([current frame].origin.x, 42)];
90			[tabView setFrameOrigin:NSMakePoint([current frame].origin.x, 0)];
91			[image setFrameOrigin:NSMakePoint([current frame].origin.x - 16, [[[self window] contentView] frame].size.height - [image frame].size.height)];
92		}
93	}*/
94}
95
96
97/* -----------------------------------------------------------------------------
98	mapTabsToToolbar:
99		Create a toolbar based on our tab control.
100
101		Tab title		-   Name for toolbar item.
102		Tab identifier  -	Image file name and toolbar item identifier.
103   -------------------------------------------------------------------------- */
104
105-(void) mapTabsToToolbar
106{
107    // Create a new toolbar instance, and attach it to our document window
108    NSToolbar		*toolbar =[[tabView window] toolbar];
109	int				itemCount = 0,
110					x = 0;
111	NSTabViewItem	*currPage = nil;
112
113	if( toolbar == nil )   // No toolbar yet? Create one!
114		toolbar = [[[NSToolbar alloc] initWithIdentifier: [NSString stringWithFormat: @"%@.prefspanel.toolbar", autosaveName]] autorelease];
115
116    // Set up toolbar properties: Allow customization, give a default display mode, and remember state in user defaults
117    [toolbar setAllowsUserCustomization: YES];
118    [toolbar setAutosavesConfiguration: YES];
119    //[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
120
121	// Set up item list based on Tab View:
122	itemCount = [tabView numberOfTabViewItems];
123
124	[itemsList removeAllObjects];	// In case we already had a toolbar.
125
126	for( x = 0; x < itemCount; x++ )
127	{
128		NSTabViewItem*		theItem = [tabView tabViewItemAtIndex:x];
129		NSString*			theIdentifier = [theItem identifier];
130		NSString*			theLabel = [theItem label];
131
132		[itemsList setObject:theLabel forKey:theIdentifier];
133	}
134
135    // We are the delegate
136    [toolbar setDelegate: self];
137
138    // Attach the toolbar to the document window
139    [[tabView window] setToolbar: toolbar];
140
141	// Set up window title:
142	currPage = [tabView selectedTabViewItem];
143	if( currPage == nil )
144		currPage = [tabView tabViewItemAtIndex:0];
145	[[tabView window] setTitle: [baseWindowName stringByAppendingString: [currPage label]]];
146
147	#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
148	if( [toolbar respondsToSelector: @selector(setSelectedItemIdentifier:)] )
149		[toolbar setSelectedItemIdentifier: [currPage identifier]];
150	#endif
151}
152
153
154/* -----------------------------------------------------------------------------
155	orderFrontPrefsPanel:
156		IBAction to assign to "Preferences..." menu item.
157   -------------------------------------------------------------------------- */
158
159-(IBAction)		orderFrontPrefsPanel: (id)sender
160{
161	[[tabView window] makeKeyAndOrderFront:sender];
162}
163
164
165/* -----------------------------------------------------------------------------
166	setTabView:
167		Accessor for specifying the tab view to query.
168   -------------------------------------------------------------------------- */
169
170-(void)			setTabView: (NSTabView*)tv
171{
172	tabView = tv;
173}
174
175
176-(NSTabView*)   tabView
177{
178	return tabView;
179}
180
181
182/* -----------------------------------------------------------------------------
183	setAutosaveName:
184		Name used for saving state of prefs window.
185   -------------------------------------------------------------------------- */
186
187-(void)			setAutosaveName: (NSString*)name
188{
189	[name retain];
190	[autosaveName release];
191	autosaveName = name;
192}
193
194
195-(NSString*)	autosaveName
196{
197	return autosaveName;
198}
199
200
201/* -----------------------------------------------------------------------------
202	toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:
203		Create an item with the proper image and name based on our list
204		of tabs for the specified identifier.
205   -------------------------------------------------------------------------- */
206
207-(NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted
208{
209    // Required delegate method:  Given an item identifier, this method returns an item
210    // The toolbar will use this method to obtain toolbar items that can be displayed in the customization sheet, or in the toolbar itself
211    NSToolbarItem   *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
212    NSString*		itemLabel;
213
214    if( (itemLabel = [itemsList objectForKey:itemIdent]) != nil )
215	{
216		// Set the text label to be displayed in the toolbar and customization palette
217		[toolbarItem setLabel: itemLabel];
218		[toolbarItem setPaletteLabel: itemLabel];
219		[toolbarItem setTag:[tabView indexOfTabViewItemWithIdentifier:itemIdent]];
220
221		// Set up a reasonable tooltip, and image   Note, these aren't localized, but you will likely want to localize many of the item's properties
222		[toolbarItem setToolTip: itemLabel];
223		[toolbarItem setImage: [NSImage imageNamed:itemIdent]];
224
225		// Tell the item what message to send when it is clicked
226		[toolbarItem setTarget: self];
227		[toolbarItem setAction: @selector(changePanes:)];
228    }
229	else
230	{
231		// itemIdent refered to a toolbar item that is not provide or supported by us or cocoa
232		// Returning nil will inform the toolbar this kind of item is not supported
233		toolbarItem = nil;
234    }
235
236    return toolbarItem;
237}
238
239
240/* -----------------------------------------------------------------------------
241	toolbarSelectableItemIdentifiers:
242		Make sure all our custom items can be selected. NSToolbar will
243		automagically select the appropriate item when it is clicked.
244   -------------------------------------------------------------------------- */
245
246#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
247-(NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar
248{
249	return [itemsList allKeys];
250}
251#endif
252
253
254/* -----------------------------------------------------------------------------
255	changePanes:
256		Action for our custom toolbar items that causes the window title to
257		reflect the current pane and the proper pane to be shown in response to
258		a click.
259   -------------------------------------------------------------------------- */
260
261-(IBAction)	changePanes: (id)sender
262{
263	[[tabView window] setTitle: [baseWindowName stringByAppendingString: [sender label]]];
264
265	//key = [NSString stringWithFormat: @"%@.prefspanel.recentpage", autosaveName];
266	//[[NSUserDefaults standardUserDefaults] setInteger:[sender tag] forKey:key];
267
268	[tabView selectTabViewItemAtIndex: [sender tag]];
269}
270
271
272/* -----------------------------------------------------------------------------
273	toolbarDefaultItemIdentifiers:
274		Return the identifiers for all toolbar items that will be shown by
275		default.
276		This is simply a list of all tab view items in order.
277   -------------------------------------------------------------------------- */
278
279-(NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
280{
281	int					itemCount = [tabView numberOfTabViewItems],
282						x;
283	NSTabViewItem*		theItem = [tabView tabViewItemAtIndex:0];
284	//NSMutableArray*	defaultItems = [NSMutableArray arrayWithObjects: [theItem identifier], NSToolbarSeparatorItemIdentifier, nil];
285	NSMutableArray*	defaultItems = [NSMutableArray array];
286
287	for( x = 0; x < itemCount; x++ )
288	{
289		theItem = [tabView tabViewItemAtIndex:x];
290
291		[defaultItems addObject: [theItem identifier]];
292	}
293
294	return defaultItems;
295}
296
297
298/* -----------------------------------------------------------------------------
299	toolbarAllowedItemIdentifiers:
300		Return the identifiers for all toolbar items that *can* be put in this
301		toolbar. We allow a couple more items (flexible space, separator lines
302		etc.) in addition to our custom items.
303   -------------------------------------------------------------------------- */
304
305-(NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
306{
307    NSMutableArray*		allowedItems = [[itemsList allKeys] mutableCopy];
308
309	[allowedItems addObjectsFromArray: [NSArray arrayWithObjects: NSToolbarSeparatorItemIdentifier,
310				NSToolbarSpaceItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier,
311				NSToolbarCustomizeToolbarItemIdentifier, nil] ];
312
313	return allowedItems;
314}
315
316- (IBAction)next:sender
317{
318	if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 0)
319	{
320		[prev setEnabled:YES];
321	}
322
323	if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 6)
324	{
325		[next setTitle:NSLocalizedString(@"Close", @"Close")];
326	}
327
328	if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 7)
329	{
330		[self close];
331	}
332	else
333	{
334		[tabView selectNextTabViewItem:sender];
335	}
336}
337
338- (IBAction)prev:sender
339{
340	if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 1)
341	{
342		[prev setEnabled:NO];
343	}
344
345	if ([tabView indexOfTabViewItem:[tabView selectedTabViewItem]] == 7)
346	{
347		[next setTitle:@"Next"];
348	}
349	[tabView selectPreviousTabViewItem:sender];
350}
351
352@end
353