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/gui_event.hpp"
14#include "../text/semantic_text_style.hpp"
15#include "../text/text_selection.hpp"
16#include "../text/text_shaper.hpp"
17#include "../observer.hpp"
18#include "../alignment.hpp"
19#include "../i18n/translate.hpp"
20#include "../undo_stack.hpp"
21#include "../scoped_task.hpp"
22#include <memory>
23#include <string>
24#include <array>
25#include <optional>
26#include <future>
27#include <limits>
28#include <chrono>
29
30namespace hi { inline namespace v1 {
31
32template<typename Context>
33concept text_widget_attribute = forward_of<Context, observer<hi::alignment>, observer<hi::semantic_text_style>>;
34
61class text_widget final : public widget {
62public:
63 using super = widget;
64 using delegate_type = text_delegate;
65
67
70 observer<alignment> alignment = hi::alignment::middle_center();
71
74 observer<semantic_text_style> text_style = semantic_text_style::label;
75
77
84
88 text_widget_attribute auto&&...attributes) noexcept :
89 text_widget(parent, std::move(delegate))
90 {
91 set_attributes(hi_forward(attributes)...);
92 }
93
101 widget *parent,
102 different_from<std::shared_ptr<delegate_type>> auto&& text,
103 text_widget_attribute auto&&...attributes) noexcept requires requires
104 {
107
109 widget_constraints const& set_constraints(set_constraints_context const &context) noexcept override;
110 void set_layout(widget_layout const& context) noexcept override;
111 void draw(draw_context const& context) noexcept override;
112 bool handle_event(gui_event const& event) noexcept override;
113 hitbox hitbox_test(point3 position) const noexcept override;
114 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
116private:
117 enum class add_type { append, insert, dead };
118
119 struct undo_type {
120 gstring text;
121 text_selection selection;
122 };
123
124 enum class cursor_state_type { off, on, busy, none };
125
126 gstring _cached_text;
127 text_shaper _shaped_text;
128 float _base_line;
129
130 delegate_type::callback_token _delegate_cbt;
131
132 decltype(text_style)::callback_token _text_style_cbt;
133
134 text_selection _selection;
135
136 scoped_task<> _blink_cursor;
137
138 observer<cursor_state_type> _cursor_state = cursor_state_type::none;
139 decltype(_cursor_state)::callback_token _cursor_state_cbt;
140
143 bool _request_scroll = false;
144
151 gui_event _last_drag_mouse_event = {};
152
155 utc_nanoseconds _last_drag_mouse_event_next_repeat = {};
156
159 float _vertical_movement_x = std::numeric_limits<float>::quiet_NaN();
160
161 bool _overwrite_mode = false;
162
168 grapheme _has_dead_character = nullptr;
169
170 undo_stack<undo_type> _undo_stack = {1000};
171
172 void set_attributes() noexcept {}
173 void set_attributes(text_widget_attribute auto&& first, text_widget_attribute auto&&...rest) noexcept
174 {
175 if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
176 alignment = hi_forward(first);
177 } else if constexpr (forward_of<decltype(first), observer<hi::semantic_text_style>>) {
178 text_style = hi_forward(first);
179 } else {
181 }
182
183 set_attributes(hi_forward(rest)...);
184 }
185
188 void scroll_to_show_selection() noexcept;
189
190 void request_scroll() noexcept;
191
201 void reset_state(char const *states) noexcept;
202
203 [[nodiscard]] gstring_view selected_text() const noexcept;
204 void undo_push() noexcept;
205 void undo() noexcept;
206 void redo() noexcept;
207
208 scoped_task<> blink_cursor() noexcept;
209
212 void fix_cursor_position() noexcept;
213
216 void replace_selection(gstring const& replacement) noexcept;
217
223 void add_character(grapheme c, add_type mode) noexcept;
224 void delete_dead_character() noexcept;
225 void delete_character_next() noexcept;
226 void delete_character_prev() noexcept;
227 void delete_word_next() noexcept;
228 void delete_word_prev() noexcept;
229};
230
231}} // namespace hi::v1
#define hi_static_no_default()
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:172
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
Defines widget.
Defines delegate_delegate and some default text delegates.
Definition of GUI event types.
@ 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:224
@ off
The 'off' state of a button.
DOXYGEN BUG.
Definition algorithm.hpp:15
@ on
The border is drawn on the edge of a quad.
The HikoGUI namespace.
Definition ascii.hpp:19
A user interface event.
Definition gui_event.hpp:76
Definition set_constraints_context.hpp:15
A delegate that controls the state of a text_widget.
Definition text_delegate.hpp:25
A text widget.
Definition text_widget.hpp:61
observer< semantic_text_style > text_style
The style of the text.
Definition text_widget.hpp:74
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:100
observer< alignment > alignment
The horizontal alignment of the text inside the space of the widget.
Definition text_widget.hpp:70
An interactive graphical object as part of the user-interface.
Definition widget.hpp:45
widget(widget *parent) noexcept
widget * parent
Pointer to the parent widget.
Definition widget.hpp:50
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:59
The constraints of a widget.
Definition widget_constraints.hpp:26
The layout of a widget.
Definition widget_layout.hpp:40
Definition text_widget.hpp:33
T move(T... args)
T quiet_NaN(T... args)