Debian Codegraph @

scene/gui/popup_menu.h

raw symbols SCIP
1 /**************************************************************************/
2 /* popup_menu.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 #pragma once
32
33 #include "core/input/shortcut.h"
34 #include "scene/gui/popup.h"
35 #include "scene/gui/scroll_container.h"
36 #include "scene/property_list_helper.h"
37 #include "scene/resources/text_line.h"
38
39 class PanelContainer;
40 class Timer;
41
42 class PopupMenu : public Popup {
43 GDCLASS(PopupMenu, Popup);
44
45 static HashMap<NativeMenu::SystemMenus, PopupMenu *> system_menus;
46
47 struct Item {
48 mutable RID accessibility_item_element;
49 mutable bool accessibility_item_dirty = true;
50
51 Ref<Texture2D> icon;
52 int icon_max_width = 0;
53 Color icon_modulate = Color(1, 1, 1, 1);
54 String text;
55 String xl_text;
56 Ref<TextLine> text_buf;
57 Ref<TextLine> accel_text_buf;
58
59 String language;
60 Control::TextDirection text_direction = Control::TEXT_DIRECTION_AUTO;
61 AutoTranslateMode auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT;
62
63 bool checked = false;
64 enum {
65 CHECKABLE_TYPE_NONE,
66 CHECKABLE_TYPE_CHECK_BOX,
67 CHECKABLE_TYPE_RADIO_BUTTON,
68 } checkable_type = CHECKABLE_TYPE_NONE;
69 int max_states = 0;
70 int state = 0;
71 bool separator = false;
72 bool disabled = false;
73 bool dirty = true;
74 int id = 0;
75 Variant metadata;
76 String submenu_name; // Compatibility.
77 PopupMenu *submenu = nullptr;
78 String tooltip;
79 Key accel = Key::NONE;
80 int _ofs_cache = 0;
81 int _height_cache = 0;
82 int indent = 0;
83 Ref<Shortcut> shortcut;
84 bool shortcut_is_global = false;
85 bool shortcut_is_disabled = false;
86 bool allow_echo = false;
87 bool submenu_bound = false;
88
89 // Returns (0,0) if icon is null.
90 Size2 get_icon_size() const {
91 return icon.is_null() ? Size2() : icon->get_size();
92 }
93
94 Item() {
95 text_buf.instantiate();
96 accel_text_buf.instantiate();
97 checkable_type = CHECKABLE_TYPE_NONE;
98 }
99
100 Item(bool p_dummy) {}
101 };
102 RID accessibility_scroll_element;
103
104 mutable Rect2i pre_popup_rect;
105 void _update_shadow_offsets() const;
106
107 static inline PropertyListHelper base_property_helper;
108 PropertyListHelper property_helper;
109
110 // To make Item available.
111 friend class OptionButton;
112 friend class MenuButton;
113
114 RID global_menu;
115 RID system_menu;
116 NativeMenu::SystemMenus system_menu_id = NativeMenu::INVALID_MENU_ID;
117 bool prefer_native = false;
118
119 bool activated_by_keyboard = false;
120
121 Timer *close_suspended_timer = nullptr;
122 bool close_was_suspended = false;
123 Timer *submenu_timer = nullptr;
124 List<Rect2> autohide_areas;
125 mutable Vector<Item> items;
126 BitField<MouseButtonMask> initial_button_mask = MouseButtonMask::NONE;
127 bool during_grabbed_click = false;
128 bool is_scrolling = false;
129 int mouse_over = -1;
130 int prev_mouse_over = -1;
131 int submenu_over = -1;
132 int this_submenu_index = -1; // Always -1 for the parent popup, and always a positive int for every open submenu.
133 int active_submenu_index = -1; // A positive int for the parent popup if any submenu is open.
134 bool is_active_submenu_left = false;
135 Vector<Point2> active_submenu_target_line;
136 Point2 last_submenu_mouse_position;
137 int submenu_mouse_exited_ticks_msec = -1;
138 bool mouse_movement_was_tested = false;
139 Point2 panel_offset_start;
140 float submenu_timer_popup_delay = 0.2;
141 const float CLOSE_SUSPENDED_TIMER_DELAY = 0.5;
142 String _get_accel_text(const Item &p_item) const;
143 int _get_mouse_over(const Point2 &p_over) const;
144 void _mouse_over_update(const Point2 &p_over);
145 virtual Size2 _get_contents_minimum_size() const override;
146
147 int _get_item_height(int p_idx) const;
148 int _get_items_total_height() const;
149 Size2 _get_item_icon_size(int p_idx) const;
150
151 void _shape_item(int p_idx) const;
152
153 void _accessibility_action_click(const Variant &p_data, int p_idx);
154
155 void _activate_submenu(int p_over, bool p_by_keyboard = false);
156 void _submenu_timeout();
157 bool _is_mouse_moving_toward_submenu(const Vector2 &p_relative, bool p_is_submenu_left, const Vector2 &p_mouse_position, const Vector<Point2> &p_active_submenu_target_line) const;
158 void _close_or_suspend();
159 void _close_suspended_timeout();
160
161 uint64_t popup_time_msec = 0;
162 bool hide_on_item_selection = true;
163 bool hide_on_checkable_item_selection = true;
164 bool hide_on_multistate_item_selection = false;
165 Vector2 moved;
166
167 HashMap<Ref<Shortcut>, int> shortcut_refcount;
168
169 void _ref_shortcut(Ref<Shortcut> p_sc);
170 void _unref_shortcut(Ref<Shortcut> p_sc);
171
172 void _shortcut_changed();
173
174 bool allow_search = true;
175 uint64_t search_time_msec = 0;
176 String search_string = "";
177
178 PanelContainer *panel = nullptr;
179 ScrollContainer *scroll_container = nullptr;
180 Control *control = nullptr;
181
182 const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
183 const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
184 float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
185 bool joypad_event_process = false;
186
187 struct ThemeCache {
188 Ref<StyleBox> panel_style;
189 Ref<StyleBox> hover_style;
190
191 Ref<StyleBox> separator_style;
192 Ref<StyleBox> labeled_separator_left;
193 Ref<StyleBox> labeled_separator_right;
194
195 int v_separation = 0;
196 int h_separation = 0;
197 int indent = 0;
198 int item_start_padding = 0;
199 int item_end_padding = 0;
200 int icon_max_width = 0;
201 int gutter_compact = 0;
202
203 Ref<Texture2D> checked;
204 Ref<Texture2D> checked_disabled;
205 Ref<Texture2D> unchecked;
206 Ref<Texture2D> unchecked_disabled;
207 Ref<Texture2D> radio_checked;
208 Ref<Texture2D> radio_checked_disabled;
209 Ref<Texture2D> radio_unchecked;
210 Ref<Texture2D> radio_unchecked_disabled;
211
212 Ref<Texture2D> submenu;
213 Ref<Texture2D> submenu_mirrored;
214
215 Ref<Font> font;
216 int font_size = 0;
217 Ref<Font> font_separator;
218 int font_separator_size = 0;
219
220 Color font_color;
221 Color font_hover_color;
222 Color font_disabled_color;
223 Color font_accelerator_color;
224 int font_outline_size = 0;
225 Color font_outline_color;
226
227 Color font_separator_color;
228 int font_separator_outline_size = 0;
229 Color font_separator_outline_color;
230 } theme_cache;
231
232 void _draw_items();
233
234 void _close_pressed();
235 void _menu_changed();
236 void _input_from_window_internal(const Ref<InputEvent> &p_event);
237 bool _set_item_accelerator(int p_index, const Ref<InputEventKey> &p_ie);
238 void _set_item_checkable_type(int p_index, int p_checkable_type);
239 int _get_item_checkable_type(int p_index) const;
240 void _native_popup(const Rect2i &p_rect);
241 String _atr(int p_idx, const String &p_text) const;
242 void _submenu_hidden();
243
244 bool shrink_height = true;
245 bool shrink_width = true;
246
247 protected:
248 virtual void _pre_popup() override;
249 virtual Rect2i _popup_adjust_rect() const override;
250
251 virtual void add_child_notify(Node *p_child) override;
252 virtual void remove_child_notify(Node *p_child) override;
253 virtual void _input_from_window(const Ref<InputEvent> &p_event) override;
254
255 virtual void _popup_base(const Rect2i &p_bounds = Rect2i()) override;
256 void _notification(int p_what);
257 bool _set(const StringName &p_name, const Variant &p_value);
258 bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
259 void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
260 bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
261 bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
262 static void _bind_methods();
263
264 #ifndef DISABLE_DEPRECATED
265 void _add_shortcut_bind_compat_36493(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
266 void _add_icon_shortcut_bind_compat_36493(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
267 void _clear_bind_compat_79965();
268
269 void _set_system_menu_root_compat_87452(const String &p_special);
270 String _get_system_menu_root_compat_87452() const;
271
272 static void _bind_compatibility_methods();
273 #endif
274
275 public:
276 // ATTENTION: This is used by the POT generator's scene parser. If the number of properties returned by `_get_items()` ever changes,
277 // this value should be updated to reflect the new size.
278 static const int ITEM_PROPERTY_SIZE = 10;
279
280 virtual RID get_focused_accessibility_element() const override;
281
282 virtual void _parent_focused() override;
283
284 RID bind_global_menu();
285 void unbind_global_menu();
286 bool is_system_menu() const;
287
288 void set_system_menu(NativeMenu::SystemMenus p_system_menu_id);
289 NativeMenu::SystemMenus get_system_menu() const;
290
291 void add_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
292 void add_icon_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
293 void add_check_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
294 void add_icon_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
295 void add_radio_check_item(const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
296 void add_icon_radio_check_item(const Ref<Texture2D> &p_icon, const String &p_label, int p_id = -1, Key p_accel = Key::NONE);
297
298 void add_multistate_item(const String &p_label, int p_max_states, int p_default_state = 0, int p_id = -1, Key p_accel = Key::NONE);
299
300 void add_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false, bool p_allow_echo = false);
301 void add_icon_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false, bool p_allow_echo = false);
302 void add_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
303 void add_icon_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
304 void add_radio_check_shortcut(const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
305 void add_icon_radio_check_shortcut(const Ref<Texture2D> &p_icon, const Ref<Shortcut> &p_shortcut, int p_id = -1, bool p_global = false);
306
307 void add_submenu_item(const String &p_label, const String &p_submenu, int p_id = -1);
308 void add_submenu_node_item(const String &p_label, PopupMenu *p_submenu, int p_id = -1);
309
310 void set_item_text(int p_idx, const String &p_text);
311
312 void set_item_text_direction(int p_idx, Control::TextDirection p_text_direction);
313 void set_item_language(int p_idx, const String &p_language);
314 void set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode);
315 void set_item_icon(int p_idx, const Ref<Texture2D> &p_icon);
316 void set_item_icon_max_width(int p_idx, int p_width);
317 void set_item_icon_modulate(int p_idx, const Color &p_modulate);
318 void set_item_checked(int p_idx, bool p_checked);
319 void set_item_id(int p_idx, int p_id);
320 void set_item_accelerator(int p_idx, Key p_accel);
321 void set_item_metadata(int p_idx, const Variant &p_meta);
322 void set_item_disabled(int p_idx, bool p_disabled);
323 void set_item_submenu(int p_idx, const String &p_submenu);
324 void set_item_submenu_node(int p_idx, PopupMenu *p_submenu);
325 void set_item_as_separator(int p_idx, bool p_separator);
326 void set_item_as_checkable(int p_idx, bool p_checkable);
327 void set_item_as_radio_checkable(int p_idx, bool p_radio_checkable);
328 void set_item_tooltip(int p_idx, const String &p_tooltip);
329 void set_item_shortcut(int p_idx, const Ref<Shortcut> &p_shortcut, bool p_global = false);
330 void set_item_indent(int p_idx, int p_indent);
331 void set_item_max_states(int p_idx, int p_max_states);
332 void set_item_multistate(int p_idx, int p_state);
333 void toggle_item_multistate(int p_idx);
334 void set_item_shortcut_disabled(int p_idx, bool p_disabled);
335
336 void toggle_item_checked(int p_idx);
337
338 String get_item_text(int p_idx) const;
339 String get_item_xl_text(int p_idx) const;
340 Control::TextDirection get_item_text_direction(int p_idx) const;
341 String get_item_language(int p_idx) const;
342 AutoTranslateMode get_item_auto_translate_mode(int p_idx) const;
343 int get_item_idx_from_text(const String &text) const;
344 Ref<Texture2D> get_item_icon(int p_idx) const;
345 int get_item_icon_max_width(int p_idx) const;
346 Color get_item_icon_modulate(int p_idx) const;
347 bool is_item_checked(int p_idx) const;
348 int get_item_id(int p_idx) const;
349 int get_item_index(int p_id) const;
350 Key get_item_accelerator(int p_idx) const;
351 Variant get_item_metadata(int p_idx) const;
352 bool is_item_disabled(int p_idx) const;
353 String get_item_submenu(int p_idx) const;
354 PopupMenu *get_item_submenu_node(int p_idx) const;
355 bool is_item_separator(int p_idx) const;
356 bool is_item_checkable(int p_idx) const;
357 bool is_item_radio_checkable(int p_idx) const;
358 bool is_item_shortcut_disabled(int p_idx) const;
359 bool is_item_shortcut_global(int p_idx) const;
360 String get_item_tooltip(int p_idx) const;
361 Ref<Shortcut> get_item_shortcut(int p_idx) const;
362 int get_item_indent(int p_idx) const;
363 int get_item_max_states(int p_idx) const;
364 int get_item_state(int p_idx) const;
365
366 void set_focused_item(int p_idx);
367 int get_focused_item() const;
368
369 void set_item_count(int p_count);
370 int get_item_count() const;
371
372 void set_prefer_native_menu(bool p_enabled);
373 bool is_prefer_native_menu() const;
374
375 bool is_native_menu() const;
376
377 void scroll_to_item(int p_idx);
378
379 bool activate_item_by_event(const Ref<InputEvent> &p_event, bool p_for_global_only = false);
380 void activate_item(int p_idx);
381
382 void _about_to_popup();
383 void _about_to_close();
384
385 void remove_item(int p_idx);
386
387 void add_separator(const String &p_text = String(), int p_id = -1);
388
389 void clear(bool p_free_submenus = true);
390
391 virtual String get_tooltip(const Point2 &p_pos) const;
392
393 #ifdef TOOLS_ENABLED
394 PackedStringArray get_configuration_warnings() const override;
395 #endif
396
397 void add_autohide_area(const Rect2 &p_area);
398 void clear_autohide_areas();
399
400 void set_hide_on_item_selection(bool p_enabled);
401 bool is_hide_on_item_selection() const;
402
403 void set_hide_on_checkable_item_selection(bool p_enabled);
404 bool is_hide_on_checkable_item_selection() const;
405
406 void set_hide_on_multistate_item_selection(bool p_enabled);
407 bool is_hide_on_multistate_item_selection() const;
408
409 void set_submenu_popup_delay(float p_time);
410 float get_submenu_popup_delay() const;
411
412 void set_allow_search(bool p_allow);
413 bool get_allow_search() const;
414
415 void set_shrink_height(bool p_shrink);
416 bool get_shrink_height() const;
417
418 void set_shrink_width(bool p_shrink);
419 bool get_shrink_width() const;
420
421 virtual void set_visible(bool p_visible) override;
422
423 PopupMenu();
424 ~PopupMenu();
425 };