Blender V5.3
rna_wm_undo.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 "BLI_listbase.hh"
10#include "BLI_utildefines.hh"
11
12#include "BKE_undo_system.hh"
14
15#include "RNA_access.hh"
16#include "RNA_define.hh"
17#include "RNA_enum_types.hh"
18#include "RNA_types.hh"
19
20#include "rna_internal.hh"
21
22namespace blender {
23
24#ifdef RNA_RUNTIME
25
26static int rna_UndoStack_steps_length(PointerRNA *ptr)
27{
28 UndoStack *ustack = static_cast<UndoStack *>(ptr->data);
29 return BLI_listbase_count(&ustack->steps);
30}
31
32static PointerRNA rna_UndoStack_steps_get(CollectionPropertyIterator *iter)
33{
34 UndoStep *us = static_cast<UndoStep *>(rna_iterator_listbase_get(iter));
35 return RNA_pointer_create_with_parent(iter->parent, RNA_UndoStep, us);
36}
37
38static void rna_UndoStack_steps_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
39{
40 UndoStack *ustack = static_cast<UndoStack *>(ptr->data);
41 rna_iterator_listbase_begin(iter, ptr, &ustack->steps, nullptr);
42}
43
44static int rna_UndoStack_active_index_get(PointerRNA *ptr)
45{
46 UndoStack *ustack = static_cast<UndoStack *>(ptr->data);
47 return BLI_findindex(&ustack->steps, ustack->step_active);
48}
49
50static PointerRNA rna_UndoStack_active_get(PointerRNA *ptr)
51{
52 UndoStack *ustack = static_cast<UndoStack *>(ptr->data);
53 return RNA_pointer_create_with_parent(*ptr, RNA_UndoStep, ustack->step_active);
54}
55
56static void rna_UndoStep_name_get(PointerRNA *ptr, char *value)
57{
58 UndoStep *us = static_cast<UndoStep *>(ptr->data);
59 BLI_strncpy(value, us->name, sizeof(us->name));
60}
61
62static int rna_UndoStep_name_length(PointerRNA *ptr)
63{
64 UndoStep *us = static_cast<UndoStep *>(ptr->data);
65 return strlen(us->name);
66}
67
68static int rna_UndoStep_type_get(PointerRNA *ptr)
69{
70 const UndoStep *us = static_cast<const UndoStep *>(ptr->data);
71 if (!us->type) {
72 return 0;
73 }
74 int index = 0, type_index = 0;
75 BKE_undosys_type_foreach([&](const UndoType *ut) {
76 if (ut == us->type) {
77 type_index = index;
78 return false;
79 }
80 index++;
81 return true;
82 });
83 return type_index;
84}
85
86static const EnumPropertyItem *rna_UndoStep_type_itemf(bContext * /*C*/,
87 PointerRNA * /*ptr*/,
88 PropertyRNA * /*prop*/,
89 bool *r_free)
90{
91 EnumPropertyItem *item = nullptr;
92 int totitem = 0, index = 0;
93
94 BKE_undosys_type_foreach([&](const UndoType *ut) {
95 EnumPropertyItem tmp = {0, "", 0, "", ""};
96 tmp.value = index++;
97 tmp.identifier = ut->identifier;
98 tmp.name = ut->identifier;
99 RNA_enum_item_add(&item, &totitem, &tmp);
100 return true;
101 });
102
103 RNA_enum_item_end(&item, &totitem);
104 *r_free = true;
105 return item;
106}
107
108static bool rna_UndoStep_is_substep_get(PointerRNA *ptr)
109{
110 UndoStep *us = static_cast<UndoStep *>(ptr->data);
111 return us->skip;
112}
113
114#else /* !RNA_RUNTIME */
115
117{
118 StructRNA *srna;
119 PropertyRNA *prop;
120
121 srna = RNA_def_struct(brna, "UndoStep", nullptr);
122 RNA_def_struct_ui_text(srna, "Undo Step", "A single step in the undo history");
124
125 prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
128 prop, "rna_UndoStep_name_get", "rna_UndoStep_name_length", nullptr);
129 RNA_def_property_ui_text(prop, "Name", "Label of the undo step");
130
131 prop = RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
134 RNA_def_property_enum_funcs(prop, "rna_UndoStep_type_get", nullptr, "rna_UndoStep_type_itemf");
135 RNA_def_property_ui_text(prop, "Type", "Type of the undo step");
136
137 prop = RNA_def_property(srna, "is_substep", PROP_BOOLEAN, PROP_NONE);
139 RNA_def_property_boolean_funcs(prop, "rna_UndoStep_is_substep_get", nullptr);
141 "Is Substep",
142 "If true, this is a sub-step and should not be shown to the user "
143 "for undo/redo selection as it is not accessible on its own");
144
145 srna = RNA_def_struct(brna, "UndoStack", nullptr);
146 RNA_def_struct_ui_text(srna, "Undo Stack", "Read-only access to the undo stack");
147
148 prop = RNA_def_property(srna, "steps", PROP_COLLECTION, PROP_NONE);
149 RNA_def_property_struct_type(prop, "UndoStep");
150 RNA_def_property_ui_text(prop, "Steps", "List of undo steps");
152 "rna_UndoStack_steps_begin",
153 "rna_iterator_listbase_next",
154 "rna_iterator_listbase_end",
155 "rna_UndoStack_steps_get",
156 "rna_UndoStack_steps_length",
157 nullptr,
158 nullptr,
159 nullptr);
160
161 prop = RNA_def_property(srna, "active_index", PROP_INT, PROP_UNSIGNED);
163 RNA_def_property_int_funcs(prop, "rna_UndoStack_active_index_get", nullptr, nullptr);
164 RNA_def_property_ui_text(prop, "Active Index", "Index of currently active undo step");
165
166 prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
168 RNA_def_property_struct_type(prop, "UndoStep");
169 RNA_def_property_pointer_funcs(prop, "rna_UndoStack_active_get", nullptr, nullptr, nullptr);
170 RNA_def_property_ui_text(prop, "Active", "Currently active undo step");
171}
172
173#endif /* RNA_RUNTIME */
174
175} // namespace blender
BMesh Iterators.
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
@ STRUCT_NO_DATABLOCK_IDPROPERTIES
void RNA_def_property_pointer_funcs(PropertyRNA *prop, const char *get, const char *set, const char *type_fn, const char *poll)
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
@ PROP_BOOLEAN
Definition RNA_types.hh:210
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_STRING
Definition RNA_types.hh:213
@ PROP_COLLECTION
Definition RNA_types.hh:216
void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, const char *next, const char *end, const char *get, const char *length, const char *lookupint, const char *lookupstring, const char *assignint)
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
bool BKE_undosys_type_foreach(FunctionRef< bool(const UndoType *ut)> fn)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
const EnumPropertyItem rna_enum_dummy_DEFAULT_items[]
Definition rna_rna.cc:34
void RNA_def_property_enum_funcs(PropertyRNA *prop, const char *get, const char *set, const char *item)
int BLI_listbase_count(const ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:531
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void * rna_iterator_listbase_get(CollectionPropertyIterator *iter)
void RNA_def_struct_flag(StructRNA *srna, StructFlag flag)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
@ PROP_EDITABLE
Definition RNA_types.hh:376
PointerRNA RNA_pointer_create_with_parent(const PointerRNA &parent, StructRNA *type, void *data)
void RNA_def_property_string_funcs(PropertyRNA *prop, const char *get, const char *length, const char *set)
@ PROP_UNSIGNED
Definition RNA_types.hh:306
void rna_iterator_listbase_begin(CollectionPropertyIterator *iter, PointerRNA *ptr, ListBase *lb, IteratorSkipFunc skip)
void RNA_def_undo(BlenderRNA *brna)