TachyonUI 0.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 <cmath>
6#include <cstdint>
7#include <string>
8#include <string_view>
9#include <type_traits>
10
11namespace tachyon
12{
15
19 {
20 public:
21 virtual ~ITextValidator() = default;
22
24 virtual bool AcceptsCharacter(uint32_t /*codepoint*/) const { return true; }
27 virtual bool Filter(std::string& /*text*/) const { return false; }
29 virtual bool AcceptsComposition() const { return true; }
30 };
31
38 template<typename T>
40
41 template<typename T>
43
44 template<NumericFieldType T>
46 {
47 std::string Format(const T& value, bool editing) const
48 {
49 // NaN and infinities aren't text to start editing from: start from 0.
50 if constexpr (std::is_floating_point_v<T>)
51 if (editing && !std::isfinite(value))
52 return "0";
53 NumericValue number{};
54 const NumericType type = CopyNumericTypeTo(value, number);
55 return FormatNumber(type, number);
56 }
57
58 bool Parse(std::string_view text, T& value) const
59 {
60 NumericValue number{};
61 const NumericType type = GetNumericType(value);
62 if (!ParseNumber(text, type, number))
63 return false;
64 return CopyNumericTypeTo(type, number, value);
65 }
66
67 bool AcceptsCharacter(uint32_t codepoint) const override { return IsNumberCharacter(GetNumericType(T{}), codepoint); }
68 bool Filter(std::string& text) const override { return FilterNumberText(GetNumericType(T{}), text); }
69 bool AcceptsComposition() const override { return false; } // digits and signs only
70 };
71
73 template<typename T>
75 {
76 static const TextConverter<T> converter;
77 return converter;
78 }
79
80}
What an editable field takes as text, asked while it is edited.
Definition gui_text_converter.h:19
virtual bool AcceptsCharacter(uint32_t) const
Whether typing codepoint can be part of the text.
Definition gui_text_converter.h:24
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:27
virtual bool AcceptsComposition() const
Whether an IME may compose text into the field.
Definition gui_text_converter.h:29
virtual ~ITextValidator()=default
Definition gui_text_converter.h:42
const TextConverter< T > & DefaultTextConverter()
The converter a field uses for T when it is given none.
Definition gui_text_converter.h:74
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.
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
std::string Format(const T &value, bool editing) const
Definition gui_text_converter.h:47
bool AcceptsCharacter(uint32_t codepoint) const override
Whether typing codepoint can be part of the text.
Definition gui_text_converter.h:67
bool Parse(std::string_view text, T &value) const
Definition gui_text_converter.h:58
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:68
bool AcceptsComposition() const override
Whether an IME may compose text into the field.
Definition gui_text_converter.h:69
Turns a field's value into the text it shows and edits, and edited text back into a value.
Definition gui_text_converter.h:39
Definition gui_numeric_type.h:37