-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSRadioButtonCell.m
358 lines (288 loc) · 10.9 KB
/
SSRadioButtonCell.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#import "SSRadioButtonCell.h"
#define DEFAULT_BUTTON_SPACING 1.0
#define END_INSET 2.0
@implementation SSRadioButtonCell
+ (void)initialize {
[self setKeys:[NSArray arrayWithObjects:@"numberOfButtons", @"widths", nil]
triggerChangeNotificationsForDependentKey:@"widths"];
[self setKeys:[NSArray arrayWithObjects:@"numberOfButtons", @"widths", nil]
triggerChangeNotificationsForDependentKey:@"indexedWidths"];
}
/*
// Implemented this NSFormatter method for debugging. Doesn't make any sense, but I get
// this message when dropping an SSRadioButtonCell onto an NSTableColumn in Interface Builder.
- (NSAttributedString*)attributedStringForObjectValue:(id)object withDefaultAttributes:(NSDictionary*)attributes {
NSAttributedString* as = [[NSAttributedString alloc] initWithString:@"foob SSRadioButtonCell"] ;
return [as autorelease] ;
}
*/
- (void)setObjectValue:(id <NSCopying>)object {
if (object) {
[super setObjectValue:object] ;
}
}
- (void)setSelectedImage:(NSImage*)newImage {
if (_selectedImage != newImage) {
[_selectedImage release];
_selectedImage = [newImage copy];
}
}
- (NSImage*)selectedImage {
return _selectedImage ;
}
- (void)setDeselectedImage:(NSImage*)newImage {
if (_deselectedImage != newImage) {
[_deselectedImage release];
_deselectedImage = [newImage copy];
}
}
- (NSImage*)deselectedImage {
return _deselectedImage ;
}
- (void)setWidths:(NSMutableArray*)newWidths {
[[NSNotificationCenter defaultCenter] postNotificationName:@"WidthsWillChange" object:nil] ;
if (_widths != newWidths) {
[_widths release];
_widths = [newWidths mutableCopy] ;
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"WidthsDidChange" object:nil] ;
}
- (NSMutableArray*)widths {
return _widths ;
}
- (void)setWidth:(CGFloat)width forSegment:(NSInteger)segment {
NSMutableArray* widths = [self widths] ;
NSInteger widthsCount = [widths count] ;
if ((segment >= 0) && (segment < widthsCount)) {
CGFloat endInset = ((segment==0) || (segment==(widthsCount-1)))
? END_INSET : 0.0 ;
NSNumber* widthNumber = [NSNumber numberWithDouble:(width - endInset)] ;
[widths replaceObjectAtIndex:segment withObject:widthNumber] ;
}
}
- (void)triggerKVO {
[self setWidths:[self widths]] ;
}
// Probably NSCoder protocol conformance is needed for archiving by Interface Builder.
- (id)initWithCoder:(NSCoder*)decoder {
self = [super initWithCoder:decoder];
_selectedImage = [[decoder decodeObjectForKey:@"selectedImage"] retain] ;
_deselectedImage = [[decoder decodeObjectForKey:@"deselectedImage"] retain] ;
_widths = [[decoder decodeObjectForKey:@"widths"] retain] ;
_numberOfButtons = [_widths count] ;
return self;
}
- (void) encodeWithCoder:(NSCoder*)encoder {
[super encodeWithCoder:encoder];
[encoder encodeObject:[self selectedImage] forKey:@"selectedImage"] ;
[encoder encodeObject:[self deselectedImage] forKey:@"deselectedImage"] ;
[encoder encodeObject:[self widths] forKey:@"widths"] ;
return;
}
// Documentation
// Control and Cell Programming Topics for Cocoa
// Subclassing NSCell
// makes the following stupidly vague statement:
// "If the subclass contains instance variables that hold pointers to objects,
// consider overriding copyWithZone: to duplicate the objects. The default
// version copies only pointers to the objects."
// What the hell do them mean by "consider"? This is supposed to be
// technical documentation, not a poetry class. Well,
// it turns out that, if this cell is used in an NSTableColumn at least,
// copies seem to be made whenever the cell is clicked.
// Therefore, if you don't implement the following, you get frequent crashes.
- (id) copyWithZone:(NSZone*)zone {
SSRadioButtonCell *cellCopy = [super copyWithZone:zone];
// For explanation of this see:
// Memory Management Programming Guide for Cocoa
// Implementing Object Copy
// Using NSCopyObject()
cellCopy->_selectedImage = nil;
[cellCopy setSelectedImage:[self selectedImage]];
cellCopy->_deselectedImage = nil;
[cellCopy setDeselectedImage:[self deselectedImage]];
cellCopy->_widths = nil ;
[cellCopy setWidths:[self widths] ] ;
cellCopy->_currentFrameInControlView = _currentFrameInControlView ;
cellCopy->_numberOfButtons = _numberOfButtons ;
return cellCopy;
}
- (void)setEnabled:(BOOL)enabled {
}
- (id) init {
self = [super init] ;
if (self != nil) {
// Since -[NSImage imageNamed:] does not work in frameworks for some reason,
// we have to dig for image resources with our bare hands...
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSString* imagePath ;
NSImage* image ;
[self setType:NSImageCellType] ;
// The above does not "take" until you set the image
imagePath = [NSBundle pathForResource:@"SSRadioButtonCellIcon" ofType:@"tiff"
inDirectory:[bundle bundlePath]];
image = [[NSImage alloc] initByReferencingFile:imagePath];
[self setImage:image] ;
[image release] ;
imagePath = [NSBundle pathForResource:@"RadioButtonSelectedSmall" ofType:@"png"
inDirectory:[bundle bundlePath]];
image = [[NSImage alloc] initByReferencingFile:imagePath];
[self setSelectedImage:image] ;
[image release] ;
imagePath = [NSBundle pathForResource:@"RadioButtonDeselectedSmall" ofType:@"png"
inDirectory:[bundle bundlePath]];
image = [[NSImage alloc] initByReferencingFile:imagePath];
[self setDeselectedImage:image] ;
[image release] ;
[self setContinuous:YES] ;
[self setWidths:[NSMutableArray arrayWithCapacity:16]] ;
// Set to a harmless default value:
[self setObjectValue:[NSNumber numberWithInteger:1]] ;
}
return self;
}
- (void) dealloc {
[_selectedImage release] ;
[_deselectedImage release] ;
[_widths release] ;
[super setObjectValue:nil] ;
[super dealloc] ;
}
- (id)defaultObjectValueAtIndex:(NSInteger)index {
return [[[SSRadioButtonCell alloc] init] autorelease] ;
}
- (id)stringForObjectValue:(id)object {
return [object description] ;
}
- (void)setNumberOfButtons:(NSInteger)x {
_numberOfButtons = x ;
NSMutableArray* widths = [self widths] ;
NSInteger currentWidthsCount = [widths count] ;
CGFloat defaultWidth = [[self selectedImage] size].width + DEFAULT_BUTTON_SPACING ;
NSNumber* defaultWidthNumber = [NSNumber numberWithDouble:defaultWidth] ;
NSInteger i ;
// If requested number is less than current, add new default widths
for (i=currentWidthsCount; i<x; i++) {
[widths addObject:defaultWidthNumber] ;
}
// If requested number is greater than current, remove widths
while ([widths count] > x) {
[widths removeLastObject] ;
}
[self setWidths:widths] ;
}
- (NSInteger)numberOfButtons {
return _numberOfButtons ;
}
// I implemented this to see if it would be invoked after dropping in
// Interface Builder, but it never gets invoked by IB.
// It does get invoked when the app runs, in lieu of drawInteriorWithFrame: ARGHHHH!!
/* - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[controlView lockFocus];
NSImage* image = [self image] ;
[image setFlipped: [controlView isFlipped]];
[image drawInRect:cellFrame
fromRect:NSMakeRect(0,
0,
NSWidth(cellFrame),
NSHeight(cellFrame))
operation:NSCompositeSourceOver fraction:1.0] ;
[controlView unlockFocus];
}
*/
// This method is invoked to draw the cell when it is instantiated and
// run in an actual application (i.e., not in Interface Builder).
- (void) drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSNumber *objectValue = [self objectValue];
if (objectValue) {
[controlView lockFocus] ;
NSInteger selectedIndex = [objectValue integerValue] ;
NSInteger i ;
NSImage* selectedImage = [self selectedImage] ;
NSImage* deselectedImage = [self deselectedImage] ;
NSArray* widths = [self widths] ;
CGFloat x = NSMinX(cellFrame) ; // will increase in loop
CGFloat verticalInset = (NSHeight(cellFrame) - [selectedImage size].height)/2 + 1.0 ;
// We add the 1.0 because it looks better to be a little closer to the bottom
// than the top. More verticalOffset <--> radio button is farther DOWN.
// Tried 0.55 instead of 1.0 but this made the buttons stretch to elliptical shape
// with longer axis vertical. Looked really weird.
CGFloat y = NSMinY(cellFrame) + verticalInset ; // will stay constant
CGFloat height = [selectedImage size].height ; // will stay constant
for (i = 0; i < [self numberOfButtons]; i++) {
CGFloat width = [[widths objectAtIndex:i] doubleValue] ;
NSRect rect = NSMakeRect(x, y, width, height) ;
if (NSIntersectsRect(rect, cellFrame)) {
NSRect intersectRect = NSIntersectionRect(rect, cellFrame) ;
NSImage* image = (i == selectedIndex) ? selectedImage : deselectedImage ;
CGFloat centeringOffset = (width - [image size].width) / 2 ;
[image setFlipped: [controlView isFlipped]];
[image drawInRect:NSOffsetRect(intersectRect, centeringOffset, 0.0)
fromRect:NSMakeRect(0,
0,
NSWidth(intersectRect),
NSHeight(intersectRect))
operation:NSCompositeSourceOver fraction:1.0] ;
}
x += width ;
}
[controlView unlockFocus];
}
}
- (BOOL) trackMouse:(NSEvent*)theEvent
inRect:(NSRect)cellFrame
ofView:(NSView*)controlView
untilMouseUp:(BOOL)untilMouseUp {
_currentFrameInControlView = [self drawingRectForBounds:cellFrame];
return [super trackMouse:theEvent
inRect:cellFrame
ofView:controlView
untilMouseUp:untilMouseUp] ;
}
- (BOOL) startTrackingAt:(NSPoint)startPoint
inView:(NSView*)controlView {
[super startTrackingAt:startPoint
inView:controlView] ;
return YES;
}
- (BOOL) continueTracking:(NSPoint)lastPoint
at:(NSPoint)currentPoint
inView:(NSView *)controlView {
if ([super continueTracking:lastPoint
at:currentPoint
inView: controlView]) {
NSNumber* newObjectValue = [self calculateSelectionForPoint:currentPoint
inView: controlView] ;
[self setObjectValue:newObjectValue] ;
}
return YES;
}
- (void) stopTracking:(NSPoint)
lastPoint at:(NSPoint)stopPoint
inView:(NSView*)controlView
mouseIsUp:(BOOL)flag {
NSNumber* newObjectValue = [self calculateSelectionForPoint:stopPoint
inView: controlView] ;
[self setObjectValue:newObjectValue];
[super stopTracking: lastPoint at: stopPoint inView: controlView mouseIsUp: flag];
}
- (NSNumber*)calculateSelectionForPoint:(NSPoint)point
inView:(NSView*)controlView {
CGFloat zeroX = NSMinX([self drawingRectForBounds:_currentFrameInControlView]) ;
CGFloat x = point.x - zeroX ;
NSArray* widths = [self widths] ;
NSInteger numberOfWidths = [widths count] ;
CGFloat end = 0 ;
NSInteger selectedIndex = 0 ;
NSInteger i ;
for (i=0; i<numberOfWidths; i++) {
end += [[widths objectAtIndex:i] doubleValue] ;
if (x < end) {
selectedIndex = i ;
break ;
}
}
NSNumber* selection = [NSNumber numberWithInteger:selectedIndex] ;
return selection ;
}
@end