API 0.9.5
AppKit/CPRadio.j
Go to the documentation of this file.
00001 /*
00002  * CPRadio.j
00003  * AppKit
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2009, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 
00064 CPRadioImageOffset = 4.0;
00065 
00066 @implementation CPRadio : CPButton
00067 {
00068     CPRadioGroup    _radioGroup;
00069 }
00070 
00071 + (id)radioWithTitle:(CPString)aTitle theme:(CPTheme)aTheme
00072 {
00073     return [self buttonWithTitle:aTitle theme:aTheme];
00074 }
00075 
00076 + (id)radioWithTitle:(CPString)aTitle
00077 {
00078     return [self buttonWithTitle:aTitle];
00079 }
00080 
00081 + (CPButton)standardButtonWithTitle:(CPString)aTitle
00082 {
00083     var button = [[CPRadio alloc] init];
00084 
00085     [button setTitle:aTitle];
00086 
00087     return button;
00088 }
00089 
00090 + (CPString)defaultThemeClass
00091 {
00092     return @"radio";
00093 }
00094 
00095 // Designated Initializer
00096 - (id)initWithFrame:(CGRect)aFrame radioGroup:(CPRadioGroup)aRadioGroup
00097 {
00098     self = [super initWithFrame:aFrame];
00099 
00100     if (self)
00101     {
00102         [self setRadioGroup:aRadioGroup];
00103 
00104         [self setHighlightsBy:CPContentsCellMask];
00105         [self setShowsStateBy:CPContentsCellMask];
00106 
00107         // Defaults?
00108         [self setImagePosition:CPImageLeft];
00109         [self setAlignment:CPLeftTextAlignment];
00110 
00111         [self setBordered:YES];
00112     }
00113 
00114     return self;
00115 }
00116 
00117 - (id)initWithFrame:(CGRect)aFrame
00118 {
00119     return [self initWithFrame:aFrame radioGroup:[CPRadioGroup new]];
00120 }
00121 
00122 - (CPInteger)nextState
00123 {
00124     return CPOnState;
00125 }
00126 
00127 - (void)setRadioGroup:(CPRadioGroup)aRadioGroup
00128 {
00129     if (_radioGroup === aRadioGroup)
00130         return;
00131 
00132     [_radioGroup _removeRadio:self];
00133     _radioGroup = aRadioGroup;
00134     [_radioGroup _addRadio:self];
00135 }
00136 
00137 - (CPRadioGroup)radioGroup
00138 {
00139     return _radioGroup;
00140 }
00141 
00142 - (void)setObjectValue:(id)aValue
00143 {
00144     [super setObjectValue:aValue];
00145 
00146     if ([self state] === CPOnState)
00147         [_radioGroup _setSelectedRadio:self];
00148 }
00149 
00150 - (void)sendAction:(SEL)anAction to:(id)anObject
00151 {
00152     [super sendAction:anAction to:anObject];
00153 
00154     if (_radioGroup)
00155         [CPApp sendAction:[_radioGroup action] to:[_radioGroup target] from:_radioGroup];
00156 }
00157 
00158 @end
00159 
00160 var CPRadioRadioGroupKey    = @"CPRadioRadioGroupKey";
00161 
00162 @implementation CPRadio (CPCoding)
00163 
00164 - (id)initWithCoder:(CPCoder)aCoder
00165 {
00166     self = [super initWithCoder:aCoder];
00167 
00168     if (self)
00169         _radioGroup = [aCoder decodeObjectForKey:CPRadioRadioGroupKey];
00170 
00171     return self;
00172 }
00173 
00174 - (void)encodeWithCoder:(CPCoder)aCoder
00175 {
00176     [super encodeWithCoder:aCoder];
00177 
00178     [aCoder encodeObject:_radioGroup forKey:CPRadioRadioGroupKey];
00179 }
00180 
00181 @end
00182 
00183 @implementation CPRadioGroup : CPObject
00184 {
00185     CPSet   _radios;
00186     CPRadio _selectedRadio;
00187 
00188     id      _target;
00189     SEL     _action;
00190 }
00191 
00192 - (id)init
00193 {
00194     self = [super init];
00195 
00196     if (self)
00197     {
00198         _radios = [CPSet set];
00199         _selectedRadio = nil;
00200     }
00201 
00202     return self;
00203 }
00204 
00205 - (void)_addRadio:(CPRadio)aRadio
00206 {
00207     [_radios addObject:aRadio];
00208 
00209     if ([aRadio state] === CPOnState)
00210         [self _setSelectedRadio:aRadio];
00211 }
00212 
00213 - (void)_removeRadio:(CPRadio)aRadio
00214 {
00215     if (_selectedRadio === aRadio)
00216         _selectedRadio = nil;
00217 
00218     [_radios removeObject:aRadio];
00219 }
00220 
00221 - (void)_setSelectedRadio:(CPRadio)aRadio
00222 {
00223     if (_selectedRadio === aRadio)
00224         return;
00225 
00226     [_selectedRadio setState:CPOffState];
00227     _selectedRadio = aRadio;
00228 }
00229 
00230 - (CPRadio)selectedRadio
00231 {
00232     return _selectedRadio;
00233 }
00234 
00235 - (CPArray)radios
00236 {
00237     return [_radios allObjects];
00238 }
00239 
00240 @end
00241 
00242 var CPRadioGroupRadiosKey           = @"CPRadioGroupRadiosKey",
00243     CPRadioGroupSelectedRadioKey    = @"CPRadioGroupSelectedRadioKey";
00244 
00245 @implementation CPRadioGroup (CPCoding)
00246 
00247 - (id)initWithCoder:(CPCoder)aCoder
00248 {
00249     self = [super init];
00250 
00251     if (self)
00252     {
00253         _radios = [aCoder decodeObjectForKey:CPRadioGroupRadiosKey];
00254         _selectedRadio = [aCoder decodeObjectForKey:CPRadioGroupSelectedRadioKey];
00255     }
00256 
00257     return self;
00258 }
00259 
00260 - (void)encodeWithCoder:(CPCoder)aCoder
00261 {
00262     [aCoder encodeObject:_radios forKey:CPRadioGroupRadiosKey];
00263     [aCoder encodeObject:_selectedRadio forKey:CPRadioGroupSelectedRadioKey];
00264 }
00265 
00266 @end
00267 
00268 @implementation CPRadioGroup (CPSynthesizedAccessors)
00269 
00273 - (id)target
00274 {
00275     return _target;
00276 }
00277 
00281 - (void)setTarget:(id)aValue
00282 {
00283     _target = aValue;
00284 }
00285 
00289 - (SEL)action
00290 {
00291     return _action;
00292 }
00293 
00297 - (void)setAction:(SEL)aValue
00298 {
00299     _action = aValue;
00300 }
00301 
00302 @end
 All Classes Files Functions Variables Defines