Skip to content
This repository
Newer
Older
100644 392 lines (308 sloc) 10.786 kb
a97bb728 »
2008-09-04 Initial commit.
1 /*
2 * CPColorWell.j
3 * AppKit
4 *
5 * Created by Ross Boucher.
6 * Copyright 2008, 280 North, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
5144a5a5 »
2008-10-26 Added @accessor support, @import, and fixed spacing bug with @selector.
23 @import <Foundation/CPString.j>
a97bb728 »
2008-09-04 Initial commit.
24
5144a5a5 »
2008-10-26 Added @accessor support, @import, and fixed spacing bug with @selector.
25 @import "CPView.j"
26 @import "CPColor.j"
27 @import "CPColorPanel.j"
a97bb728 »
2008-09-04 Initial commit.
28
29
30 var _CPColorWellDidBecomeExclusiveNotification = @"_CPColorWellDidBecomeExclusiveNotification";
31
db8e132e »
2010-10-09 Whitespace and style fixes.
32 /*!
850125b8 »
2009-05-18 Add documentation groups
33 @ingroup appkit
34 @class CPColorWell
f228934e »
2008-10-12 Initial conversion to Doxygen style comments.
35
d92fc8c4 »
2008-12-10 Cleaned up old deprecated documentation stuff and added documentation…
36 CPColorWell is a CPControl for selecting and displaying a single color value. An example of a CPColorWell object (or simply color well) is found in CPColorPanel, which uses a color well to display the current color selection.</p>
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
37
377f331b »
2009-07-28 Documentation Edits: Replaced `<code>` and `<pre>` tags with `\c`; th…
38 <p>An application can have one or more active CPColorWells. You can activate multiple CPColorWells by invoking the \c -activate: method with \c NO as its argument. When a mouse-down event occurs on an CPColorWell's border, it becomes the only active color well. When a color well becomes active, it brings up the color panel also.
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
39 */
a97bb728 »
2008-09-04 Initial commit.
40 @implementation CPColorWell : CPControl
41 {
42 BOOL _active;
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
43 BOOL _bordered;
db8e132e »
2010-10-09 Whitespace and style fixes.
44
a97bb728 »
2008-09-04 Initial commit.
45 CPColor _color;
46 }
47
697a7b30 »
2012-01-28 CPColorWell : Implements CPValueBinding (color), supports markers.
48 + (Class)_binderClassForBinding:(CPString)theBinding
49 {
50 if (theBinding == CPValueBinding)
51 return [CPColorWellValueBinder class];
52
53 return [super _binderClassForBinding:theBinding];
54 }
55
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
56 + (CPString)defaultThemeClass
57 {
58 return @"colorwell";
59 }
60
61 + (id)themeAttributes
62 {
63 return [CPDictionary dictionaryWithObjects:[_CGInsetMakeZero(), [CPNull null], _CGInsetMake(3.0, 3.0, 3.0, 3.0), _CGInsetMakeZero(), [CPNull null]]
64 forKeys:[@"bezel-inset", @"bezel-color", @"content-inset", @"content-border-inset", @"content-border-color"]];
65 }
66
697a7b30 »
2012-01-28 CPColorWell : Implements CPValueBinding (color), supports markers.
67 - (void)_reverseSetBinding
68 {
69 var binderClass = [[self class] _binderClassForBinding:CPValueBinding],
70 theBinding = [binderClass getBinding:CPValueBinding forObject:self];
71
72 [theBinding reverseSetValueFor:@"color"];
73 }
74
a97bb728 »
2008-09-04 Initial commit.
75 - (id)initWithFrame:(CGRect)aFrame
76 {
77 self = [super initWithFrame:aFrame];
db8e132e »
2010-10-09 Whitespace and style fixes.
78
a97bb728 »
2008-09-04 Initial commit.
79 if (self)
80 {
81 _active = NO;
82 _color = [CPColor whiteColor];
b09908e6 »
2012-07-09 Themable CPColorWell with Aristo look when bordered.
83 [self setBordered:YES];
db8e132e »
2010-10-09 Whitespace and style fixes.
84
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
85 [self _registerForNotifications];
a97bb728 »
2008-09-04 Initial commit.
86 }
db8e132e »
2010-10-09 Whitespace and style fixes.
87
a97bb728 »
2008-09-04 Initial commit.
88 return self;
89 }
90
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
91 - (void)_registerForNotifications
92 {
93 var defaultCenter = [CPNotificationCenter defaultCenter];
94
95 [defaultCenter
96 addObserver:self
97 selector:@selector(colorWellDidBecomeExclusive:)
98 name:_CPColorWellDidBecomeExclusiveNotification
99 object:nil];
100
101 [defaultCenter
102 addObserver:self
103 selector:@selector(colorPanelWillClose:)
104 name:CPWindowWillCloseNotification
105 object:[CPColorPanel sharedColorPanel]];
106 }
107
108 /*!
660c98d6 »
2012-07-09 Use theme system for CPColorWell bordered state.
109 Sets whether the color well is bordered.
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
110 */
660c98d6 »
2012-07-09 Use theme system for CPColorWell bordered state.
111 - (void)setBordered:(BOOL)shouldBeBordered
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
112 {
660c98d6 »
2012-07-09 Use theme system for CPColorWell bordered state.
113 if (shouldBeBordered)
114 [self setThemeState:CPThemeStateBordered];
115 else
116 [self unsetThemeState:CPThemeStateBordered];
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
117 }
118
119 /*!
660c98d6 »
2012-07-09 Use theme system for CPColorWell bordered state.
120 Returns whether the color well is bordered
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
121 */
660c98d6 »
2012-07-09 Use theme system for CPColorWell bordered state.
122 - (BOOL)isBordered
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
123 {
660c98d6 »
2012-07-09 Use theme system for CPColorWell bordered state.
124 return [self hasThemeState:CPThemeStateBordered];
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
125 }
126
a97bb728 »
2008-09-04 Initial commit.
127 // Managing Color From Color Wells
128
f228934e »
2008-10-12 Initial conversion to Doxygen style comments.
129 /*!
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
130 Returns the color well's current color.
131 */
a97bb728 »
2008-09-04 Initial commit.
132 - (CPColor)color
133 {
134 return _color;
135 }
136
f228934e »
2008-10-12 Initial conversion to Doxygen style comments.
137 /*!
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
138 Sets the color well's current color.
139 */
a97bb728 »
2008-09-04 Initial commit.
140 - (void)setColor:(CPColor)aColor
141 {
142 if (_color == aColor)
143 return;
db8e132e »
2010-10-09 Whitespace and style fixes.
144
a97bb728 »
2008-09-04 Initial commit.
145 _color = aColor;
db8e132e »
2010-10-09 Whitespace and style fixes.
146
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
147 [self setNeedsLayout];
a97bb728 »
2008-09-04 Initial commit.
148 }
149
f228934e »
2008-10-12 Initial conversion to Doxygen style comments.
150 /*!
377f331b »
2009-07-28 Documentation Edits: Replaced `<code>` and `<pre>` tags with `\c`; th…
151 Changes the color of the well to that of \c aSender.
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
152 @param aSender the object from which to retrieve the color
153 */
a97bb728 »
2008-09-04 Initial commit.
154 - (void)takeColorFrom:(id)aSender
155 {
156 [self setColor:[aSender color]];
157 }
158
159 // Activating and Deactivating Color Wells
f228934e »
2008-10-12 Initial conversion to Doxygen style comments.
160 /*!
db8e132e »
2010-10-09 Whitespace and style fixes.
161 Activates the color well, displays the color panel, and makes the panel's current color the same as its own.
377f331b »
2009-07-28 Documentation Edits: Replaced `<code>` and `<pre>` tags with `\c`; th…
162 If exclusive is \c YES, deactivates any other CPColorWells. \c NO, keeps them active.
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
163 @param shouldBeExclusive whether other color wells should be deactivated.
164 */
a97bb728 »
2008-09-04 Initial commit.
165 - (void)activate:(BOOL)shouldBeExclusive
166 {
167 if (shouldBeExclusive)
168 // FIXME: make this queue!
169 [[CPNotificationCenter defaultCenter]
170 postNotificationName:_CPColorWellDidBecomeExclusiveNotification
171 object:self];
172
173
174 if ([self isActive])
175 return;
db8e132e »
2010-10-09 Whitespace and style fixes.
176
a97bb728 »
2008-09-04 Initial commit.
177 _active = YES;
db8e132e »
2010-10-09 Whitespace and style fixes.
178
a97bb728 »
2008-09-04 Initial commit.
179 [[CPNotificationCenter defaultCenter]
180 addObserver:self
181 selector:@selector(colorPanelDidChangeColor:)
182 name:CPColorPanelColorDidChangeNotification
183 object:[CPColorPanel sharedColorPanel]];
184 }
185
f228934e »
2008-10-12 Initial conversion to Doxygen style comments.
186 /*!
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
187 Deactivates the color well.
188 */
a97bb728 »
2008-09-04 Initial commit.
189 - (void)deactivate
190 {
191 if (![self isActive])
192 return;
db8e132e »
2010-10-09 Whitespace and style fixes.
193
a97bb728 »
2008-09-04 Initial commit.
194 _active = NO;
db8e132e »
2010-10-09 Whitespace and style fixes.
195
a97bb728 »
2008-09-04 Initial commit.
196 [[CPNotificationCenter defaultCenter]
197 removeObserver:self
198 name:CPColorPanelColorDidChangeNotification
199 object:[CPColorPanel sharedColorPanel]];
200 }
201
f228934e »
2008-10-12 Initial conversion to Doxygen style comments.
202 /*!
377f331b »
2009-07-28 Documentation Edits: Replaced `<code>` and `<pre>` tags with `\c`; th…
203 Returns \c YES if the color well is active.
a40fbd79 »
2008-09-10 Add the first draft of documentation inline.
204 */
a97bb728 »
2008-09-04 Initial commit.
205 - (BOOL)isActive
206 {
207 return _active;
208 }
209
210 - (void)colorPanelDidChangeColor:(CPNotification)aNotification
211 {
212 [self takeColorFrom:[aNotification object]];
db8e132e »
2010-10-09 Whitespace and style fixes.
213
a97bb728 »
2008-09-04 Initial commit.
214 [self sendAction:[self action] to:[self target]];
215 }
216
217 - (void)colorWellDidBecomeExclusive:(CPNotification)aNotification
218 {
219 if (self != [aNotification object])
220 [self deactivate];
221 }
222
223 - (void)colorPanelWillClose:(CPNotification)aNotification
224 {
225 [self deactivate];
226 }
227
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
228 - (void)stopTracking:(CGPoint)lastPoint at:(CGPoint)aPoint mouseIsUp:(BOOL)mouseIsUp
a97bb728 »
2008-09-04 Initial commit.
229 {
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
230 [self highlight:NO];
231
232 if (!mouseIsUp || !CGRectContainsPoint([self bounds], aPoint) || ![self isEnabled])
83407918 »
2010-04-23 Don't let color wells work if they are disabled.
233 return;
234
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
235 [self activate:YES];
a97bb728 »
2008-09-04 Initial commit.
236
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
237 var colorPanel = [CPColorPanel sharedColorPanel];
83407918 »
2010-04-23 Don't let color wells work if they are disabled.
238
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
239 [colorPanel setColor:_color];
240 [colorPanel orderFront:self];
a97bb728 »
2008-09-04 Initial commit.
241 }
242
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
243 - (CGRect)contentRectForBounds:(CGRect)bounds
a97bb728 »
2008-09-04 Initial commit.
244 {
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
245 var contentInset = [self currentValueForThemeAttribute:@"content-inset"];
a97bb728 »
2008-09-04 Initial commit.
246
f398185a »
2012-07-09 CGRectInsetByInset geometry function.
247 return _CGRectInsetByInset(bounds, contentInset);
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
248 }
db8e132e »
2010-10-09 Whitespace and style fixes.
249
b09908e6 »
2012-07-09 Themable CPColorWell with Aristo look when bordered.
250 - (CGRect)bezelRectForBounds:(CGRect)bounds
251 {
252 var bezelInset = [self currentValueForThemeAttribute:@"bezel-inset"];
253
254 return _CGRectInsetByInset(bounds, bezelInset);
255 }
256
257 - (CGRect)contentBorderRectForBounds:(CGRect)bounds
258 {
259 var contentBorderInset = [self currentValueForThemeAttribute:@"content-border-inset"];
260
261 return _CGRectInsetByInset(bounds, contentBorderInset);
262 }
263
264 - (CGRect)rectForEphemeralSubviewNamed:(CPString)aName
265 {
266 switch (aName)
267 {
268 case "bezel-view":
269 return [self bezelRectForBounds:[self bounds]];
270 case "content-view":
271 return [self contentRectForBounds:[self bounds]];
272 case "content-border-view":
273 return [self contentBorderRectForBounds:[self bounds]];
274 }
275
276 return [super rectForEphemeralSubviewNamed:aName];
277 }
278
279 - (CPView)createEphemeralSubviewNamed:(CPString)aName
280 {
281 var view = [[CPView alloc] initWithFrame:_CGRectMakeZero()];
282
283 [view setHitTests:NO];
284
285 return view;
286 }
287
4390bea3 »
2012-07-09 Simplify CPColorWell drawing and mouse handling.
288 - (void)layoutSubviews
289 {
b09908e6 »
2012-07-09 Themable CPColorWell with Aristo look when bordered.
290 var bezelView = [self layoutEphemeralSubviewNamed:@"bezel-view"
291 positioned:CPWindowBelow
292 relativeToEphemeralSubviewNamed:@"content-view"];
293
294 [bezelView setBackgroundColor:[self currentValueForThemeAttribute:@"bezel-color"]];
295
296 var contentView = [self layoutEphemeralSubviewNamed:@"content-view"
297 positioned:CPWindowAbove
298 relativeToEphemeralSubviewNamed:@"bezel-view"];
299
300
301 [contentView setBackgroundColor:_color];
302
303 var contentBorderView = [self layoutEphemeralSubviewNamed:@"content-border-view"
304 positioned:CPWindowAbove
305 relativeToEphemeralSubviewNamed:@"content-view"];
306
307 [contentBorderView setBackgroundColor:[self currentValueForThemeAttribute:@"content-border-color"]];
a97bb728 »
2008-09-04 Initial commit.
308 }
309
310 @end
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
311
697a7b30 »
2012-01-28 CPColorWell : Implements CPValueBinding (color), supports markers.
312 @implementation CPColorWellValueBinder : CPBinder
313 {
314 }
315
316 - (void)_updatePlaceholdersWithOptions:(CPDictionary)options
317 {
318 var placeholderColor = [CPColor blueColor];
319
320 [self _setPlaceholder:placeholderColor forMarker:CPMultipleValuesMarker isDefault:YES];
321 [self _setPlaceholder:placeholderColor forMarker:CPNoSelectionMarker isDefault:YES];
322 [self _setPlaceholder:placeholderColor forMarker:CPNotApplicableMarker isDefault:YES];
323 [self _setPlaceholder:placeholderColor forMarker:CPNullMarker isDefault:YES];
324 }
325
326 - (void)setValueFor:(CPString)theBinding
327 {
328 var destination = [_info objectForKey:CPObservedObjectKey],
329 keyPath = [_info objectForKey:CPObservedKeyPathKey],
330 options = [_info objectForKey:CPOptionsKey],
331 newValue = [destination valueForKeyPath:keyPath],
332 isPlaceholder = CPIsControllerMarker(newValue);
333
334 if (isPlaceholder)
335 {
336 if (newValue === CPNotApplicableMarker && [options objectForKey:CPRaisesForNotApplicableKeysBindingOption])
337 {
338 [CPException raise:CPGenericException
339 reason:@"can't transform non applicable key on: " + _source + " value: " + newValue];
340 }
341
342 newValue = [self _placeholderForMarker:newValue];
343 }
344 else
345 {
346 newValue = [self transformValue:newValue withOptions:options];
347 }
85c88d7c »
2012-03-13 Whitespace.
348
697a7b30 »
2012-01-28 CPColorWell : Implements CPValueBinding (color), supports markers.
349 [_source setColor:newValue];
350 }
351
352 @end
353
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
354 var CPColorWellColorKey = "CPColorWellColorKey",
355 CPColorWellBorderedKey = "CPColorWellBorderedKey";
356
357 @implementation CPColorWell (CPCoding)
358
359 /*!
377f331b »
2009-07-28 Documentation Edits: Replaced `<code>` and `<pre>` tags with `\c`; th…
360 Initializes the color well by unarchiving data from \c aCoder.
d92fc8c4 »
2008-12-10 Cleaned up old deprecated documentation stuff and added documentation…
361 @param aCoder the coder containing the archived CPColorWell.
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
362 */
363 - (id)initWithCoder:(CPCoder)aCoder
364 {
365 self = [super initWithCoder:aCoder];
db8e132e »
2010-10-09 Whitespace and style fixes.
366
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
367 if (self)
368 {
369 _active = NO;
370 _color = [aCoder decodeObjectForKey:CPColorWellColorKey];
b09908e6 »
2012-07-09 Themable CPColorWell with Aristo look when bordered.
371 [self setBordered:[aCoder decodeBoolForKey:CPColorWellBorderedKey]];
c0aa78eb »
2008-12-04 Make CPColorWell resize correctly, and display immediately upon unarc…
372
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
373 [self _registerForNotifications];
374 }
db8e132e »
2010-10-09 Whitespace and style fixes.
375
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
376 return self;
377 }
378
379 /*!
380 Archives this button into the provided coder.
381 @param aCoder the coder to which the color well's instance data will be written.
382 */
383 - (void)encodeWithCoder:(CPCoder)aCoder
384 {
385 [super encodeWithCoder:aCoder];
db8e132e »
2010-10-09 Whitespace and style fixes.
386
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
387 [aCoder encodeObject:_color forKey:CPColorWellColorKey];
660c98d6 »
2012-07-09 Use theme system for CPColorWell bordered state.
388 [aCoder encodeObject:[self isBordered] forKey:CPColorWellBorderedKey];
ed5aa5b5 »
2008-10-28 CPColorWell coding and nib2cib support.
389 }
390
391 @end
Something went wrong with that request. Please try again.