Blender V5.3
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
16namespace blender::ui {
17
24
25/* ID Search browse menu, open */
26static Block *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 = *(static_cast<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 Block *block = block_begin(C, region, "_popup", 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 Button *but;
50 if (args.show_labels) {
51 but = uiDefIconTextButR_prop(block,
53 icon,
54 item[a].name,
55 x,
56 y,
57 w,
58 h,
59 &args.ptr,
60 args.prop,
61 -1,
62 0,
63 value,
64 std::nullopt);
65 }
66 else {
67 but = uiDefIconButR_prop(block,
69 icon,
70 x,
71 y,
72 w,
73 h,
74 &args.ptr,
75 args.prop,
76 -1,
77 0,
78 value,
79 std::nullopt);
80 }
82 }
83
84 block_bounds_set_normal(block, 0.3f * U.widget_unit);
86
87 if (free) {
88 MEM_delete(item);
89 }
90
91 return block;
92}
93
94void template_icon(Layout *layout, int icon_value, float icon_scale)
95{
96 Block *block = layout->absolute().block();
97 Button *but = uiDefIconBut(block,
99 ICON_X,
100 0,
101 0,
102 UI_UNIT_X * icon_scale,
103 UI_UNIT_Y * icon_scale,
104 nullptr,
105 0.0,
106 0.0,
107 "");
108 def_but_icon(but, icon_value, UI_HAS_ICON | BUT_ICON_PREVIEW);
109}
110
112 PointerRNA *ptr,
113 const StringRefNull propname,
114 bool show_labels,
115 float icon_scale,
116 float icon_scale_popup)
117{
118 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
119
120 if (!prop || RNA_property_type(prop) != PROP_ENUM) {
121 RNA_warning("property of type Enum not found: %s.%s",
123 propname.c_str());
124 return;
125 }
126
127 Block *block = layout->absolute().block();
128
129 int tot_items;
130 bool free_items;
131 const EnumPropertyItem *items;
133 static_cast<bContext *>(block->evil_C), ptr, prop, &items, &tot_items, &free_items);
134 const int value = RNA_property_enum_get(ptr, prop);
135 int icon = ICON_NONE;
136 RNA_enum_icon_from_value(items, value, &icon);
137
138 Button *but;
139 if (RNA_property_editable(ptr, prop)) {
140 IconViewMenuArgs *cb_args = MEM_new<IconViewMenuArgs>(__func__);
141 cb_args->ptr = *ptr;
142 cb_args->prop = prop;
143 cb_args->show_labels = show_labels;
144 cb_args->icon_scale = icon_scale_popup;
145
146 but = uiDefBlockButN(block,
148 cb_args,
149 "",
150 0,
151 0,
152 UI_UNIT_X * icon_scale,
153 UI_UNIT_Y * icon_scale,
154 "",
157 }
158 else {
159 but = uiDefIconBut(block,
161 ICON_X,
162 0,
163 0,
164 UI_UNIT_X * icon_scale,
165 UI_UNIT_Y * icon_scale,
166 nullptr,
167 0.0,
168 0.0,
169 "");
170 }
171
173
174 if (free_items) {
175 MEM_delete(items);
176 }
177}
178
179} // namespace blender::ui
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr const char * c_str() const
void free(Subdiv *subdiv)
Definition subdiv.cc:192
void block_direction_set(Block *block, char direction)
void block_bounds_set_normal(Block *block, int addval)
Definition interface.cc:651
void block_flag_enable(Block *block, int flag)
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)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
void * but_func_argN_copy(const void *argN)
void block_theme_style_set(Block *block, char theme_style)
void template_icon(Layout *layout, int icon_value, float icon_scale)
Button * uiDefIconTextButR_prop(Block *block, ButtonType type, int icon, std::optional< StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< StringRef > tip)
void but_func_argN_free(void *argN)
void def_but_icon(Button *but, const int icon, const int64_t flag)
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)
Button * uiDefBlockButN(Block *block, BlockCreateFunc func, void *argN, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip, ButtonArgNFree func_argN_free_fn=MEM_delete_void, ButtonArgNCopy func_argN_copy_fn=MEM_dupalloc_void)
static Block * icon_view_menu_cb(bContext *C, ARegion *region, void *arg_litem)
void template_icon_view(Layout *layout, PointerRNA *ptr, StringRefNull propname, bool show_labels, float icon_scale, float icon_scale_popup)
UserDef U
Definition blender.cc:58
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
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)
PropertyType RNA_property_type(PropertyRNA *prop)
bool RNA_enum_icon_from_value(const EnumPropertyItem *item, int value, int *r_icon)
const char * RNA_struct_identifier(const StructRNA *type)
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
StructRNA * type
Definition RNA_types.hh:63
Layout & absolute(bool align=false)