Blender V5.3
interface_template_list.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11
12#include "BLI_fnmatch.hh"
13#include "BLI_function_ref.hh"
14#include "BLI_listbase.hh"
15#include "BLI_math_base_c.hh"
16#include "BLI_string.hh"
17#include "BLI_string_ref.hh"
18#include "BLI_string_utf8.hh"
19#include "BLI_utildefines.hh"
20#include "BLI_vector.hh"
21
22#include "BKE_screen.hh"
23
24#include "BLT_translation.hh"
25
26#include "ED_screen.hh"
27
28#include "MEM_guardedalloc.h"
29
30#include "RNA_access.hh"
31#include "RNA_prototypes.hh"
32
34#include "UI_view2d.hh"
35
36#include "WM_api.hh"
37
38#include "interface_intern.hh"
39
40namespace blender::ui {
41
52
53 /* Index as stored in the input property. I.e. the index before sorting. */
55};
56
65
71 /* Index of the active item following visual order. I.e. unlike
72 * TemplateListInputData.active_item_idx, this is the index after sorting. */
74};
75
83
85 int visual_items; /* Visual number of items (i.e. number of items we have room to display). */
86 int start_idx; /* Index of first item to display. */
87 int end_idx; /* Index of last item to display + 1. */
88};
89
90static void uilist_draw_item_default(uiList *ui_list,
91 const bContext * /*C*/,
92 Layout &layout,
93 PointerRNA * /*dataptr*/,
94 PointerRNA *itemptr,
95 int icon,
96 PointerRNA * /*active_dataptr*/,
97 const char * /*active_propname*/,
98 int /*index*/,
99 int /*flt_flag*/)
100{
101 PropertyRNA *nameprop = RNA_struct_name_property(itemptr->type);
102
103 /* Simplest one! */
104 switch (ui_list->layout_type) {
107 default:
108 if (nameprop) {
109 layout.prop(itemptr, nameprop, RNA_NO_INDEX, 0, ITEM_R_NO_BG, "", icon);
110 }
111 else {
112 layout.label("", icon);
113 }
114 break;
115 }
116}
117
118static void uilist_draw_filter_default(uiList *ui_list, const bContext * /*C*/, Layout &layout)
119{
120 PointerRNA listptr = RNA_pointer_create_discrete(nullptr, RNA_UIList, ui_list);
121
122 Layout &row = layout.row(false);
123
124 Layout *subrow = &row.row(true);
125 subrow->prop(&listptr,
126 RNA_struct_find_property(&listptr, "filter_name"),
127 -1,
128 0,
130 "",
131 ICON_VIEWZOOM,
132 IFACE_("Search"));
133 subrow->prop(
134 &listptr, "use_filter_invert", ITEM_R_TOGGLE | ITEM_R_ICON_ONLY, "", ICON_ARROW_LEFTRIGHT);
135
136 if ((ui_list->filter_sort_flag & UILST_FLT_SORT_LOCK) == 0) {
137 subrow = &row.row(true);
138 subrow->prop(
139 &listptr, "use_filter_sort_alpha", ITEM_R_TOGGLE | ITEM_R_ICON_ONLY, "", ICON_NONE);
140 subrow->prop(&listptr,
141 "use_filter_sort_reverse",
143 "",
144 (ui_list->filter_sort_flag & UILST_FLT_SORT_REVERSE) ? ICON_SORT_DESC :
145 ICON_SORT_ASC);
146 }
147}
148
150{
151 const char *filter_raw = list.filter_byname;
152
153 if (filter_raw[0]) {
154 const size_t slen = strlen(filter_raw);
155
156 /* Implicitly add heading/trailing wildcards if needed. */
157 if (slen + 3 <= sizeof(storage_.filter_buff)) {
158 filter_ = storage_.filter_buff;
159 }
160 else {
161 filter_ = storage_.filter_dyn = MEM_new_array_uninitialized<char>((slen + 3), "filter_dyn");
162 }
163 BLI_strncpy_ensure_pad(filter_, filter_raw, '*', slen + 3);
164 }
165}
166
168{
169 MEM_SAFE_DELETE(storage_.filter_dyn);
170}
171
173 StringRefNull name,
174 int /*index*/)
175{
176 if (!filter_) {
178 }
179
180 /* Use `fnmatch` for shell-style globing.
181 * - Case-insensitive.
182 * - Don't handle escape characters as "special" characters are not expected in names.
183 * Unlike shell input - `\` should be treated like any other character.
184 */
185 const int fn_flag = FNM_CASEFOLD | FNM_NOESCAPE;
186 if (fnmatch(filter_, name.c_str(), fn_flag) == 0) {
188 }
190}
191
196
197static int cmpstringp(const void *p1, const void *p2)
198{
199 /* Case-insensitive comparison. */
200 return BLI_strcasecmp(static_cast<const StringCmp *>(p1)->name,
201 static_cast<const StringCmp *>(p2)->name);
202}
203
205 const bContext * /*C*/,
206 uiListItemFilterFn item_filter_fn,
207 PointerRNA *dataptr,
208 const char *propname,
209 uiListItemGetNameFn get_name_fn)
210{
211 uiListDyn *dyn_data = ui_list->dyn_data;
212 PropertyRNA *prop = RNA_struct_find_property(dataptr, propname);
213
214 const bool filter_exclude = (ui_list->filter_flag & UILST_FLT_EXCLUDE) != 0;
215 const bool order_by_name = (ui_list->filter_sort_flag & UILST_FLT_SORT_ALPHA) &&
217 const int len = RNA_property_collection_length(dataptr, prop);
218
219 dyn_data->items_shown = dyn_data->items_len = len;
220
221 if (len && (order_by_name || item_filter_fn)) {
222 StringCmp *names = nullptr;
223 int order_idx = 0, i = 0;
224
225 if (order_by_name) {
226 names = MEM_new_array_zeroed<StringCmp>(len, "StringCmp");
227 }
228
229 if (item_filter_fn) {
230 dyn_data->items_filter_flags = MEM_new_array_zeroed<int>(len, "items_filter_flags");
231 dyn_data->items_shown = 0;
232 }
233
234 RNA_PROP_BEGIN (dataptr, itemptr, prop) {
235 bool do_order = false;
236
237 char *namebuf;
238 if (get_name_fn) {
239 namebuf = BLI_strdup(get_name_fn(itemptr, i).c_str());
240 }
241 else {
242 namebuf = RNA_struct_name_get_alloc(&itemptr, nullptr, 0, nullptr);
243 }
244
245 const char *name = namebuf ? namebuf : "";
246
247 if (item_filter_fn) {
248 const eUIListFilterResult filter_result = item_filter_fn(itemptr, name, i);
249
250 if (filter_result == UI_LIST_ITEM_NEVER_SHOW) {
252 }
253 else if (filter_result == UI_LIST_ITEM_FILTER_MATCHES) {
254 if (!filter_exclude) {
255 dyn_data->items_filter_flags[i] = UILST_FLT_ITEM;
256 dyn_data->items_shown++;
257 do_order = order_by_name;
258 }
259 // printf("%s: '%s' matches '%s'\n", __func__, name, filter);
260 }
261 else if (filter_exclude) {
262 dyn_data->items_filter_flags[i] = UILST_FLT_ITEM;
263 dyn_data->items_shown++;
264 do_order = order_by_name;
265 }
266 }
267 else {
268 do_order = order_by_name;
269 }
270
271 if (do_order) {
272 names[order_idx].org_idx = order_idx;
273 STRNCPY(names[order_idx++].name, name);
274 }
275
276 /* free name */
277 if (namebuf) {
278 MEM_delete(namebuf);
279 }
280 i++;
281 }
283
284 if (order_by_name) {
285 int new_idx;
286 /* NOTE: order_idx equals either to ui_list->items_len if no filtering done,
287 * or to ui_list->items_shown if filter is enabled,
288 * or to (ui_list->items_len - ui_list->items_shown) if filtered items are excluded.
289 * This way, we only sort items we actually intend to draw!
290 */
291 qsort(names, order_idx, sizeof(StringCmp), cmpstringp);
292
294 "items_filter_neworder");
295 for (new_idx = 0; new_idx < order_idx; new_idx++) {
296 dyn_data->items_filter_neworder[names[new_idx].org_idx] = new_idx;
297 }
298 }
299
300 if (names) {
301 MEM_delete(names);
302 }
303 }
304}
305
306bool uilist_item_index_is_filtered_visible(const uiList *ui_list, const int item_idx)
307{
308 const uiListDyn *dyn_data = ui_list->dyn_data;
309
310 if (!dyn_data->items_filter_flags) {
311 /* If there are no filter flags to check, always consider all items visible. */
312 return true;
313 }
314
315 if (dyn_data->items_filter_flags[item_idx] & UILST_FLT_ITEM_NEVER_SHOW) {
316 return false;
317 }
318
319 return (dyn_data->items_filter_flags[item_idx] & UILST_FLT_ITEM);
320}
321
326 const bContext *C,
327 PointerRNA *dataptr,
328 const char *propname)
329{
330 if (ui_list->filter_byname[0]) {
331 uiListNameFilter name_filter(*ui_list);
332 uilist_filter_and_sort_items(ui_list, C, name_filter, dataptr, propname);
333 }
334 /* Optimization: Skip filtering entirely when there is no filter string set. */
335 else {
336 uilist_filter_and_sort_items(ui_list, C, nullptr, dataptr, propname);
337 }
338}
339
340static void uilist_free_dyn_data(uiList *ui_list)
341{
342 uiListDyn *dyn_data = ui_list->dyn_data;
343 if (!dyn_data) {
344 return;
345 }
346
347 MEM_SAFE_DELETE(dyn_data->items_filter_flags);
348 MEM_SAFE_DELETE(dyn_data->items_filter_neworder);
349}
350
357static bool template_uilist_data_retrieve(const StringRef listtype_name,
358 const char *list_id,
359 PointerRNA *dataptr,
360 const StringRefNull propname,
361 PointerRNA *active_dataptr,
362 const StringRefNull active_propname,
363 const char *item_dyntip_propname,
364 TemplateListInputData *r_input_data,
365 uiListType **r_list_type)
366{
367 *r_input_data = {};
368
369 /* Forbid default UI_UL_DEFAULT_CLASS_NAME list class without a custom list_id! */
370 if ((UI_UL_DEFAULT_CLASS_NAME == listtype_name) && !(list_id && list_id[0])) {
371 RNA_warning("template_list using default '%s' UIList class must provide a custom list_id",
373 return false;
374 }
375
376 if (!*active_dataptr) {
377 RNA_warning("No active data");
378 return false;
379 }
380
381 r_input_data->dataptr = *dataptr;
382 if (*dataptr) {
383 r_input_data->prop = RNA_struct_find_property(dataptr, propname.c_str());
384 if (!r_input_data->prop) {
386 "Property not found: %s.%s", RNA_struct_identifier(dataptr->type), propname.c_str());
387 return false;
388 }
389 }
390
391 r_input_data->active_dataptr = *active_dataptr;
392 r_input_data->activeprop = RNA_struct_find_property(active_dataptr, active_propname.c_str());
393 if (!r_input_data->activeprop) {
394 RNA_warning("Property not found: %s.%s",
395 RNA_struct_identifier(active_dataptr->type),
396 active_propname.c_str());
397 return false;
398 }
399
400 if (r_input_data->prop) {
401 const PropertyType type = RNA_property_type(r_input_data->prop);
402 if (type != PROP_COLLECTION) {
403 RNA_warning("Expected a collection data property");
404 return false;
405 }
406 }
407
408 const PropertyType activetype = RNA_property_type(r_input_data->activeprop);
409 if (activetype != PROP_INT) {
410 RNA_warning("Expected an integer active data property");
411 return false;
412 }
413
414 /* Find the uiList type. */
415 if (!(*r_list_type = WM_uilisttype_find(listtype_name, false))) {
416 RNA_warning("List type %s not found", std::string(listtype_name).c_str());
417 return false;
418 }
419
420 r_input_data->active_item_idx = RNA_property_int_get(&r_input_data->active_dataptr,
421 r_input_data->activeprop);
422 r_input_data->item_dyntip_propname = item_dyntip_propname;
423
424 return true;
425}
426
428 PropertyRNA *list_prop,
429 const uiList *ui_list,
430 int activei,
431 TemplateListItems *r_items)
432{
433 const uiListDyn *dyn_data = ui_list->dyn_data;
434 const bool order_reverse = (ui_list->filter_sort_flag & UILST_FLT_SORT_REVERSE) != 0;
435 int i = 0;
436 int reorder_i = 0;
437 bool activei_mapping_pending = true;
438
439 RNA_PROP_BEGIN (list_ptr, itemptr, list_prop) {
441 int new_order_idx;
442 if (dyn_data->items_filter_neworder) {
443 new_order_idx = dyn_data->items_filter_neworder[reorder_i++];
444 new_order_idx = order_reverse ? dyn_data->items_shown - new_order_idx - 1 : new_order_idx;
445 }
446 else {
447 new_order_idx = order_reverse ? dyn_data->items_shown - ++reorder_i : reorder_i++;
448 }
449 // printf("%s: ii: %d\n", __func__, ii);
450 r_items->item_vec[new_order_idx].item = itemptr;
451 r_items->item_vec[new_order_idx].org_idx = i;
452 r_items->item_vec[new_order_idx].flt_flag = dyn_data->items_filter_flags ?
453 dyn_data->items_filter_flags[i] :
454 0;
455
456 if (activei_mapping_pending && activei == i) {
457 activei = new_order_idx;
458 /* So that we do not map again activei! */
459 activei_mapping_pending = false;
460 }
461#if 0 /* For now, do not alter active element, even if it will be hidden... */
462 else if (activei < i) {
463 /* We do not want an active but invisible item!
464 * Only exception is when all items are filtered out...
465 */
466 if (prev_order_idx >= 0) {
467 activei = prev_order_idx;
468 RNA_property_int_set(active_dataptr, activeprop, prev_i);
469 }
470 else {
471 activei = new_order_idx;
472 RNA_property_int_set(active_dataptr, activeprop, i);
473 }
474 }
475 prev_i = i;
476 prev_ii = new_order_idx;
477#endif
478 }
479 i++;
480 }
482
483 /* If mapping is still pending, no active item was found. Mark as invalid (-1) */
484 r_items->active_item_idx = activei_mapping_pending ? -1 : activei;
485}
486
491 uiList *ui_list,
492 TemplateListInputData *input_data,
493 const uiListFilterItemsFunc filter_items_fn,
494 TemplateListItems *r_items)
495{
496 uiListDyn *dyn_data = ui_list->dyn_data;
497
498 /* Filter list items! (not for compact layout, though) */
499 if (input_data->dataptr && input_data->prop) {
500 int items_shown;
501#if 0
502 int prev_ii = -1, prev_i;
503#endif
504
505 if (ui_list->layout_type == UILST_LAYOUT_COMPACT) {
507 &input_data->dataptr, input_data->prop);
508 }
509 else {
510 // printf("%s: filtering...\n", __func__);
511 filter_items_fn(ui_list, C, &input_data->dataptr, RNA_property_identifier(input_data->prop));
512 // printf("%s: filtering done.\n", __func__);
513 }
514
515 items_shown = dyn_data->items_shown;
516 if (items_shown >= 0) {
517 r_items->item_vec.resize(items_shown);
518 // printf("%s: items shown: %d.\n", __func__, items_shown);
519
521 &input_data->dataptr, input_data->prop, ui_list, input_data->active_item_idx, r_items);
522 }
523 }
524}
525
526static void uilist_prepare(uiList *ui_list,
527 const TemplateListItems *items,
528 const TemplateListLayoutDrawData *layout_data,
529 TemplateListVisualInfo *r_visual_info)
530{
531 uiListDyn *dyn_data = ui_list->dyn_data;
532 const bool use_auto_size = (ui_list->list_grip <
533 (layout_data->rows - UI_LIST_AUTO_SIZE_THRESHOLD));
534
535 int actual_rows = layout_data->rows;
536 int actual_maxrows = layout_data->maxrows;
537
538 /* default rows */
539 if (actual_rows <= 0) {
540 actual_rows = 5;
541 }
542 dyn_data->visual_height_min = actual_rows;
543 if (actual_maxrows < actual_rows) {
544 actual_maxrows = max_ii(actual_rows, 5);
545 }
546
547 int activei_row;
548 dyn_data->height = items->item_vec.size();
549 activei_row = items->active_item_idx;
550
551 if (!use_auto_size) {
552 /* No auto-size, yet we clamp at min size! */
553 actual_rows = max_ii(ui_list->list_grip, actual_rows);
554 }
555 else if ((actual_rows != actual_maxrows) && (dyn_data->height > actual_rows)) {
556 /* Expand size if needed and possible. */
557 actual_rows = min_ii(dyn_data->height, actual_maxrows);
558 }
559
560 /* If list length changes or list is tagged to check this,
561 * and active is out of view, scroll to it. */
562 if ((ui_list->list_last_len != items->item_vec.size()) ||
564 {
565 if (activei_row < ui_list->list_scroll) {
566 ui_list->list_scroll = activei_row;
567 }
568 else if (activei_row >= ui_list->list_scroll + actual_rows) {
569 ui_list->list_scroll = activei_row - actual_rows + 1;
570 }
572 }
573
574 const int max_scroll = max_ii(0, dyn_data->height - actual_rows);
575 CLAMP(ui_list->list_scroll, 0, max_scroll);
576 ui_list->list_last_len = items->item_vec.size();
577 dyn_data->visual_height = actual_rows;
578 r_visual_info->visual_items = actual_rows;
579 r_visual_info->start_idx = ui_list->list_scroll;
580 r_visual_info->end_idx = min_ii(r_visual_info->start_idx + actual_rows, items->item_vec.size());
581}
582
583static void uilist_resize_update(bContext *C, uiList *ui_list)
584{
585 uiListDyn *dyn_data = ui_list->dyn_data;
586
587 /* This way we get diff in number of additional items to show (positive) or hide (negative). */
588 const int diff = round_fl_to_int(float(dyn_data->resize - dyn_data->resize_prev) /
589 float(UI_UNIT_Y));
590
591 if (diff != 0) {
592 ui_list->list_grip += diff;
593 dyn_data->resize_prev += diff * UI_UNIT_Y;
595 }
596
597 /* In case uilist is in popup, we need special refreshing */
599}
600
601static void *uilist_item_use_dynamic_tooltip(PointerRNA *itemptr, const char *propname)
602{
603 if (propname && propname[0] && itemptr && *itemptr) {
604 PropertyRNA *prop = RNA_struct_find_property(itemptr, propname);
605
606 if (prop && (RNA_property_type(prop) == PROP_STRING)) {
607 return RNA_property_string_get_alloc(itemptr, prop, nullptr, 0, nullptr);
608 }
609 }
610 return nullptr;
611}
612
613static std::string uilist_item_tooltip_func(bContext * /*C*/, void *argN, const StringRef tip)
614{
615 char *dyn_tooltip = static_cast<char *>(argN);
616 std::string tooltip_string = dyn_tooltip;
617 if (!tip.is_empty()) {
618 tooltip_string += '\n';
619 tooltip_string += tip;
620 }
621 return tooltip_string;
622}
623
628 uiListType *ui_list_type,
629 const char *list_id,
630 euiList_LayoutType layout_type,
631 bool sort_reverse,
632 bool sort_lock)
633{
634 /* Allows to work in popups. */
635 ARegion *region = CTX_wm_region_popup(C);
636 if (region == nullptr) {
637 region = CTX_wm_region(C);
638 }
639
640 /* Find or add the uiList to the current Region. */
641
642 char full_list_id[UI_MAX_NAME_STR];
643 WM_uilisttype_to_full_list_id(ui_list_type, list_id, full_list_id);
644
645 uiList *ui_list = static_cast<uiList *>(
646 BLI_findstring(&region->ui_lists, full_list_id, offsetof(uiList, list_id)));
647
648 if (!ui_list) {
649 ui_list = MEM_new<uiList>("uiList");
650 STRNCPY_UTF8(ui_list->list_id, full_list_id);
651 BLI_addtail(&region->ui_lists, ui_list);
652 ui_list->list_grip = -UI_LIST_AUTO_SIZE_THRESHOLD; /* Force auto size by default. */
653 if (sort_reverse) {
655 }
656 if (sort_lock) {
658 }
659 }
660
661 if (!ui_list->dyn_data) {
662 ui_list->dyn_data = MEM_new<uiListDyn>("uiList.dyn_data");
663 }
664 uiListDyn *dyn_data = ui_list->dyn_data;
665 /* Note that this isn't a `uiListType` callback, it's stored in the runtime list data. Otherwise
666 * the runtime data could leak when the type is unregistered (e.g. on "Reload Scripts"). */
668
669 /* Because we can't actually pass type across save&load... */
670 ui_list->type = ui_list_type;
671 ui_list->layout_type = layout_type;
672
673 /* Reset filtering data. */
674 MEM_SAFE_DELETE(dyn_data->items_filter_flags);
675 MEM_SAFE_DELETE(dyn_data->items_filter_neworder);
676 dyn_data->items_len = dyn_data->items_shown = -1;
677
678 return ui_list;
679}
680
682 uiList *ui_list,
683 Layout &layout,
684 TemplateListInputData *input_data,
685 TemplateListItems *items,
686 const TemplateListLayoutDrawData *layout_data,
687 const TemplateListFlags flags)
688{
689 uiListDyn *dyn_data = ui_list->dyn_data;
690 const char *active_propname = RNA_property_identifier(input_data->activeprop);
691
692 Layout *glob = nullptr, *box, *row, *col, *sub, *overlap;
693 char numstr[32];
694 int rnaicon = ICON_NONE, icon = ICON_NONE;
695 Button *but;
696
697 Block *block = layout.block();
698
699 /* get icon */
700 if (input_data->dataptr && input_data->prop) {
701 StructRNA *ptype = RNA_property_pointer_type(&input_data->dataptr, input_data->prop);
702 rnaicon = RNA_struct_ui_icon(ptype);
703 }
704
705 TemplateListVisualInfo visual_info;
706 switch (ui_list->layout_type) {
708 /* layout */
709 box = &layout.list_box(ui_list, &input_data->active_dataptr, input_data->activeprop);
710 glob = &box->column(true);
711 row = &glob->row(false);
712 col = &row->column(true);
713
714 TemplateListLayoutDrawData adjusted_layout_data = *layout_data;
715 /* init numbers */
716 uilist_prepare(ui_list, items, &adjusted_layout_data, &visual_info);
717
718 int i = 0;
719 if (input_data->dataptr && input_data->prop) {
720
721 const bool editable = (RNA_property_flag(input_data->prop) & PROP_EDITABLE);
722
723 /* create list items */
724 for (i = visual_info.start_idx; i < visual_info.end_idx; i++) {
725 PointerRNA *itemptr = &items->item_vec[i].item;
726 void *dyntip_data;
727 const int org_i = items->item_vec[i].org_idx;
728 const int flt_flag = items->item_vec[i].flt_flag;
729 Block *subblock = col->block();
730
731 overlap = &col->overlap();
732
734
735 /* list item behind label & other buttons */
736 overlap->row(false);
737
738 but = uiDefButR_prop(subblock,
740 "",
741 0,
742 0,
743 UI_UNIT_X * 10,
744 UI_UNIT_Y,
745 &input_data->active_dataptr,
746 input_data->activeprop,
747 0,
748 0,
749 org_i,
750 editable ? TIP_("Select List Item "
751 "(Double click to rename)") :
752 TIP_("Select List Item"));
753
754 if ((dyntip_data = uilist_item_use_dynamic_tooltip(itemptr,
755 input_data->item_dyntip_propname)))
756 {
758 }
759
760 Layout &item_row = overlap->row(true);
761
763
764 sub = &item_row.row(false);
765 icon = icon_from_rnaptr(C, itemptr, rnaicon, false);
766 if (icon == ICON_DOT) {
767 icon = ICON_NONE;
768 }
769 layout_data->draw_item(ui_list,
770 C,
771 *sub,
772 &input_data->dataptr,
773 itemptr,
774 icon,
775 &input_data->active_dataptr,
776 active_propname,
777 org_i,
778 flt_flag);
779
780 /* Items should be able to set context pointers for the layout. But the list-row button
781 * swallows events, so it needs the context storage too for handlers to see it. */
782 but->context = sub->context_store();
783
784 /* If we are "drawing" active item, set all labels as active. */
785 if (i == items->active_item_idx) {
787 }
788
791 }
792 }
793
794 /* add dummy buttons to fill space */
795 for (; i < visual_info.start_idx + visual_info.visual_items; i++) {
796 col->label("", ICON_NONE);
797 }
798
799 /* Add scroll-bar. */
800 if (items->item_vec.size() > visual_info.visual_items) {
801 row->column(false);
802 but = uiDefButV(block,
804 "",
805 0,
806 0,
808 UI_UNIT_Y * dyn_data->visual_height,
809 &ui_list->list_scroll,
810 0,
811 dyn_data->height - dyn_data->visual_height,
812 "");
813 auto *but_scroll = reinterpret_cast<ButtonScrollBar *>(but);
814 but_scroll->visual_height = dyn_data->visual_height;
815 }
816 break;
817 }
819 row = &layout.row(true);
820
821 if ((input_data->dataptr && input_data->prop) && (dyn_data->items_shown > 0) &&
822 (items->active_item_idx >= 0) && (items->active_item_idx < dyn_data->items_shown))
823 {
824 PointerRNA *itemptr = &items->item_vec[items->active_item_idx].item;
825 const int org_i = items->item_vec[items->active_item_idx].org_idx;
826
827 icon = icon_from_rnaptr(C, itemptr, rnaicon, false);
828 if (icon == ICON_DOT) {
829 icon = ICON_NONE;
830 }
831 layout_data->draw_item(ui_list,
832 C,
833 *row,
834 &input_data->dataptr,
835 itemptr,
836 icon,
837 &input_data->active_dataptr,
838 active_propname,
839 org_i,
840 0);
841 }
842 /* if list is empty, add in dummy button */
843 else {
844 row->label("", ICON_NONE);
845 }
846
847 /* next/prev button */
848 SNPRINTF_UTF8(numstr, "%d :", dyn_data->items_shown);
849 but = uiDefIconTextButR_prop(block,
851 ICON_NONE,
852 numstr,
853 0,
854 0,
855 UI_UNIT_X * 5,
856 UI_UNIT_Y,
857 &input_data->active_dataptr,
858 input_data->activeprop,
859 0,
860 0,
861 0,
862 "");
863 if (dyn_data->items_shown == 0) {
865 }
866 break;
867 }
868
869 if (glob) {
870 const bool add_grip_but = (flags & TEMPLATE_LIST_NO_GRIP) == 0;
871
872 /* About #ButtonType::Grip drag-resize:
873 * We can't directly use results from a grip button, since we have a
874 * rather complex behavior here (sizing by discrete steps and, overall, auto-size feature).
875 * Since we *never* know whether we are grip-resizing or not
876 * (because there is no callback for when a button enters/leaves its "edit mode"),
877 * we use the fact that grip-controlled value (dyn_data->resize) is completely handled
878 * by the grip during the grab resize, so settings its value here has no effect at all.
879 *
880 * It is only meaningful when we are not resizing,
881 * in which case this gives us the correct "init drag" value.
882 * Note we cannot affect `dyn_data->resize_prev here`,
883 * since this value is not controlled by the grip!
884 */
885 dyn_data->resize = dyn_data->resize_prev +
886 (dyn_data->visual_height - ui_list->list_grip) * UI_UNIT_Y;
887
888 row = &glob->row(true);
889 Block *subblock = row->block();
891
892 if (ui_list->filter_flag & UILST_FLT_SHOW) {
893 but = uiDefIconButBit(subblock,
896 ICON_DISCLOSURE_TRI_DOWN,
897 0,
898 0,
899 UI_UNIT_X,
900 UI_UNIT_Y * 0.5f,
901 &ui_list->filter_flag,
902 0,
903 0,
904 TIP_("Hide filtering options"));
905 button_flag_disable(but, BUT_UNDO); /* skip undo on screen buttons */
906
907 if (add_grip_but) {
908 but = uiDefIconButV(subblock,
910 ICON_GRIP,
911 0,
912 0,
913 UI_UNIT_X * 10.0f,
914 UI_UNIT_Y * 0.5f,
915 &dyn_data->resize,
916 0.0,
917 0.0,
918 "");
919 button_func_set(but, [ui_list](bContext &C) { uilist_resize_update(&C, ui_list); });
920 }
921
923
924 col = &glob->column(false);
925 subblock = col->block();
926 uiDefBut(subblock,
928 "",
929 0,
930 0,
931 UI_UNIT_X,
932 UI_UNIT_Y * 0.05f,
933 nullptr,
934 0.0,
935 0.0,
936 "");
937
938 layout_data->draw_filter(ui_list, C, *col);
939 }
940 else {
941 but = uiDefIconButBit(subblock,
944 ICON_DISCLOSURE_TRI_RIGHT,
945 0,
946 0,
947 UI_UNIT_X,
948 UI_UNIT_Y * 0.5f,
949 &ui_list->filter_flag,
950 0,
951 0,
952 TIP_("Show filtering options"));
953 button_flag_disable(but, BUT_UNDO); /* skip undo on screen buttons */
954
955 if (add_grip_but) {
956 but = uiDefIconButV(subblock,
958 ICON_GRIP,
959 0,
960 0,
961 UI_UNIT_X * 10.0f,
962 UI_UNIT_Y * 0.5f,
963 &dyn_data->resize,
964 0.0,
965 0.0,
966 "");
967 button_func_set(but, [ui_list](bContext &C) { uilist_resize_update(&C, ui_list); });
968 }
969
971 }
972 }
973}
974
976 const bContext *C,
977 const char *listtype_name,
978 const char *list_id,
979 PointerRNA *dataptr,
980 const StringRefNull propname,
981 PointerRNA *active_dataptr,
982 const StringRefNull active_propname,
983 const char *item_dyntip_propname,
984 int rows,
985 int maxrows,
986 int layout_type,
987 enum TemplateListFlags flags)
988{
989 TemplateListInputData input_data = {};
990 uiListType *ui_list_type;
991 if (!template_uilist_data_retrieve(listtype_name,
992 list_id,
993 dataptr,
994 propname,
995 active_dataptr,
996 active_propname,
997 item_dyntip_propname,
998 &input_data,
999 &ui_list_type))
1000 {
1001 return;
1002 }
1003
1004 uiListDrawItemFunc draw_item = ui_list_type->draw_item ? ui_list_type->draw_item :
1006 uiListDrawFilterFunc draw_filter = ui_list_type->draw_filter ? ui_list_type->draw_filter :
1008 uiListFilterItemsFunc filter_items = ui_list_type->filter_items ? ui_list_type->filter_items :
1010
1011 uiList *ui_list = uilist_ensure(C,
1012 ui_list_type,
1013 list_id,
1014 euiList_LayoutType(layout_type),
1016 flags & TEMPLATE_LIST_SORT_LOCK);
1017
1018 /* When active item changed since last draw, scroll to it. */
1019 if (input_data.active_item_idx != ui_list->list_last_activei) {
1021 ui_list->list_last_activei = input_data.active_item_idx;
1022 }
1023
1024 TemplateListItems items;
1025 template_uilist_collect_display_items(C, ui_list, &input_data, filter_items, &items);
1026
1027 TemplateListLayoutDrawData layout_data;
1028 layout_data.draw_item = draw_item;
1029 layout_data.draw_filter = draw_filter;
1030 layout_data.rows = rows;
1031 layout_data.maxrows = maxrows;
1032
1033 template_uilist_layout_draw(C, ui_list, *layout, &input_data, &items, &layout_data, flags);
1034}
1035
1036/* -------------------------------------------------------------------- */
1039
1044
1046
1047} // namespace blender::ui
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define TIP_(msgid)
#define IFACE_(msgid)
#define MAX_IDPROP_NAME
Definition DNA_ID.h:174
#define UI_LIST_AUTO_SIZE_THRESHOLD
Read Blender memory allocation and freeing.
#define RNA_PROP_END
#define RNA_PROP_BEGIN(sptr, itemptr, prop)
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UL_DEFAULT_CLASS_NAME
#define UI_UNIT_X
#define UI_ITEM_NONE
#define RNA_NO_INDEX
#define UI_MAX_NAME_STR
#define V2D_SCROLL_WIDTH
Definition UI_view2d.hh:79
#define CLAMP(a, b, c)
constexpr const char * c_str() const
constexpr bool is_empty() const
eUIListFilterResult operator()(const PointerRNA &itemptr, StringRefNull name, int index)
#define offsetof(t, d)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
static void template_uilist_layout_draw(const bContext *C, uiList *ui_list, Layout &layout, TemplateListInputData *input_data, TemplateListItems *items, const TemplateListLayoutDrawData *layout_data, const TemplateListFlags flags)
static void uilist_filter_items_default(uiList *ui_list, const bContext *C, PointerRNA *dataptr, const char *propname)
static int cmpstringp(const void *p1, const void *p2)
void button_func_set(Button *but, std::function< void(bContext &)> func)
static uiList * uilist_ensure(const bContext *C, uiListType *ui_list_type, const char *list_id, euiList_LayoutType layout_type, bool sort_reverse, bool sort_lock)
static void * uilist_item_use_dynamic_tooltip(PointerRNA *itemptr, const char *propname)
uiListType * UI_UL_cache_file_layers()
static void uilist_prepare(uiList *ui_list, const TemplateListItems *items, const TemplateListLayoutDrawData *layout_data, TemplateListVisualInfo *r_visual_info)
static void uilist_free_dyn_data(uiList *ui_list)
void block_flag_enable(Block *block, int flag)
void template_uilist(Layout *layout, const bContext *C, const char *listtype_name, const char *list_id, PointerRNA *dataptr, StringRefNull propname, PointerRNA *active_dataptr, StringRefNull active_propname, const char *item_dyntip_propname, int rows, int maxrows, int layout_type, enum TemplateListFlags flags)
Button * uiDefIconButV(Block *block, ButtonType type, int icon, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
void button_func_tooltip_set(Button *but, ButtonToolTipFunc func, void *arg, FreeArgFunc free_arg)
void button_flag_enable(Button *but, int64_t flag)
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)
@ UI_LIST_ITEM_FILTER_MISMATCHES
@ UI_LIST_ITEM_FILTER_MATCHES
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)
static void uilist_draw_item_default(uiList *ui_list, const bContext *, Layout &layout, PointerRNA *, PointerRNA *itemptr, int icon, PointerRNA *, const char *, int, int)
static std::string uilist_item_tooltip_func(bContext *, void *argN, const StringRef tip)
void block_emboss_set(Block *block, EmbossType emboss)
int icon_from_rnaptr(const bContext *C, PointerRNA *ptr, int rnaicon, bool big)
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 block_flag_disable(Block *block, int flag)
void layout_list_set_labels_active(Layout *layout)
void button_flag_disable(Button *but, int64_t flag)
static bool template_uilist_data_retrieve(const StringRef listtype_name, const char *list_id, PointerRNA *dataptr, const StringRefNull propname, PointerRNA *active_dataptr, const StringRefNull active_propname, const char *item_dyntip_propname, TemplateListInputData *r_input_data, uiListType **r_list_type)
bool uilist_item_index_is_filtered_visible(const struct uiList *ui_list, int item_idx)
FunctionRef< eUIListFilterResult(const PointerRNA &itemptr, StringRefNull name, int index)> uiListItemFilterFn
static void uilist_draw_filter_default(uiList *ui_list, const bContext *, Layout &layout)
static void uilist_resize_update(bContext *C, uiList *ui_list)
Button * uiDefIconButBit(Block *block, ButtonType type, int bit, int icon, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
Button * uiDefButV(Block *block, ButtonType type, StringRef str, int x, int y, short width, short height, T *poin, float min, float max, std::optional< StringRef > tip)
void uiLayoutListItemAddPadding(Layout *layout)
void uilist_filter_and_sort_items(uiList *ui_list, const bContext *C, uiListItemFilterFn item_filter_fn, PointerRNA *dataptr, const char *propname, uiListItemGetNameFn get_name_fn=nullptr)
static void template_uilist_collect_display_items(const bContext *C, uiList *ui_list, TemplateListInputData *input_data, const uiListFilterItemsFunc filter_items_fn, TemplateListItems *r_items)
FunctionRef< std::string(const PointerRNA &itemptr, int index)> uiListItemGetNameFn
static void template_uilist_collect_items(PointerRNA *list_ptr, PropertyRNA *list_prop, const uiList *ui_list, int activei, TemplateListItems *r_items)
char * RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len)
int char char int BLI_strcasecmp(const char *s1, const char *s2) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ UILST_SCROLL_TO_ACTIVE_ITEM
MINLINE int round_fl_to_int(float a)
const char * RNA_property_identifier(const PropertyRNA *prop)
void(*)(uiList *ui_list, const bContext *C, PointerRNA *, const char *propname) uiListFilterItemsFunc
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
char * RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len)
void(*)(uiList *ui_list, const bContext *C, ui::Layout &layout, PointerRNA *dataptr, PointerRNA *itemptr, int icon, PointerRNA *active_dataptr, const char *active_propname, int index, int flt_flag) uiListDrawItemFunc
MINLINE int max_ii(int a, int b)
int RNA_property_collection_length(PointerRNA *ptr, PropertyRNA *prop)
void MEM_delete_void(void *vmemh)
char char * BLI_strncpy_ensure_pad(char *__restrict dst, const char *__restrict src, char pad, size_t dst_maxncpy) ATTR_NONNULL(1
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
PropertyType RNA_property_type(PropertyRNA *prop)
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
void WM_uilisttype_to_full_list_id(const uiListType *ult, const char *list_id, char r_full_list_id[])
void ED_region_tag_refresh_ui(ARegion *region)
Definition area.cc:738
@ PROP_STRING
Definition RNA_types.hh:213
@ PROP_COLLECTION
Definition RNA_types.hh:216
@ UILST_FLT_ITEM_NEVER_SHOW
int RNA_property_flag(PropertyRNA *prop)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void(*)(uiList *ui_list, const bContext *C, ui::Layout &layout) uiListDrawFilterFunc
PropertyRNA * RNA_struct_name_property(const StructRNA *type)
int RNA_struct_ui_icon(const StructRNA *type)
const char * RNA_struct_identifier(const StructRNA *type)
bool WM_uilisttype_add(uiListType *ult)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
@ PROP_EDITABLE
Definition RNA_types.hh:376
ARegion * CTX_wm_region_popup(const bContext *C)
uiListType * WM_uilisttype_find(const StringRef idname, bool quiet)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
MINLINE int min_ii(int a, int b)
ARegion * CTX_wm_region(const bContext *C)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
ListBaseT< uiList > ui_lists
StructRNA * type
Definition RNA_types.hh:63
uiListFreeRuntimeDataFunc free_runtime_data_fn
uiListFilterItemsFunc filter_items
uiListDrawFilterFunc draw_filter
uiListDrawItemFunc draw_item
euiList_FilterSortFlag filter_sort_flag
euiList_LayoutType layout_type
euiList_FilterFlag filter_flag
struct uiListType * type
const bContextStore * context
void label(StringRef name, int icon)
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)
Layout & list_box(uiList *ui_list, PointerRNA *actptr, PropertyRNA *actprop)
Layout & row(bool align)