HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "widget.hpp"
12#include "text_delegate.hpp"
13#include "../GUI/module.hpp"
14#include "../text/module.hpp"
15#include "../geometry/module.hpp"
16#include "../l10n/module.hpp"
17#include "../container/module.hpp"
18#include "../observer/module.hpp"
19#include "../scoped_task.hpp"
20#include <memory>
21#include <string>
22#include <array>
23#include <optional>
24#include <future>
25#include <limits>
26#include <chrono>
27
28namespace hi { inline namespace v1 {
29
30template<typename Context>
31concept text_widget_attribute = forward_of<Context, observer<hi::alignment>, observer<hi::semantic_text_style>>;
32
59class text_widget final : public widget {
60public:
61 using super = widget;
62 using delegate_type = text_delegate;
63
65
68 observer<alignment> alignment = hi::alignment::top_flush();
69
72 observer<semantic_text_style> text_style = semantic_text_style::label;
73
75
82
83 text_widget(widget *parent, std::shared_ptr<delegate_type> delegate, text_widget_attribute auto&&...attributes) noexcept :
84 text_widget(parent, std::move(delegate))
85 {
86 set_attributes(hi_forward(attributes)...);
87 }
88
97 different_from<std::shared_ptr<delegate_type>> auto&& text,
98 text_widget_attribute auto&&...attributes) noexcept
99 requires requires { make_default_text_delegate(hi_forward(text)); }
101 {
102 }
103
105 [[nodiscard]] box_constraints update_constraints() noexcept override;
106 void set_layout(widget_layout const& context) noexcept override;
107 void draw(draw_context const& context) noexcept override;
108 bool handle_event(gui_event const& event) noexcept override;
109 hitbox hitbox_test(point2 position) const noexcept override;
110 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
112private:
113 enum class add_type { append, insert, dead };
114
115 struct undo_type {
116 gstring text;
117 text_selection selection;
118 };
119
120 enum class cursor_state_type { off, on, busy, none };
121
122 gstring _text_cache;
123 text_shaper _shaped_text;
124
125 mutable box_constraints _constraints_cache;
126
127 delegate_type::callback_token _delegate_cbt;
128
129 decltype(text_style)::callback_token _text_style_cbt;
130
131 text_selection _selection;
132
133 scoped_task<> _blink_cursor;
134
135 observer<cursor_state_type> _cursor_state = cursor_state_type::none;
136 decltype(_cursor_state)::callback_token _cursor_state_cbt;
137
140 bool _request_scroll = false;
141
148 gui_event _last_drag_mouse_event = {};
149
152 utc_nanoseconds _last_drag_mouse_event_next_repeat = {};
153
156 float _vertical_movement_x = std::numeric_limits<float>::quiet_NaN();
157
158 bool _overwrite_mode = false;
159
169 std::optional<grapheme> _has_dead_character = std::nullopt;
170
171 undo_stack<undo_type> _undo_stack = {1000};
172
173 void set_attributes() noexcept {}
174 void set_attributes(text_widget_attribute auto&& first, text_widget_attribute auto&&...rest) noexcept
175 {
176 if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
177 alignment = hi_forward(first);
178 } else if constexpr (forward_of<decltype(first), observer<hi::semantic_text_style>>) {
179 text_style = hi_forward(first);
180 } else {
182 }
183
184 set_attributes(hi_forward(rest)...);
185 }
186
189 void scroll_to_show_selection() noexcept;
190
191 void request_scroll() noexcept;
192
202 void reset_state(char const *states) noexcept;
203
204 [[nodiscard]] gstring_view selected_text() const noexcept;
205 void undo_push() noexcept;
206 void undo() noexcept;
207 void redo() noexcept;
208
209 scoped_task<> blink_cursor() noexcept;
210
213 void fix_cursor_position() noexcept;
214
217 void replace_selection(gstring const& replacement) noexcept;
218
224 void add_character(grapheme c, add_type mode) noexcept;
225 void delete_dead_character() noexcept;
226 void delete_character_next() noexcept;
227 void delete_character_prev() noexcept;
228 void delete_word_next() noexcept;
229 void delete_word_prev() noexcept;
230};
231
232}} // namespace hi::v1
Defines widget.
Defines delegate_delegate and some default text delegates.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:323
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
@ grapheme
The gui_event has grapheme data.
std::shared_ptr< text_delegate > make_default_text_delegate(auto &&value) noexcept
Create a shared pointer to a default text delegate.
Definition text_delegate.hpp:260
@ off
The 'off' state of a button.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
@ on
The border is drawn on the edge of a quad.
Horizontal/Vertical alignment combination.
Definition alignment.hpp:239
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:209
A user interface event.
Definition gui_event.hpp:74
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:27
The layout of a widget.
Definition widget_layout.hpp:35
2D constraints.
Definition box_constraints.hpp:22
A delegate that controls the state of a text_widget.
Definition text_delegate.hpp:25
A text widget.
Definition text_widget.hpp:59
observer< semantic_text_style > text_style
The style of the text.
Definition text_widget.hpp:72
text_widget(widget *parent, std::shared_ptr< delegate_type > delegate) noexcept
Construct a text widget.
text_widget(widget *parent, different_from< std::shared_ptr< delegate_type > > auto &&text, text_widget_attribute auto &&...attributes) noexcept
Construct a text widget.
Definition text_widget.hpp:95
observer< alignment > alignment
The horizontal alignment of the text inside the space of the widget.
Definition text_widget.hpp:68
An interactive graphical object as part of the user-interface.
Definition widget.hpp:36
widget(widget *parent) noexcept
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:41
Definition text_widget.hpp:31
T move(T... args)
T quiet_NaN(T... args)