30 virtual bool Filter(std::string& )
const {
return false; }
47 template<NumericFieldType T>
54 std::string
Format(
const T& value,
bool editing)
const
57 if constexpr (std::is_floating_point_v<T>)
59 if (editing && !std::isfinite(value))
61 if (
decimals >= 0 && std::isfinite(value))
64 const int written = std::snprintf(buffer,
sizeof(buffer),
"%.*f",
decimals,
double(value));
65 if (written > 0 && written <
int(
sizeof(buffer)))
66 return std::string(buffer,
size_t(written));
74 bool Parse(std::string_view text, T& value)
const
99 requires std::is_floating_point_v<T>
102 static const auto converters = [] {
103 std::array<TextConverter<T>, 10> all{};
104 for (
int i = 0; i < int(all.size()); ++i)
105 all[
size_t(i)].decimals = i;
110 return converters[size_t(std::min(decimals,
int(converters.size()) - 1))];
What an editable field takes as text, asked while it is edited.
Definition gui_text_converter.h:22
virtual bool AcceptsCharacter(uint32_t) const
Whether typing codepoint can be part of the text.
Definition gui_text_converter.h:27
virtual bool Filter(std::string &) const
Strips what the text can't hold (pasted or dropped text, a typed character); true if text changed.
Definition gui_text_converter.h:30
virtual bool AcceptsComposition() const
Whether an IME may compose text into the field.
Definition gui_text_converter.h:32
virtual ~ITextValidator()=default
Definition gui_text_converter.h:45
const TextConverter< T > & DefaultTextConverter()
The converter a field uses for T when it is given none.
Definition gui_text_converter.h:90
std::string FormatNumber(NumericType type, const NumericValue &value)
value as text; floats use up to 9 significant digits, NaN and infinities spelled out.
NumericType CopyNumericTypeTo(const T &, NumericValue &dst)
Definition gui_numeric_type.h:64
NumericType
Definition gui_numeric_type.h:15
bool FilterNumberText(NumericType type, std::string &text)
Strips what a number of type can't hold (letters, a second dot, ...).
bool ParseNumber(std::string_view text, NumericType type, NumericValue &outValue)
The whole text as a number of type: in range and, for floats, finite.
const TextConverter< T > & FixedDecimalsConverter(int decimals)
The converter that shows a float or double with decimals digits after the point (0 to 9; less shows a...
Definition gui_text_converter.h:100
bool IsNumberCharacter(NumericType type, uint32_t codepoint)
Whether codepoint can be typed into a number of type.
constexpr NumericType GetNumericType(const T &)
Definition gui_numeric_type.h:52
@ None
not a numeric type
Definition gui_numeric_type.h:16
Definition gui_logging.h:24
int decimals
For floats and doubles: the digits shown after the point (2: "1.00"); -1 shows as few as the value ne...
Definition gui_text_converter.h:52
std::string Format(const T &value, bool editing) const
Definition gui_text_converter.h:54
bool AcceptsCharacter(uint32_t codepoint) const override
Whether typing codepoint can be part of the text.
Definition gui_text_converter.h:83
bool Parse(std::string_view text, T &value) const
Definition gui_text_converter.h:74
bool Filter(std::string &text) const override
Strips what the text can't hold (pasted or dropped text, a typed character); true if text changed.
Definition gui_text_converter.h:84
bool AcceptsComposition() const override
Whether an IME may compose text into the field.
Definition gui_text_converter.h:85
Turns a field's value into the text it shows and edits, and edited text back into a value.
Definition gui_text_converter.h:42
Definition gui_numeric_type.h:37