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 || [CPRadioGroup new]];
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:nil];
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
00180 - (id)init
00181 {
00182 self = [super init];
00183
00184 if (self)
00185 {
00186 _radios = [CPSet set];
00187 _selectedRadio = nil;
00188 }
00189
00190 return self;
00191 }
00192
00193 - (void)_addRadio:(CPRadio)aRadio
00194 {
00195 [_radios addObject:aRadio];
00196
00197 if ([aRadio state] === CPOnState)
00198 [self _setSelectedRadio:aRadio];
00199 }
00200
00201 - (void)_removeRadio:(CPRadio)aRadio
00202 {
00203 if (_selectedRadio === aRadio)
00204 _selectedRadio = nil;
00205
00206 [_radios removeObject:aRadio];
00207 }
00208
00209 - (void)_setSelectedRadio:(CPRadio)aRadio
00210 {
00211 if (_selectedRadio === aRadio)
00212 return;
00213
00214 [_selectedRadio setState:CPOffState];
00215 _selectedRadio = aRadio;
00216 }
00217
00218 - (CPRadio)selectedRadio
00219 {
00220 return _selectedRadio;
00221 }
00222
00223 - (CPArray)radios
00224 {
00225 return [_radios allObjects];
00226 }
00227
00228 @end
00229
00230 var CPRadioGroupRadiosKey = @"CPRadioGroupRadiosKey",
00231 CPRadioGroupSelectedRadioKey = @"CPRadioGroupSelectedRadioKey";
00232
00233 @implementation CPRadioGroup (CPCoding)
00234
00235 - (id)initWithCoder:(CPCoder)aCoder
00236 {
00237 self = [super init];
00238
00239 if (self)
00240 {
00241 _radios = [aCoder decodeObjectForKey:CPRadioGroupRadiosKey];
00242 _selectedRadio = [aCoder decodeObjectForKey:CPRadioGroupSelectedRadioKey];
00243 }
00244
00245 return self;
00246 }
00247
00248 - (void)encodeWithCoder:(CPCoder)aCoder
00249 {
00250 [aCoder encodeObject:_radios forKey:CPRadioGroupRadiosKey];
00251 [aCoder encodeObject:_selectedRadio forKey:CPRadioGroupSelectedRadioKey];
00252 }
00253
00254 @end