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/CPObject.j>
00024 @import <Foundation/CPSet.j>
00025
00026 @import "CPButton.j"
00027
00065 @implementation CPRadio : CPButton
00066 {
00067 CPRadioGroup _radioGroup;
00068 }
00069
00070 + (id)radioWithTitle:(CPString)aTitle theme:(CPTheme)aTheme
00071 {
00072 return [self buttonWithTitle:aTitle theme:aTheme];
00073 }
00074
00075 + (id)radioWithTitle:(CPString)aTitle
00076 {
00077 return [self buttonWithTitle:aTitle];
00078 }
00079
00080 + (CPButton)standardButtonWithTitle:(CPString)aTitle
00081 {
00082 var button = [[CPRadio alloc] init];
00083
00084 [button setTitle:aTitle];
00085
00086 return button;
00087 }
00088
00089 + (CPString)themeClass
00090 {
00091 return @"radio";
00092 }
00093
00094
00095 - (id)initWithFrame:(CGRect)aFrame radioGroup:(CPRadioGroup)aRadioGroup
00096 {
00097 self = [super initWithFrame:aFrame];
00098
00099 if (self)
00100 {
00101 [self setRadioGroup:aRadioGroup];
00102
00103 [self setHighlightsBy:CPContentsCellMask];
00104 [self setShowsStateBy:CPContentsCellMask];
00105
00106
00107 [self setImagePosition:CPImageLeft];
00108 [self setAlignment:CPLeftTextAlignment];
00109
00110 [self setBordered:YES];
00111 }
00112
00113 return self;
00114 }
00115
00116 - (id)initWithFrame:(CGRect)aFrame
00117 {
00118 return [self initWithFrame:aFrame radioGroup:[CPRadioGroup new]];
00119 }
00120
00121 - (CPInteger)nextState
00122 {
00123 return CPOnState;
00124 }
00125
00126 - (void)setRadioGroup:(CPRadioGroup)aRadioGroup
00127 {
00128 if (_radioGroup === aRadioGroup)
00129 return;
00130
00131 [_radioGroup _removeRadio:self];
00132 _radioGroup = aRadioGroup;
00133 [_radioGroup _addRadio:self];
00134 }
00135
00136 - (CPRadioGroup)radioGroup
00137 {
00138 return _radioGroup;
00139 }
00140
00141 - (void)setObjectValue:(id)aValue
00142 {
00143 [super setObjectValue:aValue];
00144
00145 if ([self state] === CPOnState)
00146 [_radioGroup _setSelectedRadio:self];
00147 }
00148
00149 @end
00150
00151 var CPRadioRadioGroupKey = @"CPRadioRadioGroupKey";
00152
00153 @implementation CPRadio (CPCoding)
00154
00155 - (id)initWithCoder:(CPCoder)aCoder
00156 {
00157 self = [super initWithCoder:aCoder];
00158
00159 if (self)
00160 _radioGroup = [aCoder decodeObjectForKey:CPRadioRadioGroupKey];
00161
00162 return self;
00163 }
00164
00165 - (void)encodeWithCoder:(CPCoder)aCoder
00166 {
00167 [super encodeWithCoder:aCoder];
00168
00169 [aCoder encodeObject:_radioGroup forKey:CPRadioRadioGroupKey];
00170 }
00171
00172 @end
00173
00174 @implementation CPRadioGroup : CPObject
00175 {
00176 CPSet _radios;
00177 CPRadio _selectedRadio;
00178
00179 id _target @accessors(property=target);
00180 SEL _action @accessors(property=action);
00181 }
00182
00183 - (id)init
00184 {
00185 self = [super init];
00186
00187 if (self)
00188 {
00189 _radios = [CPSet set];
00190 _selectedRadio = nil;
00191 }
00192
00193 return self;
00194 }
00195
00196 - (void)_addRadio:(CPRadio)aRadio
00197 {
00198 [_radios addObject:aRadio];
00199
00200 if ([aRadio state] === CPOnState)
00201 [self _setSelectedRadio:aRadio];
00202 }
00203
00204 - (void)_removeRadio:(CPRadio)aRadio
00205 {
00206 if (_selectedRadio === aRadio)
00207 _selectedRadio = nil;
00208
00209 [_radios removeObject:aRadio];
00210 }
00211
00212 - (void)_setSelectedRadio:(CPRadio)aRadio
00213 {
00214 if (_selectedRadio === aRadio)
00215 return;
00216
00217 [_selectedRadio setState:CPOffState];
00218 _selectedRadio = aRadio;
00219
00220 [CPApp sendAction:_action to:_target from:self];
00221 }
00222
00223 - (CPRadio)selectedRadio
00224 {
00225 return _selectedRadio;
00226 }
00227
00228 - (CPArray)radios
00229 {
00230 return [_radios allObjects];
00231 }
00232
00233 @end
00234
00235 var CPRadioGroupRadiosKey = @"CPRadioGroupRadiosKey",
00236 CPRadioGroupSelectedRadioKey = @"CPRadioGroupSelectedRadioKey";
00237
00238 @implementation CPRadioGroup (CPCoding)
00239
00240 - (id)initWithCoder:(CPCoder)aCoder
00241 {
00242 self = [super init];
00243
00244 if (self)
00245 {
00246 _radios = [aCoder decodeObjectForKey:CPRadioGroupRadiosKey];
00247 _selectedRadio = [aCoder decodeObjectForKey:CPRadioGroupSelectedRadioKey];
00248 }
00249
00250 return self;
00251 }
00252
00253 - (void)encodeWithCoder:(CPCoder)aCoder
00254 {
00255 [aCoder encodeObject:_radios forKey:CPRadioGroupRadiosKey];
00256 [aCoder encodeObject:_selectedRadio forKey:CPRadioGroupSelectedRadioKey];
00257 }
00258
00259 @end