Blender V5.3
interface_undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12
13#include "BLI_listbase.hh"
14
15#include "DNA_listBase.h"
16
17#include "MEM_guardedalloc.h"
18
19#include "interface_intern.hh"
20
21namespace blender::ui {
22
23/* -------------------------------------------------------------------- */
26
32
34 ListBaseT<UndoStack_Text_State> states;
36};
37
38static const char *textedit_undo_impl(UndoStack_Text *stack, int *r_cursor_index)
39{
40 /* Don't undo if no data has been pushed yet. */
41 if (stack->current == nullptr) {
42 return nullptr;
43 }
44
45 /* Travel backwards in the stack and copy information to the caller. */
46 if (stack->current->prev != nullptr) {
47 stack->current = stack->current->prev;
48
49 *r_cursor_index = stack->current->cursor_index;
50 return stack->current->text;
51 }
52 return nullptr;
53}
54
55static const char *textedit_redo_impl(UndoStack_Text *stack, int *r_cursor_index)
56{
57 /* Don't redo if no data has been pushed yet. */
58 if (stack->current == nullptr) {
59 return nullptr;
60 }
61
62 /* Only redo if new data has not been entered since the last undo. */
63 if (stack->current->next) {
64 stack->current = stack->current->next;
65
66 *r_cursor_index = stack->current->cursor_index;
67 return stack->current->text;
68 }
69 return nullptr;
70}
71
72const char *textedit_undo(UndoStack_Text *stack, int direction, int *r_cursor_index)
73{
74 BLI_assert(ELEM(direction, -1, 1));
75 if (direction < 0) {
76 return textedit_undo_impl(stack, r_cursor_index);
77 }
78 return textedit_redo_impl(stack, r_cursor_index);
79}
80
81void textedit_undo_push(UndoStack_Text *stack, const char *text, int cursor_index)
82{
83 /* Clear all redo actions from the current state. */
84 if (stack->current != nullptr) {
85 while (stack->current->next) {
87 BLI_remlink(&stack->states, state);
88 MEM_delete(state);
89 }
90 }
91
92 /* Create the new state. */
93 const int text_size = strlen(text) + 1;
94 stack->current = static_cast<UndoStack_Text_State *>(
95 MEM_new_uninitialized(sizeof(UndoStack_Text_State) + text_size, __func__));
96 stack->current->cursor_index = cursor_index;
97 memcpy(stack->current->text, text, text_size);
98 BLI_addtail(&stack->states, stack->current);
99}
100
102{
104 stack->current = nullptr;
105 stack->states.clear_no_delete();
106
107 return stack;
108}
109
111{
112 stack->states.free_no_destruct();
113 MEM_delete(stack);
114}
115
117
118} // namespace blender::ui
These structs are the foundation for all linked lists in the library system.
Read Blender memory allocation and freeing.
#define ELEM(...)
const int state
UndoStack_Text * textedit_undo_stack_create()
static const char * textedit_redo_impl(UndoStack_Text *stack, int *r_cursor_index)
const char * textedit_undo(UndoStack_Text *stack, int direction, int *r_cursor_index)
void textedit_undo_stack_destroy(UndoStack_Text *stack)
static const char * textedit_undo_impl(UndoStack_Text *stack, int *r_cursor_index)
void textedit_undo_push(UndoStack_Text *stack, const char *text, int cursor_index)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
ListBaseT< UndoStack_Text_State > states
UndoStack_Text_State * current