root/trunk/Views/JVColorWellCell.m

Revision 3460, 6.9 kB (checked in by timothy, 2 years ago)

Removing the retain/autorelease idiom for accessor methods.

Line 
1 #import "JVColorWellCell.h"
2
3 static NSMutableSet *colorWellCells = nil;
4
5 NSString *JVColorWellCellColorDidChangeNotification = @"JVColorWellCellColorDidChangeNotification";
6
7 @implementation JVColorWellCell
8 + (void) colorPanelColorChanged:(NSNotification *) notification {
9         NSColorPanel *panel = [notification object];
10         NSEnumerator *enumerator = [colorWellCells objectEnumerator];
11         JVColorWellCell *cell = nil;
12
13         while( ( cell = [enumerator nextObject] ) ) {
14                 if( [cell isActive] ) {
15                         [cell setColor:[panel color]];
16                         [[NSNotificationCenter defaultCenter] postNotificationName:JVColorWellCellColorDidChangeNotification object:cell userInfo:nil];
17                 }
18         }
19 }
20
21 + (void) colorPanelClosed:(NSNotification *) notification {
22         NSEnumerator *enumerator = [colorWellCells objectEnumerator];
23         JVColorWellCell *cell = nil;
24
25         while( ( cell = [enumerator nextObject] ) )
26                 if( [cell isActive] ) [cell deactivate];
27 }
28
29 + (void) initialize {
30         [super initialize];
31         static BOOL tooLate = NO;
32         if( ! tooLate ) {
33                 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( colorPanelColorChanged: ) name:NSColorPanelColorDidChangeNotification object:nil];
34                 tooLate = YES;
35         }
36 }
37
38 #pragma mark -
39
40 - (id) initTextCell:(NSString *) string {
41         return ( self = [self initImageCell:nil] );
42 }
43
44 - (id) initImageCell:(NSImage *) image {
45         static BOOL observingClose = NO;
46         if( ! observingClose ) {
47                 [[NSNotificationCenter defaultCenter] addObserver:[self class] selector:@selector( colorPanelClosed: ) name:NSWindowWillCloseNotification object:[NSColorPanel sharedColorPanel]];
48                 observingClose = YES;
49         }
50
51         if( ( self = [super initImageCell:nil] ) ) {
52                 if( ! colorWellCells ) colorWellCells = [[NSMutableSet set] retain];
53                 [colorWellCells addObject:self];
54                 _releasing = NO;
55
56                 [self setShowsWebValue:YES];
57                 [self setEditable:YES];
58                 [self setColor:[NSColor whiteColor]];
59                 [self setBezelStyle:NSShadowlessSquareBezelStyle];
60                 [self setButtonType:NSOnOffButton];
61                 [self setImagePosition:NSImageOnly];
62                 [super setTarget:self];
63                 [super setAction:@selector( clicked: )];
64                 [super setTitle:@""];
65                 [super setAlternateTitle:@""];
66                 [super setImage:nil];
67                 [super setAlternateImage:nil];
68         }
69
70         return self;
71 }
72
73 - (id) copyWithZone:(NSZone *) zone {
74         JVColorWellCell *ret = [super copyWithZone:zone];
75         ret -> _color = [_color copyWithZone:zone];
76         ret -> _showsWebValue = _showsWebValue;
77         ret -> _releasing = NO;
78
79         if( ! colorWellCells ) colorWellCells = [[NSMutableSet set] retain];
80         [colorWellCells addObject:ret];
81
82         return ret;
83 }
84
85 - (void) release {
86         if( ! _releasing && ( [self retainCount] - 1 ) == 1 ) {
87                 _releasing = YES;
88                 [colorWellCells removeObject:self];
89                 if( ! [colorWellCells count] ) {
90                         [colorWellCells autorelease];
91                         colorWellCells = nil;
92                 }
93         }
94
95         [super release];
96 }
97
98 - (void) dealloc {
99         [_color release];
100         _color = nil;
101
102         [super dealloc];
103 }
104
105 #pragma mark -
106
107 - (void) drawWithFrame:(NSRect) cellFrame inView:(NSView *) controlView {
108         NSRect rect = NSInsetRect( cellFrame, 3., 3. );
109         rect.size.width = ( rect.size.height * 1.5 );
110         [super drawWithFrame:rect inView:controlView];
111 }
112
113 - (void) drawInteriorWithFrame:(NSRect) cellFrame inView:(NSView *) controlView {
114         [super drawInteriorWithFrame:cellFrame inView:controlView];
115         [_color drawSwatchInRect:NSInsetRect( cellFrame, 5., 5. )];
116
117         if( _showsWebValue ) {
118                 NSString *webValue = [_color HTMLAttributeValue];
119                 BOOL highlighted = ( [self isHighlighted] && [[controlView window] firstResponder] == controlView && [[controlView window] isKeyWindow] && [[NSApplication sharedApplication] isActive] );
120                 NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[self font], NSFontAttributeName, ( [self isEnabled] ? ( highlighted ? [NSColor alternateSelectedControlTextColor] : [NSColor controlTextColor] ) : ( highlighted ? [NSColor alternateSelectedControlTextColor] : [[NSColor controlTextColor] colorWithAlphaComponent:0.50] ) ), NSForegroundColorAttributeName, nil];
121                 NSSize stringSize = [webValue sizeWithAttributes:attributes];
122                 float y = NSMinY( cellFrame ) + ( NSHeight( cellFrame ) / 2. ) - ( stringSize.height / 2. );
123                 [webValue drawInRect:NSMakeRect( NSMinX( cellFrame ) + ( NSHeight( cellFrame ) * 1.5 ) + 8., y, NSMaxX( cellFrame ) - ( NSHeight( cellFrame ) * 1.5 ) - 8., stringSize.height ) withAttributes:attributes];
124         }
125 }
126
127 - (BOOL) trackMouse:(NSEvent *) event inRect:(NSRect) cellFrame ofView:(NSView *) controlView untilMouseUp:(BOOL) flag {
128         NSRect rect = NSInsetRect( cellFrame, 3., 3. );
129         rect.size.width = ( rect.size.height * 1.5 );
130         return [super trackMouse:event inRect:rect ofView:controlView untilMouseUp:flag];
131 }
132
133 #pragma mark -
134
135 - (void) deactivate {
136         [super setState:NSOffState];
137         [[self controlView] setNeedsDisplay:YES];
138 }
139
140 - (void) setState:(int) value {
141 // do nothing, we handle this internally
142 }
143
144 - (void) clicked:(id) sender {
145         [super setState:(! [self state])];
146         if( [self state] ) {
147                 BOOL exclusive = ! ( [[[NSApplication sharedApplication] currentEvent] modifierFlags] & NSAlternateKeyMask );
148                 [self activate:exclusive];
149         }
150 }
151
152 - (void) activate:(BOOL) exclusive {
153         if( exclusive ) {
154                 NSEnumerator *enumerator = [colorWellCells objectEnumerator];
155                 JVColorWellCell *cell = nil;
156
157                 while( ( cell = [enumerator nextObject] ) ) {
158                         if( cell != self && [cell isActive] ) [cell deactivate];
159                 }
160         }
161
162         [[NSColorPanel sharedColorPanel] setContinuous:YES];
163         [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
164         [[NSColorPanel sharedColorPanel] setColor:_color];
165         [[NSApplication sharedApplication] orderFrontColorPanel:nil];
166
167         [[self controlView] setNeedsDisplay:YES];
168 }
169
170 - (BOOL) isActive {
171         return [self state];
172 }
173
174 #pragma mark -
175
176 - (void) takeColorFrom:(id) sender {
177         NSParameterAssert( [sender respondsToSelector:@selector( color )] );
178         [self setColor:[sender performSelector:@selector( color )]];
179 }
180
181 - (void) setColor:(NSColor *) color {
182         if( ! color || [_color isEqual:color] ) return;
183
184         [_color autorelease];
185         _color = [color retain];
186
187         if( [self isActive] )
188                 [[NSColorPanel sharedColorPanel] setColor:_color];
189
190         [[self controlView] setNeedsDisplay:YES];
191 }
192
193 - (NSColor *) color {
194         return _color;
195 }
196
197 #pragma mark -
198
199 - (void) setTarget:(id) object {
200         [NSException raise:NSIllegalSelectorException format:@"JVColorWellCell does not implement setTarget:"];
201 }
202
203 - (void) setAction:(SEL) action {
204         [NSException raise:NSIllegalSelectorException format:@"JVColorWellCell does not implement setAction:"];
205 }
206
207 #pragma mark -
208
209 - (BOOL) hasValidObjectValue {
210         return YES;
211 }
212
213 - (id) objectValue {
214         return _color;
215 }
216
217 - (void) setObjectValue:(id <NSCopying>) obj {
218         if( [(NSObject *)obj isKindOfClass:[NSColor class]] ) {
219                 [self setColor:(NSColor *)obj];
220         } else if( [(NSObject *)obj isKindOfClass:[NSString class]] ) {
221                 [self setStringValue:(NSString *)obj];
222         }
223 }
224
225 #pragma mark -
226
227 - (NSString *) stringValue {
228         return [_color HTMLAttributeValue];
229 }
230
231 - (void) setStringValue:(NSString *) string {
232         [self setColor:[NSColor colorWithCSSAttributeValue:string]];
233 }
234
235 #pragma mark -
236
237 - (void) setShowsWebValue:(BOOL) web {
238         _showsWebValue = web;
239 }
240
241 - (BOOL) showsWebValue {
242         return _showsWebValue;
243 }
244 @end
Note: See TracBrowser for help on using the browser.