menu_bar.h 6.0 KB

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