popup_menu.h 15 KB

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