replace strncpy with BLI_strncpy, in some cases strncpy was being misused since it...
[blender-staging.git] / source / blender / editors / sculpt_paint / paint_undo.c
1 /*
2  * $Id$
3  *
4  * Undo system for painting and sculpting.
5  * 
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/sculpt_paint/paint_undo.c
26  *  \ingroup edsculpt
27  */
28
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_listbase.h"
36 #include "BLI_utildefines.h"
37 #include "BLI_string.h"
38
39 #include "DNA_userdef_types.h"
40
41
42 #include "BKE_context.h"
43 #include "BKE_global.h"
44
45 #include "ED_sculpt.h"
46
47 #include "paint_intern.h"
48
49 #define MAXUNDONAME     64
50
51 typedef struct UndoElem {
52         struct UndoElem *next, *prev;
53         char name[MAXUNDONAME];
54         uintptr_t undosize;
55
56         ListBase elems;
57
58         UndoRestoreCb restore;
59         UndoFreeCb free;
60 } UndoElem;
61
62 typedef struct UndoStack {
63         int type;
64         ListBase elems;
65         UndoElem *current;
66 } UndoStack;
67
68 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL};
69 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
70
71 /* Generic */
72
73 static void undo_restore(bContext *C, UndoStack *UNUSED(stack), UndoElem *uel)
74 {
75         if(uel && uel->restore)
76                 uel->restore(C, &uel->elems);
77 }
78
79 static void undo_elem_free(UndoStack *UNUSED(stack), UndoElem *uel)
80 {
81         if(uel && uel->free) {
82                 uel->free(&uel->elems);
83                 BLI_freelistN(&uel->elems);
84         }
85 }
86
87 static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free)
88 {
89         UndoElem *uel;
90         int nr;
91         
92         /* Undo push is split up in begin and end, the reason is that as painting
93          * happens more tiles/nodes are added to the list, and at the very end we
94          * know how much memory the undo used to remove old undo elements */
95
96         /* remove all undos after (also when stack->current==NULL) */
97         while(stack->elems.last != stack->current) {
98                 uel= stack->elems.last;
99                 undo_elem_free(stack, uel);
100                 BLI_freelinkN(&stack->elems, uel);
101         }
102         
103         /* make new */
104         stack->current= uel= MEM_callocN(sizeof(UndoElem), "undo file");
105         uel->restore= restore;
106         uel->free= free;
107         BLI_addtail(&stack->elems, uel);
108
109         /* name can be a dynamic string */
110         BLI_strncpy(uel->name, name, sizeof(uel->name));
111         
112         /* limit amount to the maximum amount*/
113         nr= 0;
114         uel= stack->elems.last;
115         while(uel) {
116                 nr++;
117                 if(nr==U.undosteps) break;
118                 uel= uel->prev;
119         }
120         if(uel) {
121                 while(stack->elems.first!=uel) {
122                         UndoElem *first= stack->elems.first;
123                         undo_elem_free(stack, first);
124                         BLI_freelinkN(&stack->elems, first);
125                 }
126         }
127 }
128
129 static void undo_stack_push_end(UndoStack *stack)
130 {
131         UndoElem *uel;
132         uintptr_t totmem, maxmem;
133
134         if(U.undomemory != 0) {
135                 /* limit to maximum memory (afterwards, we can't know in advance) */
136                 totmem= 0;
137                 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
138
139                 uel= stack->elems.last;
140                 while(uel) {
141                         totmem+= uel->undosize;
142                         if(totmem>maxmem) break;
143                         uel= uel->prev;
144                 }
145
146                 if(uel) {
147                         while(stack->elems.first!=uel) {
148                                 UndoElem *first= stack->elems.first;
149                                 undo_elem_free(stack, first);
150                                 BLI_freelinkN(&stack->elems, first);
151                         }
152                 }
153         }
154 }
155
156 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
157 {
158         UndoElem *undo;
159
160         if(step==1) {
161                 if(stack->current==NULL);
162                 else {
163                         if(!name || strcmp(stack->current->name, name) == 0) {
164                                 if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name);
165                                 undo_restore(C, stack, stack->current);
166                                 stack->current= stack->current->prev;
167                                 return 1;
168                         }
169                 }
170         }
171         else if(step==-1) {
172                 if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL);
173                 else {
174                         if(!name || strcmp(stack->current->name, name) == 0) {
175                                 undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first;
176                                 undo_restore(C, stack, undo);
177                                 stack->current= undo;
178                                 if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
179                                 return 1;
180                         }
181                 }
182         }
183
184         return 0;
185 }
186
187 static void undo_stack_free(UndoStack *stack)
188 {
189         UndoElem *uel;
190         
191         for(uel=stack->elems.first; uel; uel=uel->next)
192                 undo_elem_free(stack, uel);
193
194         BLI_freelistN(&stack->elems);
195         stack->current= NULL;
196 }
197
198 /* Exported Functions */
199
200 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
201 {
202         if(type == UNDO_PAINT_IMAGE)
203                 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
204         else if(type == UNDO_PAINT_MESH)
205                 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
206 }
207
208 ListBase *undo_paint_push_get_list(int type)
209 {
210         if(type == UNDO_PAINT_IMAGE) {
211                 if(ImageUndoStack.current)
212                         return &ImageUndoStack.current->elems;
213         }
214         else if(type == UNDO_PAINT_MESH) {
215                 if(MeshUndoStack.current)
216                         return &MeshUndoStack.current->elems;
217         }
218         
219         return NULL;
220 }
221
222 void undo_paint_push_count_alloc(int type, int size)
223 {
224         if(type == UNDO_PAINT_IMAGE)
225                 ImageUndoStack.current->undosize += size;
226         else if(type == UNDO_PAINT_MESH)
227                 MeshUndoStack.current->undosize += size;
228 }
229
230 void undo_paint_push_end(int type)
231 {
232         if(type == UNDO_PAINT_IMAGE)
233                 undo_stack_push_end(&ImageUndoStack);
234         else if(type == UNDO_PAINT_MESH)
235                 undo_stack_push_end(&MeshUndoStack);
236 }
237
238 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
239 {
240         if(type == UNDO_PAINT_IMAGE)
241                 return undo_stack_step(C, &ImageUndoStack, step, name);
242         else if(type == UNDO_PAINT_MESH)
243                 return undo_stack_step(C, &MeshUndoStack, step, name);
244         
245         return 0;
246 }
247
248 int ED_undo_paint_valid(int type, const char *name)
249 {
250         UndoStack *stack;
251         
252         if(type == UNDO_PAINT_IMAGE)
253                 stack= &ImageUndoStack;
254         else if(type == UNDO_PAINT_MESH)
255                 stack= &MeshUndoStack;
256         else 
257                 return 0;
258         
259         if(stack->current==NULL);
260         else {
261                 if(name && strcmp(stack->current->name, name) == 0)
262                         return 1;
263                 else
264                         return stack->elems.first != stack->elems.last;
265         }
266         return 0;
267 }
268
269 void ED_undo_paint_free(void)
270 {
271         undo_stack_free(&ImageUndoStack);
272         undo_stack_free(&MeshUndoStack);
273 }
274