BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / icon-o-matic / generic / property / Property.cpp
blob5eba3e785abdbf8401940a5aaa1e5bc1951ab352
1 /*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Stephan Aßmus <superstippi@gmx.de>
7 */
9 #include "Property.h"
11 #include <new>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <strings.h>
16 #include <Message.h>
18 #include "support.h"
20 using std::nothrow;
22 // constructor
23 Property::Property(uint32 identifier)
24 : fIdentifier(identifier),
25 fEditable(true)
29 // constructor
30 Property::Property(const Property& other)
31 : fIdentifier(other.fIdentifier),
32 fEditable(other.fEditable)
36 // constructor
37 Property::Property(BMessage* archive)
38 : fIdentifier(0),
39 fEditable(true)
41 if (!archive)
42 return;
44 if (archive->FindInt32("id", (int32*)&fIdentifier) < B_OK)
45 fIdentifier = 0;
46 if (archive->FindBool("editable", &fEditable) < B_OK)
47 fEditable = true;
50 // destructor
51 Property::~Property()
55 // Archive
56 status_t
57 Property::Archive(BMessage* into, bool deep) const
59 status_t ret = BArchivable::Archive(into, deep);
61 if (ret == B_OK)
62 ret = into->AddInt32("id", fIdentifier);
64 if (ret == B_OK)
65 ret = into->AddBool("editable", fEditable);
67 // finish off
68 if (ret >= B_OK)
69 ret = into->AddString("class", "Property");
71 return ret;
74 // #pragma mark -
76 // InterpolateTo
77 bool
78 Property::InterpolateTo(const Property* other, float scale)
80 // some properties don't support this
81 return false;
84 // SetEditable
85 void
86 Property::SetEditable(bool editable)
88 fEditable = editable;
91 // #pragma mark -
93 // constructor
94 IntProperty::IntProperty(uint32 identifier, int32 value,
95 int32 min, int32 max)
96 : Property(identifier),
97 fValue(value),
98 fMin(min),
99 fMax(max)
103 // constructor
104 IntProperty::IntProperty(const IntProperty& other)
105 : Property(other),
106 fValue(other.fValue),
107 fMin(other.fMin),
108 fMax(other.fMax)
112 // constructor
113 IntProperty::IntProperty(BMessage* archive)
114 : Property(archive),
115 fValue(0),
116 fMin(0),
117 fMax(0)
119 if (!archive)
120 return;
122 if (archive->FindInt32("value", &fValue) < B_OK)
123 fValue = 0;
124 if (archive->FindInt32("min", &fMin) < B_OK)
125 fMin = 0;
126 if (archive->FindInt32("max", &fMax) < B_OK)
127 fMax = 0;
130 // destructor
131 IntProperty::~IntProperty()
135 // Archive
136 status_t
137 IntProperty::Archive(BMessage* into, bool deep) const
139 status_t ret = Property::Archive(into, deep);
141 if (ret >= B_OK)
142 ret = into->AddInt32("value", fValue);
143 if (ret >= B_OK)
144 ret = into->AddInt32("min", fMin);
145 if (ret >= B_OK)
146 ret = into->AddInt32("max", fMax);
148 // finish off
149 if (ret >= B_OK)
150 ret = into->AddString("class", "IntProperty");
152 return ret;
155 // Instantiate
156 BArchivable*
157 IntProperty::Instantiate(BMessage* archive)
159 if (validate_instantiation(archive, "IntProperty"))
160 return new IntProperty(archive);
162 return NULL;
165 // Clone
166 Property*
167 IntProperty::Clone() const
169 return new IntProperty(*this);
172 // SetValue
173 bool
174 IntProperty::SetValue(const char* value)
176 return SetValue(atoi(value));
179 // SetValue
180 bool
181 IntProperty::SetValue(const Property* other)
183 const IntProperty* i = dynamic_cast<const IntProperty*>(other);
184 if (i) {
185 return SetValue(i->Value());
187 return false;
190 // GetValue
191 void
192 IntProperty::GetValue(BString& string)
194 string << fValue;
197 // InterpolateTo
198 bool
199 IntProperty::InterpolateTo(const Property* other, float scale)
201 const IntProperty* i = dynamic_cast<const IntProperty*>(other);
202 if (i) {
203 return SetValue(fValue + (int32)((float)(i->Value()
204 - fValue) * scale + 0.5));
206 return false;
209 // SetValue
210 bool
211 IntProperty::SetValue(int32 value)
213 // truncate
214 if (value < fMin)
215 value = fMin;
216 if (value > fMax)
217 value = fMax;
219 if (value != fValue) {
220 fValue = value;
221 return true;
223 return false;
226 // #pragma mark -
228 // constructor
229 FloatProperty::FloatProperty(uint32 identifier, float value,
230 float min, float max)
231 : Property(identifier),
232 fValue(value),
233 fMin(min),
234 fMax(max)
238 // constructor
239 FloatProperty::FloatProperty(const FloatProperty& other)
240 : Property(other),
241 fValue(other.fValue),
242 fMin(other.fMin),
243 fMax(other.fMax)
247 // constructor
248 FloatProperty::FloatProperty(BMessage* archive)
249 : Property(archive),
250 fValue(0.0),
251 fMin(0.0),
252 fMax(0.0)
254 if (!archive)
255 return;
257 if (archive->FindFloat("value", &fValue) < B_OK)
258 fValue = 0.0;
259 if (archive->FindFloat("min", &fMin) < B_OK)
260 fMin = 0.0;
261 if (archive->FindFloat("max", &fMax) < B_OK)
262 fMax = 0.0;
265 // destructor
266 FloatProperty::~FloatProperty()
270 // Archive
271 status_t
272 FloatProperty::Archive(BMessage* into, bool deep) const
274 status_t ret = Property::Archive(into, deep);
276 if (ret >= B_OK)
277 ret = into->AddFloat("value", fValue);
278 if (ret >= B_OK)
279 ret = into->AddFloat("min", fMin);
280 if (ret >= B_OK)
281 ret = into->AddFloat("max", fMax);
283 // finish off
284 if (ret >= B_OK)
285 ret = into->AddString("class", "FloatProperty");
287 return ret;
290 // Instantiate
291 BArchivable*
292 FloatProperty::Instantiate(BMessage* archive)
294 if (validate_instantiation(archive, "FloatProperty"))
295 return new FloatProperty(archive);
297 return NULL;
300 // Clone
301 Property*
302 FloatProperty::Clone() const
304 return new FloatProperty(*this);
307 // SetValue
308 bool
309 FloatProperty::SetValue(const char* value)
311 return SetValue(atof(value));
314 // SetValue
315 bool
316 FloatProperty::SetValue(const Property* other)
318 const FloatProperty* f = dynamic_cast<const FloatProperty*>(other);
319 if (f) {
320 return SetValue(f->Value());
322 return false;
325 // GetValue
326 void
327 FloatProperty::GetValue(BString& string)
329 append_float(string, fValue, 4);
332 // InterpolateTo
333 bool
334 FloatProperty::InterpolateTo(const Property* other, float scale)
336 const FloatProperty* f = dynamic_cast<const FloatProperty*>(other);
337 if (f) {
338 return SetValue(fValue + (f->Value() - fValue) * scale);
340 return false;
343 // SetValue
344 bool
345 FloatProperty::SetValue(float value)
347 // truncate
348 if (value < fMin)
349 value = fMin;
350 if (value > fMax)
351 value = fMax;
353 if (value != fValue) {
354 fValue = value;
355 return true;
357 return false;
360 // #pragma mark -
362 // constructor
363 UInt8Property::UInt8Property(uint32 identifier, uint8 value)
364 : Property(identifier),
365 fValue(value)
369 // constructor
370 UInt8Property::UInt8Property(const UInt8Property& other)
371 : Property(other),
372 fValue(other.fValue)
376 // constructor
377 UInt8Property::UInt8Property(BMessage* archive)
378 : Property(archive),
379 fValue(0)
381 if (!archive)
382 return;
384 if (archive->FindInt8("value", (int8*)&fValue) < B_OK)
385 fValue = 0;
388 // destructor
389 UInt8Property::~UInt8Property()
393 // Archive
394 status_t
395 UInt8Property::Archive(BMessage* into, bool deep) const
397 status_t ret = Property::Archive(into, deep);
399 if (ret >= B_OK)
400 ret = into->AddInt8("value", fValue);
402 // finish off
403 if (ret >= B_OK)
404 ret = into->AddString("class", "UInt8Property");
406 return ret;
409 // Instantiate
410 BArchivable*
411 UInt8Property::Instantiate(BMessage* archive)
413 if (validate_instantiation(archive, "UInt8Property"))
414 return new UInt8Property(archive);
416 return NULL;
419 // Clone
420 Property*
421 UInt8Property::Clone() const
423 return new UInt8Property(*this);
426 // SetValue
427 bool
428 UInt8Property::SetValue(const char* value)
430 return SetValue((uint8)max_c(0, min_c(255, atoi(value))));
433 // SetValue
434 bool
435 UInt8Property::SetValue(const Property* other)
437 const UInt8Property* u = dynamic_cast<const UInt8Property*>(other);
438 if (u) {
439 return SetValue(u->Value());
441 return false;
444 // GetValue
445 void
446 UInt8Property::GetValue(BString& string)
448 string << fValue;
451 // InterpolateTo
452 bool
453 UInt8Property::InterpolateTo(const Property* other, float scale)
455 const UInt8Property* u = dynamic_cast<const UInt8Property*>(other);
456 if (u) {
457 return SetValue(fValue + (uint8)((float)(u->Value()
458 - fValue) * scale + 0.5));
460 return false;
463 // SetValue
464 bool
465 UInt8Property::SetValue(uint8 value)
467 if (value != fValue) {
468 fValue = value;
469 return true;
471 return false;
474 // #pragma mark -
476 // constructor
477 BoolProperty::BoolProperty(uint32 identifier, bool value)
478 : Property(identifier),
479 fValue(value)
483 // constructor
484 BoolProperty::BoolProperty(const BoolProperty& other)
485 : Property(other),
486 fValue(other.fValue)
490 // constructor
491 BoolProperty::BoolProperty(BMessage* archive)
492 : Property(archive),
493 fValue(false)
495 if (!archive)
496 return;
498 if (archive->FindBool("value", &fValue) < B_OK)
499 fValue = false;
502 // destructor
503 BoolProperty::~BoolProperty()
507 // Archive
508 status_t
509 BoolProperty::Archive(BMessage* into, bool deep) const
511 status_t ret = Property::Archive(into, deep);
513 if (ret >= B_OK)
514 ret = into->AddBool("value", fValue);
516 // finish off
517 if (ret >= B_OK)
518 ret = into->AddString("class", "BoolProperty");
520 return ret;
523 // Instantiate
524 BArchivable*
525 BoolProperty::Instantiate(BMessage* archive)
527 if (validate_instantiation(archive, "BoolProperty"))
528 return new BoolProperty(archive);
530 return NULL;
533 // Clone
534 Property*
535 BoolProperty::Clone() const
537 return new BoolProperty(*this);
540 // SetValue
541 bool
542 BoolProperty::SetValue(const char* value)
544 bool v;
545 if (strcasecmp(value, "true") == 0)
546 v = true;
547 else if (strcasecmp(value, "on") == 0)
548 v = true;
549 else
550 v = (bool)atoi(value);
552 return SetValue(v);
555 // SetValue
556 bool
557 BoolProperty::SetValue(const Property* other)
559 const BoolProperty* b = dynamic_cast<const BoolProperty*>(other);
560 if (b) {
561 return SetValue(b->Value());
563 return false;
566 // GetValue
567 void
568 BoolProperty::GetValue(BString& string)
570 if (fValue)
571 string << "on";
572 else
573 string << "off";
576 // InterpolateTo
577 bool
578 BoolProperty::InterpolateTo(const Property* other, float scale)
580 const BoolProperty* b = dynamic_cast<const BoolProperty*>(other);
581 if (b) {
582 if (scale >= 0.5)
583 return SetValue(b->Value());
585 return false;
588 // SetValue
589 bool
590 BoolProperty::SetValue(bool value)
592 if (value != fValue) {
593 fValue = value;
594 return true;
596 return false;
599 // #pragma mark -
601 // constructor
602 StringProperty::StringProperty(uint32 identifier, const char* value)
603 : Property(identifier),
604 fValue(value)
608 // constructor
609 StringProperty::StringProperty(const StringProperty& other)
610 : Property(other),
611 fValue(other.fValue)
615 // constructor
616 StringProperty::StringProperty(BMessage* archive)
617 : Property(archive),
618 fValue()
620 if (!archive)
621 return;
623 if (archive->FindString("value", &fValue) < B_OK)
624 fValue = "";
627 // destructor
628 StringProperty::~StringProperty()
632 // Archive
633 status_t
634 StringProperty::Archive(BMessage* into, bool deep) const
636 status_t ret = Property::Archive(into, deep);
638 if (ret >= B_OK)
639 ret = into->AddString("value", fValue);
641 // finish off
642 if (ret >= B_OK)
643 ret = into->AddString("class", "StringProperty");
645 return ret;
648 // Instantiate
649 BArchivable*
650 StringProperty::Instantiate(BMessage* archive)
652 if (validate_instantiation(archive, "StringProperty"))
653 return new StringProperty(archive);
655 return NULL;
658 // Clone
659 Property*
660 StringProperty::Clone() const
662 return new StringProperty(*this);
665 // SetValue
666 bool
667 StringProperty::SetValue(const char* value)
669 BString t(value);
670 if (fValue != t) {
671 fValue = t;
672 return true;
674 return false;
677 // SetValue
678 bool
679 StringProperty::SetValue(const Property* other)
681 const StringProperty* s = dynamic_cast<const StringProperty*>(other);
682 if (s) {
683 return SetValue(s->Value());
685 return false;
688 // GetValue
689 void
690 StringProperty::GetValue(BString& string)
692 string << fValue;