TermOx
line_edit.hpp
1 #ifndef TERMOX_WIDGET_WIDGETS_LINE_EDIT_HPP
2 #define TERMOX_WIDGET_WIDGETS_LINE_EDIT_HPP
3 #include <functional>
4 #include <memory>
5 #include <optional>
6 #include <string>
7 
8 #include <signals_light/signal.hpp>
9 
10 #include <termox/painter/glyph_string.hpp>
11 #include <termox/system/key.hpp>
12 #include <termox/widget/widgets/detail/textline_base.hpp>
13 
14 namespace ox {
15 
17  public:
18  using Validator_t = std::function<bool(char32_t)>;
19 
20  struct Parameters {
21  Glyph_string initial_text = U"";
22  Align alignment = Align::Left;
23  Action focus_in_action = Action::None;
24  std::optional<Validator_t> validator = std::nullopt;
25  };
26 
27  public:
29  sl::Signal<void(std::string const&)> submitted;
30 
32  sl::Signal<void(std::string const&)> text_modified;
33 
34  public:
35  explicit Line_edit(Glyph_string initial_text = U"",
36  Align alignment = Align::Left,
37  Action focus_in_action = Action::None,
38  std::optional<Validator_t> validator = std::nullopt);
39 
40  explicit Line_edit(Parameters p);
41 
42  public:
44 
45  void set_validator(std::optional<Validator_t> x);
46 
48  void disable_validator();
49 
51  [[nodiscard]] auto validator() const noexcept
52  -> std::optional<Validator_t> const&;
53 
54  protected:
55  auto key_press_event(ox::Key k) -> bool override;
56 
57  auto focus_out_event() -> bool override;
58 
59  private:
60  std::optional<Validator_t> validator_;
61 };
62 
64 [[nodiscard]] auto line_edit(
65  Glyph_string initial_text = U"",
66  Align alignment = Align::Left,
67  Line_edit::Action focus_in_action = Line_edit::Action::None,
68  std::optional<Line_edit::Validator_t> validator = std::nullopt)
69  -> std::unique_ptr<Line_edit>;
70 
72 [[nodiscard]] auto line_edit(Line_edit::Parameters p)
73  -> std::unique_ptr<Line_edit>;
74 
75 } // namespace ox
76 #endif // TERMOX_WIDGET_WIDGETS_LINE_EDIT_HPP
Holds a collection of Glyphs with a similar interface to std::string.
Definition: glyph_string.hpp:19
Definition: line_edit.hpp:16
auto focus_out_event() -> bool override
Handles Focus_out_event objects.
Definition: line_edit.cpp:56
auto validator() const noexcept -> std::optional< Validator_t > const &
Return the current validator function object, or nullopt if not set.
Definition: line_edit.cpp:37
void disable_validator()
Turn off the validator so all text input is valid.
Definition: line_edit.cpp:34
auto key_press_event(ox::Key k) -> bool override
Handles Key_press_event objects.
Definition: line_edit.cpp:42
void set_validator(std::optional< Validator_t > x)
Set the input validator, allowing or disallowing specific char values.
Definition: line_edit.cpp:28
sl::Signal< void(std::string const &)> submitted
Emitted on Enter key press, sends along the current contents.
Definition: line_edit.hpp:29
sl::Signal< void(std::string const &)> text_modified
Emitted on every printable key press, sends along the current contents.
Definition: line_edit.hpp:32
Implements single line text Cursor navigation and deletion, no text input.
Definition: textline_base.hpp:14
auto alignment() const noexcept -> ox::Align
Return the current alignment.
Definition: textline_base.cpp:46
auto focus_in_action() const noexcept -> Action
Return the currently used focus in event Action.
Definition: textline_base.cpp:57
Definition: line_edit.hpp:20