Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 348 lines (288 sloc) 11.856 kB
dc5be1c Initial check-in.
uli authored
1 //
2 // UKToolbarFactory.m
3 // UKToolbarFactory
4 //
5 // Created by Uli Kusterer on Sat Jan 17 2004.
720b0e7 @uliwitness Added licensing information to the source files.
authored
6 // Copyright (c) 2004 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
dc5be1c Initial check-in.
uli authored
26 //
27
28 // -----------------------------------------------------------------------------
29 // Headers:
30 // -----------------------------------------------------------------------------
31
32 #import "UKToolbarFactory.h"
33 #import "UKFirstResponder.h"
34
35
36 @interface UKToolbarFactory (UKToolbarFactoryPrivateMethods)
37
38 -(void) setupToolbar: (id)sender;
39 -(void) itemClicked: (NSToolbarItem*)sender;
40 //-(BOOL) tryToPerform: (SEL)itemAction with: (id)sender onObject: (NSResponder*)resp;
41
42 @end
43
44
45 @implementation UKToolbarFactory
46
47 // -----------------------------------------------------------------------------
48 // * DESTRUCTOR:
49 // Get rid of the objects we created.
50 //
51 // REVISIONS:
52 // 2004-01-19 witness Documented.
53 // -----------------------------------------------------------------------------
54
55 -(void) dealloc
56 {
57 [toolbarItems release];
58 [toolbarIdentifier release];
59 [super dealloc];
60 }
61
62
63 // -----------------------------------------------------------------------------
64 // awakeFromNib:
65 // We were created from a NIB file. Add the toolbar to our window.
66 //
67 // REVISIONS:
68 // 2004-01-19 witness Documented.
69 // -----------------------------------------------------------------------------
70
71 -(void) awakeFromNib
72 {
73 [self setupToolbar: self]; // Create toolbar.
74 }
75
76
77 // -----------------------------------------------------------------------------
78 // setToolbarIdentifier:
79 // Lets you change the toolbar identifier at runtime. This will recreate
80 // the toolbar from the item definition .plist file for that identifier.
81 //
82 // REVISIONS:
83 // 2004-01-19 witness Documented.
84 // -----------------------------------------------------------------------------
85
86 -(void) setToolbarIdentifier: (NSString*)str
87 {
88 [str retain];
89 [toolbarIdentifier release];
90 toolbarIdentifier = str;
91
92 [self setupToolbar: nil]; // Recreate toolbar.
93 }
94
95
96 // -----------------------------------------------------------------------------
97 // toolbarIdentifier:
98 // Returns the toolbar identifier this object manages. Defaults to the
99 // application's bundle identifier with the autosave name of the owning
100 // window appended to it.
101 //
102 // REVISIONS:
103 // 2004-01-19 witness Documented.
104 // -----------------------------------------------------------------------------
105
106 -(NSString*) toolbarIdentifier
107 {
108 if( !toolbarIdentifier )
109 toolbarIdentifier = [[NSString stringWithFormat: @"%@.%@", [[NSBundle mainBundle] bundleIdentifier], [owner frameAutosaveName]] retain];
110
111 return toolbarIdentifier;
112 }
113
114
115 // -----------------------------------------------------------------------------
116 // toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:
117 // Creates the appropriate toolbar item for the specified identifier.
118 // This simply lets NSToolbarItem handle system-defined toolbar items,
119 // while setting up all others according to the dictionaries from the
120 // .plist file.
121 //
122 // REVISIONS:
123 // 2004-01-19 witness Documented.
124 // -----------------------------------------------------------------------------
125
126 -(NSToolbarItem*) toolbar: (NSToolbar*)toolbar itemForItemIdentifier: (NSString*)itemIdentifier
127 willBeInsertedIntoToolbar: (BOOL)flag;
128 {
129 NSDictionary* allItems = [toolbarItems objectForKey: @"Items"];
130 NSDictionary* currItem;
131 NSToolbarItem* tbi = nil;
132
133 // One of the system-provided items?
134 if( [itemIdentifier isEqualToString: NSToolbarSeparatorItemIdentifier]
135 || [itemIdentifier isEqualToString: NSToolbarSpaceItemIdentifier]
136 || [itemIdentifier isEqualToString: NSToolbarFlexibleSpaceItemIdentifier]
137 || [itemIdentifier isEqualToString: NSToolbarShowColorsItemIdentifier]
138 || [itemIdentifier isEqualToString: NSToolbarShowFontsItemIdentifier]
139 || [itemIdentifier isEqualToString: NSToolbarPrintItemIdentifier]
140 || [itemIdentifier isEqualToString: NSToolbarCustomizeToolbarItemIdentifier] )
141 return [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];
142
143 // Otherwise, look it up in our list of custom items:
144 currItem = [allItems objectForKey: itemIdentifier];
145 if( currItem )
146 {
147 NSString* theActionStr = [currItem objectForKey: @"Action"];
148 NSString* viewClassStr = [currItem objectForKey: @"ViewClass"];
149 SEL itemAction = @selector(itemClicked:);
150 if( theActionStr && (viewClassStr || ![self isSelectable]) )
151 itemAction = NSSelectorFromString(theActionStr);
152 NSNumber* maxw = [currItem objectForKey: @"MaxWidth"];
153 NSNumber* minw = [currItem objectForKey: @"MinWidth"];
154 NSString* itemLabel = [currItem objectForKey: @"Label"];
155 NSString* itemCustomLabel = [currItem objectForKey: @"CustomizationLabel"];
156 NSString* itemTooltip = [currItem objectForKey: @"ToolTip"];
157 Class viewClass = nil;
158 if( viewClassStr )
159 viewClass = NSClassFromString(viewClassStr);
160 NSImage* itemImage = [NSImage imageNamed: itemIdentifier];
161
162 // ... and create an NSToolbarItem for it and set that up:
163 tbi = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];
164 [tbi setAction: itemAction];
165 if( !viewClassStr && [self isSelectable] )
166 [tbi setTarget: self];
167 if( viewClassStr ) // This isn't a regular item? It's a view?
168 {
169 NSView* theView = (NSView*) [[[viewClass alloc] init] autorelease];
170 [tbi setView: theView];
171 if( theActionStr && [theView respondsToSelector: @selector(setAction:)] )
172 [(id)theView setAction: itemAction];
173 if( itemLabel && [theView respondsToSelector: @selector(cell)] )
174 {
175 NSCell* theCell = [(id)theView cell];
176 if( [theCell respondsToSelector: @selector(setPlaceholderString:)] )
177 [(id)theCell setPlaceholderString: itemLabel];
178 }
179 }
180 if( minw )
181 [tbi setMinSize: NSMakeSize([minw floatValue], 32)];
182 if( maxw )
183 [tbi setMaxSize: NSMakeSize([maxw floatValue], 32)];
184 [tbi setLabel: itemLabel];
185 [tbi setImage: itemImage];
186 if( itemCustomLabel )
187 [tbi setPaletteLabel: itemCustomLabel];
188 else
189 [tbi setPaletteLabel: itemLabel];
190 if( itemTooltip )
191 [tbi setToolTip: itemTooltip];
192 }
193
194 return tbi;
195 }
196
197
198 // -----------------------------------------------------------------------------
199 // toolbarDefaultItemIdentifiers:
200 // Returns the list of item identifiers we want to be in this toolbar by
201 // default. The list is loaded from the .plist file's "DefaultItems" array.
202 //
203 // REVISIONS:
204 // 2004-01-19 witness Documented.
205 // -----------------------------------------------------------------------------
206
207 -(NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar*)toolbar
208 {
209 return [toolbarItems objectForKey: @"DefaultItems"];
210 }
211
212
213 // -----------------------------------------------------------------------------
214 // toolbarAllowedItemIdentifiers:
215 // Returns the list of item identifiers that may be in the toolbar. This
216 // simply returns the identifiers of all the items in our "Items"
217 // dictionary, plus a few sensible defaults like separators and spacer
218 // items the user may want to add as well.
219 //
220 // If this function doesn't return the item identifier, it *can't* be in
221 // the toolbar. Though if this returns it, that doesn't mean it currently
222 // is in the toolbar.
223 //
224 // REVISIONS:
225 // 2004-01-19 witness Documented.
226 // -----------------------------------------------------------------------------
227
228 -(NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar*)toolbar
229 {
230 NSDictionary* allItems = [toolbarItems objectForKey: @"Items"];
231 int icount = [allItems count];
232 NSMutableArray* allowedItems = [NSMutableArray arrayWithCapacity: icount +4];
233 NSEnumerator* allItemItty;
234 NSString* currItem;
235
236 for( allItemItty = [allItems keyEnumerator]; currItem = [allItemItty nextObject]; )
237 [allowedItems addObject: currItem];
238
239 [allowedItems addObject: NSToolbarSeparatorItemIdentifier];
240 [allowedItems addObject: NSToolbarSpaceItemIdentifier];
241 [allowedItems addObject: NSToolbarFlexibleSpaceItemIdentifier];
242 [allowedItems addObject: NSToolbarCustomizeToolbarItemIdentifier];
243
244 return allowedItems;
245 }
246
247
248 -(NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar
249 {
250 if( [self isSelectable] )
251 {
252 NSDictionary* allItems = [toolbarItems objectForKey: @"Items"];
253 int icount = [allItems count];
254 NSMutableArray* allowedItems = [NSMutableArray arrayWithCapacity: icount];
255 NSEnumerator* allItemItty;
256 NSString* currItem;
257
258 for( allItemItty = [allItems keyEnumerator]; currItem = [allItemItty nextObject]; )
259 {
260 // View items aren't selectable, but make all others selectable:
261 if( [[allItems objectForKey: currItem] objectForKey: @"ViewClass"] == nil )
262 [allowedItems addObject: currItem];
263 }
264
265 return allowedItems;
266 }
267 else
268 return [NSArray array];
269 }
270
271
272 -(BOOL) isSelectable
273 {
274 NSNumber* n = [[toolbarItems objectForKey: @"Options"] objectForKey: @"Selectable"];
275 return n && [n boolValue];
276 }
277
278
279 -(NSString*) selectedItemIdentifier
280 {
281 return [[owner toolbar] selectedItemIdentifier];
282 }
283
284 -(void) setSelectedItemIdentifier: (NSString*)str
285 {
286 [[owner toolbar] setSelectedItemIdentifier: str];
287 }
288
289 @end
290
291 @implementation UKToolbarFactory (UKToolbarFactoryPrivateMethods)
292
293 // -----------------------------------------------------------------------------
294 // setupToolbar:
295 // (Re)create our toolbar. This loads the .plist file whose name is the
296 // toolbar identifier and loads it. Then it adds the toolbar to our
297 // window.
298 //
299 // REVISIONS:
300 // 2004-01-19 witness Documented.
301 // -----------------------------------------------------------------------------
302
303 -(void) setupToolbar: (id)sender
304 {
305 // Load list of items:
306 NSString* toolbarItemsPlistPath = [[NSBundle mainBundle] pathForResource: [self toolbarIdentifier] ofType: @"plist"];
307 if( toolbarItems )
308 [toolbarItems release];
309 toolbarItems = [[NSDictionary dictionaryWithContentsOfFile: toolbarItemsPlistPath] retain];
310
311 // (Re-) create toolbar:
312 NSToolbar* tb = [[[NSToolbar alloc] initWithIdentifier: [self toolbarIdentifier]] autorelease];
313 [tb setDelegate: self];
314 [tb setAllowsUserCustomization: YES];
315 [tb setAutosavesConfiguration: YES];
316 [owner setToolbar: tb];
317
318 if( [self isSelectable] )
319 [self setSelectedItemIdentifier: [[toolbarItems objectForKey: @"DefaultItems"] objectAtIndex: 0]];
320 }
321
322
323 // -----------------------------------------------------------------------------
324 // itemClicked:
325 // Toolbar action if we have selectable items. This selects the item, then
326 // sends the actual action that the item has.
327 //
328 // REVISIONS:
329 // 2004-10-03 witness Documented.
330 // -----------------------------------------------------------------------------
331
332 -(void) itemClicked: (NSToolbarItem*)sender
333 {
334 [self setSelectedItemIdentifier: [sender itemIdentifier]];
335
336 NSDictionary* dict = [[toolbarItems objectForKey: @"Items"] objectForKey: [sender itemIdentifier]];
337 NSString* theActionStr = [dict objectForKey: @"Action"];
338 if( theActionStr )
339 {
340 SEL itemAction = NSSelectorFromString(theActionStr);
341 [[UKFirstResponder firstResponder] performSelector: itemAction withObject: sender];
342 }
343 }
344
345
346 @end
347
Something went wrong with that request. Please try again.