5 // Created by Uli Kusterer on Sat Jan 17 2004.
6 // Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
9 // -----------------------------------------------------------------------------
11 // -----------------------------------------------------------------------------
13 #import "UKToolbarFactory.h"
14 #import "UKFirstResponder.h"
17 @interface UKToolbarFactory (UKToolbarFactoryPrivateMethods)
19 -(void) setupToolbar: (id)sender;
20 -(void) itemClicked: (NSToolbarItem*)sender;
21 //-(BOOL) tryToPerform: (SEL)itemAction with: (id)sender onObject: (NSResponder*)resp;
26 @implementation UKToolbarFactory
28 // -----------------------------------------------------------------------------
30 // Get rid of the objects we created.
33 // 2004-01-19 witness Documented.
34 // -----------------------------------------------------------------------------
38 [toolbarItems release];
39 [toolbarIdentifier release];
44 // -----------------------------------------------------------------------------
46 // We were created from a NIB file. Add the toolbar to our window.
49 // 2004-01-19 witness Documented.
50 // -----------------------------------------------------------------------------
54 [self setupToolbar: self]; // Create toolbar.
58 // -----------------------------------------------------------------------------
59 // setToolbarIdentifier:
60 // Lets you change the toolbar identifier at runtime. This will recreate
61 // the toolbar from the item definition .plist file for that identifier.
64 // 2004-01-19 witness Documented.
65 // -----------------------------------------------------------------------------
67 -(void) setToolbarIdentifier: (NSString*)str
70 [toolbarIdentifier release];
71 toolbarIdentifier = str;
73 [self setupToolbar: nil]; // Recreate toolbar.
77 // -----------------------------------------------------------------------------
79 // Returns the toolbar identifier this object manages. Defaults to the
80 // application's bundle identifier with the autosave name of the owning
81 // window appended to it.
84 // 2004-01-19 witness Documented.
85 // -----------------------------------------------------------------------------
87 -(NSString*) toolbarIdentifier
89 if( !toolbarIdentifier )
90 toolbarIdentifier = [[NSString stringWithFormat: @"%@.%@", [[NSBundle mainBundle] bundleIdentifier], [owner frameAutosaveName]] retain];
92 return toolbarIdentifier;
96 // -----------------------------------------------------------------------------
97 // toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:
98 // Creates the appropriate toolbar item for the specified identifier.
99 // This simply lets NSToolbarItem handle system-defined toolbar items,
100 // while setting up all others according to the dictionaries from the
104 // 2004-01-19 witness Documented.
105 // -----------------------------------------------------------------------------
107 -(NSToolbarItem*) toolbar: (NSToolbar*)toolbar itemForItemIdentifier: (NSString*)itemIdentifier
108 willBeInsertedIntoToolbar: (BOOL)flag;
110 NSDictionary* allItems = [toolbarItems objectForKey: @"Items"];
111 NSDictionary* currItem;
112 NSToolbarItem* tbi = nil;
114 // One of the system-provided items?
115 if( [itemIdentifier isEqualToString: NSToolbarSeparatorItemIdentifier]
116 || [itemIdentifier isEqualToString: NSToolbarSpaceItemIdentifier]
117 || [itemIdentifier isEqualToString: NSToolbarFlexibleSpaceItemIdentifier]
118 || [itemIdentifier isEqualToString: NSToolbarShowColorsItemIdentifier]
119 || [itemIdentifier isEqualToString: NSToolbarShowFontsItemIdentifier]
120 || [itemIdentifier isEqualToString: NSToolbarPrintItemIdentifier]
121 || [itemIdentifier isEqualToString: NSToolbarCustomizeToolbarItemIdentifier] )
122 return [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];
124 // Otherwise, look it up in our list of custom items:
125 currItem = [allItems objectForKey: itemIdentifier];
128 NSString* theActionStr = [currItem objectForKey: @"Action"];
129 NSString* viewClassStr = [currItem objectForKey: @"ViewClass"];
130 SEL itemAction = @selector(itemClicked:);
131 if( theActionStr && (viewClassStr || ![self isSelectable]) )
132 itemAction = NSSelectorFromString(theActionStr);
133 NSNumber* maxw = [currItem objectForKey: @"MaxWidth"];
134 NSNumber* minw = [currItem objectForKey: @"MinWidth"];
135 NSString* itemLabel = [currItem objectForKey: @"Label"];
136 NSString* itemCustomLabel = [currItem objectForKey: @"CustomizationLabel"];
137 NSString* itemTooltip = [currItem objectForKey: @"ToolTip"];
138 Class viewClass = nil;
140 viewClass = NSClassFromString(viewClassStr);
141 NSImage* itemImage = [NSImage imageNamed: itemIdentifier];
143 // ... and create an NSToolbarItem for it and set that up:
144 tbi = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdentifier] autorelease];
145 [tbi setAction: itemAction];
146 if( !viewClassStr && [self isSelectable] )
147 [tbi setTarget: self];
148 if( viewClassStr ) // This isn't a regular item? It's a view?
150 NSView* theView = (NSView*) [[[viewClass alloc] init] autorelease];
151 [tbi setView: theView];
152 if( theActionStr && [theView respondsToSelector: @selector(setAction:)] )
153 [(id)theView setAction: itemAction];
154 if( itemLabel && [theView respondsToSelector: @selector(cell)] )
156 NSCell* theCell = [(id)theView cell];
157 if( [theCell respondsToSelector: @selector(setPlaceholderString:)] )
158 [(id)theCell setPlaceholderString: itemLabel];
162 [tbi setMinSize: NSMakeSize([minw floatValue], 32)];
164 [tbi setMaxSize: NSMakeSize([maxw floatValue], 32)];
165 [tbi setLabel: itemLabel];
166 [tbi setImage: itemImage];
167 if( itemCustomLabel )
168 [tbi setPaletteLabel: itemCustomLabel];
170 [tbi setPaletteLabel: itemLabel];
172 [tbi setToolTip: itemTooltip];
179 // -----------------------------------------------------------------------------
180 // toolbarDefaultItemIdentifiers:
181 // Returns the list of item identifiers we want to be in this toolbar by
182 // default. The list is loaded from the .plist file's "DefaultItems" array.
185 // 2004-01-19 witness Documented.
186 // -----------------------------------------------------------------------------
188 -(NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar*)toolbar
190 return [toolbarItems objectForKey: @"DefaultItems"];
194 // -----------------------------------------------------------------------------
195 // toolbarAllowedItemIdentifiers:
196 // Returns the list of item identifiers that may be in the toolbar. This
197 // simply returns the identifiers of all the items in our "Items"
198 // dictionary, plus a few sensible defaults like separators and spacer
199 // items the user may want to add as well.
201 // If this function doesn't return the item identifier, it *can't* be in
202 // the toolbar. Though if this returns it, that doesn't mean it currently
203 // is in the toolbar.
206 // 2004-01-19 witness Documented.
207 // -----------------------------------------------------------------------------
209 -(NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar*)toolbar
211 NSDictionary* allItems = [toolbarItems objectForKey: @"Items"];
212 int icount = [allItems count];
213 NSMutableArray* allowedItems = [NSMutableArray arrayWithCapacity: icount +4];
214 NSEnumerator* allItemItty;
217 for( allItemItty = [allItems keyEnumerator]; currItem = [allItemItty nextObject]; )
218 [allowedItems addObject: currItem];
220 [allowedItems addObject: NSToolbarSeparatorItemIdentifier];
221 [allowedItems addObject: NSToolbarSpaceItemIdentifier];
222 [allowedItems addObject: NSToolbarFlexibleSpaceItemIdentifier];
223 [allowedItems addObject: NSToolbarCustomizeToolbarItemIdentifier];
229 -(NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar
231 if( [self isSelectable] )
233 NSDictionary* allItems = [toolbarItems objectForKey: @"Items"];
234 int icount = [allItems count];
235 NSMutableArray* allowedItems = [NSMutableArray arrayWithCapacity: icount];
236 NSEnumerator* allItemItty;
239 for( allItemItty = [allItems keyEnumerator]; currItem = [allItemItty nextObject]; )
241 // View items aren't selectable, but make all others selectable:
242 if( [[allItems objectForKey: currItem] objectForKey: @"ViewClass"] == nil )
243 [allowedItems addObject: currItem];
249 return [NSArray array];
255 NSNumber* n = [[toolbarItems objectForKey: @"Options"] objectForKey: @"Selectable"];
256 return n && [n boolValue];
260 -(NSString*) selectedItemIdentifier
262 return [[owner toolbar] selectedItemIdentifier];
265 -(void) setSelectedItemIdentifier: (NSString*)str
267 [[owner toolbar] setSelectedItemIdentifier: str];
272 @implementation UKToolbarFactory (UKToolbarFactoryPrivateMethods)
274 // -----------------------------------------------------------------------------
276 // (Re)create our toolbar. This loads the .plist file whose name is the
277 // toolbar identifier and loads it. Then it adds the toolbar to our
281 // 2004-01-19 witness Documented.
282 // -----------------------------------------------------------------------------
284 -(void) setupToolbar: (id)sender
286 // Load list of items:
287 NSString* toolbarItemsPlistPath = [[NSBundle mainBundle] pathForResource: [self toolbarIdentifier] ofType: @"plist"];
289 [toolbarItems release];
290 toolbarItems = [[NSDictionary dictionaryWithContentsOfFile: toolbarItemsPlistPath] retain];
292 // (Re-) create toolbar:
293 NSToolbar* tb = [[[NSToolbar alloc] initWithIdentifier: [self toolbarIdentifier]] autorelease];
294 [tb setDelegate: self];
295 [tb setAllowsUserCustomization: YES];
296 [tb setAutosavesConfiguration: YES];
297 [owner setToolbar: tb];
299 if( [self isSelectable] )
300 [self setSelectedItemIdentifier: [[toolbarItems objectForKey: @"DefaultItems"] objectAtIndex: 0]];
304 // -----------------------------------------------------------------------------
306 // Toolbar action if we have selectable items. This selects the item, then
307 // sends the actual action that the item has.
310 // 2004-10-03 witness Documented.
311 // -----------------------------------------------------------------------------
313 -(void) itemClicked: (NSToolbarItem*)sender
315 [self setSelectedItemIdentifier: [sender itemIdentifier]];
317 NSDictionary* dict = [[toolbarItems objectForKey: @"Items"] objectForKey: [sender itemIdentifier]];
318 NSString* theActionStr = [dict objectForKey: @"Action"];
321 SEL itemAction = NSSelectorFromString(theActionStr);
322 [[UKFirstResponder firstResponder] performSelector: itemAction withObject: sender];