Blender V5.3
interface_context_path.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLF_api.hh"
10#include "BLI_vector.hh"
11
12#include "RNA_access.hh"
13
14#include "UI_interface.hh"
16#include "UI_resources.hh"
17
18#include "RNA_prototypes.hh"
19
20namespace blender::ui {
21
23 StructRNA &rna_type,
24 void *ptr,
25 const BIFIconID icon_override,
26 std::function<void(bContext &)> handle_func,
27 MenuCreateFunc menu_func,
28 void *menu_arg)
29{
30 /* Add the null check here to make calling functions less verbose. */
31 if (!ptr) {
32 return;
33 }
34
35 PointerRNA rna_ptr = RNA_pointer_create_discrete(nullptr, &rna_type, ptr);
36 char name_buf[128], *name;
37 name = RNA_struct_name_get_alloc(&rna_ptr, name_buf, sizeof(name_buf), nullptr);
38
39 /* Use a blank icon by default to check whether to retrieve it automatically from the type. */
40 const BIFIconID icon = icon_override == ICON_NONE ? RNA_struct_ui_icon(rna_ptr.type) :
41 icon_override;
42
43 if (&rna_type == RNA_NodeTree) {
44 ID *id = static_cast<ID *>(ptr);
45 path.append({name, icon, ID_REAL_USERS(id), handle_func, menu_func, menu_arg});
46 }
47 else {
48 path.append({name, icon, 1, handle_func, menu_func, menu_arg});
49 }
50 if (name != name_buf) {
51 MEM_delete(name);
52 }
53}
54
55/* -------------------------------------------------------------------- */
58
60{
61 Layout &row = layout.row(true);
63
64 for (const int i : context_path.index_range()) {
65 Layout &sub_row = row.row(true);
67
68 Button *but;
69 int icon = context_path[i].icon;
70 std::string name = context_path[i].name;
71 if (context_path[i].handle_func) {
72 /* Pull-down adds more left padding to icons, making them misaligned with label icons. */
74 but = sub_row.button(name.c_str(), icon, context_path[i].handle_func);
75 }
76 else {
77 but = uiItemL_ex(&sub_row, name.c_str(), icon, false, false);
78 }
79 button_icon_indicator_number_set(but, context_path[i].icon_indicator_number);
80
81 if (context_path[i].menu_func) {
83 sub_row.menu_fn(
84 "", ICON_RIGHTARROW_THIN, context_path[i].menu_func, context_path[i].menu_arg);
85 }
86 else if (i < context_path.size() - 1) {
87 sub_row.label("", ICON_RIGHTARROW_THIN);
88 }
89 }
90}
91
93
94} // namespace blender::ui
#define ID_REAL_USERS(id)
Definition DNA_ID.h:679
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void append(const T &value)
void(*)(bContext *C, Layout *layout, void *arg1) MenuCreateFunc
Button * uiItemL_ex(Layout *layout, StringRef name, int icon, bool highlight, bool redalert)
void button_icon_indicator_number_set(Button *but, const int indicator_number)
void context_path_add_generic(Vector< ContextPathItem > &path, StructRNA &rna_type, void *ptr, const BIFIconID icon_override=ICON_NONE, std::function< void(bContext &)> handle_func=nullptr, MenuCreateFunc menu_func=nullptr, void *menu_arg=nullptr)
void template_breadcrumbs(Layout &layout, Span< ContextPathItem > context_path)
char * RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
int RNA_struct_ui_icon(const StructRNA *type)
int BIFIconID
Definition ED_asset.hh:30
StructRNA * type
Definition RNA_types.hh:63
void alignment_set(LayoutAlign alignment)
void label(StringRef name, int icon)
Layout & row(bool align)
Button * button(StringRef name, int icon, std::function< void(bContext &)> func, std::optional< StringRef > tooltip=std::nullopt)
void menu_fn(StringRefNull name, int icon, MenuCreateFunc func, void *arg)
void emboss_set(EmbossType emboss)