Debian Codegraph @

scene/gui/tab_bar.h

raw symbols SCIP
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 #pragma once
32
33 #include "scene/gui/control.h"
34 #include "scene/property_list_helper.h"
35 #include "scene/resources/text_line.h"
36
37 class Timer;
38
39 class TabBar : public Control {
40 GDCLASS(TabBar, Control);
41
42 public:
43 enum AlignmentMode {
44 ALIGNMENT_LEFT,
45 ALIGNMENT_CENTER,
46 ALIGNMENT_RIGHT,
47 ALIGNMENT_MAX,
48 };
49
50 enum CloseButtonDisplayPolicy {
51 CLOSE_BUTTON_SHOW_NEVER,
52 CLOSE_BUTTON_SHOW_ACTIVE_ONLY,
53 CLOSE_BUTTON_SHOW_ALWAYS,
54 CLOSE_BUTTON_MAX
55 };
56
57 enum DrawMode {
58 DRAW_NORMAL,
59 DRAW_PRESSED,
60 DRAW_HOVER,
61 DRAW_DISABLED,
62 DRAW_MAX,
63 };
64
65 private:
66 struct Tab {
67 mutable RID accessibility_item_element;
68 mutable bool accessibility_item_dirty = true;
69
70 // Corresponds to color overrides for the DrawMode enum
71 Color font_color_overrides[DrawMode::DRAW_MAX] = { Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0), Color(0, 0, 0, 0) };
72
73 String text;
74 String tooltip;
75
76 String language;
77 Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
78
79 Ref<TextLine> text_buf;
80 Ref<Texture2D> icon;
81 int icon_max_width = 0;
82
83 bool disabled = false;
84 bool hidden = false;
85 bool truncated = false;
86
87 Variant metadata;
88 int ofs_cache = 0;
89 int size_cache = 0;
90 int size_text = 0;
91
92 Ref<Texture2D> right_button;
93 Rect2 rb_rect;
94 Rect2 cb_rect;
95
96 Tab() {
97 text_buf.instantiate();
98 }
99
100 Tab(bool p_dummy) {}
101 };
102
103 static inline PropertyListHelper base_property_helper;
104 PropertyListHelper property_helper;
105
106 int offset = 0;
107 int max_drawn_tab = 0;
108 int highlight_arrow = -1;
109 bool buttons_visible = false;
110 bool missing_right = false;
111 Vector<Tab> tabs;
112 int current = -1;
113 int previous = -1;
114 AlignmentMode tab_alignment = ALIGNMENT_LEFT;
115 bool clip_tabs = true;
116 int rb_hover = -1;
117 bool rb_pressing = false;
118 bool tab_style_v_flip = false;
119
120 bool select_with_rmb = false;
121 bool deselect_enabled = false;
122 bool switch_on_release = false;
123
124 int cb_hover = -1;
125 bool cb_pressing = false;
126 CloseButtonDisplayPolicy cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
127 bool close_with_middle_mouse = true;
128
129 int hover = -1; // Hovered tab.
130 int max_width = 0;
131 bool scrolling_enabled = true;
132 bool drag_to_rearrange_enabled = false;
133 bool dragging_valid_tab = false;
134 bool scroll_to_selected = true;
135 int tabs_rearrange_group = -1;
136 bool switch_on_drag_hover = true;
137
138 static const int CURRENT_TAB_UNINITIALIZED = -2;
139 bool initialized = false;
140 int queued_current = CURRENT_TAB_UNINITIALIZED;
141
142 const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
143 const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
144 float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
145
146 struct ThemeCache {
147 int h_separation = 0;
148 int tab_separation = 0;
149 int icon_max_width = 0;
150 int hover_switch_wait_msec = 500;
151
152 Ref<StyleBox> tab_unselected_style;
153 Ref<StyleBox> tab_hovered_style;
154 Ref<StyleBox> tab_selected_style;
155 Ref<StyleBox> tab_disabled_style;
156 Ref<StyleBox> tab_focus_style;
157
158 Ref<Texture2D> increment_icon;
159 Ref<Texture2D> increment_hl_icon;
160 Ref<Texture2D> decrement_icon;
161 Ref<Texture2D> decrement_hl_icon;
162 Ref<Texture2D> drop_mark_icon;
163 Color drop_mark_color;
164
165 Ref<Font> font;
166 int font_size;
167 int outline_size = 0;
168
169 Color font_selected_color;
170 Color font_hovered_color;
171 Color font_unselected_color;
172 Color font_disabled_color;
173 Color font_outline_color;
174
175 Color icon_selected_color;
176 Color icon_hovered_color;
177 Color icon_unselected_color;
178 Color icon_disabled_color;
179
180 Ref<Texture2D> close_icon;
181 Ref<StyleBox> button_pressed_style;
182 Ref<StyleBox> button_hl_style;
183 } theme_cache;
184
185 Timer *hover_switch_delay = nullptr;
186
187 int get_tab_width(int p_idx) const;
188 Size2 _get_tab_icon_size(int p_idx) const;
189 void _ensure_no_over_offset();
190 bool _can_deselect() const;
191
192 void _update_hover();
193 void _update_cache(bool p_update_hover = true);
194 void _hover_switch_timeout();
195
196 void _on_mouse_exited();
197
198 void _shape(int p_tab);
199 void _draw_tab(Ref<StyleBox> &p_tab_style, const Color &p_font_color, const Color &p_icon_color, int p_index, float p_x, bool p_focus);
200
201 void _accessibility_action_scroll_into_view(const Variant &p_data, int p_index);
202 void _accessibility_action_focus(const Variant &p_data, int p_index);
203
204 protected:
205 virtual void gui_input(const Ref<InputEvent> &p_event) override;
206 virtual String get_tooltip(const Point2 &p_pos) const override;
207
208 bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
209 bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
210 void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
211 bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
212 bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
213 void _notification(int p_what);
214 static void _bind_methods();
215
216 Variant get_drag_data(const Point2 &p_point) override;
217 bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
218 void drop_data(const Point2 &p_point, const Variant &p_data) override;
219 void _move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
220
221 public:
222 RID get_tab_accessibility_element(int p_tab) const;
223 virtual RID get_focused_accessibility_element() const override;
224
225 Variant _handle_get_drag_data(const String &p_type, const Point2 &p_point);
226 bool _handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const;
227 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);
228 void _draw_tab_drop(RID p_canvas_item);
229 void _handle_switch_on_hover(const Variant &p_data) const;
230
231 void add_tab(const String &p_str = "", const Ref<Texture2D> &p_icon = Ref<Texture2D>());
232
233 void set_tab_title(int p_tab, const String &p_title);
234 String get_tab_title(int p_tab) const;
235
236 void set_tab_tooltip(int p_tab, const String &p_tooltip);
237 String get_tab_tooltip(int p_tab) const;
238
239 void set_tab_text_direction(int p_tab, TextDirection p_text_direction);
240 TextDirection get_tab_text_direction(int p_tab) const;
241
242 void set_tab_language(int p_tab, const String &p_language);
243 String get_tab_language(int p_tab) const;
244
245 void set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon);
246 Ref<Texture2D> get_tab_icon(int p_tab) const;
247
248 void set_tab_icon_max_width(int p_tab, int p_width);
249 int get_tab_icon_max_width(int p_tab) const;
250
251 void set_font_color_override_all(int p_tab, const Color &p_color);
252 void set_font_color_override(int p_tab, DrawMode p_draw_mode, const Color &p_color);
253 Color get_font_color_override(int p_tab, DrawMode p_draw_mode) const;
254
255 void set_tab_disabled(int p_tab, bool p_disabled);
256 bool is_tab_disabled(int p_tab) const;
257
258 void set_tab_hidden(int p_tab, bool p_hidden);
259 bool is_tab_hidden(int p_tab) const;
260
261 void set_tab_metadata(int p_tab, const Variant &p_metadata);
262 Variant get_tab_metadata(int p_tab) const;
263
264 void set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon);
265 Ref<Texture2D> get_tab_button_icon(int p_tab) const;
266
267 int get_tab_idx_at_point(const Point2 &p_point) const;
268 int get_closest_tab_idx_to_point(const Point2 &p_point) const;
269
270 void set_tab_alignment(AlignmentMode p_alignment);
271 AlignmentMode get_tab_alignment() const;
272
273 void set_clip_tabs(bool p_clip_tabs);
274 bool get_clip_tabs() const;
275
276 void set_tab_style_v_flip(bool p_tab_style_v_flip);
277
278 void move_tab(int p_from, int p_to);
279
280 void set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy);
281 CloseButtonDisplayPolicy get_tab_close_display_policy() const;
282
283 void set_close_with_middle_mouse(bool p_scroll_close);
284 bool get_close_with_middle_mouse() const;
285
286 void set_tab_count(int p_count);
287 int get_tab_count() const;
288
289 void set_current_tab(int p_current);
290 int get_current_tab() const;
291 int get_previous_tab() const;
292 int get_hovered_tab() const;
293
294 int get_previous_available(int p_idx = -1) const;
295 int get_next_available(int p_idx = -1) const;
296
297 bool select_previous_available();
298 bool select_next_available();
299
300 void set_tab_offset(int p_offset);
301 int get_tab_offset() const;
302 bool get_offset_buttons_visible() const;
303
304 void remove_tab(int p_idx);
305
306 void clear_tabs();
307
308 void set_scrolling_enabled(bool p_enabled);
309 bool get_scrolling_enabled() const;
310
311 void set_drag_to_rearrange_enabled(bool p_enabled);
312 bool get_drag_to_rearrange_enabled() const;
313 void set_tabs_rearrange_group(int p_group_id);
314 int get_tabs_rearrange_group() const;
315
316 void set_scroll_to_selected(bool p_enabled);
317 bool get_scroll_to_selected() const;
318
319 void set_switch_on_drag_hover(bool p_enabled);
320 bool get_switch_on_drag_hover() const;
321
322 void set_select_with_rmb(bool p_enabled);
323 bool get_select_with_rmb() const;
324
325 void set_deselect_enabled(bool p_enabled);
326 bool get_deselect_enabled() const;
327
328 void ensure_tab_visible(int p_idx);
329
330 void set_max_tab_width(int p_width);
331 int get_max_tab_width() const;
332
333 void set_switch_on_release(bool p_switch) { switch_on_release = p_switch; }
334
335 Rect2 get_tab_rect(int p_tab) const;
336 Size2 get_minimum_size() const override;
337
338 TabBar();
339 };
340
341 VARIANT_ENUM_CAST(TabBar::AlignmentMode);
342 VARIANT_ENUM_CAST(TabBar::CloseButtonDisplayPolicy);