Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/window.cpp
20837 views
1
/**************************************************************************/
2
/* window.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "window.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/debugger/engine_debugger.h"
35
#include "core/input/input.h"
36
#include "scene/gui/control.h"
37
#include "scene/theme/theme_db.h"
38
#include "scene/theme/theme_owner.h"
39
40
// Editor integration.
41
42
int Window::root_layout_direction = 0;
43
44
void Window::set_root_layout_direction(int p_root_dir) {
45
root_layout_direction = p_root_dir;
46
}
47
48
// Dynamic properties.
49
50
Window *Window::focused_window = nullptr;
51
52
bool Window::_set(const StringName &p_name, const Variant &p_value) {
53
ERR_MAIN_THREAD_GUARD_V(false);
54
String name = p_name;
55
56
if (!name.begins_with("theme_override")) {
57
return false;
58
}
59
60
if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
61
if (name.begins_with("theme_override_icons/")) {
62
String dname = name.get_slicec('/', 1);
63
if (theme_icon_override.has(dname)) {
64
theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
65
}
66
theme_icon_override.erase(dname);
67
_notify_theme_override_changed();
68
} else if (name.begins_with("theme_override_styles/")) {
69
String dname = name.get_slicec('/', 1);
70
if (theme_style_override.has(dname)) {
71
theme_style_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
72
}
73
theme_style_override.erase(dname);
74
_notify_theme_override_changed();
75
} else if (name.begins_with("theme_override_fonts/")) {
76
String dname = name.get_slicec('/', 1);
77
if (theme_font_override.has(dname)) {
78
theme_font_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
79
}
80
theme_font_override.erase(dname);
81
_notify_theme_override_changed();
82
} else if (name.begins_with("theme_override_font_sizes/")) {
83
String dname = name.get_slicec('/', 1);
84
theme_font_size_override.erase(dname);
85
_notify_theme_override_changed();
86
} else if (name.begins_with("theme_override_colors/")) {
87
String dname = name.get_slicec('/', 1);
88
theme_color_override.erase(dname);
89
_notify_theme_override_changed();
90
} else if (name.begins_with("theme_override_constants/")) {
91
String dname = name.get_slicec('/', 1);
92
theme_constant_override.erase(dname);
93
_notify_theme_override_changed();
94
} else {
95
return false;
96
}
97
} else {
98
if (name.begins_with("theme_override_icons/")) {
99
String dname = name.get_slicec('/', 1);
100
add_theme_icon_override(dname, p_value);
101
} else if (name.begins_with("theme_override_styles/")) {
102
String dname = name.get_slicec('/', 1);
103
add_theme_style_override(dname, p_value);
104
} else if (name.begins_with("theme_override_fonts/")) {
105
String dname = name.get_slicec('/', 1);
106
add_theme_font_override(dname, p_value);
107
} else if (name.begins_with("theme_override_font_sizes/")) {
108
String dname = name.get_slicec('/', 1);
109
add_theme_font_size_override(dname, p_value);
110
} else if (name.begins_with("theme_override_colors/")) {
111
String dname = name.get_slicec('/', 1);
112
add_theme_color_override(dname, p_value);
113
} else if (name.begins_with("theme_override_constants/")) {
114
String dname = name.get_slicec('/', 1);
115
add_theme_constant_override(dname, p_value);
116
} else {
117
return false;
118
}
119
}
120
return true;
121
}
122
123
bool Window::_get(const StringName &p_name, Variant &r_ret) const {
124
ERR_READ_THREAD_GUARD_V(false);
125
String sname = p_name;
126
127
if (!sname.begins_with("theme_override")) {
128
return false;
129
}
130
131
if (sname.begins_with("theme_override_icons/")) {
132
String name = sname.get_slicec('/', 1);
133
r_ret = theme_icon_override.has(name) ? Variant(theme_icon_override[name]) : Variant();
134
} else if (sname.begins_with("theme_override_styles/")) {
135
String name = sname.get_slicec('/', 1);
136
r_ret = theme_style_override.has(name) ? Variant(theme_style_override[name]) : Variant();
137
} else if (sname.begins_with("theme_override_fonts/")) {
138
String name = sname.get_slicec('/', 1);
139
r_ret = theme_font_override.has(name) ? Variant(theme_font_override[name]) : Variant();
140
} else if (sname.begins_with("theme_override_font_sizes/")) {
141
String name = sname.get_slicec('/', 1);
142
r_ret = theme_font_size_override.has(name) ? Variant(theme_font_size_override[name]) : Variant();
143
} else if (sname.begins_with("theme_override_colors/")) {
144
String name = sname.get_slicec('/', 1);
145
r_ret = theme_color_override.has(name) ? Variant(theme_color_override[name]) : Variant();
146
} else if (sname.begins_with("theme_override_constants/")) {
147
String name = sname.get_slicec('/', 1);
148
r_ret = theme_constant_override.has(name) ? Variant(theme_constant_override[name]) : Variant();
149
} else {
150
return false;
151
}
152
153
return true;
154
}
155
156
void Window::_get_property_list(List<PropertyInfo> *p_list) const {
157
ERR_READ_THREAD_GUARD;
158
159
Ref<Theme> default_theme = ThemeDB::get_singleton()->get_default_theme();
160
161
p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
162
163
{
164
List<StringName> names;
165
default_theme->get_color_list(get_class_name(), &names);
166
for (const StringName &E : names) {
167
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
168
if (theme_color_override.has(E)) {
169
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
170
}
171
172
p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E, PROPERTY_HINT_NONE, "", usage));
173
}
174
}
175
{
176
List<StringName> names;
177
default_theme->get_constant_list(get_class_name(), &names);
178
for (const StringName &E : names) {
179
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
180
if (theme_constant_override.has(E)) {
181
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
182
}
183
184
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E, PROPERTY_HINT_RANGE, "-16384,16384", usage));
185
}
186
}
187
{
188
List<StringName> names;
189
default_theme->get_font_list(get_class_name(), &names);
190
for (const StringName &E : names) {
191
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
192
if (theme_font_override.has(E)) {
193
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
194
}
195
196
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, Font::get_class_static(), usage));
197
}
198
}
199
{
200
List<StringName> names;
201
default_theme->get_font_size_list(get_class_name(), &names);
202
for (const StringName &E : names) {
203
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
204
if (theme_font_size_override.has(E)) {
205
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
206
}
207
208
p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_font_sizes") + String("/") + E, PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px", usage));
209
}
210
}
211
{
212
List<StringName> names;
213
default_theme->get_icon_list(get_class_name(), &names);
214
for (const StringName &E : names) {
215
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
216
if (theme_icon_override.has(E)) {
217
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
218
}
219
220
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static(), usage));
221
}
222
}
223
{
224
List<StringName> names;
225
default_theme->get_stylebox_list(get_class_name(), &names);
226
for (const StringName &E : names) {
227
uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
228
if (theme_style_override.has(E)) {
229
usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
230
}
231
232
p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, StyleBox::get_class_static(), usage));
233
}
234
}
235
}
236
237
void Window::_validate_property(PropertyInfo &p_property) const {
238
if (p_property.name == "position") {
239
if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
240
p_property.usage = PROPERTY_USAGE_NONE;
241
}
242
} else if (p_property.name == "current_screen") {
243
if (initial_position != WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
244
p_property.usage = PROPERTY_USAGE_NONE;
245
}
246
} else if (Engine::get_singleton()->is_editor_hint() && p_property.name == "theme_type_variation") {
247
List<StringName> names;
248
249
ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
250
251
// Iterate to find all themes.
252
Control *tmp_control = Object::cast_to<Control>(get_parent());
253
Window *tmp_window = Object::cast_to<Window>(get_parent());
254
while (tmp_control || tmp_window) {
255
// We go up and any non Control/Window will break the chain.
256
if (tmp_control) {
257
if (tmp_control->get_theme().is_valid()) {
258
tmp_control->get_theme()->get_type_variation_list(get_class_name(), &names);
259
}
260
tmp_window = Object::cast_to<Window>(tmp_control->get_parent());
261
tmp_control = Object::cast_to<Control>(tmp_control->get_parent());
262
} else { // Window.
263
if (tmp_window->get_theme().is_valid()) {
264
tmp_window->get_theme()->get_type_variation_list(get_class_name(), &names);
265
}
266
tmp_control = Object::cast_to<Control>(tmp_window->get_parent());
267
tmp_window = Object::cast_to<Window>(tmp_window->get_parent());
268
}
269
}
270
if (get_theme().is_valid()) {
271
get_theme()->get_type_variation_list(get_class_name(), &names);
272
}
273
if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
274
ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
275
}
276
names.sort_custom<StringName::AlphCompare>();
277
278
Vector<StringName> unique_names;
279
String hint_string;
280
for (const StringName &E : names) {
281
// Skip duplicate values.
282
if (unique_names.has(E)) {
283
continue;
284
}
285
286
hint_string += String(E) + ",";
287
unique_names.append(E);
288
}
289
290
p_property.hint_string = hint_string;
291
}
292
}
293
294
//
295
296
Window *Window::get_from_id(DisplayServer::WindowID p_window_id) {
297
if (p_window_id == DisplayServer::INVALID_WINDOW_ID) {
298
return nullptr;
299
}
300
return ObjectDB::get_instance<Window>(DisplayServer::get_singleton()->window_get_attached_instance_id(p_window_id));
301
}
302
303
void Window::set_title(const String &p_title) {
304
ERR_MAIN_THREAD_GUARD;
305
306
title = p_title;
307
_update_displayed_title();
308
309
emit_signal("title_changed");
310
}
311
312
String Window::get_title() const {
313
ERR_READ_THREAD_GUARD_V(String());
314
return title;
315
}
316
317
String Window::get_displayed_title() const {
318
ERR_READ_THREAD_GUARD_V(String());
319
return displayed_title;
320
}
321
322
void Window::_settings_changed() {
323
if (visible && initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE && is_in_edited_scene_root()) {
324
Size2 screen_size = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
325
position = (screen_size - size) / 2;
326
if (embedder) {
327
embedder->_sub_window_update(this);
328
}
329
}
330
}
331
332
void Window::set_initial_position(Window::WindowInitialPosition p_initial_position) {
333
ERR_MAIN_THREAD_GUARD;
334
335
initial_position = p_initial_position;
336
_settings_changed();
337
notify_property_list_changed();
338
}
339
340
Window::WindowInitialPosition Window::get_initial_position() const {
341
ERR_READ_THREAD_GUARD_V(WINDOW_INITIAL_POSITION_ABSOLUTE);
342
return initial_position;
343
}
344
345
void Window::set_current_screen(int p_screen) {
346
ERR_MAIN_THREAD_GUARD;
347
348
current_screen = p_screen;
349
if (window_id == DisplayServer::INVALID_WINDOW_ID) {
350
return;
351
}
352
DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
353
}
354
355
int Window::get_current_screen() const {
356
ERR_READ_THREAD_GUARD_V(0);
357
358
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
359
current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
360
}
361
return current_screen;
362
}
363
364
void Window::set_position(const Point2i &p_position) {
365
ERR_MAIN_THREAD_GUARD;
366
367
position = p_position;
368
369
if (embedder) {
370
embedder->_sub_window_update(this);
371
372
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
373
DisplayServer::get_singleton()->window_set_position(p_position, window_id);
374
}
375
}
376
377
Point2i Window::get_position() const {
378
ERR_READ_THREAD_GUARD_V(Point2i());
379
380
return position;
381
}
382
383
void Window::move_to_center() {
384
ERR_MAIN_THREAD_GUARD;
385
ERR_FAIL_COND(!is_inside_tree());
386
387
Rect2 parent_rect;
388
389
if (is_embedded()) {
390
parent_rect = get_embedder()->get_visible_rect();
391
} else {
392
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
393
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
394
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
395
}
396
397
if (parent_rect != Rect2()) {
398
set_position(parent_rect.position + (parent_rect.size - get_size()) / 2);
399
}
400
}
401
402
void Window::set_size(const Size2i &p_size) {
403
ERR_MAIN_THREAD_GUARD;
404
#if defined(ANDROID_ENABLED)
405
if (!get_parent() && is_inside_tree()) {
406
// Can't set root window size on Android.
407
return;
408
}
409
#endif
410
411
size = p_size;
412
_update_window_size();
413
_settings_changed();
414
}
415
416
Size2i Window::get_size() const {
417
ERR_READ_THREAD_GUARD_V(Size2i());
418
return size;
419
}
420
421
void Window::reset_size() {
422
ERR_MAIN_THREAD_GUARD;
423
set_size(Size2i());
424
}
425
426
Point2i Window::get_position_with_decorations() const {
427
ERR_READ_THREAD_GUARD_V(Point2i());
428
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
429
return DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
430
}
431
if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
432
Size2 border_offset;
433
if (theme_cache.embedded_border.is_valid()) {
434
border_offset = theme_cache.embedded_border->get_offset();
435
}
436
if (theme_cache.embedded_unfocused_border.is_valid()) {
437
border_offset = border_offset.max(theme_cache.embedded_unfocused_border->get_offset());
438
}
439
return position - border_offset;
440
}
441
return position;
442
}
443
444
Size2i Window::get_size_with_decorations() const {
445
ERR_READ_THREAD_GUARD_V(Size2i());
446
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
447
return DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
448
}
449
if (visible && is_embedded() && !get_flag(Window::FLAG_BORDERLESS)) {
450
Size2 border_size;
451
if (theme_cache.embedded_border.is_valid()) {
452
border_size = theme_cache.embedded_border->get_minimum_size();
453
}
454
if (theme_cache.embedded_unfocused_border.is_valid()) {
455
border_size = border_size.max(theme_cache.embedded_unfocused_border->get_minimum_size());
456
}
457
return size + border_size;
458
}
459
return size;
460
}
461
462
Size2i Window::_clamp_limit_size(const Size2i &p_limit_size) {
463
// Force window limits to respect size limitations of rendering server.
464
Size2i max_window_size = RS::get_singleton()->get_maximum_viewport_size();
465
if (max_window_size != Size2i()) {
466
return p_limit_size.clamp(Vector2i(), max_window_size);
467
} else {
468
return p_limit_size.maxi(0);
469
}
470
}
471
472
void Window::_validate_limit_size() {
473
// When max_size is invalid, max_size_used falls back to respect size limitations of rendering server.
474
bool max_size_valid = (max_size.x > 0 || max_size.y > 0) && max_size.x >= min_size.x && max_size.y >= min_size.y;
475
max_size_used = max_size_valid ? max_size : RS::get_singleton()->get_maximum_viewport_size();
476
}
477
478
void Window::set_max_size(const Size2i &p_max_size) {
479
ERR_MAIN_THREAD_GUARD;
480
#if defined(ANDROID_ENABLED)
481
if (!get_parent() && is_inside_tree()) {
482
// Can't set root window size on Android.
483
return;
484
}
485
#endif
486
Size2i max_size_clamped = _clamp_limit_size(p_max_size);
487
if (max_size == max_size_clamped) {
488
return;
489
}
490
max_size = max_size_clamped;
491
492
_validate_limit_size();
493
_update_window_size();
494
}
495
496
Size2i Window::get_max_size() const {
497
ERR_READ_THREAD_GUARD_V(Size2i());
498
return max_size;
499
}
500
501
void Window::set_min_size(const Size2i &p_min_size) {
502
ERR_MAIN_THREAD_GUARD;
503
#if defined(ANDROID_ENABLED)
504
if (!get_parent() && is_inside_tree()) {
505
// Can't set root window size on Android.
506
return;
507
}
508
#endif
509
Size2i min_size_clamped = _clamp_limit_size(p_min_size);
510
if (min_size == min_size_clamped) {
511
return;
512
}
513
min_size = min_size_clamped;
514
515
_validate_limit_size();
516
_update_window_size();
517
}
518
519
Size2i Window::get_min_size() const {
520
ERR_READ_THREAD_GUARD_V(Size2i());
521
return min_size;
522
}
523
524
void Window::set_mode(Mode p_mode) {
525
ERR_MAIN_THREAD_GUARD;
526
mode = p_mode;
527
528
if (embedder) {
529
embedder->_sub_window_update(this);
530
531
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
532
DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
533
}
534
}
535
536
Window::Mode Window::get_mode() const {
537
ERR_READ_THREAD_GUARD_V(MODE_WINDOWED);
538
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
539
mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
540
}
541
return mode;
542
}
543
544
void Window::set_flag(Flags p_flag, bool p_enabled) {
545
ERR_MAIN_THREAD_GUARD;
546
ERR_FAIL_INDEX(p_flag, FLAG_MAX);
547
flags[p_flag] = p_enabled;
548
549
if (p_flag == FLAG_TRANSPARENT) {
550
set_transparent_background(p_enabled);
551
}
552
553
if (embedder) {
554
embedder->_sub_window_update(this);
555
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
556
if (!is_in_edited_scene_root()) {
557
DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
558
}
559
}
560
}
561
562
bool Window::get_flag(Flags p_flag) const {
563
ERR_READ_THREAD_GUARD_V(false);
564
ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
565
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
566
if (!is_in_edited_scene_root()) {
567
flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
568
}
569
}
570
return flags[p_flag];
571
}
572
573
bool Window::is_popup() const {
574
return get_flag(Window::FLAG_POPUP) || get_flag(Window::FLAG_NO_FOCUS);
575
}
576
577
bool Window::is_maximize_allowed() const {
578
ERR_READ_THREAD_GUARD_V(false);
579
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
580
return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
581
}
582
return true;
583
}
584
585
void Window::request_attention() {
586
ERR_MAIN_THREAD_GUARD;
587
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
588
DisplayServer::get_singleton()->window_request_attention(window_id);
589
}
590
}
591
592
#ifndef DISABLE_DEPRECATED
593
void Window::move_to_foreground() {
594
WARN_DEPRECATED_MSG(R"*(The "move_to_foreground()" method is deprecated, use "grab_focus()" instead.)*");
595
grab_focus();
596
}
597
#endif // DISABLE_DEPRECATED
598
599
bool Window::can_draw() const {
600
ERR_READ_THREAD_GUARD_V(false);
601
if (!is_inside_tree()) {
602
return false;
603
}
604
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
605
return DisplayServer::get_singleton()->window_can_draw(window_id);
606
}
607
608
return visible;
609
}
610
611
void Window::set_ime_active(bool p_active) {
612
ERR_MAIN_THREAD_GUARD;
613
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
614
DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
615
}
616
}
617
618
void Window::set_ime_position(const Point2i &p_pos) {
619
ERR_MAIN_THREAD_GUARD;
620
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
621
DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
622
}
623
}
624
625
bool Window::is_embedded() const {
626
ERR_READ_THREAD_GUARD_V(false);
627
return get_embedder() != nullptr;
628
}
629
630
bool Window::is_in_edited_scene_root() const {
631
ERR_READ_THREAD_GUARD_V(false);
632
#ifdef TOOLS_ENABLED
633
return is_part_of_edited_scene();
634
#else
635
return false;
636
#endif
637
}
638
639
void Window::_make_window() {
640
ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
641
642
if (transient && transient_to_focused) {
643
_make_transient();
644
}
645
646
uint32_t f = 0;
647
for (int i = 0; i < FLAG_MAX; i++) {
648
if (flags[i]) {
649
f |= (1 << i);
650
}
651
}
652
653
DisplayServer::VSyncMode vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
654
Rect2i window_rect;
655
if (initial_position == WINDOW_INITIAL_POSITION_ABSOLUTE) {
656
window_rect = Rect2i(position, size);
657
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN) {
658
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_PRIMARY) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_PRIMARY) - size) / 2, size);
659
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN) {
660
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_OF_MAIN_WINDOW) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_OF_MAIN_WINDOW) - size) / 2, size);
661
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
662
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(current_screen) + (DisplayServer::get_singleton()->screen_get_size(current_screen) - size) / 2, size);
663
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS) {
664
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) - size) / 2, size);
665
} else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS) {
666
window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) - size) / 2, size);
667
}
668
669
window_id = DisplayServer::get_singleton()->create_sub_window(DisplayServer::WindowMode(mode), vsync_mode, f, window_rect, is_in_edited_scene_root() ? false : exclusive, transient_parent ? transient_parent->window_id : DisplayServer::INVALID_WINDOW_ID);
670
ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
671
DisplayServer::get_singleton()->window_set_max_size(Size2i(), window_id);
672
DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
673
DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
674
DisplayServer::get_singleton()->window_set_title(displayed_title, window_id);
675
DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
676
677
_update_window_size();
678
679
if (transient_parent) {
680
for (const Window *E : transient_children) {
681
if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
682
DisplayServer::get_singleton()->window_set_transient(E->window_id, transient_parent->window_id);
683
}
684
}
685
}
686
687
_update_window_callbacks();
688
689
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
690
DisplayServer::get_singleton()->show_window(window_id);
691
}
692
693
void Window::_update_from_window() {
694
ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
695
mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
696
for (int i = 0; i < FLAG_MAX; i++) {
697
flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
698
}
699
}
700
701
void Window::_clear_window() {
702
ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
703
704
bool had_focus = has_focus();
705
706
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
707
float win_scale = DisplayServer::get_singleton()->window_get_scale(window_id);
708
709
Size2i adjusted_size = Size2i(size.width / win_scale, size.height / win_scale);
710
Size2i adjusted_pos = Size2i(position.x / win_scale, position.y / win_scale);
711
712
_rect_changed_callback(Rect2i(adjusted_pos, adjusted_size));
713
}
714
715
if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
716
DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
717
}
718
719
for (const Window *E : transient_children) {
720
if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
721
DisplayServer::get_singleton()->window_set_transient(E->window_id, DisplayServer::INVALID_WINDOW_ID);
722
}
723
}
724
725
_update_from_window();
726
727
DisplayServer::get_singleton()->delete_sub_window(window_id);
728
window_id = DisplayServer::INVALID_WINDOW_ID;
729
730
// If closing window was focused and has a parent, return focus.
731
if (had_focus && transient_parent) {
732
transient_parent->grab_focus();
733
}
734
735
_update_viewport_size();
736
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
737
738
if (transient && transient_to_focused) {
739
_clear_transient();
740
}
741
}
742
743
void Window::_rect_changed_callback(const Rect2i &p_callback) {
744
//we must always accept this as the truth
745
if (size == p_callback.size && position == p_callback.position) {
746
return;
747
}
748
749
if (position != p_callback.position) {
750
position = p_callback.position;
751
_propagate_window_notification(this, NOTIFICATION_WM_POSITION_CHANGED);
752
}
753
754
if (size != p_callback.size) {
755
size = p_callback.size;
756
_update_viewport_size();
757
}
758
if (window_id != DisplayServer::INVALID_WINDOW_ID && !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS)) {
759
Vector2 sz_out = DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
760
Vector2 pos_out = DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
761
Vector2 sz_in = DisplayServer::get_singleton()->window_get_size(window_id);
762
Vector2 pos_in = DisplayServer::get_singleton()->window_get_position(window_id);
763
DisplayServer::get_singleton()->accessibility_set_window_rect(window_id, Rect2(pos_out, sz_out), Rect2(pos_in, sz_in));
764
}
765
queue_accessibility_update();
766
}
767
768
void Window::_propagate_window_notification(Node *p_node, int p_notification) {
769
p_node->notification(p_notification);
770
for (int i = 0; i < p_node->get_child_count(); i++) {
771
Node *child = p_node->get_child(i);
772
Window *window = Object::cast_to<Window>(child);
773
if (window) {
774
continue;
775
}
776
_propagate_window_notification(child, p_notification);
777
}
778
}
779
780
void Window::_event_callback(DisplayServer::WindowEvent p_event) {
781
switch (p_event) {
782
case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
783
if (!is_inside_tree()) {
784
return;
785
}
786
Window *root = get_tree()->get_root();
787
if (mouse_in_window && root->gui.windowmanager_window_over == this) {
788
return;
789
}
790
if (root->gui.windowmanager_window_over) {
791
#ifdef DEV_ENABLED
792
WARN_PRINT_ONCE("Entering a window while a window is hovered should never happen in DisplayServer.");
793
#endif // DEV_ENABLED
794
root->gui.windowmanager_window_over->_event_callback(DisplayServer::WINDOW_EVENT_MOUSE_EXIT);
795
}
796
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
797
root->gui.windowmanager_window_over = this;
798
mouse_in_window = true;
799
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
800
DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
801
}
802
} break;
803
case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
804
if (!is_inside_tree()) {
805
return;
806
}
807
// Ensure keeping the order of input events and window events when input events are buffered or accumulated.
808
Input::get_singleton()->flush_buffered_events();
809
810
Window *root = get_tree()->get_root();
811
if (!root->gui.windowmanager_window_over) {
812
#ifdef DEV_ENABLED
813
WARN_PRINT_ONCE("Exiting a window while no window is hovered should never happen in DisplayServer.");
814
#endif // DEV_ENABLED
815
return;
816
}
817
mouse_in_window = false;
818
root->gui.windowmanager_window_over->_mouse_leave_viewport();
819
root->gui.windowmanager_window_over = nullptr;
820
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
821
} break;
822
case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
823
focused = true;
824
focused_window = this;
825
_propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
826
emit_signal(SceneStringName(focus_entered));
827
if (get_tree() && get_tree()->is_accessibility_enabled()) {
828
Window *w = exclusive_child;
829
while (w) {
830
if (w->exclusive_child) {
831
w = w->exclusive_child;
832
} else {
833
w->grab_focus();
834
break;
835
}
836
}
837
}
838
} break;
839
case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
840
focused = false;
841
if (focused_window == this) {
842
focused_window = nullptr;
843
}
844
_propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
845
emit_signal(SceneStringName(focus_exited));
846
} break;
847
case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
848
if (exclusive_child != nullptr) {
849
break; //has an exclusive child, can't get events until child is closed
850
}
851
_propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
852
emit_signal(SNAME("close_requested"));
853
} break;
854
case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
855
_propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
856
emit_signal(SNAME("go_back_requested"));
857
} break;
858
case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
859
_update_viewport_size();
860
_propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
861
emit_signal(SNAME("dpi_changed"));
862
} break;
863
case DisplayServer::WINDOW_EVENT_TITLEBAR_CHANGE: {
864
emit_signal(SNAME("titlebar_changed"));
865
} break;
866
case DisplayServer::WINDOW_EVENT_FORCE_CLOSE: {
867
hide();
868
} break;
869
}
870
}
871
872
void Window::update_mouse_cursor_state() {
873
ERR_MAIN_THREAD_GUARD;
874
// Update states based on mouse cursor position.
875
// This includes updated mouse_enter or mouse_exit signals or the current mouse cursor shape.
876
// These details are set in Viewport::_gui_input_event. To instantly
877
// see the changes in the viewport, we need to trigger a mouse motion event.
878
// This function should be called whenever scene tree changes affect the mouse cursor.
879
Ref<InputEventMouseMotion> mm;
880
Vector2 pos = get_mouse_position();
881
Transform2D xform = get_global_canvas_transform().affine_inverse();
882
mm.instantiate();
883
mm->set_position(pos);
884
mm->set_global_position(xform.xform(pos));
885
mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
886
push_input(mm, true);
887
}
888
889
void Window::show() {
890
ERR_MAIN_THREAD_GUARD;
891
set_visible(true);
892
}
893
894
void Window::hide() {
895
ERR_MAIN_THREAD_GUARD;
896
set_visible(false);
897
}
898
899
void Window::_accessibility_notify_enter(Node *p_node) {
900
p_node->queue_accessibility_update();
901
902
if (p_node != this) {
903
const Window *window = Object::cast_to<Window>(p_node);
904
if (window) {
905
return;
906
}
907
}
908
909
for (int i = 0; i < p_node->get_child_count(); i++) {
910
_accessibility_notify_enter(p_node->get_child(i));
911
}
912
}
913
914
void Window::_accessibility_notify_exit(Node *p_node) {
915
p_node->notification(Node::NOTIFICATION_ACCESSIBILITY_INVALIDATE);
916
917
if (p_node != this) {
918
const Window *window = Object::cast_to<Window>(p_node);
919
if (window) {
920
return;
921
}
922
}
923
924
for (int i = 0; i < p_node->get_child_count(); i++) {
925
_accessibility_notify_exit(p_node->get_child(i));
926
}
927
}
928
929
void Window::set_visible(bool p_visible) {
930
ERR_MAIN_THREAD_GUARD;
931
if (visible == p_visible) {
932
return;
933
}
934
935
if (!is_inside_tree()) {
936
visible = p_visible;
937
return;
938
}
939
940
ERR_FAIL_NULL_MSG(get_parent(), "Can't change visibility of main window.");
941
942
visible = p_visible;
943
944
// Stop any queued resizing, as the window will be resized right now.
945
updating_child_controls = false;
946
947
Viewport *embedder_vp = get_embedder();
948
949
if (!embedder_vp) {
950
if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
951
_clear_window();
952
}
953
if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
954
_make_window();
955
}
956
} else {
957
if (visible) {
958
embedder = embedder_vp;
959
if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
960
if (is_in_edited_scene_root()) {
961
Size2 screen_size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
962
position = (screen_size - size) / 2;
963
} else {
964
position = (embedder->get_visible_rect().size - size) / 2;
965
}
966
}
967
embedder->_sub_window_register(this);
968
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
969
} else {
970
embedder->_sub_window_remove(this);
971
embedder = nullptr;
972
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
973
}
974
_update_window_size();
975
}
976
977
if (visible) {
978
if (get_tree() && get_tree()->is_accessibility_supported()) {
979
get_tree()->_accessibility_force_update();
980
_accessibility_notify_enter(this);
981
}
982
} else {
983
if (get_tree() && get_tree()->is_accessibility_supported()) {
984
_accessibility_notify_exit(this);
985
}
986
focused = false;
987
if (focused_window == this) {
988
focused_window = nullptr;
989
}
990
}
991
if (get_parent()) {
992
get_parent()->queue_accessibility_update();
993
}
994
if (embedder) {
995
embedder->queue_accessibility_update();
996
}
997
998
notification(NOTIFICATION_VISIBILITY_CHANGED);
999
emit_signal(SceneStringName(visibility_changed));
1000
1001
RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
1002
1003
//update transient exclusive
1004
if (transient_parent) {
1005
_set_transient_exclusive_child(true);
1006
}
1007
}
1008
1009
void Window::_clear_transient() {
1010
if (transient_parent) {
1011
if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
1012
DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
1013
}
1014
transient_parent->transient_children.erase(this);
1015
if (transient_parent->exclusive_child == this) {
1016
transient_parent->exclusive_child = nullptr;
1017
}
1018
transient_parent = nullptr;
1019
}
1020
}
1021
1022
void Window::_make_transient() {
1023
if (!get_parent()) {
1024
//main window, can't be transient
1025
return;
1026
}
1027
//find transient parent
1028
1029
Window *window = nullptr;
1030
1031
if (!is_embedded() && transient_to_focused) {
1032
DisplayServer::WindowID focused_window_id = DisplayServer::get_singleton()->get_focused_window();
1033
if (focused_window_id != DisplayServer::INVALID_WINDOW_ID) {
1034
window = Window::get_from_id(focused_window_id);
1035
}
1036
}
1037
1038
if (!window) {
1039
Viewport *vp = get_parent()->get_viewport();
1040
while (vp) {
1041
window = Object::cast_to<Window>(vp);
1042
if (window) {
1043
break;
1044
}
1045
if (!vp->get_parent()) {
1046
break;
1047
}
1048
1049
vp = vp->get_parent()->get_viewport();
1050
}
1051
}
1052
1053
if (window) {
1054
transient_parent = window;
1055
window->transient_children.insert(this);
1056
_set_transient_exclusive_child();
1057
}
1058
1059
//see if we can make transient
1060
if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
1061
DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
1062
}
1063
}
1064
1065
void Window::_set_transient_exclusive_child(bool p_clear_invalid) {
1066
if (exclusive && visible && is_inside_tree()) {
1067
if (!is_in_edited_scene_root()) {
1068
// Transient parent has another exclusive child.
1069
if (transient_parent->exclusive_child && transient_parent->exclusive_child != this) {
1070
ERR_PRINT(vformat("Attempting to make child window exclusive, but the parent window already has another exclusive child. This window: %s, parent window: %s, current exclusive child window: %s", get_description(), transient_parent->get_description(), transient_parent->exclusive_child->get_description()));
1071
}
1072
transient_parent->exclusive_child = this;
1073
}
1074
} else if (p_clear_invalid) {
1075
if (transient_parent->exclusive_child == this) {
1076
transient_parent->exclusive_child = nullptr;
1077
}
1078
}
1079
}
1080
1081
void Window::set_transient(bool p_transient) {
1082
ERR_MAIN_THREAD_GUARD;
1083
if (transient == p_transient) {
1084
return;
1085
}
1086
1087
transient = p_transient;
1088
1089
if (!is_inside_tree()) {
1090
return;
1091
}
1092
1093
if (transient) {
1094
if (!transient_to_focused) {
1095
_make_transient();
1096
}
1097
} else {
1098
_clear_transient();
1099
}
1100
}
1101
1102
bool Window::is_transient() const {
1103
return transient;
1104
}
1105
1106
void Window::set_transient_to_focused(bool p_transient_to_focused) {
1107
ERR_MAIN_THREAD_GUARD;
1108
if (transient_to_focused == p_transient_to_focused) {
1109
return;
1110
}
1111
1112
transient_to_focused = p_transient_to_focused;
1113
}
1114
1115
bool Window::is_transient_to_focused() const {
1116
ERR_READ_THREAD_GUARD_V(false);
1117
return transient_to_focused;
1118
}
1119
1120
void Window::set_exclusive(bool p_exclusive) {
1121
ERR_MAIN_THREAD_GUARD;
1122
if (exclusive == p_exclusive) {
1123
return;
1124
}
1125
1126
exclusive = p_exclusive;
1127
1128
if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
1129
if (is_in_edited_scene_root()) {
1130
DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
1131
} else {
1132
DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
1133
}
1134
}
1135
1136
if (transient_parent) {
1137
_set_transient_exclusive_child(true);
1138
}
1139
}
1140
1141
bool Window::is_exclusive() const {
1142
ERR_READ_THREAD_GUARD_V(false);
1143
return exclusive;
1144
}
1145
1146
bool Window::is_visible() const {
1147
ERR_READ_THREAD_GUARD_V(false);
1148
return visible;
1149
}
1150
1151
Size2i Window::_clamp_window_size(const Size2i &p_size) {
1152
Size2i window_size_clamped = p_size;
1153
Size2 minsize = get_clamped_minimum_size();
1154
window_size_clamped = window_size_clamped.max(minsize);
1155
1156
if (max_size_used != Size2i()) {
1157
window_size_clamped = window_size_clamped.min(max_size_used);
1158
}
1159
1160
return window_size_clamped;
1161
}
1162
1163
void Window::_update_window_size() {
1164
Size2i size_limit = get_clamped_minimum_size();
1165
if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID && keep_title_visible) {
1166
Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(displayed_title, window_id);
1167
size_limit = size_limit.max(title_size);
1168
}
1169
1170
size = size.max(size_limit);
1171
1172
bool reset_min_first = false;
1173
1174
if (max_size_used != Size2i()) {
1175
// Force window size to respect size limitations of max_size_used.
1176
size = size.min(max_size_used);
1177
1178
if (size_limit.x > max_size_used.x) {
1179
size_limit.x = max_size_used.x;
1180
reset_min_first = true;
1181
}
1182
if (size_limit.y > max_size_used.y) {
1183
size_limit.y = max_size_used.y;
1184
reset_min_first = true;
1185
}
1186
}
1187
1188
if (embedder) {
1189
size = size.maxi(1);
1190
1191
embedder->_sub_window_update(this);
1192
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1193
// When main window embedded in the editor, we can't resize the main window.
1194
if (window_id != DisplayServer::MAIN_WINDOW_ID || !Engine::get_singleton()->is_embedded_in_editor()) {
1195
if (reset_min_first && wrap_controls) {
1196
// Avoid an error if setting max_size to a value between min_size and the previous size_limit.
1197
DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
1198
}
1199
1200
DisplayServer::get_singleton()->window_set_max_size(max_size_used, window_id);
1201
DisplayServer::get_singleton()->window_set_min_size(size_limit, window_id);
1202
DisplayServer::get_singleton()->window_set_size(size, window_id);
1203
} else if (Engine::get_singleton()->is_embedded_in_editor()) {
1204
size = DisplayServer::get_singleton()->window_get_size(window_id); // Reset size.
1205
}
1206
}
1207
1208
//update the viewport
1209
_update_viewport_size();
1210
}
1211
1212
void Window::_update_viewport_size() {
1213
//update the viewport part
1214
1215
Size2i final_size;
1216
Size2 final_size_override;
1217
Rect2i attach_to_screen_rect(Point2i(), size);
1218
window_transform = Transform2D();
1219
1220
if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
1221
// We always want to make sure that the content scale factor is a whole
1222
// number, else there will be pixel wobble no matter what.
1223
content_scale_factor = Math::floor(content_scale_factor);
1224
1225
// A content scale factor of zero is pretty useless.
1226
if (content_scale_factor < 1) {
1227
content_scale_factor = 1;
1228
}
1229
}
1230
1231
if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
1232
final_size = size;
1233
final_size_override = Size2(size) / content_scale_factor;
1234
} else {
1235
//actual screen video mode
1236
Size2 video_mode = size;
1237
Size2 desired_res = content_scale_size;
1238
1239
Size2 viewport_size;
1240
Size2 screen_size;
1241
1242
float viewport_aspect = desired_res.aspect();
1243
float video_mode_aspect = video_mode.aspect();
1244
1245
if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
1246
//same aspect or ignore aspect
1247
viewport_size = desired_res;
1248
screen_size = video_mode;
1249
} else if (viewport_aspect < video_mode_aspect) {
1250
// screen ratio is smaller vertically
1251
1252
if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
1253
//will stretch horizontally
1254
viewport_size.x = desired_res.y * video_mode_aspect;
1255
viewport_size.y = desired_res.y;
1256
screen_size = video_mode;
1257
1258
} else {
1259
//will need black bars
1260
viewport_size = desired_res;
1261
screen_size.x = video_mode.y * viewport_aspect;
1262
screen_size.y = video_mode.y;
1263
}
1264
} else {
1265
//screen ratio is smaller horizontally
1266
if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
1267
//will stretch horizontally
1268
viewport_size.x = desired_res.x;
1269
viewport_size.y = desired_res.x / video_mode_aspect;
1270
screen_size = video_mode;
1271
1272
} else {
1273
//will need black bars
1274
viewport_size = desired_res;
1275
screen_size.x = video_mode.x;
1276
screen_size.y = video_mode.x / viewport_aspect;
1277
}
1278
}
1279
1280
screen_size = screen_size.floor();
1281
viewport_size = viewport_size.floor();
1282
1283
if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
1284
Size2i screen_scale = (screen_size / viewport_size).floor();
1285
int scale_factor = MIN(screen_scale.x, screen_scale.y);
1286
1287
if (scale_factor < 1) {
1288
scale_factor = 1;
1289
}
1290
1291
screen_size = viewport_size * scale_factor;
1292
}
1293
1294
Size2 margin;
1295
Size2 offset;
1296
1297
if (screen_size.x < video_mode.x) {
1298
margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
1299
offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
1300
}
1301
1302
if (screen_size.y < video_mode.y) {
1303
margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
1304
offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
1305
}
1306
1307
switch (content_scale_mode) {
1308
case CONTENT_SCALE_MODE_DISABLED: {
1309
} break;
1310
case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
1311
final_size = screen_size;
1312
final_size_override = viewport_size / content_scale_factor;
1313
attach_to_screen_rect = Rect2(margin, screen_size);
1314
1315
window_transform.translate_local(margin);
1316
} break;
1317
case CONTENT_SCALE_MODE_VIEWPORT: {
1318
final_size = (viewport_size / content_scale_factor).floor();
1319
attach_to_screen_rect = Rect2(margin, screen_size);
1320
1321
window_transform.translate_local(margin);
1322
if (final_size.x != 0 && final_size.y != 0) {
1323
Transform2D scale_transform;
1324
scale_transform.scale(Vector2(attach_to_screen_rect.size) / Vector2(final_size));
1325
window_transform *= scale_transform;
1326
}
1327
} break;
1328
}
1329
}
1330
1331
bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
1332
_set_size(final_size, final_size_override, allocate);
1333
1334
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1335
RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
1336
} else if (!is_embedded()) {
1337
RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
1338
}
1339
1340
notification(NOTIFICATION_WM_SIZE_CHANGED);
1341
1342
if (embedder) {
1343
float scale = MIN(embedder->stretch_transform.get_scale().width, embedder->stretch_transform.get_scale().height);
1344
Viewport::set_oversampling_override(scale);
1345
Size2 s = Size2(final_size.width * scale, final_size.height * scale).ceil();
1346
RS::get_singleton()->viewport_set_global_canvas_transform(get_viewport_rid(), global_canvas_transform * scale * content_scale_factor);
1347
RS::get_singleton()->viewport_set_size(get_viewport_rid(), s.width, s.height);
1348
embedder->_sub_window_update(this);
1349
}
1350
}
1351
1352
void Window::_update_window_callbacks() {
1353
DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
1354
DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
1355
DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
1356
DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
1357
DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
1358
}
1359
1360
void Window::set_force_native(bool p_force_native) {
1361
if (force_native == p_force_native) {
1362
return;
1363
}
1364
if (is_visible() && !is_in_edited_scene_root()) {
1365
ERR_FAIL_MSG("Can't change \"force_native\" while a window is displayed. Consider hiding window before changing this value.");
1366
}
1367
if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1368
return;
1369
}
1370
force_native = p_force_native;
1371
if (!is_in_edited_scene_root() && is_inside_tree() && get_tree()->get_root()->is_embedding_subwindows()) {
1372
set_embedding_subwindows(force_native);
1373
}
1374
}
1375
1376
bool Window::get_force_native() const {
1377
return force_native;
1378
}
1379
1380
Viewport *Window::get_embedder() const {
1381
ERR_READ_THREAD_GUARD_V(nullptr);
1382
if (force_native && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS) && !is_in_edited_scene_root()) {
1383
return nullptr;
1384
}
1385
1386
Viewport *vp = get_parent_viewport();
1387
1388
while (vp) {
1389
if (vp->is_embedding_subwindows()) {
1390
return vp;
1391
}
1392
1393
if (vp->get_parent()) {
1394
vp = vp->get_parent()->get_viewport();
1395
} else {
1396
vp = nullptr;
1397
}
1398
}
1399
return nullptr;
1400
}
1401
1402
RID Window::get_accessibility_element() const {
1403
if (!visible || is_part_of_edited_scene()) {
1404
return RID();
1405
}
1406
if (get_embedder() || is_popup()) {
1407
return Node::get_accessibility_element();
1408
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1409
return DisplayServer::get_singleton()->accessibility_get_window_root(window_id);
1410
} else {
1411
return RID();
1412
}
1413
}
1414
1415
RID Window::get_focused_accessibility_element() const {
1416
if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1417
if (get_child_count() > 0) {
1418
return get_child(0)->get_focused_accessibility_element(); // Try scene tree root node.
1419
}
1420
}
1421
return Node::get_focused_accessibility_element();
1422
}
1423
1424
void Window::_notification(int p_what) {
1425
ERR_MAIN_THREAD_GUARD;
1426
switch (p_what) {
1427
case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
1428
accessibility_title_element = RID();
1429
accessibility_announcement_element = RID();
1430
} break;
1431
1432
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1433
RID ae = get_accessibility_element();
1434
ERR_FAIL_COND(ae.is_null());
1435
1436
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_WINDOW);
1437
if (accessibility_name.is_empty()) {
1438
DisplayServer::get_singleton()->accessibility_update_set_name(ae, displayed_title);
1439
} else {
1440
DisplayServer::get_singleton()->accessibility_update_set_name(ae, accessibility_name);
1441
}
1442
DisplayServer::get_singleton()->accessibility_update_set_description(ae, accessibility_description);
1443
DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_MODAL, exclusive);
1444
DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Window::_accessibility_action_grab_focus));
1445
DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !visible);
1446
1447
if (get_embedder() || is_popup()) {
1448
Control *parent_ctrl = Object::cast_to<Control>(get_parent());
1449
Transform2D parent_tr = parent_ctrl ? parent_ctrl->get_global_transform() : Transform2D();
1450
Transform2D tr;
1451
if (window_id == DisplayServer::INVALID_WINDOW_ID) {
1452
tr.set_origin(position);
1453
} else {
1454
Window *np = get_non_popup_window();
1455
if (np) {
1456
tr.set_origin(get_position() - np->get_position());
1457
}
1458
}
1459
DisplayServer::get_singleton()->accessibility_update_set_transform(ae, parent_tr.affine_inverse() * tr);
1460
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), size));
1461
1462
if (accessibility_title_element.is_null()) {
1463
accessibility_title_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_TITLE_BAR);
1464
}
1465
1466
int w = get_theme_constant(SNAME("title_height"));
1467
DisplayServer::get_singleton()->accessibility_update_set_name(accessibility_title_element, displayed_title);
1468
DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_title_element, Rect2(Vector2(0, -w), Size2(size.x, w)));
1469
} else {
1470
DisplayServer::get_singleton()->accessibility_update_set_transform(ae, get_final_transform());
1471
if (_get_size_2d_override() != Size2()) {
1472
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), _get_size_2d_override()));
1473
} else {
1474
DisplayServer::get_singleton()->accessibility_update_set_bounds(ae, Rect2(Point2(), _get_size()));
1475
}
1476
1477
if (accessibility_announcement_element.is_null()) {
1478
accessibility_announcement_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
1479
}
1480
1481
if (announcement.is_empty()) {
1482
DisplayServer::get_singleton()->accessibility_update_set_live(accessibility_announcement_element, DisplayServer::LIVE_OFF);
1483
} else {
1484
DisplayServer::get_singleton()->accessibility_update_set_name(accessibility_announcement_element, announcement);
1485
DisplayServer::get_singleton()->accessibility_update_set_live(accessibility_announcement_element, DisplayServer::LIVE_ASSERTIVE);
1486
}
1487
}
1488
} break;
1489
1490
case NOTIFICATION_POSTINITIALIZE: {
1491
initialized = true;
1492
1493
_invalidate_theme_cache();
1494
_update_theme_item_cache();
1495
} break;
1496
1497
case NOTIFICATION_PARENTED: {
1498
theme_owner->assign_theme_on_parented(this);
1499
} break;
1500
1501
case NOTIFICATION_UNPARENTED: {
1502
theme_owner->clear_theme_on_unparented(this);
1503
} break;
1504
1505
case NOTIFICATION_ENTER_TREE: {
1506
if (is_in_edited_scene_root()) {
1507
if (!ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
1508
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Window::_settings_changed));
1509
}
1510
} else if (get_parent() && get_tree()->get_root()->is_embedding_subwindows()) {
1511
// Is not the main window and main window is embedding.
1512
set_embedding_subwindows(force_native);
1513
}
1514
1515
bool embedded = false;
1516
{
1517
embedder = get_embedder();
1518
if (embedder) {
1519
embedded = true;
1520
if (!visible) {
1521
embedder = nullptr; // Not yet since not visible.
1522
}
1523
}
1524
}
1525
1526
if (embedded) {
1527
// Create as embedded.
1528
if (embedder) {
1529
if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
1530
if (is_in_edited_scene_root()) {
1531
Size2 screen_size = Size2(GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_width"), GLOBAL_GET_CACHED(real_t, "display/window/size/viewport_height"));
1532
position = (screen_size - size) / 2;
1533
} else {
1534
position = (embedder->get_visible_rect().size - size) / 2;
1535
}
1536
}
1537
embedder->_sub_window_register(this);
1538
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
1539
_update_window_size();
1540
}
1541
1542
} else {
1543
if (!get_parent()) {
1544
// It's the root window!
1545
visible = true; // Always visible.
1546
window_id = DisplayServer::MAIN_WINDOW_ID;
1547
focused_window = this;
1548
DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
1549
_update_from_window();
1550
// Since this window already exists (created on start), we must update pos and size from it.
1551
{
1552
position = DisplayServer::get_singleton()->window_get_position(window_id);
1553
size = DisplayServer::get_singleton()->window_get_size(window_id);
1554
focused = DisplayServer::get_singleton()->window_is_focused(window_id);
1555
}
1556
_update_window_size(); // Inform DisplayServer of minimum and maximum size.
1557
_update_viewport_size(); // Then feed back to the viewport.
1558
_update_window_callbacks();
1559
// Simulate mouse-enter event when mouse is over the window, since OS event might arrive before setting callbacks.
1560
if (!mouse_in_window && Rect2(position, size).has_point(DisplayServer::get_singleton()->mouse_get_position())) {
1561
_event_callback(DisplayServer::WINDOW_EVENT_MOUSE_ENTER);
1562
}
1563
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
1564
if (DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(FLAG_TRANSPARENT), window_id)) {
1565
set_transparent_background(true);
1566
}
1567
} else {
1568
// Create.
1569
if (visible) {
1570
_make_window();
1571
}
1572
}
1573
}
1574
1575
if (transient && !transient_to_focused) {
1576
_make_transient();
1577
}
1578
if (visible) {
1579
if (window_id != DisplayServer::MAIN_WINDOW_ID && get_tree() && get_tree()->is_accessibility_supported()) {
1580
get_tree()->_accessibility_force_update();
1581
_accessibility_notify_enter(this);
1582
}
1583
notification(NOTIFICATION_VISIBILITY_CHANGED);
1584
emit_signal(SceneStringName(visibility_changed));
1585
RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
1586
if (get_parent()) {
1587
get_parent()->queue_accessibility_update();
1588
}
1589
if (embedder) {
1590
embedder->queue_accessibility_update();
1591
}
1592
}
1593
1594
// Emits NOTIFICATION_THEME_CHANGED internally.
1595
set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
1596
} break;
1597
1598
case NOTIFICATION_READY: {
1599
if (wrap_controls) {
1600
// Finish any resizing immediately so it doesn't interfere on stuff overriding _ready().
1601
_update_child_controls();
1602
}
1603
} break;
1604
1605
case NOTIFICATION_THEME_CHANGED: {
1606
emit_signal(SceneStringName(theme_changed));
1607
_invalidate_theme_cache();
1608
_update_theme_item_cache();
1609
} break;
1610
1611
case NOTIFICATION_TRANSLATION_CHANGED: {
1612
_invalidate_theme_cache();
1613
_update_theme_item_cache();
1614
_update_displayed_title();
1615
} break;
1616
1617
case NOTIFICATION_VISIBILITY_CHANGED: {
1618
if (unparent_when_invisible && !is_visible()) {
1619
Node *p = get_parent();
1620
if (p) {
1621
p->remove_child(this);
1622
}
1623
}
1624
} break;
1625
1626
case NOTIFICATION_EXIT_TREE: {
1627
if (ProjectSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &Window::_settings_changed))) {
1628
ProjectSettings::get_singleton()->disconnect("settings_changed", callable_mp(this, &Window::_settings_changed));
1629
}
1630
1631
set_theme_context(nullptr, false);
1632
1633
if (visible && window_id != DisplayServer::MAIN_WINDOW_ID) {
1634
if (get_tree() && get_tree()->is_accessibility_supported()) {
1635
_accessibility_notify_exit(this);
1636
if (get_parent()) {
1637
get_parent()->queue_accessibility_update();
1638
}
1639
if (embedder) {
1640
embedder->queue_accessibility_update();
1641
}
1642
}
1643
}
1644
1645
accessibility_title_element = RID();
1646
accessibility_announcement_element = RID();
1647
1648
if (transient) {
1649
_clear_transient();
1650
}
1651
1652
if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
1653
if (window_id == DisplayServer::MAIN_WINDOW_ID) {
1654
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
1655
_update_window_callbacks();
1656
} else {
1657
_clear_window();
1658
}
1659
} else {
1660
if (embedder) {
1661
embedder->_sub_window_remove(this);
1662
embedder = nullptr;
1663
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
1664
}
1665
_update_viewport_size(); //called by clear and make, which does not happen here
1666
}
1667
1668
RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
1669
} break;
1670
1671
case NOTIFICATION_VP_MOUSE_ENTER: {
1672
emit_signal(SceneStringName(mouse_entered));
1673
} break;
1674
1675
case NOTIFICATION_VP_MOUSE_EXIT: {
1676
emit_signal(SceneStringName(mouse_exited));
1677
} break;
1678
}
1679
}
1680
1681
void Window::set_content_scale_size(const Size2i &p_size) {
1682
ERR_MAIN_THREAD_GUARD;
1683
ERR_FAIL_COND(p_size.x < 0);
1684
ERR_FAIL_COND(p_size.y < 0);
1685
content_scale_size = p_size;
1686
_update_viewport_size();
1687
}
1688
1689
Size2i Window::get_content_scale_size() const {
1690
ERR_READ_THREAD_GUARD_V(Size2i());
1691
return content_scale_size;
1692
}
1693
1694
void Window::set_content_scale_mode(ContentScaleMode p_mode) {
1695
ERR_MAIN_THREAD_GUARD;
1696
content_scale_mode = p_mode;
1697
_update_viewport_size();
1698
}
1699
1700
Window::ContentScaleMode Window::get_content_scale_mode() const {
1701
ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_MODE_DISABLED);
1702
return content_scale_mode;
1703
}
1704
1705
void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
1706
ERR_MAIN_THREAD_GUARD;
1707
content_scale_aspect = p_aspect;
1708
_update_viewport_size();
1709
}
1710
1711
Window::ContentScaleAspect Window::get_content_scale_aspect() const {
1712
ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_ASPECT_IGNORE);
1713
return content_scale_aspect;
1714
}
1715
1716
void Window::set_content_scale_stretch(ContentScaleStretch p_stretch) {
1717
content_scale_stretch = p_stretch;
1718
_update_viewport_size();
1719
}
1720
1721
Window::ContentScaleStretch Window::get_content_scale_stretch() const {
1722
return content_scale_stretch;
1723
}
1724
1725
void Window::set_keep_title_visible(bool p_title_visible) {
1726
if (keep_title_visible == p_title_visible) {
1727
return;
1728
}
1729
keep_title_visible = p_title_visible;
1730
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
1731
_update_window_size();
1732
}
1733
}
1734
1735
bool Window::get_keep_title_visible() const {
1736
return keep_title_visible;
1737
}
1738
1739
void Window::set_content_scale_factor(real_t p_factor) {
1740
ERR_MAIN_THREAD_GUARD;
1741
ERR_FAIL_COND(p_factor <= 0);
1742
content_scale_factor = p_factor;
1743
_update_viewport_size();
1744
}
1745
1746
real_t Window::get_content_scale_factor() const {
1747
ERR_READ_THREAD_GUARD_V(0);
1748
return content_scale_factor;
1749
}
1750
1751
void Window::set_nonclient_area(const Rect2i &p_rect) {
1752
ERR_MAIN_THREAD_GUARD;
1753
nonclient_area = p_rect;
1754
}
1755
1756
Rect2i Window::get_nonclient_area() const {
1757
return nonclient_area;
1758
}
1759
1760
DisplayServer::WindowID Window::get_window_id() const {
1761
ERR_READ_THREAD_GUARD_V(DisplayServer::INVALID_WINDOW_ID);
1762
if (get_embedder()) {
1763
#ifdef TOOLS_ENABLED
1764
if (is_part_of_edited_scene()) {
1765
return DisplayServer::MAIN_WINDOW_ID;
1766
}
1767
#endif
1768
return parent->get_window_id();
1769
}
1770
return window_id;
1771
}
1772
1773
void Window::set_mouse_passthrough_polygon(const Vector<Vector2> &p_region) {
1774
ERR_MAIN_THREAD_GUARD;
1775
mpath = p_region;
1776
if (window_id == DisplayServer::INVALID_WINDOW_ID) {
1777
return;
1778
}
1779
DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
1780
}
1781
1782
Vector<Vector2> Window::get_mouse_passthrough_polygon() const {
1783
return mpath;
1784
}
1785
1786
void Window::set_wrap_controls(bool p_enable) {
1787
ERR_MAIN_THREAD_GUARD;
1788
wrap_controls = p_enable;
1789
1790
if (!is_inside_tree()) {
1791
return;
1792
}
1793
1794
if (updating_child_controls) {
1795
_update_child_controls();
1796
} else {
1797
_update_window_size();
1798
}
1799
}
1800
1801
bool Window::is_wrapping_controls() const {
1802
ERR_READ_THREAD_GUARD_V(false);
1803
return wrap_controls;
1804
}
1805
1806
Size2 Window::_get_contents_minimum_size() const {
1807
Size2 max;
1808
1809
for (int i = 0; i < get_child_count(); i++) {
1810
Control *c = Object::cast_to<Control>(get_child(i));
1811
if (c) {
1812
Point2i pos = c->get_position();
1813
Size2i min = c->get_combined_minimum_size();
1814
1815
max = max.max(pos + min);
1816
}
1817
}
1818
1819
return max;
1820
}
1821
1822
void Window::child_controls_changed() {
1823
ERR_MAIN_THREAD_GUARD;
1824
if (!is_inside_tree() || !visible || updating_child_controls) {
1825
return;
1826
}
1827
1828
updating_child_controls = true;
1829
callable_mp(this, &Window::_update_child_controls).call_deferred();
1830
}
1831
1832
void Window::_update_child_controls() {
1833
if (!updating_child_controls) {
1834
return;
1835
}
1836
1837
_update_window_size();
1838
1839
updating_child_controls = false;
1840
}
1841
1842
bool Window::_can_consume_input_events() const {
1843
return exclusive_child == nullptr;
1844
}
1845
1846
void Window::_window_input(const Ref<InputEvent> &p_ev) {
1847
ERR_MAIN_THREAD_GUARD;
1848
1849
if (exclusive_child != nullptr) {
1850
if (nonclient_area.has_area() && is_inside_tree()) {
1851
Ref<InputEventMouse> me = p_ev;
1852
if (me.is_valid() && nonclient_area.has_point(me->get_position())) {
1853
emit_signal(SceneStringName(nonclient_window_input), p_ev);
1854
}
1855
}
1856
if (!is_embedding_subwindows()) { // Not embedding, no need for event.
1857
return;
1858
}
1859
}
1860
1861
// If the event needs to be handled in a Window-derived class, then it should overwrite
1862
// `_input_from_window` instead of subscribing to the `window_input` signal, because the signal
1863
// filters out internal events.
1864
_input_from_window(p_ev);
1865
1866
if (p_ev->get_device() != InputEvent::DEVICE_ID_INTERNAL && is_inside_tree()) {
1867
emit_signal(SceneStringName(window_input), p_ev);
1868
}
1869
1870
if (is_inside_tree()) {
1871
push_input(p_ev);
1872
}
1873
}
1874
1875
void Window::_window_input_text(const String &p_text, bool p_emit_signal) {
1876
_push_text_input(p_text, p_emit_signal);
1877
}
1878
1879
void Window::_window_drop_files(const Vector<String> &p_files) {
1880
emit_signal(SNAME("files_dropped"), p_files);
1881
}
1882
1883
Viewport *Window::get_parent_viewport() const {
1884
ERR_READ_THREAD_GUARD_V(nullptr);
1885
if (get_parent()) {
1886
return get_parent()->get_viewport();
1887
} else {
1888
return nullptr;
1889
}
1890
}
1891
1892
Window *Window::get_non_popup_window() const {
1893
Window *w = const_cast<Window *>(this);
1894
while (w && w->is_popup()) {
1895
w = w->get_parent_visible_window();
1896
}
1897
return w;
1898
}
1899
1900
Window *Window::get_parent_visible_window() const {
1901
ERR_READ_THREAD_GUARD_V(nullptr);
1902
Viewport *vp = get_parent_viewport();
1903
Window *window = nullptr;
1904
while (vp) {
1905
window = Object::cast_to<Window>(vp);
1906
if (window && window->visible) {
1907
break;
1908
}
1909
if (!vp->get_parent()) {
1910
break;
1911
}
1912
1913
vp = vp->get_parent()->get_viewport();
1914
}
1915
return window;
1916
}
1917
1918
void Window::popup_on_parent(const Rect2i &p_parent_rect) {
1919
ERR_MAIN_THREAD_GUARD;
1920
ERR_FAIL_COND(!is_inside_tree());
1921
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1922
1923
if (!is_embedded()) {
1924
Window *window = get_parent_visible_window();
1925
1926
if (!window) {
1927
popup(p_parent_rect);
1928
} else {
1929
popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
1930
}
1931
} else {
1932
popup(p_parent_rect);
1933
}
1934
}
1935
1936
void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
1937
ERR_MAIN_THREAD_GUARD;
1938
ERR_FAIL_COND(!is_inside_tree());
1939
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1940
1941
// Consider the current size when calling with the default value.
1942
Size2i expected_size = p_size == Size2i() ? size : p_size;
1943
1944
Rect2 parent_rect;
1945
1946
if (is_embedded()) {
1947
parent_rect = get_embedder()->get_visible_rect();
1948
} else {
1949
DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
1950
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
1951
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
1952
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
1953
}
1954
1955
Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
1956
1957
Rect2i popup_rect;
1958
popup_rect.size = size_ratio.min(expected_size);
1959
popup_rect.size = _clamp_window_size(popup_rect.size);
1960
1961
if (parent_rect != Rect2()) {
1962
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
1963
}
1964
1965
emit_signal(SNAME("about_to_popup"));
1966
1967
if (popup_rect != Rect2()) {
1968
set_size(popup_rect.size);
1969
}
1970
_pre_popup();
1971
if (popup_rect != Rect2i()) {
1972
popup_rect.size = get_size();
1973
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
1974
}
1975
_popup_base(popup_rect);
1976
}
1977
1978
void Window::popup_centered(const Size2i &p_minsize) {
1979
ERR_MAIN_THREAD_GUARD;
1980
ERR_FAIL_COND(!is_inside_tree());
1981
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
1982
1983
// Consider the current size when calling with the default value.
1984
Size2i expected_size = p_minsize == Size2i() ? size : p_minsize;
1985
1986
Rect2 parent_rect;
1987
1988
if (is_embedded()) {
1989
parent_rect = get_embedder()->get_visible_rect();
1990
} else {
1991
DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
1992
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
1993
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
1994
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
1995
}
1996
1997
Rect2i popup_rect;
1998
popup_rect.size = _clamp_window_size(expected_size);
1999
2000
if (parent_rect != Rect2()) {
2001
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2002
}
2003
2004
emit_signal(SNAME("about_to_popup"));
2005
2006
if (popup_rect != Rect2()) {
2007
set_size(popup_rect.size);
2008
}
2009
_pre_popup();
2010
if (popup_rect != Rect2i()) {
2011
popup_rect.size = get_size();
2012
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2013
}
2014
_popup_base(popup_rect);
2015
}
2016
2017
void Window::popup_centered_ratio(float p_ratio) {
2018
ERR_MAIN_THREAD_GUARD;
2019
ERR_FAIL_COND(!is_inside_tree());
2020
ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
2021
ERR_FAIL_COND_MSG(p_ratio <= 0.0 || p_ratio > 1.0, "Ratio must be between 0.0 and 1.0!");
2022
2023
Rect2 parent_rect;
2024
2025
if (is_embedded()) {
2026
parent_rect = get_embedder()->get_visible_rect();
2027
} else {
2028
DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
2029
int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
2030
parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
2031
parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
2032
}
2033
2034
Rect2i popup_rect;
2035
if (parent_rect != Rect2()) {
2036
popup_rect.size = parent_rect.size * p_ratio;
2037
popup_rect.size = _clamp_window_size(popup_rect.size);
2038
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2039
}
2040
2041
emit_signal(SNAME("about_to_popup"));
2042
2043
if (popup_rect != Rect2()) {
2044
set_size(popup_rect.size);
2045
}
2046
_pre_popup();
2047
if (popup_rect != Rect2i()) {
2048
popup_rect.size = get_size();
2049
popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
2050
}
2051
_popup_base(popup_rect);
2052
}
2053
2054
void Window::popup(const Rect2i &p_screen_rect) {
2055
ERR_MAIN_THREAD_GUARD;
2056
emit_signal(SNAME("about_to_popup"));
2057
2058
Rect2i screen_rect = p_screen_rect;
2059
if (screen_rect != Rect2i()) {
2060
set_size(screen_rect.size);
2061
}
2062
_pre_popup();
2063
if (screen_rect != Rect2i()) {
2064
screen_rect.size = get_size();
2065
}
2066
_popup_base(screen_rect);
2067
}
2068
2069
void Window::_popup_base(const Rect2i &p_screen_rect) {
2070
ERR_MAIN_THREAD_GUARD;
2071
2072
if (!get_embedder() && get_flag(FLAG_POPUP)) {
2073
// Send a focus-out notification when opening a Window Manager Popup.
2074
SceneTree *scene_tree = get_tree();
2075
if (scene_tree) {
2076
scene_tree->notify_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_viewports", NOTIFICATION_WM_WINDOW_FOCUS_OUT);
2077
}
2078
}
2079
2080
// Update window size to calculate the actual window size based on contents minimum size and minimum size.
2081
_update_window_size();
2082
2083
bool should_fit = is_embedded() || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS);
2084
2085
if (p_screen_rect != Rect2i()) {
2086
set_position(p_screen_rect.position);
2087
2088
if (should_fit) {
2089
int screen_id = DisplayServer::get_singleton()->get_screen_from_rect(p_screen_rect);
2090
Size2i screen_size = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id).size;
2091
Size2i new_size = p_screen_rect.size.min(screen_size);
2092
set_size(new_size);
2093
} else {
2094
set_size(p_screen_rect.size);
2095
}
2096
}
2097
2098
Rect2i adjust = _popup_adjust_rect();
2099
if (adjust != Rect2i()) {
2100
set_position(adjust.position);
2101
set_size(adjust.size);
2102
}
2103
2104
int scr = DisplayServer::get_singleton()->get_screen_count();
2105
for (int i = 0; i < scr; i++) {
2106
Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
2107
if (r.has_point(position)) {
2108
current_screen = i;
2109
break;
2110
}
2111
}
2112
2113
set_transient(true);
2114
set_visible(true);
2115
2116
Rect2i parent_rect;
2117
if (is_embedded()) {
2118
parent_rect = get_embedder()->get_visible_rect();
2119
} else {
2120
int screen_id = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
2121
parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id);
2122
}
2123
if (should_fit && parent_rect != Rect2i() && !parent_rect.intersects(Rect2i(position, size))) {
2124
ERR_PRINT(vformat("Window %d spawned at invalid position: %s.", get_window_id(), position));
2125
set_position((parent_rect.size - size) / 2);
2126
}
2127
if (parent_rect != Rect2i() && is_clamped_to_embedder() && is_embedded()) {
2128
Rect2i new_rect = fit_rect_in_parent(Rect2i(position, size), parent_rect);
2129
set_position(new_rect.position);
2130
set_size(new_rect.size);
2131
}
2132
2133
_post_popup();
2134
notification(NOTIFICATION_POST_POPUP);
2135
}
2136
2137
bool Window::_try_parent_dialog(Node *p_from_node) {
2138
ERR_FAIL_NULL_V(p_from_node, false);
2139
ERR_FAIL_COND_V_MSG(is_inside_tree(), false, "Attempting to parent and popup a dialog that already has a parent.");
2140
2141
Window *w = p_from_node->get_last_exclusive_window();
2142
if (w && w != this) {
2143
w->add_child(this);
2144
return true;
2145
}
2146
return false;
2147
}
2148
2149
void Window::popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect) {
2150
if (_try_parent_dialog(p_from_node)) {
2151
popup(p_screen_rect);
2152
}
2153
}
2154
2155
void Window::popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect) {
2156
if (_try_parent_dialog(p_from_node)) {
2157
popup_on_parent(p_parent_rect);
2158
}
2159
}
2160
2161
void Window::popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize) {
2162
if (_try_parent_dialog(p_from_node)) {
2163
popup_centered(p_minsize);
2164
}
2165
}
2166
2167
void Window::popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio) {
2168
if (_try_parent_dialog(p_from_node)) {
2169
popup_centered_ratio(p_ratio);
2170
}
2171
}
2172
2173
void Window::popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size, float p_fallback_ratio) {
2174
if (_try_parent_dialog(p_from_node)) {
2175
popup_centered_clamped(p_size, p_fallback_ratio);
2176
}
2177
}
2178
2179
Rect2i Window::fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const {
2180
ERR_READ_THREAD_GUARD_V(Rect2i());
2181
Size2i limit = p_parent_rect.size;
2182
if (p_rect.position.x + p_rect.size.x > limit.x) {
2183
p_rect.position.x = limit.x - p_rect.size.x;
2184
}
2185
if (p_rect.position.y + p_rect.size.y > limit.y) {
2186
p_rect.position.y = limit.y - p_rect.size.y;
2187
}
2188
2189
if (p_rect.position.x < 0) {
2190
p_rect.position.x = 0;
2191
}
2192
2193
int title_height = get_flag(Window::FLAG_BORDERLESS) ? 0 : theme_cache.title_height;
2194
2195
if (p_rect.position.y < title_height) {
2196
p_rect.position.y = title_height;
2197
}
2198
2199
return p_rect;
2200
}
2201
2202
Size2 Window::get_contents_minimum_size() const {
2203
ERR_READ_THREAD_GUARD_V(Size2());
2204
Vector2 ms;
2205
if (GDVIRTUAL_CALL(_get_contents_minimum_size, ms)) {
2206
return ms;
2207
}
2208
return _get_contents_minimum_size();
2209
}
2210
2211
Size2 Window::get_clamped_minimum_size() const {
2212
ERR_READ_THREAD_GUARD_V(Size2());
2213
if (!wrap_controls) {
2214
return min_size;
2215
}
2216
2217
return min_size.max(get_contents_minimum_size() * get_content_scale_factor());
2218
}
2219
2220
void Window::grab_focus() {
2221
ERR_MAIN_THREAD_GUARD;
2222
if (embedder) {
2223
embedder->_sub_window_grab_focus(this);
2224
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2225
DisplayServer::get_singleton()->window_move_to_foreground(window_id);
2226
}
2227
}
2228
2229
bool Window::has_focus() const {
2230
ERR_READ_THREAD_GUARD_V(false);
2231
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2232
return DisplayServer::get_singleton()->window_is_focused(window_id);
2233
}
2234
return focused;
2235
}
2236
2237
bool Window::has_focus_or_active_popup() const {
2238
ERR_READ_THREAD_GUARD_V(false);
2239
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2240
return DisplayServer::get_singleton()->window_is_focused(window_id) || (DisplayServer::get_singleton()->window_get_active_popup() == window_id);
2241
}
2242
return focused;
2243
}
2244
2245
void Window::start_drag() {
2246
ERR_MAIN_THREAD_GUARD;
2247
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2248
DisplayServer::get_singleton()->window_start_drag(window_id);
2249
} else if (embedder) {
2250
embedder->_window_start_drag(this);
2251
}
2252
}
2253
2254
void Window::start_resize(DisplayServer::WindowResizeEdge p_edge) {
2255
ERR_MAIN_THREAD_GUARD;
2256
if (get_flag(FLAG_RESIZE_DISABLED)) {
2257
return;
2258
}
2259
if (window_id != DisplayServer::INVALID_WINDOW_ID) {
2260
DisplayServer::get_singleton()->window_start_resize(p_edge, window_id);
2261
} else if (embedder) {
2262
switch (p_edge) {
2263
case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
2264
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP_LEFT, this);
2265
} break;
2266
case DisplayServer::WINDOW_EDGE_TOP: {
2267
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP, this);
2268
} break;
2269
case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
2270
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_TOP_RIGHT, this);
2271
} break;
2272
case DisplayServer::WINDOW_EDGE_LEFT: {
2273
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_LEFT, this);
2274
} break;
2275
case DisplayServer::WINDOW_EDGE_RIGHT: {
2276
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_RIGHT, this);
2277
} break;
2278
case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
2279
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM_LEFT, this);
2280
} break;
2281
case DisplayServer::WINDOW_EDGE_BOTTOM: {
2282
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM, this);
2283
} break;
2284
case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
2285
embedder->_window_start_resize(Viewport::SUB_WINDOW_RESIZE_BOTTOM_RIGHT, this);
2286
} break;
2287
default:
2288
break;
2289
}
2290
}
2291
}
2292
2293
Rect2i Window::get_usable_parent_rect() const {
2294
ERR_READ_THREAD_GUARD_V(Rect2i());
2295
ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
2296
Rect2i parent_rect;
2297
if (is_embedded()) {
2298
parent_rect = get_embedder()->get_visible_rect();
2299
} else {
2300
const Window *w = is_visible() ? this : get_parent_visible_window();
2301
//find a parent that can contain us
2302
ERR_FAIL_NULL_V(w, Rect2());
2303
2304
parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
2305
}
2306
return parent_rect;
2307
}
2308
2309
void Window::set_accessibility_name(const String &p_name) {
2310
ERR_MAIN_THREAD_GUARD;
2311
if (accessibility_name != p_name) {
2312
accessibility_name = p_name;
2313
queue_accessibility_update();
2314
update_configuration_warnings();
2315
}
2316
}
2317
2318
String Window::get_accessibility_name() const {
2319
return tr(accessibility_name);
2320
}
2321
2322
void Window::set_accessibility_description(const String &p_description) {
2323
ERR_MAIN_THREAD_GUARD;
2324
if (accessibility_description != p_description) {
2325
accessibility_description = p_description;
2326
queue_accessibility_update();
2327
}
2328
}
2329
2330
String Window::get_accessibility_description() const {
2331
return tr(accessibility_description);
2332
}
2333
2334
void Window::accessibility_announcement(const String &p_announcement) {
2335
ERR_MAIN_THREAD_GUARD;
2336
announcement = p_announcement;
2337
queue_accessibility_update();
2338
}
2339
2340
void Window::add_child_notify(Node *p_child) {
2341
if (is_inside_tree() && wrap_controls) {
2342
child_controls_changed();
2343
}
2344
}
2345
2346
void Window::remove_child_notify(Node *p_child) {
2347
if (is_inside_tree() && wrap_controls) {
2348
child_controls_changed();
2349
}
2350
}
2351
2352
// Theming.
2353
2354
void Window::set_theme_owner_node(Node *p_node) {
2355
ERR_MAIN_THREAD_GUARD;
2356
theme_owner->set_owner_node(p_node);
2357
}
2358
2359
Node *Window::get_theme_owner_node() const {
2360
ERR_READ_THREAD_GUARD_V(nullptr);
2361
return theme_owner->get_owner_node();
2362
}
2363
2364
bool Window::has_theme_owner_node() const {
2365
ERR_READ_THREAD_GUARD_V(false);
2366
return theme_owner->has_owner_node();
2367
}
2368
2369
void Window::set_theme_context(ThemeContext *p_context, bool p_propagate) {
2370
ERR_MAIN_THREAD_GUARD;
2371
theme_owner->set_owner_context(p_context, p_propagate);
2372
}
2373
2374
void Window::set_theme(const Ref<Theme> &p_theme) {
2375
ERR_MAIN_THREAD_GUARD;
2376
if (theme == p_theme) {
2377
return;
2378
}
2379
2380
if (theme.is_valid()) {
2381
theme->disconnect_changed(callable_mp(this, &Window::_theme_changed));
2382
}
2383
2384
theme = p_theme;
2385
if (theme.is_valid()) {
2386
theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
2387
theme->connect_changed(callable_mp(this, &Window::_theme_changed), CONNECT_DEFERRED);
2388
return;
2389
}
2390
2391
Control *parent_c = Object::cast_to<Control>(get_parent());
2392
if (parent_c && parent_c->has_theme_owner_node()) {
2393
theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
2394
return;
2395
}
2396
2397
Window *parent_w = cast_to<Window>(get_parent());
2398
if (parent_w && parent_w->has_theme_owner_node()) {
2399
theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
2400
return;
2401
}
2402
2403
theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
2404
}
2405
2406
Ref<Theme> Window::get_theme() const {
2407
ERR_READ_THREAD_GUARD_V(Ref<Theme>());
2408
return theme;
2409
}
2410
2411
void Window::_theme_changed() {
2412
if (is_inside_tree()) {
2413
theme_owner->propagate_theme_changed(this, this, true, false);
2414
}
2415
}
2416
2417
void Window::_notify_theme_override_changed() {
2418
if (!bulk_theme_override && is_inside_tree()) {
2419
notification(NOTIFICATION_THEME_CHANGED);
2420
}
2421
}
2422
2423
void Window::_invalidate_theme_cache() {
2424
theme_icon_cache.clear();
2425
theme_style_cache.clear();
2426
theme_font_cache.clear();
2427
theme_font_size_cache.clear();
2428
theme_color_cache.clear();
2429
theme_constant_cache.clear();
2430
}
2431
2432
void Window::_update_theme_item_cache() {
2433
// Request an update on the next frame to reflect theme changes.
2434
// Updating without a delay can cause a lot of lag.
2435
if (!wrap_controls) {
2436
updating_embedded_window = true;
2437
callable_mp(this, &Window::_update_embedded_window).call_deferred();
2438
} else {
2439
child_controls_changed();
2440
}
2441
2442
ThemeDB::get_singleton()->update_class_instance_items(this);
2443
}
2444
2445
void Window::_update_embedded_window() {
2446
if (!updating_embedded_window) {
2447
return;
2448
}
2449
2450
if (embedder) {
2451
embedder->_sub_window_update(this);
2452
};
2453
2454
updating_embedded_window = false;
2455
}
2456
2457
void Window::set_theme_type_variation(const StringName &p_theme_type) {
2458
ERR_MAIN_THREAD_GUARD;
2459
theme_type_variation = p_theme_type;
2460
if (is_inside_tree()) {
2461
notification(NOTIFICATION_THEME_CHANGED);
2462
}
2463
}
2464
2465
StringName Window::get_theme_type_variation() const {
2466
ERR_READ_THREAD_GUARD_V(StringName());
2467
return theme_type_variation;
2468
}
2469
2470
/// Theme property lookup.
2471
2472
Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
2473
ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
2474
if (!initialized) {
2475
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2476
}
2477
2478
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2479
const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
2480
if (tex) {
2481
return *tex;
2482
}
2483
}
2484
2485
if (theme_icon_cache.has(p_theme_type) && theme_icon_cache[p_theme_type].has(p_name)) {
2486
return theme_icon_cache[p_theme_type][p_name];
2487
}
2488
2489
Vector<StringName> theme_types;
2490
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2491
Ref<Texture2D> icon = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
2492
theme_icon_cache[p_theme_type][p_name] = icon;
2493
return icon;
2494
}
2495
2496
Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
2497
ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
2498
if (!initialized) {
2499
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2500
}
2501
2502
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2503
const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
2504
if (style) {
2505
return *style;
2506
}
2507
}
2508
2509
if (theme_style_cache.has(p_theme_type) && theme_style_cache[p_theme_type].has(p_name)) {
2510
return theme_style_cache[p_theme_type][p_name];
2511
}
2512
2513
Vector<StringName> theme_types;
2514
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2515
Ref<StyleBox> style = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
2516
theme_style_cache[p_theme_type][p_name] = style;
2517
return style;
2518
}
2519
2520
Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
2521
ERR_READ_THREAD_GUARD_V(Ref<Font>());
2522
if (!initialized) {
2523
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2524
}
2525
2526
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2527
const Ref<Font> *font = theme_font_override.getptr(p_name);
2528
if (font) {
2529
return *font;
2530
}
2531
}
2532
2533
if (theme_font_cache.has(p_theme_type) && theme_font_cache[p_theme_type].has(p_name)) {
2534
return theme_font_cache[p_theme_type][p_name];
2535
}
2536
2537
Vector<StringName> theme_types;
2538
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2539
Ref<Font> font = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
2540
theme_font_cache[p_theme_type][p_name] = font;
2541
return font;
2542
}
2543
2544
int Window::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
2545
ERR_READ_THREAD_GUARD_V(0);
2546
if (!initialized) {
2547
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2548
}
2549
2550
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2551
const int *font_size = theme_font_size_override.getptr(p_name);
2552
if (font_size && (*font_size) > 0) {
2553
return *font_size;
2554
}
2555
}
2556
2557
if (theme_font_size_cache.has(p_theme_type) && theme_font_size_cache[p_theme_type].has(p_name)) {
2558
return theme_font_size_cache[p_theme_type][p_name];
2559
}
2560
2561
Vector<StringName> theme_types;
2562
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2563
int font_size = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
2564
theme_font_size_cache[p_theme_type][p_name] = font_size;
2565
return font_size;
2566
}
2567
2568
Color Window::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
2569
ERR_READ_THREAD_GUARD_V(Color());
2570
if (!initialized) {
2571
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2572
}
2573
2574
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2575
const Color *color = theme_color_override.getptr(p_name);
2576
if (color) {
2577
return *color;
2578
}
2579
}
2580
2581
if (theme_color_cache.has(p_theme_type) && theme_color_cache[p_theme_type].has(p_name)) {
2582
return theme_color_cache[p_theme_type][p_name];
2583
}
2584
2585
Vector<StringName> theme_types;
2586
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2587
Color color = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
2588
theme_color_cache[p_theme_type][p_name] = color;
2589
return color;
2590
}
2591
2592
int Window::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
2593
ERR_READ_THREAD_GUARD_V(0);
2594
if (!initialized) {
2595
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2596
}
2597
2598
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2599
const int *constant = theme_constant_override.getptr(p_name);
2600
if (constant) {
2601
return *constant;
2602
}
2603
}
2604
2605
if (theme_constant_cache.has(p_theme_type) && theme_constant_cache[p_theme_type].has(p_name)) {
2606
return theme_constant_cache[p_theme_type][p_name];
2607
}
2608
2609
Vector<StringName> theme_types;
2610
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2611
int constant = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
2612
theme_constant_cache[p_theme_type][p_name] = constant;
2613
return constant;
2614
}
2615
2616
Variant Window::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
2617
switch (p_data_type) {
2618
case Theme::DATA_TYPE_COLOR:
2619
return get_theme_color(p_name, p_theme_type);
2620
case Theme::DATA_TYPE_CONSTANT:
2621
return get_theme_constant(p_name, p_theme_type);
2622
case Theme::DATA_TYPE_FONT:
2623
return get_theme_font(p_name, p_theme_type);
2624
case Theme::DATA_TYPE_FONT_SIZE:
2625
return get_theme_font_size(p_name, p_theme_type);
2626
case Theme::DATA_TYPE_ICON:
2627
return get_theme_icon(p_name, p_theme_type);
2628
case Theme::DATA_TYPE_STYLEBOX:
2629
return get_theme_stylebox(p_name, p_theme_type);
2630
case Theme::DATA_TYPE_MAX:
2631
break; // Can't happen, but silences warning.
2632
}
2633
2634
return Variant();
2635
}
2636
2637
#ifdef TOOLS_ENABLED
2638
Ref<Texture2D> Window::get_editor_theme_icon(const StringName &p_name) const {
2639
return get_theme_icon(p_name, SNAME("EditorIcons"));
2640
}
2641
#endif
2642
2643
bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
2644
ERR_READ_THREAD_GUARD_V(false);
2645
if (!initialized) {
2646
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2647
}
2648
2649
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2650
if (has_theme_icon_override(p_name)) {
2651
return true;
2652
}
2653
}
2654
2655
Vector<StringName> theme_types;
2656
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2657
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
2658
}
2659
2660
bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
2661
ERR_READ_THREAD_GUARD_V(false);
2662
if (!initialized) {
2663
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2664
}
2665
2666
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2667
if (has_theme_stylebox_override(p_name)) {
2668
return true;
2669
}
2670
}
2671
2672
Vector<StringName> theme_types;
2673
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2674
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
2675
}
2676
2677
bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
2678
ERR_READ_THREAD_GUARD_V(false);
2679
if (!initialized) {
2680
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2681
}
2682
2683
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2684
if (has_theme_font_override(p_name)) {
2685
return true;
2686
}
2687
}
2688
2689
Vector<StringName> theme_types;
2690
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2691
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
2692
}
2693
2694
bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
2695
ERR_READ_THREAD_GUARD_V(false);
2696
if (!initialized) {
2697
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2698
}
2699
2700
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2701
if (has_theme_font_size_override(p_name)) {
2702
return true;
2703
}
2704
}
2705
2706
Vector<StringName> theme_types;
2707
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2708
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
2709
}
2710
2711
bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
2712
ERR_READ_THREAD_GUARD_V(false);
2713
if (!initialized) {
2714
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2715
}
2716
2717
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2718
if (has_theme_color_override(p_name)) {
2719
return true;
2720
}
2721
}
2722
2723
Vector<StringName> theme_types;
2724
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2725
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
2726
}
2727
2728
bool Window::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
2729
ERR_READ_THREAD_GUARD_V(false);
2730
if (!initialized) {
2731
WARN_PRINT_ONCE(vformat("Attempting to access theme items too early in %s; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED", get_description()));
2732
}
2733
2734
if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
2735
if (has_theme_constant_override(p_name)) {
2736
return true;
2737
}
2738
}
2739
2740
Vector<StringName> theme_types;
2741
theme_owner->get_theme_type_dependencies(this, p_theme_type, theme_types);
2742
return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
2743
}
2744
2745
/// Local property overrides.
2746
2747
void Window::add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon) {
2748
ERR_MAIN_THREAD_GUARD;
2749
ERR_FAIL_COND(p_icon.is_null());
2750
2751
if (theme_icon_override.has(p_name)) {
2752
theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2753
}
2754
2755
theme_icon_override[p_name] = p_icon;
2756
theme_icon_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2757
_notify_theme_override_changed();
2758
}
2759
2760
void Window::add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
2761
ERR_MAIN_THREAD_GUARD;
2762
ERR_FAIL_COND(p_style.is_null());
2763
2764
if (theme_style_override.has(p_name)) {
2765
theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2766
}
2767
2768
theme_style_override[p_name] = p_style;
2769
theme_style_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2770
_notify_theme_override_changed();
2771
}
2772
2773
void Window::add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font) {
2774
ERR_MAIN_THREAD_GUARD;
2775
ERR_FAIL_COND(p_font.is_null());
2776
2777
if (theme_font_override.has(p_name)) {
2778
theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2779
}
2780
2781
theme_font_override[p_name] = p_font;
2782
theme_font_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
2783
_notify_theme_override_changed();
2784
}
2785
2786
void Window::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
2787
ERR_MAIN_THREAD_GUARD;
2788
theme_font_size_override[p_name] = p_font_size;
2789
_notify_theme_override_changed();
2790
}
2791
2792
void Window::add_theme_color_override(const StringName &p_name, const Color &p_color) {
2793
ERR_MAIN_THREAD_GUARD;
2794
theme_color_override[p_name] = p_color;
2795
_notify_theme_override_changed();
2796
}
2797
2798
void Window::add_theme_constant_override(const StringName &p_name, int p_constant) {
2799
ERR_MAIN_THREAD_GUARD;
2800
theme_constant_override[p_name] = p_constant;
2801
_notify_theme_override_changed();
2802
}
2803
2804
void Window::remove_theme_icon_override(const StringName &p_name) {
2805
ERR_MAIN_THREAD_GUARD;
2806
if (theme_icon_override.has(p_name)) {
2807
theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2808
}
2809
2810
theme_icon_override.erase(p_name);
2811
_notify_theme_override_changed();
2812
}
2813
2814
void Window::remove_theme_style_override(const StringName &p_name) {
2815
ERR_MAIN_THREAD_GUARD;
2816
if (theme_style_override.has(p_name)) {
2817
theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2818
}
2819
2820
theme_style_override.erase(p_name);
2821
_notify_theme_override_changed();
2822
}
2823
2824
void Window::remove_theme_font_override(const StringName &p_name) {
2825
ERR_MAIN_THREAD_GUARD;
2826
if (theme_font_override.has(p_name)) {
2827
theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
2828
}
2829
2830
theme_font_override.erase(p_name);
2831
_notify_theme_override_changed();
2832
}
2833
2834
void Window::remove_theme_font_size_override(const StringName &p_name) {
2835
ERR_MAIN_THREAD_GUARD;
2836
theme_font_size_override.erase(p_name);
2837
_notify_theme_override_changed();
2838
}
2839
2840
void Window::remove_theme_color_override(const StringName &p_name) {
2841
ERR_MAIN_THREAD_GUARD;
2842
theme_color_override.erase(p_name);
2843
_notify_theme_override_changed();
2844
}
2845
2846
void Window::remove_theme_constant_override(const StringName &p_name) {
2847
ERR_MAIN_THREAD_GUARD;
2848
theme_constant_override.erase(p_name);
2849
_notify_theme_override_changed();
2850
}
2851
2852
bool Window::has_theme_icon_override(const StringName &p_name) const {
2853
ERR_READ_THREAD_GUARD_V(false);
2854
const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
2855
return tex != nullptr;
2856
}
2857
2858
bool Window::has_theme_stylebox_override(const StringName &p_name) const {
2859
ERR_READ_THREAD_GUARD_V(false);
2860
const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
2861
return style != nullptr;
2862
}
2863
2864
bool Window::has_theme_font_override(const StringName &p_name) const {
2865
ERR_READ_THREAD_GUARD_V(false);
2866
const Ref<Font> *font = theme_font_override.getptr(p_name);
2867
return font != nullptr;
2868
}
2869
2870
bool Window::has_theme_font_size_override(const StringName &p_name) const {
2871
ERR_READ_THREAD_GUARD_V(false);
2872
const int *font_size = theme_font_size_override.getptr(p_name);
2873
return font_size != nullptr;
2874
}
2875
2876
bool Window::has_theme_color_override(const StringName &p_name) const {
2877
ERR_READ_THREAD_GUARD_V(false);
2878
const Color *color = theme_color_override.getptr(p_name);
2879
return color != nullptr;
2880
}
2881
2882
bool Window::has_theme_constant_override(const StringName &p_name) const {
2883
ERR_READ_THREAD_GUARD_V(false);
2884
const int *constant = theme_constant_override.getptr(p_name);
2885
return constant != nullptr;
2886
}
2887
2888
/// Default theme properties.
2889
2890
float Window::get_theme_default_base_scale() const {
2891
ERR_READ_THREAD_GUARD_V(0);
2892
return theme_owner->get_theme_default_base_scale();
2893
}
2894
2895
Ref<Font> Window::get_theme_default_font() const {
2896
ERR_READ_THREAD_GUARD_V(Ref<Font>());
2897
return theme_owner->get_theme_default_font();
2898
}
2899
2900
int Window::get_theme_default_font_size() const {
2901
ERR_READ_THREAD_GUARD_V(0);
2902
return theme_owner->get_theme_default_font_size();
2903
}
2904
2905
/// Bulk actions.
2906
2907
void Window::begin_bulk_theme_override() {
2908
ERR_MAIN_THREAD_GUARD;
2909
bulk_theme_override = true;
2910
}
2911
2912
void Window::end_bulk_theme_override() {
2913
ERR_MAIN_THREAD_GUARD;
2914
ERR_FAIL_COND(!bulk_theme_override);
2915
2916
bulk_theme_override = false;
2917
_notify_theme_override_changed();
2918
}
2919
2920
//
2921
2922
Rect2i Window::get_parent_rect() const {
2923
ERR_READ_THREAD_GUARD_V(Rect2i());
2924
ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
2925
if (is_embedded()) {
2926
//viewport
2927
Node *n = get_parent();
2928
ERR_FAIL_NULL_V(n, Rect2i());
2929
Viewport *p = n->get_viewport();
2930
ERR_FAIL_NULL_V(p, Rect2i());
2931
2932
return p->get_visible_rect();
2933
} else {
2934
int x = get_position().x;
2935
int closest_dist = 0x7FFFFFFF;
2936
Rect2i closest_rect;
2937
for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
2938
Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
2939
int d;
2940
if (x >= s.position.x && x < s.size.x) {
2941
//contained
2942
closest_rect = s;
2943
break;
2944
} else if (x < s.position.x) {
2945
d = s.position.x - x;
2946
} else {
2947
d = x - (s.position.x + s.size.x);
2948
}
2949
2950
if (d < closest_dist) {
2951
closest_dist = d;
2952
closest_rect = s;
2953
}
2954
}
2955
return closest_rect;
2956
}
2957
}
2958
2959
void Window::set_clamp_to_embedder(bool p_enable) {
2960
ERR_MAIN_THREAD_GUARD;
2961
clamp_to_embedder = p_enable;
2962
}
2963
2964
bool Window::is_clamped_to_embedder() const {
2965
ERR_READ_THREAD_GUARD_V(false);
2966
return clamp_to_embedder;
2967
}
2968
2969
void Window::set_unparent_when_invisible(bool p_unparent) {
2970
unparent_when_invisible = p_unparent;
2971
}
2972
2973
void Window::set_layout_direction(Window::LayoutDirection p_direction) {
2974
ERR_MAIN_THREAD_GUARD;
2975
ERR_FAIL_INDEX(p_direction, LAYOUT_DIRECTION_MAX);
2976
2977
layout_dir = p_direction;
2978
propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
2979
}
2980
2981
Window::LayoutDirection Window::get_layout_direction() const {
2982
ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
2983
return layout_dir;
2984
}
2985
2986
bool Window::is_layout_rtl() const {
2987
ERR_READ_THREAD_GUARD_V(false);
2988
if (layout_dir == LAYOUT_DIRECTION_INHERITED) {
2989
#ifdef TOOLS_ENABLED
2990
if (is_part_of_edited_scene() && GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
2991
return true;
2992
}
2993
if (is_inside_tree()) {
2994
Node *edited_scene_root = get_tree()->get_edited_scene_root();
2995
if (edited_scene_root == this) {
2996
int proj_root_layout_direction = GLOBAL_GET_CACHED(int, "internationalization/rendering/root_node_layout_direction");
2997
if (proj_root_layout_direction == 1) {
2998
return false;
2999
} else if (proj_root_layout_direction == 2) {
3000
return true;
3001
} else if (proj_root_layout_direction == 3) {
3002
return TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
3003
} else {
3004
return TS->is_locale_right_to_left(_get_locale());
3005
}
3006
}
3007
}
3008
#else
3009
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3010
return true;
3011
}
3012
#endif
3013
const StringName domain_name = get_translation_domain();
3014
Node *parent_node = get_parent();
3015
while (parent_node && parent_node->get_translation_domain() == domain_name) {
3016
Control *parent_control = Object::cast_to<Control>(parent_node);
3017
if (parent_control) {
3018
return parent_control->is_layout_rtl();
3019
}
3020
3021
Window *parent_window = Object::cast_to<Window>(parent_node);
3022
if (parent_window) {
3023
return parent_window->is_layout_rtl();
3024
}
3025
parent_node = parent_node->get_parent();
3026
}
3027
3028
if (root_layout_direction == 1) {
3029
return false;
3030
} else if (root_layout_direction == 2) {
3031
return true;
3032
} else if (root_layout_direction == 3) {
3033
return TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
3034
} else {
3035
return TS->is_locale_right_to_left(_get_locale());
3036
}
3037
} else if (layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
3038
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3039
return true;
3040
} else {
3041
return TS->is_locale_right_to_left(_get_locale());
3042
}
3043
} else if (layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
3044
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3045
return true;
3046
} else {
3047
return TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
3048
}
3049
} else {
3050
return (layout_dir == LAYOUT_DIRECTION_RTL);
3051
}
3052
}
3053
3054
#ifndef DISABLE_DEPRECATED
3055
3056
void Window::set_use_font_oversampling(bool p_oversampling) {
3057
Viewport::set_use_oversampling(p_oversampling);
3058
}
3059
3060
bool Window::is_using_font_oversampling() const {
3061
return Viewport::is_using_oversampling();
3062
}
3063
3064
void Window::set_auto_translate(bool p_enable) {
3065
ERR_MAIN_THREAD_GUARD;
3066
set_auto_translate_mode(p_enable ? AUTO_TRANSLATE_MODE_ALWAYS : AUTO_TRANSLATE_MODE_DISABLED);
3067
}
3068
3069
bool Window::is_auto_translating() const {
3070
ERR_READ_THREAD_GUARD_V(false);
3071
return can_auto_translate();
3072
}
3073
#endif
3074
3075
Transform2D Window::get_final_transform() const {
3076
ERR_READ_THREAD_GUARD_V(Transform2D());
3077
return window_transform * stretch_transform * global_canvas_transform;
3078
}
3079
3080
Transform2D Window::get_screen_transform_internal(bool p_absolute_position) const {
3081
ERR_READ_THREAD_GUARD_V(Transform2D());
3082
Transform2D embedder_transform;
3083
if (get_embedder()) {
3084
embedder_transform.translate_local(get_position());
3085
embedder_transform = get_embedder()->get_screen_transform_internal(p_absolute_position) * embedder_transform;
3086
} else if (p_absolute_position) {
3087
embedder_transform.translate_local(get_position());
3088
}
3089
return embedder_transform * get_final_transform();
3090
}
3091
3092
Transform2D Window::get_popup_base_transform_native() const {
3093
ERR_READ_THREAD_GUARD_V(Transform2D());
3094
if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS)) {
3095
return Transform2D();
3096
}
3097
Transform2D popup_base_transform;
3098
popup_base_transform.set_origin(get_position());
3099
popup_base_transform *= get_final_transform();
3100
if (get_embedder()) {
3101
return get_embedder()->get_popup_base_transform_native() * popup_base_transform;
3102
}
3103
return popup_base_transform;
3104
}
3105
3106
Transform2D Window::get_popup_base_transform() const {
3107
ERR_READ_THREAD_GUARD_V(Transform2D());
3108
if (is_embedding_subwindows()) {
3109
return Transform2D();
3110
}
3111
Transform2D popup_base_transform;
3112
popup_base_transform.set_origin(get_position());
3113
popup_base_transform *= get_final_transform();
3114
if (get_embedder()) {
3115
return get_embedder()->get_popup_base_transform() * popup_base_transform;
3116
}
3117
return popup_base_transform;
3118
}
3119
3120
Viewport *Window::get_section_root_viewport() const {
3121
if (get_embedder()) {
3122
return get_embedder()->get_section_root_viewport();
3123
}
3124
if (is_inside_tree()) {
3125
// Native window.
3126
return SceneTree::get_singleton()->get_root();
3127
}
3128
Window *vp = const_cast<Window *>(this);
3129
return vp;
3130
}
3131
3132
bool Window::is_attached_in_viewport() const {
3133
return get_embedder();
3134
}
3135
3136
void Window::_update_mouse_over(Vector2 p_pos) {
3137
if (!mouse_in_window) {
3138
if (is_embedded()) {
3139
mouse_in_window = true;
3140
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
3141
}
3142
}
3143
3144
bool new_in = get_visible_rect().has_point(p_pos);
3145
if (new_in == gui.mouse_in_viewport) {
3146
if (new_in) {
3147
Viewport::_update_mouse_over(p_pos);
3148
}
3149
return;
3150
}
3151
3152
if (new_in) {
3153
notification(NOTIFICATION_VP_MOUSE_ENTER);
3154
Viewport::_update_mouse_over(p_pos);
3155
} else {
3156
Viewport::_mouse_leave_viewport();
3157
}
3158
}
3159
3160
void Window::_mouse_leave_viewport() {
3161
Viewport::_mouse_leave_viewport();
3162
if (is_embedded()) {
3163
mouse_in_window = false;
3164
_propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
3165
}
3166
}
3167
3168
void Window::_update_displayed_title() {
3169
displayed_title = atr(title);
3170
3171
#ifdef DEBUG_ENABLED
3172
if (window_id == DisplayServer::MAIN_WINDOW_ID && !Engine::get_singleton()->is_project_manager_hint()) {
3173
// Append a suffix to the window title to denote that the project is running
3174
// from a debug build (including the editor, excluding the project manager).
3175
// Since this results in lower performance, this should be clearly presented
3176
// to the user.
3177
displayed_title = vformat("%s (DEBUG)", displayed_title);
3178
}
3179
#endif
3180
3181
if (embedder) {
3182
embedder->_sub_window_update(this);
3183
} else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
3184
DisplayServer::get_singleton()->window_set_title(displayed_title, window_id);
3185
if (keep_title_visible) {
3186
Size2i title_size = DisplayServer::get_singleton()->window_get_title_size(displayed_title, window_id);
3187
Size2i size_limit = get_clamped_minimum_size();
3188
if (title_size.x > size_limit.x || title_size.y > size_limit.y) {
3189
_update_window_size();
3190
}
3191
}
3192
}
3193
3194
#ifdef DEBUG_ENABLED
3195
if (EngineDebugger::get_singleton() && window_id == DisplayServer::MAIN_WINDOW_ID && !Engine::get_singleton()->is_project_manager_hint()) {
3196
Array arr = { displayed_title };
3197
EngineDebugger::get_singleton()->send_message("window:title", arr);
3198
}
3199
#endif
3200
3201
queue_accessibility_update();
3202
}
3203
3204
void Window::_bind_methods() {
3205
ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
3206
ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
3207
3208
ClassDB::bind_method(D_METHOD("set_initial_position", "initial_position"), &Window::set_initial_position);
3209
ClassDB::bind_method(D_METHOD("get_initial_position"), &Window::get_initial_position);
3210
3211
ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
3212
ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
3213
3214
ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
3215
ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
3216
ClassDB::bind_method(D_METHOD("move_to_center"), &Window::move_to_center);
3217
3218
ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
3219
ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
3220
ClassDB::bind_method(D_METHOD("reset_size"), &Window::reset_size);
3221
3222
ClassDB::bind_method(D_METHOD("get_position_with_decorations"), &Window::get_position_with_decorations);
3223
ClassDB::bind_method(D_METHOD("get_size_with_decorations"), &Window::get_size_with_decorations);
3224
3225
ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
3226
ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
3227
3228
ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
3229
ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
3230
3231
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
3232
ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
3233
3234
ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
3235
ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
3236
3237
ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
3238
3239
ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
3240
3241
#ifndef DISABLE_DEPRECATED
3242
ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
3243
#endif // DISABLE_DEPRECATED
3244
3245
ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
3246
ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
3247
3248
ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
3249
ClassDB::bind_method(D_METHOD("show"), &Window::show);
3250
3251
ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
3252
ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
3253
3254
ClassDB::bind_method(D_METHOD("set_transient_to_focused", "enable"), &Window::set_transient_to_focused);
3255
ClassDB::bind_method(D_METHOD("is_transient_to_focused"), &Window::is_transient_to_focused);
3256
3257
ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
3258
ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
3259
3260
ClassDB::bind_method(D_METHOD("set_unparent_when_invisible", "unparent"), &Window::set_unparent_when_invisible);
3261
3262
ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
3263
ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
3264
ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
3265
3266
ClassDB::bind_method(D_METHOD("start_drag"), &Window::start_drag);
3267
ClassDB::bind_method(D_METHOD("start_resize", "edge"), &Window::start_resize);
3268
3269
ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);
3270
ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);
3271
3272
ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
3273
3274
ClassDB::bind_method(D_METHOD("get_contents_minimum_size"), &Window::get_contents_minimum_size);
3275
3276
ClassDB::bind_method(D_METHOD("set_force_native", "force_native"), &Window::set_force_native);
3277
ClassDB::bind_method(D_METHOD("get_force_native"), &Window::get_force_native);
3278
3279
ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
3280
ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
3281
3282
ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
3283
ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
3284
3285
ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
3286
ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
3287
3288
ClassDB::bind_method(D_METHOD("set_content_scale_stretch", "stretch"), &Window::set_content_scale_stretch);
3289
ClassDB::bind_method(D_METHOD("get_content_scale_stretch"), &Window::get_content_scale_stretch);
3290
3291
ClassDB::bind_method(D_METHOD("set_nonclient_area", "area"), &Window::set_nonclient_area);
3292
ClassDB::bind_method(D_METHOD("get_nonclient_area"), &Window::get_nonclient_area);
3293
3294
ClassDB::bind_method(D_METHOD("set_keep_title_visible", "title_visible"), &Window::set_keep_title_visible);
3295
ClassDB::bind_method(D_METHOD("get_keep_title_visible"), &Window::get_keep_title_visible);
3296
3297
ClassDB::bind_method(D_METHOD("set_content_scale_factor", "factor"), &Window::set_content_scale_factor);
3298
ClassDB::bind_method(D_METHOD("get_content_scale_factor"), &Window::get_content_scale_factor);
3299
3300
ClassDB::bind_method(D_METHOD("set_mouse_passthrough_polygon", "polygon"), &Window::set_mouse_passthrough_polygon);
3301
ClassDB::bind_method(D_METHOD("get_mouse_passthrough_polygon"), &Window::get_mouse_passthrough_polygon);
3302
3303
ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
3304
ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
3305
ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
3306
3307
ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
3308
ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
3309
3310
ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Window::set_theme_type_variation);
3311
ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Window::get_theme_type_variation);
3312
3313
ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Window::begin_bulk_theme_override);
3314
ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Window::end_bulk_theme_override);
3315
3316
ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Window::add_theme_icon_override);
3317
ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Window::add_theme_style_override);
3318
ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Window::add_theme_font_override);
3319
ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Window::add_theme_font_size_override);
3320
ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Window::add_theme_color_override);
3321
ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Window::add_theme_constant_override);
3322
3323
ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Window::remove_theme_icon_override);
3324
ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Window::remove_theme_style_override);
3325
ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Window::remove_theme_font_override);
3326
ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Window::remove_theme_font_size_override);
3327
ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Window::remove_theme_color_override);
3328
ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Window::remove_theme_constant_override);
3329
3330
ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Window::get_theme_icon, DEFVAL(StringName()));
3331
ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Window::get_theme_stylebox, DEFVAL(StringName()));
3332
ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Window::get_theme_font, DEFVAL(StringName()));
3333
ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Window::get_theme_font_size, DEFVAL(StringName()));
3334
ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Window::get_theme_color, DEFVAL(StringName()));
3335
ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Window::get_theme_constant, DEFVAL(StringName()));
3336
3337
ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Window::has_theme_icon_override);
3338
ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Window::has_theme_stylebox_override);
3339
ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Window::has_theme_font_override);
3340
ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Window::has_theme_font_size_override);
3341
ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Window::has_theme_color_override);
3342
ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Window::has_theme_constant_override);
3343
3344
ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Window::has_theme_icon, DEFVAL(StringName()));
3345
ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Window::has_theme_stylebox, DEFVAL(StringName()));
3346
ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Window::has_theme_font, DEFVAL(StringName()));
3347
ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Window::has_theme_font_size, DEFVAL(StringName()));
3348
ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Window::has_theme_color, DEFVAL(StringName()));
3349
ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Window::has_theme_constant, DEFVAL(StringName()));
3350
3351
ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Window::get_theme_default_base_scale);
3352
ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Window::get_theme_default_font);
3353
ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Window::get_theme_default_font_size);
3354
3355
ClassDB::bind_method(D_METHOD("get_window_id"), &Window::get_window_id);
3356
3357
ClassDB::bind_method(D_METHOD("set_accessibility_name", "name"), &Window::set_accessibility_name);
3358
ClassDB::bind_method(D_METHOD("get_accessibility_name"), &Window::get_accessibility_name);
3359
ClassDB::bind_method(D_METHOD("set_accessibility_description", "description"), &Window::set_accessibility_description);
3360
ClassDB::bind_method(D_METHOD("get_accessibility_description"), &Window::get_accessibility_description);
3361
3362
ClassDB::bind_static_method("Window", D_METHOD("get_focused_window"), &Window::get_focused_window);
3363
3364
ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Window::set_layout_direction);
3365
ClassDB::bind_method(D_METHOD("get_layout_direction"), &Window::get_layout_direction);
3366
ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Window::is_layout_rtl);
3367
3368
#ifndef DISABLE_DEPRECATED
3369
ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Window::set_auto_translate);
3370
ClassDB::bind_method(D_METHOD("is_auto_translating"), &Window::is_auto_translating);
3371
3372
ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
3373
ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
3374
#endif
3375
3376
ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
3377
ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
3378
ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
3379
ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
3380
ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
3381
3382
ClassDB::bind_method(D_METHOD("popup_exclusive", "from_node", "rect"), &Window::popup_exclusive, DEFVAL(Rect2i()));
3383
ClassDB::bind_method(D_METHOD("popup_exclusive_on_parent", "from_node", "parent_rect"), &Window::popup_exclusive_on_parent);
3384
ClassDB::bind_method(D_METHOD("popup_exclusive_centered", "from_node", "minsize"), &Window::popup_exclusive_centered, DEFVAL(Size2i()));
3385
ClassDB::bind_method(D_METHOD("popup_exclusive_centered_ratio", "from_node", "ratio"), &Window::popup_exclusive_centered_ratio, DEFVAL(0.8));
3386
ClassDB::bind_method(D_METHOD("popup_exclusive_centered_clamped", "from_node", "minsize", "fallback_ratio"), &Window::popup_exclusive_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
3387
3388
// Keep the enum values in sync with the `Mode` enum.
3389
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"), "set_mode", "get_mode");
3390
3391
ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
3392
3393
// Keep the enum values in sync with the `WindowInitialPosition` enum.
3394
ADD_PROPERTY(PropertyInfo(Variant::INT, "initial_position", PROPERTY_HINT_ENUM, "Absolute,Center of Primary Screen,Center of Main Window Screen,Center of Other Screen,Center of Screen With Mouse Pointer,Center of Screen With Keyboard Focus"), "set_initial_position", "get_initial_position");
3395
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
3396
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
3397
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_current_screen", "get_current_screen");
3398
ADD_PROPERTY(PropertyInfo(Variant::RECT2I, "nonclient_area", PROPERTY_HINT_NONE, ""), "set_nonclient_area", "get_nonclient_area");
3399
3400
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "mouse_passthrough_polygon"), "set_mouse_passthrough_polygon", "get_mouse_passthrough_polygon");
3401
3402
ADD_GROUP("Flags", "");
3403
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
3404
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
3405
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
3406
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient_to_focused"), "set_transient_to_focused", "is_transient_to_focused");
3407
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
3408
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
3409
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
3410
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
3411
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
3412
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
3413
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
3414
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
3415
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "mouse_passthrough"), "set_flag", "get_flag", FLAG_MOUSE_PASSTHROUGH);
3416
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "sharp_corners"), "set_flag", "get_flag", FLAG_SHARP_CORNERS);
3417
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "exclude_from_capture"), "set_flag", "get_flag", FLAG_EXCLUDE_FROM_CAPTURE);
3418
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_wm_hint"), "set_flag", "get_flag", FLAG_POPUP_WM_HINT);
3419
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "minimize_disabled"), "set_flag", "get_flag", FLAG_MINIMIZE_DISABLED);
3420
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "maximize_disabled"), "set_flag", "get_flag", FLAG_MAXIMIZE_DISABLED);
3421
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_native"), "set_force_native", "get_force_native");
3422
3423
ADD_GROUP("Limits", "");
3424
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
3425
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size", PROPERTY_HINT_NONE, "suffix:px"), "set_max_size", "get_max_size");
3426
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_title_visible"), "set_keep_title_visible", "get_keep_title_visible");
3427
3428
ADD_GROUP("Content Scale", "content_scale_");
3429
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
3430
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,Canvas Items,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
3431
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_aspect", PROPERTY_HINT_ENUM, "Ignore,Keep,Keep Width,Keep Height,Expand"), "set_content_scale_aspect", "get_content_scale_aspect");
3432
ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_stretch", PROPERTY_HINT_ENUM, "Fractional,Integer"), "set_content_scale_stretch", "get_content_scale_stretch");
3433
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "content_scale_factor", PROPERTY_HINT_RANGE, "0.5,8.0,0.01"), "set_content_scale_factor", "get_content_scale_factor");
3434
3435
#ifndef DISABLE_DEPRECATED
3436
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_auto_translate", "is_auto_translating");
3437
#endif
3438
3439
ADD_GROUP("Accessibility", "accessibility_");
3440
ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_name"), "set_accessibility_name", "get_accessibility_name");
3441
ADD_PROPERTY(PropertyInfo(Variant::STRING, "accessibility_description"), "set_accessibility_description", "get_accessibility_description");
3442
3443
ADD_GROUP("Theme", "theme_");
3444
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, Theme::get_class_static()), "set_theme", "get_theme");
3445
ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
3446
3447
ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, InputEvent::get_class_static())));
3448
ADD_SIGNAL(MethodInfo("nonclient_window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, InputEvent::get_class_static())));
3449
ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
3450
ADD_SIGNAL(MethodInfo("mouse_entered"));
3451
ADD_SIGNAL(MethodInfo("mouse_exited"));
3452
ADD_SIGNAL(MethodInfo("focus_entered"));
3453
ADD_SIGNAL(MethodInfo("focus_exited"));
3454
ADD_SIGNAL(MethodInfo("close_requested"));
3455
ADD_SIGNAL(MethodInfo("go_back_requested"));
3456
ADD_SIGNAL(MethodInfo("visibility_changed"));
3457
ADD_SIGNAL(MethodInfo("about_to_popup"));
3458
ADD_SIGNAL(MethodInfo("theme_changed"));
3459
ADD_SIGNAL(MethodInfo("dpi_changed"));
3460
ADD_SIGNAL(MethodInfo("titlebar_changed"));
3461
ADD_SIGNAL(MethodInfo("title_changed"));
3462
3463
BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
3464
BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
3465
3466
BIND_ENUM_CONSTANT(MODE_WINDOWED);
3467
BIND_ENUM_CONSTANT(MODE_MINIMIZED);
3468
BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
3469
BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
3470
BIND_ENUM_CONSTANT(MODE_EXCLUSIVE_FULLSCREEN);
3471
3472
BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
3473
BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
3474
BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
3475
BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
3476
BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
3477
BIND_ENUM_CONSTANT(FLAG_POPUP);
3478
BIND_ENUM_CONSTANT(FLAG_EXTEND_TO_TITLE);
3479
BIND_ENUM_CONSTANT(FLAG_MOUSE_PASSTHROUGH);
3480
BIND_ENUM_CONSTANT(FLAG_SHARP_CORNERS);
3481
BIND_ENUM_CONSTANT(FLAG_EXCLUDE_FROM_CAPTURE);
3482
BIND_ENUM_CONSTANT(FLAG_POPUP_WM_HINT);
3483
BIND_ENUM_CONSTANT(FLAG_MINIMIZE_DISABLED);
3484
BIND_ENUM_CONSTANT(FLAG_MAXIMIZE_DISABLED);
3485
BIND_ENUM_CONSTANT(FLAG_MAX);
3486
3487
BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
3488
BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
3489
BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
3490
3491
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
3492
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
3493
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
3494
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
3495
BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
3496
3497
BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_FRACTIONAL);
3498
BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_INTEGER);
3499
3500
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
3501
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_APPLICATION_LOCALE);
3502
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
3503
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
3504
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_SYSTEM_LOCALE);
3505
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_MAX);
3506
#ifndef DISABLE_DEPRECATED
3507
BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
3508
#endif // DISABLE_DEPRECATED
3509
3510
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_ABSOLUTE);
3511
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN);
3512
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN);
3513
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN);
3514
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS);
3515
BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS);
3516
3517
GDVIRTUAL_BIND(_get_contents_minimum_size);
3518
3519
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_border);
3520
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_unfocused_border);
3521
3522
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Window, title_font);
3523
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Window, title_font_size);
3524
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_color);
3525
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_height);
3526
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_outline_modulate);
3527
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_outline_size);
3528
3529
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close);
3530
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close_pressed);
3531
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_h_offset);
3532
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_v_offset);
3533
3534
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, resize_margin);
3535
}
3536
3537
Window::Window() {
3538
RenderingServer *rendering_server = RenderingServer::get_singleton();
3539
if (rendering_server) {
3540
max_size = rendering_server->get_maximum_viewport_size();
3541
max_size_used = max_size; // Update max_size_used.
3542
}
3543
3544
theme_owner = memnew(ThemeOwner(this));
3545
RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
3546
}
3547
3548
Window::~Window() {
3549
if (focused_window == this) {
3550
focused_window = nullptr;
3551
}
3552
memdelete(theme_owner);
3553
3554
// Resources need to be disconnected.
3555
for (KeyValue<StringName, Ref<Texture2D>> &E : theme_icon_override) {
3556
E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3557
}
3558
for (KeyValue<StringName, Ref<StyleBox>> &E : theme_style_override) {
3559
E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3560
}
3561
for (KeyValue<StringName, Ref<Font>> &E : theme_font_override) {
3562
E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
3563
}
3564
3565
// Then override maps can be simply cleared.
3566
theme_icon_override.clear();
3567
theme_style_override.clear();
3568
theme_font_override.clear();
3569
theme_font_size_override.clear();
3570
theme_color_override.clear();
3571
theme_constant_override.clear();
3572
}
3573
3574