window.h 18 KB

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