Debian Codegraph @

scene/gui/tree.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* tree.cpp */
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 #include "tree.h"
32 #include "tree.compat.inc"
33
34 #include "core/config/project_settings.h"
35 #include "core/input/input.h"
36 #include "core/math/math_funcs.h"
37 #include "core/os/keyboard.h"
38 #include "core/os/os.h"
39 #include "scene/gui/box_container.h"
40 #include "scene/gui/line_edit.h"
41 #include "scene/gui/popup_menu.h"
42 #include "scene/gui/scroll_bar.h"
43 #include "scene/gui/slider.h"
44 #include "scene/gui/text_edit.h"
45 #include "scene/main/timer.h"
46 #include "scene/main/window.h"
47 #include "scene/theme/theme_db.h"
48
49 #include <climits>
50
51 Size2 TreeItem::Cell::get_icon_size() const {
52 if (icon.is_null()) {
53 return Size2();
54 }
55 if (icon_region == Rect2i()) {
56 return icon->get_size();
57 } else {
58 return icon_region.size;
59 }
60 }
61
62 void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Rect2i &p_region, const Color &p_color) const {
63 if (icon.is_null()) {
64 return;
65 }
66
67 Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size;
68
69 if (p_region == Rect2i()) {
70 icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
71 if (icon_overlay.is_valid()) {
72 Vector2 offset = icon->get_size() - icon_overlay->get_size();
73 icon_overlay->draw_rect_region(p_where, Rect2(p_pos + offset, dsize), Rect2(Point2(), icon_overlay->get_size()), p_color);
74 }
75 } else {
76 icon->draw_rect_region(p_where, Rect2(p_pos, dsize), p_region, p_color);
77 if (icon_overlay.is_valid()) {
78 icon_overlay->draw_rect_region(p_where, Rect2(p_pos, dsize), p_region, p_color);
79 }
80 }
81 }
82
83 void TreeItem::_changed_notify(int p_cell) {
84 if (tree) {
85 tree->item_changed(p_cell, this);
86 }
87 }
88
89 void TreeItem::_changed_notify() {
90 if (tree) {
91 tree->item_changed(-1, this);
92 }
93 }
94
95 void TreeItem::_cell_selected(int p_cell) {
96 if (tree) {
97 tree->item_selected(p_cell, this);
98 }
99 }
100
101 void TreeItem::_cell_deselected(int p_cell) {
102 if (tree) {
103 tree->item_deselected(p_cell, this);
104 }
105 }
106
107 void TreeItem::_change_tree(Tree *p_tree) {
108 if (p_tree == tree) {
109 return;
110 }
111 accessibility_row_dirty = true;
112
113 TreeItem *c = first_child;
114 while (c) {
115 c->_change_tree(p_tree);
116 c = c->next;
117 }
118
119 if (tree) {
120 if (tree->root == this) {
121 tree->root = nullptr;
122 }
123
124 if (tree->popup_edited_item == this) {
125 tree->popup_edited_item = nullptr;
126 tree->popup_pressing_edited_item = nullptr;
127 tree->pressing_for_editor = false;
128 }
129
130 if (tree->cache.hover_item == this) {
131 tree->cache.hover_item = nullptr;
132 }
133
134 if (tree->selected_item == this) {
135 for (int i = 0; i < tree->selected_item->cells.size(); i++) {
136 tree->selected_item->cells.write[i].selected = false;
137 }
138
139 tree->selected_item = nullptr;
140 }
141
142 if (tree->drop_mode_over == this) {
143 tree->_reset_drop_mode_over();
144 }
145
146 if (tree->single_select_defer == this) {
147 tree->single_select_defer = nullptr;
148 }
149
150 if (tree->edited_item == this) {
151 tree->edited_item = nullptr;
152 tree->pressing_for_editor = false;
153 }
154 tree->update_min_size_for_item_change();
155 tree->queue_accessibility_update();
156 tree->queue_redraw();
157 }
158
159 tree = p_tree;
160
161 if (accessibility_row_element.is_valid()) {
162 DisplayServer::get_singleton()->accessibility_free_element(accessibility_row_element);
163 accessibility_row_element = RID();
164 }
165 for (Cell &cell : cells) {
166 if (cell.accessibility_cell_element.is_valid()) {
167 DisplayServer::get_singleton()->accessibility_free_element(cell.accessibility_cell_element);
168 cell.accessibility_cell_element = RID();
169 }
170 for (Cell::Button &btn : cell.buttons) {
171 if (btn.accessibility_button_element.is_valid()) {
172 DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
173 btn.accessibility_button_element = RID();
174 }
175 }
176 }
177
178 if (tree) {
179 tree->queue_accessibility_update();
180 tree->queue_redraw();
181 cells.resize(tree->columns.size());
182 }
183 }
184
185 void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) {
186 ERR_FAIL_INDEX(p_column, cells.size());
187
188 if (cells[p_column].mode == p_mode) {
189 return;
190 }
191
192 Cell &c = cells.write[p_column];
193 c.mode = p_mode;
194 c.min = 0;
195 c.max = 100;
196 c.step = 1;
197 c.val = 0;
198 c.checked = false;
199 c.icon = Ref<Texture2D>();
200 c.text = "";
201 c.dirty = true;
202 c.icon_max_w = 0;
203 c.cached_minimum_size_dirty = true;
204
205 _changed_notify(p_column);
206 }
207
208 TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
209 ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING);
210 return cells[p_column].mode;
211 }
212
213 void TreeItem::set_auto_translate_mode(int p_column, Node::AutoTranslateMode p_mode) {
214 ERR_FAIL_INDEX(p_column, cells.size());
215
216 if (cells[p_column].auto_translate_mode == p_mode) {
217 return;
218 }
219
220 cells.write[p_column].auto_translate_mode = p_mode;
221 cells.write[p_column].dirty = true;
222 cells.write[p_column].cached_minimum_size_dirty = true;
223
224 _changed_notify(p_column);
225 }
226
227 Node::AutoTranslateMode TreeItem::get_auto_translate_mode(int p_column) const {
228 ERR_FAIL_INDEX_V(p_column, cells.size(), Node::AUTO_TRANSLATE_MODE_INHERIT);
229 return cells[p_column].auto_translate_mode;
230 }
231
232 void TreeItem::set_edit_multiline(int p_column, bool p_multiline) {
233 ERR_FAIL_INDEX(p_column, cells.size());
234 cells.write[p_column].edit_multiline = p_multiline;
235 _changed_notify(p_column);
236 }
237
238 bool TreeItem::is_edit_multiline(int p_column) const {
239 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
240 return cells[p_column].edit_multiline;
241 }
242
243 void TreeItem::set_checked(int p_column, bool p_checked) {
244 ERR_FAIL_INDEX(p_column, cells.size());
245
246 if (cells[p_column].checked == p_checked) {
247 return;
248 }
249
250 cells.write[p_column].checked = p_checked;
251 cells.write[p_column].indeterminate = false;
252 cells.write[p_column].cached_minimum_size_dirty = true;
253
254 _changed_notify(p_column);
255 }
256
257 void TreeItem::set_indeterminate(int p_column, bool p_indeterminate) {
258 ERR_FAIL_INDEX(p_column, cells.size());
259
260 // Prevent uncheck if indeterminate set to false twice.
261 if (p_indeterminate == cells[p_column].indeterminate) {
262 return;
263 }
264
265 cells.write[p_column].indeterminate = p_indeterminate;
266 cells.write[p_column].checked = false;
267 cells.write[p_column].cached_minimum_size_dirty = true;
268
269 _changed_notify(p_column);
270 }
271
272 bool TreeItem::is_checked(int p_column) const {
273 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
274 return cells[p_column].checked;
275 }
276
277 bool TreeItem::is_indeterminate(int p_column) const {
278 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
279 return cells[p_column].indeterminate;
280 }
281
282 void TreeItem::propagate_check(int p_column, bool p_emit_signal) {
283 bool ch = cells[p_column].checked;
284
285 if (p_emit_signal) {
286 tree->emit_signal(SNAME("check_propagated_to_item"), this, p_column);
287 }
288 _propagate_check_through_children(p_column, ch, p_emit_signal);
289 _propagate_check_through_parents(p_column, p_emit_signal);
290 }
291
292 String TreeItem::atr(int p_column, const String &p_text) const {
293 ERR_FAIL_INDEX_V(p_column, cells.size(), tree->atr(p_text));
294
295 switch (cells[p_column].auto_translate_mode) {
296 case Node::AUTO_TRANSLATE_MODE_INHERIT: {
297 return tree->atr(p_text);
298 } break;
299 case Node::AUTO_TRANSLATE_MODE_ALWAYS: {
300 return tree->tr(p_text);
301 } break;
302 case Node::AUTO_TRANSLATE_MODE_DISABLED: {
303 return p_text;
304 } break;
305 }
306
307 ERR_FAIL_V_MSG(tree->atr(p_text), "Unexpected auto translate mode: " + itos(cells[p_column].auto_translate_mode));
308 }
309
310 void TreeItem::_propagate_check_through_children(int p_column, bool p_checked, bool p_emit_signal) {
311 TreeItem *current = get_first_child();
312 while (current) {
313 current->set_checked(p_column, p_checked);
314 if (p_emit_signal) {
315 current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
316 }
317 current->_propagate_check_through_children(p_column, p_checked, p_emit_signal);
318 current = current->get_next();
319 }
320 }
321
322 void TreeItem::_propagate_check_through_parents(int p_column, bool p_emit_signal) {
323 TreeItem *current = get_parent();
324 if (!current) {
325 return;
326 }
327
328 bool any_checked = false;
329 bool any_unchecked = false;
330 bool any_indeterminate = false;
331
332 TreeItem *child_item = current->get_first_child();
333 while (child_item) {
334 if (!child_item->is_checked(p_column)) {
335 any_unchecked = true;
336 if (child_item->is_indeterminate(p_column)) {
337 any_indeterminate = true;
338 break;
339 }
340 } else {
341 any_checked = true;
342 }
343 child_item = child_item->get_next();
344 }
345
346 if (any_indeterminate || (any_checked && any_unchecked)) {
347 current->set_indeterminate(p_column, true);
348 } else if (current->is_indeterminate(p_column) && !any_checked) {
349 current->set_indeterminate(p_column, false);
350 } else {
351 current->set_checked(p_column, any_checked);
352 }
353
354 if (p_emit_signal) {
355 current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
356 }
357 current->_propagate_check_through_parents(p_column, p_emit_signal);
358 }
359
360 void TreeItem::set_text(int p_column, String p_text) {
361 ERR_FAIL_INDEX(p_column, cells.size());
362
363 if (cells[p_column].text == p_text) {
364 return;
365 }
366
367 cells.write[p_column].text = p_text;
368 cells.write[p_column].dirty = true;
369
370 if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE) {
371 Vector<String> strings = p_text.split(",");
372 cells.write[p_column].min = INT_MAX;
373 cells.write[p_column].max = INT_MIN;
374 for (int i = 0; i < strings.size(); i++) {
375 int value = i;
376 if (!strings[i].get_slicec(':', 1).is_empty()) {
377 value = strings[i].get_slicec(':', 1).to_int();
378 }
379 cells.write[p_column].min = MIN(cells[p_column].min, value);
380 cells.write[p_column].max = MAX(cells[p_column].max, value);
381 }
382 cells.write[p_column].step = 0;
383 } else {
384 // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
385 if (tree && (!cells[p_column].editable || cells[p_column].mode != TreeItem::CELL_MODE_STRING)) {
386 cells.write[p_column].xl_text = atr(p_column, p_text);
387 } else {
388 cells.write[p_column].xl_text = p_text;
389 }
390 }
391
392 cells.write[p_column].cached_minimum_size_dirty = true;
393
394 _changed_notify(p_column);
395 if (get_tree()) {
396 get_tree()->update_configuration_warnings();
397 }
398 }
399
400 String TreeItem::get_text(int p_column) const {
401 ERR_FAIL_INDEX_V(p_column, cells.size(), "");
402 return cells[p_column].text;
403 }
404
405 void TreeItem::set_description(int p_column, String p_text) {
406 ERR_FAIL_INDEX(p_column, cells.size());
407
408 if (cells[p_column].description == p_text) {
409 return;
410 }
411
412 cells.write[p_column].description = p_text;
413
414 _changed_notify(p_column);
415 if (get_tree()) {
416 get_tree()->update_configuration_warnings();
417 }
418 }
419
420 String TreeItem::get_description(int p_column) const {
421 ERR_FAIL_INDEX_V(p_column, cells.size(), "");
422 return cells[p_column].description;
423 }
424
425 void TreeItem::set_text_direction(int p_column, Control::TextDirection p_text_direction) {
426 ERR_FAIL_INDEX(p_column, cells.size());
427 ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
428
429 if (cells[p_column].text_direction == p_text_direction) {
430 return;
431 }
432
433 cells.write[p_column].text_direction = p_text_direction;
434 cells.write[p_column].dirty = true;
435 _changed_notify(p_column);
436 cells.write[p_column].cached_minimum_size_dirty = true;
437 }
438
439 Control::TextDirection TreeItem::get_text_direction(int p_column) const {
440 ERR_FAIL_INDEX_V(p_column, cells.size(), Control::TEXT_DIRECTION_INHERITED);
441 return cells[p_column].text_direction;
442 }
443
444 void TreeItem::set_autowrap_mode(int p_column, TextServer::AutowrapMode p_mode) {
445 ERR_FAIL_INDEX(p_column, cells.size());
446 ERR_FAIL_COND(p_mode < TextServer::AUTOWRAP_OFF || p_mode > TextServer::AUTOWRAP_WORD_SMART);
447
448 if (cells[p_column].autowrap_mode == p_mode) {
449 return;
450 }
451
452 cells.write[p_column].autowrap_mode = p_mode;
453 cells.write[p_column].dirty = true;
454 _changed_notify(p_column);
455 cells.write[p_column].cached_minimum_size_dirty = true;
456 }
457
458 TextServer::AutowrapMode TreeItem::get_autowrap_mode(int p_column) const {
459 ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::AUTOWRAP_OFF);
460 return cells[p_column].autowrap_mode;
461 }
462
463 void TreeItem::set_text_overrun_behavior(int p_column, TextServer::OverrunBehavior p_behavior) {
464 ERR_FAIL_INDEX(p_column, cells.size());
465
466 if (cells[p_column].text_buf->get_text_overrun_behavior() == p_behavior) {
467 return;
468 }
469
470 cells.write[p_column].text_buf->set_text_overrun_behavior(p_behavior);
471 cells.write[p_column].dirty = true;
472 cells.write[p_column].cached_minimum_size_dirty = true;
473 _changed_notify(p_column);
474 }
475
476 TextServer::OverrunBehavior TreeItem::get_text_overrun_behavior(int p_column) const {
477 ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::OVERRUN_TRIM_ELLIPSIS);
478 return cells[p_column].text_buf->get_text_overrun_behavior();
479 }
480
481 void TreeItem::set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser) {
482 ERR_FAIL_INDEX(p_column, cells.size());
483
484 if (cells[p_column].st_parser != p_parser) {
485 cells.write[p_column].st_parser = p_parser;
486 cells.write[p_column].dirty = true;
487 cells.write[p_column].cached_minimum_size_dirty = true;
488
489 _changed_notify(p_column);
490 }
491 }
492
493 TextServer::StructuredTextParser TreeItem::get_structured_text_bidi_override(int p_column) const {
494 ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::STRUCTURED_TEXT_DEFAULT);
495 return cells[p_column].st_parser;
496 }
497
498 void TreeItem::set_structured_text_bidi_override_options(int p_column, const Array &p_args) {
499 ERR_FAIL_INDEX(p_column, cells.size());
500
501 if (cells[p_column].st_args == p_args) {
502 return;
503 }
504
505 cells.write[p_column].st_args = Array(p_args);
506 cells.write[p_column].dirty = true;
507 cells.write[p_column].cached_minimum_size_dirty = true;
508
509 _changed_notify(p_column);
510 }
511
512 Array TreeItem::get_structured_text_bidi_override_options(int p_column) const {
513 ERR_FAIL_INDEX_V(p_column, cells.size(), Array());
514 return Array(cells[p_column].st_args);
515 }
516
517 void TreeItem::set_language(int p_column, const String &p_language) {
518 ERR_FAIL_INDEX(p_column, cells.size());
519
520 if (cells[p_column].language != p_language) {
521 cells.write[p_column].language = p_language;
522 cells.write[p_column].dirty = true;
523 cells.write[p_column].cached_minimum_size_dirty = true;
524
525 _changed_notify(p_column);
526 }
527 }
528
529 String TreeItem::get_language(int p_column) const {
530 ERR_FAIL_INDEX_V(p_column, cells.size(), "");
531 return cells[p_column].language;
532 }
533
534 void TreeItem::set_suffix(int p_column, String p_suffix) {
535 ERR_FAIL_INDEX(p_column, cells.size());
536
537 if (cells[p_column].suffix == p_suffix) {
538 return;
539 }
540
541 cells.write[p_column].suffix = p_suffix;
542 cells.write[p_column].cached_minimum_size_dirty = true;
543
544 _changed_notify(p_column);
545 }
546
547 String TreeItem::get_suffix(int p_column) const {
548 ERR_FAIL_INDEX_V(p_column, cells.size(), "");
549 return cells[p_column].suffix;
550 }
551
552 void TreeItem::set_icon(int p_column, const Ref<Texture2D> &p_icon) {
553 ERR_FAIL_INDEX(p_column, cells.size());
554
555 if (cells[p_column].icon == p_icon) {
556 return;
557 }
558
559 cells.write[p_column].icon = p_icon;
560 cells.write[p_column].cached_minimum_size_dirty = true;
561
562 _changed_notify(p_column);
563 }
564
565 Ref<Texture2D> TreeItem::get_icon(int p_column) const {
566 ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
567 return cells[p_column].icon;
568 }
569
570 void TreeItem::set_icon_overlay(int p_column, const Ref<Texture2D> &p_icon_overlay) {
571 ERR_FAIL_INDEX(p_column, cells.size());
572
573 if (cells[p_column].icon_overlay == p_icon_overlay) {
574 return;
575 }
576
577 cells.write[p_column].icon_overlay = p_icon_overlay;
578 cells.write[p_column].cached_minimum_size_dirty = true;
579
580 _changed_notify(p_column);
581 }
582
583 Ref<Texture2D> TreeItem::get_icon_overlay(int p_column) const {
584 ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
585 return cells[p_column].icon_overlay;
586 }
587
588 void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
589 ERR_FAIL_INDEX(p_column, cells.size());
590
591 if (cells[p_column].icon_region == p_icon_region) {
592 return;
593 }
594
595 cells.write[p_column].icon_region = p_icon_region;
596 cells.write[p_column].cached_minimum_size_dirty = true;
597
598 _changed_notify(p_column);
599 }
600
601 Rect2 TreeItem::get_icon_region(int p_column) const {
602 ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2());
603 return cells[p_column].icon_region;
604 }
605
606 void TreeItem::set_icon_modulate(int p_column, const Color &p_modulate) {
607 ERR_FAIL_INDEX(p_column, cells.size());
608
609 if (cells[p_column].icon_color == p_modulate) {
610 return;
611 }
612
613 cells.write[p_column].icon_color = p_modulate;
614 _changed_notify(p_column);
615 }
616
617 Color TreeItem::get_icon_modulate(int p_column) const {
618 ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
619 return cells[p_column].icon_color;
620 }
621
622 void TreeItem::set_icon_max_width(int p_column, int p_max) {
623 ERR_FAIL_INDEX(p_column, cells.size());
624
625 if (cells[p_column].icon_max_w == p_max) {
626 return;
627 }
628
629 cells.write[p_column].icon_max_w = p_max;
630 cells.write[p_column].cached_minimum_size_dirty = true;
631
632 _changed_notify(p_column);
633 }
634
635 int TreeItem::get_icon_max_width(int p_column) const {
636 ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
637 return cells[p_column].icon_max_w;
638 }
639
640 void TreeItem::set_range(int p_column, double p_value) {
641 ERR_FAIL_INDEX(p_column, cells.size());
642 if (cells[p_column].step > 0) {
643 p_value = Math::snapped(p_value, cells[p_column].step);
644 }
645 if (p_value < cells[p_column].min) {
646 p_value = cells[p_column].min;
647 }
648 if (p_value > cells[p_column].max) {
649 p_value = cells[p_column].max;
650 }
651
652 if (cells[p_column].val == p_value) {
653 return;
654 }
655
656 cells.write[p_column].val = p_value;
657 cells.write[p_column].dirty = true;
658 _changed_notify(p_column);
659 }
660
661 double TreeItem::get_range(int p_column) const {
662 ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
663 return cells[p_column].val;
664 }
665
666 bool TreeItem::is_range_exponential(int p_column) const {
667 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
668 return cells[p_column].expr;
669 }
670
671 void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) {
672 ERR_FAIL_INDEX(p_column, cells.size());
673
674 if (cells[p_column].min == p_min && cells[p_column].max == p_max && cells[p_column].step == p_step && cells[p_column].expr == p_exp) {
675 return;
676 }
677
678 cells.write[p_column].min = p_min;
679 cells.write[p_column].max = p_max;
680 cells.write[p_column].step = p_step;
681 cells.write[p_column].expr = p_exp;
682 _changed_notify(p_column);
683 }
684
685 void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const {
686 ERR_FAIL_INDEX(p_column, cells.size());
687 r_min = cells[p_column].min;
688 r_max = cells[p_column].max;
689 r_step = cells[p_column].step;
690 }
691
692 void TreeItem::set_metadata(int p_column, const Variant &p_meta) {
693 ERR_FAIL_INDEX(p_column, cells.size());
694 cells.write[p_column].meta = p_meta;
695 }
696
697 Variant TreeItem::get_metadata(int p_column) const {
698 ERR_FAIL_INDEX_V(p_column, cells.size(), Variant());
699
700 return cells[p_column].meta;
701 }
702
703 #ifndef DISABLE_DEPRECATED
704 void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
705 WARN_DEPRECATED_MSG(R"*(The "set_custom_draw()" method is deprecated, use "set_custom_draw_callback()" instead.)*");
706 ERR_FAIL_INDEX(p_column, cells.size());
707 ERR_FAIL_NULL(p_object);
708
709 cells.write[p_column].custom_draw_callback = Callable(p_object, p_callback);
710
711 _changed_notify(p_column);
712 }
713 #endif // DISABLE_DEPRECATED
714
715 void TreeItem::set_custom_draw_callback(int p_column, const Callable &p_callback) {
716 ERR_FAIL_INDEX(p_column, cells.size());
717
718 cells.write[p_column].custom_draw_callback = p_callback;
719
720 _changed_notify(p_column);
721 }
722
723 Callable TreeItem::get_custom_draw_callback(int p_column) const {
724 ERR_FAIL_INDEX_V(p_column, cells.size(), Callable());
725
726 return cells[p_column].custom_draw_callback;
727 }
728
729 void TreeItem::set_collapsed(bool p_collapsed) {
730 if (collapsed == p_collapsed || !tree) {
731 return;
732 }
733 collapsed = p_collapsed;
734 TreeItem *ci = tree->selected_item;
735 if (ci) {
736 while (ci && ci != this) {
737 ci = ci->parent;
738 }
739 if (ci) { // Collapsing cursor/selected, move it!
740
741 if (tree->select_mode == Tree::SELECT_MULTI) {
742 tree->selected_item = this;
743 emit_signal(SNAME("cell_selected"));
744 } else {
745 select(tree->selected_col);
746 }
747
748 tree->queue_accessibility_update();
749 tree->queue_redraw();
750 }
751 }
752
753 _changed_notify();
754 tree->emit_signal(SNAME("item_collapsed"), this);
755 }
756
757 bool TreeItem::is_collapsed() {
758 return collapsed;
759 }
760
761 void TreeItem::set_collapsed_recursive(bool p_collapsed) {
762 if (!tree) {
763 return;
764 }
765
766 set_collapsed(p_collapsed);
767
768 TreeItem *child = get_first_child();
769 while (child) {
770 child->set_collapsed_recursive(p_collapsed);
771 child = child->get_next();
772 }
773 }
774
775 bool TreeItem::_is_any_collapsed(bool p_only_visible) {
776 TreeItem *child = get_first_child();
777
778 // Check on children directly first (avoid recursing if possible).
779 while (child) {
780 if (child->get_first_child() && child->is_collapsed() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count()))) {
781 return true;
782 }
783 child = child->get_next();
784 }
785
786 child = get_first_child();
787
788 // Otherwise recurse on children.
789 while (child) {
790 if (child->get_first_child() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count())) && child->_is_any_collapsed(p_only_visible)) {
791 return true;
792 }
793 child = child->get_next();
794 }
795
796 return false;
797 }
798
799 bool TreeItem::is_any_collapsed(bool p_only_visible) {
800 if (p_only_visible && !is_visible_in_tree()) {
801 return false;
802 }
803
804 // Collapsed if this is collapsed and it has children (only considers visible if only visible is set).
805 if (is_collapsed() && get_first_child() && (!p_only_visible || get_visible_child_count())) {
806 return true;
807 }
808
809 return _is_any_collapsed(p_only_visible);
810 }
811
812 void TreeItem::set_visible(bool p_visible) {
813 if (visible == p_visible) {
814 return;
815 }
816 visible = p_visible;
817 if (tree) {
818 if (!visible) {
819 for (int i = 0; i < tree->columns.size(); i++) {
820 if (cells[i].selected) {
821 deselect(i);
822 }
823 }
824 }
825
826 tree->queue_accessibility_update();
827 tree->queue_redraw();
828 _changed_notify();
829 }
830
831 _handle_visibility_changed(p_visible);
832 }
833
834 bool TreeItem::is_visible() {
835 return visible;
836 }
837
838 bool TreeItem::is_visible_in_tree() const {
839 return visible && parent_visible_in_tree;
840 }
841
842 void TreeItem::_handle_visibility_changed(bool p_visible) {
843 TreeItem *child = get_first_child();
844 while (child) {
845 child->_propagate_visibility_changed(p_visible);
846 child = child->get_next();
847 }
848 }
849
850 void TreeItem::_propagate_visibility_changed(bool p_parent_visible_in_tree) {
851 parent_visible_in_tree = p_parent_visible_in_tree;
852 _handle_visibility_changed(p_parent_visible_in_tree);
853 }
854
855 void TreeItem::uncollapse_tree() {
856 TreeItem *t = this;
857 while (t) {
858 t->set_collapsed(false);
859 t = t->parent;
860 }
861 }
862
863 void TreeItem::set_custom_minimum_height(int p_height) {
864 if (custom_min_height == p_height) {
865 return;
866 }
867
868 custom_min_height = p_height;
869
870 for (Cell &c : cells) {
871 c.cached_minimum_size_dirty = true;
872 }
873
874 _changed_notify();
875 }
876
877 int TreeItem::get_custom_minimum_height() const {
878 return custom_min_height;
879 }
880
881 void TreeItem::set_custom_stylebox(int p_column, const Ref<StyleBox> &p_stylebox) {
882 ERR_FAIL_INDEX(p_column, cells.size());
883 cells.write[p_column].custom_stylebox = p_stylebox;
884 _changed_notify(p_column);
885 }
886
887 Ref<StyleBox> TreeItem::get_custom_stylebox(int p_column) const {
888 ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<StyleBox>());
889 return cells[p_column].custom_stylebox;
890 }
891
892 TreeItem *TreeItem::create_child(int p_index) {
893 TreeItem *ti = memnew(TreeItem(tree));
894 if (tree) {
895 ti->cells.resize(tree->columns.size());
896 tree->update_min_size_for_item_change();
897 tree->queue_accessibility_update();
898 tree->queue_redraw();
899 }
900
901 TreeItem *item_prev = nullptr;
902 TreeItem *item_next = first_child;
903
904 if (p_index < 0 && last_child) {
905 item_prev = last_child;
906 } else {
907 int idx = 0;
908 if (!children_cache.is_empty()) {
909 idx = MIN(int(children_cache.size()) - 1, p_index);
910 item_next = children_cache[idx];
911 item_prev = item_next->prev;
912 }
913 while (item_next) {
914 if (idx == p_index) {
915 item_next->prev = ti;
916 ti->next = item_next;
917 break;
918 }
919
920 item_prev = item_next;
921 item_next = item_next->next;
922 idx++;
923 }
924 }
925
926 if (item_prev) {
927 item_prev->next = ti;
928 ti->prev = item_prev;
929
930 if (!children_cache.is_empty()) {
931 if (ti->next) {
932 children_cache.insert(p_index, ti);
933 } else {
934 children_cache.push_back(ti);
935 }
936 }
937 } else {
938 first_child = ti;
939 children_cache.insert(0, ti);
940 }
941
942 if (item_prev == last_child) {
943 last_child = ti;
944 }
945
946 ti->parent = this;
947 ti->parent_visible_in_tree = is_visible_in_tree();
948
949 return ti;
950 }
951
952 void TreeItem::add_child(TreeItem *p_item) {
953 ERR_FAIL_NULL(p_item);
954 ERR_FAIL_COND(p_item->tree);
955 ERR_FAIL_COND(p_item->parent);
956
957 p_item->_change_tree(tree);
958 p_item->parent = this;
959 p_item->parent_visible_in_tree = is_visible_in_tree();
960 p_item->_handle_visibility_changed(p_item->parent_visible_in_tree);
961
962 if (last_child) {
963 last_child->next = p_item;
964 p_item->prev = last_child;
965 } else {
966 first_child = p_item;
967 }
968 last_child = p_item;
969
970 if (!children_cache.is_empty()) {
971 children_cache.push_back(p_item);
972 }
973
974 validate_cache();
975 }
976
977 void TreeItem::remove_child(TreeItem *p_item) {
978 ERR_FAIL_NULL(p_item);
979 ERR_FAIL_COND(p_item->parent != this);
980
981 p_item->_unlink_from_tree();
982 p_item->_change_tree(nullptr);
983 p_item->prev = nullptr;
984 p_item->next = nullptr;
985 p_item->parent = nullptr;
986
987 validate_cache();
988 }
989
990 Tree *TreeItem::get_tree() const {
991 return tree;
992 }
993
994 TreeItem *TreeItem::get_next() const {
995 return next;
996 }
997
998 TreeItem *TreeItem::get_prev() {
999 if (prev) {
1000 return prev;
1001 }
1002
1003 if (!parent || parent->first_child == this) {
1004 return nullptr;
1005 }
1006 // This is an edge case.
1007 TreeItem *l_prev = parent->first_child;
1008 while (l_prev && l_prev->next != this) {
1009 l_prev = l_prev->next;
1010 }
1011
1012 prev = l_prev;
1013
1014 return prev;
1015 }
1016
1017 TreeItem *TreeItem::get_parent() const {
1018 return parent;
1019 }
1020
1021 TreeItem *TreeItem::get_first_child() const {
1022 return first_child;
1023 }
1024
1025 TreeItem *TreeItem::_get_prev_in_tree(bool p_wrap, bool p_include_invisible) {
1026 TreeItem *current = this;
1027
1028 TreeItem *prev_item = current->get_prev();
1029
1030 if (!prev_item) {
1031 current = current->parent;
1032 if (!current || (current == tree->root && tree->hide_root)) {
1033 if (!p_wrap) {
1034 return nullptr;
1035 }
1036 // Wrap around to the last visible item.
1037 current = this;
1038 TreeItem *temp = get_next_visible();
1039 while (temp) {
1040 current = temp;
1041 temp = temp->get_next_visible();
1042 }
1043 }
1044 } else {
1045 current = prev_item;
1046 while ((!current->collapsed || p_include_invisible) && current->last_child) {
1047 current = current->last_child;
1048 }
1049 }
1050
1051 return current;
1052 }
1053
1054 TreeItem *TreeItem::get_prev_visible(bool p_wrap) {
1055 TreeItem *loop = this;
1056 TreeItem *prev_item = _get_prev_in_tree(p_wrap);
1057 while (prev_item && !prev_item->is_visible_in_tree()) {
1058 prev_item = prev_item->_get_prev_in_tree(p_wrap);
1059 if (prev_item == loop) {
1060 // Check that we haven't looped all the way around to the start.
1061 prev_item = nullptr;
1062 break;
1063 }
1064 }
1065 return prev_item;
1066 }
1067
1068 TreeItem *TreeItem::_get_next_in_tree(bool p_wrap, bool p_include_invisible) {
1069 TreeItem *current = this;
1070
1071 if ((!current->collapsed || p_include_invisible) && current->first_child) {
1072 current = current->first_child;
1073
1074 } else if (current->next) {
1075 current = current->next;
1076 } else {
1077 while (current && !current->next) {
1078 current = current->parent;
1079 }
1080
1081 if (!current) {
1082 if (p_wrap) {
1083 return tree->root;
1084 } else {
1085 return nullptr;
1086 }
1087 } else {
1088 current = current->next;
1089 }
1090 }
1091
1092 return current;
1093 }
1094
1095 TreeItem *TreeItem::get_next_visible(bool p_wrap) {
1096 TreeItem *loop = this;
1097 TreeItem *next_item = _get_next_in_tree(p_wrap);
1098 while (next_item && !next_item->is_visible_in_tree()) {
1099 next_item = next_item->_get_next_in_tree(p_wrap);
1100 if (next_item == loop) {
1101 // Check that we haven't looped all the way around to the start.
1102 next_item = nullptr;
1103 break;
1104 }
1105 }
1106 return next_item;
1107 }
1108
1109 TreeItem *TreeItem::get_prev_in_tree(bool p_wrap) {
1110 TreeItem *prev_item = _get_prev_in_tree(p_wrap, true);
1111 return prev_item;
1112 }
1113
1114 TreeItem *TreeItem::get_next_in_tree(bool p_wrap) {
1115 TreeItem *next_item = _get_next_in_tree(p_wrap, true);
1116 return next_item;
1117 }
1118
1119 TreeItem *TreeItem::get_child(int p_index) {
1120 _create_children_cache();
1121
1122 if (p_index < 0) {
1123 p_index += children_cache.size();
1124 }
1125 ERR_FAIL_INDEX_V(p_index, (int)children_cache.size(), nullptr);
1126
1127 return children_cache[p_index];
1128 }
1129
1130 int TreeItem::get_visible_child_count() {
1131 _create_children_cache();
1132 int visible_count = 0;
1133 for (uint32_t i = 0; i < children_cache.size(); i++) {
1134 if (children_cache[i]->is_visible()) {
1135 visible_count += 1;
1136 }
1137 }
1138 return visible_count;
1139 }
1140
1141 int TreeItem::get_child_count() {
1142 _create_children_cache();
1143 return children_cache.size();
1144 }
1145
1146 TypedArray<TreeItem> TreeItem::get_children() {
1147 // Don't need to explicitly create children cache, because get_child_count creates it.
1148 int size = get_child_count();
1149 TypedArray<TreeItem> arr;
1150 arr.resize(size);
1151 for (int i = 0; i < size; i++) {
1152 arr[i] = children_cache[i];
1153 }
1154
1155 return arr;
1156 }
1157
1158 void TreeItem::clear_children() {
1159 TreeItem *c = first_child;
1160 while (c) {
1161 TreeItem *aux = c;
1162 c = c->get_next();
1163 aux->parent = nullptr; // So it won't try to recursively auto-remove from me in here.
1164 memdelete(aux);
1165 }
1166
1167 first_child = nullptr;
1168 last_child = nullptr;
1169 children_cache.clear();
1170 }
1171
1172 int TreeItem::get_index() {
1173 int idx = 0;
1174 TreeItem *c = this;
1175
1176 while (c) {
1177 c = c->get_prev();
1178 idx++;
1179 }
1180 return idx - 1;
1181 }
1182
1183 #ifdef DEV_ENABLED
1184 void TreeItem::validate_cache() const {
1185 if (!parent || parent->children_cache.is_empty()) {
1186 return;
1187 }
1188 TreeItem *scan = parent->first_child;
1189 uint32_t index = 0;
1190 while (scan) {
1191 DEV_ASSERT(parent->children_cache[index] == scan);
1192 ++index;
1193 scan = scan->get_next();
1194 }
1195 DEV_ASSERT(index == parent->children_cache.size());
1196 }
1197 #endif
1198
1199 void TreeItem::move_before(TreeItem *p_item) {
1200 ERR_FAIL_NULL(p_item);
1201 ERR_FAIL_COND(is_root);
1202 ERR_FAIL_NULL(p_item->parent);
1203
1204 if (p_item == this) {
1205 return;
1206 }
1207
1208 TreeItem *p = p_item->parent;
1209 while (p) {
1210 ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
1211 p = p->parent;
1212 }
1213
1214 Tree *old_tree = tree;
1215 _unlink_from_tree();
1216 _change_tree(p_item->tree);
1217
1218 parent = p_item->parent;
1219
1220 TreeItem *item_prev = p_item->get_prev();
1221 if (item_prev) {
1222 item_prev->next = this;
1223 parent->children_cache.clear();
1224 } else {
1225 parent->first_child = this;
1226 // If the cache is empty, it has not been built but there
1227 // are items in the tree (note p_item != nullptr) so we cannot update it.
1228 if (!parent->children_cache.is_empty()) {
1229 parent->children_cache.insert(0, this);
1230 }
1231 }
1232
1233 prev = item_prev;
1234 next = p_item;
1235 p_item->prev = this;
1236
1237 if (tree && old_tree == tree) {
1238 tree->queue_accessibility_update();
1239 tree->queue_redraw();
1240 }
1241
1242 validate_cache();
1243 }
1244
1245 void TreeItem::move_after(TreeItem *p_item) {
1246 ERR_FAIL_NULL(p_item);
1247 ERR_FAIL_COND(is_root);
1248 ERR_FAIL_NULL(p_item->parent);
1249
1250 if (p_item == this) {
1251 return;
1252 }
1253
1254 TreeItem *p = p_item->parent;
1255 while (p) {
1256 ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
1257 p = p->parent;
1258 }
1259
1260 Tree *old_tree = tree;
1261 _unlink_from_tree();
1262 _change_tree(p_item->tree);
1263
1264 if (p_item->next) {
1265 p_item->next->prev = this;
1266 }
1267 parent = p_item->parent;
1268 prev = p_item;
1269 next = p_item->next;
1270 p_item->next = this;
1271
1272 if (next) {
1273 parent->children_cache.clear();
1274 } else {
1275 parent->last_child = this;
1276 // If the cache is empty, it has not been built but there
1277 // are items in the tree (note p_item != nullptr,) so we cannot update it.
1278 if (!parent->children_cache.is_empty()) {
1279 parent->children_cache.push_back(this);
1280 }
1281 }
1282
1283 if (tree && old_tree == tree) {
1284 tree->queue_accessibility_update();
1285 tree->queue_redraw();
1286 }
1287 validate_cache();
1288 }
1289
1290 void TreeItem::set_selectable(int p_column, bool p_selectable) {
1291 ERR_FAIL_INDEX(p_column, cells.size());
1292 cells.write[p_column].selectable = p_selectable;
1293 }
1294
1295 bool TreeItem::is_selectable(int p_column) const {
1296 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
1297 return cells[p_column].selectable;
1298 }
1299
1300 bool TreeItem::is_selected(int p_column) {
1301 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
1302 return cells[p_column].selectable && cells[p_column].selected;
1303 }
1304
1305 bool TreeItem::is_any_column_selected() const {
1306 for (const Cell &cell : cells) {
1307 if (cell.selectable && cell.selected) {
1308 return true;
1309 }
1310 }
1311 return false;
1312 }
1313
1314 void TreeItem::set_as_cursor(int p_column) {
1315 ERR_FAIL_INDEX(p_column, cells.size());
1316 if (!tree) {
1317 return;
1318 }
1319 if (tree->select_mode != Tree::SELECT_MULTI) {
1320 return;
1321 }
1322 if (tree->selected_item == this && tree->selected_col == p_column) {
1323 return;
1324 }
1325 tree->selected_item = this;
1326 tree->selected_col = p_column;
1327 tree->selected_button = -1;
1328 tree->queue_accessibility_update();
1329 tree->queue_redraw();
1330 }
1331
1332 void TreeItem::select(int p_column) {
1333 ERR_FAIL_INDEX(p_column, cells.size());
1334 _cell_selected(p_column);
1335 }
1336
1337 void TreeItem::deselect(int p_column) {
1338 ERR_FAIL_INDEX(p_column, cells.size());
1339 _cell_deselected(p_column);
1340 }
1341
1342 void TreeItem::clear_buttons() {
1343 int i = 0;
1344 for (Cell &cell : cells) {
1345 if (!cell.buttons.is_empty()) {
1346 for (Cell::Button &btn : cell.buttons) {
1347 if (btn.accessibility_button_element.is_valid()) {
1348 DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
1349 }
1350 }
1351 cell.buttons.clear();
1352 cell.cached_minimum_size_dirty = true;
1353 _changed_notify(i);
1354 }
1355 ++i;
1356 }
1357 }
1358
1359 void TreeItem::add_button(int p_column, const Ref<Texture2D> &p_button, int p_id, bool p_disabled, const String &p_tooltip, const String &p_description) {
1360 ERR_FAIL_INDEX(p_column, cells.size());
1361 ERR_FAIL_COND(p_button.is_null());
1362 TreeItem::Cell::Button button;
1363 button.texture = p_button;
1364 if (p_id < 0) {
1365 p_id = cells[p_column].buttons.size();
1366 }
1367 button.id = p_id;
1368 button.disabled = p_disabled;
1369 button.tooltip = p_tooltip;
1370 button.description = p_description;
1371 cells.write[p_column].buttons.push_back(button);
1372 cells.write[p_column].cached_minimum_size_dirty = true;
1373
1374 _changed_notify(p_column);
1375 if (get_tree()) {
1376 get_tree()->update_configuration_warnings();
1377 }
1378 }
1379
1380 int TreeItem::get_button_count(int p_column) const {
1381 ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
1382 return cells[p_column].buttons.size();
1383 }
1384
1385 Ref<Texture2D> TreeItem::get_button(int p_column, int p_index) const {
1386 ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
1387 ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Ref<Texture2D>());
1388 return cells[p_column].buttons[p_index].texture;
1389 }
1390
1391 String TreeItem::get_button_tooltip_text(int p_column, int p_index) const {
1392 ERR_FAIL_INDEX_V(p_column, cells.size(), String());
1393 ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), String());
1394 return cells[p_column].buttons[p_index].tooltip;
1395 }
1396
1397 int TreeItem::get_button_id(int p_column, int p_index) const {
1398 ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
1399 ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), -1);
1400 return cells[p_column].buttons[p_index].id;
1401 }
1402
1403 void TreeItem::erase_button(int p_column, int p_index) {
1404 ERR_FAIL_INDEX(p_column, cells.size());
1405 ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
1406 if (cells[p_column].buttons[p_index].accessibility_button_element.is_valid()) {
1407 DisplayServer::get_singleton()->accessibility_free_element(cells.write[p_column].buttons.write[p_index].accessibility_button_element);
1408 }
1409 cells.write[p_column].buttons.remove_at(p_index);
1410 _changed_notify(p_column);
1411 if (get_tree()) {
1412 get_tree()->update_configuration_warnings();
1413 }
1414 }
1415
1416 int TreeItem::get_button_by_id(int p_column, int p_id) const {
1417 ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
1418 for (int i = 0; i < cells[p_column].buttons.size(); i++) {
1419 if (cells[p_column].buttons[i].id == p_id) {
1420 return i;
1421 }
1422 }
1423
1424 return -1;
1425 }
1426
1427 Color TreeItem::get_button_color(int p_column, int p_index) const {
1428 ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
1429 ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Color());
1430 return cells[p_column].buttons[p_index].color;
1431 }
1432
1433 void TreeItem::set_button_tooltip_text(int p_column, int p_index, const String &p_tooltip) {
1434 ERR_FAIL_INDEX(p_column, cells.size());
1435 ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
1436 cells.write[p_column].buttons.write[p_index].tooltip = p_tooltip;
1437
1438 _changed_notify(p_column);
1439 }
1440
1441 void TreeItem::set_button(int p_column, int p_index, const Ref<Texture2D> &p_button) {
1442 ERR_FAIL_COND(p_button.is_null());
1443 ERR_FAIL_INDEX(p_column, cells.size());
1444 ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
1445
1446 if (cells[p_column].buttons[p_index].texture == p_button) {
1447 return;
1448 }
1449
1450 cells.write[p_column].buttons.write[p_index].texture = p_button;
1451 cells.write[p_column].cached_minimum_size_dirty = true;
1452
1453 _changed_notify(p_column);
1454 }
1455
1456 void TreeItem::set_button_description(int p_column, int p_index, const String &p_description) {
1457 ERR_FAIL_INDEX(p_column, cells.size());
1458 ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
1459
1460 if (cells[p_column].buttons[p_index].description == p_description) {
1461 return;
1462 }
1463
1464 cells.write[p_column].buttons.write[p_index].description = p_description;
1465 _changed_notify(p_column);
1466 if (get_tree()) {
1467 get_tree()->update_configuration_warnings();
1468 }
1469 }
1470
1471 void TreeItem::set_button_color(int p_column, int p_index, const Color &p_color) {
1472 ERR_FAIL_INDEX(p_column, cells.size());
1473 ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
1474
1475 if (cells[p_column].buttons[p_index].color == p_color) {
1476 return;
1477 }
1478
1479 cells.write[p_column].buttons.write[p_index].color = p_color;
1480 _changed_notify(p_column);
1481 }
1482
1483 void TreeItem::set_button_disabled(int p_column, int p_index, bool p_disabled) {
1484 ERR_FAIL_INDEX(p_column, cells.size());
1485 ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
1486
1487 if (cells[p_column].buttons[p_index].disabled == p_disabled) {
1488 return;
1489 }
1490
1491 cells.write[p_column].buttons.write[p_index].disabled = p_disabled;
1492 cells.write[p_column].cached_minimum_size_dirty = true;
1493
1494 _changed_notify(p_column);
1495 }
1496
1497 bool TreeItem::is_button_disabled(int p_column, int p_index) const {
1498 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
1499 ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), false);
1500
1501 return cells[p_column].buttons[p_index].disabled;
1502 }
1503
1504 void TreeItem::set_editable(int p_column, bool p_editable) {
1505 ERR_FAIL_INDEX(p_column, cells.size());
1506
1507 if (cells[p_column].editable == p_editable) {
1508 return;
1509 }
1510
1511 cells.write[p_column].editable = p_editable;
1512 cells.write[p_column].cached_minimum_size_dirty = true;
1513
1514 _changed_notify(p_column);
1515 }
1516
1517 bool TreeItem::is_editable(int p_column) {
1518 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
1519 return cells[p_column].editable;
1520 }
1521
1522 void TreeItem::set_custom_color(int p_column, const Color &p_color) {
1523 ERR_FAIL_INDEX(p_column, cells.size());
1524
1525 if (cells[p_column].custom_color && cells[p_column].color == p_color) {
1526 return;
1527 }
1528
1529 cells.write[p_column].custom_color = true;
1530 cells.write[p_column].color = p_color;
1531 _changed_notify(p_column);
1532 }
1533
1534 Color TreeItem::get_custom_color(int p_column) const {
1535 ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
1536 if (!cells[p_column].custom_color) {
1537 return Color();
1538 }
1539 return cells[p_column].color;
1540 }
1541
1542 void TreeItem::clear_custom_color(int p_column) {
1543 ERR_FAIL_INDEX(p_column, cells.size());
1544 cells.write[p_column].custom_color = false;
1545 cells.write[p_column].color = Color();
1546 _changed_notify(p_column);
1547 }
1548
1549 void TreeItem::set_custom_font(int p_column, const Ref<Font> &p_font) {
1550 ERR_FAIL_INDEX(p_column, cells.size());
1551
1552 if (cells[p_column].custom_font == p_font) {
1553 return;
1554 }
1555
1556 cells.write[p_column].custom_font = p_font;
1557 cells.write[p_column].cached_minimum_size_dirty = true;
1558
1559 _changed_notify(p_column);
1560 }
1561
1562 Ref<Font> TreeItem::get_custom_font(int p_column) const {
1563 ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Font>());
1564 return cells[p_column].custom_font;
1565 }
1566
1567 void TreeItem::set_custom_font_size(int p_column, int p_font_size) {
1568 ERR_FAIL_INDEX(p_column, cells.size());
1569
1570 if (cells[p_column].custom_font_size == p_font_size) {
1571 return;
1572 }
1573
1574 cells.write[p_column].custom_font_size = p_font_size;
1575 cells.write[p_column].cached_minimum_size_dirty = true;
1576
1577 _changed_notify(p_column);
1578 }
1579
1580 int TreeItem::get_custom_font_size(int p_column) const {
1581 ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
1582 return cells[p_column].custom_font_size;
1583 }
1584
1585 void TreeItem::set_tooltip_text(int p_column, const String &p_tooltip) {
1586 ERR_FAIL_INDEX(p_column, cells.size());
1587 cells.write[p_column].tooltip = p_tooltip;
1588 }
1589
1590 String TreeItem::get_tooltip_text(int p_column) const {
1591 ERR_FAIL_INDEX_V(p_column, cells.size(), "");
1592 return cells[p_column].tooltip;
1593 }
1594
1595 void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
1596 ERR_FAIL_INDEX(p_column, cells.size());
1597
1598 if (cells[p_column].custom_bg_color && cells[p_column].custom_bg_outline == p_bg_outline && cells[p_column].bg_color == p_color) {
1599 return;
1600 }
1601
1602 cells.write[p_column].custom_bg_color = true;
1603 cells.write[p_column].custom_bg_outline = p_bg_outline;
1604 cells.write[p_column].bg_color = p_color;
1605 _changed_notify(p_column);
1606 }
1607
1608 void TreeItem::clear_custom_bg_color(int p_column) {
1609 ERR_FAIL_INDEX(p_column, cells.size());
1610 cells.write[p_column].custom_bg_color = false;
1611 cells.write[p_column].bg_color = Color();
1612 _changed_notify(p_column);
1613 }
1614
1615 Color TreeItem::get_custom_bg_color(int p_column) const {
1616 ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
1617 if (!cells[p_column].custom_bg_color) {
1618 return Color();
1619 }
1620 return cells[p_column].bg_color;
1621 }
1622
1623 void TreeItem::set_custom_as_button(int p_column, bool p_button) {
1624 ERR_FAIL_INDEX(p_column, cells.size());
1625
1626 if (cells[p_column].custom_button == p_button) {
1627 return;
1628 }
1629
1630 cells.write[p_column].custom_button = p_button;
1631 cells.write[p_column].cached_minimum_size_dirty = true;
1632
1633 _changed_notify(p_column);
1634 }
1635
1636 bool TreeItem::is_custom_set_as_button(int p_column) const {
1637 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
1638 return cells[p_column].custom_button;
1639 }
1640
1641 void TreeItem::set_text_alignment(int p_column, HorizontalAlignment p_alignment) {
1642 ERR_FAIL_INDEX(p_column, cells.size());
1643
1644 if (cells[p_column].text_alignment == p_alignment) {
1645 return;
1646 }
1647
1648 cells.write[p_column].text_alignment = p_alignment;
1649 cells.write[p_column].cached_minimum_size_dirty = true;
1650
1651 _changed_notify(p_column);
1652 }
1653
1654 HorizontalAlignment TreeItem::get_text_alignment(int p_column) const {
1655 ERR_FAIL_INDEX_V(p_column, cells.size(), HORIZONTAL_ALIGNMENT_LEFT);
1656 return cells[p_column].text_alignment;
1657 }
1658
1659 void TreeItem::set_expand_right(int p_column, bool p_enable) {
1660 ERR_FAIL_INDEX(p_column, cells.size());
1661
1662 if (cells[p_column].expand_right == p_enable) {
1663 return;
1664 }
1665
1666 cells.write[p_column].expand_right = p_enable;
1667 cells.write[p_column].cached_minimum_size_dirty = true;
1668
1669 _changed_notify(p_column);
1670 }
1671
1672 bool TreeItem::get_expand_right(int p_column) const {
1673 ERR_FAIL_INDEX_V(p_column, cells.size(), false);
1674 return cells[p_column].expand_right;
1675 }
1676
1677 void TreeItem::set_disable_folding(bool p_disable) {
1678 if (disable_folding == p_disable) {
1679 return;
1680 }
1681
1682 disable_folding = p_disable;
1683
1684 for (Cell &c : cells) {
1685 c.cached_minimum_size_dirty = true;
1686 }
1687
1688 _changed_notify(0);
1689 }
1690
1691 bool TreeItem::is_folding_disabled() const {
1692 return disable_folding;
1693 }
1694
1695 Size2 TreeItem::get_minimum_size(int p_column) {
1696 ERR_FAIL_INDEX_V(p_column, cells.size(), Size2());
1697 Tree *parent_tree = get_tree();
1698 ERR_FAIL_NULL_V(parent_tree, Size2());
1699
1700 const TreeItem::Cell &cell = cells[p_column];
1701
1702 if (cell.cached_minimum_size_dirty || cell.text_buf->is_dirty() || cell.dirty) {
1703 Size2 size = Size2(
1704 parent_tree->theme_cache.inner_item_margin_left + parent_tree->theme_cache.inner_item_margin_right,
1705 parent_tree->theme_cache.inner_item_margin_top + parent_tree->theme_cache.inner_item_margin_bottom);
1706 int content_height = size.height;
1707
1708 // Text.
1709 if (!cell.text.is_empty()) {
1710 if (cell.dirty) {
1711 parent_tree->update_item_cell(this, p_column);
1712 }
1713 Size2 text_size = cell.text_buf->get_size();
1714 if (get_text_overrun_behavior(p_column) == TextServer::OVERRUN_NO_TRIMMING) {
1715 size.width += text_size.width;
1716 }
1717 content_height = MAX(content_height, text_size.height);
1718 }
1719
1720 // Icon.
1721 if (cell.mode == CELL_MODE_CHECK) {
1722 Size2i check_size = parent_tree->theme_cache.checked->get_size();
1723 size.width += check_size.width + parent_tree->theme_cache.h_separation;
1724 content_height = MAX(content_height, check_size.height);
1725 } else if (cell.mode == CELL_MODE_RANGE) {
1726 Ref<Texture2D> icon = cell.text.is_empty() ? parent_tree->theme_cache.updown : parent_tree->theme_cache.select_arrow;
1727 Size2i icon_size = icon->get_size();
1728 size.width += icon_size.width + parent_tree->theme_cache.h_separation;
1729 content_height = MAX(content_height, icon_size.height);
1730 }
1731 if (cell.icon.is_valid()) {
1732 Size2i icon_size = parent_tree->_get_cell_icon_size(cell);
1733 size.width += icon_size.width + parent_tree->theme_cache.icon_h_separation;
1734 content_height = MAX(content_height, icon_size.height);
1735 }
1736
1737 // Buttons.
1738 for (int i = 0; i < cell.buttons.size(); i++) {
1739 Ref<Texture2D> texture = cell.buttons[i].texture;
1740 if (texture.is_valid()) {
1741 Size2 button_size = texture->get_size();
1742 button_size.width += parent_tree->theme_cache.button_pressed->get_minimum_size().width;
1743 size.width += button_size.width + parent_tree->theme_cache.button_margin;
1744 content_height = MAX(content_height, button_size.height);
1745 }
1746 }
1747
1748 size.height += content_height;
1749 cells.write[p_column].cached_minimum_size = size;
1750 cells.write[p_column].cached_minimum_size_dirty = false;
1751 }
1752
1753 return cell.cached_minimum_size;
1754 }
1755
1756 void TreeItem::_call_recursive_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
1757 if (p_argcount < 1) {
1758 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
1759 r_error.expected = 1;
1760 return;
1761 }
1762
1763 if (!p_args[0]->is_string()) {
1764 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
1765 r_error.argument = 0;
1766 r_error.expected = Variant::STRING_NAME;
1767 return;
1768 }
1769
1770 StringName method = *p_args[0];
1771
1772 call_recursive(method, &p_args[1], p_argcount - 1, r_error);
1773 }
1774
1775 void recursive_call_aux(TreeItem *p_item, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
1776 if (!p_item) {
1777 return;
1778 }
1779 p_item->callp(p_method, p_args, p_argcount, r_error);
1780 TreeItem *c = p_item->get_first_child();
1781 while (c) {
1782 recursive_call_aux(c, p_method, p_args, p_argcount, r_error);
1783 c = c->get_next();
1784 }
1785 }
1786
1787 void TreeItem::call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
1788 recursive_call_aux(this, p_method, p_args, p_argcount, r_error);
1789 }
1790
1791 void TreeItem::_bind_methods() {
1792 ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
1793 ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
1794
1795 ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "column", "mode"), &TreeItem::set_auto_translate_mode);
1796 ClassDB::bind_method(D_METHOD("get_auto_translate_mode", "column"), &TreeItem::get_auto_translate_mode);
1797
1798 ClassDB::bind_method(D_METHOD("set_edit_multiline", "column", "multiline"), &TreeItem::set_edit_multiline);
1799 ClassDB::bind_method(D_METHOD("is_edit_multiline", "column"), &TreeItem::is_edit_multiline);
1800
1801 ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
1802 ClassDB::bind_method(D_METHOD("set_indeterminate", "column", "indeterminate"), &TreeItem::set_indeterminate);
1803 ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
1804 ClassDB::bind_method(D_METHOD("is_indeterminate", "column"), &TreeItem::is_indeterminate);
1805
1806 ClassDB::bind_method(D_METHOD("propagate_check", "column", "emit_signal"), &TreeItem::propagate_check, DEFVAL(true));
1807
1808 ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
1809 ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
1810
1811 ClassDB::bind_method(D_METHOD("set_description", "column", "description"), &TreeItem::set_description);
1812 ClassDB::bind_method(D_METHOD("get_description", "column"), &TreeItem::get_description);
1813
1814 ClassDB::bind_method(D_METHOD("set_text_direction", "column", "direction"), &TreeItem::set_text_direction);
1815 ClassDB::bind_method(D_METHOD("get_text_direction", "column"), &TreeItem::get_text_direction);
1816
1817 ClassDB::bind_method(D_METHOD("set_autowrap_mode", "column", "autowrap_mode"), &TreeItem::set_autowrap_mode);
1818 ClassDB::bind_method(D_METHOD("get_autowrap_mode", "column"), &TreeItem::get_autowrap_mode);
1819
1820 ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "column", "overrun_behavior"), &TreeItem::set_text_overrun_behavior);
1821 ClassDB::bind_method(D_METHOD("get_text_overrun_behavior", "column"), &TreeItem::get_text_overrun_behavior);
1822
1823 ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "column", "parser"), &TreeItem::set_structured_text_bidi_override);
1824 ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override);
1825
1826 ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "column", "args"), &TreeItem::set_structured_text_bidi_override_options);
1827 ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options", "column"), &TreeItem::get_structured_text_bidi_override_options);
1828
1829 ClassDB::bind_method(D_METHOD("set_language", "column", "language"), &TreeItem::set_language);
1830 ClassDB::bind_method(D_METHOD("get_language", "column"), &TreeItem::get_language);
1831
1832 ClassDB::bind_method(D_METHOD("set_suffix", "column", "text"), &TreeItem::set_suffix);
1833 ClassDB::bind_method(D_METHOD("get_suffix", "column"), &TreeItem::get_suffix);
1834
1835 ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
1836 ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
1837
1838 ClassDB::bind_method(D_METHOD("set_icon_overlay", "column", "texture"), &TreeItem::set_icon_overlay);
1839 ClassDB::bind_method(D_METHOD("get_icon_overlay", "column"), &TreeItem::get_icon_overlay);
1840
1841 ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
1842 ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
1843
1844 ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
1845 ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
1846
1847 ClassDB::bind_method(D_METHOD("set_icon_modulate", "column", "modulate"), &TreeItem::set_icon_modulate);
1848 ClassDB::bind_method(D_METHOD("get_icon_modulate", "column"), &TreeItem::get_icon_modulate);
1849
1850 ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
1851 ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
1852 ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
1853 ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
1854
1855 ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
1856 ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
1857
1858 #ifndef DISABLE_DEPRECATED
1859 ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
1860 #endif // DISABLE_DEPRECATED
1861 ClassDB::bind_method(D_METHOD("set_custom_draw_callback", "column", "callback"), &TreeItem::set_custom_draw_callback);
1862 ClassDB::bind_method(D_METHOD("get_custom_draw_callback", "column"), &TreeItem::get_custom_draw_callback);
1863
1864 ClassDB::bind_method(D_METHOD("set_custom_stylebox", "column", "stylebox"), &TreeItem::set_custom_stylebox);
1865 ClassDB::bind_method(D_METHOD("get_custom_stylebox", "column"), &TreeItem::get_custom_stylebox);
1866
1867 ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
1868 ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
1869
1870 ClassDB::bind_method(D_METHOD("set_collapsed_recursive", "enable"), &TreeItem::set_collapsed_recursive);
1871 ClassDB::bind_method(D_METHOD("is_any_collapsed", "only_visible"), &TreeItem::is_any_collapsed, DEFVAL(false));
1872
1873 ClassDB::bind_method(D_METHOD("set_visible", "enable"), &TreeItem::set_visible);
1874 ClassDB::bind_method(D_METHOD("is_visible"), &TreeItem::is_visible);
1875 ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &TreeItem::is_visible_in_tree);
1876
1877 ClassDB::bind_method(D_METHOD("uncollapse_tree"), &TreeItem::uncollapse_tree);
1878
1879 ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
1880 ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
1881
1882 ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
1883 ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
1884
1885 ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
1886 ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
1887 ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
1888
1889 ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
1890 ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
1891
1892 ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
1893 ClassDB::bind_method(D_METHOD("get_custom_color", "column"), &TreeItem::get_custom_color);
1894 ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
1895
1896 ClassDB::bind_method(D_METHOD("set_custom_font", "column", "font"), &TreeItem::set_custom_font);
1897 ClassDB::bind_method(D_METHOD("get_custom_font", "column"), &TreeItem::get_custom_font);
1898
1899 ClassDB::bind_method(D_METHOD("set_custom_font_size", "column", "font_size"), &TreeItem::set_custom_font_size);
1900 ClassDB::bind_method(D_METHOD("get_custom_font_size", "column"), &TreeItem::get_custom_font_size);
1901
1902 ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
1903 ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
1904 ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
1905
1906 ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
1907 ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
1908
1909 ClassDB::bind_method(D_METHOD("clear_buttons"), &TreeItem::clear_buttons);
1910 ClassDB::bind_method(D_METHOD("add_button", "column", "button", "id", "disabled", "tooltip_text", "description"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""), DEFVAL(""));
1911 ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
1912 ClassDB::bind_method(D_METHOD("get_button_tooltip_text", "column", "button_index"), &TreeItem::get_button_tooltip_text);
1913 ClassDB::bind_method(D_METHOD("get_button_id", "column", "button_index"), &TreeItem::get_button_id);
1914 ClassDB::bind_method(D_METHOD("get_button_by_id", "column", "id"), &TreeItem::get_button_by_id);
1915 ClassDB::bind_method(D_METHOD("get_button_color", "column", "id"), &TreeItem::get_button_color);
1916 ClassDB::bind_method(D_METHOD("get_button", "column", "button_index"), &TreeItem::get_button);
1917 ClassDB::bind_method(D_METHOD("set_button_tooltip_text", "column", "button_index", "tooltip"), &TreeItem::set_button_tooltip_text);
1918 ClassDB::bind_method(D_METHOD("set_button", "column", "button_index", "button"), &TreeItem::set_button);
1919 ClassDB::bind_method(D_METHOD("erase_button", "column", "button_index"), &TreeItem::erase_button);
1920 ClassDB::bind_method(D_METHOD("set_button_description", "column", "button_index", "description"), &TreeItem::set_button_description);
1921 ClassDB::bind_method(D_METHOD("set_button_disabled", "column", "button_index", "disabled"), &TreeItem::set_button_disabled);
1922 ClassDB::bind_method(D_METHOD("set_button_color", "column", "button_index", "color"), &TreeItem::set_button_color);
1923 ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_index"), &TreeItem::is_button_disabled);
1924
1925 ClassDB::bind_method(D_METHOD("set_tooltip_text", "column", "tooltip"), &TreeItem::set_tooltip_text);
1926 ClassDB::bind_method(D_METHOD("get_tooltip_text", "column"), &TreeItem::get_tooltip_text);
1927 ClassDB::bind_method(D_METHOD("set_text_alignment", "column", "text_alignment"), &TreeItem::set_text_alignment);
1928 ClassDB::bind_method(D_METHOD("get_text_alignment", "column"), &TreeItem::get_text_alignment);
1929
1930 ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
1931 ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
1932
1933 ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
1934 ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
1935
1936 ClassDB::bind_method(D_METHOD("create_child", "index"), &TreeItem::create_child, DEFVAL(-1));
1937 ClassDB::bind_method(D_METHOD("add_child", "child"), &TreeItem::add_child);
1938 ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::remove_child);
1939
1940 ClassDB::bind_method(D_METHOD("get_tree"), &TreeItem::get_tree);
1941
1942 ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
1943 ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
1944 ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
1945 ClassDB::bind_method(D_METHOD("get_first_child"), &TreeItem::get_first_child);
1946
1947 ClassDB::bind_method(D_METHOD("get_next_in_tree", "wrap"), &TreeItem::get_next_in_tree, DEFVAL(false));
1948 ClassDB::bind_method(D_METHOD("get_prev_in_tree", "wrap"), &TreeItem::get_prev_in_tree, DEFVAL(false));
1949
1950 ClassDB::bind_method(D_METHOD("get_next_visible", "wrap"), &TreeItem::get_next_visible, DEFVAL(false));
1951 ClassDB::bind_method(D_METHOD("get_prev_visible", "wrap"), &TreeItem::get_prev_visible, DEFVAL(false));
1952
1953 ClassDB::bind_method(D_METHOD("get_child", "index"), &TreeItem::get_child);
1954 ClassDB::bind_method(D_METHOD("get_child_count"), &TreeItem::get_child_count);
1955 ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
1956 ClassDB::bind_method(D_METHOD("get_index"), &TreeItem::get_index);
1957
1958 ClassDB::bind_method(D_METHOD("move_before", "item"), &TreeItem::move_before);
1959 ClassDB::bind_method(D_METHOD("move_after", "item"), &TreeItem::move_after);
1960
1961 {
1962 MethodInfo mi;
1963 mi.name = "call_recursive";
1964 mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
1965
1966 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_recursive", &TreeItem::_call_recursive_bind, mi);
1967 }
1968
1969 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
1970 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
1971 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
1972 ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
1973
1974 BIND_ENUM_CONSTANT(CELL_MODE_STRING);
1975 BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
1976 BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
1977 BIND_ENUM_CONSTANT(CELL_MODE_ICON);
1978 BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
1979 }
1980
1981 TreeItem::TreeItem(Tree *p_tree) {
1982 tree = p_tree;
1983 }
1984
1985 TreeItem::~TreeItem() {
1986 _unlink_from_tree();
1987 _change_tree(nullptr);
1988
1989 validate_cache();
1990 prev = nullptr;
1991 clear_children();
1992 }
1993
1994 /*************************** END of TreeItem ***************************/
1995
1996 void Tree::_update_theme_item_cache() {
1997 Control::_update_theme_item_cache();
1998
1999 theme_cache.base_scale = get_theme_default_base_scale();
2000 }
2001
2002 Size2 Tree::_get_cell_icon_size(const TreeItem::Cell &p_cell) const {
2003 Size2i icon_size = p_cell.get_icon_size();
2004
2005 int max_width = 0;
2006 if (theme_cache.icon_max_width > 0) {
2007 max_width = theme_cache.icon_max_width;
2008 }
2009 if (p_cell.icon_max_w > 0 && (max_width == 0 || p_cell.icon_max_w < max_width)) {
2010 max_width = p_cell.icon_max_w;
2011 }
2012
2013 if (max_width > 0 && icon_size.width > max_width) {
2014 icon_size.height = icon_size.height * max_width / icon_size.width;
2015 icon_size.width = max_width;
2016 }
2017
2018 return icon_size;
2019 }
2020
2021 int Tree::compute_item_height(TreeItem *p_item) const {
2022 if ((p_item == root && hide_root) || !p_item->is_visible_in_tree()) {
2023 return 0;
2024 }
2025
2026 ERR_FAIL_COND_V(theme_cache.font.is_null(), 0);
2027 int height = 0;
2028
2029 for (int i = 0; i < columns.size(); i++) {
2030 height = MAX(height, p_item->get_minimum_size(i).y);
2031 }
2032 int item_min_height = MAX(theme_cache.font->get_height(theme_cache.font_size), p_item->get_custom_minimum_height());
2033 if (height < item_min_height) {
2034 height = item_min_height;
2035 }
2036
2037 height += theme_cache.v_separation;
2038
2039 return height;
2040 }
2041
2042 int Tree::get_item_height(TreeItem *p_item) const {
2043 if (!p_item->is_visible_in_tree()) {
2044 return 0;
2045 }
2046 int height = compute_item_height(p_item);
2047 height += theme_cache.v_separation;
2048
2049 if (!p_item->collapsed) { // If not collapsed, check the children.
2050
2051 TreeItem *c = p_item->first_child;
2052
2053 while (c) {
2054 height += get_item_height(c);
2055
2056 c = c->next;
2057 }
2058 }
2059
2060 return height;
2061 }
2062
2063 Point2i Tree::convert_rtl_position(const Point2i &pos, int width) const {
2064 if (cache.rtl) {
2065 return Point2i(get_size().width - pos.x - width, pos.y);
2066 }
2067 return pos;
2068 }
2069
2070 Rect2i Tree::convert_rtl_rect(const Rect2i &rect) const {
2071 if (cache.rtl) {
2072 return Rect2i(Point2i(get_size().width - rect.position.x - rect.size.x, rect.position.y), rect.size);
2073 }
2074 return rect;
2075 }
2076
2077 void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color, int p_ol_size, const Color &p_ol_color) const {
2078 ERR_FAIL_COND(theme_cache.font.is_null());
2079
2080 Rect2i rect = p_rect;
2081 Size2 ts = p_cell.text_buf->get_size();
2082 bool rtl = is_layout_rtl();
2083
2084 Size2i icon_size;
2085 if (p_cell.icon.is_valid()) {
2086 icon_size = _get_cell_icon_size(p_cell);
2087 }
2088
2089 int displayed_width = 0;
2090 if (p_cell.icon.is_valid()) {
2091 displayed_width = MIN(displayed_width + icon_size.width + theme_cache.icon_h_separation, rect.size.width);
2092 }
2093 if (displayed_width + ts.width > rect.size.width) {
2094 ts.width = rect.size.width - displayed_width;
2095 }
2096 displayed_width += ts.width;
2097
2098 int empty_width = rect.size.width - displayed_width;
2099
2100 switch (p_cell.text_alignment) {
2101 case HORIZONTAL_ALIGNMENT_FILL:
2102 case HORIZONTAL_ALIGNMENT_LEFT: {
2103 if (rtl) {
2104 rect.position.x += empty_width;
2105 }
2106 } break;
2107 case HORIZONTAL_ALIGNMENT_CENTER:
2108 rect.position.x += empty_width / 2;
2109 break;
2110 case HORIZONTAL_ALIGNMENT_RIGHT:
2111 if (!rtl) {
2112 rect.position.x += empty_width;
2113 }
2114 break;
2115 }
2116
2117 RID ci = content_ci;
2118
2119 if (rtl) {
2120 if (ts.width > 0) {
2121 Point2 draw_pos = rect.position;
2122 draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
2123 if (p_ol_size > 0 && p_ol_color.a > 0) {
2124 p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
2125 }
2126 p_cell.text_buf->draw(ci, draw_pos, p_color);
2127 }
2128 rect.position.x += ts.width + theme_cache.icon_h_separation;
2129 }
2130
2131 if (p_cell.icon.is_valid()) {
2132 Point2 icon_pos = rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - icon_size.y) / 2));
2133 Rect2i icon_region = (p_cell.icon_region == Rect2i()) ? Rect2i(Point2(), p_cell.icon->get_size()) : p_cell.icon_region;
2134 if (icon_size.width > rect.size.width) {
2135 icon_region.size.width = icon_region.size.width * rect.size.width / icon_size.width;
2136 icon_size.width = rect.size.width;
2137 }
2138 p_cell.draw_icon(ci, icon_pos, icon_size, icon_region, p_icon_color);
2139 rect.position.x += icon_size.x + theme_cache.icon_h_separation;
2140 }
2141
2142 if (!rtl && ts.width > 0) {
2143 Point2 draw_pos = rect.position;
2144 draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
2145 if (p_ol_size > 0 && p_ol_color.a > 0) {
2146 p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
2147 }
2148 p_cell.text_buf->draw(ci, draw_pos, p_color);
2149 }
2150 }
2151
2152 void Tree::update_column(int p_col) {
2153 columns.write[p_col].text_buf->clear();
2154 if (columns[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
2155 columns.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
2156 } else {
2157 columns.write[p_col].text_buf->set_direction((TextServer::Direction)columns[p_col].text_direction);
2158 }
2159
2160 columns.write[p_col].xl_title = atr(columns[p_col].title);
2161 const String &lang = columns[p_col].language.is_empty() ? _get_locale() : columns[p_col].language;
2162 columns.write[p_col].text_buf->add_string(columns[p_col].xl_title, theme_cache.tb_font, theme_cache.tb_font_size, lang);
2163 columns.write[p_col].cached_minimum_width_dirty = true;
2164 }
2165
2166 void Tree::update_item_cell(TreeItem *p_item, int p_col) const {
2167 String valtext;
2168
2169 p_item->cells.write[p_col].text_buf->clear();
2170 if (p_item->cells[p_col].mode == TreeItem::CELL_MODE_RANGE) {
2171 if (!p_item->cells[p_col].text.is_empty()) {
2172 if (!p_item->cells[p_col].editable) {
2173 return;
2174 }
2175
2176 int option = (int)p_item->cells[p_col].val;
2177
2178 valtext = p_item->atr(p_col, ETR("(Other)"));
2179 Vector<String> strings = p_item->cells[p_col].text.split(",");
2180 for (int j = 0; j < strings.size(); j++) {
2181 int value = j;
2182 if (!strings[j].get_slicec(':', 1).is_empty()) {
2183 value = strings[j].get_slicec(':', 1).to_int();
2184 }
2185 if (option == value) {
2186 valtext = p_item->atr(p_col, strings[j].get_slicec(':', 0));
2187 break;
2188 }
2189 }
2190
2191 } else {
2192 valtext = String::num(p_item->cells[p_col].val, Math::range_step_decimals(p_item->cells[p_col].step));
2193 }
2194 } else {
2195 // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
2196 if (!p_item->cells[p_col].editable || p_item->cells[p_col].mode != TreeItem::CELL_MODE_STRING) {
2197 p_item->cells.write[p_col].xl_text = p_item->atr(p_col, p_item->cells[p_col].text);
2198 } else {
2199 p_item->cells.write[p_col].xl_text = p_item->cells[p_col].text;
2200 }
2201
2202 valtext = p_item->cells[p_col].xl_text;
2203 }
2204
2205 if (!p_item->cells[p_col].suffix.is_empty()) {
2206 if (!valtext.is_empty()) {
2207 valtext += " ";
2208 }
2209 valtext += p_item->cells[p_col].suffix;
2210 }
2211
2212 if (p_item->cells[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
2213 p_item->cells.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
2214 } else {
2215 p_item->cells.write[p_col].text_buf->set_direction((TextServer::Direction)p_item->cells[p_col].text_direction);
2216 }
2217
2218 Ref<Font> font;
2219 if (p_item->cells[p_col].custom_font.is_valid()) {
2220 font = p_item->cells[p_col].custom_font;
2221 } else {
2222 font = theme_cache.font;
2223 }
2224
2225 int font_size;
2226 if (p_item->cells[p_col].custom_font_size > 0) {
2227 font_size = p_item->cells[p_col].custom_font_size;
2228 } else {
2229 font_size = theme_cache.font_size;
2230 }
2231 const String &lang = p_item->cells[p_col].language.is_empty() ? _get_locale() : p_item->cells[p_col].language;
2232 p_item->cells.write[p_col].text_buf->add_string(valtext, font, font_size, lang);
2233
2234 BitField<TextServer::LineBreakFlag> break_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES;
2235 switch (p_item->cells.write[p_col].autowrap_mode) {
2236 case TextServer::AUTOWRAP_OFF:
2237 break;
2238 case TextServer::AUTOWRAP_ARBITRARY:
2239 break_flags.set_flag(TextServer::BREAK_GRAPHEME_BOUND);
2240 break;
2241 case TextServer::AUTOWRAP_WORD:
2242 break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
2243 break;
2244 case TextServer::AUTOWRAP_WORD_SMART:
2245 break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
2246 break_flags.set_flag(TextServer::BREAK_ADAPTIVE);
2247 break;
2248 }
2249 p_item->cells.write[p_col].text_buf->set_break_flags(break_flags);
2250
2251 TS->shaped_text_set_bidi_override(p_item->cells[p_col].text_buf->get_rid(), structured_text_parser(p_item->cells[p_col].st_parser, p_item->cells[p_col].st_args, valtext));
2252 p_item->cells.write[p_col].dirty = false;
2253 }
2254
2255 void Tree::update_item_cache(TreeItem *p_item) const {
2256 for (int i = 0; i < p_item->cells.size(); i++) {
2257 update_item_cell(p_item, i);
2258 }
2259
2260 TreeItem *c = p_item->first_child;
2261 while (c) {
2262 update_item_cache(c);
2263 c = c->next;
2264 }
2265 }
2266
2267 int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item, int &r_self_height) {
2268 const real_t bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM); // Extra stylebox space below the content
2269 const real_t draw_height = p_draw_size.height + bottom_margin; // Visible height including bottom margin
2270
2271 // Cull item if it's beyond the bottom of the visible area.
2272 if (p_pos.y - theme_cache.offset.y > draw_height) {
2273 return -1;
2274 }
2275
2276 if (!p_item->is_visible_in_tree()) {
2277 return 0;
2278 }
2279
2280 RID ci = content_ci;
2281
2282 int htotal = 0;
2283
2284 int label_h = 0;
2285 bool rtl = cache.rtl;
2286
2287 // Draw label, if height fits.
2288 bool skip = (p_item == root && hide_root);
2289
2290 if (!skip) {
2291 // Draw separation.
2292
2293 ERR_FAIL_COND_V(theme_cache.font.is_null(), -1);
2294
2295 int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin);
2296 int skip2 = 0;
2297
2298 bool is_row_hovered = (!cache.hover_header_row && cache.hover_item == p_item);
2299
2300 for (int i = 0; i < columns.size(); i++) {
2301 if (skip2) {
2302 skip2--;
2303 continue;
2304 }
2305
2306 bool is_col_hovered = cache.hover_column == i;
2307 bool is_cell_hovered = is_row_hovered && is_col_hovered;
2308 bool is_cell_button_hovered = is_cell_hovered && cache.hover_button_index_in_column != -1;
2309 int item_width = get_column_width(i);
2310
2311 if (i == 0) {
2312 item_width -= ofs;
2313
2314 if (item_width <= 0) {
2315 ofs = get_column_width(0);
2316 r_self_height = compute_item_height(p_item);
2317 label_h = r_self_height + theme_cache.v_separation;
2318 continue;
2319 }
2320 } else {
2321 ofs += theme_cache.h_separation;
2322 item_width -= theme_cache.h_separation;
2323 }
2324
2325 if (p_item->cells[i].expand_right) {
2326 int plus = 1;
2327 while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].xl_text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
2328 item_width += get_column_width(i + plus);
2329 plus++;
2330 skip2++;
2331 }
2332 }
2333
2334 if (!p_item->cells[i].buttons.is_empty()) {
2335 int buttons_width = 0;
2336 for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
2337 Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
2338 buttons_width += button_texture->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
2339 }
2340
2341 double total_ofs = ofs - theme_cache.offset.x;
2342
2343 // If part of the column is beyond the right side of the control due to scrolling, clamp the label width
2344 // so that all buttons attached to the cell remain within view.
2345 if (total_ofs + item_width > p_draw_size.width) {
2346 item_width = MAX(buttons_width, p_draw_size.width - total_ofs);
2347 }
2348 }
2349
2350 int item_width_with_buttons = item_width; // Used later for drawing buttons.
2351 int buttons_width = 0;
2352 for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
2353 Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
2354 Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
2355
2356 item_width -= button_size.width + theme_cache.button_margin;
2357 buttons_width += button_size.width + theme_cache.button_margin;
2358 }
2359
2360 int text_width = item_width - theme_cache.inner_item_margin_left - theme_cache.inner_item_margin_right;
2361 if (p_item->cells[i].icon.is_valid()) {
2362 text_width -= _get_cell_icon_size(p_item->cells[i]).x + theme_cache.icon_h_separation;
2363 }
2364
2365 p_item->cells.write[i].text_buf->set_width(text_width);
2366
2367 r_self_height = compute_item_height(p_item);
2368 label_h = r_self_height + theme_cache.v_separation;
2369
2370 // Bottom edge for top-culling
2371 const real_t item_bottom = p_pos.y + label_h - theme_cache.offset.y + p_draw_ofs.y;
2372 if (item_bottom < 0.0f) {
2373 continue; // No need to draw.
2374 }
2375
2376 Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - theme_cache.offset + p_draw_ofs, Size2i(item_width, label_h));
2377 Rect2i cell_rect = item_rect;
2378 if (i != 0) {
2379 cell_rect.position.x -= theme_cache.h_separation;
2380 cell_rect.size.x += theme_cache.h_separation;
2381 }
2382
2383 if (i == 0 && select_mode == SELECT_ROW) {
2384 if (p_item->cells[0].selected || is_row_hovered) {
2385 const Rect2 content_rect = _get_content_rect();
2386 Rect2i row_rect = Rect2i(Point2i(content_rect.position.x, item_rect.position.y), Size2i(content_rect.size.x, item_rect.size.y));
2387 row_rect = convert_rtl_rect(row_rect);
2388
2389 if (p_item->cells[0].selected) {
2390 if (is_row_hovered) {
2391 if (has_focus(true)) {
2392 theme_cache.hovered_selected_focus->draw(ci, row_rect);
2393 } else {
2394 theme_cache.hovered_selected->draw(ci, row_rect);
2395 }
2396 } else {
2397 if (has_focus(true)) {
2398 theme_cache.selected_focus->draw(ci, row_rect);
2399 } else {
2400 theme_cache.selected->draw(ci, row_rect);
2401 }
2402 }
2403 } else if (!drop_mode_flags) {
2404 if (is_cell_button_hovered) {
2405 theme_cache.hovered_dimmed->draw(ci, row_rect);
2406 } else {
2407 theme_cache.hovered->draw(ci, row_rect);
2408 }
2409 }
2410 }
2411 }
2412
2413 if (select_mode != SELECT_ROW) {
2414 Rect2i r = convert_rtl_rect(cell_rect);
2415
2416 // Cell hover.
2417 if (is_cell_hovered && !p_item->cells[i].selected && !drop_mode_flags) {
2418 if (is_cell_button_hovered) {
2419 theme_cache.hovered_dimmed->draw(ci, r);
2420 } else {
2421 theme_cache.hovered->draw(ci, r);
2422 }
2423 }
2424 }
2425
2426 if ((select_mode == SELECT_ROW && selected_item == p_item) || (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) || p_item->cells[i].selected || !p_item->focus_rect.has_area()) {
2427 Rect2i r = cell_rect;
2428
2429 if (select_mode != SELECT_ROW) {
2430 p_item->focus_rect = Rect2(r.position, r.size);
2431 r = convert_rtl_rect(r);
2432 if (p_item->cells[i].selected) {
2433 if (is_cell_hovered) {
2434 if (has_focus(true)) {
2435 theme_cache.hovered_selected_focus->draw(ci, r);
2436 } else {
2437 theme_cache.hovered_selected->draw(ci, r);
2438 }
2439 } else {
2440 if (has_focus(true)) {
2441 theme_cache.selected_focus->draw(ci, r);
2442 } else {
2443 theme_cache.selected->draw(ci, r);
2444 }
2445 }
2446 }
2447 } else {
2448 p_item->cells.write[i].focus_rect = Rect2(r.position, r.size);
2449 }
2450 }
2451
2452 if (theme_cache.draw_guides) {
2453 Rect2 r = convert_rtl_rect(cell_rect);
2454 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(r.position.x, r.position.y + r.size.height), r.position + r.size, theme_cache.guide_color, 1);
2455 }
2456
2457 if (p_item->cells[i].custom_bg_color) {
2458 Rect2 r = cell_rect;
2459 if (i == 0) {
2460 r.position.x = p_draw_ofs.x;
2461 r.size.x = item_width + ofs;
2462 }
2463 r = convert_rtl_rect(r);
2464 if (p_item->cells[i].custom_bg_outline) {
2465 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), p_item->cells[i].bg_color);
2466 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y - 1, r.size.x, 1), p_item->cells[i].bg_color);
2467 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
2468 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
2469 } else {
2470 RenderingServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
2471 }
2472 }
2473
2474 if (p_item->cells[i].custom_stylebox.is_valid()) {
2475 Rect2 r = cell_rect;
2476 if (i == 0) {
2477 r.position.x = p_draw_ofs.x;
2478 r.size.x = item_width + ofs;
2479 }
2480 r = convert_rtl_rect(r);
2481 p_item->cells[i].custom_stylebox->draw(ci, r);
2482 }
2483
2484 if (drop_mode_flags && drop_mode_over) {
2485 Rect2 r = convert_rtl_rect(cell_rect);
2486 if (drop_mode_over == p_item) {
2487 if (drop_mode_section == 0 || drop_mode_section == -1) {
2488 // Line above.
2489 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
2490 }
2491 if (drop_mode_section == 0) {
2492 // Side lines.
2493 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), theme_cache.drop_position_color);
2494 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), theme_cache.drop_position_color);
2495 }
2496 if (drop_mode_section == 0 || (drop_mode_section == 1 && (!p_item->get_first_child() || p_item->is_collapsed()))) {
2497 // Line below.
2498 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y, r.size.x, 1), theme_cache.drop_position_color);
2499 }
2500 } else if (drop_mode_over == p_item->get_parent()) {
2501 if (drop_mode_section == 1 && !p_item->get_prev()) {
2502 // Line above.
2503 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
2504 }
2505 }
2506 }
2507
2508 Color cell_color;
2509 if (p_item->cells[i].custom_color) {
2510 cell_color = p_item->cells[i].color;
2511 } else {
2512 bool draw_as_hover = !drop_mode_flags && (select_mode == SELECT_ROW ? is_row_hovered : is_cell_hovered);
2513 bool draw_as_hover_dim = draw_as_hover && is_cell_button_hovered;
2514 cell_color = p_item->cells[i].selected && draw_as_hover ? theme_cache.font_hovered_selected_color : (p_item->cells[i].selected ? theme_cache.font_selected_color : (draw_as_hover_dim ? theme_cache.font_hovered_dimmed_color : (draw_as_hover ? theme_cache.font_hovered_color : theme_cache.font_color)));
2515 }
2516
2517 Color font_outline_color = theme_cache.font_outline_color;
2518 int outline_size = theme_cache.font_outline_size;
2519 Color icon_col = p_item->cells[i].icon_color;
2520
2521 if (p_item->cells[i].dirty) {
2522 update_item_cell(p_item, i);
2523 }
2524
2525 item_rect = convert_rtl_rect(item_rect);
2526 item_rect = item_rect.grow_individual(-theme_cache.inner_item_margin_left, -theme_cache.inner_item_margin_top, -theme_cache.inner_item_margin_right, -theme_cache.inner_item_margin_bottom);
2527
2528 Point2i text_pos = item_rect.position;
2529 text_pos.y += Math::floor((item_rect.size.y - p_item->cells[i].text_buf->get_size().y) * 0.5);
2530
2531 switch (p_item->cells[i].mode) {
2532 case TreeItem::CELL_MODE_STRING: {
2533 draw_item_rect(p_item->cells[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
2534 } break;
2535 case TreeItem::CELL_MODE_CHECK: {
2536 Point2i check_ofs = item_rect.position;
2537 check_ofs.y += Math::floor((real_t)(item_rect.size.y - theme_cache.checked->get_height()) / 2);
2538
2539 if (p_item->cells[i].editable) {
2540 if (p_item->cells[i].indeterminate) {
2541 theme_cache.indeterminate->draw(ci, check_ofs);
2542 } else if (p_item->cells[i].checked) {
2543 theme_cache.checked->draw(ci, check_ofs);
2544 } else {
2545 theme_cache.unchecked->draw(ci, check_ofs);
2546 }
2547 } else {
2548 if (p_item->cells[i].indeterminate) {
2549 theme_cache.indeterminate_disabled->draw(ci, check_ofs);
2550 } else if (p_item->cells[i].checked) {
2551 theme_cache.checked_disabled->draw(ci, check_ofs);
2552 } else {
2553 theme_cache.unchecked_disabled->draw(ci, check_ofs);
2554 }
2555 }
2556
2557 int check_w = theme_cache.checked->get_width() + theme_cache.check_h_separation;
2558
2559 text_pos.x += check_w;
2560
2561 item_rect.size.x -= check_w;
2562 item_rect.position.x += check_w;
2563
2564 if (!p_item->cells[i].editable) {
2565 cell_color = theme_cache.font_disabled_color;
2566 }
2567
2568 draw_item_rect(p_item->cells[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
2569
2570 } break;
2571 case TreeItem::CELL_MODE_RANGE: {
2572 if (!p_item->cells[i].text.is_empty()) {
2573 if (!p_item->cells[i].editable) {
2574 break;
2575 }
2576
2577 Ref<Texture2D> downarrow = theme_cache.select_arrow;
2578 int cell_width = item_rect.size.x - downarrow->get_width();
2579
2580 if (rtl) {
2581 if (outline_size > 0 && font_outline_color.a > 0) {
2582 p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
2583 }
2584 p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
2585 } else {
2586 if (outline_size > 0 && font_outline_color.a > 0) {
2587 p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
2588 }
2589 p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
2590 }
2591
2592 Point2i arrow_pos = item_rect.position;
2593 arrow_pos.x += item_rect.size.x - downarrow->get_width();
2594 arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
2595
2596 downarrow->draw(ci, arrow_pos);
2597 } else {
2598 Ref<Texture2D> updown = theme_cache.updown;
2599
2600 int cell_width = item_rect.size.x - updown->get_width();
2601
2602 if (rtl) {
2603 if (outline_size > 0 && font_outline_color.a > 0) {
2604 p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
2605 }
2606 p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
2607 } else {
2608 if (outline_size > 0 && font_outline_color.a > 0) {
2609 p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
2610 }
2611 p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
2612 }
2613
2614 if (!p_item->cells[i].editable) {
2615 break;
2616 }
2617
2618 Point2i updown_pos = item_rect.position;
2619 updown_pos.x += item_rect.size.x - updown->get_width();
2620 updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
2621
2622 updown->draw(ci, updown_pos);
2623 }
2624
2625 } break;
2626 case TreeItem::CELL_MODE_ICON: {
2627 if (p_item->cells[i].icon.is_null()) {
2628 break;
2629 }
2630 Size2i icon_size = _get_cell_icon_size(p_item->cells[i]);
2631 Point2i icon_ofs = (item_rect.size - icon_size) / 2;
2632 icon_ofs += item_rect.position;
2633
2634 p_item->cells[i].icon->draw_rect(ci, Rect2(icon_ofs, icon_size), false, icon_col);
2635
2636 } break;
2637 case TreeItem::CELL_MODE_CUSTOM: {
2638 if (p_item->cells[i].custom_draw_callback.is_valid()) {
2639 Variant args[] = { p_item, Rect2(item_rect) };
2640 const Variant *argptrs[] = { &args[0], &args[1] };
2641
2642 Callable::CallError ce;
2643 Variant ret;
2644 p_item->cells[i].custom_draw_callback.callp(argptrs, 2, ret, ce);
2645 if (ce.error != Callable::CallError::CALL_OK) {
2646 ERR_PRINT("Error calling custom draw method: " + Variant::get_callable_error_text(p_item->cells[i].custom_draw_callback, argptrs, 2, ce) + ".");
2647 }
2648 }
2649
2650 if (!p_item->cells[i].editable) {
2651 draw_item_rect(p_item->cells[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
2652 break;
2653 }
2654
2655 Ref<Texture2D> downarrow = theme_cache.select_arrow;
2656
2657 Rect2i ir = item_rect;
2658
2659 Point2i arrow_pos = item_rect.position;
2660 arrow_pos.x += item_rect.size.x - downarrow->get_width();
2661 arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
2662 ir.size.width -= downarrow->get_width();
2663
2664 if (p_item->cells[i].custom_button) {
2665 if (cache.hover_item == p_item && cache.hover_column == i) {
2666 if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
2667 theme_cache.custom_button_pressed->draw(ci, ir);
2668 } else {
2669 theme_cache.custom_button_hover->draw(ci, ir);
2670 cell_color = theme_cache.custom_button_font_highlight;
2671 }
2672 } else {
2673 theme_cache.custom_button->draw(ci, ir);
2674 }
2675 ir.size -= theme_cache.custom_button->get_minimum_size();
2676 ir.position += theme_cache.custom_button->get_offset();
2677 }
2678
2679 draw_item_rect(p_item->cells[i], ir, cell_color, icon_col, outline_size, font_outline_color);
2680
2681 downarrow->draw(ci, arrow_pos);
2682
2683 } break;
2684 }
2685
2686 // Draw the buttons inside the cell.
2687 for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
2688 Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
2689 Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
2690
2691 Point2i button_ofs = Point2i(ofs + item_width_with_buttons - button_size.width, p_pos.y) - theme_cache.offset + p_draw_ofs;
2692
2693 bool should_draw_pressed = cache.click_type == Cache::CLICK_BUTTON && cache.click_item == p_item && cache.click_column == i && cache.click_index == j && !p_item->cells[i].buttons[j].disabled;
2694 bool should_draw_hovered = !drop_mode_flags && cache.hover_item == p_item && cache.hover_column == i && cache.hover_button_index_in_column == j && !p_item->cells[i].buttons[j].disabled;
2695
2696 if (should_draw_pressed || should_draw_hovered) {
2697 Point2 od = convert_rtl_position(button_ofs, button_size.x);
2698 if (should_draw_pressed && should_draw_hovered) {
2699 theme_cache.button_pressed->draw(ci, Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
2700 } else {
2701 theme_cache.button_hover->draw(ci, Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
2702 }
2703 }
2704 if (selected_item == p_item && selected_col == i && selected_button == j) {
2705 Point2 od = convert_rtl_position(button_ofs, button_size.x);
2706 theme_cache.button_hover->draw(ci, Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
2707 }
2708
2709 button_ofs.y += (label_h - button_size.height) / 2;
2710 button_ofs += theme_cache.button_pressed->get_offset();
2711
2712 button_ofs = convert_rtl_position(button_ofs, button_texture->get_width());
2713 button_texture->draw(ci, button_ofs, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
2714 item_width_with_buttons -= button_size.width + theme_cache.button_margin;
2715 }
2716
2717 if (i == 0) {
2718 ofs = get_column_width(0);
2719 } else {
2720 ofs += item_width + buttons_width;
2721 }
2722
2723 if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
2724 cell_rect = convert_rtl_rect(cell_rect);
2725 if (has_focus(true)) {
2726 theme_cache.cursor->draw(ci, cell_rect);
2727 } else {
2728 theme_cache.cursor_unfocus->draw(ci, cell_rect);
2729 }
2730 }
2731 }
2732
2733 // Bottom edge for arrow visibility
2734 const real_t item_bottom = p_pos.y + label_h - theme_cache.offset.y + p_draw_ofs.y;
2735 if (item_bottom >= 0.0f) {
2736 // Draw the folding arrow.
2737 if (!p_item->disable_folding && !hide_folding && p_item->first_child && p_item->get_visible_child_count() != 0) { // Has visible children, draw the guide box.
2738 Ref<Texture2D> arrow;
2739
2740 if (p_item->collapsed) {
2741 if (rtl) {
2742 arrow = theme_cache.arrow_collapsed_mirrored;
2743 } else {
2744 arrow = theme_cache.arrow_collapsed;
2745 }
2746 } else {
2747 arrow = theme_cache.arrow;
2748 }
2749
2750 Size2 arrow_full_size = arrow->get_size();
2751
2752 Point2 apos = p_pos + Point2i(0, (label_h - arrow_full_size.height) / 2) - theme_cache.offset + p_draw_ofs;
2753 apos.x += theme_cache.item_margin - arrow_full_size.width;
2754
2755 Size2 arrow_draw_size = arrow_full_size;
2756 int out_width = p_pos.x + theme_cache.item_margin - get_column_width(0);
2757 if (out_width > 0) {
2758 arrow_draw_size.width -= out_width;
2759 }
2760
2761 if (arrow_draw_size.width > 0) {
2762 apos = convert_rtl_position(apos, arrow_draw_size.width);
2763 Point2 src_pos = Point2();
2764 if (rtl) {
2765 src_pos = Point2(arrow_full_size.width - arrow_draw_size.width, 0);
2766 }
2767 Rect2 arrow_rect = Rect2(apos, arrow_draw_size);
2768 Rect2 arrow_src_rect = Rect2(src_pos, arrow_draw_size);
2769 arrow->draw_rect_region(ci, arrow_rect, arrow_src_rect);
2770 }
2771 }
2772 }
2773 }
2774
2775 Point2 children_pos = p_pos;
2776
2777 if (!skip) {
2778 children_pos.x += theme_cache.item_margin;
2779 htotal += label_h;
2780 children_pos.y += htotal;
2781 }
2782
2783 if (!p_item->collapsed) { // If not collapsed, check the children.
2784 TreeItem *c = p_item->first_child;
2785
2786 int base_ofs = children_pos.y - theme_cache.offset.y + p_draw_ofs.y;
2787 float prev_ofs = base_ofs;
2788 float prev_hl_ofs = base_ofs;
2789 bool has_sibling_selection = c && _is_sibling_branch_selected(c);
2790
2791 while (c) {
2792 int child_h = -1;
2793 int child_self_height = 0;
2794 if (htotal >= 0) {
2795 child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c, child_self_height);
2796 child_self_height += theme_cache.v_separation;
2797 }
2798
2799 // Draw relationship lines.
2800 if (theme_cache.draw_relationship_lines > 0 && (!hide_root || c->parent != root) && c->is_visible_in_tree()) {
2801 int parent_ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin - theme_cache.arrow->get_width() * 0.5);
2802 Point2i parent_pos = Point2i(parent_ofs, p_pos.y + label_h * 0.5 + theme_cache.arrow->get_height() * 0.5) - theme_cache.offset + p_draw_ofs;
2803
2804 int root_ofs = children_pos.x + ((c->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin - (c->get_visible_child_count() > 0 ? theme_cache.arrow->get_width() : 0));
2805 root_ofs = MIN(root_ofs, get_column_width(0) + p_draw_ofs.x);
2806 Point2i root_pos = Point2i(root_ofs, children_pos.y + child_self_height * 0.5) - theme_cache.offset + p_draw_ofs;
2807
2808 bool is_no_space = root_pos.x <= parent_pos.x;
2809
2810 float line_width = theme_cache.relationship_line_width * Math::round(theme_cache.base_scale);
2811 float parent_line_width = theme_cache.parent_hl_line_width * Math::round(theme_cache.base_scale);
2812 float children_line_width = theme_cache.children_hl_line_width * Math::round(theme_cache.base_scale);
2813
2814 float line_pixel_shift = int(Math::round(line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
2815 float parent_line_pixel_shift = int(Math::round(parent_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
2816 float children_line_pixel_shift = int(Math::round(children_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
2817
2818 int more_prev_ofs = 0;
2819
2820 if (root_pos.y + line_width >= 0) {
2821 root_pos = convert_rtl_position(root_pos);
2822 parent_pos = convert_rtl_position(parent_pos);
2823 float parent_bottom_y = root_pos.y + parent_line_width * 0.5 + parent_line_pixel_shift;
2824
2825 // Order of parts on this bend: the horizontal line first, then the vertical line.
2826 if (_is_branch_selected(c)) {
2827 // If this item or one of its children is selected, we draw the line using parent highlight style.
2828 if (!is_no_space) {
2829 if (htotal >= 0) {
2830 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + parent_line_pixel_shift), Point2(parent_pos.x + parent_line_width * 0.5 + parent_line_pixel_shift, root_pos.y + parent_line_pixel_shift), theme_cache.parent_hl_line_color, parent_line_width);
2831 }
2832 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
2833 }
2834
2835 more_prev_ofs = theme_cache.parent_hl_line_margin;
2836 prev_hl_ofs = parent_bottom_y;
2837 has_sibling_selection = _is_sibling_branch_selected(c);
2838 } else if (p_item->is_any_column_selected()) {
2839 // If parent item is selected (but this item is not), we draw the line using children highlight style.
2840 // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
2841 if (has_sibling_selection) {
2842 if (!is_no_space) {
2843 if (htotal >= 0) {
2844 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2i(parent_pos.x + parent_line_width * 0.5 + children_line_pixel_shift, root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
2845 }
2846 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
2847 }
2848
2849 prev_hl_ofs = parent_bottom_y;
2850 } else {
2851 if (!is_no_space) {
2852 if (htotal >= 0) {
2853 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2(parent_pos.x + children_line_width * 0.5 + children_line_pixel_shift, root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
2854 }
2855 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + children_line_pixel_shift, root_pos.y + children_line_width * 0.5 + children_line_pixel_shift), Point2(parent_pos.x + children_line_pixel_shift, prev_ofs + children_line_width * 0.5), theme_cache.children_hl_line_color, children_line_width);
2856 }
2857 }
2858 } else {
2859 // If nothing of the above is true, we draw the line using normal style.
2860 // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
2861 if (has_sibling_selection) {
2862 if (!is_no_space) {
2863 if (htotal >= 0) {
2864 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + theme_cache.parent_hl_line_margin, root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
2865 }
2866 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
2867 }
2868
2869 prev_hl_ofs = parent_bottom_y;
2870 } else {
2871 if (!is_no_space) {
2872 if (htotal >= 0) {
2873 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + line_width * 0.5 + line_pixel_shift, root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
2874 }
2875 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + line_pixel_shift, root_pos.y + line_width * 0.5 + line_pixel_shift), Point2(parent_pos.x + line_pixel_shift, prev_ofs + line_width * 0.5), theme_cache.relationship_line_color, line_width);
2876 }
2877 }
2878 }
2879 } else {
2880 if (_is_branch_selected(c)) {
2881 has_sibling_selection = _is_sibling_branch_selected(c);
2882 }
2883 }
2884
2885 prev_ofs = root_pos.y + more_prev_ofs + line_pixel_shift;
2886 }
2887
2888 if (child_h < 0) {
2889 if (htotal == -1) {
2890 break; // Last loop done, stop.
2891 }
2892
2893 if (theme_cache.draw_relationship_lines == 0) {
2894 return -1; // No need to draw anymore, full stop.
2895 }
2896
2897 htotal = -1;
2898 children_pos.y = theme_cache.offset.y + draw_height;
2899 } else {
2900 htotal += child_h;
2901 children_pos.y += child_h;
2902 }
2903
2904 c = c->next;
2905 }
2906 }
2907
2908 return htotal;
2909 }
2910
2911 int Tree::_count_selected_items(TreeItem *p_from) const {
2912 int count = 0;
2913 for (int i = 0; i < columns.size(); i++) {
2914 if (p_from->is_selected(i)) {
2915 count++;
2916 }
2917 }
2918
2919 for (TreeItem *c = p_from->get_first_child(); c; c = c->get_next()) {
2920 count += _count_selected_items(c);
2921 }
2922
2923 return count;
2924 }
2925
2926 bool Tree::_is_branch_selected(TreeItem *p_from) const {
2927 if (p_from->is_any_column_selected()) {
2928 return true;
2929 }
2930
2931 TreeItem *child_item = p_from->get_first_child();
2932 while (child_item) {
2933 if (_is_branch_selected(child_item)) {
2934 return true;
2935 }
2936 child_item = child_item->get_next();
2937 }
2938
2939 return false;
2940 }
2941
2942 bool Tree::_is_sibling_branch_selected(TreeItem *p_from) const {
2943 TreeItem *sibling_item = p_from->get_next();
2944 while (sibling_item) {
2945 if (_is_branch_selected(sibling_item)) {
2946 return true;
2947 }
2948 sibling_item = sibling_item->get_next();
2949 }
2950
2951 return false;
2952 }
2953
2954 void Tree::select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev, bool *r_in_range, bool p_force_deselect) {
2955 popup_editor->hide();
2956
2957 TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
2958
2959 bool switched = false;
2960 if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
2961 *r_in_range = true;
2962 switched = true;
2963 }
2964
2965 bool emitted_row = false;
2966
2967 for (int i = 0; i < columns.size(); i++) {
2968 TreeItem::Cell &c = p_current->cells.write[i];
2969
2970 if (!c.selectable) {
2971 continue;
2972 }
2973
2974 if (select_mode == SELECT_ROW) {
2975 if (p_selected == p_current && (!c.selected || allow_reselect)) {
2976 c.selected = true;
2977 selected_item = p_selected;
2978 if (!emitted_row) {
2979 emit_signal(SceneStringName(item_selected));
2980 emitted_row = true;
2981 }
2982 } else if (c.selected) {
2983 if (p_selected != p_current) {
2984 // Deselect other rows.
2985 c.selected = false;
2986 }
2987 }
2988 if (&selected_cell == &c) {
2989 selected_col = i;
2990 }
2991 } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
2992 if (!r_in_range && &selected_cell == &c) {
2993 if (!selected_cell.selected || allow_reselect) {
2994 selected_cell.selected = true;
2995
2996 selected_item = p_selected;
2997 selected_col = i;
2998 selected_button = -1;
2999
3000 emit_signal(SNAME("cell_selected"));
3001 if (select_mode == SELECT_MULTI) {
3002 emit_signal(SNAME("multi_selected"), p_current, i, true);
3003 } else if (select_mode == SELECT_SINGLE) {
3004 emit_signal(SceneStringName(item_selected));
3005 }
3006
3007 } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
3008 selected_item = p_selected;
3009 selected_col = i;
3010 selected_button = -1;
3011 emit_signal(SNAME("cell_selected"));
3012 }
3013 } else {
3014 if (r_in_range && *r_in_range && !p_force_deselect) {
3015 if (!c.selected && c.selectable) {
3016 c.selected = true;
3017 emit_signal(SNAME("multi_selected"), p_current, i, true);
3018 }
3019
3020 } else if (!r_in_range || p_force_deselect) {
3021 if (select_mode == SELECT_MULTI && c.selected) {
3022 emit_signal(SNAME("multi_selected"), p_current, i, false);
3023 }
3024 c.selected = false;
3025 }
3026 }
3027 }
3028 }
3029
3030 if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
3031 *r_in_range = false;
3032 }
3033
3034 TreeItem *c = p_current->first_child;
3035
3036 while (c) {
3037 if (c->is_visible()) {
3038 select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
3039 }
3040 c = c->next;
3041 }
3042 queue_accessibility_update();
3043 }
3044
3045 Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
3046 return Rect2();
3047 }
3048
3049 void Tree::_range_click_timeout() {
3050 if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
3051 Point2 pos = get_local_mouse_position() - theme_cache.panel_style->get_offset();
3052 if (show_column_titles) {
3053 pos.y -= _get_title_button_height();
3054
3055 if (pos.y < 0) {
3056 range_click_timer->stop();
3057 return;
3058 }
3059 }
3060
3061 if (!root) {
3062 return;
3063 }
3064
3065 click_handled = false;
3066 Ref<InputEventMouseButton> mb;
3067 mb.instantiate();
3068
3069 int x_limit = _get_content_rect().size.x;
3070
3071 cache.rtl = is_layout_rtl();
3072
3073 propagate_mouse_activated = false; // Done from outside, so signal handler can't clear the tree in the middle of emit (which is a common case).
3074 blocked++;
3075 propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, false, root, MouseButton::LEFT, mb);
3076 blocked--;
3077
3078 if (range_click_timer->is_one_shot()) {
3079 range_click_timer->set_wait_time(0.05);
3080 range_click_timer->set_one_shot(false);
3081 range_click_timer->start();
3082 }
3083
3084 if (!click_handled) {
3085 range_click_timer->stop();
3086 }
3087
3088 if (propagate_mouse_activated) {
3089 emit_signal(SNAME("item_activated"));
3090 propagate_mouse_activated = false;
3091 }
3092
3093 } else {
3094 range_click_timer->stop();
3095 }
3096 }
3097
3098 int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int x_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod) {
3099 if (p_item && !p_item->is_visible_in_tree()) {
3100 // Skip any processing of invisible items.
3101 return 0;
3102 }
3103 if (p_pos.x > x_limit) {
3104 // Inside the scroll area.
3105 return -1;
3106 }
3107
3108 int item_h = compute_item_height(p_item) + theme_cache.v_separation;
3109
3110 bool skip = (p_item == root && hide_root);
3111
3112 if (!skip && p_pos.y < item_h) {
3113 // Check event!
3114 if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
3115 return -1;
3116 }
3117
3118 if (!p_item->disable_folding && !hide_folding && p_item->first_child && (p_pos.x < (x_ofs + theme_cache.item_margin))) {
3119 if (enable_recursive_folding && p_mod->is_shift_pressed()) {
3120 p_item->set_collapsed_recursive(!p_item->is_collapsed());
3121 } else {
3122 p_item->set_collapsed(!p_item->is_collapsed());
3123 }
3124 return -1;
3125 }
3126
3127 FindColumnButtonResult find_result = _find_column_and_button_at_pos(p_pos.x, p_item, x_ofs, x_limit);
3128
3129 if (find_result.column_index == -1) {
3130 return -1;
3131 }
3132
3133 int col = find_result.column_index;
3134 int col_width = find_result.column_width;
3135 int col_ofs = find_result.column_offset;
3136 int x = find_result.pos_x;
3137 const TreeItem::Cell &c = p_item->cells[col];
3138
3139 if (find_result.button_index >= 0) {
3140 if (c.buttons[find_result.button_index].disabled) {
3141 pressed_button = -1;
3142 cache.click_type = Cache::CLICK_NONE;
3143 return -1;
3144 }
3145
3146 // Make sure the click is correct.
3147 const Point2 click_pos = get_local_mouse_position();
3148 if (!get_item_at_position(click_pos)) {
3149 pressed_button = -1;
3150 cache.click_type = Cache::CLICK_NONE;
3151 return -1;
3152 }
3153
3154 pressed_button = find_result.button_index;
3155 cache.click_type = Cache::CLICK_BUTTON;
3156 cache.click_index = find_result.button_index;
3157 cache.click_id = c.buttons[find_result.button_index].id;
3158 cache.click_item = p_item;
3159 cache.click_column = col;
3160 cache.click_pos = click_pos;
3161 queue_redraw();
3162 return -1;
3163 }
3164
3165 if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_first_child()) {
3166 if (enable_recursive_folding && p_mod->is_shift_pressed()) {
3167 p_item->set_collapsed_recursive(!p_item->is_collapsed());
3168 } else {
3169 p_item->set_collapsed(!p_item->is_collapsed());
3170 }
3171 return -1; // Collapse/uncollapse, because nothing can be done with the item.
3172 }
3173
3174 bool already_selected = c.selected;
3175 bool already_cursor = (p_item == selected_item) && col == selected_col;
3176
3177 if (p_button == MouseButton::LEFT || (p_button == MouseButton::RIGHT && allow_rmb_select)) {
3178 // Process selection.
3179
3180 if (p_double_click && (!c.editable || c.mode == TreeItem::CELL_MODE_CUSTOM || c.mode == TreeItem::CELL_MODE_ICON)) {
3181 // Emits the `item_activated` signal.
3182 propagate_mouse_activated = true;
3183
3184 incr_search.clear();
3185 return -1;
3186 }
3187
3188 if (c.selectable) {
3189 if (select_mode == SELECT_MULTI && p_mod->is_command_or_control_pressed()) {
3190 if (c.selected && p_button == MouseButton::LEFT) {
3191 p_item->deselect(col);
3192 emit_signal(SNAME("multi_selected"), p_item, col, false);
3193 } else {
3194 p_item->select(col);
3195 emit_signal(SNAME("multi_selected"), p_item, col, true);
3196 emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
3197 }
3198 } else {
3199 if (select_mode == SELECT_MULTI && p_mod->is_shift_pressed() && selected_item && selected_item != p_item) {
3200 bool inrange = false;
3201
3202 select_single_item(p_item, root, col, selected_item, &inrange);
3203 emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
3204 } else {
3205 int icount = _count_selected_items(root);
3206
3207 if (select_mode == SELECT_MULTI && icount > 1 && p_button != MouseButton::RIGHT) {
3208 if (!already_selected) {
3209 select_single_item(p_item, root, col);
3210 }
3211 single_select_defer = p_item;
3212 single_select_defer_column = col;
3213 } else {
3214 if (p_button != MouseButton::RIGHT || !c.selected) {
3215 select_single_item(p_item, root, col);
3216 }
3217
3218 emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
3219 }
3220 }
3221 queue_accessibility_update();
3222 queue_redraw();
3223 }
3224 }
3225 }
3226
3227 if (!c.editable) {
3228 return -1; // If cell is not editable, don't bother.
3229 }
3230
3231 // Editing.
3232 bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
3233 String editor_text = c.text;
3234
3235 switch (c.mode) {
3236 case TreeItem::CELL_MODE_STRING: {
3237 // Nothing in particular.
3238
3239 if (select_mode == SELECT_MULTI && (get_viewport()->get_processed_events_count() == focus_in_id || !already_cursor)) {
3240 bring_up_editor = false;
3241 }
3242
3243 } break;
3244 case TreeItem::CELL_MODE_CHECK: {
3245 bring_up_editor = false; // Checkboxes are not edited with editor.
3246 if (force_edit_checkbox_only_on_checkbox) {
3247 if (x < theme_cache.checked->get_width()) {
3248 p_item->set_checked(col, !c.checked);
3249 item_edited(col, p_item, p_button);
3250 }
3251 } else {
3252 p_item->set_checked(col, !c.checked);
3253 item_edited(col, p_item, p_button);
3254 }
3255 click_handled = true;
3256
3257 } break;
3258 case TreeItem::CELL_MODE_RANGE: {
3259 if (!c.text.is_empty()) {
3260 _update_popup_menu(c);
3261 popup_menu->set_size(Size2(col_width, 0));
3262
3263 int pos_x = 0;
3264 Point2 panel_ofs = theme_cache.panel_style->get_offset();
3265 if (is_layout_rtl()) {
3266 pos_x = get_size().width - col_ofs - panel_ofs.x - popup_menu->get_size().width;
3267 } else {
3268 pos_x = col_ofs + panel_ofs.x;
3269 }
3270 popup_menu->set_position(get_screen_position() + Point2i(pos_x, _get_title_button_height() + y_ofs + item_h + panel_ofs.y) - theme_cache.offset);
3271
3272 popup_menu->popup();
3273 popup_edited_item = p_item;
3274 popup_edited_item_col = col;
3275 bring_up_editor = false;
3276 } else {
3277 if (x >= (col_width - item_h / 2)) {
3278 // Touching the combo.
3279 bool up = p_pos.y < (item_h / 2);
3280
3281 if (p_button == MouseButton::LEFT) {
3282 if (range_click_timer->get_time_left() == 0) {
3283 range_item_last = p_item;
3284 range_up_last = up;
3285
3286 range_click_timer->set_wait_time(0.6);
3287 range_click_timer->set_one_shot(true);
3288 range_click_timer->start();
3289
3290 } else if (up != range_up_last) {
3291 return -1; // Break. Avoid changing direction on mouse held.
3292 }
3293
3294 p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
3295
3296 item_edited(col, p_item, p_button);
3297
3298 } else if (p_button == MouseButton::RIGHT) {
3299 p_item->set_range(col, (up ? c.max : c.min));
3300 item_edited(col, p_item, p_button);
3301 } else if (p_button == MouseButton::WHEEL_UP) {
3302 p_item->set_range(col, c.val + c.step);
3303 item_edited(col, p_item, p_button);
3304 } else if (p_button == MouseButton::WHEEL_DOWN) {
3305 p_item->set_range(col, c.val - c.step);
3306 item_edited(col, p_item, p_button);
3307 }
3308
3309 bring_up_editor = false;
3310
3311 } else {
3312 editor_text = String::num(p_item->cells[col].val, Math::range_step_decimals(p_item->cells[col].step));
3313 if (select_mode == SELECT_MULTI && get_viewport()->get_processed_events_count() == focus_in_id) {
3314 bring_up_editor = false;
3315 }
3316 }
3317 }
3318 click_handled = true;
3319
3320 } break;
3321 case TreeItem::CELL_MODE_ICON: {
3322 bring_up_editor = false;
3323 } break;
3324 case TreeItem::CELL_MODE_CUSTOM: {
3325 edited_item = p_item;
3326 edited_col = col;
3327 bool on_arrow = x > col_width - theme_cache.select_arrow->get_width();
3328
3329 custom_popup_rect = Rect2i(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h - theme_cache.offset.y), Size2(get_column_width(col), item_h));
3330
3331 if (on_arrow || !p_item->cells[col].custom_button) {
3332 emit_signal(SNAME("custom_popup_edited"), ((bool)(x >= (col_width - item_h / 2))));
3333 }
3334
3335 if (!p_item->cells[col].custom_button || !on_arrow) {
3336 item_edited(col, p_item, p_button);
3337 }
3338 click_handled = true;
3339 return -1;
3340 } break;
3341 };
3342
3343 if (!bring_up_editor || p_button != MouseButton::LEFT) {
3344 return -1;
3345 }
3346
3347 click_handled = true;
3348 popup_pressing_edited_item = p_item;
3349 popup_pressing_edited_item_column = col;
3350 pressing_for_editor = true;
3351
3352 return -1; // Select.
3353 } else {
3354 Point2i new_pos = p_pos;
3355
3356 if (!skip) {
3357 x_ofs += theme_cache.item_margin;
3358 y_ofs += item_h;
3359 new_pos.y -= item_h;
3360 }
3361
3362 if (!p_item->collapsed) { // If not collapsed, check the children.
3363
3364 TreeItem *c = p_item->first_child;
3365
3366 while (c) {
3367 int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, x_limit, p_double_click, c, p_button, p_mod);
3368
3369 if (child_h < 0) {
3370 return -1; // Break, stop propagating, no need to anymore.
3371 }
3372
3373 new_pos.y -= child_h;
3374 y_ofs += child_h;
3375 c = c->next;
3376 item_h += child_h;
3377 }
3378 }
3379 if (p_item == root) {
3380 emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), p_button);
3381 }
3382 }
3383
3384 return item_h; // Nothing found.
3385 }
3386
3387 void Tree::_text_editor_popup_modal_close() {
3388 if (popup_edit_committed) {
3389 return; // Already processed by `LineEdit` / `TextEdit` commit.
3390 }
3391
3392 if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
3393 return; // ESC pressed, app focus lost, or forced close from code.
3394 }
3395
3396 if (value_editor && value_editor->has_point(value_editor->get_local_mouse_position())) {
3397 return;
3398 }
3399
3400 if (!popup_edited_item) {
3401 return;
3402 }
3403
3404 if (popup_edited_item->is_edit_multiline(popup_edited_item_col) && popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_STRING) {
3405 _apply_multiline_edit();
3406 } else {
3407 _line_editor_submit(line_editor->get_text());
3408 }
3409 }
3410
3411 void Tree::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
3412 if (popup_edit_committed) {
3413 return; // Already processed by `_text_editor_popup_modal_close`.
3414 }
3415
3416 if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
3417 return; // ESC pressed, app focus lost, or forced close from code.
3418 }
3419
3420 if (p_event->is_action_pressed("ui_text_newline_blank", true)) {
3421 accept_event();
3422 } else if (p_event->is_action_pressed("ui_text_newline")) {
3423 bool hide_focus = !text_editor->has_focus(true);
3424 popup_edit_committed = true; // End edit popup processing.
3425 popup_editor->hide();
3426 _apply_multiline_edit(hide_focus);
3427 accept_event();
3428 }
3429 }
3430
3431 void Tree::_apply_multiline_edit(bool p_hide_focus) {
3432 if (!popup_edited_item) {
3433 return;
3434 }
3435
3436 if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
3437 return;
3438 }
3439
3440 TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
3441 switch (c.mode) {
3442 case TreeItem::CELL_MODE_STRING: {
3443 c.text = text_editor->get_text();
3444 } break;
3445 default: {
3446 ERR_FAIL();
3447 }
3448 }
3449
3450 grab_focus(p_hide_focus);
3451 item_edited(popup_edited_item_col, popup_edited_item);
3452 queue_redraw();
3453 }
3454
3455 void Tree::_line_editor_submit(String p_text) {
3456 if (popup_edit_committed) {
3457 return; // Already processed by _text_editor_popup_modal_close.
3458 }
3459
3460 if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
3461 return; // ESC pressed, app focus lost, or forced close from code.
3462 }
3463
3464 bool hide_focus = !line_editor->has_focus(true);
3465
3466 popup_edit_committed = true; // End edit popup processing.
3467 popup_editor->hide();
3468
3469 if (!popup_edited_item) {
3470 return;
3471 }
3472
3473 if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
3474 return;
3475 }
3476
3477 TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
3478 switch (c.mode) {
3479 case TreeItem::CELL_MODE_STRING: {
3480 c.text = p_text;
3481 } break;
3482 case TreeItem::CELL_MODE_RANGE: {
3483 c.val = p_text.to_float();
3484 if (c.step > 0) {
3485 c.val = Math::snapped(c.val, c.step);
3486 }
3487 if (c.val < c.min) {
3488 c.val = c.min;
3489 } else if (c.val > c.max) {
3490 c.val = c.max;
3491 }
3492 } break;
3493 default: {
3494 ERR_FAIL();
3495 }
3496 }
3497
3498 grab_focus(hide_focus);
3499 item_edited(popup_edited_item_col, popup_edited_item);
3500 queue_redraw();
3501 }
3502
3503 void Tree::value_editor_changed(double p_value) {
3504 if (updating_value_editor) {
3505 return;
3506 }
3507 if (!popup_edited_item) {
3508 return;
3509 }
3510
3511 const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
3512
3513 line_editor->set_text(String::num(p_value, Math::range_step_decimals(c.step)));
3514
3515 queue_redraw();
3516 }
3517
3518 void Tree::_update_popup_menu(const TreeItem::Cell &p_cell) {
3519 if (popup_menu == nullptr) {
3520 popup_menu = memnew(PopupMenu);
3521 popup_menu->set_shrink_width(false);
3522 popup_menu->hide();
3523 add_child(popup_menu, false, INTERNAL_MODE_FRONT);
3524 popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &Tree::popup_select));
3525 }
3526 popup_menu->clear();
3527 for (int i = 0; i < p_cell.text.get_slice_count(","); i++) {
3528 String s = p_cell.text.get_slicec(',', i);
3529 popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).is_empty() ? i : s.get_slicec(':', 1).to_int());
3530 }
3531 }
3532
3533 void Tree::_update_value_editor(const TreeItem::Cell &p_cell) {
3534 if (value_editor == nullptr) {
3535 value_editor = memnew(HSlider);
3536 value_editor->set_v_size_flags(SIZE_EXPAND_FILL);
3537 value_editor->hide();
3538 popup_editor_vb->add_child(value_editor);
3539 value_editor->connect(SceneStringName(value_changed), callable_mp(this, &Tree::value_editor_changed));
3540 }
3541 updating_value_editor = true;
3542 value_editor->set_min(p_cell.min);
3543 value_editor->set_max(p_cell.max);
3544 value_editor->set_step(p_cell.step);
3545 value_editor->set_value(p_cell.val);
3546 value_editor->set_exp_ratio(p_cell.expr);
3547 updating_value_editor = false;
3548 }
3549
3550 void Tree::popup_select(int p_option) {
3551 if (!popup_edited_item) {
3552 return;
3553 }
3554
3555 if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
3556 return;
3557 }
3558
3559 popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
3560 queue_redraw();
3561 item_edited(popup_edited_item_col, popup_edited_item);
3562 }
3563
3564 void Tree::_go_left() {
3565 if (get_tree()->is_accessibility_enabled() && selected_button >= 0) {
3566 selected_button--;
3567 } else if (selected_col == 0) {
3568 selected_button = -1;
3569 if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
3570 selected_item->set_collapsed(true);
3571 } else {
3572 if (columns.size() == 1) { // Goto parent with one column.
3573 TreeItem *parent = selected_item->get_parent();
3574 if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
3575 select_single_item(parent, get_root(), selected_col);
3576 }
3577 } else if (selected_item->get_prev_visible()) {
3578 selected_col = columns.size() - 1;
3579 _go_up(); // Go to upper column if possible.
3580 }
3581 }
3582 } else {
3583 selected_button = -1;
3584 if (select_mode == SELECT_MULTI) {
3585 selected_col--;
3586 emit_signal(SNAME("cell_selected"));
3587 } else {
3588 selected_item->select(selected_col - 1);
3589 }
3590 }
3591 queue_accessibility_update();
3592 queue_redraw();
3593 accept_event();
3594 ensure_cursor_is_visible();
3595 }
3596
3597 void Tree::_go_right() {
3598 int buttons = (selected_item && selected_col >= 0 && selected_col < columns.size()) ? selected_item->cells[selected_col].buttons.size() : 0;
3599 if (get_tree()->is_accessibility_enabled() && selected_button < buttons - 1) {
3600 selected_button++;
3601 } else if (selected_col == (columns.size() - 1)) {
3602 selected_button = -1;
3603 if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
3604 selected_item->set_collapsed(false);
3605 } else if (selected_item->get_next_visible()) {
3606 selected_col = 0;
3607 _go_down();
3608 }
3609 } else {
3610 selected_button = -1;
3611 if (select_mode == SELECT_MULTI) {
3612 selected_col++;
3613 emit_signal(SNAME("cell_selected"));
3614 } else {
3615 selected_item->select(selected_col + 1);
3616 }
3617 }
3618 queue_accessibility_update();
3619 queue_redraw();
3620 ensure_cursor_is_visible();
3621 accept_event();
3622 }
3623
3624 void Tree::_go_up() {
3625 TreeItem *prev = nullptr;
3626 if (!selected_item) {
3627 prev = get_last_item();
3628 selected_col = 0;
3629 selected_button = -1;
3630 } else {
3631 prev = selected_item->get_prev_visible();
3632 }
3633
3634 int col = MAX(selected_col, 0);
3635
3636 if (select_mode == SELECT_MULTI) {
3637 if (!prev) {
3638 return;
3639 }
3640
3641 selected_item = prev;
3642 emit_signal(SNAME("cell_selected"));
3643 queue_redraw();
3644 } else {
3645 while (prev && !prev->cells[col].selectable) {
3646 prev = prev->get_prev_visible();
3647 }
3648 if (!prev) {
3649 return; // Do nothing.
3650 }
3651 prev->select(col);
3652 }
3653
3654 queue_accessibility_update();
3655 ensure_cursor_is_visible();
3656 accept_event();
3657 }
3658
3659 void Tree::_shift_select_range(TreeItem *new_item) {
3660 if (!new_item) {
3661 new_item = selected_item;
3662 }
3663 int s_col = selected_col;
3664 bool in_range = false;
3665 TreeItem *item = root;
3666
3667 if (!shift_anchor) {
3668 shift_anchor = selected_item;
3669 }
3670
3671 while (item) {
3672 bool at_range_edge = item == shift_anchor || item == new_item;
3673 if (at_range_edge) {
3674 in_range = !in_range;
3675 }
3676 if (new_item == shift_anchor) {
3677 in_range = false;
3678 }
3679 if (item->is_visible_in_tree()) {
3680 if (in_range || at_range_edge) {
3681 if (!item->is_selected(selected_col) && item->is_selectable(selected_col)) {
3682 item->select(selected_col);
3683 emit_signal(SNAME("multi_selected"), item, selected_col, true);
3684 }
3685 } else if (item->is_selected(selected_col)) {
3686 item->deselect(selected_col);
3687 emit_signal(SNAME("multi_selected"), item, selected_col, false);
3688 }
3689 }
3690 item = item->get_next_in_tree(false);
3691 }
3692
3693 selected_item = new_item;
3694 selected_col = s_col;
3695 ensure_cursor_is_visible();
3696 queue_redraw();
3697 accept_event();
3698 }
3699
3700 void Tree::_go_down() {
3701 TreeItem *next = nullptr;
3702 if (!selected_item) {
3703 if (root) {
3704 next = hide_root ? root->get_next_visible() : root;
3705 }
3706 } else {
3707 next = selected_item->get_next_visible();
3708 }
3709
3710 int col = MAX(selected_col, 0);
3711
3712 if (select_mode == SELECT_MULTI) {
3713 if (!next) {
3714 return;
3715 }
3716
3717 selected_item = next;
3718 emit_signal(SNAME("cell_selected"));
3719 queue_redraw();
3720 } else {
3721 while (next && !next->cells[col].selectable) {
3722 next = next->get_next_visible();
3723 }
3724 if (!next) {
3725 return; // Do nothing.
3726 }
3727 next->select(col);
3728 }
3729
3730 queue_accessibility_update();
3731 ensure_cursor_is_visible();
3732 accept_event();
3733 }
3734
3735 bool Tree::_scroll(bool p_horizontal, float p_pages) {
3736 ScrollBar *scroll = p_horizontal ? (ScrollBar *)h_scroll : (ScrollBar *)v_scroll;
3737
3738 double prev_value = scroll->get_value();
3739 scroll->set_value(scroll->get_value() + scroll->get_page() * p_pages);
3740
3741 bool scroll_happened = scroll->get_value() != prev_value;
3742 if (scroll_happened) {
3743 _determine_hovered_item();
3744 }
3745 return scroll_happened;
3746 }
3747
3748 Rect2 Tree::_get_scrollbar_layout_rect() const {
3749 const Size2 control_size = get_size();
3750 const Ref<StyleBox> background = theme_cache.panel_style;
3751
3752 // This is the background stylebox's content rect.
3753 const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
3754 const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
3755 const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
3756
3757 // Use the stylebox's margins by default. Can be overridden by `scrollbar_margin_*`.
3758 const real_t top = theme_cache.scrollbar_margin_top < 0 ? content_rect.get_position().y : theme_cache.scrollbar_margin_top;
3759 const real_t right = theme_cache.scrollbar_margin_right < 0 ? content_rect.get_end().x : (control_size.x - theme_cache.scrollbar_margin_right);
3760 const real_t bottom = theme_cache.scrollbar_margin_bottom < 0 ? content_rect.get_end().y : (control_size.y - theme_cache.scrollbar_margin_bottom);
3761 const real_t left = theme_cache.scrollbar_margin_left < 0 ? content_rect.get_position().x : theme_cache.scrollbar_margin_left;
3762
3763 return Rect2(left, top, right - left, bottom - top);
3764 }
3765
3766 Rect2 Tree::_get_content_rect() const {
3767 const Size2 control_size = get_size();
3768 const Ref<StyleBox> background = theme_cache.panel_style;
3769
3770 // This is the background stylebox's content rect.
3771 const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
3772 const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
3773 const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
3774
3775 // Scrollbars won't affect Tree's content rect if they're not visible or placed inside the stylebox margin area.
3776 const real_t v_size = v_scroll->is_visible() ? (v_scroll->get_combined_minimum_size().x + theme_cache.scrollbar_h_separation) : 0;
3777 const real_t h_size = h_scroll->is_visible() ? (h_scroll->get_combined_minimum_size().y + theme_cache.scrollbar_v_separation) : 0;
3778 const Point2 scroll_begin = _get_scrollbar_layout_rect().get_end() - Vector2(v_size, h_size);
3779 const Size2 offset = (content_rect.get_end() - scroll_begin).maxf(0);
3780
3781 return content_rect.grow_individual(0, 0, -offset.x, -offset.y);
3782 }
3783
3784 void Tree::gui_input(const Ref<InputEvent> &p_event) {
3785 ERR_FAIL_COND(p_event.is_null());
3786
3787 Ref<InputEventKey> k = p_event;
3788
3789 if (k.is_valid() && k->get_keycode() == Key::SHIFT && !k->is_pressed()) {
3790 shift_anchor = nullptr;
3791 }
3792
3793 bool is_command = k.is_valid() && k->is_command_or_control_pressed();
3794 if (p_event->is_action(cache.rtl ? "ui_left" : "ui_right") && p_event->is_pressed()) {
3795 if (!cursor_can_exit_tree) {
3796 accept_event();
3797 }
3798
3799 if (!selected_item || selected_col > (columns.size() - 1)) {
3800 return;
3801 }
3802
3803 if (k.is_valid() && k->is_shift_pressed()) {
3804 selected_item->set_collapsed_recursive(false);
3805 } else if (select_mode != SELECT_ROW) {
3806 _go_right();
3807 } else if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
3808 selected_item->set_collapsed(false);
3809 } else {
3810 _go_down();
3811 }
3812 } else if (p_event->is_action(cache.rtl ? "ui_right" : "ui_left") && p_event->is_pressed()) {
3813 if (!cursor_can_exit_tree) {
3814 accept_event();
3815 }
3816
3817 if (!selected_item || selected_col < 0) {
3818 return;
3819 }
3820
3821 if (k.is_valid() && k->is_shift_pressed()) {
3822 selected_item->set_collapsed_recursive(true);
3823 } else if (select_mode != SELECT_ROW) {
3824 _go_left();
3825 } else if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
3826 selected_item->set_collapsed(true);
3827 } else {
3828 _go_up();
3829 }
3830 } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !is_command) {
3831 if (!cursor_can_exit_tree) {
3832 accept_event();
3833 }
3834 // Shift Up Selection.
3835 if (k.is_valid() && k->is_shift_pressed() && selected_item && select_mode == SELECT_MULTI) {
3836 TreeItem *new_item = selected_item->get_prev_visible(false);
3837 _shift_select_range(new_item);
3838 } else {
3839 _go_up();
3840 }
3841
3842 } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !is_command) {
3843 if (!cursor_can_exit_tree) {
3844 accept_event();
3845 }
3846 // Shift Down Selection.
3847 if (k.is_valid() && k->is_shift_pressed() && selected_item && select_mode == SELECT_MULTI) {
3848 TreeItem *new_item = selected_item->get_next_visible(false);
3849 _shift_select_range(new_item);
3850 } else {
3851 _go_down();
3852 }
3853 } else if (p_event->is_action("ui_menu") && p_event->is_pressed()) {
3854 if (allow_rmb_select && selected_item) {
3855 emit_signal(SNAME("item_mouse_selected"), get_item_rect(selected_item).position, MouseButton::RIGHT);
3856 }
3857
3858 accept_event();
3859 } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
3860 if (!cursor_can_exit_tree) {
3861 accept_event();
3862 }
3863
3864 TreeItem *next = nullptr;
3865 if (!selected_item) {
3866 return;
3867 }
3868 next = selected_item;
3869
3870 for (int i = 0; i < 10; i++) {
3871 TreeItem *_n = next->get_next_visible();
3872 if (_n) {
3873 next = _n;
3874 } else {
3875 break;
3876 }
3877 }
3878 if (next == selected_item) {
3879 return;
3880 }
3881
3882 if (select_mode == SELECT_MULTI) {
3883 selected_item = next;
3884 emit_signal(SNAME("cell_selected"));
3885 queue_accessibility_update();
3886 queue_redraw();
3887 } else {
3888 while (next && !next->cells[selected_col].selectable) {
3889 next = next->get_next_visible();
3890 }
3891 if (!next) {
3892 return; // Do nothing.
3893 }
3894 next->select(selected_col);
3895 }
3896
3897 ensure_cursor_is_visible();
3898 } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
3899 if (!cursor_can_exit_tree) {
3900 accept_event();
3901 }
3902
3903 TreeItem *prev = nullptr;
3904 if (!selected_item) {
3905 return;
3906 }
3907 prev = selected_item;
3908
3909 for (int i = 0; i < 10; i++) {
3910 TreeItem *_n = prev->get_prev_visible();
3911 if (_n) {
3912 prev = _n;
3913 } else {
3914 break;
3915 }
3916 }
3917 if (prev == selected_item) {
3918 return;
3919 }
3920
3921 if (select_mode == SELECT_MULTI) {
3922 selected_item = prev;
3923 emit_signal(SNAME("cell_selected"));
3924 queue_accessibility_update();
3925 queue_redraw();
3926 } else {
3927 while (prev && !prev->cells[selected_col].selectable) {
3928 prev = prev->get_prev_visible();
3929 }
3930 if (!prev) {
3931 return; // Do nothing.
3932 }
3933 prev->select(selected_col);
3934 }
3935 ensure_cursor_is_visible();
3936 } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
3937 if (select_mode == SELECT_MULTI) {
3938 if (!selected_item) {
3939 return;
3940 }
3941 if (selected_item && selected_col != -1 && selected_button != -1) {
3942 const TreeItem::Cell &c = selected_item->cells[selected_col];
3943 emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
3944 } else if (selected_item->is_selected(selected_col)) {
3945 selected_item->deselect(selected_col);
3946 emit_signal(SNAME("multi_selected"), selected_item, selected_col, false);
3947 } else if (selected_item->is_selectable(selected_col)) {
3948 selected_item->select(selected_col);
3949 emit_signal(SNAME("multi_selected"), selected_item, selected_col, true);
3950 }
3951 } else if (selected_item && selected_col != -1 && selected_button != -1) {
3952 const TreeItem::Cell &c = selected_item->cells[selected_col];
3953 emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
3954 }
3955 accept_event();
3956 } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
3957 if (selected_item) {
3958 // Bring up editor if possible.
3959 if (selected_item && selected_col != -1 && selected_button != -1) {
3960 const TreeItem::Cell &c = selected_item->cells[selected_col];
3961 emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
3962 } else if (!edit_selected()) {
3963 emit_signal(SNAME("item_activated"));
3964 incr_search.clear();
3965 }
3966 }
3967 accept_event();
3968 }
3969
3970 if (allow_search && k.is_valid()) { // Incremental search.
3971
3972 if (!k->is_pressed()) {
3973 return;
3974 }
3975 if (k->is_command_or_control_pressed() || (k->is_shift_pressed() && k->get_unicode() == 0) || k->is_meta_pressed()) {
3976 return;
3977 }
3978 if (!root) {
3979 return;
3980 }
3981
3982 if (hide_root && !root->get_next_visible()) {
3983 return;
3984 }
3985
3986 if (k->get_unicode() > 0) {
3987 _do_incr_search(String::chr(k->get_unicode()));
3988 accept_event();
3989
3990 return;
3991 } else {
3992 if (k->get_keycode() != Key::SHIFT) {
3993 last_keypress = 0;
3994 }
3995 }
3996 }
3997
3998 Ref<InputEventMouseMotion> mm = p_event;
3999 if (mm.is_valid()) {
4000 hovered_pos = mm->get_position();
4001 _determine_hovered_item();
4002
4003 bool rtl = is_layout_rtl();
4004 if (pressing_for_editor && popup_pressing_edited_item && !popup_pressing_edited_item->cells.is_empty() && (popup_pressing_edited_item->get_cell_mode(popup_pressing_edited_item_column) == TreeItem::CELL_MODE_RANGE)) {
4005 // This needs to happen now, because the popup can be closed when pressing another item, and must remain the popup edited item until it actually closes.
4006 popup_edited_item = popup_pressing_edited_item;
4007 popup_edited_item_col = popup_pressing_edited_item_column;
4008
4009 popup_pressing_edited_item = nullptr;
4010 popup_pressing_edited_item_column = -1;
4011
4012 if (!range_drag_enabled) {
4013 // Range drag.
4014 Vector2 cpos = mm->get_position();
4015 if (rtl) {
4016 cpos.x = get_size().width - cpos.x;
4017 }
4018 if (cpos.distance_to(pressing_pos) > 2) {
4019 range_drag_enabled = true;
4020 range_drag_capture_pos = cpos;
4021 range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
4022 Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
4023 }
4024 } else {
4025 const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
4026 float diff_y = -mm->get_relative().y;
4027 diff_y = Math::pow(Math::abs(diff_y), 1.8f) * SIGN(diff_y);
4028 diff_y *= 0.1;
4029 range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
4030 popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
4031 item_edited(popup_edited_item_col, popup_edited_item);
4032 }
4033 }
4034
4035 if (drag_touching && !drag_touching_deaccel) {
4036 drag_accum -= mm->get_relative().y;
4037 v_scroll->set_value(drag_from + drag_accum);
4038 drag_speed = -mm->get_velocity().y;
4039 }
4040 }
4041
4042 Ref<InputEventMouseButton> mb = p_event;
4043 if (mb.is_valid()) {
4044 bool rtl = is_layout_rtl();
4045
4046 if (!mb->is_pressed()) {
4047 if (mb->get_button_index() == MouseButton::LEFT ||
4048 mb->get_button_index() == MouseButton::RIGHT) {
4049 Point2 pos = mb->get_position();
4050 if (rtl) {
4051 pos.x = get_size().width - pos.x - 1;
4052 }
4053 pos -= theme_cache.panel_style->get_offset();
4054 if (show_column_titles) {
4055 pos.y -= _get_title_button_height();
4056
4057 if (pos.y < 0) {
4058 pos.x += theme_cache.offset.x;
4059 int len = 0;
4060 for (int i = 0; i < columns.size(); i++) {
4061 len += get_column_width(i);
4062 if (pos.x < static_cast<real_t>(len)) {
4063 emit_signal(SNAME("column_title_clicked"), i, mb->get_button_index());
4064 break;
4065 }
4066 }
4067 }
4068 }
4069 }
4070
4071 if (mb->get_button_index() == MouseButton::LEFT) {
4072 if (single_select_defer) {
4073 select_single_item(single_select_defer, root, single_select_defer_column);
4074 single_select_defer = nullptr;
4075 }
4076
4077 range_click_timer->stop();
4078
4079 if (pressing_for_editor) {
4080 if (range_drag_enabled) {
4081 range_drag_enabled = false;
4082 Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
4083 warp_mouse(range_drag_capture_pos);
4084 } else {
4085 Rect2 rect = _get_item_focus_rect(get_selected());
4086 Point2 mpos = mb->get_position();
4087 int icon_size_x = 0;
4088 Ref<Texture2D> icon = get_selected()->get_icon(selected_col);
4089 if (icon.is_valid()) {
4090 icon_size_x = _get_cell_icon_size(get_selected()->cells[selected_col]).x;
4091 }
4092 // Icon is treated as if it is outside of the rect so that double clicking on it will emit the `item_icon_double_clicked` signal.
4093 if (rtl) {
4094 mpos.x = get_size().width - (mpos.x + icon_size_x);
4095 } else {
4096 mpos.x -= icon_size_x;
4097 }
4098 if (rect.has_point(mpos)) {
4099 if (!edit_selected()) {
4100 emit_signal(SNAME("item_icon_double_clicked"));
4101 }
4102 } else {
4103 emit_signal(SNAME("item_icon_double_clicked"));
4104 }
4105 }
4106 pressing_for_editor = false;
4107 }
4108
4109 if (drag_touching) {
4110 if (drag_speed == 0) {
4111 drag_touching_deaccel = false;
4112 drag_touching = false;
4113 set_process_internal(false);
4114 } else {
4115 drag_touching_deaccel = true;
4116 }
4117 }
4118 }
4119
4120 if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item != nullptr) {
4121 // Make sure the reference is current in case TreeItems is reconstructed.
4122 cache.click_item = get_item_at_position(cache.click_pos);
4123
4124 // Only emit the event if the click is still within the button rect.
4125 TreeItem *current_item;
4126 int current_column, current_index, current_section;
4127 _find_button_at_pos(mb->get_position(), current_item, current_column, current_index, current_section);
4128 if (current_item == cache.click_item && current_column == cache.click_column && current_index == cache.click_index) {
4129 emit_signal("button_clicked", cache.click_item, cache.click_column, cache.click_id, mb->get_button_index());
4130 }
4131 }
4132
4133 cache.click_type = Cache::CLICK_NONE;
4134 cache.click_index = -1;
4135 cache.click_id = -1;
4136 cache.click_item = nullptr;
4137 cache.click_column = 0;
4138 queue_redraw();
4139 return;
4140 }
4141
4142 if (range_drag_enabled) {
4143 return;
4144 }
4145
4146 switch (mb->get_button_index()) {
4147 case MouseButton::RIGHT:
4148 case MouseButton::LEFT: {
4149 Ref<StyleBox> bg = theme_cache.panel_style;
4150
4151 Point2 pos = mb->get_position();
4152 if (rtl) {
4153 pos.x = get_size().width - pos.x - 1;
4154 }
4155 pos -= bg->get_offset();
4156 cache.click_type = Cache::CLICK_NONE;
4157 if (show_column_titles) {
4158 pos.y -= _get_title_button_height();
4159
4160 if (pos.y < 0) {
4161 pos.x += theme_cache.offset.x;
4162 int len = 0;
4163 for (int i = 0; i < columns.size(); i++) {
4164 len += get_column_width(i);
4165 if (pos.x < static_cast<real_t>(len)) {
4166 cache.click_type = Cache::CLICK_TITLE;
4167 cache.click_index = i;
4168 queue_redraw();
4169 break;
4170 }
4171 }
4172 break;
4173 }
4174 }
4175
4176 if (!root || (!root->get_first_child() && hide_root)) {
4177 emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), mb->get_button_index());
4178 break;
4179 }
4180
4181 click_handled = false;
4182 pressing_for_editor = false;
4183 propagate_mouse_activated = false;
4184
4185 int x_limit = _get_content_rect().size.x;
4186
4187 cache.rtl = is_layout_rtl();
4188 blocked++;
4189 propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, mb->is_double_click(), root, mb->get_button_index(), mb);
4190 blocked--;
4191
4192 if (pressing_for_editor) {
4193 pressing_pos = mb->get_position();
4194 if (rtl) {
4195 pressing_pos.x = get_size().width - pressing_pos.x;
4196 }
4197 } else if (mb->is_double_click() && cache.click_type != Cache::CLICK_BUTTON && get_item_at_position(mb->get_position()) != nullptr) {
4198 emit_signal(SNAME("item_icon_double_clicked"));
4199 }
4200
4201 if (mb->get_button_index() == MouseButton::RIGHT) {
4202 break;
4203 }
4204
4205 if (drag_touching) {
4206 set_process_internal(false);
4207 drag_touching_deaccel = false;
4208 drag_touching = false;
4209 drag_speed = 0;
4210 drag_from = 0;
4211 }
4212
4213 if (!click_handled) {
4214 drag_speed = 0;
4215 drag_accum = 0;
4216 drag_from = v_scroll->get_value();
4217
4218 drag_touching = DisplayServer::get_singleton()->is_touchscreen_available();
4219 drag_touching_deaccel = false;
4220 if (drag_touching) {
4221 set_process_internal(true);
4222 }
4223
4224 if (mb->get_button_index() == MouseButton::LEFT) {
4225 if (get_item_at_position(mb->get_position()) == nullptr && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed()) {
4226 emit_signal(SNAME("nothing_selected"));
4227 }
4228 }
4229 }
4230
4231 if (propagate_mouse_activated) {
4232 emit_signal(SNAME("item_activated"));
4233 propagate_mouse_activated = false;
4234 }
4235
4236 } break;
4237 case MouseButton::WHEEL_UP: {
4238 if (_scroll(mb->is_shift_pressed(), -mb->get_factor() / 8)) {
4239 accept_event();
4240 }
4241
4242 } break;
4243 case MouseButton::WHEEL_DOWN: {
4244 if (_scroll(mb->is_shift_pressed(), mb->get_factor() / 8)) {
4245 accept_event();
4246 }
4247
4248 } break;
4249 case MouseButton::WHEEL_LEFT: {
4250 if (_scroll(!mb->is_shift_pressed(), -mb->get_factor() / 8)) {
4251 accept_event();
4252 }
4253
4254 } break;
4255 case MouseButton::WHEEL_RIGHT: {
4256 if (_scroll(!mb->is_shift_pressed(), mb->get_factor() / 8)) {
4257 accept_event();
4258 }
4259
4260 } break;
4261 default:
4262 break;
4263 }
4264 }
4265
4266 Ref<InputEventPanGesture> pan_gesture = p_event;
4267 if (pan_gesture.is_valid()) {
4268 double prev_v = v_scroll->get_value();
4269 v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
4270
4271 double prev_h = h_scroll->get_value();
4272 if (is_layout_rtl()) {
4273 h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * -pan_gesture->get_delta().x / 8);
4274 } else {
4275 h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
4276 }
4277
4278 if (v_scroll->get_value() != prev_v || h_scroll->get_value() != prev_h) {
4279 accept_event();
4280 }
4281 }
4282 }
4283
4284 void Tree::_determine_hovered_item() {
4285 hovered_update_queued = false;
4286
4287 Ref<StyleBox> bg = theme_cache.panel_style;
4288 bool rtl = is_layout_rtl();
4289
4290 Point2 pos = hovered_pos;
4291 if (rtl) {
4292 pos.x = get_size().width - pos.x - 1;
4293 }
4294 pos -= theme_cache.panel_style->get_offset();
4295
4296 bool old_header_row = cache.hover_header_row;
4297 int old_header_column = cache.hover_header_column;
4298 TreeItem *old_item = cache.hover_item;
4299 int old_column = cache.hover_column;
4300 int old_button_index_in_column = cache.hover_button_index_in_column;
4301
4302 // Determine hover on column headers.
4303 cache.hover_header_row = false;
4304 cache.hover_header_column = 0;
4305 if (show_column_titles && is_mouse_hovering) {
4306 pos.y -= _get_title_button_height();
4307 if (pos.y < 0) {
4308 pos.x += theme_cache.offset.x;
4309 int len = 0;
4310 for (int i = 0; i < columns.size(); i++) {
4311 len += get_column_width(i);
4312 if (pos.x < len) {
4313 cache.hover_header_row = true;
4314 cache.hover_header_column = i;
4315 cache.hover_button_index_in_column = -1;
4316 break;
4317 }
4318 }
4319 }
4320 }
4321
4322 // Determine hover on rows and items.
4323 if (root && is_mouse_hovering && hovered_pos.y >= 0) {
4324 TreeItem *it;
4325 int col, section, col_button_index;
4326 _find_button_at_pos(hovered_pos, it, col, col_button_index, section);
4327
4328 if (drop_mode_flags) {
4329 if (it != drop_mode_over) {
4330 drop_mode_over = it;
4331 if (enable_drag_unfolding) {
4332 dropping_unfold_timer->start(theme_cache.dragging_unfold_wait_msec * 0.001);
4333 }
4334 queue_redraw();
4335 }
4336 if (it && section != drop_mode_section) {
4337 drop_mode_section = section;
4338 queue_redraw();
4339 }
4340 }
4341
4342 cache.hover_item = it;
4343 cache.hover_column = col;
4344 cache.hover_button_index_in_column = col_button_index;
4345
4346 if (it != old_item || col != old_column) {
4347 if (old_item && old_column >= old_item->cells.size()) {
4348 // Columns may have changed since last `redraw()`.
4349 queue_redraw();
4350 } else {
4351 // Only need to update if mouse enters/exits a button.
4352 bool was_over_button = old_item && old_item->cells[old_column].custom_button;
4353 bool is_over_button = it && it->cells[col].custom_button;
4354 if (was_over_button || is_over_button) {
4355 queue_redraw();
4356 }
4357 }
4358 }
4359 }
4360
4361 // Reduce useless redraw calls.
4362 bool hovered_cell_button_changed = (cache.hover_button_index_in_column != old_button_index_in_column);
4363 bool hovered_column_changed = (cache.hover_column != old_column);
4364
4365 // Mouse has moved from row to row, or from cell to cell within same row unless selection mode is full row which saves a useless redraw.
4366 bool item_hover_needs_redraw = !cache.hover_header_row && (cache.hover_item != old_item || hovered_cell_button_changed || (select_mode != SELECT_ROW && hovered_column_changed));
4367 // Mouse has moved between two different column header sections.
4368 bool header_hover_needs_redraw = cache.hover_header_row && cache.hover_header_column != old_header_column;
4369 // Mouse has moved between header and "main" areas.
4370 bool whole_needs_redraw = cache.hover_header_row != old_header_row;
4371
4372 if (whole_needs_redraw || header_hover_needs_redraw || item_hover_needs_redraw) {
4373 queue_redraw();
4374 }
4375 }
4376
4377 void Tree::_on_dropping_unfold_timer_timeout() {
4378 if (enable_drag_unfolding && drop_mode_over && drop_mode_section == 0) {
4379 drop_mode_over->set_collapsed(false);
4380 }
4381 }
4382
4383 void Tree::_reset_drop_mode_over() {
4384 drop_mode_over = nullptr;
4385 if (!dropping_unfold_timer->is_stopped()) {
4386 dropping_unfold_timer->stop();
4387 }
4388 }
4389
4390 void Tree::_queue_update_hovered_item() {
4391 if (hovered_update_queued) {
4392 return;
4393 }
4394 hovered_update_queued = true;
4395 callable_mp(this, &Tree::_determine_hovered_item).call_deferred();
4396 }
4397
4398 bool Tree::edit_selected(bool p_force_edit) {
4399 TreeItem *s = get_selected();
4400 ERR_FAIL_NULL_V_MSG(s, false, "No item selected.");
4401 ensure_cursor_is_visible();
4402 int col = get_selected_column();
4403 ERR_FAIL_INDEX_V_MSG(col, columns.size(), false, "No item column selected.");
4404
4405 if (!s->cells[col].editable && !p_force_edit) {
4406 return false;
4407 }
4408
4409 Size2 scale = popup_editor->get_parent_viewport()->get_popup_base_transform().get_scale() * get_global_transform_with_canvas().get_scale();
4410 real_t popup_scale = MIN(scale.x, scale.y);
4411
4412 Rect2 rect = _get_item_focus_rect(s);
4413 rect.position *= popup_scale;
4414 popup_edited_item = s;
4415 popup_edited_item_col = col;
4416
4417 const TreeItem::Cell &c = s->cells[col];
4418
4419 if (c.mode == TreeItem::CELL_MODE_CHECK) {
4420 s->set_checked(col, !c.checked);
4421 item_edited(col, s);
4422 return true;
4423 } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
4424 edited_item = s;
4425 edited_col = col;
4426 custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
4427 emit_signal(SNAME("custom_popup_edited"), false);
4428 item_edited(col, s);
4429
4430 return true;
4431 } else if (c.mode == TreeItem::CELL_MODE_RANGE && !c.text.is_empty()) {
4432 _update_popup_menu(c);
4433 popup_menu->set_size(Size2(rect.size.width, 0));
4434 popup_menu->set_position(get_screen_position() + rect.position + Point2i(0, rect.size.height));
4435 popup_menu->popup();
4436 popup_edited_item = s;
4437 popup_edited_item_col = col;
4438
4439 return true;
4440 } else if ((c.mode == TreeItem::CELL_MODE_STRING && !c.edit_multiline) || c.mode == TreeItem::CELL_MODE_RANGE) {
4441 int value_editor_height = 0;
4442 if (c.mode == TreeItem::CELL_MODE_RANGE) {
4443 _update_value_editor(c);
4444 value_editor_height = value_editor->get_minimum_size().height;
4445 value_editor->show();
4446 } else if (value_editor) {
4447 value_editor->hide();
4448 }
4449
4450 Rect2 popup_rect;
4451 // `floor()` centers vertically.
4452 Vector2 ofs(0, Math::floor((MAX(line_editor->get_minimum_size().height, rect.size.height - value_editor_height) - rect.size.height) / 2));
4453
4454 // Account for icon.
4455 real_t icon_ofs = 0;
4456 if (c.icon.is_valid()) {
4457 icon_ofs = _get_cell_icon_size(c).x * popup_scale + theme_cache.icon_h_separation;
4458 }
4459
4460 popup_rect.size = rect.size + Vector2(-icon_ofs, value_editor_height);
4461
4462 popup_rect.position = rect.position - ofs;
4463 popup_rect.position.x += icon_ofs;
4464 if (cache.rtl) {
4465 popup_rect.position.x = get_size().width - popup_rect.position.x - popup_rect.size.x;
4466 }
4467 popup_rect.position += get_screen_position();
4468
4469 bool hide_focus = !has_focus(true);
4470
4471 line_editor->clear();
4472 line_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::range_step_decimals(c.step)));
4473 line_editor->select_all();
4474 line_editor->show();
4475
4476 const String &col_title = columns[col].title;
4477 line_editor->set_accessibility_name(col_title.is_empty() ? c.text : col_title);
4478
4479 text_editor->hide();
4480
4481 popup_editor->set_position(popup_rect.position);
4482 popup_editor->set_size(popup_rect.size * popup_scale);
4483 if (!popup_editor->is_embedded()) {
4484 popup_editor->set_content_scale_factor(popup_scale);
4485 }
4486 popup_edit_committed = false; // Start edit popup processing.
4487 popup_editor->popup();
4488 popup_editor->child_controls_changed();
4489
4490 line_editor->grab_focus(hide_focus);
4491
4492 return true;
4493 } else if (c.mode == TreeItem::CELL_MODE_STRING && c.edit_multiline) {
4494 bool hide_focus = !has_focus(true);
4495
4496 line_editor->hide();
4497
4498 text_editor->clear();
4499 text_editor->set_text(c.text);
4500 text_editor->select_all();
4501 text_editor->show();
4502
4503 const String &col_title_ml = columns[col].title;
4504 text_editor->set_accessibility_name(col_title_ml.is_empty() ? c.text : col_title_ml);
4505
4506 popup_editor->set_position(get_screen_position() + rect.position);
4507 popup_editor->set_size(rect.size * popup_scale);
4508 if (!popup_editor->is_embedded()) {
4509 popup_editor->set_content_scale_factor(popup_scale);
4510 }
4511 popup_edit_committed = false; // Start edit popup processing.
4512 popup_editor->popup();
4513 popup_editor->child_controls_changed();
4514
4515 text_editor->grab_focus(hide_focus);
4516
4517 return true;
4518 }
4519
4520 return false;
4521 }
4522
4523 Rect2 Tree::_get_item_focus_rect(const TreeItem *p_item) const {
4524 Rect2 rect;
4525 if (select_mode == SELECT_ROW) {
4526 rect = p_item->cells[selected_col].focus_rect;
4527 } else {
4528 rect = p_item->focus_rect;
4529 }
4530 return rect;
4531 }
4532
4533 bool Tree::is_editing() {
4534 return popup_editor->is_visible();
4535 }
4536
4537 void Tree::set_editor_selection(int p_from_line, int p_to_line, int p_from_column, int p_to_column, int p_caret) {
4538 if (p_from_column == -1 || p_to_column == -1) {
4539 line_editor->select(p_from_line, p_to_line);
4540 } else {
4541 text_editor->select(p_from_line, p_from_column, p_to_line, p_to_column, p_caret);
4542 }
4543 }
4544
4545 Size2 Tree::get_internal_min_size() const {
4546 Size2i size;
4547 if (root) {
4548 size.height += get_item_height(root);
4549 }
4550 for (int i = 0; i < columns.size(); i++) {
4551 size.width += get_column_minimum_width(i);
4552 }
4553
4554 return size;
4555 }
4556
4557 void Tree::update_scrollbars() {
4558 const Size2 control_size = get_size();
4559 const Ref<StyleBox> background = theme_cache.panel_style;
4560
4561 // This is the background stylebox's content rect.
4562 const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
4563 const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
4564 const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
4565
4566 const Size2 hmin = h_scroll->get_combined_minimum_size();
4567 const Size2 vmin = v_scroll->get_combined_minimum_size();
4568
4569 const Size2 internal_min_size = get_internal_min_size();
4570 const int title_button_height = _get_title_button_height();
4571
4572 Size2 tree_content_size = content_rect.get_size() - Vector2(0, title_button_height);
4573 bool display_vscroll = internal_min_size.height > tree_content_size.height;
4574 bool display_hscroll = internal_min_size.width > tree_content_size.width;
4575 for (int i = 0; i < 2; i++) {
4576 // Check twice, as both values are dependent on each other.
4577 if (display_hscroll) {
4578 tree_content_size.height = content_rect.get_size().height - title_button_height - hmin.height;
4579 display_vscroll = internal_min_size.height > tree_content_size.height;
4580 }
4581 if (display_vscroll) {
4582 tree_content_size.width = content_rect.get_size().width - vmin.width;
4583 display_hscroll = internal_min_size.width > tree_content_size.width;
4584 }
4585 }
4586
4587 if (display_vscroll) {
4588 v_scroll->show();
4589 v_scroll->set_max(internal_min_size.height);
4590 v_scroll->set_page(tree_content_size.height);
4591 theme_cache.offset.y = v_scroll->get_value();
4592 } else {
4593 v_scroll->hide();
4594 v_scroll->set_value(0);
4595 theme_cache.offset.y = 0;
4596 }
4597
4598 if (display_hscroll) {
4599 h_scroll->show();
4600 h_scroll->set_max(internal_min_size.width);
4601 h_scroll->set_page(tree_content_size.width);
4602 theme_cache.offset.x = h_scroll->get_value();
4603 } else {
4604 h_scroll->hide();
4605 h_scroll->set_value(0);
4606 theme_cache.offset.x = 0;
4607 }
4608
4609 const Rect2 scroll_rect = _get_scrollbar_layout_rect();
4610 v_scroll->set_begin(scroll_rect.get_position() + Vector2(scroll_rect.get_size().x - vmin.width, 0));
4611 v_scroll->set_end(scroll_rect.get_end() - Vector2(0, display_hscroll ? hmin.height : 0));
4612 h_scroll->set_begin(scroll_rect.get_position() + Vector2(0, scroll_rect.get_size().y - hmin.height));
4613 h_scroll->set_end(scroll_rect.get_end() - Vector2(display_vscroll ? vmin.width : 0, 0));
4614 }
4615
4616 int Tree::_get_title_button_height() const {
4617 ERR_FAIL_COND_V(theme_cache.tb_font.is_null() || theme_cache.title_button.is_null(), 0);
4618 int h = 0;
4619 if (show_column_titles) {
4620 for (int i = 0; i < columns.size(); i++) {
4621 h = MAX(h, columns[i].text_buf->get_size().y + theme_cache.title_button->get_minimum_size().height);
4622 }
4623 }
4624 return h;
4625 }
4626
4627 void Tree::_check_item_accessibility(TreeItem *p_item, PackedStringArray &r_warnings, int &r_row) const {
4628 for (int i = 0; i < p_item->cells.size(); i++) {
4629 const TreeItem::Cell &cell = p_item->cells[i];
4630 if (cell.description.strip_edges().is_empty() && cell.text.strip_edges().is_empty()) {
4631 r_warnings.push_back(vformat(RTR("Cell %d x %d: either text or alternative text must not be empty."), r_row, i));
4632 }
4633 for (int j = 0; j < cell.buttons.size(); j++) {
4634 if (cell.buttons[j].description.strip_edges().is_empty()) {
4635 r_warnings.push_back(vformat(RTR("Button %d in %d x %d: alternative text must not be empty."), j, r_row, i));
4636 }
4637 }
4638 }
4639 r_row++;
4640
4641 // Children.
4642 if (!p_item->collapsed) {
4643 TreeItem *c = p_item->first_child;
4644 while (c) {
4645 _check_item_accessibility(c, r_warnings, r_row);
4646 c = c->next;
4647 }
4648 }
4649 }
4650
4651 PackedStringArray Tree::get_accessibility_configuration_warnings() const {
4652 PackedStringArray warnings = Control::get_accessibility_configuration_warnings();
4653
4654 if (root) {
4655 int row = 1;
4656 _check_item_accessibility(root, warnings, row);
4657 }
4658
4659 return warnings;
4660 }
4661
4662 void Tree::_accessibility_action_scroll_down(const Variant &p_data) {
4663 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
4664 v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 4);
4665 } else {
4666 v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page());
4667 }
4668 }
4669
4670 void Tree::_accessibility_action_scroll_left(const Variant &p_data) {
4671 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
4672 h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 4);
4673 } else {
4674 h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page());
4675 }
4676 }
4677
4678 void Tree::_accessibility_action_scroll_right(const Variant &p_data) {
4679 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
4680 h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 4);
4681 } else {
4682 h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page());
4683 }
4684 }
4685
4686 void Tree::_accessibility_action_scroll_up(const Variant &p_data) {
4687 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
4688 v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 4);
4689 } else {
4690 v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page());
4691 }
4692 }
4693
4694 void Tree::_accessibility_action_scroll_set(const Variant &p_data) {
4695 const Point2 &pos = p_data;
4696 h_scroll->set_value(pos.x);
4697 v_scroll->set_value(pos.y);
4698 }
4699
4700 void Tree::_accessibility_action_scroll_into_view(const Variant &p_data, TreeItem *p_item, int p_col) {
4701 scroll_to_item(p_item);
4702 }
4703
4704 void Tree::_accessibility_action_focus(const Variant &p_data, TreeItem *p_item, int p_col) {
4705 p_item->select(p_col);
4706 }
4707
4708 void Tree::_accessibility_action_blur(const Variant &p_data, TreeItem *p_item, int p_col) {
4709 p_item->deselect(p_col);
4710 }
4711
4712 void Tree::_accessibility_action_collapse(const Variant &p_data, TreeItem *p_item) {
4713 p_item->set_collapsed(true);
4714 }
4715
4716 void Tree::_accessibility_action_expand(const Variant &p_data, TreeItem *p_item) {
4717 p_item->set_collapsed(false);
4718 }
4719
4720 void Tree::_accessibility_action_set_text_value(const Variant &p_data, TreeItem *p_item, int p_col) {
4721 p_item->set_text(p_col, p_data);
4722 }
4723
4724 void Tree::_accessibility_action_set_num_value(const Variant &p_data, TreeItem *p_item, int p_col) {
4725 p_item->set_range(p_col, p_data);
4726 }
4727
4728 void Tree::_accessibility_action_set_bool_value(const Variant &p_data, TreeItem *p_item, int p_col) {
4729 p_item->set_checked(p_col, !p_item->cells[p_col].checked);
4730 }
4731
4732 void Tree::_accessibility_action_edit_custom(const Variant &p_data, TreeItem *p_item, int p_col) {
4733 float popup_scale = popup_editor->is_embedded() ? 1.0 : popup_editor->get_parent_visible_window()->get_content_scale_factor();
4734 Rect2 rect;
4735 if (select_mode == SELECT_ROW) {
4736 rect = p_item->cells[p_col].focus_rect;
4737 } else {
4738 rect = p_item->focus_rect;
4739 }
4740 rect.position *= popup_scale;
4741
4742 edited_item = p_item;
4743 edited_col = p_col;
4744 custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
4745 emit_signal(SNAME("custom_popup_edited"), false);
4746 item_edited(p_col, p_item);
4747 }
4748
4749 void Tree::_accessibility_action_set_inc(const Variant &p_data, TreeItem *p_item, int p_col) {
4750 p_item->set_range(p_col, p_item->cells[p_col].val + p_item->cells[p_col].step);
4751 }
4752
4753 void Tree::_accessibility_action_set_dec(const Variant &p_data, TreeItem *p_item, int p_col) {
4754 p_item->set_range(p_col, p_item->cells[p_col].val - p_item->cells[p_col].step);
4755 }
4756
4757 void Tree::_accessibility_action_button_press(const Variant &p_data, TreeItem *p_item, int p_col, int p_btn) {
4758 emit_signal("button_clicked", p_item, p_col, p_btn, MouseButton::LEFT);
4759 }
4760
4761 RID Tree::get_focused_accessibility_element() const {
4762 if (selected_item) {
4763 if (selected_col >= 0) {
4764 if (selected_button >= 0) {
4765 return selected_item->cells[selected_col].buttons[selected_button].accessibility_button_element;
4766 } else {
4767 return selected_item->cells[selected_col].accessibility_cell_element;
4768 }
4769 } else {
4770 return selected_item->accessibility_row_element;
4771 }
4772 } else {
4773 return get_accessibility_element();
4774 }
4775 }
4776
4777 void Tree::_accessibility_clean_info(TreeItem *p_item) {
4778 p_item->accessibility_row_element = RID();
4779 for (TreeItem::Cell &cell : p_item->cells) {
4780 cell.accessibility_cell_element = RID();
4781 for (TreeItem::Cell::Button &btn : cell.buttons) {
4782 btn.accessibility_button_element = RID();
4783 }
4784 }
4785
4786 // Children.
4787 TreeItem *c = p_item->first_child;
4788 while (c) {
4789 _accessibility_clean_info(c);
4790 c = c->next;
4791 }
4792 }
4793
4794 void Tree::_accessibility_update_item(Point2 &r_ofs, TreeItem *p_item, int &r_row, int p_level) {
4795 // Row.
4796 if ((p_item != root || !hide_root) && p_item->is_visible()) {
4797 if (p_item->accessibility_row_element.is_null()) {
4798 p_item->accessibility_row_element = DisplayServer::get_singleton()->accessibility_create_sub_element(accessibility_scroll_element, DisplayServer::AccessibilityRole::ROLE_TREE_ITEM);
4799 p_item->accessibility_row_dirty = true;
4800 }
4801
4802 DisplayServer::get_singleton()->accessibility_update_set_table_row_index(p_item->accessibility_row_element, r_row);
4803 DisplayServer::get_singleton()->accessibility_update_set_list_item_level(p_item->accessibility_row_element, p_level);
4804 DisplayServer::get_singleton()->accessibility_update_set_list_item_expanded(p_item->accessibility_row_element, !p_item->collapsed);
4805 DisplayServer::get_singleton()->accessibility_update_set_flag(p_item->accessibility_row_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !(p_item->visible && !p_item->parent_visible_in_tree));
4806 DisplayServer::get_singleton()->accessibility_update_add_action(p_item->accessibility_row_element, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &Tree::_accessibility_action_collapse).bind(p_item));
4807 DisplayServer::get_singleton()->accessibility_update_add_action(p_item->accessibility_row_element, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &Tree::_accessibility_action_expand).bind(p_item));
4808
4809 DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(p_item->accessibility_row_element, selected_item == p_item);
4810 if (p_item == root && is_root_hidden()) {
4811 DisplayServer::get_singleton()->accessibility_update_set_flag(p_item->accessibility_row_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, true);
4812 }
4813 Transform2D row_xform;
4814 row_xform.set_origin(r_ofs);
4815 DisplayServer::get_singleton()->accessibility_update_set_transform(p_item->accessibility_row_element, row_xform);
4816
4817 Size2 item_size = Size2(get_size().width, compute_item_height(p_item));
4818 DisplayServer::get_singleton()->accessibility_update_set_bounds(p_item->accessibility_row_element, Rect2(Vector2(), item_size));
4819
4820 if (p_item->accessibility_row_dirty) {
4821 // Cells.
4822 int col_offset = 0;
4823 for (int i = 0; i < p_item->cells.size(); i++) {
4824 TreeItem::Cell &cell = p_item->cells.write[i];
4825
4826 if (cell.accessibility_cell_element.is_null()) {
4827 cell.accessibility_cell_element = DisplayServer::get_singleton()->accessibility_create_sub_element(p_item->accessibility_row_element, DisplayServer::AccessibilityRole::ROLE_CELL);
4828 }
4829
4830 float cw = get_column_width(i);
4831
4832 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &Tree::_accessibility_action_scroll_into_view).bind(p_item, i));
4833 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Tree::_accessibility_action_focus).bind(p_item, i));
4834 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &Tree::_accessibility_action_blur).bind(p_item, i));
4835 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &Tree::_accessibility_action_collapse).bind(p_item));
4836 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &Tree::_accessibility_action_expand).bind(p_item));
4837
4838 DisplayServer::get_singleton()->accessibility_update_set_table_cell_position(cell.accessibility_cell_element, r_row, i);
4839 DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(cell.accessibility_cell_element, cell.selected);
4840 if (cell.description.is_empty()) {
4841 DisplayServer::get_singleton()->accessibility_update_set_name(cell.accessibility_cell_element, cell.xl_text);
4842 } else {
4843 DisplayServer::get_singleton()->accessibility_update_set_name(cell.accessibility_cell_element, cell.description);
4844 }
4845
4846 DisplayServer::get_singleton()->accessibility_update_set_text_align(cell.accessibility_cell_element, cell.text_alignment);
4847 DisplayServer::get_singleton()->accessibility_update_set_flag(cell.accessibility_cell_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !(p_item->visible && !p_item->parent_visible_in_tree));
4848 DisplayServer::get_singleton()->accessibility_update_set_flag(cell.accessibility_cell_element, DisplayServer::AccessibilityFlags::FLAG_READONLY, !cell.editable);
4849 DisplayServer::get_singleton()->accessibility_update_set_tooltip(cell.accessibility_cell_element, cell.tooltip);
4850 switch (cell.mode) {
4851 case TreeItem::CELL_MODE_STRING: {
4852 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Tree::_accessibility_action_set_text_value).bind(p_item, i));
4853 DisplayServer::get_singleton()->accessibility_update_set_value(cell.accessibility_cell_element, cell.xl_text);
4854 } break;
4855 case TreeItem::CELL_MODE_CHECK: {
4856 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_set_bool_value).bind(p_item, i));
4857 DisplayServer::get_singleton()->accessibility_update_set_checked(cell.accessibility_cell_element, cell.checked);
4858 } break;
4859 case TreeItem::CELL_MODE_RANGE: {
4860 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &Tree::_accessibility_action_set_dec).bind(p_item, i));
4861 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &Tree::_accessibility_action_set_inc).bind(p_item, i));
4862 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Tree::_accessibility_action_set_num_value).bind(p_item, i));
4863 DisplayServer::get_singleton()->accessibility_update_set_num_value(cell.accessibility_cell_element, cell.val);
4864 DisplayServer::get_singleton()->accessibility_update_set_num_range(cell.accessibility_cell_element, cell.min, cell.max);
4865 if (cell.step > 0) {
4866 DisplayServer::get_singleton()->accessibility_update_set_num_step(cell.accessibility_cell_element, cell.step);
4867 } else {
4868 DisplayServer::get_singleton()->accessibility_update_set_num_step(cell.accessibility_cell_element, 1);
4869 }
4870 } break;
4871 case TreeItem::CELL_MODE_ICON: {
4872 // NOP
4873 } break;
4874 case TreeItem::CELL_MODE_CUSTOM: {
4875 DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_edit_custom).bind(p_item, i));
4876 } break;
4877 }
4878 DisplayServer::get_singleton()->accessibility_update_set_background_color(cell.accessibility_cell_element, cell.color);
4879 DisplayServer::get_singleton()->accessibility_update_set_foreground_color(cell.accessibility_cell_element, cell.bg_color);
4880
4881 DisplayServer::get_singleton()->accessibility_update_set_bounds(cell.accessibility_cell_element, Rect2(Point2(col_offset, 0), Size2(cw, item_size.y)));
4882
4883 Vector2 ofst = Vector2(col_offset + cw, 0);
4884 for (int j = cell.buttons.size() - 1; j >= 0; j--) {
4885 if (cell.buttons[j].accessibility_button_element.is_null()) {
4886 cell.buttons[j].accessibility_button_element = DisplayServer::get_singleton()->accessibility_create_sub_element(cell.accessibility_cell_element, DisplayServer::AccessibilityRole::ROLE_BUTTON);
4887 }
4888
4889 DisplayServer::get_singleton()->accessibility_update_add_action(cell.buttons[j].accessibility_button_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_button_press).bind(p_item, i, j));
4890 DisplayServer::get_singleton()->accessibility_update_set_flag(cell.buttons[j].accessibility_button_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, cell.buttons[j].disabled);
4891 DisplayServer::get_singleton()->accessibility_update_set_tooltip(cell.buttons[j].accessibility_button_element, cell.buttons[j].tooltip);
4892 if (cell.buttons[j].description.is_empty()) {
4893 DisplayServer::get_singleton()->accessibility_update_set_name(cell.buttons[j].accessibility_button_element, cell.buttons[j].tooltip);
4894 } else {
4895 DisplayServer::get_singleton()->accessibility_update_set_name(cell.buttons[j].accessibility_button_element, cell.buttons[j].description);
4896 }
4897
4898 Ref<Texture2D> b = cell.buttons[j].texture;
4899 Size2 b_size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
4900 ofst.x -= b_size.x;
4901
4902 DisplayServer::get_singleton()->accessibility_update_set_bounds(cell.buttons[j].accessibility_button_element, Rect2(ofst, b_size));
4903 }
4904 col_offset += cw;
4905 }
4906 }
4907
4908 r_ofs.y += item_size.y;
4909 r_ofs.y += theme_cache.v_separation;
4910
4911 p_item->accessibility_row_dirty = false;
4912 r_row++;
4913 }
4914
4915 // Children.
4916 if (!p_item->collapsed) {
4917 TreeItem *c = p_item->first_child;
4918 while (c) {
4919 _accessibility_update_item(r_ofs, c, r_row, p_level + 1);
4920 c = c->next;
4921 }
4922 }
4923 }
4924
4925 void Tree::_notification(int p_what) {
4926 switch (p_what) {
4927 case NOTIFICATION_EXIT_TREE:
4928 case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
4929 if (root) {
4930 _accessibility_clean_info(root);
4931 }
4932 for (ColumnInfo &col : columns) {
4933 col.accessibility_col_element = RID();
4934 }
4935 accessibility_scroll_element = RID();
4936 } break;
4937
4938 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
4939 RID ae = get_accessibility_element();
4940 ERR_FAIL_COND(ae.is_null());
4941
4942 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_TREE);
4943
4944 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &Tree::_accessibility_action_scroll_down));
4945 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &Tree::_accessibility_action_scroll_left));
4946 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &Tree::_accessibility_action_scroll_right));
4947 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &Tree::_accessibility_action_scroll_up));
4948 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &Tree::_accessibility_action_scroll_set));
4949
4950 Ref<StyleBox> bg = theme_cache.panel_style;
4951 int tbh = _get_title_button_height();
4952
4953 // Columns.
4954 int ofs = theme_cache.panel_style->get_margin(SIDE_LEFT);
4955 int cs = columns.size();
4956 for (int i = 0; i < cs; i++) {
4957 ColumnInfo &column = columns.write[i];
4958
4959 if (column.accessibility_col_element.is_null()) {
4960 column.accessibility_col_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_COLUMN_HEADER);
4961 }
4962
4963 DisplayServer::get_singleton()->accessibility_update_set_table_column_index(column.accessibility_col_element, i);
4964 DisplayServer::get_singleton()->accessibility_update_set_name(column.accessibility_col_element, column.xl_title);
4965 DisplayServer::get_singleton()->accessibility_update_set_text_align(column.accessibility_col_element, column.title_alignment);
4966
4967 Rect2 tbrect = Rect2(ofs - theme_cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
4968 if (cache.rtl) {
4969 tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
4970 }
4971 ofs += tbrect.size.width;
4972 DisplayServer::get_singleton()->accessibility_update_set_bounds(column.accessibility_col_element, Rect2(tbrect.position, tbrect.size));
4973 }
4974
4975 DisplayServer::get_singleton()->accessibility_update_set_table_column_count(ae, cs);
4976
4977 // Scroll container.
4978 if (accessibility_scroll_element.is_null()) {
4979 accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
4980 }
4981
4982 Transform2D scroll_xform;
4983 scroll_xform.set_origin(Vector2i(-h_scroll->get_value(), -v_scroll->get_value()));
4984 DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform);
4985 DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, Rect2(0, 0, h_scroll->get_max(), v_scroll->get_max()));
4986
4987 // Rows (and cells).
4988 Point2 origin = Point2(theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.offset.x, bg->get_margin(SIDE_TOP) + tbh);
4989 int rows = 0;
4990 if (root) {
4991 _accessibility_update_item(origin, root, rows, 0);
4992 }
4993 DisplayServer::get_singleton()->accessibility_update_set_table_row_count(ae, rows);
4994
4995 } break;
4996
4997 case NOTIFICATION_FOCUS_ENTER: {
4998 if (get_viewport()) {
4999 focus_in_id = get_viewport()->get_processed_events_count();
5000 }
5001 } break;
5002
5003 case NOTIFICATION_MOUSE_ENTER: {
5004 is_mouse_hovering = true;
5005 _determine_hovered_item();
5006 } break;
5007
5008 case NOTIFICATION_MOUSE_EXIT: {
5009 is_mouse_hovering = false;
5010 // Clear hovered item cache.
5011 if (cache.hover_header_row || cache.hover_item != nullptr) {
5012 cache.hover_header_row = false;
5013 cache.hover_header_column = -1;
5014 cache.hover_item = nullptr;
5015 cache.hover_column = -1;
5016 cache.hover_button_index_in_column = -1;
5017 queue_redraw();
5018 }
5019 } break;
5020
5021 case NOTIFICATION_VISIBILITY_CHANGED: {
5022 drag_touching = false;
5023 } break;
5024
5025 case NOTIFICATION_DRAG_END: {
5026 drop_mode_flags = 0;
5027 scrolling = false;
5028 set_process_internal(false);
5029 queue_redraw();
5030 } break;
5031
5032 case NOTIFICATION_DRAG_BEGIN: {
5033 single_select_defer = nullptr;
5034 if (theme_cache.scroll_speed > 0) {
5035 scrolling = true;
5036 set_process_internal(true);
5037 }
5038 } break;
5039
5040 case NOTIFICATION_INTERNAL_PROCESS: {
5041 if (drag_touching) {
5042 if (drag_touching_deaccel) {
5043 float pos = v_scroll->get_value();
5044 pos += drag_speed * get_process_delta_time();
5045
5046 bool turnoff = false;
5047 if (pos < 0) {
5048 pos = 0;
5049 turnoff = true;
5050 set_process_internal(false);
5051 drag_touching = false;
5052 drag_touching_deaccel = false;
5053 }
5054 if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
5055 pos = v_scroll->get_max() - v_scroll->get_page();
5056 turnoff = true;
5057 }
5058
5059 v_scroll->set_value(pos);
5060 float sgn = drag_speed < 0 ? -1 : 1;
5061 float val = Math::abs(drag_speed);
5062 val -= 1000 * get_process_delta_time();
5063
5064 if (val < 0) {
5065 turnoff = true;
5066 }
5067 drag_speed = sgn * val;
5068
5069 if (turnoff) {
5070 set_process_internal(false);
5071 drag_touching = false;
5072 drag_touching_deaccel = false;
5073 }
5074 }
5075 }
5076
5077 Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
5078 if (scrolling && get_rect().grow(theme_cache.scroll_border).has_point(mouse_position)) {
5079 Point2 point;
5080
5081 if ((Math::abs(mouse_position.x) < Math::abs(mouse_position.x - get_size().width)) && (Math::abs(mouse_position.x) < theme_cache.scroll_border)) {
5082 point.x = mouse_position.x - theme_cache.scroll_border;
5083 } else if (Math::abs(mouse_position.x - get_size().width) < theme_cache.scroll_border) {
5084 point.x = mouse_position.x - (get_size().width - theme_cache.scroll_border);
5085 }
5086
5087 if ((Math::abs(mouse_position.y) < Math::abs(mouse_position.y - get_size().height)) && (Math::abs(mouse_position.y) < theme_cache.scroll_border)) {
5088 point.y = mouse_position.y - theme_cache.scroll_border;
5089 } else if (Math::abs(mouse_position.y - get_size().height) < theme_cache.scroll_border) {
5090 point.y = mouse_position.y - (get_size().height - theme_cache.scroll_border);
5091 }
5092
5093 point *= theme_cache.scroll_speed * get_process_delta_time();
5094 point += get_scroll();
5095 h_scroll->set_value(point.x);
5096 v_scroll->set_value(point.y);
5097 }
5098 } break;
5099
5100 case NOTIFICATION_DRAW: {
5101 v_scroll->set_custom_step(theme_cache.font->get_height(theme_cache.font_size));
5102
5103 update_scrollbars();
5104 RID ci = get_canvas_item();
5105
5106 Ref<StyleBox> bg = theme_cache.panel_style;
5107 const Rect2 content_rect = _get_content_rect();
5108
5109 Point2 draw_ofs = content_rect.position;
5110 Size2 draw_size = content_rect.size;
5111
5112 int tbh = _get_title_button_height();
5113
5114 draw_ofs.y += tbh;
5115 draw_size.y -= tbh;
5116
5117 const real_t clip_top = show_column_titles ? draw_ofs.y : 0.0f; // Start clipping below headers when they are visible
5118 const real_t bottom_margin = theme_cache.panel_style->get_margin(SIDE_BOTTOM);
5119 const real_t clip_bottom = draw_ofs.y + draw_size.y + bottom_margin; // End clip after the bottom margin
5120 const Rect2 main_clip_rect = Rect2(0.0f, clip_top, get_size().x, clip_bottom - clip_top);
5121 RenderingServer *rendering_server = RenderingServer::get_singleton();
5122
5123 bg->draw(ci, Rect2(Point2(), get_size()));
5124
5125 Rect2 header_clip_rect = Rect2(content_rect.position.x, 0.0f, content_rect.size.x, get_size().y); // Keep headers out of the scrollbar area.
5126 if (v_scroll->is_visible()) {
5127 const Rect2 v_scroll_rect = Rect2(v_scroll->get_position(), v_scroll->get_size()); // Actual scrollbar rect in Tree coordinates.
5128 if (v_scroll_rect.position.x <= header_clip_rect.position.x) {
5129 const real_t header_right = header_clip_rect.get_end().x;
5130 header_clip_rect.position.x = v_scroll_rect.get_end().x;
5131 header_clip_rect.size.x = MAX(0.0f, header_right - header_clip_rect.position.x);
5132 } else if (v_scroll_rect.position.x < header_clip_rect.get_end().x) {
5133 header_clip_rect.size.x = MAX(0.0f, v_scroll_rect.position.x - header_clip_rect.position.x);
5134 }
5135 }
5136 rendering_server->canvas_item_clear(header_ci);
5137 rendering_server->canvas_item_set_custom_rect(header_ci, !is_visibility_clip_disabled(), header_clip_rect);
5138 rendering_server->canvas_item_set_clip(header_ci, true);
5139
5140 rendering_server->canvas_item_clear(content_ci);
5141 rendering_server->canvas_item_set_custom_rect(content_ci, !is_visibility_clip_disabled(), main_clip_rect);
5142 rendering_server->canvas_item_set_clip(content_ci, true);
5143
5144 cache.rtl = is_layout_rtl();
5145 content_scale_factor = popup_editor->is_embedded() ? 1.0 : popup_editor->get_parent_visible_window()->get_content_scale_factor();
5146
5147 if (root && get_size().x > 0 && get_size().y > 0) {
5148 int self_height = 0; // Just to pass a reference, we don't need the root's `self_height`.
5149 draw_item(Point2(), draw_ofs, draw_size, root, self_height);
5150 }
5151
5152 if (show_column_titles) {
5153 // Title buttons.
5154 int ofs2 = theme_cache.panel_style->get_margin(SIDE_LEFT);
5155 for (int i = 0; i < columns.size(); i++) {
5156 Ref<StyleBox> sb = (cache.click_type == Cache::CLICK_TITLE && cache.click_index == i) ? theme_cache.title_button_pressed : ((cache.hover_header_row && cache.hover_header_column == i) ? theme_cache.title_button_hover : theme_cache.title_button);
5157 Rect2 tbrect = Rect2(ofs2 - theme_cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
5158 if (cache.rtl) {
5159 tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
5160 }
5161 sb->draw(header_ci, tbrect);
5162 ofs2 += tbrect.size.width;
5163 // Text.
5164 int clip_w = tbrect.size.width - sb->get_minimum_size().width;
5165 columns.write[i].text_buf->set_width(clip_w);
5166 columns.write[i].cached_minimum_width_dirty = true;
5167
5168 Vector2 text_pos = Point2i(tbrect.position.x, tbrect.position.y + (tbrect.size.height - columns[i].text_buf->get_size().y) / 2);
5169 switch (columns[i].title_alignment) {
5170 case HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT: {
5171 text_pos.x += cache.rtl ? tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x) : sb->get_offset().x;
5172 break;
5173 }
5174
5175 case HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT: {
5176 text_pos.x += cache.rtl ? sb->get_offset().x : tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x);
5177 break;
5178 }
5179
5180 default: {
5181 text_pos.x += (tbrect.size.width - columns[i].text_buf->get_size().x) / 2;
5182 break;
5183 }
5184 }
5185
5186 if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
5187 columns[i].text_buf->draw_outline(header_ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
5188 }
5189 columns[i].text_buf->draw(header_ci, text_pos, theme_cache.title_button_color);
5190 }
5191 }
5192
5193 if (scroll_hint_mode != SCROLL_HINT_MODE_DISABLED) {
5194 Size2 size = get_size();
5195 float v_scroll_value = v_scroll->get_value();
5196 bool v_scroll_below_max = v_scroll_value < (get_internal_min_size().height - (content_rect.get_size().height - _get_title_button_height()) - 1);
5197 if (v_scroll_value > 1 || v_scroll_below_max) {
5198 int hint_height = theme_cache.scroll_hint->get_height();
5199 if ((scroll_hint_mode == SCROLL_HINT_MODE_BOTH || scroll_hint_mode == SCROLL_HINT_MODE_TOP) && v_scroll_value > 1) {
5200 draw_texture_rect(theme_cache.scroll_hint, Rect2(Point2(), Size2(size.width, hint_height)), tile_scroll_hint, theme_cache.scroll_hint_color);
5201 }
5202 if ((scroll_hint_mode == SCROLL_HINT_MODE_BOTH || scroll_hint_mode == SCROLL_HINT_MODE_BOTTOM) && v_scroll_below_max) {
5203 draw_texture_rect(theme_cache.scroll_hint, Rect2(Point2(0, size.height - hint_height), Size2(size.width, -hint_height)), tile_scroll_hint, theme_cache.scroll_hint_color);
5204 }
5205 }
5206 }
5207
5208 // Draw the focus outline last, so that it is drawn in front of the section headings.
5209 // Otherwise, section heading backgrounds can appear to be in front of the focus outline when scrolling.
5210 if (has_focus(true)) {
5211 RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
5212 theme_cache.focus_style->draw(ci, Rect2(Point2(), get_size()));
5213 RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
5214 }
5215 } break;
5216
5217 case NOTIFICATION_THEME_CHANGED:
5218 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
5219 case NOTIFICATION_TRANSLATION_CHANGED: {
5220 _update_all();
5221 } break;
5222
5223 case NOTIFICATION_RESIZED:
5224 case NOTIFICATION_TRANSFORM_CHANGED: {
5225 if (popup_edited_item != nullptr) {
5226 Rect2 rect = _get_item_focus_rect(popup_edited_item);
5227
5228 popup_editor->set_position(get_global_position() + rect.position);
5229 popup_editor->set_size(rect.size);
5230 popup_editor->child_controls_changed();
5231 }
5232 } break;
5233 }
5234 }
5235
5236 void Tree::set_self_modulate(const Color &p_self_modulate) {
5237 if (get_self_modulate() == p_self_modulate) {
5238 return;
5239 }
5240
5241 CanvasItem::set_self_modulate(p_self_modulate);
5242 RS::get_singleton()->canvas_item_set_self_modulate(header_ci, p_self_modulate);
5243 RS::get_singleton()->canvas_item_set_self_modulate(content_ci, p_self_modulate);
5244 }
5245
5246 void Tree::_update_all() {
5247 for (int i = 0; i < columns.size(); i++) {
5248 update_column(i);
5249 }
5250 if (root) {
5251 update_item_cache(root);
5252 }
5253 }
5254
5255 Size2 Tree::get_minimum_size() const {
5256 Vector2 min_size = Vector2(0, _get_title_button_height());
5257
5258 if (theme_cache.panel_style.is_valid()) {
5259 min_size += theme_cache.panel_style->get_minimum_size();
5260 }
5261
5262 Vector2 content_min_size = get_internal_min_size();
5263 if (h_scroll_enabled) {
5264 content_min_size.x = 0;
5265 min_size.y += h_scroll->get_combined_minimum_size().height;
5266 }
5267 if (v_scroll_enabled) {
5268 min_size.x += v_scroll->get_combined_minimum_size().width;
5269 content_min_size.y = 0;
5270 }
5271
5272 return min_size + content_min_size;
5273 }
5274
5275 TreeItem *Tree::create_item(TreeItem *p_parent, int p_index) {
5276 ERR_FAIL_COND_V(blocked > 0, nullptr);
5277
5278 TreeItem *ti = nullptr;
5279
5280 if (p_parent) {
5281 ERR_FAIL_COND_V_MSG(p_parent->tree != this, nullptr, "A different tree owns the given parent");
5282 ti = p_parent->create_child(p_index);
5283 } else {
5284 if (!root) {
5285 // No root exists, make the given item the new root.
5286 ti = memnew(TreeItem(this));
5287 ERR_FAIL_NULL_V(ti, nullptr);
5288 ti->cells.resize(columns.size());
5289 ti->is_root = true;
5290 root = ti;
5291 } else {
5292 // Root exists, append or insert to root.
5293 ti = create_item(root, p_index);
5294 }
5295 }
5296 _queue_update_hovered_item();
5297 queue_accessibility_update();
5298 return ti;
5299 }
5300
5301 TreeItem *Tree::get_root() const {
5302 return root;
5303 }
5304
5305 TreeItem *Tree::get_last_item() const {
5306 TreeItem *last = root;
5307 while (last && last->last_child && !last->collapsed) {
5308 last = last->last_child;
5309 }
5310
5311 return last;
5312 }
5313
5314 void Tree::item_edited(int p_column, TreeItem *p_item, MouseButton p_custom_mouse_index) {
5315 edited_item = p_item;
5316 edited_col = p_column;
5317 if (p_item != nullptr && p_column >= 0 && p_column < p_item->cells.size()) {
5318 edited_item->cells.write[p_column].dirty = true;
5319 edited_item->cells.write[p_column].cached_minimum_size_dirty = true;
5320 }
5321 emit_signal(SNAME("item_edited"));
5322 if (p_custom_mouse_index != MouseButton::NONE) {
5323 emit_signal(SNAME("custom_item_clicked"), p_custom_mouse_index);
5324 }
5325 queue_accessibility_update();
5326 }
5327
5328 void Tree::item_changed(int p_column, TreeItem *p_item) {
5329 if (p_item != nullptr) {
5330 if (p_column >= 0 && p_column < p_item->cells.size()) {
5331 p_item->cells.write[p_column].dirty = true;
5332 columns.write[p_column].cached_minimum_width_dirty = true;
5333 } else if (p_column == -1) {
5334 for (int i = 0; i < p_item->cells.size(); i++) {
5335 p_item->cells.write[i].dirty = true;
5336 columns.write[i].cached_minimum_width_dirty = true;
5337 }
5338 }
5339 p_item->accessibility_row_dirty = true;
5340 }
5341 update_min_size_for_item_change();
5342 queue_accessibility_update();
5343 queue_redraw();
5344 }
5345
5346 void Tree::item_selected(int p_column, TreeItem *p_item) {
5347 if (select_mode == SELECT_MULTI) {
5348 if (!p_item->cells[p_column].selectable) {
5349 return;
5350 }
5351
5352 p_item->cells.write[p_column].selected = true;
5353 //emit_signal(SNAME("multi_selected"),p_item,p_column,true); - NO this is for `TreeItem::select`
5354
5355 selected_col = p_column;
5356 selected_item = p_item;
5357 selected_button = -1;
5358 } else {
5359 select_single_item(p_item, root, p_column);
5360 }
5361 p_item->accessibility_row_dirty = true;
5362 queue_accessibility_update();
5363 queue_redraw();
5364 }
5365
5366 void Tree::item_deselected(int p_column, TreeItem *p_item) {
5367 if (select_mode == SELECT_SINGLE && selected_item == p_item && selected_col == p_column) {
5368 selected_item = nullptr;
5369 selected_col = -1;
5370 } else {
5371 if (select_mode == SELECT_ROW && selected_item == p_item) {
5372 selected_item = nullptr;
5373 selected_col = -1;
5374 } else {
5375 if (select_mode == SELECT_MULTI) {
5376 selected_item = p_item;
5377 selected_col = p_column;
5378 }
5379 }
5380 }
5381 selected_button = -1;
5382
5383 if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
5384 p_item->cells.write[p_column].selected = false;
5385 } else if (select_mode == SELECT_ROW) {
5386 for (int i = 0; i < p_item->cells.size(); i++) {
5387 p_item->cells.write[i].selected = false;
5388 }
5389 }
5390 p_item->accessibility_row_dirty = true;
5391 queue_accessibility_update();
5392 queue_redraw();
5393 }
5394
5395 void Tree::update_min_size_for_item_change() {
5396 // Only need to update when any scroll bar is disabled because that's the only time item size
5397 // affects tree size.
5398 if (!h_scroll_enabled || !v_scroll_enabled) {
5399 update_minimum_size();
5400 }
5401 }
5402
5403 void Tree::set_select_mode(SelectMode p_mode) {
5404 select_mode = p_mode;
5405 }
5406
5407 Tree::SelectMode Tree::get_select_mode() const {
5408 return select_mode;
5409 }
5410
5411 void Tree::deselect_all() {
5412 if (root) {
5413 TreeItem *item = root;
5414 while (item) {
5415 if (select_mode == SELECT_ROW) {
5416 item->deselect(0);
5417 } else {
5418 for (int i = 0; i < columns.size(); i++) {
5419 item->deselect(i);
5420 }
5421 }
5422 TreeItem *prev_item = item;
5423 item = get_next_selected(root);
5424 ERR_FAIL_COND(item == prev_item);
5425 }
5426 }
5427
5428 selected_item = nullptr;
5429 selected_col = -1;
5430 selected_button = -1;
5431 queue_accessibility_update();
5432 queue_redraw();
5433 }
5434
5435 bool Tree::is_anything_selected() {
5436 return (selected_item != nullptr);
5437 }
5438
5439 void Tree::clear() {
5440 ERR_FAIL_COND(blocked > 0);
5441
5442 if (pressing_for_editor) {
5443 if (range_drag_enabled) {
5444 range_drag_enabled = false;
5445 Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
5446 warp_mouse(range_drag_capture_pos);
5447 }
5448 pressing_for_editor = false;
5449 }
5450
5451 if (root) {
5452 memdelete(root);
5453 root = nullptr;
5454 };
5455
5456 selected_item = nullptr;
5457 edited_item = nullptr;
5458 popup_edited_item = nullptr;
5459 popup_pressing_edited_item = nullptr;
5460
5461 _determine_hovered_item();
5462
5463 queue_accessibility_update();
5464 queue_redraw();
5465 }
5466
5467 void Tree::set_hide_root(bool p_enabled) {
5468 if (hide_root == p_enabled) {
5469 return;
5470 }
5471
5472 hide_root = p_enabled;
5473 queue_accessibility_update();
5474 queue_redraw();
5475 update_minimum_size();
5476 }
5477
5478 bool Tree::is_root_hidden() const {
5479 return hide_root;
5480 }
5481
5482 void Tree::set_column_custom_minimum_width(int p_column, int p_min_width) {
5483 ERR_FAIL_INDEX(p_column, columns.size());
5484
5485 if (columns[p_column].custom_min_width == p_min_width) {
5486 return;
5487 }
5488
5489 if (p_min_width < 0) {
5490 return;
5491 }
5492 columns.write[p_column].custom_min_width = p_min_width;
5493 columns.write[p_column].cached_minimum_width_dirty = true;
5494 queue_accessibility_update();
5495 queue_redraw();
5496 }
5497
5498 void Tree::set_column_expand(int p_column, bool p_expand) {
5499 ERR_FAIL_INDEX(p_column, columns.size());
5500
5501 if (columns[p_column].expand == p_expand) {
5502 return;
5503 }
5504
5505 columns.write[p_column].expand = p_expand;
5506 columns.write[p_column].cached_minimum_width_dirty = true;
5507 queue_accessibility_update();
5508 queue_redraw();
5509 }
5510
5511 void Tree::set_column_expand_ratio(int p_column, int p_ratio) {
5512 ERR_FAIL_INDEX(p_column, columns.size());
5513
5514 if (columns[p_column].expand_ratio == p_ratio) {
5515 return;
5516 }
5517
5518 columns.write[p_column].expand_ratio = p_ratio;
5519 columns.write[p_column].cached_minimum_width_dirty = true;
5520 queue_accessibility_update();
5521 queue_redraw();
5522 }
5523
5524 void Tree::set_column_clip_content(int p_column, bool p_fit) {
5525 ERR_FAIL_INDEX(p_column, columns.size());
5526
5527 if (columns[p_column].clip_content == p_fit) {
5528 return;
5529 }
5530
5531 columns.write[p_column].clip_content = p_fit;
5532 columns.write[p_column].cached_minimum_width_dirty = true;
5533 queue_accessibility_update();
5534 queue_redraw();
5535 }
5536
5537 bool Tree::is_column_expanding(int p_column) const {
5538 ERR_FAIL_INDEX_V(p_column, columns.size(), false);
5539
5540 return columns[p_column].expand;
5541 }
5542
5543 int Tree::get_column_expand_ratio(int p_column) const {
5544 ERR_FAIL_INDEX_V(p_column, columns.size(), 1);
5545
5546 return columns[p_column].expand_ratio;
5547 }
5548
5549 bool Tree::is_column_clipping_content(int p_column) const {
5550 ERR_FAIL_INDEX_V(p_column, columns.size(), false);
5551
5552 return columns[p_column].clip_content;
5553 }
5554
5555 TreeItem *Tree::get_selected() const {
5556 return selected_item;
5557 }
5558
5559 void Tree::set_selected(TreeItem *p_item, int p_column) {
5560 ERR_FAIL_INDEX(p_column, columns.size());
5561 ERR_FAIL_NULL(p_item);
5562 ERR_FAIL_COND_MSG(p_item->get_tree() != this, "The provided TreeItem does not belong to this Tree. Ensure that the TreeItem is a part of the Tree before setting it as selected.");
5563
5564 select_single_item(p_item, get_root(), p_column);
5565 }
5566
5567 int Tree::get_selected_column() const {
5568 return selected_col;
5569 }
5570
5571 TreeItem *Tree::get_edited() const {
5572 return edited_item;
5573 }
5574
5575 int Tree::get_edited_column() const {
5576 return edited_col;
5577 }
5578
5579 TreeItem *Tree::get_next_selected(TreeItem *p_item) {
5580 if (!root) {
5581 return nullptr;
5582 }
5583
5584 while (true) {
5585 if (!p_item) {
5586 p_item = root;
5587 } else {
5588 if (p_item->first_child) {
5589 p_item = p_item->first_child;
5590
5591 } else if (p_item->next) {
5592 p_item = p_item->next;
5593 } else {
5594 while (!p_item->next) {
5595 p_item = p_item->parent;
5596 if (p_item == nullptr) {
5597 return nullptr;
5598 }
5599 }
5600
5601 p_item = p_item->next;
5602 }
5603 }
5604
5605 for (int i = 0; i < columns.size(); i++) {
5606 if (p_item->cells[i].selected) {
5607 return p_item;
5608 }
5609 }
5610 }
5611
5612 return nullptr;
5613 }
5614
5615 int Tree::get_column_minimum_width(int p_column) const {
5616 ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
5617
5618 if (columns[p_column].cached_minimum_width_dirty) {
5619 // Use the custom minimum width.
5620 int min_width = columns[p_column].custom_min_width;
5621
5622 // Check if the visible title of the column is wider.
5623 if (show_column_titles) {
5624 const float padding = theme_cache.title_button->get_margin(SIDE_LEFT) + theme_cache.title_button->get_margin(SIDE_RIGHT);
5625 min_width = MAX(theme_cache.font->get_string_size(columns[p_column].xl_title, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + padding, min_width);
5626 }
5627
5628 if (root && !columns[p_column].clip_content) {
5629 int depth = 1;
5630
5631 TreeItem *last = nullptr;
5632 TreeItem *first = hide_root ? root->get_next_visible() : root;
5633 for (TreeItem *item = first; item; last = item, item = item->get_next_visible()) {
5634 // Get column indentation.
5635 int indent;
5636 if (p_column == 0) {
5637 if (last) {
5638 if (item->parent == last) {
5639 depth += 1;
5640 } else if (item->parent != last->parent) {
5641 depth = hide_root ? 0 : 1;
5642 for (TreeItem *iter = item->parent; iter; iter = iter->parent) {
5643 depth += 1;
5644 }
5645 }
5646 }
5647 indent = theme_cache.item_margin * depth;
5648 } else {
5649 indent = theme_cache.h_separation;
5650 }
5651
5652 // Get the item minimum size.
5653 Size2 item_size = item->get_minimum_size(p_column);
5654 item_size.width += indent;
5655
5656 // Check if the item is wider.
5657 min_width = MAX(min_width, item_size.width);
5658 }
5659 }
5660
5661 columns.get(p_column).cached_minimum_width = min_width;
5662 columns.get(p_column).cached_minimum_width_dirty = false;
5663 }
5664
5665 return columns[p_column].cached_minimum_width;
5666 }
5667
5668 int Tree::get_column_width(int p_column) const {
5669 ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
5670
5671 int column_width = get_column_minimum_width(p_column);
5672
5673 if (columns[p_column].expand) {
5674 int expand_area = _get_content_rect().size.width;
5675 int expanding_total = 0;
5676
5677 for (int i = 0; i < columns.size(); i++) {
5678 expand_area -= get_column_minimum_width(i);
5679 if (columns[i].expand) {
5680 expanding_total += columns[i].expand_ratio;
5681 }
5682 }
5683
5684 if (expand_area >= expanding_total && expanding_total > 0) {
5685 column_width += expand_area * columns[p_column].expand_ratio / expanding_total;
5686 }
5687 }
5688
5689 return column_width;
5690 }
5691
5692 void Tree::propagate_set_columns(TreeItem *p_item) {
5693 p_item->cells.resize(columns.size());
5694 p_item->accessibility_row_dirty = true;
5695 for (TreeItem::Cell &cell : p_item->cells) {
5696 if (cell.accessibility_cell_element.is_valid()) {
5697 DisplayServer::get_singleton()->accessibility_free_element(cell.accessibility_cell_element);
5698 cell.accessibility_cell_element = RID();
5699 }
5700 for (TreeItem::Cell::Button &btn : cell.buttons) {
5701 if (btn.accessibility_button_element.is_valid()) {
5702 DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
5703 btn.accessibility_button_element = RID();
5704 }
5705 }
5706 }
5707
5708 TreeItem *c = p_item->get_first_child();
5709 while (c) {
5710 propagate_set_columns(c);
5711 c = c->next;
5712 }
5713 }
5714
5715 void Tree::set_columns(int p_columns) {
5716 ERR_FAIL_COND(p_columns < 1);
5717 ERR_FAIL_COND(blocked > 0);
5718
5719 if (columns.size() > p_columns) {
5720 for (int i = p_columns; i < columns.size(); i++) {
5721 if (columns[i].accessibility_col_element.is_valid()) {
5722 DisplayServer::get_singleton()->accessibility_free_element(columns[i].accessibility_col_element);
5723 columns.write[i].accessibility_col_element = RID();
5724 }
5725 }
5726 }
5727
5728 columns.resize(p_columns);
5729
5730 if (root) {
5731 propagate_set_columns(root);
5732 }
5733 if (selected_col >= p_columns) {
5734 selected_col = p_columns - 1;
5735 selected_button = -1;
5736 }
5737 update_min_size_for_item_change();
5738 queue_accessibility_update();
5739 queue_redraw();
5740 }
5741
5742 int Tree::get_columns() const {
5743 return columns.size();
5744 }
5745
5746 void Tree::_scroll_moved(float) {
5747 _determine_hovered_item();
5748 queue_redraw();
5749 }
5750
5751 Rect2 Tree::get_custom_popup_rect() const {
5752 return custom_popup_rect;
5753 }
5754
5755 int Tree::get_item_offset(TreeItem *p_item) const {
5756 TreeItem *it = root;
5757 int ofs = _get_title_button_height();
5758 if (!it) {
5759 return 0;
5760 }
5761
5762 while (true) {
5763 if (it == p_item) {
5764 return ofs;
5765 }
5766
5767 if ((it != root || !hide_root) && it->is_visible_in_tree()) {
5768 ofs += compute_item_height(it);
5769 ofs += theme_cache.v_separation;
5770 }
5771
5772 if (it->first_child && !it->collapsed) {
5773 it = it->first_child;
5774
5775 } else if (it->next) {
5776 it = it->next;
5777 } else {
5778 while (!it->next) {
5779 it = it->parent;
5780 if (it == nullptr) {
5781 return 0;
5782 }
5783 }
5784
5785 it = it->next;
5786 }
5787 }
5788
5789 return -1; // Not found.
5790 }
5791
5792 void Tree::ensure_cursor_is_visible() {
5793 if (!is_inside_tree()) {
5794 return;
5795 }
5796 if (!selected_item || (selected_col == -1)) {
5797 return; // Nothing under cursor.
5798 }
5799
5800 // Note: Code below similar to `Tree::scroll_to_item()`, in case of bug fix both.
5801 const Size2 area_size = _get_content_rect().size;
5802
5803 int y_offset = get_item_offset(selected_item);
5804 if (y_offset != -1) {
5805 const int tbh = _get_title_button_height();
5806 y_offset -= tbh;
5807
5808 const int cell_h = compute_item_height(selected_item) + theme_cache.v_separation;
5809 int screen_h = area_size.height - tbh;
5810
5811 if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
5812 v_scroll->set_value(y_offset);
5813 } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
5814 callable_mp((Range *)v_scroll, &Range::set_value).call_deferred(y_offset - screen_h + cell_h);
5815 } else if (y_offset < v_scroll->get_value()) {
5816 v_scroll->set_value(y_offset);
5817 }
5818 }
5819
5820 if (select_mode != SELECT_ROW) { // Cursor always at column 0 in this mode.
5821 int x_offset = 0;
5822 for (int i = 0; i < selected_col; i++) {
5823 x_offset += get_column_width(i);
5824 }
5825
5826 const int cell_w = get_column_width(selected_col);
5827 const int screen_w = area_size.width;
5828
5829 if (cell_w > screen_w) {
5830 h_scroll->set_value(x_offset);
5831 } else if (x_offset + cell_w > h_scroll->get_value() + screen_w) {
5832 callable_mp((Range *)h_scroll, &Range::set_value).call_deferred(x_offset - screen_w + cell_w);
5833 } else if (x_offset < h_scroll->get_value()) {
5834 h_scroll->set_value(x_offset);
5835 }
5836 }
5837
5838 queue_accessibility_update();
5839 }
5840
5841 int Tree::get_pressed_button() const {
5842 return pressed_button;
5843 }
5844
5845 Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column, int p_button) const {
5846 ERR_FAIL_NULL_V(p_item, Rect2());
5847 ERR_FAIL_COND_V(p_item->tree != this, Rect2());
5848 if (p_column != -1) {
5849 ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
5850 }
5851 if (p_button != -1) {
5852 ERR_FAIL_COND_V(p_column == -1, Rect2()); // Pass a column if you want to pass a button.
5853 ERR_FAIL_INDEX_V(p_button, p_item->cells[p_column].buttons.size(), Rect2());
5854 }
5855
5856 int ofs = get_item_offset(p_item);
5857 int height = compute_item_height(p_item) + theme_cache.v_separation;
5858 Rect2 r;
5859 r.position.y = ofs - theme_cache.offset.y + theme_cache.panel_style->get_offset().y;
5860 r.size.height = height;
5861 bool rtl = is_layout_rtl();
5862 const Rect2 content_rect = _get_content_rect();
5863
5864 if (p_column == -1) {
5865 r.position.x = 0;
5866 r.size.x = get_size().width;
5867 } else {
5868 int accum = 0;
5869 for (int i = 0; i < p_column; i++) {
5870 accum += get_column_width(i);
5871 }
5872 r.position.x = (rtl) ? get_size().x - (accum - theme_cache.offset.x) - get_column_width(p_column) - theme_cache.panel_style->get_margin(SIDE_LEFT) : accum - theme_cache.offset.x + theme_cache.panel_style->get_margin(SIDE_LEFT);
5873 r.size.x = get_column_width(p_column);
5874 if (p_button != -1) {
5875 // RTL direction support for button rect is different due to buttons not
5876 // having same behavior as they do in LTR when tree is scrolled.
5877 const TreeItem::Cell &c = p_item->cells[p_column];
5878 Vector2 ofst = Vector2(r.position.x + r.size.x + theme_cache.button_margin, r.position.y);
5879
5880 // Compute total width of buttons block including spacings.
5881 int buttons_width = 0;
5882 for (int j = c.buttons.size() - 1; j >= 0; j--) {
5883 Ref<Texture2D> b = c.buttons[j].texture;
5884 Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
5885 buttons_width += size.width + theme_cache.button_margin;
5886 }
5887 for (int j = c.buttons.size() - 1; j >= 0; j--) {
5888 Ref<Texture2D> b = c.buttons[j].texture;
5889 Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
5890 ofst.x -= size.x + theme_cache.button_margin;
5891
5892 if (rtl) {
5893 if (j == p_button) {
5894 return Rect2(r.position, Size2(size.x, r.size.y));
5895 }
5896 r.position.x += size.x + theme_cache.button_margin;
5897 continue;
5898 }
5899
5900 if (j == p_button) {
5901 float content_rect_end_x = content_rect.position.x + content_rect.size.width;
5902 if (r.position.x + r.size.x < content_rect_end_x) {
5903 return Rect2(ofst, Size2(size.x, r.size.y));
5904 }
5905 // Button block can be outside of `content_rect`.
5906 if (content_rect_end_x - (r.position.x + theme_cache.h_separation) < buttons_width) {
5907 return Rect2(r.position + Point2(theme_cache.h_separation + (buttons_width - ((r.position.x + r.size.x) - ofst.x)), 0), Size2(size.x, r.size.y));
5908 }
5909
5910 return Rect2(ofst - Vector2((r.position.x + r.size.x) - content_rect_end_x, 0), Size2(size.x, r.size.y));
5911 }
5912 }
5913 }
5914 }
5915
5916 return r;
5917 }
5918
5919 void Tree::set_column_titles_visible(bool p_show) {
5920 if (show_column_titles == p_show) {
5921 return;
5922 }
5923
5924 show_column_titles = p_show;
5925 queue_accessibility_update();
5926 queue_redraw();
5927 update_minimum_size();
5928 }
5929
5930 bool Tree::are_column_titles_visible() const {
5931 return show_column_titles;
5932 }
5933
5934 void Tree::set_column_title(int p_column, const String &p_title) {
5935 ERR_FAIL_INDEX(p_column, columns.size());
5936
5937 if (columns[p_column].title == p_title) {
5938 return;
5939 }
5940
5941 columns.write[p_column].title = p_title;
5942 columns.write[p_column].xl_title = atr(p_title);
5943 update_column(p_column);
5944 queue_accessibility_update();
5945 queue_redraw();
5946 }
5947
5948 String Tree::get_column_title(int p_column) const {
5949 ERR_FAIL_INDEX_V(p_column, columns.size(), "");
5950 return columns[p_column].title;
5951 }
5952
5953 void Tree::set_column_title_tooltip_text(int p_column, const String &p_tooltip) {
5954 ERR_FAIL_INDEX(p_column, columns.size());
5955 columns.write[p_column].title_tooltip = p_tooltip;
5956 }
5957
5958 String Tree::get_column_title_tooltip_text(int p_column) const {
5959 ERR_FAIL_INDEX_V(p_column, columns.size(), "");
5960 return columns[p_column].title_tooltip;
5961 }
5962
5963 void Tree::set_column_title_alignment(int p_column, HorizontalAlignment p_alignment) {
5964 ERR_FAIL_INDEX(p_column, columns.size());
5965
5966 if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
5967 WARN_PRINT("HORIZONTAL_ALIGNMENT_FILL is not supported for column titles.");
5968 }
5969
5970 if (columns[p_column].title_alignment == p_alignment) {
5971 return;
5972 }
5973
5974 columns.write[p_column].title_alignment = p_alignment;
5975 update_column(p_column);
5976 queue_accessibility_update();
5977 queue_redraw();
5978 }
5979
5980 HorizontalAlignment Tree::get_column_title_alignment(int p_column) const {
5981 ERR_FAIL_INDEX_V(p_column, columns.size(), HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
5982 return columns[p_column].title_alignment;
5983 }
5984
5985 void Tree::set_column_title_direction(int p_column, Control::TextDirection p_text_direction) {
5986 ERR_FAIL_INDEX(p_column, columns.size());
5987 ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
5988 if (columns[p_column].text_direction != p_text_direction) {
5989 columns.write[p_column].text_direction = p_text_direction;
5990 update_column(p_column);
5991 queue_accessibility_update();
5992 queue_redraw();
5993 }
5994 }
5995
5996 Control::TextDirection Tree::get_column_title_direction(int p_column) const {
5997 ERR_FAIL_INDEX_V(p_column, columns.size(), TEXT_DIRECTION_INHERITED);
5998 return columns[p_column].text_direction;
5999 }
6000
6001 void Tree::set_column_title_language(int p_column, const String &p_language) {
6002 ERR_FAIL_INDEX(p_column, columns.size());
6003 if (columns[p_column].language != p_language) {
6004 columns.write[p_column].language = p_language;
6005 update_column(p_column);
6006 queue_accessibility_update();
6007 queue_redraw();
6008 }
6009 }
6010
6011 String Tree::get_column_title_language(int p_column) const {
6012 ERR_FAIL_INDEX_V(p_column, columns.size(), "");
6013 return columns[p_column].language;
6014 }
6015
6016 Point2 Tree::get_scroll() const {
6017 Point2 ofs;
6018 if (h_scroll->is_visible_in_tree()) {
6019 ofs.x = h_scroll->get_value();
6020 }
6021 if (v_scroll->is_visible_in_tree()) {
6022 ofs.y = v_scroll->get_value();
6023 }
6024 return ofs;
6025 }
6026
6027 void Tree::scroll_to_item(TreeItem *p_item, bool p_center_on_item) {
6028 ERR_FAIL_NULL(p_item);
6029
6030 update_scrollbars();
6031
6032 // Note: Code below similar to `Tree::ensure_cursor_is_visible()`, in case of bug fix both.
6033 const Size2 area_size = _get_content_rect().size;
6034
6035 int y_offset = get_item_offset(p_item);
6036 if (y_offset != -1) {
6037 const int title_button_height = _get_title_button_height();
6038 y_offset -= title_button_height;
6039
6040 const int cell_h = compute_item_height(p_item) + theme_cache.v_separation;
6041 int screen_h = area_size.height - title_button_height;
6042
6043 if (p_center_on_item) {
6044 // This makes sure that centering the offset doesn't overflow.
6045 const double v_scroll_value = y_offset - MAX((screen_h - cell_h) / 2.0, 0.0);
6046 v_scroll->set_value(v_scroll_value);
6047 } else {
6048 if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
6049 v_scroll->set_value(y_offset);
6050 } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
6051 v_scroll->set_value(y_offset - screen_h + cell_h);
6052 } else if (y_offset < v_scroll->get_value()) {
6053 v_scroll->set_value(y_offset);
6054 }
6055 }
6056 }
6057 queue_accessibility_update();
6058 }
6059
6060 void Tree::set_h_scroll_enabled(bool p_enable) {
6061 if (h_scroll_enabled == p_enable) {
6062 return;
6063 }
6064
6065 h_scroll_enabled = p_enable;
6066 update_minimum_size();
6067 }
6068
6069 bool Tree::is_h_scroll_enabled() const {
6070 return h_scroll_enabled;
6071 }
6072
6073 void Tree::set_v_scroll_enabled(bool p_enable) {
6074 if (v_scroll_enabled == p_enable) {
6075 return;
6076 }
6077
6078 v_scroll_enabled = p_enable;
6079 update_minimum_size();
6080 }
6081
6082 bool Tree::is_v_scroll_enabled() const {
6083 return v_scroll_enabled;
6084 }
6085
6086 void Tree::set_scroll_hint_mode(ScrollHintMode p_mode) {
6087 if (scroll_hint_mode == p_mode) {
6088 return;
6089 }
6090
6091 scroll_hint_mode = p_mode;
6092 queue_redraw();
6093 }
6094
6095 Tree::ScrollHintMode Tree::get_scroll_hint_mode() const {
6096 return scroll_hint_mode;
6097 }
6098
6099 void Tree::set_tile_scroll_hint(bool p_enable) {
6100 if (tile_scroll_hint == p_enable) {
6101 return;
6102 }
6103
6104 tile_scroll_hint = p_enable;
6105 queue_redraw();
6106 }
6107
6108 bool Tree::is_scroll_hint_tiled() {
6109 return tile_scroll_hint;
6110 }
6111
6112 TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
6113 TreeItem *from = p_at;
6114 TreeItem *loop = nullptr; // Safe-guard against infinite loop.
6115
6116 while (p_at) {
6117 for (int i = 0; i < columns.size(); i++) {
6118 if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
6119 if (r_col) {
6120 *r_col = i;
6121 }
6122 return p_at;
6123 }
6124 }
6125
6126 if (p_backwards) {
6127 p_at = p_at->get_prev_visible(true);
6128 } else {
6129 p_at = p_at->get_next_visible(true);
6130 }
6131
6132 if ((p_at) == from) {
6133 break;
6134 }
6135
6136 if (!loop) {
6137 loop = p_at;
6138 } else if (loop == p_at) {
6139 break;
6140 }
6141 }
6142
6143 return nullptr;
6144 }
6145
6146 TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
6147 TreeItem *from = get_selected();
6148
6149 if (!from) {
6150 from = root;
6151 }
6152 if (!from) {
6153 return nullptr;
6154 }
6155
6156 return _search_item_text(from->get_next_visible(true), p_find, r_col, p_selectable);
6157 }
6158
6159 TreeItem *Tree::get_item_with_text(const String &p_find) const {
6160 for (TreeItem *current = root; current; current = current->get_next_visible()) {
6161 for (int i = 0; i < columns.size(); i++) {
6162 if (current->get_text(i) == p_find) {
6163 return current;
6164 }
6165 }
6166 }
6167 return nullptr;
6168 }
6169
6170 TreeItem *Tree::get_item_with_metadata(const Variant &p_find, int p_column) const {
6171 if (p_column < 0) {
6172 for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
6173 for (int i = 0; i < columns.size(); i++) {
6174 if (current->get_metadata(i) == p_find) {
6175 return current;
6176 }
6177 }
6178 }
6179 return nullptr;
6180 }
6181
6182 for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
6183 if (current->get_metadata(p_column) == p_find) {
6184 return current;
6185 }
6186 }
6187 return nullptr;
6188 }
6189
6190 void Tree::_do_incr_search(const String &p_add) {
6191 uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // Convert to msec.
6192 uint64_t diff = time - last_keypress;
6193 if (diff > uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec"))) {
6194 incr_search = p_add;
6195 } else if (incr_search != p_add) {
6196 incr_search += p_add;
6197 }
6198
6199 last_keypress = time;
6200 int col;
6201 TreeItem *item = search_item_text(incr_search, &col, true);
6202 if (!item) {
6203 return;
6204 }
6205
6206 if (select_mode == SELECT_MULTI) {
6207 item->set_as_cursor(col);
6208 } else {
6209 item->select(col);
6210 }
6211 ensure_cursor_is_visible();
6212 }
6213
6214 TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &r_height, int &r_section) const {
6215 r_column = -1;
6216 r_height = 0;
6217 r_section = -100;
6218
6219 if (!root) {
6220 return nullptr;
6221 }
6222
6223 Point2 pos = p_pos;
6224
6225 if ((root != p_item || !hide_root) && p_item->is_visible_in_tree()) {
6226 r_height = compute_item_height(p_item) + theme_cache.v_separation;
6227 if (pos.y < r_height) {
6228 if (drop_mode_flags == DROP_MODE_ON_ITEM) {
6229 r_section = 0;
6230 } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
6231 r_section = pos.y < r_height / 2 ? -1 : 1;
6232 } else if (pos.y < r_height / 4) {
6233 r_section = -1;
6234 } else if (pos.y >= (r_height * 3 / 4)) {
6235 r_section = 1;
6236 } else {
6237 r_section = 0;
6238 }
6239
6240 for (int i = 0; i < columns.size(); i++) {
6241 int col_width = get_column_width(i);
6242
6243 if (p_item->cells[i].expand_right) {
6244 int plus = 1;
6245 while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text.is_empty() && p_item->cells[i + plus].icon.is_null() && p_item->cells[i + plus].buttons.is_empty()) {
6246 col_width += theme_cache.h_separation;
6247 col_width += get_column_width(i + plus);
6248 plus++;
6249 }
6250 }
6251
6252 if (pos.x < col_width) {
6253 r_column = i;
6254 return p_item;
6255 }
6256 pos.x -= col_width;
6257 }
6258
6259 return nullptr;
6260 } else {
6261 pos.y -= r_height;
6262 }
6263 } else {
6264 r_height = 0;
6265 }
6266
6267 if (p_item->is_collapsed() || !p_item->is_visible_in_tree()) {
6268 return nullptr; // Do not try children, it's collapsed.
6269 }
6270
6271 TreeItem *n = p_item->get_first_child();
6272 while (n) {
6273 int ch;
6274 TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, r_section);
6275 pos.y -= ch;
6276 r_height += ch;
6277 if (r) {
6278 return r;
6279 }
6280 n = n->get_next();
6281 }
6282
6283 return nullptr;
6284 }
6285
6286 void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index, int &r_section) const {
6287 // When on a button, `r_index` is valid.
6288 // When on an item, both `r_item` and `r_column` are valid.
6289 // Otherwise, all output arguments are invalid.
6290 r_item = nullptr;
6291 r_column = -1;
6292 r_index = -1;
6293 r_section = -1;
6294
6295 if (!root) {
6296 return;
6297 }
6298
6299 Point2 pos = p_pos;
6300 if (cache.rtl) {
6301 pos.x = get_size().width - pos.x - 1;
6302 }
6303 Point2 margin_offset = theme_cache.panel_style->get_offset();
6304 pos -= margin_offset;
6305 pos.y -= _get_title_button_height();
6306 if (pos.y + margin_offset.y < 0) {
6307 return;
6308 }
6309 pos += theme_cache.offset; // Scrolling.
6310
6311 int x_limit = _get_content_rect().size.x + theme_cache.offset.width;
6312
6313 if (pos.x > x_limit) {
6314 // Inside the scroll area.
6315 return;
6316 }
6317
6318 int col, h, section;
6319 TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
6320 if (!it) {
6321 return;
6322 }
6323
6324 r_item = it;
6325 r_column = col;
6326 r_section = section;
6327
6328 const TreeItem::Cell &c = it->cells[col];
6329 if (c.buttons.is_empty()) {
6330 return;
6331 }
6332
6333 // Search the button index.
6334 FindColumnButtonResult result = _find_column_and_button_at_pos(pos.x, it, 0, x_limit);
6335 r_index = result.button_index;
6336 }
6337
6338 Tree::FindColumnButtonResult Tree::_find_column_and_button_at_pos(int p_x, const TreeItem *p_item, int p_x_ofs, int p_x_limit) const {
6339 int x = p_x;
6340
6341 int col = -1;
6342 int col_ofs = 0;
6343 int col_width = 0;
6344
6345 int limit_w = p_x_limit;
6346
6347 FindColumnButtonResult result;
6348
6349 for (int i = 0; i < columns.size(); i++) {
6350 col_width = get_column_width(i);
6351
6352 if (p_item->cells[i].expand_right) {
6353 int plus = 1;
6354 while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
6355 col_width += theme_cache.h_separation;
6356 col_width += get_column_width(i + plus);
6357 plus++;
6358 }
6359 }
6360
6361 if (x < col_width) {
6362 col = i;
6363 break;
6364 }
6365
6366 col_ofs += col_width;
6367 x -= col_width;
6368 limit_w -= col_width;
6369 }
6370
6371 if (col >= 0) {
6372 if (col == 0) {
6373 int margin = p_x_ofs + theme_cache.item_margin;
6374 col_width -= margin;
6375 limit_w -= margin;
6376 col_ofs += margin;
6377 x -= margin;
6378 } else {
6379 col_width -= theme_cache.h_separation;
6380 limit_w -= theme_cache.h_separation;
6381 x -= theme_cache.h_separation;
6382 }
6383
6384 if (!cache.rtl && !p_item->cells[col].buttons.is_empty()) {
6385 int button_w = 0;
6386 for (int j = p_item->cells[col].buttons.size() - 1; j >= 0; j--) {
6387 const Ref<Texture2D> &b = p_item->cells[col].buttons[j].texture;
6388 button_w += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
6389 }
6390
6391 col_width = MAX(button_w, MIN(limit_w, col_width));
6392 }
6393
6394 // Cell button detection code.
6395 // The first half of the button margin will be applied to the left button,
6396 // and the other half to the right button.
6397 const int offset_button_width = theme_cache.button_margin / 2;
6398 const TreeItem::Cell &c = p_item->cells[col];
6399 for (int j = c.buttons.size() - 1; j >= 0; j--) {
6400 const Ref<Texture2D> &b = c.buttons[j].texture;
6401 int w = b->get_size().width + theme_cache.button_pressed->get_minimum_size().width;
6402
6403 if (x >= col_width - w - offset_button_width - 1) {
6404 result.button_index = j;
6405 break;
6406 }
6407
6408 col_width -= w + theme_cache.button_margin;
6409 }
6410 }
6411
6412 result.column_index = col;
6413 result.column_offset = col_ofs;
6414 result.column_width = col_width;
6415 result.pos_x = x;
6416
6417 return result;
6418 }
6419
6420 int Tree::get_column_at_position(const Point2 &p_pos) const {
6421 if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
6422 return -1;
6423 }
6424
6425 Point2 pos = p_pos;
6426 if (is_layout_rtl()) {
6427 pos.x = get_size().width - pos.x - 1;
6428 }
6429 Point2 margin_offset = theme_cache.panel_style->get_offset();
6430 pos -= margin_offset;
6431 pos.y -= _get_title_button_height();
6432 if (pos.y + margin_offset.y < 0) {
6433 return -1;
6434 }
6435
6436 if (h_scroll->is_visible_in_tree()) {
6437 pos.x += h_scroll->get_value();
6438 }
6439 if (v_scroll->is_visible_in_tree()) {
6440 pos.y += v_scroll->get_value();
6441 }
6442
6443 int col, h, section;
6444 TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
6445
6446 if (it) {
6447 return col;
6448 }
6449 return -1;
6450 }
6451
6452 int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
6453 if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
6454 return -100;
6455 }
6456
6457 Point2 pos = p_pos;
6458 if (is_layout_rtl()) {
6459 pos.x = get_size().width - pos.x - 1;
6460 }
6461 Point2 margin_offset = theme_cache.panel_style->get_offset();
6462 pos -= margin_offset;
6463 pos.y -= _get_title_button_height();
6464 if (pos.y + margin_offset.y < 0) {
6465 return -100;
6466 }
6467
6468 if (h_scroll->is_visible_in_tree()) {
6469 pos.x += h_scroll->get_value();
6470 }
6471 if (v_scroll->is_visible_in_tree()) {
6472 pos.y += v_scroll->get_value();
6473 }
6474
6475 int col, h, section;
6476 TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
6477
6478 if (it) {
6479 return section;
6480 }
6481 return -100;
6482 }
6483
6484 bool Tree::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
6485 if (drag_touching) {
6486 // Disable data drag & drop when touch dragging.
6487 return false;
6488 }
6489
6490 return Control::can_drop_data(p_point, p_data);
6491 }
6492
6493 Variant Tree::get_drag_data(const Point2 &p_point) {
6494 if (drag_touching) {
6495 // Disable data drag & drop when touch dragging.
6496 return Variant();
6497 }
6498
6499 return Control::get_drag_data(p_point);
6500 }
6501
6502 TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
6503 if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
6504 return nullptr;
6505 }
6506
6507 Point2 pos = p_pos;
6508 if (is_layout_rtl()) {
6509 pos.x = get_size().width - pos.x - 1;
6510 }
6511 Point2 margin_offset = theme_cache.panel_style->get_offset();
6512 pos -= margin_offset;
6513 pos.y -= _get_title_button_height();
6514 if (pos.y + margin_offset.y < 0) {
6515 return nullptr;
6516 }
6517
6518 if (h_scroll->is_visible_in_tree()) {
6519 pos.x += h_scroll->get_value();
6520 }
6521 if (v_scroll->is_visible_in_tree()) {
6522 pos.y += v_scroll->get_value();
6523 }
6524
6525 int col, h, section;
6526 TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
6527
6528 if (it) {
6529 return it;
6530 }
6531 return nullptr;
6532 }
6533
6534 int Tree::get_button_id_at_position(const Point2 &p_pos) const {
6535 if (!root) {
6536 return -1;
6537 }
6538
6539 if (p_pos == Vector2(Math::INF, Math::INF)) {
6540 if (selected_item && selected_button >= 0) {
6541 return selected_item->cells[selected_col].buttons[selected_button].id;
6542 }
6543 } else {
6544 if (!Rect2(Vector2(), get_size()).has_point(p_pos)) {
6545 return -1;
6546 }
6547
6548 TreeItem *it;
6549 int col, index, section;
6550 _find_button_at_pos(p_pos, it, col, index, section);
6551
6552 if (index == -1) {
6553 return -1;
6554 }
6555 return it->cells[col].buttons[index].id;
6556 }
6557 return -1;
6558 }
6559
6560 String Tree::get_tooltip(const Point2 &p_pos) const {
6561 Point2 pos = p_pos - theme_cache.panel_style->get_offset();
6562 pos.y -= _get_title_button_height();
6563
6564 // `pos.y` less than 0 indicates we're in the header.
6565 if (pos.y < 0) {
6566 // Get the x position of the cursor.
6567 real_t pos_x = p_pos.x;
6568 if (is_layout_rtl()) {
6569 pos_x = get_size().width - pos_x;
6570 }
6571 pos_x -= theme_cache.panel_style->get_offset().x;
6572 if (h_scroll->is_visible_in_tree()) {
6573 pos_x += h_scroll->get_value();
6574 }
6575
6576 // Walk forwards until we know which column we're in.
6577 int next_edge = 0;
6578 int i = 0;
6579 for (const ColumnInfo &column : columns) {
6580 next_edge += get_column_width(i++);
6581
6582 if (pos_x < next_edge) {
6583 if (!column.title_tooltip.is_empty()) {
6584 return column.title_tooltip;
6585 }
6586 break;
6587 }
6588 }
6589
6590 // If the column has no tooltip, use the default.
6591 return Control::get_tooltip(p_pos);
6592 }
6593
6594 TreeItem *it;
6595 int col, index, section;
6596 _find_button_at_pos(p_pos, it, col, index, section);
6597
6598 if (index != -1) {
6599 return it->cells[col].buttons[index].tooltip;
6600 }
6601
6602 if (it) {
6603 const String item_tooltip = it->get_tooltip_text(col);
6604 if (enable_auto_tooltip && item_tooltip.is_empty()) {
6605 return it->get_text(col);
6606 }
6607 return item_tooltip;
6608 }
6609
6610 return Control::get_tooltip(p_pos);
6611 }
6612
6613 void Tree::set_cursor_can_exit_tree(bool p_enable) {
6614 cursor_can_exit_tree = p_enable;
6615 }
6616
6617 void Tree::set_hide_folding(bool p_hide) {
6618 if (hide_folding == p_hide) {
6619 return;
6620 }
6621
6622 hide_folding = p_hide;
6623 queue_redraw();
6624 }
6625
6626 bool Tree::is_folding_hidden() const {
6627 return hide_folding;
6628 }
6629
6630 void Tree::set_enable_recursive_folding(bool p_enable) {
6631 enable_recursive_folding = p_enable;
6632 }
6633
6634 bool Tree::is_recursive_folding_enabled() const {
6635 return enable_recursive_folding;
6636 }
6637
6638 void Tree::set_enable_drag_unfolding(bool p_enable) {
6639 if (enable_drag_unfolding == p_enable) {
6640 return;
6641 }
6642
6643 enable_drag_unfolding = p_enable;
6644 if (!enable_drag_unfolding && !dropping_unfold_timer->is_stopped()) {
6645 dropping_unfold_timer->stop();
6646 }
6647 }
6648
6649 bool Tree::is_drag_unfolding_enabled() const {
6650 return enable_drag_unfolding;
6651 }
6652
6653 void Tree::set_drop_mode_flags(int p_flags) {
6654 if (drop_mode_flags == p_flags) {
6655 return;
6656 }
6657 drop_mode_flags = p_flags;
6658 if (drop_mode_flags == 0) {
6659 _reset_drop_mode_over();
6660 }
6661
6662 queue_redraw();
6663 }
6664
6665 int Tree::get_drop_mode_flags() const {
6666 return drop_mode_flags;
6667 }
6668
6669 void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
6670 force_edit_checkbox_only_on_checkbox = p_enable;
6671 }
6672
6673 bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
6674 return force_edit_checkbox_only_on_checkbox;
6675 }
6676
6677 void Tree::set_allow_rmb_select(bool p_allow) {
6678 allow_rmb_select = p_allow;
6679 }
6680
6681 bool Tree::get_allow_rmb_select() const {
6682 return allow_rmb_select;
6683 }
6684
6685 void Tree::set_allow_reselect(bool p_allow) {
6686 allow_reselect = p_allow;
6687 }
6688
6689 bool Tree::get_allow_reselect() const {
6690 return allow_reselect;
6691 }
6692
6693 void Tree::set_allow_search(bool p_allow) {
6694 allow_search = p_allow;
6695 }
6696
6697 bool Tree::get_allow_search() const {
6698 return allow_search;
6699 }
6700
6701 void Tree::set_auto_tooltip(bool p_enable) {
6702 enable_auto_tooltip = p_enable;
6703 }
6704
6705 bool Tree::is_auto_tooltip_enabled() const {
6706 return enable_auto_tooltip;
6707 }
6708
6709 void Tree::_bind_methods() {
6710 ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
6711 ClassDB::bind_method(D_METHOD("create_item", "parent", "index"), &Tree::create_item, DEFVAL(Variant()), DEFVAL(-1));
6712
6713 ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
6714 ClassDB::bind_method(D_METHOD("set_column_custom_minimum_width", "column", "min_width"), &Tree::set_column_custom_minimum_width);
6715 ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
6716 ClassDB::bind_method(D_METHOD("set_column_expand_ratio", "column", "ratio"), &Tree::set_column_expand_ratio);
6717 ClassDB::bind_method(D_METHOD("set_column_clip_content", "column", "enable"), &Tree::set_column_clip_content);
6718 ClassDB::bind_method(D_METHOD("is_column_expanding", "column"), &Tree::is_column_expanding);
6719 ClassDB::bind_method(D_METHOD("is_column_clipping_content", "column"), &Tree::is_column_clipping_content);
6720 ClassDB::bind_method(D_METHOD("get_column_expand_ratio", "column"), &Tree::get_column_expand_ratio);
6721
6722 ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
6723
6724 ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
6725 ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
6726 ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::get_next_selected);
6727 ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
6728 ClassDB::bind_method(D_METHOD("set_selected", "item", "column"), &Tree::set_selected);
6729 ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
6730 ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
6731 ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
6732 ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
6733 ClassDB::bind_method(D_METHOD("deselect_all"), &Tree::deselect_all);
6734
6735 ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
6736 ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
6737
6738 ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
6739 ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
6740 ClassDB::bind_method(D_METHOD("edit_selected", "force_edit"), &Tree::edit_selected, DEFVAL(false));
6741 ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
6742 ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column", "button_index"), &Tree::get_item_rect, DEFVAL(-1), DEFVAL(-1));
6743 ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
6744 ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
6745 ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
6746 ClassDB::bind_method(D_METHOD("get_button_id_at_position", "position"), &Tree::get_button_id_at_position);
6747
6748 ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
6749
6750 ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
6751 ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
6752
6753 ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
6754 ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
6755
6756 ClassDB::bind_method(D_METHOD("set_column_title_tooltip_text", "column", "tooltip_text"), &Tree::set_column_title_tooltip_text);
6757 ClassDB::bind_method(D_METHOD("get_column_title_tooltip_text", "column"), &Tree::get_column_title_tooltip_text);
6758
6759 ClassDB::bind_method(D_METHOD("set_column_title_alignment", "column", "title_alignment"), &Tree::set_column_title_alignment);
6760 ClassDB::bind_method(D_METHOD("get_column_title_alignment", "column"), &Tree::get_column_title_alignment);
6761
6762 ClassDB::bind_method(D_METHOD("set_column_title_direction", "column", "direction"), &Tree::set_column_title_direction);
6763 ClassDB::bind_method(D_METHOD("get_column_title_direction", "column"), &Tree::get_column_title_direction);
6764
6765 ClassDB::bind_method(D_METHOD("set_column_title_language", "column", "language"), &Tree::set_column_title_language);
6766 ClassDB::bind_method(D_METHOD("get_column_title_language", "column"), &Tree::get_column_title_language);
6767
6768 ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
6769 ClassDB::bind_method(D_METHOD("scroll_to_item", "item", "center_on_item"), &Tree::scroll_to_item, DEFVAL(false));
6770
6771 ClassDB::bind_method(D_METHOD("set_h_scroll_enabled", "h_scroll"), &Tree::set_h_scroll_enabled);
6772 ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &Tree::is_h_scroll_enabled);
6773
6774 ClassDB::bind_method(D_METHOD("set_v_scroll_enabled", "h_scroll"), &Tree::set_v_scroll_enabled);
6775 ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &Tree::is_v_scroll_enabled);
6776
6777 ClassDB::bind_method(D_METHOD("set_scroll_hint_mode", "scroll_hint_mode"), &Tree::set_scroll_hint_mode);
6778 ClassDB::bind_method(D_METHOD("get_scroll_hint_mode"), &Tree::get_scroll_hint_mode);
6779
6780 ClassDB::bind_method(D_METHOD("set_tile_scroll_hint", "tile_scroll_hint"), &Tree::set_tile_scroll_hint);
6781 ClassDB::bind_method(D_METHOD("is_scroll_hint_tiled"), &Tree::is_scroll_hint_tiled);
6782
6783 ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
6784 ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
6785
6786 ClassDB::bind_method(D_METHOD("set_enable_recursive_folding", "enable"), &Tree::set_enable_recursive_folding);
6787 ClassDB::bind_method(D_METHOD("is_recursive_folding_enabled"), &Tree::is_recursive_folding_enabled);
6788
6789 ClassDB::bind_method(D_METHOD("set_enable_drag_unfolding", "enable"), &Tree::set_enable_drag_unfolding);
6790 ClassDB::bind_method(D_METHOD("is_drag_unfolding_enabled"), &Tree::is_drag_unfolding_enabled);
6791
6792 ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
6793 ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
6794
6795 ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
6796 ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
6797
6798 ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
6799 ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
6800
6801 ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &Tree::set_allow_search);
6802 ClassDB::bind_method(D_METHOD("get_allow_search"), &Tree::get_allow_search);
6803
6804 ClassDB::bind_method(D_METHOD("set_auto_tooltip", "enable"), &Tree::set_auto_tooltip);
6805 ClassDB::bind_method(D_METHOD("is_auto_tooltip_enabled"), &Tree::is_auto_tooltip_enabled);
6806
6807 ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
6808 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "column_titles_visible"), "set_column_titles_visible", "are_column_titles_visible");
6809 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
6810 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
6811 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
6812 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
6813 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_recursive_folding"), "set_enable_recursive_folding", "is_recursive_folding_enabled");
6814 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_drag_unfolding"), "set_enable_drag_unfolding", "is_drag_unfolding_enabled");
6815 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
6816 ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In Between"), "set_drop_mode_flags", "get_drop_mode_flags");
6817 ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
6818 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_tooltip"), "set_auto_tooltip", "is_auto_tooltip_enabled");
6819 ADD_GROUP("Scroll", "");
6820 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_h_scroll_enabled", "is_h_scroll_enabled");
6821 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_v_scroll_enabled", "is_v_scroll_enabled");
6822 ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_hint_mode", PROPERTY_HINT_ENUM, "Disabled,Both,Top,Bottom"), "set_scroll_hint_mode", "get_scroll_hint_mode");
6823 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tile_scroll_hint"), "set_tile_scroll_hint", "is_scroll_hint_tiled");
6824
6825 ADD_SIGNAL(MethodInfo("item_selected"));
6826 ADD_SIGNAL(MethodInfo("cell_selected"));
6827 ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
6828 ADD_SIGNAL(MethodInfo("item_mouse_selected", PropertyInfo(Variant::VECTOR2, "mouse_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
6829 ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "click_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
6830 ADD_SIGNAL(MethodInfo("item_edited"));
6831 ADD_SIGNAL(MethodInfo("custom_item_clicked", PropertyInfo(Variant::INT, "mouse_button_index")));
6832 ADD_SIGNAL(MethodInfo("item_icon_double_clicked"));
6833 ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
6834 ADD_SIGNAL(MethodInfo("check_propagated_to_item", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column")));
6835 ADD_SIGNAL(MethodInfo("button_clicked", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::INT, "mouse_button_index")));
6836 ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
6837 ADD_SIGNAL(MethodInfo("item_activated"));
6838 ADD_SIGNAL(MethodInfo("column_title_clicked", PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "mouse_button_index")));
6839 ADD_SIGNAL(MethodInfo("nothing_selected"));
6840
6841 BIND_ENUM_CONSTANT(SELECT_SINGLE);
6842 BIND_ENUM_CONSTANT(SELECT_ROW);
6843 BIND_ENUM_CONSTANT(SELECT_MULTI);
6844
6845 BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
6846 BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
6847 BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
6848
6849 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_DISABLED);
6850 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_BOTH);
6851 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_TOP);
6852 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_BOTTOM);
6853
6854 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, panel_style, "panel");
6855 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, focus_style, "focus");
6856
6857 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Tree, font);
6858 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Tree, font_size);
6859 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, Tree, tb_font, "title_button_font");
6860 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, Tree, tb_font_size, "title_button_font_size");
6861
6862 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered);
6863 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_dimmed);
6864 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_selected);
6865 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_selected_focus);
6866 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected);
6867 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected_focus);
6868 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor);
6869 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor_unfocus, "cursor_unfocused");
6870 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, button_hover);
6871 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, button_pressed);
6872
6873 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked);
6874 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked);
6875 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked_disabled);
6876 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked_disabled);
6877 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate);
6878 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate_disabled);
6879 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow);
6880 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed);
6881 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed_mirrored);
6882 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, select_arrow);
6883 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, updown);
6884 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, scroll_hint);
6885 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, scroll_hint_color);
6886
6887 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button);
6888 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_hover);
6889 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_pressed);
6890 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, custom_button_font_highlight);
6891
6892 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_color);
6893 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_color);
6894 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_dimmed_color);
6895 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_selected_color);
6896 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_selected_color);
6897 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_disabled_color);
6898 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, drop_position_color);
6899 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, h_separation);
6900 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, v_separation);
6901 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_bottom);
6902 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_left);
6903 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_right);
6904 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_top);
6905 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, item_margin);
6906 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, check_h_separation);
6907 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, icon_h_separation);
6908 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, button_margin);
6909 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, icon_max_width);
6910
6911 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_outline_color);
6912 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Tree, font_outline_size, "outline_size");
6913
6914 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_guides);
6915 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, guide_color);
6916 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_relationship_lines);
6917 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, relationship_line_width);
6918 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_width);
6919 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, children_hl_line_width);
6920 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_margin);
6921 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, relationship_line_color);
6922 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, parent_hl_line_color);
6923 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, children_hl_line_color);
6924
6925 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, dragging_unfold_wait_msec);
6926
6927 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_border);
6928 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_speed);
6929
6930 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_top);
6931 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_right);
6932 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_bottom);
6933 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_left);
6934 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_h_separation);
6935 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_v_separation);
6936
6937 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button, "title_button_normal");
6938 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_pressed);
6939 BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_hover);
6940 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, title_button_color);
6941
6942 ADD_CLASS_DEPENDENCY("HScrollBar");
6943 ADD_CLASS_DEPENDENCY("HSlider");
6944 ADD_CLASS_DEPENDENCY("LineEdit");
6945 ADD_CLASS_DEPENDENCY("Popup");
6946 ADD_CLASS_DEPENDENCY("TextEdit");
6947 ADD_CLASS_DEPENDENCY("Timer");
6948 ADD_CLASS_DEPENDENCY("VScrollBar");
6949 }
6950
6951 Tree::Tree() {
6952 columns.resize(1);
6953
6954 set_focus_mode(FOCUS_ALL);
6955
6956 RenderingServer *rs = RenderingServer::get_singleton();
6957
6958 content_ci = rs->canvas_item_create();
6959 rs->canvas_item_set_parent(content_ci, get_canvas_item());
6960 rs->canvas_item_set_use_parent_material(content_ci, true);
6961
6962 header_ci = rs->canvas_item_create();
6963 rs->canvas_item_set_parent(header_ci, get_canvas_item());
6964 rs->canvas_item_set_use_parent_material(header_ci, true);
6965
6966 popup_editor = memnew(Popup);
6967 add_child(popup_editor, false, INTERNAL_MODE_FRONT);
6968
6969 popup_editor_vb = memnew(VBoxContainer);
6970 popup_editor_vb->add_theme_constant_override("separation", 0);
6971 popup_editor_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
6972 popup_editor->add_child(popup_editor_vb);
6973
6974 line_editor = memnew(LineEdit);
6975 line_editor->set_theme_type_variation("TreeLineEdit");
6976 line_editor->set_v_size_flags(SIZE_EXPAND_FILL);
6977 line_editor->hide();
6978 popup_editor_vb->add_child(line_editor);
6979
6980 text_editor = memnew(TextEdit);
6981 text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
6982 text_editor->hide();
6983 popup_editor_vb->add_child(text_editor);
6984
6985 h_scroll = memnew(HScrollBar);
6986 v_scroll = memnew(VScrollBar);
6987
6988 add_child(h_scroll, false, INTERNAL_MODE_FRONT);
6989 add_child(v_scroll, false, INTERNAL_MODE_FRONT);
6990
6991 range_click_timer = memnew(Timer);
6992 range_click_timer->connect("timeout", callable_mp(this, &Tree::_range_click_timeout));
6993 add_child(range_click_timer, false, INTERNAL_MODE_FRONT);
6994
6995 dropping_unfold_timer = memnew(Timer);
6996 dropping_unfold_timer->set_one_shot(true);
6997 dropping_unfold_timer->connect("timeout", callable_mp(this, &Tree::_on_dropping_unfold_timer_timeout));
6998 add_child(dropping_unfold_timer);
6999
7000 h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Tree::_scroll_moved));
7001 v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Tree::_scroll_moved));
7002 line_editor->connect(SceneStringName(text_submitted), callable_mp(this, &Tree::_line_editor_submit));
7003 text_editor->connect(SceneStringName(gui_input), callable_mp(this, &Tree::_text_editor_gui_input));
7004 popup_editor->connect("popup_hide", callable_mp(this, &Tree::_text_editor_popup_modal_close));
7005
7006 set_notify_transform(true);
7007
7008 set_mouse_filter(MOUSE_FILTER_STOP);
7009
7010 set_clip_contents(true);
7011 }
7012
7013 Tree::~Tree() {
7014 if (root) {
7015 memdelete(root);
7016 }
7017 RenderingServer::get_singleton()->free_rid(content_ci);
7018 RenderingServer::get_singleton()->free_rid(header_ci);
7019 }