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