popup_menu.cpp 102 KB

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