API  0.9.10
CPCheckBox.j
Go to the documentation of this file.
1 /*
2  * CPCheckBox.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2009, 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 
23 
24 
26 @implementation CPCheckBox : CPButton
27 {
28  id __doxygen__;
29 }
30 
31 + (id)checkBoxWithTitle:(CPString)aTitle theme:(CPTheme)aTheme
32 {
33  return [self buttonWithTitle:aTitle theme:aTheme];
34 }
35 
36 + (id)checkBoxWithTitle:(CPString)aTitle
37 {
38  return [self buttonWithTitle:aTitle];
39 }
40 
42 {
43  return @"check-box";
44 }
45 
46 + (Class)_binderClassForBinding:(CPString)aBinding
47 {
48  if (aBinding === CPValueBinding)
49  return [_CPCheckBoxValueBinder class];
50 
51  return [super _binderClassForBinding:aBinding];
52 }
53 
54 - (id)initWithFrame:(CGRect)aFrame
55 {
56  self = [super initWithFrame:aFrame];
57 
58  if (self)
59  {
60  [self setHighlightsBy:CPContentsCellMask];
61  [self setShowsStateBy:CPContentsCellMask];
62 
63  // Defaults?
64  [self setImagePosition:CPImageLeft];
65  [self setAlignment:CPLeftTextAlignment];
66 
67  [self setBordered:NO];
68  }
69 
70  return self;
71 }
72 
73 - (void)takeStateFromKeyPath:(CPString)aKeyPath ofObjects:(CPArray)objects
74 {
75  var count = objects.length,
76  value = [objects[0] valueForKeyPath:aKeyPath] ? CPOnState : CPOffState;
77 
78  [self setAllowsMixedState:NO];
79  [self setState:value];
80 
81  while (count-- > 1)
82  {
83  if (value !== ([objects[count] valueForKeyPath:aKeyPath] ? CPOnState : CPOffState))
84  {
85  [self setAllowsMixedState:YES];
86  [self setState:CPMixedState];
87  }
88  }
89 }
90 
91 - (void)takeValueFromKeyPath:(CPString)aKeyPath ofObjects:(CPArray)objects
92 {
93  [self takeStateFromKeyPath:aKeyPath ofObjects:objects];
94 }
95 
97 {
98  return [self currentValueForThemeAttribute:@"image"];
99 }
100 
102 {
103  return [self currentValueForThemeAttribute:@"image"];
104 }
105 
106 - (BOOL)startTrackingAt:(CGPoint)aPoint
107 {
108  var startedTracking = [super startTrackingAt:aPoint];
109  [self highlight:YES];
110  return startedTracking;
111 }
112 
113 
114 #pragma mark -
115 #pragma mark Override methods from CPButton
116 
117 - (CGSize)_minimumFrameSize
118 {
119  var size = [super _minimumFrameSize],
120  contentView = [self ephemeralSubviewNamed:@"content-view"];
121 
122  if (!contentView && [[self title] length])
123  {
124  var minSize = [self currentValueForThemeAttribute:@"min-size"],
125  maxSize = [self currentValueForThemeAttribute:@"max-size"];
126 
127  // Here we always add the min size to the control which is the size of the view of the checkBox
128  size.width += minSize.width + CPCheckBoxImageOffset;
129 
130  if (maxSize.width >= 0.0)
131  size.width = MIN(size.width, maxSize.width);
132  }
133 
134  return size;
135 }
136 
137 @end
138 @implementation _CPCheckBoxValueBinder : CPBinder
139 {
140  id __doxygen__;
141 }
142 
143 - (void)_updatePlaceholdersWithOptions:(CPDictionary)options
144 {
145  [super _updatePlaceholdersWithOptions:options];
146 
147  [self _setPlaceholder:CPMixedState forMarker:CPMultipleValuesMarker isDefault:YES];
148  [self _setPlaceholder:CPOffState forMarker:CPNoSelectionMarker isDefault:YES];
149  [self _setPlaceholder:CPOffState forMarker:CPNotApplicableMarker isDefault:YES];
150  [self _setPlaceholder:CPOffState forMarker:CPNullMarker isDefault:YES];
151 }
152 
153 - (void)setPlaceholderValue:(id)aValue withMarker:(CPString)aMarker forBinding:(CPString)aBinding
154 {
155  [_source setAllowsMixedState:(aValue === CPMixedState)];
156  [_source setState:aValue];
157 }
158 
159 - (void)setValue:(id)aValue forBinding:(CPString)aBinding
160 {
161  [_source setState:aValue];
162 }
163 
164 @end
void setBordered:(BOOL shouldBeBordered)
Definition: CPButton.j:768
void setAllowsMixedState:(BOOL aFlag)
Definition: CPButton.j:222
CPString title()
Definition: CPButton.j:351
id initWithFrame:(CGRect aFrame)
Definition: CPButton.j:161
void takeStateFromKeyPath:ofObjects:(CPString aKeyPath, [ofObjects] CPArray objects)
Definition: CPCheckBox.j:73
void setImagePosition:(CPCellImagePosition position)
Definition: CPControl.j:917
id buttonWithTitle:theme:(CPString aTitle, [theme] CPTheme aTheme)
Definition: CPButton.j:129
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPCheckBoxImageOffset
Definition: CPCheckBox.j:25
BOOL startTrackingAt:(CGPoint aPoint)
Definition: CPButton.j:551
CPView ephemeralSubviewNamed:(CPString aViewName)
Definition: CPView.j:3404
An immutable string (collection of characters).
Definition: CPString.h:2
void setState:(CPInteger aState)
Definition: CPButton.j:317
void setHighlightsBy:(CPInteger aMask)
Definition: CPButton.j:433
Definition: CPImage.h:2
int length()
Definition: CPString.j:186
CPImage image()
Definition: CPCheckBox.j:96
void highlight:(BOOL shouldHighlight)
Definition: CPControl.j:980
void setAlignment:(CPTextAlignment alignment)
Definition: CPControl.j:776
void setShowsStateBy:(CPInteger aMask)
Definition: CPButton.j:409
Definition: CPTheme.h:2
CPOffState
Definition: CPControl.j:78
id buttonWithTitle:(CPString aTitle)
Definition: CPButton.j:124
CPValueBinding
CPOnState
Definition: CPControl.j:77
CPImage alternateImage()
Definition: CPCheckBox.j:101
CPString defaultThemeClass()
Definition: CPCheckBox.j:41