Blender V5.3
interface_templates.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 "BKE_library.hh"
10#include "BKE_screen.hh"
11
12#include "BLI_math_color_c.hh"
13#include "BLI_string_ref.hh"
14
15#include "ED_fileselect.hh"
16#include "ED_screen.hh"
17
18#include "RNA_access.hh"
19
21#include "interface_intern.hh"
23
24namespace blender::ui {
25
26/* -------------------------------------------------------------------- */
29
31{
32 char str[UI_MAX_DRAW_STR];
33 int buf_len = 0;
34
36
37 const char *name = RNA_property_string_get_alloc(ptr, name_prop, str, sizeof(str), &buf_len);
38
39 const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
40 const int margin = UI_UNIT_X * 0.75f;
41 const int estimated_width = fontstyle_string_width(fstyle, name) + margin;
42
43 if (name != str) {
44 MEM_delete(name);
45 }
46
47 /* Clamp to some min/max width. */
48 return std::clamp(
50}
51
56
58 Layout &layout,
59 Block *block,
60 PointerRNA *ptr,
61 PropertyRNA *prop,
62 BlockCreateFunc block_func,
63 void *block_argN,
64 const std::optional<StringRef> tip,
65 const bool use_previews,
66 const bool editable,
67 const bool live_icon,
68 ButtonArgNFree func_argN_free_fn,
69 ButtonArgNCopy func_argN_copy_fn)
70{
71 const PointerRNA active_ptr = RNA_property_pointer_get(ptr, prop);
72 ID *id = (active_ptr && RNA_struct_is_ID(active_ptr.type)) ? static_cast<ID *>(active_ptr.data) :
73 nullptr;
74 const ID *idfrom = ptr->owner_id;
75 const StructRNA *type = active_ptr.type ? active_ptr.type : RNA_property_pointer_type(ptr, prop);
76 Button *but;
77
78 if (use_previews) {
79 ARegion *region = CTX_wm_region(C);
80 /* Ugly tool header exception. */
81 const bool use_big_size = (region->regiontype != RGN_TYPE_TOOL_HEADER);
82 /* Ugly exception for screens here,
83 * drawing their preview in icon size looks ugly/useless */
84 const bool use_preview_icon = use_big_size || (id && (GS(id->name) != ID_SCR));
85 const short width = UI_UNIT_X * (use_big_size ? 6 : 1.6f);
86 const short height = UI_UNIT_Y * (use_big_size ? 6 : 1);
87 Layout *col = nullptr;
88
89 if (use_big_size) {
90 /* Assume column layout here. To be more correct, we should check if the layout passed to
91 * template_id is a column one, but this should work well in practice. */
92 col = &layout.column(true);
93 }
94
95 but = uiDefBlockButN(block,
96 block_func,
97 block_argN,
98 "",
99 0,
100 0,
101 width,
102 height,
103 tip,
104 func_argN_free_fn,
105 func_argN_copy_fn);
106 if (use_preview_icon) {
107 const int icon = id ? id_icon_get(C, id, use_big_size) : RNA_struct_ui_icon(type);
109 }
110 else {
113 }
114
115 if ((idfrom && !ID_IS_EDITABLE(idfrom)) || !editable) {
117 }
118 if (use_big_size) {
119 (col ? col : &layout)->row(true);
120 }
121 }
122 else {
123 but = uiDefBlockButN(block,
124 block_func,
125 block_argN,
126 "",
127 0,
128 0,
129 UI_UNIT_X * 1.6,
130 UI_UNIT_Y,
131 tip,
132 func_argN_free_fn,
133 func_argN_copy_fn);
134
135 if (live_icon) {
136 const int icon = id ? id_icon_get(C, id, false) : RNA_struct_ui_icon(type);
138 }
139 else {
141 }
142 if (id) {
143 /* default dragging of icon for id browse buttons */
144 button_drag_set_id(but, id);
145 }
147
148 if ((idfrom && !ID_IS_EDITABLE(idfrom)) || !editable) {
150 }
151 }
152}
153
155 ARegion *region,
156 ButtonSearchUpdateFn search_update_fn,
157 void *search_arg,
158 ButtonHandleFunc search_exec_fn,
159 void *active_item,
160 ButtonSearchTooltipFn item_tooltip_fn,
161 const int preview_rows,
162 const int preview_cols,
163 float scale)
164{
165 static char search[256];
166 wmWindow *win = CTX_wm_window(C);
167 Button *but;
168
169 /* clear initial search string, then all items show */
170 search[0] = 0;
171
172 Block *block = block_begin(C, region, "_popup", EmbossType::Emboss);
175
176 /* preview thumbnails */
177 if (preview_rows > 0 && preview_cols > 0) {
178 const int w = 4 * U.widget_unit * preview_cols * scale;
179 const int h = 5 * U.widget_unit * preview_rows * scale + 2 * UI_SEARCHBOX_TRIA_H -
181
182 /* fake button, it holds space for search items */
183 uiDefBut(block, ButtonType::Label, "", 0, UI_UNIT_Y, w, h, nullptr, 0, 0, std::nullopt);
184 but = uiDefSearchBut(block, search, ICON_VIEWZOOM, sizeof(search), 0, 0, w, UI_UNIT_Y, "");
185 button_search_preview_grid_size_set(but, preview_rows, preview_cols);
186 }
187 /* list view */
188 else {
189 const int searchbox_width = searchbox_size_x_guess(C, search_update_fn, search_arg);
190 const int searchbox_height = searchbox_size_y();
191 const int search_but_height = UI_UNIT_Y - 1.0f * UI_SCALE_FAC;
192
193 /* fake button, it holds space for search items */
194 uiDefBut(block,
196 "",
197 0,
198 search_but_height,
199 searchbox_width,
200 searchbox_height - UI_SEARCHBOX_BOUNDS,
201 nullptr,
202 0,
203 0,
204 std::nullopt);
205 but = uiDefSearchBut(block,
206 search,
207
208 ICON_VIEWZOOM,
209 sizeof(search),
210 0,
211 0,
212 searchbox_width,
213 search_but_height,
214 "");
215 }
218 search_update_fn,
219 search_arg,
220 false,
221 nullptr,
222 search_exec_fn,
223 active_item);
224 button_func_search_set_tooltip(but, item_tooltip_fn);
225
228
229 /* give search-field focus */
231 /* this type of search menu requires undo */
232 but->flag |= BUT_UNDO;
233
234 return block;
235}
236
238
239/* -------------------------------------------------------------------- */
242
244{
245 Block *block = layout->absolute().block();
247}
248
250
251/* -------------------------------------------------------------------- */
254
256 PointerRNA *ptr,
257 const StringRefNull propname,
258 PointerRNA * /*root_ptr*/,
259 const std::optional<StringRefNull> text)
260{
261 /* check that properties are valid */
262 PropertyRNA *propPath = RNA_struct_find_property(ptr, propname.c_str());
263 if (!propPath || RNA_property_type(propPath) != PROP_STRING) {
265 "path property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
266 return;
267 }
268
269 /* Start drawing UI Elements using standard defines */
270 Layout &row = layout->row(true);
271
272 /* Path (existing string) Widget */
273 row.prop(ptr, propname, UI_ITEM_NONE, text, ICON_RNA);
274
275 /* TODO: attach something to this to make allow
276 * searching of nested properties to 'build' the path */
277}
278
280
281/* -------------------------------------------------------------------- */
284
285void template_node_socket(Layout *layout, bContext * /*C*/, const float color[4])
286{
287 Block *block = layout->block();
288 block_align_begin(block);
289
290 /* XXX using explicit socket colors is not quite ideal.
291 * Eventually it should be possible to use theme colors for this purpose,
292 * but this requires a better design for extendable color palettes in user preferences. */
293 Button *but = uiDefBut(
294 block, ButtonType::NodeSocket, "", 0, 0, UI_UNIT_X, UI_UNIT_Y, nullptr, 0, 0, "");
296
297 block_align_end(block);
298}
299
301
302/* -------------------------------------------------------------------- */
305
307{
308 bScreen *screen = CTX_wm_screen(C);
309 SpaceFile *sfile = CTX_wm_space_file(C);
310
311 ED_file_path_button(screen, sfile, params, layout->block());
312}
313
315
316} // namespace blender::ui
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define UI_SCALE_FAC
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_SEARCHBOX_BOUNDS
#define UI_FSTYLE_WIDGET
#define UI_UNIT_X
#define UI_SEARCHBOX_TRIA_H
#define UI_ITEM_NONE
#define UI_MAX_DRAW_STR
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
constexpr const char * c_str() const
#define str(s)
float3x3 scale(float3x3 mat, float2 scale)
#define TEMPLATE_SEARCH_TEXTBUT_HEIGHT
#define TEMPLATE_SEARCH_TEXTBUT_MIN_WIDTH
void button_search_preview_grid_size_set(Button *but, int rows, int cols)
void template_file_select_path(Layout *layout, bContext *C, FileSelectParams *params)
void block_direction_set(Block *block, char direction)
int template_search_textbut_width(PointerRNA *ptr, PropertyRNA *name_prop)
void(*)(bContext *C, void *arg1, void *arg2) ButtonHandleFunc
void block_bounds_set_normal(Block *block, int addval)
Definition interface.cc:651
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)
void template_header(Layout *layout, bContext *C)
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)
void block_flag_enable(Block *block, int flag)
ARegion *(*)(bContext *C, ARegion *region, const rcti *item_rect, void *arg, void *active) ButtonSearchTooltipFn
int id_icon_get(const bContext *C, ID *id, const bool big)
void button_focus_on_enter_event(wmWindow *win, Button *but)
int searchbox_size_x_guess(const bContext *C, const ButtonSearchUpdateFn update_fn, void *arg)
void button_drawflag_enable(Button *but, int flag)
void template_node_socket(Layout *layout, bContext *C, const float color[4])
void button_flag_enable(Button *but, int64_t flag)
Block * block_begin(const bContext *C, ARegion *region, std::string name, EmbossType emboss)
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)
Block *(*)(bContext *C, ARegion *region, void *arg1) BlockCreateFunc
void *(*)(const void *argN) ButtonArgNCopy
Button * uiDefSearchBut(Block *block, void *arg, int icon, int maxncpy, int x, int y, short width, short height, std::optional< StringRef > tip)
void block_theme_style_set(Block *block, char theme_style)
void block_align_end(Block *block)
void def_but_icon(Button *but, const int icon, const int64_t flag)
ARegion * searchbox_create_generic(bContext *C, ARegion *butregion, ButtonSearch *search_but)
void(*)(const bContext *C, void *arg, const char *str, SearchItems *items, bool is_first) ButtonSearchUpdateFn
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)
void template_path_builder(Layout *layout, PointerRNA *ptr, StringRefNull propname, PointerRNA *root_ptr, std::optional< StringRefNull > text)
void(*)(void *argN) ButtonArgNFree
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_func_search_set_tooltip(Button *but, ButtonSearchTooltipFn tooltip_fn)
int template_search_textbut_height()
void button_drag_set_id(Button *but, ID *id)
char * RNA_property_string_get_alloc(PointerRNA *ptr, PropertyRNA *prop, char *fixedbuf, int fixedlen, int *r_len)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
wmWindow * CTX_wm_window(const bContext *C)
PropertyType RNA_property_type(PropertyRNA *prop)
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
@ PROP_STRING
Definition RNA_types.hh:213
void ED_file_path_button(bScreen *screen, SpaceFile *sfile, FileSelectParams *params, ui::Block *block)
Definition file_draw.cc:94
int RNA_struct_ui_icon(const StructRNA *type)
bool RNA_struct_is_ID(const StructRNA *type)
const char * RNA_struct_identifier(const StructRNA *type)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
int ED_area_header_switchbutton(const bContext *C, ui::Block *block, int yco)
Definition area.cc:3137
SpaceFile * CTX_wm_space_file(const bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
eRegion_Type regiontype
StructRNA * type
Definition RNA_types.hh:63
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)
Layout & absolute(bool align=false)
Layout & row(bool align)