API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
CPRadio.j
Go to the documentation of this file.
1 /*
2  * CPRadio.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 
25 
65 
66 @implementation CPRadio : CPButton
67 {
68  CPRadioGroup _radioGroup;
69 }
70 
71 + (id)radioWithTitle:(CPString)aTitle theme:(CPTheme)aTheme
72 {
73  return [self buttonWithTitle:aTitle theme:aTheme];
74 }
75 
76 + (id)radioWithTitle:(CPString)aTitle
77 {
78  return [self buttonWithTitle:aTitle];
79 }
80 
81 + (CPButton)standardButtonWithTitle:(CPString)aTitle
82 {
83  var button = [[CPRadio alloc] init];
84 
85  [button setTitle:aTitle];
86 
87  return button;
88 }
89 
90 + (CPString)defaultThemeClass
91 {
92  return @"radio";
93 }
94 
95 // Designated Initializer
96 - (id)initWithFrame:(CGRect)aFrame radioGroup:(CPRadioGroup)aRadioGroup
97 {
98  self = [super initWithFrame:aFrame];
99 
100  if (self)
101  {
102  [self setRadioGroup:aRadioGroup];
103 
104  [self setHighlightsBy:CPContentsCellMask];
105  [self setShowsStateBy:CPContentsCellMask];
106 
107  // Defaults?
108  [self setImagePosition:CPImageLeft];
109  [self setAlignment:CPLeftTextAlignment];
110 
111  [self setBordered:YES];
112  }
113 
114  return self;
115 }
116 
117 - (id)initWithFrame:(CGRect)aFrame
118 {
119  return [self initWithFrame:aFrame radioGroup:[CPRadioGroup new]];
120 }
121 
122 - (CPInteger)nextState
123 {
124  return CPOnState;
125 }
126 
127 - (void)setRadioGroup:(CPRadioGroup)aRadioGroup
128 {
129  if (_radioGroup === aRadioGroup)
130  return;
131 
132  [_radioGroup _removeRadio:self];
133  _radioGroup = aRadioGroup;
134  [_radioGroup _addRadio:self];
135 }
136 
137 - (CPRadioGroup)radioGroup
138 {
139  return _radioGroup;
140 }
141 
142 - (void)setObjectValue:(id)aValue
143 {
144  [super setObjectValue:aValue];
145 
146  if ([self state] === CPOnState)
147  [_radioGroup _setSelectedRadio:self];
148 }
149 
150 - (void)sendAction:(SEL)anAction to:(id)anObject
151 {
152  [super sendAction:anAction to:anObject];
153 
154  if (_radioGroup)
155  [CPApp sendAction:[_radioGroup action] to:[_radioGroup target] from:_radioGroup];
156 }
157 
158 @end
159 
160 var CPRadioRadioGroupKey = @"CPRadioRadioGroupKey";
161 
162 @implementation CPRadio (CPCoding)
163 
164 - (id)initWithCoder:(CPCoder)aCoder
165 {
166  self = [super initWithCoder:aCoder];
167 
168  if (self)
169  _radioGroup = [aCoder decodeObjectForKey:CPRadioRadioGroupKey];
170 
171  return self;
172 }
173 
174 - (void)encodeWithCoder:(CPCoder)aCoder
175 {
176  [super encodeWithCoder:aCoder];
177 
178  [aCoder encodeObject:_radioGroup forKey:CPRadioRadioGroupKey];
179 }
180 
181 - (CPImage)image
182 {
183  return [self currentValueForThemeAttribute:@"image"];
184 }
185 
186 - (CPImage)alternateImage
187 {
188  return [self currentValueForThemeAttribute:@"image"];
189 }
190 
191 - (BOOL)startTrackingAt:(CGPoint)aPoint
192 {
193  var startedTracking = [super startTrackingAt:aPoint];
194  [self highlight:YES];
195  return startedTracking;
196 }
197 
198 @end
199 
200 @implementation CPRadioGroup : CPObject
201 {
202  CPSet _radios;
203  CPRadio _selectedRadio;
204 
205  id _target;
206  SEL _action;
207 }
208 
209 - (id)init
210 {
211  self = [super init];
212 
213  if (self)
214  {
215  _radios = [CPSet set];
216  _selectedRadio = nil;
217  }
218 
219  return self;
220 }
221 
222 - (void)_addRadio:(CPRadio)aRadio
223 {
224  [_radios addObject:aRadio];
225 
226  if ([aRadio state] === CPOnState)
227  [self _setSelectedRadio:aRadio];
228 }
229 
230 - (void)_removeRadio:(CPRadio)aRadio
231 {
232  if (_selectedRadio === aRadio)
233  _selectedRadio = nil;
234 
235  [_radios removeObject:aRadio];
236 }
237 
238 - (void)_setSelectedRadio:(CPRadio)aRadio
239 {
240  if (_selectedRadio === aRadio)
241  return;
242 
243  [_selectedRadio setState:CPOffState];
244  _selectedRadio = aRadio;
245 }
246 
247 - (CPRadio)selectedRadio
248 {
249  return _selectedRadio;
250 }
251 
252 - (CPArray)radios
253 {
254  return [_radios allObjects];
255 }
256 
257 @end
258 
259 var CPRadioGroupRadiosKey = @"CPRadioGroupRadiosKey",
260  CPRadioGroupSelectedRadioKey = @"CPRadioGroupSelectedRadioKey";
261 
262 @implementation CPRadioGroup (CPCoding)
263 
264 - (id)initWithCoder:(CPCoder)aCoder
265 {
266  self = [super init];
267 
268  if (self)
269  {
270  _radios = [aCoder decodeObjectForKey:CPRadioGroupRadiosKey];
271  _selectedRadio = [aCoder decodeObjectForKey:CPRadioGroupSelectedRadioKey];
272  }
273 
274  return self;
275 }
276 
277 - (void)encodeWithCoder:(CPCoder)aCoder
278 {
279  [aCoder encodeObject:_radios forKey:CPRadioGroupRadiosKey];
280  [aCoder encodeObject:_selectedRadio forKey:CPRadioGroupSelectedRadioKey];
281 }
282 
283 @end
284 
286 
290 - (id)target
291 {
292  return _target;
293 }
294 
298 - (void)setTarget:(id)aValue
299 {
300  _target = aValue;
301 }
302 
306 - (SEL)action
307 {
308  return _action;
309 }
310 
314 - (void)setAction:(SEL)aValue
315 {
316  _action = aValue;
317 }
318 
319 @end