2
0

tab_bar.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**************************************************************************/
  2. /* tab_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. #pragma once
  31. #include "scene/gui/control.h"
  32. #include "scene/property_list_helper.h"
  33. #include "scene/resources/text_line.h"
  34. class TabBar : public Control {
  35. GDCLASS(TabBar, Control);
  36. public:
  37. enum AlignmentMode {
  38. ALIGNMENT_LEFT,
  39. ALIGNMENT_CENTER,
  40. ALIGNMENT_RIGHT,
  41. ALIGNMENT_MAX,
  42. };
  43. enum CloseButtonDisplayPolicy {
  44. CLOSE_BUTTON_SHOW_NEVER,
  45. CLOSE_BUTTON_SHOW_ACTIVE_ONLY,
  46. CLOSE_BUTTON_SHOW_ALWAYS,
  47. CLOSE_BUTTON_MAX
  48. };
  49. private:
  50. struct Tab {
  51. mutable RID accessibility_item_element;
  52. mutable bool accessibility_item_dirty = true;
  53. String text;
  54. String tooltip;
  55. String language;
  56. Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
  57. Ref<TextLine> text_buf;
  58. Ref<Texture2D> icon;
  59. int icon_max_width = 0;
  60. bool disabled = false;
  61. bool hidden = false;
  62. bool truncated = false;
  63. Variant metadata;
  64. int ofs_cache = 0;
  65. int size_cache = 0;
  66. int size_text = 0;
  67. Ref<Texture2D> right_button;
  68. Rect2 rb_rect;
  69. Rect2 cb_rect;
  70. Tab() {
  71. text_buf.instantiate();
  72. }
  73. Tab(bool p_dummy) {}
  74. };
  75. static inline PropertyListHelper base_property_helper;
  76. PropertyListHelper property_helper;
  77. int offset = 0;
  78. int max_drawn_tab = 0;
  79. int highlight_arrow = -1;
  80. bool buttons_visible = false;
  81. bool missing_right = false;
  82. Vector<Tab> tabs;
  83. int current = -1;
  84. int previous = -1;
  85. AlignmentMode tab_alignment = ALIGNMENT_LEFT;
  86. bool clip_tabs = true;
  87. int rb_hover = -1;
  88. bool rb_pressing = false;
  89. bool tab_style_v_flip = false;
  90. bool select_with_rmb = false;
  91. bool deselect_enabled = false;
  92. int cb_hover = -1;
  93. bool cb_pressing = false;
  94. CloseButtonDisplayPolicy cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
  95. bool close_with_middle_mouse = true;
  96. int hover = -1; // Hovered tab.
  97. int max_width = 0;
  98. bool scrolling_enabled = true;
  99. bool drag_to_rearrange_enabled = false;
  100. bool dragging_valid_tab = false;
  101. bool scroll_to_selected = true;
  102. int tabs_rearrange_group = -1;
  103. static const int CURRENT_TAB_UNINITIALIZED = -2;
  104. bool initialized = false;
  105. int queued_current = CURRENT_TAB_UNINITIALIZED;
  106. const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
  107. const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
  108. float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  109. struct ThemeCache {
  110. int h_separation = 0;
  111. int tab_separation = 0;
  112. int icon_max_width = 0;
  113. Ref<StyleBox> tab_unselected_style;
  114. Ref<StyleBox> tab_hovered_style;
  115. Ref<StyleBox> tab_selected_style;
  116. Ref<StyleBox> tab_disabled_style;
  117. Ref<StyleBox> tab_focus_style;
  118. Ref<Texture2D> increment_icon;
  119. Ref<Texture2D> increment_hl_icon;
  120. Ref<Texture2D> decrement_icon;
  121. Ref<Texture2D> decrement_hl_icon;
  122. Ref<Texture2D> drop_mark_icon;
  123. Color drop_mark_color;
  124. Ref<Font> font;
  125. int font_size;
  126. int outline_size = 0;
  127. Color font_selected_color;
  128. Color font_hovered_color;
  129. Color font_unselected_color;
  130. Color font_disabled_color;
  131. Color font_outline_color;
  132. Ref<Texture2D> close_icon;
  133. Ref<StyleBox> button_pressed_style;
  134. Ref<StyleBox> button_hl_style;
  135. } theme_cache;
  136. int get_tab_width(int p_idx) const;
  137. Size2 _get_tab_icon_size(int p_idx) const;
  138. void _ensure_no_over_offset();
  139. bool _can_deselect() const;
  140. void _update_hover();
  141. void _update_cache(bool p_update_hover = true);
  142. void _on_mouse_exited();
  143. void _shape(int p_tab);
  144. void _draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x, bool p_focus);
  145. void _accessibility_action_scroll_into_view(const Variant &p_data, int p_index);
  146. void _accessibility_action_focus(const Variant &p_data, int p_index);
  147. protected:
  148. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  149. virtual String get_tooltip(const Point2 &p_pos) const override;
  150. bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
  151. bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
  152. void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
  153. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  154. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  155. void _notification(int p_what);
  156. static void _bind_methods();
  157. Variant get_drag_data(const Point2 &p_point) override;
  158. bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  159. void drop_data(const Point2 &p_point, const Variant &p_data) override;
  160. void _move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
  161. public:
  162. RID get_tab_accessibility_element(int p_tab) const;
  163. virtual RID get_focused_accessibility_element() const override;
  164. Variant _handle_get_drag_data(const String &p_type, const Point2 &p_point);
  165. bool _handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const;
  166. void _handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback);
  167. void add_tab(const String &p_str = "", const Ref<Texture2D> &p_icon = Ref<Texture2D>());
  168. void set_tab_title(int p_tab, const String &p_title);
  169. String get_tab_title(int p_tab) const;
  170. void set_tab_tooltip(int p_tab, const String &p_tooltip);
  171. String get_tab_tooltip(int p_tab) const;
  172. void set_tab_text_direction(int p_tab, TextDirection p_text_direction);
  173. TextDirection get_tab_text_direction(int p_tab) const;
  174. void set_tab_language(int p_tab, const String &p_language);
  175. String get_tab_language(int p_tab) const;
  176. void set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon);
  177. Ref<Texture2D> get_tab_icon(int p_tab) const;
  178. void set_tab_icon_max_width(int p_tab, int p_width);
  179. int get_tab_icon_max_width(int p_tab) const;
  180. void set_tab_disabled(int p_tab, bool p_disabled);
  181. bool is_tab_disabled(int p_tab) const;
  182. void set_tab_hidden(int p_tab, bool p_hidden);
  183. bool is_tab_hidden(int p_tab) const;
  184. void set_tab_metadata(int p_tab, const Variant &p_metadata);
  185. Variant get_tab_metadata(int p_tab) const;
  186. void set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon);
  187. Ref<Texture2D> get_tab_button_icon(int p_tab) const;
  188. int get_tab_idx_at_point(const Point2 &p_point) const;
  189. int get_closest_tab_idx_to_point(const Point2 &p_point) const;
  190. void set_tab_alignment(AlignmentMode p_alignment);
  191. AlignmentMode get_tab_alignment() const;
  192. void set_clip_tabs(bool p_clip_tabs);
  193. bool get_clip_tabs() const;
  194. void set_tab_style_v_flip(bool p_tab_style_v_flip);
  195. void move_tab(int p_from, int p_to);
  196. void set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy);
  197. CloseButtonDisplayPolicy get_tab_close_display_policy() const;
  198. void set_close_with_middle_mouse(bool p_scroll_close);
  199. bool get_close_with_middle_mouse() const;
  200. void set_tab_count(int p_count);
  201. int get_tab_count() const;
  202. void set_current_tab(int p_current);
  203. int get_current_tab() const;
  204. int get_previous_tab() const;
  205. int get_hovered_tab() const;
  206. int get_previous_available(int p_idx = -1) const;
  207. int get_next_available(int p_idx = -1) const;
  208. bool select_previous_available();
  209. bool select_next_available();
  210. void set_tab_offset(int p_offset);
  211. int get_tab_offset() const;
  212. bool get_offset_buttons_visible() const;
  213. void remove_tab(int p_idx);
  214. void clear_tabs();
  215. void set_scrolling_enabled(bool p_enabled);
  216. bool get_scrolling_enabled() const;
  217. void set_drag_to_rearrange_enabled(bool p_enabled);
  218. bool get_drag_to_rearrange_enabled() const;
  219. void set_tabs_rearrange_group(int p_group_id);
  220. int get_tabs_rearrange_group() const;
  221. void set_scroll_to_selected(bool p_enabled);
  222. bool get_scroll_to_selected() const;
  223. void set_select_with_rmb(bool p_enabled);
  224. bool get_select_with_rmb() const;
  225. void set_deselect_enabled(bool p_enabled);
  226. bool get_deselect_enabled() const;
  227. void ensure_tab_visible(int p_idx);
  228. void set_max_tab_width(int p_width);
  229. int get_max_tab_width() const;
  230. Rect2 get_tab_rect(int p_tab) const;
  231. Size2 get_minimum_size() const override;
  232. TabBar();
  233. };
  234. VARIANT_ENUM_CAST(TabBar::AlignmentMode);
  235. VARIANT_ENUM_CAST(TabBar::CloseButtonDisplayPolicy);