Blender V5.3
interface_template_search.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#include "RNA_prototypes.hh"
13
14#include "BLT_translation.hh"
15
16#include "ANIM_action.hh"
17
19#include "interface_intern.hh"
21
22namespace blender::ui {
23
30
31static void template_search_exec_fn(bContext *C, void *arg_template, void *item)
32{
33 TemplateSearch *template_search = static_cast<TemplateSearch *>(arg_template);
34 RNACollectionSearch *coll_search = &template_search->search_data;
35 StructRNA *type = RNA_property_pointer_type(&coll_search->target_ptr, coll_search->target_prop);
36
37 PointerRNA item_ptr = RNA_pointer_create_discrete(nullptr, type, item);
38 RNA_property_pointer_set(&coll_search->target_ptr, coll_search->target_prop, item_ptr, nullptr);
39 RNA_property_update(C, &coll_search->target_ptr, coll_search->target_prop);
40}
41
42static Block *template_search_menu(bContext *C, ARegion *region, void *arg_template)
43{
45
46 /* arg_template is malloced, can be freed by parent button */
47 template_search = *(static_cast<TemplateSearch *>(arg_template));
48 PointerRNA active_ptr = RNA_property_pointer_get(&template_search.search_data.target_ptr,
49 template_search.search_data.target_prop);
50
52 region,
56 active_ptr.data,
57 nullptr,
58 template_search.preview_rows,
59 template_search.preview_cols,
60 1.0f);
61}
62
64 Layout *layout,
65 Block *block,
67 const bool editable,
68 const bool live_icon)
69{
70 const StringRef ui_description = RNA_property_ui_description(
71 template_search.search_data.target_prop);
72
74 *layout,
75 block,
76 &template_search.search_data.target_ptr,
77 template_search.search_data.target_prop,
79 MEM_new<TemplateSearch>(__func__, template_search),
80 ui_description,
81 template_search.use_previews,
82 editable,
83 live_icon,
86}
87
89 PointerRNA *active_ptr,
90 const StructRNA *type)
91{
92 /* Skip text button without an active item. */
93 if (!*active_ptr) {
94 return;
95 }
96
97 int iconid = ICON_NONE;
98
99 PropertyRNA *name_prop;
100 if (type == RNA_ActionSlot) {
101 name_prop = RNA_struct_find_property(active_ptr, "name_display");
102 /* Also show an icon for the data-block type that each slot is intended for. */
103 animrig::Slot &slot = reinterpret_cast<ActionSlot *>(active_ptr->data)->wrap();
104 iconid = icon_from_idcode(slot.idtype);
105 }
106 else {
107 name_prop = RNA_struct_name_property(type);
108 }
109
110 int width = template_search_textbut_width(active_ptr, name_prop);
111 if (iconid != ICON_NONE) {
112 /* Widen a bit to make room for the icon. #152027. */
113 width += int(18.0f * UI_SCALE_FAC);
114 }
115
116 const int height = template_search_textbut_height();
117 uiDefAutoButR(block, active_ptr, name_prop, 0, "", iconid, 0, 0, width, height);
118}
119
121 Block *block,
122 const char *const operator_name,
123 const wm::OpCallContext opcontext,
124 const int icon,
125 const bool editable,
126 const std::optional<StringRefNull> button_text = {})
127{
128 if (!operator_name) {
129 return;
130 }
131
132 Button *but;
133 if (button_text) {
134 const int button_width = std::max(
135 fontstyle_string_width(UI_FSTYLE_WIDGET, button_text->c_str()) + int(UI_UNIT_X * 1.5f),
136 UI_UNIT_X * 5);
137
138 but = uiDefIconTextButO(block,
140 operator_name,
141 opcontext,
142 icon,
143 *button_text,
144 0,
145 0,
146 button_width,
147 UI_UNIT_Y,
148 std::nullopt);
149 }
150 else {
151 but = uiDefIconButO(block,
153 operator_name,
154 opcontext,
155 icon,
156 0,
157 0,
158 UI_UNIT_X,
159 UI_UNIT_Y,
160 std::nullopt);
161 }
162
163 if (!editable) {
165 }
166}
167
169 Layout &layout,
171 const char *newop,
172 const char *unlinkop,
173 const std::optional<StringRef> text)
174{
175 Block *block = layout.block();
176 RNACollectionSearch *search_data = &template_search.search_data;
177 const StructRNA *type = RNA_property_pointer_type(&search_data->target_ptr,
178 search_data->target_prop);
179 const bool editable = RNA_property_editable(&search_data->target_ptr, search_data->target_prop);
180 PointerRNA active_ptr = RNA_property_pointer_get(&search_data->target_ptr,
181 search_data->target_prop);
182
183 if (active_ptr.has_type()) {
184 /* can only get correct type when there is an active item */
185 type = active_ptr.type;
186 }
187
188 Layout &row = layout.row(true);
189 block_align_begin(block);
190
191 Layout *decorator_layout = nullptr;
192 if (text && !text->is_empty()) {
193 /* Add label respecting the separated layout property split state. */
194 decorator_layout = uiItemL_respect_property_split(&row, *text, ICON_NONE);
195 }
196
197 template_search_add_button_searchmenu(C, &row, block, template_search, editable, false);
198 template_search_add_button_name(block, &active_ptr, type);
199
200 /* For Blender 4.4, the "New" button is only shown on Action Slot selectors.
201 * Blender 4.5 may have this enabled for all uses of this template, in which
202 * case this type-specific code will be removed. */
203 const bool may_show_new_button = (type == RNA_ActionSlot);
204 if (may_show_new_button && !active_ptr) {
206 block, newop, wm::OpCallContext::InvokeDefault, ICON_ADD, editable, IFACE_("New"));
207 }
208 else {
210 block, newop, wm::OpCallContext::InvokeDefault, ICON_DUPLICATE, editable);
212 block, unlinkop, wm::OpCallContext::InvokeRegionWin, ICON_X, editable);
213 }
214
215 block_align_end(block);
216
217 if (decorator_layout) {
218 decorator_layout->decorator(nullptr, "", RNA_NO_INDEX);
219 }
220}
221
223 PropertyRNA *targetprop,
224 PointerRNA *searchptr,
225 const char *const searchpropname)
226{
227 PropertyRNA *searchprop;
228
229 if (searchptr && !*searchptr) {
230 searchptr = nullptr;
231 }
232
233 if (!searchptr && !searchpropname) {
234 /* both nullptr means we don't use a custom rna collection to search in */
235 }
236 else if (!searchptr && searchpropname) {
237 RNA_warning("searchpropname defined (%s) but searchptr is missing", searchpropname);
238 }
239 else if (searchptr && !searchpropname) {
240 RNA_warning("searchptr defined (%s) but searchpropname is missing",
241 RNA_struct_identifier(searchptr->type));
242 }
243 else if (!(searchprop = RNA_struct_find_property(searchptr, searchpropname))) {
244 RNA_warning("search collection property not found: %s.%s",
245 RNA_struct_identifier(searchptr->type),
246 searchpropname);
247 }
248 else if (RNA_property_type(searchprop) != PROP_COLLECTION) {
249 RNA_warning("search collection property is not a collection type: %s.%s",
250 RNA_struct_identifier(searchptr->type),
251 searchpropname);
252 }
253 /* check if searchprop has same type as targetprop */
254 else if (RNA_property_pointer_type(searchptr, searchprop) !=
255 RNA_property_pointer_type(targetptr, targetprop))
256 {
257 RNA_warning("search collection items from %s.%s are not of type %s",
258 RNA_struct_identifier(searchptr->type),
259 searchpropname,
260 RNA_struct_identifier(RNA_property_pointer_type(targetptr, targetprop)));
261 }
262 else {
263 return searchprop;
264 }
265
266 return nullptr;
267}
268
270 PointerRNA *ptr,
271 const StringRefNull propname,
272 PointerRNA *searchptr,
273 const char *const searchpropname)
274{
275 template_search = {};
276 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
277
278 if (!prop || RNA_property_type(prop) != PROP_POINTER) {
280 "pointer property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
281 return false;
282 }
283 PropertyRNA *searchprop = template_search_get_searchprop(ptr, prop, searchptr, searchpropname);
284
285 template_search.search_data.target_ptr = *ptr;
286 template_search.search_data.target_prop = prop;
287 template_search.search_data.search_ptr = *searchptr;
288 template_search.search_data.search_prop = searchprop;
289
290 return true;
291}
292
294 const bContext *C,
295 PointerRNA *ptr,
296 const StringRefNull propname,
297 PointerRNA *searchptr,
298 const char *searchpropname,
299 const char *newop,
300 const char *unlinkop,
301 const std::optional<StringRef> text)
302{
304 if (template_search_setup(template_search, ptr, propname, searchptr, searchpropname)) {
305 template_search_buttons(C, *layout, template_search, newop, unlinkop, text);
306 }
307}
308
310 bContext *C,
311 PointerRNA *ptr,
312 const StringRefNull propname,
313 PointerRNA *searchptr,
314 const char *searchpropname,
315 const char *newop,
316 const char *unlinkop,
317 const int rows,
318 const int cols,
319 const std::optional<StringRef> text)
320{
322 if (template_search_setup(template_search, ptr, propname, searchptr, searchpropname)) {
323 template_search.use_previews = true;
324 template_search.preview_rows = rows;
325 template_search.preview_cols = cols;
326
327 template_search_buttons(C, *layout, template_search, newop, unlinkop, text);
328 }
329}
330
331} // namespace blender::ui
Functions and classes to work with Actions.
#define IFACE_(msgid)
#define UI_SCALE_FAC
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_FSTYLE_WIDGET
#define UI_UNIT_X
#define RNA_NO_INDEX
constexpr const char * c_str() const
BLI_INLINE Texture * wrap(MTLTexture *texture)
int template_search_textbut_width(PointerRNA *ptr, PropertyRNA *name_prop)
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)
static Block * template_search_menu(bContext *C, ARegion *region, void *arg_template)
void template_search_preview(Layout *layout, bContext *C, PointerRNA *ptr, StringRefNull propname, PointerRNA *searchptr, const char *searchpropname, const char *newop, const char *unlinkop, int rows, int cols, std::optional< StringRef > text=std::nullopt)
int fontstyle_string_width(const uiFontStyle *fs, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void template_add_button_search_menu(const bContext *C, Layout &layout, Block *block, PointerRNA *ptr, PropertyRNA *prop, BlockCreateFunc block_func, void *block_argN, const std::optional< StringRef > tip, const bool use_previews, const bool editable, const bool live_icon, ButtonArgNFree func_argN_free_fn, ButtonArgNCopy func_argN_copy_fn)
void block_align_begin(Block *block)
int icon_from_idcode(int idcode)
static void template_search_add_button_searchmenu(const bContext *C, Layout *layout, Block *block, TemplateSearch &template_search, const bool editable, const bool live_icon)
Block * template_common_search_menu(const bContext *C, ARegion *region, ButtonSearchUpdateFn search_update_fn, void *search_arg, ButtonHandleFunc search_exec_fn, void *active_item, ButtonSearchTooltipFn item_tooltip_fn, const int preview_rows, const int preview_cols, float scale)
static void template_search_add_button_name(Block *block, PointerRNA *active_ptr, const StructRNA *type)
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)
void button_drawflag_enable(Button *but, int flag)
void * but_func_argN_copy(const void *argN)
void template_search(Layout *layout, const bContext *C, PointerRNA *ptr, StringRefNull propname, PointerRNA *searchptr, const char *searchpropname, const char *newop, const char *unlinkop, std::optional< StringRef > text=std::nullopt)
static void template_search_exec_fn(bContext *C, void *arg_template, void *item)
void block_align_end(Block *block)
Button * uiDefIconTextButO(Block *block, ButtonType type, StringRefNull, wm::OpCallContext opcontext, int icon, StringRef str, int x, int y, short width, short height, std::optional< StringRef > tip)
static bool template_search_setup(TemplateSearch &template_search, PointerRNA *ptr, const StringRefNull propname, PointerRNA *searchptr, const char *const searchpropname)
void rna_collection_search_update_fn(const bContext *C, void *arg, const char *str, SearchItems *items, bool is_first)
void but_func_argN_free(void *argN)
Layout * uiItemL_respect_property_split(Layout *layout, StringRef text, int icon)
static void template_search_add_button_operator(Block *block, const char *const operator_name, const wm::OpCallContext opcontext, const int icon, const bool editable, const std::optional< StringRefNull > button_text={})
static PropertyRNA * template_search_get_searchprop(PointerRNA *targetptr, PropertyRNA *targetprop, PointerRNA *searchptr, const char *const searchpropname)
int template_search_textbut_height()
static void template_search_buttons(const bContext *C, Layout &layout, TemplateSearch &template_search, const char *newop, const char *unlinkop, const std::optional< StringRef > text)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
PropertyType RNA_property_type(PropertyRNA *prop)
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_COLLECTION
Definition RNA_types.hh:216
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
PropertyRNA * RNA_struct_name_property(const StructRNA *type)
const char * RNA_struct_identifier(const StructRNA *type)
const char * RNA_property_ui_description(const PropertyRNA *prop, const PointerRNA *ptr)
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
bool has_type() const
Definition RNA_types.hh:180
StructRNA * type
Definition RNA_types.hh:63
void decorator(PointerRNA *ptr, PropertyRNA *prop, int index)
Layout & row(bool align)