menu_bar.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. 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. Point2 screen_pos = get_screen_position() + item_rect.position * get_viewport()->get_canvas_transform().get_scale();
  113. Size2 screen_size = item_rect.size * get_viewport()->get_canvas_transform().get_scale();
  114. active_menu = p_index;
  115. pm->set_size(Size2(screen_size.x, 0));
  116. screen_pos.y += screen_size.y;
  117. if (is_layout_rtl()) {
  118. screen_pos.x += screen_size.x - pm->get_size().width;
  119. }
  120. pm->set_position(screen_pos);
  121. pm->set_parent_rect(Rect2(Point2(screen_pos - pm->get_position()), Size2(screen_size.x, screen_pos.y)));
  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() && !p_event->is_echo() && (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. Window *wnd = Object::cast_to<Window>(get_viewport());
  164. if (wnd) {
  165. mouse_pos_adjusted = wnd->get_position();
  166. if (wnd->is_embedded()) {
  167. Window *wnd_parent = Object::cast_to<Window>(wnd->get_parent()->get_viewport());
  168. while (wnd_parent) {
  169. if (!wnd_parent->is_embedded()) {
  170. mouse_pos_adjusted += wnd_parent->get_position();
  171. break;
  172. }
  173. wnd_parent = Object::cast_to<Window>(wnd_parent->get_parent()->get_viewport());
  174. }
  175. }
  176. set_process_internal(true);
  177. }
  178. }
  179. }
  180. void MenuBar::_update_submenu(const String &p_menu_name, PopupMenu *p_child) {
  181. int count = p_child->get_item_count();
  182. global_menus.insert(p_menu_name);
  183. for (int i = 0; i < count; i++) {
  184. if (p_child->is_item_separator(i)) {
  185. DisplayServer::get_singleton()->global_menu_add_separator(p_menu_name);
  186. } else if (!p_child->get_item_submenu(i).is_empty()) {
  187. Node *n = p_child->get_node(p_child->get_item_submenu(i));
  188. ERR_FAIL_COND_MSG(!n, "Item subnode does not exist: " + p_child->get_item_submenu(i) + ".");
  189. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  190. ERR_FAIL_COND_MSG(!pm, "Item subnode is not a PopupMenu: " + p_child->get_item_submenu(i) + ".");
  191. DisplayServer::get_singleton()->global_menu_add_submenu_item(p_menu_name, atr(p_child->get_item_text(i)), p_menu_name + "/" + itos(i));
  192. _update_submenu(p_menu_name + "/" + itos(i), pm);
  193. } else {
  194. int index = DisplayServer::get_singleton()->global_menu_add_item(p_menu_name, atr(p_child->get_item_text(i)), callable_mp(p_child, &PopupMenu::activate_item), Callable(), i);
  195. if (p_child->is_item_checkable(i)) {
  196. DisplayServer::get_singleton()->global_menu_set_item_checkable(p_menu_name, index, true);
  197. }
  198. if (p_child->is_item_radio_checkable(i)) {
  199. DisplayServer::get_singleton()->global_menu_set_item_radio_checkable(p_menu_name, index, true);
  200. }
  201. DisplayServer::get_singleton()->global_menu_set_item_checked(p_menu_name, index, p_child->is_item_checked(i));
  202. DisplayServer::get_singleton()->global_menu_set_item_disabled(p_menu_name, index, p_child->is_item_disabled(i));
  203. DisplayServer::get_singleton()->global_menu_set_item_max_states(p_menu_name, index, p_child->get_item_max_states(i));
  204. DisplayServer::get_singleton()->global_menu_set_item_icon(p_menu_name, index, p_child->get_item_icon(i));
  205. DisplayServer::get_singleton()->global_menu_set_item_state(p_menu_name, index, p_child->get_item_state(i));
  206. DisplayServer::get_singleton()->global_menu_set_item_indentation_level(p_menu_name, index, p_child->get_item_indent(i));
  207. DisplayServer::get_singleton()->global_menu_set_item_tooltip(p_menu_name, index, p_child->get_item_tooltip(i));
  208. if (!p_child->is_item_shortcut_disabled(i) && p_child->get_item_shortcut(i).is_valid() && p_child->get_item_shortcut(i)->has_valid_event()) {
  209. Array events = p_child->get_item_shortcut(i)->get_events();
  210. for (int j = 0; j < events.size(); j++) {
  211. Ref<InputEventKey> ie = events[j];
  212. if (ie.is_valid()) {
  213. DisplayServer::get_singleton()->global_menu_set_item_accelerator(p_menu_name, index, ie->get_keycode_with_modifiers());
  214. break;
  215. }
  216. }
  217. } else if (p_child->get_item_accelerator(i) != Key::NONE) {
  218. DisplayServer::get_singleton()->global_menu_set_item_accelerator(p_menu_name, i, p_child->get_item_accelerator(i));
  219. }
  220. }
  221. }
  222. }
  223. bool MenuBar::is_native_menu() const {
  224. if (Engine::get_singleton()->is_editor_hint() && is_inside_tree() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this)) {
  225. return false;
  226. }
  227. return (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU) && is_native);
  228. }
  229. void MenuBar::_clear_menu() {
  230. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU)) {
  231. return;
  232. }
  233. // Remove root menu items.
  234. int count = DisplayServer::get_singleton()->global_menu_get_item_count("_main");
  235. for (int i = count - 1; i >= 0; i--) {
  236. if (global_menus.has(DisplayServer::get_singleton()->global_menu_get_item_submenu("_main", i))) {
  237. DisplayServer::get_singleton()->global_menu_remove_item("_main", i);
  238. }
  239. }
  240. // Erase submenu contents.
  241. for (const String &E : global_menus) {
  242. DisplayServer::get_singleton()->global_menu_clear(E);
  243. }
  244. global_menus.clear();
  245. }
  246. void MenuBar::_update_menu() {
  247. _clear_menu();
  248. if (!is_visible_in_tree()) {
  249. return;
  250. }
  251. int index = start_index;
  252. if (is_native_menu()) {
  253. Vector<PopupMenu *> popups = _get_popups();
  254. String root_name = "MenuBar<" + String::num_int64((uint64_t)this, 16) + ">";
  255. for (int i = 0; i < popups.size(); i++) {
  256. if (menu_cache[i].hidden) {
  257. continue;
  258. }
  259. String menu_name = atr(String(popups[i]->get_meta("_menu_name", popups[i]->get_name())));
  260. index = DisplayServer::get_singleton()->global_menu_add_submenu_item("_main", menu_name, root_name + "/" + itos(i), index);
  261. if (menu_cache[i].disabled) {
  262. DisplayServer::get_singleton()->global_menu_set_item_disabled("_main", index, true);
  263. }
  264. _update_submenu(root_name + "/" + itos(i), popups[i]);
  265. index++;
  266. }
  267. }
  268. update_minimum_size();
  269. queue_redraw();
  270. }
  271. void MenuBar::_update_theme_item_cache() {
  272. Control::_update_theme_item_cache();
  273. theme_cache.normal = get_theme_stylebox(SNAME("normal"));
  274. theme_cache.normal_mirrored = get_theme_stylebox(SNAME("normal_mirrored"));
  275. theme_cache.disabled = get_theme_stylebox(SNAME("disabled"));
  276. theme_cache.disabled_mirrored = get_theme_stylebox(SNAME("disabled_mirrored"));
  277. theme_cache.pressed = get_theme_stylebox(SNAME("pressed"));
  278. theme_cache.pressed_mirrored = get_theme_stylebox(SNAME("pressed_mirrored"));
  279. theme_cache.hover = get_theme_stylebox(SNAME("hover"));
  280. theme_cache.hover_mirrored = get_theme_stylebox(SNAME("hover_mirrored"));
  281. theme_cache.hover_pressed = get_theme_stylebox(SNAME("hover_pressed"));
  282. theme_cache.hover_pressed_mirrored = get_theme_stylebox(SNAME("hover_pressed_mirrored"));
  283. theme_cache.font = get_theme_font(SNAME("font"));
  284. theme_cache.font_size = get_theme_font_size(SNAME("font_size"));
  285. theme_cache.outline_size = get_theme_constant(SNAME("outline_size"));
  286. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  287. theme_cache.font_color = get_theme_color(SNAME("font_color"));
  288. theme_cache.font_disabled_color = get_theme_color(SNAME("font_disabled_color"));
  289. theme_cache.font_pressed_color = get_theme_color(SNAME("font_pressed_color"));
  290. theme_cache.font_hover_color = get_theme_color(SNAME("font_hover_color"));
  291. theme_cache.font_hover_pressed_color = get_theme_color(SNAME("font_hover_pressed_color"));
  292. theme_cache.font_focus_color = get_theme_color(SNAME("font_focus_color"));
  293. theme_cache.h_separation = get_theme_constant(SNAME("h_separation"));
  294. }
  295. void MenuBar::_notification(int p_what) {
  296. switch (p_what) {
  297. case NOTIFICATION_ENTER_TREE: {
  298. if (get_menu_count() > 0) {
  299. _refresh_menu_names();
  300. }
  301. } break;
  302. case NOTIFICATION_EXIT_TREE: {
  303. _clear_menu();
  304. } break;
  305. case NOTIFICATION_MOUSE_EXIT: {
  306. focused_menu = -1;
  307. selected_menu = -1;
  308. queue_redraw();
  309. } break;
  310. case NOTIFICATION_TRANSLATION_CHANGED:
  311. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  312. case NOTIFICATION_THEME_CHANGED: {
  313. for (int i = 0; i < menu_cache.size(); i++) {
  314. shape(menu_cache.write[i]);
  315. }
  316. _update_menu();
  317. } break;
  318. case NOTIFICATION_VISIBILITY_CHANGED: {
  319. _update_menu();
  320. } break;
  321. case NOTIFICATION_DRAW: {
  322. if (is_native_menu()) {
  323. return;
  324. }
  325. for (int i = 0; i < menu_cache.size(); i++) {
  326. _draw_menu_item(i);
  327. }
  328. } break;
  329. case NOTIFICATION_INTERNAL_PROCESS: {
  330. MutexLock lock(mutex);
  331. if (is_native_menu()) {
  332. // Handled by OS.
  333. return;
  334. }
  335. Vector2 pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted - get_global_position();
  336. if (pos == old_mouse_pos) {
  337. return;
  338. }
  339. old_mouse_pos = pos;
  340. int index = _get_index_at_point(pos);
  341. if (index >= 0 && index != active_menu) {
  342. selected_menu = index;
  343. focused_menu = selected_menu;
  344. if (active_menu >= 0) {
  345. get_menu_popup(active_menu)->hide();
  346. }
  347. _open_popup(index);
  348. }
  349. } break;
  350. }
  351. }
  352. int MenuBar::_get_index_at_point(const Point2 &p_point) const {
  353. Ref<StyleBox> style = theme_cache.normal;
  354. int offset = 0;
  355. for (int i = 0; i < menu_cache.size(); i++) {
  356. if (menu_cache[i].hidden) {
  357. continue;
  358. }
  359. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  360. if (p_point.x > offset && p_point.x < offset + size.x) {
  361. if (p_point.y > 0 && p_point.y < size.y) {
  362. return i;
  363. }
  364. }
  365. offset += size.x + theme_cache.h_separation;
  366. }
  367. return -1;
  368. }
  369. Rect2 MenuBar::_get_menu_item_rect(int p_index) const {
  370. ERR_FAIL_INDEX_V(p_index, menu_cache.size(), Rect2());
  371. Ref<StyleBox> style = theme_cache.normal;
  372. int offset = 0;
  373. for (int i = 0; i < p_index; i++) {
  374. if (menu_cache[i].hidden) {
  375. continue;
  376. }
  377. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  378. offset += size.x + theme_cache.h_separation;
  379. }
  380. return Rect2(Point2(offset, 0), menu_cache[p_index].text_buf->get_size() + style->get_minimum_size());
  381. }
  382. void MenuBar::_draw_menu_item(int p_index) {
  383. ERR_FAIL_INDEX(p_index, menu_cache.size());
  384. RID ci = get_canvas_item();
  385. bool hovered = (focused_menu == p_index);
  386. bool pressed = (active_menu == p_index);
  387. bool rtl = is_layout_rtl();
  388. if (menu_cache[p_index].hidden) {
  389. return;
  390. }
  391. Color color;
  392. Ref<StyleBox> style = theme_cache.normal;
  393. Rect2 item_rect = _get_menu_item_rect(p_index);
  394. if (menu_cache[p_index].disabled) {
  395. if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
  396. style = theme_cache.disabled_mirrored;
  397. } else {
  398. style = theme_cache.disabled;
  399. }
  400. if (!flat) {
  401. style->draw(ci, item_rect);
  402. }
  403. color = theme_cache.font_disabled_color;
  404. } else if (hovered && pressed && has_theme_stylebox("hover_pressed")) {
  405. if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
  406. style = theme_cache.hover_pressed_mirrored;
  407. } else {
  408. style = theme_cache.hover_pressed;
  409. }
  410. if (!flat) {
  411. style->draw(ci, item_rect);
  412. }
  413. if (has_theme_color(SNAME("font_hover_pressed_color"))) {
  414. color = theme_cache.font_hover_pressed_color;
  415. }
  416. } else if (pressed) {
  417. if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
  418. style = theme_cache.pressed_mirrored;
  419. } else {
  420. style = theme_cache.pressed;
  421. }
  422. if (!flat) {
  423. style->draw(ci, item_rect);
  424. }
  425. if (has_theme_color(SNAME("font_pressed_color"))) {
  426. color = theme_cache.font_pressed_color;
  427. } else {
  428. color = theme_cache.font_color;
  429. }
  430. } else if (hovered) {
  431. if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
  432. style = theme_cache.hover_mirrored;
  433. } else {
  434. style = theme_cache.hover;
  435. }
  436. if (!flat) {
  437. style->draw(ci, item_rect);
  438. }
  439. color = theme_cache.font_hover_color;
  440. } else {
  441. if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
  442. style = theme_cache.normal_mirrored;
  443. } else {
  444. style = theme_cache.normal;
  445. }
  446. if (!flat) {
  447. style->draw(ci, item_rect);
  448. }
  449. // Focus colors only take precedence over normal state.
  450. if (has_focus()) {
  451. color = theme_cache.font_focus_color;
  452. } else {
  453. color = theme_cache.font_color;
  454. }
  455. }
  456. Point2 text_ofs = item_rect.position + Point2(style->get_margin(SIDE_LEFT), style->get_margin(SIDE_TOP));
  457. Color font_outline_color = theme_cache.font_outline_color;
  458. int outline_size = theme_cache.outline_size;
  459. if (outline_size > 0 && font_outline_color.a > 0) {
  460. menu_cache[p_index].text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  461. }
  462. menu_cache[p_index].text_buf->draw(ci, text_ofs, color);
  463. }
  464. void MenuBar::shape(Menu &p_menu) {
  465. p_menu.text_buf->clear();
  466. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  467. p_menu.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  468. } else {
  469. p_menu.text_buf->set_direction((TextServer::Direction)text_direction);
  470. }
  471. p_menu.text_buf->add_string(atr(p_menu.name), theme_cache.font, theme_cache.font_size, language);
  472. }
  473. void MenuBar::_refresh_menu_names() {
  474. Vector<PopupMenu *> popups = _get_popups();
  475. for (int i = 0; i < popups.size(); i++) {
  476. if (!popups[i]->has_meta("_menu_name") && String(popups[i]->get_name()) != get_menu_title(i)) {
  477. menu_cache.write[i].name = popups[i]->get_name();
  478. shape(menu_cache.write[i]);
  479. }
  480. }
  481. _update_menu();
  482. }
  483. Vector<PopupMenu *> MenuBar::_get_popups() const {
  484. Vector<PopupMenu *> popups;
  485. for (int i = 0; i < get_child_count(); i++) {
  486. PopupMenu *pm = Object::cast_to<PopupMenu>(get_child(i));
  487. if (!pm) {
  488. continue;
  489. }
  490. popups.push_back(pm);
  491. }
  492. return popups;
  493. }
  494. int MenuBar::get_menu_idx_from_control(PopupMenu *p_child) const {
  495. ERR_FAIL_NULL_V(p_child, -1);
  496. ERR_FAIL_COND_V(p_child->get_parent() != this, -1);
  497. Vector<PopupMenu *> popups = _get_popups();
  498. for (int i = 0; i < popups.size(); i++) {
  499. if (popups[i] == p_child) {
  500. return i;
  501. }
  502. }
  503. return -1;
  504. }
  505. void MenuBar::add_child_notify(Node *p_child) {
  506. Control::add_child_notify(p_child);
  507. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  508. if (!pm) {
  509. return;
  510. }
  511. Menu menu = Menu(p_child->get_name());
  512. shape(menu);
  513. menu_cache.push_back(menu);
  514. p_child->connect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  515. p_child->connect("menu_changed", callable_mp(this, &MenuBar::_update_menu));
  516. p_child->connect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(true));
  517. p_child->connect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(false));
  518. _update_menu();
  519. }
  520. void MenuBar::move_child_notify(Node *p_child) {
  521. Control::move_child_notify(p_child);
  522. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  523. if (!pm) {
  524. return;
  525. }
  526. int old_idx = -1;
  527. String menu_name = String(pm->get_meta("_menu_name", pm->get_name()));
  528. // Find the previous menu index of the control.
  529. for (int i = 0; i < get_menu_count(); i++) {
  530. if (get_menu_title(i) == menu_name) {
  531. old_idx = i;
  532. break;
  533. }
  534. }
  535. Menu menu = menu_cache[old_idx];
  536. menu_cache.remove_at(old_idx);
  537. menu_cache.insert(get_menu_idx_from_control(pm), menu);
  538. _update_menu();
  539. }
  540. void MenuBar::remove_child_notify(Node *p_child) {
  541. Control::remove_child_notify(p_child);
  542. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  543. if (!pm) {
  544. return;
  545. }
  546. int idx = get_menu_idx_from_control(pm);
  547. menu_cache.remove_at(idx);
  548. p_child->remove_meta("_menu_name");
  549. p_child->remove_meta("_menu_tooltip");
  550. p_child->disconnect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  551. p_child->disconnect("menu_changed", callable_mp(this, &MenuBar::_update_menu));
  552. p_child->disconnect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed));
  553. p_child->disconnect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed));
  554. _update_menu();
  555. }
  556. void MenuBar::_bind_methods() {
  557. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuBar::set_switch_on_hover);
  558. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuBar::is_switch_on_hover);
  559. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuBar::set_disable_shortcuts);
  560. ClassDB::bind_method(D_METHOD("set_prefer_global_menu", "enabled"), &MenuBar::set_prefer_global_menu);
  561. ClassDB::bind_method(D_METHOD("is_prefer_global_menu"), &MenuBar::is_prefer_global_menu);
  562. ClassDB::bind_method(D_METHOD("is_native_menu"), &MenuBar::is_native_menu);
  563. ClassDB::bind_method(D_METHOD("get_menu_count"), &MenuBar::get_menu_count);
  564. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &MenuBar::set_text_direction);
  565. ClassDB::bind_method(D_METHOD("get_text_direction"), &MenuBar::get_text_direction);
  566. ClassDB::bind_method(D_METHOD("set_language", "language"), &MenuBar::set_language);
  567. ClassDB::bind_method(D_METHOD("get_language"), &MenuBar::get_language);
  568. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &MenuBar::set_flat);
  569. ClassDB::bind_method(D_METHOD("is_flat"), &MenuBar::is_flat);
  570. ClassDB::bind_method(D_METHOD("set_start_index", "enabled"), &MenuBar::set_start_index);
  571. ClassDB::bind_method(D_METHOD("get_start_index"), &MenuBar::get_start_index);
  572. ClassDB::bind_method(D_METHOD("set_menu_title", "menu", "title"), &MenuBar::set_menu_title);
  573. ClassDB::bind_method(D_METHOD("get_menu_title", "menu"), &MenuBar::get_menu_title);
  574. ClassDB::bind_method(D_METHOD("set_menu_tooltip", "menu", "tooltip"), &MenuBar::set_menu_tooltip);
  575. ClassDB::bind_method(D_METHOD("get_menu_tooltip", "menu"), &MenuBar::get_menu_tooltip);
  576. ClassDB::bind_method(D_METHOD("set_menu_disabled", "menu", "disabled"), &MenuBar::set_menu_disabled);
  577. ClassDB::bind_method(D_METHOD("is_menu_disabled", "menu"), &MenuBar::is_menu_disabled);
  578. ClassDB::bind_method(D_METHOD("set_menu_hidden", "menu", "hidden"), &MenuBar::set_menu_hidden);
  579. ClassDB::bind_method(D_METHOD("is_menu_hidden", "menu"), &MenuBar::is_menu_hidden);
  580. ClassDB::bind_method(D_METHOD("get_menu_popup", "menu"), &MenuBar::get_menu_popup);
  581. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  582. ADD_PROPERTY(PropertyInfo(Variant::INT, "start_index"), "set_start_index", "get_start_index");
  583. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  584. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefer_global_menu"), "set_prefer_global_menu", "is_prefer_global_menu");
  585. ADD_GROUP("BiDi", "");
  586. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  587. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  588. }
  589. void MenuBar::set_switch_on_hover(bool p_enabled) {
  590. switch_on_hover = p_enabled;
  591. }
  592. bool MenuBar::is_switch_on_hover() {
  593. return switch_on_hover;
  594. }
  595. void MenuBar::set_disable_shortcuts(bool p_disabled) {
  596. disable_shortcuts = p_disabled;
  597. }
  598. void MenuBar::set_text_direction(Control::TextDirection p_text_direction) {
  599. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  600. if (text_direction != p_text_direction) {
  601. text_direction = p_text_direction;
  602. _update_menu();
  603. }
  604. }
  605. Control::TextDirection MenuBar::get_text_direction() const {
  606. return text_direction;
  607. }
  608. void MenuBar::set_language(const String &p_language) {
  609. if (language != p_language) {
  610. language = p_language;
  611. _update_menu();
  612. }
  613. }
  614. String MenuBar::get_language() const {
  615. return language;
  616. }
  617. void MenuBar::set_flat(bool p_enabled) {
  618. if (flat != p_enabled) {
  619. flat = p_enabled;
  620. queue_redraw();
  621. }
  622. }
  623. bool MenuBar::is_flat() const {
  624. return flat;
  625. }
  626. void MenuBar::set_start_index(int p_index) {
  627. if (start_index != p_index) {
  628. start_index = p_index;
  629. _update_menu();
  630. }
  631. }
  632. int MenuBar::get_start_index() const {
  633. return start_index;
  634. }
  635. void MenuBar::set_prefer_global_menu(bool p_enabled) {
  636. if (is_native != p_enabled) {
  637. if (is_native) {
  638. _clear_menu();
  639. }
  640. is_native = p_enabled;
  641. _update_menu();
  642. }
  643. }
  644. bool MenuBar::is_prefer_global_menu() const {
  645. return is_native;
  646. }
  647. Size2 MenuBar::get_minimum_size() const {
  648. if (is_native_menu()) {
  649. return Size2();
  650. }
  651. Ref<StyleBox> style = theme_cache.normal;
  652. Vector2 size;
  653. for (int i = 0; i < menu_cache.size(); i++) {
  654. if (menu_cache[i].hidden) {
  655. continue;
  656. }
  657. Size2 sz = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  658. size.y = MAX(size.y, sz.y);
  659. size.x += sz.x;
  660. }
  661. if (menu_cache.size() > 1) {
  662. size.x += theme_cache.h_separation * (menu_cache.size() - 1);
  663. }
  664. return size;
  665. }
  666. int MenuBar::get_menu_count() const {
  667. return menu_cache.size();
  668. }
  669. void MenuBar::set_menu_title(int p_menu, const String &p_title) {
  670. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  671. PopupMenu *pm = get_menu_popup(p_menu);
  672. if (p_title == pm->get_name()) {
  673. pm->remove_meta("_menu_name");
  674. } else {
  675. pm->set_meta("_menu_name", p_title);
  676. }
  677. menu_cache.write[p_menu].name = p_title;
  678. shape(menu_cache.write[p_menu]);
  679. _update_menu();
  680. }
  681. String MenuBar::get_menu_title(int p_menu) const {
  682. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  683. return menu_cache[p_menu].name;
  684. }
  685. void MenuBar::set_menu_tooltip(int p_menu, const String &p_tooltip) {
  686. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  687. PopupMenu *pm = get_menu_popup(p_menu);
  688. pm->set_meta("_menu_tooltip", p_tooltip);
  689. menu_cache.write[p_menu].name = p_tooltip;
  690. }
  691. String MenuBar::get_menu_tooltip(int p_menu) const {
  692. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  693. return menu_cache[p_menu].tooltip;
  694. }
  695. void MenuBar::set_menu_disabled(int p_menu, bool p_disabled) {
  696. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  697. menu_cache.write[p_menu].disabled = p_disabled;
  698. _update_menu();
  699. }
  700. bool MenuBar::is_menu_disabled(int p_menu) const {
  701. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  702. return menu_cache[p_menu].disabled;
  703. }
  704. void MenuBar::set_menu_hidden(int p_menu, bool p_hidden) {
  705. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  706. menu_cache.write[p_menu].hidden = p_hidden;
  707. _update_menu();
  708. }
  709. bool MenuBar::is_menu_hidden(int p_menu) const {
  710. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  711. return menu_cache[p_menu].hidden;
  712. }
  713. PopupMenu *MenuBar::get_menu_popup(int p_idx) const {
  714. Vector<PopupMenu *> controls = _get_popups();
  715. if (p_idx >= 0 && p_idx < controls.size()) {
  716. return controls[p_idx];
  717. } else {
  718. return nullptr;
  719. }
  720. }
  721. String MenuBar::get_tooltip(const Point2 &p_pos) const {
  722. int index = _get_index_at_point(p_pos);
  723. if (index >= 0 && index < menu_cache.size()) {
  724. return menu_cache[index].tooltip;
  725. } else {
  726. return String();
  727. }
  728. }
  729. MenuBar::MenuBar() {
  730. set_process_shortcut_input(true);
  731. }
  732. MenuBar::~MenuBar() {
  733. }