popup_menu.cpp 113 KB

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