menu_bar.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /**************************************************************************/
  2. /* menu_bar.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 "menu_bar.h"
  31. #include "scene/main/window.h"
  32. #include "scene/theme/theme_db.h"
  33. void MenuBar::gui_input(const Ref<InputEvent> &p_event) {
  34. ERR_FAIL_COND(p_event.is_null());
  35. if (is_native_menu()) {
  36. // Handled by OS.
  37. return;
  38. }
  39. MutexLock lock(mutex);
  40. if (p_event->is_action("ui_left", true) && p_event->is_pressed()) {
  41. int new_sel = selected_menu;
  42. int old_sel = (selected_menu < 0) ? 0 : selected_menu;
  43. do {
  44. new_sel--;
  45. if (new_sel < 0) {
  46. new_sel = menu_cache.size() - 1;
  47. }
  48. if (old_sel == new_sel) {
  49. return;
  50. }
  51. } while (menu_cache[new_sel].hidden || menu_cache[new_sel].disabled);
  52. if (selected_menu != new_sel) {
  53. selected_menu = new_sel;
  54. focused_menu = selected_menu;
  55. if (active_menu >= 0) {
  56. get_menu_popup(active_menu)->hide();
  57. }
  58. _open_popup(selected_menu, true);
  59. }
  60. return;
  61. } else if (p_event->is_action("ui_right", true) && p_event->is_pressed()) {
  62. int new_sel = selected_menu;
  63. int old_sel = (selected_menu < 0) ? menu_cache.size() - 1 : selected_menu;
  64. do {
  65. new_sel++;
  66. if (new_sel >= menu_cache.size()) {
  67. new_sel = 0;
  68. }
  69. if (old_sel == new_sel) {
  70. return;
  71. }
  72. } while (menu_cache[new_sel].hidden || menu_cache[new_sel].disabled);
  73. if (selected_menu != new_sel) {
  74. selected_menu = new_sel;
  75. focused_menu = selected_menu;
  76. if (active_menu >= 0) {
  77. get_menu_popup(active_menu)->hide();
  78. }
  79. _open_popup(selected_menu, true);
  80. }
  81. return;
  82. } else if (p_event->is_action("ui_accept", true) && p_event->is_pressed()) {
  83. if (focused_menu == -1) {
  84. focused_menu = 0;
  85. }
  86. selected_menu = focused_menu;
  87. if (active_menu >= 0) {
  88. get_menu_popup(active_menu)->hide();
  89. }
  90. _open_popup(selected_menu, true);
  91. }
  92. Ref<InputEventMouseMotion> mm = p_event;
  93. if (mm.is_valid()) {
  94. int old_sel = selected_menu;
  95. focused_menu = _get_index_at_point(mm->get_position());
  96. if (focused_menu >= 0) {
  97. selected_menu = focused_menu;
  98. }
  99. if (selected_menu != old_sel) {
  100. queue_redraw();
  101. }
  102. }
  103. Ref<InputEventMouseButton> mb = p_event;
  104. if (mb.is_valid()) {
  105. if (mb->is_pressed() && (mb->get_button_index() == MouseButton::LEFT || mb->get_button_index() == MouseButton::RIGHT)) {
  106. int index = _get_index_at_point(mb->get_position());
  107. if (index >= 0) {
  108. _open_popup(index);
  109. }
  110. }
  111. }
  112. }
  113. void MenuBar::_open_popup(int p_index, bool p_focus_item) {
  114. ERR_FAIL_INDEX(p_index, menu_cache.size());
  115. PopupMenu *pm = get_menu_popup(p_index);
  116. if (pm->is_visible()) {
  117. pm->hide();
  118. return;
  119. }
  120. Rect2 item_rect = _get_menu_item_rect(p_index);
  121. Size2 canvas_scale = get_canvas_transform().get_scale();
  122. Point2 screen_pos = get_screen_position() + item_rect.position * canvas_scale;
  123. Size2 screen_size = item_rect.size * canvas_scale;
  124. active_menu = p_index;
  125. pm->set_size(Size2(screen_size.x, 0));
  126. screen_pos.y += screen_size.y;
  127. if (is_layout_rtl()) {
  128. screen_pos.x += screen_size.x - pm->get_size().width;
  129. }
  130. pm->set_position(screen_pos);
  131. pm->popup();
  132. if (p_focus_item) {
  133. for (int i = 0; i < pm->get_item_count(); i++) {
  134. if (!pm->is_item_disabled(i)) {
  135. pm->set_focused_item(i);
  136. break;
  137. }
  138. }
  139. }
  140. queue_redraw();
  141. }
  142. void MenuBar::shortcut_input(const Ref<InputEvent> &p_event) {
  143. ERR_FAIL_COND(p_event.is_null());
  144. if (disable_shortcuts) {
  145. return;
  146. }
  147. if (p_event->is_pressed() && (Object::cast_to<InputEventKey>(p_event.ptr()) || Object::cast_to<InputEventJoypadButton>(p_event.ptr()) || Object::cast_to<InputEventAction>(*p_event) || Object::cast_to<InputEventShortcut>(*p_event))) {
  148. if (!get_parent() || !is_visible_in_tree()) {
  149. return;
  150. }
  151. Vector<PopupMenu *> popups = _get_popups();
  152. for (int i = 0; i < popups.size(); i++) {
  153. if (menu_cache[i].hidden || menu_cache[i].disabled) {
  154. continue;
  155. }
  156. if (popups[i]->activate_item_by_event(p_event, false)) {
  157. accept_event();
  158. return;
  159. }
  160. }
  161. }
  162. }
  163. void MenuBar::_popup_visibility_changed(bool p_visible) {
  164. if (!p_visible) {
  165. active_menu = -1;
  166. focused_menu = -1;
  167. set_process_internal(false);
  168. queue_redraw();
  169. return;
  170. }
  171. if (switch_on_hover) {
  172. set_process_internal(true);
  173. }
  174. }
  175. bool MenuBar::is_native_menu() const {
  176. #ifdef TOOLS_ENABLED
  177. if (is_part_of_edited_scene()) {
  178. return false;
  179. }
  180. #endif
  181. return (NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU) && prefer_native);
  182. }
  183. void MenuBar::bind_global_menu() {
  184. #ifdef TOOLS_ENABLED
  185. if (is_part_of_edited_scene()) {
  186. return;
  187. }
  188. #endif
  189. if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)) {
  190. return;
  191. }
  192. if (!global_menu_tag.is_empty()) {
  193. return; // Already bound.
  194. }
  195. NativeMenu *nmenu = NativeMenu::get_singleton();
  196. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  197. global_menu_tag = "__MenuBar#" + itos(get_instance_id());
  198. int global_start_idx = -1;
  199. int count = nmenu->get_item_count(main_menu);
  200. String prev_tag;
  201. if (start_index >= 0) {
  202. for (int i = 0; i < count; i++) {
  203. String tag = nmenu->get_item_tag(main_menu, i).operator String().get_slicec('#', 1);
  204. if (!tag.is_empty() && tag != prev_tag) {
  205. MenuBar *mb = ObjectDB::get_instance<MenuBar>(ObjectID(static_cast<uint64_t>(tag.to_int())));
  206. if (mb && mb->get_start_index() >= start_index) {
  207. global_start_idx = i;
  208. break;
  209. }
  210. }
  211. prev_tag = tag;
  212. }
  213. }
  214. if (global_start_idx == -1) {
  215. global_start_idx = count;
  216. }
  217. Vector<PopupMenu *> popups = _get_popups();
  218. for (int i = 0; i < menu_cache.size(); i++) {
  219. RID submenu_rid = popups[i]->bind_global_menu();
  220. if (!popups[i]->is_system_menu()) {
  221. int index = nmenu->add_submenu_item(main_menu, menu_cache[i].name, submenu_rid, global_menu_tag + "#" + itos(i), global_start_idx + i);
  222. menu_cache.write[i].submenu_rid = submenu_rid;
  223. nmenu->set_item_hidden(main_menu, index, menu_cache[i].hidden);
  224. nmenu->set_item_disabled(main_menu, index, menu_cache[i].disabled);
  225. nmenu->set_item_tooltip(main_menu, index, menu_cache[i].tooltip);
  226. } else {
  227. menu_cache.write[i].submenu_rid = RID();
  228. }
  229. }
  230. }
  231. void MenuBar::unbind_global_menu() {
  232. if (global_menu_tag.is_empty()) {
  233. return;
  234. }
  235. NativeMenu *nmenu = NativeMenu::get_singleton();
  236. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  237. Vector<PopupMenu *> popups = _get_popups();
  238. for (int i = menu_cache.size() - 1; i >= 0; i--) {
  239. if (!popups[i]->is_system_menu()) {
  240. if (menu_cache[i].submenu_rid.is_valid()) {
  241. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[i].submenu_rid);
  242. if (item_idx >= 0) {
  243. nmenu->remove_item(main_menu, item_idx);
  244. }
  245. }
  246. popups[i]->unbind_global_menu();
  247. menu_cache.write[i].submenu_rid = RID();
  248. }
  249. }
  250. global_menu_tag = String();
  251. }
  252. void MenuBar::_notification(int p_what) {
  253. switch (p_what) {
  254. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  255. RID ae = get_accessibility_element();
  256. ERR_FAIL_COND(ae.is_null());
  257. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_MENU_BAR);
  258. } break;
  259. case NOTIFICATION_ENTER_TREE: {
  260. if (get_menu_count() > 0) {
  261. _refresh_menu_names();
  262. }
  263. if (is_native_menu()) {
  264. bind_global_menu();
  265. }
  266. } break;
  267. case NOTIFICATION_EXIT_TREE: {
  268. unbind_global_menu();
  269. } break;
  270. case NOTIFICATION_MOUSE_EXIT: {
  271. focused_menu = -1;
  272. selected_menu = -1;
  273. queue_redraw();
  274. } break;
  275. case NOTIFICATION_TRANSLATION_CHANGED: {
  276. NativeMenu *nmenu = NativeMenu::get_singleton();
  277. bool is_global = !global_menu_tag.is_empty();
  278. RID main_menu = is_global ? nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID) : RID();
  279. for (int i = 0; i < menu_cache.size(); i++) {
  280. shape(menu_cache.write[i]);
  281. if (is_global && menu_cache[i].submenu_rid.is_valid()) {
  282. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[i].submenu_rid);
  283. if (item_idx >= 0) {
  284. nmenu->set_item_text(main_menu, item_idx, atr(menu_cache[i].name));
  285. }
  286. }
  287. }
  288. if (!is_global) {
  289. update_minimum_size();
  290. }
  291. } break;
  292. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  293. case NOTIFICATION_THEME_CHANGED: {
  294. for (int i = 0; i < menu_cache.size(); i++) {
  295. shape(menu_cache.write[i]);
  296. }
  297. if (global_menu_tag.is_empty()) {
  298. update_minimum_size();
  299. }
  300. } break;
  301. case NOTIFICATION_VISIBILITY_CHANGED: {
  302. if (is_native_menu()) {
  303. if (is_visible_in_tree()) {
  304. bind_global_menu();
  305. } else {
  306. unbind_global_menu();
  307. }
  308. }
  309. } break;
  310. case NOTIFICATION_DRAW: {
  311. if (is_native_menu()) {
  312. return;
  313. }
  314. for (int i = 0; i < menu_cache.size(); i++) {
  315. _draw_menu_item(i);
  316. }
  317. } break;
  318. case NOTIFICATION_INTERNAL_PROCESS: {
  319. MutexLock lock(mutex);
  320. if (is_native_menu()) {
  321. // Handled by OS.
  322. return;
  323. }
  324. Vector2 pos = get_local_mouse_position();
  325. if (pos == old_mouse_pos) {
  326. return;
  327. }
  328. old_mouse_pos = pos;
  329. int index = _get_index_at_point(pos);
  330. if (index >= 0 && index != active_menu) {
  331. selected_menu = index;
  332. focused_menu = selected_menu;
  333. if (active_menu >= 0) {
  334. get_menu_popup(active_menu)->hide();
  335. }
  336. _open_popup(index);
  337. }
  338. } break;
  339. }
  340. }
  341. int MenuBar::_get_index_at_point(const Point2 &p_point) const {
  342. Ref<StyleBox> style = theme_cache.normal;
  343. int offset = 0;
  344. Point2 point = p_point;
  345. if (is_layout_rtl()) {
  346. point.x = get_size().x - point.x;
  347. }
  348. for (int i = 0; i < menu_cache.size(); i++) {
  349. if (menu_cache[i].hidden) {
  350. continue;
  351. }
  352. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  353. if (point.x > offset && point.x < offset + size.x) {
  354. if (point.y > 0 && point.y < size.y) {
  355. return i;
  356. }
  357. }
  358. offset += size.x + theme_cache.h_separation;
  359. }
  360. return -1;
  361. }
  362. Rect2 MenuBar::_get_menu_item_rect(int p_index) const {
  363. ERR_FAIL_INDEX_V(p_index, menu_cache.size(), Rect2());
  364. Ref<StyleBox> style = theme_cache.normal;
  365. int offset = 0;
  366. for (int i = 0; i < p_index; i++) {
  367. if (menu_cache[i].hidden) {
  368. continue;
  369. }
  370. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  371. offset += size.x + theme_cache.h_separation;
  372. }
  373. Size2 size = menu_cache[p_index].text_buf->get_size() + style->get_minimum_size();
  374. if (is_layout_rtl()) {
  375. return Rect2(Point2(get_size().x - offset - size.x, 0), size);
  376. } else {
  377. return Rect2(Point2(offset, 0), size);
  378. }
  379. }
  380. void MenuBar::_draw_menu_item(int p_index) {
  381. ERR_FAIL_INDEX(p_index, menu_cache.size());
  382. RID ci = get_canvas_item();
  383. bool hovered = (focused_menu == p_index);
  384. bool pressed = (active_menu == p_index);
  385. bool rtl = is_layout_rtl();
  386. if (menu_cache[p_index].hidden) {
  387. return;
  388. }
  389. Color color;
  390. Ref<StyleBox> style;
  391. Rect2 item_rect = _get_menu_item_rect(p_index);
  392. if (menu_cache[p_index].disabled) {
  393. if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
  394. style = theme_cache.disabled_mirrored;
  395. } else {
  396. style = theme_cache.disabled;
  397. }
  398. if (!flat) {
  399. style->draw(ci, item_rect);
  400. }
  401. color = theme_cache.font_disabled_color;
  402. } else if (hovered && pressed && has_theme_stylebox("hover_pressed")) {
  403. if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
  404. style = theme_cache.hover_pressed_mirrored;
  405. } else {
  406. style = theme_cache.hover_pressed;
  407. }
  408. if (!flat) {
  409. style->draw(ci, item_rect);
  410. }
  411. if (has_theme_color(SNAME("font_hover_pressed_color"))) {
  412. color = theme_cache.font_hover_pressed_color;
  413. }
  414. } else if (pressed) {
  415. if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
  416. style = theme_cache.pressed_mirrored;
  417. } else {
  418. style = theme_cache.pressed;
  419. }
  420. if (!flat) {
  421. style->draw(ci, item_rect);
  422. }
  423. if (has_theme_color(SNAME("font_pressed_color"))) {
  424. color = theme_cache.font_pressed_color;
  425. } else {
  426. color = theme_cache.font_color;
  427. }
  428. } else if (hovered) {
  429. if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
  430. style = theme_cache.hover_mirrored;
  431. } else {
  432. style = theme_cache.hover;
  433. }
  434. if (!flat) {
  435. style->draw(ci, item_rect);
  436. }
  437. color = theme_cache.font_hover_color;
  438. } else {
  439. if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
  440. style = theme_cache.normal_mirrored;
  441. } else {
  442. style = theme_cache.normal;
  443. }
  444. if (!flat) {
  445. style->draw(ci, item_rect);
  446. }
  447. // Focus colors only take precedence over normal state.
  448. if (has_focus()) {
  449. color = theme_cache.font_focus_color;
  450. } else {
  451. color = theme_cache.font_color;
  452. }
  453. }
  454. Point2 text_ofs = item_rect.position + Point2(style->get_margin(SIDE_LEFT), style->get_margin(SIDE_TOP));
  455. Color font_outline_color = theme_cache.font_outline_color;
  456. int outline_size = theme_cache.outline_size;
  457. if (outline_size > 0 && font_outline_color.a > 0) {
  458. menu_cache[p_index].text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  459. }
  460. menu_cache[p_index].text_buf->draw(ci, text_ofs, color);
  461. }
  462. void MenuBar::shape(Menu &p_menu) {
  463. p_menu.text_buf->clear();
  464. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  465. p_menu.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  466. } else {
  467. p_menu.text_buf->set_direction((TextServer::Direction)text_direction);
  468. }
  469. p_menu.text_buf->add_string(atr(p_menu.name), theme_cache.font, theme_cache.font_size, language);
  470. }
  471. void MenuBar::_refresh_menu_names() {
  472. NativeMenu *nmenu = NativeMenu::get_singleton();
  473. bool is_global = !global_menu_tag.is_empty();
  474. RID main_menu = is_global ? nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID) : RID();
  475. bool dirty = false;
  476. Vector<PopupMenu *> popups = _get_popups();
  477. for (int i = 0; i < popups.size(); i++) {
  478. String menu_name = popups[i]->get_title().is_empty() ? String(popups[i]->get_name()) : popups[i]->get_title();
  479. if (!popups[i]->has_meta("_menu_name") && menu_name != get_menu_title(i)) {
  480. menu_cache.write[i].name = menu_name;
  481. shape(menu_cache.write[i]);
  482. dirty = true;
  483. if (is_global && menu_cache[i].submenu_rid.is_valid()) {
  484. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[i].submenu_rid);
  485. if (item_idx >= 0) {
  486. nmenu->set_item_text(main_menu, item_idx, atr(menu_cache[i].name));
  487. }
  488. }
  489. }
  490. }
  491. if (dirty && !is_global) {
  492. queue_redraw();
  493. update_minimum_size();
  494. }
  495. }
  496. Vector<PopupMenu *> MenuBar::_get_popups() const {
  497. Vector<PopupMenu *> popups;
  498. for (int i = 0; i < get_child_count(); i++) {
  499. PopupMenu *pm = Object::cast_to<PopupMenu>(get_child(i));
  500. if (!pm) {
  501. continue;
  502. }
  503. popups.push_back(pm);
  504. }
  505. return popups;
  506. }
  507. int MenuBar::get_menu_idx_from_control(PopupMenu *p_child) const {
  508. ERR_FAIL_NULL_V(p_child, -1);
  509. ERR_FAIL_COND_V(p_child->get_parent() != this, -1);
  510. Vector<PopupMenu *> popups = _get_popups();
  511. for (int i = 0; i < popups.size(); i++) {
  512. if (popups[i] == p_child) {
  513. return i;
  514. }
  515. }
  516. return -1;
  517. }
  518. void MenuBar::_popup_changed(ObjectID p_menu) {
  519. PopupMenu *pm = ObjectDB::get_instance<PopupMenu>(p_menu);
  520. if (!pm) {
  521. return;
  522. }
  523. int idx = get_menu_idx_from_control(pm);
  524. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  525. menu_name = String(pm->get_meta("_menu_name", menu_name));
  526. menu_cache.write[idx].name = menu_name;
  527. shape(menu_cache.write[idx]);
  528. update_minimum_size();
  529. queue_redraw();
  530. }
  531. void MenuBar::add_child_notify(Node *p_child) {
  532. Control::add_child_notify(p_child);
  533. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  534. if (!pm) {
  535. return;
  536. }
  537. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  538. Menu menu = Menu(menu_name);
  539. shape(menu);
  540. pm->connect("title_changed", callable_mp(this, &MenuBar::_popup_changed).bind(pm->get_instance_id()), CONNECT_REFERENCE_COUNTED);
  541. menu_cache.push_back(menu);
  542. p_child->connect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  543. p_child->connect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(true));
  544. p_child->connect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(false));
  545. if (!global_menu_tag.is_empty()) {
  546. NativeMenu *nmenu = NativeMenu::get_singleton();
  547. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  548. RID submenu_rid = pm->bind_global_menu();
  549. if (!pm->is_system_menu()) {
  550. nmenu->add_submenu_item(main_menu, atr(menu.name), submenu_rid, global_menu_tag + "#" + itos(menu_cache.size() - 1), _find_global_start_index() + menu_cache.size() - 1);
  551. menu_cache.write[menu_cache.size() - 1].submenu_rid = submenu_rid;
  552. }
  553. }
  554. update_minimum_size();
  555. }
  556. void MenuBar::move_child_notify(Node *p_child) {
  557. Control::move_child_notify(p_child);
  558. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  559. if (!pm) {
  560. return;
  561. }
  562. int old_idx = -1;
  563. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  564. menu_name = String(pm->get_meta("_menu_name", menu_name));
  565. // Find the previous menu index of the control.
  566. for (int i = 0; i < get_menu_count(); i++) {
  567. if (get_menu_title(i) == menu_name) {
  568. old_idx = i;
  569. break;
  570. }
  571. }
  572. Menu menu = menu_cache[old_idx];
  573. menu_cache.remove_at(old_idx);
  574. int new_idx = get_menu_idx_from_control(pm);
  575. menu_cache.insert(new_idx, menu);
  576. if (!global_menu_tag.is_empty()) {
  577. if (!pm->is_system_menu()) {
  578. NativeMenu *nmenu = NativeMenu::get_singleton();
  579. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  580. int global_start = _find_global_start_index();
  581. if (menu.submenu_rid.is_valid()) {
  582. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu.submenu_rid);
  583. if (item_idx >= 0) {
  584. nmenu->remove_item(main_menu, item_idx);
  585. }
  586. }
  587. if (new_idx != -1) {
  588. nmenu->add_submenu_item(main_menu, atr(menu.name), menu.submenu_rid, global_menu_tag + "#" + itos(new_idx), global_start + new_idx);
  589. }
  590. }
  591. }
  592. }
  593. void MenuBar::remove_child_notify(Node *p_child) {
  594. Control::remove_child_notify(p_child);
  595. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  596. if (!pm) {
  597. return;
  598. }
  599. int idx = get_menu_idx_from_control(pm);
  600. if (!global_menu_tag.is_empty()) {
  601. if (!pm->is_system_menu()) {
  602. if (menu_cache[idx].submenu_rid.is_valid()) {
  603. NativeMenu *nmenu = NativeMenu::get_singleton();
  604. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  605. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[idx].submenu_rid);
  606. if (item_idx >= 0) {
  607. nmenu->remove_item(main_menu, item_idx);
  608. }
  609. }
  610. pm->unbind_global_menu();
  611. }
  612. }
  613. pm->disconnect("title_changed", callable_mp(this, &MenuBar::_popup_changed));
  614. menu_cache.remove_at(idx);
  615. p_child->remove_meta("_menu_name");
  616. p_child->remove_meta("_menu_tooltip");
  617. p_child->disconnect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  618. p_child->disconnect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed));
  619. p_child->disconnect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed));
  620. update_minimum_size();
  621. }
  622. void MenuBar::_bind_methods() {
  623. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuBar::set_switch_on_hover);
  624. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuBar::is_switch_on_hover);
  625. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuBar::set_disable_shortcuts);
  626. ClassDB::bind_method(D_METHOD("set_prefer_global_menu", "enabled"), &MenuBar::set_prefer_global_menu);
  627. ClassDB::bind_method(D_METHOD("is_prefer_global_menu"), &MenuBar::is_prefer_global_menu);
  628. ClassDB::bind_method(D_METHOD("is_native_menu"), &MenuBar::is_native_menu);
  629. ClassDB::bind_method(D_METHOD("get_menu_count"), &MenuBar::get_menu_count);
  630. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &MenuBar::set_text_direction);
  631. ClassDB::bind_method(D_METHOD("get_text_direction"), &MenuBar::get_text_direction);
  632. ClassDB::bind_method(D_METHOD("set_language", "language"), &MenuBar::set_language);
  633. ClassDB::bind_method(D_METHOD("get_language"), &MenuBar::get_language);
  634. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &MenuBar::set_flat);
  635. ClassDB::bind_method(D_METHOD("is_flat"), &MenuBar::is_flat);
  636. ClassDB::bind_method(D_METHOD("set_start_index", "enabled"), &MenuBar::set_start_index);
  637. ClassDB::bind_method(D_METHOD("get_start_index"), &MenuBar::get_start_index);
  638. ClassDB::bind_method(D_METHOD("set_menu_title", "menu", "title"), &MenuBar::set_menu_title);
  639. ClassDB::bind_method(D_METHOD("get_menu_title", "menu"), &MenuBar::get_menu_title);
  640. ClassDB::bind_method(D_METHOD("set_menu_tooltip", "menu", "tooltip"), &MenuBar::set_menu_tooltip);
  641. ClassDB::bind_method(D_METHOD("get_menu_tooltip", "menu"), &MenuBar::get_menu_tooltip);
  642. ClassDB::bind_method(D_METHOD("set_menu_disabled", "menu", "disabled"), &MenuBar::set_menu_disabled);
  643. ClassDB::bind_method(D_METHOD("is_menu_disabled", "menu"), &MenuBar::is_menu_disabled);
  644. ClassDB::bind_method(D_METHOD("set_menu_hidden", "menu", "hidden"), &MenuBar::set_menu_hidden);
  645. ClassDB::bind_method(D_METHOD("is_menu_hidden", "menu"), &MenuBar::is_menu_hidden);
  646. ClassDB::bind_method(D_METHOD("get_menu_popup", "menu"), &MenuBar::get_menu_popup);
  647. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  648. ADD_PROPERTY(PropertyInfo(Variant::INT, "start_index"), "set_start_index", "get_start_index");
  649. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  650. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefer_global_menu"), "set_prefer_global_menu", "is_prefer_global_menu");
  651. ADD_GROUP("BiDi", "");
  652. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  653. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  654. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, normal);
  655. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, normal_mirrored);
  656. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, disabled);
  657. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, disabled_mirrored);
  658. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, pressed);
  659. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, pressed_mirrored);
  660. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover);
  661. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover_mirrored);
  662. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover_pressed);
  663. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover_pressed_mirrored);
  664. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, MenuBar, font);
  665. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, MenuBar, font_size);
  666. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MenuBar, outline_size);
  667. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_outline_color);
  668. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_color);
  669. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_disabled_color);
  670. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_pressed_color);
  671. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_hover_color);
  672. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_hover_pressed_color);
  673. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_focus_color);
  674. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MenuBar, h_separation);
  675. ADD_CLASS_DEPENDENCY("PopupMenu");
  676. }
  677. void MenuBar::set_switch_on_hover(bool p_enabled) {
  678. switch_on_hover = p_enabled;
  679. }
  680. bool MenuBar::is_switch_on_hover() {
  681. return switch_on_hover;
  682. }
  683. void MenuBar::set_disable_shortcuts(bool p_disabled) {
  684. disable_shortcuts = p_disabled;
  685. }
  686. void MenuBar::set_text_direction(Control::TextDirection p_text_direction) {
  687. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  688. if (text_direction != p_text_direction) {
  689. text_direction = p_text_direction;
  690. update_minimum_size();
  691. queue_redraw();
  692. }
  693. }
  694. Control::TextDirection MenuBar::get_text_direction() const {
  695. return text_direction;
  696. }
  697. void MenuBar::set_language(const String &p_language) {
  698. if (language != p_language) {
  699. language = p_language;
  700. update_minimum_size();
  701. queue_redraw();
  702. }
  703. }
  704. String MenuBar::get_language() const {
  705. return language;
  706. }
  707. void MenuBar::set_flat(bool p_enabled) {
  708. if (flat != p_enabled) {
  709. flat = p_enabled;
  710. queue_redraw();
  711. }
  712. }
  713. bool MenuBar::is_flat() const {
  714. return flat;
  715. }
  716. void MenuBar::set_start_index(int p_index) {
  717. if (start_index != p_index) {
  718. start_index = p_index;
  719. if (!global_menu_tag.is_empty()) {
  720. unbind_global_menu();
  721. bind_global_menu();
  722. }
  723. }
  724. }
  725. int MenuBar::get_start_index() const {
  726. return start_index;
  727. }
  728. void MenuBar::set_prefer_global_menu(bool p_enabled) {
  729. if (prefer_native != p_enabled) {
  730. prefer_native = p_enabled;
  731. if (prefer_native) {
  732. bind_global_menu();
  733. } else {
  734. unbind_global_menu();
  735. }
  736. }
  737. }
  738. bool MenuBar::is_prefer_global_menu() const {
  739. return prefer_native;
  740. }
  741. Size2 MenuBar::get_minimum_size() const {
  742. if (is_native_menu()) {
  743. return Size2();
  744. }
  745. Ref<StyleBox> style = theme_cache.normal;
  746. Vector2 size;
  747. for (int i = 0; i < menu_cache.size(); i++) {
  748. if (menu_cache[i].hidden) {
  749. continue;
  750. }
  751. Size2 sz = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  752. size.y = MAX(size.y, sz.y);
  753. size.x += sz.x;
  754. }
  755. if (menu_cache.size() > 1) {
  756. size.x += theme_cache.h_separation * (menu_cache.size() - 1);
  757. }
  758. return size;
  759. }
  760. int MenuBar::get_menu_count() const {
  761. return menu_cache.size();
  762. }
  763. void MenuBar::set_menu_title(int p_menu, const String &p_title) {
  764. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  765. PopupMenu *pm = get_menu_popup(p_menu);
  766. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  767. if (p_title == menu_name) {
  768. pm->remove_meta("_menu_name");
  769. } else {
  770. pm->set_meta("_menu_name", p_title);
  771. }
  772. menu_cache.write[p_menu].name = p_title;
  773. shape(menu_cache.write[p_menu]);
  774. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  775. NativeMenu *nmenu = NativeMenu::get_singleton();
  776. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  777. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  778. if (item_idx >= 0) {
  779. nmenu->set_item_text(main_menu, item_idx, atr(menu_cache[p_menu].name));
  780. }
  781. }
  782. update_minimum_size();
  783. }
  784. String MenuBar::get_menu_title(int p_menu) const {
  785. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  786. return menu_cache[p_menu].name;
  787. }
  788. void MenuBar::set_menu_tooltip(int p_menu, const String &p_tooltip) {
  789. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  790. PopupMenu *pm = get_menu_popup(p_menu);
  791. pm->set_meta("_menu_tooltip", p_tooltip);
  792. menu_cache.write[p_menu].tooltip = p_tooltip;
  793. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  794. NativeMenu *nmenu = NativeMenu::get_singleton();
  795. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  796. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  797. if (item_idx >= 0) {
  798. nmenu->set_item_tooltip(main_menu, item_idx, p_tooltip);
  799. }
  800. }
  801. }
  802. String MenuBar::get_menu_tooltip(int p_menu) const {
  803. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  804. return menu_cache[p_menu].tooltip;
  805. }
  806. void MenuBar::set_menu_disabled(int p_menu, bool p_disabled) {
  807. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  808. menu_cache.write[p_menu].disabled = p_disabled;
  809. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  810. NativeMenu *nmenu = NativeMenu::get_singleton();
  811. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  812. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  813. if (item_idx >= 0) {
  814. nmenu->set_item_disabled(main_menu, item_idx, p_disabled);
  815. }
  816. }
  817. }
  818. bool MenuBar::is_menu_disabled(int p_menu) const {
  819. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  820. return menu_cache[p_menu].disabled;
  821. }
  822. void MenuBar::set_menu_hidden(int p_menu, bool p_hidden) {
  823. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  824. menu_cache.write[p_menu].hidden = p_hidden;
  825. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  826. NativeMenu *nmenu = NativeMenu::get_singleton();
  827. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  828. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  829. if (item_idx >= 0) {
  830. nmenu->set_item_hidden(main_menu, item_idx, p_hidden);
  831. }
  832. }
  833. update_minimum_size();
  834. }
  835. bool MenuBar::is_menu_hidden(int p_menu) const {
  836. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  837. return menu_cache[p_menu].hidden;
  838. }
  839. PopupMenu *MenuBar::get_menu_popup(int p_idx) const {
  840. Vector<PopupMenu *> controls = _get_popups();
  841. if (p_idx >= 0 && p_idx < controls.size()) {
  842. return controls[p_idx];
  843. } else {
  844. return nullptr;
  845. }
  846. }
  847. String MenuBar::get_tooltip(const Point2 &p_pos) const {
  848. int index = _get_index_at_point(p_pos);
  849. if (index >= 0 && index < menu_cache.size()) {
  850. return menu_cache[index].tooltip;
  851. } else {
  852. return String();
  853. }
  854. }
  855. MenuBar::MenuBar() {
  856. set_focus_mode(FOCUS_ACCESSIBILITY);
  857. set_process_shortcut_input(true);
  858. }
  859. MenuBar::~MenuBar() {
  860. }