Blender V5.3
interface_template_filepath.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "BLI_path_utils.hh"
12#include "BLI_string.hh"
13#include "BLI_string_ref.hh"
14
15#include "RNA_access.hh"
16#include "RNA_prototypes.hh"
17
19#include "interface_intern.hh"
21
22namespace blender::ui {
23
32
33namespace {
34
36struct PathSplitEdit {
37 enum class Part {
38 Directory,
39 Name,
40 };
41
42 PointerRNA ptr;
43 PropertyRNA *prop;
44 Part part;
45
47 char text[FILE_MAX];
48};
49
50} // namespace
51
52/* Split the path property into a parent directory and name.
53 * Returns true if the path has a trailing slash. */
54static bool path_split_get(PointerRNA &ptr,
55 PropertyRNA *prop,
56 char (&r_dir)[FILE_MAX],
57 char (&r_name)[FILE_MAXFILE])
58{
59 /* String properties may be longer than #FILE_MAX, truncate them. */
60 char path[FILE_MAX];
61 STRNCPY(path, RNA_property_string_get(&ptr, prop).c_str());
62
63 const size_t path_len = strlen(path);
64 const bool has_trailing_slash = path_len > 0 &&
65 BLI_path_slash_is_native_compat(path[path_len - 1]);
66
68
69 /* If we stripped away all slashes from a path like "//", "/" or "C:\",
70 * restore the original one and give an empty name. */
71 if (has_trailing_slash && (path[0] == '\0' || BLI_path_is_win32_drive_only(path))) {
72 STRNCPY(r_dir, RNA_property_string_get(&ptr, prop).c_str());
73 r_name[0] = '\0';
74 return true;
75 }
76
77 BLI_path_split_dir_file(path, r_dir, sizeof(r_dir), r_name, sizeof(r_name));
78 return has_trailing_slash;
79}
80
81static void path_split_edit_apply(bContext *C, void *argN, void * /*arg2*/)
82{
83 PathSplitEdit *edit = static_cast<PathSplitEdit *>(argN);
84
85 /* An empty name would turn the path into its parent directory. */
86 if (edit->part == PathSplitEdit::Part::Name &&
87 (edit->text[0] == '\0' || BLI_path_slash_find(edit->text)))
88 {
89 return;
90 }
91
92 char dir[FILE_MAX];
93 char name[FILE_MAXFILE];
94 const bool has_trailing_slash = path_split_get(edit->ptr, edit->prop, dir, name);
95
96 char new_path[FILE_MAX];
97 if (edit->part == PathSplitEdit::Part::Name) {
98 BLI_path_join(new_path, sizeof(new_path), dir, edit->text);
99 }
100 else {
101 BLI_path_join(new_path, sizeof(new_path), edit->text, name);
102 }
103
104 /* Preserve trailing slash, as used for directory paths. */
105 if (has_trailing_slash) {
106 BLI_path_slash_ensure(new_path, sizeof(new_path));
107 }
108
109 RNA_property_string_set(&edit->ptr, edit->prop, new_path);
110 RNA_property_update(C, &edit->ptr, edit->prop);
111}
112
113static void path_split_edit_free(void *argN)
114{
115 MEM_delete(static_cast<PathSplitEdit *>(argN));
116}
117
118static void *path_split_edit_copy(const void *argN)
119{
120 return MEM_new<PathSplitEdit>(__func__, *static_cast<const PathSplitEdit *>(argN));
121}
122
123static bool path_split_edit_identity_cmp(const Button *a, const Button *b)
124{
125 const PathSplitEdit *edit_a = static_cast<const PathSplitEdit *>(a->func_argN);
126 const PathSplitEdit *edit_b = static_cast<const PathSplitEdit *>(b->func_argN);
127 return edit_a->ptr.data == edit_b->ptr.data && edit_a->prop == edit_b->prop &&
128 edit_a->part == edit_b->part;
129}
130
131/* Parent directory or name button for split paths. */
133 const PointerRNA &ptr,
134 PropertyRNA *prop,
135 const PathSplitEdit::Part part)
136{
137 PathSplitEdit *edit = MEM_new<PathSplitEdit>(__func__);
138 edit->ptr = ptr;
139 edit->prop = prop;
140 edit->part = part;
141
142 char dir[FILE_MAX];
143 char name[FILE_MAXFILE];
144 path_split_get(edit->ptr, prop, dir, name);
145 if (part == PathSplitEdit::Part::Name) {
146 STRNCPY(edit->text, name);
147 }
148 else {
149 /* Without trailing slash (unless that leaves nothing). */
150 STRNCPY(edit->text, dir);
151 BLI_path_slash_rstrip(edit->text);
152 if (edit->text[0] == '\0') {
153 STRNCPY(edit->text, dir);
154 }
155 }
156
157 Block *block = layout.block();
158 block_layout_set_current(block, &layout);
159 Button *but = uiDefBut(block,
161 "",
162 0,
163 0,
164 UI_UNIT_X * 5,
165 UI_UNIT_Y,
166 edit->text,
167 0,
168 (part == PathSplitEdit::Part::Name) ? FILE_MAXFILE : FILE_MAX,
170 if (!RNA_property_editable(&edit->ptr, prop)) {
172 }
173 if (!RNA_property_string_is_utf8(prop)) {
175 }
176 if (!(edit->ptr.owner_id && ID_CHECK_UNDO(edit->ptr.owner_id) &&
177 RNA_property_undo_check(prop, edit->ptr.type)))
178 {
180 }
181
185
186 return but;
187}
188
189/* Parent directory and name text separated by a splash. */
191 TemplatePathData &path_data,
192 const std::optional<StringRef> text,
193 const std::optional<StringRef> placeholder)
194{
195 const StringRef label = text ? *text : RNA_property_ui_name(path_data.path_prop);
196 if (!label.is_empty()) {
197 row.label(label, ICON_NONE);
198 }
199
200 Layout &parts_row = row.row(true);
201
202 /* Parent directory field. */
204 parts_row, path_data.ptr, path_data.path_prop, PathSplitEdit::Part::Directory);
205 if (placeholder && RNA_property_string_length(&path_data.ptr, path_data.path_prop) == 0) {
206 button_placeholder_set(dir_but, *placeholder);
207 }
208
209 /* Slash label. */
210 Layout &separator = parts_row.row(true);
211 separator.ui_units_x_set(0.5f);
212 separator.fixed_size_set(true);
213 separator.label_multiline(SEP_STR, ICON_NONE, UI_STYLE_TEXT_CENTER);
214
215 /* Name field. */
216 Layout &name_row = parts_row.row(true);
217 name_row.ui_units_x_set(7.0f);
218 name_row.fixed_size_set(true);
220 name_row, path_data.ptr, path_data.path_prop, PathSplitEdit::Part::Name);
221
222 /* Hidden button for the browser operator to work. */
223 block_layout_set_current(parts_row.block(), &parts_row);
224 Button *but = uiDefAutoButR(parts_row.block(),
225 &path_data.ptr,
226 path_data.path_prop,
227 -1,
228 "",
229 ICON_NONE,
230 0,
231 0,
232 0,
233 UI_UNIT_Y);
234 but->flag |= UI_HIDDEN;
235}
236
237static void template_filepath_buttons(const bContext * /* C */,
238 Layout &layout,
239 TemplatePathData &path_data,
240 const char *pathselect_op,
241 std::optional<StringRef> text,
242 const std::optional<StringRef> placeholder,
243 const bool split_name)
244{
245 /* Don't align open button for split path, since it applies to both parts. */
246 const bool align = !split_name;
247 Layout &row = layout.row(align);
248
249 Layout *decorate_column = nullptr;
250 if (layout.use_property_split()) {
252 split.label_column->label(text ? *text : RNA_property_ui_name(path_data.path_prop), ICON_NONE);
253 decorate_column = split.decorate_column;
254 text = "";
255 }
256
257 if (split_name) {
258 template_filepath_split_name_buttons(row, path_data, text, placeholder);
259 }
260 else {
261 row.prop(&path_data.ptr,
262 path_data.path_prop,
264 0,
266 text,
267 ICON_NONE,
268 placeholder);
269 }
270
271 const int icon = RNA_property_editable(&path_data.ptr, path_data.path_prop) ?
272 ICON_FILEBROWSER :
273 ICON_FOLDER_REDIRECT;
274 if (pathselect_op) {
275 PointerRNA op_ptr = row.op(
276 pathselect_op, std::nullopt, icon, wm::OpCallContext::InvokeDefault, ITEM_R_ICON_ONLY);
277 /* Unknown operators return an empty pointer. */
278 if (op_ptr.data && path_data.path_prop_subtype == PROP_FILEPATH && path_data.filter_glob) {
279 PropertyRNA *filter_glob_prop = RNA_struct_find_property(&op_ptr, "filter_glob");
280 if (filter_glob_prop) {
281 RNA_property_string_set(&op_ptr, filter_glob_prop, path_data.filter_glob->c_str());
282 }
283 }
284 }
285 else if (path_data.path_prop_subtype == PROP_DIRPATH) {
286 /* #BUTTONS_OT_directory_browse calls #context_active_but_prop_get_filebrowser. */
287 row.op("BUTTONS_OT_directory_browse",
288 std::nullopt,
289 icon,
292 }
293 else {
294 /* #BUTTONS_OT_file_browse calls #context_active_but_prop_get_filebrowser. */
295 PointerRNA op_ptr = row.op("BUTTONS_OT_file_browse",
296 std::nullopt,
297 icon,
300 if (path_data.filter_glob) {
301 RNA_string_set(&op_ptr, "filter_glob", path_data.filter_glob->c_str());
302 }
303 }
304
305 if (decorate_column) {
306 decorate_column->decorator(&path_data.ptr, path_data.path_prop, RNA_NO_INDEX);
307 }
308}
309
311 const bContext *C,
312 PointerRNA *ptr,
313 const StringRefNull propname,
314 const char *pathselect_op,
315 const char *filter_glob,
316 const std::optional<StringRef> text,
317 const std::optional<StringRef> placeholder,
318 const bool split_name)
319{
320 TemplatePathData template_path_data;
321 template_path_data.ptr = *ptr;
322
323 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
324 if (!prop || RNA_property_type(prop) != PROP_STRING) {
326 "String property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
327 return;
328 }
329 template_path_data.path_prop = prop;
330
331 template_path_data.path_prop_subtype = RNA_property_subtype(prop);
332 if (!ELEM(template_path_data.path_prop_subtype, PROP_FILEPATH, PROP_DIRPATH)) {
333 RNA_warning("String property is not a dirpath or filepath: %s.%s",
335 propname.c_str());
336 return;
337 }
338
339 if (filter_glob) {
340 template_path_data.filter_glob = filter_glob;
341 }
342
344 C, *layout, template_path_data, pathselect_op, text, placeholder, split_name);
345}
346
347} // namespace blender::ui
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define ID_CHECK_UNDO(id)
Definition DNA_ID.h:686
Read Blender memory allocation and freeing.
#define RNA_warning(format,...)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define RNA_NO_INDEX
#define ELEM(...)
constexpr const char * c_str() const
constexpr bool is_empty() const
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
void template_filepath(Layout *layout, const bContext *C, PointerRNA *ptr, const StringRefNull propname, const char *pathselect_op, const char *filter_glob, const std::optional< StringRef > name, const std::optional< StringRef > placeholder, bool split_name=false)
static void template_filepath_split_name_buttons(Layout &row, TemplatePathData &path_data, const std::optional< StringRef > text, const std::optional< StringRef > placeholder)
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 void * path_split_edit_copy(const void *argN)
static bool path_split_edit_identity_cmp(const Button *a, const Button *b)
static void path_split_edit_free(void *argN)
void button_func_identity_compare_set(Button *but, ButtonIdentityCompareFunc cmp_fn)
static void template_filepath_buttons(const bContext *, Layout &layout, TemplatePathData &path_data, const char *pathselect_op, std::optional< StringRef > text, const std::optional< StringRef > placeholder, const bool split_name)
void button_flag_enable(Button *but, int64_t flag)
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 button_placeholder_set(Button *but, StringRef placeholder_text)
void button_funcN_set(Button *but, ButtonHandleNFunc funcN, void *argN, void *arg2, ButtonArgNFree func_argN_free_fn=MEM_delete_void, ButtonArgNCopy func_argN_copy_fn=MEM_dupalloc_void)
void button_flag_disable(Button *but, int64_t flag)
static bool path_split_get(PointerRNA &ptr, PropertyRNA *prop, char(&r_dir)[FILE_MAX], char(&r_name)[FILE_MAXFILE])
static void path_split_edit_apply(bContext *C, void *argN, void *)
void block_layout_set_current(Block *block, Layout *layout)
PropertySplitWrapper uiItemPropertySplitWrapperCreate(Layout *parent_layout)
static Button * template_filepath_split_button(Layout &layout, const PointerRNA &ptr, PropertyRNA *prop, const PathSplitEdit::Part part)
char * STRNCPY(char(&dst)[N], const char *src)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
int RNA_property_string_length(PointerRNA *ptr, PropertyRNA *prop)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
BLI_INLINE bool BLI_path_slash_is_native_compat(const char ch)
std::string RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_string_is_utf8(PropertyRNA *prop)
PropertyType RNA_property_type(PropertyRNA *prop)
bool RNA_property_undo_check(const PropertyRNA *prop, const StructRNA *type)
void BLI_path_slash_rstrip(char *path) ATTR_NONNULL(1)
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
@ PROP_STRING
Definition RNA_types.hh:213
bool BLI_path_is_win32_drive_only(const char *path)
int BLI_path_slash_ensure(char *path, size_t path_maxncpy) ATTR_NONNULL(1)
const char * RNA_struct_identifier(const StructRNA *type)
PropertySubType RNA_property_subtype(PropertyRNA *prop)
const char * RNA_property_ui_description(const PropertyRNA *prop, const PointerRNA *ptr)
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
@ PROP_FILEPATH
Definition RNA_types.hh:284
@ PROP_DIRPATH
Definition RNA_types.hh:285
const char * BLI_path_slash_find(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
StructRNA * type
Definition RNA_types.hh:63
void fixed_size_set(bool fixed_size)
bool use_property_split() const
void label_multiline(StringRefNull label, int icon, FontStyleAlign align=UI_STYLE_TEXT_LEFT, int max_lines=0)
void label(StringRef name, int icon)
void decorator(PointerRNA *ptr, PropertyRNA *prop, int index)
void ui_units_x_set(float width)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
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 & row(bool align)
std::optional< std::string > filter_glob
#define SEP_STR
Definition unit.cc:41