DecimalSpinner: Use BNumberFormat for formatting

Change-Id: Idb45e98a61a0f16893e20aa02b10d7b2e1705454
Reviewed-on: https://review.haiku-os.org/c/haiku/+/7589
Reviewed-by: Emir SARI <[email protected]>
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
This commit is contained in:
Emir SARI
2025-04-07 21:30:48 +00:00
committed by waddlesplash
parent caa62d911c
commit cf961dc707
2 changed files with 29 additions and 13 deletions
+4 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Haiku, Inc. All rights reserved.
* Copyright 2015-2025 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
@@ -9,6 +9,7 @@
#define _DECIMAL_SPINNER_H
#include <NumberFormat.h>
#include <Spinner.h>
@@ -37,7 +38,7 @@ public:
virtual void SetEnabled(bool enable);
uint32 Precision() const { return fPrecision; };
virtual void SetPrecision(uint32 precision) { fPrecision = precision; };
virtual void SetPrecision(uint32 precision);
double MinValue() const { return fMinValue; }
virtual void SetMinValue(double min);
@@ -87,6 +88,7 @@ private:
double fStep;
double fValue;
uint32 fPrecision;
BNumberFormat* fNumberFormat;
// FBC padding
uint32 _reserved[20];
+24 -10
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2015 Haiku, Inc. All rights reserved.
* Copyright 2015-2025 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Authors:
@@ -144,7 +144,7 @@ BDecimalSpinner::BDecimalSpinner(BMessage* data)
fMinValue = 0.0;
if (data->FindDouble("_max", &fMaxValue) != B_OK)
fMinValue = 100.0;
fMaxValue = 100.0;
if (data->FindUInt32("_precision", &fPrecision) != B_OK)
fPrecision = 2;
@@ -159,6 +159,7 @@ BDecimalSpinner::BDecimalSpinner(BMessage* data)
BDecimalSpinner::~BDecimalSpinner()
{
delete fNumberFormat;
}
@@ -245,6 +246,17 @@ BDecimalSpinner::SetEnabled(bool enable)
}
void
BDecimalSpinner::SetPrecision(uint32 precision)
{
if (precision == fPrecision)
return;
fPrecision = precision;
fNumberFormat->SetPrecision(precision);
}
void
BDecimalSpinner::SetMinValue(double min)
{
@@ -294,13 +306,10 @@ BDecimalSpinner::SetValue(double value)
value = fMaxValue;
// update the text view
char* format;
asprintf(&format, "%%.%" B_PRId32 "f", fPrecision);
char* valueString;
asprintf(&valueString, format, value);
TextView()->SetText(valueString);
free(format);
free(valueString);
BString valueString;
fNumberFormat->Format(valueString, value);
TextView()->SetText(valueString.String());
// update the up and down arrows
SetIncrementEnabled(IsEnabled() && value < fMaxValue);
@@ -320,7 +329,9 @@ BDecimalSpinner::SetValue(double value)
void
BDecimalSpinner::SetValueFromText()
{
SetValue(roundTo(atof(TextView()->Text()), Precision()));
double parsedValue;
if (fNumberFormat->Parse(TextView()->Text(), parsedValue) == B_OK)
SetValue(roundTo(parsedValue, Precision()));
}
@@ -336,6 +347,9 @@ BDecimalSpinner::_InitObject()
fStep = 1.0;
fValue = 0.0;
fNumberFormat = new BNumberFormat();
fNumberFormat->SetPrecision(fPrecision);
TextView()->SetAlignment(B_ALIGN_RIGHT);
for (uint32 c = 0; c <= 42; c++)
TextView()->DisallowChar(c);