tab_bar.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*************************************************************************/
  2. /* tab_bar.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 TAB_BAR_H
  31. #define TAB_BAR_H
  32. #include "scene/gui/control.h"
  33. #include "scene/resources/text_line.h"
  34. class TabBar : public Control {
  35. GDCLASS(TabBar, Control);
  36. public:
  37. enum TabAlign {
  38. ALIGN_LEFT,
  39. ALIGN_CENTER,
  40. ALIGN_RIGHT,
  41. ALIGN_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. String text;
  52. String xl_text;
  53. Dictionary opentype_features;
  54. String language;
  55. Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
  56. Ref<TextLine> text_buf;
  57. Ref<Texture2D> icon;
  58. int ofs_cache = 0;
  59. bool disabled = false;
  60. int size_cache = 0;
  61. int size_text = 0;
  62. int x_cache = 0;
  63. int x_size_cache = 0;
  64. Ref<Texture2D> right_button;
  65. Rect2 rb_rect;
  66. Rect2 cb_rect;
  67. };
  68. int offset = 0;
  69. int max_drawn_tab = 0;
  70. int highlight_arrow = -1;
  71. bool buttons_visible = false;
  72. bool missing_right = false;
  73. Vector<Tab> tabs;
  74. int current = 0;
  75. int previous = 0;
  76. TabAlign tab_align = ALIGN_CENTER;
  77. bool clip_tabs = true;
  78. int rb_hover = -1;
  79. bool rb_pressing = false;
  80. bool select_with_rmb = false;
  81. int cb_hover = -1;
  82. bool cb_pressing = false;
  83. CloseButtonDisplayPolicy cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
  84. int hover = -1; // Hovered tab.
  85. int min_width = 0;
  86. bool scrolling_enabled = true;
  87. bool drag_to_rearrange_enabled = false;
  88. int tabs_rearrange_group = -1;
  89. int get_tab_width(int p_idx) const;
  90. void _ensure_no_over_offset();
  91. void _update_hover();
  92. void _update_cache();
  93. void _on_mouse_exited();
  94. void _shape(int p_tab);
  95. protected:
  96. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  97. void _notification(int p_what);
  98. static void _bind_methods();
  99. Variant get_drag_data(const Point2 &p_point) override;
  100. bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  101. void drop_data(const Point2 &p_point, const Variant &p_data) override;
  102. int get_tab_idx_at_point(const Point2 &p_point) const;
  103. public:
  104. void add_tab(const String &p_str = "", const Ref<Texture2D> &p_icon = Ref<Texture2D>());
  105. void set_tab_title(int p_tab, const String &p_title);
  106. String get_tab_title(int p_tab) const;
  107. void set_tab_text_direction(int p_tab, TextDirection p_text_direction);
  108. TextDirection get_tab_text_direction(int p_tab) const;
  109. void set_tab_opentype_feature(int p_tab, const String &p_name, int p_value);
  110. int get_tab_opentype_feature(int p_tab, const String &p_name) const;
  111. void clear_tab_opentype_features(int p_tab);
  112. void set_tab_language(int p_tab, const String &p_language);
  113. String get_tab_language(int p_tab) const;
  114. void set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon);
  115. Ref<Texture2D> get_tab_icon(int p_tab) const;
  116. void set_tab_disabled(int p_tab, bool p_disabled);
  117. bool get_tab_disabled(int p_tab) const;
  118. void set_tab_right_button(int p_tab, const Ref<Texture2D> &p_right_button);
  119. Ref<Texture2D> get_tab_right_button(int p_tab) const;
  120. void set_tab_align(TabAlign p_align);
  121. TabAlign get_tab_align() const;
  122. void set_clip_tabs(bool p_clip_tabs);
  123. bool get_clip_tabs() const;
  124. void move_tab(int from, int to);
  125. void set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy);
  126. CloseButtonDisplayPolicy get_tab_close_display_policy() const;
  127. int get_tab_count() const;
  128. void set_current_tab(int p_current);
  129. int get_current_tab() const;
  130. int get_previous_tab() const;
  131. int get_hovered_tab() const;
  132. int get_tab_offset() const;
  133. bool get_offset_buttons_visible() const;
  134. void remove_tab(int p_idx);
  135. void clear_tabs();
  136. void set_scrolling_enabled(bool p_enabled);
  137. bool get_scrolling_enabled() const;
  138. void set_drag_to_rearrange_enabled(bool p_enabled);
  139. bool get_drag_to_rearrange_enabled() const;
  140. void set_tabs_rearrange_group(int p_group_id);
  141. int get_tabs_rearrange_group() const;
  142. void set_select_with_rmb(bool p_enabled);
  143. bool get_select_with_rmb() const;
  144. void ensure_tab_visible(int p_idx);
  145. void set_min_width(int p_width);
  146. Rect2 get_tab_rect(int p_tab) const;
  147. Size2 get_minimum_size() const override;
  148. TabBar();
  149. };
  150. VARIANT_ENUM_CAST(TabBar::TabAlign);
  151. VARIANT_ENUM_CAST(TabBar::CloseButtonDisplayPolicy);
  152. #endif // TAB_BAR_H