Blender V5.3
interface_layout.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 <algorithm>
10#include <cmath>
11#include <cstdlib>
12#include <cstring>
13#include <fmt/format.h>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_screen_types.h"
18#include "DNA_userdef_types.h"
19
20#include "BLI_array.hh"
21#include "BLI_dynstr.hh"
22#include "BLI_enum_flags.hh"
23#include "BLI_listbase.hh"
24#include "BLI_math_base_c.hh"
25#include "BLI_path_utils.hh"
26#include "BLI_rect.hh"
27#include "BLI_string_ref.hh"
28#include "BLI_string_utf8.hh"
29
30#include "BLT_translation.hh"
31
32#include "BKE_context.hh"
33#include "BKE_global.hh"
34#include "BKE_idprop.hh"
35#include "BKE_lib_id.hh"
36#include "BKE_path_templates.hh"
37#include "BKE_screen.hh"
38
39#include "IMB_imbuf_types.hh"
40
41#include "RNA_access.hh"
42#include "RNA_prototypes.hh"
43
45
46#include "ED_id_management.hh"
47
48#include "WM_api.hh"
49#include "WM_types.hh"
50
54#include "interface_intern.hh"
55
56namespace blender::ui {
57
58struct ButtonItem;
59
60/* Show an icon button after each RNA button to use to quickly set keyframes,
61 * this is a way to display animation/driven/override status, see #54951. */
62#define UI_PROP_DECORATE
63/* Alternate draw mode where some buttons can use single icon width,
64 * giving more room for the text at the expense of nicely aligned text. */
65#define UI_PROP_SEP_ICON_WIDTH_EXCEPTION
66
67/* -------------------------------------------------------------------- */
70
75#define UI_OPERATOR_ERROR_RET(_ot, _opname, _caller_fn_name) \
76 if (ot == nullptr) { \
77 item_disabled(this, _opname); \
78 RNA_warning_bare("%s: '%s' unknown operator", _caller_fn_name, _opname); \
79 return {}; \
80 } \
81 (void)0
82
83/* uiLayoutRoot */
84
102
103/* Item */
104
105enum class ItemType : int8_t {
107
119 LayoutRadial, /* AKA: menu pie. */
120
122#if 0
123 TemplateColumnFlow,
124 TemplateSplit,
125 TemplateBox,
126
127 TemplateHeader,
128 TemplateHeaderID,
129#endif
130};
131
134 FixedSize = 1 << 1,
135
136 BoxItem = 1 << 2, /* The item is "inside" a box item */
137 PropSep = 1 << 3,
139 /* Show an icon button next to each property (to set keyframes, show status).
140 * Enabled by default, depends on 'ItemInternalFlag::PropSep'. */
141 PropDecorate = 1 << 5,
143};
145
146
148 static void inside_property_split_set(Item *item, bool inside_prop_sep)
149 {
151 }
152
153 [[nodiscard]] static bool use_property_decorate_no_pad(const Item *item)
154 {
155 return flag_is_set(item->flag_, ItemInternalFlag::PropDecorateNoPad);
156 };
157
158 [[nodiscard]] static bool box_item(const Item *item)
159 {
160 return flag_is_set(item->flag_, ItemInternalFlag::BoxItem);
161 }
162 static void box_item_set(Item *item, bool box_item)
163 {
165 }
166
167 [[nodiscard]] static bool auto_fixed_size(const Item *item)
168 {
169 return flag_is_set(item->flag_, ItemInternalFlag::AutoFixedSize);
170 }
175};
176
179 static void init_from_parent(Layout *item, Layout *layout, int align);
180
181 static void layout_add_but(Layout *layout, Button *but);
182 static void layout_remove_but(Layout *layout, const Button *but);
183 static void layout_estimate(Layout *layout);
184 static void layout_resolve(Layout *layout);
185 static ButtonItem *layout_find_button_item(const Layout *layout, const Button *but);
186 static Layout *item_prop_split_layout_hack(Layout *layout_parent, Layout *layout_split);
187 static void layout_offset_size_set(Layout *layout, int x, int y, int w, int h);
188 static void layout_move(Layout *layout, int delta_xmin, int delta_xmax);
189 static void layout_translate_y(Layout *layout, int delta);
190 static void layout_space_set(Layout *layout, int space);
191 static int layout_space_get(Layout *layout);
192};
193
195
197{
198 return type_;
199};
200
202
203struct ButtonItem : public Item {
204 Button *but = nullptr;
206};
207
208struct LayoutRow : public Layout {
211
212 void estimate_impl() override;
213 void resolve_impl() override;
214};
215
216struct LayoutColumn : public Layout {
219
220 void estimate_impl() override;
221 void resolve_impl() override;
222};
223
228
229struct LayoutOverlap : public Layout {
231
232 void estimate_impl() override;
233 void resolve_impl() override;
234};
235
236struct LayoutRadial : public Layout {
238
239 void estimate_impl() override {};
240 void resolve_impl() override;
241};
242
243struct LayoutAbsolute : public Layout {
245
246 void estimate_impl() override;
247 void resolve_impl() override;
248};
249
250struct LayoutItemFlow : public Layout {
251 int number = 0;
252 int totcol = 0;
254
255 void estimate_impl() override;
256 void resolve_impl() override;
257};
258
259struct LayoutItemGridFlow : public Layout {
260 /* Extra parameters */
261 bool row_major = false; /* Fill first row first, instead of filling first column first. */
262 bool even_columns = false; /* Same width for all columns. */
263 bool even_rows = false; /* Same height for all rows. */
270 int columns_len = 0;
271
272 /* Pure internal runtime storage. */
273 int tot_items = 0, tot_columns = 0, tot_rows = 0;
274
276
277 void estimate_impl() override;
278 void resolve_impl() override;
279};
280
281struct LayoutItemBx : public LayoutColumn {
282 Button *roundbox = nullptr;
284
285 void estimate_impl() override;
286 void resolve_impl() override;
287 void resolve_dynamic_height() override;
288};
289
292 std::string open_prop_name;
293 int index = 0;
295
296 void estimate_impl() override;
297 void resolve_impl() override;
298 void resolve_dynamic_height() override;
299};
300
307
308struct LayoutItemSplit : public LayoutRow {
309 float percentage = 0.0f;
311
312 void estimate_impl() override;
313 void resolve_impl() override;
314};
315
317
318static void item_disabled(Layout *layout, const char *name);
319
320/* -------------------------------------------------------------------- */
323
325{
326 const int len = name.size();
327
328 if (len != 0 && len + 1 < UI_MAX_NAME_STR) {
329 memcpy(namestr, name.data(), len);
330 namestr[len] = ':';
331 namestr[len + 1] = '\0';
332 return namestr;
333 }
334
335 return name;
336}
337
339{
340 const int len = name.size();
341
342 if (len != 0 && len + 1 < UI_MAX_NAME_STR) {
343 memcpy(namestr, name.data(), len);
344 namestr[len] = ':';
345 namestr[len + 1] = '\0';
346 return namestr;
347 }
348
349 return name;
350}
351
352static int item_fit(const int item,
353 const int pos,
354 const int all,
355 const int available,
356 const bool is_last,
357 const LayoutAlign alignment,
358 float *extra_pixel)
359{
360 /* available == 0 is unlimited */
361 if (ELEM(0, available, all)) {
362 return item;
363 }
364
365 if (all > available) {
366 /* contents is bigger than available space */
367 if (is_last) {
368 return available - pos;
369 }
370
371 const float width = *extra_pixel + (item * available) / float(all);
372 *extra_pixel = width - int(width);
373 return int(width);
374 }
375
376 /* contents is smaller or equal to available space */
377 if (alignment == LayoutAlign::Expand) {
378 if (is_last) {
379 return available - pos;
380 }
381
382 const float width = *extra_pixel + (item * available) / float(all);
383 *extra_pixel = width - int(width);
384 return int(width);
385 }
386 return item;
387}
388
389/* variable button size in which direction? */
390#define UI_ITEM_VARY_X 1
391#define UI_ITEM_VARY_Y 2
392
393static int layout_vary_direction(Layout *layout)
394{
395 return ((ELEM(layout->root()->type, LayoutType::Header, LayoutType::PieMenu) ||
396 (layout->alignment() != LayoutAlign::Expand)) ?
399}
400
401static bool layout_variable_size(Layout *layout)
402{
403 /* Note that this code is probably a bit unreliable, we'd probably want to know whether it's
404 * variable in X and/or Y, etc. But for now it mimics previous one,
405 * with addition of variable flag set for children of grid-flow layouts. */
406 return layout_vary_direction(layout) == UI_ITEM_VARY_X || layout->variable_size();
407}
408
414 float text;
415 float icon;
417};
418
427constexpr TextIconPadFactor text_pad_default = {1.50f, 0.25f, 0.0f};
428
430constexpr TextIconPadFactor text_pad_compact = {1.25f, 0.35f, 0.0f};
431
433constexpr TextIconPadFactor text_pad_none = {0.25f, 1.50f, 0.0f};
434
438static int text_icon_width_ex(Layout *layout,
439 const StringRef name,
440 int icon,
441 const TextIconPadFactor &pad_factor,
442 const uiFontStyle *fstyle)
443{
444 const int unit_x = UI_UNIT_X * (layout->scale_x() ? layout->scale_x() : 1.0f);
445
446 /* When there is no text, always behave as if this is an icon-only button
447 * since it's not useful to return empty space. */
448 if (icon && name.is_empty()) {
449 return unit_x * (1.0f + pad_factor.icon_only);
450 }
451
452 if (layout_variable_size(layout)) {
453 if (!icon && name.is_empty()) {
454 return unit_x * (1.0f + pad_factor.icon_only);
455 }
456
457 if (layout->alignment() != LayoutAlign::Expand) {
458 layout->fixed_size_set(true);
459 }
460
461 float margin = pad_factor.text;
462 if (icon) {
463 margin += pad_factor.icon;
464 }
465
466 const float aspect = layout->block()->aspect;
467 return fontstyle_string_width_with_block_aspect(fstyle, name, aspect) +
468 int(ceilf(unit_x * margin));
469 }
470 return unit_x * 10;
471}
472
473static int text_icon_width(Layout *layout,
474 const StringRef name,
475 const int icon,
476 const bool compact)
477{
478 return text_icon_width_ex(
479 layout, name, icon, compact ? text_pad_compact : text_pad_default, UI_FSTYLE_WIDGET);
480}
481
483{
484 if (this->type() == ItemType::Button) {
485 const ButtonItem *bitem = static_cast<const ButtonItem *>(this);
486 return {int(BLI_rctf_size_x(&bitem->but->rect)), int(BLI_rctf_size_y(&bitem->but->rect))};
487 }
488 return static_cast<const Layout *>(this)->size();
489}
490
492{
493 return {x_, y_};
494}
495
497{
498 return {w_, h_};
499}
500
502{
503 if (this->type() == ItemType::Button) {
504 const ButtonItem *bitem = static_cast<const ButtonItem *>(this);
505 return {int(bitem->but->rect.xmin), int(bitem->but->rect.ymin)};
506 }
507 return {0, 0};
508}
509
510static void item_position(Item *item, const int x, const int y, const int w, const int h)
511{
512 if (item->type() == ItemType::Button) {
513 ButtonItem *bitem = static_cast<ButtonItem *>(item);
514
515 bitem->but->rect.xmin = x;
516 bitem->but->rect.ymin = y;
517 bitem->but->rect.xmax = x + w;
518 bitem->but->rect.ymax = y + h;
519
520 button_update(bitem->but); /* For `strlen`. */
521 }
522 else {
523 LayoutInternal::layout_offset_size_set(static_cast<Layout *>(item), x, y + h, w, h);
524 }
525}
526
527void LayoutInternal::layout_offset_size_set(Layout *layout, int x, int y, int w, int h)
528{
529 layout->x_ = x;
530 layout->y_ = y;
531 layout->w_ = w;
532 layout->h_ = h;
533}
534
536{
537 layout->y_ += delta;
538}
539
540static void item_translate_y(Item *item, const int delta)
541{
542 if (delta == 0) {
543 /* Early return for recursive calls. */
544 return;
545 }
546 if (item->type() == ItemType::Button) {
547 const auto *bitem = static_cast<const ButtonItem *>(item);
548 bitem->but->rect.ymin += delta;
549 bitem->but->rect.ymax += delta;
550 }
551 else {
552 auto *layout = static_cast<Layout *>(item);
554 for (Item *sub : layout->items()) {
555 item_translate_y(sub, delta);
556 }
557 }
558}
559
560static void item_move(Item *item, const int delta_xmin, const int delta_xmax)
561{
562 if (item->type() == ItemType::Button) {
563 ButtonItem *bitem = static_cast<ButtonItem *>(item);
564
565 bitem->but->rect.xmin += delta_xmin;
566 bitem->but->rect.xmax += delta_xmax;
567
568 button_update(bitem->but); /* For `strlen`. */
569 }
570 else {
571 LayoutInternal::layout_move(static_cast<Layout *>(item), delta_xmin, delta_xmax);
572 }
573}
574
575void LayoutInternal::layout_move(Layout *layout, int delta_xmin, int delta_xmax)
576{
577 if (delta_xmin > 0) {
578 layout->x_ += delta_xmin;
579 }
580 else {
581 layout->w_ += delta_xmax;
582 }
583}
584
586{
587 layout->space_ = space;
588}
589
591{
592 return layout->space_;
593}
594
596
597/* -------------------------------------------------------------------- */
600
620
621static Layout *item_local_sublayout(Layout *test, Layout *layout, bool align)
622{
623 Layout *sub;
625 sub = &layout->row(align);
626 }
627 else {
628 sub = &layout->column(align);
629 }
630
632 return sub;
633}
634
635static void layer_but_cb(bContext *C, void *arg_but, void *arg_index)
636{
637 wmWindow *win = CTX_wm_window(C);
638 Button *but = static_cast<Button *>(arg_but);
639 PointerRNA *ptr = &but->rnapoin;
640 PropertyRNA *prop = but->rnaprop;
641 const int index = POINTER_AS_INT(arg_index);
642 const bool shift = win->runtime->eventstate->modifier & KM_SHIFT;
643 const int len = RNA_property_array_length(ptr, prop);
644
645 if (!shift) {
648 value_array.fill(false);
649 value_array[index] = true;
650
651 RNA_property_boolean_set_array(ptr, prop, value_array.data());
652
653 RNA_property_update(C, ptr, prop);
654
655 for (Button &cbut : but->block->buttons()) {
656 button_update(&cbut);
657 }
658 }
659}
660
661/* create buttons for an item with an RNA array */
662static void item_array(Layout *layout,
663 Block *block,
664 const StringRef name,
665 int icon,
666 PointerRNA *ptr,
667 PropertyRNA *prop,
668 const int len,
669 int x,
670 const int y,
671 int w,
672 const int /*h*/,
673 const bool expand,
674 const bool slider,
675 const int toggle,
676 const bool icon_only,
677 const bool compact,
678 const bool show_text)
679{
680 const uiStyle *style = layout->root()->style;
681
682 /* retrieve type and subtype */
683 const PropertyType type = RNA_property_type(prop);
684 const PropertySubType subtype = RNA_property_subtype(prop);
685
686 Layout *sub = item_local_sublayout(layout, layout, true);
687 block_layout_set_current(block, sub);
688
689 /* create label */
690 if (!name.is_empty() && show_text) {
691 uiDefBut(block, ButtonType::Label, name, 0, 0, w, UI_UNIT_Y, nullptr, 0.0, 0.0, "");
692 }
693
694 /* create buttons */
695 if (type == PROP_BOOLEAN && ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER)) {
696 /* special check for layer layout */
697 const int cols = (len >= 20) ? 2 : 1;
698 const int colbuts = len / (2 * cols);
699 uint layer_used = 0;
700 uint layer_active = 0;
701
702 block_layout_set_current(block, &layout->absolute(false));
703
704 const int butw = UI_UNIT_X * 0.75;
705 const int buth = UI_UNIT_X * 0.75;
706
707 for (int b = 0; b < cols; b++) {
708 block_align_begin(block);
709
710 for (int a = 0; a < colbuts; a++) {
711 const int layer_num = a + b * colbuts;
712 const uint layer_flag = (1u << layer_num);
713
714 if (layer_used & layer_flag) {
715 if (layer_active & layer_flag) {
716 icon = ICON_LAYER_ACTIVE;
717 }
718 else {
719 icon = ICON_LAYER_USED;
720 }
721 }
722 else {
723 icon = ICON_BLANK1;
724 }
725
726 Button *but = uiDefAutoButR(
727 block, ptr, prop, layer_num, "", icon, x + butw * a, y + buth, butw, buth);
728 if (subtype == PROP_LAYER_MEMBER) {
729 button_func_set(but, layer_but_cb, but, POINTER_FROM_INT(layer_num));
730 }
731 }
732 for (int a = 0; a < colbuts; a++) {
733 const int layer_num = a + len / 2 + b * colbuts;
734 const uint layer_flag = (1u << layer_num);
735
736 if (layer_used & layer_flag) {
737 if (layer_active & layer_flag) {
738 icon = ICON_LAYER_ACTIVE;
739 }
740 else {
741 icon = ICON_LAYER_USED;
742 }
743 }
744 else {
745 icon = ICON_BLANK1;
746 }
747
748 Button *but = uiDefAutoButR(
749 block, ptr, prop, layer_num, "", icon, x + butw * a, y, butw, buth);
750 if (subtype == PROP_LAYER_MEMBER) {
751 button_func_set(but, layer_but_cb, but, POINTER_FROM_INT(layer_num));
752 }
753 }
754 block_align_end(block);
755
756 x += colbuts * butw + style->buttonspacex;
757 }
758 }
759 else if (subtype == PROP_MATRIX) {
760 int totdim, dim_size[/*RNA_MAX_ARRAY_DIMENSION*/ 3];
761 int row, col;
762
763 block_layout_set_current(block, &layout->absolute(true));
764
765 totdim = RNA_property_array_dimension(ptr, prop, dim_size);
766 if (totdim != 2) {
767 /* Only 2D matrices supported in UI so far. */
768 return;
769 }
770
771 w /= dim_size[1];
772 // h /= dim_size[0]; /* UNUSED */
773
774 for (int a = 0; a < len; a++) {
775 /* We are going over flat array indices (the way matrices are stored internally [also check
776 * logic in #pyrna_py_from_array_index()]) -- and they are not ordered "row first" -- , so
777 * map these to rows/columns. */
778 col = a % dim_size[1];
779 row = a / dim_size[1];
780 std::optional<ButtonType> button_type = slider ? std::optional(ButtonType::NumSlider) :
781 std::nullopt;
782 uiDefAutoButR(block,
783 ptr,
784 prop,
785 a,
786 "",
787 ICON_NONE,
788 x + w * col,
789 y + (dim_size[0] * UI_UNIT_Y) - (row * UI_UNIT_Y),
790 w,
791 UI_UNIT_Y,
792 button_type);
793 }
794 }
795 else if (subtype == PROP_DIRECTION && !expand) {
796 uiDefButR_prop(block,
798 name,
799 x,
800 y,
801 UI_UNIT_X * 3,
802 UI_UNIT_Y * 3,
803 ptr,
804 prop,
805 -1,
806 0,
807 0,
808 std::nullopt);
809 }
810 else {
811 /* NOTE: this block of code is a bit arbitrary and has just been made
812 * to work with common cases, but may need to be re-worked */
813
814 /* special case, boolean array in a menu, this could be used in a more generic way too */
815 if (ELEM(subtype, PROP_COLOR, PROP_COLOR_GAMMA) && !expand && ELEM(len, 3, 4)) {
816 uiDefAutoButR(block, ptr, prop, -1, "", ICON_NONE, 0, 0, w, UI_UNIT_Y);
817 }
818 else {
819 /* Even if 'expand' is false, we expand anyway. */
820
821 /* Layout for known array sub-types. */
822 char str[3] = {'\0'};
823
824 if (!icon_only && show_text) {
825 if (type != PROP_BOOLEAN) {
826 str[1] = ':';
827 }
828 }
829
830 /* Show check-boxes for rna on a non-emboss block (menu for eg). */
831 bool *boolarr = nullptr;
832 if (type == PROP_BOOLEAN &&
834 {
835 boolarr = MEM_new_array_zeroed<bool>(len, __func__);
836 RNA_property_boolean_get_array(ptr, prop, boolarr);
837 }
838
839 const char *str_buf = show_text ? str : "";
840 for (int a = 0; a < len; a++) {
841 if (!icon_only && show_text) {
842 str[0] = RNA_property_array_item_char(prop, a);
843 }
844 if (boolarr) {
845 icon = boolarr[a] ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
846 }
847
848 const int width_item = ((compact && type == PROP_BOOLEAN) ?
849 min_ii(w, text_icon_width(layout, str_buf, icon, false)) :
850 w);
851 std::optional<ButtonType> button_type = slider ? std::optional(ButtonType::NumSlider) :
852 std::nullopt;
853 Button *but = uiDefAutoButR(
854 block, ptr, prop, a, str_buf, icon, 0, 0, width_item, UI_UNIT_Y, button_type);
855 if ((toggle == 1) && but->type == ButtonType::Checkbox) {
857 }
858 if ((a == 0) && (subtype == PROP_AXISANGLE)) {
860 }
861 }
862
863 if (boolarr) {
864 MEM_delete(boolarr);
865 }
866 }
867 }
868
869 block_layout_set_current(block, layout);
870}
871
872static void item_enum_expand_handle(bContext *C, void *arg1, void *arg2)
873{
874 wmWindow *win = CTX_wm_window(C);
875
876 if ((win->runtime->eventstate->modifier & KM_SHIFT) == 0) {
877 Button *but = static_cast<Button *>(arg1);
878 const int enum_value = POINTER_AS_INT(arg2);
879
880 int current_value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
881 if (!(current_value & enum_value)) {
882 current_value = enum_value;
883 }
884 else {
885 current_value &= enum_value;
886 }
887 RNA_property_enum_set(&but->rnapoin, but->rnaprop, current_value);
888 }
889}
890
895 Block *block,
896 PointerRNA *ptr,
897 PropertyRNA *prop,
898 const std::optional<StringRef> uiname,
899 const int h,
900 const ButtonType but_type,
901 const bool icon_only,
902 const EnumPropertyItem *item,
903 const bool is_first)
904{
905 const char *name = (!uiname || !uiname->is_empty()) ? item->name : "";
906 const int icon = item->icon;
907 const int value = item->value;
908 const int itemw = text_icon_width(block->curlayout, icon_only ? "" : name, icon, false);
909
910 Button *but;
911 if (icon && name[0] && !icon_only) {
913 block, but_type, icon, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, std::nullopt);
914 }
915 else if (icon) {
916 const int w = (is_first) ? itemw : ceilf(itemw - U.pixelsize);
917 but = uiDefIconButR_prop(
918 block, but_type, icon, 0, 0, w, h, ptr, prop, -1, 0, value, std::nullopt);
919 }
920 else {
921 but = uiDefButR_prop(
922 block, but_type, name, 0, 0, itemw, h, ptr, prop, -1, 0, value, std::nullopt);
923 }
924
925 if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
926 /* If this is set, assert since we're clobbering someone else's callback. */
927 /* Buttons get their block's func by default, so we cannot assert in that case either. */
928 BLI_assert(ELEM(but->func, nullptr, block->func));
930 }
931
933 but->drawflag |= BUT_TEXT_LEFT;
934 }
935
936 /* Allow quick, inaccurate swipe motions to switch tabs
937 * (no need to keep cursor over them). */
938 if (but_type == ButtonType::Tab) {
939 but->flag |= BUT_DRAG_LOCK;
940 }
941}
942
943static void item_enum_expand_exec(Layout *layout,
944 Block *block,
945 PointerRNA *ptr,
946 PropertyRNA *prop,
947 const std::optional<StringRef> uiname,
948 const int h,
949 const ButtonType but_type,
950 const bool icon_only)
951{
952 /* XXX: The way this function currently handles uiname parameter
953 * is insane and inconsistent with general UI API:
954 *
955 * - uiname is the *enum property* label.
956 * - when it is nullptr or empty, we do not draw *enum items* labels,
957 * this doubles the icon_only parameter.
958 * - we *never* draw (i.e. really use) the enum label uiname, it is just used as a mere flag!
959 *
960 * Unfortunately, fixing this implies an API "soft break", so better to defer it for later... :/
961 * - mont29
962 */
963
965
966 const bool radial = (layout->root()->type == LayoutType::PieMenu);
967
968 bool free;
970 if (radial) {
972 static_cast<bContext *>(block->evil_C), ptr, prop, &item_array, nullptr, &free);
973 }
974 else {
976 static_cast<bContext *>(block->evil_C), ptr, prop, &item_array, nullptr, &free);
977 }
978
979 /* We don't want nested rows, cols in menus. */
980 Layout *layout_radial = nullptr;
981 if (radial) {
982 if (layout->root()->layout == layout) {
983 layout_radial = &layout->menu_pie();
984 block_layout_set_current(block, layout_radial);
985 }
986 else {
987 if (layout->type() == ItemType::LayoutRadial) {
988 layout_radial = layout;
989 }
990 block_layout_set_current(block, layout);
991 }
992 }
994 layout->root()->type == LayoutType::Menu)
995 {
996 block_layout_set_current(block, layout);
997 }
998 else {
999 block_layout_set_current(block, item_local_sublayout(layout, layout, true));
1000 }
1001
1002 if (!item_array->identifier) {
1003 item_disabled(layout, IFACE_("Empty list"));
1004 }
1005
1006 for (const EnumPropertyItem *item = item_array; item->identifier; item++) {
1007 const bool is_first = item == item_array;
1008
1009 if (!item->identifier[0]) {
1010 const EnumPropertyItem *next_item = item + 1;
1011
1012 /* Separate items, potentially with a label. */
1013 if (next_item->identifier) {
1014 /* Item without identifier but with name:
1015 * Add group label for the following items. */
1016 if (item->name) {
1017 if (!is_first) {
1018 block->curlayout->separator();
1019 }
1020 block->curlayout->label(item->name, item->icon);
1021 }
1022 else if (radial && layout_radial) {
1023 layout_radial->separator();
1024 }
1025 else {
1026 block->curlayout->separator();
1027 }
1028 }
1029 continue;
1030 }
1031
1033 layout, block, ptr, prop, uiname, h, but_type, icon_only, item, is_first);
1034 }
1035
1036 block_layout_set_current(block, layout);
1037
1038 if (free) {
1039 MEM_delete(item_array);
1040 }
1041}
1042static void item_enum_expand(Layout *layout,
1043 Block *block,
1044 PointerRNA *ptr,
1045 PropertyRNA *prop,
1046 const std::optional<StringRef> uiname,
1047 const int h,
1048 const bool icon_only)
1049{
1050 item_enum_expand_exec(layout, block, ptr, prop, uiname, h, ButtonType::Row, icon_only);
1051}
1052static void item_enum_expand_tabs(Layout *layout,
1053 bContext *C,
1054 Block *block,
1055 PointerRNA *ptr,
1056 PropertyRNA *prop,
1057 PointerRNA *ptr_highlight,
1058 PropertyRNA *prop_highlight,
1059 const std::optional<StringRef> uiname,
1060 const int h,
1061 const bool icon_only,
1062 EnumTabExpand expand_as)
1063{
1064 const int start_size = block->buttons_ptrs.size();
1065
1066 item_enum_expand_exec(layout,
1067 block,
1068 ptr,
1069 prop,
1070 uiname,
1071 h,
1073 icon_only);
1074
1075 if (block->buttons_ptrs.is_empty()) {
1076 return;
1077 }
1078
1079 BLI_assert(start_size != block->buttons_ptrs.size());
1080
1081 if (expand_as == EnumTabExpand::Default) {
1082 for (Button &tab : block->buttons() | std::views::drop(start_size)) {
1084 if (icon_only) {
1086 }
1087 }
1088 }
1089
1090 const bool use_custom_highlight = (prop_highlight != nullptr);
1091
1092 if (use_custom_highlight) {
1093 const int highlight_array_len = RNA_property_array_length(ptr_highlight, prop_highlight);
1094 Array<bool, 64> highlight_array(highlight_array_len);
1095 RNA_property_boolean_get_array(ptr_highlight, prop_highlight, highlight_array.data());
1096 const int end = std::min<int>(start_size + highlight_array_len, block->buttons_ptrs.size());
1097 for (int i = start_size; i < end; i++) {
1098 Button *tab_but = block->buttons_ptrs[i].get();
1099 SET_FLAG_FROM_TEST(tab_but->flag, !highlight_array[i - start_size], BUT_INACTIVE);
1100 }
1101 }
1102}
1103
1104/* callback for keymap item change button */
1105static void keymap_but_cb(bContext * /*C*/, void *but_v, void * /*key_v*/)
1106{
1107 Button *but = static_cast<Button *>(but_v);
1109 const ButtonHotkeyEvent *hotkey_but = static_cast<ButtonHotkeyEvent *>(but);
1110
1112 &but->rnapoin, "shift", (hotkey_but->modifier_key & KM_SHIFT) ? KM_MOD_HELD : KM_NOTHING);
1114 &but->rnapoin, "ctrl", (hotkey_but->modifier_key & KM_CTRL) ? KM_MOD_HELD : KM_NOTHING);
1116 &but->rnapoin, "alt", (hotkey_but->modifier_key & KM_ALT) ? KM_MOD_HELD : KM_NOTHING);
1118 &but->rnapoin, "oskey", (hotkey_but->modifier_key & KM_OSKEY) ? KM_MOD_HELD : KM_NOTHING);
1120 &but->rnapoin, "hyper", (hotkey_but->modifier_key & KM_HYPER) ? KM_MOD_HELD : KM_NOTHING);
1121}
1122
1133 Block *block,
1134 const StringRef name,
1135 const int icon,
1136 PointerRNA *ptr,
1137 PropertyRNA *prop,
1138 const int index,
1139 const int x,
1140 const int y,
1141 const int w_hint,
1142 const int h,
1143 const int flag,
1144 std::optional<ButtonType> button_type_override,
1145 const char *caller_fn_name)
1146{
1147 Layout *sub = layout;
1148 int prop_but_width = w_hint;
1149#ifdef UI_PROP_DECORATE
1150 Layout *layout_prop_decorate = nullptr;
1151 const bool use_prop_sep = layout->use_property_split();
1152 const bool use_prop_decorate = use_prop_sep && layout->use_property_decorate() &&
1154#endif
1155
1156 const bool is_keymapitem_ptr = RNA_struct_is_a(ptr->type, RNA_KeyMapItem);
1157 if ((flag & ITEM_R_FULL_EVENT) && !is_keymapitem_ptr) {
1158 RNA_warning_bare("%s: Data is not a keymap item struct: %s. Ignoring 'full_event' option.",
1159 caller_fn_name,
1161 }
1162
1163 block_layout_set_current(block, layout);
1164
1165 /* Only add new row if more than 1 item will be added. */
1166 if (!name.is_empty()
1167#ifdef UI_PROP_DECORATE
1168 || use_prop_decorate
1169#endif
1170 )
1171 {
1172 /* Also avoid setting 'align' if possible. Set the space to zero instead as aligning a large
1173 * number of labels can end up aligning thousands of buttons when displaying key-map search (a
1174 * heavy operation), see: #78636. */
1175 sub = &layout->row(layout->align());
1177 }
1178
1179 if (!name.is_empty()) {
1180#ifdef UI_PROP_DECORATE
1181 if (use_prop_sep) {
1182 layout_prop_decorate = uiItemL_respect_property_split(layout, name, ICON_NONE);
1183 }
1184 else
1185#endif
1186 {
1187 int w_label;
1188 if (layout_variable_size(layout)) {
1189 /* In this case, a pure label without additional padding.
1190 * Use a default width for property button(s). */
1191 prop_but_width = UI_UNIT_X * 5;
1192 w_label = text_icon_width_ex(layout, name, ICON_NONE, text_pad_none, UI_FSTYLE_WIDGET);
1193 }
1194 else {
1195 w_label = w_hint / 3;
1196 }
1197 uiDefBut(block, ButtonType::Label, name, x, y, w_label, h, nullptr, 0.0, 0.0, "");
1198 }
1199 }
1200
1201 const PropertyType type = RNA_property_type(prop);
1202 const PropertySubType subtype = RNA_property_subtype(prop);
1203
1204 Button *but;
1205 if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)) {
1206 block_layout_set_current(block, &sub->row(true));
1207 but = uiDefAutoButR(block,
1208 ptr,
1209 prop,
1210 index,
1211 "",
1212 icon,
1213 x,
1214 y,
1215 prop_but_width - UI_UNIT_X,
1216 h,
1217 button_type_override);
1218
1219 if (but != nullptr) {
1220 if (ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)) {
1222 if (BLI_path_is_rel(but->drawstr.c_str())) {
1224 }
1225 }
1226 }
1227 }
1228
1229 if ((flag & ITEM_R_PATH_NO_OPEN_BUTTON) == 0) {
1230 /* #BUTTONS_OT_file_browse calls #context_active_but_prop_get_filebrowser. */
1231 uiDefIconButO(block,
1233 subtype == PROP_DIRPATH ? "BUTTONS_OT_directory_browse" :
1234 "BUTTONS_OT_file_browse",
1236 RNA_property_editable(ptr, prop) ? ICON_FILEBROWSER : ICON_FOLDER_REDIRECT,
1237 x,
1238 y,
1239 UI_UNIT_X,
1240 h,
1241 std::nullopt);
1242 }
1243 }
1244 else if (flag & ITEM_R_EVENT) {
1245 but = uiDefButR_prop(block,
1247 name,
1248 x,
1249 y,
1250 prop_but_width,
1251 h,
1252 ptr,
1253 prop,
1254 index,
1255 0,
1256 0,
1257 std::nullopt);
1258 }
1259 else if ((flag & ITEM_R_FULL_EVENT) && is_keymapitem_ptr) {
1260 std::string kmi_str =
1261 WM_keymap_item_to_string(static_cast<const wmKeyMapItem *>(ptr->data), false).value_or("");
1262
1263 but = uiDefButR_prop(block,
1265 kmi_str,
1266 x,
1267 y,
1268 prop_but_width,
1269 h,
1270 ptr,
1271 prop,
1272 0,
1273 0,
1274 0,
1275 std::nullopt);
1276 button_func_set(but, keymap_but_cb, but, nullptr);
1277 }
1278 else {
1279 const std::optional<StringRefNull> str = (type == PROP_ENUM && !(flag & ITEM_R_ICON_ONLY)) ?
1280 std::nullopt :
1281 std::make_optional<StringRefNull>("");
1282 but = uiDefAutoButR(
1283 block, ptr, prop, index, str, icon, x, y, prop_but_width, h, button_type_override);
1284 if (flag & ITEM_R_TEXT_RIGHT) {
1285 but->drawflag |= BUT_TEXT_RIGHT;
1286 but->drawflag &= ~BUT_TEXT_LEFT;
1287 }
1288 }
1289
1290 /* Highlight in red on path template validity errors. */
1291 if (but != nullptr && ELEM(but->type, ButtonType::Text)) {
1292 /* We include PROP_NONE here because some plain string properties are used
1293 * as parts of paths. For example, the sub-paths in the compositor's File
1294 * Output node. */
1297 const std::string path = RNA_property_string_get(ptr, prop);
1299 const std::optional<bke::path_templates::VariableMap> variables =
1301 static_cast<const bContext *>(block->evil_C), ptr, prop);
1302 BLI_assert(variables.has_value());
1303
1304 if (!BKE_path_validate_template(path, *variables).is_empty()) {
1306 }
1307 }
1308 }
1309 }
1310 }
1311
1312 if (flag & ITEM_R_IMMEDIATE) {
1314 }
1315
1316#ifdef UI_PROP_DECORATE
1317 /* Only for alignment. */
1318 if (use_prop_decorate) { /* Note that sep flag may have been unset meanwhile. */
1319 (layout_prop_decorate ? layout_prop_decorate : sub)->label(nullptr, ICON_BLANK1);
1320 }
1321#endif /* UI_PROP_DECORATE */
1322
1323 block_layout_set_current(block, layout);
1324 return but;
1325}
1326
1328 PointerRNA *r_ptr,
1329 PropertyRNA **r_prop,
1330 bool *r_is_undo,
1331 bool *r_is_userdef)
1332{
1334 Button *prevbut = nullptr;
1335
1336 *r_ptr = {};
1337 *r_prop = nullptr;
1338 *r_is_undo = false;
1339 *r_is_userdef = false;
1340
1341 if (!region) {
1342 return;
1343 }
1344
1345 for (Block &block : region->runtime->uiblocks) {
1346 for (Button &but : block.buttons()) {
1347 if (but.rnapoin) {
1348 if (RNA_property_type(but.rnaprop) == PROP_STRING) {
1349 prevbut = &but;
1350 }
1351 }
1352 /* find the button before the active one */
1353 if ((but.flag & BUT_LAST_ACTIVE) && prevbut) {
1354 *r_ptr = prevbut->rnapoin;
1355 *r_prop = prevbut->rnaprop;
1356 *r_is_undo = (prevbut->flag & BUT_UNDO) != 0;
1357 *r_is_userdef = button_is_userdef(prevbut);
1358 return;
1359 }
1360 }
1361 }
1362}
1363
1365
1366/* -------------------------------------------------------------------- */
1369
1373static void but_tip_from_enum_item(Button *but, const EnumPropertyItem *item)
1374{
1375 if (but->tip == nullptr || but->tip[0] == '\0') {
1376 if (item->description && item->description[0] &&
1377 !(but->optype && but->optype->get_description))
1378 {
1379 but->tip = item->description;
1380 }
1381 }
1382}
1383
1384/* disabled item */
1385static void item_disabled(Layout *layout, const char *name)
1386{
1387 Block *block = layout->block();
1388
1389 block_layout_set_current(block, layout);
1390
1391 if (!name) {
1392 name = "";
1393 }
1394
1395 const int w = text_icon_width(layout, name, 0, false);
1396
1397 Button *but = uiDefBut(
1398 block, ButtonType::Label, name, 0, 0, w, UI_UNIT_Y, nullptr, 0.0, 0.0, "");
1399 button_disable(but, "");
1400}
1401
1408 wmOperatorType *ot,
1409 std::optional<StringRef> name,
1410 int icon,
1411 const wm::OpCallContext context,
1412 const eUI_Item_Flag flag,
1413 PointerRNA *r_opptr)
1414{
1415 /* Take care to fill 'r_opptr' whatever happens. */
1416 Block *block = layout->block();
1417
1418 std::string operator_name;
1419 if (!name) {
1420 if (ot && ot->srna && (flag & ITEM_R_ICON_ONLY) == 0) {
1421 operator_name = WM_operatortype_name(ot, nullptr);
1422 name = operator_name.c_str();
1423 }
1424 else {
1425 name = "";
1426 }
1427 }
1428
1429 if (layout->root()->type == LayoutType::Menu && !icon) {
1430 icon = ICON_BLANK1;
1431 }
1432
1433 block_layout_set_current(block, layout);
1435
1436 const int w = text_icon_width(layout, *name, icon, false);
1437
1438 const EmbossType prev_emboss = layout->emboss_or_undefined();
1439 if (flag & ITEM_R_NO_BG) {
1441 }
1442
1443 /* create the button */
1444 Button *but;
1445 if (icon) {
1446 if (!name->is_empty()) {
1448 block, ButtonType::But, ot, context, icon, *name, 0, 0, w, UI_UNIT_Y, std::nullopt);
1449 }
1450 else {
1451 but = uiDefIconButO_ptr(
1452 block, ButtonType::But, ot, context, icon, 0, 0, w, UI_UNIT_Y, std::nullopt);
1453 }
1454 }
1455 else {
1456 but = uiDefButO_ptr(
1457 block, ButtonType::But, ot, context, *name, 0, 0, w, UI_UNIT_Y, std::nullopt);
1458 }
1459
1460 BLI_assert(but->optype != nullptr);
1461
1462 if (flag & ITEM_R_NO_BG) {
1463 layout->emboss_set(prev_emboss);
1464 }
1465
1466 if (flag & ITEM_O_DEPRESS) {
1467 but->flag |= UI_SELECT_DRAW;
1468 }
1469
1470 if (flag & ITEM_R_ICON_ONLY) {
1472 }
1473
1474 if (layout->red_alert()) {
1476 }
1477
1478 if (layout->active_default()) {
1480 }
1481
1482 /* assign properties */
1483 if (r_opptr) {
1485 opptr->data = bke::idprop::create_group("wmOperatorProperties").release();
1486 *r_opptr = *opptr;
1487 }
1488
1489 return but;
1490}
1491
1492static void item_menu_hold(bContext *C, ARegion *butregion, Button *but)
1493{
1494 PopupMenu *pup = popup_menu_begin(C, "", ICON_NONE);
1495 Layout *layout = popup_menu_layout(pup);
1496 Block *block = layout->block();
1497 popup_menu_but_set(pup, butregion, but);
1498
1499 block->flag |= BLOCK_POPUP_HOLD;
1500
1501 char direction = UI_DIR_DOWN;
1502 if (but->drawstr.empty()) {
1503 switch (RGN_ALIGN_ENUM_FROM_MASK(butregion->alignment)) {
1504 case RGN_ALIGN_LEFT:
1505 direction = UI_DIR_RIGHT;
1506 break;
1507 case RGN_ALIGN_RIGHT:
1508 direction = UI_DIR_LEFT;
1509 break;
1510 case RGN_ALIGN_BOTTOM:
1511 direction = UI_DIR_UP;
1512 break;
1513 default:
1514 direction = UI_DIR_DOWN;
1515 break;
1516 }
1517 }
1518 block_direction_set(block, direction);
1519
1520 const char *menu_id = static_cast<const char *>(but->hold_argN);
1521 MenuType *mt = WM_menutype_find(menu_id, true);
1522 if (mt) {
1523 layout->context_set_from_but(but);
1524 menutype_draw(C, mt, layout);
1525 }
1526 else {
1527 layout->label(RPT_("Menu Missing:"), ICON_NONE);
1528 layout->label(menu_id, ICON_NONE);
1529 }
1530 popup_menu_end(C, pup);
1531}
1532
1534 std::optional<StringRef> name,
1535 const int icon,
1537 const eUI_Item_Flag flag)
1538{
1539 PointerRNA ptr;
1540 uiItemFullO_ptr_ex(this, ot, name, icon, context, flag, &ptr);
1541 return ptr;
1542}
1543
1545 std::optional<StringRef> name,
1546 int icon,
1548 const eUI_Item_Flag flag,
1549 const char *menu_id)
1550{
1551 PointerRNA ptr;
1552 Button *but = uiItemFullO_ptr_ex(this, ot, name, icon, context, flag, &ptr);
1554 return ptr;
1555}
1556
1558 const std::optional<StringRef> name,
1559 int icon,
1561 const eUI_Item_Flag flag)
1562{
1563 wmOperatorType *ot = WM_operatortype_find(opname.c_str(), false); /* print error next */
1564 UI_OPERATOR_ERROR_RET(ot, opname.c_str(), "UILayout.operator()");
1565 return this->op(ot, name, icon, context, flag);
1566}
1567
1569{
1570 return (layout->type() == ItemType::LayoutRadial) ||
1571 ((layout->type() == ItemType::LayoutRoot) &&
1572 (layout->root()->type == LayoutType::PieMenu));
1573}
1574
1576 const PointerRNA &ptr,
1578 IDProperty *properties,
1580 eUI_Item_Flag flag,
1581 const EnumPropertyItem *item_array,
1582 int totitem,
1583 int active)
1584{
1585 const StringRefNull propname = RNA_property_identifier(prop);
1587 RNA_warning_bare("UILayout.operator_enum_items(): %s.%s, not an enum type",
1589 propname.c_str());
1590 return;
1591 }
1592
1593 Layout *target, *split = nullptr;
1594 Block *block = this->block();
1595 const bool radial = layout_is_radial(this);
1596
1597 if (radial) {
1598 target = &this->menu_pie();
1599 }
1600 else if ((this->local_direction() == LayoutDirection::Horizontal) && (flag & ITEM_R_ICON_ONLY)) {
1601 target = this;
1603
1604 /* Add a blank button to the beginning of the row. */
1607 ICON_BLANK1,
1608 0,
1609 0,
1610 1.25f * UI_UNIT_X,
1611 UI_UNIT_Y,
1612 nullptr,
1613 0,
1614 0,
1615 std::nullopt);
1616 }
1617 else {
1618 split = &this->split(0.0f, false);
1619 target = &split->column(this->align());
1620 }
1621
1622 bool last_iter = false;
1623 const EnumPropertyItem *item = item_array;
1624 for (int i = 1; item->identifier && !last_iter; i++, item++) {
1625 /* Handle over-sized pies. */
1626 if (radial && (totitem > PIE_MAX_ITEMS) && (i >= PIE_MAX_ITEMS)) {
1627 if (item->name) { /* only visible items */
1628 const EnumPropertyItem *tmp;
1629
1630 /* Check if there are more visible items for the next level. If not, we don't
1631 * add a new level and add the remaining item instead of the 'more' button. */
1632 for (tmp = item + 1; tmp->identifier; tmp++) {
1633 if (tmp->name) {
1634 break;
1635 }
1636 }
1637
1638 if (tmp->identifier) { /* only true if loop above found item and did early-exit */
1640 block, ot, propname, properties, item_array, totitem, context, flag);
1641 /* break since rest of items is handled in new pie level */
1642 break;
1643 }
1644 last_iter = true;
1645 }
1646 else {
1647 continue;
1648 }
1649 }
1650
1651 if (item->identifier[0]) {
1652 PointerRNA tptr = target->op(
1653 ot, (flag & ITEM_R_ICON_ONLY) ? nullptr : item->name, item->icon, context, flag);
1654 if (properties) {
1655 IDP_CopyPropertyContent(tptr.data_as<IDProperty>(), properties);
1656 }
1657 RNA_property_enum_set(&tptr, prop, item->value);
1658
1659 Button *but = block->buttons_ptrs.last().get();
1660
1661 if (active == (i - 1)) {
1662 but->flag |= UI_SELECT_DRAW;
1663 }
1664
1665 but_tip_from_enum_item(but, item);
1666 }
1667 else {
1668 if (item->name) {
1669 if (item != item_array && !radial && split != nullptr) {
1670 target = &split->column(this->align());
1671 }
1672
1673 Button *but;
1674 if (item->icon || radial) {
1675 target->label(item->name, item->icon);
1676
1677 but = block->buttons_ptrs.last().get();
1678 }
1679 else {
1680 /* Do not use Layout::label here, as our root layout is a menu one,
1681 * it will add a fake blank icon! */
1682 but = uiDefBut(block,
1684 item->name,
1685 0,
1686 0,
1687 UI_UNIT_X * 5,
1688 UI_UNIT_Y,
1689 nullptr,
1690 0.0,
1691 0.0,
1692 "");
1693 target->separator();
1694 }
1695 but_tip_from_enum_item(but, item);
1696 }
1697 else {
1698 if (radial) {
1699 /* invisible dummy button to ensure all items are
1700 * always at the same position */
1701 target->separator();
1702 }
1703 else {
1704 /* XXX bug here, columns draw bottom item badly */
1705 target->separator();
1706 }
1707 }
1708 }
1709 }
1710}
1711
1713 const StringRefNull propname,
1714 IDProperty *properties,
1716 eUI_Item_Flag flag,
1717 const int active)
1718{
1719 wmOperatorType *ot = WM_operatortype_find(opname.c_str(), false); /* print error next */
1720
1721 if (!ot || !ot->srna) {
1722 item_disabled(this, opname.c_str());
1723 RNA_warning_bare("UILayout.operator_enum(): %s '%s'",
1724 ot ? "operator missing srna" : "unknown operator",
1725 opname.c_str());
1726 return;
1727 }
1728
1730 /* so the context is passed to itemf functions (some need it) */
1732 PropertyRNA *prop = RNA_struct_find_property(&ptr, propname.c_str());
1733
1734 /* don't let bad properties slip through */
1735 BLI_assert((prop == nullptr) || (RNA_property_type(prop) == PROP_ENUM));
1736
1737 Block *block = this->block();
1738 if (prop && RNA_property_type(prop) == PROP_ENUM) {
1739 const EnumPropertyItem *item_array = nullptr;
1740 int totitem;
1741 bool free;
1742
1743 if (layout_is_radial(this)) {
1744 /* XXX: While "_all()" guarantees spatial stability,
1745 * it's bad when an enum has > 8 items total,
1746 * but only a small subset will ever be shown at once
1747 * (e.g. Mode Switch menu, after the introduction of GP editing modes).
1748 */
1749#if 0
1751 static_cast<bContext *>(block->evil_C), &ptr, prop, &item_array, &totitem, &free);
1752#else
1754 static_cast<bContext *>(block->evil_C), &ptr, prop, &item_array, &totitem, &free);
1755#endif
1756 }
1757 else {
1758 bContext *C = static_cast<bContext *>(block->evil_C);
1759 const bContextStore *previous_ctx = CTX_store_get(C);
1762 CTX_store_set(C, previous_ctx);
1763 }
1764
1765 /* add items */
1766 this->op_enum_items(ot, ptr, prop, properties, context, flag, item_array, totitem, active);
1767
1768 if (free) {
1769 MEM_delete(item_array);
1770 }
1771 }
1772 else if (prop && RNA_property_type(prop) != PROP_ENUM) {
1773 RNA_warning_bare("UILayout.operator_enum() %s.%s, not an enum type",
1775 propname.c_str());
1776 return;
1777 }
1778 else {
1779 RNA_warning_bare("UILayout.operator_enum() %s.%s not found",
1781 propname.c_str());
1782 return;
1783 }
1784}
1785
1786void Layout::op_enum(const StringRefNull opname, const StringRefNull propname)
1787{
1788 this->op_enum(opname, propname, nullptr, root_->opcontext, UI_ITEM_NONE);
1789}
1790
1791PointerRNA Layout::op(wmOperatorType *ot, const std::optional<StringRef> name, int icon)
1792{
1793 return this->op(ot, name, icon, root_->opcontext, UI_ITEM_NONE);
1794}
1795
1796PointerRNA Layout::op(const StringRefNull opname, const std::optional<StringRef> name, int icon)
1797{
1798 return this->op(opname, name, icon, root_->opcontext, UI_ITEM_NONE);
1799}
1800
1801/* RNA property items */
1802
1803static void item_rna_size(Layout *layout,
1804 StringRef name,
1805 int icon,
1806 PointerRNA *ptr,
1807 PropertyRNA *prop,
1808 int index,
1809 bool icon_only,
1810 bool compact,
1811 int *r_w,
1812 int *r_h)
1813{
1814 int w = 0, h;
1815
1816 /* arbitrary extended width by type */
1817 const PropertyType type = RNA_property_type(prop);
1818 const PropertySubType subtype = RNA_property_subtype(prop);
1819 const int len = RNA_property_array_length(ptr, prop);
1820
1821 bool is_checkbox_only = false;
1822 if (name.is_empty() && !icon_only) {
1823 if (ELEM(type, PROP_STRING, PROP_POINTER)) {
1824 name = "non-empty text";
1825 }
1826 else if (type == PROP_BOOLEAN) {
1827 if (icon == ICON_NONE) {
1828 /* Exception for check-boxes, they need a little less space to align nicely. */
1829 is_checkbox_only = true;
1830 }
1831 icon = ICON_DOT;
1832 }
1833 else if (type == PROP_ENUM) {
1834 /* Find the longest enum item name, instead of using a dummy text! */
1836 bool free;
1837 RNA_property_enum_items_gettexted(static_cast<bContext *>(layout->block()->evil_C),
1838 ptr,
1839 prop,
1840 &item_array,
1841 nullptr,
1842 &free);
1843
1844 for (const EnumPropertyItem *item = item_array; item->identifier; item++) {
1845 if (item->identifier[0]) {
1846 w = max_ii(w, text_icon_width(layout, item->name, item->icon, compact));
1847 }
1848 }
1849 if (free) {
1850 MEM_delete(item_array);
1851 }
1852 }
1853 }
1854
1855 if (!w) {
1856 if (type == PROP_ENUM && icon_only) {
1857 w = text_icon_width(layout, "", ICON_BLANK1, compact);
1858 if (index != RNA_ENUM_VALUE) {
1859 w += 0.6f * UI_UNIT_X;
1860 }
1861 }
1862 else {
1863 /* not compact for float/int buttons, looks too squashed */
1864 w = text_icon_width(layout, name, icon, ELEM(type, PROP_FLOAT, PROP_INT) ? false : compact);
1865 }
1866 }
1867 h = UI_UNIT_Y;
1868
1869 /* increase height for arrays */
1870 if (index == RNA_NO_INDEX && len > 0) {
1871 if (name.is_empty() && icon == ICON_NONE) {
1872 h = 0;
1873 }
1874 if (layout->use_property_split()) {
1875 h = 0;
1876 }
1877 if (ELEM(subtype, PROP_LAYER, PROP_LAYER_MEMBER)) {
1878 h += 2 * UI_UNIT_Y;
1879 }
1880 else if (subtype == PROP_MATRIX) {
1881 int dim_size[/*RNA_MAX_ARRAY_DIMENSION*/ 3];
1882 RNA_property_array_dimension(ptr, prop, dim_size);
1883 h += dim_size[0] * UI_UNIT_Y;
1884 }
1885 else {
1886 h += len * UI_UNIT_Y;
1887 }
1888 }
1889
1890 /* Increase width requirement if in a variable size layout. */
1891 if (layout_variable_size(layout)) {
1892 if (type == PROP_BOOLEAN && !name.is_empty()) {
1893 w += UI_UNIT_X / 5;
1894 }
1895 else if (is_checkbox_only) {
1896 w -= UI_UNIT_X / 4;
1897 }
1898 else if (type == PROP_ENUM && !icon_only) {
1899 w += UI_UNIT_X / 4;
1900 }
1901 else if (ELEM(type, PROP_FLOAT, PROP_INT)) {
1902 w += UI_UNIT_X * 3;
1903 }
1904 }
1905
1906 *r_w = w;
1907 *r_h = h;
1908}
1909
1911{
1912 const bool is_array = RNA_property_array_check(prop);
1913 const int subtype = RNA_property_subtype(prop);
1914 return is_array && (index == RNA_NO_INDEX) &&
1915 ((item_flag & ITEM_R_EXPAND) ||
1917}
1918
1927{
1928 for (Layout *parent = cur_layout; parent; parent = parent->parent()) {
1929 if (!parent->heading().is_empty()) {
1930 return parent;
1931 }
1932 }
1933
1934 return nullptr;
1935}
1936
1938 Layout *heading_layout,
1939 bool right_align,
1940 bool respect_prop_split)
1941{
1942 const LayoutAlign prev_alignment = layout->alignment();
1943
1944 if (right_align) {
1946 }
1947
1948 if (respect_prop_split) {
1949 uiItemL_respect_property_split(layout, heading_layout->heading(), ICON_NONE);
1950 }
1951 else {
1952 layout->label(heading_layout->heading(), ICON_NONE);
1953 }
1954 /* After adding the heading label, we have to mark it somehow as added, so it's not added again
1955 * for other items in this layout. For now just clear it. */
1956 heading_layout->heading_reset();
1957
1958 layout->alignment_set(prev_alignment);
1959}
1960
1967{
1968 /* Tag item as using property split layout, this is inherited to children so they can get special
1969 * treatment if needed. */
1970 ItemInternal::inside_property_split_set(layout_parent, true);
1971
1972 if (layout_parent->type() == ItemType::LayoutRow) {
1973 /* Prevent further splits within the row. */
1974 layout_parent->use_property_split_set(false);
1975
1976 layout_parent->child_items_layout_ = &layout_split->row(true);
1977 return layout_parent->child_items_layout_;
1978 }
1979 return layout_split;
1980}
1981
1984 int index,
1985 int value,
1986 eUI_Item_Flag flag,
1987 const std::optional<StringRef> name_opt,
1988 int icon,
1989 const std::optional<StringRef> placeholder)
1990{
1991
1992 Block *block = this->block();
1993 char namestr[UI_MAX_NAME_STR];
1994 const bool use_prop_sep = this->use_property_split();
1995 const bool inside_prop_sep = flag_is_set(flag_, ItemInternalFlag::InsidePropSep);
1996 /* Columns can define a heading to insert. If the first item added to a split layout doesn't have
1997 * a label to display in the first column, the heading is inserted there. Otherwise it's inserted
1998 * as a new row before the first item. */
1999 Layout *heading_layout = layout_heading_find(this);
2000 /* Although check-boxes use the split layout, they are an exception and should only place their
2001 * label in the second column, to not make that almost empty.
2002 *
2003 * Keep using 'use_prop_sep' instead of disabling it entirely because
2004 * we need the ability to have decorators still. */
2005 bool use_prop_sep_split_label = use_prop_sep;
2006 bool use_split_empty_name = (flag & ITEM_R_SPLIT_EMPTY_NAME);
2007
2008#ifdef UI_PROP_DECORATE
2009 struct DecorateInfo {
2010 bool use_prop_decorate;
2011 int len;
2012 Layout *layout;
2013 Button *but;
2014 };
2015 DecorateInfo ui_decorate{};
2016 ui_decorate.use_prop_decorate = this->use_property_decorate() && use_prop_sep;
2017
2018#endif /* UI_PROP_DECORATE */
2019
2022
2023 /* retrieve info */
2025 const bool is_array = RNA_property_array_check(prop);
2026 const int len = (is_array) ? RNA_property_array_length(ptr, prop) : 0;
2027 const bool is_id_name_prop = (ptr->owner_id == ptr->data && type == PROP_STRING &&
2029
2030 const bool icon_only = (flag & ITEM_R_ICON_ONLY) != 0;
2031
2032 /* Boolean with -1 to signify that the value depends on the presence of an icon. */
2033 const int toggle = ((flag & ITEM_R_TOGGLE) ? 1 : ((flag & ITEM_R_ICON_NEVER) ? 0 : -1));
2034 const bool no_icon = (toggle == 0);
2035
2036 /* set name and icon */
2037 StringRef name = name_opt.value_or(icon_only ? "" : RNA_property_ui_name(prop));
2038
2039 if (type != PROP_BOOLEAN) {
2041 }
2042
2043 if (flag & ITEM_R_ICON_ONLY) {
2044 /* pass */
2045 }
2047 if (use_prop_sep == false) {
2048 name = item_name_add_colon(name, namestr);
2049 }
2050 }
2051 else if (type == PROP_BOOLEAN && is_array && index == RNA_NO_INDEX) {
2052 if (use_prop_sep == false) {
2053 name = item_name_add_colon(name, namestr);
2054 }
2055 }
2056 else if (type == PROP_ENUM && index != RNA_ENUM_VALUE) {
2057 if (flag & ITEM_R_COMPACT) {
2058 name = "";
2059 }
2060 else {
2061 if (use_prop_sep == false) {
2062 name = item_name_add_colon(name, namestr);
2063 }
2064 }
2065 }
2066
2067 if (no_icon == false) {
2068 if (icon == ICON_NONE) {
2069 icon = RNA_property_ui_icon(prop);
2070 }
2071
2072 /* Menus and pie-menus don't show checkbox without this. */
2073 if ((root_->type == LayoutType::Menu) ||
2074 /* Use check-boxes only as a fallback in pie-menu's, when no icon is defined. */
2075 ((root_->type == LayoutType::PieMenu) && (icon == ICON_NONE)))
2076 {
2077 const int prop_flag = RNA_property_flag(prop);
2078 if (type == PROP_BOOLEAN) {
2079 if ((is_array == false) || (index != RNA_NO_INDEX)) {
2080 if (prop_flag & PROP_ICONS_CONSECUTIVE) {
2081 icon = ICON_CHECKBOX_DEHLT; /* but->iconadd will set to correct icon */
2082 }
2083 else if (is_array) {
2084 icon = RNA_property_boolean_get_index(ptr, prop, index) ? ICON_CHECKBOX_HLT :
2085 ICON_CHECKBOX_DEHLT;
2086 }
2087 else {
2088 icon = RNA_property_boolean_get(ptr, prop) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
2089 }
2090 }
2091 }
2092 else if (type == PROP_ENUM) {
2093 if (index == RNA_ENUM_VALUE) {
2094 const int enum_value = RNA_property_enum_get(ptr, prop);
2095 if (prop_flag & PROP_ICONS_CONSECUTIVE) {
2096 icon = ICON_CHECKBOX_DEHLT; /* but->iconadd will set to correct icon */
2097 }
2098 else if (prop_flag & PROP_ENUM_FLAG) {
2099 icon = (enum_value & value) ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
2100 }
2101 else {
2102 /* Only a single value can be chosen, so display as radio buttons. */
2103 icon = (enum_value == value) ? ICON_RADIOBUT_ON : ICON_RADIOBUT_OFF;
2104 }
2105 }
2106 }
2107 }
2108 }
2109
2110#ifdef UI_PROP_SEP_ICON_WIDTH_EXCEPTION
2111 if (use_prop_sep) {
2112 if (type == PROP_BOOLEAN && (icon == ICON_NONE) && !icon_only) {
2113 use_prop_sep_split_label = false;
2114 /* For check-boxes we make an exception: We allow showing them in a split row even without
2115 * label. It typically relates to its neighbor items, so no need for an extra label. */
2116 use_split_empty_name = true;
2117 }
2118 }
2119#endif
2120
2122 flag |= ITEM_R_EXPAND;
2123 }
2124
2125 const bool slider = (flag & ITEM_R_SLIDER) != 0;
2126 const bool expand = (flag & ITEM_R_EXPAND) != 0;
2127 const bool no_bg = (flag & ITEM_R_NO_BG) != 0;
2128 const bool compact = (flag & ITEM_R_COMPACT) != 0;
2129
2130 /* get size */
2131 int w, h;
2132 item_rna_size(this, name, icon, ptr, prop, index, icon_only, compact, &w, &h);
2133
2134 const EmbossType prev_emboss = emboss_;
2135 if (no_bg) {
2137 }
2138
2139 Button *but = nullptr;
2140
2141 /* Split the label / property. */
2142 Layout *layout_parent = this;
2143 Layout *layout = this;
2144 if (use_prop_sep) {
2145 Layout *layout_row = nullptr;
2146#ifdef UI_PROP_DECORATE
2147 if (ui_decorate.use_prop_decorate) {
2148 layout_row = &layout->row(true);
2149 layout_row->space_ = 0;
2150 ui_decorate.len = max_ii(1, len);
2151 }
2152#endif /* UI_PROP_DECORATE */
2153
2154 if (name.is_empty() && !use_split_empty_name) {
2155 /* Ensure we get a column when text is not set. */
2156 layout = &(layout_row ? layout_row : layout)->column(true);
2157 layout->space_ = 0;
2158 if (heading_layout) {
2159 layout_heading_label_add(layout, heading_layout, false, false);
2160 }
2161 }
2162 else {
2163 Layout *layout_split =
2164 &(layout_row ? layout_row : layout)->split(Layout::PROPERTY_SPLIT_FACTOR, true);
2165 bool label_added = false;
2166 Layout *layout_sub = &layout_split->column(true);
2167 layout_sub->space_ = 0;
2168
2169 if (!RNA_property_editable(ptr, prop)) {
2170 layout_sub->enabled_set(false);
2171 }
2172
2173 if (!use_prop_sep_split_label) {
2174 /* Pass */
2175 }
2176 else if (item_rna_is_expand(prop, index, flag)) {
2177 fmt::memory_buffer name_with_suffix;
2178 char str[2] = {'\0'};
2179 for (int a = 0; a < len; a++) {
2181 const bool use_prefix = (a == 0 && !name.is_empty());
2182 if (use_prefix) {
2183 fmt::format_to(fmt::appender(name_with_suffix), "{} {}", name, str[0]);
2184 }
2185 but = uiDefBut(block,
2187 use_prefix ? StringRef(name_with_suffix.data(), name_with_suffix.size()) :
2188 str,
2189 0,
2190 0,
2191 w,
2192 UI_UNIT_Y,
2193 nullptr,
2194 0.0,
2195 0.0,
2196 "");
2197 but->drawflag |= BUT_TEXT_RIGHT;
2198 but->drawflag &= ~BUT_TEXT_LEFT;
2199
2200 label_added = true;
2201 }
2202 }
2203 else {
2204 but = uiDefBut(block, ButtonType::Label, name, 0, 0, w, UI_UNIT_Y, nullptr, 0.0, 0.0, "");
2205 but->drawflag |= BUT_TEXT_RIGHT;
2206 but->drawflag &= ~BUT_TEXT_LEFT;
2207
2208 label_added = true;
2209 }
2210
2211 if (!label_added && heading_layout) {
2212 layout_heading_label_add(layout_sub, heading_layout, true, false);
2213 }
2214
2215 layout_split = LayoutInternal::item_prop_split_layout_hack(layout_parent, layout_split);
2216
2217 /* Watch out! We can only write into the new layout now. */
2218 if ((type == PROP_ENUM) && (flag & ITEM_R_EXPAND)) {
2219 /* Expanded enums each have their own name. */
2220
2221 /* Often expanded enum's are better arranged into a row,
2222 * so check the existing layout. */
2224 layout = &layout_split->row(true);
2225 }
2226 else {
2227 layout = &layout_split->column(true);
2228 }
2229 }
2230 else {
2231 if (use_prop_sep_split_label) {
2232 name = "";
2233 }
2234 layout = &layout_split->column(true);
2235 }
2236 layout->space_ = 0;
2237 }
2238
2239#ifdef UI_PROP_DECORATE
2240 if (ui_decorate.use_prop_decorate) {
2241 ui_decorate.layout = &layout_row->column(true);
2242 ui_decorate.layout->space_ = 0;
2244 ui_decorate.but = block->last_but();
2245
2246 /* Clear after. */
2248 }
2249#endif /* UI_PROP_DECORATE */
2250 }
2251 /* End split. */
2252 else if (heading_layout) {
2253 /* Could not add heading to split layout, fall back to inserting it to the layout with the
2254 * heading itself. */
2255 layout_heading_label_add(heading_layout, heading_layout, false, false);
2256 }
2257
2258 /* array property */
2259 if (index == RNA_NO_INDEX && is_array) {
2260 if (inside_prop_sep) {
2261 /* Within a split row, add array items to a column so they match the column layout of
2262 * previous items (e.g. transform vector with lock icon for each item). */
2263 layout = &layout->column(true);
2264 }
2265
2266 item_array(layout,
2267 block,
2268 name,
2269 icon,
2270 ptr,
2271 prop,
2272 len,
2273 0,
2274 0,
2275 w,
2276 h,
2277 expand,
2278 slider,
2279 toggle,
2280 icon_only,
2281 compact,
2282 !use_prop_sep_split_label);
2283 }
2284 /* enum item */
2285 else if (type == PROP_ENUM && index == RNA_ENUM_VALUE) {
2286 if (icon && !name.is_empty() && !icon_only) {
2288 block, ButtonType::Row, icon, name, 0, 0, w, h, ptr, prop, -1, 0, value, std::nullopt);
2289 }
2290 else if (icon) {
2292 block, ButtonType::Row, icon, 0, 0, w, h, ptr, prop, -1, 0, value, std::nullopt);
2293 }
2294 else {
2296 block, ButtonType::Row, name, 0, 0, w, h, ptr, prop, -1, 0, value, std::nullopt);
2297 }
2298 }
2299 /* expanded enum */
2300 else if (type == PROP_ENUM && expand) {
2301 item_enum_expand(layout, block, ptr, prop, name, h, icon_only);
2302 }
2303 /* property with separate label */
2305 but = item_with_label(layout,
2306 block,
2307 name,
2308 icon,
2309 ptr,
2310 prop,
2311 index,
2312 0,
2313 0,
2314 w,
2315 h,
2316 flag,
2317 std::nullopt,
2318 "UILayout.prop()");
2319
2320 if (is_id_name_prop) {
2321 Main *bmain = CTX_data_main(static_cast<bContext *>(block->evil_C));
2322 ID *id = ptr->owner_id;
2326 but, [bmain, id](StringRefNull new_name) { ED_id_rename(*bmain, *id, new_name); });
2327 }
2328
2329 if (layout->red_alert()) {
2331 }
2332
2333 if (layout->activate_init()) {
2335 }
2336 }
2337 /* single button */
2338 else {
2339 std::optional<ButtonType> button_type = slider ? std::optional(ButtonType::NumSlider) :
2340 std::nullopt;
2341 but = uiDefAutoButR(block, ptr, prop, index, name, icon, 0, 0, w, h, button_type);
2342
2343 if (flag & ITEM_R_CHECKBOX_INVERT) {
2344 if (ELEM(but->type,
2349 {
2351 }
2352 }
2353
2354 if ((toggle == 1) && but->type == ButtonType::Checkbox) {
2355 but->type = ButtonType::Toggle;
2356 }
2357
2358 if (layout->red_alert()) {
2360 }
2361
2362 if (layout->activate_init()) {
2364 }
2365 }
2366
2367 /* The resulting button may have the icon set since boolean button drawing
2368 * is being 'helpful' and adding an icon for us.
2369 * In this case we want the ability not to have an icon.
2370 *
2371 * We could pass an argument not to set the icon to begin with however this is the one case
2372 * the functionality is needed. */
2373 if (but && no_icon) {
2374 if ((icon == ICON_NONE) && (but->icon != ICON_NONE)) {
2375 def_but_icon_clear(but);
2376 }
2377 }
2378
2379 /* Mark non-embossed text-fields inside a list-box. */
2380 if (but && (block->flag & BLOCK_LIST_ITEM) && (but->type == ButtonType::Text) &&
2382 {
2384 }
2385
2386 if (but) {
2387 if (placeholder) {
2388 button_placeholder_set(but, *placeholder);
2389 }
2392 }
2393 if (ELEM(but->type, ButtonType::Text) && (flag & ITEM_R_TEXT_BUT_LABEL_STYLE)) {
2394 static_cast<ButtonText *>(but)->use_label_style = true;
2395 }
2396 }
2397
2398#ifdef UI_PROP_DECORATE
2399 if (ui_decorate.use_prop_decorate) {
2400 Button *but_decorate = ui_decorate.but ? block->next_but(ui_decorate.but) : block->first_but();
2401
2402 /* Move temporarily last buts to avoid multiple reallocations while inserting decorators. */
2404 tmp.reserve(ui_decorate.len);
2405 while (but_decorate && but_decorate != block->buttons_ptrs.last().get()) {
2406 tmp.append(block->buttons_ptrs.pop_last());
2407 }
2408 const bool use_blank_decorator = (flag & ITEM_R_FORCE_BLANK_DECORATE);
2409 Layout *layout_col = &ui_decorate.layout->column(false);
2410 layout_col->space_ = 0;
2411 layout_col->emboss_ = EmbossType::None;
2412
2413 int i;
2414 for (i = 0; i < ui_decorate.len && but_decorate; i++) {
2415 PointerRNA *ptr_dec = use_blank_decorator ? nullptr : &but_decorate->rnapoin;
2416 PropertyRNA *prop_dec = use_blank_decorator ? nullptr : but_decorate->rnaprop;
2417
2418 /* The icons are set in 'but_anim_flag' */
2419 layout_col->decorator(ptr_dec, prop_dec, but_decorate->rnaindex);
2420 but = block->buttons_ptrs.last().get();
2421
2422 if (!tmp.is_empty()) {
2423 block->buttons_ptrs.append(tmp.pop_last());
2424 but_decorate = block->buttons_ptrs.last().get();
2425 }
2426 else {
2427 but_decorate = nullptr;
2428 }
2429 }
2430 while (!tmp.is_empty()) {
2431 block->buttons_ptrs.append(tmp.pop_last());
2432 }
2433 BLI_assert(ELEM(i, 1, ui_decorate.len));
2434
2435 layout->flag_ &= ~ItemInternalFlag::PropDecorateNoPad;
2436 }
2437#endif /* UI_PROP_DECORATE */
2438
2439 if (no_bg) {
2440 emboss_ = prev_emboss;
2441 }
2442
2443 /* ensure text isn't added to icon_only buttons */
2444 if (but && icon_only) {
2445 BLI_assert(but->str.empty());
2446 }
2447}
2448
2450 const StringRefNull propname,
2451 const eUI_Item_Flag flag,
2452 const std::optional<StringRef> name,
2453 int icon)
2454{
2455 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
2456
2457 if (!prop) {
2458 item_disabled(this, propname.c_str());
2459 RNA_warning_bare("UILayout.prop(): property not found: %s.%s",
2461 propname.c_str());
2462 return;
2463 }
2464
2465 this->prop(ptr, prop, RNA_NO_INDEX, 0, flag, name, icon);
2466}
2467
2470 int index,
2471 int value,
2472 const eUI_Item_Flag flag,
2473 const std::optional<StringRefNull> name,
2474 int icon,
2475 const char *panel_type)
2476{
2477 Block *block = this->block();
2478 int i = block->buttons_ptrs.size();
2479 this->prop(ptr, prop, index, value, flag, name, icon);
2480 for (; i < block->buttons_ptrs.size(); i++) {
2481 Button *but = block->buttons_ptrs[i].get();
2482 if (but->rnaprop == prop && ELEM(but->type, ButtonType::Menu, ButtonType::Color)) {
2484 break;
2485 }
2486 }
2487 if (i == block->buttons_ptrs.size()) {
2488 const StringRefNull propname = RNA_property_identifier(prop);
2489 item_disabled(this, panel_type);
2490 RNA_warning_bare("UILayout.prop_with_popover(): property could not use a popover: %s.%s (%s)",
2492 propname.c_str(),
2493 panel_type);
2494 }
2495}
2496
2498 const StringRefNull propname,
2499 const eUI_Item_Flag flag,
2500 const std::optional<StringRefNull> name,
2501 int icon,
2502 const char *menu_type)
2503{
2504 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
2505 if (!prop) {
2506 item_disabled(this, propname.c_str());
2507 RNA_warning("property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
2508 return;
2509 }
2510
2511 this->prop_with_menu(ptr, prop, RNA_NO_INDEX, 0, flag, name, icon, menu_type);
2512}
2513
2516 int index,
2517 int value,
2518 const eUI_Item_Flag flag,
2519 const std::optional<StringRefNull> name,
2520 int icon,
2521 const char *menu_type)
2522{
2523 Block *block = this->block();
2524 int i = block->buttons_ptrs.size();
2525 this->prop(ptr, prop, index, value, flag, name, icon);
2526 while (i < block->buttons_ptrs.size()) {
2527 Button *but = block->buttons_ptrs[i].get();
2528 if (but->rnaprop == prop && but->type == ButtonType::Menu) {
2530 break;
2531 }
2532 i++;
2533 }
2534 if (i == block->buttons_ptrs.size()) {
2535 const StringRefNull propname = RNA_property_identifier(prop);
2536 item_disabled(this, menu_type);
2537 RNA_warning_bare("UILayout.prop_with_menu(): property could not use a menu: %s.%s (%s)",
2539 propname.c_str(),
2540 menu_type);
2541 }
2542}
2543
2546 int value,
2547 const std::optional<StringRefNull> name,
2548 int icon)
2549{
2551 const StringRefNull propname = RNA_property_identifier(prop);
2552 item_disabled(this, propname.c_str());
2553 RNA_warning_bare("UILayout.prop_enum(): property not an enum: %s.%s",
2555 propname.c_str());
2556 return;
2557 }
2558
2559 this->prop(ptr, prop, RNA_ENUM_VALUE, value, UI_ITEM_NONE, name, icon);
2560}
2561
2564 const char *value,
2565 const std::optional<StringRefNull> name,
2566 int icon)
2567{
2568 if (RNA_property_type(prop) != PROP_ENUM) [[unlikely]] {
2569 const StringRefNull propname = RNA_property_identifier(prop);
2570 item_disabled(this, propname.c_str());
2571 RNA_warning_bare("UILayout.prop_enum(): not an enum property: %s.%s",
2573 propname.c_str());
2574 return;
2575 }
2576
2577 const EnumPropertyItem *item;
2578 bool free;
2580 static_cast<bContext *>(this->block()->evil_C), ptr, prop, &item, nullptr, &free);
2581
2582 int ivalue;
2583 if (!RNA_enum_value_from_id(item, value, &ivalue)) {
2584 const StringRefNull propname = RNA_property_identifier(prop);
2585 if (free) {
2586 MEM_delete(item);
2587 }
2588 item_disabled(this, propname.c_str());
2589 RNA_warning_bare("UILayout.prop_enum(): enum property value not found: %s", value);
2590 return;
2591 }
2592
2593 for (int a = 0; item[a].identifier; a++) {
2594 if (item[a].identifier[0] == '\0') {
2595 /* Skip enum item separators. */
2596 continue;
2597 }
2598 if (item[a].value == ivalue) {
2599 const StringRefNull item_name = name.value_or(
2601 const eUI_Item_Flag flag = !item_name.is_empty() ? UI_ITEM_NONE : ITEM_R_ICON_ONLY;
2602
2603 this->prop(ptr, prop, RNA_ENUM_VALUE, ivalue, flag, item_name, icon ? icon : item[a].icon);
2604 break;
2605 }
2606 }
2607
2608 if (free) {
2609 MEM_delete(item);
2610 }
2611}
2612
2614 const StringRefNull propname,
2615 const char *value,
2616 const std::optional<StringRefNull> name,
2617 int icon)
2618{
2619 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
2620 if (prop == nullptr) [[unlikely]] {
2621 item_disabled(this, propname.c_str());
2622 RNA_warning_bare("UILayout.prop_enum(): enum property not found: %s.%s",
2624 propname.c_str());
2625 return;
2626 }
2627 this->prop_enum(ptr, prop, value, name, icon);
2628}
2629
2631{
2632 Block *block = this->block();
2633
2634 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
2635
2636 if (!prop) {
2637 item_disabled(this, propname.c_str());
2638 RNA_warning_bare("UILayout.props_enum(): enum property not found: %s.%s",
2640 propname.c_str());
2641 return;
2642 }
2643
2645 RNA_warning_bare("UILayout.props_enum(): not an enum property: %s.%s",
2647 propname.c_str());
2648 return;
2649 }
2650
2651 Layout *split = &this->split(0.0f, false);
2652 Layout *column = &split->column(false);
2653
2654 int totitem;
2655 const EnumPropertyItem *item;
2656 bool free;
2658 static_cast<bContext *>(block->evil_C), ptr, prop, &item, &totitem, &free);
2659
2660 for (int i = 0; i < totitem; i++) {
2661 if (item[i].identifier[0]) {
2662 column->prop_enum(ptr, prop, item[i].value, item[i].name, item[i].icon);
2663 but_tip_from_enum_item(block->buttons_ptrs.last().get(), &item[i]);
2664 }
2665 else {
2666 if (item[i].name) {
2667 if (i != 0) {
2668 column = &split->column(false);
2669 }
2670
2671 column->label(item[i].name, ICON_NONE);
2672 Button *bt = block->buttons_ptrs.last().get();
2673 bt->drawflag = BUT_TEXT_LEFT;
2674
2675 but_tip_from_enum_item(bt, &item[i]);
2676 }
2677 else {
2678 column->separator();
2679 }
2680 }
2681 }
2682
2683 if (free) {
2684 MEM_delete(item);
2685 }
2686}
2687
2688/* Pointer RNA button with search */
2689
2690static void search_id_collection(StructRNA *ptype, PointerRNA *r_ptr, PropertyRNA **r_prop)
2691{
2692 /* look for collection property in Main */
2693 /* NOTE: using global Main is OK-ish here, UI shall not access other Mains anyway. */
2695
2696 *r_prop = nullptr;
2697
2698 RNA_STRUCT_BEGIN (r_ptr, iprop) {
2699 /* if it's a collection and has same pointer type, we've got it */
2700 if (RNA_property_type(iprop) == PROP_COLLECTION) {
2701 StructRNA *srna = RNA_property_pointer_type(r_ptr, iprop);
2702
2703 if (ptype == srna) {
2704 *r_prop = iprop;
2705 break;
2706 }
2707 }
2708 }
2710}
2711
2713{
2714 RNACollectionSearch *coll_search = static_cast<RNACollectionSearch *>(ptr);
2715 butstore_free(coll_search->butstore_block, coll_search->butstore);
2716 MEM_delete(coll_search);
2717}
2718
2720 PointerRNA *ptr,
2721 PropertyRNA *prop,
2722 PointerRNA *searchptr,
2723 PropertyRNA *searchprop,
2724 PropertyRNA *item_searchprop,
2725 const bool results_are_suggestions)
2726{
2727 /* for ID's we do automatic lookup */
2728 bool has_search_fn = false;
2729
2730 PointerRNA sptr;
2731 if (!searchprop) {
2732 if (RNA_property_type(prop) == PROP_STRING) {
2733 has_search_fn = (RNA_property_string_search_flag(prop) != 0);
2734 }
2735 if (RNA_property_type(prop) == PROP_POINTER) {
2736 StructRNA *ptype = RNA_property_pointer_type(ptr, prop);
2737 search_id_collection(ptype, &sptr, &searchprop);
2738 searchptr = &sptr;
2739 }
2740 }
2741
2742 /* turn button into search button */
2743 if (has_search_fn || searchprop) {
2744 RNACollectionSearch *coll_search = MEM_new<RNACollectionSearch>(__func__);
2745
2747 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
2748
2749 if (searchptr) {
2750 search_but->rnasearchpoin = *searchptr;
2751 search_but->rnasearchprop = searchprop;
2752 }
2753
2754 but->hardmax = std::max(but->hardmax, 256.0f);
2756 if (RNA_property_is_unlink(prop)) {
2757 but->flag |= BUT_VALUE_CLEAR;
2758 }
2759
2760 coll_search->target_ptr = *ptr;
2761 coll_search->target_prop = prop;
2762
2763 if (searchptr) {
2764 coll_search->search_ptr = *searchptr;
2765 coll_search->search_prop = searchprop;
2766 coll_search->item_search_prop = item_searchprop;
2767 }
2768 else {
2769 /* Rely on `has_search_fn`. */
2770 coll_search->search_ptr = {};
2771 coll_search->search_prop = nullptr;
2772 coll_search->item_search_prop = nullptr;
2773 }
2774
2775 coll_search->search_but = but;
2776 coll_search->butstore_block = but->block;
2777 coll_search->butstore = butstore_create(coll_search->butstore_block);
2778 butstore_register(coll_search->butstore, &coll_search->search_but);
2779
2780 if (RNA_property_type(prop) == PROP_ENUM) {
2781 /* XXX, this will have a menu string,
2782 * but in this case we just want the text */
2783 but->str.clear();
2784 }
2785
2786 button_func_search_set_results_are_suggestions(but, results_are_suggestions);
2787
2791 coll_search,
2792 false,
2794 nullptr,
2795 nullptr);
2796 /* If this is called multiple times for the same button, an earlier call may have taken the
2797 * else branch below so the button was disabled. Now we have a searchprop, so it can be enabled
2798 * again. */
2799 but->flag &= ~BUT_DISABLED;
2800 }
2801 else if (but->type == ButtonType::SearchMenu) {
2802 /* In case we fail to find proper searchprop,
2803 * so other code might have already set but->type to search menu... */
2804 but->flag |= BUT_DISABLED;
2805 }
2806}
2807
2809 PointerRNA *ptr,
2810 StringRefNull propname,
2811 std::optional<StringRefNull> name_opt,
2812 std::optional<StringRefNull> placeholder,
2813 const int initial_visible_lines)
2814{
2815 TextboxState *textbox_state = textbox_ensure_state(
2817 fmt::format("{}.{}", RNA_struct_identifier(ptr->type), propname),
2818 initial_visible_lines);
2819 this->textbox_with_state(ptr, propname, textbox_state, name_opt, placeholder);
2820}
2821
2823 StringRefNull propname,
2824 TextboxState *textbox_state,
2825 std::optional<StringRefNull> name_opt,
2826 std::optional<StringRefNull> placeholder)
2827{
2828 Block *block = this->block();
2830
2831 BLI_assert(textbox_state);
2832
2833 if (!prop) {
2834 item_disabled(this, propname.c_str());
2836 "string property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
2837 return;
2838 }
2839
2840 StringRefNull name = name_opt.value_or(RNA_property_ui_name(prop));
2841
2842 if (!name.is_empty()) {
2843 uiItemL_respect_property_split(this, name, ICON_NONE);
2844 }
2845 else {
2847 }
2848
2849 int w, h;
2850 item_rna_size(block->curlayout, "", ICON_NONE, ptr, prop, -1, false, false, &w, &h);
2851 Button *but = uiDefButTextBoxR(block, ptr, propname, textbox_state, 0, 0, w);
2852 if (placeholder) {
2853 button_placeholder_set(but, *placeholder);
2854 }
2856}
2857
2860 PointerRNA *searchptr,
2861 PropertyRNA *searchprop,
2862 PropertyRNA *item_searchprop,
2863 const std::optional<StringRefNull> name_opt,
2864 int icon,
2865 bool results_are_suggestions)
2866{
2867 const bool use_prop_sep = this->use_property_split();
2868 Block *block = this->block();
2870
2873 RNA_warning_bare("UILayout.prop_search(): Property %s.%s must be a pointer, string or enum",
2876 return;
2877 }
2878 if (RNA_property_type(searchprop) != PROP_COLLECTION) {
2880 "UILayout.prop_search(): search collection property is not a collection type: %s.%s",
2881 RNA_struct_identifier(searchptr->type),
2882 RNA_property_identifier(searchprop));
2883 return;
2884 }
2885 if (item_searchprop && RNA_property_type(item_searchprop) != PROP_STRING) {
2887 "UILayout.prop_search(): Search collection items' property is not a string type: %s.%s",
2888 RNA_struct_identifier(RNA_property_pointer_type(searchptr, searchprop)),
2889 RNA_property_identifier(item_searchprop));
2890 return;
2891 }
2892
2893 /* get icon & name */
2894 if (icon == ICON_NONE) {
2895 const StructRNA *icontype;
2896 if (type == PROP_POINTER) {
2897 icontype = RNA_property_pointer_type(ptr, prop);
2898 }
2899 else {
2900 icontype = RNA_property_pointer_type(searchptr, searchprop);
2901 }
2902
2903 icon = RNA_struct_ui_icon(icontype);
2904 }
2905 StringRefNull name = name_opt.value_or(RNA_property_ui_name(prop));
2906
2907 char namestr[UI_MAX_NAME_STR];
2908 if (use_prop_sep == false) {
2909 name = item_name_add_colon(name, namestr);
2910 }
2911
2912 /* create button */
2913
2914 int w, h;
2915 item_rna_size(this, name, icon, ptr, prop, 0, false, false, &w, &h);
2916 w += UI_UNIT_X; /* X icon needs more space */
2917 Button *but = item_with_label(this,
2918 block,
2919 name,
2920 icon,
2921 ptr,
2922 prop,
2923 0,
2924 0,
2925 0,
2926 w,
2927 h,
2928 0,
2930 "UILayout.prop_search()");
2933 but, ptr, prop, searchptr, searchprop, item_searchprop, results_are_suggestions);
2934}
2935
2937 const StringRefNull propname,
2938 PointerRNA *searchptr,
2939 const StringRefNull searchpropname,
2940 const std::optional<StringRefNull> name,
2941 int icon)
2942{
2943 /* validate arguments */
2944 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
2945 if (!prop) {
2946 RNA_warning_bare("UILayout.prop_search(): property not found: %s.%s",
2948 propname.c_str());
2949 return;
2950 }
2951 PropertyRNA *searchprop = RNA_struct_find_property(searchptr, searchpropname.c_str());
2952 if (!searchprop) {
2953 RNA_warning_bare("UILayout.prop_search(): search collection property not found: %s.%s",
2954 RNA_struct_identifier(searchptr->type),
2955 searchpropname.c_str());
2956 return;
2957 }
2958
2959 this->prop_search(ptr, prop, searchptr, searchprop, nullptr, name, icon, false);
2960}
2961
2962void item_menutype_func(bContext *C, Layout *layout, void *arg_mt)
2963{
2964 MenuType *mt = static_cast<MenuType *>(arg_mt);
2965 menutype_draw(C, mt, layout);
2966}
2967
2968void item_paneltype_func(bContext *C, Layout *layout, void *arg_pt)
2969{
2970 PanelType *pt = static_cast<PanelType *>(arg_pt);
2971 UI_paneltype_draw(C, pt, layout);
2972}
2973
2974static Button *item_menu(Layout *layout,
2975 const StringRef name,
2976 int icon,
2977 MenuCreateFunc func,
2978 void *arg,
2979 void *argN,
2980 const std::optional<StringRef> tip,
2981 bool force_menu,
2982 ButtonArgNFree func_argN_free_fn = MEM_delete_void,
2983 ButtonArgNCopy func_argN_copy_fn = MEM_dupalloc_void)
2984{
2985 Block *block = layout->block();
2986 Layout *heading_layout = layout_heading_find(layout);
2987
2988 block_layout_set_current(block, layout);
2990
2991 if (layout->root()->type == LayoutType::Menu && !icon) {
2992 icon = ICON_BLANK1;
2993 }
2994
2996 if (layout->root()->type == LayoutType::Header) { /* Ugly! */
2997 if (icon == ICON_NONE && force_menu) {
2998 /* pass */
2999 }
3000 else if (force_menu) {
3001 pad_factor.text = 1.85;
3002 pad_factor.icon_only = 0.6f;
3003 }
3004 else {
3005 pad_factor.text = 0.75f;
3006 }
3007 }
3008
3009 const int w = text_icon_width_ex(layout, name, icon, pad_factor, UI_FSTYLE_WIDGET);
3010 const int h = UI_UNIT_Y;
3011
3012 if (heading_layout) {
3013 layout_heading_label_add(layout, heading_layout, true, true);
3014 }
3015
3016 Button *but;
3017 if (!name.is_empty() && icon) {
3018 but = uiDefIconTextMenuBut(block, func, arg, icon, name, 0, 0, w, h, tip);
3019 }
3020 else if (icon) {
3021 but = uiDefIconMenuBut(block, func, arg, icon, 0, 0, w, h, tip);
3022 if (force_menu && !name.is_empty()) {
3024 }
3025 }
3026 else {
3027 but = uiDefMenuBut(block, func, arg, name, 0, 0, w, h, tip);
3028 }
3029
3030 if (argN) {
3031 /* ugly! */
3032 if (arg != argN) {
3033 but->poin = reinterpret_cast<char *>(but);
3034 }
3035 but->func_argN = argN;
3036 but->func_argN_free_fn = func_argN_free_fn;
3037 but->func_argN_copy_fn = func_argN_copy_fn;
3038 }
3039
3041 /* We never want a drop-down in menu! */
3042 (force_menu && layout->root()->type != LayoutType::Menu))
3043 {
3045 }
3046
3047 return but;
3048}
3049
3050void Layout::menu(MenuType *mt, const std::optional<StringRef> name_opt, int icon)
3051{
3052 Block *block = this->block();
3053 bContext *C = static_cast<bContext *>(block->evil_C);
3054 if (WM_menutype_poll(C, mt) == false) {
3055 return;
3056 }
3057
3058 const StringRef name = name_opt.value_or(CTX_IFACE_(mt->translation_context, mt->label));
3059
3060 if (root_->type == LayoutType::Menu && !icon) {
3061 icon = ICON_BLANK1;
3062 }
3063
3064 item_menu(this,
3065 name,
3066 icon,
3068 mt,
3069 nullptr,
3070 mt->description ? TIP_(mt->description) : "",
3071 false);
3072}
3073
3074void Layout::menu(const StringRef menuname, const std::optional<StringRef> name, int icon)
3075{
3076 MenuType *mt = WM_menutype_find(menuname, false);
3077 if (mt == nullptr) {
3078 RNA_warning_bare("UILayout.menu(): not found %s", std::string(menuname).c_str());
3079 return;
3080 }
3081 this->menu(mt, name, icon);
3082}
3083
3085{
3086 MenuType *mt = WM_menutype_find(menuname, false);
3087 if (mt == nullptr) {
3088 RNA_warning_bare("UILayout.menu_contents(): not found %s", std::string(menuname).c_str());
3089 return;
3090 }
3091
3092 Block *block = this->block();
3093 bContext *C = static_cast<bContext *>(block->evil_C);
3094 if (WM_menutype_poll(C, mt) == false) {
3095 return;
3096 }
3097
3098 menutype_draw(C, mt, this);
3099}
3100
3102{
3103 Block *block = this->block();
3104
3106 Layout &col = this->column(false);
3107 col.space_ = 0;
3109
3110 if (ELEM(nullptr, ptr, prop) || !RNA_property_animateable(ptr, prop)) {
3111 Button *but = uiDefIconBut(block,
3113 ICON_BLANK1,
3114 0,
3115 0,
3116 UI_UNIT_X,
3117 UI_UNIT_Y,
3118 nullptr,
3119 0.0,
3120 0.0,
3121 "");
3122 but->flag |= BUT_DISABLED;
3123 return;
3124 }
3125
3126 const bool is_expand = item_rna_is_expand(prop, index, UI_ITEM_NONE);
3127 const bool is_array = RNA_property_array_check(prop);
3128
3129 /* Loop for the array-case, but only do in case of an expanded array. */
3130 for (int i = 0; i < (is_expand ? RNA_property_array_length(ptr, prop) : 1); i++) {
3131 ButtonDecorator *but = static_cast<ButtonDecorator *>(uiDefIconBut(block,
3133 ICON_DOT,
3134 0,
3135 0,
3136 UI_UNIT_X,
3137 UI_UNIT_Y,
3138 nullptr,
3139 0.0,
3140 0.0,
3141 TIP_("Animate property")));
3142
3143 button_func_set(but, button_anim_decorate_cb, but, nullptr);
3144 but->flag |= BUT_UNDO | BUT_DRAG_LOCK;
3145 /* Decorators have their own RNA data, using the normal #Button RNA members has many
3146 * side-effects. */
3147 but->decorated_rnapoin = *ptr;
3148 but->decorated_rnaprop = prop;
3149 /* def_but_rna() sets non-array buttons to have a RNA index of 0. */
3150 but->decorated_rnaindex = (!is_array || is_expand) ? i : index;
3151 }
3152}
3153
3154void Layout::decorator(PointerRNA *ptr, const std::optional<StringRefNull> propname, int index)
3155{
3156 PropertyRNA *prop = nullptr;
3157
3158 if (ptr && propname) {
3159 /* validate arguments */
3160 prop = RNA_struct_find_property(ptr, propname->c_str());
3161 if (!prop) {
3162 item_disabled(this, propname->c_str());
3163 RNA_warning_bare("UILayout::decorator(): property not found: %s.%s",
3165 propname->c_str());
3166 return;
3167 }
3168 }
3169
3170 /* ptr and prop are allowed to be nullptr here. */
3171 this->decorator(ptr, prop, index);
3172}
3173
3175 PanelType *pt,
3176 const std::optional<StringRef> name_opt,
3177 int icon)
3178{
3179 Layout *layout = this;
3180 const StringRef name = name_opt.value_or(CTX_IFACE_(pt->translation_context, pt->label));
3181
3182 if (root_->type == LayoutType::Menu && !icon) {
3183 icon = ICON_BLANK1;
3184 }
3185
3186 const bContextStore *previous_ctx = CTX_store_get(C);
3187 /* Set context for polling (and panel header drawing). */
3188 CTX_store_set(const_cast<bContext *>(C), context_);
3189
3190 const bool ok = (pt->poll == nullptr) || pt->poll(C, pt);
3191 if (ok && (pt->draw_header != nullptr)) {
3192 layout = &this->row(true);
3193 Panel panel{};
3194 Panel_Runtime panel_runtime{};
3195 panel.runtime = &panel_runtime;
3196 panel.type = pt;
3197 panel.layout = layout;
3199 pt->draw_header(C, &panel);
3200 }
3201
3202 CTX_store_set(const_cast<bContext *>(C), previous_ctx);
3203
3204 Button *but = item_menu(
3205 layout, name, icon, item_paneltype_func, pt, nullptr, TIP_(pt->description), true);
3207
3208 /* Override button size when there is no icon or label. */
3209 if (layout->root()->type == LayoutType::VerticalBar && !icon && name.is_empty()) {
3210 but->rect.xmax = but->rect.xmin + UI_UNIT_X;
3211 }
3212
3213 if (!ok) {
3214 but->flag |= BUT_DISABLED;
3215 }
3216}
3217
3219 const StringRef panel_type,
3220 std::optional<StringRef> name_opt,
3221 int icon,
3222 PopupAttachDirection direction)
3223{
3224 PanelType *pt = WM_paneltype_find(panel_type, true);
3225 if (pt == nullptr) {
3226 RNA_warning_bare("UILayout.popover(): Panel type not found '%s'",
3227 std::string(panel_type).c_str());
3228 return;
3229 }
3230 this->popover(C, pt, name_opt, icon);
3231 ButtonMenu *popover_button = static_cast<ButtonMenu *>(this->block()->buttons_ptrs.last().get());
3232 popover_button->popup_attach_direction = direction;
3233}
3234
3236 bContext *C, int space_id, int region_id, const char *context, const char *category)
3237{
3238 SpaceType *st = BKE_spacetype_from_id(space_id);
3239 if (st == nullptr) {
3240 RNA_warning_bare("UILayout.popover(): space type not found %d", space_id);
3241 return;
3242 }
3243 ARegionType *art = BKE_regiontype_from_id(st, region_id);
3244 if (art == nullptr) {
3245 RNA_warning_bare("UILayout.popover(): region type not found %d", region_id);
3246 return;
3247 }
3248
3249 for (PanelType &pt : art->paneltypes) {
3250 /* Causes too many panels, check context. */
3251 if (pt.parent_id[0] == '\0') {
3252 if (/* (*context == '\0') || */ STREQ(pt.context, context)) {
3253 if ((*category == '\0') || STREQ(pt.category, category)) {
3254 if (pt.poll == nullptr || pt.poll(C, &pt)) {
3255 this->popover(C, &pt, std::nullopt, ICON_NONE);
3256 }
3257 }
3258 }
3259 }
3260 }
3261}
3262
3268 const StringRef name,
3269 int icon,
3270 std::optional<StringRef> tooltip = std::nullopt,
3271 const ButtonType but_type = ButtonType::Label)
3272{
3273 Block *block = layout->block();
3274
3275 block_layout_set_current(block, layout);
3277
3278 if (layout->root()->type == LayoutType::Menu && !icon) {
3279 icon = ICON_BLANK1;
3280 }
3281
3282 /* When drawing text over an emboss background (like for a normal push button), the widget needs
3283 * to be slightly wider than just the text, since the text will get some horizontal padding
3284 * inside the background box when drawing. Otherwise drawing would truncate the text to fit.
3285 * This is not the most thorough check, but should work well enough and can be expanded as
3286 * needed. */
3287 const bool has_emboss = but_type != ButtonType::Label &&
3288 layout->emboss() == ui::EmbossType::Emboss;
3289 const int w = text_icon_width_ex(
3290 layout, name, icon, has_emboss ? text_pad_default : text_pad_none, UI_FSTYLE_WIDGET);
3291 Button *but;
3292 if (icon && !name.is_empty()) {
3293 but = uiDefIconTextBut(block, but_type, icon, name, 0, 0, w, UI_UNIT_Y, nullptr, tooltip);
3294 }
3295 else if (icon) {
3296 but = uiDefIconBut(block, but_type, icon, 0, 0, w, UI_UNIT_Y, nullptr, 0.0, 0.0, tooltip);
3297 }
3298 else {
3299 but = uiDefBut(block, but_type, name, 0, 0, w, UI_UNIT_Y, nullptr, 0.0, 0.0, tooltip);
3300 }
3301
3302 /* to compensate for string size padding in text_icon_width,
3303 * make text aligned right if the layout is aligned right.
3304 */
3305 if (layout->alignment() == LayoutAlign::Right) {
3306 but->drawflag &= ~BUT_TEXT_LEFT; /* default, needs to be unset */
3307 but->drawflag |= BUT_TEXT_RIGHT;
3308 }
3309
3310 /* Mark as a label inside a list-box. */
3311 if (block->flag & BLOCK_LIST_ITEM) {
3312 but->flag |= BUT_LIST_ITEM;
3313 }
3314
3315 if (layout->red_alert()) {
3317 }
3318
3319 return but;
3320}
3321
3323 Layout *layout, const StringRef name, int icon, const bool highlight, const bool redalert)
3324{
3325 Button *but = uiItem_simple(layout, name, icon);
3326
3327 if (highlight) {
3328 /* TODO: add another flag for this. */
3330 }
3331
3332 if (redalert) {
3334 }
3335
3336 return but;
3337}
3338
3339void Layout::label(const StringRef name, int icon)
3340{
3341 uiItem_simple(this, name, icon);
3342}
3343
3344void Layout::label_multiline(StringRefNull text, int icon, FontStyleAlign align, int max_lines)
3345{
3346 block_layout_set_current(this->block(), this);
3347 Button *button = nullptr;
3348 /* Use a dummy string, let the layout system to be resolved and then do text wrap. */
3349 const int width = text_icon_width_ex(
3350 this, "non-empty text", icon, text_pad_none, UI_FSTYLE_WIDGET);
3351 if (icon) {
3352 button = uiDefIconTextBut(this->block(),
3354 icon,
3355 text,
3356 0,
3357 0,
3358 width,
3359 UI_UNIT_Y,
3360 nullptr,
3361 std::nullopt);
3362 }
3363 else {
3364 button = uiDefBut(this->block(),
3366 text,
3367 0,
3368 0,
3369 width,
3370 UI_UNIT_Y,
3371 nullptr,
3372 0,
3373 0,
3374 std::nullopt);
3375 }
3376 this->root_->use_dynamic_height = true;
3377 ButtonLabel *label = static_cast<ButtonLabel *>(button);
3380 label->max_lines = max_lines;
3381 if (this->red_alert()) {
3383 }
3384}
3385
3387{
3388 if (G.debug_value == 4002) {
3390 }
3391
3392 block_layout_set_current(this->block(), this);
3393 /* Must be >0 for it to work on horizontal layouts. */
3394 const int dummy_width = 1;
3395 ButtonLabel *button = static_cast<ButtonLabel *>(uiDefBut(
3396 this->block(), ButtonType::Label, text, 0, 0, dummy_width, 0, nullptr, 0, 0, std::nullopt));
3397 this->root_->use_dynamic_height = true;
3401 if (this->red_alert()) {
3403 }
3404}
3405
3407{
3408 /* Take over the image with the given key from the previous redraw. */
3409 if (block->oldblock == nullptr) {
3410 return nullptr;
3411 }
3412 for (const std::unique_ptr<Button> &old_but : block->oldblock->buttons_ptrs) {
3413 if (old_but->type != ButtonType::Image) {
3414 continue;
3415 }
3416 ButtonImage *old_image_but = static_cast<ButtonImage *>(old_but.get());
3417 if (old_image_but->key != key) {
3418 continue;
3419 }
3420 return old_image_but;
3421 }
3422 return nullptr;
3423}
3424
3425bool Layout::image(const StringRef key, const FunctionRef<ImBuf *()> create_fn)
3426{
3427 BLI_assert(!key.is_empty());
3428 Block *block = this->block();
3429
3430 ImBuf *ibuf = nullptr;
3431 if (ButtonImage *old_but = image_find_in_old_block(block, key)) {
3432 ibuf = reinterpret_cast<ImBuf *>(std::exchange(old_but->poin, nullptr));
3433 old_but->key.clear();
3434 }
3435 else {
3436 ibuf = create_fn();
3437 }
3438
3440 /* Placeholder size, the height is resolved from the final width. */
3441 ButtonImage *but = static_cast<ButtonImage *>(
3442 uiDefButImage(block, ibuf, 0, 0, UI_UNIT_X, ibuf ? UI_UNIT_Y : 0, nullptr));
3443 but->key = key;
3444 this->root_->use_dynamic_height = true;
3445 return ibuf != nullptr;
3446}
3447
3448void Layout::link(const StringRef url, const StringRef name, int icon)
3449{
3450 wmOperatorType *ot = WM_operatortype_find("WM_OT_url_open", false); /* print error next */
3451
3452 if (!ot || !ot->srna) {
3453 item_disabled(this, "WM_OT_url_open");
3454 RNA_warning_bare("UILayout::link(): %s '%s'",
3455 ot ? "operator missing srna" : "unknown operator",
3456 "WM_OT_url_open");
3457 return;
3458 }
3459 if (name.is_empty()) {
3460 item_disabled(this, "WM_OT_url_open");
3461 RNA_warning_bare("UILayout::link(): missing link name");
3462 return;
3463 }
3464
3465 /* Force the button to not be expanded full width. */
3466 Layout *layout = &this->row(false);
3467 layout->alignment_set(this->alignment());
3468 layout = &layout->row(false);
3470
3471 if (this->root()->type == LayoutType::Menu && !icon) {
3472 icon = ICON_BLANK1;
3473 }
3474 Block *block = layout->block();
3477
3478 /* Match button width to label items. */
3479 const int w = text_icon_width_ex(layout, name, icon, text_pad_none, UI_FSTYLE_WIDGET);
3480
3481 /* Create the button. */
3482 Button *button;
3484 if (icon) {
3486 block, ButtonType::But, ot, context, icon, name, 0, 0, w, UI_UNIT_Y, std::nullopt);
3487 }
3488 else {
3490 block, ButtonType::But, ot, context, name, 0, 0, w, UI_UNIT_Y, std::nullopt);
3491 }
3492
3493 if (layout->red_alert()) {
3495 }
3497 opptr->data = bke::idprop::create_group("wmOperatorProperties").release();
3498
3499 static_cast<ButtonPush *>(button)->draw_as_link = true;
3500
3501 if (this->alignment() == LayoutAlign::Right) {
3504 }
3505 else if (this->alignment() == LayoutAlign::Left) {
3508 }
3509 /* Show only URL in the tooltip. */
3511 button,
3512 [](bContext & /*C*/, ui::TooltipData &data, ui::Button *but, void * /*argN*/) {
3515 RNA_string_get(but->opptr, "url"),
3516 {},
3519 false);
3520 },
3521 nullptr,
3522 nullptr);
3523 RNA_string_set(opptr, "url", url.data());
3524}
3525
3527{
3528 PropertySplitWrapper split_wrapper = {nullptr};
3529
3530 Layout *layout_row = &parent_layout->row(true);
3531 Layout *layout_split = &layout_row->split(Layout::PROPERTY_SPLIT_FACTOR, true);
3532
3533 split_wrapper.label_column = &layout_split->column(true);
3535 split_wrapper.property_row = LayoutInternal::item_prop_split_layout_hack(parent_layout,
3536 layout_split);
3537 split_wrapper.decorate_column = parent_layout->use_property_decorate() ?
3538 &layout_row->column(true) :
3539 nullptr;
3540
3541 return split_wrapper;
3542}
3543
3545{
3546 if (layout->use_property_split()) {
3547 Block *block = layout->block();
3548 const PropertySplitWrapper split_wrapper = uiItemPropertySplitWrapperCreate(layout);
3549 /* Further items added to 'layout' will automatically be added to split_wrapper.property_row */
3550
3551 uiItem_simple(split_wrapper.label_column, text, icon);
3552 block_layout_set_current(block, split_wrapper.property_row);
3553
3554 return split_wrapper.decorate_column;
3555 }
3556
3557 char namestr[UI_MAX_NAME_STR];
3558 text = item_name_add_colon(text, namestr);
3559 uiItem_simple(layout, text, icon);
3560
3561 return nullptr;
3562}
3563
3564void uiItemLDrag(Layout *layout, PointerRNA *ptr, StringRef name, int icon)
3565{
3566 Button *but = uiItem_simple(layout, name, icon);
3567
3568 if (ptr && ptr->has_type()) {
3569 if (RNA_struct_is_ID(ptr->type)) {
3570 button_drag_set_id(but, ptr->owner_id);
3571 }
3572 }
3573}
3574
3576 const int icon,
3577 std::function<void(bContext &)> func,
3578 std::optional<StringRef> tooltip)
3579{
3580 Button *but = uiItem_simple(this, name, icon, tooltip, ButtonType::But);
3581 button_func_set(but, std::move(func));
3582 return but;
3583}
3584
3586{
3587 Block *block = this->block();
3588 const bool is_menu = block_is_menu(block);
3589 const bool is_pie = block_is_pie_menu(block);
3590 if (is_menu && !block_can_add_separator(block)) {
3591 return;
3592 }
3593
3594 /* Sizing of spaces should not depend on line width. */
3595 const int space = (is_menu) ? int(7.0f * UI_SCALE_FAC * factor) :
3596 int(6.0f * UI_SCALE_FAC * factor);
3597
3598 ButtonType but_type;
3599
3600 switch (type) {
3602 but_type = ButtonType::SeprLine;
3603 break;
3605 but_type = (is_menu && !is_pie) ? ButtonType::SeprLine : ButtonType::Sepr;
3606 break;
3607 default:
3608 but_type = ButtonType::Sepr;
3609 }
3610
3611 bool is_vertical_bar = (w_ == 0) && but_type == ButtonType::SeprLine;
3612
3614 Button *but = uiDefBut(block,
3615 but_type,
3616 "",
3617 0,
3618 0,
3619 space,
3620 is_vertical_bar ? UI_UNIT_Y : space,
3621 nullptr,
3622 0.0,
3623 0.0,
3624 "");
3625
3626 if (but_type == ButtonType::SeprLine) {
3627 auto *but_line = static_cast<ButtonSeparatorLine *>(but);
3628 but_line->is_vertical = is_vertical_bar;
3629 }
3630}
3631
3632void Layout::progress_indicator(const char *text,
3633 const float factor,
3634 const ButProgressType progress_type)
3635{
3636 const bool has_text = text && text[0];
3637 Block *block = this->block();
3638 short width;
3639
3640 if (progress_type == ButProgressType::Bar) {
3641 width = UI_UNIT_X * 5;
3642 }
3643 else if (has_text) {
3644 width = UI_UNIT_X * 8;
3645 }
3646 else {
3647 width = UI_UNIT_X;
3648 }
3649
3651 Button *but = uiDefBut(block,
3653 (text) ? text : "",
3654 0,
3655 0,
3656 width,
3657 short(UI_UNIT_Y),
3658 nullptr,
3659 0.0,
3660 0.0,
3661 "");
3662
3663 if (has_text && (progress_type == ButProgressType::Ring)) {
3664 /* For progress bar, centered is okay, left aligned for ring/pie. */
3665 but->drawflag |= BUT_TEXT_LEFT;
3666 }
3667
3668 ButtonProgress *progress_bar = static_cast<ButtonProgress *>(but);
3669 progress_bar->progress_type = progress_type;
3670 progress_bar->progress_factor = factor;
3671}
3672
3674{
3675 Block *block = this->block();
3676 const bool is_popup = block_is_popup_any(block);
3677
3678 if (is_popup) {
3679 printf("Error: separator_spacer() not supported in popups.\n");
3680 return;
3681 }
3682
3683 if (block->direction & UI_DIR_RIGHT) {
3684 printf("Error: separator_spacer() only supported in horizontal blocks.\n");
3685 return;
3686 }
3687
3689 uiDefBut(
3690 block, ButtonType::SeprSpacer, "", 0, 0, 0.3f * UI_UNIT_X, UI_UNIT_Y, nullptr, 0.0, 0.0, "");
3691}
3692
3693void Layout::menu_fn(const StringRefNull name, int icon, MenuCreateFunc func, void *arg)
3694{
3695 if (!func) {
3696 return;
3697 }
3698
3699 item_menu(this, name, icon, func, arg, nullptr, "", false);
3700}
3701
3702void Layout::menu_fn_argN_free(const StringRefNull name, int icon, MenuCreateFunc func, void *argN)
3703{
3704 if (!func) {
3705 return;
3706 }
3707
3708 /* Second 'argN' only ensures it gets freed. */
3709 item_menu(this, name, icon, func, argN, argN, "", false);
3710}
3711
3714 /* don't use pointers to the strings because python can dynamically
3715 * allocate strings and free before the menu draws, see #27304. */
3719};
3720
3721/* Obtain the active menu item based on the calling button's text. */
3723{
3724 wmOperatorType *ot = WM_operatortype_find(lvl->opname, true);
3725
3726 if (!ot) {
3727 return -1;
3728 }
3729
3730 const EnumPropertyItem *item_array = nullptr;
3731 bool free;
3732 int totitem;
3734 /* so the context is passed to itemf functions (some need it) */
3736 PropertyRNA *prop = RNA_struct_find_property(&ptr, lvl->propname);
3737 if (!prop) {
3738 return -1;
3739 }
3740 RNA_property_enum_items_gettexted(C, &ptr, prop, &item_array, &totitem, &free);
3741 int active = RNA_enum_from_name(item_array, but->str.c_str());
3742 if (free) {
3743 MEM_delete(item_array);
3744 }
3745
3746 return active;
3747}
3748
3749static void menu_item_enum_opname_menu(bContext *C, Layout *layout, void *arg)
3750{
3751 Button *but = static_cast<Button *>(arg);
3752 MenuItemLevel *lvl = static_cast<MenuItemLevel *>(but->func_argN);
3753 /* Use the operator properties from the button owning the menu. */
3754 BLI_assert(but->opptr);
3755 IDProperty *op_props = but->opptr->data_as<IDProperty>();
3756
3757 /* The calling but's str _probably_ contains the active
3758 * menu item name, set in #Layout::op_menu_enum. */
3759 const int active = menu_item_enum_opname_menu_active(C, but, lvl);
3760
3761 layout->operator_context_set(lvl->opcontext);
3762 layout->op_enum(lvl->opname, lvl->propname, op_props, lvl->opcontext, UI_ITEM_NONE, active);
3763
3764 /* override default, needed since this was assumed pre 2.70 */
3766}
3767
3769 wmOperatorType *ot,
3770 const StringRefNull propname,
3771 std::optional<StringRefNull> name,
3772 int icon)
3773{
3774 /* Caller must check */
3775 BLI_assert(ot->srna != nullptr);
3776
3777 std::string operator_name;
3778 if (!name) {
3779 operator_name = WM_operatortype_name(ot, nullptr);
3780 name = operator_name.c_str();
3781 }
3782
3783 if (root_->type == LayoutType::Menu && !icon) {
3784 icon = ICON_BLANK1;
3785 }
3786
3787 MenuItemLevel *lvl = MEM_new<MenuItemLevel>("MenuItemLevel");
3788 STRNCPY_UTF8(lvl->opname, ot->idname);
3789 STRNCPY_UTF8(lvl->propname, propname.c_str());
3790 lvl->opcontext = root_->opcontext;
3791
3792 Button *but = item_menu(this,
3793 *name,
3794 icon,
3796 nullptr,
3797 lvl,
3798 std::nullopt,
3799 true,
3802 /* Use the menu button as owner for the operator properties, which will then be passed to the
3803 * individual menu items. */
3804 but->opptr = MEM_new<PointerRNA>("uiButOpPtr", WM_operator_properties_create_ptr(ot));
3805 BLI_assert(!*but->opptr);
3807 &but->opptr, reinterpret_cast<IDProperty **>(&but->opptr->data), ot->idname);
3808
3809 /* add hotkey here, lower UI code can't detect it */
3810 if ((this->block()->flag & BLOCK_LOOP) && (ot->prop && ot->invoke)) {
3811 if (std::optional<std::string> shortcut_str = WM_key_event_operator_string(
3812 C, ot->idname, root_->opcontext, nullptr, false))
3813 {
3814 button_add_shortcut(but, shortcut_str->c_str(), false);
3815 }
3816 }
3817 return *but->opptr;
3818}
3819
3821 const StringRefNull opname,
3822 const StringRefNull propname,
3823 StringRefNull name,
3824 int icon)
3825{
3826 wmOperatorType *ot = WM_operatortype_find(opname.c_str(), false); /* print error next */
3827
3828 UI_OPERATOR_ERROR_RET(ot, opname.c_str(), "UILayout.operator_menu_enum()");
3829
3830 if (!ot->srna) {
3831 item_disabled(this, opname.c_str());
3832 RNA_warning_bare("UILayout.operator_menu_enum(): operator missing srna '%s'", opname.c_str());
3833 return {};
3834 }
3835
3836 return this->op_menu_enum(C, ot, propname, name, icon);
3837}
3838
3839static void menu_item_enum_rna_menu(bContext * /*C*/, Layout *layout, void *arg)
3840{
3841 MenuItemLevel *lvl = static_cast<MenuItemLevel *>((static_cast<Button *>(arg))->func_argN);
3842
3843 layout->operator_context_set(lvl->opcontext);
3844 layout->props_enum(&lvl->rnapoin, lvl->propname);
3845}
3846
3849 const std::optional<StringRefNull> name,
3850 int icon)
3851{
3852 if (root_->type == LayoutType::Menu && !icon) {
3853 icon = ICON_BLANK1;
3854 }
3855
3856 MenuItemLevel *lvl = MEM_new<MenuItemLevel>("MenuItemLevel");
3857 lvl->rnapoin = *ptr;
3859 lvl->opcontext = root_->opcontext;
3860
3861 item_menu(this,
3862 name.value_or(RNA_property_ui_name(prop)),
3863 icon,
3865 nullptr,
3866 lvl,
3868 false,
3871}
3872
3874 PointerRNA *ptr,
3876 PointerRNA *ptr_highlight,
3877 PropertyRNA *prop_highlight,
3878 bool icon_only,
3879 EnumTabExpand expand_as)
3880{
3881 Block *block = this->block();
3882
3885 C,
3886 block,
3887 ptr,
3888 prop,
3889 ptr_highlight,
3890 prop_highlight,
3891 std::nullopt,
3892 UI_UNIT_Y,
3893 icon_only,
3894 expand_as);
3895}
3896
3898
3899/* -------------------------------------------------------------------- */
3902
3904{
3905 layout->estimate();
3906}
3907
3909{
3910 layout->resolve();
3911 if (layout->root_->use_dynamic_height) {
3912 layout->resolve_dynamic_height();
3913 }
3914}
3915
3916/* single-row layout */
3918{
3919 if (this->type() == ItemType::LayoutRoot) {
3920 return;
3921 }
3922 bool min_size_flag = true;
3923
3924 w_ = 0;
3925 h_ = 0;
3926
3927 if (this->items().is_empty()) {
3928 return;
3929 }
3930
3931 const Item *item_last = this->items().last();
3932 for (Item *item : this->items()) {
3933 const bool is_item_last = (item == item_last);
3934 const int2 size = item->size();
3935
3936 min_size_flag = min_size_flag && item->fixed_size();
3937
3938 w_ += size.x;
3939 h_ = std::max(size.y, h_);
3940
3941 if (!is_item_last) {
3942 w_ += space_;
3943 }
3944 }
3945
3946 if (min_size_flag) {
3947 this->fixed_size_set(true);
3948 }
3949}
3950
3951static int litem_min_width(int itemw)
3952{
3953 return std::min(2 * UI_UNIT_X, itemw);
3954}
3955
3957{
3958 if (this->items().is_empty()) {
3959 return;
3960 }
3961
3962 int last_free_item_idx = -1;
3963 int x, neww, newtotw, minw, offset;
3964 int freew, fixedx, freex, flag = 0, lastw = 0;
3965 float extra_pixel;
3966
3967 const int y = y_;
3968 int w = w_;
3969 int totw = 0;
3970 int tot = 0;
3971
3972 for (Item *item : this->items()) {
3973 totw += item->size().x;
3974 tot++;
3975 }
3976
3977 if (totw == 0) {
3978 return;
3979 }
3980
3981 if (w != 0) {
3982 w -= (tot - 1) * space_;
3983 }
3984 int fixedw = 0;
3985
3986 const Item *item_last = this->items().last();
3987
3988 /* keep clamping items to fixed minimum size until all are done */
3989 do {
3990 freew = 0;
3991 x = 0;
3992 flag = 0;
3993 newtotw = totw;
3994 extra_pixel = 0.0f;
3995
3996 for (Item *item : this->items()) {
3998 continue;
3999 }
4000 const bool is_item_last = (item == item_last);
4001
4002 int2 size = item->size();
4003 minw = litem_min_width(size.x);
4004
4005 if (w - lastw > 0) {
4006 neww = item_fit(size.x, x, totw, w - lastw, is_item_last, this->alignment(), &extra_pixel);
4007 }
4008 else {
4009 neww = 0; /* no space left, all will need clamping to minimum size */
4010 }
4011
4012 x += neww;
4013
4014 bool min_flag = item->fixed_size();
4015 /* ignore min flag for rows with right or center alignment */
4016 if (item->type() != ItemType::Button &&
4017 ELEM((static_cast<Layout *>(item))->alignment(),
4020 this->alignment() == LayoutAlign::Expand && this->fixed_size())
4021 {
4022 min_flag = false;
4023 }
4024
4025 if ((neww < minw || min_flag) && w != 0) {
4026 /* fixed size */
4028 if (item->type() != ItemType::Button && item->fixed_size()) {
4029 minw = size.x;
4030 }
4031 fixedw += minw;
4032 flag = 1;
4033 newtotw -= size.x;
4034 }
4035 else {
4036 /* keep free size */
4038 freew += size.x;
4039 }
4040 }
4041
4042 totw = newtotw;
4043 lastw = fixedw;
4044 } while (flag);
4045
4046 freex = 0;
4047 fixedx = 0;
4048 extra_pixel = 0.0f;
4049 x = x_;
4050
4051 int item_idx = -1;
4052 for (Item *item : this->items()) {
4053 item_idx++;
4054 const bool is_item_last = (item == item_last);
4055 int2 size = item->size();
4056 minw = litem_min_width(size.x);
4057
4059 /* fixed minimum size items */
4060 if (item->type() != ItemType::Button && item->fixed_size()) {
4061 minw = size.x;
4062 }
4063 size.x = item_fit(
4064 minw, fixedx, fixedw, min_ii(w, fixedw), is_item_last, this->alignment(), &extra_pixel);
4065 fixedx += size.x;
4066 }
4067 else {
4068 /* free size item */
4069 size.x = item_fit(
4070 size.x, freex, freew, w - fixedw, is_item_last, this->alignment(), &extra_pixel);
4071 freex += size.x;
4072 last_free_item_idx = item_idx;
4073 }
4074
4075 /* align right/center */
4076 offset = 0;
4077 if (this->alignment() == LayoutAlign::Right) {
4078 if (freew + fixedw > 0 && freew + fixedw < w) {
4079 offset = w - (fixedw + freew);
4080 }
4081 }
4082 else if (this->alignment() == LayoutAlign::Center) {
4083 if (freew + fixedw > 0 && freew + fixedw < w) {
4084 offset = (w - (fixedw + freew)) / 2;
4085 }
4086 }
4087
4088 /* position item */
4089 item_position(item, x + offset, y - size.y, size.x, size.y);
4090
4091 x += size.x;
4092 if (!is_item_last) {
4093 x += space_;
4094 }
4095 }
4096
4097 /* add extra pixel */
4098 int extra_pixel_move = w_ - (x - x_);
4099 if (extra_pixel_move > 0 && this->alignment() == LayoutAlign::Expand &&
4100 last_free_item_idx >= 0 && item_last && ItemInternal::auto_fixed_size(item_last))
4101 {
4102 item_move(this->items()[last_free_item_idx], 0, extra_pixel_move);
4103 Span<Item *> items_after_last_free = this->items().drop_front(last_free_item_idx + 1);
4104 for (Item *item : items_after_last_free) {
4105 item_move(item, extra_pixel_move, extra_pixel_move);
4106 }
4107 }
4108
4109 w_ = x - x_;
4110 h_ = y_ - y;
4111 x_ = x;
4112 y_ = y;
4113}
4114
4115static int spaces_after_column_item(const Layout *litem,
4116 const Item *item,
4117 const Item *next_item,
4118 const bool is_box)
4119{
4120 if (next_item == nullptr) {
4121 return item->type() == ItemType::LayoutPanelHeader ? 1 : 0;
4122 }
4123 if (item->type() == ItemType::LayoutPanelHeader &&
4124 next_item->type() == ItemType::LayoutPanelHeader)
4125 {
4126 /* No extra space between layout panel headers. */
4127 return 0;
4128 }
4129 if (item->type() == ItemType::LayoutPanelHeader &&
4130 next_item->type() == ItemType::LayoutPanelBody)
4131 {
4132 /* One for the end of the panel header and one for the start of panel body. */
4133 return 2;
4134 }
4135 if (item->type() == ItemType::LayoutPanelBody &&
4136 next_item->type() == ItemType::LayoutPanelHeader)
4137 {
4138 /* One for the end of the panel body and one for the start of panel header. */
4139 return 2;
4140 }
4141 if (!is_box) {
4142 return 1;
4143 }
4144 if (item != litem->items().first()) {
4145 return 1;
4146 }
4147 return 0;
4148}
4149
4150/* single-column layout */
4152{
4153 if (this->type() == ItemType::LayoutRoot) {
4154 return;
4155 }
4156 const bool is_box = this->type() == ItemType::LayoutBox;
4157 bool min_size_flag = true;
4158
4159 w_ = 0;
4160 h_ = 0;
4161
4162 for (const auto *iter = this->items().begin(); iter != this->items().end(); iter++) {
4163 Item *item = *iter;
4164 const int2 size = item->size();
4165
4166 min_size_flag = min_size_flag && item->fixed_size();
4167
4168 w_ = std::max(w_, size.x);
4169 h_ += size.y;
4170
4171 const Item *next_item = (item == this->items().last()) ? nullptr : *(iter + 1);
4172 const int spaces_num = spaces_after_column_item(this, item, next_item, is_box);
4173 h_ += spaces_num * space_;
4174 }
4175
4176 if (min_size_flag) {
4177 this->fixed_size_set(true);
4178 }
4179}
4180
4182{
4183 const bool is_box = this->type() == ItemType::LayoutBox;
4184 const bool is_menu = this->type() == ItemType::LayoutRoot &&
4185 this->root()->type == LayoutType::Menu;
4186 const int x = x_;
4187 int y = y_;
4188
4189 for (const auto *iter = this->items().begin(); iter != this->items().end(); iter++) {
4190 Item *item = *iter;
4191 const int2 size = item->size();
4192
4193 y -= size.y;
4194 item_position(item, x, y, is_menu ? size.x : w_, size.y);
4195
4196 const Item *next_item = (item == this->items().last()) ? nullptr : *(iter + 1);
4197 const int spaces_num = spaces_after_column_item(this, item, next_item, is_box);
4198 y -= spaces_num * space_;
4199
4200 if (is_box) {
4201 ItemInternal::box_item_set(item, true);
4202 }
4203 }
4204
4205 h_ = y_ - y;
4206 x_ = x;
4207 y_ = y;
4208}
4209
4210/* calculates the angle of a specified button in a radial menu,
4211 * stores a float vector in unit circle */
4212static RadialDirection get_radialbut_vec(float vec[2], short itemnum)
4213{
4214 if (itemnum >= PIE_MAX_ITEMS) {
4215 itemnum %= PIE_MAX_ITEMS;
4216 printf("Warning: Pie menus with more than %i items are currently unsupported\n",
4218 }
4219
4220 const RadialDirection dir = RadialDirection(radial_dir_order[itemnum]);
4221 button_pie_dir(dir, vec);
4222
4223 return dir;
4224}
4225
4227{
4228
4229 if ((item->type() == ItemType::Button) &&
4230 ((static_cast<ButtonItem *>(item))->but->type == ButtonType::Label))
4231 {
4232 return false;
4233 }
4234
4235 return true;
4236}
4237
4239{
4240
4242 return false;
4243 }
4244
4245 return true;
4246}
4247
4249{
4250 int itemnum = 0;
4251
4252 /* For the radial layout we will use Matt Ebb's design
4253 * for radiation, see http://mattebb.com/weblog/radiation/
4254 * also the old code at #5103. */
4255
4256 const int pie_radius = U.pie_menu_radius * UI_SCALE_FAC;
4257
4258 const int x = x_;
4259 const int y = y_;
4260
4261 int minx = x, miny = y, maxx = x, maxy = y;
4262
4263 this->block()->pie_data->pie_dir_mask = 0;
4264
4265 for (Item *item : this->items()) {
4266 /* Not all button types are drawn in a radial menu, do filtering here. */
4267 if (!item_is_radial_displayable(item)) {
4268 continue;
4269 }
4270
4271 float vec[2];
4272 const RadialDirection dir = get_radialbut_vec(vec, itemnum);
4273 const float factor[2] = {
4274 (vec[0] > 0.01f) ? 0.0f : ((vec[0] < -0.01f) ? -1.0f : -0.5f),
4275 (vec[1] > 0.99f) ? 0.0f : ((vec[1] < -0.99f) ? -1.0f : -0.5f),
4276 };
4277 itemnum++;
4278
4279 /* Enable for non-buttons because a direction may reference a layout, see: #112610. */
4280 bool use_dir = true;
4281
4282 if (item->type() == ItemType::Button) {
4283 ButtonItem *bitem = static_cast<ButtonItem *>(item);
4284
4285 bitem->but->pie_dir = dir;
4286 /* Scale the buttons. */
4287 bitem->but->rect.ymax *= 1.5f;
4288 /* Add a little bit more here to include number. */
4289 bitem->but->rect.xmax += 1.5f * UI_UNIT_X;
4290 /* Enable drawing as pie item if supported by widget. */
4291 if (item_is_radial_drawable(bitem)) {
4292 bitem->but->emboss = EmbossType::PieMenu;
4293 bitem->but->drawflag |= BUT_ICON_LEFT;
4294 }
4295
4297 use_dir = false;
4298 }
4299 }
4300
4301 if (use_dir) {
4302 this->block()->pie_data->pie_dir_mask |= 1 << int(dir);
4303 }
4304
4305 const int2 size = item->size();
4306
4307 item_position(item,
4308 x + (vec[0] * pie_radius) + (factor[0] * size.x),
4309 y + (vec[1] * pie_radius) + (factor[1] * size.y),
4310 size.x,
4311 size.y);
4312
4313 minx = min_ii(minx, x + (vec[0] * pie_radius) - (size.x / 2));
4314 maxx = max_ii(maxx, x + (vec[0] * pie_radius) + (size.x / 2));
4315 miny = min_ii(miny, y + (vec[1] * pie_radius) - (size.y / 2));
4316 maxy = max_ii(maxy, y + (vec[1] * pie_radius) + (size.y / 2));
4317 }
4318
4319 x_ = minx;
4320 y_ = miny;
4321 w_ = maxx - minx;
4322 h_ = maxy - miny;
4323}
4324
4326{
4327 /* nothing to do */
4328}
4330{
4331 /* Nothing to do. */
4332}
4333
4335{
4336 /* first item is pie menu title, align on center of menu */
4337 Item *item = this->items().first();
4338
4339 if (item->type() == ItemType::Button) {
4340 int x, y;
4341 x = x_;
4342 y = y_;
4343
4344 const int2 size = item->size();
4345
4347 item, x - size.x / 2, y + UI_SCALE_FAC * (U.pie_menu_threshold + 9.0f), size.x, size.y);
4348 }
4349}
4350
4351/* panel header layout */
4353{
4354 BLI_assert(this->items().size() == 1);
4355 Item *item = this->items().first();
4356
4357 const int2 size = item->size();
4358 w_ = size.x;
4359 h_ = size.y;
4360}
4361
4363{
4364 Panel *panel = this->root_panel();
4365
4366 BLI_assert(this->items().size() == 1);
4367 Item *item = this->items().first();
4368
4369 const int2 size = item->size();
4370 y_ -= size.y;
4371 item_position(item, x_, y_, w_, size.y);
4372 this->index = panel->runtime->layout_panels.headers.size();
4375}
4376
4378{
4380 const Panel *panel = this->root_panel();
4382 header.start_y = float(y_);
4383 header.end_y = float(y_ + h_);
4384}
4385
4386/* panel body layout */
4388{
4389 Panel *panel = this->root_panel();
4391 const int space = LayoutInternal::layout_space_get(this->parent_);
4392 this->index = panel->runtime->layout_panels.bodies.size();
4394 float(y_ - space),
4395 float(y_ + h_ + space),
4396 });
4397}
4398
4400{
4402 const Panel *panel = this->root_panel();
4404 const int space = LayoutInternal::layout_space_get(this->parent_);
4405 body.start_y = float(y_ - space);
4406 body.end_y = float(y_ + h_ + space);
4407}
4408
4409/* box layout */
4411{
4412 const uiStyle *style = this->root()->style;
4413
4415
4416 int boxspace = style->boxspace;
4417 if (this->root()->type == LayoutType::Header) {
4418 boxspace = 0;
4419 }
4420 w_ += 2 * boxspace;
4421 h_ += 2 * boxspace;
4422}
4423
4425{
4426 const uiStyle *style = this->root()->style;
4427
4428 int boxspace = style->boxspace;
4429 if (this->root()->type == LayoutType::Header) {
4430 boxspace = 0;
4431 }
4432
4433 const int w = w_;
4434 const int h = h_;
4435
4436 x_ += boxspace;
4437 y_ -= boxspace;
4438
4439 if (w != 0) {
4440 w_ -= 2 * boxspace;
4441 }
4442 if (h != 0) {
4443 h_ -= 2 * boxspace;
4444 }
4445
4447
4448 x_ -= boxspace;
4449 y_ -= boxspace;
4450
4451 if (w != 0) {
4452 w_ += 2 * boxspace;
4453 }
4454 if (h != 0) {
4455 h_ += 2 * boxspace;
4456 }
4457
4458 /* roundbox around the sublayout */
4459 Button *but = this->roundbox;
4460 but->rect.xmin = x_;
4461 but->rect.ymin = y_;
4462 but->rect.xmax = x_ + w_;
4463 but->rect.ymax = y_ + h_;
4464}
4465
4467{
4469 /* roundbox around the sublayout */
4470 Button *but = this->roundbox;
4471 but->rect.ymin = y_;
4472 but->rect.ymax = y_ + h_;
4473}
4474
4475/* multi-column layout, automatically flowing to the next */
4477{
4478 const uiStyle *style = this->root()->style;
4479 LayoutItemFlow *flow = this;
4480
4481 int maxw = 0;
4482
4483 /* compute max needed width and total height */
4484 int toth = 0;
4485 int totitem = 0;
4486 for (Item *item : this->items()) {
4487 const int2 size = item->size();
4488 maxw = std::max(maxw, size.x);
4489 toth += size.y;
4490 totitem++;
4491 }
4492
4493 if (flow->number <= 0) {
4494 /* auto compute number of columns, not very good */
4495 if (maxw == 0) {
4496 flow->totcol = 1;
4497 return;
4498 }
4499
4500 flow->totcol = max_ii(this->root()->emw / maxw, 1);
4501 flow->totcol = min_ii(flow->totcol, totitem);
4502 }
4503 else {
4504 flow->totcol = flow->number;
4505 }
4506
4507 /* compute sizes */
4508 int x = 0;
4509 int y = 0;
4510 int emy = 0;
4511 int miny = 0;
4512
4513 maxw = 0;
4514 const int emh = toth / flow->totcol;
4515
4516 /* create column per column */
4517 int col = 0;
4518 for (Item *item : this->items()) {
4519 const int2 size = item->size();
4520
4521 y -= size.y + style->buttonspacey;
4522 miny = min_ii(miny, y);
4523 emy -= size.y;
4524 maxw = max_ii(size.x, maxw);
4525
4526 /* decide to go to next one */
4527 if (col < flow->totcol - 1 && emy <= -emh) {
4528 x += maxw + space_;
4529 maxw = 0;
4530 y = 0;
4531 emy = 0; /* need to reset height again for next column */
4532 col++;
4533 }
4534 }
4535
4536 w_ = x;
4537 h_ = y_ - miny;
4538}
4539
4541{
4542 const uiStyle *style = this->root()->style;
4543 int col, emh;
4544
4545 /* compute max needed width and total height */
4546 int toth = 0;
4547 for (Item *item : this->items()) {
4548 const int2 size = item->size();
4549 toth += size.y;
4550 }
4551
4552 /* compute sizes */
4553 int x = x_;
4554 int y = y_;
4555 int emy = 0;
4556 int miny = 0;
4557
4558 emh = toth / this->totcol;
4559
4560 /* create column per column */
4561 col = 0;
4562 int w = (w_ - (this->totcol - 1) * style->columnspace) / this->totcol;
4563 for (Item *item : this->items()) {
4564 int2 size = item->size();
4565
4566 size.x = (this->alignment() == LayoutAlign::Expand) ? w : min_ii(w, size.x);
4567
4568 y -= size.y;
4569 emy -= size.y;
4570 item_position(item, x, y, size.x, size.y);
4571 y -= style->buttonspacey;
4572 miny = min_ii(miny, y);
4573
4574 /* decide to go to next one */
4575 if (col < this->totcol - 1 && emy <= -emh) {
4576 x += w + style->columnspace;
4577 y = y_;
4578 emy = 0; /* need to reset height again for next column */
4579 col++;
4580
4581 const int remaining_width = w_ - (x - x_);
4582 const int remaining_width_between_columns = (this->totcol - col - 1) * style->columnspace;
4583 const int remaining_columns = this->totcol - col;
4584 w = (remaining_width - remaining_width_between_columns) / remaining_columns;
4585 }
4586 }
4587
4588 h_ = y_ - miny;
4589 x_ = x;
4590 y_ = miny;
4591}
4592
4593/* multi-column and multi-row layout. */
4595 /* General layout control settings. */
4596 bool row_major : 1; /* Fill rows before columns */
4597 bool even_columns : 1; /* All columns will have same width. */
4598 bool even_rows : 1; /* All rows will have same height. */
4599 int space_x; /* Space between columns. */
4600 int space_y; /* Space between rows. */
4601 /* Real data about current position and size of this layout item
4602 * (either estimated, or final values). */
4603 int litem_w; /* Layout item width. */
4604 int litem_x; /* Layout item X position. */
4605 int litem_y; /* Layout item Y position. */
4606 /* Actual number of columns and rows to generate (computed from first pass usually). */
4607 int tot_columns; /* Number of columns. */
4608 int tot_rows; /* Number of rows. */
4609};
4610
4612 int *tot_items; /* Total number of items in this grid layout. */
4613 /* Width / X pos data. */
4614 float *global_avg_w; /* Computed average width of the columns. */
4615 int *cos_x_array; /* Computed X coordinate of each column. */
4616 int *widths_array; /* Computed width of each column. */
4617 int *tot_w; /* Computed total width. */
4618 /* Height / Y pos data. */
4619 int *global_max_h; /* Computed height of the tallest item in the grid. */
4620 int *cos_y_array; /* Computed Y coordinate of each column. */
4621 int *heights_array; /* Computed height of each column. */
4622 int *tot_h; /* Computed total height. */
4623};
4624
4626 const UILayoutGridFlowInput *parameters,
4627 UILayoutGridFlowOutput *results)
4628{
4629 float tot_w = 0.0f, tot_h = 0.0f;
4630 float global_avg_w = 0.0f, global_totweight_w = 0.0f;
4631 int global_max_h = 0;
4632
4633 BLI_assert(parameters->tot_columns != 0 ||
4634 (results->cos_x_array == nullptr && results->widths_array == nullptr &&
4635 results->tot_w == nullptr));
4636 BLI_assert(parameters->tot_rows != 0 ||
4637 (results->cos_y_array == nullptr && results->heights_array == nullptr &&
4638 results->tot_h == nullptr));
4639
4640 if (results->tot_items) {
4641 *results->tot_items = 0;
4642 }
4643
4644 if (items.is_empty()) {
4645 if (results->global_avg_w) {
4646 *results->global_avg_w = 0.0f;
4647 }
4648 if (results->global_max_h) {
4649 *results->global_max_h = 0;
4650 }
4651 return;
4652 }
4653
4654 Array<float, 64> avg_w(parameters->tot_columns, 0.0f);
4655 Array<float, 64> totweight_w(parameters->tot_columns, 0.0f);
4656 Array<int, 64> max_h(parameters->tot_rows, 0);
4657
4658 int i = 0;
4659 for (const Item *item : items) {
4660 const int2 size = item->size();
4661
4662 global_avg_w += float(size.x * size.x);
4663 global_totweight_w += float(size.x);
4664 global_max_h = max_ii(global_max_h, size.y);
4665
4666 if (parameters->tot_rows != 0 && parameters->tot_columns != 0) {
4667 const int index_col = parameters->row_major ? i % parameters->tot_columns :
4668 i / parameters->tot_rows;
4669 const int index_row = parameters->row_major ? i / parameters->tot_columns :
4670 i % parameters->tot_rows;
4671
4672 avg_w[index_col] += float(size.x * size.x);
4673 totweight_w[index_col] += float(size.x);
4674
4675 max_h[index_row] = max_ii(max_h[index_row], size.y);
4676 }
4677
4678 if (results->tot_items) {
4679 (*results->tot_items)++;
4680 }
4681 i++;
4682 }
4683
4684 /* Finalize computing of column average sizes */
4685 global_avg_w /= global_totweight_w;
4686 if (parameters->tot_columns != 0) {
4687 for (i = 0; i < parameters->tot_columns; i++) {
4688 avg_w[i] /= totweight_w[i];
4689 tot_w += avg_w[i];
4690 }
4691 if (parameters->even_columns) {
4692 tot_w = ceilf(global_avg_w) * parameters->tot_columns;
4693 }
4694 }
4695 /* Finalize computing of rows max sizes */
4696 if (parameters->tot_rows != 0) {
4697 for (i = 0; i < parameters->tot_rows; i++) {
4698 tot_h += max_h[i];
4699 }
4700 if (parameters->even_rows) {
4701 tot_h = global_max_h * parameters->tot_columns;
4702 }
4703 }
4704
4705 /* Compute positions and sizes of all cells. */
4706 if (results->cos_x_array != nullptr && results->widths_array != nullptr) {
4707 /* We enlarge/narrow columns evenly to match available width. */
4708 const float wfac = float(parameters->litem_w -
4709 (parameters->tot_columns - 1) * parameters->space_x) /
4710 tot_w;
4711
4712 for (int col = 0; col < parameters->tot_columns; col++) {
4713 results->cos_x_array[col] = (col ? results->cos_x_array[col - 1] +
4714 results->widths_array[col - 1] + parameters->space_x :
4715 parameters->litem_x);
4716 if (parameters->even_columns) {
4717 /* (< remaining width > - < space between remaining columns >) / < remaining columns > */
4718 results->widths_array[col] = (((parameters->litem_w -
4719 (results->cos_x_array[col] - parameters->litem_x)) -
4720 (parameters->tot_columns - col - 1) * parameters->space_x) /
4721 (parameters->tot_columns - col));
4722 }
4723 else if (col == parameters->tot_columns - 1) {
4724 /* Last column copes width rounding errors... */
4725 results->widths_array[col] = parameters->litem_w -
4726 (results->cos_x_array[col] - parameters->litem_x);
4727 }
4728 else {
4729 results->widths_array[col] = int(avg_w[col] * wfac);
4730 }
4731 }
4732 }
4733 if (results->cos_y_array != nullptr && results->heights_array != nullptr) {
4734 for (int row = 0; row < parameters->tot_rows; row++) {
4735 if (parameters->even_rows) {
4736 results->heights_array[row] = global_max_h;
4737 }
4738 else {
4739 results->heights_array[row] = max_h[row];
4740 }
4741 results->cos_y_array[row] = (row ? results->cos_y_array[row - 1] - parameters->space_y -
4742 results->heights_array[row] :
4743 parameters->litem_y - results->heights_array[row]);
4744 }
4745 }
4746
4747 if (results->global_avg_w) {
4748 *results->global_avg_w = global_avg_w;
4749 }
4750 if (results->global_max_h) {
4751 *results->global_max_h = global_max_h;
4752 }
4753 if (results->tot_w) {
4754 *results->tot_w = int(tot_w) + parameters->space_x * (parameters->tot_columns - 1);
4755 }
4756 if (results->tot_h) {
4757 *results->tot_h = tot_h + parameters->space_y * (parameters->tot_rows - 1);
4758 }
4759}
4760
4762{
4763 const uiStyle *style = this->root()->style;
4764 LayoutItemGridFlow *gflow = this;
4765
4766 const int space_x = style->columnspace;
4767 const int space_y = style->buttonspacey;
4768
4769 /* Estimate average needed width and height per item. */
4770 {
4771 float avg_w;
4772 int max_h;
4773
4774 UILayoutGridFlowInput input{};
4775 input.row_major = gflow->row_major;
4776 input.even_columns = gflow->even_columns;
4777 input.even_rows = gflow->even_rows;
4778 input.litem_w = w_;
4779 input.litem_x = x_;
4780 input.litem_y = y_;
4781 input.space_x = space_x;
4782 input.space_y = space_y;
4784 output.tot_items = &gflow->tot_items;
4785 output.global_avg_w = &avg_w;
4786 output.global_max_h = &max_h;
4787 litem_grid_flow_compute(this->items(), &input, &output);
4788
4789 if (gflow->tot_items == 0) {
4790 w_ = h_ = 0;
4791 gflow->tot_columns = gflow->tot_rows = 0;
4792 return;
4793 }
4794
4795 /* Even in varying column width case,
4796 * we fix our columns number from weighted average width of items,
4797 * a proper solving of required width would be too costly,
4798 * and this should give reasonably good results in all reasonable cases. */
4799 if (gflow->columns_len > 0) {
4800 gflow->tot_columns = gflow->columns_len;
4801 }
4802 else {
4803 if (avg_w == 0.0f) {
4804 gflow->tot_columns = 1;
4805 }
4806 else {
4807 gflow->tot_columns = min_ii(max_ii(int(w_ / avg_w), 1), gflow->tot_items);
4808 }
4809 }
4810 gflow->tot_rows = int(ceilf(float(gflow->tot_items) / gflow->tot_columns));
4811
4812 /* Try to tweak number of columns and rows to get better filling of last column or row,
4813 * and apply 'modulo' value to number of columns or rows.
4814 * Note that modulo does not prevent ending with fewer columns/rows than modulo, if mandatory
4815 * to avoid empty column/row. */
4816 {
4817 const int modulo = (gflow->columns_len < -1) ? -gflow->columns_len : 0;
4818 const int step = modulo ? modulo : 1;
4819
4820 if (gflow->row_major) {
4821 /* Adjust number of columns to be multiple of given modulo. */
4822 if (modulo && gflow->tot_columns % modulo != 0 && gflow->tot_columns > modulo) {
4823 gflow->tot_columns = gflow->tot_columns - (gflow->tot_columns % modulo);
4824 }
4825 /* Find smallest number of columns conserving computed optimal number of rows. */
4826 for (gflow->tot_rows = int(ceilf(float(gflow->tot_items) / gflow->tot_columns));
4827 (gflow->tot_columns - step) > 0 &&
4828 int(ceilf(float(gflow->tot_items) / (gflow->tot_columns - step))) <= gflow->tot_rows;
4829 gflow->tot_columns -= step)
4830 {
4831 /* pass */
4832 }
4833 }
4834 else {
4835 /* Adjust number of rows to be multiple of given modulo. */
4836 if (modulo && gflow->tot_rows % modulo != 0) {
4837 gflow->tot_rows = min_ii(gflow->tot_rows + modulo - (gflow->tot_rows % modulo),
4838 gflow->tot_items);
4839 }
4840 /* Find smallest number of rows conserving computed optimal number of columns. */
4841 for (gflow->tot_columns = int(ceilf(float(gflow->tot_items) / gflow->tot_rows));
4842 (gflow->tot_rows - step) > 0 &&
4843 int(ceilf(float(gflow->tot_items) / (gflow->tot_rows - step))) <= gflow->tot_columns;
4844 gflow->tot_rows -= step)
4845 {
4846 /* pass */
4847 }
4848 }
4849 }
4850
4851 /* Set evenly-spaced axes size
4852 * (quick optimization in case we have even columns and rows). */
4853 if (gflow->even_columns && gflow->even_rows) {
4854 w_ = int(gflow->tot_columns * avg_w) + space_x * (gflow->tot_columns - 1);
4855 h_ = int(gflow->tot_rows * max_h) + space_y * (gflow->tot_rows - 1);
4856 return;
4857 }
4858 }
4859
4860 /* Now that we have our final number of columns and rows,
4861 * we can compute actual needed space for non-evenly sized axes. */
4862 {
4863 int tot_w, tot_h;
4864 UILayoutGridFlowInput input{};
4865 input.row_major = gflow->row_major;
4866 input.even_columns = gflow->even_columns;
4867 input.even_rows = gflow->even_rows;
4868 input.litem_w = w_;
4869 input.litem_x = x_;
4870 input.litem_y = y_;
4871 input.space_x = space_x;
4872 input.space_y = space_y;
4873 input.tot_columns = gflow->tot_columns;
4874 input.tot_rows = gflow->tot_rows;
4876 output.tot_w = &tot_w;
4877 output.tot_h = &tot_h;
4878 litem_grid_flow_compute(this->items(), &input, &output);
4879
4880 w_ = tot_w;
4881 h_ = tot_h;
4882 }
4883}
4884
4886{
4887 const uiStyle *style = this->root()->style;
4888
4889 if (this->tot_items == 0) {
4890 w_ = h_ = 0;
4891 return;
4892 }
4893
4894 BLI_assert(this->tot_columns > 0);
4895 BLI_assert(this->tot_rows > 0);
4896
4897 const int space_x = style->columnspace;
4898 const int space_y = style->buttonspacey;
4899
4900 Array<int, 64> widths(this->tot_columns);
4901 Array<int, 64> heights(this->tot_rows);
4902 Array<int, 64> cos_x(this->tot_columns);
4903 Array<int, 64> cos_y(this->tot_rows);
4904
4905 /* This time we directly compute coordinates and sizes of all cells. */
4906 UILayoutGridFlowInput input{};
4907 input.row_major = this->row_major;
4908 input.even_columns = this->even_columns;
4909 input.even_rows = this->even_rows;
4910 input.litem_w = w_;
4911 input.litem_x = x_;
4912 input.litem_y = y_;
4913 input.space_x = space_x;
4914 input.space_y = space_y;
4915 input.tot_columns = this->tot_columns;
4916 input.tot_rows = this->tot_rows;
4918 output.cos_x_array = cos_x.data();
4919 output.cos_y_array = cos_y.data();
4920 output.widths_array = widths.data();
4921 output.heights_array = heights.data();
4922 litem_grid_flow_compute(this->items(), &input, &output);
4923
4924 int i = 0;
4925 for (Item *item : this->items()) {
4926 const int col = this->row_major ? i % this->tot_columns : i / this->tot_rows;
4927 const int row = this->row_major ? i / this->tot_columns : i % this->tot_rows;
4928 int2 size = item->size();
4929
4930 const int w = widths[col];
4931 const int h = heights[row];
4932 if (this->alignment() == LayoutAlign::Expand) {
4933 size = {w, h};
4934 }
4935 else {
4936 size = {min_ii(w, size.x), min_ii(h, size.y)};
4937 }
4938
4939 item_position(item, cos_x[col], cos_y[row], size.x, size.y);
4940 i++;
4941 }
4942
4943 h_ = y_ - cos_y[this->tot_rows - 1];
4944 x_ = (cos_x[this->tot_columns - 1] - x_) + widths[this->tot_columns - 1];
4945 y_ = y_ - h_;
4946}
4947
4948/* free layout */
4950{
4951 int minx = 1e6;
4952 int miny = 1e6;
4953 w_ = 0;
4954 h_ = 0;
4955
4956 for (Item *item : this->items()) {
4957 const int2 offset = item->offset();
4958 const int2 size = item->size();
4959
4960 minx = min_ii(minx, offset.x);
4961 miny = min_ii(miny, offset.y);
4962
4963 w_ = std::max(w_, offset.x + size.x);
4964 h_ = std::max(h_, offset.y + size.y);
4965 }
4966
4967 w_ -= minx;
4968 h_ -= miny;
4969}
4970
4972{
4973 float scalex = 1.0f, scaley = 1.0f;
4974 int x, y, newx, newy;
4975
4976 int minx = 1e6;
4977 int miny = 1e6;
4978 int totw = 0;
4979 int toth = 0;
4980
4981 for (Item *item : this->items()) {
4982 const int2 offset = item->offset();
4983 const int2 size = item->size();
4984
4985 minx = min_ii(minx, offset.x);
4986 miny = min_ii(miny, offset.y);
4987
4988 totw = max_ii(totw, offset.x + size.x);
4989 toth = max_ii(toth, offset.y + size.y);
4990 }
4991
4992 totw -= minx;
4993 toth -= miny;
4994
4995 if (w_ && totw > 0) {
4996 scalex = float(w_) / float(totw);
4997 }
4998 if (h_ && toth > 0) {
4999 scaley = float(h_) / float(toth);
5000 }
5001
5002 x = x_;
5003 y = y_ - scaley * toth;
5004
5005 for (Item *item : this->items()) {
5006 int2 offset = item->offset();
5007 int2 size = item->size();
5008
5009 if (scalex != 1.0f) {
5010 newx = (offset.x - minx) * scalex;
5011 size.x = (offset.x - minx + size.x) * scalex - newx;
5012 offset.x = minx + newx;
5013 }
5014
5015 if (scaley != 1.0f) {
5016 newy = (offset.y - miny) * scaley;
5017 size.y = (offset.y - miny + size.y) * scaley - newy;
5018 offset.y = miny + newy;
5019 }
5020
5021 item_position(item, x + offset.x - minx, y + offset.y - miny, size.x, size.y);
5022 }
5023
5024 w_ = scalex * totw;
5025 h_ = y_ - y;
5026 x_ = x + w_;
5027 y_ = y;
5028}
5029
5030/* split layout */
5032{
5034 this->fixed_size_set(false);
5035}
5036
5038{
5039 float extra_pixel = 0.0f;
5040 const int tot = int(this->items().size());
5041
5042 if (tot == 0) {
5043 return;
5044 }
5045
5046 int x = x_;
5047 const int y = y_;
5048
5049 const float percentage = (this->percentage == 0.0f) ? 1.0f / float(tot) : this->percentage;
5050
5051 const int w = (w_ - (tot - 1) * space_);
5052 int colw = w * percentage;
5053 colw = std::max(colw, 0);
5054
5055 const Item *item_last = this->items().last();
5056 for (Item *item : this->items()) {
5057 const bool is_item_last = (item == item_last);
5058 const int2 size = item->size();
5059
5060 item_position(item, x, y - size.y, colw, size.y);
5061 x += colw;
5062
5063 if (!is_item_last) {
5064 const float width = extra_pixel + (w - int(w * percentage)) / (float(tot) - 1);
5065 extra_pixel = width - int(width);
5066 colw = int(width);
5067 colw = std::max(colw, 0);
5068
5069 x += space_;
5070 }
5071 }
5072
5073 w_ = x - x_;
5074 h_ = y_ - y;
5075 x_ = x;
5076 y_ = y;
5077}
5078
5079/* overlap layout */
5081{
5082 w_ = 0;
5083 h_ = 0;
5084
5085 for (Item *item : this->items()) {
5086 const int2 size = item->size();
5087
5088 w_ = std::max(size.x, w_);
5089 h_ = std::max(size.y, h_);
5090 }
5091}
5092
5094{
5095
5096 const int x = x_;
5097 const int y = y_;
5098
5099 for (Item *item : this->items()) {
5100 const int2 size = item->size();
5101 item_position(item, x, y - size.y, w_, size.y);
5102
5103 h_ = std::max(h_, size.y);
5104 }
5105
5106 x_ = x;
5107 y_ = y - h_;
5108}
5109
5110void LayoutInternal::init_from_parent(Layout *litem, Layout *layout, int align)
5111{
5112 litem->root_ = layout->root_;
5113 litem->align_ = align;
5114 /* Children of grid-flow layout shall never have "ideal big size" returned as estimated size. */
5115 litem->variable_size_ = layout->variable_size_ || layout->type() == ItemType::LayoutGridFlow;
5116 litem->active_ = true;
5117 litem->enabled_ = true;
5118 litem->context_ = layout->context_;
5119 litem->redalert_ = layout->redalert_;
5120 litem->w_ = layout->w_;
5121 litem->emboss_ = layout->emboss_;
5124
5125 if (layout->child_items_layout_) {
5126 layout->child_items_layout_->items_.append(litem);
5127 litem->parent_ = layout->child_items_layout_;
5128 }
5129 else {
5130 layout->items_.append(litem);
5131 litem->parent_ = layout;
5132 }
5133}
5134
5136{
5137 Layout *litem = MEM_new<LayoutRow>(__func__, nullptr);
5139
5140 litem->space_ = (align) ? 0 : root_->style->buttonspacex;
5141
5142 block_layout_set_current(this->block(), litem);
5143
5144 return *litem;
5145}
5146
5148 PointerRNA *open_prop_owner,
5149 const StringRefNull open_prop_name)
5150{
5151 PropertyRNA *prop = RNA_struct_find_property(open_prop_owner, open_prop_name.c_str());
5152
5153 if (!prop) {
5154 item_disabled(this, open_prop_name.c_str());
5155 RNA_warning_bare("UILayout.panel_prop(): property not found: %s.%s",
5156 RNA_struct_identifier(open_prop_owner->type),
5157 open_prop_name.c_str());
5158 return PanelLayout{};
5159 }
5161 item_disabled(this, open_prop_name.c_str());
5162 RNA_warning_bare("UILayout.panel_prop(): %s.%s is not an boolean property",
5163 RNA_struct_identifier(open_prop_owner->type),
5164 open_prop_name.c_str());
5165 return PanelLayout{};
5166 }
5167
5168 const ARegion *region = CTX_wm_region(C);
5169
5170 const bool is_real_open = RNA_property_boolean_get(open_prop_owner, prop);
5171 const bool search_filter_active = region && (region->flag & RGN_FLAG_SEARCH_FILTER_ACTIVE);
5172 const bool is_open = is_real_open || search_filter_active;
5173
5174 PanelLayout panel_layout{};
5175 {
5176 LayoutItemPanelHeader *header_litem = MEM_new<LayoutItemPanelHeader>(__func__);
5177 LayoutInternal::init_from_parent(header_litem, this, false);
5178
5179 header_litem->open_prop_owner = *open_prop_owner;
5180 header_litem->open_prop_name = open_prop_name;
5181
5182 Layout *row = &header_litem->row(true);
5183
5184 Block *block = row->block();
5185
5186 const bool is_popup = block_is_popup_any(block);
5187 bool inside_layout_panel = false;
5188
5189 if (is_popup) {
5190 Layout *parent = this;
5191 while (parent) {
5192 inside_layout_panel = parent->type_ == ItemType::LayoutPanelBody;
5194 if (inside_layout_panel) {
5195 break;
5196 }
5197 }
5198 }
5199 if (!is_popup || inside_layout_panel) {
5200 uiDefBut(this->block(),
5202 "",
5203 0,
5204 0,
5205 std::round(0.85 * UI_UNIT_X - float(root_->style->panelspace)),
5206 0,
5207 nullptr,
5208 0.0,
5209 0.0,
5210 "");
5211 }
5212 const int icon = is_open ? ICON_DOWNARROW_HLT : ICON_RIGHTARROW;
5213 const int icon_width = (UI_UNIT_X * 0.9) + 1.1f * UI_SCALE_FAC;
5214 uiDefIconTextBut(block, ButtonType::Label, icon, "", 0, 0, icon_width, UI_UNIT_Y, nullptr, "");
5215
5216 panel_layout.header = row;
5217 }
5218
5219 if (!is_open) {
5220 return panel_layout;
5221 }
5222
5223 LayoutItemPanelBody *body_litem = MEM_new<LayoutItemPanelBody>(__func__);
5224 body_litem->space_ = root_->style->templatespace;
5225 LayoutInternal::init_from_parent(body_litem, this, false);
5226 block_layout_set_current(this->block(), body_litem);
5227 panel_layout.body = body_litem;
5228
5229 return panel_layout;
5230}
5231
5233 PointerRNA *open_prop_owner,
5234 const StringRefNull open_prop_name,
5235 PointerRNA *bool_prop_owner,
5236 const StringRefNull bool_prop_name,
5237 const std::optional<StringRef> label)
5238{
5239 PanelLayout panel_layout = this->panel_prop(C, open_prop_owner, open_prop_name);
5240 if (!panel_layout.header) {
5241 return panel_layout;
5242 }
5243
5244 Layout *panel_header = panel_layout.header;
5247 panel_header->prop(bool_prop_owner, bool_prop_name, UI_ITEM_NONE, label, ICON_NONE);
5248
5249 return panel_layout;
5250}
5251
5253 PointerRNA *open_prop_owner,
5254 const StringRefNull open_prop_name,
5255 const StringRef label)
5256{
5257 PanelLayout panel_layout = this->panel_prop(C, open_prop_owner, open_prop_name);
5258 if (!panel_layout.header) {
5259 return nullptr;
5260 }
5261 panel_layout.header->label(label, ICON_NONE);
5262
5263 return panel_layout.body;
5264}
5265
5266PanelLayout Layout::panel(const bContext *C, const StringRef idname, const bool default_closed)
5267{
5268 Panel *root_panel = this->root_panel();
5269 BLI_assert(root_panel != nullptr);
5270
5272 root_panel, idname, default_closed);
5273 PointerRNA state_ptr = RNA_pointer_create_discrete(nullptr, RNA_LayoutPanelState, state);
5274
5275 return this->panel_prop(C, &state_ptr, "is_open");
5276}
5277
5279 const StringRef idname,
5280 const bool default_closed,
5281 const StringRef label)
5282{
5283 PanelLayout panel_layout = this->panel(C, idname, default_closed);
5284 panel_layout.header->label(label, ICON_NONE);
5285
5286 return panel_layout.body;
5287}
5288
5290{
5291 if (layout.items().is_empty()) {
5292 return false;
5293 }
5294 const Item *item = layout.items().last();
5295 return item->type() == ItemType::LayoutPanelHeader;
5296}
5297
5299{
5300 Layout &litem = this->row(align);
5301 litem.heading_ = heading;
5302 return litem;
5303}
5304
5306{
5307 Layout *litem = MEM_new<LayoutColumn>(__func__, nullptr);
5309
5310 litem->space_ = (align) ? 0 : root_->style->buttonspacey;
5311
5312 block_layout_set_current(this->block(), litem);
5313
5314 return *litem;
5315}
5316
5318{
5319 Layout &litem = this->column(align);
5320 litem.heading_ = heading;
5321 return litem;
5322}
5323
5325{
5326 LayoutItemFlow *flow = MEM_new<LayoutItemFlow>(__func__);
5328
5329 flow->space_ = flow->align() ? 0 : root_->style->columnspace;
5330 flow->number = number;
5331
5332 block_layout_set_current(this->block(), flow);
5333
5334 return *flow;
5335}
5336
5338 bool row_major, int columns_len, bool even_columns, bool even_rows, bool align)
5339{
5340 LayoutItemGridFlow *flow = MEM_new<LayoutItemGridFlow>(__func__);
5342
5343 flow->space_ = flow->align() ? 0 : root_->style->columnspace;
5344 flow->row_major = row_major;
5345 flow->columns_len = columns_len;
5346 flow->even_columns = even_columns;
5347 flow->even_rows = even_rows;
5348
5349 block_layout_set_current(this->block(), flow);
5350
5351 return *flow;
5352}
5353
5355{
5356 LayoutItemBx *box = MEM_new<LayoutItemBx>(__func__);
5357 LayoutInternal::init_from_parent(box, layout, false);
5358
5360
5361 block_layout_set_current(layout->block(), box);
5362
5363 box->roundbox = uiDefBut(layout->block(), type, "", 0, 0, 0, 0, nullptr, 0.0, 0.0, "");
5364
5365 return box;
5366}
5367
5369{
5370 /* radial layouts are only valid for radial menus */
5371 if (root_->type != LayoutType::PieMenu) {
5372 return *item_local_sublayout(this, this, false);
5373 }
5374
5375 /* only one radial wheel per root layout is allowed, so check and return that, if it exists */
5376 for (Item *item : root_->layout->items()) {
5377 if (item->type() == ItemType::LayoutRadial) {
5378 Layout *litem = static_cast<Layout *>(item);
5379 block_layout_set_current(this->block(), litem);
5380 return *litem;
5381 }
5382 }
5383
5384 Layout *litem = MEM_new<LayoutRadial>(__func__);
5385 LayoutInternal::init_from_parent(litem, this, false);
5386
5387 block_layout_set_current(this->block(), litem);
5388
5389 return *litem;
5390}
5391
5393{
5394 return *layout_box(this, ButtonType::Roundbox);
5395}
5396
5398{
5399 for (Item *item : layout->items()) {
5400 if (item->type() != ItemType::Button) {
5401 layout_list_set_labels_active(static_cast<Layout *>(item));
5402 }
5403 else {
5404 ButtonItem *bitem = static_cast<ButtonItem *>(item);
5405 if (bitem->but->flag & BUT_LIST_ITEM) {
5407 }
5408 }
5409 }
5410}
5411
5413{
5414 LayoutItemBx *item_box = layout_box(this, ButtonType::ListBox);
5415 Button *but = item_box->roundbox;
5416
5417 but->custom_data = ui_list;
5418
5419 but->rnapoin = *actptr;
5420 but->rnaprop = actprop;
5421
5422 /* only for the undo string */
5423 if (but->flag & BUT_UNDO) {
5424 but->tip = RNA_property_description(actprop);
5425 }
5426
5427 return *item_box;
5428}
5429
5431{
5432 Layout *litem = MEM_new<LayoutAbsolute>(__func__);
5434
5435 block_layout_set_current(this->block(), litem);
5436
5437 return *litem;
5438}
5439
5441{
5442 Layout *litem = MEM_new<LayoutOverlap>(__func__);
5443 LayoutInternal::init_from_parent(litem, this, false);
5444
5445 block_layout_set_current(this->block(), litem);
5446
5447 return *litem;
5448}
5449
5450Layout &Layout::split(float percentage, bool align)
5451{
5452 LayoutItemSplit *split = MEM_new<LayoutItemSplit>(__func__);
5454
5455 split->space_ = root_->style->columnspace;
5456 split->percentage = percentage;
5457
5459
5460 return *split;
5461}
5462
5467
5469{
5470 return flag_is_set(flag_, ItemInternalFlag::PropSep);
5471}
5472
5477
5479{
5480 return flag_is_set(flag_, ItemInternalFlag::PropDecorate);
5481}
5482
5487
5489{
5490 return this->block()->panel;
5491}
5492
5494{
5496 return this->block()->emboss;
5497 }
5498 return emboss_;
5499}
5500
5502{
5503 return 5 * UI_SCALE_FAC;
5504}
5505
5507{
5508 Block *block = layout->block();
5509 Layout *row = &layout->row(true);
5510 row->fixed_size_set(true);
5511
5512 uiDefBut(
5513 block, ButtonType::Sepr, "", 0, 0, uiLayoutListItemPaddingWidth(), 0, nullptr, 0.0, 0.0, "");
5514
5515 /* Restore. */
5516 block_layout_set_current(block, layout);
5517}
5518
5520
5521/* -------------------------------------------------------------------- */
5524
5525/* Disabled for performance reasons, but this could be turned on in the future. */
5526// #define PROPERTY_SEARCH_USE_TOOLTIPS
5527
5528static bool block_search_panel_label_matches(const Block *block, const char *search_string)
5529{
5530 if ((block->panel != nullptr) && (block->panel->type != nullptr)) {
5531 if (BLI_strcasestr(block->panel->type->label, search_string)) {
5532 return true;
5533 }
5534 }
5535 return false;
5536}
5537
5541static bool button_matches_search_filter(Button *but, const char *search_filter)
5542{
5543 /* Do the shorter checks first for better performance in case there is a match. */
5544 if (BLI_strcasestr(but->str.c_str(), search_filter)) {
5545 return true;
5546 }
5547
5548 if (but->optype != nullptr) {
5549 if (BLI_strcasestr(but->optype->name, search_filter)) {
5550 return true;
5551 }
5552 }
5553
5554 if (but->rnaprop != nullptr) {
5555 if (BLI_strcasestr(RNA_property_ui_name(but->rnaprop), search_filter)) {
5556 return true;
5557 }
5558#ifdef PROPERTY_SEARCH_USE_TOOLTIPS
5559 if (BLI_strcasestr(RNA_property_description(but->rnaprop), search_filter)) {
5560 return true;
5561 }
5562#endif
5563
5564 /* Search through labels of enum property items if they are in a drop-down menu.
5565 * Unfortunately we have no #bContext here so we cannot search through RNA enums
5566 * with dynamic entries (or "itemf" functions) which require context. */
5567 if (but->type == ButtonType::Menu) {
5568 PointerRNA *ptr = &but->rnapoin;
5569 PropertyRNA *enum_prop = but->rnaprop;
5570 int items_len;
5571 const EnumPropertyItem *items_array = nullptr;
5572 bool free;
5573 RNA_property_enum_items_gettexted(nullptr, ptr, enum_prop, &items_array, &items_len, &free);
5574 if (items_array == nullptr) {
5575 return false;
5576 }
5577
5578 bool found = false;
5579 for (int i = 0; i < items_len; i++) {
5580 /* Check for nullptr name field which enums use for separators. */
5581 if (items_array[i].name == nullptr) {
5582 continue;
5583 }
5584 if (BLI_strcasestr(items_array[i].name, search_filter)) {
5585 found = true;
5586 break;
5587 }
5588 }
5589 if (free) {
5590 MEM_delete(const_cast<EnumPropertyItem *>(items_array));
5591 }
5592 if (found) {
5593 return true;
5594 }
5595 }
5596 }
5597
5598 return false;
5599}
5600
5604static bool button_group_has_search_match(const ButtonGroup &group, const char *search_filter)
5605{
5606 for (Button *but : group.buttons) {
5607 if (button_matches_search_filter(but, search_filter)) {
5608 return true;
5609 }
5610 }
5611
5612 return false;
5613}
5614
5624static bool block_search_filter_tag_buttons(Block *block, const char *search_filter)
5625{
5626 bool has_result = false;
5627 for (const ButtonGroup &group : block->button_groups) {
5628 if (button_group_has_search_match(group, search_filter)) {
5629 has_result = true;
5630 }
5631 else {
5632 for (Button *but : group.buttons) {
5634 }
5635 }
5636 }
5637 return has_result;
5638}
5639
5640bool block_apply_search_filter(Block *block, const char *search_filter)
5641{
5642 if (search_filter == nullptr || search_filter[0] == '\0') {
5643 return false;
5644 }
5645
5646 Panel *panel = block->panel;
5647
5648 if (panel != nullptr) {
5649 /* Panels for active blocks should always have a valid `panel->type`,
5650 * otherwise they wouldn't be created. */
5651 if (panel->type->flag & PANEL_TYPE_NO_SEARCH) {
5652 return false;
5653 }
5654 }
5655
5656 const bool panel_label_matches = block_search_panel_label_matches(block, search_filter);
5657
5658 const bool has_result = (panel_label_matches) ?
5659 true :
5660 block_search_filter_tag_buttons(block, search_filter);
5661
5662 if (panel != nullptr) {
5663 if (has_result) {
5665 }
5666 }
5667
5668 return has_result;
5669}
5670
5672
5673/* -------------------------------------------------------------------- */
5676
5677static void item_scale(Layout *litem, const float scale[2])
5678{
5679 for (Item *item : litem->items()) {
5680 if (item->type() != ItemType::Button) {
5681 Layout *subitem = static_cast<Layout *>(item);
5682 item_scale(subitem, scale);
5683 }
5684
5685 int2 size = item->size();
5686 int2 offset = item->offset();
5687
5688 if (scale[0] != 0.0f) {
5689 offset.x *= scale[0];
5690 size.x *= scale[0];
5691 }
5692
5693 if (scale[1] != 0.0f) {
5694 offset.y *= scale[1];
5695 size.y *= scale[1];
5696 }
5697
5698 item_position(item, offset.x, offset.y, size.x, size.y);
5699 }
5700}
5701
5703{
5704 if (this->items().is_empty()) {
5705 w_ = 0;
5706 h_ = 0;
5707 return;
5708 }
5709
5710 for (Item *subitem : this->items()) {
5711 if (subitem->type() == ItemType::Button) {
5712 continue;
5713 }
5714 static_cast<Layout *>(subitem)->estimate();
5715 }
5716
5717 if (this->scale_x() != 0.0f || this->scale_y() != 0.0f) {
5718 item_scale(this, float2{this->scale_x(), this->scale_y()});
5719 }
5720 this->estimate_impl();
5721
5722 /* Force fixed size. */
5723 if (this->ui_units_x() > 0) {
5724 w_ = UI_UNIT_X * this->ui_units_x();
5725 }
5726 if (this->ui_units_y() > 0) {
5727 h_ = UI_UNIT_Y * this->ui_units_y();
5728 }
5729}
5730
5731static void item_align(Layout *litem, short nr)
5732{
5733 for (Item *item : litem->items()) {
5734 if (item->type() == ItemType::Button) {
5735 ButtonItem *bitem = static_cast<ButtonItem *>(item);
5736 if (!bitem->but->alignnr) {
5737 bitem->but->alignnr = nr;
5738 }
5739 }
5740 else if (item->type() == ItemType::LayoutAbsolute) {
5741 /* pass */
5742 }
5743 else if (item->type() == ItemType::LayoutOverlap) {
5744 /* pass */
5745 }
5746 else if (item->type() == ItemType::LayoutBox) {
5747 LayoutItemBx *box = static_cast<LayoutItemBx *>(item);
5748 if (!box->roundbox->alignnr) {
5749 box->roundbox->alignnr = nr;
5750 }
5751 }
5752 else {
5753 Layout *litem = static_cast<Layout *>(item);
5754 if (litem->align()) {
5755 item_align(litem, nr);
5756 }
5757 }
5758 }
5759}
5760
5761static void item_flag(Layout *litem, int64_t flag)
5762{
5763 for (Item *item : litem->items()) {
5764 if (item->type() == ItemType::Button) {
5765 ButtonItem *bitem = static_cast<ButtonItem *>(item);
5766 bitem->but->flag |= flag;
5767 }
5768 else {
5769 item_flag(static_cast<Layout *>(item), flag);
5770 }
5771 }
5772}
5773
5775{
5776
5777 if (this->items().is_empty()) {
5778 return;
5779 }
5780
5781 if (this->align()) {
5782 item_align(this, ++this->block()->alignnr);
5783 }
5784 if (!this->active()) {
5785 item_flag(this, BUT_INACTIVE);
5786 }
5787 if (!this->enabled()) {
5788 item_flag(this, BUT_DISABLED);
5789 }
5790 this->resolve_impl();
5791
5792 for (Item *subitem : this->items()) {
5793 if (ItemInternal::box_item(this)) {
5794 ItemInternal::box_item_set(subitem, true);
5795 }
5796 if (subitem->type() == ItemType::Button) {
5797 if (ItemInternal::box_item(this)) {
5798 ButtonItem *sub_bitem = static_cast<ButtonItem *>(subitem);
5799 sub_bitem->but->drawflag |= BUT_BOX_ITEM;
5800 }
5801 continue;
5802 }
5803 static_cast<Layout *>(subitem)->resolve();
5804 }
5805}
5806
5808{
5809 int icon_pad = 0;
5810 if (button->flag & UI_HAS_ICON) {
5812 }
5813 label_multiline_wrap_lines(button, icon_pad);
5814 const float line_height = ui::fontstyle_height_max(UI_FSTYLE_WIDGET);
5815 /* Top and bottom text padding. */
5816 const float padding = std::max(UI_UNIT_Y - line_height, 0.0f);
5817 int lines = button->wrap_cache->wrapped_lines.size();
5818 if (button->max_lines > 0) {
5819 lines = std::min(button->max_lines, lines);
5820 }
5821 const float height = padding + line_height * lines;
5822 button->rect.ymin = button->rect.ymax - std::max<float>(UI_UNIT_Y, height);
5823}
5824
5826static void resolve_image_height(Button *button)
5827{
5828 const ImBuf *ibuf = reinterpret_cast<const ImBuf *>(button->poin);
5829 if (ibuf == nullptr || ibuf->x <= 0 || ibuf->y <= 0) {
5830 return;
5831 }
5832 const float height = BLI_rctf_size_x(&button->rect) * ibuf->y / ibuf->x;
5833 button->rect.ymin = button->rect.ymax - std::max(height, 1.0f);
5834}
5835
5837{
5838 if (this->items().is_empty()) {
5839 return;
5840 }
5841 /* Extra vertical offset. */
5842 int y_offs = 0;
5843
5844 /* For simplicity a column is a grid of n rows and 1 columns, and a row is a grid of 1 rows and n
5845 * columns. */
5846 const LayoutDirection direction = this->type() == ItemType::LayoutRoot ?
5847 this->root_->direction :
5848 this->type() == ItemType::LayoutSplit ?
5850 this->local_direction();
5851 int rows = direction == LayoutDirection::Vertical ? this->items().size() : 1;
5852 int cols = direction == LayoutDirection::Horizontal ? this->items().size() : 1;
5853 bool row_major = direction == LayoutDirection::Vertical;
5854
5855 if (const LayoutItemGridFlow *flow = this->type() == ItemType::LayoutGridFlow ?
5856 static_cast<const LayoutItemGridFlow *>(this) :
5857 nullptr)
5858 {
5859 row_major = flow->row_major;
5860 rows = flow->tot_rows;
5861 cols = flow->tot_columns;
5862 }
5863 if (const LayoutItemFlow *flow = this->type() == ItemType::LayoutColumnFlow ?
5864 static_cast<const LayoutItemFlow *>(this) :
5865 nullptr)
5866 {
5867 row_major = false;
5868 cols = flow->totcol;
5869 rows = std::ceil(float(flow->items().size() / float(std::max(cols, 1))));
5870 }
5871 /* Dynamic height is resolved row by row, and each row pushes down following rows. */
5872 for (const int row : IndexRange(rows)) {
5873 /* Maximum sub-item height in the row before resolving its dynamic height. */
5874 int max_row_subitem_height = 0;
5875 /* Maximum sub-item height in the row after resolving its dynamic height. */
5876 int max_row_subitem_height_new = 0;
5877
5878 for (const int col : IndexRange(cols)) {
5879 const int i = (row_major ? (row * cols + col) : (col * rows + row));
5880 if (i >= this->items_.size()) {
5881 continue;
5882 }
5883 Item *subitem = this->items_[i];
5884 const int2 size = subitem->size();
5885 max_row_subitem_height = std::max(max_row_subitem_height, size.y);
5886
5887 /* Apply accumulated offset from previous rows. */
5888 item_translate_y(subitem, -y_offs);
5889
5890 /* Resolve sub-item dynamic height. */
5891 if (subitem->type() == ItemType::Button) {
5892 const auto *sub_bitem = static_cast<const ButtonItem *>(subitem);
5893 if (button_label_is_multiline(sub_bitem->but)) {
5894 resolve_label_multiline(static_cast<ButtonLabel *>(sub_bitem->but));
5895 }
5896 else if (button_label_is_markdown(sub_bitem->but)) {
5897 label_markdown_resolve(static_cast<ButtonLabel *>(sub_bitem->but));
5898 }
5899 else if (sub_bitem->but->type == ButtonType::Image) {
5900 resolve_image_height(sub_bitem->but);
5901 }
5902 }
5903 else {
5904 static_cast<Layout *>(subitem)->resolve_dynamic_height();
5905 }
5906 const int2 new_size = subitem->size();
5907 max_row_subitem_height_new = std::max(max_row_subitem_height_new, new_size.y);
5908 }
5909 /* Apply this row's extra height as offset to following rows. */
5910 y_offs += std::max(max_row_subitem_height_new - max_row_subitem_height, 0);
5911 }
5912
5913 /* Apply change in height to this layout. */
5914 this->y_ -= y_offs;
5915 this->h_ += y_offs;
5916}
5917
5918static int2 layout_end(Layout *layout)
5919{
5922 return layout->offset();
5923}
5924
5925static void layout_free(Layout *layout)
5926{
5927 for (Item *item : layout->items()) {
5928 if (item->type() == ItemType::Button) {
5929 ButtonItem *bitem = static_cast<ButtonItem *>(item);
5930
5931 bitem->but->layout = nullptr;
5932 MEM_delete(item);
5933 }
5934 else {
5935 Layout *litem = static_cast<Layout *>(item);
5936 layout_free(litem);
5937 }
5938 }
5939
5940 MEM_delete(layout);
5941}
5942
5944{
5945 if (root->padding) {
5946 /* add an invisible button for padding */
5947 Block *block = root->block;
5948 Layout *prev_layout = block->curlayout;
5949
5950 block->curlayout = root->layout;
5951 uiDefBut(
5952 block, ButtonType::Sepr, "", 0, 0, root->padding, root->padding, nullptr, 0.0, 0.0, "");
5953 block->curlayout = prev_layout;
5954 }
5955}
5956
5958 LayoutDirection dir,
5959 LayoutType type,
5960 int x,
5961 int y,
5962 int size,
5963 int em,
5964 int padding,
5965 const uiStyle *style)
5966{
5967 LayoutRoot *root = MEM_new_zeroed<LayoutRoot>(__func__);
5968 root->type = type;
5969 root->style = style;
5970 root->block = block;
5971 root->padding = padding;
5973 root->direction = dir;
5974 const char *func = __func__;
5975 Layout *layout = [&]() -> Layout * {
5976 switch (type) {
5978 return MEM_new<LayoutColumn>(func, root);
5980 BLI_assert(block->pie_data);
5981 return MEM_new<LayoutRootPieMenu>(func, root);
5982 case LayoutType::Header:
5983 return MEM_new<LayoutRow>(func, ItemType::LayoutRoot, root);
5984 default:
5985 return MEM_new<LayoutColumn>(func, ItemType::LayoutRoot, root);
5986 }
5987 }();
5988
5989 /* Only used when 'ItemInternalFlag::PropSep' is set. */
5990 layout->use_property_decorate_set(true);
5991
5993 layout->active_set(true);
5994 layout->enabled_set(true);
5996 int w = 0, h = 0;
5999 }
6000
6001 if (dir == LayoutDirection::Horizontal) {
6002 h = size;
6003 layout->root()->emh = em * UI_UNIT_Y;
6004 }
6005 else {
6006 w = size;
6007 layout->root()->emw = em * UI_UNIT_X;
6008 }
6010
6011 block->curlayout = layout;
6012 root->layout = layout;
6013 BLI_addtail(&block->layouts, root);
6014
6016
6017 return *layout;
6018}
6019
6021{
6022 return root_->block;
6023}
6024
6029
6031{
6032 ButtonItem *bitem = MEM_new<ButtonItem>(__func__);
6033 bitem->but = but;
6034
6035 int2 size = bitem->size();
6036 /* XXX Button hasn't scaled yet
6037 * we can flag the button as not expandable, depending on its size */
6038 if (size.x <= 2 * UI_UNIT_X && but->str.empty()) {
6039 bitem->fixed_size_set(true);
6040 }
6041
6042 if (layout->child_items_layout_) {
6043 layout->child_items_layout_->items_.append(bitem);
6044 }
6045 else {
6046 layout->items_.append(bitem);
6047 }
6048 but->layout = layout;
6049 but->search_weight = layout->search_weight_;
6050
6051 if (layout->context_) {
6052 but->context = layout->context_;
6053 layout->context_->used = true;
6054 }
6055
6056 if (layout->emboss_ != EmbossType::Undefined) {
6057 but->emboss = layout->emboss_;
6058 }
6059
6060 button_group_add_but(layout->block(), but);
6061}
6062
6063void layout_add_but(Layout *layout, Button *but)
6064{
6065 LayoutInternal::layout_add_but(layout, but);
6066};
6067
6069{
6070 const Vector<Item *> &child_list = layout->child_items_layout_ ?
6071 layout->child_items_layout_->items() :
6072 layout->items();
6073
6074 for (Item *item : child_list) {
6075 if (item->type() == ItemType::Button) {
6076 ButtonItem *bitem = static_cast<ButtonItem *>(item);
6077
6078 if (bitem->but == but) {
6079 return bitem;
6080 }
6081 }
6082 else {
6084 static_cast<Layout *>(item), but);
6085 if (nested_item) {
6086 return nested_item;
6087 }
6088 }
6089 }
6090
6091 return nullptr;
6092}
6093
6095{
6096 Vector<Item *> &child_list = layout->child_items_layout_ ? layout->child_items_layout_->items_ :
6097 layout->items_;
6098 const int64_t removed_num = child_list.remove_if([but](auto item) {
6099 if (item->type() == ItemType::Button) {
6100 ButtonItem *bitem = static_cast<ButtonItem *>(item);
6101 return (bitem->but == but);
6102 }
6103 return false;
6104 });
6105
6106 BLI_assert(removed_num <= 1);
6107 UNUSED_VARS_NDEBUG(removed_num);
6108}
6109
6110void layout_remove_but(Layout *layout, const Button *but)
6111{
6113}
6114
6115bool layout_replace_but_ptr(Layout *layout, const void *old_but_ptr, Button *new_but)
6116{
6118 layout, static_cast<const Button *>(old_but_ptr));
6119 if (!bitem) {
6120 return false;
6121 }
6122
6123 bitem->but = new_but;
6124 return true;
6125}
6126
6131
6133{
6134 return flag_is_set(flag_, ItemInternalFlag::FixedSize);
6135}
6136
6138{
6139 root_->opcontext = opcontext;
6140}
6141
6143{
6144 block->curlayout = layout;
6145}
6146
6148{
6149 for (LayoutRoot &root : block->layouts.items_mutable()) {
6150 layout_free(root.layout);
6151 MEM_delete(&root);
6152 }
6153}
6154
6156{
6157 BLI_assert(block->active);
6158 int2 block_size = {0, 0};
6159
6160 block->curlayout = nullptr;
6161
6162 for (LayoutRoot &root : block->layouts.items_mutable()) {
6164
6165 /* nullptr in advance so we don't interfere when adding button */
6166 block_size = layout_end(root.layout);
6167 layout_free(root.layout);
6168 MEM_delete(&root);
6169 }
6170
6171 block->layouts.clear_no_delete();
6172
6173 /* Link hit-targets need final button positions from layout resolve. */
6175
6176 return block_size;
6177}
6179{
6180 return !block->layouts.is_empty();
6181}
6182
6184{
6185 if (!context_) {
6186 return nullptr;
6187 }
6188 return CTX_store_ptr_lookup(context_, name, type);
6189}
6190
6192{
6193 Block *block = this->block();
6194 context_ = CTX_store_add(block->contexts, name, ptr);
6195}
6196std::optional<StringRefNull> Layout::context_string_get(const StringRef name) const
6197{
6198 if (!context_) {
6199 return std::nullopt;
6200 }
6201 return CTX_store_string_lookup(context_, name);
6202}
6203
6205{
6206 Block *block = this->block();
6207 context_ = CTX_store_add(block->contexts, name, value);
6208}
6209
6210std::optional<int64_t> Layout::context_int_get(const StringRef name) const
6211{
6212 if (!context_) {
6213 return std::nullopt;
6214 }
6215 return CTX_store_int_lookup(context_, name);
6216}
6217
6219{
6220 Block *block = this->block();
6221 context_ = CTX_store_add(block->contexts, name, value);
6222}
6223
6225{
6226 Block *block = this->block();
6228}
6229
6231 Layout *layout, ButtonToolTipFunc func, void *arg, CopyArgFunc copy_arg, FreeArgFunc free_arg)
6232{
6233 bool arg_used = false;
6234
6235 for (Item *item : layout->items()) {
6236 /* Each button will call free_arg for "its" argument, so we need to
6237 * duplicate the allocation for each button after the first. */
6238 if (copy_arg != nullptr && arg_used) {
6239 arg = copy_arg(arg);
6240 }
6241
6242 if (item->type() == ItemType::Button) {
6243 ButtonItem *bitem = static_cast<ButtonItem *>(item);
6244 if (bitem->but->type == ButtonType::Decorator) {
6245 continue;
6246 }
6247 button_func_tooltip_set(bitem->but, func, arg, free_arg);
6248 arg_used = true;
6249 }
6250 else {
6251 uiLayoutSetTooltipFunc(static_cast<Layout *>(item), func, arg, copy_arg, free_arg);
6252 arg_used = true;
6253 }
6254 }
6255
6256 if (free_arg != nullptr && !arg_used) {
6257 /* Free the original copy of arg in case the layout is empty. */
6258 free_arg(arg);
6259 }
6260}
6261
6264 void *arg,
6265 CopyArgFunc copy_arg,
6266 FreeArgFunc free_arg)
6267{
6268 bool arg_used = false;
6269
6270 for (Item *item : layout->items()) {
6271 /* Each button will call free_arg for "its" argument, so we need to
6272 * duplicate the allocation for each button after the first. */
6273 if (copy_arg != nullptr && arg_used) {
6274 arg = copy_arg(arg);
6275 }
6276
6277 if (item->type() == ItemType::Button) {
6278 ButtonItem *bitem = static_cast<ButtonItem *>(item);
6279 if (bitem->but->type == ButtonType::Decorator) {
6280 continue;
6281 }
6282 button_func_tooltip_custom_set(bitem->but, func, arg, free_arg);
6283 }
6284 else {
6285 uiLayoutSetTooltipCustomFunc(static_cast<Layout *>(item), func, arg, copy_arg, free_arg);
6286 }
6287 arg_used = true;
6288 }
6289
6290 if (free_arg != nullptr && !arg_used) {
6291 /* Free the original copy of arg in case the layout is empty. */
6292 free_arg(arg);
6293 }
6294}
6295
6297{
6298 if (but->opptr) {
6299 this->context_ptr_set("button_operator", but->opptr);
6300 }
6301
6302 if (but->rnapoin && but->rnaprop) {
6303 /* TODO: index could be supported as well */
6304 PointerRNA ptr_prop = RNA_pointer_create_discrete(nullptr, RNA_Property, but->rnaprop);
6305 this->context_ptr_set("button_prop", &ptr_prop);
6306 this->context_ptr_set("button_pointer", &but->rnapoin);
6307 }
6308}
6309
6311{
6312 if (r_prop != nullptr) {
6313 *r_prop = nullptr;
6314 }
6315
6317 MenuItemLevel *lvl = static_cast<MenuItemLevel *>(but->func_argN);
6318 wmOperatorType *ot = WM_operatortype_find(lvl->opname, false);
6319 if ((ot != nullptr) && (r_prop != nullptr)) {
6320 *r_prop = RNA_struct_type_find_property(ot->srna, lvl->propname);
6321 }
6322 return ot;
6323 }
6324 return nullptr;
6325}
6326
6328{
6330 return reinterpret_cast<MenuType *>(but->poin);
6331 }
6332 return nullptr;
6333}
6334
6336{
6338 return reinterpret_cast<PanelType *>(but->poin);
6339 }
6340 return nullptr;
6341}
6342
6343std::optional<StringRefNull> button_asset_shelf_type_idname_get(const Button *but)
6344{
6346}
6347
6349{
6350 Menu menu{};
6351 menu.layout = layout;
6352 menu.type = mt;
6353
6354 if (G.debug & G_DEBUG_WM) {
6355 printf("%s: opening menu \"%s\"\n", __func__, mt->idname);
6356 }
6357
6358 Block *block = layout->block();
6359 if (flag_is_set(mt->flag, MenuTypeFlag::SearchOnKeyPress)) {
6361 }
6362 if (mt->listener) {
6363 /* Forward the menu type listener to the block we're drawing in. */
6365 }
6366
6367 bContextStore context_store;
6368 if (layout->context()) {
6369 context_store = *layout->context();
6370 }
6371 const bContextStore *previous_context_store = CTX_store_get(C);
6372 if (previous_context_store) {
6373 context_store.entries.extend(previous_context_store->entries);
6374 }
6375 CTX_store_set(C, &context_store);
6376
6377 mt->draw(C, &menu);
6378
6379 CTX_store_set(C, previous_context_store);
6380}
6381
6382static bool layout_has_panel_label(const Layout *layout, const PanelType *pt)
6383{
6384 for (Item *subitem : layout->items()) {
6385 if (subitem->type() == ItemType::Button) {
6386 ButtonItem *bitem = static_cast<ButtonItem *>(subitem);
6387 if (!(bitem->but->flag & UI_HIDDEN) &&
6388 bitem->but->str == CTX_IFACE_(pt->translation_context, pt->label))
6389 {
6390 return true;
6391 }
6392 }
6393 else {
6394 Layout *litem = static_cast<Layout *>(subitem);
6395 if (layout_has_panel_label(litem, pt)) {
6396 return true;
6397 }
6398 }
6399 }
6400
6401 return false;
6402}
6403
6404static void paneltype_draw_impl(bContext *C, PanelType *pt, Layout *layout, bool show_header)
6405{
6406 Block *block = layout->block();
6407 Panel *panel = BKE_panel_new(pt);
6408 panel->flag = PNL_POPOVER;
6409
6410 if (pt->listener) {
6412 }
6413
6414 /* This check may be paranoid, this function might run outside the context of a popup or can run
6415 * in popovers that are not supposed to support refreshing, see #popover_create_block. */
6416 const bool support_layout_panel = block->handle && block->handle->region;
6417 if (support_layout_panel) {
6418 /* Allow popovers to contain collapsible sections, see #Layout::popover. */
6419 popup_dummy_panel_set(block->handle->region, block, pt->idname);
6420 }
6421
6422 Layout *body = nullptr;
6423 /* Draw main panel. */
6424 if (show_header) {
6425 Layout *header = nullptr;
6426 if (support_layout_panel && !(pt->flag & PANEL_TYPE_NO_HEADER)) {
6427 PanelLayout panel_layout = layout->panel(
6428 C, panel->type->idname, panel->type->flag & PANEL_TYPE_DEFAULT_CLOSED);
6429 header = panel_layout.header;
6430 body = panel_layout.body;
6431 }
6432 else {
6433 header = &layout->row(false);
6434 body = &layout->column(false);
6435 }
6436 if (pt->draw_header) {
6437 panel->layout = header;
6438 pt->draw_header(C, panel);
6439 panel->layout = nullptr;
6440 }
6441
6442 /* draw_header() is often used to add a checkbox to the header. If we add the label like below
6443 * the label is disconnected from the checkbox, adding a weird looking gap. As workaround, let
6444 * the checkbox add the label instead. */
6445 if (!layout_has_panel_label(header, pt)) {
6446 header->label(CTX_IFACE_(pt->translation_context, pt->label), ICON_NONE);
6447 }
6448 }
6449 else {
6450 body = layout;
6451 }
6452
6453 if (body) {
6454 panel->layout = body;
6455 pt->draw(C, panel);
6456 panel->layout = nullptr;
6457 }
6458 BLI_assert(panel->runtime->custom_data_ptr == nullptr);
6459
6460 BKE_panel_free(panel);
6461 if (!body) {
6462 return;
6463 }
6464 /* Draw child panels. */
6465 for (LinkData &link : pt->children) {
6466 PanelType *child_pt = static_cast<PanelType *>(link.data);
6467 if (child_pt->poll == nullptr || child_pt->poll(C, child_pt)) {
6468 paneltype_draw_impl(C, child_pt, body, true);
6469 }
6470 }
6471}
6472
6474{
6475 if (layout->context()) {
6476 CTX_store_set(C, layout->context());
6477 }
6478
6479 paneltype_draw_impl(C, pt, layout, false);
6480
6481 if (layout->context()) {
6482 CTX_store_set(C, nullptr);
6483 }
6484}
6485
6487
6488/* -------------------------------------------------------------------- */
6498
6499static void layout_introspect_button(fmt::appender ds, const ButtonItem *bitem)
6500{
6501 Button *but = bitem->but;
6502 fmt::format_to(ds, "'type':{}, ", int(but->type));
6503 fmt::format_to(ds, "'draw_string':'''{}''', ", but->drawstr);
6504 /* Not exactly needed, rna has this. */
6505 fmt::format_to(ds, "'tip':'''{}''', ", but->tip);
6506
6507 if (but->optype) {
6508 std::string opstr = WM_operator_pystring_ex(static_cast<bContext *>(but->block->evil_C),
6509 nullptr,
6510 false,
6511 true,
6512 but->optype,
6513 but->opptr);
6514 fmt::format_to(ds, "'operator':'''{}''', ", opstr);
6515 }
6516
6517 {
6518 PropertyRNA *prop = nullptr;
6520 if (ot) {
6521 std::string opstr = WM_operator_pystring_ex(
6522 static_cast<bContext *>(but->block->evil_C), nullptr, false, true, ot, nullptr);
6523 fmt::format_to(ds, "'operator':'''{}''', ", opstr);
6524 fmt::format_to(ds, "'property':'''{}''', ", prop ? RNA_property_identifier(prop) : "");
6525 }
6526 }
6527
6528 if (but->rnaprop) {
6529 fmt::format_to(ds,
6530 "'rna':'{}.{}[{}]', ",
6533 but->rnaindex);
6534 }
6535}
6536
6537static void layout_introspect_items(fmt::appender ds, Span<const Item *> items)
6538{
6539 fmt::format_to(ds, "[");
6540
6541 for (const Item *item : items) {
6542
6543 fmt::format_to(ds, "{{");
6544
6545#define CASE_ITEM(type, name) \
6546 case type: { \
6547 fmt::format_to(ds, "'type': '{}', ", name); \
6548 break; \
6549 } \
6550 ((void)0)
6551
6552 switch (item->type()) {
6553 CASE_ITEM(ItemType::Button, "BUTTON");
6554 CASE_ITEM(ItemType::LayoutRow, "LAYOUT_ROW");
6555 CASE_ITEM(ItemType::LayoutPanelHeader, "LAYOUT_PANEL_HEADER");
6556 CASE_ITEM(ItemType::LayoutPanelBody, "LAYOUT_PANEL_BODY");
6557 CASE_ITEM(ItemType::LayoutColumn, "LAYOUT_COLUMN");
6558 CASE_ITEM(ItemType::LayoutColumnFlow, "LAYOUT_COLUMN_FLOW");
6559 CASE_ITEM(ItemType::LayoutRowFlow, "LAYOUT_ROW_FLOW");
6560 CASE_ITEM(ItemType::LayoutBox, "LAYOUT_BOX");
6561 CASE_ITEM(ItemType::LayoutAbsolute, "LAYOUT_ABSOLUTE");
6562 CASE_ITEM(ItemType::LayoutSplit, "LAYOUT_SPLIT");
6563 CASE_ITEM(ItemType::LayoutOverlap, "LAYOUT_OVERLAP");
6564 CASE_ITEM(ItemType::LayoutRoot, "LAYOUT_ROOT");
6565 CASE_ITEM(ItemType::LayoutGridFlow, "LAYOUT_GRID_FLOW");
6566 CASE_ITEM(ItemType::LayoutRadial, "LAYOUT_RADIAL");
6567 }
6568
6569#undef CASE_ITEM
6570
6571 switch (item->type()) {
6572 case ItemType::Button:
6573 layout_introspect_button(ds, static_cast<const ButtonItem *>(item));
6574 break;
6575 default:
6576 fmt::format_to(ds, "'items':");
6577 layout_introspect_items(ds, (static_cast<const Layout *>(item))->items());
6578 break;
6579 }
6580
6581 fmt::format_to(ds, "}}");
6582
6583 if (item != items.last()) {
6584 fmt::format_to(ds, ", ");
6585 }
6586 }
6587 /* Don't use a comma here as it's not needed and
6588 * causes the result to evaluate to a tuple of 1. */
6589 fmt::format_to(ds, "]");
6590}
6591
6592std::string layout_introspect(Layout *layout)
6593{
6594 fmt::memory_buffer buffer;
6595 Vector<Item *> layout_dummy_list(1, layout);
6596 layout_introspect_items(fmt::appender(buffer), layout_dummy_list);
6597 return fmt::to_string(buffer);
6598}
6599
6601
6602/* -------------------------------------------------------------------- */
6605
6607 const uiStyle *style,
6608 const int dialog_width,
6609 const AlertIcon icon,
6610 const int icon_size)
6611{
6612 /* By default, the space between icon and text/buttons will be equal to the 'columnspace',
6613 * this extra padding will add some space by increasing the left column width,
6614 * making the icon placement more symmetrical, between the block edge and the text. */
6615 const float icon_padding = 5.0f * UI_SCALE_FAC;
6616 /* Calculate the factor of the fixed icon column depending on the block width. */
6617 const float split_factor = (float(icon_size) + icon_padding) /
6618 float(dialog_width - style->columnspace);
6619
6621 block, LayoutDirection::Vertical, LayoutType::Panel, 0, 0, dialog_width, 0, 0, style);
6622
6623 if (icon == AlertIcon::Info) {
6625 }
6626 else if (icon == AlertIcon::Warning) {
6628 }
6629 else if (icon == AlertIcon::Question) {
6631 }
6632 else if (icon == AlertIcon::Error) {
6634 }
6635 else {
6637 }
6638
6639 /* Split layout to put alert icon on left side. */
6640 Layout *split_block = &block_layout.split(split_factor, false);
6641
6642 /* Alert icon on the left. */
6643 Layout *layout = &split_block->row(false);
6644 /* Using 'align_left' with 'row' avoids stretching the icon along the width of column. */
6646 uiDefButAlert(block, icon, 0, 0, icon_size, icon_size);
6647
6648 /* The rest of the content on the right. */
6649 layout = &split_block->column(false);
6650
6651 return layout;
6652}
6653
6654Layout *uiItemsAlertBox(Block *block, const int size, const AlertIcon icon)
6655{
6656 const uiStyle *style = style_get_dpi();
6657 const short icon_size = 40 * UI_SCALE_FAC;
6658 const int dialog_width = icon_size + (style->widget.points * size * UI_SCALE_FAC);
6659 return uiItemsAlertBox(block, style, dialog_width, icon, icon_size);
6660}
6661
6663
6665{
6666 return root_;
6667};
6669{
6670 return context_;
6671};
6673{
6674 return parent_;
6675};
6677{
6678 return heading_;
6679};
6681{
6682 heading_ = {};
6683}
6685{
6686 return items_;
6687};
6688bool Layout::align() const
6689{
6690 return align_;
6691}
6693{
6694 return variable_size_;
6695}
6697{
6698 return emboss_;
6699}
6700
6701} // namespace blender::ui
#define G_MAIN
Functions and classes for evaluating template expressions in filepaths.
A dynamically sized string ADT.
#define STRNCPY_UTF8(dst, src)
#define RPT_(msgid)
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define MAX_IDPROP_NAME
Definition DNA_ID.h:174
#define RGN_ALIGN_ENUM_FROM_MASK(align)
#define UI_SCALE_FAC
#define OP_MAX_TYPENAME
Read Blender memory allocation and freeing.
#define RNA_STRUCT_BEGIN(sptr, prop)
#define RNA_warning_bare(format,...)
#define RNA_STRUCT_END
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_FSTYLE_WIDGET
#define UI_UNIT_X
#define UI_ITEM_NONE
#define RNA_NO_INDEX
#define UI_MAX_NAME_STR
#define KM_MOD_HELD
Definition WM_types.hh:339
#define POINTER_FROM_INT(i)
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
#define SET_FLAG_FROM_TEST(value, test, flag)
#define POINTER_AS_INT(i)
#define ELEM(...)
#define STREQ(a, b)
#define output
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
const T * data() const
Definition BLI_array.hh:323
void fill(const T &value) const
Definition BLI_array.hh:283
constexpr IndexRange()=default
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr const char * c_str() const
constexpr StringRef()
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr const char * data() const
void append(const T &value)
int64_t remove_if(Predicate &&predicate)
void reserve(const int64_t min_capacity)
#define str(s)
bool all(VecOp< bool, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
#define PIE_MAX_ITEMS
#define RNA_ENUM_VALUE
#define CASE_ITEM(type, name)
#define UI_ITEM_VARY_X
#define UI_ITEM_VARY_Y
#define UI_PROP_DECORATE
#define UI_OPERATOR_ERROR_RET(_ot, _opname, _caller_fn_name)
const int state
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ENUM_OPERATORS(Layer::Flags)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
void free(Subdiv *subdiv)
Definition subdiv.cc:192
void butstore_free(Block *block, ButStore *bs_handle)
static bool layout_variable_size(Layout *layout)
Button * uiDefIconTextBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, StringRef str, int x, int y, short width, short height, void *poin, std::optional< StringRef > tip)
static Layout * item_local_sublayout(Layout *test, Layout *layout, bool align)
void item_menutype_func(bContext *C, Layout *layout, void *arg_mt)
void block_direction_set(Block *block, char direction)
static void layout_heading_label_add(Layout *layout, Layout *heading_layout, bool right_align, bool respect_prop_split)
static bool layout_has_panel_label(const Layout *layout, const PanelType *pt)
void(*)(bContext *C, Layout *layout, void *arg1) MenuCreateFunc
void button_func_search_set_results_are_suggestions(Button *but, bool value)
void button_configure_search(Button *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchprop, bool results_are_suggestions)
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 label_markdown_create_link_buttons(Block *block)
static void item_flag(Layout *litem, int64_t flag)
void def_but_icon_clear(Button *but)
void butstore_register(ButStore *bs_handle, Button **but_p)
void button_func_set(Button *but, std::function< void(bContext &)> func)
static void item_disabled(Layout *layout, const char *name)
static ButtonImage * image_find_in_old_block(Block *block, const StringRef key)
bool button_label_is_markdown(const Button *button)
void label_markdown_dev_config(Layout &layout)
constexpr TextIconPadFactor text_pad_none
void button_unit_type_set(Button *but, int unit_type)
Button * uiDefIconTextButO_ptr(Block *block, ButtonType type, wmOperatorType *ot, wm::OpCallContext opcontext, int icon, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip)
static void layout_free(Layout *layout)
void uiLayoutSetTooltipCustomFunc(Layout *layout, ButtonToolTipCustomFunc func, void *arg, CopyArgFunc copy_arg, FreeArgFunc free_arg)
void context_active_but_prop_get_filebrowser(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop, bool *r_is_undo, bool *r_is_userdef)
void button_anim_decorate_cb(bContext *C, void *arg_but, void *)
void popup_menu_but_set(PopupMenu *pup, ARegion *butregion, Button *but)
void button_pie_dir(RadialDirection dir, float vec[2])
static Button * uiItemFullO_ptr_ex(Layout *layout, wmOperatorType *ot, std::optional< StringRef > name, int icon, const wm::OpCallContext context, const eUI_Item_Flag flag, PointerRNA *r_opptr)
bool block_layout_needs_resolving(const Block *block)
bool button_is_userdef(const Button *but)
void label_markdown_resolve(ButtonLabel *button)
void layout_remove_but(Layout *layout, const Button *but)
void text_button_func_rename_full_set(Button *but, std::function< void(StringRefNull new_name)> rename_full_func)
void layout_add_but(Layout *layout, Button *but)
static bool item_is_radial_displayable(Item *item)
bool block_apply_search_filter(Block *block, const char *search_filter)
std::optional< StringRefNull > button_asset_shelf_type_idname_get(const Button *but)
constexpr TextIconPadFactor text_pad_default
static Layout * layout_heading_find(Layout *cur_layout)
PointerRNA * button_operator_ptr_ensure(Button *but)
void block_align_begin(Block *block)
void(*)(void *arg) FreeArgFunc
@ ITEM_R_TEXT_BUT_FORCE_SEMI_MODAL_ACTIVE
static int text_icon_width(Layout *layout, const StringRef name, const int icon, const bool compact)
void uiItemLDrag(Layout *layout, PointerRNA *ptr, StringRef name, int icon)
BLI_INLINE bool layout_is_radial(const Layout *layout)
int uiLayoutListItemPaddingWidth()
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
wmOperatorType * button_operatortype_get_from_enum_menu(Button *but, PropertyRNA **r_prop)
static void but_tip_from_enum_item(Button *but, const EnumPropertyItem *item)
bool block_can_add_separator(const Block *block)
static bool item_rna_is_expand(PropertyRNA *prop, int index, const eUI_Item_Flag item_flag)
static RadialDirection get_radialbut_vec(float vec[2], short itemnum)
static void item_array(Layout *layout, Block *block, const StringRef name, int icon, PointerRNA *ptr, PropertyRNA *prop, const int len, int x, const int y, int w, const int, const bool expand, const bool slider, const int toggle, const bool icon_only, const bool compact, const bool show_text)
void block_flag_enable(Block *block, int flag)
static void item_enum_expand_tabs(Layout *layout, bContext *C, Block *block, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *ptr_highlight, PropertyRNA *prop_highlight, const std::optional< StringRef > uiname, const int h, const bool icon_only, EnumTabExpand expand_as)
static bool block_search_panel_label_matches(const Block *block, const char *search_string)
Layout * uiItemsAlertBox(Block *block, const uiStyle *style, const int dialog_width, const AlertIcon icon, const int icon_size)
bool block_is_menu(const Block *block) ATTR_WARN_UNUSED_RESULT
Button * uiDefIconButO(Block *block, ButtonType type, StringRefNull opname, wm::OpCallContext opcontext, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
const uiStyle * style_get_dpi()
PanelType * button_paneltype_get(const Button *but)
std::string layout_introspect(Layout *layout)
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)
static void resolve_label_multiline(ButtonLabel *button)
static void item_enum_expand_exec(Layout *layout, Block *block, PointerRNA *ptr, PropertyRNA *prop, const std::optional< StringRef > uiname, const int h, const ButtonType but_type, const bool icon_only)
static void item_enum_expand(Layout *layout, Block *block, PointerRNA *ptr, PropertyRNA *prop, const std::optional< StringRef > uiname, const int h, const bool icon_only)
static void item_rna_size(Layout *layout, StringRef name, int icon, PointerRNA *ptr, PropertyRNA *prop, int index, bool icon_only, bool compact, int *r_w, int *r_h)
static int2 layout_end(Layout *layout)
void button_func_tooltip_custom_set(Button *but, ButtonToolTipCustomFunc func, void *arg, FreeArgFunc free_arg)
static void layer_but_cb(bContext *C, void *arg_but, void *arg_index)
void button_drawflag_enable(Button *but, int flag)
static Button * uiItem_simple(Layout *layout, const StringRef name, int icon, std::optional< StringRef > tooltip=std::nullopt, const ButtonType but_type=ButtonType::Label)
static void resolve_image_height(Button *button)
static void item_move(Item *item, const int delta_xmin, const int delta_xmax)
Button * uiDefIconTextMenuBut(Block *block, MenuCreateFunc func, void *arg, int icon, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip)
void button_disable(Button *but, const char *disabled_hint)
void button_func_tooltip_set(Button *but, ButtonToolTipFunc func, void *arg, FreeArgFunc free_arg)
static void keymap_but_cb(bContext *, void *but_v, void *)
static int text_icon_width_ex(Layout *layout, const StringRef name, int icon, const TextIconPadFactor &pad_factor, const uiFontStyle *fstyle)
void button_flag_enable(Button *but, int64_t flag)
static void litem_grid_flow_compute(Span< Item * > items, const UILayoutGridFlowInput *parameters, UILayoutGridFlowOutput *results)
static void item_enum_expand_elem_exec(Layout *layout, Block *block, PointerRNA *ptr, PropertyRNA *prop, const std::optional< StringRef > uiname, const int h, const ButtonType but_type, const bool icon_only, const EnumPropertyItem *item, const bool is_first)
Layout * popup_menu_layout(PopupMenu *pup)
void button_rna_menu_convert_to_menu_type(Button *but, const char *menu_type)
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)
void *(*)(const void *argN) ButtonArgNCopy
static void menu_item_enum_rna_menu(bContext *, Layout *layout, void *arg)
static void layout_add_padding_button(LayoutRoot *root)
void button_placeholder_set(Button *but, StringRef placeholder_text)
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 * but_func_argN_copy(const void *argN)
static Button * item_menu(Layout *layout, const StringRef name, int icon, MenuCreateFunc func, void *arg, void *argN, const std::optional< StringRef > tip, bool force_menu, ButtonArgNFree func_argN_free_fn=MEM_delete_void, ButtonArgNCopy func_argN_copy_fn=MEM_dupalloc_void)
static void item_menu_hold(bContext *C, ARegion *butregion, Button *but)
static void item_enum_expand_handle(bContext *C, void *arg1, void *arg2)
void UI_paneltype_draw(bContext *C, PanelType *pt, Layout *layout)
void block_align_end(Block *block)
static void rna_collection_search_arg_free_fn(void *ptr)
Button * uiDefButAlert(Block *block, AlertIcon icon, int x, int y, short width, short height)
void block_new_button_group(Block *block, ButtonGroupFlag flag)
static int layout_vary_direction(Layout *layout)
Button * uiDefMenuBut(Block *block, MenuCreateFunc func, void *arg, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip)
void label_multiline_wrap_lines(ButtonLabel *button, int icon_pad)
void tooltip_text_field_add(TooltipData &data, std::string text, std::string suffix, const TooltipStyle style, const TooltipColorID color_id, const bool is_pad=false)
static void search_id_collection(StructRNA *ptype, PointerRNA *r_ptr, PropertyRNA **r_prop)
bool uiLayoutEndsWithPanelHeader(const Layout &layout)
static int litem_min_width(int itemw)
static void item_position(Item *item, const int x, const int y, const int w, const int h)
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)
bool block_is_pie_menu(const Block *block) ATTR_WARN_UNUSED_RESULT
static bool button_matches_search_filter(Button *but, const char *search_filter)
void item_paneltype_func(bContext *C, Layout *layout, void *arg_pt)
Button * uiDefIconMenuBut(Block *block, MenuCreateFunc func, void *arg, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
void rna_collection_search_update_fn(const bContext *C, void *arg, const char *str, SearchItems *items, bool is_first)
void layout_list_set_labels_active(Layout *layout)
Button * uiItemL_ex(Layout *layout, StringRef name, int icon, bool highlight, bool redalert)
bool button_label_is_multiline(const Button *button)
void block_layout_free(Block *block)
int2 block_layout_resolve(Block *block)
void menutype_draw(bContext *C, MenuType *mt, Layout *layout)
Button * uiDefButO_ptr(Block *block, ButtonType type, wmOperatorType *ot, wm::OpCallContext opcontext, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip)
static Button * item_with_label(Layout *layout, Block *block, const StringRef name, const int icon, PointerRNA *ptr, PropertyRNA *prop, const int index, const int x, const int y, const int w_hint, const int h, const int flag, std::optional< ButtonType > button_type_override, const char *caller_fn_name)
static bool block_search_filter_tag_buttons(Block *block, const char *search_filter)
std::optional< StringRefNull > asset_shelf_idname_from_button_context(const Button *but)
static void layout_introspect_items(fmt::appender ds, Span< const Item * > items)
Button * uiDefButTextBoxR(Block *block, PointerRNA *ptr, StringRefNull propname, TextboxState *state, int x, int y, short width)
void but_func_argN_free(void *argN)
const char radial_dir_order[8]
int fontstyle_height_max(const uiFontStyle *fs)
Layout * uiItemL_respect_property_split(Layout *layout, StringRef text, int icon)
void button_drawflag_disable(Button *but, int flag)
bool layout_replace_but_ptr(Layout *layout, const void *old_but_ptr, Button *new_but)
static void paneltype_draw_impl(bContext *C, PanelType *pt, Layout *layout, bool show_header)
ARegion * searchbox_create_generic(bContext *C, ARegion *butregion, ButtonSearch *search_but)
static void item_align(Layout *litem, short nr)
Button * uiDefIconBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
static bool button_group_has_search_match(const ButtonGroup &group, const char *search_filter)
static void item_translate_y(Item *item, const int delta)
static void item_scale(Layout *litem, const float scale[2])
void(*)(bContext &C, TooltipData &data, Button *but, void *argN) ButtonToolTipCustomFunc
MenuType * button_menutype_get(const Button *but)
static void menu_item_enum_opname_menu(bContext *C, Layout *layout, void *arg)
void button_group_add_but(Block *block, Button *but)
static LayoutItemBx * layout_box(Layout *layout, ButtonType type)
void *(*)(const void *arg) CopyArgFunc
void popup_dummy_panel_set(ARegion *region, Block *block, StringRef idname)
static int spaces_after_column_item(const Layout *litem, const Item *item, const Item *next_item, const bool is_box)
Layout & block_layout(Block *block, LayoutDirection direction, LayoutType type, int x, int y, int size, int em, int padding, const uiStyle *style)
void uiLayoutListItemAddPadding(Layout *layout)
std::string(*)(bContext *C, void *argN, StringRef tip) ButtonToolTipFunc
void block_add_dynamic_listener(Block *block, void(*listener_func)(const wmRegionListenerParams *params))
void uiLayoutSetTooltipFunc(Layout *layout, ButtonToolTipFunc func, void *arg, CopyArgFunc copy_arg, FreeArgFunc free_arg)
static bool item_is_radial_drawable(ButtonItem *bitem)
TextboxState * textbox_ensure_state(ARegion *region, StringRefNull idname, const int initial_visible_lines)
void block_layout_set_current(Block *block, Layout *layout)
void button_func_hold_set(Button *but, ButtonHandleHoldFunc func, void *argN)
int int fontstyle_string_width_with_block_aspect(const uiFontStyle *fs, StringRef str, float aspect) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
static int menu_item_enum_opname_menu_active(bContext *C, Button *but, MenuItemLevel *lvl)
void panel_tag_search_filter_match(Panel *panel)
void(*)(void *argN) ButtonArgNFree
void popup_menu_end(bContext *C, PopupMenu *pup)
void button_rna_menu_convert_to_panel_type(Button *but, const char *panel_type)
static void layout_introspect_button(fmt::appender ds, const ButtonItem *bitem)
Button * uiDefIconButO_ptr(Block *block, ButtonType type, wmOperatorType *ot, wm::OpCallContext opcontext, int icon, int x, int y, short width, short height, std::optional< StringRef > tip)
static int item_fit(const int item, const int pos, const int all, const int available, const bool is_last, const LayoutAlign alignment, float *extra_pixel)
bool block_is_popup_any(const Block *block) ATTR_WARN_UNUSED_RESULT
void button_update(Button *but)
static StringRef item_name_add_colon(StringRef name, char namestr[UI_MAX_NAME_STR])
PropertySplitWrapper uiItemPropertySplitWrapperCreate(Layout *parent_layout)
void button_func_search_set(Button *but, ButtonSearchCreateFn search_create_fn, ButtonSearchUpdateFn search_update_fn, void *arg, bool free_arg, FreeArgFunc search_arg_free_fn, ButtonHandleFunc search_exec_fn, void *active)
void button_add_shortcut(Button *but, const char *shortcut_str, const bool do_strip)
ButStore * butstore_create(Block *block)
int button_align_opposite_to_area_align_get(const ARegion *region)
void pie_menu_level_create(Block *block, wmOperatorType *ot, StringRefNull propname, IDProperty *properties, const EnumPropertyItem *items, int totitem, wm::OpCallContext context, eUI_Item_Flag flag)
void button_drag_set_id(Button *but, ID *id)
Button * uiDefButImage(Block *block, void *imbuf, int x, int y, short width, short height, const uchar color[4])
constexpr TextIconPadFactor text_pad_compact
void button_type_set_menu_from_pulldown(Button *but)
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)
__int64 int64_t
Definition BKE_text.h:90
uint padding(uint offset, uint alignment)
const PointerRNA * CTX_store_ptr_lookup(const bContextStore *store, StringRef name, const StructRNA *type=nullptr)
PointerRNA RNA_main_pointer_create(Main *main)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Panel * BKE_panel_new(PanelType *panel_type)
Definition screen.cc:685
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void WM_operator_properties_alloc(PointerRNA **ptr, IDProperty **properties, const char *opstring)
bool RNA_struct_is_a(const StructRNA *type, const StructRNA *srna)
unsigned int uint
Definition BKE_text.h:55
std::string WM_operatortype_name(wmOperatorType *ot, PointerRNA *properties)
void WM_operator_properties_sanitize(PointerRNA *ptr, const bool no_context)
const char * RNA_property_identifier(const PropertyRNA *prop)
void RNA_property_boolean_get_array(PointerRNA *ptr, PropertyRNA *prop, bool *values)
bool BKE_path_contains_template_syntax(StringRef path)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_enum_items(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **r_item, int *r_totitem, bool *r_free)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
@ PROP_UNIT_ROTATION
Definition RNA_types.hh:226
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
bContextStore * CTX_store_add(Vector< std::unique_ptr< bContextStore > > &contexts, StringRef name, const PointerRNA *ptr)
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
PropertyRNA * RNA_struct_find_property_check(PointerRNA &props, const char *name, const PropertyType property_type_check)
std::string RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop)
const char * RNA_property_description(PropertyRNA *prop)
wmWindow * CTX_wm_window(const bContext *C)
const char * RNA_property_translation_context(const PropertyRNA *prop)
MINLINE int max_ii(int a, int b)
std::optional< std::string > WM_key_event_operator_string(const bContext *C, const char *opname, wm::OpCallContext opcontext, IDProperty *properties, const bool is_strict)
void RNA_property_boolean_set_array(PointerRNA *ptr, PropertyRNA *prop, const bool *values)
bool RNA_property_animateable(const PointerRNA *ptr, PropertyRNA *prop_orig)
int char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void MEM_delete_void(void *vmemh)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_array_check(PropertyRNA *prop)
Global G
Definition blender.cc:57
bool RNA_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *r_value)
const bContextStore * CTX_store_get(const bContext *C)
void IDP_CopyPropertyContent(IDProperty *dst, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:991
PropertyType RNA_property_type(PropertyRNA *prop)
std::string WM_operator_pystring_ex(bContext *C, wmOperator *op, const bool all_args, const bool macro_args, wmOperatorType *ot, PointerRNA *opptr)
bContextStore * CTX_store_add_all(Vector< std::unique_ptr< bContextStore > > &contexts, const bContextStore *context)
void * MEM_dupalloc_void(const void *vmemh) ATTR_WARN_UNUSED_RESULT
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
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)
@ 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
std::optional< int64_t > CTX_store_int_lookup(const bContextStore *store, StringRef name)
BMesh const char void * data
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
bool RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
unsigned char uint8_t
Definition BKE_text.h:79
std::optional< bke::path_templates::VariableMap > BKE_build_template_variables_for_prop(const bContext *C, PointerRNA *ptr, PropertyRNA *prop)
char RNA_property_array_item_char(PropertyRNA *prop, int index)
int RNA_property_flag(PropertyRNA *prop)
PanelType * WM_paneltype_find(const StringRef idname, bool quiet)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
bool WM_menutype_poll(bContext *C, MenuType *mt)
PropertyRNA * RNA_struct_type_find_property(StructRNA *srna, const char *identifier)
ARegionType * BKE_regiontype_from_id(const SpaceType *st, int regionid)
Definition screen.cc:364
SpaceType * BKE_spacetype_from_id(int spaceid)
Definition screen.cc:354
void RNA_property_enum_items_gettexted(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **r_item, int *r_totitem, bool *r_free)
LayoutPanelState * BKE_panel_layout_panel_state_ensure(Panel *panel, StringRef idname, bool default_closed)
Definition screen.cc:656
PropertyRNA * RNA_struct_name_property(const StructRNA *type)
int RNA_property_array_dimension(const PointerRNA *ptr, PropertyRNA *prop, int length[])
void BKE_panel_free(Panel *panel)
Definition screen.cc:702
int RNA_struct_ui_icon(const StructRNA *type)
bool RNA_struct_is_ID(const StructRNA *type)
const char * RNA_struct_identifier(const StructRNA *type)
std::optional< std::string > WM_keymap_item_to_string(const wmKeyMapItem *kmi, const bool compact)
PropertySubType RNA_property_subtype(PropertyRNA *prop)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
bool ED_id_rename(Main &bmain, ID &id, StringRefNull name)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
@ PANEL_TYPE_DEFAULT_CLOSED
@ PANEL_TYPE_NO_HEADER
@ PANEL_TYPE_NO_SEARCH
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
iter begin(iter)
Vector< bke::path_templates::Error > BKE_path_validate_template(StringRef path, const bke::path_templates::VariableMap &template_variables)
@ PROP_ENUM_FLAG
Definition RNA_types.hh:474
@ PROP_ICONS_CONSECUTIVE
Definition RNA_types.hh:399
@ PROP_PATH_SUPPORTS_TEMPLATES
Definition RNA_types.hh:540
@ PROP_PATH_SUPPORTS_BLEND_RELATIVE
Definition RNA_types.hh:526
ARegion * CTX_wm_region_popup(const bContext *C)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
@ KM_NOTHING
Definition WM_types.hh:323
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
std::optional< StringRefNull > CTX_store_string_lookup(const bContextStore *store, StringRef name)
int RNA_property_ui_icon(const PropertyRNA *prop)
int RNA_enum_from_name(const EnumPropertyItem *item, const char *name)
@ RGN_FLAG_SEARCH_FILTER_ACTIVE
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void CTX_store_set(bContext *C, const bContextStore *store)
@ PROP_LAYER_MEMBER
Definition RNA_types.hh:335
@ PROP_FILENAME
Definition RNA_types.hh:286
@ PROP_MATRIX
Definition RNA_types.hh:322
@ PROP_FILEPATH
Definition RNA_types.hh:284
@ PROP_DIRPATH
Definition RNA_types.hh:285
@ PROP_AXISANGLE
Definition RNA_types.hh:325
@ PROP_DIRECTION
Definition RNA_types.hh:319
@ PROP_COLOR_GAMMA
Definition RNA_types.hh:329
eStringPropertySearchFlag RNA_property_string_search_flag(PropertyRNA *prop)
MINLINE int min_ii(int a, int b)
ARegion * CTX_wm_region(const bContext *C)
signed char int8_t
Definition BKE_text.h:76
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
void RNA_property_enum_items_gettexted_all(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **r_item, int *r_totitem, bool *r_free)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool RNA_property_is_unlink(PropertyRNA *prop)
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< PanelType > paneltypes
bke::ARegionRuntime * runtime
eRegion_Alignment alignment
Vector< LayoutPanelBody > bodies
Vector< LayoutPanelHeader > headers
char idname[BKE_ST_MAXNAME]
char translation_context[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
MenuTypeFlag flag
void(*) draw(const bContext *C, Menu *menu)
const char * description
void(*) listener(const wmRegionListenerParams *params)
ui::Layout * layout
MenuType * type
char translation_context[BKE_ST_MAXNAME]
ListBaseT< LinkData > children
char parent_id[BKE_ST_MAXNAME]
void(*) listener(const wmRegionListenerParams *params)
char context[BKE_ST_MAXNAME]
void(*) draw(const bContext *C, Panel *panel)
const char * description
char category[BKE_ST_MAXNAME]
bool(*) poll(const bContext *C, PanelType *pt)
void(*) draw_header(const bContext *C, Panel *panel)
char idname[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
LayoutPanels layout_panels
PointerRNA * custom_data_ptr
struct PanelType * type
ui::Layout * layout
struct Panel_Runtime * runtime
T * data_as() const
Definition RNA_types.hh:139
bool has_type() const
Definition RNA_types.hh:180
StructRNA * type
Definition RNA_types.hh:63
Vector< bContextStoreEntry > entries
ListBaseT< ui::Block > uiblocks
BlockAlertLevel alert_level
Button * last_but() const
Definition interface.cc:311
Vector< ButtonGroup > button_groups
ListBaseT< LayoutRoot > layouts
Vector< std::unique_ptr< bContextStore > > contexts
Vector< std::unique_ptr< Button > > buttons_ptrs
Button * first_but() const
Definition interface.cc:306
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
Button * next_but(const Button *but) const
Definition interface.cc:288
std::unique_ptr< PieMenuData > pie_data
PopupBlockHandle * handle
ButtonHandleFunc func
std::shared_ptr< TextWrapCache > wrap_cache
PopupAttachDirection popup_attach_direction
ButtonArgNFree func_argN_free_fn
ButtonHandleFunc func
RadialDirection pie_dir
MenuCreateFunc menu_create_func
ButtonArgNCopy func_argN_copy_fn
wmOperatorType * optype
const bContextStore * context
static void box_item_set(Item *item, bool box_item)
static void auto_fixed_size_set(Item *item, bool auto_fixed_size)
static void inside_property_split_set(Item *item, bool inside_prop_sep)
static bool use_property_decorate_no_pad(const Item *item)
static bool box_item(const Item *item)
static bool auto_fixed_size(const Item *item)
ItemInternalFlag flag_
ItemType type() const
void fixed_size_set(bool fixed_size)
LayoutColumn(ItemType type, LayoutRoot *root)
static void layout_translate_y(Layout *layout, int delta)
static int layout_space_get(Layout *layout)
static void init_from_parent(Layout *item, Layout *layout, int align)
static void layout_estimate(Layout *layout)
static Layout * item_prop_split_layout_hack(Layout *layout_parent, Layout *layout_split)
static void layout_move(Layout *layout, int delta_xmin, int delta_xmax)
static void layout_space_set(Layout *layout, int space)
static void layout_resolve(Layout *layout)
static ButtonItem * layout_find_button_item(const Layout *layout, const Button *but)
static void layout_add_but(Layout *layout, Button *but)
static void layout_remove_but(Layout *layout, const Button *but)
static void layout_offset_size_set(Layout *layout, int x, int y, int w, int h)
void resolve_dynamic_height() override
wm::OpCallContext opcontext
LayoutRow(LayoutRoot *root)
LayoutRow(ItemType type, LayoutRoot *root)
void prop_menu_enum(PointerRNA *ptr, PropertyRNA *prop, std::optional< StringRefNull > name, int icon)
void progress_indicator(const char *text, float factor, ButProgressType progress_type)
void enabled_set(bool enabled)
virtual void estimate_impl()
void textbox(const bContext *C, PointerRNA *ptr, StringRefNull propname, std::optional< StringRefNull > name_opt=std::nullopt, std::optional< StringRefNull > placeholder=std::nullopt, const int initial_visible_lines=3)
void alignment_set(LayoutAlign alignment)
void context_int_set(StringRef name, int64_t value)
void prop_search(PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop, PropertyRNA *item_searchpropname, std::optional< StringRefNull > name, int icon, bool results_are_suggestions)
Panel * root_panel() const
void use_property_decorate_set(bool is_decorate)
void textbox_with_state(PointerRNA *ptr, StringRefNull propname, TextboxState *textbox_state, std::optional< StringRefNull > name_opt=std::nullopt, std::optional< StringRefNull > placeholder=std::nullopt)
bool use_property_split() const
void label_multiline(StringRefNull label, int icon, FontStyleAlign align=UI_STYLE_TEXT_LEFT, int max_lines=0)
PointerRNA op_menu_hold(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag, const char *menu_id)
void operator_context_set(wm::OpCallContext opcontext)
std::optional< StringRefNull > context_string_get(const StringRef name) const
wm::OpCallContext operator_context() const
void use_property_split_set(bool is_sep)
LayoutRoot * root() const
PointerRNA op_menu_enum(const bContext *C, wmOperatorType *ot, StringRefNull propname, std::optional< StringRefNull > name, int icon)
Span< Item * > items() const
void link(StringRef url, StringRef name, int icon)
void label(StringRef name, int icon)
void decorator(PointerRNA *ptr, PropertyRNA *prop, int index)
const bContextStore * context() const
void menu_contents(StringRef menuname)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void op_enum_items(wmOperatorType *ot, const PointerRNA &ptr, PropertyRNA *prop, IDProperty *properties, wm::OpCallContext context, eUI_Item_Flag flag, const EnumPropertyItem *item_array, int totitem, int active=-1)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void context_set_from_but(const Button *but)
std::optional< int64_t > context_int_get(const StringRef name) const
Layout & column_flow(int number, bool align)
void prop_with_menu(PointerRNA *ptr, blender::StringRefNull propname, eUI_Item_Flag flag, std::optional< blender::StringRefNull > name, int icon, const char *menu_type)
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)
void menu(MenuType *mt, std::optional< StringRef > name, int icon)
const PointerRNA * context_ptr_get(const StringRef name, const StructRNA *type) const
EmbossType emboss_or_undefined() const
void active_set(bool active)
virtual void resolve_impl()
virtual void resolve_dynamic_height()
StringRef heading() const
void prop_with_popover(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRefNull > name, int icon, const char *panel_type)
Layout & split(float percentage, bool align)
LayoutAlign alignment() const
Layout & column(bool align)
bool use_property_decorate() const
void context_string_set(StringRef name, StringRef value)
PanelLayout panel_prop_with_bool_header(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name, PointerRNA *bool_prop_owner, StringRefNull bool_prop_name, std::optional< StringRef > label)
void op_enum(StringRefNull opname, StringRefNull propname, IDProperty *properties, wm::OpCallContext context, eUI_Item_Flag flag, const int active=-1)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & grid_flow(bool row_major, int columns_len, bool even_columns, bool even_rows, bool align)
void label_markdown(StringRef text)
Layout & absolute(bool align=false)
Layout & list_box(uiList *ui_list, PointerRNA *actptr, PropertyRNA *actprop)
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, StringRefNull open_prop_name)
Layout & row(bool align)
void prop_tabs_enum(bContext *C, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *ptr_highlight, PropertyRNA *prop_highlight, bool icon_only, EnumTabExpand expand_as=EnumTabExpand::Default)
void popover(const bContext *C, PanelType *pt, std::optional< StringRef > name_opt, int icon)
bool image(StringRef key, FunctionRef< ImBuf *()> create_fn)
Button * button(StringRef name, int icon, std::function< void(bContext &)> func, std::optional< StringRef > tooltip=std::nullopt)
static constexpr float PROPERTY_SPLIT_FACTOR
LayoutDirection local_direction() const
void context_ptr_set(StringRef name, const PointerRNA *ptr)
void props_enum(PointerRNA *ptr, StringRefNull propname)
void popover_group(bContext *C, int space_id, int region_id, const char *context, const char *category)
EmbossType emboss() const
void menu_fn_argN_free(StringRefNull name, int icon, MenuCreateFunc func, void *argN)
Layout * parent() const
void context_copy(const bContextStore *context)
void menu_fn(StringRefNull name, int icon, MenuCreateFunc func, void *arg)
Layout(ItemType type, LayoutRoot *root)
void prop_enum(PointerRNA *ptr, PropertyRNA *prop, int value, std::optional< StringRefNull > name, int icon)
void emboss_set(EmbossType emboss)
char propname[MAX_IDPROP_NAME]
char opname[OP_MAX_TYPENAME]
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
std::string(*) get_description(bContext *C, wmOperatorType *ot, PointerRNA *ptr)
Definition WM_types.hh:1197
bke::WindowRuntime * runtime
short buttonspacey
short buttonspacex
short templatespace
short panelspace
uiFontStyle widget
short columnspace