1 /*************************************************************************/
2 /* tab_container.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 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
31 #include "tab_container.h"
32
33 #include "core/message_queue.h"
34 #include "scene/gui/box_container.h"
35 #include "scene/gui/label.h"
36 #include "scene/gui/texture_rect.h"
37
_get_top_margin() const38 int TabContainer::_get_top_margin() const {
39
40 if (!tabs_visible)
41 return 0;
42
43 // Respect the minimum tab height.
44 Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
45 Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
46 Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
47
48 int tab_height = MAX(MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height), tab_disabled->get_minimum_size().height);
49
50 // Font height or higher icon wins.
51 Ref<Font> font = get_font("font");
52 int content_height = font->get_height();
53
54 Vector<Control *> tabs = _get_tabs();
55 for (int i = 0; i < tabs.size(); i++) {
56
57 Control *c = tabs[i];
58 if (!c->has_meta("_tab_icon"))
59 continue;
60
61 Ref<Texture> tex = c->get_meta("_tab_icon");
62 if (!tex.is_valid())
63 continue;
64 content_height = MAX(content_height, tex->get_size().height);
65 }
66
67 return tab_height + content_height;
68 }
69
_gui_input(const Ref<InputEvent> & p_event)70 void TabContainer::_gui_input(const Ref<InputEvent> &p_event) {
71
72 Ref<InputEventMouseButton> mb = p_event;
73
74 Popup *popup = get_popup();
75
76 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
77
78 Point2 pos(mb->get_position().x, mb->get_position().y);
79 Size2 size = get_size();
80
81 // Click must be on tabs in the tab header area.
82 if (pos.x < tabs_ofs_cache || pos.y > _get_top_margin())
83 return;
84
85 // Handle menu button.
86 Ref<Texture> menu = get_icon("menu");
87 if (popup && pos.x > size.width - menu->get_width()) {
88 emit_signal("pre_popup_pressed");
89
90 Vector2 popup_pos = get_global_position();
91 popup_pos.x += size.width * get_global_transform().get_scale().x - popup->get_size().width * popup->get_global_transform().get_scale().x;
92 popup_pos.y += menu->get_height() * get_global_transform().get_scale().y;
93
94 popup->set_global_position(popup_pos);
95 popup->popup();
96 return;
97 }
98
99 // Do not activate tabs when tabs is empty.
100 if (get_tab_count() == 0)
101 return;
102
103 Vector<Control *> tabs = _get_tabs();
104
105 // Handle navigation buttons.
106 if (buttons_visible_cache) {
107 int popup_ofs = 0;
108 if (popup) {
109 popup_ofs = menu->get_width();
110 }
111
112 Ref<Texture> increment = get_icon("increment");
113 Ref<Texture> decrement = get_icon("decrement");
114 if (pos.x > size.width - increment->get_width() - popup_ofs) {
115 if (last_tab_cache < tabs.size() - 1) {
116 first_tab_cache += 1;
117 update();
118 }
119 return;
120 } else if (pos.x > size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
121 if (first_tab_cache > 0) {
122 first_tab_cache -= 1;
123 update();
124 }
125 return;
126 }
127 }
128
129 // Activate the clicked tab.
130 pos.x -= tabs_ofs_cache;
131 for (int i = first_tab_cache; i <= last_tab_cache; i++) {
132 if (get_tab_hidden(i)) {
133 continue;
134 }
135 int tab_width = _get_tab_width(i);
136 if (pos.x < tab_width) {
137 if (!get_tab_disabled(i)) {
138 set_current_tab(i);
139 }
140 break;
141 }
142 pos.x -= tab_width;
143 }
144 }
145
146 Ref<InputEventMouseMotion> mm = p_event;
147
148 if (mm.is_valid()) {
149
150 Point2 pos(mm->get_position().x, mm->get_position().y);
151 Size2 size = get_size();
152
153 // Mouse must be on tabs in the tab header area.
154 if (pos.x < tabs_ofs_cache || pos.y > _get_top_margin()) {
155
156 if (menu_hovered || highlight_arrow > -1) {
157 menu_hovered = false;
158 highlight_arrow = -1;
159 update();
160 }
161 return;
162 }
163
164 Ref<Texture> menu = get_icon("menu");
165 if (popup) {
166
167 if (pos.x >= size.width - menu->get_width()) {
168 if (!menu_hovered) {
169 menu_hovered = true;
170 highlight_arrow = -1;
171 update();
172 return;
173 }
174 } else if (menu_hovered) {
175 menu_hovered = false;
176 update();
177 }
178
179 if (menu_hovered) {
180 return;
181 }
182 }
183
184 // Do not activate tabs when tabs is empty.
185 if ((get_tab_count() == 0 || !buttons_visible_cache) && menu_hovered) {
186 highlight_arrow = -1;
187 update();
188 return;
189 }
190
191 int popup_ofs = 0;
192 if (popup) {
193 popup_ofs = menu->get_width();
194 }
195
196 Ref<Texture> increment = get_icon("increment");
197 Ref<Texture> decrement = get_icon("decrement");
198 if (pos.x >= size.width - increment->get_width() - popup_ofs) {
199
200 if (highlight_arrow != 1) {
201 highlight_arrow = 1;
202 update();
203 }
204 } else if (pos.x >= size.width - increment->get_width() - decrement->get_width() - popup_ofs) {
205
206 if (highlight_arrow != 0) {
207 highlight_arrow = 0;
208 update();
209 }
210 } else if (highlight_arrow > -1) {
211 highlight_arrow = -1;
212 update();
213 }
214 }
215 }
216
_notification(int p_what)217 void TabContainer::_notification(int p_what) {
218
219 switch (p_what) {
220
221 case NOTIFICATION_TRANSLATION_CHANGED: {
222
223 minimum_size_changed();
224 update();
225 } break;
226 case NOTIFICATION_RESIZED: {
227
228 Vector<Control *> tabs = _get_tabs();
229 int side_margin = get_constant("side_margin");
230 Ref<Texture> menu = get_icon("menu");
231 Ref<Texture> increment = get_icon("increment");
232 Ref<Texture> decrement = get_icon("decrement");
233 int header_width = get_size().width - side_margin * 2;
234
235 // Find the width of the header area.
236 Popup *popup = get_popup();
237 if (popup)
238 header_width -= menu->get_width();
239 if (buttons_visible_cache)
240 header_width -= increment->get_width() + decrement->get_width();
241 if (popup || buttons_visible_cache)
242 header_width += side_margin;
243
244 // Find the width of all tabs after first_tab_cache.
245 int all_tabs_width = 0;
246 for (int i = first_tab_cache; i < tabs.size(); i++) {
247 int tab_width = _get_tab_width(i);
248 all_tabs_width += tab_width;
249 }
250
251 // Check if tabs before first_tab_cache would fit into the header area.
252 for (int i = first_tab_cache - 1; i >= 0; i--) {
253 int tab_width = _get_tab_width(i);
254
255 if (all_tabs_width + tab_width > header_width)
256 break;
257
258 all_tabs_width += tab_width;
259 first_tab_cache--;
260 }
261 } break;
262 case NOTIFICATION_DRAW: {
263
264 RID canvas = get_canvas_item();
265 Size2 size = get_size();
266
267 // Draw only the tab area if the header is hidden.
268 Ref<StyleBox> panel = get_stylebox("panel");
269 if (!tabs_visible) {
270 panel->draw(canvas, Rect2(0, 0, size.width, size.height));
271 return;
272 }
273
274 Vector<Control *> tabs = _get_tabs();
275 Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
276 Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
277 Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
278 Ref<Texture> increment = get_icon("increment");
279 Ref<Texture> increment_hl = get_icon("increment_highlight");
280 Ref<Texture> decrement = get_icon("decrement");
281 Ref<Texture> decrement_hl = get_icon("decrement_highlight");
282 Ref<Texture> menu = get_icon("menu");
283 Ref<Texture> menu_hl = get_icon("menu_highlight");
284 Ref<Font> font = get_font("font");
285 Color font_color_fg = get_color("font_color_fg");
286 Color font_color_bg = get_color("font_color_bg");
287 Color font_color_disabled = get_color("font_color_disabled");
288 int side_margin = get_constant("side_margin");
289 int icon_text_distance = get_constant("hseparation");
290
291 // Find out start and width of the header area.
292 int header_x = side_margin;
293 int header_width = size.width - side_margin * 2;
294 int header_height = _get_top_margin();
295 Popup *popup = get_popup();
296 if (popup)
297 header_width -= menu->get_width();
298
299 // Check if all tabs would fit into the header area.
300 int all_tabs_width = 0;
301 for (int i = 0; i < tabs.size(); i++) {
302 if (get_tab_hidden(i)) {
303 continue;
304 }
305 int tab_width = _get_tab_width(i);
306 all_tabs_width += tab_width;
307
308 if (all_tabs_width > header_width) {
309 // Not all tabs are visible at the same time - reserve space for navigation buttons.
310 buttons_visible_cache = true;
311 header_width -= decrement->get_width() + increment->get_width();
312 break;
313 } else {
314 buttons_visible_cache = false;
315 }
316 }
317 // With buttons, a right side margin does not need to be respected.
318 if (popup || buttons_visible_cache) {
319 header_width += side_margin;
320 }
321
322 if (!buttons_visible_cache) {
323 first_tab_cache = 0;
324 }
325
326 // Go through the visible tabs to find the width they occupy.
327 all_tabs_width = 0;
328 Vector<int> tab_widths;
329 for (int i = first_tab_cache; i < tabs.size(); i++) {
330 if (get_tab_hidden(i)) {
331 continue;
332 }
333 int tab_width = _get_tab_width(i);
334 if (all_tabs_width + tab_width > header_width && tab_widths.size() > 0)
335 break;
336 all_tabs_width += tab_width;
337 tab_widths.push_back(tab_width);
338 }
339
340 // Find the offset at which to draw tabs, according to the alignment.
341 switch (align) {
342 case ALIGN_LEFT:
343 tabs_ofs_cache = header_x;
344 break;
345 case ALIGN_CENTER:
346 tabs_ofs_cache = header_x + (header_width / 2) - (all_tabs_width / 2);
347 break;
348 case ALIGN_RIGHT:
349 tabs_ofs_cache = header_x + header_width - all_tabs_width;
350 break;
351 }
352
353 // Draw the tab area.
354 panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
355
356 // Draw all visible tabs.
357 int x = 0;
358 for (int i = 0; i < tab_widths.size(); i++) {
359 if (get_tab_hidden(i)) {
360 continue;
361 }
362 Ref<StyleBox> tab_style;
363 Color font_color;
364 if (get_tab_disabled(i + first_tab_cache)) {
365 tab_style = tab_disabled;
366 font_color = font_color_disabled;
367 } else if (i + first_tab_cache == current) {
368 tab_style = tab_fg;
369 font_color = font_color_fg;
370 } else {
371 tab_style = tab_bg;
372 font_color = font_color_bg;
373 }
374
375 // Draw the tab background.
376 int tab_width = tab_widths[i];
377 Rect2 tab_rect(tabs_ofs_cache + x, 0, tab_width, header_height);
378 tab_style->draw(canvas, tab_rect);
379
380 // Draw the tab contents.
381 Control *control = Object::cast_to<Control>(tabs[i + first_tab_cache]);
382 String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
383
384 int x_content = tab_rect.position.x + tab_style->get_margin(MARGIN_LEFT);
385 int top_margin = tab_style->get_margin(MARGIN_TOP);
386 int y_center = top_margin + (tab_rect.size.y - tab_style->get_minimum_size().y) / 2;
387
388 // Draw the tab icon.
389 if (control->has_meta("_tab_icon")) {
390 Ref<Texture> icon = control->get_meta("_tab_icon");
391 if (icon.is_valid()) {
392 int y = y_center - (icon->get_height() / 2);
393 icon->draw(canvas, Point2i(x_content, y));
394 if (text != "")
395 x_content += icon->get_width() + icon_text_distance;
396 }
397 }
398
399 // Draw the tab text.
400 Point2i text_pos(x_content, y_center - (font->get_height() / 2) + font->get_ascent());
401 font->draw(canvas, text_pos, text, font_color);
402
403 x += tab_width;
404 last_tab_cache = i + first_tab_cache;
405 }
406
407 // Draw the popup menu.
408 x = get_size().width;
409 if (popup) {
410 x -= menu->get_width();
411 if (menu_hovered)
412 menu_hl->draw(get_canvas_item(), Size2(x, (header_height - menu_hl->get_height()) / 2));
413 else
414 menu->draw(get_canvas_item(), Size2(x, (header_height - menu->get_height()) / 2));
415 }
416
417 // Draw the navigation buttons.
418 if (buttons_visible_cache) {
419
420 x -= increment->get_width();
421 if (last_tab_cache < tabs.size() - 1) {
422 draw_texture(highlight_arrow == 1 ? increment_hl : increment, Point2(x, (header_height - increment->get_height()) / 2));
423 } else {
424 draw_texture(increment, Point2(x, (header_height - increment->get_height()) / 2), Color(1, 1, 1, 0.5));
425 }
426
427 x -= decrement->get_width();
428 if (first_tab_cache > 0) {
429 draw_texture(highlight_arrow == 0 ? decrement_hl : decrement, Point2(x, (header_height - decrement->get_height()) / 2));
430 } else {
431 draw_texture(decrement, Point2(x, (header_height - decrement->get_height()) / 2), Color(1, 1, 1, 0.5));
432 }
433 }
434 } break;
435 case NOTIFICATION_THEME_CHANGED: {
436
437 minimum_size_changed();
438 call_deferred("_on_theme_changed"); // Wait until all changed theme.
439 } break;
440 }
441 }
442
_on_theme_changed()443 void TabContainer::_on_theme_changed() {
444 if (get_tab_count() > 0) {
445 _repaint();
446 update();
447 }
448 }
449
_repaint()450 void TabContainer::_repaint() {
451 Ref<StyleBox> sb = get_stylebox("panel");
452 Vector<Control *> tabs = _get_tabs();
453 for (int i = 0; i < tabs.size(); i++) {
454 Control *c = tabs[i];
455 if (i == current) {
456 c->show();
457 c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
458 if (tabs_visible) {
459 c->set_margin(MARGIN_TOP, _get_top_margin());
460 }
461 c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
462 c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
463 c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
464 c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
465
466 } else {
467 c->hide();
468 }
469 }
470 }
471
_on_mouse_exited()472 void TabContainer::_on_mouse_exited() {
473 if (menu_hovered || highlight_arrow > -1) {
474 menu_hovered = false;
475 highlight_arrow = -1;
476 update();
477 }
478 }
479
_get_tab_width(int p_index) const480 int TabContainer::_get_tab_width(int p_index) const {
481
482 ERR_FAIL_INDEX_V(p_index, get_tab_count(), 0);
483 Control *control = Object::cast_to<Control>(_get_tabs()[p_index]);
484 if (!control || control->is_set_as_toplevel() || get_tab_hidden(p_index))
485 return 0;
486
487 // Get the width of the text displayed on the tab.
488 Ref<Font> font = get_font("font");
489 String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(control->get_name());
490 int width = font->get_string_size(text).width;
491
492 // Add space for a tab icon.
493 if (control->has_meta("_tab_icon")) {
494 Ref<Texture> icon = control->get_meta("_tab_icon");
495 if (icon.is_valid()) {
496 width += icon->get_width();
497 if (text != "")
498 width += get_constant("hseparation");
499 }
500 }
501
502 // Respect a minimum size.
503 Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
504 Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
505 Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
506 if (get_tab_disabled(p_index)) {
507 width += tab_disabled->get_minimum_size().width;
508 } else if (p_index == current) {
509 width += tab_fg->get_minimum_size().width;
510 } else {
511 width += tab_bg->get_minimum_size().width;
512 }
513
514 return width;
515 }
516
_get_tabs() const517 Vector<Control *> TabContainer::_get_tabs() const {
518
519 Vector<Control *> controls;
520 for (int i = 0; i < get_child_count(); i++) {
521
522 Control *control = Object::cast_to<Control>(get_child(i));
523 if (!control || control->is_toplevel_control())
524 continue;
525
526 controls.push_back(control);
527 }
528 return controls;
529 }
530
_child_renamed_callback()531 void TabContainer::_child_renamed_callback() {
532
533 update();
534 }
535
add_child_notify(Node * p_child)536 void TabContainer::add_child_notify(Node *p_child) {
537
538 Container::add_child_notify(p_child);
539
540 Control *c = Object::cast_to<Control>(p_child);
541 if (!c)
542 return;
543 if (c->is_set_as_toplevel())
544 return;
545
546 bool first = false;
547
548 if (get_tab_count() != 1)
549 c->hide();
550 else {
551 c->show();
552 //call_deferred("set_current_tab",0);
553 first = true;
554 current = 0;
555 previous = 0;
556 }
557 c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
558 if (tabs_visible)
559 c->set_margin(MARGIN_TOP, _get_top_margin());
560 Ref<StyleBox> sb = get_stylebox("panel");
561 c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP)));
562 c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT)));
563 c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT)));
564 c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM)));
565
566 update();
567 p_child->connect("renamed", this, "_child_renamed_callback");
568 if (first)
569 emit_signal("tab_changed", current);
570 }
571
get_tab_count() const572 int TabContainer::get_tab_count() const {
573
574 return _get_tabs().size();
575 }
576
set_current_tab(int p_current)577 void TabContainer::set_current_tab(int p_current) {
578
579 ERR_FAIL_INDEX(p_current, get_tab_count());
580
581 int pending_previous = current;
582 current = p_current;
583
584 _repaint();
585
586 _change_notify("current_tab");
587
588 if (pending_previous == current)
589 emit_signal("tab_selected", current);
590 else {
591 previous = pending_previous;
592 emit_signal("tab_selected", current);
593 emit_signal("tab_changed", current);
594 }
595
596 update();
597 }
598
get_current_tab() const599 int TabContainer::get_current_tab() const {
600
601 return current;
602 }
603
get_previous_tab() const604 int TabContainer::get_previous_tab() const {
605
606 return previous;
607 }
608
get_tab_control(int p_idx) const609 Control *TabContainer::get_tab_control(int p_idx) const {
610
611 Vector<Control *> tabs = _get_tabs();
612 if (p_idx >= 0 && p_idx < tabs.size())
613 return tabs[p_idx];
614 else
615 return NULL;
616 }
617
get_current_tab_control() const618 Control *TabContainer::get_current_tab_control() const {
619
620 Vector<Control *> tabs = _get_tabs();
621 if (current >= 0 && current < tabs.size())
622 return tabs[current];
623 else
624 return NULL;
625 }
626
remove_child_notify(Node * p_child)627 void TabContainer::remove_child_notify(Node *p_child) {
628
629 Container::remove_child_notify(p_child);
630
631 call_deferred("_update_current_tab");
632
633 p_child->disconnect("renamed", this, "_child_renamed_callback");
634
635 update();
636 }
637
_update_current_tab()638 void TabContainer::_update_current_tab() {
639
640 int tc = get_tab_count();
641 if (current >= tc)
642 current = tc - 1;
643 if (current < 0)
644 current = 0;
645 else
646 set_current_tab(current);
647 }
648
get_drag_data(const Point2 & p_point)649 Variant TabContainer::get_drag_data(const Point2 &p_point) {
650
651 if (!drag_to_rearrange_enabled)
652 return Variant();
653
654 int tab_over = get_tab_idx_at_point(p_point);
655
656 if (tab_over < 0)
657 return Variant();
658
659 HBoxContainer *drag_preview = memnew(HBoxContainer);
660
661 Ref<Texture> icon = get_tab_icon(tab_over);
662 if (!icon.is_null()) {
663 TextureRect *tf = memnew(TextureRect);
664 tf->set_texture(icon);
665 drag_preview->add_child(tf);
666 }
667 Label *label = memnew(Label(get_tab_title(tab_over)));
668 drag_preview->add_child(label);
669 set_drag_preview(drag_preview);
670
671 Dictionary drag_data;
672 drag_data["type"] = "tabc_element";
673 drag_data["tabc_element"] = tab_over;
674 drag_data["from_path"] = get_path();
675 return drag_data;
676 }
677
can_drop_data(const Point2 & p_point,const Variant & p_data) const678 bool TabContainer::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
679
680 if (!drag_to_rearrange_enabled)
681 return false;
682
683 Dictionary d = p_data;
684 if (!d.has("type"))
685 return false;
686
687 if (String(d["type"]) == "tabc_element") {
688
689 NodePath from_path = d["from_path"];
690 NodePath to_path = get_path();
691 if (from_path == to_path) {
692 return true;
693 } else if (get_tabs_rearrange_group() != -1) {
694 // drag and drop between other TabContainers
695 Node *from_node = get_node(from_path);
696 TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
697 if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
698 return true;
699 }
700 }
701 }
702 return false;
703 }
704
drop_data(const Point2 & p_point,const Variant & p_data)705 void TabContainer::drop_data(const Point2 &p_point, const Variant &p_data) {
706
707 if (!drag_to_rearrange_enabled)
708 return;
709
710 int hover_now = get_tab_idx_at_point(p_point);
711
712 Dictionary d = p_data;
713 if (!d.has("type"))
714 return;
715
716 if (String(d["type"]) == "tabc_element") {
717
718 int tab_from_id = d["tabc_element"];
719 NodePath from_path = d["from_path"];
720 NodePath to_path = get_path();
721 if (from_path == to_path) {
722 if (hover_now < 0)
723 hover_now = get_tab_count() - 1;
724 move_child(get_tab_control(tab_from_id), hover_now);
725 set_current_tab(hover_now);
726 } else if (get_tabs_rearrange_group() != -1) {
727 // drag and drop between TabContainers
728 Node *from_node = get_node(from_path);
729 TabContainer *from_tabc = Object::cast_to<TabContainer>(from_node);
730 if (from_tabc && from_tabc->get_tabs_rearrange_group() == get_tabs_rearrange_group()) {
731 Control *moving_tabc = from_tabc->get_tab_control(tab_from_id);
732 from_tabc->remove_child(moving_tabc);
733 add_child(moving_tabc);
734 if (hover_now < 0)
735 hover_now = get_tab_count() - 1;
736 move_child(moving_tabc, hover_now);
737 set_current_tab(hover_now);
738 emit_signal("tab_changed", hover_now);
739 }
740 }
741 }
742 update();
743 }
744
get_tab_idx_at_point(const Point2 & p_point) const745 int TabContainer::get_tab_idx_at_point(const Point2 &p_point) const {
746
747 if (get_tab_count() == 0)
748 return -1;
749
750 // must be on tabs in the tab header area.
751 if (p_point.x < tabs_ofs_cache || p_point.y > _get_top_margin())
752 return -1;
753
754 Size2 size = get_size();
755 int right_ofs = 0;
756
757 Popup *popup = get_popup();
758 if (popup) {
759 Ref<Texture> menu = get_icon("menu");
760 right_ofs += menu->get_width();
761 }
762 if (buttons_visible_cache) {
763 Ref<Texture> increment = get_icon("increment");
764 Ref<Texture> decrement = get_icon("decrement");
765 right_ofs += increment->get_width() + decrement->get_width();
766 }
767 if (p_point.x > size.width - right_ofs) {
768 return -1;
769 }
770
771 // get the tab at the point
772 Vector<Control *> tabs = _get_tabs();
773 int px = p_point.x;
774 px -= tabs_ofs_cache;
775 for (int i = first_tab_cache; i <= last_tab_cache; i++) {
776 int tab_width = _get_tab_width(i);
777 if (px < tab_width) {
778 return i;
779 }
780 px -= tab_width;
781 }
782 return -1;
783 }
784
set_tab_align(TabAlign p_align)785 void TabContainer::set_tab_align(TabAlign p_align) {
786
787 ERR_FAIL_INDEX(p_align, 3);
788 align = p_align;
789 update();
790
791 _change_notify("tab_align");
792 }
793
get_tab_align() const794 TabContainer::TabAlign TabContainer::get_tab_align() const {
795
796 return align;
797 }
798
set_tabs_visible(bool p_visible)799 void TabContainer::set_tabs_visible(bool p_visible) {
800
801 if (p_visible == tabs_visible)
802 return;
803
804 tabs_visible = p_visible;
805
806 Vector<Control *> tabs = _get_tabs();
807 for (int i = 0; i < tabs.size(); i++) {
808
809 Control *c = tabs[i];
810 if (p_visible)
811 c->set_margin(MARGIN_TOP, _get_top_margin());
812 else
813 c->set_margin(MARGIN_TOP, 0);
814 }
815
816 update();
817 minimum_size_changed();
818 }
819
are_tabs_visible() const820 bool TabContainer::are_tabs_visible() const {
821
822 return tabs_visible;
823 }
824
_get_tab(int p_idx) const825 Control *TabContainer::_get_tab(int p_idx) const {
826
827 return get_tab_control(p_idx);
828 }
829
set_tab_title(int p_tab,const String & p_title)830 void TabContainer::set_tab_title(int p_tab, const String &p_title) {
831
832 Control *child = _get_tab(p_tab);
833 ERR_FAIL_COND(!child);
834 child->set_meta("_tab_name", p_title);
835 update();
836 }
837
get_tab_title(int p_tab) const838 String TabContainer::get_tab_title(int p_tab) const {
839
840 Control *child = _get_tab(p_tab);
841 ERR_FAIL_COND_V(!child, "");
842 if (child->has_meta("_tab_name"))
843 return child->get_meta("_tab_name");
844 else
845 return child->get_name();
846 }
847
set_tab_icon(int p_tab,const Ref<Texture> & p_icon)848 void TabContainer::set_tab_icon(int p_tab, const Ref<Texture> &p_icon) {
849
850 Control *child = _get_tab(p_tab);
851 ERR_FAIL_COND(!child);
852 child->set_meta("_tab_icon", p_icon);
853 update();
854 }
get_tab_icon(int p_tab) const855 Ref<Texture> TabContainer::get_tab_icon(int p_tab) const {
856
857 Control *child = _get_tab(p_tab);
858 ERR_FAIL_COND_V(!child, Ref<Texture>());
859 if (child->has_meta("_tab_icon"))
860 return child->get_meta("_tab_icon");
861 else
862 return Ref<Texture>();
863 }
864
set_tab_disabled(int p_tab,bool p_disabled)865 void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) {
866
867 Control *child = _get_tab(p_tab);
868 ERR_FAIL_COND(!child);
869 child->set_meta("_tab_disabled", p_disabled);
870 update();
871 }
872
get_tab_disabled(int p_tab) const873 bool TabContainer::get_tab_disabled(int p_tab) const {
874
875 Control *child = _get_tab(p_tab);
876 ERR_FAIL_COND_V(!child, false);
877 if (child->has_meta("_tab_disabled"))
878 return child->get_meta("_tab_disabled");
879 else
880 return false;
881 }
882
set_tab_hidden(int p_tab,bool p_hidden)883 void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) {
884
885 Control *child = _get_tab(p_tab);
886 ERR_FAIL_COND(!child);
887 child->set_meta("_tab_hidden", p_hidden);
888 update();
889 for (int i = 0; i < get_tab_count(); i++) {
890 int try_tab = (p_tab + 1 + i) % get_tab_count();
891 if (get_tab_disabled(try_tab) || get_tab_hidden(try_tab)) {
892 continue;
893 }
894
895 set_current_tab(try_tab);
896 return;
897 }
898
899 //assumed no other tab can be switched to, just hide
900 child->hide();
901 }
902
get_tab_hidden(int p_tab) const903 bool TabContainer::get_tab_hidden(int p_tab) const {
904
905 Control *child = _get_tab(p_tab);
906 ERR_FAIL_COND_V(!child, false);
907 if (child->has_meta("_tab_hidden"))
908 return child->get_meta("_tab_hidden");
909 else
910 return false;
911 }
912
get_translatable_strings(List<String> * p_strings) const913 void TabContainer::get_translatable_strings(List<String> *p_strings) const {
914
915 Vector<Control *> tabs = _get_tabs();
916 for (int i = 0; i < tabs.size(); i++) {
917
918 Control *c = tabs[i];
919
920 if (!c->has_meta("_tab_name"))
921 continue;
922
923 String name = c->get_meta("_tab_name");
924
925 if (name != "")
926 p_strings->push_back(name);
927 }
928 }
929
get_minimum_size() const930 Size2 TabContainer::get_minimum_size() const {
931
932 Size2 ms;
933
934 Vector<Control *> tabs = _get_tabs();
935 for (int i = 0; i < tabs.size(); i++) {
936
937 Control *c = tabs[i];
938
939 if (!c->is_visible_in_tree() && !use_hidden_tabs_for_min_size)
940 continue;
941
942 Size2 cms = c->get_combined_minimum_size();
943 ms.x = MAX(ms.x, cms.x);
944 ms.y = MAX(ms.y, cms.y);
945 }
946
947 Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
948 Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
949 Ref<StyleBox> tab_disabled = get_stylebox("tab_disabled");
950 Ref<Font> font = get_font("font");
951
952 if (tabs_visible) {
953 ms.y += MAX(MAX(tab_bg->get_minimum_size().y, tab_fg->get_minimum_size().y), tab_disabled->get_minimum_size().y);
954 ms.y += font->get_height();
955 }
956
957 Ref<StyleBox> sb = get_stylebox("panel");
958 ms += sb->get_minimum_size();
959
960 return ms;
961 }
962
set_popup(Node * p_popup)963 void TabContainer::set_popup(Node *p_popup) {
964 ERR_FAIL_NULL(p_popup);
965 Popup *popup = Object::cast_to<Popup>(p_popup);
966 popup_obj_id = popup ? popup->get_instance_id() : 0;
967 update();
968 }
969
get_popup() const970 Popup *TabContainer::get_popup() const {
971 if (popup_obj_id) {
972 Popup *popup = Object::cast_to<Popup>(ObjectDB::get_instance(popup_obj_id));
973 if (popup) {
974 return popup;
975 } else {
976 #ifdef DEBUG_ENABLED
977 ERR_PRINT("Popup assigned to TabContainer is gone!");
978 #endif
979 popup_obj_id = 0;
980 }
981 }
982 return NULL;
983 }
984
set_drag_to_rearrange_enabled(bool p_enabled)985 void TabContainer::set_drag_to_rearrange_enabled(bool p_enabled) {
986 drag_to_rearrange_enabled = p_enabled;
987 }
988
get_drag_to_rearrange_enabled() const989 bool TabContainer::get_drag_to_rearrange_enabled() const {
990 return drag_to_rearrange_enabled;
991 }
set_tabs_rearrange_group(int p_group_id)992 void TabContainer::set_tabs_rearrange_group(int p_group_id) {
993 tabs_rearrange_group = p_group_id;
994 }
995
get_tabs_rearrange_group() const996 int TabContainer::get_tabs_rearrange_group() const {
997 return tabs_rearrange_group;
998 }
999
set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs)1000 void TabContainer::set_use_hidden_tabs_for_min_size(bool p_use_hidden_tabs) {
1001 use_hidden_tabs_for_min_size = p_use_hidden_tabs;
1002 }
1003
get_use_hidden_tabs_for_min_size() const1004 bool TabContainer::get_use_hidden_tabs_for_min_size() const {
1005 return use_hidden_tabs_for_min_size;
1006 }
1007
_bind_methods()1008 void TabContainer::_bind_methods() {
1009
1010 ClassDB::bind_method(D_METHOD("_gui_input"), &TabContainer::_gui_input);
1011 ClassDB::bind_method(D_METHOD("get_tab_count"), &TabContainer::get_tab_count);
1012 ClassDB::bind_method(D_METHOD("set_current_tab", "tab_idx"), &TabContainer::set_current_tab);
1013 ClassDB::bind_method(D_METHOD("get_current_tab"), &TabContainer::get_current_tab);
1014 ClassDB::bind_method(D_METHOD("get_previous_tab"), &TabContainer::get_previous_tab);
1015 ClassDB::bind_method(D_METHOD("get_current_tab_control"), &TabContainer::get_current_tab_control);
1016 ClassDB::bind_method(D_METHOD("get_tab_control", "tab_idx"), &TabContainer::get_tab_control);
1017 ClassDB::bind_method(D_METHOD("set_tab_align", "align"), &TabContainer::set_tab_align);
1018 ClassDB::bind_method(D_METHOD("get_tab_align"), &TabContainer::get_tab_align);
1019 ClassDB::bind_method(D_METHOD("set_tabs_visible", "visible"), &TabContainer::set_tabs_visible);
1020 ClassDB::bind_method(D_METHOD("are_tabs_visible"), &TabContainer::are_tabs_visible);
1021 ClassDB::bind_method(D_METHOD("set_tab_title", "tab_idx", "title"), &TabContainer::set_tab_title);
1022 ClassDB::bind_method(D_METHOD("get_tab_title", "tab_idx"), &TabContainer::get_tab_title);
1023 ClassDB::bind_method(D_METHOD("set_tab_icon", "tab_idx", "icon"), &TabContainer::set_tab_icon);
1024 ClassDB::bind_method(D_METHOD("get_tab_icon", "tab_idx"), &TabContainer::get_tab_icon);
1025 ClassDB::bind_method(D_METHOD("set_tab_disabled", "tab_idx", "disabled"), &TabContainer::set_tab_disabled);
1026 ClassDB::bind_method(D_METHOD("get_tab_disabled", "tab_idx"), &TabContainer::get_tab_disabled);
1027 ClassDB::bind_method(D_METHOD("set_popup", "popup"), &TabContainer::set_popup);
1028 ClassDB::bind_method(D_METHOD("get_popup"), &TabContainer::get_popup);
1029 ClassDB::bind_method(D_METHOD("set_drag_to_rearrange_enabled", "enabled"), &TabContainer::set_drag_to_rearrange_enabled);
1030 ClassDB::bind_method(D_METHOD("get_drag_to_rearrange_enabled"), &TabContainer::get_drag_to_rearrange_enabled);
1031 ClassDB::bind_method(D_METHOD("set_tabs_rearrange_group", "group_id"), &TabContainer::set_tabs_rearrange_group);
1032 ClassDB::bind_method(D_METHOD("get_tabs_rearrange_group"), &TabContainer::get_tabs_rearrange_group);
1033
1034 ClassDB::bind_method(D_METHOD("set_use_hidden_tabs_for_min_size", "enabled"), &TabContainer::set_use_hidden_tabs_for_min_size);
1035 ClassDB::bind_method(D_METHOD("get_use_hidden_tabs_for_min_size"), &TabContainer::get_use_hidden_tabs_for_min_size);
1036
1037 ClassDB::bind_method(D_METHOD("_child_renamed_callback"), &TabContainer::_child_renamed_callback);
1038 ClassDB::bind_method(D_METHOD("_on_theme_changed"), &TabContainer::_on_theme_changed);
1039 ClassDB::bind_method(D_METHOD("_on_mouse_exited"), &TabContainer::_on_mouse_exited);
1040 ClassDB::bind_method(D_METHOD("_update_current_tab"), &TabContainer::_update_current_tab);
1041
1042 ADD_SIGNAL(MethodInfo("tab_changed", PropertyInfo(Variant::INT, "tab")));
1043 ADD_SIGNAL(MethodInfo("tab_selected", PropertyInfo(Variant::INT, "tab")));
1044 ADD_SIGNAL(MethodInfo("pre_popup_pressed"));
1045
1046 ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_align", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_tab_align", "get_tab_align");
1047 ADD_PROPERTY(PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE, "-1,4096,1", PROPERTY_USAGE_EDITOR), "set_current_tab", "get_current_tab");
1048 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tabs_visible"), "set_tabs_visible", "are_tabs_visible");
1049 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_to_rearrange_enabled"), "set_drag_to_rearrange_enabled", "get_drag_to_rearrange_enabled");
1050 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_hidden_tabs_for_min_size"), "set_use_hidden_tabs_for_min_size", "get_use_hidden_tabs_for_min_size");
1051
1052 BIND_ENUM_CONSTANT(ALIGN_LEFT);
1053 BIND_ENUM_CONSTANT(ALIGN_CENTER);
1054 BIND_ENUM_CONSTANT(ALIGN_RIGHT);
1055 }
1056
TabContainer()1057 TabContainer::TabContainer() {
1058
1059 first_tab_cache = 0;
1060 last_tab_cache = 0;
1061 buttons_visible_cache = false;
1062 menu_hovered = false;
1063 highlight_arrow = -1;
1064 tabs_ofs_cache = 0;
1065 current = 0;
1066 previous = 0;
1067 align = ALIGN_CENTER;
1068 tabs_visible = true;
1069 popup_obj_id = 0;
1070 drag_to_rearrange_enabled = false;
1071 tabs_rearrange_group = -1;
1072 use_hidden_tabs_for_min_size = false;
1073
1074 connect("mouse_exited", this, "_on_mouse_exited");
1075 }
1076