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