2
0

popup_menu.cpp 104 KB

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