tab_container.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**************************************************************************/
  2. /* tab_container.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/container.h"
  32. #include "scene/gui/popup.h"
  33. #include "scene/gui/tab_bar.h"
  34. class TabContainer : public Container {
  35. GDCLASS(TabContainer, Container);
  36. public:
  37. enum TabPosition {
  38. POSITION_TOP,
  39. POSITION_BOTTOM,
  40. POSITION_MAX,
  41. };
  42. private:
  43. TabBar *tab_bar = nullptr;
  44. bool tabs_visible = true;
  45. bool all_tabs_in_front = false;
  46. TabPosition tabs_position = POSITION_TOP;
  47. bool menu_hovered = false;
  48. mutable ObjectID popup_obj_id;
  49. bool use_hidden_tabs_for_min_size = false;
  50. bool theme_changing = false;
  51. Vector<Control *> children_removing;
  52. bool drag_to_rearrange_enabled = false;
  53. // Set the default setup current tab to be an invalid index.
  54. int setup_current_tab = -2;
  55. bool updating_visibility = false;
  56. struct ThemeCache {
  57. int side_margin = 0;
  58. Ref<StyleBox> panel_style;
  59. Ref<StyleBox> tabbar_style;
  60. Ref<Texture2D> menu_icon;
  61. Ref<Texture2D> menu_hl_icon;
  62. // TabBar overrides.
  63. int icon_separation = 0;
  64. int tab_separation = 0;
  65. int icon_max_width = 0;
  66. int outline_size = 0;
  67. Ref<StyleBox> tab_unselected_style;
  68. Ref<StyleBox> tab_hovered_style;
  69. Ref<StyleBox> tab_selected_style;
  70. Ref<StyleBox> tab_disabled_style;
  71. Ref<StyleBox> tab_focus_style;
  72. Ref<Texture2D> increment_icon;
  73. Ref<Texture2D> increment_hl_icon;
  74. Ref<Texture2D> decrement_icon;
  75. Ref<Texture2D> decrement_hl_icon;
  76. Ref<Texture2D> drop_mark_icon;
  77. Color drop_mark_color;
  78. Color font_selected_color;
  79. Color font_hovered_color;
  80. Color font_unselected_color;
  81. Color font_disabled_color;
  82. Color font_outline_color;
  83. Ref<Font> tab_font;
  84. int tab_font_size;
  85. } theme_cache;
  86. HashMap<Node *, RID> tab_panels;
  87. Rect2 _get_tab_rect() const;
  88. int _get_tab_height() const;
  89. Vector<Control *> _get_tab_controls() const;
  90. void _on_theme_changed();
  91. void _repaint();
  92. void _refresh_tab_indices();
  93. void _refresh_tab_names();
  94. void _update_margins();
  95. void _on_mouse_exited();
  96. void _on_tab_changed(int p_tab);
  97. void _on_tab_clicked(int p_tab);
  98. void _on_tab_hovered(int p_tab);
  99. void _on_tab_selected(int p_tab);
  100. void _on_tab_button_pressed(int p_tab);
  101. void _on_active_tab_rearranged(int p_tab);
  102. void _on_tab_visibility_changed(Control *p_child);
  103. Variant _get_drag_data_fw(const Point2 &p_point, Control *p_from_control);
  104. bool _can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const;
  105. void _drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control);
  106. void _drag_move_tab(int p_from_index, int p_to_index);
  107. void _drag_move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
  108. protected:
  109. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  110. void _notification(int p_what);
  111. virtual void add_child_notify(Node *p_child) override;
  112. virtual void move_child_notify(Node *p_child) override;
  113. virtual void remove_child_notify(Node *p_child) override;
  114. static void _bind_methods();
  115. public:
  116. virtual bool accessibility_override_tree_hierarchy() const override { return true; }
  117. TabBar *get_tab_bar() const;
  118. int get_tab_idx_at_point(const Point2 &p_point) const;
  119. int get_tab_idx_from_control(Control *p_child) const;
  120. void set_tab_alignment(TabBar::AlignmentMode p_alignment);
  121. TabBar::AlignmentMode get_tab_alignment() const;
  122. void set_tabs_position(TabPosition p_tab_position);
  123. TabPosition get_tabs_position() const;
  124. void set_tab_focus_mode(FocusMode p_focus_mode);
  125. FocusMode get_tab_focus_mode() const;
  126. void set_clip_tabs(bool p_clip_tabs);
  127. bool get_clip_tabs() const;
  128. void set_tabs_visible(bool p_visible);
  129. bool are_tabs_visible() const;
  130. void set_all_tabs_in_front(bool p_is_front);
  131. bool is_all_tabs_in_front() const;
  132. void set_tab_title(int p_tab, const String &p_title);
  133. String get_tab_title(int p_tab) const;
  134. void set_tab_tooltip(int p_tab, const String &p_tooltip);
  135. String get_tab_tooltip(int p_tab) const;
  136. void set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon);
  137. Ref<Texture2D> get_tab_icon(int p_tab) const;
  138. void set_tab_icon_max_width(int p_tab, int p_width);
  139. int get_tab_icon_max_width(int p_tab) const;
  140. void set_tab_disabled(int p_tab, bool p_disabled);
  141. bool is_tab_disabled(int p_tab) const;
  142. void set_tab_hidden(int p_tab, bool p_hidden);
  143. bool is_tab_hidden(int p_tab) const;
  144. void set_tab_metadata(int p_tab, const Variant &p_metadata);
  145. Variant get_tab_metadata(int p_tab) const;
  146. void set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon);
  147. Ref<Texture2D> get_tab_button_icon(int p_tab) const;
  148. int get_tab_count() const;
  149. void set_current_tab(int p_current);
  150. int get_current_tab() const;
  151. int get_previous_tab() const;
  152. bool select_previous_available();
  153. bool select_next_available();
  154. void set_deselect_enabled(bool p_enabled);
  155. bool get_deselect_enabled() const;
  156. Control *get_tab_control(int p_idx) const;
  157. Control *get_current_tab_control() const;
  158. virtual Size2 get_minimum_size() const override;
  159. void set_popup(Node *p_popup);
  160. Popup *get_popup() const;
  161. void move_tab_from_tab_container(TabContainer *p_from, int p_from_index, int p_to_index = -1);
  162. void set_drag_to_rearrange_enabled(bool p_enabled);
  163. bool get_drag_to_rearrange_enabled() const;
  164. void set_tabs_rearrange_group(int p_group_id);
  165. int get_tabs_rearrange_group() const;
  166. void set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs);
  167. bool get_use_hidden_tabs_for_min_size() const;
  168. virtual Vector<int> get_allowed_size_flags_horizontal() const override;
  169. virtual Vector<int> get_allowed_size_flags_vertical() const override;
  170. TabContainer();
  171. };
  172. VARIANT_ENUM_CAST(TabContainer::TabPosition);