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:
@@ -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.
|
* Distributed under the terms of the MIT license.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
#define _DECIMAL_SPINNER_H
|
#define _DECIMAL_SPINNER_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <NumberFormat.h>
|
||||||
#include <Spinner.h>
|
#include <Spinner.h>
|
||||||
|
|
||||||
|
|
||||||
@@ -37,7 +38,7 @@ public:
|
|||||||
virtual void SetEnabled(bool enable);
|
virtual void SetEnabled(bool enable);
|
||||||
|
|
||||||
uint32 Precision() const { return fPrecision; };
|
uint32 Precision() const { return fPrecision; };
|
||||||
virtual void SetPrecision(uint32 precision) { fPrecision = precision; };
|
virtual void SetPrecision(uint32 precision);
|
||||||
|
|
||||||
double MinValue() const { return fMinValue; }
|
double MinValue() const { return fMinValue; }
|
||||||
virtual void SetMinValue(double min);
|
virtual void SetMinValue(double min);
|
||||||
@@ -87,8 +88,9 @@ private:
|
|||||||
double fStep;
|
double fStep;
|
||||||
double fValue;
|
double fValue;
|
||||||
uint32 fPrecision;
|
uint32 fPrecision;
|
||||||
|
BNumberFormat* fNumberFormat;
|
||||||
|
|
||||||
// FBC padding
|
// FBC padding
|
||||||
uint32 _reserved[20];
|
uint32 _reserved[20];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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.
|
* Distributed under the terms of the MIT license.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
@@ -144,7 +144,7 @@ BDecimalSpinner::BDecimalSpinner(BMessage* data)
|
|||||||
fMinValue = 0.0;
|
fMinValue = 0.0;
|
||||||
|
|
||||||
if (data->FindDouble("_max", &fMaxValue) != B_OK)
|
if (data->FindDouble("_max", &fMaxValue) != B_OK)
|
||||||
fMinValue = 100.0;
|
fMaxValue = 100.0;
|
||||||
|
|
||||||
if (data->FindUInt32("_precision", &fPrecision) != B_OK)
|
if (data->FindUInt32("_precision", &fPrecision) != B_OK)
|
||||||
fPrecision = 2;
|
fPrecision = 2;
|
||||||
@@ -159,6 +159,7 @@ BDecimalSpinner::BDecimalSpinner(BMessage* data)
|
|||||||
|
|
||||||
BDecimalSpinner::~BDecimalSpinner()
|
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
|
void
|
||||||
BDecimalSpinner::SetMinValue(double min)
|
BDecimalSpinner::SetMinValue(double min)
|
||||||
{
|
{
|
||||||
@@ -294,13 +306,10 @@ BDecimalSpinner::SetValue(double value)
|
|||||||
value = fMaxValue;
|
value = fMaxValue;
|
||||||
|
|
||||||
// update the text view
|
// update the text view
|
||||||
char* format;
|
BString valueString;
|
||||||
asprintf(&format, "%%.%" B_PRId32 "f", fPrecision);
|
fNumberFormat->Format(valueString, value);
|
||||||
char* valueString;
|
|
||||||
asprintf(&valueString, format, value);
|
TextView()->SetText(valueString.String());
|
||||||
TextView()->SetText(valueString);
|
|
||||||
free(format);
|
|
||||||
free(valueString);
|
|
||||||
|
|
||||||
// update the up and down arrows
|
// update the up and down arrows
|
||||||
SetIncrementEnabled(IsEnabled() && value < fMaxValue);
|
SetIncrementEnabled(IsEnabled() && value < fMaxValue);
|
||||||
@@ -320,7 +329,9 @@ BDecimalSpinner::SetValue(double value)
|
|||||||
void
|
void
|
||||||
BDecimalSpinner::SetValueFromText()
|
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;
|
fStep = 1.0;
|
||||||
fValue = 0.0;
|
fValue = 0.0;
|
||||||
|
|
||||||
|
fNumberFormat = new BNumberFormat();
|
||||||
|
fNumberFormat->SetPrecision(fPrecision);
|
||||||
|
|
||||||
TextView()->SetAlignment(B_ALIGN_RIGHT);
|
TextView()->SetAlignment(B_ALIGN_RIGHT);
|
||||||
for (uint32 c = 0; c <= 42; c++)
|
for (uint32 c = 0; c <= 42; c++)
|
||||||
TextView()->DisallowChar(c);
|
TextView()->DisallowChar(c);
|
||||||
|
|||||||
Reference in New Issue
Block a user