Blender  V2.93
interface_undo.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
26 #include <string.h>
27 
28 #include "BLI_listbase.h"
29 
30 #include "DNA_listBase.h"
31 
32 #include "MEM_guardedalloc.h"
33 
34 #include "interface_intern.h"
35 
36 /* -------------------------------------------------------------------- */
40 typedef struct uiUndoStack_Text_State {
43  char text[0];
45 
46 typedef struct uiUndoStack_Text {
50 
51 static const char *ui_textedit_undo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
52 {
53  /* Don't undo if no data has been pushed yet. */
54  if (stack->current == NULL) {
55  return NULL;
56  }
57 
58  /* Travel backwards in the stack and copy information to the caller. */
59  if (stack->current->prev != NULL) {
60  stack->current = stack->current->prev;
61 
62  *r_cursor_index = stack->current->cursor_index;
63  return stack->current->text;
64  }
65  return NULL;
66 }
67 
68 static const char *ui_textedit_redo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
69 {
70  /* Don't redo if no data has been pushed yet. */
71  if (stack->current == NULL) {
72  return NULL;
73  }
74 
75  /* Only redo if new data has not been entered since the last undo. */
76  if (stack->current->next) {
77  stack->current = stack->current->next;
78 
79  *r_cursor_index = stack->current->cursor_index;
80  return stack->current->text;
81  }
82  return NULL;
83 }
84 
85 const char *ui_textedit_undo(uiUndoStack_Text *stack, int direction, int *r_cursor_index)
86 {
87  BLI_assert(ELEM(direction, -1, 1));
88  if (direction < 0) {
89  return ui_textedit_undo_impl(stack, r_cursor_index);
90  }
91  return ui_textedit_redo_impl(stack, r_cursor_index);
92 }
93 
99 void ui_textedit_undo_push(uiUndoStack_Text *stack, const char *text, int cursor_index)
100 {
101  /* Clear all redo actions from the current state. */
102  if (stack->current != NULL) {
103  while (stack->current->next) {
105  BLI_remlink(&stack->states, state);
106  MEM_freeN(state);
107  }
108  }
109 
110  /* Create the new state */
111  const int text_size = strlen(text) + 1;
112  stack->current = MEM_mallocN(sizeof(uiUndoStack_Text_State) + text_size, __func__);
113  stack->current->cursor_index = cursor_index;
114  memcpy(stack->current->text, text, text_size);
115  BLI_addtail(&stack->states, stack->current);
116 }
123 {
124  uiUndoStack_Text *stack = MEM_mallocN(sizeof(uiUndoStack_Text), __func__);
125  stack->current = NULL;
126  BLI_listbase_clear(&stack->states);
127 
128  return stack;
129 }
130 
132 {
133  BLI_freelistN(&stack->states);
134  MEM_freeN(stack);
135 }
136 
#define BLI_assert(a)
Definition: BLI_assert.h:58
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
#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)
struct uiUndoStack_Text uiUndoStack_Text
static const char * ui_textedit_undo_impl(uiUndoStack_Text *stack, int *r_cursor_index)
const char * ui_textedit_undo(uiUndoStack_Text *stack, int direction, int *r_cursor_index)
struct uiUndoStack_Text_State uiUndoStack_Text_State
uiUndoStack_Text * ui_textedit_undo_stack_create(void)
void ui_textedit_undo_stack_destroy(uiUndoStack_Text *stack)
void ui_textedit_undo_push(uiUndoStack_Text *stack, const char *text, int cursor_index)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static ulong state[N]
struct uiUndoStack_Text_State * next
struct uiUndoStack_Text_State * prev
uiUndoStack_Text_State * current