]> err.no Git - sope/blob - sopex/SOPEX/SOPEXToolbarController.m
SOPE:X 2.x
[sope] / sopex / SOPEX / SOPEXToolbarController.m
1 // $Id: SOPEXToolbarController.m,v 1.3 2004/05/02 16:27:46 znek Exp $
2
3 #import "SOPEXToolbarController.h"
4 #import <AppKit/AppKit.h>
5
6 @implementation SOPEXToolbarController
7
8 - (id)initWithIdentifier:(NSString *)_tid target:(id)_target {
9     if ((self = [super init])) {
10         self->toolbarID = [_tid copy];
11         self->target    = _target;
12     }
13     return self;
14 }
15 - (id)init {
16     return [self initWithIdentifier:nil target:nil];
17 }
18
19 - (void)dealloc {
20     [self->toolbar      release];
21     [self->toolbarID    release];
22     [self->idToInfo     release];
23     [self->cachedItems release];
24     [super dealloc];
25 }
26
27 /* accessors */
28
29 - (NSToolbar *)toolbar {
30     if (self->toolbar == nil) {
31         self->toolbar = 
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];
37     }
38     return self->toolbar;
39 }
40
41 - (NSDictionary *)toolbarDictionary {
42     NSString *p;
43     
44     if (self->idToInfo)
45         return self->idToInfo;
46     
47     p = [[NSBundle bundleForClass:isa] 
48           pathForResource:self->toolbarID ofType:@"toolbar"];
49     if (p == nil) {
50         NSLog(@"did not find %@.toolbar !", self->toolbarID);
51         return nil;
52     }
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;
58 }
59
60 - (NSArray *)defaultItemIdentifiers {
61     return [[self toolbarDictionary] objectForKey:@"defaultItems"];
62 }
63 - (NSArray *)allowedItemIdentifiers {
64     return [[self toolbarDictionary] objectForKey:@"allowedItems"];
65 }
66 - (NSArray *)selectableItemIdentifiers {
67     return [[self toolbarDictionary] objectForKey:@"selectableItems"];
68 }
69
70 - (NSDictionary *)infoForIdentifier:(NSString *)_itemID {
71     return [[self toolbarDictionary] objectForKey:_itemID];
72 }
73
74 /* operations */
75
76 - (void)applyOnWindow:(NSWindow *)_window {
77     [_window setToolbar:[self toolbar]];
78 }
79
80 /* toolbar delegate */
81
82 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
83      itemForItemIdentifier:(NSString *)itemIdent 
84  willBeInsertedIntoToolbar:(BOOL)willBeInserted 
85 {
86     NSToolbarItem *toolbarItem;
87     NSDictionary *itemInfo;
88     NSString *s;
89     
90     if ((itemInfo = [self infoForIdentifier:itemIdent]) == nil)
91         return nil;
92     
93     if ((toolbarItem = [self->cachedItems objectForKey:itemIdent]) != nil)
94         return toolbarItem;
95     
96     toolbarItem = 
97         [[NSToolbarItem alloc] initWithItemIdentifier:itemIdent];
98     
99     if ((s = [itemInfo objectForKey:@"initialState"]))
100     {
101         if ([s isEqualToString:@"disabled"])
102             [toolbarItem setEnabled:NO];
103     }
104     else
105     {
106         [toolbarItem setEnabled:YES];
107     }
108     
109     if ((s = [itemInfo objectForKey:@"label"]))
110         [toolbarItem setLabel:s];
111     if ((s = [itemInfo objectForKey:@"paletteLabel"]))
112         [toolbarItem setPaletteLabel:s];
113     else
114         [toolbarItem setPaletteLabel:[toolbarItem label]];
115     
116     if ((s = [itemInfo objectForKey:@"toolTip"]))
117         [toolbarItem setToolTip:s];
118     else
119         [toolbarItem setToolTip:[toolbarItem paletteLabel]];
120
121     if ((s = [itemInfo objectForKey:@"imageName"]))
122     {
123         NSString *path = [[NSBundle bundleForClass:isa] pathForResource:[s stringByDeletingPathExtension] ofType:[s pathExtension]];
124         if(path != nil)
125         {
126             NSData *data = [NSData dataWithContentsOfFile:path];
127             if(data != nil)
128             {
129                 NSImage *image;
130                 
131                 image = [[NSImage alloc] initWithData:data];
132                 [toolbarItem setImage:image];
133                 [image release];
134             }
135             else
136             {
137                 NSLog(@"%s cannot open image file at path:%@", __PRETTY_FUNCTION__, path);
138             }
139         }
140         else
141         {
142             NSLog(@"%s cannot find image named:%@", __PRETTY_FUNCTION__, s);
143         }
144     }
145     if ((s = [itemInfo objectForKey:@"action"])) {
146         [toolbarItem setTarget:self->target];
147         [toolbarItem setAction:NSSelectorFromString(s)];
148     } 
149     
150     [self->cachedItems setObject:toolbarItem forKey:itemIdent];
151     [toolbarItem release];
152
153     return toolbarItem;
154 }
155
156 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
157     return [self defaultItemIdentifiers];
158 }
159 - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
160     return [self allowedItemIdentifiers];
161 }
162 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
163     return [self selectableItemIdentifiers];
164 }
165
166 @end /* SOPEXToolbarController */