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
85
87 gui_window& window,
90 text_widget_attribute auto&&...attributes) noexcept :
92 {
93 set_attributes(hi_forward(attributes)...);
94 }
95
104 gui_window& window,
105 widget *parent,
106 different_from<std::shared_ptr<delegate_type>> auto&& text,
107 text_widget_attribute auto&&...attributes) noexcept requires requires
108 {
111
113 widget_constraints const& set_constraints() noexcept override;
114 void set_layout(widget_layout const& layout) noexcept override;
115 void draw(draw_context const& context) noexcept override;
116 bool handle_event(gui_event const& event) noexcept override;
117 hitbox hitbox_test(point3 position) const noexcept override;
118 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override;
120private:
121 enum class add_type { append, insert, dead };
122
123 struct undo_type {
124 gstring text;
125 text_selection selection;
126 };
127
128 enum class cursor_state_type { off, on, busy, none };
129
130 gstring _cached_text;
131 text_shaper _shaped_text;
132 float _base_line;
133
134 delegate_type::callback_token _delegate_cbt;
135
136 decltype(text_style)::callback_token _text_style_cbt;
137
138 text_selection _selection;
139
140 scoped_task<> _blink_cursor;
141
142 observer<cursor_state_type> _cursor_state = cursor_state_type::none;
143 decltype(_cursor_state)::callback_token _cursor_state_cbt;
144
147 bool _request_scroll = false;
148
155 gui_event _last_drag_mouse_event = {};
156
159 utc_nanoseconds _last_drag_mouse_event_next_repeat = {};
160
163 float _vertical_movement_x = std::numeric_limits<float>::quiet_NaN();
164
165 bool _overwrite_mode = false;
166
172 grapheme _has_dead_character = nullptr;
173
174 undo_stack<undo_type> _undo_stack = {1000};
175
176 void set_attributes() noexcept {}
177 void set_attributes(text_widget_attribute auto&& first, text_widget_attribute auto&&...rest) noexcept
178 {
179 if constexpr (forward_of<decltype(first), observer<hi::alignment>>) {
180 alignment = hi_forward(first);
181 } else if constexpr (forward_of<decltype(first), observer<hi::semantic_text_style>>) {
182 text_style = hi_forward(first);
183 } else {
184 hi_static_no_default();
185 }
186
187 set_attributes(hi_forward(rest)...);
188 }
189
194 void update_shaped_text() noexcept;
195
198 void scroll_to_show_selection() noexcept;
199
200 void request_scroll() noexcept;
201
211 void reset_state(char const *states) noexcept;
212
213 [[nodiscard]] gstring_view selected_text() const noexcept;
214 void undo_push() noexcept;
215 void undo() noexcept;
216 void redo() noexcept;
217
218 scoped_task<> blink_cursor() noexcept;
219
222 void fix_cursor_position() noexcept;
223
224 void replace_selection(gstring const& replacement) noexcept;
225
231 void add_character(grapheme c, add_type mode) noexcept;
232 void delete_dead_character() noexcept;
233 void delete_character_next() noexcept;
234 void delete_character_prev() noexcept;
235 void delete_word_next() noexcept;
236 void delete_word_prev() noexcept;
237};
238
239}} // namespace hi::v1
#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:66
A delegate that controls the state of a text_widget.
Definition text_delegate.hpp:25
A text widget.
Definition text_widget.hpp:61
text_widget(gui_window &window, widget *parent, std::shared_ptr< delegate_type > delegate) noexcept
Construct a text widget.
text_widget(gui_window &window, 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:103
observer< semantic_text_style > text_style
The style of the text.
Definition text_widget.hpp:74
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:44
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:198
widget *const parent
Pointer to the parent widget.
Definition widget.hpp:53
widget(gui_window &window, widget *parent) noexcept
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:62
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:48
The constraints of a widget.
Definition widget_constraints.hpp:26
The layout of a widget.
Definition widget_layout.hpp:37
Definition text_widget.hpp:33
T move(T... args)
T quiet_NaN(T... args)