window.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*************************************************************************/
  2. /* window.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/debugger/engine_debugger.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string/translation.h"
  34. #include "scene/gui/control.h"
  35. #include "scene/resources/font.h"
  36. #include "scene/scene_string_names.h"
  37. void Window::set_title(const String &p_title) {
  38. title = p_title;
  39. if (embedder) {
  40. embedder->_sub_window_update(this);
  41. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  42. DisplayServer::get_singleton()->window_set_title(p_title, window_id);
  43. }
  44. }
  45. String Window::get_title() const {
  46. return title;
  47. }
  48. void Window::set_current_screen(int p_screen) {
  49. current_screen = p_screen;
  50. if (window_id == DisplayServer::INVALID_WINDOW_ID) {
  51. return;
  52. }
  53. DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
  54. }
  55. int Window::get_current_screen() const {
  56. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  57. current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
  58. }
  59. return current_screen;
  60. }
  61. void Window::set_position(const Point2i &p_position) {
  62. position = p_position;
  63. if (embedder) {
  64. embedder->_sub_window_update(this);
  65. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  66. DisplayServer::get_singleton()->window_set_position(p_position, window_id);
  67. }
  68. }
  69. Point2i Window::get_position() const {
  70. return position;
  71. }
  72. void Window::set_size(const Size2i &p_size) {
  73. size = p_size;
  74. _update_window_size();
  75. }
  76. Size2i Window::get_size() const {
  77. return size;
  78. }
  79. Size2i Window::get_real_size() const {
  80. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  81. return DisplayServer::get_singleton()->window_get_real_size(window_id);
  82. }
  83. return size;
  84. }
  85. void Window::set_max_size(const Size2i &p_max_size) {
  86. max_size = p_max_size;
  87. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  88. DisplayServer::get_singleton()->window_set_max_size(max_size, window_id);
  89. }
  90. _update_window_size();
  91. }
  92. Size2i Window::get_max_size() const {
  93. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  94. max_size = DisplayServer::get_singleton()->window_get_max_size(window_id);
  95. }
  96. return max_size;
  97. }
  98. void Window::set_min_size(const Size2i &p_min_size) {
  99. min_size = p_min_size;
  100. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  101. DisplayServer::get_singleton()->window_set_min_size(min_size, window_id);
  102. }
  103. _update_window_size();
  104. }
  105. Size2i Window::get_min_size() const {
  106. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  107. min_size = DisplayServer::get_singleton()->window_get_min_size(window_id);
  108. }
  109. return min_size;
  110. }
  111. void Window::set_mode(Mode p_mode) {
  112. mode = p_mode;
  113. if (embedder) {
  114. embedder->_sub_window_update(this);
  115. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  116. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
  117. }
  118. }
  119. Window::Mode Window::get_mode() const {
  120. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  121. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  122. }
  123. return mode;
  124. }
  125. void Window::set_flag(Flags p_flag, bool p_enabled) {
  126. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  127. flags[p_flag] = p_enabled;
  128. if (embedder) {
  129. embedder->_sub_window_update(this);
  130. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  131. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
  132. }
  133. }
  134. bool Window::get_flag(Flags p_flag) const {
  135. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  136. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  137. flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
  138. }
  139. return flags[p_flag];
  140. }
  141. bool Window::is_maximize_allowed() const {
  142. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  143. return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
  144. }
  145. return true;
  146. }
  147. void Window::request_attention() {
  148. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  149. DisplayServer::get_singleton()->window_request_attention(window_id);
  150. }
  151. }
  152. void Window::move_to_foreground() {
  153. if (embedder) {
  154. embedder->_sub_window_grab_focus(this);
  155. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  156. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  157. }
  158. }
  159. bool Window::can_draw() const {
  160. if (!is_inside_tree()) {
  161. return false;
  162. }
  163. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  164. return DisplayServer::get_singleton()->window_can_draw(window_id);
  165. }
  166. return visible;
  167. }
  168. void Window::set_ime_active(bool p_active) {
  169. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  170. DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
  171. }
  172. }
  173. void Window::set_ime_position(const Point2i &p_pos) {
  174. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  175. DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
  176. }
  177. }
  178. bool Window::is_embedded() const {
  179. ERR_FAIL_COND_V(!is_inside_tree(), false);
  180. return _get_embedder() != nullptr;
  181. }
  182. void Window::_make_window() {
  183. ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
  184. uint32_t f = 0;
  185. for (int i = 0; i < FLAG_MAX; i++) {
  186. if (flags[i]) {
  187. f |= (1 << i);
  188. }
  189. }
  190. window_id = DisplayServer::get_singleton()->create_sub_window(DisplayServer::WindowMode(mode), f, Rect2i(position, size));
  191. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  192. DisplayServer::get_singleton()->window_set_current_screen(current_screen, window_id);
  193. DisplayServer::get_singleton()->window_set_max_size(max_size, window_id);
  194. DisplayServer::get_singleton()->window_set_min_size(min_size, window_id);
  195. DisplayServer::get_singleton()->window_set_title(tr(title), window_id);
  196. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  197. _update_window_size();
  198. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  199. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  200. }
  201. for (Set<Window *>::Element *E = transient_children.front(); E; E = E->next()) {
  202. if (E->get()->window_id != DisplayServer::INVALID_WINDOW_ID) {
  203. DisplayServer::get_singleton()->window_set_transient(E->get()->window_id, transient_parent->window_id);
  204. }
  205. }
  206. _update_window_callbacks();
  207. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  208. DisplayServer::get_singleton()->show_window(window_id);
  209. }
  210. void Window::_update_from_window() {
  211. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  212. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  213. for (int i = 0; i < FLAG_MAX; i++) {
  214. flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
  215. }
  216. }
  217. void Window::_clear_window() {
  218. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  219. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  220. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  221. }
  222. for (Set<Window *>::Element *E = transient_children.front(); E; E = E->next()) {
  223. if (E->get()->window_id != DisplayServer::INVALID_WINDOW_ID) {
  224. DisplayServer::get_singleton()->window_set_transient(E->get()->window_id, DisplayServer::INVALID_WINDOW_ID);
  225. }
  226. }
  227. _update_from_window();
  228. DisplayServer::get_singleton()->delete_sub_window(window_id);
  229. window_id = DisplayServer::INVALID_WINDOW_ID;
  230. _update_viewport_size();
  231. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  232. }
  233. void Window::_rect_changed_callback(const Rect2i &p_callback) {
  234. //we must always accept this as the truth
  235. if (size == p_callback.size && position == p_callback.position) {
  236. return;
  237. }
  238. position = p_callback.position;
  239. if (size != p_callback.size) {
  240. size = p_callback.size;
  241. _update_viewport_size();
  242. }
  243. }
  244. void Window::_propagate_window_notification(Node *p_node, int p_notification) {
  245. p_node->notification(p_notification);
  246. for (int i = 0; i < p_node->get_child_count(); i++) {
  247. Node *child = p_node->get_child(i);
  248. Window *window = Object::cast_to<Window>(child);
  249. if (window) {
  250. break;
  251. }
  252. _propagate_window_notification(child, p_notification);
  253. }
  254. }
  255. void Window::_event_callback(DisplayServer::WindowEvent p_event) {
  256. switch (p_event) {
  257. case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
  258. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
  259. emit_signal("mouse_entered");
  260. DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
  261. } break;
  262. case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
  263. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
  264. emit_signal("mouse_exited");
  265. } break;
  266. case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
  267. focused = true;
  268. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
  269. emit_signal("focus_entered");
  270. } break;
  271. case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
  272. focused = false;
  273. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  274. emit_signal("focus_exited");
  275. } break;
  276. case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
  277. if (exclusive_child != nullptr) {
  278. break; //has an exclusive child, can't get events until child is closed
  279. }
  280. _propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
  281. emit_signal("close_requested");
  282. } break;
  283. case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
  284. _propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
  285. emit_signal("go_back_requested");
  286. } break;
  287. case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
  288. _update_viewport_size();
  289. _propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
  290. emit_signal("dpi_changed");
  291. } break;
  292. }
  293. }
  294. void Window::show() {
  295. set_visible(true);
  296. }
  297. void Window::hide() {
  298. set_visible(false);
  299. }
  300. void Window::set_visible(bool p_visible) {
  301. if (visible == p_visible) {
  302. return;
  303. }
  304. visible = p_visible;
  305. if (!is_inside_tree()) {
  306. return;
  307. }
  308. if (updating_child_controls) {
  309. _update_child_controls();
  310. }
  311. ERR_FAIL_COND_MSG(get_parent() == nullptr, "Can't change visibility of main window.");
  312. Viewport *embedder_vp = _get_embedder();
  313. if (!embedder_vp) {
  314. if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
  315. _clear_window();
  316. }
  317. if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
  318. _make_window();
  319. }
  320. } else {
  321. if (visible) {
  322. embedder = embedder_vp;
  323. embedder->_sub_window_register(this);
  324. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  325. } else {
  326. embedder->_sub_window_remove(this);
  327. embedder = nullptr;
  328. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  329. }
  330. _update_window_size();
  331. }
  332. if (!visible) {
  333. focused = false;
  334. }
  335. notification(NOTIFICATION_VISIBILITY_CHANGED);
  336. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  337. RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
  338. //update transient exclusive
  339. if (transient_parent) {
  340. if (exclusive && visible) {
  341. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  342. transient_parent->exclusive_child = this;
  343. } else {
  344. if (transient_parent->exclusive_child == this) {
  345. transient_parent->exclusive_child = nullptr;
  346. }
  347. }
  348. }
  349. }
  350. void Window::_clear_transient() {
  351. if (transient_parent) {
  352. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  353. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  354. }
  355. transient_parent->transient_children.erase(this);
  356. if (transient_parent->exclusive_child == this) {
  357. transient_parent->exclusive_child = nullptr;
  358. }
  359. transient_parent = nullptr;
  360. }
  361. }
  362. void Window::_make_transient() {
  363. if (!get_parent()) {
  364. //main window, can't be transient
  365. return;
  366. }
  367. //find transient parent
  368. Viewport *vp = get_parent()->get_viewport();
  369. Window *window = nullptr;
  370. while (vp) {
  371. window = Object::cast_to<Window>(vp);
  372. if (window) {
  373. break;
  374. }
  375. if (!vp->get_parent()) {
  376. break;
  377. }
  378. vp = vp->get_parent()->get_viewport();
  379. }
  380. if (window) {
  381. transient_parent = window;
  382. window->transient_children.insert(this);
  383. if (is_inside_tree() && is_visible() && exclusive) {
  384. if (transient_parent->exclusive_child == nullptr) {
  385. transient_parent->exclusive_child = this;
  386. } else if (transient_parent->exclusive_child != this) {
  387. ERR_PRINT("Making child transient exclusive, but parent has another exclusive child");
  388. }
  389. }
  390. }
  391. //see if we can make transient
  392. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  393. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  394. }
  395. }
  396. void Window::set_transient(bool p_transient) {
  397. if (transient == p_transient) {
  398. return;
  399. }
  400. transient = p_transient;
  401. if (!is_inside_tree()) {
  402. return;
  403. }
  404. if (transient) {
  405. _make_transient();
  406. } else {
  407. _clear_transient();
  408. }
  409. }
  410. bool Window::is_transient() const {
  411. return transient;
  412. }
  413. void Window::set_exclusive(bool p_exclusive) {
  414. if (exclusive == p_exclusive) {
  415. return;
  416. }
  417. exclusive = p_exclusive;
  418. if (transient_parent) {
  419. if (p_exclusive && is_inside_tree() && is_visible()) {
  420. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  421. transient_parent->exclusive_child = this;
  422. } else {
  423. if (transient_parent->exclusive_child == this) {
  424. transient_parent->exclusive_child = nullptr;
  425. }
  426. }
  427. }
  428. }
  429. bool Window::is_exclusive() const {
  430. return exclusive;
  431. }
  432. bool Window::is_visible() const {
  433. return visible;
  434. }
  435. void Window::_update_window_size() {
  436. Size2i size_limit;
  437. if (wrap_controls) {
  438. size_limit = get_contents_minimum_size();
  439. }
  440. size_limit.x = MAX(size_limit.x, min_size.x);
  441. size_limit.y = MAX(size_limit.y, min_size.y);
  442. size.x = MAX(size_limit.x, size.x);
  443. size.y = MAX(size_limit.y, size.y);
  444. if (max_size.x > 0 && max_size.x > min_size.x && size.x > max_size.x) {
  445. size.x = max_size.x;
  446. }
  447. if (max_size.y > 0 && max_size.y > min_size.y && size.y > max_size.y) {
  448. size.y = max_size.y;
  449. }
  450. if (embedder) {
  451. embedder->_sub_window_update(this);
  452. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  453. DisplayServer::get_singleton()->window_set_size(size, window_id);
  454. }
  455. //update the viewport
  456. _update_viewport_size();
  457. }
  458. void Window::_update_viewport_size() {
  459. //update the viewport part
  460. Size2i final_size;
  461. Size2i final_size_override;
  462. Rect2i attach_to_screen_rect(Point2i(), size);
  463. Transform2D stretch_transform;
  464. float font_oversampling = 1.0;
  465. if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
  466. stretch_transform = Transform2D();
  467. final_size = size;
  468. } else {
  469. //actual screen video mode
  470. Size2 video_mode = size;
  471. Size2 desired_res = content_scale_size;
  472. Size2 viewport_size;
  473. Size2 screen_size;
  474. float viewport_aspect = desired_res.aspect();
  475. float video_mode_aspect = video_mode.aspect();
  476. if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  477. //same aspect or ignore aspect
  478. viewport_size = desired_res;
  479. screen_size = video_mode;
  480. } else if (viewport_aspect < video_mode_aspect) {
  481. // screen ratio is smaller vertically
  482. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  483. //will stretch horizontally
  484. viewport_size.x = desired_res.y * video_mode_aspect;
  485. viewport_size.y = desired_res.y;
  486. screen_size = video_mode;
  487. } else {
  488. //will need black bars
  489. viewport_size = desired_res;
  490. screen_size.x = video_mode.y * viewport_aspect;
  491. screen_size.y = video_mode.y;
  492. }
  493. } else {
  494. //screen ratio is smaller horizontally
  495. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  496. //will stretch horizontally
  497. viewport_size.x = desired_res.x;
  498. viewport_size.y = desired_res.x / video_mode_aspect;
  499. screen_size = video_mode;
  500. } else {
  501. //will need black bars
  502. viewport_size = desired_res;
  503. screen_size.x = video_mode.x;
  504. screen_size.y = video_mode.x / viewport_aspect;
  505. }
  506. }
  507. screen_size = screen_size.floor();
  508. viewport_size = viewport_size.floor();
  509. Size2 margin;
  510. Size2 offset;
  511. //black bars and margin
  512. if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.x < video_mode.x) {
  513. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  514. //RenderingServer::get_singleton()->black_bars_set_margins(margin.x, 0, margin.x, 0);
  515. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  516. } else if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.y < video_mode.y) {
  517. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  518. //RenderingServer::get_singleton()->black_bars_set_margins(0, margin.y, 0, margin.y);
  519. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  520. } else {
  521. //RenderingServer::get_singleton()->black_bars_set_margins(0, 0, 0, 0);
  522. }
  523. switch (content_scale_mode) {
  524. case CONTENT_SCALE_MODE_DISABLED: {
  525. // Already handled above
  526. //_update_font_oversampling(1.0);
  527. } break;
  528. case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
  529. final_size = screen_size;
  530. final_size_override = viewport_size;
  531. attach_to_screen_rect = Rect2(margin, screen_size);
  532. font_oversampling = screen_size.x / viewport_size.x;
  533. Size2 scale = Vector2(screen_size) / Vector2(final_size_override);
  534. stretch_transform.scale(scale);
  535. } break;
  536. case CONTENT_SCALE_MODE_VIEWPORT: {
  537. final_size = viewport_size;
  538. attach_to_screen_rect = Rect2(margin, screen_size);
  539. } break;
  540. }
  541. }
  542. bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
  543. _set_size(final_size, final_size_override, attach_to_screen_rect, stretch_transform, allocate);
  544. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  545. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
  546. } else {
  547. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
  548. }
  549. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  550. if (!use_font_oversampling) {
  551. font_oversampling = 1.0;
  552. }
  553. if (TS->font_get_oversampling() != font_oversampling) {
  554. TS->font_set_oversampling(font_oversampling);
  555. }
  556. }
  557. notification(NOTIFICATION_WM_SIZE_CHANGED);
  558. if (embedder) {
  559. embedder->_sub_window_update(this);
  560. }
  561. }
  562. void Window::_update_window_callbacks() {
  563. DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
  564. DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
  565. DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
  566. DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
  567. DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
  568. }
  569. Viewport *Window::_get_embedder() const {
  570. Viewport *vp = get_parent_viewport();
  571. while (vp) {
  572. if (vp->is_embedding_subwindows()) {
  573. return vp;
  574. }
  575. if (vp->get_parent()) {
  576. vp = vp->get_parent()->get_viewport();
  577. } else {
  578. vp = nullptr;
  579. }
  580. }
  581. return nullptr;
  582. }
  583. void Window::_notification(int p_what) {
  584. if (p_what == NOTIFICATION_ENTER_TREE) {
  585. bool embedded = false;
  586. {
  587. embedder = _get_embedder();
  588. if (embedder) {
  589. embedded = true;
  590. if (!visible) {
  591. embedder = nullptr; //not yet since not visible
  592. }
  593. }
  594. }
  595. if (embedded) {
  596. //create as embedded
  597. if (embedder) {
  598. embedder->_sub_window_register(this);
  599. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  600. _update_window_size();
  601. }
  602. } else {
  603. if (get_parent() == nullptr) {
  604. //it's the root window!
  605. visible = true; //always visible
  606. window_id = DisplayServer::MAIN_WINDOW_ID;
  607. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  608. _update_from_window();
  609. //since this window already exists (created on start), we must update pos and size from it
  610. {
  611. position = DisplayServer::get_singleton()->window_get_position(window_id);
  612. size = DisplayServer::get_singleton()->window_get_size(window_id);
  613. }
  614. _update_viewport_size(); //then feed back to the viewport
  615. _update_window_callbacks();
  616. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  617. } else {
  618. //create
  619. if (visible) {
  620. _make_window();
  621. }
  622. }
  623. }
  624. if (transient) {
  625. _make_transient();
  626. }
  627. if (visible) {
  628. notification(NOTIFICATION_VISIBILITY_CHANGED);
  629. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  630. RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
  631. }
  632. }
  633. if (p_what == NOTIFICATION_READY) {
  634. if (wrap_controls) {
  635. _update_child_controls();
  636. }
  637. }
  638. if (p_what == NOTIFICATION_TRANSLATION_CHANGED) {
  639. child_controls_changed();
  640. }
  641. if (p_what == NOTIFICATION_EXIT_TREE) {
  642. if (transient) {
  643. _clear_transient();
  644. }
  645. if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
  646. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  647. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  648. _update_window_callbacks();
  649. } else {
  650. _clear_window();
  651. }
  652. } else {
  653. if (embedder) {
  654. embedder->_sub_window_remove(this);
  655. embedder = nullptr;
  656. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  657. }
  658. _update_viewport_size(); //called by clear and make, which does not happen here
  659. }
  660. RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
  661. }
  662. }
  663. void Window::set_content_scale_size(const Size2i &p_size) {
  664. ERR_FAIL_COND(p_size.x < 0);
  665. ERR_FAIL_COND(p_size.y < 0);
  666. content_scale_size = p_size;
  667. _update_viewport_size();
  668. }
  669. Size2i Window::get_content_scale_size() const {
  670. return content_scale_size;
  671. }
  672. void Window::set_content_scale_mode(ContentScaleMode p_mode) {
  673. content_scale_mode = p_mode;
  674. _update_viewport_size();
  675. }
  676. Window::ContentScaleMode Window::get_content_scale_mode() const {
  677. return content_scale_mode;
  678. }
  679. void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
  680. content_scale_aspect = p_aspect;
  681. _update_viewport_size();
  682. }
  683. Window::ContentScaleAspect Window::get_content_scale_aspect() const {
  684. return content_scale_aspect;
  685. }
  686. void Window::set_use_font_oversampling(bool p_oversampling) {
  687. if (is_inside_tree() && window_id != DisplayServer::MAIN_WINDOW_ID) {
  688. ERR_FAIL_MSG("Only the root window can set and use font oversampling.");
  689. }
  690. use_font_oversampling = p_oversampling;
  691. _update_viewport_size();
  692. }
  693. bool Window::is_using_font_oversampling() const {
  694. return use_font_oversampling;
  695. }
  696. DisplayServer::WindowID Window::get_window_id() const {
  697. if (embedder) {
  698. return parent->get_window_id();
  699. }
  700. return window_id;
  701. }
  702. void Window::set_wrap_controls(bool p_enable) {
  703. wrap_controls = p_enable;
  704. if (wrap_controls) {
  705. child_controls_changed();
  706. }
  707. }
  708. bool Window::is_wrapping_controls() const {
  709. return wrap_controls;
  710. }
  711. Size2 Window::_get_contents_minimum_size() const {
  712. Size2 max;
  713. for (int i = 0; i < get_child_count(); i++) {
  714. Control *c = Object::cast_to<Control>(get_child(i));
  715. if (c) {
  716. Point2i pos = c->get_position();
  717. Size2i min = c->get_combined_minimum_size();
  718. max.x = MAX(pos.x + min.x, max.x);
  719. max.y = MAX(pos.y + min.y, max.y);
  720. }
  721. }
  722. return max;
  723. }
  724. void Window::_update_child_controls() {
  725. if (!updating_child_controls) {
  726. return;
  727. }
  728. _update_window_size();
  729. updating_child_controls = false;
  730. }
  731. void Window::child_controls_changed() {
  732. if (!is_inside_tree() || !visible || updating_child_controls) {
  733. return;
  734. }
  735. updating_child_controls = true;
  736. call_deferred("_update_child_controls");
  737. }
  738. bool Window::_can_consume_input_events() const {
  739. return exclusive_child == nullptr;
  740. }
  741. void Window::_window_input(const Ref<InputEvent> &p_ev) {
  742. if (EngineDebugger::is_active()) {
  743. //quit from game window using F8
  744. Ref<InputEventKey> k = p_ev;
  745. if (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_keycode() == KEY_F8) {
  746. EngineDebugger::get_singleton()->send_message("request_quit", Array());
  747. }
  748. }
  749. if (exclusive_child != nullptr) {
  750. Window *focus_target = exclusive_child;
  751. focus_target->grab_focus();
  752. while (focus_target->exclusive_child != nullptr) {
  753. focus_target = focus_target->exclusive_child;
  754. focus_target->grab_focus();
  755. }
  756. if (!is_embedding_subwindows()) { //not embedding, no need for event
  757. return;
  758. }
  759. }
  760. emit_signal(SceneStringNames::get_singleton()->window_input, p_ev);
  761. input(p_ev);
  762. if (!is_input_handled()) {
  763. unhandled_input(p_ev);
  764. }
  765. }
  766. void Window::_window_input_text(const String &p_text) {
  767. input_text(p_text);
  768. }
  769. void Window::_window_drop_files(const Vector<String> &p_files) {
  770. emit_signal("files_dropped", p_files, current_screen);
  771. }
  772. Viewport *Window::get_parent_viewport() const {
  773. if (get_parent()) {
  774. return get_parent()->get_viewport();
  775. } else {
  776. return nullptr;
  777. }
  778. }
  779. Window *Window::get_parent_visible_window() const {
  780. Viewport *vp = get_parent_viewport();
  781. Window *window = nullptr;
  782. while (vp) {
  783. window = Object::cast_to<Window>(vp);
  784. if (window && window->visible) {
  785. break;
  786. }
  787. if (!vp->get_parent()) {
  788. break;
  789. }
  790. vp = vp->get_parent()->get_viewport();
  791. }
  792. return window;
  793. }
  794. void Window::popup_on_parent(const Rect2i &p_parent_rect) {
  795. ERR_FAIL_COND(!is_inside_tree());
  796. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  797. if (!is_embedded()) {
  798. Window *window = get_parent_visible_window();
  799. if (!window) {
  800. popup(p_parent_rect);
  801. } else {
  802. popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
  803. }
  804. } else {
  805. popup(p_parent_rect);
  806. }
  807. }
  808. void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
  809. ERR_FAIL_COND(!is_inside_tree());
  810. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  811. Rect2 parent_rect;
  812. if (is_embedded()) {
  813. parent_rect = get_parent_viewport()->get_visible_rect();
  814. } else {
  815. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  816. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  817. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  818. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  819. }
  820. Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
  821. Rect2i popup_rect;
  822. popup_rect.size = Vector2i(MIN(size_ratio.x, p_size.x), MIN(size_ratio.y, p_size.y));
  823. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  824. popup(popup_rect);
  825. }
  826. void Window::popup_centered(const Size2i &p_minsize) {
  827. ERR_FAIL_COND(!is_inside_tree());
  828. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  829. Rect2 parent_rect;
  830. if (is_embedded()) {
  831. parent_rect = get_parent_viewport()->get_visible_rect();
  832. } else {
  833. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  834. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  835. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  836. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  837. }
  838. Rect2i popup_rect;
  839. if (p_minsize == Size2i()) {
  840. popup_rect.size = _get_contents_minimum_size();
  841. } else {
  842. popup_rect.size = p_minsize;
  843. }
  844. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  845. popup(popup_rect);
  846. }
  847. void Window::popup_centered_ratio(float p_ratio) {
  848. ERR_FAIL_COND(!is_inside_tree());
  849. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  850. Rect2 parent_rect;
  851. if (is_embedded()) {
  852. parent_rect = get_parent_viewport()->get_visible_rect();
  853. } else {
  854. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  855. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  856. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  857. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  858. }
  859. Rect2i popup_rect;
  860. popup_rect.size = parent_rect.size * p_ratio;
  861. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  862. popup(popup_rect);
  863. }
  864. void Window::popup(const Rect2i &p_screen_rect) {
  865. emit_signal("about_to_popup");
  866. // Update window size to calculate the actual window size based on contents minimum size and minimum size.
  867. _update_window_size();
  868. if (p_screen_rect != Rect2i()) {
  869. set_position(p_screen_rect.position);
  870. set_size(p_screen_rect.size);
  871. }
  872. Rect2i adjust = _popup_adjust_rect();
  873. if (adjust != Rect2i()) {
  874. set_position(adjust.position);
  875. set_size(adjust.size);
  876. }
  877. int scr = DisplayServer::get_singleton()->get_screen_count();
  878. for (int i = 0; i < scr; i++) {
  879. Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
  880. if (r.has_point(position)) {
  881. current_screen = i;
  882. break;
  883. }
  884. }
  885. set_transient(true);
  886. set_visible(true);
  887. _post_popup();
  888. notification(NOTIFICATION_POST_POPUP);
  889. }
  890. Size2 Window::get_contents_minimum_size() const {
  891. return _get_contents_minimum_size();
  892. }
  893. void Window::grab_focus() {
  894. if (embedder) {
  895. embedder->_sub_window_grab_focus(this);
  896. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  897. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  898. }
  899. }
  900. bool Window::has_focus() const {
  901. return focused;
  902. }
  903. Rect2i Window::get_usable_parent_rect() const {
  904. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  905. Rect2i parent;
  906. if (is_embedded()) {
  907. parent = _get_embedder()->get_visible_rect();
  908. } else {
  909. const Window *w = is_visible() ? this : get_parent_visible_window();
  910. //find a parent that can contain us
  911. ERR_FAIL_COND_V(!w, Rect2());
  912. parent = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
  913. }
  914. return parent;
  915. }
  916. void Window::add_child_notify(Node *p_child) {
  917. Control *child_c = Object::cast_to<Control>(p_child);
  918. if (child_c && child_c->data.theme.is_null() && (theme_owner || theme_owner_window)) {
  919. Control::_propagate_theme_changed(child_c, theme_owner, theme_owner_window); //need to propagate here, since many controls may require setting up stuff
  920. }
  921. Window *child_w = Object::cast_to<Window>(p_child);
  922. if (child_w && child_w->theme.is_null() && (theme_owner || theme_owner_window)) {
  923. Control::_propagate_theme_changed(child_w, theme_owner, theme_owner_window); //need to propagate here, since many controls may require setting up stuff
  924. }
  925. if (is_inside_tree() && wrap_controls) {
  926. child_controls_changed();
  927. }
  928. }
  929. void Window::remove_child_notify(Node *p_child) {
  930. Control *child_c = Object::cast_to<Control>(p_child);
  931. if (child_c && (child_c->data.theme_owner || child_c->data.theme_owner_window) && child_c->data.theme.is_null()) {
  932. Control::_propagate_theme_changed(child_c, nullptr, nullptr);
  933. }
  934. Window *child_w = Object::cast_to<Window>(p_child);
  935. if (child_w && (child_w->theme_owner || child_w->theme_owner_window) && child_w->theme.is_null()) {
  936. Control::_propagate_theme_changed(child_w, nullptr, nullptr);
  937. }
  938. if (is_inside_tree() && wrap_controls) {
  939. child_controls_changed();
  940. }
  941. }
  942. void Window::set_theme(const Ref<Theme> &p_theme) {
  943. if (theme == p_theme) {
  944. return;
  945. }
  946. theme = p_theme;
  947. if (!p_theme.is_null()) {
  948. theme_owner = nullptr;
  949. theme_owner_window = this;
  950. Control::_propagate_theme_changed(this, nullptr, this);
  951. } else {
  952. Control *parent_c = cast_to<Control>(get_parent());
  953. if (parent_c && (parent_c->data.theme_owner || parent_c->data.theme_owner_window)) {
  954. Control::_propagate_theme_changed(this, parent_c->data.theme_owner, parent_c->data.theme_owner_window);
  955. } else {
  956. Window *parent_w = cast_to<Window>(get_parent());
  957. if (parent_w && (parent_w->theme_owner || parent_w->theme_owner_window)) {
  958. Control::_propagate_theme_changed(this, parent_w->theme_owner, parent_w->theme_owner_window);
  959. } else {
  960. Control::_propagate_theme_changed(this, nullptr, nullptr);
  961. }
  962. }
  963. }
  964. }
  965. Ref<Theme> Window::get_theme() const {
  966. return theme;
  967. }
  968. Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_type) const {
  969. StringName type = p_type ? p_type : get_class_name();
  970. return Control::get_icons(theme_owner, theme_owner_window, p_name, type);
  971. }
  972. Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_type) const {
  973. StringName type = p_type ? p_type : get_class_name();
  974. return Control::get_styleboxs(theme_owner, theme_owner_window, p_name, type);
  975. }
  976. Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_type) const {
  977. StringName type = p_type ? p_type : get_class_name();
  978. return Control::get_fonts(theme_owner, theme_owner_window, p_name, type);
  979. }
  980. int Window::get_theme_font_size(const StringName &p_name, const StringName &p_type) const {
  981. StringName type = p_type ? p_type : get_class_name();
  982. return Control::get_font_sizes(theme_owner, theme_owner_window, p_name, type);
  983. }
  984. Color Window::get_theme_color(const StringName &p_name, const StringName &p_type) const {
  985. StringName type = p_type ? p_type : get_class_name();
  986. return Control::get_colors(theme_owner, theme_owner_window, p_name, type);
  987. }
  988. int Window::get_theme_constant(const StringName &p_name, const StringName &p_type) const {
  989. StringName type = p_type ? p_type : get_class_name();
  990. return Control::get_constants(theme_owner, theme_owner_window, p_name, type);
  991. }
  992. bool Window::has_theme_icon(const StringName &p_name, const StringName &p_type) const {
  993. StringName type = p_type ? p_type : get_class_name();
  994. return Control::has_icons(theme_owner, theme_owner_window, p_name, type);
  995. }
  996. bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_type) const {
  997. StringName type = p_type ? p_type : get_class_name();
  998. return Control::has_styleboxs(theme_owner, theme_owner_window, p_name, type);
  999. }
  1000. bool Window::has_theme_font(const StringName &p_name, const StringName &p_type) const {
  1001. StringName type = p_type ? p_type : get_class_name();
  1002. return Control::has_fonts(theme_owner, theme_owner_window, p_name, type);
  1003. }
  1004. bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_type) const {
  1005. StringName type = p_type ? p_type : get_class_name();
  1006. return Control::has_font_sizes(theme_owner, theme_owner_window, p_name, type);
  1007. }
  1008. bool Window::has_theme_color(const StringName &p_name, const StringName &p_type) const {
  1009. StringName type = p_type ? p_type : get_class_name();
  1010. return Control::has_colors(theme_owner, theme_owner_window, p_name, type);
  1011. }
  1012. bool Window::has_theme_constant(const StringName &p_name, const StringName &p_type) const {
  1013. StringName type = p_type ? p_type : get_class_name();
  1014. return Control::has_constants(theme_owner, theme_owner_window, p_name, type);
  1015. }
  1016. Rect2i Window::get_parent_rect() const {
  1017. ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
  1018. if (is_embedded()) {
  1019. //viewport
  1020. Node *n = get_parent();
  1021. ERR_FAIL_COND_V(!n, Rect2i());
  1022. Viewport *p = n->get_viewport();
  1023. ERR_FAIL_COND_V(!p, Rect2i());
  1024. return p->get_visible_rect();
  1025. } else {
  1026. int x = get_position().x;
  1027. int closest_dist = 0x7FFFFFFF;
  1028. Rect2i closest_rect;
  1029. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  1030. Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
  1031. int d;
  1032. if (x >= s.position.x && x < s.size.x) {
  1033. //contained
  1034. closest_rect = s;
  1035. break;
  1036. } else if (x < s.position.x) {
  1037. d = s.position.x - x;
  1038. } else {
  1039. d = x - (s.position.x + s.size.x);
  1040. }
  1041. if (d < closest_dist) {
  1042. closest_dist = d;
  1043. closest_rect = s;
  1044. }
  1045. }
  1046. return closest_rect;
  1047. }
  1048. }
  1049. void Window::set_clamp_to_embedder(bool p_enable) {
  1050. clamp_to_embedder = p_enable;
  1051. }
  1052. bool Window::is_clamped_to_embedder() const {
  1053. return clamp_to_embedder;
  1054. }
  1055. void Window::set_layout_direction(Window::LayoutDirection p_direction) {
  1056. ERR_FAIL_INDEX((int)p_direction, 4);
  1057. layout_dir = p_direction;
  1058. propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  1059. }
  1060. Window::LayoutDirection Window::get_layout_direction() const {
  1061. return layout_dir;
  1062. }
  1063. bool Window::is_layout_rtl() const {
  1064. if (layout_dir == LAYOUT_DIRECTION_INHERITED) {
  1065. Window *parent = Object::cast_to<Window>(get_parent());
  1066. if (parent) {
  1067. return parent->is_layout_rtl();
  1068. } else {
  1069. if (GLOBAL_GET("internationalization/rendering/force_right_to_left_layout_direction")) {
  1070. return true;
  1071. }
  1072. String locale = TranslationServer::get_singleton()->get_tool_locale();
  1073. return TS->is_locale_right_to_left(locale);
  1074. }
  1075. } else if (layout_dir == LAYOUT_DIRECTION_LOCALE) {
  1076. if (GLOBAL_GET("internationalization/rendering/force_right_to_left_layout_direction")) {
  1077. return true;
  1078. }
  1079. String locale = TranslationServer::get_singleton()->get_tool_locale();
  1080. return TS->is_locale_right_to_left(locale);
  1081. } else {
  1082. return (layout_dir == LAYOUT_DIRECTION_RTL);
  1083. }
  1084. }
  1085. void Window::_bind_methods() {
  1086. ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
  1087. ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
  1088. ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
  1089. ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
  1090. ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
  1091. ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
  1092. ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
  1093. ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
  1094. ClassDB::bind_method(D_METHOD("get_real_size"), &Window::get_real_size);
  1095. ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
  1096. ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
  1097. ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
  1098. ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
  1099. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
  1100. ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
  1101. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
  1102. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
  1103. ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
  1104. ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
  1105. ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
  1106. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
  1107. ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
  1108. ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
  1109. ClassDB::bind_method(D_METHOD("show"), &Window::show);
  1110. ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
  1111. ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
  1112. ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
  1113. ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
  1114. ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
  1115. ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
  1116. ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
  1117. ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);
  1118. ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);
  1119. ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
  1120. ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
  1121. ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
  1122. ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
  1123. ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
  1124. ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
  1125. ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
  1126. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
  1127. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
  1128. ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
  1129. ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
  1130. ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
  1131. ClassDB::bind_method(D_METHOD("_update_child_controls"), &Window::_update_child_controls);
  1132. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
  1133. ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
  1134. ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "type"), &Window::get_theme_icon, DEFVAL(""));
  1135. ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "type"), &Window::get_theme_stylebox, DEFVAL(""));
  1136. ClassDB::bind_method(D_METHOD("get_theme_font", "name", "type"), &Window::get_theme_font, DEFVAL(""));
  1137. ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "type"), &Window::get_theme_font_size, DEFVAL(""));
  1138. ClassDB::bind_method(D_METHOD("get_theme_color", "name", "type"), &Window::get_theme_color, DEFVAL(""));
  1139. ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "type"), &Window::get_theme_constant, DEFVAL(""));
  1140. ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "type"), &Window::has_theme_icon, DEFVAL(""));
  1141. ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "type"), &Window::has_theme_stylebox, DEFVAL(""));
  1142. ClassDB::bind_method(D_METHOD("has_theme_font", "name", "type"), &Window::has_theme_font, DEFVAL(""));
  1143. ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "type"), &Window::has_theme_font_size, DEFVAL(""));
  1144. ClassDB::bind_method(D_METHOD("has_theme_color", "name", "type"), &Window::has_theme_color, DEFVAL(""));
  1145. ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "type"), &Window::has_theme_constant, DEFVAL(""));
  1146. ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Window::set_layout_direction);
  1147. ClassDB::bind_method(D_METHOD("get_layout_direction"), &Window::get_layout_direction);
  1148. ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Window::is_layout_rtl);
  1149. ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
  1150. ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
  1151. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
  1152. ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
  1153. ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
  1154. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  1155. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position"), "set_position", "get_position");
  1156. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size");
  1157. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,FullScreen"), "set_mode", "get_mode");
  1158. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_screen"), "set_current_screen", "get_current_screen");
  1159. ADD_GROUP("Flags", "");
  1160. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1161. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
  1162. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
  1163. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
  1164. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
  1165. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
  1166. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
  1167. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
  1168. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
  1169. ADD_GROUP("Limits", "");
  1170. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size"), "set_min_size", "get_min_size");
  1171. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size"), "set_max_size", "get_max_size");
  1172. ADD_GROUP("Content Scale", "content_scale_");
  1173. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
  1174. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,CanvasItems,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
  1175. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_aspect", PROPERTY_HINT_ENUM, "Ignore,Keep,KeepWidth,KeepHeight,Expand"), "set_content_scale_aspect", "get_content_scale_aspect");
  1176. ADD_GROUP("Theme", "");
  1177. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  1178. ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  1179. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
  1180. ADD_SIGNAL(MethodInfo("mouse_entered"));
  1181. ADD_SIGNAL(MethodInfo("mouse_exited"));
  1182. ADD_SIGNAL(MethodInfo("focus_entered"));
  1183. ADD_SIGNAL(MethodInfo("focus_exited"));
  1184. ADD_SIGNAL(MethodInfo("close_requested"));
  1185. ADD_SIGNAL(MethodInfo("go_back_requested"));
  1186. ADD_SIGNAL(MethodInfo("visibility_changed"));
  1187. ADD_SIGNAL(MethodInfo("about_to_popup"));
  1188. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  1189. BIND_ENUM_CONSTANT(MODE_WINDOWED);
  1190. BIND_ENUM_CONSTANT(MODE_MINIMIZED);
  1191. BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
  1192. BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
  1193. BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
  1194. BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
  1195. BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
  1196. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  1197. BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
  1198. BIND_ENUM_CONSTANT(FLAG_MAX);
  1199. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
  1200. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
  1201. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
  1202. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
  1203. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
  1204. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
  1205. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
  1206. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
  1207. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
  1208. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
  1209. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
  1210. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
  1211. }
  1212. Window::Window() {
  1213. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  1214. }
  1215. Window::~Window() {
  1216. }