Blender V4.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
11#include <cstring>
12
13#include "BLI_listbase.h"
14
15#include "DNA_listBase.h"
16
17#include "MEM_guardedalloc.h"
18
19#include "interface_intern.hh"
20
21/* -------------------------------------------------------------------- */
30
35
36static const char *ui_textedit_undo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
37{
38 /* Don't undo if no data has been pushed yet. */
39 if (stack->current == nullptr) {
40 return nullptr;
41 }
42
43 /* Travel backwards in the stack and copy information to the caller. */
44 if (stack->current->prev != nullptr) {
45 stack->current = stack->current->prev;
46
47 *r_cursor_index = stack->current->cursor_index;
48 return stack->current->text;
49 }
50 return nullptr;
51}
52
53static const char *ui_textedit_redo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
54{
55 /* Don't redo if no data has been pushed yet. */
56 if (stack->current == nullptr) {
57 return nullptr;
58 }
59
60 /* Only redo if new data has not been entered since the last undo. */
61 if (stack->current->next) {
62 stack->current = stack->current->next;
63
64 *r_cursor_index = stack->current->cursor_index;
65 return stack->current->text;
66 }
67 return nullptr;
68}
69
70const char *ui_textedit_undo(uiUndoStack_Text *stack, int direction, int *r_cursor_index)
71{
72 BLI_assert(ELEM(direction, -1, 1));
73 if (direction < 0) {
74 return ui_textedit_undo_impl(stack, r_cursor_index);
75 }
76 return ui_textedit_redo_impl(stack, r_cursor_index);
77}
78
79void ui_textedit_undo_push(uiUndoStack_Text *stack, const char *text, int cursor_index)
80{
81 /* Clear all redo actions from the current state. */
82 if (stack->current != nullptr) {
83 while (stack->current->next) {
85 BLI_remlink(&stack->states, state);
87 }
88 }
89
90 /* Create the new state. */
91 const int text_size = strlen(text) + 1;
92 stack->current = static_cast<uiUndoStack_Text_State *>(
93 MEM_mallocN(sizeof(uiUndoStack_Text_State) + text_size, __func__));
94 stack->current->cursor_index = cursor_index;
95 memcpy(stack->current->text, text, text_size);
96 BLI_addtail(&stack->states, stack->current);
97}
98
100{
101 uiUndoStack_Text *stack = MEM_cnew<uiUndoStack_Text>(__func__);
102 stack->current = nullptr;
103 BLI_listbase_clear(&stack->states);
104
105 return stack;
106}
107
109{
110 BLI_freelistN(&stack->states);
111 MEM_freeN(stack);
112}
113
#define BLI_assert(a)
Definition BLI_assert.h:50
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:496
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:130
#define ELEM(...)
These structs are the foundation for all linked lists in the library system.
Read Guarded memory(de)allocation.
static const char * ui_textedit_redo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
const char * ui_textedit_undo(uiUndoStack_Text *stack, int direction, int *r_cursor_index)
void ui_textedit_undo_stack_destroy(uiUndoStack_Text *stack)
uiUndoStack_Text * ui_textedit_undo_stack_create()
static const char * ui_textedit_undo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
void ui_textedit_undo_push(uiUndoStack_Text *stack, const char *text, int cursor_index)
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
static ulong state[N]
uiUndoStack_Text_State * next
uiUndoStack_Text_State * prev
uiUndoStack_Text_State * current