window.cpp 103 KB

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