menu_bar.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*************************************************************************/
  2. /* menu_bar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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") && 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") && 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_current_index(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 (!_is_focus_owner_in_shortcut_context()) {
  136. return;
  137. }
  138. if (disable_shortcuts) {
  139. return;
  140. }
  141. 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))) {
  142. if (!get_parent() || !is_visible_in_tree()) {
  143. return;
  144. }
  145. Vector<PopupMenu *> popups = _get_popups();
  146. for (int i = 0; i < popups.size(); i++) {
  147. if (menu_cache[i].hidden || menu_cache[i].disabled) {
  148. continue;
  149. }
  150. if (popups[i]->activate_item_by_event(p_event, false)) {
  151. accept_event();
  152. return;
  153. }
  154. }
  155. }
  156. }
  157. void MenuBar::set_shortcut_context(Node *p_node) {
  158. if (p_node != nullptr) {
  159. shortcut_context = p_node->get_instance_id();
  160. } else {
  161. shortcut_context = ObjectID();
  162. }
  163. }
  164. Node *MenuBar::get_shortcut_context() const {
  165. Object *ctx_obj = ObjectDB::get_instance(shortcut_context);
  166. Node *ctx_node = Object::cast_to<Node>(ctx_obj);
  167. return ctx_node;
  168. }
  169. bool MenuBar::_is_focus_owner_in_shortcut_context() const {
  170. if (shortcut_context == ObjectID()) {
  171. // No context, therefore global - always "in" context.
  172. return true;
  173. }
  174. Node *ctx_node = get_shortcut_context();
  175. Control *vp_focus = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  176. // If the context is valid and the viewport focus is valid, check if the context is the focus or is a parent of it.
  177. return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus));
  178. }
  179. void MenuBar::_popup_visibility_changed(bool p_visible) {
  180. if (!p_visible) {
  181. active_menu = -1;
  182. focused_menu = -1;
  183. set_process_internal(false);
  184. queue_redraw();
  185. return;
  186. }
  187. if (switch_on_hover) {
  188. Window *window = Object::cast_to<Window>(get_viewport());
  189. if (window) {
  190. mouse_pos_adjusted = window->get_position();
  191. if (window->is_embedded()) {
  192. Window *window_parent = Object::cast_to<Window>(window->get_parent()->get_viewport());
  193. while (window_parent) {
  194. if (!window_parent->is_embedded()) {
  195. mouse_pos_adjusted += window_parent->get_position();
  196. break;
  197. }
  198. window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport());
  199. }
  200. }
  201. set_process_internal(true);
  202. }
  203. }
  204. }
  205. void MenuBar::_update_submenu(const String &p_menu_name, PopupMenu *p_child) {
  206. int count = p_child->get_item_count();
  207. global_menus.insert(p_menu_name);
  208. for (int i = 0; i < count; i++) {
  209. if (p_child->is_item_separator(i)) {
  210. DisplayServer::get_singleton()->global_menu_add_separator(p_menu_name);
  211. } else if (!p_child->get_item_submenu(i).is_empty()) {
  212. Node *n = p_child->get_node(p_child->get_item_submenu(i));
  213. ERR_FAIL_COND_MSG(!n, "Item subnode does not exist: " + p_child->get_item_submenu(i) + ".");
  214. PopupMenu *pm = Object::cast_to<PopupMenu>(n);
  215. ERR_FAIL_COND_MSG(!pm, "Item subnode is not a PopupMenu: " + p_child->get_item_submenu(i) + ".");
  216. DisplayServer::get_singleton()->global_menu_add_submenu_item(p_menu_name, p_child->get_item_text(i), p_menu_name + "/" + itos(i));
  217. _update_submenu(p_menu_name + "/" + itos(i), pm);
  218. } else {
  219. int index = DisplayServer::get_singleton()->global_menu_add_item(p_menu_name, p_child->get_item_text(i), callable_mp(p_child, &PopupMenu::activate_item), Callable(), i);
  220. if (p_child->is_item_checkable(i)) {
  221. DisplayServer::get_singleton()->global_menu_set_item_checkable(p_menu_name, index, true);
  222. }
  223. if (p_child->is_item_radio_checkable(i)) {
  224. DisplayServer::get_singleton()->global_menu_set_item_radio_checkable(p_menu_name, index, true);
  225. }
  226. DisplayServer::get_singleton()->global_menu_set_item_checked(p_menu_name, index, p_child->is_item_checked(i));
  227. DisplayServer::get_singleton()->global_menu_set_item_disabled(p_menu_name, index, p_child->is_item_disabled(i));
  228. DisplayServer::get_singleton()->global_menu_set_item_max_states(p_menu_name, index, p_child->get_item_max_states(i));
  229. DisplayServer::get_singleton()->global_menu_set_item_icon(p_menu_name, index, p_child->get_item_icon(i));
  230. DisplayServer::get_singleton()->global_menu_set_item_state(p_menu_name, index, p_child->get_item_state(i));
  231. DisplayServer::get_singleton()->global_menu_set_item_indentation_level(p_menu_name, index, p_child->get_item_indent(i));
  232. DisplayServer::get_singleton()->global_menu_set_item_tooltip(p_menu_name, index, p_child->get_item_tooltip(i));
  233. 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()) {
  234. Array events = p_child->get_item_shortcut(i)->get_events();
  235. for (int j = 0; j < events.size(); j++) {
  236. Ref<InputEventKey> ie = events[j];
  237. if (ie.is_valid()) {
  238. DisplayServer::get_singleton()->global_menu_set_item_accelerator(p_menu_name, index, ie->get_keycode_with_modifiers());
  239. break;
  240. }
  241. }
  242. } else if (p_child->get_item_accelerator(i) != Key::NONE) {
  243. DisplayServer::get_singleton()->global_menu_set_item_accelerator(p_menu_name, i, p_child->get_item_accelerator(i));
  244. }
  245. }
  246. }
  247. }
  248. bool MenuBar::is_native_menu() const {
  249. 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)) {
  250. return false;
  251. }
  252. return (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU) && is_native);
  253. }
  254. void MenuBar::_clear_menu() {
  255. if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU)) {
  256. return;
  257. }
  258. // Remove root menu items.
  259. int count = DisplayServer::get_singleton()->global_menu_get_item_count("_main");
  260. for (int i = count - 1; i >= 0; i--) {
  261. if (global_menus.has(DisplayServer::get_singleton()->global_menu_get_item_submenu("_main", i))) {
  262. DisplayServer::get_singleton()->global_menu_remove_item("_main", i);
  263. }
  264. }
  265. // Erase submenu contents.
  266. for (const String &E : global_menus) {
  267. DisplayServer::get_singleton()->global_menu_clear(E);
  268. }
  269. global_menus.clear();
  270. }
  271. void MenuBar::_update_menu() {
  272. _clear_menu();
  273. if (!is_visible_in_tree()) {
  274. return;
  275. }
  276. int index = start_index;
  277. if (is_native_menu()) {
  278. Vector<PopupMenu *> popups = _get_popups();
  279. String root_name = "MenuBar<" + String::num_int64((uint64_t)this, 16) + ">";
  280. for (int i = 0; i < popups.size(); i++) {
  281. if (menu_cache[i].hidden) {
  282. continue;
  283. }
  284. String menu_name = String(popups[i]->get_meta("_menu_name", popups[i]->get_name()));
  285. index = DisplayServer::get_singleton()->global_menu_add_submenu_item("_main", menu_name, root_name + "/" + itos(i), index);
  286. if (menu_cache[i].disabled) {
  287. DisplayServer::get_singleton()->global_menu_set_item_disabled("_main", index, true);
  288. }
  289. _update_submenu(root_name + "/" + itos(i), popups[i]);
  290. index++;
  291. }
  292. }
  293. update_minimum_size();
  294. queue_redraw();
  295. }
  296. void MenuBar::_notification(int p_what) {
  297. switch (p_what) {
  298. case NOTIFICATION_ENTER_TREE: {
  299. if (get_menu_count() > 0) {
  300. _refresh_menu_names();
  301. }
  302. } break;
  303. case NOTIFICATION_EXIT_TREE: {
  304. _clear_menu();
  305. } break;
  306. case NOTIFICATION_MOUSE_EXIT: {
  307. focused_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 = get_theme_stylebox(SNAME("normal"));
  354. int hsep = get_theme_constant(SNAME("h_separation"));
  355. int offset = 0;
  356. for (int i = 0; i < menu_cache.size(); i++) {
  357. if (menu_cache[i].hidden) {
  358. continue;
  359. }
  360. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  361. if (p_point.x > offset && p_point.x < offset + size.x) {
  362. if (p_point.y > 0 && p_point.y < size.y) {
  363. return i;
  364. }
  365. }
  366. offset += size.x + hsep;
  367. }
  368. return -1;
  369. }
  370. Rect2 MenuBar::_get_menu_item_rect(int p_index) const {
  371. ERR_FAIL_INDEX_V(p_index, menu_cache.size(), Rect2());
  372. Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
  373. int hsep = get_theme_constant(SNAME("h_separation"));
  374. int offset = 0;
  375. for (int i = 0; i < p_index; i++) {
  376. if (menu_cache[i].hidden) {
  377. continue;
  378. }
  379. Size2 size = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  380. offset += size.x + hsep;
  381. }
  382. return Rect2(Point2(offset, 0), menu_cache[p_index].text_buf->get_size() + style->get_minimum_size());
  383. }
  384. void MenuBar::_draw_menu_item(int p_index) {
  385. ERR_FAIL_INDEX(p_index, menu_cache.size());
  386. RID ci = get_canvas_item();
  387. bool hovered = (focused_menu == p_index);
  388. bool pressed = (active_menu == p_index);
  389. bool rtl = is_layout_rtl();
  390. if (menu_cache[p_index].hidden) {
  391. return;
  392. }
  393. Color color;
  394. Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
  395. Rect2 item_rect = _get_menu_item_rect(p_index);
  396. if (menu_cache[p_index].disabled) {
  397. if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
  398. style = get_theme_stylebox(SNAME("disabled_mirrored"));
  399. } else {
  400. style = get_theme_stylebox(SNAME("disabled"));
  401. }
  402. if (!flat) {
  403. style->draw(ci, item_rect);
  404. }
  405. color = get_theme_color(SNAME("font_disabled_color"));
  406. } else if (hovered && pressed && has_theme_stylebox("hover_pressed")) {
  407. if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
  408. style = get_theme_stylebox(SNAME("hover_pressed_mirrored"));
  409. } else {
  410. style = get_theme_stylebox(SNAME("hover_pressed"));
  411. }
  412. if (!flat) {
  413. style->draw(ci, item_rect);
  414. }
  415. if (has_theme_color(SNAME("font_hover_pressed_color"))) {
  416. color = get_theme_color(SNAME("font_hover_pressed_color"));
  417. }
  418. } else if (pressed) {
  419. if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
  420. style = get_theme_stylebox(SNAME("pressed_mirrored"));
  421. } else {
  422. style = get_theme_stylebox(SNAME("pressed"));
  423. }
  424. if (!flat) {
  425. style->draw(ci, item_rect);
  426. }
  427. if (has_theme_color(SNAME("font_pressed_color"))) {
  428. color = get_theme_color(SNAME("font_pressed_color"));
  429. } else {
  430. color = get_theme_color(SNAME("font_color"));
  431. }
  432. } else if (hovered) {
  433. if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
  434. style = get_theme_stylebox(SNAME("hover_mirrored"));
  435. } else {
  436. style = get_theme_stylebox(SNAME("hover"));
  437. }
  438. if (!flat) {
  439. style->draw(ci, item_rect);
  440. }
  441. color = get_theme_color(SNAME("font_hover_color"));
  442. } else {
  443. if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
  444. style = get_theme_stylebox(SNAME("normal_mirrored"));
  445. } else {
  446. style = get_theme_stylebox(SNAME("normal"));
  447. }
  448. if (!flat) {
  449. style->draw(ci, item_rect);
  450. }
  451. // Focus colors only take precedence over normal state.
  452. if (has_focus()) {
  453. color = get_theme_color(SNAME("font_focus_color"));
  454. } else {
  455. color = get_theme_color(SNAME("font_color"));
  456. }
  457. }
  458. Point2 text_ofs = item_rect.position + Point2(style->get_margin(SIDE_LEFT), style->get_margin(SIDE_TOP));
  459. Color font_outline_color = get_theme_color(SNAME("font_outline_color"));
  460. int outline_size = get_theme_constant(SNAME("outline_size"));
  461. if (outline_size > 0 && font_outline_color.a > 0) {
  462. menu_cache[p_index].text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
  463. }
  464. menu_cache[p_index].text_buf->draw(ci, text_ofs, color);
  465. }
  466. void MenuBar::shape(Menu &p_menu) {
  467. Ref<Font> font = get_theme_font(SNAME("font"));
  468. int font_size = get_theme_font_size(SNAME("font_size"));
  469. p_menu.text_buf->clear();
  470. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  471. p_menu.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  472. } else {
  473. p_menu.text_buf->set_direction((TextServer::Direction)text_direction);
  474. }
  475. p_menu.text_buf->add_string(p_menu.name, font, font_size, language);
  476. }
  477. void MenuBar::_refresh_menu_names() {
  478. Vector<PopupMenu *> popups = _get_popups();
  479. for (int i = 0; i < popups.size(); i++) {
  480. if (!popups[i]->has_meta("_menu_name") && String(popups[i]->get_name()) != get_menu_title(i)) {
  481. menu_cache.write[i].name = popups[i]->get_name();
  482. shape(menu_cache.write[i]);
  483. }
  484. }
  485. _update_menu();
  486. }
  487. Vector<PopupMenu *> MenuBar::_get_popups() const {
  488. Vector<PopupMenu *> popups;
  489. for (int i = 0; i < get_child_count(); i++) {
  490. PopupMenu *pm = Object::cast_to<PopupMenu>(get_child(i));
  491. if (!pm) {
  492. continue;
  493. }
  494. popups.push_back(pm);
  495. }
  496. return popups;
  497. }
  498. int MenuBar::get_menu_idx_from_control(PopupMenu *p_child) const {
  499. ERR_FAIL_NULL_V(p_child, -1);
  500. ERR_FAIL_COND_V(p_child->get_parent() != this, -1);
  501. Vector<PopupMenu *> popups = _get_popups();
  502. for (int i = 0; i < popups.size(); i++) {
  503. if (popups[i] == p_child) {
  504. return i;
  505. }
  506. }
  507. return -1;
  508. }
  509. void MenuBar::add_child_notify(Node *p_child) {
  510. Control::add_child_notify(p_child);
  511. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  512. if (!pm) {
  513. return;
  514. }
  515. Menu menu = Menu(p_child->get_name());
  516. shape(menu);
  517. menu_cache.push_back(menu);
  518. p_child->connect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  519. p_child->connect("menu_changed", callable_mp(this, &MenuBar::_update_menu));
  520. p_child->connect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(true));
  521. p_child->connect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed).bind(false));
  522. _update_menu();
  523. }
  524. void MenuBar::move_child_notify(Node *p_child) {
  525. Control::move_child_notify(p_child);
  526. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  527. if (!pm) {
  528. return;
  529. }
  530. int old_idx = -1;
  531. String menu_name = String(pm->get_meta("_menu_name", pm->get_name()));
  532. // Find the previous menu index of the control.
  533. for (int i = 0; i < get_menu_count(); i++) {
  534. if (get_menu_title(i) == menu_name) {
  535. old_idx = i;
  536. break;
  537. }
  538. }
  539. Menu menu = menu_cache[old_idx];
  540. menu_cache.remove_at(old_idx);
  541. menu_cache.insert(get_menu_idx_from_control(pm), menu);
  542. _update_menu();
  543. }
  544. void MenuBar::remove_child_notify(Node *p_child) {
  545. Control::remove_child_notify(p_child);
  546. PopupMenu *pm = Object::cast_to<PopupMenu>(p_child);
  547. if (!pm) {
  548. return;
  549. }
  550. int idx = get_menu_idx_from_control(pm);
  551. menu_cache.remove_at(idx);
  552. p_child->remove_meta("_menu_name");
  553. p_child->remove_meta("_menu_tooltip");
  554. p_child->disconnect("renamed", callable_mp(this, &MenuBar::_refresh_menu_names));
  555. p_child->disconnect("menu_changed", callable_mp(this, &MenuBar::_update_menu));
  556. p_child->disconnect("about_to_popup", callable_mp(this, &MenuBar::_popup_visibility_changed));
  557. p_child->disconnect("popup_hide", callable_mp(this, &MenuBar::_popup_visibility_changed));
  558. _update_menu();
  559. }
  560. void MenuBar::_bind_methods() {
  561. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuBar::set_switch_on_hover);
  562. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuBar::is_switch_on_hover);
  563. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuBar::set_disable_shortcuts);
  564. ClassDB::bind_method(D_METHOD("set_prefer_global_menu", "enabled"), &MenuBar::set_prefer_global_menu);
  565. ClassDB::bind_method(D_METHOD("is_prefer_global_menu"), &MenuBar::is_prefer_global_menu);
  566. ClassDB::bind_method(D_METHOD("is_native_menu"), &MenuBar::is_native_menu);
  567. ClassDB::bind_method(D_METHOD("get_menu_count"), &MenuBar::get_menu_count);
  568. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &MenuBar::set_text_direction);
  569. ClassDB::bind_method(D_METHOD("get_text_direction"), &MenuBar::get_text_direction);
  570. ClassDB::bind_method(D_METHOD("set_language", "language"), &MenuBar::set_language);
  571. ClassDB::bind_method(D_METHOD("get_language"), &MenuBar::get_language);
  572. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &MenuBar::set_flat);
  573. ClassDB::bind_method(D_METHOD("is_flat"), &MenuBar::is_flat);
  574. ClassDB::bind_method(D_METHOD("set_start_index", "enabled"), &MenuBar::set_start_index);
  575. ClassDB::bind_method(D_METHOD("get_start_index"), &MenuBar::get_start_index);
  576. ClassDB::bind_method(D_METHOD("set_menu_title", "menu", "title"), &MenuBar::set_menu_title);
  577. ClassDB::bind_method(D_METHOD("get_menu_title", "menu"), &MenuBar::get_menu_title);
  578. ClassDB::bind_method(D_METHOD("set_menu_tooltip", "menu", "tooltip"), &MenuBar::set_menu_tooltip);
  579. ClassDB::bind_method(D_METHOD("get_menu_tooltip", "menu"), &MenuBar::get_menu_tooltip);
  580. ClassDB::bind_method(D_METHOD("set_menu_disabled", "menu", "disabled"), &MenuBar::set_menu_disabled);
  581. ClassDB::bind_method(D_METHOD("is_menu_disabled", "menu"), &MenuBar::is_menu_disabled);
  582. ClassDB::bind_method(D_METHOD("set_menu_hidden", "menu", "hidden"), &MenuBar::set_menu_hidden);
  583. ClassDB::bind_method(D_METHOD("is_menu_hidden", "menu"), &MenuBar::is_menu_hidden);
  584. ClassDB::bind_method(D_METHOD("set_shortcut_context", "node"), &MenuBar::set_shortcut_context);
  585. ClassDB::bind_method(D_METHOD("get_shortcut_context"), &MenuBar::get_shortcut_context);
  586. ClassDB::bind_method(D_METHOD("get_menu_popup", "menu"), &MenuBar::get_menu_popup);
  587. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  588. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "start_index"), "set_start_index", "get_start_index");
  589. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  590. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefer_global_menu"), "set_prefer_global_menu", "is_prefer_global_menu");
  591. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut_context", PROPERTY_HINT_RESOURCE_TYPE, "Node"), "set_shortcut_context", "get_shortcut_context");
  592. ADD_GROUP("BiDi", "");
  593. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  594. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  595. }
  596. void MenuBar::set_switch_on_hover(bool p_enabled) {
  597. switch_on_hover = p_enabled;
  598. }
  599. bool MenuBar::is_switch_on_hover() {
  600. return switch_on_hover;
  601. }
  602. void MenuBar::set_disable_shortcuts(bool p_disabled) {
  603. disable_shortcuts = p_disabled;
  604. }
  605. void MenuBar::set_text_direction(Control::TextDirection p_text_direction) {
  606. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  607. if (text_direction != p_text_direction) {
  608. text_direction = p_text_direction;
  609. _update_menu();
  610. }
  611. }
  612. Control::TextDirection MenuBar::get_text_direction() const {
  613. return text_direction;
  614. }
  615. void MenuBar::set_language(const String &p_language) {
  616. if (language != p_language) {
  617. language = p_language;
  618. _update_menu();
  619. }
  620. }
  621. String MenuBar::get_language() const {
  622. return language;
  623. }
  624. void MenuBar::set_flat(bool p_enabled) {
  625. if (flat != p_enabled) {
  626. flat = p_enabled;
  627. queue_redraw();
  628. }
  629. }
  630. bool MenuBar::is_flat() const {
  631. return flat;
  632. }
  633. void MenuBar::set_start_index(int p_index) {
  634. if (start_index != p_index) {
  635. start_index = p_index;
  636. _update_menu();
  637. }
  638. }
  639. int MenuBar::get_start_index() const {
  640. return start_index;
  641. }
  642. void MenuBar::set_prefer_global_menu(bool p_enabled) {
  643. if (is_native != p_enabled) {
  644. if (is_native) {
  645. _clear_menu();
  646. }
  647. is_native = p_enabled;
  648. _update_menu();
  649. }
  650. }
  651. bool MenuBar::is_prefer_global_menu() const {
  652. return is_native;
  653. }
  654. Size2 MenuBar::get_minimum_size() const {
  655. if (is_native_menu()) {
  656. return Size2();
  657. }
  658. Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
  659. Vector2 size;
  660. for (int i = 0; i < menu_cache.size(); i++) {
  661. if (menu_cache[i].hidden) {
  662. continue;
  663. }
  664. Size2 sz = menu_cache[i].text_buf->get_size() + style->get_minimum_size();
  665. size.y = MAX(size.y, sz.y);
  666. size.x += sz.x;
  667. }
  668. if (menu_cache.size() > 1) {
  669. size.x += get_theme_constant(SNAME("h_separation")) * (menu_cache.size() - 1);
  670. }
  671. return size;
  672. }
  673. int MenuBar::get_menu_count() const {
  674. return menu_cache.size();
  675. }
  676. void MenuBar::set_menu_title(int p_menu, const String &p_title) {
  677. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  678. PopupMenu *pm = get_menu_popup(p_menu);
  679. if (p_title == pm->get_name()) {
  680. pm->remove_meta("_menu_name");
  681. } else {
  682. pm->set_meta("_menu_name", p_title);
  683. }
  684. menu_cache.write[p_menu].name = p_title;
  685. shape(menu_cache.write[p_menu]);
  686. _update_menu();
  687. }
  688. String MenuBar::get_menu_title(int p_menu) const {
  689. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  690. return menu_cache[p_menu].name;
  691. }
  692. void MenuBar::set_menu_tooltip(int p_menu, const String &p_tooltip) {
  693. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  694. PopupMenu *pm = get_menu_popup(p_menu);
  695. pm->set_meta("_menu_tooltip", p_tooltip);
  696. menu_cache.write[p_menu].name = p_tooltip;
  697. }
  698. String MenuBar::get_menu_tooltip(int p_menu) const {
  699. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), String());
  700. return menu_cache[p_menu].tooltip;
  701. }
  702. void MenuBar::set_menu_disabled(int p_menu, bool p_disabled) {
  703. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  704. menu_cache.write[p_menu].disabled = p_disabled;
  705. _update_menu();
  706. }
  707. bool MenuBar::is_menu_disabled(int p_menu) const {
  708. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  709. return menu_cache[p_menu].disabled;
  710. }
  711. void MenuBar::set_menu_hidden(int p_menu, bool p_hidden) {
  712. ERR_FAIL_INDEX(p_menu, menu_cache.size());
  713. menu_cache.write[p_menu].hidden = p_hidden;
  714. _update_menu();
  715. }
  716. bool MenuBar::is_menu_hidden(int p_menu) const {
  717. ERR_FAIL_INDEX_V(p_menu, menu_cache.size(), false);
  718. return menu_cache[p_menu].hidden;
  719. }
  720. PopupMenu *MenuBar::get_menu_popup(int p_idx) const {
  721. Vector<PopupMenu *> controls = _get_popups();
  722. if (p_idx >= 0 && p_idx < controls.size()) {
  723. return controls[p_idx];
  724. } else {
  725. return nullptr;
  726. }
  727. }
  728. String MenuBar::get_tooltip(const Point2 &p_pos) const {
  729. int index = _get_index_at_point(p_pos);
  730. if (index >= 0 && index < menu_cache.size()) {
  731. return menu_cache[index].tooltip;
  732. } else {
  733. return String();
  734. }
  735. }
  736. void MenuBar::get_translatable_strings(List<String> *p_strings) const {
  737. Vector<PopupMenu *> popups = _get_popups();
  738. for (int i = 0; i < popups.size(); i++) {
  739. PopupMenu *pm = popups[i];
  740. if (!pm->has_meta("_menu_name") && !pm->has_meta("_menu_tooltip")) {
  741. continue;
  742. }
  743. String name = pm->get_meta("_menu_name");
  744. if (!name.is_empty()) {
  745. p_strings->push_back(name);
  746. }
  747. String tooltip = pm->get_meta("_menu_tooltip");
  748. if (!tooltip.is_empty()) {
  749. p_strings->push_back(tooltip);
  750. }
  751. }
  752. }
  753. MenuBar::MenuBar() {
  754. set_process_shortcut_input(true);
  755. }
  756. MenuBar::~MenuBar() {
  757. }