1 // $Id: SOPEXToolbarController.m 1 2004-08-20 11:17:52Z znek $
3 #import "SOPEXToolbarController.h"
4 #import <AppKit/AppKit.h>
6 @implementation SOPEXToolbarController
8 - (id)initWithIdentifier:(NSString *)_tid target:(id)_target {
9 if ((self = [super init])) {
10 self->toolbarID = [_tid copy];
11 self->target = _target;
16 return [self initWithIdentifier:nil target:nil];
20 [self->toolbar release];
21 [self->toolbarID release];
22 [self->idToInfo release];
23 [self->cachedItems release];
29 - (NSToolbar *)toolbar {
30 if (self->toolbar == nil) {
32 [[NSToolbar alloc] initWithIdentifier:self->toolbarID];
33 [self->toolbar setAllowsUserCustomization:YES];
34 [self->toolbar setAutosavesConfiguration:YES];
35 [self->toolbar setDisplayMode:NSToolbarDisplayModeDefault];
36 [self->toolbar setDelegate:self];
41 - (NSDictionary *)toolbarDictionary {
45 return self->idToInfo;
47 p = [[NSBundle bundleForClass:isa]
48 pathForResource:self->toolbarID ofType:@"toolbar"];
50 NSLog(@"did not find %@.toolbar !", self->toolbarID);
53 self->idToInfo = [[NSDictionary alloc] initWithContentsOfFile:p];
54 if (self->idToInfo == nil)
55 NSLog(@"could not load %@.toolbar: %@", self->toolbarID, p);
56 self->cachedItems = [[NSMutableDictionary alloc] initWithCapacity:[[self->idToInfo objectForKey:@"allowedItems"] count]];
57 return self->idToInfo;
60 - (NSArray *)defaultItemIdentifiers {
61 return [[self toolbarDictionary] objectForKey:@"defaultItems"];
63 - (NSArray *)allowedItemIdentifiers {
64 return [[self toolbarDictionary] objectForKey:@"allowedItems"];
66 - (NSArray *)selectableItemIdentifiers {
67 return [[self toolbarDictionary] objectForKey:@"selectableItems"];
70 - (NSDictionary *)infoForIdentifier:(NSString *)_itemID {
71 return [[self toolbarDictionary] objectForKey:_itemID];
76 - (void)applyOnWindow:(NSWindow *)_window {
77 [_window setToolbar:[self toolbar]];
80 /* toolbar delegate */
82 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
83 itemForItemIdentifier:(NSString *)itemIdent
84 willBeInsertedIntoToolbar:(BOOL)willBeInserted
86 NSToolbarItem *toolbarItem;
87 NSDictionary *itemInfo;
90 if ((itemInfo = [self infoForIdentifier:itemIdent]) == nil)
93 if ((toolbarItem = [self->cachedItems objectForKey:itemIdent]) != nil)
97 [[NSToolbarItem alloc] initWithItemIdentifier:itemIdent];
99 if ((s = [itemInfo objectForKey:@"initialState"]))
101 if ([s isEqualToString:@"disabled"])
102 [toolbarItem setEnabled:NO];
106 [toolbarItem setEnabled:YES];
109 if ((s = [itemInfo objectForKey:@"label"]))
110 [toolbarItem setLabel:s];
111 if ((s = [itemInfo objectForKey:@"paletteLabel"]))
112 [toolbarItem setPaletteLabel:s];
114 [toolbarItem setPaletteLabel:[toolbarItem label]];
116 if ((s = [itemInfo objectForKey:@"toolTip"]))
117 [toolbarItem setToolTip:s];
119 [toolbarItem setToolTip:[toolbarItem paletteLabel]];
121 if ((s = [itemInfo objectForKey:@"imageName"]))
123 NSString *path = [[NSBundle bundleForClass:isa] pathForResource:[s stringByDeletingPathExtension] ofType:[s pathExtension]];
126 NSData *data = [NSData dataWithContentsOfFile:path];
131 image = [[NSImage alloc] initWithData:data];
132 [toolbarItem setImage:image];
137 NSLog(@"%s cannot open image file at path:%@", __PRETTY_FUNCTION__, path);
142 NSLog(@"%s cannot find image named:%@", __PRETTY_FUNCTION__, s);
145 if ((s = [itemInfo objectForKey:@"action"])) {
146 [toolbarItem setTarget:self->target];
147 [toolbarItem setAction:NSSelectorFromString(s)];
150 [self->cachedItems setObject:toolbarItem forKey:itemIdent];
151 [toolbarItem release];
156 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
157 return [self defaultItemIdentifiers];
159 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
160 return [self allowedItemIdentifiers];
162 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
163 return [self selectableItemIdentifiers];
166 @end /* SOPEXToolbarController */