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
00038 @implementation CPColorWell : CPControl
00039 {
00040 BOOL _active;
00041 BOOL _bordered;
00042
00043 CPColor _color;
00044 CPView _wellView;
00045 }
00046
00047 - (id)initWithFrame:(CGRect)aFrame
00048 {
00049 self = [super initWithFrame:aFrame];
00050
00051 if (self)
00052 {
00053 _active = NO;
00054 _bordered = YES;
00055 _color = [CPColor whiteColor];
00056
00057 [self drawBezelWithHighlight:NO];
00058 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00059
00060 [self _registerForNotifications];
00061 }
00062
00063 return self;
00064 }
00065
00066 - (void)_registerForNotifications
00067 {
00068 var defaultCenter = [CPNotificationCenter defaultCenter];
00069
00070 [defaultCenter
00071 addObserver:self
00072 selector:@selector(colorWellDidBecomeExclusive:)
00073 name:_CPColorWellDidBecomeExclusiveNotification
00074 object:nil];
00075
00076 [defaultCenter
00077 addObserver:self
00078 selector:@selector(colorPanelWillClose:)
00079 name:CPWindowWillCloseNotification
00080 object:[CPColorPanel sharedColorPanel]];
00081 }
00082
00086 - (BOOL)isBordered
00087 {
00088 return _bordered;
00089 }
00090
00094 - (void)setBordered:(BOOL)bordered
00095 {
00096 if (_bordered == bordered)
00097 return;
00098
00099 _bordered = bordered;
00100
00101 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00102 }
00103
00104
00105
00109 - (CPColor)color
00110 {
00111 return _color;
00112 }
00113
00117 - (void)setColor:(CPColor)aColor
00118 {
00119 if (_color == aColor)
00120 return;
00121
00122 _color = aColor;
00123
00124 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00125 }
00126
00131 - (void)takeColorFrom:(id)aSender
00132 {
00133 [self setColor:[aSender color]];
00134 }
00135
00136
00142 - (void)activate:(BOOL)shouldBeExclusive
00143 {
00144 if (shouldBeExclusive)
00145
00146 [[CPNotificationCenter defaultCenter]
00147 postNotificationName:_CPColorWellDidBecomeExclusiveNotification
00148 object:self];
00149
00150
00151 if ([self isActive])
00152 return;
00153
00154 _active = YES;
00155
00156 [[CPNotificationCenter defaultCenter]
00157 addObserver:self
00158 selector:@selector(colorPanelDidChangeColor:)
00159 name:CPColorPanelColorDidChangeNotification
00160 object:[CPColorPanel sharedColorPanel]];
00161 }
00162
00166 - (void)deactivate
00167 {
00168 if (![self isActive])
00169 return;
00170
00171 _active = NO;
00172
00173 [[CPNotificationCenter defaultCenter]
00174 removeObserver:self
00175 name:CPColorPanelColorDidChangeNotification
00176 object:[CPColorPanel sharedColorPanel]];
00177 }
00178
00182 - (BOOL)isActive
00183 {
00184 return _active;
00185 }
00186
00187
00188
00189 - (void)drawBezelWithHighlight:(BOOL)shouldHighlight
00190 {
00191 }
00192
00197 - (void)drawWellInside:(CGRect)aRect
00198 {
00199 if (!_wellView)
00200 {
00201 _wellView = [[CPView alloc] initWithFrame:aRect];
00202 [_wellView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
00203
00204 [self addSubview:_wellView];
00205 }
00206 else
00207 [_wellView setFrame:aRect];
00208
00209 [_wellView setBackgroundColor:_color];
00210 }
00211
00212 - (void)colorPanelDidChangeColor:(CPNotification)aNotification
00213 {
00214 [self takeColorFrom:[aNotification object]];
00215
00216 [self sendAction:[self action] to:[self target]];
00217 }
00218
00219 - (void)colorWellDidBecomeExclusive:(CPNotification)aNotification
00220 {
00221 if (self != [aNotification object])
00222 [self deactivate];
00223 }
00224
00225 - (void)colorPanelWillClose:(CPNotification)aNotification
00226 {
00227 [self deactivate];
00228 }
00229
00230 - (void)mouseDown:(CPEvent)anEvent
00231 {
00232 [self drawBezelWithHighlight:YES];
00233 }
00234
00235 - (void)mouseDragged:(CPEvent)anEvent
00236 {
00237 [self drawBezelWithHighlight:CGRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil])];
00238 }
00239
00240 -(void)mouseUp:(CPEvent)anEvent
00241 {
00242 [self drawBezelWithHighlight:NO];
00243
00244 if (!CGRectContainsPoint([self bounds], [self convertPoint:[anEvent locationInWindow] fromView:nil]))
00245 return;
00246
00247 [self activate:YES];
00248
00249 var colorPanel = [CPColorPanel sharedColorPanel];
00250
00251 [colorPanel setColor:_color];
00252
00253 [colorPanel orderFront:self];
00254 }
00255
00256 @end
00257
00258 var CPColorWellColorKey = "CPColorWellColorKey",
00259 CPColorWellBorderedKey = "CPColorWellBorderedKey";
00260
00261 @implementation CPColorWell (CPCoding)
00262
00267 - (id)initWithCoder:(CPCoder)aCoder
00268 {
00269 self = [super initWithCoder:aCoder];
00270
00271 if (self)
00272 {
00273 _active = NO;
00274 _bordered = [aCoder decodeObjectForKey:CPColorWellBorderedKey];
00275 _color = [aCoder decodeObjectForKey:CPColorWellColorKey];
00276
00277 [self drawBezelWithHighlight:NO];
00278 [self drawWellInside:CGRectInset([self bounds], 3.0, 3.0)];
00279
00280 [self _registerForNotifications];
00281 }
00282
00283 return self;
00284 }
00285
00290 - (void)encodeWithCoder:(CPCoder)aCoder
00291 {
00292
00293
00294 var actualSubviews = _subviews;
00295
00296 _subviews = [_subviews copy];
00297 [_subviews removeObjectIdenticalTo:_wellView];
00298
00299 [super encodeWithCoder:aCoder];
00300
00301 _subviews = actualSubviews;
00302
00303 [aCoder encodeObject:_color forKey:CPColorWellColorKey];
00304 [aCoder encodeObject:_bordered forKey:CPColorWellBorderedKey];
00305 }
00306
00307 @end