menu_bar.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. } break;
  289. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  290. case NOTIFICATION_THEME_CHANGED: {
  291. for (int i = 0; i < menu_cache.size(); i++) {
  292. shape(menu_cache.write[i]);
  293. }
  294. } break;
  295. case NOTIFICATION_VISIBILITY_CHANGED: {
  296. if (is_native_menu()) {
  297. if (is_visible_in_tree()) {
  298. bind_global_menu();
  299. } else {
  300. unbind_global_menu();
  301. }
  302. }
  303. } break;
  304. case NOTIFICATION_DRAW: {
  305. if (is_native_menu()) {
  306. return;
  307. }
  308. for (int i = 0; i < menu_cache.size(); i++) {
  309. _draw_menu_item(i);
  310. }
  311. } break;
  312. case NOTIFICATION_INTERNAL_PROCESS: {
  313. MutexLock lock(mutex);
  314. if (is_native_menu()) {
  315. // Handled by OS.
  316. return;
  317. }
  318. Vector2 pos = get_local_mouse_position();
  319. if (pos == old_mouse_pos) {
  320. return;
  321. }
  322. old_mouse_pos = pos;
  323. int index = _get_index_at_point(pos);
  324. if (index >= 0 && index != active_menu) {
  325. selected_menu = index;
  326. focused_menu = selected_menu;
  327. if (active_menu >= 0) {
  328. get_menu_popup(active_menu)->hide();
  329. }
  330. _open_popup(index);
  331. }
  332. } break;
  333. }
  334. }
  335. int MenuBar::_get_index_at_point(const Point2 &p_point) const {
  336. Ref<StyleBox> style = theme_cache.normal;
  337. int offset = 0;
  338. Point2 point = p_point;
  339. if (is_layout_rtl()) {
  340. point.x = get_size().x - point.x;
  341. }
  342. for (int i = 0; i < menu_cache.size(); i++) {
  343. if (menu_cache[i].hidden) {
  344. continue;
  345. }
  346. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  347. if (point.x > offset && point.x < offset + size.x) {
  348. if (point.y > 0 && point.y < size.y) {
  349. return i;
  350. }
  351. }
  352. offset += size.x + theme_cache.h_separation;
  353. }
  354. return -1;
  355. }
  356. Rect2 MenuBar::_get_menu_item_rect(int p_index) const {
  357. ERR_FAIL_INDEX_V(p_index, menu_cache.size(), Rect2());
  358. Ref<StyleBox> style = theme_cache.normal;
  359. int offset = 0;
  360. for (int i = 0; i < p_index; i++) {
  361. if (menu_cache[i].hidden) {
  362. continue;
  363. }
  364. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  365. offset += size.x + theme_cache.h_separation;
  366. }
  367. Size2 size = menu_cache[p_index].text_buf->get_size() + style->get_minimum_size();
  368. if (is_layout_rtl()) {
  369. return Rect2(Point2(get_size().x - offset - size.x, 0), size);
  370. } else {
  371. return Rect2(Point2(offset, 0), size);
  372. }
  373. }
  374. void MenuBar::_draw_menu_item(int p_index) {
  375. ERR_FAIL_INDEX(p_index, menu_cache.size());
  376. RID ci = get_canvas_item();
  377. bool hovered = (focused_menu == p_index);
  378. bool pressed = (active_menu == p_index);
  379. bool rtl = is_layout_rtl();
  380. if (menu_cache[p_index].hidden) {
  381. return;
  382. }
  383. Color color;
  384. Ref<StyleBox> style;
  385. Rect2 item_rect = _get_menu_item_rect(p_index);
  386. if (menu_cache[p_index].disabled) {
  387. if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
  388. style = theme_cache.disabled_mirrored;
  389. } else {
  390. style = theme_cache.disabled;
  391. }
  392. if (!flat) {
  393. style->draw(ci, item_rect);
  394. }
  395. color = theme_cache.font_disabled_color;
  396. } else if (hovered && pressed && has_theme_stylebox("hover_pressed")) {
  397. if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
  398. style = theme_cache.hover_pressed_mirrored;
  399. } else {
  400. style = theme_cache.hover_pressed;
  401. }
  402. if (!flat) {
  403. style->draw(ci, item_rect);
  404. }
  405. if (has_theme_color(SNAME("font_hover_pressed_color"))) {
  406. color = theme_cache.font_hover_pressed_color;
  407. }
  408. } else if (pressed) {
  409. if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
  410. style = theme_cache.pressed_mirrored;
  411. } else {
  412. style = theme_cache.pressed;
  413. }
  414. if (!flat) {
  415. style->draw(ci, item_rect);
  416. }
  417. if (has_theme_color(SNAME("font_pressed_color"))) {
  418. color = theme_cache.font_pressed_color;
  419. } else {
  420. color = theme_cache.font_color;
  421. }
  422. } else if (hovered) {
  423. if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
  424. style = theme_cache.hover_mirrored;
  425. } else {
  426. style = theme_cache.hover;
  427. }
  428. if (!flat) {
  429. style->draw(ci, item_rect);
  430. }
  431. color = theme_cache.font_hover_color;
  432. } else {
  433. if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
  434. style = theme_cache.normal_mirrored;
  435. } else {
  436. style = theme_cache.normal;
  437. }
  438. if (!flat) {
  439. style->draw(ci, item_rect);
  440. }
  441. // Focus colors only take precedence over normal state.
  442. if (has_focus()) {
  443. color = theme_cache.font_focus_color;
  444. } else {
  445. color = theme_cache.font_color;
  446. }
  447. }
  448. Point2 text_ofs = item_rect.position + Point2(style->get_margin(SIDE_LEFT), style->get_margin(SIDE_TOP));
  449. Color font_outline_color = theme_cache.font_outline_color;
  450. int outline_size = theme_cache.outline_size;
  451. if (outline_size > 0 && font_outline_color.a > 0) {
  452. menu_cache[p_index].text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  453. }
  454. menu_cache[p_index].text_buf->draw(ci, text_ofs, color);
  455. }
  456. void MenuBar::shape(Menu &p_menu) {
  457. p_menu.text_buf->clear();
  458. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  459. p_menu.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  460. } else {
  461. p_menu.text_buf->set_direction((TextServer::Direction)text_direction);
  462. }
  463. p_menu.text_buf->add_string(atr(p_menu.name), theme_cache.font, theme_cache.font_size, language);
  464. }
  465. void MenuBar::_refresh_menu_names() {
  466. NativeMenu *nmenu = NativeMenu::get_singleton();
  467. bool is_global = !global_menu_tag.is_empty();
  468. RID main_menu = is_global ? nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID) : RID();
  469. Vector<PopupMenu *> popups = _get_popups();
  470. for (int i = 0; i < popups.size(); i++) {
  471. String menu_name = popups[i]->get_title().is_empty() ? String(popups[i]->get_name()) : popups[i]->get_title();
  472. if (!popups[i]->has_meta("_menu_name") && menu_name != get_menu_title(i)) {
  473. menu_cache.write[i].name = menu_name;
  474. shape(menu_cache.write[i]);
  475. queue_redraw();
  476. if (is_global && menu_cache[i].submenu_rid.is_valid()) {
  477. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[i].submenu_rid);
  478. if (item_idx >= 0) {
  479. nmenu->set_item_text(main_menu, item_idx, atr(menu_cache[i].name));
  480. }
  481. }
  482. }
  483. }
  484. }
  485. Vector<PopupMenu *> MenuBar::_get_popups() const {
  486. Vector<PopupMenu *> popups;
  487. for (int i = 0; i < get_child_count(); i++) {
  488. PopupMenu *pm = Object::cast_to<PopupMenu>(get_child(i));
  489. if (!pm) {
  490. continue;
  491. }
  492. popups.push_back(pm);
  493. }
  494. return popups;
  495. }
  496. int MenuBar::get_menu_idx_from_control(PopupMenu *p_child) const {
  497. ERR_FAIL_NULL_V(p_child, -1);
  498. ERR_FAIL_COND_V(p_child->get_parent() != this, -1);
  499. Vector<PopupMenu *> popups = _get_popups();
  500. for (int i = 0; i < popups.size(); i++) {
  501. if (popups[i] == p_child) {
  502. return i;
  503. }
  504. }
  505. return -1;
  506. }
  507. void MenuBar::_popup_changed(ObjectID p_menu) {
  508. PopupMenu *pm = ObjectDB::get_instance<PopupMenu>(p_menu);
  509. if (!pm) {
  510. return;
  511. }
  512. int idx = get_menu_idx_from_control(pm);
  513. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  514. menu_name = String(pm->get_meta("_menu_name", menu_name));
  515. menu_cache.write[idx].name = menu_name;
  516. shape(menu_cache.write[idx]);
  517. update_minimum_size();
  518. queue_redraw();
  519. }
  520. void MenuBar::add_child_notify(Node *p_child) {
  521. Control::add_child_notify(p_child);
  522. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  523. if (!pm) {
  524. return;
  525. }
  526. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  527. Menu menu = Menu(menu_name);
  528. shape(menu);
  529. pm->connect("title_changed", callable_mp(this, &MenuBar::_popup_changed).bind(pm->get_instance_id()), CONNECT_REFERENCE_COUNTED);
  530. menu_cache.push_back(menu);
  531. p_child->connect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  532. p_child->connect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(true));
  533. p_child->connect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(false));
  534. if (!global_menu_tag.is_empty()) {
  535. NativeMenu *nmenu = NativeMenu::get_singleton();
  536. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  537. RID submenu_rid = pm->bind_global_menu();
  538. if (!pm->is_system_menu()) {
  539. 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);
  540. menu_cache.write[menu_cache.size() - 1].submenu_rid = submenu_rid;
  541. }
  542. }
  543. update_minimum_size();
  544. }
  545. void MenuBar::move_child_notify(Node *p_child) {
  546. Control::move_child_notify(p_child);
  547. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  548. if (!pm) {
  549. return;
  550. }
  551. int old_idx = -1;
  552. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  553. menu_name = String(pm->get_meta("_menu_name", menu_name));
  554. // Find the previous menu index of the control.
  555. for (int i = 0; i < get_menu_count(); i++) {
  556. if (get_menu_title(i) == menu_name) {
  557. old_idx = i;
  558. break;
  559. }
  560. }
  561. Menu menu = menu_cache[old_idx];
  562. menu_cache.remove_at(old_idx);
  563. int new_idx = get_menu_idx_from_control(pm);
  564. menu_cache.insert(new_idx, menu);
  565. if (!global_menu_tag.is_empty()) {
  566. if (!pm->is_system_menu()) {
  567. NativeMenu *nmenu = NativeMenu::get_singleton();
  568. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  569. int global_start = _find_global_start_index();
  570. if (menu.submenu_rid.is_valid()) {
  571. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu.submenu_rid);
  572. if (item_idx >= 0) {
  573. nmenu->remove_item(main_menu, item_idx);
  574. }
  575. }
  576. if (new_idx != -1) {
  577. nmenu->add_submenu_item(main_menu, atr(menu.name), menu.submenu_rid, global_menu_tag + "#" + itos(new_idx), global_start + new_idx);
  578. }
  579. }
  580. }
  581. }
  582. void MenuBar::remove_child_notify(Node *p_child) {
  583. Control::remove_child_notify(p_child);
  584. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  585. if (!pm) {
  586. return;
  587. }
  588. int idx = get_menu_idx_from_control(pm);
  589. if (!global_menu_tag.is_empty()) {
  590. if (!pm->is_system_menu()) {
  591. if (menu_cache[idx].submenu_rid.is_valid()) {
  592. NativeMenu *nmenu = NativeMenu::get_singleton();
  593. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  594. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[idx].submenu_rid);
  595. if (item_idx >= 0) {
  596. nmenu->remove_item(main_menu, item_idx);
  597. }
  598. }
  599. pm->unbind_global_menu();
  600. }
  601. }
  602. pm->disconnect("title_changed", callable_mp(this, &MenuBar::_popup_changed));
  603. menu_cache.remove_at(idx);
  604. p_child->remove_meta("_menu_name");
  605. p_child->remove_meta("_menu_tooltip");
  606. p_child->disconnect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  607. p_child->disconnect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed));
  608. p_child->disconnect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed));
  609. update_minimum_size();
  610. }
  611. void MenuBar::_bind_methods() {
  612. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuBar::set_switch_on_hover);
  613. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuBar::is_switch_on_hover);
  614. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuBar::set_disable_shortcuts);
  615. ClassDB::bind_method(D_METHOD("set_prefer_global_menu", "enabled"), &MenuBar::set_prefer_global_menu);
  616. ClassDB::bind_method(D_METHOD("is_prefer_global_menu"), &MenuBar::is_prefer_global_menu);
  617. ClassDB::bind_method(D_METHOD("is_native_menu"), &MenuBar::is_native_menu);
  618. ClassDB::bind_method(D_METHOD("get_menu_count"), &MenuBar::get_menu_count);
  619. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &MenuBar::set_text_direction);
  620. ClassDB::bind_method(D_METHOD("get_text_direction"), &MenuBar::get_text_direction);
  621. ClassDB::bind_method(D_METHOD("set_language", "language"), &MenuBar::set_language);
  622. ClassDB::bind_method(D_METHOD("get_language"), &MenuBar::get_language);
  623. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &MenuBar::set_flat);
  624. ClassDB::bind_method(D_METHOD("is_flat"), &MenuBar::is_flat);
  625. ClassDB::bind_method(D_METHOD("set_start_index", "enabled"), &MenuBar::set_start_index);
  626. ClassDB::bind_method(D_METHOD("get_start_index"), &MenuBar::get_start_index);
  627. ClassDB::bind_method(D_METHOD("set_menu_title", "menu", "title"), &MenuBar::set_menu_title);
  628. ClassDB::bind_method(D_METHOD("get_menu_title", "menu"), &MenuBar::get_menu_title);
  629. ClassDB::bind_method(D_METHOD("set_menu_tooltip", "menu", "tooltip"), &MenuBar::set_menu_tooltip);
  630. ClassDB::bind_method(D_METHOD("get_menu_tooltip", "menu"), &MenuBar::get_menu_tooltip);
  631. ClassDB::bind_method(D_METHOD("set_menu_disabled", "menu", "disabled"), &MenuBar::set_menu_disabled);
  632. ClassDB::bind_method(D_METHOD("is_menu_disabled", "menu"), &MenuBar::is_menu_disabled);
  633. ClassDB::bind_method(D_METHOD("set_menu_hidden", "menu", "hidden"), &MenuBar::set_menu_hidden);
  634. ClassDB::bind_method(D_METHOD("is_menu_hidden", "menu"), &MenuBar::is_menu_hidden);
  635. ClassDB::bind_method(D_METHOD("get_menu_popup", "menu"), &MenuBar::get_menu_popup);
  636. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  637. ADD_PROPERTY(PropertyInfo(Variant::INT, "start_index"), "set_start_index", "get_start_index");
  638. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  639. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefer_global_menu"), "set_prefer_global_menu", "is_prefer_global_menu");
  640. ADD_GROUP("BiDi", "");
  641. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  642. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  643. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, normal);
  644. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, normal_mirrored);
  645. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, disabled);
  646. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, disabled_mirrored);
  647. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, pressed);
  648. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, pressed_mirrored);
  649. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover);
  650. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover_mirrored);
  651. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover_pressed);
  652. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, MenuBar, hover_pressed_mirrored);
  653. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, MenuBar, font);
  654. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, MenuBar, font_size);
  655. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MenuBar, outline_size);
  656. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_outline_color);
  657. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_color);
  658. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_disabled_color);
  659. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_pressed_color);
  660. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_hover_color);
  661. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_hover_pressed_color);
  662. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, MenuBar, font_focus_color);
  663. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, MenuBar, h_separation);
  664. }
  665. void MenuBar::set_switch_on_hover(bool p_enabled) {
  666. switch_on_hover = p_enabled;
  667. }
  668. bool MenuBar::is_switch_on_hover() {
  669. return switch_on_hover;
  670. }
  671. void MenuBar::set_disable_shortcuts(bool p_disabled) {
  672. disable_shortcuts = p_disabled;
  673. }
  674. void MenuBar::set_text_direction(Control::TextDirection p_text_direction) {
  675. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  676. if (text_direction != p_text_direction) {
  677. text_direction = p_text_direction;
  678. update_minimum_size();
  679. queue_redraw();
  680. }
  681. }
  682. Control::TextDirection MenuBar::get_text_direction() const {
  683. return text_direction;
  684. }
  685. void MenuBar::set_language(const String &p_language) {
  686. if (language != p_language) {
  687. language = p_language;
  688. update_minimum_size();
  689. queue_redraw();
  690. }
  691. }
  692. String MenuBar::get_language() const {
  693. return language;
  694. }
  695. void MenuBar::set_flat(bool p_enabled) {
  696. if (flat != p_enabled) {
  697. flat = p_enabled;
  698. queue_redraw();
  699. }
  700. }
  701. bool MenuBar::is_flat() const {
  702. return flat;
  703. }
  704. void MenuBar::set_start_index(int p_index) {
  705. if (start_index != p_index) {
  706. start_index = p_index;
  707. if (!global_menu_tag.is_empty()) {
  708. unbind_global_menu();
  709. bind_global_menu();
  710. }
  711. }
  712. }
  713. int MenuBar::get_start_index() const {
  714. return start_index;
  715. }
  716. void MenuBar::set_prefer_global_menu(bool p_enabled) {
  717. if (prefer_native != p_enabled) {
  718. prefer_native = p_enabled;
  719. if (prefer_native) {
  720. bind_global_menu();
  721. } else {
  722. unbind_global_menu();
  723. }
  724. }
  725. }
  726. bool MenuBar::is_prefer_global_menu() const {
  727. return prefer_native;
  728. }
  729. Size2 MenuBar::get_minimum_size() const {
  730. if (is_native_menu()) {
  731. return Size2();
  732. }
  733. Ref<StyleBox> style = theme_cache.normal;
  734. Vector2 size;
  735. for (int i = 0; i < menu_cache.size(); i++) {
  736. if (menu_cache[i].hidden) {
  737. continue;
  738. }
  739. Size2 sz = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  740. size.y = MAX(size.y, sz.y);
  741. size.x += sz.x;
  742. }
  743. if (menu_cache.size() > 1) {
  744. size.x += theme_cache.h_separation * (menu_cache.size() - 1);
  745. }
  746. return size;
  747. }
  748. int MenuBar::get_menu_count() const {
  749. return menu_cache.size();
  750. }
  751. void MenuBar::set_menu_title(int p_menu, const String &p_title) {
  752. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  753. PopupMenu *pm = get_menu_popup(p_menu);
  754. String menu_name = pm->get_title().is_empty() ? String(pm->get_name()) : pm->get_title();
  755. if (p_title == menu_name) {
  756. pm->remove_meta("_menu_name");
  757. } else {
  758. pm->set_meta("_menu_name", p_title);
  759. }
  760. menu_cache.write[p_menu].name = p_title;
  761. shape(menu_cache.write[p_menu]);
  762. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  763. NativeMenu *nmenu = NativeMenu::get_singleton();
  764. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  765. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  766. if (item_idx >= 0) {
  767. nmenu->set_item_text(main_menu, item_idx, atr(menu_cache[p_menu].name));
  768. }
  769. }
  770. update_minimum_size();
  771. }
  772. String MenuBar::get_menu_title(int p_menu) const {
  773. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  774. return menu_cache[p_menu].name;
  775. }
  776. void MenuBar::set_menu_tooltip(int p_menu, const String &p_tooltip) {
  777. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  778. PopupMenu *pm = get_menu_popup(p_menu);
  779. pm->set_meta("_menu_tooltip", p_tooltip);
  780. menu_cache.write[p_menu].tooltip = p_tooltip;
  781. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  782. NativeMenu *nmenu = NativeMenu::get_singleton();
  783. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  784. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  785. if (item_idx >= 0) {
  786. nmenu->set_item_tooltip(main_menu, item_idx, p_tooltip);
  787. }
  788. }
  789. }
  790. String MenuBar::get_menu_tooltip(int p_menu) const {
  791. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  792. return menu_cache[p_menu].tooltip;
  793. }
  794. void MenuBar::set_menu_disabled(int p_menu, bool p_disabled) {
  795. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  796. menu_cache.write[p_menu].disabled = p_disabled;
  797. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  798. NativeMenu *nmenu = NativeMenu::get_singleton();
  799. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  800. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  801. if (item_idx >= 0) {
  802. nmenu->set_item_disabled(main_menu, item_idx, p_disabled);
  803. }
  804. }
  805. }
  806. bool MenuBar::is_menu_disabled(int p_menu) const {
  807. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  808. return menu_cache[p_menu].disabled;
  809. }
  810. void MenuBar::set_menu_hidden(int p_menu, bool p_hidden) {
  811. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  812. menu_cache.write[p_menu].hidden = p_hidden;
  813. if (!global_menu_tag.is_empty() && menu_cache[p_menu].submenu_rid.is_valid()) {
  814. NativeMenu *nmenu = NativeMenu::get_singleton();
  815. RID main_menu = nmenu->get_system_menu(NativeMenu::MAIN_MENU_ID);
  816. int item_idx = nmenu->find_item_index_with_submenu(main_menu, menu_cache[p_menu].submenu_rid);
  817. if (item_idx >= 0) {
  818. nmenu->set_item_hidden(main_menu, item_idx, p_hidden);
  819. }
  820. }
  821. update_minimum_size();
  822. }
  823. bool MenuBar::is_menu_hidden(int p_menu) const {
  824. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  825. return menu_cache[p_menu].hidden;
  826. }
  827. PopupMenu *MenuBar::get_menu_popup(int p_idx) const {
  828. Vector<PopupMenu *> controls = _get_popups();
  829. if (p_idx >= 0 && p_idx < controls.size()) {
  830. return controls[p_idx];
  831. } else {
  832. return nullptr;
  833. }
  834. }
  835. String MenuBar::get_tooltip(const Point2 &p_pos) const {
  836. int index = _get_index_at_point(p_pos);
  837. if (index >= 0 && index < menu_cache.size()) {
  838. return menu_cache[index].tooltip;
  839. } else {
  840. return String();
  841. }
  842. }
  843. MenuBar::MenuBar() {
  844. set_focus_mode(FOCUS_ACCESSIBILITY);
  845. set_process_shortcut_input(true);
  846. }
  847. MenuBar::~MenuBar() {
  848. }