00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPString.j>
00024
00025 @import "CPView.j"
00026 @import "CPColor.j"
00027 @import "CPColorPanel.j"
00028
00029
00030 var _CPColorWellDidBecomeExclusiveNotification = @"_CPColorWellDidBecomeExclusiveNotification";
00031
00040 @implementation CPColorWell : CPControl
00041 {
00042 BOOL _active;
00043 BOOL _bordered;
00044
00045 CPColor _color;
00046 CPView _wellView;
00047 }
00048
00049 - (id)initWithFrame:(CGRect)aFrame
00050 {
00051 self = [super initWithFrame:aFrame];
00052
00053 if (self)
00054 {
00055 _active = NO;
00056 _bordered = YES;
00057 _color = [CPColor whiteColor];
00058
00059 [self drawBezelWithHighlight:NO];
00060 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00061
00062 [self _registerForNotifications];
00063 }
00064
00065 return self;
00066 }
00067
00068 - (void)_registerForNotifications
00069 {
00070 var defaultCenter = [CPNotificationCenter defaultCenter];
00071
00072 [defaultCenter
00073 addObserver:self
00074 selector:@selector(colorWellDidBecomeExclusive:)
00075 name:_CPColorWellDidBecomeExclusiveNotification
00076 object:nil];
00077
00078 [defaultCenter
00079 addObserver:self
00080 selector:@selector(colorPanelWillClose:)
00081 name:CPWindowWillCloseNotification
00082 object:[CPColorPanel sharedColorPanel]];
00083 }
00084
00088 - (BOOL)isBordered
00089 {
00090 return _bordered;
00091 }
00092
00096 - (void)setBordered:(BOOL)bordered
00097 {
00098 if (_bordered == bordered)
00099 return;
00100
00101 _bordered = bordered;
00102
00103 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00104 }
00105
00106
00107
00111 - (CPColor)color
00112 {
00113 return _color;
00114 }
00115
00119 - (void)setColor:(CPColor)aColor
00120 {
00121 if (_color == aColor)
00122 return;
00123
00124 _color = aColor;
00125
00126 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00127 }
00128
00133 - (void)takeColorFrom:(id)aSender
00134 {
00135 [self setColor:[aSender color]];
00136 }
00137
00138
00144 - (void)activate:(BOOL)shouldBeExclusive
00145 {
00146 if (shouldBeExclusive)
00147
00148 [[CPNotificationCenter defaultCenter]
00149 postNotificationName:_CPColorWellDidBecomeExclusiveNotification
00150 object:self];
00151
00152
00153 if ([self isActive])
00154 return;
00155
00156 _active = YES;
00157
00158 [[CPNotificationCenter defaultCenter]
00159 addObserver:self
00160 selector:@selector(colorPanelDidChangeColor:)
00161 name:CPColorPanelColorDidChangeNotification
00162 object:[CPColorPanel sharedColorPanel]];
00163 }
00164
00168 - (void)deactivate
00169 {
00170 if (![self isActive])
00171 return;
00172
00173 _active = NO;
00174
00175 [[CPNotificationCenter defaultCenter]
00176 removeObserver:self
00177 name:CPColorPanelColorDidChangeNotification
00178 object:[CPColorPanel sharedColorPanel]];
00179 }
00180
00184 - (BOOL)isActive
00185 {
00186 return _active;
00187 }
00188
00189
00190
00191 - (void)drawBezelWithHighlight:(BOOL)shouldHighlight
00192 {
00193 }
00194
00199 - (void)drawWellInside:(CGRect)aRect
00200 {
00201 if (!_wellView)
00202 {
00203 _wellView = [[CPView alloc] initWithFrame:aRect];
00204 [_wellView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
00205
00206 [self addSubview:_wellView];
00207 }
00208 else
00209 [_wellView setFrame:aRect];
00210
00211 [_wellView setBackgroundColor:_color];
00212 }
00213
00214 - (void)colorPanelDidChangeColor:(CPNotification)aNotification
00215 {
00216 [self takeColorFrom:[aNotification object]];
00217
00218 [self sendAction:[self action] to:[self target]];
00219 }
00220
00221 - (void)colorWellDidBecomeExclusive:(CPNotification)aNotification
00222 {
00223 if (self != [aNotification object])
00224 [self deactivate];
00225 }
00226
00227 - (void)colorPanelWillClose:(CPNotification)aNotification
00228 {
00229 [self deactivate];
00230 }
00231
00232 - (void)mouseDown:(CPEvent)anEvent
00233 {
00234 [self drawBezelWithHighlight:YES];
00235 }
00236
00237 - (void)mouseDragged:(CPEvent)anEvent
00238 {
00239 [self drawBezelWithHighlight:CGRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil])];
00240 }
00241
00242 -(void)mouseUp:(CPEvent)anEvent
00243 {
00244 [self drawBezelWithHighlight:NO];
00245
00246 if (!CGRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]))
00247 return;
00248
00249 [self activate:YES];
00250
00251 var colorPanel = [CPColorPanel sharedColorPanel];
00252
00253 [colorPanel setColor:_color];
00254 [colorPanel orderFront:self];
00255 }
00256
00257 @end
00258
00259 var CPColorWellColorKey = "CPColorWellColorKey",
00260 CPColorWellBorderedKey = "CPColorWellBorderedKey";
00261
00262 @implementation CPColorWell (CPCoding)
00263
00268 - (id)initWithCoder:(CPCoder)aCoder
00269 {
00270 self = [super initWithCoder:aCoder];
00271
00272 if (self)
00273 {
00274 _active = NO;
00275 _bordered = [aCoder decodeObjectForKey:CPColorWellBorderedKey];
00276 _color = [aCoder decodeObjectForKey:CPColorWellColorKey];
00277
00278 [self drawBezelWithHighlight:NO];
00279 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00280
00281 [self _registerForNotifications];
00282 }
00283
00284 return self;
00285 }
00286
00291 - (void)encodeWithCoder:(CPCoder)aCoder
00292 {
00293
00294
00295 var actualSubviews = _subviews;
00296
00297 _subviews = [_subviews copy];
00298 [_subviews removeObjectIdenticalTo:_wellView];
00299
00300 [super encodeWithCoder:aCoder];
00301
00302 _subviews = actualSubviews;
00303
00304 [aCoder encodeObject:_color forKey:CPColorWellColorKey];
00305 [aCoder encodeObject:_bordered forKey:CPColorWellBorderedKey];
00306 }
00307
00308 @end