popup_menu.cpp 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113
  1. /**************************************************************************/
  2. /* popup_menu.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 "popup_menu.h"
  31. #include "popup_menu.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "scene/gui/menu_bar.h"
  37. #include "scene/gui/panel_container.h"
  38. #include "scene/resources/style_box_flat.h"
  39. #include "scene/theme/theme_db.h"
  40. HashMap<NativeMenu::SystemMenus, PopupMenu *> PopupMenu::system_menus;
  41. bool PopupMenu::_set_item_accelerator(int p_index, const Ref<InputEventKey> &p_ie) {
  42. NativeMenu *nmenu = NativeMenu::get_singleton();
  43. if (p_ie->get_physical_keycode() == Key::NONE && p_ie->get_keycode() == Key::NONE && p_ie->get_key_label() != Key::NONE) {
  44. nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_key_label_with_modifiers());
  45. return true;
  46. } else if (p_ie->get_keycode() != Key::NONE) {
  47. nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_keycode_with_modifiers());
  48. return true;
  49. } else if (p_ie->get_physical_keycode() != Key::NONE) {
  50. nmenu->set_item_accelerator(global_menu, p_index, DisplayServer::get_singleton()->keyboard_get_keycode_from_physical(p_ie->get_physical_keycode_with_modifiers()));
  51. return true;
  52. }
  53. return false;
  54. }
  55. void PopupMenu::_set_item_checkable_type(int p_index, int p_checkable_type) {
  56. switch (p_checkable_type) {
  57. case Item::CHECKABLE_TYPE_NONE: {
  58. set_item_as_checkable(p_index, false);
  59. } break;
  60. case Item::CHECKABLE_TYPE_CHECK_BOX: {
  61. set_item_as_checkable(p_index, true);
  62. } break;
  63. case Item::CHECKABLE_TYPE_RADIO_BUTTON: {
  64. set_item_as_radio_checkable(p_index, true);
  65. } break;
  66. }
  67. }
  68. int PopupMenu::_get_item_checkable_type(int p_index) const {
  69. ERR_FAIL_INDEX_V(p_index, items.size(), Item::CHECKABLE_TYPE_NONE);
  70. return items[p_index].checkable_type;
  71. }
  72. RID PopupMenu::bind_global_menu() {
  73. #ifdef TOOLS_ENABLED
  74. if (is_part_of_edited_scene()) {
  75. return RID();
  76. }
  77. #endif
  78. if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_POPUP_MENU)) {
  79. return RID();
  80. }
  81. if (global_menu.is_valid()) {
  82. return global_menu; // Already bound;
  83. }
  84. NativeMenu *nmenu = NativeMenu::get_singleton();
  85. if (system_menu_id != NativeMenu::INVALID_MENU_ID && nmenu->has_system_menu(system_menu_id)) {
  86. if (system_menus.has(system_menu_id)) {
  87. WARN_PRINT(vformat("Attempting to bind PopupMenu to the system menu %s, but another menu is already bound to it. This menu: %s, current menu: %s", nmenu->get_system_menu_name(system_menu_id), get_description(), system_menus[system_menu_id]->get_description()));
  88. global_menu = nmenu->create_menu();
  89. } else {
  90. system_menus[system_menu_id] = this;
  91. system_menu = nmenu->get_system_menu(system_menu_id);
  92. global_menu = system_menu;
  93. }
  94. } else {
  95. global_menu = nmenu->create_menu();
  96. }
  97. nmenu->set_interface_direction(global_menu, control->is_layout_rtl());
  98. nmenu->set_popup_open_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_popup));
  99. nmenu->set_popup_close_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_close));
  100. for (int i = 0; i < items.size(); i++) {
  101. Item &item = items.write[i];
  102. if (item.separator) {
  103. nmenu->add_separator(global_menu);
  104. } else {
  105. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), item.shortcut_is_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), i);
  106. if (item.submenu) {
  107. RID submenu_rid = item.submenu->bind_global_menu();
  108. nmenu->set_item_submenu(global_menu, index, submenu_rid);
  109. item.submenu_bound = true;
  110. }
  111. if (item.checkable_type == Item::CHECKABLE_TYPE_CHECK_BOX) {
  112. nmenu->set_item_checkable(global_menu, index, true);
  113. } else if (item.checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON) {
  114. nmenu->set_item_radio_checkable(global_menu, index, true);
  115. }
  116. nmenu->set_item_checked(global_menu, index, item.checked);
  117. nmenu->set_item_disabled(global_menu, index, item.disabled);
  118. nmenu->set_item_max_states(global_menu, index, item.max_states);
  119. nmenu->set_item_icon(global_menu, index, item.icon);
  120. nmenu->set_item_state(global_menu, index, item.state);
  121. nmenu->set_item_indentation_level(global_menu, index, item.indent);
  122. nmenu->set_item_tooltip(global_menu, index, item.tooltip);
  123. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  124. Array events = item.shortcut->get_events();
  125. for (int j = 0; j < events.size(); j++) {
  126. Ref<InputEventKey> ie = events[j];
  127. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  128. break;
  129. }
  130. }
  131. } else if (item.accel != Key::NONE) {
  132. nmenu->set_item_accelerator(global_menu, index, item.accel);
  133. }
  134. }
  135. }
  136. return global_menu;
  137. }
  138. void PopupMenu::unbind_global_menu() {
  139. if (global_menu.is_null()) {
  140. return;
  141. }
  142. if (global_menu == system_menu && system_menus[system_menu_id] == this) {
  143. system_menus.erase(system_menu_id);
  144. }
  145. for (int i = 0; i < items.size(); i++) {
  146. Item &item = items.write[i];
  147. if (item.submenu) {
  148. item.submenu->unbind_global_menu();
  149. item.submenu_bound = false;
  150. }
  151. }
  152. if (system_menu != global_menu) {
  153. NativeMenu::get_singleton()->free_menu(global_menu);
  154. } else {
  155. NativeMenu::get_singleton()->clear(global_menu);
  156. }
  157. system_menu = RID();
  158. global_menu = RID();
  159. }
  160. bool PopupMenu::is_system_menu() const {
  161. return (global_menu == system_menu) && (system_menu_id != NativeMenu::INVALID_MENU_ID);
  162. }
  163. void PopupMenu::set_system_menu(NativeMenu::SystemMenus p_system_menu_id) {
  164. if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) {
  165. unbind_global_menu();
  166. }
  167. system_menu_id = p_system_menu_id;
  168. if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) {
  169. bind_global_menu();
  170. }
  171. }
  172. NativeMenu::SystemMenus PopupMenu::get_system_menu() const {
  173. return system_menu_id;
  174. }
  175. String PopupMenu::_get_accel_text(const Item &p_item) const {
  176. if (p_item.shortcut.is_valid()) {
  177. return p_item.shortcut->get_as_text();
  178. } else if (p_item.accel != Key::NONE) {
  179. return keycode_get_string(p_item.accel);
  180. }
  181. return String();
  182. }
  183. Size2 PopupMenu::_get_item_icon_size(int p_idx) const {
  184. const PopupMenu::Item &item = items[p_idx];
  185. Size2 icon_size = item.get_icon_size();
  186. int max_width = 0;
  187. if (theme_cache.icon_max_width > 0) {
  188. max_width = theme_cache.icon_max_width;
  189. }
  190. if (item.icon_max_width > 0 && (max_width == 0 || item.icon_max_width < max_width)) {
  191. max_width = item.icon_max_width;
  192. }
  193. if (max_width > 0 && icon_size.width > max_width) {
  194. icon_size.height = icon_size.height * max_width / icon_size.width;
  195. icon_size.width = max_width;
  196. }
  197. return icon_size;
  198. }
  199. Size2 PopupMenu::_get_contents_minimum_size() const {
  200. Size2 minsize = theme_cache.panel_style->get_minimum_size();
  201. minsize.width += scroll_container->get_v_scroll_bar()->get_size().width;
  202. // Take shadows into account.
  203. minsize.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
  204. minsize.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
  205. float max_w = 0.0;
  206. float icon_w = 0.0;
  207. int check_w = MAX(theme_cache.checked->get_width(), theme_cache.radio_checked->get_width()) + theme_cache.h_separation;
  208. int accel_max_w = 0;
  209. bool has_check = false;
  210. for (int i = 0; i < items.size(); i++) {
  211. Size2 item_size;
  212. _shape_item(i);
  213. Size2 icon_size = _get_item_icon_size(i);
  214. item_size.height = _get_item_height(i);
  215. icon_w = MAX(icon_size.width, icon_w);
  216. item_size.width += items[i].indent * theme_cache.indent;
  217. if (items[i].checkable_type && !items[i].separator) {
  218. has_check = true;
  219. }
  220. item_size.width += items[i].text_buf->get_size().x;
  221. item_size.height += theme_cache.v_separation;
  222. if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  223. int accel_w = theme_cache.h_separation * 2;
  224. accel_w += items[i].accel_text_buf->get_size().x;
  225. accel_max_w = MAX(accel_w, accel_max_w);
  226. }
  227. if (items[i].submenu) {
  228. item_size.width += theme_cache.submenu->get_width();
  229. }
  230. max_w = MAX(max_w, item_size.width);
  231. minsize.height += item_size.height;
  232. }
  233. int item_side_padding = theme_cache.item_start_padding + theme_cache.item_end_padding;
  234. minsize.width += max_w + icon_w + accel_max_w + item_side_padding;
  235. if (has_check) {
  236. minsize.width += check_w;
  237. }
  238. if (is_inside_tree()) {
  239. int height_limit = get_usable_parent_rect().size.height;
  240. if (minsize.height > height_limit) {
  241. minsize.height = height_limit;
  242. }
  243. }
  244. minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing.
  245. return minsize;
  246. }
  247. int PopupMenu::_get_item_height(int p_idx) const {
  248. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  249. Size2 icon_size = _get_item_icon_size(p_idx);
  250. int icon_height = icon_size.height;
  251. if (items[p_idx].checkable_type && !items[p_idx].separator) {
  252. icon_height = MAX(icon_height, MAX(theme_cache.checked->get_height(), theme_cache.radio_checked->get_height()));
  253. }
  254. int text_height = items[p_idx].text_buf->get_size().height;
  255. if (text_height == 0 && !items[p_idx].separator) {
  256. text_height = theme_cache.font->get_height(theme_cache.font_size);
  257. }
  258. int separator_height = 0;
  259. if (items[p_idx].separator) {
  260. separator_height = MAX(theme_cache.separator_style->get_minimum_size().height, MAX(theme_cache.labeled_separator_left->get_minimum_size().height, theme_cache.labeled_separator_right->get_minimum_size().height));
  261. }
  262. return MAX(separator_height, MAX(text_height, icon_height));
  263. }
  264. int PopupMenu::_get_items_total_height() const {
  265. // Get total height of all items by taking max of icon height and font height
  266. int items_total_height = 0;
  267. for (int i = 0; i < items.size(); i++) {
  268. items_total_height += _get_item_height(i) + theme_cache.v_separation;
  269. }
  270. return items_total_height;
  271. }
  272. int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
  273. // Make the item area exclude shadows and the vertical margins and scrollbar.
  274. Rect2 item_clickable_area = panel->get_global_rect();
  275. if (scroll_container->get_v_scroll_bar()->is_visible_in_tree()) {
  276. const int scroll_width = scroll_container->get_v_scroll_bar()->get_size().width;
  277. if (is_layout_rtl()) {
  278. item_clickable_area.position.x += scroll_width;
  279. item_clickable_area.size.width -= scroll_width;
  280. }
  281. item_clickable_area.size.width -= scroll_width;
  282. }
  283. float win_scale = get_content_scale_factor();
  284. item_clickable_area.position.x += theme_cache.panel_style->get_margin(SIDE_LEFT);
  285. item_clickable_area.position.y += theme_cache.panel_style->get_margin(SIDE_TOP);
  286. item_clickable_area.position *= win_scale;
  287. item_clickable_area.size.y -= theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM);
  288. item_clickable_area.size *= win_scale;
  289. if (!item_clickable_area.has_point(p_over)) {
  290. return -1;
  291. }
  292. float ofs = item_clickable_area.position.y + (float)theme_cache.v_separation * win_scale * 0.5;
  293. for (int i = 0; i < items.size(); i++) {
  294. ofs += i > 0 ? (float)theme_cache.v_separation * win_scale : (float)theme_cache.v_separation * win_scale * 0.5;
  295. ofs += _get_item_height(i) * win_scale;
  296. if (p_over.y - control->get_position().y * win_scale < ofs) {
  297. return i;
  298. }
  299. }
  300. return -1;
  301. }
  302. void PopupMenu::_activate_submenu(int p_over, bool p_by_keyboard) {
  303. PopupMenu *submenu_popup = items[p_over].submenu;
  304. if (submenu_popup->is_visible()) {
  305. return; // Already visible.
  306. }
  307. const float win_scale = get_content_scale_factor();
  308. const Point2 panel_ofs_start = Point2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * win_scale;
  309. const Point2 panel_ofs_end = Point2(panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_BOTTOM)).abs() * win_scale;
  310. const Point2 this_pos = get_position() + Point2(0, panel_ofs_start.y + theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale);
  311. Rect2 this_rect(this_pos, get_size());
  312. const float scroll_offset = control->get_position().y;
  313. const float scaled_ofs_cache = items[p_over]._ofs_cache * win_scale;
  314. const float scaled_height_cache = items[p_over]._height_cache * win_scale;
  315. submenu_popup->reset_size(); // Shrink the popup size to its contents.
  316. const Size2 submenu_size = submenu_popup->get_size();
  317. // Calculate the submenu's position.
  318. Point2 submenu_pos(0, -submenu_popup->get_theme_stylebox(SceneStringName(panel))->get_margin(SIDE_TOP) * submenu_popup->get_content_scale_factor());
  319. Rect2i screen_rect = is_embedded() ? Rect2i(get_embedder()->get_visible_rect()) : get_parent_rect();
  320. if (is_layout_rtl()) {
  321. submenu_pos += this_pos + Point2(-submenu_size.width + panel_ofs_end.x, scaled_ofs_cache + scroll_offset - theme_cache.v_separation / 2);
  322. if (submenu_pos.x < screen_rect.position.x) {
  323. submenu_pos.x = this_pos.x + this_rect.size.width - panel_ofs_start.x;
  324. }
  325. this_rect.position.x += panel_ofs_end.x;
  326. } else {
  327. submenu_pos += this_pos + Point2(this_rect.size.width - panel_ofs_end.x, scaled_ofs_cache + scroll_offset - theme_cache.v_separation / 2);
  328. if (submenu_pos.x + submenu_size.width > screen_rect.position.x + screen_rect.size.width) {
  329. submenu_pos.x = this_pos.x - submenu_size.width + panel_ofs_start.x;
  330. }
  331. this_rect.position.x += panel_ofs_start.x;
  332. }
  333. submenu_popup->set_position(submenu_pos);
  334. PopupMenu *submenu_pum = Object::cast_to<PopupMenu>(submenu_popup);
  335. if (!submenu_pum) {
  336. submenu_popup->popup();
  337. return;
  338. }
  339. submenu_pum->activated_by_keyboard = p_by_keyboard;
  340. // If not triggered by the mouse, start the popup with its first enabled item focused.
  341. if (p_by_keyboard) {
  342. for (int i = 0; i < submenu_pum->get_item_count(); i++) {
  343. if (!submenu_pum->is_item_disabled(i)) {
  344. submenu_pum->set_focused_item(i);
  345. break;
  346. }
  347. }
  348. }
  349. submenu_pum->popup();
  350. // Set autohide areas.
  351. const Rect2 safe_area(get_position(), get_size());
  352. Viewport *vp = submenu_popup->get_embedder();
  353. if (vp) {
  354. vp->subwindow_set_popup_safe_rect(submenu_popup, safe_area);
  355. } else {
  356. DisplayServer::get_singleton()->window_set_popup_safe_rect(submenu_popup->get_window_id(), safe_area);
  357. }
  358. this_rect.position -= submenu_pum->get_position(); // Make the position of the parent popup relative to submenu popup.
  359. this_rect.size.width -= panel_ofs_start.x + panel_ofs_end.x;
  360. this_rect.size.height -= panel_ofs_end.y + (theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM)) * win_scale;
  361. // Autohide area above the submenu item.
  362. submenu_pum->clear_autohide_areas();
  363. submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y - theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale,
  364. this_rect.size.x, scaled_ofs_cache + scroll_offset + theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale - theme_cache.v_separation / 2));
  365. // If there is an area below the submenu item, add an autohide area there.
  366. if (scaled_ofs_cache + scaled_height_cache + scroll_offset <= control->get_size().height * win_scale) {
  367. const int from = scaled_ofs_cache + scaled_height_cache + scroll_offset + theme_cache.v_separation / 2;
  368. submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + from, this_rect.size.x, this_rect.size.y - from));
  369. }
  370. }
  371. void PopupMenu::_parent_focused() {
  372. if (is_embedded()) {
  373. Point2 mouse_pos_adjusted;
  374. Window *window_parent = Object::cast_to<Window>(get_parent()->get_viewport());
  375. while (window_parent) {
  376. if (!window_parent->is_embedded()) {
  377. mouse_pos_adjusted += window_parent->get_position();
  378. break;
  379. }
  380. window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport());
  381. }
  382. Rect2 safe_area = get_embedder()->subwindow_get_popup_safe_rect(this);
  383. Point2 pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted;
  384. if (safe_area == Rect2i() || !safe_area.has_point(pos)) {
  385. Popup::_parent_focused();
  386. } else {
  387. grab_focus();
  388. }
  389. }
  390. }
  391. void PopupMenu::_submenu_timeout() {
  392. if (mouse_over == submenu_over) {
  393. _activate_submenu(mouse_over);
  394. }
  395. submenu_over = -1;
  396. }
  397. void PopupMenu::_input_from_window(const Ref<InputEvent> &p_event) {
  398. if (p_event.is_valid()) {
  399. _input_from_window_internal(p_event);
  400. } else {
  401. WARN_PRINT_ONCE("PopupMenu has received an invalid InputEvent. Consider filtering out invalid events.");
  402. }
  403. Popup::_input_from_window(p_event);
  404. }
  405. void PopupMenu::_input_from_window_internal(const Ref<InputEvent> &p_event) {
  406. if (!items.is_empty()) {
  407. Input *input = Input::get_singleton();
  408. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  409. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  410. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  411. if (p_event->is_action("ui_down", true) && p_event->is_pressed()) {
  412. if (is_joypad_event) {
  413. if (!input->is_action_just_pressed("ui_down", true)) {
  414. return;
  415. }
  416. set_process_internal(true);
  417. }
  418. int search_from = mouse_over + 1;
  419. if (search_from >= items.size()) {
  420. search_from = 0;
  421. }
  422. bool match_found = false;
  423. for (int i = search_from; i < items.size(); i++) {
  424. if (!items[i].separator && !items[i].disabled) {
  425. mouse_over = i;
  426. emit_signal(SNAME("id_focused"), items[i].id);
  427. scroll_to_item(i);
  428. control->queue_redraw();
  429. set_input_as_handled();
  430. match_found = true;
  431. break;
  432. }
  433. }
  434. if (!match_found) {
  435. // If the last item is not selectable, try re-searching from the start.
  436. for (int i = 0; i < search_from; i++) {
  437. if (!items[i].separator && !items[i].disabled) {
  438. mouse_over = i;
  439. emit_signal(SNAME("id_focused"), items[i].id);
  440. scroll_to_item(i);
  441. control->queue_redraw();
  442. set_input_as_handled();
  443. break;
  444. }
  445. }
  446. }
  447. } else if (p_event->is_action("ui_up", true) && p_event->is_pressed()) {
  448. if (is_joypad_event) {
  449. if (!input->is_action_just_pressed("ui_up", true)) {
  450. return;
  451. }
  452. set_process_internal(true);
  453. }
  454. int search_from = mouse_over - 1;
  455. if (search_from < 0) {
  456. search_from = items.size() - 1;
  457. }
  458. bool match_found = false;
  459. for (int i = search_from; i >= 0; i--) {
  460. if (!items[i].separator && !items[i].disabled) {
  461. mouse_over = i;
  462. emit_signal(SNAME("id_focused"), items[i].id);
  463. scroll_to_item(i);
  464. control->queue_redraw();
  465. set_input_as_handled();
  466. match_found = true;
  467. break;
  468. }
  469. }
  470. if (!match_found) {
  471. // If the first item is not selectable, try re-searching from the end.
  472. for (int i = items.size() - 1; i >= search_from; i--) {
  473. if (!items[i].separator && !items[i].disabled) {
  474. mouse_over = i;
  475. emit_signal(SNAME("id_focused"), items[i].id);
  476. scroll_to_item(i);
  477. control->queue_redraw();
  478. set_input_as_handled();
  479. break;
  480. }
  481. }
  482. }
  483. } else if (p_event->is_action("ui_left", true) && p_event->is_pressed()) {
  484. Node *n = get_parent();
  485. if (n) {
  486. if (Object::cast_to<PopupMenu>(n)) {
  487. hide();
  488. set_input_as_handled();
  489. } else if (Object::cast_to<MenuBar>(n)) {
  490. Object::cast_to<MenuBar>(n)->gui_input(p_event);
  491. set_input_as_handled();
  492. return;
  493. }
  494. }
  495. } else if (p_event->is_action("ui_right", true) && p_event->is_pressed()) {
  496. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu && submenu_over != mouse_over) {
  497. _activate_submenu(mouse_over, true);
  498. set_input_as_handled();
  499. } else {
  500. Node *n = get_parent();
  501. if (n && Object::cast_to<MenuBar>(n)) {
  502. Object::cast_to<MenuBar>(n)->gui_input(p_event);
  503. set_input_as_handled();
  504. return;
  505. }
  506. }
  507. } else if (p_event->is_action("ui_accept", true) && p_event->is_pressed()) {
  508. if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
  509. if (items[mouse_over].submenu && submenu_over != mouse_over) {
  510. _activate_submenu(mouse_over, true);
  511. } else {
  512. activate_item(mouse_over);
  513. }
  514. set_input_as_handled();
  515. }
  516. }
  517. }
  518. // Make the item area exclude shadows and the vertical margins and scrollbar.
  519. Rect2 item_clickable_area = panel->get_global_rect();
  520. if (scroll_container->get_v_scroll_bar()->is_visible_in_tree()) {
  521. int scroll_width = scroll_container->get_v_scroll_bar()->get_size().width;
  522. if (is_layout_rtl()) {
  523. item_clickable_area.position.x += scroll_width;
  524. item_clickable_area.size.width -= scroll_width;
  525. }
  526. item_clickable_area.size.width -= scroll_width;
  527. }
  528. float win_scale = get_content_scale_factor();
  529. item_clickable_area.position.x += theme_cache.panel_style->get_margin(SIDE_LEFT);
  530. item_clickable_area.position.y += theme_cache.panel_style->get_margin(SIDE_TOP);
  531. item_clickable_area.position *= win_scale;
  532. item_clickable_area.size.y -= theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM);
  533. item_clickable_area.size *= win_scale;
  534. Ref<InputEventMouseButton> b = p_event;
  535. if (b.is_valid()) {
  536. MouseButton button_idx = b->get_button_index();
  537. // Activate the item on release of either the left mouse button or
  538. // any mouse button held down when the popup was opened.
  539. // This allows for opening the popup and triggering an action in a single mouse click.
  540. if (button_idx == MouseButton::LEFT || initial_button_mask.has_flag(mouse_button_to_mask(button_idx))) {
  541. if (b->is_pressed()) {
  542. during_grabbed_click = false;
  543. is_scrolling = is_layout_rtl() ? b->get_position().x < item_clickable_area.position.x : b->get_position().x > item_clickable_area.size.width;
  544. // Hide it if the shadows have been clicked.
  545. if (get_flag(FLAG_POPUP)) {
  546. Rect2 panel_area = panel->get_global_rect();
  547. panel_area.position *= win_scale;
  548. panel_area.size *= win_scale;
  549. if (!panel_area.has_point(b->get_position())) {
  550. _close_pressed();
  551. return;
  552. }
  553. }
  554. if (!item_clickable_area.has_point(b->get_position())) {
  555. return;
  556. }
  557. _mouse_over_update(b->get_position());
  558. } else {
  559. if (is_scrolling) {
  560. is_scrolling = false;
  561. return;
  562. }
  563. bool was_during_grabbed_click = during_grabbed_click;
  564. during_grabbed_click = false;
  565. initial_button_mask.clear();
  566. if (!item_clickable_area.has_point(b->get_position())) {
  567. return;
  568. }
  569. // Disable clicks under a time threshold to avoid selection right when opening the popup.
  570. if (was_during_grabbed_click && OS::get_singleton()->get_ticks_msec() - popup_time_msec < 400) {
  571. return;
  572. }
  573. int over = _get_mouse_over(b->get_position());
  574. if (over < 0) {
  575. if (!was_during_grabbed_click) {
  576. hide();
  577. }
  578. return;
  579. }
  580. if (items[over].separator || items[over].disabled) {
  581. return;
  582. }
  583. if (items[over].submenu) {
  584. _activate_submenu(over);
  585. return;
  586. }
  587. activate_item(over);
  588. }
  589. }
  590. }
  591. Ref<InputEventMouseMotion> m = p_event;
  592. if (m.is_valid()) {
  593. if (m->get_velocity().is_zero_approx()) {
  594. return;
  595. }
  596. activated_by_keyboard = false;
  597. for (const Rect2 &E : autohide_areas) {
  598. if (!scroll_container->get_global_rect().has_point(m->get_position()) && E.has_point(m->get_position())) {
  599. // The mouse left the safe area, prepare to close.
  600. _close_pressed();
  601. return;
  602. }
  603. }
  604. if (!minimum_lifetime_timer->is_stopped()) {
  605. // The mouse left the safe area, but came back again, so cancel the auto-closing.
  606. minimum_lifetime_timer->stop();
  607. }
  608. if (mouse_over == -1 && !item_clickable_area.has_point(m->get_position())) {
  609. return;
  610. }
  611. _mouse_over_update(m->get_position());
  612. }
  613. Ref<InputEventKey> k = p_event;
  614. if (allow_search && k.is_valid() && k->get_unicode() && k->is_pressed()) {
  615. uint64_t now = OS::get_singleton()->get_ticks_msec();
  616. uint64_t diff = now - search_time_msec;
  617. uint64_t max_interval = uint64_t(GLOBAL_GET("gui/timers/incremental_search_max_interval_msec"));
  618. search_time_msec = now;
  619. if (diff > max_interval) {
  620. search_string = "";
  621. }
  622. if (String::chr(k->get_unicode()) != search_string) {
  623. search_string += String::chr(k->get_unicode());
  624. }
  625. for (int i = mouse_over + 1; i <= items.size(); i++) {
  626. if (i == items.size()) {
  627. if (mouse_over <= 0) {
  628. break;
  629. } else {
  630. i = 0;
  631. }
  632. }
  633. if (i == mouse_over) {
  634. break;
  635. }
  636. if (items[i].text.findn(search_string) == 0) {
  637. mouse_over = i;
  638. emit_signal(SNAME("id_focused"), items[i].id);
  639. scroll_to_item(i);
  640. control->queue_redraw();
  641. set_input_as_handled();
  642. break;
  643. }
  644. }
  645. }
  646. }
  647. void PopupMenu::_mouse_over_update(const Point2 &p_over) {
  648. int over = _get_mouse_over(p_over);
  649. int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].id >= 0 ? items[over].id : over);
  650. if (id < 0) {
  651. mouse_over = -1;
  652. control->queue_redraw();
  653. return;
  654. }
  655. if (!is_scrolling && items[over].submenu && submenu_over != over) {
  656. submenu_over = over;
  657. submenu_timer->start();
  658. }
  659. if (over != mouse_over) {
  660. mouse_over = over;
  661. control->queue_redraw();
  662. }
  663. }
  664. void PopupMenu::_draw_items() {
  665. control->set_custom_minimum_size(Size2(0, _get_items_total_height()));
  666. RID ci = control->get_canvas_item();
  667. // Space between the item content and the sides of popup menu.
  668. bool rtl = control->is_layout_rtl();
  669. // In Item::checkable_type enum order (less the non-checkable member), with disabled repeated at the end.
  670. Ref<Texture2D> check[] = { theme_cache.checked, theme_cache.radio_checked, theme_cache.checked_disabled, theme_cache.radio_checked_disabled };
  671. Ref<Texture2D> uncheck[] = { theme_cache.unchecked, theme_cache.radio_unchecked, theme_cache.unchecked_disabled, theme_cache.radio_unchecked_disabled };
  672. Ref<Texture2D> submenu;
  673. if (rtl) {
  674. submenu = theme_cache.submenu_mirrored;
  675. } else {
  676. submenu = theme_cache.submenu;
  677. }
  678. float display_width = control->get_size().width;
  679. // Find the widest icon and whether any items have a checkbox, and store the offsets for each.
  680. float icon_ofs = 0.0;
  681. bool has_check = false;
  682. for (int i = 0; i < items.size(); i++) {
  683. if (items[i].separator) {
  684. continue;
  685. }
  686. Size2 icon_size = _get_item_icon_size(i);
  687. icon_ofs = MAX(icon_size.width, icon_ofs);
  688. if (items[i].checkable_type) {
  689. has_check = true;
  690. }
  691. }
  692. if (icon_ofs > 0.0) {
  693. icon_ofs += theme_cache.h_separation;
  694. }
  695. float check_ofs = 0.0;
  696. if (has_check) {
  697. for (int i = 0; i < 4; i++) {
  698. check_ofs = MAX(check_ofs, check[i]->get_width());
  699. check_ofs = MAX(check_ofs, uncheck[i]->get_width());
  700. }
  701. check_ofs += theme_cache.h_separation;
  702. }
  703. Point2 ofs;
  704. // Loop through all items and draw each.
  705. for (int i = 0; i < items.size(); i++) {
  706. // For the first item only add half a separation. For all other items, add a whole separation to the offset.
  707. ofs.y += i > 0 ? theme_cache.v_separation : (float)theme_cache.v_separation / 2;
  708. _shape_item(i);
  709. Point2 item_ofs = ofs;
  710. Size2 icon_size = _get_item_icon_size(i);
  711. float h = _get_item_height(i);
  712. if (i == mouse_over) {
  713. theme_cache.hover_style->draw(ci, Rect2(item_ofs + Point2(0, -theme_cache.v_separation / 2), Size2(display_width, h + theme_cache.v_separation)));
  714. }
  715. String text = items[i].xl_text;
  716. // Separator
  717. item_ofs.x += items[i].indent * theme_cache.indent;
  718. if (items[i].separator) {
  719. if (!text.is_empty() || items[i].icon.is_valid()) {
  720. int content_size = items[i].text_buf->get_size().width + theme_cache.h_separation * 2;
  721. if (items[i].icon.is_valid()) {
  722. content_size += icon_size.width + theme_cache.h_separation;
  723. }
  724. int content_center = display_width / 2;
  725. int content_left = content_center - content_size / 2;
  726. int content_right = content_center + content_size / 2;
  727. if (content_left > item_ofs.x) {
  728. int sep_h = theme_cache.labeled_separator_left->get_minimum_size().height;
  729. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  730. theme_cache.labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(MAX(0, content_left - item_ofs.x), sep_h)));
  731. }
  732. if (content_right < display_width) {
  733. int sep_h = theme_cache.labeled_separator_right->get_minimum_size().height;
  734. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  735. theme_cache.labeled_separator_right->draw(ci, Rect2(Point2(content_right, item_ofs.y + sep_ofs), Size2(MAX(0, display_width - content_right), sep_h)));
  736. }
  737. } else {
  738. int sep_h = theme_cache.separator_style->get_minimum_size().height;
  739. int sep_ofs = Math::floor((h - sep_h) / 2.0);
  740. theme_cache.separator_style->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(display_width, sep_h)));
  741. }
  742. }
  743. Color icon_color = items[i].icon_modulate;
  744. // For non-separator items, add some padding for the content.
  745. if (!items[i].separator) {
  746. item_ofs.x += theme_cache.item_start_padding;
  747. }
  748. // Checkboxes
  749. if (items[i].checkable_type && !items[i].separator) {
  750. int disabled = int(items[i].disabled) * 2;
  751. Texture2D *icon = (items[i].checked ? check[items[i].checkable_type - 1 + disabled] : uncheck[items[i].checkable_type - 1 + disabled]).ptr();
  752. if (rtl) {
  753. icon->draw(ci, Size2(control->get_size().width - item_ofs.x - icon->get_width(), item_ofs.y) + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  754. } else {
  755. icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
  756. }
  757. }
  758. int separator_ofs = (display_width - items[i].text_buf->get_size().width) / 2;
  759. // Icon
  760. if (items[i].icon.is_valid()) {
  761. const Point2 icon_offset = Point2(0, Math::floor((h - icon_size.height) / 2.0));
  762. Point2 icon_pos;
  763. if (items[i].separator) {
  764. separator_ofs -= (icon_size.width + theme_cache.h_separation) / 2;
  765. if (rtl) {
  766. icon_pos = Size2(control->get_size().width - item_ofs.x - separator_ofs - icon_size.width, item_ofs.y);
  767. } else {
  768. icon_pos = item_ofs + Size2(separator_ofs, 0);
  769. separator_ofs += icon_size.width + theme_cache.h_separation;
  770. }
  771. } else {
  772. if (rtl) {
  773. icon_pos = Size2(control->get_size().width - item_ofs.x - check_ofs - icon_size.width, item_ofs.y);
  774. } else {
  775. icon_pos = item_ofs + Size2(check_ofs, 0);
  776. }
  777. }
  778. items[i].icon->draw_rect(ci, Rect2(icon_pos + icon_offset, icon_size), false, icon_color);
  779. }
  780. // Submenu arrow on right hand side.
  781. if (items[i].submenu) {
  782. if (rtl) {
  783. submenu->draw(ci, Point2(theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  784. } else {
  785. submenu->draw(ci, Point2(display_width - theme_cache.panel_style->get_margin(SIDE_RIGHT) - submenu->get_width() - theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
  786. }
  787. }
  788. // Text
  789. if (items[i].separator) {
  790. if (!text.is_empty()) {
  791. Vector2 text_pos = Point2(separator_ofs, item_ofs.y + Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  792. if (theme_cache.font_separator_outline_size > 0 && theme_cache.font_separator_outline_color.a > 0) {
  793. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_separator_outline_size, theme_cache.font_separator_outline_color);
  794. }
  795. items[i].text_buf->draw(ci, text_pos, theme_cache.font_separator_color);
  796. }
  797. } else {
  798. item_ofs.x += icon_ofs + check_ofs;
  799. if (rtl) {
  800. Vector2 text_pos = Size2(control->get_size().width - items[i].text_buf->get_size().width - item_ofs.x, item_ofs.y) + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  801. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  802. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  803. }
  804. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_color));
  805. } else {
  806. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  807. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  808. items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  809. }
  810. items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_color));
  811. }
  812. }
  813. // Accelerator / Shortcut
  814. if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
  815. if (rtl) {
  816. item_ofs.x = theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.item_end_padding;
  817. } else {
  818. item_ofs.x = display_width - theme_cache.panel_style->get_margin(SIDE_RIGHT) - items[i].accel_text_buf->get_size().x - theme_cache.item_end_padding;
  819. }
  820. Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
  821. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  822. items[i].accel_text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  823. }
  824. items[i].accel_text_buf->draw(ci, text_pos, i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_accelerator_color);
  825. }
  826. // Cache the item vertical offset from the first item and the height.
  827. items.write[i]._ofs_cache = ofs.y;
  828. items.write[i]._height_cache = h;
  829. ofs.y += h;
  830. }
  831. }
  832. void PopupMenu::_minimum_lifetime_timeout() {
  833. close_allowed = true;
  834. // If the mouse still isn't in this popup after timer expires, close.
  835. if (!activated_by_keyboard && !get_visible_rect().has_point(get_mouse_position())) {
  836. _close_pressed();
  837. }
  838. }
  839. void PopupMenu::_close_pressed() {
  840. // Only apply minimum lifetime to submenus.
  841. PopupMenu *parent_pum = Object::cast_to<PopupMenu>(get_parent());
  842. if (!parent_pum) {
  843. Popup::_close_pressed();
  844. return;
  845. }
  846. // If the timer has expired, close. If timer is still running, do nothing.
  847. if (close_allowed) {
  848. close_allowed = false;
  849. Popup::_close_pressed();
  850. } else if (minimum_lifetime_timer->is_stopped()) {
  851. minimum_lifetime_timer->start();
  852. }
  853. }
  854. void PopupMenu::_shape_item(int p_idx) const {
  855. if (items.write[p_idx].dirty) {
  856. items.write[p_idx].text_buf->clear();
  857. Ref<Font> font = items[p_idx].separator ? theme_cache.font_separator : theme_cache.font;
  858. int font_size = items[p_idx].separator ? theme_cache.font_separator_size : theme_cache.font_size;
  859. if (items[p_idx].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  860. items.write[p_idx].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  861. } else {
  862. items.write[p_idx].text_buf->set_direction((TextServer::Direction)items[p_idx].text_direction);
  863. }
  864. items.write[p_idx].text_buf->add_string(items.write[p_idx].xl_text, font, font_size, items[p_idx].language);
  865. items.write[p_idx].accel_text_buf->clear();
  866. items.write[p_idx].accel_text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  867. items.write[p_idx].accel_text_buf->add_string(_get_accel_text(items.write[p_idx]), font, font_size);
  868. items.write[p_idx].dirty = false;
  869. }
  870. }
  871. void PopupMenu::_menu_changed() {
  872. emit_signal(SNAME("menu_changed"));
  873. }
  874. void PopupMenu::_update_shadow_offsets() const {
  875. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !is_embedded()) {
  876. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  877. return;
  878. }
  879. Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  880. if (sb.is_null()) {
  881. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  882. return;
  883. }
  884. const int shadow_size = sb->get_shadow_size();
  885. if (shadow_size == 0) {
  886. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  887. return;
  888. }
  889. // Offset the background panel so it leaves space inside the window for the shadows to be drawn.
  890. const Point2 shadow_offset = sb->get_shadow_offset();
  891. if (is_layout_rtl()) {
  892. panel->set_offset(SIDE_LEFT, shadow_size + shadow_offset.x);
  893. panel->set_offset(SIDE_RIGHT, -shadow_size + shadow_offset.x);
  894. } else {
  895. panel->set_offset(SIDE_LEFT, shadow_size - shadow_offset.x);
  896. panel->set_offset(SIDE_RIGHT, -shadow_size - shadow_offset.x);
  897. }
  898. panel->set_offset(SIDE_TOP, shadow_size - shadow_offset.y);
  899. panel->set_offset(SIDE_BOTTOM, -shadow_size - shadow_offset.y);
  900. }
  901. Rect2i PopupMenu::_popup_adjust_rect() const {
  902. Rect2i current = Popup::_popup_adjust_rect();
  903. if (current == Rect2i()) {
  904. return current;
  905. }
  906. pre_popup_rect = current;
  907. _update_shadow_offsets();
  908. if (is_layout_rtl()) {
  909. current.position -= Vector2(ABS(panel->get_offset(SIDE_RIGHT)), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  910. } else {
  911. current.position -= Vector2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * get_content_scale_factor();
  912. }
  913. current.size += Vector2(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM)) * get_content_scale_factor();
  914. return current;
  915. }
  916. void PopupMenu::add_child_notify(Node *p_child) {
  917. Window::add_child_notify(p_child);
  918. if (global_menu.is_valid()) {
  919. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  920. for (int i = 0; i < items.size(); i++) {
  921. if (items[i].submenu == p_child) {
  922. RID submenu_rid = pm->bind_global_menu();
  923. NativeMenu::get_singleton()->set_item_submenu(global_menu, i, submenu_rid);
  924. items.write[i].submenu_bound = true;
  925. }
  926. }
  927. }
  928. _menu_changed();
  929. }
  930. void PopupMenu::remove_child_notify(Node *p_child) {
  931. Window::remove_child_notify(p_child);
  932. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  933. if (!pm) {
  934. return;
  935. }
  936. if (Object::cast_to<PopupMenu>(p_child) && global_menu.is_valid()) {
  937. for (int i = 0; i < items.size(); i++) {
  938. if (items[i].submenu == p_child) {
  939. NativeMenu::get_singleton()->set_item_submenu(global_menu, i, RID());
  940. items.write[i].submenu_bound = false;
  941. }
  942. }
  943. pm->unbind_global_menu();
  944. }
  945. _menu_changed();
  946. }
  947. void PopupMenu::_notification(int p_what) {
  948. switch (p_what) {
  949. case NOTIFICATION_ENTER_TREE: {
  950. PopupMenu *pm = Object::cast_to<PopupMenu>(get_parent());
  951. if (pm) {
  952. // Inherit submenu's popup delay time from parent menu.
  953. float pm_delay = pm->get_submenu_popup_delay();
  954. set_submenu_popup_delay(pm_delay);
  955. }
  956. if (system_menu_id != NativeMenu::INVALID_MENU_ID) {
  957. bind_global_menu();
  958. }
  959. } break;
  960. case NOTIFICATION_EXIT_TREE: {
  961. if (system_menu_id != NativeMenu::INVALID_MENU_ID) {
  962. unbind_global_menu();
  963. }
  964. } break;
  965. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  966. case NOTIFICATION_THEME_CHANGED: {
  967. panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);
  968. if (is_visible()) {
  969. _update_shadow_offsets();
  970. }
  971. #ifdef TOOLS_ENABLED
  972. update_configuration_warnings();
  973. #endif
  974. [[fallthrough]];
  975. }
  976. case NOTIFICATION_TRANSLATION_CHANGED: {
  977. NativeMenu *nmenu = NativeMenu::get_singleton();
  978. bool is_global = global_menu.is_valid();
  979. if (is_global) {
  980. nmenu->set_interface_direction(global_menu, control->is_layout_rtl());
  981. }
  982. for (int i = 0; i < items.size(); i++) {
  983. Item &item = items.write[i];
  984. item.xl_text = _atr(i, item.text);
  985. item.dirty = true;
  986. if (is_global) {
  987. nmenu->set_item_text(global_menu, i, item.xl_text);
  988. }
  989. _shape_item(i);
  990. }
  991. child_controls_changed();
  992. _menu_changed();
  993. control->queue_redraw();
  994. } break;
  995. case NOTIFICATION_WM_MOUSE_ENTER: {
  996. grab_focus();
  997. } break;
  998. case NOTIFICATION_WM_MOUSE_EXIT: {
  999. if (mouse_over >= 0 && (!items[mouse_over].submenu || submenu_over != -1)) {
  1000. mouse_over = -1;
  1001. control->queue_redraw();
  1002. }
  1003. } break;
  1004. case NOTIFICATION_WM_SIZE_CHANGED: {
  1005. if (is_visible()) {
  1006. const Vector2i offsets = Vector2i(panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM));
  1007. // Check if the size actually changed.
  1008. if (pre_popup_rect.size + offsets != get_size()) {
  1009. // Play safe, and stick with the new size.
  1010. pre_popup_rect = Rect2i();
  1011. }
  1012. }
  1013. } break;
  1014. case NOTIFICATION_POST_POPUP: {
  1015. popup_time_msec = OS::get_singleton()->get_ticks_msec();
  1016. initial_button_mask = Input::get_singleton()->get_mouse_button_mask();
  1017. during_grabbed_click = (bool)initial_button_mask;
  1018. } break;
  1019. case NOTIFICATION_INTERNAL_PROCESS: {
  1020. Input *input = Input::get_singleton();
  1021. if (input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) {
  1022. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  1023. set_process_internal(false);
  1024. return;
  1025. }
  1026. gamepad_event_delay_ms -= get_process_delta_time();
  1027. if (gamepad_event_delay_ms <= 0) {
  1028. if (input->is_action_pressed("ui_down")) {
  1029. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  1030. int search_from = mouse_over + 1;
  1031. if (search_from >= items.size()) {
  1032. search_from = 0;
  1033. }
  1034. bool match_found = false;
  1035. for (int i = search_from; i < items.size(); i++) {
  1036. if (!items[i].separator && !items[i].disabled) {
  1037. mouse_over = i;
  1038. emit_signal(SNAME("id_focused"), items[i].id);
  1039. scroll_to_item(i);
  1040. control->queue_redraw();
  1041. match_found = true;
  1042. break;
  1043. }
  1044. }
  1045. if (!match_found) {
  1046. // If the last item is not selectable, try re-searching from the start.
  1047. for (int i = 0; i < search_from; i++) {
  1048. if (!items[i].separator && !items[i].disabled) {
  1049. mouse_over = i;
  1050. emit_signal(SNAME("id_focused"), items[i].id);
  1051. scroll_to_item(i);
  1052. control->queue_redraw();
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. }
  1058. if (input->is_action_pressed("ui_up")) {
  1059. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  1060. int search_from = mouse_over - 1;
  1061. if (search_from < 0) {
  1062. search_from = items.size() - 1;
  1063. }
  1064. bool match_found = false;
  1065. for (int i = search_from; i >= 0; i--) {
  1066. if (!items[i].separator && !items[i].disabled) {
  1067. mouse_over = i;
  1068. emit_signal(SNAME("id_focused"), items[i].id);
  1069. scroll_to_item(i);
  1070. control->queue_redraw();
  1071. match_found = true;
  1072. break;
  1073. }
  1074. }
  1075. if (!match_found) {
  1076. // If the first item is not selectable, try re-searching from the end.
  1077. for (int i = items.size() - 1; i >= search_from; i--) {
  1078. if (!items[i].separator && !items[i].disabled) {
  1079. mouse_over = i;
  1080. emit_signal(SNAME("id_focused"), items[i].id);
  1081. scroll_to_item(i);
  1082. control->queue_redraw();
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. }
  1089. // Only used when using operating system windows.
  1090. if (!activated_by_keyboard && !is_embedded() && autohide_areas.size()) {
  1091. Point2 mouse_pos = DisplayServer::get_singleton()->mouse_get_position();
  1092. mouse_pos -= get_position();
  1093. for (const Rect2 &E : autohide_areas) {
  1094. if (!Rect2(Point2(), get_size()).has_point(mouse_pos) && E.has_point(mouse_pos)) {
  1095. _close_pressed();
  1096. return;
  1097. }
  1098. }
  1099. }
  1100. } break;
  1101. case NOTIFICATION_VISIBILITY_CHANGED: {
  1102. if (!is_visible()) {
  1103. if (mouse_over >= 0) {
  1104. mouse_over = -1;
  1105. control->queue_redraw();
  1106. }
  1107. for (int i = 0; i < items.size(); i++) {
  1108. if (!items[i].submenu) {
  1109. continue;
  1110. }
  1111. items[i].submenu->hide();
  1112. }
  1113. set_process_internal(false);
  1114. // Remove the extra space used by the shadows, so they can be ignored when the popup is hidden.
  1115. panel->set_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 0);
  1116. if (pre_popup_rect != Rect2i()) {
  1117. set_position(pre_popup_rect.position);
  1118. set_size(pre_popup_rect.size);
  1119. pre_popup_rect = Rect2i();
  1120. }
  1121. } else {
  1122. if (!is_embedded()) {
  1123. set_process_internal(true);
  1124. }
  1125. // The popup was made visible directly (without `popup_*()`), so just update the offsets without touching the rect.
  1126. if (pre_popup_rect == Rect2i()) {
  1127. _update_shadow_offsets();
  1128. }
  1129. }
  1130. } break;
  1131. }
  1132. }
  1133. /* Methods to add items with or without icon, checkbox, shortcut.
  1134. * Be sure to keep them in sync when adding new properties in the Item struct.
  1135. */
  1136. #define ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel) \
  1137. item.text = p_label; \
  1138. item.xl_text = atr(p_label); \
  1139. item.id = p_id == -1 ? items.size() : p_id; \
  1140. item.accel = p_accel;
  1141. void PopupMenu::add_item(const String &p_label, int p_id, Key p_accel) {
  1142. Item item;
  1143. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1144. items.push_back(item);
  1145. if (global_menu.is_valid()) {
  1146. NativeMenu *nmenu = NativeMenu::get_singleton();
  1147. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1148. if (item.accel != Key::NONE) {
  1149. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1150. }
  1151. }
  1152. _shape_item(items.size() - 1);
  1153. control->queue_redraw();
  1154. child_controls_changed();
  1155. notify_property_list_changed();
  1156. _menu_changed();
  1157. }
  1158. void PopupMenu::add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1159. Item item;
  1160. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1161. item.icon = p_icon;
  1162. items.push_back(item);
  1163. if (global_menu.is_valid()) {
  1164. NativeMenu *nmenu = NativeMenu::get_singleton();
  1165. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1166. if (item.accel != Key::NONE) {
  1167. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1168. }
  1169. nmenu->set_item_icon(global_menu, index, item.icon);
  1170. }
  1171. _shape_item(items.size() - 1);
  1172. control->queue_redraw();
  1173. child_controls_changed();
  1174. notify_property_list_changed();
  1175. _menu_changed();
  1176. }
  1177. void PopupMenu::add_check_item(const String &p_label, int p_id, Key p_accel) {
  1178. Item item;
  1179. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1180. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1181. items.push_back(item);
  1182. if (global_menu.is_valid()) {
  1183. NativeMenu *nmenu = NativeMenu::get_singleton();
  1184. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1185. if (item.accel != Key::NONE) {
  1186. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1187. }
  1188. nmenu->set_item_checkable(global_menu, index, true);
  1189. }
  1190. _shape_item(items.size() - 1);
  1191. control->queue_redraw();
  1192. child_controls_changed();
  1193. notify_property_list_changed();
  1194. _menu_changed();
  1195. }
  1196. void PopupMenu::add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1197. Item item;
  1198. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1199. item.icon = p_icon;
  1200. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1201. items.push_back(item);
  1202. if (global_menu.is_valid()) {
  1203. NativeMenu *nmenu = NativeMenu::get_singleton();
  1204. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1205. if (item.accel != Key::NONE) {
  1206. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1207. }
  1208. nmenu->set_item_icon(global_menu, index, item.icon);
  1209. nmenu->set_item_checkable(global_menu, index, true);
  1210. }
  1211. _shape_item(items.size() - 1);
  1212. control->queue_redraw();
  1213. child_controls_changed();
  1214. notify_property_list_changed();
  1215. _menu_changed();
  1216. }
  1217. void PopupMenu::add_radio_check_item(const String &p_label, int p_id, Key p_accel) {
  1218. Item item;
  1219. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1220. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1221. items.push_back(item);
  1222. if (global_menu.is_valid()) {
  1223. NativeMenu *nmenu = NativeMenu::get_singleton();
  1224. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1225. if (item.accel != Key::NONE) {
  1226. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1227. }
  1228. nmenu->set_item_radio_checkable(global_menu, index, true);
  1229. }
  1230. _shape_item(items.size() - 1);
  1231. control->queue_redraw();
  1232. child_controls_changed();
  1233. notify_property_list_changed();
  1234. _menu_changed();
  1235. }
  1236. void PopupMenu::add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id, Key p_accel) {
  1237. Item item;
  1238. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1239. item.icon = p_icon;
  1240. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1241. items.push_back(item);
  1242. if (global_menu.is_valid()) {
  1243. NativeMenu *nmenu = NativeMenu::get_singleton();
  1244. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1245. if (item.accel != Key::NONE) {
  1246. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1247. }
  1248. nmenu->set_item_icon(global_menu, index, item.icon);
  1249. nmenu->set_item_radio_checkable(global_menu, index, true);
  1250. }
  1251. _shape_item(items.size() - 1);
  1252. control->queue_redraw();
  1253. child_controls_changed();
  1254. notify_property_list_changed();
  1255. _menu_changed();
  1256. }
  1257. void PopupMenu::add_multistate_item(const String &p_label, int p_max_states, int p_default_state, int p_id, Key p_accel) {
  1258. Item item;
  1259. ITEM_SETUP_WITH_ACCEL(p_label, p_id, p_accel);
  1260. item.max_states = p_max_states;
  1261. item.state = p_default_state;
  1262. items.push_back(item);
  1263. if (global_menu.is_valid()) {
  1264. NativeMenu *nmenu = NativeMenu::get_singleton();
  1265. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1266. if (item.accel != Key::NONE) {
  1267. nmenu->set_item_accelerator(global_menu, index, item.accel);
  1268. }
  1269. nmenu->set_item_max_states(global_menu, index, item.max_states);
  1270. nmenu->set_item_state(global_menu, index, item.state);
  1271. }
  1272. _shape_item(items.size() - 1);
  1273. control->queue_redraw();
  1274. child_controls_changed();
  1275. _menu_changed();
  1276. notify_property_list_changed();
  1277. }
  1278. #define ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo) \
  1279. ERR_FAIL_COND_MSG(p_shortcut.is_null(), "Cannot add item with invalid Shortcut."); \
  1280. _ref_shortcut(p_shortcut); \
  1281. item.text = p_shortcut->get_name(); \
  1282. item.xl_text = atr(item.text); \
  1283. item.id = p_id == -1 ? items.size() : p_id; \
  1284. item.shortcut = p_shortcut; \
  1285. item.shortcut_is_global = p_global; \
  1286. item.allow_echo = p_allow_echo;
  1287. void PopupMenu::add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) {
  1288. Item item;
  1289. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo);
  1290. items.push_back(item);
  1291. if (global_menu.is_valid()) {
  1292. NativeMenu *nmenu = NativeMenu::get_singleton();
  1293. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1294. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1295. Array events = item.shortcut->get_events();
  1296. for (int j = 0; j < events.size(); j++) {
  1297. Ref<InputEventKey> ie = events[j];
  1298. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1299. break;
  1300. }
  1301. }
  1302. }
  1303. }
  1304. _shape_item(items.size() - 1);
  1305. control->queue_redraw();
  1306. child_controls_changed();
  1307. notify_property_list_changed();
  1308. _menu_changed();
  1309. }
  1310. void PopupMenu::add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global, bool p_allow_echo) {
  1311. Item item;
  1312. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, p_allow_echo);
  1313. item.icon = p_icon;
  1314. items.push_back(item);
  1315. if (global_menu.is_valid()) {
  1316. NativeMenu *nmenu = NativeMenu::get_singleton();
  1317. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1318. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1319. Array events = item.shortcut->get_events();
  1320. for (int j = 0; j < events.size(); j++) {
  1321. Ref<InputEventKey> ie = events[j];
  1322. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. nmenu->set_item_icon(global_menu, index, item.icon);
  1328. }
  1329. _shape_item(items.size() - 1);
  1330. control->queue_redraw();
  1331. child_controls_changed();
  1332. notify_property_list_changed();
  1333. _menu_changed();
  1334. }
  1335. void PopupMenu::add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1336. Item item;
  1337. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false); // Echo for check shortcuts doesn't make sense.
  1338. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1339. items.push_back(item);
  1340. if (global_menu.is_valid()) {
  1341. NativeMenu *nmenu = NativeMenu::get_singleton();
  1342. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1343. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1344. Array events = item.shortcut->get_events();
  1345. for (int j = 0; j < events.size(); j++) {
  1346. Ref<InputEventKey> ie = events[j];
  1347. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1348. break;
  1349. }
  1350. }
  1351. }
  1352. nmenu->set_item_checkable(global_menu, index, true);
  1353. }
  1354. _shape_item(items.size() - 1);
  1355. control->queue_redraw();
  1356. child_controls_changed();
  1357. notify_property_list_changed();
  1358. _menu_changed();
  1359. }
  1360. void PopupMenu::add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1361. Item item;
  1362. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1363. item.icon = p_icon;
  1364. item.checkable_type = Item::CHECKABLE_TYPE_CHECK_BOX;
  1365. items.push_back(item);
  1366. if (global_menu.is_valid()) {
  1367. NativeMenu *nmenu = NativeMenu::get_singleton();
  1368. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1369. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1370. Array events = item.shortcut->get_events();
  1371. for (int j = 0; j < events.size(); j++) {
  1372. Ref<InputEventKey> ie = events[j];
  1373. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1374. break;
  1375. }
  1376. }
  1377. }
  1378. nmenu->set_item_icon(global_menu, index, item.icon);
  1379. nmenu->set_item_checkable(global_menu, index, true);
  1380. }
  1381. _shape_item(items.size() - 1);
  1382. control->queue_redraw();
  1383. child_controls_changed();
  1384. notify_property_list_changed();
  1385. _menu_changed();
  1386. }
  1387. void PopupMenu::add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1388. Item item;
  1389. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1390. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1391. items.push_back(item);
  1392. if (global_menu.is_valid()) {
  1393. NativeMenu *nmenu = NativeMenu::get_singleton();
  1394. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1395. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1396. Array events = item.shortcut->get_events();
  1397. for (int j = 0; j < events.size(); j++) {
  1398. Ref<InputEventKey> ie = events[j];
  1399. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1400. break;
  1401. }
  1402. }
  1403. }
  1404. nmenu->set_item_radio_checkable(global_menu, index, true);
  1405. }
  1406. _shape_item(items.size() - 1);
  1407. control->queue_redraw();
  1408. child_controls_changed();
  1409. notify_property_list_changed();
  1410. _menu_changed();
  1411. }
  1412. void PopupMenu::add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id, bool p_global) {
  1413. Item item;
  1414. ITEM_SETUP_WITH_SHORTCUT(p_shortcut, p_id, p_global, false);
  1415. item.icon = p_icon;
  1416. item.checkable_type = Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1417. items.push_back(item);
  1418. if (global_menu.is_valid()) {
  1419. NativeMenu *nmenu = NativeMenu::get_singleton();
  1420. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), p_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), items.size() - 1);
  1421. if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
  1422. Array events = item.shortcut->get_events();
  1423. for (int j = 0; j < events.size(); j++) {
  1424. Ref<InputEventKey> ie = events[j];
  1425. if (ie.is_valid() && _set_item_accelerator(index, ie)) {
  1426. break;
  1427. }
  1428. }
  1429. }
  1430. nmenu->set_item_icon(global_menu, index, item.icon);
  1431. nmenu->set_item_radio_checkable(global_menu, index, true);
  1432. }
  1433. _shape_item(items.size() - 1);
  1434. control->queue_redraw();
  1435. child_controls_changed();
  1436. notify_property_list_changed();
  1437. _menu_changed();
  1438. }
  1439. void PopupMenu::add_submenu_item(const String &p_label, const String &p_submenu, int p_id) {
  1440. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(p_submenu));
  1441. ERR_FAIL_NULL_MSG(pm, vformat("Child PopupMenu \"%s\" does not exist.", p_submenu));
  1442. add_submenu_node_item(p_label, pm, p_id);
  1443. }
  1444. void PopupMenu::add_submenu_node_item(const String &p_label, PopupMenu *p_submenu, int p_id) {
  1445. ERR_FAIL_NULL(p_submenu);
  1446. if (p_submenu->get_parent() != this) {
  1447. ERR_FAIL_COND_MSG(p_submenu->get_parent() != nullptr, vformat("The submenu \"%s\" already has a different parent.", p_submenu->get_name()));
  1448. add_child(p_submenu);
  1449. }
  1450. Item item;
  1451. item.text = p_label;
  1452. item.xl_text = atr(p_label);
  1453. item.id = p_id == -1 ? items.size() : p_id;
  1454. item.submenu = p_submenu;
  1455. item.submenu_name = p_submenu->get_name();
  1456. items.push_back(item);
  1457. if (global_menu.is_valid()) {
  1458. NativeMenu *nmenu = NativeMenu::get_singleton();
  1459. int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), Callable(), items.size() - 1);
  1460. RID submenu_rid = p_submenu->bind_global_menu();
  1461. nmenu->set_item_submenu(global_menu, index, submenu_rid);
  1462. items.write[index].submenu_bound = true;
  1463. }
  1464. _shape_item(items.size() - 1);
  1465. control->queue_redraw();
  1466. child_controls_changed();
  1467. notify_property_list_changed();
  1468. _menu_changed();
  1469. }
  1470. #undef ITEM_SETUP_WITH_ACCEL
  1471. #undef ITEM_SETUP_WITH_SHORTCUT
  1472. /* Methods to modify existing items. */
  1473. void PopupMenu::set_item_text(int p_idx, const String &p_text) {
  1474. if (p_idx < 0) {
  1475. p_idx += get_item_count();
  1476. }
  1477. ERR_FAIL_INDEX(p_idx, items.size());
  1478. if (items[p_idx].text == p_text) {
  1479. return;
  1480. }
  1481. items.write[p_idx].text = p_text;
  1482. items.write[p_idx].xl_text = _atr(p_idx, p_text);
  1483. items.write[p_idx].dirty = true;
  1484. if (global_menu.is_valid()) {
  1485. NativeMenu::get_singleton()->set_item_text(global_menu, p_idx, items[p_idx].xl_text);
  1486. }
  1487. _shape_item(p_idx);
  1488. control->queue_redraw();
  1489. child_controls_changed();
  1490. _menu_changed();
  1491. }
  1492. void PopupMenu::set_item_text_direction(int p_idx, Control::TextDirection p_text_direction) {
  1493. if (p_idx < 0) {
  1494. p_idx += get_item_count();
  1495. }
  1496. ERR_FAIL_INDEX(p_idx, items.size());
  1497. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1498. if (items[p_idx].text_direction != p_text_direction) {
  1499. items.write[p_idx].text_direction = p_text_direction;
  1500. items.write[p_idx].dirty = true;
  1501. control->queue_redraw();
  1502. }
  1503. }
  1504. void PopupMenu::set_item_language(int p_idx, const String &p_language) {
  1505. if (p_idx < 0) {
  1506. p_idx += get_item_count();
  1507. }
  1508. ERR_FAIL_INDEX(p_idx, items.size());
  1509. if (items[p_idx].language != p_language) {
  1510. items.write[p_idx].language = p_language;
  1511. items.write[p_idx].dirty = true;
  1512. control->queue_redraw();
  1513. }
  1514. }
  1515. void PopupMenu::set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode) {
  1516. if (p_idx < 0) {
  1517. p_idx += get_item_count();
  1518. }
  1519. ERR_FAIL_INDEX(p_idx, items.size());
  1520. if (items[p_idx].auto_translate_mode == p_mode) {
  1521. return;
  1522. }
  1523. items.write[p_idx].auto_translate_mode = p_mode;
  1524. items.write[p_idx].xl_text = _atr(p_idx, items[p_idx].text);
  1525. items.write[p_idx].dirty = true;
  1526. control->queue_redraw();
  1527. }
  1528. void PopupMenu::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  1529. if (p_idx < 0) {
  1530. p_idx += get_item_count();
  1531. }
  1532. ERR_FAIL_INDEX(p_idx, items.size());
  1533. if (items[p_idx].icon == p_icon) {
  1534. return;
  1535. }
  1536. items.write[p_idx].icon = p_icon;
  1537. if (global_menu.is_valid()) {
  1538. NativeMenu::get_singleton()->set_item_icon(global_menu, p_idx, items[p_idx].icon);
  1539. }
  1540. control->queue_redraw();
  1541. child_controls_changed();
  1542. _menu_changed();
  1543. }
  1544. void PopupMenu::set_item_icon_max_width(int p_idx, int p_width) {
  1545. if (p_idx < 0) {
  1546. p_idx += get_item_count();
  1547. }
  1548. ERR_FAIL_INDEX(p_idx, items.size());
  1549. if (items[p_idx].icon_max_width == p_width) {
  1550. return;
  1551. }
  1552. items.write[p_idx].icon_max_width = p_width;
  1553. control->queue_redraw();
  1554. child_controls_changed();
  1555. _menu_changed();
  1556. }
  1557. void PopupMenu::set_item_icon_modulate(int p_idx, const Color &p_modulate) {
  1558. if (p_idx < 0) {
  1559. p_idx += get_item_count();
  1560. }
  1561. ERR_FAIL_INDEX(p_idx, items.size());
  1562. if (items[p_idx].icon_modulate == p_modulate) {
  1563. return;
  1564. }
  1565. items.write[p_idx].icon_modulate = p_modulate;
  1566. control->queue_redraw();
  1567. }
  1568. void PopupMenu::set_item_checked(int p_idx, bool p_checked) {
  1569. if (p_idx < 0) {
  1570. p_idx += get_item_count();
  1571. }
  1572. ERR_FAIL_INDEX(p_idx, items.size());
  1573. if (items[p_idx].checked == p_checked) {
  1574. return;
  1575. }
  1576. items.write[p_idx].checked = p_checked;
  1577. if (global_menu.is_valid()) {
  1578. NativeMenu::get_singleton()->set_item_checked(global_menu, p_idx, p_checked);
  1579. }
  1580. control->queue_redraw();
  1581. child_controls_changed();
  1582. _menu_changed();
  1583. }
  1584. void PopupMenu::set_item_id(int p_idx, int p_id) {
  1585. if (p_idx < 0) {
  1586. p_idx += get_item_count();
  1587. }
  1588. ERR_FAIL_INDEX(p_idx, items.size());
  1589. if (items[p_idx].id == p_id) {
  1590. return;
  1591. }
  1592. items.write[p_idx].id = p_id;
  1593. if (global_menu.is_valid()) {
  1594. NativeMenu::get_singleton()->set_item_tag(global_menu, p_idx, p_id);
  1595. }
  1596. control->queue_redraw();
  1597. child_controls_changed();
  1598. _menu_changed();
  1599. }
  1600. void PopupMenu::set_item_accelerator(int p_idx, Key p_accel) {
  1601. if (p_idx < 0) {
  1602. p_idx += get_item_count();
  1603. }
  1604. ERR_FAIL_INDEX(p_idx, items.size());
  1605. if (items[p_idx].accel == p_accel) {
  1606. return;
  1607. }
  1608. items.write[p_idx].accel = p_accel;
  1609. items.write[p_idx].dirty = true;
  1610. if (global_menu.is_valid()) {
  1611. NativeMenu::get_singleton()->set_item_accelerator(global_menu, p_idx, p_accel);
  1612. }
  1613. control->queue_redraw();
  1614. child_controls_changed();
  1615. _menu_changed();
  1616. }
  1617. void PopupMenu::set_item_metadata(int p_idx, const Variant &p_meta) {
  1618. if (p_idx < 0) {
  1619. p_idx += get_item_count();
  1620. }
  1621. ERR_FAIL_INDEX(p_idx, items.size());
  1622. if (items[p_idx].metadata == p_meta) {
  1623. return;
  1624. }
  1625. items.write[p_idx].metadata = p_meta;
  1626. child_controls_changed();
  1627. _menu_changed();
  1628. }
  1629. void PopupMenu::set_item_disabled(int p_idx, bool p_disabled) {
  1630. if (p_idx < 0) {
  1631. p_idx += get_item_count();
  1632. }
  1633. ERR_FAIL_INDEX(p_idx, items.size());
  1634. if (items[p_idx].disabled == p_disabled) {
  1635. return;
  1636. }
  1637. items.write[p_idx].disabled = p_disabled;
  1638. if (global_menu.is_valid()) {
  1639. NativeMenu::get_singleton()->set_item_disabled(global_menu, p_idx, p_disabled);
  1640. }
  1641. control->queue_redraw();
  1642. child_controls_changed();
  1643. _menu_changed();
  1644. }
  1645. void PopupMenu::set_item_submenu(int p_idx, const String &p_submenu) {
  1646. if (p_idx < 0) {
  1647. p_idx += get_item_count();
  1648. }
  1649. ERR_FAIL_INDEX(p_idx, items.size());
  1650. if (items[p_idx].submenu_name == p_submenu) {
  1651. return;
  1652. }
  1653. PopupMenu *pm = Object::cast_to<PopupMenu>(get_node_or_null(p_submenu));
  1654. ERR_FAIL_NULL_MSG(pm, vformat("Child PopupMenu \"%s\" does not exist.", p_submenu));
  1655. set_item_submenu_node(p_idx, pm);
  1656. }
  1657. void PopupMenu::set_item_submenu_node(int p_idx, PopupMenu *p_submenu) {
  1658. ERR_FAIL_NULL(p_submenu);
  1659. if (p_idx < 0) {
  1660. p_idx += get_item_count();
  1661. }
  1662. ERR_FAIL_INDEX(p_idx, items.size());
  1663. if (p_submenu->get_parent() != this) {
  1664. ERR_FAIL_COND_MSG(p_submenu->get_parent() != nullptr, vformat("The submenu \"%s\" already has a different parent.", p_submenu->get_name()));
  1665. add_child(p_submenu);
  1666. }
  1667. if (global_menu.is_valid()) {
  1668. if (items[p_idx].submenu_bound) {
  1669. PopupMenu *pm = items[p_idx].submenu;
  1670. if (pm) {
  1671. NativeMenu::get_singleton()->set_item_submenu(global_menu, p_idx, RID());
  1672. pm->unbind_global_menu();
  1673. }
  1674. items.write[p_idx].submenu_bound = false;
  1675. }
  1676. }
  1677. items.write[p_idx].submenu = p_submenu;
  1678. if (global_menu.is_valid()) {
  1679. PopupMenu *pm = items[p_idx].submenu;
  1680. if (pm) {
  1681. RID submenu_rid = pm->bind_global_menu();
  1682. NativeMenu::get_singleton()->set_item_submenu(global_menu, p_idx, submenu_rid);
  1683. items.write[p_idx].submenu_bound = true;
  1684. }
  1685. }
  1686. control->queue_redraw();
  1687. child_controls_changed();
  1688. _menu_changed();
  1689. }
  1690. void PopupMenu::toggle_item_checked(int p_idx) {
  1691. ERR_FAIL_INDEX(p_idx, items.size());
  1692. items.write[p_idx].checked = !items[p_idx].checked;
  1693. if (global_menu.is_valid()) {
  1694. NativeMenu::get_singleton()->set_item_checked(global_menu, p_idx, items[p_idx].checked);
  1695. }
  1696. control->queue_redraw();
  1697. child_controls_changed();
  1698. _menu_changed();
  1699. }
  1700. String PopupMenu::get_item_text(int p_idx) const {
  1701. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1702. return items[p_idx].text;
  1703. }
  1704. String PopupMenu::get_item_xl_text(int p_idx) const {
  1705. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1706. return items[p_idx].xl_text;
  1707. }
  1708. Control::TextDirection PopupMenu::get_item_text_direction(int p_idx) const {
  1709. ERR_FAIL_INDEX_V(p_idx, items.size(), Control::TEXT_DIRECTION_INHERITED);
  1710. return items[p_idx].text_direction;
  1711. }
  1712. String PopupMenu::get_item_language(int p_idx) const {
  1713. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1714. return items[p_idx].language;
  1715. }
  1716. Node::AutoTranslateMode PopupMenu::get_item_auto_translate_mode(int p_idx) const {
  1717. ERR_FAIL_INDEX_V(p_idx, items.size(), AUTO_TRANSLATE_MODE_INHERIT);
  1718. return items[p_idx].auto_translate_mode;
  1719. }
  1720. int PopupMenu::get_item_idx_from_text(const String &text) const {
  1721. for (int idx = 0; idx < items.size(); idx++) {
  1722. if (items[idx].text == text) {
  1723. return idx;
  1724. }
  1725. }
  1726. return -1;
  1727. }
  1728. Ref<Texture2D> PopupMenu::get_item_icon(int p_idx) const {
  1729. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>());
  1730. return items[p_idx].icon;
  1731. }
  1732. int PopupMenu::get_item_icon_max_width(int p_idx) const {
  1733. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  1734. return items[p_idx].icon_max_width;
  1735. }
  1736. Color PopupMenu::get_item_icon_modulate(int p_idx) const {
  1737. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  1738. return items[p_idx].icon_modulate;
  1739. }
  1740. Key PopupMenu::get_item_accelerator(int p_idx) const {
  1741. ERR_FAIL_INDEX_V(p_idx, items.size(), Key::NONE);
  1742. return items[p_idx].accel;
  1743. }
  1744. Variant PopupMenu::get_item_metadata(int p_idx) const {
  1745. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  1746. return items[p_idx].metadata;
  1747. }
  1748. bool PopupMenu::is_item_disabled(int p_idx) const {
  1749. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1750. return items[p_idx].disabled;
  1751. }
  1752. bool PopupMenu::is_item_checked(int p_idx) const {
  1753. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1754. return items[p_idx].checked;
  1755. }
  1756. int PopupMenu::get_item_id(int p_idx) const {
  1757. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  1758. return items[p_idx].id;
  1759. }
  1760. int PopupMenu::get_item_index(int p_id) const {
  1761. for (int i = 0; i < items.size(); i++) {
  1762. if (items[i].id == p_id) {
  1763. return i;
  1764. }
  1765. }
  1766. return -1;
  1767. }
  1768. String PopupMenu::get_item_submenu(int p_idx) const {
  1769. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1770. return items[p_idx].submenu_name;
  1771. }
  1772. PopupMenu *PopupMenu::get_item_submenu_node(int p_idx) const {
  1773. ERR_FAIL_INDEX_V(p_idx, items.size(), nullptr);
  1774. return items[p_idx].submenu;
  1775. }
  1776. String PopupMenu::get_item_tooltip(int p_idx) const {
  1777. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  1778. return items[p_idx].tooltip;
  1779. }
  1780. Ref<Shortcut> PopupMenu::get_item_shortcut(int p_idx) const {
  1781. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Shortcut>());
  1782. return items[p_idx].shortcut;
  1783. }
  1784. int PopupMenu::get_item_indent(int p_idx) const {
  1785. ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
  1786. return items[p_idx].indent;
  1787. }
  1788. int PopupMenu::get_item_max_states(int p_idx) const {
  1789. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  1790. return items[p_idx].max_states;
  1791. }
  1792. int PopupMenu::get_item_state(int p_idx) const {
  1793. ERR_FAIL_INDEX_V(p_idx, items.size(), -1);
  1794. return items[p_idx].state;
  1795. }
  1796. void PopupMenu::set_item_as_separator(int p_idx, bool p_separator) {
  1797. if (p_idx < 0) {
  1798. p_idx += get_item_count();
  1799. }
  1800. ERR_FAIL_INDEX(p_idx, items.size());
  1801. if (items[p_idx].separator == p_separator) {
  1802. return;
  1803. }
  1804. items.write[p_idx].separator = p_separator;
  1805. control->queue_redraw();
  1806. }
  1807. bool PopupMenu::is_item_separator(int p_idx) const {
  1808. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1809. return items[p_idx].separator;
  1810. }
  1811. void PopupMenu::set_item_as_checkable(int p_idx, bool p_checkable) {
  1812. if (p_idx < 0) {
  1813. p_idx += get_item_count();
  1814. }
  1815. ERR_FAIL_INDEX(p_idx, items.size());
  1816. int type = (int)(p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE);
  1817. if (type == items[p_idx].checkable_type) {
  1818. return;
  1819. }
  1820. items.write[p_idx].checkable_type = p_checkable ? Item::CHECKABLE_TYPE_CHECK_BOX : Item::CHECKABLE_TYPE_NONE;
  1821. if (global_menu.is_valid()) {
  1822. NativeMenu::get_singleton()->set_item_checkable(global_menu, p_idx, p_checkable);
  1823. }
  1824. control->queue_redraw();
  1825. _menu_changed();
  1826. }
  1827. void PopupMenu::set_item_as_radio_checkable(int p_idx, bool p_radio_checkable) {
  1828. if (p_idx < 0) {
  1829. p_idx += get_item_count();
  1830. }
  1831. ERR_FAIL_INDEX(p_idx, items.size());
  1832. int type = (int)(p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE);
  1833. if (type == items[p_idx].checkable_type) {
  1834. return;
  1835. }
  1836. items.write[p_idx].checkable_type = p_radio_checkable ? Item::CHECKABLE_TYPE_RADIO_BUTTON : Item::CHECKABLE_TYPE_NONE;
  1837. if (global_menu.is_valid()) {
  1838. NativeMenu::get_singleton()->set_item_radio_checkable(global_menu, p_idx, p_radio_checkable);
  1839. }
  1840. control->queue_redraw();
  1841. _menu_changed();
  1842. }
  1843. void PopupMenu::set_item_tooltip(int p_idx, const String &p_tooltip) {
  1844. if (p_idx < 0) {
  1845. p_idx += get_item_count();
  1846. }
  1847. ERR_FAIL_INDEX(p_idx, items.size());
  1848. if (items[p_idx].tooltip == p_tooltip) {
  1849. return;
  1850. }
  1851. items.write[p_idx].tooltip = p_tooltip;
  1852. if (global_menu.is_valid()) {
  1853. NativeMenu::get_singleton()->set_item_tooltip(global_menu, p_idx, p_tooltip);
  1854. }
  1855. control->queue_redraw();
  1856. _menu_changed();
  1857. }
  1858. void PopupMenu::set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global) {
  1859. if (p_idx < 0) {
  1860. p_idx += get_item_count();
  1861. }
  1862. ERR_FAIL_INDEX(p_idx, items.size());
  1863. if (items[p_idx].shortcut == p_shortcut && items[p_idx].shortcut_is_global == p_global && items[p_idx].shortcut.is_valid() == p_shortcut.is_valid()) {
  1864. return;
  1865. }
  1866. if (items[p_idx].shortcut.is_valid()) {
  1867. _unref_shortcut(items[p_idx].shortcut);
  1868. }
  1869. items.write[p_idx].shortcut = p_shortcut;
  1870. items.write[p_idx].shortcut_is_global = p_global;
  1871. items.write[p_idx].dirty = true;
  1872. if (items[p_idx].shortcut.is_valid()) {
  1873. _ref_shortcut(items[p_idx].shortcut);
  1874. }
  1875. if (global_menu.is_valid()) {
  1876. NativeMenu *nmenu = NativeMenu::get_singleton();
  1877. nmenu->set_item_accelerator(global_menu, p_idx, Key::NONE);
  1878. if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) {
  1879. Array events = items[p_idx].shortcut->get_events();
  1880. for (int j = 0; j < events.size(); j++) {
  1881. Ref<InputEventKey> ie = events[j];
  1882. if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) {
  1883. break;
  1884. }
  1885. }
  1886. if (p_global) {
  1887. nmenu->set_item_key_callback(global_menu, p_idx, callable_mp(this, &PopupMenu::activate_item));
  1888. } else {
  1889. nmenu->set_item_key_callback(global_menu, p_idx, Callable());
  1890. }
  1891. }
  1892. }
  1893. control->queue_redraw();
  1894. _menu_changed();
  1895. }
  1896. void PopupMenu::set_item_indent(int p_idx, int p_indent) {
  1897. if (p_idx < 0) {
  1898. p_idx += get_item_count();
  1899. }
  1900. ERR_FAIL_INDEX(p_idx, items.size());
  1901. if (items.write[p_idx].indent == p_indent) {
  1902. return;
  1903. }
  1904. items.write[p_idx].indent = p_indent;
  1905. if (global_menu.is_valid()) {
  1906. NativeMenu::get_singleton()->set_item_indentation_level(global_menu, p_idx, p_indent);
  1907. }
  1908. control->queue_redraw();
  1909. child_controls_changed();
  1910. _menu_changed();
  1911. }
  1912. void PopupMenu::set_item_max_states(int p_idx, int p_max_states) {
  1913. if (p_idx < 0) {
  1914. p_idx += get_item_count();
  1915. }
  1916. ERR_FAIL_INDEX(p_idx, items.size());
  1917. if (items[p_idx].max_states == p_max_states) {
  1918. return;
  1919. }
  1920. items.write[p_idx].max_states = p_max_states;
  1921. if (global_menu.is_valid()) {
  1922. NativeMenu::get_singleton()->set_item_max_states(global_menu, p_idx, p_max_states);
  1923. }
  1924. control->queue_redraw();
  1925. _menu_changed();
  1926. }
  1927. void PopupMenu::set_item_multistate(int p_idx, int p_state) {
  1928. if (p_idx < 0) {
  1929. p_idx += get_item_count();
  1930. }
  1931. ERR_FAIL_INDEX(p_idx, items.size());
  1932. if (items[p_idx].state == p_state) {
  1933. return;
  1934. }
  1935. items.write[p_idx].state = p_state;
  1936. if (global_menu.is_valid()) {
  1937. NativeMenu::get_singleton()->set_item_state(global_menu, p_idx, p_state);
  1938. }
  1939. control->queue_redraw();
  1940. _menu_changed();
  1941. }
  1942. void PopupMenu::set_item_shortcut_disabled(int p_idx, bool p_disabled) {
  1943. if (p_idx < 0) {
  1944. p_idx += get_item_count();
  1945. }
  1946. ERR_FAIL_INDEX(p_idx, items.size());
  1947. if (items[p_idx].shortcut_is_disabled == p_disabled) {
  1948. return;
  1949. }
  1950. items.write[p_idx].shortcut_is_disabled = p_disabled;
  1951. if (global_menu.is_valid()) {
  1952. NativeMenu *nmenu = NativeMenu::get_singleton();
  1953. nmenu->set_item_accelerator(global_menu, p_idx, Key::NONE);
  1954. if (!items[p_idx].shortcut_is_disabled && items[p_idx].shortcut.is_valid() && items[p_idx].shortcut->has_valid_event()) {
  1955. Array events = items[p_idx].shortcut->get_events();
  1956. for (int j = 0; j < events.size(); j++) {
  1957. Ref<InputEventKey> ie = events[j];
  1958. if (ie.is_valid() && _set_item_accelerator(p_idx, ie)) {
  1959. break;
  1960. }
  1961. }
  1962. }
  1963. }
  1964. control->queue_redraw();
  1965. _menu_changed();
  1966. }
  1967. void PopupMenu::toggle_item_multistate(int p_idx) {
  1968. ERR_FAIL_INDEX(p_idx, items.size());
  1969. if (0 >= items[p_idx].max_states) {
  1970. return;
  1971. }
  1972. ++items.write[p_idx].state;
  1973. if (items.write[p_idx].max_states <= items[p_idx].state) {
  1974. items.write[p_idx].state = 0;
  1975. }
  1976. if (global_menu.is_valid()) {
  1977. NativeMenu::get_singleton()->set_item_state(global_menu, p_idx, items[p_idx].state);
  1978. }
  1979. control->queue_redraw();
  1980. _menu_changed();
  1981. }
  1982. bool PopupMenu::is_item_checkable(int p_idx) const {
  1983. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1984. return items[p_idx].checkable_type;
  1985. }
  1986. bool PopupMenu::is_item_radio_checkable(int p_idx) const {
  1987. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1988. return items[p_idx].checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  1989. }
  1990. bool PopupMenu::is_item_shortcut_global(int p_idx) const {
  1991. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1992. return items[p_idx].shortcut_is_global;
  1993. }
  1994. bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
  1995. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  1996. return items[p_idx].shortcut_is_disabled;
  1997. }
  1998. void PopupMenu::set_focused_item(int p_idx) {
  1999. if (p_idx != -1) {
  2000. ERR_FAIL_INDEX(p_idx, items.size());
  2001. }
  2002. if (mouse_over == p_idx) {
  2003. return;
  2004. }
  2005. mouse_over = p_idx;
  2006. if (mouse_over != -1) {
  2007. scroll_to_item(mouse_over);
  2008. }
  2009. control->queue_redraw();
  2010. }
  2011. int PopupMenu::get_focused_item() const {
  2012. return mouse_over;
  2013. }
  2014. void PopupMenu::set_item_count(int p_count) {
  2015. ERR_FAIL_COND(p_count < 0);
  2016. int prev_size = items.size();
  2017. if (prev_size == p_count) {
  2018. return;
  2019. }
  2020. NativeMenu *nmenu = NativeMenu::get_singleton();
  2021. bool is_global = global_menu.is_valid();
  2022. if (is_global && prev_size > p_count) {
  2023. for (int i = prev_size - 1; i >= p_count; i--) {
  2024. nmenu->remove_item(global_menu, i);
  2025. }
  2026. }
  2027. items.resize(p_count);
  2028. if (prev_size < p_count) {
  2029. for (int i = prev_size; i < p_count; i++) {
  2030. items.write[i].id = i;
  2031. if (is_global) {
  2032. nmenu->add_item(global_menu, String(), callable_mp(this, &PopupMenu::activate_item), Callable(), i);
  2033. }
  2034. }
  2035. }
  2036. control->queue_redraw();
  2037. child_controls_changed();
  2038. notify_property_list_changed();
  2039. _menu_changed();
  2040. }
  2041. int PopupMenu::get_item_count() const {
  2042. return items.size();
  2043. }
  2044. void PopupMenu::scroll_to_item(int p_idx) {
  2045. ERR_FAIL_INDEX(p_idx, items.size());
  2046. // Calculate the position of the item relative to the visible area.
  2047. int item_y = items[p_idx]._ofs_cache;
  2048. int visible_height = scroll_container->get_size().height;
  2049. int relative_y = item_y - scroll_container->get_v_scroll();
  2050. // If item is not fully visible, adjust scroll.
  2051. if (relative_y < 0) {
  2052. scroll_container->set_v_scroll(item_y);
  2053. } else if (relative_y + items[p_idx]._height_cache > visible_height) {
  2054. scroll_container->set_v_scroll(item_y + items[p_idx]._height_cache - visible_height);
  2055. }
  2056. }
  2057. void PopupMenu::set_prefer_native_menu(bool p_enabled) {
  2058. if (prefer_native != p_enabled) {
  2059. prefer_native = p_enabled;
  2060. if (prefer_native) {
  2061. bind_global_menu();
  2062. } else {
  2063. unbind_global_menu();
  2064. }
  2065. }
  2066. }
  2067. bool PopupMenu::is_prefer_native_menu() const {
  2068. return prefer_native;
  2069. }
  2070. bool PopupMenu::is_native_menu() const {
  2071. #ifdef TOOLS_ENABLED
  2072. if (is_part_of_edited_scene()) {
  2073. return false;
  2074. }
  2075. #endif
  2076. return global_menu.is_valid();
  2077. }
  2078. bool PopupMenu::activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only) {
  2079. ERR_FAIL_COND_V(p_event.is_null(), false);
  2080. Key code = Key::NONE;
  2081. Ref<InputEventKey> k = p_event;
  2082. if (k.is_valid()) {
  2083. code = k->get_keycode();
  2084. if (code == Key::NONE) {
  2085. code = (Key)k->get_unicode();
  2086. }
  2087. if (k->is_ctrl_pressed()) {
  2088. code |= KeyModifierMask::CTRL;
  2089. }
  2090. if (k->is_alt_pressed()) {
  2091. code |= KeyModifierMask::ALT;
  2092. }
  2093. if (k->is_meta_pressed()) {
  2094. code |= KeyModifierMask::META;
  2095. }
  2096. if (k->is_shift_pressed()) {
  2097. code |= KeyModifierMask::SHIFT;
  2098. }
  2099. }
  2100. for (int i = 0; i < items.size(); i++) {
  2101. if (is_item_disabled(i) || items[i].shortcut_is_disabled || (!items[i].allow_echo && p_event->is_echo())) {
  2102. continue;
  2103. }
  2104. if (items[i].shortcut.is_valid() && items[i].shortcut->matches_event(p_event) && (items[i].shortcut_is_global || !p_for_global_only)) {
  2105. activate_item(i);
  2106. return true;
  2107. }
  2108. if (code != Key::NONE && items[i].accel == code) {
  2109. activate_item(i);
  2110. return true;
  2111. }
  2112. if (items[i].submenu) {
  2113. if (items[i].submenu->activate_item_by_event(p_event, p_for_global_only)) {
  2114. return true;
  2115. }
  2116. }
  2117. }
  2118. return false;
  2119. }
  2120. void PopupMenu::_about_to_popup() {
  2121. ERR_MAIN_THREAD_GUARD;
  2122. emit_signal(SNAME("about_to_popup"));
  2123. }
  2124. void PopupMenu::_about_to_close() {
  2125. ERR_MAIN_THREAD_GUARD;
  2126. emit_signal(SNAME("popup_hide"));
  2127. }
  2128. void PopupMenu::activate_item(int p_idx) {
  2129. ERR_FAIL_INDEX(p_idx, items.size());
  2130. ERR_FAIL_COND(items[p_idx].separator);
  2131. int id = items[p_idx].id >= 0 ? items[p_idx].id : p_idx;
  2132. //hide all parent PopupMenus
  2133. Node *next = get_parent();
  2134. PopupMenu *pop = Object::cast_to<PopupMenu>(next);
  2135. while (pop) {
  2136. // We close all parents that are chained together,
  2137. // with hide_on_item_selection enabled
  2138. if (items[p_idx].checkable_type) {
  2139. if (!hide_on_checkable_item_selection || !pop->is_hide_on_checkable_item_selection()) {
  2140. break;
  2141. }
  2142. } else if (0 < items[p_idx].max_states) {
  2143. if (!hide_on_multistate_item_selection || !pop->is_hide_on_multistate_item_selection()) {
  2144. break;
  2145. }
  2146. } else if (!hide_on_item_selection || !pop->is_hide_on_item_selection()) {
  2147. break;
  2148. }
  2149. pop->hide();
  2150. next = next->get_parent();
  2151. pop = Object::cast_to<PopupMenu>(next);
  2152. }
  2153. // Hides popup by default; unless otherwise specified
  2154. // by using set_hide_on_item_selection and set_hide_on_checkable_item_selection
  2155. bool need_hide = true;
  2156. if (items[p_idx].checkable_type) {
  2157. if (!hide_on_checkable_item_selection) {
  2158. need_hide = false;
  2159. }
  2160. } else if (0 < items[p_idx].max_states) {
  2161. if (!hide_on_multistate_item_selection) {
  2162. need_hide = false;
  2163. }
  2164. } else if (!hide_on_item_selection) {
  2165. need_hide = false;
  2166. }
  2167. if (need_hide) {
  2168. hide();
  2169. }
  2170. emit_signal(SceneStringName(id_pressed), id);
  2171. emit_signal(SNAME("index_pressed"), p_idx);
  2172. }
  2173. void PopupMenu::remove_item(int p_idx) {
  2174. ERR_FAIL_INDEX(p_idx, items.size());
  2175. if (items[p_idx].shortcut.is_valid()) {
  2176. _unref_shortcut(items[p_idx].shortcut);
  2177. }
  2178. items.remove_at(p_idx);
  2179. if (global_menu.is_valid()) {
  2180. NativeMenu::get_singleton()->remove_item(global_menu, p_idx);
  2181. }
  2182. control->queue_redraw();
  2183. child_controls_changed();
  2184. _menu_changed();
  2185. }
  2186. void PopupMenu::add_separator(const String &p_text, int p_id) {
  2187. Item sep;
  2188. sep.separator = true;
  2189. sep.id = p_id;
  2190. if (!p_text.is_empty()) {
  2191. sep.text = p_text;
  2192. sep.xl_text = atr(p_text);
  2193. }
  2194. items.push_back(sep);
  2195. if (global_menu.is_valid()) {
  2196. NativeMenu::get_singleton()->add_separator(global_menu);
  2197. }
  2198. control->queue_redraw();
  2199. _menu_changed();
  2200. }
  2201. void PopupMenu::clear(bool p_free_submenus) {
  2202. for (const Item &I : items) {
  2203. if (I.shortcut.is_valid()) {
  2204. _unref_shortcut(I.shortcut);
  2205. }
  2206. if (p_free_submenus && I.submenu) {
  2207. remove_child(I.submenu);
  2208. I.submenu->queue_free();
  2209. }
  2210. }
  2211. if (global_menu.is_valid()) {
  2212. NativeMenu *nmenu = NativeMenu::get_singleton();
  2213. for (int i = items.size() - 1; i >= 0; i--) {
  2214. Item &item = items.write[i];
  2215. if (item.submenu) {
  2216. item.submenu->unbind_global_menu();
  2217. item.submenu_bound = false;
  2218. }
  2219. nmenu->remove_item(global_menu, i);
  2220. }
  2221. }
  2222. items.clear();
  2223. mouse_over = -1;
  2224. control->queue_redraw();
  2225. child_controls_changed();
  2226. notify_property_list_changed();
  2227. _menu_changed();
  2228. }
  2229. void PopupMenu::_ref_shortcut(Ref<Shortcut> p_sc) {
  2230. if (!shortcut_refcount.has(p_sc)) {
  2231. shortcut_refcount[p_sc] = 1;
  2232. p_sc->connect_changed(callable_mp(this, &PopupMenu::_shortcut_changed));
  2233. } else {
  2234. shortcut_refcount[p_sc] += 1;
  2235. }
  2236. }
  2237. void PopupMenu::_unref_shortcut(Ref<Shortcut> p_sc) {
  2238. ERR_FAIL_COND(!shortcut_refcount.has(p_sc));
  2239. shortcut_refcount[p_sc]--;
  2240. if (shortcut_refcount[p_sc] == 0) {
  2241. p_sc->disconnect_changed(callable_mp(this, &PopupMenu::_shortcut_changed));
  2242. shortcut_refcount.erase(p_sc);
  2243. }
  2244. }
  2245. void PopupMenu::_shortcut_changed() {
  2246. for (int i = 0; i < items.size(); i++) {
  2247. items.write[i].dirty = true;
  2248. }
  2249. control->queue_redraw();
  2250. }
  2251. // Hide on item selection determines whether or not the popup will close after item selection
  2252. void PopupMenu::set_hide_on_item_selection(bool p_enabled) {
  2253. hide_on_item_selection = p_enabled;
  2254. }
  2255. bool PopupMenu::is_hide_on_item_selection() const {
  2256. return hide_on_item_selection;
  2257. }
  2258. void PopupMenu::set_hide_on_checkable_item_selection(bool p_enabled) {
  2259. hide_on_checkable_item_selection = p_enabled;
  2260. }
  2261. bool PopupMenu::is_hide_on_checkable_item_selection() const {
  2262. return hide_on_checkable_item_selection;
  2263. }
  2264. void PopupMenu::set_hide_on_multistate_item_selection(bool p_enabled) {
  2265. hide_on_multistate_item_selection = p_enabled;
  2266. }
  2267. bool PopupMenu::is_hide_on_multistate_item_selection() const {
  2268. return hide_on_multistate_item_selection;
  2269. }
  2270. void PopupMenu::set_submenu_popup_delay(float p_time) {
  2271. if (p_time <= 0) {
  2272. p_time = 0.01;
  2273. }
  2274. submenu_timer->set_wait_time(p_time);
  2275. }
  2276. float PopupMenu::get_submenu_popup_delay() const {
  2277. return submenu_timer->get_wait_time();
  2278. }
  2279. void PopupMenu::set_allow_search(bool p_allow) {
  2280. allow_search = p_allow;
  2281. }
  2282. bool PopupMenu::get_allow_search() const {
  2283. return allow_search;
  2284. }
  2285. String PopupMenu::get_tooltip(const Point2 &p_pos) const {
  2286. int over = _get_mouse_over(p_pos);
  2287. if (over < 0 || over >= items.size()) {
  2288. return "";
  2289. }
  2290. return items[over].tooltip;
  2291. }
  2292. #ifdef TOOLS_ENABLED
  2293. PackedStringArray PopupMenu::get_configuration_warnings() const {
  2294. PackedStringArray warnings = Popup::get_configuration_warnings();
  2295. if (!DisplayServer::get_singleton()->is_window_transparency_available() && !GLOBAL_GET("display/window/subwindows/embed_subwindows")) {
  2296. Ref<StyleBoxFlat> sb = theme_cache.panel_style;
  2297. if (sb.is_valid() && (sb->get_shadow_size() > 0 || sb->get_corner_radius(CORNER_TOP_LEFT) > 0 || sb->get_corner_radius(CORNER_TOP_RIGHT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_LEFT) > 0 || sb->get_corner_radius(CORNER_BOTTOM_RIGHT) > 0)) {
  2298. warnings.push_back(RTR("The current theme style has shadows and/or rounded corners for popups, but those won't display correctly if \"display/window/per_pixel_transparency/allowed\" isn't enabled in the Project Settings, nor if it isn't supported."));
  2299. }
  2300. }
  2301. return warnings;
  2302. }
  2303. #endif
  2304. void PopupMenu::add_autohide_area(const Rect2 &p_area) {
  2305. autohide_areas.push_back(p_area);
  2306. }
  2307. void PopupMenu::clear_autohide_areas() {
  2308. autohide_areas.clear();
  2309. }
  2310. bool PopupMenu::_set(const StringName &p_name, const Variant &p_value) {
  2311. if (property_helper.property_set_value(p_name, p_value)) {
  2312. return true;
  2313. }
  2314. #ifndef DISABLE_DEPRECATED
  2315. // Compatibility.
  2316. if (p_name == "items") {
  2317. Array arr = p_value;
  2318. ERR_FAIL_COND_V(arr.size() % 10, false);
  2319. clear();
  2320. for (int i = 0; i < arr.size(); i += 10) {
  2321. String text = arr[i + 0];
  2322. Ref<Texture2D> icon = arr[i + 1];
  2323. // For compatibility, use false/true for no/checkbox and integers for other values
  2324. bool checkable = arr[i + 2];
  2325. bool radio_checkable = (int)arr[i + 2] == Item::CHECKABLE_TYPE_RADIO_BUTTON;
  2326. bool checked = arr[i + 3];
  2327. bool disabled = arr[i + 4];
  2328. int id = arr[i + 5];
  2329. int accel = arr[i + 6];
  2330. Variant meta = arr[i + 7];
  2331. String subm = arr[i + 8];
  2332. bool sep = arr[i + 9];
  2333. int idx = get_item_count();
  2334. add_item(text, id);
  2335. set_item_icon(idx, icon);
  2336. if (checkable) {
  2337. if (radio_checkable) {
  2338. set_item_as_radio_checkable(idx, true);
  2339. } else {
  2340. set_item_as_checkable(idx, true);
  2341. }
  2342. }
  2343. set_item_checked(idx, checked);
  2344. set_item_disabled(idx, disabled);
  2345. set_item_id(idx, id);
  2346. set_item_metadata(idx, meta);
  2347. set_item_as_separator(idx, sep);
  2348. set_item_accelerator(idx, (Key)accel);
  2349. set_item_submenu(idx, subm);
  2350. }
  2351. }
  2352. #endif
  2353. return false;
  2354. }
  2355. void PopupMenu::_bind_methods() {
  2356. ClassDB::bind_method(D_METHOD("activate_item_by_event", "event", "for_global_only"), &PopupMenu::activate_item_by_event, DEFVAL(false));
  2357. ClassDB::bind_method(D_METHOD("set_prefer_native_menu", "enabled"), &PopupMenu::set_prefer_native_menu);
  2358. ClassDB::bind_method(D_METHOD("is_prefer_native_menu"), &PopupMenu::is_prefer_native_menu);
  2359. ClassDB::bind_method(D_METHOD("is_native_menu"), &PopupMenu::is_native_menu);
  2360. ClassDB::bind_method(D_METHOD("add_item", "label", "id", "accel"), &PopupMenu::add_item, DEFVAL(-1), DEFVAL(0));
  2361. ClassDB::bind_method(D_METHOD("add_icon_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_item, DEFVAL(-1), DEFVAL(0));
  2362. ClassDB::bind_method(D_METHOD("add_check_item", "label", "id", "accel"), &PopupMenu::add_check_item, DEFVAL(-1), DEFVAL(0));
  2363. ClassDB::bind_method(D_METHOD("add_icon_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_check_item, DEFVAL(-1), DEFVAL(0));
  2364. ClassDB::bind_method(D_METHOD("add_radio_check_item", "label", "id", "accel"), &PopupMenu::add_radio_check_item, DEFVAL(-1), DEFVAL(0));
  2365. ClassDB::bind_method(D_METHOD("add_icon_radio_check_item", "texture", "label", "id", "accel"), &PopupMenu::add_icon_radio_check_item, DEFVAL(-1), DEFVAL(0));
  2366. ClassDB::bind_method(D_METHOD("add_multistate_item", "label", "max_states", "default_state", "id", "accel"), &PopupMenu::add_multistate_item, DEFVAL(0), DEFVAL(-1), DEFVAL(0));
  2367. ClassDB::bind_method(D_METHOD("add_shortcut", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false));
  2368. ClassDB::bind_method(D_METHOD("add_icon_shortcut", "texture", "shortcut", "id", "global", "allow_echo"), &PopupMenu::add_icon_shortcut, DEFVAL(-1), DEFVAL(false), DEFVAL(false));
  2369. ClassDB::bind_method(D_METHOD("add_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2370. ClassDB::bind_method(D_METHOD("add_icon_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2371. ClassDB::bind_method(D_METHOD("add_radio_check_shortcut", "shortcut", "id", "global"), &PopupMenu::add_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2372. ClassDB::bind_method(D_METHOD("add_icon_radio_check_shortcut", "texture", "shortcut", "id", "global"), &PopupMenu::add_icon_radio_check_shortcut, DEFVAL(-1), DEFVAL(false));
  2373. ClassDB::bind_method(D_METHOD("add_submenu_item", "label", "submenu", "id"), &PopupMenu::add_submenu_item, DEFVAL(-1));
  2374. ClassDB::bind_method(D_METHOD("add_submenu_node_item", "label", "submenu", "id"), &PopupMenu::add_submenu_node_item, DEFVAL(-1));
  2375. ClassDB::bind_method(D_METHOD("set_item_text", "index", "text"), &PopupMenu::set_item_text);
  2376. ClassDB::bind_method(D_METHOD("set_item_text_direction", "index", "direction"), &PopupMenu::set_item_text_direction);
  2377. ClassDB::bind_method(D_METHOD("set_item_language", "index", "language"), &PopupMenu::set_item_language);
  2378. ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "index", "mode"), &PopupMenu::set_item_auto_translate_mode);
  2379. ClassDB::bind_method(D_METHOD("set_item_icon", "index", "icon"), &PopupMenu::set_item_icon);
  2380. ClassDB::bind_method(D_METHOD("set_item_icon_max_width", "index", "width"), &PopupMenu::set_item_icon_max_width);
  2381. ClassDB::bind_method(D_METHOD("set_item_icon_modulate", "index", "modulate"), &PopupMenu::set_item_icon_modulate);
  2382. ClassDB::bind_method(D_METHOD("set_item_checked", "index", "checked"), &PopupMenu::set_item_checked);
  2383. ClassDB::bind_method(D_METHOD("set_item_id", "index", "id"), &PopupMenu::set_item_id);
  2384. ClassDB::bind_method(D_METHOD("set_item_accelerator", "index", "accel"), &PopupMenu::set_item_accelerator);
  2385. ClassDB::bind_method(D_METHOD("set_item_metadata", "index", "metadata"), &PopupMenu::set_item_metadata);
  2386. ClassDB::bind_method(D_METHOD("set_item_disabled", "index", "disabled"), &PopupMenu::set_item_disabled);
  2387. ClassDB::bind_method(D_METHOD("set_item_submenu", "index", "submenu"), &PopupMenu::set_item_submenu);
  2388. ClassDB::bind_method(D_METHOD("set_item_submenu_node", "index", "submenu"), &PopupMenu::set_item_submenu_node);
  2389. ClassDB::bind_method(D_METHOD("set_item_as_separator", "index", "enable"), &PopupMenu::set_item_as_separator);
  2390. ClassDB::bind_method(D_METHOD("set_item_as_checkable", "index", "enable"), &PopupMenu::set_item_as_checkable);
  2391. ClassDB::bind_method(D_METHOD("set_item_as_radio_checkable", "index", "enable"), &PopupMenu::set_item_as_radio_checkable);
  2392. ClassDB::bind_method(D_METHOD("set_item_tooltip", "index", "tooltip"), &PopupMenu::set_item_tooltip);
  2393. ClassDB::bind_method(D_METHOD("set_item_shortcut", "index", "shortcut", "global"), &PopupMenu::set_item_shortcut, DEFVAL(false));
  2394. ClassDB::bind_method(D_METHOD("set_item_indent", "index", "indent"), &PopupMenu::set_item_indent);
  2395. ClassDB::bind_method(D_METHOD("set_item_multistate", "index", "state"), &PopupMenu::set_item_multistate);
  2396. ClassDB::bind_method(D_METHOD("set_item_multistate_max", "index", "max_states"), &PopupMenu::set_item_max_states);
  2397. ClassDB::bind_method(D_METHOD("set_item_shortcut_disabled", "index", "disabled"), &PopupMenu::set_item_shortcut_disabled);
  2398. ClassDB::bind_method(D_METHOD("toggle_item_checked", "index"), &PopupMenu::toggle_item_checked);
  2399. ClassDB::bind_method(D_METHOD("toggle_item_multistate", "index"), &PopupMenu::toggle_item_multistate);
  2400. ClassDB::bind_method(D_METHOD("get_item_text", "index"), &PopupMenu::get_item_text);
  2401. ClassDB::bind_method(D_METHOD("get_item_text_direction", "index"), &PopupMenu::get_item_text_direction);
  2402. ClassDB::bind_method(D_METHOD("get_item_language", "index"), &PopupMenu::get_item_language);
  2403. ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "index"), &PopupMenu::get_item_auto_translate_mode);
  2404. ClassDB::bind_method(D_METHOD("get_item_icon", "index"), &PopupMenu::get_item_icon);
  2405. ClassDB::bind_method(D_METHOD("get_item_icon_max_width", "index"), &PopupMenu::get_item_icon_max_width);
  2406. ClassDB::bind_method(D_METHOD("get_item_icon_modulate", "index"), &PopupMenu::get_item_icon_modulate);
  2407. ClassDB::bind_method(D_METHOD("is_item_checked", "index"), &PopupMenu::is_item_checked);
  2408. ClassDB::bind_method(D_METHOD("get_item_id", "index"), &PopupMenu::get_item_id);
  2409. ClassDB::bind_method(D_METHOD("get_item_index", "id"), &PopupMenu::get_item_index);
  2410. ClassDB::bind_method(D_METHOD("get_item_accelerator", "index"), &PopupMenu::get_item_accelerator);
  2411. ClassDB::bind_method(D_METHOD("get_item_metadata", "index"), &PopupMenu::get_item_metadata);
  2412. ClassDB::bind_method(D_METHOD("is_item_disabled", "index"), &PopupMenu::is_item_disabled);
  2413. ClassDB::bind_method(D_METHOD("get_item_submenu", "index"), &PopupMenu::get_item_submenu);
  2414. ClassDB::bind_method(D_METHOD("get_item_submenu_node", "index"), &PopupMenu::get_item_submenu_node);
  2415. ClassDB::bind_method(D_METHOD("is_item_separator", "index"), &PopupMenu::is_item_separator);
  2416. ClassDB::bind_method(D_METHOD("is_item_checkable", "index"), &PopupMenu::is_item_checkable);
  2417. ClassDB::bind_method(D_METHOD("is_item_radio_checkable", "index"), &PopupMenu::is_item_radio_checkable);
  2418. ClassDB::bind_method(D_METHOD("is_item_shortcut_disabled", "index"), &PopupMenu::is_item_shortcut_disabled);
  2419. ClassDB::bind_method(D_METHOD("get_item_tooltip", "index"), &PopupMenu::get_item_tooltip);
  2420. ClassDB::bind_method(D_METHOD("get_item_shortcut", "index"), &PopupMenu::get_item_shortcut);
  2421. ClassDB::bind_method(D_METHOD("get_item_indent", "index"), &PopupMenu::get_item_indent);
  2422. ClassDB::bind_method(D_METHOD("get_item_multistate_max", "index"), &PopupMenu::get_item_max_states);
  2423. ClassDB::bind_method(D_METHOD("get_item_multistate", "index"), &PopupMenu::get_item_state);
  2424. ClassDB::bind_method(D_METHOD("set_focused_item", "index"), &PopupMenu::set_focused_item);
  2425. ClassDB::bind_method(D_METHOD("get_focused_item"), &PopupMenu::get_focused_item);
  2426. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &PopupMenu::set_item_count);
  2427. ClassDB::bind_method(D_METHOD("get_item_count"), &PopupMenu::get_item_count);
  2428. ClassDB::bind_method(D_METHOD("scroll_to_item", "index"), &PopupMenu::scroll_to_item);
  2429. ClassDB::bind_method(D_METHOD("remove_item", "index"), &PopupMenu::remove_item);
  2430. ClassDB::bind_method(D_METHOD("add_separator", "label", "id"), &PopupMenu::add_separator, DEFVAL(String()), DEFVAL(-1));
  2431. ClassDB::bind_method(D_METHOD("clear", "free_submenus"), &PopupMenu::clear, DEFVAL(false));
  2432. ClassDB::bind_method(D_METHOD("set_hide_on_item_selection", "enable"), &PopupMenu::set_hide_on_item_selection);
  2433. ClassDB::bind_method(D_METHOD("is_hide_on_item_selection"), &PopupMenu::is_hide_on_item_selection);
  2434. ClassDB::bind_method(D_METHOD("set_hide_on_checkable_item_selection", "enable"), &PopupMenu::set_hide_on_checkable_item_selection);
  2435. ClassDB::bind_method(D_METHOD("is_hide_on_checkable_item_selection"), &PopupMenu::is_hide_on_checkable_item_selection);
  2436. ClassDB::bind_method(D_METHOD("set_hide_on_state_item_selection", "enable"), &PopupMenu::set_hide_on_multistate_item_selection);
  2437. ClassDB::bind_method(D_METHOD("is_hide_on_state_item_selection"), &PopupMenu::is_hide_on_multistate_item_selection);
  2438. ClassDB::bind_method(D_METHOD("set_submenu_popup_delay", "seconds"), &PopupMenu::set_submenu_popup_delay);
  2439. ClassDB::bind_method(D_METHOD("get_submenu_popup_delay"), &PopupMenu::get_submenu_popup_delay);
  2440. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &PopupMenu::set_allow_search);
  2441. ClassDB::bind_method(D_METHOD("get_allow_search"), &PopupMenu::get_allow_search);
  2442. ClassDB::bind_method(D_METHOD("is_system_menu"), &PopupMenu::is_system_menu);
  2443. ClassDB::bind_method(D_METHOD("set_system_menu", "system_menu_id"), &PopupMenu::set_system_menu);
  2444. ClassDB::bind_method(D_METHOD("get_system_menu"), &PopupMenu::get_system_menu);
  2445. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_item_selection"), "set_hide_on_item_selection", "is_hide_on_item_selection");
  2446. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_checkable_item_selection"), "set_hide_on_checkable_item_selection", "is_hide_on_checkable_item_selection");
  2447. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_on_state_item_selection"), "set_hide_on_state_item_selection", "is_hide_on_state_item_selection");
  2448. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "submenu_popup_delay", PROPERTY_HINT_NONE, "suffix:s"), "set_submenu_popup_delay", "get_submenu_popup_delay");
  2449. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  2450. ADD_PROPERTY(PropertyInfo(Variant::INT, "system_menu_id", PROPERTY_HINT_ENUM, "None:0,Application Menu:2,Window Menu:3,Help Menu:4,Dock:5"), "set_system_menu", "get_system_menu");
  2451. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefer_native_menu"), "set_prefer_native_menu", "is_prefer_native_menu");
  2452. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "item_");
  2453. ADD_SIGNAL(MethodInfo("id_pressed", PropertyInfo(Variant::INT, "id")));
  2454. ADD_SIGNAL(MethodInfo("id_focused", PropertyInfo(Variant::INT, "id")));
  2455. ADD_SIGNAL(MethodInfo("index_pressed", PropertyInfo(Variant::INT, "index")));
  2456. ADD_SIGNAL(MethodInfo("menu_changed"));
  2457. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, panel_style, "panel");
  2458. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, hover_style, "hover");
  2459. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, separator_style, "separator");
  2460. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_left);
  2461. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, PopupMenu, labeled_separator_right);
  2462. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, v_separation);
  2463. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, h_separation);
  2464. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, indent);
  2465. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_start_padding);
  2466. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, item_end_padding);
  2467. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, PopupMenu, icon_max_width);
  2468. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked);
  2469. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, checked_disabled);
  2470. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked);
  2471. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, unchecked_disabled);
  2472. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked);
  2473. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_checked_disabled);
  2474. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked);
  2475. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, radio_unchecked_disabled);
  2476. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu);
  2477. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, PopupMenu, submenu_mirrored);
  2478. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font);
  2479. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_size);
  2480. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, PopupMenu, font_separator);
  2481. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, PopupMenu, font_separator_size);
  2482. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_color);
  2483. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_hover_color);
  2484. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_disabled_color);
  2485. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_accelerator_color);
  2486. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_outline_size, "outline_size");
  2487. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_outline_color);
  2488. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_color);
  2489. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, PopupMenu, font_separator_outline_size, "separator_outline_size");
  2490. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, PopupMenu, font_separator_outline_color);
  2491. Item defaults(true);
  2492. base_property_helper.set_prefix("item_");
  2493. base_property_helper.set_array_length_getter(&PopupMenu::get_item_count);
  2494. base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &PopupMenu::set_item_text, &PopupMenu::get_item_text);
  2495. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &PopupMenu::set_item_icon, &PopupMenu::get_item_icon);
  2496. base_property_helper.register_property(PropertyInfo(Variant::INT, "checkable", PROPERTY_HINT_ENUM, "No,As checkbox,As radio button"), defaults.checkable_type, &PopupMenu::_set_item_checkable_type, &PopupMenu::_get_item_checkable_type);
  2497. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "checked"), defaults.checked, &PopupMenu::set_item_checked, &PopupMenu::is_item_checked);
  2498. base_property_helper.register_property(PropertyInfo(Variant::INT, "id", PROPERTY_HINT_RANGE, "0,10,1,or_greater", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL), defaults.id, &PopupMenu::set_item_id, &PopupMenu::get_item_id);
  2499. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &PopupMenu::set_item_disabled, &PopupMenu::is_item_disabled);
  2500. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator, &PopupMenu::set_item_as_separator, &PopupMenu::is_item_separator);
  2501. PropertyListHelper::register_base_helper(&base_property_helper);
  2502. }
  2503. void PopupMenu::_native_popup(const Rect2i &p_rect) {
  2504. Point2i popup_pos = p_rect.position;
  2505. if (is_embedded()) {
  2506. popup_pos = get_embedder()->get_screen_transform().xform(popup_pos); // Note: for embedded windows "screen transform" is transform relative to embedder not the actual screen.
  2507. DisplayServer::WindowID wid = get_window_id();
  2508. if (wid == DisplayServer::INVALID_WINDOW_ID) {
  2509. wid = DisplayServer::MAIN_WINDOW_ID;
  2510. }
  2511. popup_pos += DisplayServer::get_singleton()->window_get_position(wid);
  2512. }
  2513. float win_scale = get_parent_visible_window()->get_content_scale_factor();
  2514. NativeMenu::get_singleton()->set_minimum_width(global_menu, p_rect.size.x * win_scale);
  2515. NativeMenu::get_singleton()->popup(global_menu, popup_pos);
  2516. }
  2517. String PopupMenu::_atr(int p_idx, const String &p_text) const {
  2518. ERR_FAIL_INDEX_V(p_idx, items.size(), atr(p_text));
  2519. switch (items[p_idx].auto_translate_mode) {
  2520. case AUTO_TRANSLATE_MODE_INHERIT: {
  2521. return atr(p_text);
  2522. } break;
  2523. case AUTO_TRANSLATE_MODE_ALWAYS: {
  2524. return tr(p_text);
  2525. } break;
  2526. case AUTO_TRANSLATE_MODE_DISABLED: {
  2527. return p_text;
  2528. } break;
  2529. }
  2530. ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(items[p_idx].auto_translate_mode));
  2531. }
  2532. void PopupMenu::popup(const Rect2i &p_bounds) {
  2533. bool native = global_menu.is_valid();
  2534. #ifdef TOOLS_ENABLED
  2535. if (is_part_of_edited_scene()) {
  2536. native = false;
  2537. }
  2538. #endif
  2539. if (native) {
  2540. _native_popup(p_bounds != Rect2i() ? p_bounds : Rect2i(get_position(), Size2i()));
  2541. } else {
  2542. set_flag(FLAG_NO_FOCUS, !is_embedded());
  2543. moved = Vector2();
  2544. popup_time_msec = OS::get_singleton()->get_ticks_msec();
  2545. if (!is_embedded()) {
  2546. float win_scale = get_parent_visible_window()->get_content_scale_factor();
  2547. set_content_scale_factor(win_scale);
  2548. Size2 minsize = get_contents_minimum_size() * win_scale;
  2549. minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing.
  2550. set_min_size(minsize); // `height` is truncated here by the cast to Size2i for Window.min_size.
  2551. set_size(Vector2(0, 0)); // Shrinkwraps to min size.
  2552. }
  2553. Popup::popup(p_bounds);
  2554. }
  2555. }
  2556. void PopupMenu::set_visible(bool p_visible) {
  2557. bool native = global_menu.is_valid();
  2558. #ifdef TOOLS_ENABLED
  2559. if (is_part_of_edited_scene()) {
  2560. native = false;
  2561. }
  2562. #endif
  2563. if (native) {
  2564. if (p_visible) {
  2565. _native_popup(Rect2i(get_position(), get_size()));
  2566. }
  2567. } else {
  2568. set_flag(FLAG_NO_FOCUS, !is_embedded());
  2569. Popup::set_visible(p_visible);
  2570. }
  2571. }
  2572. PopupMenu::PopupMenu() {
  2573. set_flag(FLAG_TRANSPARENT, true);
  2574. // The panel used to draw the panel style.
  2575. panel = memnew(PanelContainer);
  2576. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2577. add_child(panel, false, INTERNAL_MODE_FRONT);
  2578. // Scroll Container
  2579. scroll_container = memnew(ScrollContainer);
  2580. scroll_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2581. panel->add_child(scroll_container, false, INTERNAL_MODE_FRONT);
  2582. // The control which will display the items
  2583. control = memnew(Control);
  2584. control->set_clip_contents(false);
  2585. control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2586. control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2587. control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2588. scroll_container->add_child(control, false, INTERNAL_MODE_FRONT);
  2589. control->connect(SceneStringName(draw), callable_mp(this, &PopupMenu::_draw_items));
  2590. submenu_timer = memnew(Timer);
  2591. submenu_timer->set_wait_time(0.3);
  2592. submenu_timer->set_one_shot(true);
  2593. submenu_timer->connect("timeout", callable_mp(this, &PopupMenu::_submenu_timeout));
  2594. add_child(submenu_timer, false, INTERNAL_MODE_FRONT);
  2595. minimum_lifetime_timer = memnew(Timer);
  2596. minimum_lifetime_timer->set_wait_time(0.3);
  2597. minimum_lifetime_timer->set_one_shot(true);
  2598. minimum_lifetime_timer->connect("timeout", callable_mp(this, &PopupMenu::_minimum_lifetime_timeout));
  2599. add_child(minimum_lifetime_timer, false, INTERNAL_MODE_FRONT);
  2600. property_helper.setup_for_instance(base_property_helper, this);
  2601. #ifdef TOOLS_ENABLED
  2602. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
  2603. #endif
  2604. }
  2605. PopupMenu::~PopupMenu() {
  2606. unbind_global_menu();
  2607. }