2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
6 * John Scipione, jscipione@gmail.com
10 #include <DecimalSpinner.h>
15 #include <PropertyInfo.h>
20 roundTo(double value
, uint32 n
)
22 return floor(value
* pow(10.0, n
) + 0.5) / pow(10.0, n
);
26 static property_info sProperties
[] = {
29 { B_GET_PROPERTY
, 0 },
30 { B_DIRECT_SPECIFIER
, 0 },
31 "Returns the maximum value of the spinner.",
37 { B_SET_PROPERTY
, 0 },
38 { B_DIRECT_SPECIFIER
, 0},
39 "Sets the maximum value of the spinner.",
46 { B_GET_PROPERTY
, 0 },
47 { B_DIRECT_SPECIFIER
, 0 },
48 "Returns the minimum value of the spinner.",
54 { B_SET_PROPERTY
, 0 },
55 { B_DIRECT_SPECIFIER
, 0},
56 "Sets the minimum value of the spinner.",
63 { B_SET_PROPERTY
, 0 },
64 { B_DIRECT_SPECIFIER
, 0},
65 "Sets the number of decimal places of precision of the spinner.",
71 { B_GET_PROPERTY
, 0 },
72 { B_DIRECT_SPECIFIER
, 0 },
73 "Returns the number of decimal places of precision of the spinner.",
80 { B_GET_PROPERTY
, 0 },
81 { B_DIRECT_SPECIFIER
, 0 },
82 "Returns the step size of the spinner.",
88 { B_SET_PROPERTY
, 0 },
89 { B_DIRECT_SPECIFIER
, 0},
90 "Sets the step size of the spinner.",
97 { B_GET_PROPERTY
, 0 },
98 { B_DIRECT_SPECIFIER
, 0 },
99 "Returns the value of the spinner.",
105 { B_SET_PROPERTY
, 0 },
106 { B_DIRECT_SPECIFIER
, 0},
107 "Sets the value of the spinner.",
116 // #pragma mark - BDecimalSpinner
119 BDecimalSpinner::BDecimalSpinner(BRect frame
, const char* name
,
120 const char* label
, BMessage
* message
, uint32 resizingMode
, uint32 flags
)
122 BAbstractSpinner(frame
, name
, label
, message
, resizingMode
, flags
)
128 BDecimalSpinner::BDecimalSpinner(const char* name
, const char* label
,
129 BMessage
* message
, uint32 flags
)
131 BAbstractSpinner(name
, label
, message
, flags
)
137 BDecimalSpinner::BDecimalSpinner(BMessage
* data
)
139 BAbstractSpinner(data
)
143 if (data
->FindDouble("_min", &fMinValue
) != B_OK
)
146 if (data
->FindDouble("_max", &fMaxValue
) != B_OK
)
149 if (data
->FindUInt32("_precision", &fPrecision
) != B_OK
)
152 if (data
->FindDouble("_step", &fStep
) != B_OK
)
155 if (data
->FindDouble("_val", &fValue
) != B_OK
)
160 BDecimalSpinner::~BDecimalSpinner()
166 BDecimalSpinner::Instantiate(BMessage
* data
)
168 if (validate_instantiation(data
, "DecimalSpinner"))
169 return new BDecimalSpinner(data
);
176 BDecimalSpinner::Archive(BMessage
* data
, bool deep
) const
178 status_t status
= BAbstractSpinner::Archive(data
, deep
);
179 data
->AddString("class", "DecimalSpinner");
182 status
= data
->AddDouble("_min", fMinValue
);
185 status
= data
->AddDouble("_max", fMaxValue
);
188 status
= data
->AddUInt32("_precision", fPrecision
);
191 status
= data
->AddDouble("_step", fStep
);
194 status
= data
->AddDouble("_val", fValue
);
201 BDecimalSpinner::GetSupportedSuites(BMessage
* message
)
203 message
->AddString("suites", "suite/vnd.Haiku-decimal-spinner");
205 BPropertyInfo
prop_info(sProperties
);
206 message
->AddFlat("messages", &prop_info
);
208 return BView::GetSupportedSuites(message
);
213 BDecimalSpinner::AttachedToWindow()
217 BAbstractSpinner::AttachedToWindow();
222 BDecimalSpinner::Decrement()
224 SetValue(Value() - Step());
229 BDecimalSpinner::Increment()
231 SetValue(Value() + Step());
236 BDecimalSpinner::SetEnabled(bool enable
)
238 if (IsEnabled() == enable
)
241 SetIncrementEnabled(enable
&& Value() < fMaxValue
);
242 SetDecrementEnabled(enable
&& Value() > fMinValue
);
244 BAbstractSpinner::SetEnabled(enable
);
249 BDecimalSpinner::SetMinValue(double min
)
252 if (fValue
< fMinValue
)
258 BDecimalSpinner::SetMaxValue(double max
)
261 if (fValue
> fMaxValue
)
267 BDecimalSpinner::Range(double* min
, double* max
)
275 BDecimalSpinner::SetRange(double min
, double max
)
283 BDecimalSpinner::SetValue(int32 value
)
285 SetValue((double)value
);
290 BDecimalSpinner::SetValue(double value
)
293 if (value
< fMinValue
)
295 else if (value
> fMaxValue
)
298 // update the text view
300 asprintf(&format
, "%%.%" B_PRId32
"f", fPrecision
);
302 asprintf(&valueString
, format
, value
);
303 TextView()->SetText(valueString
);
307 // update the up and down arrows
308 SetIncrementEnabled(IsEnabled() && value
< fMaxValue
);
309 SetDecrementEnabled(IsEnabled() && value
> fMinValue
);
323 BDecimalSpinner::SetValueFromText()
325 SetValue(roundTo(atof(TextView()->Text()), Precision()));
329 // #pragma mark - BDecimalSpinner private methods
333 BDecimalSpinner::_InitObject()
341 TextView()->SetAlignment(B_ALIGN_RIGHT
);
342 for (uint32 c
= 0; c
<= 42; c
++)
343 TextView()->DisallowChar(c
);
345 TextView()->DisallowChar('/');
346 for (uint32 c
= 58; c
<= 127; c
++)
347 TextView()->DisallowChar(c
);
353 void BDecimalSpinner::_ReservedDecimalSpinner20() {}
354 void BDecimalSpinner::_ReservedDecimalSpinner19() {}
355 void BDecimalSpinner::_ReservedDecimalSpinner18() {}
356 void BDecimalSpinner::_ReservedDecimalSpinner17() {}
357 void BDecimalSpinner::_ReservedDecimalSpinner16() {}
358 void BDecimalSpinner::_ReservedDecimalSpinner15() {}
359 void BDecimalSpinner::_ReservedDecimalSpinner14() {}
360 void BDecimalSpinner::_ReservedDecimalSpinner13() {}
361 void BDecimalSpinner::_ReservedDecimalSpinner12() {}
362 void BDecimalSpinner::_ReservedDecimalSpinner11() {}
363 void BDecimalSpinner::_ReservedDecimalSpinner10() {}
364 void BDecimalSpinner::_ReservedDecimalSpinner9() {}
365 void BDecimalSpinner::_ReservedDecimalSpinner8() {}
366 void BDecimalSpinner::_ReservedDecimalSpinner7() {}
367 void BDecimalSpinner::_ReservedDecimalSpinner6() {}
368 void BDecimalSpinner::_ReservedDecimalSpinner5() {}
369 void BDecimalSpinner::_ReservedDecimalSpinner4() {}
370 void BDecimalSpinner::_ReservedDecimalSpinner3() {}
371 void BDecimalSpinner::_ReservedDecimalSpinner2() {}
372 void BDecimalSpinner::_ReservedDecimalSpinner1() {}