1 /* 2 * Copyright 2013, Rene Gollent, rene@gollent.com. 3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 4 * Distributed under the terms of the MIT License. 5 */ 6 7 8 #include "Setting.h" 9 10 #include <new> 11 12 13 // #pragma mark - Setting 14 15 16 Setting::~Setting() 17 { 18 } 19 20 21 // #pragma mark - BoolSetting 22 23 24 setting_type 25 BoolSetting::Type() const 26 { 27 return SETTING_TYPE_BOOL; 28 } 29 30 31 BVariant 32 BoolSetting::DefaultValue() const 33 { 34 return DefaultBoolValue(); 35 } 36 37 38 // #pragma mark - FloatSetting 39 40 41 setting_type 42 FloatSetting::Type() const 43 { 44 return SETTING_TYPE_FLOAT; 45 } 46 47 48 BVariant 49 FloatSetting::DefaultValue() const 50 { 51 return DefaultFloatValue(); 52 } 53 54 55 // #pragma mark - SettingsOption 56 57 58 SettingsOption::~SettingsOption() 59 { 60 } 61 62 63 // #pragma mark - OptionsSetting 64 65 66 setting_type 67 OptionsSetting::Type() const 68 { 69 return SETTING_TYPE_OPTIONS; 70 } 71 72 73 BVariant 74 OptionsSetting::DefaultValue() const 75 { 76 SettingsOption* option = DefaultOption(); 77 return option != NULL 78 ? BVariant(option->ID(), B_VARIANT_DONT_COPY_DATA) : BVariant(); 79 } 80 81 82 // #pragma mark - BoundedSetting 83 84 85 setting_type 86 BoundedSetting::Type() const 87 { 88 return SETTING_TYPE_BOUNDED; 89 } 90 91 92 // #pragma mark - RangeSetting 93 94 95 setting_type 96 RangeSetting::Type() const 97 { 98 return SETTING_TYPE_RANGE; 99 } 100 101 102 // #pragma mark - RectSetting 103 104 setting_type 105 RectSetting::Type() const 106 { 107 return SETTING_TYPE_RECT; 108 } 109 110 111 BVariant 112 RectSetting::DefaultValue() const 113 { 114 return DefaultRectValue(); 115 } 116 117 118 // #pragma mark - AbstractSetting 119 120 121 AbstractSetting::AbstractSetting(const BString& id, const BString& name) 122 : 123 fID(id), 124 fName(name) 125 { 126 } 127 128 129 const char* 130 AbstractSetting::ID() const 131 { 132 return fID; 133 } 134 135 136 const char* 137 AbstractSetting::Name() const 138 { 139 return fName; 140 } 141 142 143 // #pragma mark - BoolSettingImpl 144 145 146 BoolSettingImpl::BoolSettingImpl(const BString& id, const BString& name, 147 bool defaultValue) 148 : 149 AbstractSetting(id, name), 150 fDefaultValue(defaultValue) 151 { 152 } 153 154 155 bool 156 BoolSettingImpl::DefaultBoolValue() const 157 { 158 return fDefaultValue; 159 } 160 161 162 // #pragma mark - FloatSettingImpl 163 164 165 FloatSettingImpl::FloatSettingImpl(const BString& id, const BString& name, 166 float defaultValue) 167 : 168 AbstractSetting(id, name), 169 fDefaultValue(defaultValue) 170 { 171 } 172 173 174 float 175 FloatSettingImpl::DefaultFloatValue() const 176 { 177 return fDefaultValue; 178 } 179 180 181 // #pragma mark - OptionsSettingImpl 182 183 184 class OptionsSettingImpl::Option : public SettingsOption { 185 public: 186 Option(const BString& id, const BString& name) 187 { 188 } 189 190 virtual const char* ID() const 191 { 192 return fID; 193 } 194 195 virtual const char* Name() const 196 { 197 return fName; 198 } 199 200 private: 201 BString fID; 202 BString fName; 203 }; 204 205 206 OptionsSettingImpl::OptionsSettingImpl(const BString& id, const BString& name) 207 : 208 AbstractSetting(id, name), 209 fDefaultOption(NULL) 210 { 211 } 212 213 214 OptionsSettingImpl::~OptionsSettingImpl() 215 { 216 SetDefaultOption(NULL); 217 218 for (int32 i = 0; SettingsOption* option = fOptions.ItemAt(i); i++) 219 option->ReleaseReference(); 220 } 221 222 223 SettingsOption* 224 OptionsSettingImpl::DefaultOption() const 225 { 226 return fDefaultOption != NULL ? fDefaultOption : fOptions.ItemAt(0); 227 } 228 229 230 int32 231 OptionsSettingImpl::CountOptions() const 232 { 233 return fOptions.CountItems(); 234 } 235 236 237 SettingsOption* 238 OptionsSettingImpl::OptionAt(int32 index) const 239 { 240 return fOptions.ItemAt(index); 241 } 242 243 244 SettingsOption* 245 OptionsSettingImpl::OptionByID(const char* id) const 246 { 247 for (int32 i = 0; SettingsOption* option = fOptions.ItemAt(i); i++) { 248 if (strcmp(option->ID(), id) == 0) 249 return option; 250 } 251 252 return NULL; 253 } 254 255 256 bool 257 OptionsSettingImpl::AddOption(SettingsOption* option) 258 { 259 if (!fOptions.AddItem(option)) 260 return false; 261 262 option->AcquireReference(); 263 return true; 264 } 265 266 267 bool 268 OptionsSettingImpl::AddOption(const BString& id, const BString& name) 269 { 270 Option* option = new(std::nothrow) Option(id, name); 271 if (option == NULL) 272 return false; 273 BReference<Option> optionReference(option, true); 274 275 return AddOption(option); 276 } 277 278 279 void 280 OptionsSettingImpl::SetDefaultOption(SettingsOption* option) 281 { 282 if (option == fDefaultOption) 283 return; 284 285 if (fDefaultOption != NULL) 286 fDefaultOption->ReleaseReference(); 287 288 fDefaultOption = option; 289 290 if (fDefaultOption != NULL) 291 fDefaultOption->AcquireReference(); 292 } 293 294 295 // #pragma mark - RangeSettingImpl 296 297 298 BoundedSettingImpl::BoundedSettingImpl(const BString& id, const BString& name, 299 const BVariant& lowerBound, const BVariant& upperBound, 300 const BVariant& defaultValue) 301 : 302 AbstractSetting(id, name), 303 fLowerBound(lowerBound), 304 fUpperBound(upperBound), 305 fDefaultValue(defaultValue) 306 { 307 } 308 309 310 BVariant 311 BoundedSettingImpl::DefaultValue() const 312 { 313 return fDefaultValue; 314 } 315 316 317 BVariant 318 BoundedSettingImpl::LowerBound() const 319 { 320 return fLowerBound; 321 } 322 323 324 BVariant 325 BoundedSettingImpl::UpperBound() const 326 { 327 return fUpperBound; 328 } 329 330 331 // #pragma mark - RangeSettingImpl 332 333 334 RangeSettingImpl::RangeSettingImpl(const BString& id, const BString& name, 335 const BVariant& lowerBound, const BVariant& upperBound, 336 const BVariant& lowerValue, const BVariant& upperValue) 337 : 338 AbstractSetting(id, name), 339 fLowerBound(lowerBound), 340 fUpperBound(upperBound), 341 fLowerValue(lowerValue), 342 fUpperValue(upperValue) 343 { 344 } 345 346 347 BVariant 348 RangeSettingImpl::DefaultValue() const 349 { 350 // this one doesn't really make sense for RangeSetting since it 351 // describes a pair of values, which BVariant can't readily 352 // represent. 353 return BVariant(); 354 } 355 356 357 BVariant 358 RangeSettingImpl::LowerBound() const 359 { 360 return fLowerBound; 361 } 362 363 364 BVariant 365 RangeSettingImpl::UpperBound() const 366 { 367 return fUpperBound; 368 } 369 370 371 BVariant 372 RangeSettingImpl::LowerValue() const 373 { 374 return fLowerValue; 375 } 376 377 378 BVariant 379 RangeSettingImpl::UpperValue() const 380 { 381 return fUpperValue; 382 } 383 384 385 // #pragma mark - RectSettingImpl 386 387 388 RectSettingImpl::RectSettingImpl(const BString& id, const BString& name, 389 const BRect& defaultValue) 390 : 391 AbstractSetting(id, name), 392 fDefaultValue(defaultValue) 393 { 394 } 395 396 397 BRect 398 RectSettingImpl::DefaultRectValue() const 399 { 400 return fDefaultValue; 401 } 402