window.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. #ifndef WINDOW_H
  31. #define WINDOW_H
  32. #include "scene/main/viewport.h"
  33. #include "scene/resources/theme.h"
  34. #include "servers/display_server.h"
  35. class Font;
  36. class Shortcut;
  37. class StyleBox;
  38. class ThemeOwner;
  39. class ThemeContext;
  40. class Window : public Viewport {
  41. GDCLASS(Window, Viewport);
  42. public:
  43. // Keep synced with enum hint for `mode` property.
  44. enum Mode {
  45. MODE_WINDOWED = DisplayServer::WINDOW_MODE_WINDOWED,
  46. MODE_MINIMIZED = DisplayServer::WINDOW_MODE_MINIMIZED,
  47. MODE_MAXIMIZED = DisplayServer::WINDOW_MODE_MAXIMIZED,
  48. MODE_FULLSCREEN = DisplayServer::WINDOW_MODE_FULLSCREEN,
  49. MODE_EXCLUSIVE_FULLSCREEN = DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN,
  50. };
  51. enum Flags {
  52. FLAG_RESIZE_DISABLED = DisplayServer::WINDOW_FLAG_RESIZE_DISABLED,
  53. FLAG_BORDERLESS = DisplayServer::WINDOW_FLAG_BORDERLESS,
  54. FLAG_ALWAYS_ON_TOP = DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP,
  55. FLAG_TRANSPARENT = DisplayServer::WINDOW_FLAG_TRANSPARENT,
  56. FLAG_NO_FOCUS = DisplayServer::WINDOW_FLAG_NO_FOCUS,
  57. FLAG_POPUP = DisplayServer::WINDOW_FLAG_POPUP,
  58. FLAG_EXTEND_TO_TITLE = DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE,
  59. FLAG_MOUSE_PASSTHROUGH = DisplayServer::WINDOW_FLAG_MOUSE_PASSTHROUGH,
  60. FLAG_SHARP_CORNERS = DisplayServer::WINDOW_FLAG_SHARP_CORNERS,
  61. FLAG_EXCLUDE_FROM_CAPTURE = DisplayServer::WINDOW_FLAG_EXCLUDE_FROM_CAPTURE,
  62. FLAG_MAX = DisplayServer::WINDOW_FLAG_MAX,
  63. };
  64. enum ContentScaleMode {
  65. CONTENT_SCALE_MODE_DISABLED,
  66. CONTENT_SCALE_MODE_CANVAS_ITEMS,
  67. CONTENT_SCALE_MODE_VIEWPORT,
  68. };
  69. enum ContentScaleAspect {
  70. CONTENT_SCALE_ASPECT_IGNORE,
  71. CONTENT_SCALE_ASPECT_KEEP,
  72. CONTENT_SCALE_ASPECT_KEEP_WIDTH,
  73. CONTENT_SCALE_ASPECT_KEEP_HEIGHT,
  74. CONTENT_SCALE_ASPECT_EXPAND,
  75. };
  76. enum ContentScaleStretch {
  77. CONTENT_SCALE_STRETCH_FRACTIONAL,
  78. CONTENT_SCALE_STRETCH_INTEGER,
  79. };
  80. enum LayoutDirection {
  81. LAYOUT_DIRECTION_INHERITED,
  82. LAYOUT_DIRECTION_APPLICATION_LOCALE,
  83. LAYOUT_DIRECTION_LTR,
  84. LAYOUT_DIRECTION_RTL,
  85. LAYOUT_DIRECTION_SYSTEM_LOCALE,
  86. LAYOUT_DIRECTION_MAX,
  87. #ifndef DISABLE_DEPRECATED
  88. LAYOUT_DIRECTION_LOCALE = LAYOUT_DIRECTION_APPLICATION_LOCALE,
  89. #endif // DISABLE_DEPRECATED
  90. };
  91. enum {
  92. DEFAULT_WINDOW_SIZE = 100,
  93. };
  94. // Keep synced with enum hint for `initial_position` property.
  95. enum WindowInitialPosition {
  96. WINDOW_INITIAL_POSITION_ABSOLUTE,
  97. WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN,
  98. WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN,
  99. WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN,
  100. WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS,
  101. WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS,
  102. };
  103. private:
  104. DisplayServer::WindowID window_id = DisplayServer::INVALID_WINDOW_ID;
  105. bool initialized = false;
  106. String title;
  107. String tr_title;
  108. mutable int current_screen = 0;
  109. mutable Point2i position;
  110. mutable Size2i size = Size2i(DEFAULT_WINDOW_SIZE, DEFAULT_WINDOW_SIZE);
  111. mutable Size2i min_size;
  112. mutable Size2i max_size;
  113. mutable Vector<Vector2> mpath;
  114. mutable Mode mode = MODE_WINDOWED;
  115. mutable bool flags[FLAG_MAX] = {};
  116. bool visible = true;
  117. bool focused = false;
  118. WindowInitialPosition initial_position = WINDOW_INITIAL_POSITION_ABSOLUTE;
  119. bool force_native = false;
  120. bool use_font_oversampling = false;
  121. bool transient = false;
  122. bool transient_to_focused = false;
  123. bool exclusive = false;
  124. bool wrap_controls = false;
  125. bool updating_child_controls = false;
  126. bool updating_embedded_window = false;
  127. bool clamp_to_embedder = false;
  128. bool unparent_when_invisible = false;
  129. bool keep_title_visible = false;
  130. LayoutDirection layout_dir = LAYOUT_DIRECTION_INHERITED;
  131. void _update_child_controls();
  132. void _update_embedded_window();
  133. Size2i content_scale_size;
  134. ContentScaleMode content_scale_mode = CONTENT_SCALE_MODE_DISABLED;
  135. ContentScaleAspect content_scale_aspect = CONTENT_SCALE_ASPECT_IGNORE;
  136. ContentScaleStretch content_scale_stretch = CONTENT_SCALE_STRETCH_FRACTIONAL;
  137. real_t content_scale_factor = 1.0;
  138. void _make_window();
  139. void _clear_window();
  140. void _update_from_window();
  141. bool _try_parent_dialog(Node *p_from_node);
  142. Size2i max_size_used;
  143. Size2i _clamp_limit_size(const Size2i &p_limit_size);
  144. Size2i _clamp_window_size(const Size2i &p_size);
  145. void _validate_limit_size();
  146. void _update_viewport_size();
  147. void _update_window_size();
  148. void _propagate_window_notification(Node *p_node, int p_notification);
  149. void _update_window_callbacks();
  150. Window *transient_parent = nullptr;
  151. Window *exclusive_child = nullptr;
  152. HashSet<Window *> transient_children;
  153. void _clear_transient();
  154. void _make_transient();
  155. void _set_transient_exclusive_child(bool p_clear_invalid = false);
  156. ThemeOwner *theme_owner = nullptr;
  157. Ref<Theme> theme;
  158. StringName theme_type_variation;
  159. bool bulk_theme_override = false;
  160. Theme::ThemeIconMap theme_icon_override;
  161. Theme::ThemeStyleMap theme_style_override;
  162. Theme::ThemeFontMap theme_font_override;
  163. Theme::ThemeFontSizeMap theme_font_size_override;
  164. Theme::ThemeColorMap theme_color_override;
  165. Theme::ThemeConstantMap theme_constant_override;
  166. mutable HashMap<StringName, Theme::ThemeIconMap> theme_icon_cache;
  167. mutable HashMap<StringName, Theme::ThemeStyleMap> theme_style_cache;
  168. mutable HashMap<StringName, Theme::ThemeFontMap> theme_font_cache;
  169. mutable HashMap<StringName, Theme::ThemeFontSizeMap> theme_font_size_cache;
  170. mutable HashMap<StringName, Theme::ThemeColorMap> theme_color_cache;
  171. mutable HashMap<StringName, Theme::ThemeConstantMap> theme_constant_cache;
  172. void _theme_changed();
  173. void _notify_theme_override_changed();
  174. void _invalidate_theme_cache();
  175. struct ThemeCache {
  176. Ref<StyleBox> embedded_border;
  177. Ref<StyleBox> embedded_unfocused_border;
  178. Ref<Font> title_font;
  179. int title_font_size = 0;
  180. Color title_color;
  181. int title_height = 0;
  182. Color title_outline_modulate;
  183. int title_outline_size = 0;
  184. Ref<Texture2D> close;
  185. Ref<Texture2D> close_pressed;
  186. int close_h_offset = 0;
  187. int close_v_offset = 0;
  188. int resize_margin = 0;
  189. } theme_cache;
  190. void _settings_changed();
  191. Viewport *embedder = nullptr;
  192. Transform2D window_transform;
  193. friend class Viewport; //friend back, can call the methods below
  194. void _window_input(const Ref<InputEvent> &p_ev);
  195. void _window_input_text(const String &p_text);
  196. void _window_drop_files(const Vector<String> &p_files);
  197. void _rect_changed_callback(const Rect2i &p_callback);
  198. void _event_callback(DisplayServer::WindowEvent p_event);
  199. virtual bool _can_consume_input_events() const override;
  200. bool mouse_in_window = false;
  201. void _update_mouse_over(Vector2 p_pos) override;
  202. void _mouse_leave_viewport() override;
  203. Ref<Shortcut> debugger_stop_shortcut;
  204. static int root_layout_direction;
  205. protected:
  206. virtual Rect2i _popup_adjust_rect() const { return Rect2i(); }
  207. virtual void _post_popup() {}
  208. virtual void _update_theme_item_cache();
  209. virtual void _input_from_window(const Ref<InputEvent> &p_event) {}
  210. void _notification(int p_what);
  211. static void _bind_methods();
  212. bool _set(const StringName &p_name, const Variant &p_value);
  213. bool _get(const StringName &p_name, Variant &r_ret) const;
  214. void _get_property_list(List<PropertyInfo> *p_list) const;
  215. void _validate_property(PropertyInfo &p_property) const;
  216. virtual void add_child_notify(Node *p_child) override;
  217. virtual void remove_child_notify(Node *p_child) override;
  218. GDVIRTUAL0RC(Vector2, _get_contents_minimum_size)
  219. public:
  220. enum {
  221. NOTIFICATION_VISIBILITY_CHANGED = 30,
  222. NOTIFICATION_POST_POPUP = 31,
  223. NOTIFICATION_THEME_CHANGED = 32
  224. };
  225. static void set_root_layout_direction(int p_root_dir);
  226. static Window *get_from_id(DisplayServer::WindowID p_window_id);
  227. void set_title(const String &p_title);
  228. String get_title() const;
  229. String get_translated_title() const;
  230. void set_initial_position(WindowInitialPosition p_initial_position);
  231. WindowInitialPosition get_initial_position() const;
  232. void set_force_native(bool p_force_native);
  233. bool get_force_native() const;
  234. void set_current_screen(int p_screen);
  235. int get_current_screen() const;
  236. void set_position(const Point2i &p_position);
  237. Point2i get_position() const;
  238. void move_to_center();
  239. void set_size(const Size2i &p_size);
  240. Size2i get_size() const;
  241. void reset_size();
  242. Point2i get_position_with_decorations() const;
  243. Size2i get_size_with_decorations() const;
  244. void set_max_size(const Size2i &p_max_size);
  245. Size2i get_max_size() const;
  246. void set_min_size(const Size2i &p_min_size);
  247. Size2i get_min_size() const;
  248. void set_mode(Mode p_mode);
  249. Mode get_mode() const;
  250. void set_flag(Flags p_flag, bool p_enabled);
  251. bool get_flag(Flags p_flag) const;
  252. bool is_maximize_allowed() const;
  253. void request_attention();
  254. #ifndef DISABLE_DEPRECATED
  255. void move_to_foreground();
  256. #endif // DISABLE_DEPRECATED
  257. virtual void set_visible(bool p_visible);
  258. bool is_visible() const;
  259. void update_mouse_cursor_state() override;
  260. void show();
  261. void hide();
  262. void set_transient(bool p_transient);
  263. bool is_transient() const;
  264. void set_transient_to_focused(bool p_transient_to_focused);
  265. bool is_transient_to_focused() const;
  266. void set_exclusive(bool p_exclusive);
  267. bool is_exclusive() const;
  268. void set_clamp_to_embedder(bool p_enable);
  269. bool is_clamped_to_embedder() const;
  270. void set_unparent_when_invisible(bool p_unparent);
  271. bool is_in_edited_scene_root() const;
  272. bool can_draw() const;
  273. void set_ime_active(bool p_active);
  274. void set_ime_position(const Point2i &p_pos);
  275. bool is_embedded() const;
  276. Viewport *get_embedder() const;
  277. void set_content_scale_size(const Size2i &p_size);
  278. Size2i get_content_scale_size() const;
  279. void set_content_scale_mode(ContentScaleMode p_mode);
  280. ContentScaleMode get_content_scale_mode() const;
  281. void set_content_scale_aspect(ContentScaleAspect p_aspect);
  282. ContentScaleAspect get_content_scale_aspect() const;
  283. void set_content_scale_stretch(ContentScaleStretch p_stretch);
  284. ContentScaleStretch get_content_scale_stretch() const;
  285. void set_keep_title_visible(bool p_title_visible);
  286. bool get_keep_title_visible() const;
  287. void set_content_scale_factor(real_t p_factor);
  288. real_t get_content_scale_factor() const;
  289. void set_use_font_oversampling(bool p_oversampling);
  290. bool is_using_font_oversampling() const;
  291. void set_mouse_passthrough_polygon(const Vector<Vector2> &p_region);
  292. Vector<Vector2> get_mouse_passthrough_polygon() const;
  293. void set_wrap_controls(bool p_enable);
  294. bool is_wrapping_controls() const;
  295. void child_controls_changed();
  296. Window *get_exclusive_child() const { return exclusive_child; }
  297. Window *get_parent_visible_window() const;
  298. Viewport *get_parent_viewport() const;
  299. virtual void popup(const Rect2i &p_screen_rect = Rect2i());
  300. void popup_on_parent(const Rect2i &p_parent_rect);
  301. void popup_centered(const Size2i &p_minsize = Size2i());
  302. void popup_centered_ratio(float p_ratio = 0.8);
  303. void popup_centered_clamped(const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
  304. void popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect = Rect2i());
  305. void popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect);
  306. void popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize = Size2i());
  307. void popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio = 0.8);
  308. void popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
  309. Rect2i fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const;
  310. Size2 get_contents_minimum_size() const;
  311. Size2 get_clamped_minimum_size() const;
  312. void grab_focus();
  313. bool has_focus() const;
  314. void start_drag();
  315. void start_resize(DisplayServer::WindowResizeEdge p_edge);
  316. Rect2i get_usable_parent_rect() const;
  317. // Internationalization.
  318. void set_layout_direction(LayoutDirection p_direction);
  319. LayoutDirection get_layout_direction() const;
  320. bool is_layout_rtl() const;
  321. #ifndef DISABLE_DEPRECATED
  322. void set_auto_translate(bool p_enable);
  323. bool is_auto_translating() const;
  324. #endif
  325. // Theming.
  326. void set_theme_owner_node(Node *p_node);
  327. Node *get_theme_owner_node() const;
  328. bool has_theme_owner_node() const;
  329. void set_theme_context(ThemeContext *p_context, bool p_propagate = true);
  330. void set_theme(const Ref<Theme> &p_theme);
  331. Ref<Theme> get_theme() const;
  332. void set_theme_type_variation(const StringName &p_theme_type);
  333. StringName get_theme_type_variation() const;
  334. void begin_bulk_theme_override();
  335. void end_bulk_theme_override();
  336. void add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon);
  337. void add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style);
  338. void add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font);
  339. void add_theme_font_size_override(const StringName &p_name, int p_font_size);
  340. void add_theme_color_override(const StringName &p_name, const Color &p_color);
  341. void add_theme_constant_override(const StringName &p_name, int p_constant);
  342. void remove_theme_icon_override(const StringName &p_name);
  343. void remove_theme_style_override(const StringName &p_name);
  344. void remove_theme_font_override(const StringName &p_name);
  345. void remove_theme_font_size_override(const StringName &p_name);
  346. void remove_theme_color_override(const StringName &p_name);
  347. void remove_theme_constant_override(const StringName &p_name);
  348. Ref<Texture2D> get_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  349. Ref<StyleBox> get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  350. Ref<Font> get_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  351. int get_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  352. Color get_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  353. int get_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  354. Variant get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  355. #ifdef TOOLS_ENABLED
  356. Ref<Texture2D> get_editor_theme_icon(const StringName &p_name) const;
  357. #endif
  358. bool has_theme_icon_override(const StringName &p_name) const;
  359. bool has_theme_stylebox_override(const StringName &p_name) const;
  360. bool has_theme_font_override(const StringName &p_name) const;
  361. bool has_theme_font_size_override(const StringName &p_name) const;
  362. bool has_theme_color_override(const StringName &p_name) const;
  363. bool has_theme_constant_override(const StringName &p_name) const;
  364. bool has_theme_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  365. bool has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  366. bool has_theme_font(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  367. bool has_theme_font_size(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  368. bool has_theme_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  369. bool has_theme_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const;
  370. float get_theme_default_base_scale() const;
  371. Ref<Font> get_theme_default_font() const;
  372. int get_theme_default_font_size() const;
  373. //
  374. virtual Transform2D get_final_transform() const override;
  375. virtual Transform2D get_screen_transform_internal(bool p_absolute_position = false) const override;
  376. virtual Transform2D get_popup_base_transform() const override;
  377. virtual Viewport *get_section_root_viewport() const override;
  378. virtual bool is_attached_in_viewport() const override;
  379. Rect2i get_parent_rect() const;
  380. virtual DisplayServer::WindowID get_window_id() const override;
  381. virtual Size2 _get_contents_minimum_size() const;
  382. Window();
  383. ~Window();
  384. };
  385. VARIANT_ENUM_CAST(Window::Mode);
  386. VARIANT_ENUM_CAST(Window::Flags);
  387. VARIANT_ENUM_CAST(Window::ContentScaleMode);
  388. VARIANT_ENUM_CAST(Window::ContentScaleAspect);
  389. VARIANT_ENUM_CAST(Window::ContentScaleStretch);
  390. VARIANT_ENUM_CAST(Window::LayoutDirection);
  391. VARIANT_ENUM_CAST(Window::WindowInitialPosition);
  392. #endif // WINDOW_H