Debian Codegraph @

scene/gui/menu_bar.cpp

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