menu_bar.cpp 29 KB

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