menu_bar.cpp 30 KB

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