window.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. /*************************************************************************/
  2. /* window.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "window.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/input/shortcut.h"
  34. #include "core/string/translation.h"
  35. #include "core/variant/variant_parser.h"
  36. #include "scene/gui/control.h"
  37. #include "scene/scene_string_names.h"
  38. #include "scene/theme/theme_db.h"
  39. void Window::set_title(const String &p_title) {
  40. title = p_title;
  41. if (embedder) {
  42. embedder->_sub_window_update(this);
  43. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  44. String tr_title = atr(p_title);
  45. #ifdef DEBUG_ENABLED
  46. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  47. // Append a suffix to the window title to denote that the project is running
  48. // from a debug build (including the editor). Since this results in lower performance,
  49. // this should be clearly presented to the user.
  50. tr_title = vformat("%s (DEBUG)", tr_title);
  51. }
  52. #endif
  53. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  54. }
  55. }
  56. String Window::get_title() const {
  57. return title;
  58. }
  59. void Window::set_current_screen(int p_screen) {
  60. current_screen = p_screen;
  61. if (window_id == DisplayServer::INVALID_WINDOW_ID) {
  62. return;
  63. }
  64. DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
  65. }
  66. int Window::get_current_screen() const {
  67. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  68. current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
  69. }
  70. return current_screen;
  71. }
  72. void Window::set_position(const Point2i &p_position) {
  73. position = p_position;
  74. if (embedder) {
  75. embedder->_sub_window_update(this);
  76. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  77. DisplayServer::get_singleton()->window_set_position(p_position, window_id);
  78. }
  79. }
  80. Point2i Window::get_position() const {
  81. return position;
  82. }
  83. void Window::set_size(const Size2i &p_size) {
  84. size = p_size;
  85. _update_window_size();
  86. }
  87. Size2i Window::get_size() const {
  88. return size;
  89. }
  90. void Window::reset_size() {
  91. set_size(Size2i());
  92. }
  93. Size2i Window::get_real_size() const {
  94. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  95. return DisplayServer::get_singleton()->window_get_real_size(window_id);
  96. }
  97. return size;
  98. }
  99. void Window::set_max_size(const Size2i &p_max_size) {
  100. max_size = p_max_size;
  101. _update_window_size();
  102. }
  103. Size2i Window::get_max_size() const {
  104. return max_size;
  105. }
  106. void Window::set_min_size(const Size2i &p_min_size) {
  107. min_size = p_min_size;
  108. _update_window_size();
  109. }
  110. Size2i Window::get_min_size() const {
  111. return min_size;
  112. }
  113. void Window::set_mode(Mode p_mode) {
  114. mode = p_mode;
  115. if (embedder) {
  116. embedder->_sub_window_update(this);
  117. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  118. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
  119. }
  120. }
  121. Window::Mode Window::get_mode() const {
  122. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  123. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  124. }
  125. return mode;
  126. }
  127. void Window::set_flag(Flags p_flag, bool p_enabled) {
  128. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  129. flags[p_flag] = p_enabled;
  130. if (embedder) {
  131. embedder->_sub_window_update(this);
  132. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  133. #ifdef TOOLS_ENABLED
  134. if ((p_flag != FLAG_POPUP) || !(Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this))) {
  135. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
  136. }
  137. #else
  138. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
  139. #endif
  140. }
  141. }
  142. bool Window::get_flag(Flags p_flag) const {
  143. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  144. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  145. #ifdef TOOLS_ENABLED
  146. if ((p_flag != FLAG_POPUP) || !(Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this))) {
  147. flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
  148. }
  149. #else
  150. flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
  151. #endif
  152. }
  153. return flags[p_flag];
  154. }
  155. bool Window::is_maximize_allowed() const {
  156. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  157. return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
  158. }
  159. return true;
  160. }
  161. void Window::request_attention() {
  162. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  163. DisplayServer::get_singleton()->window_request_attention(window_id);
  164. }
  165. }
  166. void Window::move_to_foreground() {
  167. if (embedder) {
  168. embedder->_sub_window_grab_focus(this);
  169. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  170. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  171. }
  172. }
  173. bool Window::can_draw() const {
  174. if (!is_inside_tree()) {
  175. return false;
  176. }
  177. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  178. return DisplayServer::get_singleton()->window_can_draw(window_id);
  179. }
  180. return visible;
  181. }
  182. void Window::set_ime_active(bool p_active) {
  183. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  184. DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
  185. }
  186. }
  187. void Window::set_ime_position(const Point2i &p_pos) {
  188. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  189. DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
  190. }
  191. }
  192. bool Window::is_embedded() const {
  193. ERR_FAIL_COND_V(!is_inside_tree(), false);
  194. return _get_embedder() != nullptr;
  195. }
  196. void Window::_make_window() {
  197. ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
  198. uint32_t f = 0;
  199. for (int i = 0; i < FLAG_MAX; i++) {
  200. if (flags[i]) {
  201. f |= (1 << i);
  202. }
  203. }
  204. DisplayServer::VSyncMode vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
  205. window_id = DisplayServer::get_singleton()->create_sub_window(DisplayServer::WindowMode(mode), vsync_mode, f, Rect2i(position, size));
  206. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  207. DisplayServer::get_singleton()->window_set_current_screen(current_screen, window_id);
  208. DisplayServer::get_singleton()->window_set_max_size(Size2i(), window_id);
  209. DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
  210. String tr_title = atr(title);
  211. #ifdef DEBUG_ENABLED
  212. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  213. // Append a suffix to the window title to denote that the project is running
  214. // from a debug build (including the editor). Since this results in lower performance,
  215. // this should be clearly presented to the user.
  216. tr_title = vformat("%s (DEBUG)", tr_title);
  217. }
  218. #endif
  219. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  220. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  221. #ifdef TOOLS_ENABLED
  222. if (!(Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this))) {
  223. DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
  224. } else {
  225. DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
  226. }
  227. #else
  228. DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
  229. #endif
  230. _update_window_size();
  231. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  232. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  233. }
  234. for (const Window *E : transient_children) {
  235. if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
  236. DisplayServer::get_singleton()->window_set_transient(E->window_id, transient_parent->window_id);
  237. }
  238. }
  239. _update_window_callbacks();
  240. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  241. DisplayServer::get_singleton()->show_window(window_id);
  242. }
  243. void Window::_update_from_window() {
  244. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  245. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  246. for (int i = 0; i < FLAG_MAX; i++) {
  247. flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
  248. }
  249. }
  250. void Window::_clear_window() {
  251. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  252. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  253. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  254. }
  255. for (const Window *E : transient_children) {
  256. if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
  257. DisplayServer::get_singleton()->window_set_transient(E->window_id, DisplayServer::INVALID_WINDOW_ID);
  258. }
  259. }
  260. _update_from_window();
  261. DisplayServer::get_singleton()->delete_sub_window(window_id);
  262. window_id = DisplayServer::INVALID_WINDOW_ID;
  263. // If closing window was focused and has a parent, return focus.
  264. if (focused && transient_parent) {
  265. transient_parent->grab_focus();
  266. }
  267. _update_viewport_size();
  268. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  269. }
  270. void Window::_rect_changed_callback(const Rect2i &p_callback) {
  271. //we must always accept this as the truth
  272. if (size == p_callback.size && position == p_callback.position) {
  273. return;
  274. }
  275. position = p_callback.position;
  276. if (size != p_callback.size) {
  277. size = p_callback.size;
  278. _update_viewport_size();
  279. }
  280. }
  281. void Window::_propagate_window_notification(Node *p_node, int p_notification) {
  282. p_node->notification(p_notification);
  283. for (int i = 0; i < p_node->get_child_count(); i++) {
  284. Node *child = p_node->get_child(i);
  285. Window *window = Object::cast_to<Window>(child);
  286. if (window) {
  287. continue;
  288. }
  289. _propagate_window_notification(child, p_notification);
  290. }
  291. }
  292. void Window::_event_callback(DisplayServer::WindowEvent p_event) {
  293. switch (p_event) {
  294. case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
  295. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
  296. emit_signal(SNAME("mouse_entered"));
  297. notification(NOTIFICATION_VP_MOUSE_ENTER);
  298. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
  299. DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
  300. }
  301. } break;
  302. case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
  303. notification(NOTIFICATION_VP_MOUSE_EXIT);
  304. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
  305. emit_signal(SNAME("mouse_exited"));
  306. } break;
  307. case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
  308. focused = true;
  309. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
  310. emit_signal(SNAME("focus_entered"));
  311. } break;
  312. case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
  313. focused = false;
  314. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  315. emit_signal(SNAME("focus_exited"));
  316. } break;
  317. case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
  318. if (exclusive_child != nullptr) {
  319. break; //has an exclusive child, can't get events until child is closed
  320. }
  321. _propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
  322. emit_signal(SNAME("close_requested"));
  323. } break;
  324. case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
  325. _propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
  326. emit_signal(SNAME("go_back_requested"));
  327. } break;
  328. case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
  329. _update_viewport_size();
  330. _propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
  331. emit_signal(SNAME("dpi_changed"));
  332. } break;
  333. }
  334. }
  335. void Window::show() {
  336. set_visible(true);
  337. }
  338. void Window::hide() {
  339. set_visible(false);
  340. }
  341. void Window::set_visible(bool p_visible) {
  342. if (visible == p_visible) {
  343. return;
  344. }
  345. visible = p_visible;
  346. if (!is_inside_tree()) {
  347. return;
  348. }
  349. if (updating_child_controls) {
  350. _update_child_controls();
  351. }
  352. ERR_FAIL_COND_MSG(get_parent() == nullptr, "Can't change visibility of main window.");
  353. Viewport *embedder_vp = _get_embedder();
  354. if (!embedder_vp) {
  355. if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
  356. _clear_window();
  357. }
  358. if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
  359. _make_window();
  360. }
  361. } else {
  362. if (visible) {
  363. embedder = embedder_vp;
  364. embedder->_sub_window_register(this);
  365. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  366. } else {
  367. embedder->_sub_window_remove(this);
  368. embedder = nullptr;
  369. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  370. }
  371. _update_window_size();
  372. }
  373. if (!visible) {
  374. focused = false;
  375. }
  376. notification(NOTIFICATION_VISIBILITY_CHANGED);
  377. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  378. RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
  379. //update transient exclusive
  380. if (transient_parent) {
  381. if (exclusive && visible) {
  382. #ifdef TOOLS_ENABLED
  383. if (!(Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this))) {
  384. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  385. transient_parent->exclusive_child = this;
  386. }
  387. #else
  388. transient_parent->exclusive_child = this;
  389. #endif
  390. } else {
  391. if (transient_parent->exclusive_child == this) {
  392. transient_parent->exclusive_child = nullptr;
  393. }
  394. }
  395. }
  396. }
  397. void Window::_clear_transient() {
  398. if (transient_parent) {
  399. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  400. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  401. }
  402. transient_parent->transient_children.erase(this);
  403. if (transient_parent->exclusive_child == this) {
  404. transient_parent->exclusive_child = nullptr;
  405. }
  406. transient_parent = nullptr;
  407. }
  408. }
  409. void Window::_make_transient() {
  410. if (!get_parent()) {
  411. //main window, can't be transient
  412. return;
  413. }
  414. //find transient parent
  415. Viewport *vp = get_parent()->get_viewport();
  416. Window *window = nullptr;
  417. while (vp) {
  418. window = Object::cast_to<Window>(vp);
  419. if (window) {
  420. break;
  421. }
  422. if (!vp->get_parent()) {
  423. break;
  424. }
  425. vp = vp->get_parent()->get_viewport();
  426. }
  427. if (window) {
  428. transient_parent = window;
  429. window->transient_children.insert(this);
  430. if (is_inside_tree() && is_visible() && exclusive) {
  431. if (transient_parent->exclusive_child == nullptr) {
  432. #ifdef TOOLS_ENABLED
  433. if (!(Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this))) {
  434. transient_parent->exclusive_child = this;
  435. }
  436. #else
  437. transient_parent->exclusive_child = this;
  438. #endif
  439. } else if (transient_parent->exclusive_child != this) {
  440. ERR_PRINT("Making child transient exclusive, but parent has another exclusive child");
  441. }
  442. }
  443. }
  444. //see if we can make transient
  445. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  446. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  447. }
  448. }
  449. void Window::set_transient(bool p_transient) {
  450. if (transient == p_transient) {
  451. return;
  452. }
  453. transient = p_transient;
  454. if (!is_inside_tree()) {
  455. return;
  456. }
  457. if (transient) {
  458. _make_transient();
  459. } else {
  460. _clear_transient();
  461. }
  462. }
  463. bool Window::is_transient() const {
  464. return transient;
  465. }
  466. void Window::set_exclusive(bool p_exclusive) {
  467. if (exclusive == p_exclusive) {
  468. return;
  469. }
  470. exclusive = p_exclusive;
  471. if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
  472. #ifdef TOOLS_ENABLED
  473. if (!(Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this))) {
  474. DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
  475. } else {
  476. DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
  477. }
  478. #else
  479. DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
  480. #endif
  481. }
  482. if (transient_parent) {
  483. if (p_exclusive && is_inside_tree() && is_visible()) {
  484. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  485. #ifdef TOOLS_ENABLED
  486. if (!(Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this))) {
  487. transient_parent->exclusive_child = this;
  488. }
  489. #else
  490. transient_parent->exclusive_child = this;
  491. #endif
  492. } else {
  493. if (transient_parent->exclusive_child == this) {
  494. transient_parent->exclusive_child = nullptr;
  495. }
  496. }
  497. }
  498. }
  499. bool Window::is_exclusive() const {
  500. return exclusive;
  501. }
  502. bool Window::is_visible() const {
  503. return visible;
  504. }
  505. void Window::_update_window_size() {
  506. Size2i size_limit;
  507. if (wrap_controls) {
  508. size_limit = get_contents_minimum_size();
  509. }
  510. size_limit.x = MAX(size_limit.x, min_size.x);
  511. size_limit.y = MAX(size_limit.y, min_size.y);
  512. size.x = MAX(size_limit.x, size.x);
  513. size.y = MAX(size_limit.y, size.y);
  514. bool reset_min_first = false;
  515. bool max_size_valid = false;
  516. if ((max_size.x > 0 || max_size.y > 0) && (max_size.x >= min_size.x && max_size.y >= min_size.y)) {
  517. max_size_valid = true;
  518. if (size.x > max_size.x) {
  519. size.x = max_size.x;
  520. }
  521. if (size_limit.x > max_size.x) {
  522. size_limit.x = max_size.x;
  523. reset_min_first = true;
  524. }
  525. if (size.y > max_size.y) {
  526. size.y = max_size.y;
  527. }
  528. if (size_limit.y > max_size.y) {
  529. size_limit.y = max_size.y;
  530. reset_min_first = true;
  531. }
  532. }
  533. if (embedder) {
  534. size.x = MAX(size.x, 1);
  535. size.y = MAX(size.y, 1);
  536. embedder->_sub_window_update(this);
  537. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  538. if (reset_min_first && wrap_controls) {
  539. // Avoid an error if setting max_size to a value between min_size and the previous size_limit.
  540. DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
  541. }
  542. DisplayServer::get_singleton()->window_set_size(size, window_id);
  543. DisplayServer::get_singleton()->window_set_max_size(max_size_valid ? max_size : Size2i(), window_id);
  544. DisplayServer::get_singleton()->window_set_min_size(size_limit, window_id);
  545. }
  546. //update the viewport
  547. _update_viewport_size();
  548. }
  549. void Window::_update_viewport_size() {
  550. //update the viewport part
  551. Size2i final_size;
  552. Size2i final_size_override;
  553. Rect2i attach_to_screen_rect(Point2i(), size);
  554. Transform2D stretch_transform;
  555. float font_oversampling = 1.0;
  556. if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
  557. font_oversampling = content_scale_factor;
  558. final_size = size;
  559. final_size_override = Size2(size) / content_scale_factor;
  560. stretch_transform = Transform2D();
  561. stretch_transform.scale(Size2(content_scale_factor, content_scale_factor));
  562. } else {
  563. //actual screen video mode
  564. Size2 video_mode = size;
  565. Size2 desired_res = content_scale_size;
  566. Size2 viewport_size;
  567. Size2 screen_size;
  568. float viewport_aspect = desired_res.aspect();
  569. float video_mode_aspect = video_mode.aspect();
  570. if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  571. //same aspect or ignore aspect
  572. viewport_size = desired_res;
  573. screen_size = video_mode;
  574. } else if (viewport_aspect < video_mode_aspect) {
  575. // screen ratio is smaller vertically
  576. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  577. //will stretch horizontally
  578. viewport_size.x = desired_res.y * video_mode_aspect;
  579. viewport_size.y = desired_res.y;
  580. screen_size = video_mode;
  581. } else {
  582. //will need black bars
  583. viewport_size = desired_res;
  584. screen_size.x = video_mode.y * viewport_aspect;
  585. screen_size.y = video_mode.y;
  586. }
  587. } else {
  588. //screen ratio is smaller horizontally
  589. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  590. //will stretch horizontally
  591. viewport_size.x = desired_res.x;
  592. viewport_size.y = desired_res.x / video_mode_aspect;
  593. screen_size = video_mode;
  594. } else {
  595. //will need black bars
  596. viewport_size = desired_res;
  597. screen_size.x = video_mode.x;
  598. screen_size.y = video_mode.x / viewport_aspect;
  599. }
  600. }
  601. screen_size = screen_size.floor();
  602. viewport_size = viewport_size.floor();
  603. Size2 margin;
  604. Size2 offset;
  605. //black bars and margin
  606. if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.x < video_mode.x) {
  607. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  608. //RenderingServer::get_singleton()->black_bars_set_margins(margin.x, 0, margin.x, 0);
  609. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  610. } else if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.y < video_mode.y) {
  611. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  612. //RenderingServer::get_singleton()->black_bars_set_margins(0, margin.y, 0, margin.y);
  613. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  614. } else {
  615. //RenderingServer::get_singleton()->black_bars_set_margins(0, 0, 0, 0);
  616. }
  617. switch (content_scale_mode) {
  618. case CONTENT_SCALE_MODE_DISABLED: {
  619. // Already handled above
  620. //_update_font_oversampling(1.0);
  621. } break;
  622. case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
  623. final_size = screen_size;
  624. final_size_override = viewport_size / content_scale_factor;
  625. attach_to_screen_rect = Rect2(margin, screen_size);
  626. font_oversampling = (screen_size.x / viewport_size.x) * content_scale_factor;
  627. Size2 scale = Vector2(screen_size) / Vector2(final_size_override);
  628. stretch_transform.scale(scale);
  629. } break;
  630. case CONTENT_SCALE_MODE_VIEWPORT: {
  631. final_size = (viewport_size / content_scale_factor).floor();
  632. attach_to_screen_rect = Rect2(margin, screen_size);
  633. } break;
  634. }
  635. }
  636. bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
  637. _set_size(final_size, final_size_override, attach_to_screen_rect, stretch_transform, allocate);
  638. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  639. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
  640. } else {
  641. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
  642. }
  643. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  644. if (!use_font_oversampling) {
  645. font_oversampling = 1.0;
  646. }
  647. if (TS->font_get_global_oversampling() != font_oversampling) {
  648. TS->font_set_global_oversampling(font_oversampling);
  649. }
  650. }
  651. notification(NOTIFICATION_WM_SIZE_CHANGED);
  652. if (embedder) {
  653. embedder->_sub_window_update(this);
  654. }
  655. }
  656. void Window::_update_window_callbacks() {
  657. DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
  658. DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
  659. DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
  660. DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
  661. DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
  662. }
  663. Viewport *Window::_get_embedder() const {
  664. Viewport *vp = get_parent_viewport();
  665. while (vp) {
  666. if (vp->is_embedding_subwindows()) {
  667. return vp;
  668. }
  669. if (vp->get_parent()) {
  670. vp = vp->get_parent()->get_viewport();
  671. } else {
  672. vp = nullptr;
  673. }
  674. }
  675. return nullptr;
  676. }
  677. void Window::_notification(int p_what) {
  678. switch (p_what) {
  679. case NOTIFICATION_ENTER_TREE: {
  680. bool embedded = false;
  681. {
  682. embedder = _get_embedder();
  683. if (embedder) {
  684. embedded = true;
  685. if (!visible) {
  686. embedder = nullptr; // Not yet since not visible.
  687. }
  688. }
  689. }
  690. if (embedded) {
  691. // Create as embedded.
  692. if (embedder) {
  693. embedder->_sub_window_register(this);
  694. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  695. _update_window_size();
  696. }
  697. } else {
  698. if (!get_parent()) {
  699. // It's the root window!
  700. visible = true; // Always visible.
  701. window_id = DisplayServer::MAIN_WINDOW_ID;
  702. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  703. _update_from_window();
  704. // Since this window already exists (created on start), we must update pos and size from it.
  705. {
  706. position = DisplayServer::get_singleton()->window_get_position(window_id);
  707. size = DisplayServer::get_singleton()->window_get_size(window_id);
  708. }
  709. _update_viewport_size(); // Then feed back to the viewport.
  710. _update_window_callbacks();
  711. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  712. } else {
  713. // Create.
  714. if (visible) {
  715. _make_window();
  716. }
  717. }
  718. }
  719. if (transient) {
  720. _make_transient();
  721. }
  722. if (visible) {
  723. notification(NOTIFICATION_VISIBILITY_CHANGED);
  724. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  725. RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
  726. }
  727. // Need to defer here, because theme owner information might be set in
  728. // add_child_notify, which doesn't get called until right after this.
  729. call_deferred(SNAME("notification"), NOTIFICATION_THEME_CHANGED);
  730. } break;
  731. case NOTIFICATION_THEME_CHANGED: {
  732. emit_signal(SceneStringNames::get_singleton()->theme_changed);
  733. } break;
  734. case NOTIFICATION_READY: {
  735. if (wrap_controls) {
  736. _update_child_controls();
  737. }
  738. } break;
  739. case NOTIFICATION_TRANSLATION_CHANGED: {
  740. if (embedder) {
  741. embedder->_sub_window_update(this);
  742. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  743. String tr_title = atr(title);
  744. #ifdef DEBUG_ENABLED
  745. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  746. // Append a suffix to the window title to denote that the project is running
  747. // from a debug build (including the editor). Since this results in lower performance,
  748. // this should be clearly presented to the user.
  749. tr_title = vformat("%s (DEBUG)", tr_title);
  750. }
  751. #endif
  752. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  753. }
  754. child_controls_changed();
  755. } break;
  756. case NOTIFICATION_EXIT_TREE: {
  757. if (transient) {
  758. _clear_transient();
  759. }
  760. if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
  761. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  762. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  763. _update_window_callbacks();
  764. } else {
  765. _clear_window();
  766. }
  767. } else {
  768. if (embedder) {
  769. embedder->_sub_window_remove(this);
  770. embedder = nullptr;
  771. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  772. }
  773. _update_viewport_size(); //called by clear and make, which does not happen here
  774. }
  775. RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
  776. } break;
  777. }
  778. }
  779. void Window::set_content_scale_size(const Size2i &p_size) {
  780. ERR_FAIL_COND(p_size.x < 0);
  781. ERR_FAIL_COND(p_size.y < 0);
  782. content_scale_size = p_size;
  783. _update_viewport_size();
  784. }
  785. Size2i Window::get_content_scale_size() const {
  786. return content_scale_size;
  787. }
  788. void Window::set_content_scale_mode(ContentScaleMode p_mode) {
  789. content_scale_mode = p_mode;
  790. _update_viewport_size();
  791. }
  792. Window::ContentScaleMode Window::get_content_scale_mode() const {
  793. return content_scale_mode;
  794. }
  795. void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
  796. content_scale_aspect = p_aspect;
  797. _update_viewport_size();
  798. }
  799. Window::ContentScaleAspect Window::get_content_scale_aspect() const {
  800. return content_scale_aspect;
  801. }
  802. void Window::set_content_scale_factor(real_t p_factor) {
  803. ERR_FAIL_COND(p_factor <= 0);
  804. content_scale_factor = p_factor;
  805. _update_viewport_size();
  806. }
  807. real_t Window::get_content_scale_factor() const {
  808. return content_scale_factor;
  809. }
  810. void Window::set_use_font_oversampling(bool p_oversampling) {
  811. if (is_inside_tree() && window_id != DisplayServer::MAIN_WINDOW_ID) {
  812. ERR_FAIL_MSG("Only the root window can set and use font oversampling.");
  813. }
  814. use_font_oversampling = p_oversampling;
  815. _update_viewport_size();
  816. }
  817. bool Window::is_using_font_oversampling() const {
  818. return use_font_oversampling;
  819. }
  820. DisplayServer::WindowID Window::get_window_id() const {
  821. if (embedder) {
  822. return parent->get_window_id();
  823. }
  824. return window_id;
  825. }
  826. void Window::warp_mouse(const Vector2 &p_position) {
  827. Transform2D xform = get_screen_transform();
  828. Vector2 gpos = xform.xform(p_position);
  829. if (transient_parent && !transient_parent->is_embedding_subwindows()) {
  830. Transform2D window_trans = Transform2D().translated(get_position() + (transient_parent->get_visible_rect().size - transient_parent->get_real_size()));
  831. gpos = window_trans.xform(gpos);
  832. }
  833. Input::get_singleton()->warp_mouse(gpos);
  834. }
  835. void Window::set_wrap_controls(bool p_enable) {
  836. wrap_controls = p_enable;
  837. if (wrap_controls) {
  838. child_controls_changed();
  839. } else {
  840. _update_window_size();
  841. }
  842. }
  843. bool Window::is_wrapping_controls() const {
  844. return wrap_controls;
  845. }
  846. Size2 Window::_get_contents_minimum_size() const {
  847. Size2 max;
  848. for (int i = 0; i < get_child_count(); i++) {
  849. Control *c = Object::cast_to<Control>(get_child(i));
  850. if (c) {
  851. Point2i pos = c->get_position();
  852. Size2i min = c->get_combined_minimum_size();
  853. max.x = MAX(pos.x + min.x, max.x);
  854. max.y = MAX(pos.y + min.y, max.y);
  855. }
  856. }
  857. return max;
  858. }
  859. void Window::_update_child_controls() {
  860. if (!updating_child_controls) {
  861. return;
  862. }
  863. _update_window_size();
  864. updating_child_controls = false;
  865. }
  866. void Window::child_controls_changed() {
  867. if (!is_inside_tree() || !visible || updating_child_controls) {
  868. return;
  869. }
  870. updating_child_controls = true;
  871. call_deferred(SNAME("_update_child_controls"));
  872. }
  873. bool Window::_can_consume_input_events() const {
  874. return exclusive_child == nullptr;
  875. }
  876. void Window::_window_input(const Ref<InputEvent> &p_ev) {
  877. if (EngineDebugger::is_active()) {
  878. // Quit from game window using the stop shortcut (F8 by default).
  879. // The custom shortcut is provided via environment variable when running from the editor.
  880. if (debugger_stop_shortcut.is_null()) {
  881. String shortcut_str = OS::get_singleton()->get_environment("__GODOT_EDITOR_STOP_SHORTCUT__");
  882. if (!shortcut_str.is_empty()) {
  883. Variant shortcut_var;
  884. VariantParser::StreamString ss;
  885. ss.s = shortcut_str;
  886. String errs;
  887. int line;
  888. VariantParser::parse(&ss, shortcut_var, errs, line);
  889. debugger_stop_shortcut = shortcut_var;
  890. }
  891. if (debugger_stop_shortcut.is_null()) {
  892. // Define a default shortcut if it wasn't provided or is invalid.
  893. debugger_stop_shortcut.instantiate();
  894. debugger_stop_shortcut->set_events({ (Variant)InputEventKey::create_reference(Key::F8) });
  895. }
  896. }
  897. Ref<InputEventKey> k = p_ev;
  898. if (k.is_valid() && k->is_pressed() && !k->is_echo() && debugger_stop_shortcut->matches_event(k)) {
  899. EngineDebugger::get_singleton()->send_message("request_quit", Array());
  900. }
  901. }
  902. if (exclusive_child != nullptr) {
  903. if (!is_embedding_subwindows()) { // Not embedding, no need for event.
  904. return;
  905. }
  906. }
  907. emit_signal(SceneStringNames::get_singleton()->window_input, p_ev);
  908. push_input(p_ev);
  909. if (!is_input_handled()) {
  910. push_unhandled_input(p_ev);
  911. }
  912. }
  913. void Window::_window_input_text(const String &p_text) {
  914. push_text_input(p_text);
  915. }
  916. void Window::_window_drop_files(const Vector<String> &p_files) {
  917. emit_signal(SNAME("files_dropped"), p_files);
  918. }
  919. Viewport *Window::get_parent_viewport() const {
  920. if (get_parent()) {
  921. return get_parent()->get_viewport();
  922. } else {
  923. return nullptr;
  924. }
  925. }
  926. Window *Window::get_parent_visible_window() const {
  927. Viewport *vp = get_parent_viewport();
  928. Window *window = nullptr;
  929. while (vp) {
  930. window = Object::cast_to<Window>(vp);
  931. if (window && window->visible) {
  932. break;
  933. }
  934. if (!vp->get_parent()) {
  935. break;
  936. }
  937. vp = vp->get_parent()->get_viewport();
  938. }
  939. return window;
  940. }
  941. void Window::popup_on_parent(const Rect2i &p_parent_rect) {
  942. ERR_FAIL_COND(!is_inside_tree());
  943. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  944. if (!is_embedded()) {
  945. Window *window = get_parent_visible_window();
  946. if (!window) {
  947. popup(p_parent_rect);
  948. } else {
  949. popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
  950. }
  951. } else {
  952. popup(p_parent_rect);
  953. }
  954. }
  955. void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
  956. ERR_FAIL_COND(!is_inside_tree());
  957. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  958. Rect2 parent_rect;
  959. if (is_embedded()) {
  960. parent_rect = get_parent_viewport()->get_visible_rect();
  961. } else {
  962. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  963. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  964. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  965. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  966. }
  967. Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
  968. Rect2i popup_rect;
  969. popup_rect.size = Vector2i(MIN(size_ratio.x, p_size.x), MIN(size_ratio.y, p_size.y));
  970. if (parent_rect != Rect2()) {
  971. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  972. }
  973. popup(popup_rect);
  974. }
  975. void Window::popup_centered(const Size2i &p_minsize) {
  976. ERR_FAIL_COND(!is_inside_tree());
  977. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  978. Rect2 parent_rect;
  979. if (is_embedded()) {
  980. parent_rect = get_parent_viewport()->get_visible_rect();
  981. } else {
  982. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  983. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  984. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  985. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  986. }
  987. Rect2i popup_rect;
  988. Size2 contents_minsize = _get_contents_minimum_size();
  989. popup_rect.size.x = MAX(p_minsize.x, contents_minsize.x);
  990. popup_rect.size.y = MAX(p_minsize.y, contents_minsize.y);
  991. if (parent_rect != Rect2()) {
  992. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  993. }
  994. popup(popup_rect);
  995. }
  996. void Window::popup_centered_ratio(float p_ratio) {
  997. ERR_FAIL_COND(!is_inside_tree());
  998. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  999. ERR_FAIL_COND_MSG(p_ratio <= 0.0 || p_ratio > 1.0, "Ratio must be between 0.0 and 1.0!");
  1000. Rect2 parent_rect;
  1001. if (is_embedded()) {
  1002. parent_rect = get_parent_viewport()->get_visible_rect();
  1003. } else {
  1004. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  1005. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  1006. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  1007. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  1008. }
  1009. Rect2i popup_rect;
  1010. if (parent_rect != Rect2()) {
  1011. popup_rect.size = parent_rect.size * p_ratio;
  1012. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  1013. }
  1014. popup(popup_rect);
  1015. }
  1016. void Window::popup(const Rect2i &p_screen_rect) {
  1017. emit_signal(SNAME("about_to_popup"));
  1018. if (!_get_embedder() && get_flag(FLAG_POPUP)) {
  1019. // Send a focus-out notification when opening a Window Manager Popup.
  1020. SceneTree *scene_tree = get_tree();
  1021. if (scene_tree) {
  1022. scene_tree->notify_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_viewports", NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  1023. }
  1024. }
  1025. // Update window size to calculate the actual window size based on contents minimum size and minimum size.
  1026. _update_window_size();
  1027. if (p_screen_rect != Rect2i()) {
  1028. set_position(p_screen_rect.position);
  1029. set_size(p_screen_rect.size);
  1030. }
  1031. Rect2i adjust = _popup_adjust_rect();
  1032. if (adjust != Rect2i()) {
  1033. set_position(adjust.position);
  1034. set_size(adjust.size);
  1035. }
  1036. int scr = DisplayServer::get_singleton()->get_screen_count();
  1037. for (int i = 0; i < scr; i++) {
  1038. Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
  1039. if (r.has_point(position)) {
  1040. current_screen = i;
  1041. break;
  1042. }
  1043. }
  1044. set_transient(true);
  1045. set_visible(true);
  1046. _post_popup();
  1047. notification(NOTIFICATION_POST_POPUP);
  1048. }
  1049. Size2 Window::get_contents_minimum_size() const {
  1050. return _get_contents_minimum_size();
  1051. }
  1052. void Window::grab_focus() {
  1053. if (embedder) {
  1054. embedder->_sub_window_grab_focus(this);
  1055. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1056. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  1057. }
  1058. }
  1059. bool Window::has_focus() const {
  1060. return focused;
  1061. }
  1062. Rect2i Window::get_usable_parent_rect() const {
  1063. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  1064. Rect2i parent;
  1065. if (is_embedded()) {
  1066. parent = _get_embedder()->get_visible_rect();
  1067. } else {
  1068. const Window *w = is_visible() ? this : get_parent_visible_window();
  1069. //find a parent that can contain us
  1070. ERR_FAIL_COND_V(!w, Rect2());
  1071. parent = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
  1072. }
  1073. return parent;
  1074. }
  1075. void Window::add_child_notify(Node *p_child) {
  1076. // We propagate when this node uses a custom theme, so it can pass it on to its children.
  1077. if (theme_owner || theme_owner_window) {
  1078. // `p_notify` is false here as `NOTIFICATION_THEME_CHANGED` will be handled by `NOTIFICATION_ENTER_TREE`.
  1079. Control::_propagate_theme_changed(this, theme_owner, theme_owner_window, false, true);
  1080. }
  1081. if (is_inside_tree() && wrap_controls) {
  1082. child_controls_changed();
  1083. }
  1084. }
  1085. void Window::remove_child_notify(Node *p_child) {
  1086. // If the removed child isn't inheriting any theme items through this node, then there's no need to propagate.
  1087. if (theme_owner || theme_owner_window) {
  1088. Control::_propagate_theme_changed(this, nullptr, nullptr, false, true);
  1089. }
  1090. if (is_inside_tree() && wrap_controls) {
  1091. child_controls_changed();
  1092. }
  1093. }
  1094. void Window::set_theme(const Ref<Theme> &p_theme) {
  1095. if (theme == p_theme) {
  1096. return;
  1097. }
  1098. if (theme.is_valid()) {
  1099. theme->disconnect("changed", callable_mp(this, &Window::_theme_changed));
  1100. }
  1101. theme = p_theme;
  1102. if (theme.is_valid()) {
  1103. Control::_propagate_theme_changed(this, nullptr, this, is_inside_tree(), true);
  1104. theme->connect("changed", callable_mp(this, &Window::_theme_changed), CONNECT_DEFERRED);
  1105. return;
  1106. }
  1107. Control *parent_c = Object::cast_to<Control>(get_parent());
  1108. if (parent_c && (parent_c->data.theme_owner || parent_c->data.theme_owner_window)) {
  1109. Control::_propagate_theme_changed(this, parent_c->data.theme_owner, parent_c->data.theme_owner_window, is_inside_tree(), true);
  1110. return;
  1111. }
  1112. Window *parent_w = cast_to<Window>(get_parent());
  1113. if (parent_w && (parent_w->theme_owner || parent_w->theme_owner_window)) {
  1114. Control::_propagate_theme_changed(this, parent_w->theme_owner, parent_w->theme_owner_window, is_inside_tree(), true);
  1115. return;
  1116. }
  1117. Control::_propagate_theme_changed(this, nullptr, nullptr, is_inside_tree(), true);
  1118. }
  1119. Ref<Theme> Window::get_theme() const {
  1120. return theme;
  1121. }
  1122. void Window::_theme_changed() {
  1123. if (is_inside_tree()) {
  1124. Control::_propagate_theme_changed(this, nullptr, this, true, false);
  1125. }
  1126. }
  1127. void Window::set_theme_type_variation(const StringName &p_theme_type) {
  1128. theme_type_variation = p_theme_type;
  1129. if (is_inside_tree()) {
  1130. notification(NOTIFICATION_THEME_CHANGED);
  1131. }
  1132. }
  1133. StringName Window::get_theme_type_variation() const {
  1134. return theme_type_variation;
  1135. }
  1136. void Window::_get_theme_type_dependencies(const StringName &p_theme_type, List<StringName> *p_list) const {
  1137. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1138. if (ThemeDB::get_singleton()->get_project_theme().is_valid() && ThemeDB::get_singleton()->get_project_theme()->get_type_variation_base(theme_type_variation) != StringName()) {
  1139. ThemeDB::get_singleton()->get_project_theme()->get_type_dependencies(get_class_name(), theme_type_variation, p_list);
  1140. } else {
  1141. ThemeDB::get_singleton()->get_default_theme()->get_type_dependencies(get_class_name(), theme_type_variation, p_list);
  1142. }
  1143. } else {
  1144. ThemeDB::get_singleton()->get_default_theme()->get_type_dependencies(p_theme_type, StringName(), p_list);
  1145. }
  1146. }
  1147. Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  1148. List<StringName> theme_types;
  1149. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1150. return Control::get_theme_item_in_types<Ref<Texture2D>>(theme_owner, theme_owner_window, Theme::DATA_TYPE_ICON, p_name, theme_types);
  1151. }
  1152. Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  1153. List<StringName> theme_types;
  1154. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1155. return Control::get_theme_item_in_types<Ref<StyleBox>>(theme_owner, theme_owner_window, Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  1156. }
  1157. Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  1158. List<StringName> theme_types;
  1159. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1160. return Control::get_theme_item_in_types<Ref<Font>>(theme_owner, theme_owner_window, Theme::DATA_TYPE_FONT, p_name, theme_types);
  1161. }
  1162. int Window::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  1163. List<StringName> theme_types;
  1164. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1165. return Control::get_theme_item_in_types<int>(theme_owner, theme_owner_window, Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  1166. }
  1167. Color Window::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  1168. List<StringName> theme_types;
  1169. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1170. return Control::get_theme_item_in_types<Color>(theme_owner, theme_owner_window, Theme::DATA_TYPE_COLOR, p_name, theme_types);
  1171. }
  1172. int Window::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  1173. List<StringName> theme_types;
  1174. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1175. return Control::get_theme_item_in_types<int>(theme_owner, theme_owner_window, Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  1176. }
  1177. bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  1178. List<StringName> theme_types;
  1179. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1180. return Control::has_theme_item_in_types(theme_owner, theme_owner_window, Theme::DATA_TYPE_ICON, p_name, theme_types);
  1181. }
  1182. bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  1183. List<StringName> theme_types;
  1184. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1185. return Control::has_theme_item_in_types(theme_owner, theme_owner_window, Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  1186. }
  1187. bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  1188. List<StringName> theme_types;
  1189. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1190. return Control::has_theme_item_in_types(theme_owner, theme_owner_window, Theme::DATA_TYPE_FONT, p_name, theme_types);
  1191. }
  1192. bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  1193. List<StringName> theme_types;
  1194. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1195. return Control::has_theme_item_in_types(theme_owner, theme_owner_window, Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  1196. }
  1197. bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  1198. List<StringName> theme_types;
  1199. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1200. return Control::has_theme_item_in_types(theme_owner, theme_owner_window, Theme::DATA_TYPE_COLOR, p_name, theme_types);
  1201. }
  1202. bool Window::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  1203. List<StringName> theme_types;
  1204. _get_theme_type_dependencies(p_theme_type, &theme_types);
  1205. return Control::has_theme_item_in_types(theme_owner, theme_owner_window, Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  1206. }
  1207. float Window::get_theme_default_base_scale() const {
  1208. return Control::fetch_theme_default_base_scale(theme_owner, theme_owner_window);
  1209. }
  1210. Ref<Font> Window::get_theme_default_font() const {
  1211. return Control::fetch_theme_default_font(theme_owner, theme_owner_window);
  1212. }
  1213. int Window::get_theme_default_font_size() const {
  1214. return Control::fetch_theme_default_font_size(theme_owner, theme_owner_window);
  1215. }
  1216. Rect2i Window::get_parent_rect() const {
  1217. ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
  1218. if (is_embedded()) {
  1219. //viewport
  1220. Node *n = get_parent();
  1221. ERR_FAIL_COND_V(!n, Rect2i());
  1222. Viewport *p = n->get_viewport();
  1223. ERR_FAIL_COND_V(!p, Rect2i());
  1224. return p->get_visible_rect();
  1225. } else {
  1226. int x = get_position().x;
  1227. int closest_dist = 0x7FFFFFFF;
  1228. Rect2i closest_rect;
  1229. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  1230. Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
  1231. int d;
  1232. if (x >= s.position.x && x < s.size.x) {
  1233. //contained
  1234. closest_rect = s;
  1235. break;
  1236. } else if (x < s.position.x) {
  1237. d = s.position.x - x;
  1238. } else {
  1239. d = x - (s.position.x + s.size.x);
  1240. }
  1241. if (d < closest_dist) {
  1242. closest_dist = d;
  1243. closest_rect = s;
  1244. }
  1245. }
  1246. return closest_rect;
  1247. }
  1248. }
  1249. void Window::set_clamp_to_embedder(bool p_enable) {
  1250. clamp_to_embedder = p_enable;
  1251. }
  1252. bool Window::is_clamped_to_embedder() const {
  1253. return clamp_to_embedder;
  1254. }
  1255. void Window::set_layout_direction(Window::LayoutDirection p_direction) {
  1256. ERR_FAIL_INDEX((int)p_direction, 4);
  1257. layout_dir = p_direction;
  1258. propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  1259. }
  1260. Window::LayoutDirection Window::get_layout_direction() const {
  1261. return layout_dir;
  1262. }
  1263. bool Window::is_layout_rtl() const {
  1264. if (layout_dir == LAYOUT_DIRECTION_INHERITED) {
  1265. Window *parent = Object::cast_to<Window>(get_parent());
  1266. if (parent) {
  1267. return parent->is_layout_rtl();
  1268. } else {
  1269. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  1270. return true;
  1271. }
  1272. String locale = TranslationServer::get_singleton()->get_tool_locale();
  1273. return TS->is_locale_right_to_left(locale);
  1274. }
  1275. } else if (layout_dir == LAYOUT_DIRECTION_LOCALE) {
  1276. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  1277. return true;
  1278. }
  1279. String locale = TranslationServer::get_singleton()->get_tool_locale();
  1280. return TS->is_locale_right_to_left(locale);
  1281. } else {
  1282. return (layout_dir == LAYOUT_DIRECTION_RTL);
  1283. }
  1284. }
  1285. void Window::set_auto_translate(bool p_enable) {
  1286. if (p_enable == auto_translate) {
  1287. return;
  1288. }
  1289. auto_translate = p_enable;
  1290. notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  1291. }
  1292. bool Window::is_auto_translating() const {
  1293. return auto_translate;
  1294. }
  1295. void Window::_validate_property(PropertyInfo &p_property) const {
  1296. if (p_property.name == "theme_type_variation") {
  1297. List<StringName> names;
  1298. // Only the default theme and the project theme are used for the list of options.
  1299. // This is an imposed limitation to simplify the logic needed to leverage those options.
  1300. ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
  1301. if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
  1302. ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
  1303. }
  1304. names.sort_custom<StringName::AlphCompare>();
  1305. Vector<StringName> unique_names;
  1306. String hint_string;
  1307. for (const StringName &E : names) {
  1308. // Skip duplicate values.
  1309. if (unique_names.has(E)) {
  1310. continue;
  1311. }
  1312. hint_string += String(E) + ",";
  1313. unique_names.append(E);
  1314. }
  1315. p_property.hint_string = hint_string;
  1316. }
  1317. }
  1318. Transform2D Window::get_screen_transform() const {
  1319. Transform2D embedder_transform = Transform2D();
  1320. if (_get_embedder()) {
  1321. embedder_transform.translate_local(get_position());
  1322. embedder_transform = _get_embedder()->get_screen_transform() * embedder_transform;
  1323. }
  1324. return embedder_transform * Viewport::get_screen_transform();
  1325. }
  1326. void Window::_bind_methods() {
  1327. ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
  1328. ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
  1329. ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
  1330. ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
  1331. ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
  1332. ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
  1333. ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
  1334. ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
  1335. ClassDB::bind_method(D_METHOD("reset_size"), &Window::reset_size);
  1336. ClassDB::bind_method(D_METHOD("get_real_size"), &Window::get_real_size);
  1337. ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
  1338. ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
  1339. ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
  1340. ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
  1341. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
  1342. ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
  1343. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
  1344. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
  1345. ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
  1346. ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
  1347. ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
  1348. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
  1349. ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
  1350. ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
  1351. ClassDB::bind_method(D_METHOD("show"), &Window::show);
  1352. ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
  1353. ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
  1354. ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
  1355. ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
  1356. ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
  1357. ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
  1358. ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
  1359. ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);
  1360. ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);
  1361. ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
  1362. ClassDB::bind_method(D_METHOD("get_contents_minimum_size"), &Window::get_contents_minimum_size);
  1363. ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
  1364. ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
  1365. ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
  1366. ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
  1367. ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
  1368. ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
  1369. ClassDB::bind_method(D_METHOD("set_content_scale_factor", "factor"), &Window::set_content_scale_factor);
  1370. ClassDB::bind_method(D_METHOD("get_content_scale_factor"), &Window::get_content_scale_factor);
  1371. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
  1372. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
  1373. ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
  1374. ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
  1375. ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
  1376. ClassDB::bind_method(D_METHOD("_update_child_controls"), &Window::_update_child_controls);
  1377. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
  1378. ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
  1379. ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Window::set_theme_type_variation);
  1380. ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Window::get_theme_type_variation);
  1381. ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Window::get_theme_icon, DEFVAL(""));
  1382. ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Window::get_theme_stylebox, DEFVAL(""));
  1383. ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Window::get_theme_font, DEFVAL(""));
  1384. ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Window::get_theme_font_size, DEFVAL(""));
  1385. ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Window::get_theme_color, DEFVAL(""));
  1386. ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Window::get_theme_constant, DEFVAL(""));
  1387. ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Window::has_theme_icon, DEFVAL(""));
  1388. ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Window::has_theme_stylebox, DEFVAL(""));
  1389. ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Window::has_theme_font, DEFVAL(""));
  1390. ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Window::has_theme_font_size, DEFVAL(""));
  1391. ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Window::has_theme_color, DEFVAL(""));
  1392. ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Window::has_theme_constant, DEFVAL(""));
  1393. ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Window::get_theme_default_base_scale);
  1394. ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Window::get_theme_default_font);
  1395. ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Window::get_theme_default_font_size);
  1396. ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Window::set_layout_direction);
  1397. ClassDB::bind_method(D_METHOD("get_layout_direction"), &Window::get_layout_direction);
  1398. ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Window::is_layout_rtl);
  1399. ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Window::set_auto_translate);
  1400. ClassDB::bind_method(D_METHOD("is_auto_translating"), &Window::is_auto_translating);
  1401. ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
  1402. ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
  1403. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
  1404. ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
  1405. ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
  1406. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  1407. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
  1408. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
  1409. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen"), "set_mode", "get_mode");
  1410. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen"), "set_current_screen", "get_current_screen");
  1411. ADD_GROUP("Flags", "");
  1412. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1413. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
  1414. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
  1415. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
  1416. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
  1417. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
  1418. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
  1419. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
  1420. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
  1421. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
  1422. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
  1423. ADD_GROUP("Limits", "");
  1424. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
  1425. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size", PROPERTY_HINT_NONE, "suffix:px"), "set_max_size", "get_max_size");
  1426. ADD_GROUP("Content Scale", "content_scale_");
  1427. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
  1428. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,Canvas Items,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
  1429. 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");
  1430. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "content_scale_factor"), "set_content_scale_factor", "get_content_scale_factor");
  1431. ADD_GROUP("Theme", "theme_");
  1432. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  1433. ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
  1434. ADD_GROUP("Auto Translate", "");
  1435. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate"), "set_auto_translate", "is_auto_translating");
  1436. ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  1437. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
  1438. ADD_SIGNAL(MethodInfo("mouse_entered"));
  1439. ADD_SIGNAL(MethodInfo("mouse_exited"));
  1440. ADD_SIGNAL(MethodInfo("focus_entered"));
  1441. ADD_SIGNAL(MethodInfo("focus_exited"));
  1442. ADD_SIGNAL(MethodInfo("close_requested"));
  1443. ADD_SIGNAL(MethodInfo("go_back_requested"));
  1444. ADD_SIGNAL(MethodInfo("visibility_changed"));
  1445. ADD_SIGNAL(MethodInfo("about_to_popup"));
  1446. ADD_SIGNAL(MethodInfo("theme_changed"));
  1447. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  1448. BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
  1449. BIND_ENUM_CONSTANT(MODE_WINDOWED);
  1450. BIND_ENUM_CONSTANT(MODE_MINIMIZED);
  1451. BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
  1452. BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
  1453. BIND_ENUM_CONSTANT(MODE_EXCLUSIVE_FULLSCREEN);
  1454. BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
  1455. BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
  1456. BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
  1457. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  1458. BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
  1459. BIND_ENUM_CONSTANT(FLAG_POPUP);
  1460. BIND_ENUM_CONSTANT(FLAG_EXTEND_TO_TITLE);
  1461. BIND_ENUM_CONSTANT(FLAG_MAX);
  1462. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
  1463. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
  1464. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
  1465. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
  1466. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
  1467. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
  1468. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
  1469. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
  1470. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
  1471. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
  1472. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
  1473. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
  1474. }
  1475. Window::Window() {
  1476. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  1477. }
  1478. Window::~Window() {
  1479. }