/
SegmentedToolbarItem.m
245 lines (189 loc) · 6.89 KB
/
SegmentedToolbarItem.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#import "SegmentedToolbarItem.h"
@interface ValData : NSObject <NSValidatedUserInterfaceItem>
{
SEL sel;
}
@property (nonatomic, assign) SEL sel;
+ (ValData*)valData:(SEL)sel;
- (SEL)action;
- (NSInteger)tag;
@end
@implementation ValData
@synthesize sel;
+ (ValData*)valData:(SEL)sel_
{
ValData* d = [[ValData alloc] init];
d.sel = sel_;
return d;
}
- (SEL)action
{
return sel;
}
- (NSInteger)tag;
{
return 0;
}
@end
@implementation SegmentedToolbarItem
+(SegmentedToolbarItem*)itemWithIdentifier:(NSString *)identifier label:(NSString *)label paletteLabel:(NSString *)pallabel segments:(int)segments
{
return [[SegmentedToolbarItem alloc] initWithItemIdentifier:identifier label:label paletteLabel:pallabel segments:segments];
}
-(id)initWithItemIdentifier:(NSString *)identifier label:(NSString *)label paletteLabel:(NSString *)pallabel segments:(int)segments
{
if(self = [super initWithItemIdentifier:identifier])
{
[self setLabel:label];
[self setPaletteLabel:pallabel];
control = [[NSSegmentedControl alloc] init];
[control setSegmentCount:segments];
[[control cell] setTrackingMode:NSSegmentSwitchTrackingMomentary];
if (segments != 1)
{
menu = [[NSMenu alloc] init];
for (int i = 0; i < segments; i++)
[menu addItem:[[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""]];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:pallabel action:NULL keyEquivalent:@""];
[item setSubmenu:menu];
[self setMenuFormRepresentation:item];
}
else
{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
[self setMenuFormRepresentation:item];
menu = nil;
}
[control setTarget:self];
[control setAction:@selector(clicked:)];
actions = [NSMutableArray array];
targets = [NSMutableArray array];
for (int i = 0; i < segments; i++)
{
[actions addObject:[NSNull null]];
[targets addObject:[NSNull null]];
}
}
return self;
}
-(void)validate
{
if ([[NSApplication sharedApplication] mainWindow] != [control window])
{
[self setEnabled:NO];
}
else
{
[self setEnabled:YES];
int count = (int)[control segmentCount];
for (int i = 0; i < count; i++)
{
SEL action = [actions[i] pointerValue];
id target = targets[i];
if (target == [NSNull null])
target = nil;
id validator = [NSApp targetForAction:action to:target from:self];
BOOL enable;
if ((validator == nil) || ![validator respondsToSelector:action])
{
enable = NO;
}
else if ([validator respondsToSelector:@selector(validateUserInterfaceItem:)])
{
enable = [validator validateUserInterfaceItem:[ValData valData:action]];
}
else
{
enable = YES;
}
[control setEnabled:enable forSegment:i];
}
}
}
-(void)setSegment:(int)segment label:(NSString *)label image:(NSImage *)image longLabel:(NSString *)longlabel width:(int)width target:(id)target action:(SEL)action
{
if (segment < 0 || segment >= [control segmentCount])
return;
[control setLabel:label forSegment:segment];
[control setImage:image forSegment:segment];
[control setWidth:width forSegment:segment];
[[control cell] setToolTip:longlabel forSegment:segment];
actions[segment] = [NSValue valueWithPointer:action];
targets[segment] = target ? target : [NSNull null];
NSMenuItem *item;
if(menu)
item = [menu itemAtIndex:segment];
else
item = [self menuFormRepresentation];
[item setTitle:longlabel];
[item setImage:image];
[item setAction:action];
}
-(void)setSegment:(int)segment label:(NSString *)label longLabel:(NSString *)longlabel target:(id)target action:(SEL)action
{
[self setSegment:segment label:label image:nil longLabel:longlabel width:0 target:target action:action];
}
-(void)setSegment:(int)segment imageName:(NSString *)imagename longLabel:(NSString *)longlabel target:(id)target action:(SEL)action
{
NSImage *image = [NSImage imageNamed:imagename];
int width = [image size].width;
if (width < 22)
width = 22;
[self setSegment:segment label:nil image:image longLabel:longlabel width:width target:target action:action];
}
-(void)setupView
{
[control sizeToFit];
[self setView:control];
[self setMinSize:[control frame].size];
[self setMaxSize:[control frame].size];
}
-(void)clicked:(id)sender
{
id action = actions[[sender selectedSegment]];
SEL selAction = [action pointerValue];
[[NSApplication sharedApplication] sendAction:selAction to:nil from:self];
}
- (NSView*)control
{
return control;
}
@end
@implementation ToolItem
+(SegmentedToolbarItem *)itemWithIdentifier:(NSString*)identifier label:(NSString*)label paletteLabel:(NSString*)pallabel imageName:(NSString*)imagename longLabel:(NSString*)longlabel action:(SEL)action activeSelector:(SEL)activeselector target:(id)activetarget
{
return [[ToolItem alloc] initWithItemIdentifier:identifier label:label paletteLabel:pallabel imageName:imagename longLabel:longlabel action:action activeSelector:activeselector target:activetarget];
}
-(id)initWithItemIdentifier:(NSString *)identifier label:(NSString *)label paletteLabel:(NSString *)pallabel imageName:(NSString *)imagename longLabel:(NSString *)longlabel action:(SEL)action activeSelector:(SEL)activeselector target:(id)activetarget
{
if(self = [super initWithItemIdentifier:identifier label:label paletteLabel:pallabel segments:1])
{
sel = activeselector;
[[control cell] setTrackingMode:NSSegmentSwitchTrackingSelectAny];
[self setSegment:0 imageName:imagename longLabel:longlabel target:activetarget action:action];
[self setupView];
}
return self;
}
-(void)validate
{
[super validate];
id action = actions[0];
id target = targets[0];
SEL selAction = [action pointerValue];
if (target == [NSNull null])
target = nil;
id validator = [NSApp targetForAction:selAction to:target from:self];
BOOL active = NO;
if (validator)
{
IMP imp = [validator methodForSelector:sel];
BOOL (*func)(id, SEL) = (void*)imp;
active = func(validator, sel);
}
[control setSelected:active forSegment:0];
}
@end
@implementation NSSegmentedCell (AlwaysTextured)
-(BOOL)_isTextured { return YES; }
@end