Blender V5.3
interface_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cctype>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <fmt/format.h>
15
16#include "DNA_object_types.h"
17#include "DNA_screen_types.h"
18
19#include "ED_screen.hh"
20
21#include "BLI_listbase.hh"
22#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24#include "BLI_utildefines.hh"
25
26#include "BLT_translation.hh"
27
28#include "BKE_context.hh"
29#include "BKE_global.hh"
30#include "BKE_lib_id.hh"
31#include "BKE_screen.hh"
32
33#include "MEM_guardedalloc.h"
34
35#include "RNA_access.hh"
36#include "RNA_prototypes.hh"
37
38#include "ANIM_action.hh"
39
40#include "UI_interface_icons.hh"
42#include "UI_resources.hh"
43#include "UI_string_search.hh"
44#include "UI_view2d.hh"
45
46#include "WM_api.hh"
47#include "WM_types.hh"
48
49#include "interface_intern.hh"
50
51namespace blender::ui {
52
53/* -------------------------------------------------------------------- */
56
58 PointerRNA *ptr,
59 PropertyRNA *prop,
60 int index,
61 const std::optional<StringRef> name,
62 int icon,
63 int x,
64 int y,
65 int width,
66 int height,
67 std::optional<ButtonType> button_type_override)
68{
69 Button *but = nullptr;
70
71 switch (RNA_property_type(prop)) {
72 case PROP_BOOLEAN: {
73 if (RNA_property_array_check(prop) && index == -1) {
74 return nullptr;
75 }
76
77 if (icon && name && name->is_empty()) {
78 but = uiDefIconButR_prop(block,
80 icon,
81 x,
82 y,
83 width,
84 height,
85 ptr,
86 prop,
87 index,
88 0,
89 0,
90 std::nullopt);
91 }
92 else if (icon) {
93 but = uiDefIconTextButR_prop(block,
95 icon,
96 name,
97 x,
98 y,
99 width,
100 height,
101 ptr,
102 prop,
103 index,
104 0,
105 0,
106 std::nullopt);
107 }
108 else {
109 but = uiDefButR_prop(block,
111 name,
112 x,
113 y,
114 width,
115 height,
116 ptr,
117 prop,
118 index,
119 0,
120 0,
121 std::nullopt);
122 }
123 break;
124 }
125 case PROP_INT:
126 case PROP_FLOAT: {
127 if (RNA_property_array_check(prop) && index == -1) {
129 but = uiDefButR_prop(block,
131 name,
132 x,
133 y,
134 width,
135 height,
136 ptr,
137 prop,
138 -1,
139 0,
140 0,
141 std::nullopt);
142 }
143 else {
144 return nullptr;
145 }
146 }
147 else if (RNA_property_subtype(prop) == PROP_PERCENTAGE ||
149 {
150 but = uiDefButR_prop(block,
152 name,
153 x,
154 y,
155 width,
156 height,
157 ptr,
158 prop,
159 index,
160 0,
161 0,
162 std::nullopt);
163 }
164 else {
165 const ButtonType button_type = button_type_override.value_or(ButtonType::Num);
167
168 but = uiDefButR_prop(
169 block, button_type, name, x, y, width, height, ptr, prop, index, 0, 0, std::nullopt);
170 }
171
174 }
175 break;
176 }
177 case PROP_ENUM: {
178 const ButtonType button_type = button_type_override.value_or(ButtonType::Menu);
180 if (icon && name && name->is_empty()) {
181 but = uiDefIconButR_prop(
182 block, button_type, icon, x, y, width, height, ptr, prop, index, 0, 0, std::nullopt);
183 }
184 else if (icon) {
185 but = uiDefIconTextButR_prop(block,
186 button_type,
187 icon,
188 std::nullopt,
189 x,
190 y,
191 width,
192 height,
193 ptr,
194 prop,
195 index,
196 0,
197 0,
198 std::nullopt);
199 }
200 else {
201 but = uiDefButR_prop(
202 block, button_type, name, x, y, width, height, ptr, prop, index, 0, 0, std::nullopt);
203 }
204 break;
205 }
206 case PROP_STRING: {
208 const ButtonType button_type = bool(search_flag) ?
210 button_type_override.value_or(ButtonType::Text);
212 if (icon && name && name->is_empty()) {
213 but = uiDefIconButR_prop(
214 block, button_type, icon, x, y, width, height, ptr, prop, index, 0, 0, std::nullopt);
215 }
216 else if (icon) {
217 but = uiDefIconTextButR_prop(block,
218 button_type,
219 icon,
220 name,
221 x,
222 y,
223 width,
224 height,
225 ptr,
226 prop,
227 index,
228 0,
229 0,
230 std::nullopt);
231 }
232 else {
233 but = uiDefButR_prop(
234 block, button_type, name, x, y, width, height, ptr, prop, index, 0, 0, std::nullopt);
235 }
236 if (search_flag) {
238 ptr,
239 prop,
240 nullptr,
241 nullptr,
242 nullptr,
243 search_flag & PROP_STRING_SEARCH_SUGGESTION);
244 }
245
247 /* TEXTEDIT_UPDATE is usually used for search buttons. For these we also want
248 * the 'x' icon to clear search string, so setting VALUE_CLEAR flag, too. */
250 }
251 break;
252 }
253 case PROP_POINTER: {
254 if (icon == 0) {
255 const PointerRNA pptr = RNA_property_pointer_get(ptr, prop);
256 icon = RNA_struct_ui_icon(pptr.type ? pptr.type : RNA_property_pointer_type(ptr, prop));
257 }
258 if (icon == ICON_DOT) {
259 icon = 0;
260 }
261
262 but = uiDefIconTextButR_prop(block,
264 icon,
265 name,
266 x,
267 y,
268 width,
269 height,
270 ptr,
271 prop,
272 index,
273 0,
274 0,
275 std::nullopt);
276 button_configure_search(but, ptr, prop, nullptr, nullptr, nullptr, false);
277 break;
278 }
279 case PROP_COLLECTION: {
280 char text[256];
281 SNPRINTF_UTF8(text, IFACE_("%d items"), RNA_property_collection_length(ptr, prop));
282 but = uiDefBut(
283 block, ButtonType::Label, text, x, y, width, height, nullptr, 0, 0, std::nullopt);
285 break;
286 }
287 default:
288 but = nullptr;
289 break;
290 }
291
292 return but;
293}
294
296 PointerRNA *ptr,
297 PropertyRNA *prop,
298 const int icon,
299 const int x,
300 const int y,
301 const int tot_width,
302 const int height)
303{
304 const int len = RNA_property_array_length(ptr, prop);
305 if (len == 0) {
306 return;
307 }
308
309 const int item_width = tot_width / len;
310
311 block_align_begin(block);
312 for (int i = 0; i < len; i++) {
313 uiDefAutoButR(block, ptr, prop, i, "", icon, x + i * item_width, y, item_width, height);
314 }
315 block_align_end(block);
316}
317
319 PointerRNA *ptr,
320 bool (*check_prop)(PointerRNA *ptr,
321 PropertyRNA *prop,
322 void *user_data),
323 void *user_data,
324 PropertyRNA *prop_activate_init,
325 const eButLabelAlign label_align,
326 const bool compact)
327{
329 Layout *col;
330 std::optional<StringRefNull> name;
331
332 RNA_STRUCT_BEGIN (ptr, prop) {
333 const int flag = RNA_property_flag(prop);
334
335 if (flag & PROP_HIDDEN) {
336 continue;
337 }
338 if (check_prop && check_prop(ptr, prop, user_data) == 0) {
339 return_info |= PROP_BUTS_ANY_FAILED_CHECK;
340 continue;
341 }
342
343 const PropertyType type = RNA_property_type(prop);
344 switch (label_align) {
347 const bool is_boolean = (type == PROP_BOOLEAN && !RNA_property_array_check(prop));
348
349 name = RNA_property_ui_name(prop);
350
351 if (label_align == BUT_LABEL_ALIGN_COLUMN) {
352 col = &layout->column(true);
353
354 if (!is_boolean) {
355 col->label(*name, ICON_NONE);
356 }
357 }
358 else {
360 col = &layout->column(true);
361 /* Let Layout::prop() create the split layout. */
362 col->use_property_split_set(true);
363 }
364
365 break;
366 }
368 default:
369 col = layout;
370 name = std::nullopt; /* no smart label alignment, show default name with button */
371 break;
372 }
373
374 /* Only buttons that can be edited as text. */
375 const bool use_activate_init = ((prop == prop_activate_init) &&
377
378 if (use_activate_init) {
379 col->activate_init_set(true);
380 }
381
382 col->prop(ptr, prop, -1, 0, compact ? ITEM_R_COMPACT : UI_ITEM_NONE, name, ICON_NONE);
383 return_info &= ~PROP_BUTS_NONE_ADDED;
384
385 if (use_activate_init) {
386 col->activate_init_set(false);
387 }
388 }
390
391 return return_info;
392}
393
398
400
401/* -------------------------------------------------------------------- */
404
406 void *data;
407 std::string name;
408 int index;
411 bool is_id;
413};
414
416 const bool requires_exact_data_name,
417 const bool has_id_icon,
418 SearchItems *items)
419{
420
421 /* If no item has its own icon to display, libraries can use the library icons rather than the
422 * name prefix for showing the library status. */
423 int name_prefix_offset = cis.name_prefix_offset;
424 if (!has_id_icon && cis.is_id && !requires_exact_data_name) {
425 cis.iconid = icon_from_library(static_cast<const ID *>(cis.data));
426 char name_buf[UI_MAX_DRAW_STR];
428 name_buf, static_cast<const ID *>(cis.data), false, UI_SEP_CHAR, &name_prefix_offset);
429 cis.name = name_buf;
430 }
431
432 return search_item_add(items,
433 cis.name,
434 cis.data,
435 cis.iconid,
437 name_prefix_offset);
438}
439
441 const bContext *C, void *arg, const char *str, SearchItems *items, const bool is_first)
442{
443 RNACollectionSearch *data = static_cast<RNACollectionSearch *>(arg);
444 const int flag = RNA_property_flag(data->target_prop);
445 const bool is_ptr_target = (RNA_property_type(data->target_prop) == PROP_POINTER);
446 /* For non-pointer properties, UI code acts entirely based on the item's name. So the name has to
447 * match the RNA name exactly. So only for pointer properties, the name can be modified to add
448 * further UI hints. */
449 const bool requires_exact_data_name = !is_ptr_target;
450 const bool skip_filter = is_first;
451 char name_buf[UI_MAX_DRAW_STR];
452 bool has_id_icon = false;
453
454 /* The string search API requires pointer stability. */
456
457 if (data->search_prop != nullptr) {
458 /* build a temporary list of relevant items first */
459 RNA_PROP_BEGIN (&data->search_ptr, itemptr, data->search_prop) {
460 if (flag & PROP_ID_SELF_CHECK) {
461 if (itemptr.data == data->target_ptr.owner_id) {
462 continue;
463 }
464 }
465
466 /* use filter */
467 if (is_ptr_target) {
468 if (RNA_property_pointer_poll(&data->target_ptr, data->target_prop, &itemptr) == 0) {
469 continue;
470 }
471 }
472
473 int name_prefix_offset = 0;
474 int iconid = ICON_NONE;
475 bool has_sep_char = false;
476 const bool is_id = itemptr.type && RNA_struct_is_ID(itemptr.type);
477
478 char *name;
479 if (is_id) {
480 const ID *id = static_cast<ID *>(itemptr.data);
481
482 /* Hide dot prefixed data-blocks, but only if filter does not force them visible. */
483 if (U.flag & USER_HIDE_DOT_DATABLOCK) {
484 if ((id->name[2] == '.') && (str[0] != '.')) {
485 continue;
486 }
487 }
488
489 iconid = id_icon_get(C, static_cast<ID *>(itemptr.data), false);
490 if (!ELEM(iconid, 0, ICON_BLANK1)) {
491 has_id_icon = true;
492 }
493
494 if (requires_exact_data_name) {
495 name = RNA_struct_name_get_alloc(&itemptr, name_buf, sizeof(name_buf), nullptr);
496 }
497 else {
498 BKE_id_full_name_ui_prefix_get(name_buf, id, true, UI_SEP_CHAR, &name_prefix_offset);
499 BLI_STATIC_ASSERT(sizeof(name_buf) >= MAX_ID_FULL_NAME_UI,
500 "Name string buffer should be big enough to hold full UI ID name");
501 name = name_buf;
502 has_sep_char = ID_IS_LINKED(id);
503 }
504 }
505 else if (data->item_search_prop) {
507 &itemptr, data->item_search_prop, name_buf, sizeof(name_buf), nullptr);
508 }
509 else if (itemptr.type == RNA_ActionSlot) {
510 /* FIXME: This special case is fairly annoying.
511 *
512 * `item_search_prop` now allows to specify another string property than the default RNA
513 * struct name one as source, but icons are still an issue. RNA access API for icons likely
514 * needs some love, to allow callbacks, data-based icons retrieval, in addition to the
515 * purely static options currently available (see #RNA_struct_ui_icon and
516 * #RNA_property_ui_icon).
517 */
518 PropertyRNA *prop = RNA_struct_find_property(&itemptr, "name_display");
519 name = RNA_property_string_get_alloc(&itemptr, prop, name_buf, sizeof(name_buf), nullptr);
520 /* Also show an icon for the data-block type that each slot is intended for. */
521 animrig::Slot &slot = reinterpret_cast<ActionSlot *>(itemptr.data)->wrap();
522 iconid = icon_from_idcode(slot.idtype);
523 /* So indentation is kept when no icon is present. */
524 if (iconid == ICON_NONE) {
525 iconid = ICON_BLANK1;
526 }
527 }
528 else {
529 name = RNA_struct_name_get_alloc(&itemptr, name_buf, sizeof(name_buf), nullptr);
530 }
531
532 if (name) {
533 auto cis = std::make_unique<CollItemSearch>();
534 cis->data = itemptr.data;
535 cis->name = name;
536 cis->index = items_list.size();
537 cis->iconid = iconid;
538 cis->is_id = is_id;
539 cis->name_prefix_offset = name_prefix_offset;
540 cis->has_sep_char = has_sep_char;
541 items_list.append(std::move(cis));
542 if (name != name_buf) {
543 MEM_delete(name);
544 }
545 }
546 }
548
549 /* Sort alphabetically (matches other search layouts). */
550 std::ranges::sort(
551 items_list,
552 [](const std::unique_ptr<CollItemSearch> &a, const std::unique_ptr<CollItemSearch> &b) {
553 return BLI_strcasecmp_natural(a->name.c_str(), b->name.c_str()) < 0;
554 });
555 for (const int i : items_list.index_range()) {
556 items_list[i]->index = i;
557 }
558 }
559 else {
562 data->target_prop);
564
565 const bool show_extra_info = (G.debug_value == 102);
566
568 &data->target_ptr,
569 data->target_prop,
570 str,
571 [&](StringPropertySearchVisitParams visit_params) {
572 auto cis = std::make_unique<CollItemSearch>();
573
574 cis->data = nullptr;
575 if (visit_params.info && show_extra_info) {
576 cis->name = fmt::format("{}" UI_SEP_CHAR_S "{}",
577 visit_params.text,
578 *visit_params.info);
579 }
580 else {
581 cis->name = std::move(visit_params.text);
582 }
583
584 cis->index = items_list.size();
585 cis->iconid = visit_params.icon_id.value_or(ICON_NONE);
586 cis->is_id = false;
587 cis->name_prefix_offset = 0;
588 cis->has_sep_char = visit_params.info.has_value();
589 items_list.append(std::move(cis));
590 });
591
592 if (search_flag & PROP_STRING_SEARCH_SORT) {
593 std::ranges::sort(
594 items_list,
595 [](const std::unique_ptr<CollItemSearch> &a, const std::unique_ptr<CollItemSearch> &b) {
596 return BLI_strcasecmp_natural(a->name.c_str(), b->name.c_str()) < 0;
597 });
598 for (const int i : items_list.index_range()) {
599 items_list[i]->index = i;
600 }
601 }
602 }
603
604 if (skip_filter) {
605 for (std::unique_ptr<CollItemSearch> &cis : items_list) {
606 if (!add_collection_search_item(*cis, requires_exact_data_name, has_id_icon, items)) {
607 break;
608 }
609 }
610 }
611 else {
613 for (std::unique_ptr<CollItemSearch> &cis : items_list) {
614 search.add(cis->name, cis.get());
615 }
616
617 const Vector<CollItemSearch *> filtered_items = search.query(str);
618 for (CollItemSearch *cis : filtered_items) {
619 if (!add_collection_search_item(*cis, requires_exact_data_name, has_id_icon, items)) {
620 break;
621 }
622 }
623 }
624}
625
627
628/* -------------------------------------------------------------------- */
631
632int icon_from_id(const ID *id)
633{
634 if (id == nullptr) {
635 return ICON_NONE;
636 }
637
638 /* exception for objects */
639 if (GS(id->name) == ID_OB) {
640 Object *ob = id_cast<Object *>(const_cast<ID *>(id));
641
642 if (ob->type == OB_EMPTY) {
643 return ICON_EMPTY_DATA;
644 }
645 return icon_from_id(static_cast<const ID *>(ob->data));
646 }
647
648 /* otherwise get it through RNA, creating the pointer
649 * will set the right type, also with subclassing */
650 PointerRNA ptr = RNA_id_pointer_create(const_cast<ID *>(id));
651
652 return (ptr.type) ? RNA_struct_ui_icon(ptr.type) : ICON_NONE;
653}
654
656{
657 if (type & RPT_ERROR_ALL) {
658 return ICON_STATUS_ERROR_FILLED;
659 }
660 if (type & RPT_WARNING_ALL) {
661 return ICON_STATUS_WARNING_FILLED;
662 }
663 if (type & RPT_INFO_ALL) {
664 return ICON_STATUS_INFO_FILLED;
665 }
666 if (type & RPT_DEBUG_ALL) {
667 return ICON_SYSTEM;
668 }
669 if (type & RPT_PROPERTY) {
670 return ICON_OPTIONS;
671 }
672 if (type & RPT_OPERATOR) {
673 return ICON_CHECKMARK;
674 }
675 return ICON_STATUS_INFO_FILLED;
676}
677
679{
680 if (type & RPT_ERROR_ALL) {
681 return TH_ERROR;
682 }
683 if (type & RPT_WARNING_ALL) {
684 return TH_WARNING;
685 }
686 if (type & RPT_INFO_ALL) {
687 return TH_INFO;
688 }
689 if (type & RPT_DEBUG_ALL) {
690 return TH_INFO_DEBUG;
691 }
692 if (type & RPT_PROPERTY) {
693 return TH_INFO_PROPERTY;
694 }
695 if (type & RPT_OPERATOR) {
696 return TH_INFO_OPERATOR;
697 }
698 return TH_WARNING;
699}
700
702{
703 if (type & RPT_ERROR_ALL) {
704 return TH_INFO_ERROR_TEXT;
705 }
706 if (type & RPT_WARNING_ALL) {
708 }
709 if (type & RPT_INFO_ALL) {
710 return TH_INFO_INFO_TEXT;
711 }
712 if (type & RPT_DEBUG_ALL) {
713 return TH_INFO_DEBUG_TEXT;
714 }
715 if (type & RPT_PROPERTY) {
717 }
718 if (type & RPT_OPERATOR) {
720 }
722}
723
725
726/* -------------------------------------------------------------------- */
729
730int calc_float_precision(int prec, double value)
731{
732 static const double pow10_neg[UI_PRECISION_FLOAT_MAX + 1] = {
733 1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6};
734 static const double max_pow = 10000000.0; /* pow(10, UI_PRECISION_FLOAT_MAX) */
735
737 BLI_assert(fabs(pow10_neg[prec] - pow(10, -prec)) < 1e-16);
738
739 /* Check on the number of decimal places need to display the number,
740 * this is so 0.00001 is not displayed as 0.00,
741 * _but_, this is only for small values as 10.0001 will not get the same treatment.
742 */
743 value = fabs(value);
744 if ((value < pow10_neg[prec]) && (value > (1.0 / max_pow))) {
745 int value_i = int(lround(value * max_pow));
746 if (value_i != 0) {
747 const int prec_span = 3; /* show: 0.01001, 5 would allow 0.0100001 for eg. */
748 int test_prec;
749 int prec_min = -1;
750 int dec_flag = 0;
752 while (i && value_i) {
753 if (value_i % 10) {
754 dec_flag |= 1 << i;
755 prec_min = i;
756 }
757 value_i /= 10;
758 i--;
759 }
760
761 /* even though its a small value, if the second last digit is not 0, use it */
762 test_prec = prec_min;
763
764 dec_flag = (dec_flag >> (prec_min + 1)) & ((1 << prec_span) - 1);
765
766 while (dec_flag) {
767 test_prec++;
768 dec_flag = dec_flag >> 1;
769 }
770
771 prec = std::max(test_prec, prec);
772 }
773 }
774
776
777 return prec;
778}
779
780std::optional<std::string> button_online_manual_id(const Button *but)
781{
782 if (but->rnapoin && but->rnaprop) {
783 return fmt::format(
785 }
786 if (but->optype) {
787 char idname[OP_MAX_TYPENAME];
788 const size_t idname_len = WM_operator_py_idname(idname, but->optype->idname);
789 return std::string(idname, idname_len);
790 }
791
792 return std::nullopt;
793}
794
795std::optional<std::string> button_online_manual_id_from_active(const bContext *C)
796{
798 return button_online_manual_id(but);
799 }
800 return std::nullopt;
801}
802
804
805/* -------------------------------------------------------------------- */
808static rctf but_rect_to_view(const Button *but, const ARegion *region, const View2D *v2d)
809{
810 rctf region_rect;
811 block_to_region_rctf(region, but->block, &region_rect, &but->rect);
812
813 rctf view_rect;
814 view2d_region_to_view_rctf(v2d, &region_rect, &view_rect);
815
816 return view_rect;
817}
818
826static bool view2d_cur_ensure_rect_in_view(View2D *v2d, const rctf *rect)
827{
828 const float rect_width = BLI_rctf_size_x(rect);
829 const float rect_height = BLI_rctf_size_y(rect);
830
831 rctf *cur = &v2d->cur;
832 const float cur_width = BLI_rctf_size_x(cur);
833 const float cur_height = BLI_rctf_size_y(cur);
834
835 bool changed = false;
836
837 /* Snap to bottom edge. Also use if rect is higher than view bounds (could be a parameter). */
838 if ((cur->ymin > rect->ymin) || (rect_height > cur_height)) {
839 cur->ymin = rect->ymin;
840 cur->ymax = cur->ymin + cur_height;
841 changed = true;
842 }
843 /* Snap to upper edge. */
844 else if (cur->ymax < rect->ymax) {
845 cur->ymax = rect->ymax;
846 cur->ymin = cur->ymax - cur_height;
847 changed = true;
848 }
849 /* Snap to left edge. Also use if rect is wider than view bounds. */
850 else if ((cur->xmin > rect->xmin) || (rect_width > cur_width)) {
851 cur->xmin = rect->xmin;
852 cur->xmax = cur->xmin + cur_width;
853 changed = true;
854 }
855 /* Snap to right edge. */
856 else if (cur->xmax < rect->xmax) {
857 cur->xmax = rect->xmax;
858 cur->xmin = cur->xmax - cur_width;
859 changed = true;
860 }
861 else {
863 }
864
865 return changed;
866}
867
868void but_ensure_in_view(const bContext *C, ARegion *region, const Button *but)
869{
870 View2D *v2d = &region->v2d;
871 /* Uninitialized view or region that doesn't use View2D. */
872 if ((v2d->flag & V2D_IS_INIT) == 0) {
873 return;
874 }
875
876 rctf rect = but_rect_to_view(but, region, v2d);
877
878 const int margin = UI_UNIT_X * 0.5f;
879 BLI_rctf_pad(&rect, margin, margin);
880
881 const bool changed = view2d_cur_ensure_rect_in_view(v2d, &rect);
882 if (changed) {
885 }
886}
887
889
890/* -------------------------------------------------------------------- */
901
902struct ButStoreElem;
903
904struct ButStore {
907 ListBaseT<ButStoreElem> items;
908};
909
914
916{
917 ButStore *bs_handle = MEM_new_zeroed<ButStore>(__func__);
918
919 bs_handle->block = block;
920 BLI_addtail(&block->butstore, bs_handle);
921
922 return bs_handle;
923}
924
925void butstore_free(Block *block, ButStore *bs_handle)
926{
927 /* NOTE(@ideasman42): Workaround for button store being moved into new block,
928 * which then can't use the previous buttons state
929 * (#but_update_from_old_block fails to find a match),
930 * keeping the active button in the old block holding a reference
931 * to the button-state in the new block: see #49034.
932 *
933 * Ideally we would manage moving the 'ButStore', keeping a correct state.
934 * All things considered this is the most straightforward fix. */
935 if (block != bs_handle->block && bs_handle->block != nullptr) {
936 block = bs_handle->block;
937 }
938
939 bs_handle->items.free_no_destruct();
940 BLI_assert(BLI_findindex(&block->butstore, bs_handle) != -1);
941 BLI_remlink(&block->butstore, bs_handle);
942
943 MEM_delete(bs_handle);
944}
945
947{
948 return (bs_handle->block != nullptr);
949}
950
952{
953 for (ButStore &bs_handle : block->butstore) {
954 for (ButStoreElem &bs_elem : bs_handle.items) {
955 if (*bs_elem.but_p == but) {
956 return true;
957 }
958 }
959 }
960
961 return false;
962}
963
964void butstore_register(ButStore *bs_handle, Button **but_p)
965{
966 ButStoreElem *bs_elem = MEM_new_zeroed<ButStoreElem>(__func__);
967 BLI_assert(*but_p);
968 bs_elem->but_p = but_p;
969
970 BLI_addtail(&bs_handle->items, bs_elem);
971}
972
973void butstore_unregister(ButStore *bs_handle, Button **but_p)
974{
975 for (ButStoreElem &bs_elem : bs_handle->items.items_mutable()) {
976 if (bs_elem.but_p == but_p) {
977 BLI_remlink(&bs_handle->items, &bs_elem);
978 MEM_delete(&bs_elem);
979 }
980 }
981
982 BLI_assert(0);
983}
984
985bool butstore_register_update(Block *block, Button *but_dst, const Button *but_src)
986{
987 bool found = false;
988
989 for (ButStore &bs_handle : block->butstore) {
990 for (ButStoreElem &bs_elem : bs_handle.items) {
991 if (*bs_elem.but_p == but_src) {
992 *bs_elem.but_p = but_dst;
993 found = true;
994 }
995 }
996 }
997
998 return found;
999}
1000
1002{
1003 for (ButStore &bs_handle : block->butstore) {
1004 bs_handle.block = nullptr;
1005 for (ButStoreElem &bs_elem : bs_handle.items) {
1006 *bs_elem.but_p = nullptr;
1007 }
1008 }
1009}
1010
1012{
1013 /* move this list to the new block */
1014 if (block->oldblock) {
1015 if (block->oldblock->butstore.first_) {
1016 BLI_movelisttolist(&block->butstore, &block->oldblock->butstore);
1017 }
1018 }
1019
1020 if (block->butstore.first_ == nullptr) [[likely]] {
1021 return;
1022 }
1023
1024 /* warning, loop-in-loop, in practice we only store <10 buttons at a time,
1025 * so this isn't going to be a problem, if that changes old-new mapping can be cached first */
1026 for (ButStore &bs_handle : block->butstore) {
1027 BLI_assert(ELEM(bs_handle.block, nullptr, block) ||
1028 (block->oldblock && block->oldblock == bs_handle.block));
1029
1030 if (bs_handle.block == block->oldblock) {
1031 bs_handle.block = block;
1032
1033 for (ButStoreElem &bs_elem : bs_handle.items) {
1034 if (*bs_elem.but_p) {
1035 Button *but_new = button_find_new(block, *bs_elem.but_p);
1036
1037 /* can be nullptr if the buttons removed,
1038 * NOTE: we could allow passing in a callback when buttons are removed
1039 * so the caller can cleanup */
1040 *bs_elem.but_p = but_new;
1041 }
1042 }
1043 }
1044 }
1045}
1046
1048
1049} // namespace blender::ui
Functions and classes to work with Actions.
#define MAX_ID_FULL_NAME_UI
#define RPT_ERROR_ALL
Definition BKE_report.hh:53
#define RPT_INFO_ALL
Definition BKE_report.hh:49
#define RPT_WARNING_ALL
Definition BKE_report.hh:52
#define RPT_DEBUG_ALL
Definition BKE_report.hh:48
#define SNPRINTF_UTF8(dst, format,...)
#define IFACE_(msgid)
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
Object is a sort of wrapper for general info.
#define OP_MAX_TYPENAME
Read Blender memory allocation and freeing.
#define RNA_PROP_END
#define RNA_STRUCT_BEGIN(sptr, prop)
#define RNA_STRUCT_END
#define RNA_PROP_BEGIN(sptr, itemptr, prop)
#define C
Definition RandGen.cpp:29
#define UI_SEP_CHAR
#define UI_PRECISION_FLOAT_MAX
#define UI_UNIT_X
#define UI_ITEM_NONE
#define UI_MAX_DRAW_STR
#define CLAMP(a, b, c)
#define ELEM(...)
void append(const T &value)
IndexRange index_range() const
#define str(s)
#define pow
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
BLI_INLINE Texture * wrap(MTLTexture *texture)
void butstore_free(Block *block, ButStore *bs_handle)
void butstore_clear(Block *block)
int icon_from_report_type(int type)
void butstore_update(Block *block)
BLI_STATIC_ASSERT(int(TIP_LC_MAX)==int(TIP_LC_ALERT)+1, "invalid lc-max")
int icon_colorid_from_report_type(int type)
std::optional< std::string > button_online_manual_id_from_active(const bContext *C) ATTR_WARN_UNUSED_RESULT
void button_configure_search(Button *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchprop, bool results_are_suggestions)
void block_to_region_rctf(const ARegion *region, const Block *block, rctf *rct_dst, const rctf *rct_src)
Definition interface.cc:162
std::optional< std::string > button_online_manual_id(const Button *but) ATTR_WARN_UNUSED_RESULT
Button * uiDefAutoButR(Block *block, PointerRNA *ptr, PropertyRNA *prop, int index, std::optional< StringRef > name, int icon, int x, int y, int width, int height, std::optional< ButtonType > button_type_override=std::nullopt)
void butstore_register(ButStore *bs_handle, Button **but_p)
void butstore_unregister(ButStore *bs_handle, Button **but_p)
Button * context_active_but_get_respect_popup(const bContext *C)
int calc_float_precision(int prec, double value)
bool(*)(const Button *a, const Button *b) ButtonIdentityCompareFunc
void block_align_begin(Block *block)
void button_func_identity_compare_set(Button *but, ButtonIdentityCompareFunc cmp_fn)
int icon_from_idcode(int idcode)
bool butstore_register_update(Block *block, Button *but_dst, const Button *but_src)
int icon_from_id(const ID *id)
int id_icon_get(const bContext *C, ID *id, const bool big)
AutoPropButsReturn uiDefAutoButsRNA(Layout *layout, PointerRNA *ptr, bool(*check_prop)(PointerRNA *ptr, PropertyRNA *prop, void *user_data), void *user_data, PropertyRNA *prop_activate_init, eButLabelAlign label_align, bool compact)
Button * uiDefIconButR_prop(Block *block, ButtonType type, int icon, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< StringRef > tip)
void uiDefAutoButsArrayR(Block *block, PointerRNA *ptr, PropertyRNA *prop, const int icon, const int x, const int y, const int tot_width, const int height)
bool butstore_is_registered(Block *block, Button *but)
void button_flag_enable(Button *but, int64_t flag)
static std::unique_ptr< Button > but_new(const ButtonType type)
static bool add_collection_search_item(CollItemSearch &cis, const bool requires_exact_data_name, const bool has_id_icon, SearchItems *items)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
bool butstore_is_valid(ButStore *bs_handle)
Button * uiDefButR_prop(Block *block, ButtonType type, std::optional< StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< StringRef > tip)
void block_align_end(Block *block)
void but_ensure_in_view(const bContext *C, ARegion *region, const Button *but)
Button * button_find_new(Block *block_new, const Button *but_old)
Definition interface.cc:922
bool search_item_add(SearchItems *items, StringRef name, void *poin, int iconid, int64_t but_flag, uint8_t name_prefix_offset)
Button * uiDefIconTextButR_prop(Block *block, ButtonType type, int icon, std::optional< StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< StringRef > tip)
void rna_collection_search_update_fn(const bContext *C, void *arg, const char *str, SearchItems *items, bool is_first)
void view2d_curRect_changed(const bContext *C, View2D *v2d)
Definition view2d.cc:815
void view2d_region_to_view_rctf(const View2D *v2d, const rctf *rect_src, rctf *rect_dst) ATTR_NONNULL()
Definition view2d.cc:1657
int icon_from_library(const ID *id)
static bool view2d_cur_ensure_rect_in_view(View2D *v2d, const rctf *rect)
int UI_text_colorid_from_report_type(int type)
ButStore * butstore_create(Block *block)
static rctf but_rect_to_view(const Button *but, const ARegion *region, const View2D *v2d)
char * RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len)
UserDef U
Definition blender.cc:58
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
__int64 int64_t
Definition BKE_text.h:90
@ RPT_OPERATOR
Definition BKE_report.hh:38
@ RPT_PROPERTY
Definition BKE_report.hh:39
int char char int int int BLI_strcasecmp_natural(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
PointerRNA RNA_id_pointer_create(ID *id)
const char * RNA_property_identifier(const PropertyRNA *prop)
void void void BLI_movelisttolist(ListBase *dst, ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:32
char * RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len)
@ TH_INFO_ERROR_TEXT
@ TH_INFO_OPERATOR_TEXT
@ TH_INFO_PROPERTY_TEXT
@ TH_INFO_WARNING_TEXT
@ TH_INFO_INFO_TEXT
@ TH_INFO_DEBUG_TEXT
void RNA_property_string_search(const bContext *C, PointerRNA *ptr, PropertyRNA *prop, const char *edit_text, FunctionRef< void(StringPropertySearchVisitParams)> visit_fn)
int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_array_check(PropertyRNA *prop)
Global G
Definition blender.cc:57
PropertyType RNA_property_type(PropertyRNA *prop)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void BKE_id_full_name_ui_prefix_get(char name[MAX_ID_FULL_NAME_UI], const ID *id, bool add_lib_hint, char separator_char, int *r_prefix_len)
Definition lib_id.cc:2708
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
eStringPropertySearchFlag
Definition RNA_types.hh:869
@ PROP_STRING_SEARCH_SUPPORTED
Definition RNA_types.hh:874
@ PROP_STRING_SEARCH_SORT
Definition RNA_types.hh:876
@ PROP_STRING_SEARCH_SUGGESTION
Definition RNA_types.hh:883
@ PROP_BOOLEAN
Definition RNA_types.hh:210
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_STRING
Definition RNA_types.hh:213
@ PROP_COLLECTION
Definition RNA_types.hh:216
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BMesh const char void * data
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
int RNA_property_flag(PropertyRNA *prop)
bool BLI_rctf_inside_rctf(const rctf *rct_a, const rctf *rct_b)
Definition rct.cc:195
int RNA_struct_ui_icon(const StructRNA *type)
bool RNA_struct_is_ID(const StructRNA *type)
const char * RNA_struct_identifier(const StructRNA *type)
PropertySubType RNA_property_subtype(PropertyRNA *prop)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
@ USER_HIDE_DOT_DATABLOCK
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ PROP_TEXTEDIT_UPDATE
Definition RNA_types.hh:396
@ PROP_ID_SELF_CHECK
Definition RNA_types.hh:440
@ PROP_HIDDEN
Definition RNA_types.hh:408
@ PROP_PERCENTAGE
Definition RNA_types.hh:307
@ PROP_FACTOR
Definition RNA_types.hh:308
@ PROP_COLOR_GAMMA
Definition RNA_types.hh:329
eStringPropertySearchFlag RNA_property_string_search_flag(PropertyRNA *prop)
size_t WM_operator_py_idname(char *dst, const char *src)
void ED_region_tag_redraw_no_rebuild(ARegion *region)
Definition area.cc:727
bool RNA_property_pointer_poll(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *value)
void BLI_rctf_pad(struct rctf *rect, float pad_x, float pad_y)
Definition rct.cc:651
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
StructRNA * type
Definition RNA_types.hh:63
std::optional< std::string > info
Definition RNA_types.hh:864
ListBaseT< ButStore > butstore
ListBaseT< ButStoreElem > items
ButtonIdentityCompareFunc identity_cmp_func
wmOperatorType * optype
void use_property_split_set(bool is_sep)
void label(StringRef name, int icon)
void activate_init_set(bool activate_init)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)