Debian Codegraph @

scene/main/window.h

raw symbols SCIP
1 /**************************************************************************/
2 /* window.h */
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 #pragma once
32
33 #include "scene/main/viewport.h"
34 #include "scene/resources/theme.h"
35 #include "servers/display/display_server.h"
36
37 class Font;
38 class Shortcut;
39 class StyleBox;
40 class ThemeOwner;
41 class ThemeContext;
42
43 class Window : public Viewport {
44 GDCLASS(Window, Viewport);
45
46 public:
47 // Keep synced with enum hint for `mode` property.
48 enum Mode {
49 MODE_WINDOWED = DisplayServer::WINDOW_MODE_WINDOWED,
50 MODE_MINIMIZED = DisplayServer::WINDOW_MODE_MINIMIZED,
51 MODE_MAXIMIZED = DisplayServer::WINDOW_MODE_MAXIMIZED,
52 MODE_FULLSCREEN = DisplayServer::WINDOW_MODE_FULLSCREEN,
53 MODE_EXCLUSIVE_FULLSCREEN = DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN,
54 };
55
56 enum Flags {
57 FLAG_RESIZE_DISABLED = DisplayServer::WINDOW_FLAG_RESIZE_DISABLED,
58 FLAG_BORDERLESS = DisplayServer::WINDOW_FLAG_BORDERLESS,
59 FLAG_ALWAYS_ON_TOP = DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP,
60 FLAG_TRANSPARENT = DisplayServer::WINDOW_FLAG_TRANSPARENT,
61 FLAG_NO_FOCUS = DisplayServer::WINDOW_FLAG_NO_FOCUS,
62 FLAG_POPUP = DisplayServer::WINDOW_FLAG_POPUP,
63 FLAG_EXTEND_TO_TITLE = DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE,
64 FLAG_MOUSE_PASSTHROUGH = DisplayServer::WINDOW_FLAG_MOUSE_PASSTHROUGH,
65 FLAG_SHARP_CORNERS = DisplayServer::WINDOW_FLAG_SHARP_CORNERS,
66 FLAG_EXCLUDE_FROM_CAPTURE = DisplayServer::WINDOW_FLAG_EXCLUDE_FROM_CAPTURE,
67 FLAG_POPUP_WM_HINT = DisplayServer::WINDOW_FLAG_POPUP_WM_HINT,
68 FLAG_MINIMIZE_DISABLED = DisplayServer::WINDOW_FLAG_MINIMIZE_DISABLED,
69 FLAG_MAXIMIZE_DISABLED = DisplayServer::WINDOW_FLAG_MAXIMIZE_DISABLED,
70 FLAG_MAX = DisplayServer::WINDOW_FLAG_MAX,
71 };
72
73 enum ContentScaleMode {
74 CONTENT_SCALE_MODE_DISABLED,
75 CONTENT_SCALE_MODE_CANVAS_ITEMS,
76 CONTENT_SCALE_MODE_VIEWPORT,
77 };
78
79 enum ContentScaleAspect {
80 CONTENT_SCALE_ASPECT_IGNORE,
81 CONTENT_SCALE_ASPECT_KEEP,
82 CONTENT_SCALE_ASPECT_KEEP_WIDTH,
83 CONTENT_SCALE_ASPECT_KEEP_HEIGHT,
84 CONTENT_SCALE_ASPECT_EXPAND,
85 };
86
87 enum ContentScaleStretch {
88 CONTENT_SCALE_STRETCH_FRACTIONAL,
89 CONTENT_SCALE_STRETCH_INTEGER,
90 };
91
92 enum LayoutDirection {
93 LAYOUT_DIRECTION_INHERITED,
94 LAYOUT_DIRECTION_APPLICATION_LOCALE,
95 LAYOUT_DIRECTION_LTR,
96 LAYOUT_DIRECTION_RTL,
97 LAYOUT_DIRECTION_SYSTEM_LOCALE,
98 LAYOUT_DIRECTION_MAX,
99 #ifndef DISABLE_DEPRECATED
100 LAYOUT_DIRECTION_LOCALE = LAYOUT_DIRECTION_APPLICATION_LOCALE,
101 #endif // DISABLE_DEPRECATED
102 };
103
104 enum {
105 DEFAULT_WINDOW_SIZE = 100,
106 };
107
108 // Keep synced with enum hint for `initial_position` property and `display/window/size/initial_position_type` project setting.
109 enum WindowInitialPosition {
110 WINDOW_INITIAL_POSITION_ABSOLUTE,
111 WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN,
112 WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN,
113 WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN,
114 WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS,
115 WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS,
116 };
117
118 private:
119 DisplayServer::WindowID window_id = DisplayServer::INVALID_WINDOW_ID;
120 bool initialized = false;
121
122 String title;
123 String displayed_title;
124 mutable int current_screen = 0;
125 mutable Point2i position;
126 mutable Size2i size = Size2i(DEFAULT_WINDOW_SIZE, DEFAULT_WINDOW_SIZE);
127 mutable Size2i min_size;
128 mutable Size2i max_size;
129 mutable Vector<Vector2> mpath;
130 mutable Mode mode = MODE_WINDOWED;
131 mutable bool flags[FLAG_MAX] = {};
132 bool visible = true;
133 bool focused = false;
134 WindowInitialPosition initial_position = WINDOW_INITIAL_POSITION_ABSOLUTE;
135 bool force_native = false;
136
137 bool transient = false;
138 bool transient_to_focused = false;
139 bool exclusive = false;
140 bool wrap_controls = false;
141 bool updating_child_controls = false;
142 bool updating_embedded_window = false;
143 bool clamp_to_embedder = false;
144 bool unparent_when_invisible = false;
145 bool keep_title_visible = false;
146
147 LayoutDirection layout_dir = LAYOUT_DIRECTION_INHERITED;
148
149 void _update_child_controls();
150 void _update_embedded_window();
151
152 Size2i content_scale_size;
153 ContentScaleMode content_scale_mode = CONTENT_SCALE_MODE_DISABLED;
154 ContentScaleAspect content_scale_aspect = CONTENT_SCALE_ASPECT_IGNORE;
155 ContentScaleStretch content_scale_stretch = CONTENT_SCALE_STRETCH_FRACTIONAL;
156 real_t content_scale_factor = 1.0;
157
158 RID accessibility_title_element;
159 RID accessibility_announcement_element;
160 String announcement;
161 String accessibility_name;
162 String accessibility_description;
163
164 void _make_window();
165 void _clear_window();
166 void _update_from_window();
167 void _accessibility_notify_enter(Node *p_node);
168 void _accessibility_notify_exit(Node *p_node);
169
170 void _accessibility_activate();
171 void _accessibility_deactivate();
172
173 bool _try_parent_dialog(Node *p_from_node);
174
175 Size2i max_size_used;
176
177 Rect2i nonclient_area;
178
179 Size2i _clamp_limit_size(const Size2i &p_limit_size);
180 Size2i _clamp_window_size(const Size2i &p_size);
181 void _validate_limit_size();
182 void _update_viewport_size();
183 void _update_window_size();
184
185 void _propagate_window_notification(Node *p_node, int p_notification);
186
187 void _update_window_callbacks();
188
189 Window *transient_parent = nullptr;
190 Window *exclusive_child = nullptr;
191 HashSet<Window *> transient_children;
192
193 void _clear_transient();
194 void _make_transient();
195 void _set_transient_exclusive_child(bool p_clear_invalid = false);
196
197 static Window *focused_window;
198
199 ThemeOwner *theme_owner = nullptr;
200 Ref<Theme> theme;
201 StringName theme_type_variation;
202
203 bool bulk_theme_override = false;
204 Theme::ThemeIconMap theme_icon_override;
205 Theme::ThemeStyleMap theme_style_override;
206 Theme::ThemeFontMap theme_font_override;
207 Theme::ThemeFontSizeMap theme_font_size_override;
208 Theme::ThemeColorMap theme_color_override;
209 Theme::ThemeConstantMap theme_constant_override;
210
211 mutable HashMap<StringName, Theme::ThemeIconMap> theme_icon_cache;
212 mutable HashMap<StringName, Theme::ThemeStyleMap> theme_style_cache;
213 mutable HashMap<StringName, Theme::ThemeFontMap> theme_font_cache;
214 mutable HashMap<StringName, Theme::ThemeFontSizeMap> theme_font_size_cache;
215 mutable HashMap<StringName, Theme::ThemeColorMap> theme_color_cache;
216 mutable HashMap<StringName, Theme::ThemeConstantMap> theme_constant_cache;
217
218 void _theme_changed();
219 void _notify_theme_override_changed();
220 void _invalidate_theme_cache();
221
222 struct ThemeCache {
223 Ref<StyleBox> embedded_border;
224 Ref<StyleBox> embedded_unfocused_border;
225
226 Ref<Font> title_font;
227 int title_font_size = 0;
228 Color title_color;
229 int title_height = 0;
230 Color title_outline_modulate;
231 int title_outline_size = 0;
232
233 Ref<Texture2D> close;
234 Ref<Texture2D> close_pressed;
235 int close_h_offset = 0;
236 int close_v_offset = 0;
237
238 int resize_margin = 0;
239 } theme_cache;
240
241 void _settings_changed();
242
243 Viewport *embedder = nullptr;
244
245 Transform2D window_transform;
246
247 friend class Viewport; //friend back, can call the methods below
248
249 void _window_input(const Ref<InputEvent> &p_ev);
250 void _window_input_text(const String &p_text, bool p_emit_signal = false);
251 void _window_drop_files(const Vector<String> &p_files);
252 void _rect_changed_callback(const Rect2i &p_callback);
253 void _event_callback(DisplayServer::WindowEvent p_event);
254 virtual bool _can_consume_input_events() const override;
255
256 bool mouse_in_window = false;
257 void _update_mouse_over(Vector2 p_pos) override;
258 void _mouse_leave_viewport() override;
259
260 void _update_displayed_title();
261
262 static int root_layout_direction;
263
264 protected:
265 virtual void _popup_base(const Rect2i &p_screen_rect = Rect2i());
266 virtual void _pre_popup() {} // Called after "about_to_popup", but before window is shown.
267 virtual Rect2i _popup_adjust_rect() const { return Rect2i(); }
268 virtual void _post_popup() {}
269
270 virtual void _update_theme_item_cache();
271 virtual void _input_from_window(const Ref<InputEvent> &p_event) {}
272
273 void _notification(int p_what);
274 static void _bind_methods();
275
276 bool _set(const StringName &p_name, const Variant &p_value);
277 bool _get(const StringName &p_name, Variant &r_ret) const;
278 void _get_property_list(List<PropertyInfo> *p_list) const;
279 void _validate_property(PropertyInfo &p_property) const;
280
281 void _accessibility_action_grab_focus(const Variant &p_data) {
282 grab_focus();
283 }
284
285 virtual void add_child_notify(Node *p_child) override;
286 virtual void remove_child_notify(Node *p_child) override;
287
288 GDVIRTUAL0RC(Vector2, _get_contents_minimum_size)
289
290 public:
291 enum {
292 NOTIFICATION_VISIBILITY_CHANGED = 30,
293 NOTIFICATION_POST_POPUP = 31,
294 NOTIFICATION_THEME_CHANGED = 32
295 };
296
297 static void set_root_layout_direction(int p_root_dir);
298 static Window *get_from_id(DisplayServer::WindowID p_window_id);
299
300 RID get_accessibility_element() const override;
301 virtual RID get_focused_accessibility_element() const override;
302
303 void set_title(const String &p_title);
304 String get_title() const;
305 String get_displayed_title() const;
306
307 void set_initial_position(WindowInitialPosition p_initial_position);
308 WindowInitialPosition get_initial_position() const;
309
310 void set_force_native(bool p_force_native);
311 bool get_force_native() const;
312
313 void set_current_screen(int p_screen);
314 int get_current_screen() const;
315
316 void set_position(const Point2i &p_position);
317 Point2i get_position() const;
318 void move_to_center();
319
320 void set_size(const Size2i &p_size);
321 Size2i get_size() const;
322 void reset_size();
323
324 Point2i get_position_with_decorations() const;
325 Size2i get_size_with_decorations() const;
326
327 void set_max_size(const Size2i &p_max_size);
328 Size2i get_max_size() const;
329
330 void set_min_size(const Size2i &p_min_size);
331 Size2i get_min_size() const;
332
333 void set_mode(Mode p_mode);
334 Mode get_mode() const;
335
336 void set_flag(Flags p_flag, bool p_enabled);
337 bool get_flag(Flags p_flag) const;
338
339 bool is_popup() const;
340
341 bool is_maximize_allowed() const;
342
343 void request_attention();
344 #ifndef DISABLE_DEPRECATED
345 void move_to_foreground();
346 #endif // DISABLE_DEPRECATED
347
348 virtual void set_visible(bool p_visible);
349 bool is_visible() const;
350
351 void update_mouse_cursor_state() override;
352
353 void show();
354 void hide();
355
356 void set_transient(bool p_transient);
357 bool is_transient() const;
358
359 void set_transient_to_focused(bool p_transient_to_focused);
360 bool is_transient_to_focused() const;
361
362 void set_exclusive(bool p_exclusive);
363 bool is_exclusive() const;
364
365 void set_clamp_to_embedder(bool p_enable);
366 bool is_clamped_to_embedder() const;
367
368 void set_unparent_when_invisible(bool p_unparent);
369
370 bool is_in_edited_scene_root() const;
371
372 bool can_draw() const;
373
374 void set_ime_active(bool p_active);
375 void set_ime_position(const Point2i &p_pos);
376
377 bool is_embedded() const;
378 Viewport *get_embedder() const;
379
380 void set_content_scale_size(const Size2i &p_size);
381 Size2i get_content_scale_size() const;
382
383 void set_content_scale_mode(ContentScaleMode p_mode);
384 ContentScaleMode get_content_scale_mode() const;
385
386 void set_content_scale_aspect(ContentScaleAspect p_aspect);
387 ContentScaleAspect get_content_scale_aspect() const;
388
389 void set_content_scale_stretch(ContentScaleStretch p_stretch);
390 ContentScaleStretch get_content_scale_stretch() const;
391
392 void set_keep_title_visible(bool p_title_visible);
393 bool get_keep_title_visible() const;
394
395 void set_content_scale_factor(real_t p_factor);
396 real_t get_content_scale_factor() const;
397
398 void set_nonclient_area(const Rect2i &p_rect);
399 Rect2i get_nonclient_area() const;
400
401 void set_mouse_passthrough_polygon(const Vector<Vector2> &p_region);
402 Vector<Vector2> get_mouse_passthrough_polygon() const;
403
404 void set_wrap_controls(bool p_enable);
405 bool is_wrapping_controls() const;
406 void child_controls_changed();
407
408 Window *get_exclusive_child() const { return exclusive_child; }
409 HashSet<Window *> get_transient_children() const { return transient_children; }
410 Window *get_parent_visible_window() const;
411 Window *get_non_popup_window() const;
412 Viewport *get_parent_viewport() const;
413
414 void popup(const Rect2i &p_screen_rect = Rect2i());
415 void popup_on_parent(const Rect2i &p_parent_rect);
416 void popup_centered(const Size2i &p_minsize = Size2i());
417 void popup_centered_ratio(float p_ratio = 0.8);
418 void popup_centered_clamped(const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
419
420 void popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect = Rect2i());
421 void popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect);
422 void popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize = Size2i());
423 void popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio = 0.8);
424 void popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
425
426 Rect2i fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const;
427 Size2 get_contents_minimum_size() const;
428 Size2 get_clamped_minimum_size() const;
429
430 void grab_focus();
431 bool has_focus() const;
432 bool has_focus_or_active_popup() const;
433
434 void start_drag();
435 void start_resize(DisplayServer::WindowResizeEdge p_edge);
436
437 Rect2i get_usable_parent_rect() const;
438
439 void set_accessibility_name(const String &p_name);
440 String get_accessibility_name() const;
441
442 void set_accessibility_description(const String &p_description);
443 String get_accessibility_description() const;
444
445 void accessibility_announcement(const String &p_announcement);
446
447 // Internationalization.
448
449 void set_layout_direction(LayoutDirection p_direction);
450 LayoutDirection get_layout_direction() const;
451 bool is_layout_rtl() const;
452
453 #ifndef DISABLE_DEPRECATED
454 void set_use_font_oversampling(bool p_oversampling);
455 bool is_using_font_oversampling() const;
456
457 void set_auto_translate(bool p_enable);
458 bool is_auto_translating() const;
459 #endif
460
461 // Theming.
462
463 void set_theme_owner_node(Node *p_node);
464 Node *get_theme_owner_node() const;
465 bool has_theme_owner_node() const;
466
467 void set_theme_context(ThemeContext *p_context, bool p_propagate = true);
468
469 void set_theme(const Ref<Theme> &p_theme);
470 Ref<Theme> get_theme() const;
471
472 void set_theme_type_variation(const StringName &p_theme_type);
473 StringName get_theme_type_variation() const;
474
475 void begin_bulk_theme_override();
476 void end_bulk_theme_override();
477
478 void add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon);
479 void add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style);
480 void add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font);
481 void add_theme_font_size_override(const StringName &p_name, int p_font_size);
482 void add_theme_color_override(const StringName &p_name, const Color &p_color);
483 void add_theme_constant_override(const StringName &p_name, int p_constant);
484
485 void remove_theme_icon_override(const StringName &p_name);
486 void remove_theme_style_override(const StringName &p_name);
487 void remove_theme_font_override(const StringName &p_name);
488 void remove_theme_font_size_override(const StringName &p_name);
489 void remove_theme_color_override(const StringName &p_name);
490 void remove_theme_constant_override(const StringName &p_name);
491
492 Ref<Texture2D> get_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
493 Ref<StyleBox> get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
494 Ref<Font> get_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
495 int get_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
496 Color get_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
497 int get_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
498 Variant get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type = StringName()) const;
499 #ifdef TOOLS_ENABLED
500 Ref<Texture2D> get_editor_theme_icon(const StringName &p_name) const;
501 #endif
502
503 bool has_theme_icon_override(const StringName &p_name) const;
504 bool has_theme_stylebox_override(const StringName &p_name) const;
505 bool has_theme_font_override(const StringName &p_name) const;
506 bool has_theme_font_size_override(const StringName &p_name) const;
507 bool has_theme_color_override(const StringName &p_name) const;
508 bool has_theme_constant_override(const StringName &p_name) const;
509
510 bool has_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
511 bool has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
512 bool has_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
513 bool has_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
514 bool has_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
515 bool has_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
516
517 float get_theme_default_base_scale() const;
518 Ref<Font> get_theme_default_font() const;
519 int get_theme_default_font_size() const;
520
521 virtual Transform2D get_final_transform() const override;
522 virtual Transform2D get_screen_transform_internal(bool p_absolute_position = false) const override;
523 virtual Transform2D get_popup_base_transform() const override;
524 virtual Transform2D get_popup_base_transform_native() const override;
525 virtual Viewport *get_section_root_viewport() const override;
526 virtual bool is_attached_in_viewport() const override;
527
528 Rect2i get_parent_rect() const;
529 virtual DisplayServer::WindowID get_window_id() const override;
530 static Window *get_focused_window() { return focused_window; }
531
532 virtual Size2 _get_contents_minimum_size() const;
533
534 Window();
535 ~Window();
536 };
537
538 VARIANT_ENUM_CAST(Window::Mode);
539 VARIANT_ENUM_CAST(Window::Flags);
540 VARIANT_ENUM_CAST(Window::ContentScaleMode);
541 VARIANT_ENUM_CAST(Window::ContentScaleAspect);
542 VARIANT_ENUM_CAST(Window::ContentScaleStretch);
543 VARIANT_ENUM_CAST(Window::LayoutDirection);
544 VARIANT_ENUM_CAST(Window::WindowInitialPosition);