popup_menu.cpp 113 KB

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