HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
gui_window.hpp
1// Copyright Take Vos 2019-2021.
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
5#pragma once
6
7#include "gui_window_size.hpp"
8#include "gui_window_delegate.hpp"
9#include "mouse_cursor.hpp"
10#include "hitbox.hpp"
11#include "mouse_event.hpp"
12#include "keyboard_event.hpp"
13#include "keyboard_focus_direction.hpp"
14#include "keyboard_focus_group.hpp"
15#include "theme.hpp"
16#include "../text/gstring.hpp"
17#include "../geometry/axis_aligned_rectangle.hpp"
18#include "../chrono.hpp"
19#include "../label.hpp"
20#include "../widgets/window_widget.hpp"
21#include "../widgets/grid_widget.hpp"
22#include "../widgets/toolbar_widget.hpp"
23#include <unordered_set>
24#include <memory>
25#include <mutex>
26
27namespace tt {
28class gfx_device;
29class gfx_system;
30class gfx_surface;
31class gui_system;
32class keyboard_bindings;
33
40public:
42
43 gui_system &gui;
44
46
57
65 mouse_cursor currentmouse_cursor = mouse_cursor::None;
66
70
74
78
83 bool resizing = false;
84
88 bool active = false;
89
92 gui_window_size size_state = gui_window_size::normal;
93
94 label title;
95
100 float dpi = 72.0;
101
104
105 gui_window(gui_system &gui, label const &title, std::weak_ptr<delegate_type> delegate = {}) noexcept;
106
107 virtual ~gui_window();
108
109 gui_window(gui_window const &) = delete;
110 gui_window &operator=(gui_window const &) = delete;
111 gui_window(gui_window &&) = delete;
112 gui_window &operator=(gui_window &&) = delete;
113
120 virtual void init();
121
127 virtual void deinit();
128
131 [[nodiscard]] bool is_gui_thread() const noexcept;
132
133 void set_device(gfx_device *device) noexcept;
134
137 tt::keyboard_bindings const &keyboard_bindings() const noexcept;
138
142 {
143 _request_redraw_rectangle |= rectangle;
144 }
145
148 void request_redraw() noexcept
149 {
150 tt_axiom(is_gui_thread());
152 }
153
157 [[nodiscard]] float fontScale() const noexcept
158 {
159 return dpi / (window_scale() * 72.0f);
160 }
161
165 virtual void render(utc_nanoseconds displayTimePoint);
166
169 [[nodiscard]] bool is_closed() const noexcept;
170
175 [[nodiscard]] grid_widget &content() noexcept
176 {
177 tt_axiom(is_gui_thread());
178 tt_axiom(widget);
179 return widget->content();
180 }
181
186 [[nodiscard]] toolbar_widget &toolbar() noexcept
187 {
188 tt_axiom(is_gui_thread());
189 tt_axiom(widget);
190 return widget->toolbar();
191 }
192
195 virtual void set_cursor(mouse_cursor cursor) = 0;
196
197 void set_resize_border_priority(bool left, bool right, bool bottom, bool top) noexcept;
198
201 virtual void close_window() = 0;
202
205 virtual void minimize_window() = 0;
206
209 virtual void maximize_window() = 0;
210
213 virtual void normalize_window() = 0;
214
217 virtual void set_window_size(extent2 extent) = 0;
218
221 [[nodiscard]] virtual std::string get_text_from_clipboard() const noexcept = 0;
222
225 virtual void set_text_on_clipboard(std::string str) noexcept = 0;
226
227 void update_mouse_target(tt::widget const *new_target_widget, point2 position = {}) noexcept;
228
235 void update_keyboard_target(tt::widget const *widget, keyboard_focus_group group = keyboard_focus_group::normal) noexcept;
236
245 void
246 update_keyboard_target(tt::widget const *widget, keyboard_focus_group group, keyboard_focus_direction direction) noexcept;
247
255 void update_keyboard_target(keyboard_focus_group group, keyboard_focus_direction direction) noexcept;
256
261 [[nodiscard]] virtual extent2 virtual_screen_size() const noexcept = 0;
262
263 [[nodiscard]] translate2 window_to_screen() const noexcept
264 {
265 return translate2{screen_rectangle.left(), screen_rectangle.bottom()};
266 }
267
268 [[nodiscard]] translate2 screen_to_window() const noexcept
269 {
270 return ~window_to_screen();
271 }
272
273protected:
275
276 std::atomic<aarectangle> _request_redraw_rectangle = aarectangle{};
277
285 utc_nanoseconds last_forced_redraw = {};
286
290 virtual void create_window(extent2 new_size) = 0;
291
296 [[nodiscard]] float window_scale() const noexcept;
297
301 [[nodiscard]] virtual bool handle_event(tt::command command) noexcept;
302
303 [[nodiscard]] virtual bool handle_event(std::vector<tt::command> const &commands) noexcept
304 {
305 for (ttlet command : commands) {
306 if (handle_event(command)) {
307 return true;
308 }
309 }
310 return false;
311 }
312
316 [[nodiscard]] virtual bool handle_event(mouse_event const &event) noexcept
317 {
318 return false;
319 }
320
324 [[nodiscard]] virtual bool handle_event(keyboard_event const &event) noexcept
325 {
326 return false;
327 }
328
334 bool send_event(mouse_event const &event) noexcept;
335
340 bool send_event(keyboard_event const &event) noexcept;
341
342 bool send_event(KeyboardState _state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept;
343
344 bool send_event(grapheme grapheme, bool full = true) noexcept;
345
346 bool send_event(char32_t c, bool full = true) noexcept;
347
348private:
349 std::shared_ptr<std::function<void()>> _setting_change_callback;
350
355 tt::widget const *_mouse_target_widget = nullptr;
356
360 tt::widget const *_keyboard_target_widget = nullptr;
361
366 void widget_is_destructing(tt::widget const *sender) noexcept
367 {
368 if (_mouse_target_widget == sender) {
369 _mouse_target_widget = nullptr;
370 }
371 if (_keyboard_target_widget == sender) {
372 _keyboard_target_widget = nullptr;
373 }
374 }
375
384 template<typename Event>
385 bool send_event_to_widget(tt::widget const *target_widget, Event const &event) noexcept;
386
387 friend class widget;
388};
389
390} // namespace tt
STL namespace.
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
extent2 size() const noexcept
Get size of the rectangle.
Definition axis_aligned_rectangle.hpp:159
Class which represents an rectangle.
Definition rectangle.hpp:16
Definition gfx_device.hpp:22
Graphics system.
Definition gui_system.hpp:29
Definition gui_window.hpp:39
mouse_cursor currentmouse_cursor
The current cursor.
Definition gui_window.hpp:65
gui_window_size size_state
Definition gui_window.hpp:92
virtual void set_text_on_clipboard(std::string str) noexcept=0
Place a text string on the operating system's clip-board.
std::atomic< bool > request_resize
When set to true the window will resize to the preferred size of the contained widget.
Definition gui_window.hpp:73
virtual void set_window_size(extent2 extent)=0
Ask the operating system to set the size of this window.
void update_keyboard_target(keyboard_focus_group group, keyboard_focus_direction direction) noexcept
Change the keyboard focus to the given, previous or next widget.
virtual void close_window()=0
Ask the operating system to close this window.
std::atomic< bool > request_constrain
When set to true the widget will calculate their constraints.
Definition gui_window.hpp:69
bool resizing
Definition gui_window.hpp:83
virtual void init()
2 phase constructor.
toolbar_widget & toolbar() noexcept
Get a reference to window's toolbar widget.
Definition gui_window.hpp:186
virtual void maximize_window()=0
Ask the operating system to maximize this window.
bool is_gui_thread() const noexcept
Check if the current thread is the same as the gui_system loop.
std::atomic< bool > request_layout
When set to true the widgets will be laid out.
Definition gui_window.hpp:77
void update_keyboard_target(tt::widget const *widget, keyboard_focus_group group=keyboard_focus_group::normal) noexcept
Change the keyboard focus to the given widget.
virtual void set_cursor(mouse_cursor cursor)=0
Set the mouse cursor icon.
std::unique_ptr< window_widget > widget
The widget covering the complete window.
Definition gui_window.hpp:103
virtual void minimize_window()=0
Ask the operating system to minimize this window.
void update_keyboard_target(tt::widget const *widget, keyboard_focus_group group, keyboard_focus_direction direction) noexcept
Change the keyboard focus to the previous or next widget from the given widget.
virtual std::string get_text_from_clipboard() const noexcept=0
Retrieve a text string from the operating system's clip-board.
virtual void deinit()
2 phase constructor.
bool active
Definition gui_window.hpp:88
virtual extent2 virtual_screen_size() const noexcept=0
Get the size of the virtual-screen.
grid_widget & content() noexcept
Get a reference to the window's content widget.
Definition gui_window.hpp:175
bool is_closed() const noexcept
Check if the window was closed by the operating system.
virtual void render(utc_nanoseconds displayTimePoint)
Update window.
void request_redraw() noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:148
aarectangle screen_rectangle
The current rectangle of the window relative to the screen.
Definition gui_window.hpp:56
virtual void normalize_window()=0
Ask the operating system to normalize this window.
float fontScale() const noexcept
By how much the font needs to be scaled compared to current windowScale.
Definition gui_window.hpp:157
float dpi
Definition gui_window.hpp:100
Definition gui_window_delegate.hpp:12
Definition keyboard_bindings.hpp:17
A label consisting of localizable text and an icon.
Definition label.hpp:27
A GUI widget that lays out child-widgets in a grid with variable sized cells.
Definition grid_widget.hpp:37
A toolbar widget is located at the top of a window and lays out its children horizontally.
Definition toolbar_widget.hpp:28
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37