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 "../geometry/module.hpp"
18#include "../i18n/translate.hpp"
19#include "../undo_stack.hpp"
20#include "../scoped_task.hpp"
21#include "../observer.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::top_flush();
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 [[nodiscard]] box_constraints update_constraints() 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(point2i 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 _text_cache;
127 text_shaper _shaped_text;
128
129 mutable box_constraints _constraints_cache;
130
131 delegate_type::callback_token _delegate_cbt;
132
133 decltype(text_style)::callback_token _text_style_cbt;
134
135 text_selection _selection;
136
137 scoped_task<> _blink_cursor;
138
139 observer<cursor_state_type> _cursor_state = cursor_state_type::none;
140 decltype(_cursor_state)::callback_token _cursor_state_cbt;
141
144 bool _request_scroll = false;
145
152 gui_event _last_drag_mouse_event = {};
153
156 utc_nanoseconds _last_drag_mouse_event_next_repeat = {};
157
160 float _vertical_movement_x = std::numeric_limits<float>::quiet_NaN();
161
162 bool _overwrite_mode = false;
163
169 grapheme _has_dead_character = nullptr;
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.
Definition of GUI event types.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:308
#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.
Draw context for drawing using the HikoGUI shaders.
Definition draw_context.hpp:211
A user interface event.
Definition gui_event.hpp:74
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: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:46
widget(widget *parent) noexcept
widget * parent
Pointer to the parent widget.
Definition widget.hpp:51
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:62
The layout of a widget.
Definition widget_layout.hpp:38
Definition text_widget.hpp:33
T move(T... args)
T quiet_NaN(T... args)