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 "../text/gstring.hpp"
16#include "../geometry/axis_aligned_rectangle.hpp"
17#include "../hires_utc_clock.hpp"
18#include "../label.hpp"
19#include "../widgets/window_widget.hpp"
20#include "../widgets/grid_widget.hpp"
21#include "../widgets/toolbar_widget.hpp"
22#include <unordered_set>
23#include <memory>
24#include <mutex>
25
26namespace tt {
27class gfx_device;
28class gfx_system;
29class gfx_surface;
30
37public:
39
41
49 mouse_cursor currentmouse_cursor = mouse_cursor::None;
50
54
58
63 bool resizing = false;
64
68 bool active = false;
69
72 gui_window_size size_state = gui_window_size::normal;
73
77
78 label title;
79
84 float dpi = 72.0;
85
88
89 gui_window(label const &title, std::weak_ptr<delegate_type> delegate = {}) noexcept;
90
91 virtual ~gui_window();
92
93 gui_window(gui_window const &) = delete;
94 gui_window &operator=(gui_window const &) = delete;
95 gui_window(gui_window &&) = delete;
96 gui_window &operator=(gui_window &&) = delete;
97
104 virtual void init();
105
111 virtual void deinit();
112
113 void set_device(gfx_device *device) noexcept;
114
118 {
119 _request_redraw_rectangle |= rectangle;
120 }
121
124 void request_redraw() noexcept
125 {
126 tt_axiom(is_gui_thread());
128 }
129
133 [[nodiscard]] float fontScale() const noexcept
134 {
135 return dpi / (window_scale() * 72.0f);
136 }
137
141 virtual void render(hires_utc_clock::time_point displayTimePoint);
142
145 [[nodiscard]] bool is_closed() const noexcept;
146
151 [[nodiscard]] grid_widget &content() noexcept
152 {
153 tt_axiom(is_gui_thread());
154 tt_axiom(widget);
155 return widget->content();
156 }
157
162 [[nodiscard]] toolbar_widget &toolbar() noexcept
163 {
164 tt_axiom(is_gui_thread());
165 tt_axiom(widget);
166 return widget->toolbar();
167 }
168
171 virtual void set_cursor(mouse_cursor cursor) = 0;
172
173 void set_resize_border_priority(bool left, bool right, bool bottom, bool top) noexcept;
174
177 virtual void close_window() = 0;
178
181 virtual void minimize_window() = 0;
182
185 virtual void maximize_window() = 0;
186
189 virtual void normalize_window() = 0;
190
193 virtual void set_window_size(extent2 extent) = 0;
194
197 [[nodiscard]] virtual std::string get_text_from_clipboard() const noexcept = 0;
198
201 virtual void set_text_on_clipboard(std::string str) noexcept = 0;
202
203 void update_mouse_target(tt::widget const *new_target_widget, point2 position = {}) noexcept;
204
211 void update_keyboard_target(tt::widget const *widget, keyboard_focus_group group = keyboard_focus_group::normal) noexcept;
212
221 void
222 update_keyboard_target(tt::widget const *widget, keyboard_focus_group group, keyboard_focus_direction direction) noexcept;
223
231 void update_keyboard_target(keyboard_focus_group group, keyboard_focus_direction direction) noexcept;
232
237 [[nodiscard]] virtual extent2 virtual_screen_size() const noexcept = 0;
238
239 [[nodiscard]] translate2 window_to_screen() const noexcept
240 {
241 return translate2{_screen_rectangle.left(), _screen_rectangle.bottom()};
242 }
243
244 [[nodiscard]] translate2 screen_to_window() const noexcept
245 {
246 return ~window_to_screen();
247 }
248
249protected:
251
263 aarectangle _screen_rectangle;
264
265 std::atomic<bool> _request_setting_change = true;
266
267 std::atomic<aarectangle> _request_redraw_rectangle = aarectangle{};
268
276 hires_utc_clock::time_point last_forced_redraw = {};
277
281 virtual void create_window() = 0;
282
287 [[nodiscard]] float window_scale() const noexcept;
288
292 [[nodiscard]] virtual bool handle_event(tt::command command) noexcept;
293
294 [[nodiscard]] virtual bool handle_event(std::vector<tt::command> const &commands) noexcept
295 {
296 for (ttlet command : commands) {
297 if (handle_event(command)) {
298 return true;
299 }
300 }
301 return false;
302 }
303
307 [[nodiscard]] virtual bool handle_event(mouse_event const &event) noexcept
308 {
309 return false;
310 }
311
315 [[nodiscard]] virtual bool handle_event(keyboard_event const &event) noexcept
316 {
317 return false;
318 }
319
325 bool send_event(mouse_event const &event) noexcept;
326
331 bool send_event(keyboard_event const &event) noexcept;
332
333 bool send_event(KeyboardState _state, keyboard_modifiers modifiers, keyboard_virtual_key key) noexcept;
334
335 bool send_event(grapheme grapheme, bool full = true) noexcept;
336
337 bool send_event(char32_t c, bool full = true) noexcept;
338
339private:
340 std::shared_ptr<std::function<void()>> _setting_change_callback;
341
346 tt::widget const *_mouse_target_widget = nullptr;
347
351 tt::widget const *_keyboard_target_widget = nullptr;
352
357 void widget_is_destructing(tt::widget const *sender) noexcept
358 {
359 if (_mouse_target_widget == sender) {
360 _mouse_target_widget = nullptr;
361 }
362 if (_keyboard_target_widget == sender) {
363 _keyboard_target_widget = nullptr;
364 }
365 }
366
375 template<typename Event>
376 bool send_event_to_widget(tt::widget const *target_widget, Event const &event) noexcept;
377
378 friend class widget;
379};
380
381} // namespace tt
STL namespace.
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
Class which represents an rectangle.
Definition rectangle.hpp:16
Definition gfx_device.hpp:22
Definition gui_window.hpp:36
mouse_cursor currentmouse_cursor
The current cursor.
Definition gui_window.hpp:49
gui_window_size size_state
Definition gui_window.hpp:72
virtual void set_text_on_clipboard(std::string str) noexcept=0
Place a text string on the operating system's clip-board.
extent2 size
The size from the surface, clamped to combined widget's size.
Definition gui_window.hpp:76
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.
bool resizing
Definition gui_window.hpp:63
virtual void init()
2 phase constructor.
toolbar_widget & toolbar() noexcept
Get a reference to window's toolbar widget.
Definition gui_window.hpp:162
std::atomic< bool > requestResize
When set to true the window will resize to the size of the contained widget.
Definition gui_window.hpp:57
virtual void maximize_window()=0
Ask the operating system to maximize this window.
std::atomic< bool > requestLayout
When set to true the widgets will be laid out.
Definition gui_window.hpp:53
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:87
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:68
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:151
bool is_closed() const noexcept
Check if the window was closed by the operating system.
void request_redraw() noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:124
virtual void normalize_window()=0
Ask the operating system to normalize this window.
virtual void render(hires_utc_clock::time_point displayTimePoint)
Update window.
float fontScale() const noexcept
By how much the font needs to be scaled compared to current windowScale.
Definition gui_window.hpp:133
void request_redraw(aarectangle rectangle) noexcept
Request a rectangle on the window to be redrawn.
Definition gui_window.hpp:117
float dpi
Definition gui_window.hpp:84
Definition gui_window_delegate.hpp:12
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:39