2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
23 Property::Property(uint32 identifier
)
24 : fIdentifier(identifier
),
30 Property::Property(const Property
& other
)
31 : fIdentifier(other
.fIdentifier
),
32 fEditable(other
.fEditable
)
37 Property::Property(BMessage
* archive
)
44 if (archive
->FindInt32("id", (int32
*)&fIdentifier
) < B_OK
)
46 if (archive
->FindBool("editable", &fEditable
) < B_OK
)
57 Property::Archive(BMessage
* into
, bool deep
) const
59 status_t ret
= BArchivable::Archive(into
, deep
);
62 ret
= into
->AddInt32("id", fIdentifier
);
65 ret
= into
->AddBool("editable", fEditable
);
69 ret
= into
->AddString("class", "Property");
78 Property::InterpolateTo(const Property
* other
, float scale
)
80 // some properties don't support this
86 Property::SetEditable(bool editable
)
94 IntProperty::IntProperty(uint32 identifier
, int32 value
,
96 : Property(identifier
),
104 IntProperty::IntProperty(const IntProperty
& other
)
106 fValue(other
.fValue
),
113 IntProperty::IntProperty(BMessage
* archive
)
122 if (archive
->FindInt32("value", &fValue
) < B_OK
)
124 if (archive
->FindInt32("min", &fMin
) < B_OK
)
126 if (archive
->FindInt32("max", &fMax
) < B_OK
)
131 IntProperty::~IntProperty()
137 IntProperty::Archive(BMessage
* into
, bool deep
) const
139 status_t ret
= Property::Archive(into
, deep
);
142 ret
= into
->AddInt32("value", fValue
);
144 ret
= into
->AddInt32("min", fMin
);
146 ret
= into
->AddInt32("max", fMax
);
150 ret
= into
->AddString("class", "IntProperty");
157 IntProperty::Instantiate(BMessage
* archive
)
159 if (validate_instantiation(archive
, "IntProperty"))
160 return new IntProperty(archive
);
167 IntProperty::Clone() const
169 return new IntProperty(*this);
174 IntProperty::SetValue(const char* value
)
176 return SetValue(atoi(value
));
181 IntProperty::SetValue(const Property
* other
)
183 const IntProperty
* i
= dynamic_cast<const IntProperty
*>(other
);
185 return SetValue(i
->Value());
192 IntProperty::GetValue(BString
& string
)
199 IntProperty::InterpolateTo(const Property
* other
, float scale
)
201 const IntProperty
* i
= dynamic_cast<const IntProperty
*>(other
);
203 return SetValue(fValue
+ (int32
)((float)(i
->Value()
204 - fValue
) * scale
+ 0.5));
211 IntProperty::SetValue(int32 value
)
219 if (value
!= fValue
) {
229 FloatProperty::FloatProperty(uint32 identifier
, float value
,
230 float min
, float max
)
231 : Property(identifier
),
239 FloatProperty::FloatProperty(const FloatProperty
& other
)
241 fValue(other
.fValue
),
248 FloatProperty::FloatProperty(BMessage
* archive
)
257 if (archive
->FindFloat("value", &fValue
) < B_OK
)
259 if (archive
->FindFloat("min", &fMin
) < B_OK
)
261 if (archive
->FindFloat("max", &fMax
) < B_OK
)
266 FloatProperty::~FloatProperty()
272 FloatProperty::Archive(BMessage
* into
, bool deep
) const
274 status_t ret
= Property::Archive(into
, deep
);
277 ret
= into
->AddFloat("value", fValue
);
279 ret
= into
->AddFloat("min", fMin
);
281 ret
= into
->AddFloat("max", fMax
);
285 ret
= into
->AddString("class", "FloatProperty");
292 FloatProperty::Instantiate(BMessage
* archive
)
294 if (validate_instantiation(archive
, "FloatProperty"))
295 return new FloatProperty(archive
);
302 FloatProperty::Clone() const
304 return new FloatProperty(*this);
309 FloatProperty::SetValue(const char* value
)
311 return SetValue(atof(value
));
316 FloatProperty::SetValue(const Property
* other
)
318 const FloatProperty
* f
= dynamic_cast<const FloatProperty
*>(other
);
320 return SetValue(f
->Value());
327 FloatProperty::GetValue(BString
& string
)
329 append_float(string
, fValue
, 4);
334 FloatProperty::InterpolateTo(const Property
* other
, float scale
)
336 const FloatProperty
* f
= dynamic_cast<const FloatProperty
*>(other
);
338 return SetValue(fValue
+ (f
->Value() - fValue
) * scale
);
345 FloatProperty::SetValue(float value
)
353 if (value
!= fValue
) {
363 UInt8Property::UInt8Property(uint32 identifier
, uint8 value
)
364 : Property(identifier
),
370 UInt8Property::UInt8Property(const UInt8Property
& other
)
377 UInt8Property::UInt8Property(BMessage
* archive
)
384 if (archive
->FindInt8("value", (int8
*)&fValue
) < B_OK
)
389 UInt8Property::~UInt8Property()
395 UInt8Property::Archive(BMessage
* into
, bool deep
) const
397 status_t ret
= Property::Archive(into
, deep
);
400 ret
= into
->AddInt8("value", fValue
);
404 ret
= into
->AddString("class", "UInt8Property");
411 UInt8Property::Instantiate(BMessage
* archive
)
413 if (validate_instantiation(archive
, "UInt8Property"))
414 return new UInt8Property(archive
);
421 UInt8Property::Clone() const
423 return new UInt8Property(*this);
428 UInt8Property::SetValue(const char* value
)
430 return SetValue((uint8
)max_c(0, min_c(255, atoi(value
))));
435 UInt8Property::SetValue(const Property
* other
)
437 const UInt8Property
* u
= dynamic_cast<const UInt8Property
*>(other
);
439 return SetValue(u
->Value());
446 UInt8Property::GetValue(BString
& string
)
453 UInt8Property::InterpolateTo(const Property
* other
, float scale
)
455 const UInt8Property
* u
= dynamic_cast<const UInt8Property
*>(other
);
457 return SetValue(fValue
+ (uint8
)((float)(u
->Value()
458 - fValue
) * scale
+ 0.5));
465 UInt8Property::SetValue(uint8 value
)
467 if (value
!= fValue
) {
477 BoolProperty::BoolProperty(uint32 identifier
, bool value
)
478 : Property(identifier
),
484 BoolProperty::BoolProperty(const BoolProperty
& other
)
491 BoolProperty::BoolProperty(BMessage
* archive
)
498 if (archive
->FindBool("value", &fValue
) < B_OK
)
503 BoolProperty::~BoolProperty()
509 BoolProperty::Archive(BMessage
* into
, bool deep
) const
511 status_t ret
= Property::Archive(into
, deep
);
514 ret
= into
->AddBool("value", fValue
);
518 ret
= into
->AddString("class", "BoolProperty");
525 BoolProperty::Instantiate(BMessage
* archive
)
527 if (validate_instantiation(archive
, "BoolProperty"))
528 return new BoolProperty(archive
);
535 BoolProperty::Clone() const
537 return new BoolProperty(*this);
542 BoolProperty::SetValue(const char* value
)
545 if (strcasecmp(value
, "true") == 0)
547 else if (strcasecmp(value
, "on") == 0)
550 v
= (bool)atoi(value
);
557 BoolProperty::SetValue(const Property
* other
)
559 const BoolProperty
* b
= dynamic_cast<const BoolProperty
*>(other
);
561 return SetValue(b
->Value());
568 BoolProperty::GetValue(BString
& string
)
578 BoolProperty::InterpolateTo(const Property
* other
, float scale
)
580 const BoolProperty
* b
= dynamic_cast<const BoolProperty
*>(other
);
583 return SetValue(b
->Value());
590 BoolProperty::SetValue(bool value
)
592 if (value
!= fValue
) {
602 StringProperty::StringProperty(uint32 identifier
, const char* value
)
603 : Property(identifier
),
609 StringProperty::StringProperty(const StringProperty
& other
)
616 StringProperty::StringProperty(BMessage
* archive
)
623 if (archive
->FindString("value", &fValue
) < B_OK
)
628 StringProperty::~StringProperty()
634 StringProperty::Archive(BMessage
* into
, bool deep
) const
636 status_t ret
= Property::Archive(into
, deep
);
639 ret
= into
->AddString("value", fValue
);
643 ret
= into
->AddString("class", "StringProperty");
650 StringProperty::Instantiate(BMessage
* archive
)
652 if (validate_instantiation(archive
, "StringProperty"))
653 return new StringProperty(archive
);
660 StringProperty::Clone() const
662 return new StringProperty(*this);
667 StringProperty::SetValue(const char* value
)
679 StringProperty::SetValue(const Property
* other
)
681 const StringProperty
* s
= dynamic_cast<const StringProperty
*>(other
);
683 return SetValue(s
->Value());
690 StringProperty::GetValue(BString
& string
)