code cleanup: use single define for undo string size, was 64 mostly, but 512 in the UI.
[blender-staging.git] / source / blender / editors / sculpt_paint / paint_undo.c
index 345cda63f5abd6bffa16c11d7c3d81d87c8ae5ef..f5b9aa742c6ac516fee7eb62737cc3b916df467d 100644 (file)
@@ -1,8 +1,4 @@
 /*
- * $Id$
- *
- * Undo system for painting and sculpting.
- * 
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -24,9 +20,9 @@
 
 /** \file blender/editors/sculpt_paint/paint_undo.c
  *  \ingroup edsculpt
+ *  \brief Undo system for painting and sculpting.
  */
 
-
 #include <stdlib.h>
 #include <string.h>
 
@@ -38,7 +34,7 @@
 
 #include "DNA_userdef_types.h"
 
-
+#include "BKE_blender.h"
 #include "BKE_context.h"
 #include "BKE_global.h"
 
 
 #include "paint_intern.h"
 
-#define MAXUNDONAME    64
-
 typedef struct UndoElem {
        struct UndoElem *next, *prev;
-       char name[MAXUNDONAME];
+       char name[BKE_UNDO_STR_MAX];
        uintptr_t undosize;
 
        ListBase elems;
@@ -72,13 +66,13 @@ static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
 
 static void undo_restore(bContext *C, UndoStack *UNUSED(stack), UndoElem *uel)
 {
-       if(uel && uel->restore)
+       if (uel && uel->restore)
                uel->restore(C, &uel->elems);
 }
 
 static void undo_elem_free(UndoStack *UNUSED(stack), UndoElem *uel)
 {
-       if(uel && uel->free) {
+       if (uel && uel->free) {
                uel->free(&uel->elems);
                BLI_freelistN(&uel->elems);
        }
@@ -94,32 +88,32 @@ static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestor
         * know how much memory the undo used to remove old undo elements */
 
        /* remove all undos after (also when stack->current==NULL) */
-       while(stack->elems.last != stack->current) {
-               uel= stack->elems.last;
+       while (stack->elems.last != stack->current) {
+               uel = stack->elems.last;
                undo_elem_free(stack, uel);
                BLI_freelinkN(&stack->elems, uel);
        }
        
        /* make new */
-       stack->current= uel= MEM_callocN(sizeof(UndoElem), "undo file");
-       uel->restore= restore;
-       uel->free= free;
+       stack->current = uel = MEM_callocN(sizeof(UndoElem), "undo file");
+       uel->restore = restore;
+       uel->free = free;
        BLI_addtail(&stack->elems, uel);
 
        /* name can be a dynamic string */
        BLI_strncpy(uel->name, name, sizeof(uel->name));
        
        /* limit amount to the maximum amount*/
-       nr= 0;
-       uel= stack->elems.last;
-       while(uel) {
+       nr = 0;
+       uel = stack->elems.last;
+       while (uel) {
                nr++;
-               if(nr==U.undosteps) break;
-               uel= uel->prev;
+               if (nr == U.undosteps) break;
+               uel = uel->prev;
        }
-       if(uel) {
-               while(stack->elems.first!=uel) {
-                       UndoElem *first= stack->elems.first;
+       if (uel) {
+               while (stack->elems.first != uel) {
+                       UndoElem *first = stack->elems.first;
                        undo_elem_free(stack, first);
                        BLI_freelinkN(&stack->elems, first);
                }
@@ -131,21 +125,21 @@ static void undo_stack_push_end(UndoStack *stack)
        UndoElem *uel;
        uintptr_t totmem, maxmem;
 
-       if(U.undomemory != 0) {
+       if (U.undomemory != 0) {
                /* limit to maximum memory (afterwards, we can't know in advance) */
-               totmem= 0;
-               maxmem= ((uintptr_t)U.undomemory)*1024*1024;
-
-               uel= stack->elems.last;
-               while(uel) {
-                       totmem+= uel->undosize;
-                       if(totmem>maxmem) break;
-                       uel= uel->prev;
+               totmem = 0;
+               maxmem = ((uintptr_t)U.undomemory) * 1024 * 1024;
+
+               uel = stack->elems.last;
+               while (uel) {
+                       totmem += uel->undosize;
+                       if (totmem > maxmem) break;
+                       uel = uel->prev;
                }
 
-               if(uel) {
-                       while(stack->elems.first!=uel) {
-                               UndoElem *first= stack->elems.first;
+               if (uel) {
+                       while (stack->elems.first != uel) {
+                               UndoElem *first = stack->elems.first;
                                undo_elem_free(stack, first);
                                BLI_freelinkN(&stack->elems, first);
                        }
@@ -157,25 +151,29 @@ static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *
 {
        UndoElem *undo;
 
-       if(step==1) {
-               if(stack->current==NULL);
+       if (step == 1) {
+               if (stack->current == NULL) ;
                else {
-                       if(!name || strcmp(stack->current->name, name) == 0) {
-                               if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name);
+                       if (!name || strcmp(stack->current->name, name) == 0) {
+                               if (G.debug & G_DEBUG_WM) {
+                                       printf("%s: undo '%s'\n", __func__, stack->current->name);
+                               }
                                undo_restore(C, stack, stack->current);
-                               stack->current= stack->current->prev;
+                               stack->current = stack->current->prev;
                                return 1;
                        }
                }
        }
-       else if(step==-1) {
-               if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL);
+       else if (step == -1) {
+               if ((stack->current != NULL && stack->current->next == NULL) || stack->elems.first == NULL) ;
                else {
-                       if(!name || strcmp(stack->current->name, name) == 0) {
-                               undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first;
+                       if (!name || strcmp(stack->current->name, name) == 0) {
+                               undo = (stack->current && stack->current->next) ? stack->current->next : stack->elems.first;
                                undo_restore(C, stack, undo);
-                               stack->current= undo;
-                               if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
+                               stack->current = undo;
+                               if (G.debug & G_DEBUG_WM) {
+                                       printf("%s: redo %s\n", __func__, undo->name);
+                               }
                                return 1;
                        }
                }
@@ -188,31 +186,31 @@ static void undo_stack_free(UndoStack *stack)
 {
        UndoElem *uel;
        
-       for(uel=stack->elems.first; uel; uel=uel->next)
+       for (uel = stack->elems.first; uel; uel = uel->next)
                undo_elem_free(stack, uel);
 
        BLI_freelistN(&stack->elems);
-       stack->current= NULL;
+       stack->current = NULL;
 }
 
 /* Exported Functions */
 
 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
 {
-       if(type == UNDO_PAINT_IMAGE)
+       if (type == UNDO_PAINT_IMAGE)
                undo_stack_push_begin(&ImageUndoStack, name, restore, free);
-       else if(type == UNDO_PAINT_MESH)
+       else if (type == UNDO_PAINT_MESH)
                undo_stack_push_begin(&MeshUndoStack, name, restore, free);
 }
 
 ListBase *undo_paint_push_get_list(int type)
 {
-       if(type == UNDO_PAINT_IMAGE) {
-               if(ImageUndoStack.current)
+       if (type == UNDO_PAINT_IMAGE) {
+               if (ImageUndoStack.current)
                        return &ImageUndoStack.current->elems;
        }
-       else if(type == UNDO_PAINT_MESH) {
-               if(MeshUndoStack.current)
+       else if (type == UNDO_PAINT_MESH) {
+               if (MeshUndoStack.current)
                        return &MeshUndoStack.current->elems;
        }
        
@@ -221,25 +219,25 @@ ListBase *undo_paint_push_get_list(int type)
 
 void undo_paint_push_count_alloc(int type, int size)
 {
-       if(type == UNDO_PAINT_IMAGE)
+       if (type == UNDO_PAINT_IMAGE)
                ImageUndoStack.current->undosize += size;
-       else if(type == UNDO_PAINT_MESH)
+       else if (type == UNDO_PAINT_MESH)
                MeshUndoStack.current->undosize += size;
 }
 
 void undo_paint_push_end(int type)
 {
-       if(type == UNDO_PAINT_IMAGE)
+       if (type == UNDO_PAINT_IMAGE)
                undo_stack_push_end(&ImageUndoStack);
-       else if(type == UNDO_PAINT_MESH)
+       else if (type == UNDO_PAINT_MESH)
                undo_stack_push_end(&MeshUndoStack);
 }
 
 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
 {
-       if(type == UNDO_PAINT_IMAGE)
+       if (type == UNDO_PAINT_IMAGE)
                return undo_stack_step(C, &ImageUndoStack, step, name);
-       else if(type == UNDO_PAINT_MESH)
+       else if (type == UNDO_PAINT_MESH)
                return undo_stack_step(C, &MeshUndoStack, step, name);
        
        return 0;
@@ -249,16 +247,16 @@ int ED_undo_paint_valid(int type, const char *name)
 {
        UndoStack *stack;
        
-       if(type == UNDO_PAINT_IMAGE)
-               stack= &ImageUndoStack;
-       else if(type == UNDO_PAINT_MESH)
-               stack= &MeshUndoStack;
+       if (type == UNDO_PAINT_IMAGE)
+               stack = &ImageUndoStack;
+       else if (type == UNDO_PAINT_MESH)
+               stack = &MeshUndoStack;
        else 
                return 0;
        
-       if(stack->current==NULL);
+       if (stack->current == NULL) ;
        else {
-               if(name && strcmp(stack->current->name, name) == 0)
+               if (name && strcmp(stack->current->name, name) == 0)
                        return 1;
                else
                        return stack->elems.first != stack->elems.last;
@@ -271,4 +269,3 @@ void ED_undo_paint_free(void)
        undo_stack_free(&ImageUndoStack);
        undo_stack_free(&MeshUndoStack);
 }
-