popup_menu.cpp 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312
  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("ui_down", 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("ui_up", 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].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. bool ac = get_tree()->is_accessibility_enabled();
  2698. // Note: Native popup menus need keyboard focus to work with screen reader.
  2699. set_flag(FLAG_POPUP, !ac);
  2700. set_flag(FLAG_NO_FOCUS, !is_embedded() && !ac);
  2701. if (ac) {
  2702. set_ac_popup();
  2703. }
  2704. }
  2705. moved = Vector2();
  2706. popup_time_msec = OS::get_singleton()->get_ticks_msec();
  2707. Popup::popup(p_bounds);
  2708. }
  2709. }
  2710. void PopupMenu::_pre_popup() {
  2711. Size2 scale = get_force_native() ? get_parent_viewport()->get_popup_base_transform_native().get_scale() : get_parent_viewport()->get_popup_base_transform().get_scale();
  2712. CanvasItem *c = Object::cast_to<CanvasItem>(get_parent());
  2713. if (c) {
  2714. scale *= c->get_global_transform_with_canvas().get_scale();
  2715. }
  2716. real_t popup_scale = MIN(scale.x, scale.y);
  2717. set_content_scale_factor(popup_scale);
  2718. Size2 minsize = get_contents_minimum_size() * popup_scale;
  2719. minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing.
  2720. set_min_size(minsize); // `height` is truncated here by the cast to Size2i for Window.min_size.
  2721. reset_size(); // Shrinkwraps to min size.
  2722. }
  2723. void PopupMenu::set_visible(bool p_visible) {
  2724. bool native = global_menu.is_valid();
  2725. #ifdef TOOLS_ENABLED
  2726. if (is_part_of_edited_scene()) {
  2727. native = false;
  2728. }
  2729. #endif
  2730. if (native) {
  2731. if (p_visible) {
  2732. _native_popup(Rect2i(get_position(), get_size()));
  2733. }
  2734. } else {
  2735. if (is_inside_tree()) {
  2736. bool ac = get_tree()->is_accessibility_enabled();
  2737. // Note: Native popup menus need keyboard focus to work with screen reader.
  2738. set_flag(FLAG_POPUP, !ac);
  2739. set_flag(FLAG_NO_FOCUS, !is_embedded() && !ac);
  2740. if (ac) {
  2741. set_ac_popup();
  2742. }
  2743. }
  2744. Popup::set_visible(p_visible);
  2745. }
  2746. }
  2747. PopupMenu::PopupMenu() {
  2748. set_flag(FLAG_TRANSPARENT, true);
  2749. // The panel used to draw the panel style.
  2750. panel = memnew(PanelContainer);
  2751. panel->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2752. add_child(panel, false, INTERNAL_MODE_FRONT);
  2753. // Scroll Container
  2754. scroll_container = memnew(ScrollContainer);
  2755. scroll_container->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2756. panel->add_child(scroll_container, false, INTERNAL_MODE_FRONT);
  2757. // The control which will display the items
  2758. control = memnew(Control);
  2759. control->set_clip_contents(false);
  2760. control->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2761. control->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  2762. control->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2763. scroll_container->add_child(control, false, INTERNAL_MODE_FRONT);
  2764. control->connect(SceneStringName(draw), callable_mp(this, &PopupMenu::_draw_items));
  2765. submenu_timer = memnew(Timer);
  2766. submenu_timer->set_wait_time(0.3);
  2767. submenu_timer->set_one_shot(true);
  2768. submenu_timer->connect("timeout", callable_mp(this, &PopupMenu::_submenu_timeout));
  2769. add_child(submenu_timer, false, INTERNAL_MODE_FRONT);
  2770. minimum_lifetime_timer = memnew(Timer);
  2771. minimum_lifetime_timer->set_wait_time(0.3);
  2772. minimum_lifetime_timer->set_one_shot(true);
  2773. minimum_lifetime_timer->connect("timeout", callable_mp(this, &PopupMenu::_minimum_lifetime_timeout));
  2774. add_child(minimum_lifetime_timer, false, INTERNAL_MODE_FRONT);
  2775. property_helper.setup_for_instance(base_property_helper, this);
  2776. #ifdef TOOLS_ENABLED
  2777. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp((Node *)this, &Node::update_configuration_warnings));
  2778. #endif
  2779. }
  2780. PopupMenu::~PopupMenu() {
  2781. unbind_global_menu();
  2782. }