Prepare USB_DataHddFileEnd with a separate function.
[MacTF.git] / UKToolbarFactory.m
blob20783f4fb0c8154e0bb71796a57e4151957bfb92
1 //
2 //  UKToolbarFactory.m
3 //  UKToolbarFactory
4 //
5 //  Created by Uli Kusterer on Sat Jan 17 2004.
6 //  Copyright (c) 2004 M. Uli Kusterer. All rights reserved.
7 //
9 // -----------------------------------------------------------------------------
10 //      Headers:
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;
23 @end
26 @implementation UKToolbarFactory
28 // -----------------------------------------------------------------------------
29 //      * DESTRUCTOR:
30 //              Get rid of the objects we created.
32 //      REVISIONS:
33 //              2004-01-19      witness Documented.
34 // -----------------------------------------------------------------------------
36 -(void) dealloc
38         [toolbarItems release];
39         [toolbarIdentifier release];
40         [super dealloc];
44 // -----------------------------------------------------------------------------
45 //      awakeFromNib:
46 //              We were created from a NIB file. Add the toolbar to our window.
48 //      REVISIONS:
49 //              2004-01-19      witness Documented.
50 // -----------------------------------------------------------------------------
52 -(void) awakeFromNib
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.
63 //      REVISIONS:
64 //              2004-01-19      witness Documented.
65 // -----------------------------------------------------------------------------
67 -(void)                 setToolbarIdentifier: (NSString*)str
69         [str retain];
70         [toolbarIdentifier release];
71         toolbarIdentifier = str;
72         
73         [self setupToolbar: nil];   // Recreate toolbar.
77 // -----------------------------------------------------------------------------
78 //      toolbarIdentifier:
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.
83 //      REVISIONS:
84 //              2004-01-19      witness Documented.
85 // -----------------------------------------------------------------------------
87 -(NSString*)    toolbarIdentifier
89         if( !toolbarIdentifier )
90                 toolbarIdentifier = [[NSString stringWithFormat: @"%@.%@", [[NSBundle mainBundle] bundleIdentifier], [owner frameAutosaveName]] retain];
91         
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
101 //              .plist file.
103 //      REVISIONS:
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;
113                 
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];
123         
124         // Otherwise, look it up in our list of custom items:
125         currItem = [allItems objectForKey: itemIdentifier];
126         if( currItem )
127         {
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;
139                 if( viewClassStr )
140                         viewClass = NSClassFromString(viewClassStr);
141                 NSImage*        itemImage = [NSImage imageNamed: itemIdentifier];
142                 
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?
149                 {
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)] )
155                         {
156                                 NSCell* theCell = [(id)theView cell];
157                                 if( [theCell respondsToSelector: @selector(setPlaceholderString:)] )
158                                         [(id)theCell setPlaceholderString: itemLabel];
159                         }
160                 }
161                 if( minw )
162                         [tbi setMinSize: NSMakeSize([minw floatValue], 32)];
163                 if( maxw )
164                         [tbi setMaxSize: NSMakeSize([maxw floatValue], 32)];
165                 [tbi setLabel: itemLabel];
166                 [tbi setImage: itemImage];
167                 if( itemCustomLabel )
168                         [tbi setPaletteLabel: itemCustomLabel];
169                 else
170                         [tbi setPaletteLabel: itemLabel];
171                 if( itemTooltip )
172                         [tbi setToolTip: itemTooltip];
173         }
174         
175         return tbi;
177     
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.
184 //      REVISIONS:
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.
205 //      REVISIONS:
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;
215         NSString*               currItem;
216         
217         for( allItemItty = [allItems keyEnumerator]; currItem = [allItemItty nextObject]; )
218                 [allowedItems addObject: currItem];
219         
220         [allowedItems addObject: NSToolbarSeparatorItemIdentifier];
221         [allowedItems addObject: NSToolbarSpaceItemIdentifier];
222         [allowedItems addObject: NSToolbarFlexibleSpaceItemIdentifier];
223         [allowedItems addObject: NSToolbarCustomizeToolbarItemIdentifier];
224         
225         return allowedItems;
229 -(NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar*)toolbar
231         if( [self isSelectable] )
232         {
233                 NSDictionary*   allItems = [toolbarItems objectForKey: @"Items"];
234                 int                             icount = [allItems count];
235                 NSMutableArray* allowedItems = [NSMutableArray arrayWithCapacity: icount];
236                 NSEnumerator*   allItemItty;
237                 NSString*               currItem;
238                 
239                 for( allItemItty = [allItems keyEnumerator]; currItem = [allItemItty nextObject]; )
240                 {
241                         // View items aren't selectable, but make all others selectable:
242                         if( [[allItems objectForKey: currItem] objectForKey: @"ViewClass"] == nil )
243                                 [allowedItems addObject: currItem];
244                 }
245                 
246                 return allowedItems;
247         }
248         else
249                 return [NSArray array];
253 -(BOOL) isSelectable
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];
270 @end
272 @implementation UKToolbarFactory (UKToolbarFactoryPrivateMethods)
274 // -----------------------------------------------------------------------------
275 //      setupToolbar:
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
278 //              window.
280 //      REVISIONS:
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"];
288         if( toolbarItems )
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];
298         
299         if( [self isSelectable] )
300                 [self setSelectedItemIdentifier: [[toolbarItems objectForKey: @"DefaultItems"] objectAtIndex: 0]];
304 // -----------------------------------------------------------------------------
305 //      itemClicked:
306 //              Toolbar action if we have selectable items. This selects the item, then
307 //              sends the actual action that the item has.
309 //      REVISIONS:
310 //              2004-10-03      witness Documented.
311 // -----------------------------------------------------------------------------
313 -(void) itemClicked: (NSToolbarItem*)sender
315         [self setSelectedItemIdentifier: [sender itemIdentifier]];
316         
317         NSDictionary*   dict = [[toolbarItems objectForKey: @"Items"] objectForKey: [sender itemIdentifier]];
318         NSString*               theActionStr = [dict objectForKey: @"Action"];
319         if( theActionStr )
320         {
321                 SEL             itemAction = NSSelectorFromString(theActionStr);
322                 [[UKFirstResponder firstResponder] performSelector: itemAction withObject: sender];
323         }
327 @end