-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYPrefsWindowController.m
336 lines (263 loc) · 11.3 KB
/
SSYPrefsWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#import "SSYPrefsWindowController.h"
@interface SSYPrefsWindowController ()
-(void) mapTabsToToolbar ;
@end
@implementation SSYPrefsWindowController
+ (NSSet*)standardToolbarIdentifiers {
return [NSSet setWithObjects:
NSToolbarSeparatorItemIdentifier,
NSToolbarSpaceItemIdentifier,
NSToolbarFlexibleSpaceItemIdentifier,
NSToolbarCustomizeToolbarItemIdentifier,
nil] ;
}
-(id)init {
if ((self = [super initWithWindowNibName:@"PrefsWindow"])) {
itemsList = [[NSMutableDictionary alloc] init];
baseWindowName = [@"" retain];
autosaveName = [@"PrefsWindow" retain];
}
return self;
}
-(void) dealloc {
[itemsList release];
[baseWindowName release];
[autosaveName release];
[super dealloc] ;
}
-(void) awakeFromNib {
NSInteger index = 0;
NSString* wndTitle = nil;
NSString* title = NSLocalizedString(@"Preferences", nil) ;
[[self window] setTitle:title] ;
// Generate a string containing the window's title so we can display the original window title plus the selected pane:
wndTitle = [[ibOutlet_tabView window] title];
if( [wndTitle length] > 0 ) {
[baseWindowName release];
baseWindowName = [[NSString stringWithFormat: @"%@ : ", wndTitle] retain];
}
[[self window] setFrameAutosaveName:@"PrefsWindow"] ;
// Select the preferences page the user last had selected when this window was opened:
NSString* key = [NSString stringWithFormat: @"%@.prefspanel.recentTab", @"PrefsWindow"];
index = [[NSUserDefaults standardUserDefaults] integerForKey:key];
[ibOutlet_tabView selectTabViewItemAtIndex:index];
// Actually hook up our toolbar and the tabs:
[self mapTabsToToolbar];
}
/* -----------------------------------------------------------------------------
mapTabsToToolbar:
Create a toolbar based on our tab control.
Tab title - Name for toolbar item.
Tab identifier - Image file name and toolbar item identifier.
-------------------------------------------------------------------------- */
-(void) mapTabsToToolbar {
// Create a new toolbar instance, and attach it to our document window
NSToolbar *toolbar =[[ibOutlet_tabView window] toolbar];
NSInteger itemCount = 0,
x = 0;
if( toolbar == nil ) // No toolbar yet? Create one!
toolbar = [[[NSToolbar alloc] initWithIdentifier: [NSString stringWithFormat: @"%@.prefspanel.toolbar", autosaveName]] autorelease];
// Set up toolbar properties: Allow customization, give a default display mode, and remember state in user defaults
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
[toolbar setDisplayMode: NSToolbarDisplayModeIconAndLabel];
// Set up item list based on Tab View:
itemCount = [ibOutlet_tabView numberOfTabViewItems];
[itemsList removeAllObjects]; // In case we already had a toolbar.
BOOL didFindFirstRealItem = NO ;
for( x = 0; x < itemCount; x++ ) {
NSTabViewItem* theItem = [ibOutlet_tabView tabViewItemAtIndex:x];
NSString* theIdentifier = [theItem identifier];
NSString* theLabel = [self localizeString:[theItem label]] ;
[itemsList setObject:theLabel forKey:theIdentifier];
// Select the first tab view item which is a "real" item
if (!didFindFirstRealItem) {
if (YES
// Flexible Space and Separator are not "real" items
&& ![theIdentifier isEqualToString:NSToolbarFlexibleSpaceItemIdentifier]
&& ![theIdentifier isEqualToString:NSToolbarSeparatorItemIdentifier]
) {
[ibOutlet_tabView selectTabViewItem:theItem] ;
didFindFirstRealItem = YES ;
}
}
}
// We are the delegate
[toolbar setDelegate: self];
// Attach the toolbar to the document window
[[ibOutlet_tabView window] setToolbar: toolbar];
// Set up window title:
NSString* label = [[ibOutlet_tabView selectedTabViewItem] label] ;
if (label) {
[[ibOutlet_tabView window] setTitle: [baseWindowName stringByAppendingString: [self localizeString:label]]];
}
NSString* identifier = [[ibOutlet_tabView selectedTabViewItem] identifier] ;
if( [toolbar respondsToSelector: @selector(setSelectedItemIdentifier:)] ) {
[toolbar setSelectedItemIdentifier: identifier];
}
}
/* -----------------------------------------------------------------------------
orderFrontPrefsPanel:
IBAction to assign to "Preferences..." menu item.
-------------------------------------------------------------------------- */
-(IBAction) orderFrontPrefsPanel: (id)sender
{
[[ibOutlet_tabView window] makeKeyAndOrderFront:sender];
}
/* -----------------------------------------------------------------------------
setAutosaveName:
Name used for saving state of prefs window.
-------------------------------------------------------------------------- */
-(void) setAutosaveName: (NSString*)name
{
[name retain];
[autosaveName release];
autosaveName = name;
}
-(NSString*) autosaveName
{
return autosaveName;
}
/* -----------------------------------------------------------------------------
toolTipForLabel:
Subclasses should over-ride this to provide tooltips for each toobar item
-------------------------------------------------------------------------- */
-(NSString*)toolTipForIdentifier:(NSString*)identifier
{
return nil ;
}
/* -----------------------------------------------------------------------------
NSToolbar Delegate method:
toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:
Create an item with the proper image and name based on our list
of tabs for the specified identifier.
-------------------------------------------------------------------------- */
-(NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted
{
// Required delegate method: Given an item identifier, this method returns an item
// The toolbar will use this method to obtain toolbar items that can be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
NSString* itemLabel = [itemsList objectForKey:itemIdent] ;
/* if([[SSYPrefsWindowController standardToolbarIdentifiers] member:itemIdent] != nil) {
// This is a standard toolbar item which Cocoa will configure
}
else */
if([[SSYPrefsWindowController standardToolbarIdentifiers] member:itemIdent] != nil) {
// This is a standard toolbar item which Cocoa will configure
}
else if (itemLabel != nil) {
// Set the text label to be displayed in the toolbar and customization palette
[toolbarItem setLabel: itemLabel];
[toolbarItem setPaletteLabel: itemLabel];
[toolbarItem setTag:[ibOutlet_tabView indexOfTabViewItemWithIdentifier:itemIdent]];
// 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
[toolbarItem setToolTip: [self toolTipForIdentifier:itemIdent]];
NSImage* image = [self specialImageForTabViewIdentifier:itemIdent];
if (!image) {
image = [NSImage imageNamed:itemIdent];
}
[toolbarItem setImage:image];
// Tell the item what message to send when it is clicked
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(changePanes:)]; }
else
{
// itemIdent refered to a toolbar item that is not provide or supported by us or cocoa
// Returning nil will inform the toolbar this kind of item is not supported
toolbarItem = nil;
}
return toolbarItem;
}
/* -----------------------------------------------------------------------------
toolbarSelectableItemIdentifiers:
Make sure all our custom items can be selected. NSToolbar will
automagically select the appropriate item when it is clicked.
-------------------------------------------------------------------------- */
-(NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar
{
return [itemsList allKeys];
}
/* -----------------------------------------------------------------------------
changePanes:
Action for our custom toolbar items that causes the window title to
reflect the current pane and the proper pane to be shown in response to
a click.
-------------------------------------------------------------------------- */
- (void)selectTabViewItemWithTag:(NSInteger)tag
label:(NSString*)label {
[ibOutlet_tabView selectTabViewItemAtIndex:tag] ;
[[ibOutlet_tabView window] setTitle: [baseWindowName stringByAppendingString:label]];
NSString* key = [NSString stringWithFormat: @"%@.prefspanel.recentpage", autosaveName];
[[NSUserDefaults standardUserDefaults] setInteger:tag forKey:key];
}
- (IBAction) changePanes: (id)sender
{
[self selectTabViewItemWithTag:[sender tag]
label:[sender label]] ;
}
/* -----------------------------------------------------------------------------
toolbarDefaultItemIdentifiers:
Return the identifiers for all toolbar items that will be shown by
default.
This is simply a list of all tab view items in order.
-------------------------------------------------------------------------- */
-(NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar
{
NSInteger itemCount = [ibOutlet_tabView numberOfTabViewItems],
x;
//NSTabViewItem* theItem = [ibOutlet_tabView tabViewItemAtIndex:0];
//NSMutableArray* defaultItems = [NSMutableArray arrayWithObjects: [theItem identifier], NSToolbarSeparatorItemIdentifier, nil];
NSMutableArray* defaultItems = [NSMutableArray array];
for( x = 0; x < itemCount; x++ )
{
NSTabViewItem* theItem = [ibOutlet_tabView tabViewItemAtIndex:x];
[defaultItems addObject: [theItem identifier]];
}
return defaultItems;
}
- (BOOL)revealTabViewIdentifier:(NSString*)identifier {
BOOL didDo = NO ;
NSInteger i = 0 ;
for (NSTabViewItem* item in [ibOutlet_tabView tabViewItems]) {
// item i=0 is the flexible space item
if ([[item identifier] isEqualToString:identifier]) {
NSToolbar *toolbar =[[ibOutlet_tabView window] toolbar] ;
[toolbar setSelectedItemIdentifier:identifier] ;
NSToolbarItem* toolbarItem = nil ;
if (i < [[toolbar items] count]) {
toolbarItem = [[toolbar items] objectAtIndex:i] ;
}
NSString* label = [toolbarItem label] ;
if (label) {
[self selectTabViewItemWithTag:i
label:label] ;
didDo = YES ;
}
else {
NSLog(@"Internal Error 624-0202 %@", [item identifier]) ;
}
break ;
}
i++ ;
}
return didDo ;
}
/* -----------------------------------------------------------------------------
toolbarAllowedItemIdentifiers:
Return the identifiers for all toolbar items that *can* be put in this
toolbar. We allow a couple more items (flexible space, separator lines
etc.) in addition to our custom items.
-------------------------------------------------------------------------- */
-(NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar
{
NSMutableArray* allowedItems = [[[itemsList allKeys] mutableCopy] autorelease];
[allowedItems addObjectsFromArray:[[SSYPrefsWindowController standardToolbarIdentifiers] allObjects]];
return allowedItems;
}
- (NSString*)localizeString:(NSString*)key {
return key ;
}
- (NSImage*)specialImageForTabViewIdentifier:(NSString*)identifier {
return nil;
}
@end