Blender V5.0
interface_template_icon.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_string_ref.hh"
10
11#include "RNA_access.hh"
12
14#include "interface_intern.hh"
15
17
24
25/* ID Search browse menu, open */
26static uiBlock *ui_icon_view_menu_cb(bContext *C, ARegion *region, void *arg_litem)
27{
28 static IconViewMenuArgs args;
29
30 /* arg_litem is malloced, can be freed by parent button */
31 args = *((IconViewMenuArgs *)arg_litem);
32 const int w = UI_UNIT_X * (args.icon_scale);
33 const int h = UI_UNIT_X * (args.icon_scale + args.show_labels);
34
35 uiBlock *block = UI_block_begin(C, region, "_popup", blender::ui::EmbossType::Pulldown);
38
39 bool free;
40 const EnumPropertyItem *item;
41 RNA_property_enum_items(C, &args.ptr, args.prop, &item, nullptr, &free);
42
43 for (int a = 0; item[a].identifier; a++) {
44 const int x = (a % 8) * w;
45 const int y = -(a / 8) * h;
46
47 const int icon = item[a].icon;
48 const int value = item[a].value;
49 uiBut *but;
50 if (args.show_labels) {
51 but = uiDefIconTextButR_prop(block,
53 0,
54 icon,
55 item[a].name,
56 x,
57 y,
58 w,
59 h,
60 &args.ptr,
61 args.prop,
62 -1,
63 0,
64 value,
65 std::nullopt);
66 }
67 else {
68 but = uiDefIconButR_prop(block,
70 0,
71 icon,
72 x,
73 y,
74 w,
75 h,
76 &args.ptr,
77 args.prop,
78 -1,
79 0,
80 value,
81 std::nullopt);
82 }
84 }
85
86 UI_block_bounds_set_normal(block, 0.3f * U.widget_unit);
88
89 if (free) {
90 MEM_freeN(item);
91 }
92
93 return block;
94}
95
96void uiTemplateIcon(uiLayout *layout, int icon_value, float icon_scale)
97{
98 uiBlock *block = layout->absolute_block();
99 uiBut *but = uiDefIconBut(block,
101 0,
102 ICON_X,
103 0,
104 0,
105 UI_UNIT_X * icon_scale,
106 UI_UNIT_Y * icon_scale,
107 nullptr,
108 0.0,
109 0.0,
110 "");
112}
113
116 const StringRefNull propname,
117 bool show_labels,
118 float icon_scale,
119 float icon_scale_popup)
120{
121 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
122
123 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
124 RNA_warning("property of type Enum not found: %s.%s",
126 propname.c_str());
127 return;
128 }
129
130 uiBlock *block = layout->absolute_block();
131
132 int tot_items;
133 bool free_items;
134 const EnumPropertyItem *items;
136 static_cast<bContext *>(block->evil_C), ptr, prop, &items, &tot_items, &free_items);
137 const int value = RNA_property_enum_get(ptr, prop);
138 int icon = ICON_NONE;
139 RNA_enum_icon_from_value(items, value, &icon);
140
141 uiBut *but;
142 if (RNA_property_editable(ptr, prop)) {
143 IconViewMenuArgs *cb_args = MEM_new<IconViewMenuArgs>(__func__);
144 cb_args->ptr = *ptr;
145 cb_args->prop = prop;
146 cb_args->show_labels = show_labels;
147 cb_args->icon_scale = icon_scale_popup;
148
149 but = uiDefBlockButN(block,
151 cb_args,
152 "",
153 0,
154 0,
155 UI_UNIT_X * icon_scale,
156 UI_UNIT_Y * icon_scale,
157 "",
160 }
161 else {
162 but = uiDefIconBut(block,
164 0,
165 ICON_X,
166 0,
167 0,
168 UI_UNIT_X * icon_scale,
169 UI_UNIT_Y * icon_scale,
170 nullptr,
171 0.0,
172 0.0,
173 "");
174 }
175
177
178 if (free_items) {
179 MEM_freeN(items);
180 }
181}
void BLI_kdtree_nd_ free(KDTree *tree)
#define RNA_warning(format,...)
@ PROP_ENUM
Definition RNA_types.hh:166
#define C
Definition RandGen.cpp:29
void * but_func_argN_copy(const void *argN)
void but_func_argN_free(void *argN)
#define UI_UNIT_Y
@ UI_BLOCK_LOOP
void UI_block_theme_style_set(uiBlock *block, char theme_style)
uiBlock * UI_block_begin(const bContext *C, ARegion *region, std::string name, blender::ui::EmbossType emboss)
@ UI_BUT_ICON_PREVIEW
uiBut * uiDefIconBut(uiBlock *block, uiButTypeWithPointerType but_and_ptr_type, int retval, int icon, int x, int y, short width, short height, void *poin, float min, float max, std::optional< blender::StringRef > tip)
void UI_block_bounds_set_normal(uiBlock *block, int addval)
Definition interface.cc:637
uiBut * uiDefIconTextButR_prop(uiBlock *block, ButType type, int retval, int icon, std::optional< blender::StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< blender::StringRef > tip)
uiBut * uiDefIconButR_prop(uiBlock *block, ButType type, int retval, int icon, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< blender::StringRef > tip)
void UI_block_direction_set(uiBlock *block, char direction)
@ UI_BLOCK_THEME_STYLE_POPUP
#define UI_UNIT_X
void UI_block_flag_enable(uiBlock *block, int flag)
uiBut * uiDefBlockButN(uiBlock *block, uiBlockCreateFunc func, void *argN, blender::StringRef str, int x, int y, short width, short height, std::optional< blender::StringRef > tip, uiButArgNFree func_argN_free_fn=MEM_freeN, uiButArgNCopy func_argN_copy_fn=MEM_dupallocN)
@ UI_DIR_DOWN
#define U
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr const char * c_str() const
void ui_def_but_icon(uiBut *but, const int icon, const int flag)
@ UI_HAS_ICON
void uiTemplateIcon(uiLayout *layout, int icon_value, float icon_scale)
static uiBlock * ui_icon_view_menu_cb(bContext *C, ARegion *region, void *arg_litem)
void uiTemplateIconView(uiLayout *layout, PointerRNA *ptr, const StringRefNull propname, bool show_labels, float icon_scale, float icon_scale_popup)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
const char * name
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
bool RNA_enum_icon_from_value(const EnumPropertyItem *item, int value, int *r_icon)
PropertyType RNA_property_type(PropertyRNA *prop)
const char * RNA_struct_identifier(const StructRNA *type)
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
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)
const char * identifier
Definition RNA_types.hh:657
uiBlock * absolute_block()
PointerRNA * ptr
Definition wm_files.cc:4238