popup_menu.cpp 96 KB

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