TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_text_converter.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <algorithm>
6#include <array>
7#include <cmath>
8#include <cstdint>
9#include <cstdio>
10#include <string>
11#include <string_view>
12#include <type_traits>
13
14namespace tachyon
15{
18
22 {
23 public:
24 virtual ~ITextValidator() = default;
25
27 virtual bool AcceptsCharacter(uint32_t /*codepoint*/) const { return true; }
30 virtual bool Filter(std::string& /*text*/) const { return false; }
32 virtual bool AcceptsComposition() const { return true; }
33 };
34
41 template<typename T>
43
44 template<typename T>
46
47 template<NumericFieldType T>
49 {
52 int decimals = -1;
53
54 std::string Format(const T& value, bool editing) const
55 {
56 // NaN and infinities aren't text to start editing from: start from 0.
57 if constexpr (std::is_floating_point_v<T>)
58 {
59 if (editing && !std::isfinite(value))
60 return "0";
61 if (decimals >= 0 && std::isfinite(value))
62 {
63 char buffer[64];
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));
67 }
68 }
69 NumericValue number{};
70 const NumericType type = CopyNumericTypeTo(value, number);
71 return FormatNumber(type, number);
72 }
73
74 bool Parse(std::string_view text, T& value) const
75 {
76 NumericValue number{};
77 const NumericType type = GetNumericType(value);
78 if (!ParseNumber(text, type, number))
79 return false;
80 return CopyNumericTypeTo(type, number, value);
81 }
82
83 bool AcceptsCharacter(uint32_t codepoint) const override { return IsNumberCharacter(GetNumericType(T{}), codepoint); }
84 bool Filter(std::string& text) const override { return FilterNumberText(GetNumericType(T{}), text); }
85 bool AcceptsComposition() const override { return false; } // digits and signs only
86 };
87
89 template<typename T>
91 {
92 static const TextConverter<T> converter;
93 return converter;
94 }
95
98 template<typename T>
99 requires std::is_floating_point_v<T>
101 {
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;
106 return all;
107 }();
108 if (decimals < 0)
110 return converters[size_t(std::min(decimals, int(converters.size()) - 1))];
111 }
112
113}
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