window.cpp 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964
  1. /**************************************************************************/
  2. /* window.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #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. #include "scene/theme/theme_owner.h"
  40. // Dynamic properties.
  41. bool Window::_set(const StringName &p_name, const Variant &p_value) {
  42. ERR_MAIN_THREAD_GUARD_V(false);
  43. String name = p_name;
  44. if (!name.begins_with("theme_override")) {
  45. return false;
  46. }
  47. if (p_value.get_type() == Variant::NIL || (p_value.get_type() == Variant::OBJECT && (Object *)p_value == nullptr)) {
  48. if (name.begins_with("theme_override_icons/")) {
  49. String dname = name.get_slicec('/', 1);
  50. if (theme_icon_override.has(dname)) {
  51. theme_icon_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  52. }
  53. theme_icon_override.erase(dname);
  54. _notify_theme_override_changed();
  55. } else if (name.begins_with("theme_override_styles/")) {
  56. String dname = name.get_slicec('/', 1);
  57. if (theme_style_override.has(dname)) {
  58. theme_style_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  59. }
  60. theme_style_override.erase(dname);
  61. _notify_theme_override_changed();
  62. } else if (name.begins_with("theme_override_fonts/")) {
  63. String dname = name.get_slicec('/', 1);
  64. if (theme_font_override.has(dname)) {
  65. theme_font_override[dname]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  66. }
  67. theme_font_override.erase(dname);
  68. _notify_theme_override_changed();
  69. } else if (name.begins_with("theme_override_font_sizes/")) {
  70. String dname = name.get_slicec('/', 1);
  71. theme_font_size_override.erase(dname);
  72. _notify_theme_override_changed();
  73. } else if (name.begins_with("theme_override_colors/")) {
  74. String dname = name.get_slicec('/', 1);
  75. theme_color_override.erase(dname);
  76. _notify_theme_override_changed();
  77. } else if (name.begins_with("theme_override_constants/")) {
  78. String dname = name.get_slicec('/', 1);
  79. theme_constant_override.erase(dname);
  80. _notify_theme_override_changed();
  81. } else {
  82. return false;
  83. }
  84. } else {
  85. if (name.begins_with("theme_override_icons/")) {
  86. String dname = name.get_slicec('/', 1);
  87. add_theme_icon_override(dname, p_value);
  88. } else if (name.begins_with("theme_override_styles/")) {
  89. String dname = name.get_slicec('/', 1);
  90. add_theme_style_override(dname, p_value);
  91. } else if (name.begins_with("theme_override_fonts/")) {
  92. String dname = name.get_slicec('/', 1);
  93. add_theme_font_override(dname, p_value);
  94. } else if (name.begins_with("theme_override_font_sizes/")) {
  95. String dname = name.get_slicec('/', 1);
  96. add_theme_font_size_override(dname, p_value);
  97. } else if (name.begins_with("theme_override_colors/")) {
  98. String dname = name.get_slicec('/', 1);
  99. add_theme_color_override(dname, p_value);
  100. } else if (name.begins_with("theme_override_constants/")) {
  101. String dname = name.get_slicec('/', 1);
  102. add_theme_constant_override(dname, p_value);
  103. } else {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. bool Window::_get(const StringName &p_name, Variant &r_ret) const {
  110. ERR_READ_THREAD_GUARD_V(false);
  111. String sname = p_name;
  112. if (!sname.begins_with("theme_override")) {
  113. return false;
  114. }
  115. if (sname.begins_with("theme_override_icons/")) {
  116. String name = sname.get_slicec('/', 1);
  117. r_ret = theme_icon_override.has(name) ? Variant(theme_icon_override[name]) : Variant();
  118. } else if (sname.begins_with("theme_override_styles/")) {
  119. String name = sname.get_slicec('/', 1);
  120. r_ret = theme_style_override.has(name) ? Variant(theme_style_override[name]) : Variant();
  121. } else if (sname.begins_with("theme_override_fonts/")) {
  122. String name = sname.get_slicec('/', 1);
  123. r_ret = theme_font_override.has(name) ? Variant(theme_font_override[name]) : Variant();
  124. } else if (sname.begins_with("theme_override_font_sizes/")) {
  125. String name = sname.get_slicec('/', 1);
  126. r_ret = theme_font_size_override.has(name) ? Variant(theme_font_size_override[name]) : Variant();
  127. } else if (sname.begins_with("theme_override_colors/")) {
  128. String name = sname.get_slicec('/', 1);
  129. r_ret = theme_color_override.has(name) ? Variant(theme_color_override[name]) : Variant();
  130. } else if (sname.begins_with("theme_override_constants/")) {
  131. String name = sname.get_slicec('/', 1);
  132. r_ret = theme_constant_override.has(name) ? Variant(theme_constant_override[name]) : Variant();
  133. } else {
  134. return false;
  135. }
  136. return true;
  137. }
  138. void Window::_get_property_list(List<PropertyInfo> *p_list) const {
  139. ERR_READ_THREAD_GUARD;
  140. Ref<Theme> default_theme = ThemeDB::get_singleton()->get_default_theme();
  141. p_list->push_back(PropertyInfo(Variant::NIL, GNAME("Theme Overrides", "theme_override_"), PROPERTY_HINT_NONE, "theme_override_", PROPERTY_USAGE_GROUP));
  142. {
  143. List<StringName> names;
  144. default_theme->get_color_list(get_class_name(), &names);
  145. for (const StringName &E : names) {
  146. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  147. if (theme_color_override.has(E)) {
  148. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  149. }
  150. p_list->push_back(PropertyInfo(Variant::COLOR, PNAME("theme_override_colors") + String("/") + E, PROPERTY_HINT_NONE, "", usage));
  151. }
  152. }
  153. {
  154. List<StringName> names;
  155. default_theme->get_constant_list(get_class_name(), &names);
  156. for (const StringName &E : names) {
  157. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  158. if (theme_constant_override.has(E)) {
  159. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  160. }
  161. p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_constants") + String("/") + E, PROPERTY_HINT_RANGE, "-16384,16384", usage));
  162. }
  163. }
  164. {
  165. List<StringName> names;
  166. default_theme->get_font_list(get_class_name(), &names);
  167. for (const StringName &E : names) {
  168. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  169. if (theme_font_override.has(E)) {
  170. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  171. }
  172. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_fonts") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Font", usage));
  173. }
  174. }
  175. {
  176. List<StringName> names;
  177. default_theme->get_font_size_list(get_class_name(), &names);
  178. for (const StringName &E : names) {
  179. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  180. if (theme_font_size_override.has(E)) {
  181. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  182. }
  183. p_list->push_back(PropertyInfo(Variant::INT, PNAME("theme_override_font_sizes") + String("/") + E, PROPERTY_HINT_RANGE, "1,256,1,or_greater,suffix:px", usage));
  184. }
  185. }
  186. {
  187. List<StringName> names;
  188. default_theme->get_icon_list(get_class_name(), &names);
  189. for (const StringName &E : names) {
  190. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  191. if (theme_icon_override.has(E)) {
  192. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  193. }
  194. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_icons") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", usage));
  195. }
  196. }
  197. {
  198. List<StringName> names;
  199. default_theme->get_stylebox_list(get_class_name(), &names);
  200. for (const StringName &E : names) {
  201. uint32_t usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CHECKABLE;
  202. if (theme_style_override.has(E)) {
  203. usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED;
  204. }
  205. p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("theme_override_styles") + String("/") + E, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", usage));
  206. }
  207. }
  208. }
  209. void Window::_validate_property(PropertyInfo &p_property) const {
  210. if (p_property.name == "position" && initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
  211. p_property.usage = PROPERTY_USAGE_NONE;
  212. }
  213. if (p_property.name == "current_screen" && initial_position != WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
  214. p_property.usage = PROPERTY_USAGE_NONE;
  215. }
  216. if (p_property.name == "theme_type_variation") {
  217. List<StringName> names;
  218. // Only the default theme and the project theme are used for the list of options.
  219. // This is an imposed limitation to simplify the logic needed to leverage those options.
  220. ThemeDB::get_singleton()->get_default_theme()->get_type_variation_list(get_class_name(), &names);
  221. if (ThemeDB::get_singleton()->get_project_theme().is_valid()) {
  222. ThemeDB::get_singleton()->get_project_theme()->get_type_variation_list(get_class_name(), &names);
  223. }
  224. names.sort_custom<StringName::AlphCompare>();
  225. Vector<StringName> unique_names;
  226. String hint_string;
  227. for (const StringName &E : names) {
  228. // Skip duplicate values.
  229. if (unique_names.has(E)) {
  230. continue;
  231. }
  232. hint_string += String(E) + ",";
  233. unique_names.append(E);
  234. }
  235. p_property.hint_string = hint_string;
  236. }
  237. }
  238. //
  239. void Window::set_title(const String &p_title) {
  240. ERR_MAIN_THREAD_GUARD;
  241. title = p_title;
  242. if (embedder) {
  243. embedder->_sub_window_update(this);
  244. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  245. String tr_title = atr(p_title);
  246. #ifdef DEBUG_ENABLED
  247. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  248. // Append a suffix to the window title to denote that the project is running
  249. // from a debug build (including the editor). Since this results in lower performance,
  250. // this should be clearly presented to the user.
  251. tr_title = vformat("%s (DEBUG)", tr_title);
  252. }
  253. #endif
  254. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  255. }
  256. }
  257. String Window::get_title() const {
  258. ERR_READ_THREAD_GUARD_V(String());
  259. return title;
  260. }
  261. void Window::set_initial_position(Window::WindowInitialPosition p_initial_position) {
  262. ERR_MAIN_THREAD_GUARD;
  263. initial_position = p_initial_position;
  264. notify_property_list_changed();
  265. }
  266. Window::WindowInitialPosition Window::get_initial_position() const {
  267. ERR_READ_THREAD_GUARD_V(WINDOW_INITIAL_POSITION_ABSOLUTE);
  268. return initial_position;
  269. }
  270. void Window::set_current_screen(int p_screen) {
  271. ERR_MAIN_THREAD_GUARD;
  272. current_screen = p_screen;
  273. if (window_id == DisplayServer::INVALID_WINDOW_ID) {
  274. return;
  275. }
  276. DisplayServer::get_singleton()->window_set_current_screen(p_screen, window_id);
  277. }
  278. int Window::get_current_screen() const {
  279. ERR_READ_THREAD_GUARD_V(0);
  280. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  281. current_screen = DisplayServer::get_singleton()->window_get_current_screen(window_id);
  282. }
  283. return current_screen;
  284. }
  285. void Window::set_position(const Point2i &p_position) {
  286. ERR_MAIN_THREAD_GUARD;
  287. position = p_position;
  288. if (embedder) {
  289. embedder->_sub_window_update(this);
  290. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  291. DisplayServer::get_singleton()->window_set_position(p_position, window_id);
  292. }
  293. }
  294. Point2i Window::get_position() const {
  295. ERR_READ_THREAD_GUARD_V(Point2i());
  296. return position;
  297. }
  298. void Window::move_to_center() {
  299. ERR_MAIN_THREAD_GUARD;
  300. ERR_FAIL_COND(!is_inside_tree());
  301. Rect2 parent_rect;
  302. if (is_embedded()) {
  303. parent_rect = get_embedder()->get_visible_rect();
  304. } else {
  305. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
  306. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  307. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  308. }
  309. if (parent_rect != Rect2()) {
  310. set_position(parent_rect.position + (parent_rect.size - get_size()) / 2);
  311. }
  312. }
  313. void Window::set_size(const Size2i &p_size) {
  314. ERR_MAIN_THREAD_GUARD;
  315. size = p_size;
  316. _update_window_size();
  317. }
  318. Size2i Window::get_size() const {
  319. ERR_READ_THREAD_GUARD_V(Size2i());
  320. return size;
  321. }
  322. void Window::reset_size() {
  323. ERR_MAIN_THREAD_GUARD;
  324. set_size(Size2i());
  325. }
  326. Point2i Window::get_position_with_decorations() const {
  327. ERR_READ_THREAD_GUARD_V(Point2i());
  328. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  329. return DisplayServer::get_singleton()->window_get_position_with_decorations(window_id);
  330. }
  331. return position;
  332. }
  333. Size2i Window::get_size_with_decorations() const {
  334. ERR_READ_THREAD_GUARD_V(Size2i());
  335. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  336. return DisplayServer::get_singleton()->window_get_size_with_decorations(window_id);
  337. }
  338. return size;
  339. }
  340. Size2i Window::_clamp_limit_size(const Size2i &p_limit_size) {
  341. // Force window limits to respect size limitations of rendering server.
  342. Size2i max_window_size = RS::get_singleton()->get_maximum_viewport_size();
  343. if (max_window_size != Size2i()) {
  344. return p_limit_size.clamp(Vector2i(), max_window_size);
  345. } else {
  346. return p_limit_size.max(Vector2i());
  347. }
  348. }
  349. void Window::_validate_limit_size() {
  350. // When max_size is invalid, max_size_used falls back to respect size limitations of rendering server.
  351. bool max_size_valid = (max_size.x > 0 || max_size.y > 0) && max_size.x >= min_size.x && max_size.y >= min_size.y;
  352. max_size_used = max_size_valid ? max_size : RS::get_singleton()->get_maximum_viewport_size();
  353. }
  354. void Window::set_max_size(const Size2i &p_max_size) {
  355. ERR_MAIN_THREAD_GUARD;
  356. Size2i max_size_clamped = _clamp_limit_size(p_max_size);
  357. if (max_size == max_size_clamped) {
  358. return;
  359. }
  360. max_size = max_size_clamped;
  361. _validate_limit_size();
  362. _update_window_size();
  363. }
  364. Size2i Window::get_max_size() const {
  365. ERR_READ_THREAD_GUARD_V(Size2i());
  366. return max_size;
  367. }
  368. void Window::set_min_size(const Size2i &p_min_size) {
  369. ERR_MAIN_THREAD_GUARD;
  370. Size2i min_size_clamped = _clamp_limit_size(p_min_size);
  371. if (min_size == min_size_clamped) {
  372. return;
  373. }
  374. min_size = min_size_clamped;
  375. _validate_limit_size();
  376. _update_window_size();
  377. }
  378. Size2i Window::get_min_size() const {
  379. ERR_READ_THREAD_GUARD_V(Size2i());
  380. return min_size;
  381. }
  382. void Window::set_mode(Mode p_mode) {
  383. ERR_MAIN_THREAD_GUARD;
  384. mode = p_mode;
  385. if (embedder) {
  386. embedder->_sub_window_update(this);
  387. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  388. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WindowMode(p_mode), window_id);
  389. }
  390. }
  391. Window::Mode Window::get_mode() const {
  392. ERR_READ_THREAD_GUARD_V(MODE_WINDOWED);
  393. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  394. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  395. }
  396. return mode;
  397. }
  398. void Window::set_flag(Flags p_flag, bool p_enabled) {
  399. ERR_MAIN_THREAD_GUARD;
  400. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  401. flags[p_flag] = p_enabled;
  402. if (embedder) {
  403. embedder->_sub_window_update(this);
  404. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  405. if (!is_in_edited_scene_root()) {
  406. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WindowFlags(p_flag), p_enabled, window_id);
  407. }
  408. }
  409. }
  410. bool Window::get_flag(Flags p_flag) const {
  411. ERR_READ_THREAD_GUARD_V(false);
  412. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  413. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  414. if (!is_in_edited_scene_root()) {
  415. flags[p_flag] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(p_flag), window_id);
  416. }
  417. }
  418. return flags[p_flag];
  419. }
  420. bool Window::is_maximize_allowed() const {
  421. ERR_READ_THREAD_GUARD_V(false);
  422. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  423. return DisplayServer::get_singleton()->window_is_maximize_allowed(window_id);
  424. }
  425. return true;
  426. }
  427. void Window::request_attention() {
  428. ERR_MAIN_THREAD_GUARD;
  429. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  430. DisplayServer::get_singleton()->window_request_attention(window_id);
  431. }
  432. }
  433. void Window::move_to_foreground() {
  434. ERR_MAIN_THREAD_GUARD;
  435. if (embedder) {
  436. embedder->_sub_window_grab_focus(this);
  437. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  438. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  439. }
  440. }
  441. bool Window::can_draw() const {
  442. ERR_READ_THREAD_GUARD_V(false);
  443. if (!is_inside_tree()) {
  444. return false;
  445. }
  446. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  447. return DisplayServer::get_singleton()->window_can_draw(window_id);
  448. }
  449. return visible;
  450. }
  451. void Window::set_ime_active(bool p_active) {
  452. ERR_MAIN_THREAD_GUARD;
  453. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  454. DisplayServer::get_singleton()->window_set_ime_active(p_active, window_id);
  455. }
  456. }
  457. void Window::set_ime_position(const Point2i &p_pos) {
  458. ERR_MAIN_THREAD_GUARD;
  459. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  460. DisplayServer::get_singleton()->window_set_ime_position(p_pos, window_id);
  461. }
  462. }
  463. bool Window::is_embedded() const {
  464. ERR_READ_THREAD_GUARD_V(false);
  465. return get_embedder() != nullptr;
  466. }
  467. bool Window::is_in_edited_scene_root() const {
  468. ERR_READ_THREAD_GUARD_V(false);
  469. #ifdef TOOLS_ENABLED
  470. return is_part_of_edited_scene();
  471. #else
  472. return false;
  473. #endif
  474. }
  475. void Window::_make_window() {
  476. ERR_FAIL_COND(window_id != DisplayServer::INVALID_WINDOW_ID);
  477. uint32_t f = 0;
  478. for (int i = 0; i < FLAG_MAX; i++) {
  479. if (flags[i]) {
  480. f |= (1 << i);
  481. }
  482. }
  483. DisplayServer::VSyncMode vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
  484. Rect2i window_rect;
  485. if (initial_position == WINDOW_INITIAL_POSITION_ABSOLUTE) {
  486. window_rect = Rect2i(position, size);
  487. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN) {
  488. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_PRIMARY) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_PRIMARY) - size) / 2, size);
  489. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN) {
  490. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_OF_MAIN_WINDOW) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_OF_MAIN_WINDOW) - size) / 2, size);
  491. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN) {
  492. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(current_screen) + (DisplayServer::get_singleton()->screen_get_size(current_screen) - size) / 2, size);
  493. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS) {
  494. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_MOUSE_FOCUS) - size) / 2, size);
  495. } else if (initial_position == WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS) {
  496. window_rect = Rect2i(DisplayServer::get_singleton()->screen_get_position(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) + (DisplayServer::get_singleton()->screen_get_size(DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS) - size) / 2, size);
  497. }
  498. window_id = DisplayServer::get_singleton()->create_sub_window(DisplayServer::WindowMode(mode), vsync_mode, f, window_rect);
  499. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  500. DisplayServer::get_singleton()->window_set_max_size(Size2i(), window_id);
  501. DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
  502. DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
  503. String tr_title = atr(title);
  504. #ifdef DEBUG_ENABLED
  505. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  506. // Append a suffix to the window title to denote that the project is running
  507. // from a debug build (including the editor). Since this results in lower performance,
  508. // this should be clearly presented to the user.
  509. tr_title = vformat("%s (DEBUG)", tr_title);
  510. }
  511. #endif
  512. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  513. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  514. if (is_in_edited_scene_root()) {
  515. DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
  516. } else {
  517. DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
  518. }
  519. _update_window_size();
  520. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  521. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  522. }
  523. if (transient_parent) {
  524. for (const Window *E : transient_children) {
  525. if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
  526. DisplayServer::get_singleton()->window_set_transient(E->window_id, transient_parent->window_id);
  527. }
  528. }
  529. }
  530. _update_window_callbacks();
  531. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  532. DisplayServer::get_singleton()->show_window(window_id);
  533. }
  534. void Window::_update_from_window() {
  535. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  536. mode = (Mode)DisplayServer::get_singleton()->window_get_mode(window_id);
  537. for (int i = 0; i < FLAG_MAX; i++) {
  538. flags[i] = DisplayServer::get_singleton()->window_get_flag(DisplayServer::WindowFlags(i), window_id);
  539. }
  540. }
  541. void Window::_clear_window() {
  542. ERR_FAIL_COND(window_id == DisplayServer::INVALID_WINDOW_ID);
  543. bool had_focus = has_focus();
  544. if (transient_parent && transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID) {
  545. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  546. }
  547. for (const Window *E : transient_children) {
  548. if (E->window_id != DisplayServer::INVALID_WINDOW_ID) {
  549. DisplayServer::get_singleton()->window_set_transient(E->window_id, DisplayServer::INVALID_WINDOW_ID);
  550. }
  551. }
  552. _update_from_window();
  553. DisplayServer::get_singleton()->delete_sub_window(window_id);
  554. window_id = DisplayServer::INVALID_WINDOW_ID;
  555. // If closing window was focused and has a parent, return focus.
  556. if (had_focus && transient_parent) {
  557. transient_parent->grab_focus();
  558. }
  559. _update_viewport_size();
  560. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  561. }
  562. void Window::_rect_changed_callback(const Rect2i &p_callback) {
  563. //we must always accept this as the truth
  564. if (size == p_callback.size && position == p_callback.position) {
  565. return;
  566. }
  567. position = p_callback.position;
  568. if (size != p_callback.size) {
  569. size = p_callback.size;
  570. _update_viewport_size();
  571. }
  572. }
  573. void Window::_propagate_window_notification(Node *p_node, int p_notification) {
  574. p_node->notification(p_notification);
  575. for (int i = 0; i < p_node->get_child_count(); i++) {
  576. Node *child = p_node->get_child(i);
  577. Window *window = Object::cast_to<Window>(child);
  578. if (window) {
  579. continue;
  580. }
  581. _propagate_window_notification(child, p_notification);
  582. }
  583. }
  584. void Window::_event_callback(DisplayServer::WindowEvent p_event) {
  585. switch (p_event) {
  586. case DisplayServer::WINDOW_EVENT_MOUSE_ENTER: {
  587. Window *root = get_tree()->get_root();
  588. if (root->gui.windowmanager_window_over) {
  589. #ifdef DEV_ENABLED
  590. WARN_PRINT_ONCE("Entering a window while a window is hovered should never happen in DisplayServer.");
  591. #endif // DEV_ENABLED
  592. root->gui.windowmanager_window_over->_event_callback(DisplayServer::WINDOW_EVENT_MOUSE_EXIT);
  593. }
  594. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
  595. root->gui.windowmanager_window_over = this;
  596. mouse_in_window = true;
  597. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
  598. DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CURSOR_ARROW); //restore cursor shape
  599. }
  600. } break;
  601. case DisplayServer::WINDOW_EVENT_MOUSE_EXIT: {
  602. Window *root = get_tree()->get_root();
  603. if (!root->gui.windowmanager_window_over) {
  604. #ifdef DEV_ENABLED
  605. WARN_PRINT_ONCE("Exiting a window while no window is hovered should never happen in DisplayServer.");
  606. #endif // DEV_ENABLED
  607. return;
  608. }
  609. mouse_in_window = false;
  610. root->gui.windowmanager_window_over->_mouse_leave_viewport();
  611. root->gui.windowmanager_window_over = nullptr;
  612. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
  613. } break;
  614. case DisplayServer::WINDOW_EVENT_FOCUS_IN: {
  615. focused = true;
  616. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_IN);
  617. emit_signal(SNAME("focus_entered"));
  618. } break;
  619. case DisplayServer::WINDOW_EVENT_FOCUS_OUT: {
  620. focused = false;
  621. _propagate_window_notification(this, NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  622. emit_signal(SNAME("focus_exited"));
  623. } break;
  624. case DisplayServer::WINDOW_EVENT_CLOSE_REQUEST: {
  625. if (exclusive_child != nullptr) {
  626. break; //has an exclusive child, can't get events until child is closed
  627. }
  628. _propagate_window_notification(this, NOTIFICATION_WM_CLOSE_REQUEST);
  629. emit_signal(SNAME("close_requested"));
  630. } break;
  631. case DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST: {
  632. _propagate_window_notification(this, NOTIFICATION_WM_GO_BACK_REQUEST);
  633. emit_signal(SNAME("go_back_requested"));
  634. } break;
  635. case DisplayServer::WINDOW_EVENT_DPI_CHANGE: {
  636. _update_viewport_size();
  637. _propagate_window_notification(this, NOTIFICATION_WM_DPI_CHANGE);
  638. emit_signal(SNAME("dpi_changed"));
  639. } break;
  640. case DisplayServer::WINDOW_EVENT_TITLEBAR_CHANGE: {
  641. emit_signal(SNAME("titlebar_changed"));
  642. } break;
  643. }
  644. }
  645. void Window::update_mouse_cursor_state() {
  646. ERR_MAIN_THREAD_GUARD;
  647. // Update states based on mouse cursor position.
  648. // This includes updated mouse_enter or mouse_exit signals or the current mouse cursor shape.
  649. // These details are set in Viewport::_gui_input_event. To instantly
  650. // see the changes in the viewport, we need to trigger a mouse motion event.
  651. // This function should be called whenever scene tree changes affect the mouse cursor.
  652. Ref<InputEventMouseMotion> mm;
  653. Vector2 pos = get_mouse_position();
  654. Transform2D xform = get_global_canvas_transform().affine_inverse();
  655. mm.instantiate();
  656. mm->set_position(pos);
  657. mm->set_global_position(xform.xform(pos));
  658. mm->set_device(InputEvent::DEVICE_ID_INTERNAL);
  659. push_input(mm);
  660. }
  661. void Window::show() {
  662. ERR_MAIN_THREAD_GUARD;
  663. set_visible(true);
  664. }
  665. void Window::hide() {
  666. ERR_MAIN_THREAD_GUARD;
  667. set_visible(false);
  668. }
  669. void Window::set_visible(bool p_visible) {
  670. ERR_MAIN_THREAD_GUARD;
  671. if (visible == p_visible) {
  672. return;
  673. }
  674. if (!is_inside_tree()) {
  675. visible = p_visible;
  676. return;
  677. }
  678. ERR_FAIL_COND_MSG(get_parent() == nullptr, "Can't change visibility of main window.");
  679. visible = p_visible;
  680. // Stop any queued resizing, as the window will be resized right now.
  681. updating_child_controls = false;
  682. Viewport *embedder_vp = get_embedder();
  683. if (!embedder_vp) {
  684. if (!p_visible && window_id != DisplayServer::INVALID_WINDOW_ID) {
  685. _clear_window();
  686. }
  687. if (p_visible && window_id == DisplayServer::INVALID_WINDOW_ID) {
  688. _make_window();
  689. }
  690. } else {
  691. if (visible) {
  692. embedder = embedder_vp;
  693. if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
  694. position = (embedder->get_visible_rect().size - size) / 2;
  695. }
  696. embedder->_sub_window_register(this);
  697. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  698. } else {
  699. embedder->_sub_window_remove(this);
  700. embedder = nullptr;
  701. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  702. }
  703. _update_window_size();
  704. }
  705. if (!visible) {
  706. focused = false;
  707. }
  708. notification(NOTIFICATION_VISIBILITY_CHANGED);
  709. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  710. RS::get_singleton()->viewport_set_active(get_viewport_rid(), visible);
  711. //update transient exclusive
  712. if (transient_parent) {
  713. if (exclusive && visible) {
  714. if (!is_in_edited_scene_root()) {
  715. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  716. transient_parent->exclusive_child = this;
  717. }
  718. } else {
  719. if (transient_parent->exclusive_child == this) {
  720. transient_parent->exclusive_child = nullptr;
  721. }
  722. }
  723. }
  724. }
  725. void Window::_clear_transient() {
  726. if (transient_parent) {
  727. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  728. DisplayServer::get_singleton()->window_set_transient(window_id, DisplayServer::INVALID_WINDOW_ID);
  729. }
  730. transient_parent->transient_children.erase(this);
  731. if (transient_parent->exclusive_child == this) {
  732. transient_parent->exclusive_child = nullptr;
  733. }
  734. transient_parent = nullptr;
  735. }
  736. }
  737. void Window::_make_transient() {
  738. if (!get_parent()) {
  739. //main window, can't be transient
  740. return;
  741. }
  742. //find transient parent
  743. Viewport *vp = get_parent()->get_viewport();
  744. Window *window = nullptr;
  745. while (vp) {
  746. window = Object::cast_to<Window>(vp);
  747. if (window) {
  748. break;
  749. }
  750. if (!vp->get_parent()) {
  751. break;
  752. }
  753. vp = vp->get_parent()->get_viewport();
  754. }
  755. if (window) {
  756. transient_parent = window;
  757. window->transient_children.insert(this);
  758. if (is_inside_tree() && is_visible() && exclusive) {
  759. if (transient_parent->exclusive_child == nullptr) {
  760. if (!is_in_edited_scene_root()) {
  761. transient_parent->exclusive_child = this;
  762. }
  763. } else if (transient_parent->exclusive_child != this) {
  764. ERR_PRINT("Making child transient exclusive, but parent has another exclusive child");
  765. }
  766. }
  767. }
  768. //see if we can make transient
  769. if (transient_parent->window_id != DisplayServer::INVALID_WINDOW_ID && window_id != DisplayServer::INVALID_WINDOW_ID) {
  770. DisplayServer::get_singleton()->window_set_transient(window_id, transient_parent->window_id);
  771. }
  772. }
  773. void Window::set_transient(bool p_transient) {
  774. ERR_MAIN_THREAD_GUARD;
  775. if (transient == p_transient) {
  776. return;
  777. }
  778. transient = p_transient;
  779. if (!is_inside_tree()) {
  780. return;
  781. }
  782. if (transient) {
  783. _make_transient();
  784. } else {
  785. _clear_transient();
  786. }
  787. }
  788. bool Window::is_transient() const {
  789. ERR_READ_THREAD_GUARD_V(false);
  790. return transient;
  791. }
  792. void Window::set_exclusive(bool p_exclusive) {
  793. ERR_MAIN_THREAD_GUARD;
  794. if (exclusive == p_exclusive) {
  795. return;
  796. }
  797. exclusive = p_exclusive;
  798. if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
  799. if (is_in_edited_scene_root()) {
  800. DisplayServer::get_singleton()->window_set_exclusive(window_id, false);
  801. } else {
  802. DisplayServer::get_singleton()->window_set_exclusive(window_id, exclusive);
  803. }
  804. }
  805. if (transient_parent) {
  806. if (p_exclusive && is_inside_tree() && is_visible()) {
  807. ERR_FAIL_COND_MSG(transient_parent->exclusive_child && transient_parent->exclusive_child != this, "Transient parent has another exclusive child.");
  808. if (!is_in_edited_scene_root()) {
  809. transient_parent->exclusive_child = this;
  810. }
  811. } else {
  812. if (transient_parent->exclusive_child == this) {
  813. transient_parent->exclusive_child = nullptr;
  814. }
  815. }
  816. }
  817. }
  818. bool Window::is_exclusive() const {
  819. ERR_READ_THREAD_GUARD_V(false);
  820. return exclusive;
  821. }
  822. bool Window::is_visible() const {
  823. ERR_READ_THREAD_GUARD_V(false);
  824. return visible;
  825. }
  826. Size2i Window::_clamp_window_size(const Size2i &p_size) {
  827. Size2i window_size_clamped = p_size;
  828. Size2 minsize = get_clamped_minimum_size();
  829. window_size_clamped = window_size_clamped.max(minsize);
  830. if (max_size_used != Size2i()) {
  831. window_size_clamped = window_size_clamped.min(max_size_used);
  832. }
  833. return window_size_clamped;
  834. }
  835. void Window::_update_window_size() {
  836. Size2i size_limit = get_clamped_minimum_size();
  837. size = size.max(size_limit);
  838. bool reset_min_first = false;
  839. if (max_size_used != Size2i()) {
  840. // Force window size to respect size limitations of max_size_used.
  841. size = size.min(max_size_used);
  842. if (size_limit.x > max_size_used.x) {
  843. size_limit.x = max_size_used.x;
  844. reset_min_first = true;
  845. }
  846. if (size_limit.y > max_size_used.y) {
  847. size_limit.y = max_size_used.y;
  848. reset_min_first = true;
  849. }
  850. }
  851. if (embedder) {
  852. size.x = MAX(size.x, 1);
  853. size.y = MAX(size.y, 1);
  854. embedder->_sub_window_update(this);
  855. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  856. if (reset_min_first && wrap_controls) {
  857. // Avoid an error if setting max_size to a value between min_size and the previous size_limit.
  858. DisplayServer::get_singleton()->window_set_min_size(Size2i(), window_id);
  859. }
  860. DisplayServer::get_singleton()->window_set_max_size(max_size_used, window_id);
  861. DisplayServer::get_singleton()->window_set_min_size(size_limit, window_id);
  862. DisplayServer::get_singleton()->window_set_size(size, window_id);
  863. }
  864. //update the viewport
  865. _update_viewport_size();
  866. }
  867. void Window::_update_viewport_size() {
  868. //update the viewport part
  869. Size2i final_size;
  870. Size2i final_size_override;
  871. Rect2i attach_to_screen_rect(Point2i(), size);
  872. float font_oversampling = 1.0;
  873. window_transform = Transform2D();
  874. if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
  875. // We always want to make sure that the content scale factor is a whole
  876. // number, else there will be pixel wobble no matter what.
  877. content_scale_factor = Math::floor(content_scale_factor);
  878. // A content scale factor of zero is pretty useless.
  879. if (content_scale_factor < 1) {
  880. content_scale_factor = 1;
  881. }
  882. }
  883. if (content_scale_mode == CONTENT_SCALE_MODE_DISABLED || content_scale_size.x == 0 || content_scale_size.y == 0) {
  884. font_oversampling = content_scale_factor;
  885. final_size = size;
  886. final_size_override = Size2(size) / content_scale_factor;
  887. } else {
  888. //actual screen video mode
  889. Size2 video_mode = size;
  890. Size2 desired_res = content_scale_size;
  891. Size2 viewport_size;
  892. Size2 screen_size;
  893. float viewport_aspect = desired_res.aspect();
  894. float video_mode_aspect = video_mode.aspect();
  895. if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
  896. //same aspect or ignore aspect
  897. viewport_size = desired_res;
  898. screen_size = video_mode;
  899. } else if (viewport_aspect < video_mode_aspect) {
  900. // screen ratio is smaller vertically
  901. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_HEIGHT || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  902. //will stretch horizontally
  903. viewport_size.x = desired_res.y * video_mode_aspect;
  904. viewport_size.y = desired_res.y;
  905. screen_size = video_mode;
  906. } else {
  907. //will need black bars
  908. viewport_size = desired_res;
  909. screen_size.x = video_mode.y * viewport_aspect;
  910. screen_size.y = video_mode.y;
  911. }
  912. } else {
  913. //screen ratio is smaller horizontally
  914. if (content_scale_aspect == CONTENT_SCALE_ASPECT_KEEP_WIDTH || content_scale_aspect == CONTENT_SCALE_ASPECT_EXPAND) {
  915. //will stretch horizontally
  916. viewport_size.x = desired_res.x;
  917. viewport_size.y = desired_res.x / video_mode_aspect;
  918. screen_size = video_mode;
  919. } else {
  920. //will need black bars
  921. viewport_size = desired_res;
  922. screen_size.x = video_mode.x;
  923. screen_size.y = video_mode.x / viewport_aspect;
  924. }
  925. }
  926. screen_size = screen_size.floor();
  927. viewport_size = viewport_size.floor();
  928. if (content_scale_stretch == Window::CONTENT_SCALE_STRETCH_INTEGER) {
  929. Size2i screen_scale = (screen_size / viewport_size).floor();
  930. int scale_factor = MIN(screen_scale.x, screen_scale.y);
  931. if (scale_factor < 1) {
  932. scale_factor = 1;
  933. }
  934. screen_size = viewport_size * scale_factor;
  935. }
  936. Size2 margin;
  937. Size2 offset;
  938. if (screen_size.x < video_mode.x) {
  939. margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
  940. offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
  941. }
  942. if (screen_size.y < video_mode.y) {
  943. margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
  944. offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
  945. }
  946. switch (content_scale_mode) {
  947. case CONTENT_SCALE_MODE_DISABLED: {
  948. // Already handled above
  949. //_update_font_oversampling(1.0);
  950. } break;
  951. case CONTENT_SCALE_MODE_CANVAS_ITEMS: {
  952. final_size = screen_size;
  953. final_size_override = viewport_size / content_scale_factor;
  954. attach_to_screen_rect = Rect2(margin, screen_size);
  955. font_oversampling = (screen_size.x / viewport_size.x) * content_scale_factor;
  956. window_transform.translate_local(margin);
  957. } break;
  958. case CONTENT_SCALE_MODE_VIEWPORT: {
  959. final_size = (viewport_size / content_scale_factor).floor();
  960. attach_to_screen_rect = Rect2(margin, screen_size);
  961. window_transform.translate_local(margin);
  962. if (final_size.x != 0 && final_size.y != 0) {
  963. Transform2D scale_transform;
  964. scale_transform.scale(Vector2(attach_to_screen_rect.size) / Vector2(final_size));
  965. window_transform *= scale_transform;
  966. }
  967. } break;
  968. }
  969. }
  970. bool allocate = is_inside_tree() && visible && (window_id != DisplayServer::INVALID_WINDOW_ID || embedder != nullptr);
  971. _set_size(final_size, final_size_override, allocate);
  972. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  973. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), attach_to_screen_rect, window_id);
  974. } else {
  975. RenderingServer::get_singleton()->viewport_attach_to_screen(get_viewport_rid(), Rect2i(), DisplayServer::INVALID_WINDOW_ID);
  976. }
  977. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  978. if (!use_font_oversampling) {
  979. font_oversampling = 1.0;
  980. }
  981. if (TS->font_get_global_oversampling() != font_oversampling) {
  982. TS->font_set_global_oversampling(font_oversampling);
  983. }
  984. }
  985. notification(NOTIFICATION_WM_SIZE_CHANGED);
  986. if (embedder) {
  987. embedder->_sub_window_update(this);
  988. }
  989. }
  990. void Window::_update_window_callbacks() {
  991. DisplayServer::get_singleton()->window_set_rect_changed_callback(callable_mp(this, &Window::_rect_changed_callback), window_id);
  992. DisplayServer::get_singleton()->window_set_window_event_callback(callable_mp(this, &Window::_event_callback), window_id);
  993. DisplayServer::get_singleton()->window_set_input_event_callback(callable_mp(this, &Window::_window_input), window_id);
  994. DisplayServer::get_singleton()->window_set_input_text_callback(callable_mp(this, &Window::_window_input_text), window_id);
  995. DisplayServer::get_singleton()->window_set_drop_files_callback(callable_mp(this, &Window::_window_drop_files), window_id);
  996. }
  997. Viewport *Window::get_embedder() const {
  998. ERR_READ_THREAD_GUARD_V(nullptr);
  999. Viewport *vp = get_parent_viewport();
  1000. while (vp) {
  1001. if (vp->is_embedding_subwindows()) {
  1002. return vp;
  1003. }
  1004. if (vp->get_parent()) {
  1005. vp = vp->get_parent()->get_viewport();
  1006. } else {
  1007. vp = nullptr;
  1008. }
  1009. }
  1010. return nullptr;
  1011. }
  1012. void Window::_notification(int p_what) {
  1013. ERR_MAIN_THREAD_GUARD;
  1014. switch (p_what) {
  1015. case NOTIFICATION_POSTINITIALIZE: {
  1016. initialized = true;
  1017. _invalidate_theme_cache();
  1018. _update_theme_item_cache();
  1019. } break;
  1020. case NOTIFICATION_PARENTED: {
  1021. theme_owner->assign_theme_on_parented(this);
  1022. } break;
  1023. case NOTIFICATION_UNPARENTED: {
  1024. theme_owner->clear_theme_on_unparented(this);
  1025. } break;
  1026. case NOTIFICATION_ENTER_TREE: {
  1027. bool embedded = false;
  1028. {
  1029. embedder = get_embedder();
  1030. if (embedder) {
  1031. embedded = true;
  1032. if (!visible) {
  1033. embedder = nullptr; // Not yet since not visible.
  1034. }
  1035. }
  1036. }
  1037. if (embedded) {
  1038. // Create as embedded.
  1039. if (embedder) {
  1040. if (initial_position != WINDOW_INITIAL_POSITION_ABSOLUTE) {
  1041. position = (embedder->get_visible_rect().size - size) / 2;
  1042. }
  1043. embedder->_sub_window_register(this);
  1044. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_PARENT_VISIBLE);
  1045. _update_window_size();
  1046. }
  1047. } else {
  1048. if (!get_parent()) {
  1049. // It's the root window!
  1050. visible = true; // Always visible.
  1051. window_id = DisplayServer::MAIN_WINDOW_ID;
  1052. DisplayServer::get_singleton()->window_attach_instance_id(get_instance_id(), window_id);
  1053. _update_from_window();
  1054. // Since this window already exists (created on start), we must update pos and size from it.
  1055. {
  1056. position = DisplayServer::get_singleton()->window_get_position(window_id);
  1057. size = DisplayServer::get_singleton()->window_get_size(window_id);
  1058. focused = DisplayServer::get_singleton()->window_is_focused(window_id);
  1059. }
  1060. _update_window_size(); // Inform DisplayServer of minimum and maximum size.
  1061. _update_viewport_size(); // Then feed back to the viewport.
  1062. _update_window_callbacks();
  1063. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
  1064. } else {
  1065. // Create.
  1066. if (visible) {
  1067. _make_window();
  1068. }
  1069. }
  1070. }
  1071. if (transient) {
  1072. _make_transient();
  1073. }
  1074. if (visible) {
  1075. notification(NOTIFICATION_VISIBILITY_CHANGED);
  1076. emit_signal(SceneStringNames::get_singleton()->visibility_changed);
  1077. RS::get_singleton()->viewport_set_active(get_viewport_rid(), true);
  1078. }
  1079. #ifdef TOOLS_ENABLED
  1080. if (is_part_of_edited_scene()) {
  1081. // Don't translate Windows on scene when inside editor.
  1082. set_message_translation(false);
  1083. notification(NOTIFICATION_TRANSLATION_CHANGED);
  1084. }
  1085. #endif
  1086. // Emits NOTIFICATION_THEME_CHANGED internally.
  1087. set_theme_context(ThemeDB::get_singleton()->get_nearest_theme_context(this));
  1088. } break;
  1089. case NOTIFICATION_READY: {
  1090. if (wrap_controls) {
  1091. // Finish any resizing immediately so it doesn't interfere on stuff overriding _ready().
  1092. _update_child_controls();
  1093. }
  1094. } break;
  1095. case NOTIFICATION_THEME_CHANGED: {
  1096. emit_signal(SceneStringNames::get_singleton()->theme_changed);
  1097. _invalidate_theme_cache();
  1098. _update_theme_item_cache();
  1099. } break;
  1100. case NOTIFICATION_TRANSLATION_CHANGED: {
  1101. _invalidate_theme_cache();
  1102. _update_theme_item_cache();
  1103. if (!embedder && window_id != DisplayServer::INVALID_WINDOW_ID) {
  1104. String tr_title = atr(title);
  1105. #ifdef DEBUG_ENABLED
  1106. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  1107. // Append a suffix to the window title to denote that the project is running
  1108. // from a debug build (including the editor). Since this results in lower performance,
  1109. // this should be clearly presented to the user.
  1110. tr_title = vformat("%s (DEBUG)", tr_title);
  1111. }
  1112. #endif
  1113. DisplayServer::get_singleton()->window_set_title(tr_title, window_id);
  1114. }
  1115. } break;
  1116. case NOTIFICATION_VISIBILITY_CHANGED: {
  1117. if (unparent_when_invisible && !is_visible()) {
  1118. Node *p = get_parent();
  1119. if (p) {
  1120. p->remove_child(this);
  1121. }
  1122. }
  1123. } break;
  1124. case NOTIFICATION_EXIT_TREE: {
  1125. set_theme_context(nullptr, false);
  1126. if (transient) {
  1127. _clear_transient();
  1128. }
  1129. if (!is_embedded() && window_id != DisplayServer::INVALID_WINDOW_ID) {
  1130. if (window_id == DisplayServer::MAIN_WINDOW_ID) {
  1131. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  1132. _update_window_callbacks();
  1133. } else {
  1134. _clear_window();
  1135. }
  1136. } else {
  1137. if (embedder) {
  1138. embedder->_sub_window_remove(this);
  1139. embedder = nullptr;
  1140. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  1141. }
  1142. _update_viewport_size(); //called by clear and make, which does not happen here
  1143. }
  1144. RS::get_singleton()->viewport_set_active(get_viewport_rid(), false);
  1145. } break;
  1146. case NOTIFICATION_VP_MOUSE_ENTER: {
  1147. emit_signal(SceneStringNames::get_singleton()->mouse_entered);
  1148. } break;
  1149. case NOTIFICATION_VP_MOUSE_EXIT: {
  1150. emit_signal(SceneStringNames::get_singleton()->mouse_exited);
  1151. } break;
  1152. }
  1153. }
  1154. void Window::set_content_scale_size(const Size2i &p_size) {
  1155. ERR_MAIN_THREAD_GUARD;
  1156. ERR_FAIL_COND(p_size.x < 0);
  1157. ERR_FAIL_COND(p_size.y < 0);
  1158. content_scale_size = p_size;
  1159. _update_viewport_size();
  1160. }
  1161. Size2i Window::get_content_scale_size() const {
  1162. ERR_READ_THREAD_GUARD_V(Size2i());
  1163. return content_scale_size;
  1164. }
  1165. void Window::set_content_scale_mode(ContentScaleMode p_mode) {
  1166. ERR_MAIN_THREAD_GUARD;
  1167. content_scale_mode = p_mode;
  1168. _update_viewport_size();
  1169. }
  1170. Window::ContentScaleMode Window::get_content_scale_mode() const {
  1171. ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_MODE_DISABLED);
  1172. return content_scale_mode;
  1173. }
  1174. void Window::set_content_scale_aspect(ContentScaleAspect p_aspect) {
  1175. ERR_MAIN_THREAD_GUARD;
  1176. content_scale_aspect = p_aspect;
  1177. _update_viewport_size();
  1178. }
  1179. Window::ContentScaleAspect Window::get_content_scale_aspect() const {
  1180. ERR_READ_THREAD_GUARD_V(CONTENT_SCALE_ASPECT_IGNORE);
  1181. return content_scale_aspect;
  1182. }
  1183. void Window::set_content_scale_stretch(ContentScaleStretch p_stretch) {
  1184. content_scale_stretch = p_stretch;
  1185. _update_viewport_size();
  1186. }
  1187. Window::ContentScaleStretch Window::get_content_scale_stretch() const {
  1188. return content_scale_stretch;
  1189. }
  1190. void Window::set_content_scale_factor(real_t p_factor) {
  1191. ERR_MAIN_THREAD_GUARD;
  1192. ERR_FAIL_COND(p_factor <= 0);
  1193. content_scale_factor = p_factor;
  1194. _update_viewport_size();
  1195. }
  1196. real_t Window::get_content_scale_factor() const {
  1197. ERR_READ_THREAD_GUARD_V(0);
  1198. return content_scale_factor;
  1199. }
  1200. void Window::set_use_font_oversampling(bool p_oversampling) {
  1201. ERR_MAIN_THREAD_GUARD;
  1202. if (is_inside_tree() && window_id != DisplayServer::MAIN_WINDOW_ID) {
  1203. ERR_FAIL_MSG("Only the root window can set and use font oversampling.");
  1204. }
  1205. use_font_oversampling = p_oversampling;
  1206. _update_viewport_size();
  1207. }
  1208. bool Window::is_using_font_oversampling() const {
  1209. ERR_READ_THREAD_GUARD_V(false);
  1210. return use_font_oversampling;
  1211. }
  1212. DisplayServer::WindowID Window::get_window_id() const {
  1213. ERR_READ_THREAD_GUARD_V(DisplayServer::INVALID_WINDOW_ID);
  1214. if (embedder) {
  1215. return parent->get_window_id();
  1216. }
  1217. return window_id;
  1218. }
  1219. void Window::set_mouse_passthrough_polygon(const Vector<Vector2> &p_region) {
  1220. ERR_MAIN_THREAD_GUARD;
  1221. mpath = p_region;
  1222. if (window_id == DisplayServer::INVALID_WINDOW_ID) {
  1223. return;
  1224. }
  1225. DisplayServer::get_singleton()->window_set_mouse_passthrough(mpath, window_id);
  1226. }
  1227. Vector<Vector2> Window::get_mouse_passthrough_polygon() const {
  1228. return mpath;
  1229. }
  1230. void Window::set_wrap_controls(bool p_enable) {
  1231. ERR_MAIN_THREAD_GUARD;
  1232. wrap_controls = p_enable;
  1233. if (!is_inside_tree()) {
  1234. return;
  1235. }
  1236. if (updating_child_controls) {
  1237. _update_child_controls();
  1238. } else {
  1239. _update_window_size();
  1240. }
  1241. }
  1242. bool Window::is_wrapping_controls() const {
  1243. ERR_READ_THREAD_GUARD_V(false);
  1244. return wrap_controls;
  1245. }
  1246. Size2 Window::_get_contents_minimum_size() const {
  1247. Size2 max;
  1248. for (int i = 0; i < get_child_count(); i++) {
  1249. Control *c = Object::cast_to<Control>(get_child(i));
  1250. if (c) {
  1251. Point2i pos = c->get_position();
  1252. Size2i min = c->get_combined_minimum_size();
  1253. max.x = MAX(pos.x + min.x, max.x);
  1254. max.y = MAX(pos.y + min.y, max.y);
  1255. }
  1256. }
  1257. return max;
  1258. }
  1259. void Window::child_controls_changed() {
  1260. ERR_MAIN_THREAD_GUARD;
  1261. if (!is_inside_tree() || !visible || updating_child_controls) {
  1262. return;
  1263. }
  1264. updating_child_controls = true;
  1265. call_deferred(SNAME("_update_child_controls"));
  1266. }
  1267. void Window::_update_child_controls() {
  1268. if (!updating_child_controls) {
  1269. return;
  1270. }
  1271. _update_window_size();
  1272. updating_child_controls = false;
  1273. }
  1274. bool Window::_can_consume_input_events() const {
  1275. return exclusive_child == nullptr;
  1276. }
  1277. void Window::_window_input(const Ref<InputEvent> &p_ev) {
  1278. if (EngineDebugger::is_active()) {
  1279. // Quit from game window using the stop shortcut (F8 by default).
  1280. // The custom shortcut is provided via environment variable when running from the editor.
  1281. if (debugger_stop_shortcut.is_null()) {
  1282. String shortcut_str = OS::get_singleton()->get_environment("__GODOT_EDITOR_STOP_SHORTCUT__");
  1283. if (!shortcut_str.is_empty()) {
  1284. Variant shortcut_var;
  1285. VariantParser::StreamString ss;
  1286. ss.s = shortcut_str;
  1287. String errs;
  1288. int line;
  1289. VariantParser::parse(&ss, shortcut_var, errs, line);
  1290. debugger_stop_shortcut = shortcut_var;
  1291. }
  1292. if (debugger_stop_shortcut.is_null()) {
  1293. // Define a default shortcut if it wasn't provided or is invalid.
  1294. debugger_stop_shortcut.instantiate();
  1295. debugger_stop_shortcut->set_events({ (Variant)InputEventKey::create_reference(Key::F8) });
  1296. }
  1297. }
  1298. Ref<InputEventKey> k = p_ev;
  1299. if (k.is_valid() && k->is_pressed() && !k->is_echo() && debugger_stop_shortcut->matches_event(k)) {
  1300. EngineDebugger::get_singleton()->send_message("request_quit", Array());
  1301. }
  1302. }
  1303. if (exclusive_child != nullptr) {
  1304. if (!is_embedding_subwindows()) { // Not embedding, no need for event.
  1305. return;
  1306. }
  1307. }
  1308. if (p_ev->get_device() != InputEvent::DEVICE_ID_INTERNAL) {
  1309. emit_signal(SceneStringNames::get_singleton()->window_input, p_ev);
  1310. }
  1311. if (is_inside_tree()) {
  1312. push_input(p_ev);
  1313. }
  1314. }
  1315. void Window::_window_input_text(const String &p_text) {
  1316. push_text_input(p_text);
  1317. }
  1318. void Window::_window_drop_files(const Vector<String> &p_files) {
  1319. emit_signal(SNAME("files_dropped"), p_files);
  1320. }
  1321. Viewport *Window::get_parent_viewport() const {
  1322. ERR_READ_THREAD_GUARD_V(nullptr);
  1323. if (get_parent()) {
  1324. return get_parent()->get_viewport();
  1325. } else {
  1326. return nullptr;
  1327. }
  1328. }
  1329. Window *Window::get_parent_visible_window() const {
  1330. ERR_READ_THREAD_GUARD_V(nullptr);
  1331. Viewport *vp = get_parent_viewport();
  1332. Window *window = nullptr;
  1333. while (vp) {
  1334. window = Object::cast_to<Window>(vp);
  1335. if (window && window->visible) {
  1336. break;
  1337. }
  1338. if (!vp->get_parent()) {
  1339. break;
  1340. }
  1341. vp = vp->get_parent()->get_viewport();
  1342. }
  1343. return window;
  1344. }
  1345. void Window::popup_on_parent(const Rect2i &p_parent_rect) {
  1346. ERR_MAIN_THREAD_GUARD;
  1347. ERR_FAIL_COND(!is_inside_tree());
  1348. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1349. if (!is_embedded()) {
  1350. Window *window = get_parent_visible_window();
  1351. if (!window) {
  1352. popup(p_parent_rect);
  1353. } else {
  1354. popup(Rect2i(window->get_position() + p_parent_rect.position, p_parent_rect.size));
  1355. }
  1356. } else {
  1357. popup(p_parent_rect);
  1358. }
  1359. }
  1360. void Window::popup_centered_clamped(const Size2i &p_size, float p_fallback_ratio) {
  1361. ERR_MAIN_THREAD_GUARD;
  1362. ERR_FAIL_COND(!is_inside_tree());
  1363. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1364. // Consider the current size when calling with the default value.
  1365. Size2i expected_size = p_size == Size2i() ? size : p_size;
  1366. Rect2 parent_rect;
  1367. if (is_embedded()) {
  1368. parent_rect = get_embedder()->get_visible_rect();
  1369. } else {
  1370. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  1371. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  1372. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  1373. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  1374. }
  1375. Vector2i size_ratio = parent_rect.size * p_fallback_ratio;
  1376. Rect2i popup_rect;
  1377. popup_rect.size = Vector2i(MIN(size_ratio.x, expected_size.x), MIN(size_ratio.y, expected_size.y));
  1378. popup_rect.size = _clamp_window_size(popup_rect.size);
  1379. if (parent_rect != Rect2()) {
  1380. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  1381. }
  1382. popup(popup_rect);
  1383. }
  1384. void Window::popup_centered(const Size2i &p_minsize) {
  1385. ERR_MAIN_THREAD_GUARD;
  1386. ERR_FAIL_COND(!is_inside_tree());
  1387. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1388. // Consider the current size when calling with the default value.
  1389. Size2i expected_size = p_minsize == Size2i() ? size : p_minsize;
  1390. Rect2 parent_rect;
  1391. if (is_embedded()) {
  1392. parent_rect = get_embedder()->get_visible_rect();
  1393. } else {
  1394. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  1395. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  1396. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  1397. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  1398. }
  1399. Rect2i popup_rect;
  1400. popup_rect.size = _clamp_window_size(expected_size);
  1401. if (parent_rect != Rect2()) {
  1402. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  1403. }
  1404. popup(popup_rect);
  1405. }
  1406. void Window::popup_centered_ratio(float p_ratio) {
  1407. ERR_MAIN_THREAD_GUARD;
  1408. ERR_FAIL_COND(!is_inside_tree());
  1409. ERR_FAIL_COND_MSG(window_id == DisplayServer::MAIN_WINDOW_ID, "Can't popup the main window.");
  1410. ERR_FAIL_COND_MSG(p_ratio <= 0.0 || p_ratio > 1.0, "Ratio must be between 0.0 and 1.0!");
  1411. Rect2 parent_rect;
  1412. if (is_embedded()) {
  1413. parent_rect = get_embedder()->get_visible_rect();
  1414. } else {
  1415. DisplayServer::WindowID parent_id = get_parent_visible_window()->get_window_id();
  1416. int parent_screen = DisplayServer::get_singleton()->window_get_current_screen(parent_id);
  1417. parent_rect.position = DisplayServer::get_singleton()->screen_get_position(parent_screen);
  1418. parent_rect.size = DisplayServer::get_singleton()->screen_get_size(parent_screen);
  1419. }
  1420. Rect2i popup_rect;
  1421. if (parent_rect != Rect2()) {
  1422. popup_rect.size = parent_rect.size * p_ratio;
  1423. popup_rect.size = _clamp_window_size(popup_rect.size);
  1424. popup_rect.position = parent_rect.position + (parent_rect.size - popup_rect.size) / 2;
  1425. }
  1426. popup(popup_rect);
  1427. }
  1428. void Window::popup(const Rect2i &p_screen_rect) {
  1429. ERR_MAIN_THREAD_GUARD;
  1430. emit_signal(SNAME("about_to_popup"));
  1431. if (!get_embedder() && get_flag(FLAG_POPUP)) {
  1432. // Send a focus-out notification when opening a Window Manager Popup.
  1433. SceneTree *scene_tree = get_tree();
  1434. if (scene_tree) {
  1435. scene_tree->notify_group_flags(SceneTree::GROUP_CALL_DEFERRED, "_viewports", NOTIFICATION_WM_WINDOW_FOCUS_OUT);
  1436. }
  1437. }
  1438. // Update window size to calculate the actual window size based on contents minimum size and minimum size.
  1439. _update_window_size();
  1440. if (p_screen_rect != Rect2i()) {
  1441. set_position(p_screen_rect.position);
  1442. set_size(p_screen_rect.size);
  1443. }
  1444. Rect2i adjust = _popup_adjust_rect();
  1445. if (adjust != Rect2i()) {
  1446. set_position(adjust.position);
  1447. set_size(adjust.size);
  1448. }
  1449. int scr = DisplayServer::get_singleton()->get_screen_count();
  1450. for (int i = 0; i < scr; i++) {
  1451. Rect2i r = DisplayServer::get_singleton()->screen_get_usable_rect(i);
  1452. if (r.has_point(position)) {
  1453. current_screen = i;
  1454. break;
  1455. }
  1456. }
  1457. set_transient(true);
  1458. set_visible(true);
  1459. Rect2i parent_rect;
  1460. if (is_embedded()) {
  1461. parent_rect = get_embedder()->get_visible_rect();
  1462. } else {
  1463. int screen_id = DisplayServer::get_singleton()->window_get_current_screen(get_window_id());
  1464. parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen_id);
  1465. }
  1466. if (parent_rect != Rect2i() && !parent_rect.intersects(Rect2i(position, size))) {
  1467. ERR_PRINT(vformat("Window %d spawned at invalid position: %s.", get_window_id(), position));
  1468. set_position((parent_rect.size - size) / 2);
  1469. }
  1470. if (parent_rect != Rect2i() && is_clamped_to_embedder() && is_embedded()) {
  1471. Rect2i new_rect = fit_rect_in_parent(Rect2i(position, size), parent_rect);
  1472. set_position(new_rect.position);
  1473. set_size(new_rect.size);
  1474. }
  1475. _post_popup();
  1476. notification(NOTIFICATION_POST_POPUP);
  1477. }
  1478. bool Window::_try_parent_dialog(Node *p_from_node) {
  1479. ERR_FAIL_NULL_V(p_from_node, false);
  1480. ERR_FAIL_COND_V_MSG(is_inside_tree(), false, "Attempting to parent and popup a dialog that already has a parent.");
  1481. Window *w = p_from_node->get_last_exclusive_window();
  1482. if (w && w != this) {
  1483. w->add_child(this);
  1484. return true;
  1485. }
  1486. return false;
  1487. }
  1488. void Window::popup_exclusive(Node *p_from_node, const Rect2i &p_screen_rect) {
  1489. if (_try_parent_dialog(p_from_node)) {
  1490. popup(p_screen_rect);
  1491. }
  1492. }
  1493. void Window::popup_exclusive_on_parent(Node *p_from_node, const Rect2i &p_parent_rect) {
  1494. if (_try_parent_dialog(p_from_node)) {
  1495. popup_on_parent(p_parent_rect);
  1496. }
  1497. }
  1498. void Window::popup_exclusive_centered(Node *p_from_node, const Size2i &p_minsize) {
  1499. if (_try_parent_dialog(p_from_node)) {
  1500. popup_centered(p_minsize);
  1501. }
  1502. }
  1503. void Window::popup_exclusive_centered_ratio(Node *p_from_node, float p_ratio) {
  1504. if (_try_parent_dialog(p_from_node)) {
  1505. popup_centered_ratio(p_ratio);
  1506. }
  1507. }
  1508. void Window::popup_exclusive_centered_clamped(Node *p_from_node, const Size2i &p_size, float p_fallback_ratio) {
  1509. if (_try_parent_dialog(p_from_node)) {
  1510. popup_centered_clamped(p_size, p_fallback_ratio);
  1511. }
  1512. }
  1513. Rect2i Window::fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const {
  1514. ERR_READ_THREAD_GUARD_V(Rect2i());
  1515. Size2i limit = p_parent_rect.size;
  1516. if (p_rect.position.x + p_rect.size.x > limit.x) {
  1517. p_rect.position.x = limit.x - p_rect.size.x;
  1518. }
  1519. if (p_rect.position.y + p_rect.size.y > limit.y) {
  1520. p_rect.position.y = limit.y - p_rect.size.y;
  1521. }
  1522. if (p_rect.position.x < 0) {
  1523. p_rect.position.x = 0;
  1524. }
  1525. int title_height = get_flag(Window::FLAG_BORDERLESS) ? 0 : theme_cache.title_height;
  1526. if (p_rect.position.y < title_height) {
  1527. p_rect.position.y = title_height;
  1528. }
  1529. return p_rect;
  1530. }
  1531. Size2 Window::get_contents_minimum_size() const {
  1532. ERR_READ_THREAD_GUARD_V(Size2());
  1533. Vector2 ms;
  1534. if (GDVIRTUAL_CALL(_get_contents_minimum_size, ms)) {
  1535. return ms;
  1536. }
  1537. return _get_contents_minimum_size();
  1538. }
  1539. Size2 Window::get_clamped_minimum_size() const {
  1540. ERR_READ_THREAD_GUARD_V(Size2());
  1541. if (!wrap_controls) {
  1542. return min_size;
  1543. }
  1544. return min_size.max(get_contents_minimum_size());
  1545. }
  1546. void Window::grab_focus() {
  1547. ERR_MAIN_THREAD_GUARD;
  1548. if (embedder) {
  1549. embedder->_sub_window_grab_focus(this);
  1550. } else if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1551. DisplayServer::get_singleton()->window_move_to_foreground(window_id);
  1552. }
  1553. }
  1554. bool Window::has_focus() const {
  1555. ERR_READ_THREAD_GUARD_V(false);
  1556. if (window_id != DisplayServer::INVALID_WINDOW_ID) {
  1557. return DisplayServer::get_singleton()->window_is_focused(window_id);
  1558. }
  1559. return focused;
  1560. }
  1561. Rect2i Window::get_usable_parent_rect() const {
  1562. ERR_READ_THREAD_GUARD_V(Rect2i());
  1563. ERR_FAIL_COND_V(!is_inside_tree(), Rect2());
  1564. Rect2i parent_rect;
  1565. if (is_embedded()) {
  1566. parent_rect = get_embedder()->get_visible_rect();
  1567. } else {
  1568. const Window *w = is_visible() ? this : get_parent_visible_window();
  1569. //find a parent that can contain us
  1570. ERR_FAIL_NULL_V(w, Rect2());
  1571. parent_rect = DisplayServer::get_singleton()->screen_get_usable_rect(DisplayServer::get_singleton()->window_get_current_screen(w->get_window_id()));
  1572. }
  1573. return parent_rect;
  1574. }
  1575. void Window::add_child_notify(Node *p_child) {
  1576. if (is_inside_tree() && wrap_controls) {
  1577. child_controls_changed();
  1578. }
  1579. }
  1580. void Window::remove_child_notify(Node *p_child) {
  1581. if (is_inside_tree() && wrap_controls) {
  1582. child_controls_changed();
  1583. }
  1584. }
  1585. // Theming.
  1586. void Window::set_theme_owner_node(Node *p_node) {
  1587. ERR_MAIN_THREAD_GUARD;
  1588. theme_owner->set_owner_node(p_node);
  1589. }
  1590. Node *Window::get_theme_owner_node() const {
  1591. ERR_READ_THREAD_GUARD_V(nullptr);
  1592. return theme_owner->get_owner_node();
  1593. }
  1594. bool Window::has_theme_owner_node() const {
  1595. ERR_READ_THREAD_GUARD_V(false);
  1596. return theme_owner->has_owner_node();
  1597. }
  1598. void Window::set_theme_context(ThemeContext *p_context, bool p_propagate) {
  1599. ERR_MAIN_THREAD_GUARD;
  1600. theme_owner->set_owner_context(p_context, p_propagate);
  1601. }
  1602. void Window::set_theme(const Ref<Theme> &p_theme) {
  1603. ERR_MAIN_THREAD_GUARD;
  1604. if (theme == p_theme) {
  1605. return;
  1606. }
  1607. if (theme.is_valid()) {
  1608. theme->disconnect_changed(callable_mp(this, &Window::_theme_changed));
  1609. }
  1610. theme = p_theme;
  1611. if (theme.is_valid()) {
  1612. theme_owner->propagate_theme_changed(this, this, is_inside_tree(), true);
  1613. theme->connect_changed(callable_mp(this, &Window::_theme_changed), CONNECT_DEFERRED);
  1614. return;
  1615. }
  1616. Control *parent_c = Object::cast_to<Control>(get_parent());
  1617. if (parent_c && parent_c->has_theme_owner_node()) {
  1618. theme_owner->propagate_theme_changed(this, parent_c->get_theme_owner_node(), is_inside_tree(), true);
  1619. return;
  1620. }
  1621. Window *parent_w = cast_to<Window>(get_parent());
  1622. if (parent_w && parent_w->has_theme_owner_node()) {
  1623. theme_owner->propagate_theme_changed(this, parent_w->get_theme_owner_node(), is_inside_tree(), true);
  1624. return;
  1625. }
  1626. theme_owner->propagate_theme_changed(this, nullptr, is_inside_tree(), true);
  1627. }
  1628. Ref<Theme> Window::get_theme() const {
  1629. ERR_READ_THREAD_GUARD_V(Ref<Theme>());
  1630. return theme;
  1631. }
  1632. void Window::_theme_changed() {
  1633. if (is_inside_tree()) {
  1634. theme_owner->propagate_theme_changed(this, this, true, false);
  1635. }
  1636. }
  1637. void Window::_notify_theme_override_changed() {
  1638. if (!bulk_theme_override && is_inside_tree()) {
  1639. notification(NOTIFICATION_THEME_CHANGED);
  1640. }
  1641. }
  1642. void Window::_invalidate_theme_cache() {
  1643. theme_icon_cache.clear();
  1644. theme_style_cache.clear();
  1645. theme_font_cache.clear();
  1646. theme_font_size_cache.clear();
  1647. theme_color_cache.clear();
  1648. theme_constant_cache.clear();
  1649. }
  1650. void Window::_update_theme_item_cache() {
  1651. // Request an update on the next frame to reflect theme changes.
  1652. // Updating without a delay can cause a lot of lag.
  1653. if (!wrap_controls) {
  1654. updating_embedded_window = true;
  1655. call_deferred(SNAME("_update_embedded_window"));
  1656. } else {
  1657. child_controls_changed();
  1658. }
  1659. ThemeDB::get_singleton()->update_class_instance_items(this);
  1660. }
  1661. void Window::_update_embedded_window() {
  1662. if (!updating_embedded_window) {
  1663. return;
  1664. }
  1665. if (embedder) {
  1666. embedder->_sub_window_update(this);
  1667. };
  1668. updating_embedded_window = false;
  1669. }
  1670. void Window::set_theme_type_variation(const StringName &p_theme_type) {
  1671. ERR_MAIN_THREAD_GUARD;
  1672. theme_type_variation = p_theme_type;
  1673. if (is_inside_tree()) {
  1674. notification(NOTIFICATION_THEME_CHANGED);
  1675. }
  1676. }
  1677. StringName Window::get_theme_type_variation() const {
  1678. ERR_READ_THREAD_GUARD_V(StringName());
  1679. return theme_type_variation;
  1680. }
  1681. /// Theme property lookup.
  1682. Ref<Texture2D> Window::get_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  1683. ERR_READ_THREAD_GUARD_V(Ref<Texture2D>());
  1684. if (!initialized) {
  1685. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1686. }
  1687. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1688. const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
  1689. if (tex) {
  1690. return *tex;
  1691. }
  1692. }
  1693. if (theme_icon_cache.has(p_theme_type) && theme_icon_cache[p_theme_type].has(p_name)) {
  1694. return theme_icon_cache[p_theme_type][p_name];
  1695. }
  1696. List<StringName> theme_types;
  1697. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1698. Ref<Texture2D> icon = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  1699. theme_icon_cache[p_theme_type][p_name] = icon;
  1700. return icon;
  1701. }
  1702. Ref<StyleBox> Window::get_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  1703. ERR_READ_THREAD_GUARD_V(Ref<StyleBox>());
  1704. if (!initialized) {
  1705. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1706. }
  1707. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1708. const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
  1709. if (style) {
  1710. return *style;
  1711. }
  1712. }
  1713. if (theme_style_cache.has(p_theme_type) && theme_style_cache[p_theme_type].has(p_name)) {
  1714. return theme_style_cache[p_theme_type][p_name];
  1715. }
  1716. List<StringName> theme_types;
  1717. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1718. Ref<StyleBox> style = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  1719. theme_style_cache[p_theme_type][p_name] = style;
  1720. return style;
  1721. }
  1722. Ref<Font> Window::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  1723. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  1724. if (!initialized) {
  1725. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1726. }
  1727. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1728. const Ref<Font> *font = theme_font_override.getptr(p_name);
  1729. if (font) {
  1730. return *font;
  1731. }
  1732. }
  1733. if (theme_font_cache.has(p_theme_type) && theme_font_cache[p_theme_type].has(p_name)) {
  1734. return theme_font_cache[p_theme_type][p_name];
  1735. }
  1736. List<StringName> theme_types;
  1737. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1738. Ref<Font> font = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  1739. theme_font_cache[p_theme_type][p_name] = font;
  1740. return font;
  1741. }
  1742. int Window::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  1743. ERR_READ_THREAD_GUARD_V(0);
  1744. if (!initialized) {
  1745. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1746. }
  1747. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1748. const int *font_size = theme_font_size_override.getptr(p_name);
  1749. if (font_size && (*font_size) > 0) {
  1750. return *font_size;
  1751. }
  1752. }
  1753. if (theme_font_size_cache.has(p_theme_type) && theme_font_size_cache[p_theme_type].has(p_name)) {
  1754. return theme_font_size_cache[p_theme_type][p_name];
  1755. }
  1756. List<StringName> theme_types;
  1757. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1758. int font_size = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  1759. theme_font_size_cache[p_theme_type][p_name] = font_size;
  1760. return font_size;
  1761. }
  1762. Color Window::get_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  1763. ERR_READ_THREAD_GUARD_V(Color());
  1764. if (!initialized) {
  1765. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1766. }
  1767. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1768. const Color *color = theme_color_override.getptr(p_name);
  1769. if (color) {
  1770. return *color;
  1771. }
  1772. }
  1773. if (theme_color_cache.has(p_theme_type) && theme_color_cache[p_theme_type].has(p_name)) {
  1774. return theme_color_cache[p_theme_type][p_name];
  1775. }
  1776. List<StringName> theme_types;
  1777. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1778. Color color = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  1779. theme_color_cache[p_theme_type][p_name] = color;
  1780. return color;
  1781. }
  1782. int Window::get_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  1783. ERR_READ_THREAD_GUARD_V(0);
  1784. if (!initialized) {
  1785. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1786. }
  1787. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1788. const int *constant = theme_constant_override.getptr(p_name);
  1789. if (constant) {
  1790. return *constant;
  1791. }
  1792. }
  1793. if (theme_constant_cache.has(p_theme_type) && theme_constant_cache[p_theme_type].has(p_name)) {
  1794. return theme_constant_cache[p_theme_type][p_name];
  1795. }
  1796. List<StringName> theme_types;
  1797. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1798. int constant = theme_owner->get_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  1799. theme_constant_cache[p_theme_type][p_name] = constant;
  1800. return constant;
  1801. }
  1802. Variant Window::get_theme_item(Theme::DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
  1803. switch (p_data_type) {
  1804. case Theme::DATA_TYPE_COLOR:
  1805. return get_theme_color(p_name, p_theme_type);
  1806. case Theme::DATA_TYPE_CONSTANT:
  1807. return get_theme_constant(p_name, p_theme_type);
  1808. case Theme::DATA_TYPE_FONT:
  1809. return get_theme_font(p_name, p_theme_type);
  1810. case Theme::DATA_TYPE_FONT_SIZE:
  1811. return get_theme_font_size(p_name, p_theme_type);
  1812. case Theme::DATA_TYPE_ICON:
  1813. return get_theme_icon(p_name, p_theme_type);
  1814. case Theme::DATA_TYPE_STYLEBOX:
  1815. return get_theme_stylebox(p_name, p_theme_type);
  1816. case Theme::DATA_TYPE_MAX:
  1817. break; // Can't happen, but silences warning.
  1818. }
  1819. return Variant();
  1820. }
  1821. #ifdef TOOLS_ENABLED
  1822. Ref<Texture2D> Window::get_editor_theme_icon(const StringName &p_name) const {
  1823. return get_theme_icon(p_name, SNAME("EditorIcons"));
  1824. }
  1825. #endif
  1826. bool Window::has_theme_icon(const StringName &p_name, const StringName &p_theme_type) const {
  1827. ERR_READ_THREAD_GUARD_V(false);
  1828. if (!initialized) {
  1829. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1830. }
  1831. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1832. if (has_theme_icon_override(p_name)) {
  1833. return true;
  1834. }
  1835. }
  1836. List<StringName> theme_types;
  1837. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1838. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_ICON, p_name, theme_types);
  1839. }
  1840. bool Window::has_theme_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
  1841. ERR_READ_THREAD_GUARD_V(false);
  1842. if (!initialized) {
  1843. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1844. }
  1845. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1846. if (has_theme_stylebox_override(p_name)) {
  1847. return true;
  1848. }
  1849. }
  1850. List<StringName> theme_types;
  1851. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1852. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_STYLEBOX, p_name, theme_types);
  1853. }
  1854. bool Window::has_theme_font(const StringName &p_name, const StringName &p_theme_type) const {
  1855. ERR_READ_THREAD_GUARD_V(false);
  1856. if (!initialized) {
  1857. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1858. }
  1859. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1860. if (has_theme_font_override(p_name)) {
  1861. return true;
  1862. }
  1863. }
  1864. List<StringName> theme_types;
  1865. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1866. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT, p_name, theme_types);
  1867. }
  1868. bool Window::has_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const {
  1869. ERR_READ_THREAD_GUARD_V(false);
  1870. if (!initialized) {
  1871. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1872. }
  1873. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1874. if (has_theme_font_size_override(p_name)) {
  1875. return true;
  1876. }
  1877. }
  1878. List<StringName> theme_types;
  1879. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1880. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_FONT_SIZE, p_name, theme_types);
  1881. }
  1882. bool Window::has_theme_color(const StringName &p_name, const StringName &p_theme_type) const {
  1883. ERR_READ_THREAD_GUARD_V(false);
  1884. if (!initialized) {
  1885. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1886. }
  1887. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1888. if (has_theme_color_override(p_name)) {
  1889. return true;
  1890. }
  1891. }
  1892. List<StringName> theme_types;
  1893. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1894. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_COLOR, p_name, theme_types);
  1895. }
  1896. bool Window::has_theme_constant(const StringName &p_name, const StringName &p_theme_type) const {
  1897. ERR_READ_THREAD_GUARD_V(false);
  1898. if (!initialized) {
  1899. WARN_PRINT_ONCE("Attempting to access theme items too early; prefer NOTIFICATION_POSTINITIALIZE and NOTIFICATION_THEME_CHANGED");
  1900. }
  1901. if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == theme_type_variation) {
  1902. if (has_theme_constant_override(p_name)) {
  1903. return true;
  1904. }
  1905. }
  1906. List<StringName> theme_types;
  1907. theme_owner->get_theme_type_dependencies(this, p_theme_type, &theme_types);
  1908. return theme_owner->has_theme_item_in_types(Theme::DATA_TYPE_CONSTANT, p_name, theme_types);
  1909. }
  1910. /// Local property overrides.
  1911. void Window::add_theme_icon_override(const StringName &p_name, const Ref<Texture2D> &p_icon) {
  1912. ERR_MAIN_THREAD_GUARD;
  1913. ERR_FAIL_COND(!p_icon.is_valid());
  1914. if (theme_icon_override.has(p_name)) {
  1915. theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  1916. }
  1917. theme_icon_override[p_name] = p_icon;
  1918. theme_icon_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  1919. _notify_theme_override_changed();
  1920. }
  1921. void Window::add_theme_style_override(const StringName &p_name, const Ref<StyleBox> &p_style) {
  1922. ERR_MAIN_THREAD_GUARD;
  1923. ERR_FAIL_COND(!p_style.is_valid());
  1924. if (theme_style_override.has(p_name)) {
  1925. theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  1926. }
  1927. theme_style_override[p_name] = p_style;
  1928. theme_style_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  1929. _notify_theme_override_changed();
  1930. }
  1931. void Window::add_theme_font_override(const StringName &p_name, const Ref<Font> &p_font) {
  1932. ERR_MAIN_THREAD_GUARD;
  1933. ERR_FAIL_COND(!p_font.is_valid());
  1934. if (theme_font_override.has(p_name)) {
  1935. theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  1936. }
  1937. theme_font_override[p_name] = p_font;
  1938. theme_font_override[p_name]->connect_changed(callable_mp(this, &Window::_notify_theme_override_changed), CONNECT_REFERENCE_COUNTED);
  1939. _notify_theme_override_changed();
  1940. }
  1941. void Window::add_theme_font_size_override(const StringName &p_name, int p_font_size) {
  1942. ERR_MAIN_THREAD_GUARD;
  1943. theme_font_size_override[p_name] = p_font_size;
  1944. _notify_theme_override_changed();
  1945. }
  1946. void Window::add_theme_color_override(const StringName &p_name, const Color &p_color) {
  1947. ERR_MAIN_THREAD_GUARD;
  1948. theme_color_override[p_name] = p_color;
  1949. _notify_theme_override_changed();
  1950. }
  1951. void Window::add_theme_constant_override(const StringName &p_name, int p_constant) {
  1952. ERR_MAIN_THREAD_GUARD;
  1953. theme_constant_override[p_name] = p_constant;
  1954. _notify_theme_override_changed();
  1955. }
  1956. void Window::remove_theme_icon_override(const StringName &p_name) {
  1957. ERR_MAIN_THREAD_GUARD;
  1958. if (theme_icon_override.has(p_name)) {
  1959. theme_icon_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  1960. }
  1961. theme_icon_override.erase(p_name);
  1962. _notify_theme_override_changed();
  1963. }
  1964. void Window::remove_theme_style_override(const StringName &p_name) {
  1965. ERR_MAIN_THREAD_GUARD;
  1966. if (theme_style_override.has(p_name)) {
  1967. theme_style_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  1968. }
  1969. theme_style_override.erase(p_name);
  1970. _notify_theme_override_changed();
  1971. }
  1972. void Window::remove_theme_font_override(const StringName &p_name) {
  1973. ERR_MAIN_THREAD_GUARD;
  1974. if (theme_font_override.has(p_name)) {
  1975. theme_font_override[p_name]->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  1976. }
  1977. theme_font_override.erase(p_name);
  1978. _notify_theme_override_changed();
  1979. }
  1980. void Window::remove_theme_font_size_override(const StringName &p_name) {
  1981. ERR_MAIN_THREAD_GUARD;
  1982. theme_font_size_override.erase(p_name);
  1983. _notify_theme_override_changed();
  1984. }
  1985. void Window::remove_theme_color_override(const StringName &p_name) {
  1986. ERR_MAIN_THREAD_GUARD;
  1987. theme_color_override.erase(p_name);
  1988. _notify_theme_override_changed();
  1989. }
  1990. void Window::remove_theme_constant_override(const StringName &p_name) {
  1991. ERR_MAIN_THREAD_GUARD;
  1992. theme_constant_override.erase(p_name);
  1993. _notify_theme_override_changed();
  1994. }
  1995. bool Window::has_theme_icon_override(const StringName &p_name) const {
  1996. ERR_READ_THREAD_GUARD_V(false);
  1997. const Ref<Texture2D> *tex = theme_icon_override.getptr(p_name);
  1998. return tex != nullptr;
  1999. }
  2000. bool Window::has_theme_stylebox_override(const StringName &p_name) const {
  2001. ERR_READ_THREAD_GUARD_V(false);
  2002. const Ref<StyleBox> *style = theme_style_override.getptr(p_name);
  2003. return style != nullptr;
  2004. }
  2005. bool Window::has_theme_font_override(const StringName &p_name) const {
  2006. ERR_READ_THREAD_GUARD_V(false);
  2007. const Ref<Font> *font = theme_font_override.getptr(p_name);
  2008. return font != nullptr;
  2009. }
  2010. bool Window::has_theme_font_size_override(const StringName &p_name) const {
  2011. ERR_READ_THREAD_GUARD_V(false);
  2012. const int *font_size = theme_font_size_override.getptr(p_name);
  2013. return font_size != nullptr;
  2014. }
  2015. bool Window::has_theme_color_override(const StringName &p_name) const {
  2016. ERR_READ_THREAD_GUARD_V(false);
  2017. const Color *color = theme_color_override.getptr(p_name);
  2018. return color != nullptr;
  2019. }
  2020. bool Window::has_theme_constant_override(const StringName &p_name) const {
  2021. ERR_READ_THREAD_GUARD_V(false);
  2022. const int *constant = theme_constant_override.getptr(p_name);
  2023. return constant != nullptr;
  2024. }
  2025. /// Default theme properties.
  2026. float Window::get_theme_default_base_scale() const {
  2027. ERR_READ_THREAD_GUARD_V(0);
  2028. return theme_owner->get_theme_default_base_scale();
  2029. }
  2030. Ref<Font> Window::get_theme_default_font() const {
  2031. ERR_READ_THREAD_GUARD_V(Ref<Font>());
  2032. return theme_owner->get_theme_default_font();
  2033. }
  2034. int Window::get_theme_default_font_size() const {
  2035. ERR_READ_THREAD_GUARD_V(0);
  2036. return theme_owner->get_theme_default_font_size();
  2037. }
  2038. /// Bulk actions.
  2039. void Window::begin_bulk_theme_override() {
  2040. ERR_MAIN_THREAD_GUARD;
  2041. bulk_theme_override = true;
  2042. }
  2043. void Window::end_bulk_theme_override() {
  2044. ERR_MAIN_THREAD_GUARD;
  2045. ERR_FAIL_COND(!bulk_theme_override);
  2046. bulk_theme_override = false;
  2047. _notify_theme_override_changed();
  2048. }
  2049. //
  2050. Rect2i Window::get_parent_rect() const {
  2051. ERR_READ_THREAD_GUARD_V(Rect2i());
  2052. ERR_FAIL_COND_V(!is_inside_tree(), Rect2i());
  2053. if (is_embedded()) {
  2054. //viewport
  2055. Node *n = get_parent();
  2056. ERR_FAIL_NULL_V(n, Rect2i());
  2057. Viewport *p = n->get_viewport();
  2058. ERR_FAIL_NULL_V(p, Rect2i());
  2059. return p->get_visible_rect();
  2060. } else {
  2061. int x = get_position().x;
  2062. int closest_dist = 0x7FFFFFFF;
  2063. Rect2i closest_rect;
  2064. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  2065. Rect2i s(DisplayServer::get_singleton()->screen_get_position(i), DisplayServer::get_singleton()->screen_get_size(i));
  2066. int d;
  2067. if (x >= s.position.x && x < s.size.x) {
  2068. //contained
  2069. closest_rect = s;
  2070. break;
  2071. } else if (x < s.position.x) {
  2072. d = s.position.x - x;
  2073. } else {
  2074. d = x - (s.position.x + s.size.x);
  2075. }
  2076. if (d < closest_dist) {
  2077. closest_dist = d;
  2078. closest_rect = s;
  2079. }
  2080. }
  2081. return closest_rect;
  2082. }
  2083. }
  2084. void Window::set_clamp_to_embedder(bool p_enable) {
  2085. ERR_MAIN_THREAD_GUARD;
  2086. clamp_to_embedder = p_enable;
  2087. }
  2088. bool Window::is_clamped_to_embedder() const {
  2089. ERR_READ_THREAD_GUARD_V(false);
  2090. return clamp_to_embedder;
  2091. }
  2092. void Window::set_unparent_when_invisible(bool p_unparent) {
  2093. unparent_when_invisible = p_unparent;
  2094. }
  2095. void Window::set_layout_direction(Window::LayoutDirection p_direction) {
  2096. ERR_MAIN_THREAD_GUARD;
  2097. ERR_FAIL_INDEX((int)p_direction, 4);
  2098. layout_dir = p_direction;
  2099. propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
  2100. }
  2101. Window::LayoutDirection Window::get_layout_direction() const {
  2102. ERR_READ_THREAD_GUARD_V(LAYOUT_DIRECTION_INHERITED);
  2103. return layout_dir;
  2104. }
  2105. bool Window::is_layout_rtl() const {
  2106. ERR_READ_THREAD_GUARD_V(false);
  2107. if (layout_dir == LAYOUT_DIRECTION_INHERITED) {
  2108. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2109. return true;
  2110. }
  2111. Node *parent_node = get_parent();
  2112. while (parent_node) {
  2113. Control *parent_control = Object::cast_to<Control>(parent_node);
  2114. if (parent_control) {
  2115. return parent_control->is_layout_rtl();
  2116. }
  2117. Window *parent_window = Object::cast_to<Window>(parent_node);
  2118. if (parent_window) {
  2119. return parent_window->is_layout_rtl();
  2120. }
  2121. parent_node = parent_node->get_parent();
  2122. }
  2123. int root_dir = GLOBAL_GET(SNAME("internationalization/rendering/root_node_layout_direction"));
  2124. if (root_dir == 1) {
  2125. return false;
  2126. } else if (root_dir == 2) {
  2127. return true;
  2128. } else {
  2129. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2130. return TS->is_locale_right_to_left(locale);
  2131. }
  2132. } else if (layout_dir == LAYOUT_DIRECTION_LOCALE) {
  2133. if (GLOBAL_GET(SNAME("internationalization/rendering/force_right_to_left_layout_direction"))) {
  2134. return true;
  2135. } else {
  2136. String locale = TranslationServer::get_singleton()->get_tool_locale();
  2137. return TS->is_locale_right_to_left(locale);
  2138. }
  2139. } else {
  2140. return (layout_dir == LAYOUT_DIRECTION_RTL);
  2141. }
  2142. }
  2143. void Window::set_auto_translate(bool p_enable) {
  2144. ERR_MAIN_THREAD_GUARD;
  2145. if (p_enable == auto_translate) {
  2146. return;
  2147. }
  2148. auto_translate = p_enable;
  2149. notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  2150. }
  2151. bool Window::is_auto_translating() const {
  2152. ERR_READ_THREAD_GUARD_V(false);
  2153. return auto_translate;
  2154. }
  2155. Transform2D Window::get_final_transform() const {
  2156. ERR_READ_THREAD_GUARD_V(Transform2D());
  2157. return window_transform * stretch_transform * global_canvas_transform;
  2158. }
  2159. Transform2D Window::get_screen_transform_internal(bool p_absolute_position) const {
  2160. ERR_READ_THREAD_GUARD_V(Transform2D());
  2161. Transform2D embedder_transform;
  2162. if (get_embedder()) {
  2163. embedder_transform.translate_local(get_position());
  2164. embedder_transform = get_embedder()->get_screen_transform_internal(p_absolute_position) * embedder_transform;
  2165. } else if (p_absolute_position) {
  2166. embedder_transform.translate_local(get_position());
  2167. }
  2168. return embedder_transform * get_final_transform();
  2169. }
  2170. Transform2D Window::get_popup_base_transform() const {
  2171. ERR_READ_THREAD_GUARD_V(Transform2D());
  2172. if (is_embedding_subwindows()) {
  2173. return Transform2D();
  2174. }
  2175. Transform2D popup_base_transform;
  2176. popup_base_transform.set_origin(get_position());
  2177. popup_base_transform *= get_final_transform();
  2178. if (get_embedder()) {
  2179. return get_embedder()->get_popup_base_transform() * popup_base_transform;
  2180. }
  2181. return popup_base_transform;
  2182. }
  2183. bool Window::is_directly_attached_to_screen() const {
  2184. if (get_embedder()) {
  2185. return get_embedder()->is_directly_attached_to_screen();
  2186. }
  2187. // Distinguish between the case that this is a native Window and not inside the tree.
  2188. return is_inside_tree();
  2189. }
  2190. bool Window::is_attached_in_viewport() const {
  2191. return get_embedder();
  2192. }
  2193. void Window::_update_mouse_over(Vector2 p_pos) {
  2194. if (!mouse_in_window) {
  2195. if (is_embedded()) {
  2196. mouse_in_window = true;
  2197. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_ENTER);
  2198. } else {
  2199. // Prevent update based on delayed InputEvents from DisplayServer.
  2200. return;
  2201. }
  2202. }
  2203. bool new_in = get_visible_rect().has_point(p_pos);
  2204. if (new_in == gui.mouse_in_viewport) {
  2205. if (new_in) {
  2206. Viewport::_update_mouse_over(p_pos);
  2207. }
  2208. return;
  2209. }
  2210. if (new_in) {
  2211. notification(NOTIFICATION_VP_MOUSE_ENTER);
  2212. Viewport::_update_mouse_over(p_pos);
  2213. } else {
  2214. Viewport::_mouse_leave_viewport();
  2215. }
  2216. }
  2217. void Window::_mouse_leave_viewport() {
  2218. Viewport::_mouse_leave_viewport();
  2219. if (is_embedded()) {
  2220. mouse_in_window = false;
  2221. _propagate_window_notification(this, NOTIFICATION_WM_MOUSE_EXIT);
  2222. }
  2223. }
  2224. void Window::_bind_methods() {
  2225. ClassDB::bind_method(D_METHOD("set_title", "title"), &Window::set_title);
  2226. ClassDB::bind_method(D_METHOD("get_title"), &Window::get_title);
  2227. ClassDB::bind_method(D_METHOD("get_window_id"), &Window::get_window_id);
  2228. ClassDB::bind_method(D_METHOD("set_initial_position", "initial_position"), &Window::set_initial_position);
  2229. ClassDB::bind_method(D_METHOD("get_initial_position"), &Window::get_initial_position);
  2230. ClassDB::bind_method(D_METHOD("set_current_screen", "index"), &Window::set_current_screen);
  2231. ClassDB::bind_method(D_METHOD("get_current_screen"), &Window::get_current_screen);
  2232. ClassDB::bind_method(D_METHOD("set_position", "position"), &Window::set_position);
  2233. ClassDB::bind_method(D_METHOD("get_position"), &Window::get_position);
  2234. ClassDB::bind_method(D_METHOD("move_to_center"), &Window::move_to_center);
  2235. ClassDB::bind_method(D_METHOD("set_size", "size"), &Window::set_size);
  2236. ClassDB::bind_method(D_METHOD("get_size"), &Window::get_size);
  2237. ClassDB::bind_method(D_METHOD("reset_size"), &Window::reset_size);
  2238. ClassDB::bind_method(D_METHOD("get_position_with_decorations"), &Window::get_position_with_decorations);
  2239. ClassDB::bind_method(D_METHOD("get_size_with_decorations"), &Window::get_size_with_decorations);
  2240. ClassDB::bind_method(D_METHOD("set_max_size", "max_size"), &Window::set_max_size);
  2241. ClassDB::bind_method(D_METHOD("get_max_size"), &Window::get_max_size);
  2242. ClassDB::bind_method(D_METHOD("set_min_size", "min_size"), &Window::set_min_size);
  2243. ClassDB::bind_method(D_METHOD("get_min_size"), &Window::get_min_size);
  2244. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &Window::set_mode);
  2245. ClassDB::bind_method(D_METHOD("get_mode"), &Window::get_mode);
  2246. ClassDB::bind_method(D_METHOD("set_flag", "flag", "enabled"), &Window::set_flag);
  2247. ClassDB::bind_method(D_METHOD("get_flag", "flag"), &Window::get_flag);
  2248. ClassDB::bind_method(D_METHOD("is_maximize_allowed"), &Window::is_maximize_allowed);
  2249. ClassDB::bind_method(D_METHOD("request_attention"), &Window::request_attention);
  2250. ClassDB::bind_method(D_METHOD("move_to_foreground"), &Window::move_to_foreground);
  2251. ClassDB::bind_method(D_METHOD("set_visible", "visible"), &Window::set_visible);
  2252. ClassDB::bind_method(D_METHOD("is_visible"), &Window::is_visible);
  2253. ClassDB::bind_method(D_METHOD("hide"), &Window::hide);
  2254. ClassDB::bind_method(D_METHOD("show"), &Window::show);
  2255. ClassDB::bind_method(D_METHOD("set_transient", "transient"), &Window::set_transient);
  2256. ClassDB::bind_method(D_METHOD("is_transient"), &Window::is_transient);
  2257. ClassDB::bind_method(D_METHOD("set_exclusive", "exclusive"), &Window::set_exclusive);
  2258. ClassDB::bind_method(D_METHOD("is_exclusive"), &Window::is_exclusive);
  2259. ClassDB::bind_method(D_METHOD("set_unparent_when_invisible", "unparent"), &Window::set_unparent_when_invisible);
  2260. ClassDB::bind_method(D_METHOD("can_draw"), &Window::can_draw);
  2261. ClassDB::bind_method(D_METHOD("has_focus"), &Window::has_focus);
  2262. ClassDB::bind_method(D_METHOD("grab_focus"), &Window::grab_focus);
  2263. ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &Window::set_ime_active);
  2264. ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &Window::set_ime_position);
  2265. ClassDB::bind_method(D_METHOD("is_embedded"), &Window::is_embedded);
  2266. ClassDB::bind_method(D_METHOD("get_contents_minimum_size"), &Window::get_contents_minimum_size);
  2267. ClassDB::bind_method(D_METHOD("set_content_scale_size", "size"), &Window::set_content_scale_size);
  2268. ClassDB::bind_method(D_METHOD("get_content_scale_size"), &Window::get_content_scale_size);
  2269. ClassDB::bind_method(D_METHOD("set_content_scale_mode", "mode"), &Window::set_content_scale_mode);
  2270. ClassDB::bind_method(D_METHOD("get_content_scale_mode"), &Window::get_content_scale_mode);
  2271. ClassDB::bind_method(D_METHOD("set_content_scale_aspect", "aspect"), &Window::set_content_scale_aspect);
  2272. ClassDB::bind_method(D_METHOD("get_content_scale_aspect"), &Window::get_content_scale_aspect);
  2273. ClassDB::bind_method(D_METHOD("set_content_scale_stretch", "stretch"), &Window::set_content_scale_stretch);
  2274. ClassDB::bind_method(D_METHOD("get_content_scale_stretch"), &Window::get_content_scale_stretch);
  2275. ClassDB::bind_method(D_METHOD("set_content_scale_factor", "factor"), &Window::set_content_scale_factor);
  2276. ClassDB::bind_method(D_METHOD("get_content_scale_factor"), &Window::get_content_scale_factor);
  2277. ClassDB::bind_method(D_METHOD("set_use_font_oversampling", "enable"), &Window::set_use_font_oversampling);
  2278. ClassDB::bind_method(D_METHOD("is_using_font_oversampling"), &Window::is_using_font_oversampling);
  2279. ClassDB::bind_method(D_METHOD("set_mouse_passthrough_polygon", "polygon"), &Window::set_mouse_passthrough_polygon);
  2280. ClassDB::bind_method(D_METHOD("get_mouse_passthrough_polygon"), &Window::get_mouse_passthrough_polygon);
  2281. ClassDB::bind_method(D_METHOD("set_wrap_controls", "enable"), &Window::set_wrap_controls);
  2282. ClassDB::bind_method(D_METHOD("is_wrapping_controls"), &Window::is_wrapping_controls);
  2283. ClassDB::bind_method(D_METHOD("child_controls_changed"), &Window::child_controls_changed);
  2284. ClassDB::bind_method(D_METHOD("_update_child_controls"), &Window::_update_child_controls);
  2285. ClassDB::bind_method(D_METHOD("_update_embedded_window"), &Window::_update_embedded_window);
  2286. ClassDB::bind_method(D_METHOD("set_theme", "theme"), &Window::set_theme);
  2287. ClassDB::bind_method(D_METHOD("get_theme"), &Window::get_theme);
  2288. ClassDB::bind_method(D_METHOD("set_theme_type_variation", "theme_type"), &Window::set_theme_type_variation);
  2289. ClassDB::bind_method(D_METHOD("get_theme_type_variation"), &Window::get_theme_type_variation);
  2290. ClassDB::bind_method(D_METHOD("begin_bulk_theme_override"), &Window::begin_bulk_theme_override);
  2291. ClassDB::bind_method(D_METHOD("end_bulk_theme_override"), &Window::end_bulk_theme_override);
  2292. ClassDB::bind_method(D_METHOD("add_theme_icon_override", "name", "texture"), &Window::add_theme_icon_override);
  2293. ClassDB::bind_method(D_METHOD("add_theme_stylebox_override", "name", "stylebox"), &Window::add_theme_style_override);
  2294. ClassDB::bind_method(D_METHOD("add_theme_font_override", "name", "font"), &Window::add_theme_font_override);
  2295. ClassDB::bind_method(D_METHOD("add_theme_font_size_override", "name", "font_size"), &Window::add_theme_font_size_override);
  2296. ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Window::add_theme_color_override);
  2297. ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Window::add_theme_constant_override);
  2298. ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Window::remove_theme_icon_override);
  2299. ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Window::remove_theme_style_override);
  2300. ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Window::remove_theme_font_override);
  2301. ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Window::remove_theme_font_size_override);
  2302. ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Window::remove_theme_color_override);
  2303. ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Window::remove_theme_constant_override);
  2304. ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "theme_type"), &Window::get_theme_icon, DEFVAL(""));
  2305. ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "theme_type"), &Window::get_theme_stylebox, DEFVAL(""));
  2306. ClassDB::bind_method(D_METHOD("get_theme_font", "name", "theme_type"), &Window::get_theme_font, DEFVAL(""));
  2307. ClassDB::bind_method(D_METHOD("get_theme_font_size", "name", "theme_type"), &Window::get_theme_font_size, DEFVAL(""));
  2308. ClassDB::bind_method(D_METHOD("get_theme_color", "name", "theme_type"), &Window::get_theme_color, DEFVAL(""));
  2309. ClassDB::bind_method(D_METHOD("get_theme_constant", "name", "theme_type"), &Window::get_theme_constant, DEFVAL(""));
  2310. ClassDB::bind_method(D_METHOD("has_theme_icon_override", "name"), &Window::has_theme_icon_override);
  2311. ClassDB::bind_method(D_METHOD("has_theme_stylebox_override", "name"), &Window::has_theme_stylebox_override);
  2312. ClassDB::bind_method(D_METHOD("has_theme_font_override", "name"), &Window::has_theme_font_override);
  2313. ClassDB::bind_method(D_METHOD("has_theme_font_size_override", "name"), &Window::has_theme_font_size_override);
  2314. ClassDB::bind_method(D_METHOD("has_theme_color_override", "name"), &Window::has_theme_color_override);
  2315. ClassDB::bind_method(D_METHOD("has_theme_constant_override", "name"), &Window::has_theme_constant_override);
  2316. ClassDB::bind_method(D_METHOD("has_theme_icon", "name", "theme_type"), &Window::has_theme_icon, DEFVAL(""));
  2317. ClassDB::bind_method(D_METHOD("has_theme_stylebox", "name", "theme_type"), &Window::has_theme_stylebox, DEFVAL(""));
  2318. ClassDB::bind_method(D_METHOD("has_theme_font", "name", "theme_type"), &Window::has_theme_font, DEFVAL(""));
  2319. ClassDB::bind_method(D_METHOD("has_theme_font_size", "name", "theme_type"), &Window::has_theme_font_size, DEFVAL(""));
  2320. ClassDB::bind_method(D_METHOD("has_theme_color", "name", "theme_type"), &Window::has_theme_color, DEFVAL(""));
  2321. ClassDB::bind_method(D_METHOD("has_theme_constant", "name", "theme_type"), &Window::has_theme_constant, DEFVAL(""));
  2322. ClassDB::bind_method(D_METHOD("get_theme_default_base_scale"), &Window::get_theme_default_base_scale);
  2323. ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Window::get_theme_default_font);
  2324. ClassDB::bind_method(D_METHOD("get_theme_default_font_size"), &Window::get_theme_default_font_size);
  2325. ClassDB::bind_method(D_METHOD("set_layout_direction", "direction"), &Window::set_layout_direction);
  2326. ClassDB::bind_method(D_METHOD("get_layout_direction"), &Window::get_layout_direction);
  2327. ClassDB::bind_method(D_METHOD("is_layout_rtl"), &Window::is_layout_rtl);
  2328. ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Window::set_auto_translate);
  2329. ClassDB::bind_method(D_METHOD("is_auto_translating"), &Window::is_auto_translating);
  2330. ClassDB::bind_method(D_METHOD("popup", "rect"), &Window::popup, DEFVAL(Rect2i()));
  2331. ClassDB::bind_method(D_METHOD("popup_on_parent", "parent_rect"), &Window::popup_on_parent);
  2332. ClassDB::bind_method(D_METHOD("popup_centered", "minsize"), &Window::popup_centered, DEFVAL(Size2i()));
  2333. ClassDB::bind_method(D_METHOD("popup_centered_ratio", "ratio"), &Window::popup_centered_ratio, DEFVAL(0.8));
  2334. ClassDB::bind_method(D_METHOD("popup_centered_clamped", "minsize", "fallback_ratio"), &Window::popup_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
  2335. ClassDB::bind_method(D_METHOD("popup_exclusive", "from_node", "rect"), &Window::popup_exclusive, DEFVAL(Rect2i()));
  2336. ClassDB::bind_method(D_METHOD("popup_exclusive_on_parent", "from_node", "parent_rect"), &Window::popup_exclusive_on_parent);
  2337. ClassDB::bind_method(D_METHOD("popup_exclusive_centered", "from_node", "minsize"), &Window::popup_exclusive_centered, DEFVAL(Size2i()));
  2338. ClassDB::bind_method(D_METHOD("popup_exclusive_centered_ratio", "from_node", "ratio"), &Window::popup_exclusive_centered_ratio, DEFVAL(0.8));
  2339. ClassDB::bind_method(D_METHOD("popup_exclusive_centered_clamped", "from_node", "minsize", "fallback_ratio"), &Window::popup_exclusive_centered_clamped, DEFVAL(Size2i()), DEFVAL(0.75));
  2340. // Keep the enum values in sync with the `Mode` enum.
  2341. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"), "set_mode", "get_mode");
  2342. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  2343. // Keep the enum values in sync with the `WindowInitialPosition` enum.
  2344. ADD_PROPERTY(PropertyInfo(Variant::INT, "initial_position", PROPERTY_HINT_ENUM, "Absolute,Center of Primary Screen,Center of Main Window Screen,Center of Other Screen,Center of Screen With Mouse Pointer,Center of Screen With Keyboard Focus"), "set_initial_position", "get_initial_position");
  2345. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "position", PROPERTY_HINT_NONE, "suffix:px"), "set_position", "get_position");
  2346. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
  2347. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_current_screen", "get_current_screen");
  2348. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "mouse_passthrough_polygon"), "set_mouse_passthrough_polygon", "get_mouse_passthrough_polygon");
  2349. ADD_GROUP("Flags", "");
  2350. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  2351. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wrap_controls"), "set_wrap_controls", "is_wrapping_controls");
  2352. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transient"), "set_transient", "is_transient");
  2353. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclusive"), "set_exclusive", "is_exclusive");
  2354. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unresizable"), "set_flag", "get_flag", FLAG_RESIZE_DISABLED);
  2355. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "borderless"), "set_flag", "get_flag", FLAG_BORDERLESS);
  2356. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "always_on_top"), "set_flag", "get_flag", FLAG_ALWAYS_ON_TOP);
  2357. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_flag", "get_flag", FLAG_TRANSPARENT);
  2358. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "unfocusable"), "set_flag", "get_flag", FLAG_NO_FOCUS);
  2359. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "popup_window"), "set_flag", "get_flag", FLAG_POPUP);
  2360. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "extend_to_title"), "set_flag", "get_flag", FLAG_EXTEND_TO_TITLE);
  2361. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "mouse_passthrough"), "set_flag", "get_flag", FLAG_MOUSE_PASSTHROUGH);
  2362. ADD_GROUP("Limits", "");
  2363. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "min_size", PROPERTY_HINT_NONE, "suffix:px"), "set_min_size", "get_min_size");
  2364. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "max_size", PROPERTY_HINT_NONE, "suffix:px"), "set_max_size", "get_max_size");
  2365. ADD_GROUP("Content Scale", "content_scale_");
  2366. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "content_scale_size"), "set_content_scale_size", "get_content_scale_size");
  2367. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_mode", PROPERTY_HINT_ENUM, "Disabled,Canvas Items,Viewport"), "set_content_scale_mode", "get_content_scale_mode");
  2368. 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");
  2369. ADD_PROPERTY(PropertyInfo(Variant::INT, "content_scale_stretch", PROPERTY_HINT_ENUM, "Fractional,Integer"), "set_content_scale_stretch", "get_content_scale_stretch");
  2370. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "content_scale_factor", PROPERTY_HINT_RANGE, "0.5,8.0,0.01"), "set_content_scale_factor", "get_content_scale_factor");
  2371. ADD_GROUP("Localization", "");
  2372. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate"), "set_auto_translate", "is_auto_translating");
  2373. ADD_GROUP("Theme", "theme_");
  2374. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "theme", PROPERTY_HINT_RESOURCE_TYPE, "Theme"), "set_theme", "get_theme");
  2375. ADD_PROPERTY(PropertyInfo(Variant::STRING, "theme_type_variation", PROPERTY_HINT_ENUM_SUGGESTION), "set_theme_type_variation", "get_theme_type_variation");
  2376. ADD_SIGNAL(MethodInfo("window_input", PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")));
  2377. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files")));
  2378. ADD_SIGNAL(MethodInfo("mouse_entered"));
  2379. ADD_SIGNAL(MethodInfo("mouse_exited"));
  2380. ADD_SIGNAL(MethodInfo("focus_entered"));
  2381. ADD_SIGNAL(MethodInfo("focus_exited"));
  2382. ADD_SIGNAL(MethodInfo("close_requested"));
  2383. ADD_SIGNAL(MethodInfo("go_back_requested"));
  2384. ADD_SIGNAL(MethodInfo("visibility_changed"));
  2385. ADD_SIGNAL(MethodInfo("about_to_popup"));
  2386. ADD_SIGNAL(MethodInfo("theme_changed"));
  2387. ADD_SIGNAL(MethodInfo("dpi_changed"));
  2388. ADD_SIGNAL(MethodInfo("titlebar_changed"));
  2389. BIND_CONSTANT(NOTIFICATION_VISIBILITY_CHANGED);
  2390. BIND_CONSTANT(NOTIFICATION_THEME_CHANGED);
  2391. BIND_ENUM_CONSTANT(MODE_WINDOWED);
  2392. BIND_ENUM_CONSTANT(MODE_MINIMIZED);
  2393. BIND_ENUM_CONSTANT(MODE_MAXIMIZED);
  2394. BIND_ENUM_CONSTANT(MODE_FULLSCREEN);
  2395. BIND_ENUM_CONSTANT(MODE_EXCLUSIVE_FULLSCREEN);
  2396. BIND_ENUM_CONSTANT(FLAG_RESIZE_DISABLED);
  2397. BIND_ENUM_CONSTANT(FLAG_BORDERLESS);
  2398. BIND_ENUM_CONSTANT(FLAG_ALWAYS_ON_TOP);
  2399. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  2400. BIND_ENUM_CONSTANT(FLAG_NO_FOCUS);
  2401. BIND_ENUM_CONSTANT(FLAG_POPUP);
  2402. BIND_ENUM_CONSTANT(FLAG_EXTEND_TO_TITLE);
  2403. BIND_ENUM_CONSTANT(FLAG_MOUSE_PASSTHROUGH);
  2404. BIND_ENUM_CONSTANT(FLAG_MAX);
  2405. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_DISABLED);
  2406. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_CANVAS_ITEMS);
  2407. BIND_ENUM_CONSTANT(CONTENT_SCALE_MODE_VIEWPORT);
  2408. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_IGNORE);
  2409. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP);
  2410. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_WIDTH);
  2411. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_KEEP_HEIGHT);
  2412. BIND_ENUM_CONSTANT(CONTENT_SCALE_ASPECT_EXPAND);
  2413. BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_FRACTIONAL);
  2414. BIND_ENUM_CONSTANT(CONTENT_SCALE_STRETCH_INTEGER);
  2415. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_INHERITED);
  2416. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LOCALE);
  2417. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_LTR);
  2418. BIND_ENUM_CONSTANT(LAYOUT_DIRECTION_RTL);
  2419. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_ABSOLUTE);
  2420. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_PRIMARY_SCREEN);
  2421. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_MAIN_WINDOW_SCREEN);
  2422. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_OTHER_SCREEN);
  2423. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_MOUSE_FOCUS);
  2424. BIND_ENUM_CONSTANT(WINDOW_INITIAL_POSITION_CENTER_SCREEN_WITH_KEYBOARD_FOCUS);
  2425. GDVIRTUAL_BIND(_get_contents_minimum_size);
  2426. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_border);
  2427. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Window, embedded_unfocused_border);
  2428. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Window, title_font);
  2429. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Window, title_font_size);
  2430. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_color);
  2431. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_height);
  2432. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Window, title_outline_modulate);
  2433. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, title_outline_size);
  2434. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close);
  2435. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Window, close_pressed);
  2436. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_h_offset);
  2437. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, close_v_offset);
  2438. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Window, resize_margin);
  2439. }
  2440. Window::Window() {
  2441. RenderingServer *rendering_server = RenderingServer::get_singleton();
  2442. if (rendering_server) {
  2443. max_size = rendering_server->get_maximum_viewport_size();
  2444. max_size_used = max_size; // Update max_size_used.
  2445. }
  2446. theme_owner = memnew(ThemeOwner(this));
  2447. RS::get_singleton()->viewport_set_update_mode(get_viewport_rid(), RS::VIEWPORT_UPDATE_DISABLED);
  2448. }
  2449. Window::~Window() {
  2450. memdelete(theme_owner);
  2451. // Resources need to be disconnected.
  2452. for (KeyValue<StringName, Ref<Texture2D>> &E : theme_icon_override) {
  2453. E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2454. }
  2455. for (KeyValue<StringName, Ref<StyleBox>> &E : theme_style_override) {
  2456. E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2457. }
  2458. for (KeyValue<StringName, Ref<Font>> &E : theme_font_override) {
  2459. E.value->disconnect_changed(callable_mp(this, &Window::_notify_theme_override_changed));
  2460. }
  2461. // Then override maps can be simply cleared.
  2462. theme_icon_override.clear();
  2463. theme_style_override.clear();
  2464. theme_font_override.clear();
  2465. theme_font_size_override.clear();
  2466. theme_color_override.clear();
  2467. theme_constant_override.clear();
  2468. }