1/**************************************************************************/
2/* menu_bar.h */
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#ifndef MENU_BAR_H
32#define MENU_BAR_H
33
34#include "scene/gui/button.h"
35#include "scene/gui/popup_menu.h"
36
37class MenuBar : public Control {
38 GDCLASS(MenuBar, Control);
39
40 Mutex mutex;
41
42 bool switch_on_hover = true;
43 bool disable_shortcuts = false;
44 bool is_native = true;
45 bool flat = false;
46 int start_index = -1;
47
48 String language;
49 TextDirection text_direction = TEXT_DIRECTION_AUTO;
50
51 struct Menu {
52 String name;
53 String tooltip;
54
55 Ref<TextLine> text_buf;
56 bool hidden = false;
57 bool disabled = false;
58
59 Menu(const String &p_name) {
60 name = p_name;
61 text_buf.instantiate();
62 }
63
64 Menu() {
65 text_buf.instantiate();
66 }
67 };
68 Vector<Menu> menu_cache;
69 HashSet<String> global_menus;
70
71 int focused_menu = -1;
72 int selected_menu = -1;
73 int active_menu = -1;
74
75 Vector2i mouse_pos_adjusted;
76 Vector2i old_mouse_pos;
77 ObjectID shortcut_context;
78
79 struct ThemeCache {
80 Ref<StyleBox> normal;
81 Ref<StyleBox> normal_mirrored;
82 Ref<StyleBox> disabled;
83 Ref<StyleBox> disabled_mirrored;
84 Ref<StyleBox> pressed;
85 Ref<StyleBox> pressed_mirrored;
86 Ref<StyleBox> hover;
87 Ref<StyleBox> hover_mirrored;
88 Ref<StyleBox> hover_pressed;
89 Ref<StyleBox> hover_pressed_mirrored;
90
91 Ref<Font> font;
92 int font_size = 0;
93 int outline_size = 0;
94 Color font_outline_color;
95
96 Color font_color;
97 Color font_disabled_color;
98 Color font_pressed_color;
99 Color font_hover_color;
100 Color font_hover_pressed_color;
101 Color font_focus_color;
102
103 int h_separation = 0;
104 } theme_cache;
105
106 int _get_index_at_point(const Point2 &p_point) const;
107 Rect2 _get_menu_item_rect(int p_index) const;
108 void _draw_menu_item(int p_index);
109
110 void shape(Menu &p_menu);
111 void _refresh_menu_names();
112 Vector<PopupMenu *> _get_popups() const;
113 int get_menu_idx_from_control(PopupMenu *p_child) const;
114
115 void _open_popup(int p_index, bool p_focus_item = false);
116 void _popup_visibility_changed(bool p_visible);
117 void _update_submenu(const String &p_menu_name, PopupMenu *p_child);
118 void _clear_menu();
119 void _update_menu();
120
121protected:
122 virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
123
124 void _notification(int p_what);
125 virtual void add_child_notify(Node *p_child) override;
126 virtual void move_child_notify(Node *p_child) override;
127 virtual void remove_child_notify(Node *p_child) override;
128 static void _bind_methods();
129
130public:
131 virtual void gui_input(const Ref<InputEvent> &p_event) override;
132
133 void set_switch_on_hover(bool p_enabled);
134 bool is_switch_on_hover();
135 void set_disable_shortcuts(bool p_disabled);
136
137 void set_prefer_global_menu(bool p_enabled);
138 bool is_prefer_global_menu() const;
139
140 bool is_native_menu() const;
141
142 virtual Size2 get_minimum_size() const override;
143
144 int get_menu_count() const;
145
146 void set_text_direction(TextDirection p_text_direction);
147 TextDirection get_text_direction() const;
148
149 void set_language(const String &p_language);
150 String get_language() const;
151
152 void set_start_index(int p_index);
153 int get_start_index() const;
154
155 void set_flat(bool p_enabled);
156 bool is_flat() const;
157
158 void set_menu_title(int p_menu, const String &p_title);
159 String get_menu_title(int p_menu) const;
160
161 void set_menu_tooltip(int p_menu, const String &p_tooltip);
162 String get_menu_tooltip(int p_menu) const;
163
164 void set_menu_disabled(int p_menu, bool p_disabled);
165 bool is_menu_disabled(int p_menu) const;
166
167 void set_menu_hidden(int p_menu, bool p_hidden);
168 bool is_menu_hidden(int p_menu) const;
169
170 PopupMenu *get_menu_popup(int p_menu) const;
171
172 virtual String get_tooltip(const Point2 &p_pos) const override;
173
174 MenuBar();
175 ~MenuBar();
176};
177
178#endif // MENU_BAR_H
179