]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/undo.cpp
Initial python support
[xonotic/netradiant.git] / radiant / undo.cpp
index d1465af12185b977d1b57e08cddb9cb05c075f10..dd92e8ab3c3e829c3b87a8af5bbf4cc193bcff40 100644 (file)
-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-*/\r
-\r
-\r
-/*\r
-\r
-  QERadiant Undo/Redo\r
-\r
-\r
-basic setup:\r
-\r
-<-g_undolist---------g_lastundo> <---map data---> <-g_lastredo---------g_redolist->\r
-\r
-\r
-  undo/redo on the world_entity is special, only the epair changes are remembered\r
-  and the world entity never gets deleted.\r
-\r
-  FIXME: maybe reset the Undo system at map load\r
-                maybe also reset the entityId at map load\r
-*/\r
-\r
-#include "stdafx.h"\r
-\r
-typedef struct undo_s\r
-{\r
-       double time;                            //time operation was performed\r
-       int id;                                         //every undo has an unique id\r
-       int done;                                       //true when undo is build\r
-       char *operation;                        //name of the operation\r
-       brush_t brushlist;                      //deleted brushes\r
-       entity_t entitylist;            //deleted entities\r
-       struct undo_s *prev, *next;     //next and prev undo in list\r
-} undo_t;\r
-\r
-undo_t *g_undolist;                                            //first undo in the list\r
-undo_t *g_lastundo;                                            //last undo in the list\r
-undo_t *g_redolist;                                            //first redo in the list\r
-undo_t *g_lastredo;                                            //last undo in list\r
-int g_undoMaxSize = 64;                                        //maximum number of undos\r
-int g_undoSize = 0;                                            //number of undos in the list\r
-int g_undoMaxMemorySize = 2*1024*1024; //maximum undo memory (default 2 MB)\r
-int g_undoMemorySize = 0;                              //memory size of undo buffer\r
-int g_undoId = 1;                                              //current undo ID (zero is invalid id)\r
-int g_redoId = 1;                                              //current redo ID (zero is invalid id)\r
-\r
-/*\r
-=============\r
-Undo_MemorySize\r
-=============\r
-*/\r
-int Undo_MemorySize(void)\r
-{\r
-       return g_undoMemorySize;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_ClearRedo\r
-=============\r
-*/\r
-void Undo_ClearRedo(void)\r
-{\r
-       undo_t *redo, *nextredo;\r
-       brush_t *pBrush, *pNextBrush;\r
-       entity_t *pEntity, *pNextEntity;\r
-\r
-       for (redo = g_redolist; redo; redo = nextredo)\r
-       {\r
-               nextredo = redo->next;\r
-               for (pBrush = redo->brushlist.next ; pBrush != NULL && pBrush != &redo->brushlist ; pBrush = pNextBrush)\r
-               {\r
-                       pNextBrush = pBrush->next;\r
-                       Brush_Free(pBrush);\r
-               }\r
-               for (pEntity = redo->entitylist.next; pEntity != NULL && pEntity != &redo->entitylist; pEntity = pNextEntity)\r
-               {\r
-                       pNextEntity = pEntity->next;\r
-                       Entity_Free(pEntity);\r
-               }\r
-               free(redo);\r
-       }\r
-       g_redolist = NULL;\r
-       g_lastredo = NULL;\r
-       g_redoId = 1;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_Clear\r
-\r
-  Clears the undo buffer.\r
-=============\r
-*/\r
-void Undo_Clear(void)\r
-{\r
-       undo_t *undo, *nextundo;\r
-       brush_t *pBrush, *pNextBrush;\r
-       entity_t *pEntity, *pNextEntity;\r
-\r
-       Undo_ClearRedo();\r
-       for (undo = g_undolist; undo; undo = nextundo)\r
-       {\r
-               nextundo = undo->next;\r
-               for (pBrush = undo->brushlist.next ; pBrush != NULL && pBrush != &undo->brushlist ; pBrush = pNextBrush)\r
-               {\r
-                       pNextBrush = pBrush->next;\r
-                       g_undoMemorySize -= Brush_MemorySize(pBrush);\r
-                       Brush_Free(pBrush);\r
-               }\r
-               for (pEntity = undo->entitylist.next; pEntity != NULL && pEntity != &undo->entitylist; pEntity = pNextEntity)\r
-               {\r
-                       pNextEntity = pEntity->next;\r
-                       g_undoMemorySize -= Entity_MemorySize(pEntity);\r
-                       Entity_Free(pEntity);\r
-               }\r
-               g_undoMemorySize -= sizeof(undo_t);\r
-               free(undo);\r
-       }\r
-       g_undolist = NULL;\r
-       g_lastundo = NULL;\r
-       g_undoSize = 0;\r
-       g_undoMemorySize = 0;\r
-       g_undoId = 1;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_SetMaxSize\r
-=============\r
-*/\r
-void Undo_SetMaxSize(int size)\r
-{\r
-       Undo_Clear();\r
-       if (size < 1) g_undoMaxSize = 1;\r
-       else g_undoMaxSize = size;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_GetMaxSize\r
-=============\r
-*/\r
-int Undo_GetMaxSize(void)\r
-{\r
-       return g_undoMaxSize;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_SetMaxMemorySize\r
-=============\r
-*/\r
-void Undo_SetMaxMemorySize(int size)\r
-{\r
-       Undo_Clear();\r
-       if (size < 1024) g_undoMaxMemorySize = 1024;\r
-       else g_undoMaxMemorySize = size;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_GetMaxMemorySize\r
-=============\r
-*/\r
-int Undo_GetMaxMemorySize(void)\r
-{\r
-       return g_undoMaxMemorySize;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_FreeFirstUndo\r
-=============\r
-*/\r
-void Undo_FreeFirstUndo(void)\r
-{\r
-       undo_t *undo;\r
-       brush_t *pBrush, *pNextBrush;\r
-       entity_t *pEntity, *pNextEntity;\r
-\r
-       //remove the oldest undo from the undo buffer\r
-       undo = g_undolist;\r
-       g_undolist = g_undolist->next;\r
-       g_undolist->prev = NULL;\r
-       //\r
-       for (pBrush = undo->brushlist.next ; pBrush != NULL && pBrush != &undo->brushlist ; pBrush = pNextBrush)\r
-       {\r
-               pNextBrush = pBrush->next;\r
-               g_undoMemorySize -= Brush_MemorySize(pBrush);\r
-               Brush_Free(pBrush);\r
-       }\r
-       for (pEntity = undo->entitylist.next; pEntity != NULL && pEntity != &undo->entitylist; pEntity = pNextEntity)\r
-       {\r
-               pNextEntity = pEntity->next;\r
-               g_undoMemorySize -= Entity_MemorySize(pEntity);\r
-               Entity_Free(pEntity);\r
-       }\r
-       g_undoMemorySize -= sizeof(undo_t);\r
-       free(undo);\r
-       g_undoSize--;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_GeneralStart\r
-=============\r
-*/\r
-void Undo_GeneralStart(char *operation)\r
-{\r
-       undo_t *undo;\r
-       brush_t *pBrush;\r
-       entity_t *pEntity;\r
-\r
-\r
-       if (g_lastundo)\r
-       {\r
-               if (!g_lastundo->done)\r
-               {\r
-                       Sys_Printf("Undo_Start: WARNING last undo not finished.\n");\r
-               }\r
-       }\r
-\r
-       undo = (undo_t *) malloc(sizeof(undo_t));\r
-       if (!undo) \r
-    return;\r
-       memset(undo, 0, sizeof(undo_t));\r
-       undo->brushlist.next = &undo->brushlist;\r
-       undo->brushlist.prev = &undo->brushlist;\r
-       undo->entitylist.next = &undo->entitylist;\r
-       undo->entitylist.prev = &undo->entitylist;\r
-       if (g_lastundo)\r
-    g_lastundo->next = undo;\r
-       else\r
-    g_undolist = undo;\r
-       undo->prev = g_lastundo;\r
-       undo->next = NULL;\r
-       g_lastundo = undo;\r
-       \r
-       undo->time = Sys_DoubleTime();\r
-       //\r
-       if (g_undoId > g_undoMaxSize * 2) g_undoId = 1;\r
-       if (g_undoId <= 0) g_undoId = 1;\r
-       undo->id = g_undoId++;\r
-       undo->done = false;\r
-       undo->operation = operation;\r
-       //reset the undo IDs of all brushes using the new ID\r
-       for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush = pBrush->next)\r
-       {\r
-               if (pBrush->undoId == undo->id)\r
-               {\r
-                       pBrush->undoId = 0;\r
-               }\r
-       }\r
-       for (pBrush = selected_brushes.next; pBrush != NULL && pBrush != &selected_brushes; pBrush = pBrush->next)\r
-       {\r
-               if (pBrush->undoId == undo->id)\r
-               {\r
-                       pBrush->undoId = 0;\r
-               }\r
-       }\r
-       //reset the undo IDs of all entities using thew new ID\r
-       for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pEntity->next)\r
-       {\r
-               if (pEntity->undoId == undo->id)\r
-               {\r
-                       pEntity->undoId = 0;\r
-               }\r
-       }\r
-       g_undoMemorySize += sizeof(undo_t);\r
-       g_undoSize++;\r
-       //undo buffer is bound to a max\r
-       if (g_undoSize > g_undoMaxSize)\r
-       {\r
-               Undo_FreeFirstUndo();\r
-       }\r
-}\r
-\r
-/*\r
-=============\r
-Undo_BrushInUndo\r
-=============\r
-*/\r
-int Undo_BrushInUndo(undo_t *undo, brush_t *brush)\r
-{\r
-/*     brush_t *b;\r
-\r
-       for (b = undo->brushlist.next; b != &undo->brushlist; b = b->next)\r
-       {\r
-    // Arnout: NOTE - can't do a pointer compare as the brushes get cloned into the undo brushlist, and not just referenced from it\r
-    // For entities we have a unique ID, for brushes we have numberID - but brush full clone increases that anyway so it's useless right now.\r
-               if (b == brush) return true;\r
-       }*/\r
-  // Arnout: function is pointless right now, see above explanation\r
-       return false;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_EntityInUndo\r
-=============\r
-*/\r
-int Undo_EntityInUndo(undo_t *undo, entity_t *ent)\r
-{\r
-       entity_t *e;\r
-\r
-       for (e = undo->entitylist.next; e != &undo->entitylist; e = e->next)\r
-       {\r
-    // Arnout: NOTE - can't do a pointer compare as the entities get cloned into the undo entitylist, and not just referenced from it\r
-               //if (e == ent) return true;\r
-    if( e->entityId == ent->entityId ) return true;\r
-       }\r
-       return false;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_Start\r
-=============\r
-*/\r
-void Undo_Start(char *operation)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_Start: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-       \r
-       Undo_ClearRedo();\r
-       Undo_GeneralStart(operation);\r
-}\r
-\r
-/*\r
-=============\r
-Undo_AddBrush\r
-=============\r
-*/\r
-void Undo_AddBrush(brush_t *pBrush)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_AddBrush: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-       \r
-       if (!g_lastundo)\r
-       {\r
-               Sys_Printf("Undo_AddBrushList: no last undo.\n");\r
-               return;\r
-       }\r
-       if (g_lastundo->entitylist.next != &g_lastundo->entitylist)\r
-       {\r
-               Sys_Printf("Undo_AddBrushList: WARNING adding brushes after entity.\n");\r
-       }\r
-       //if the brush is already in the undo\r
-       if (Undo_BrushInUndo(g_lastundo, pBrush))\r
-               return;\r
-       //clone the brush\r
-       brush_t* pClone = Brush_FullClone(pBrush);\r
-       //save the ID of the owner entity\r
-       pClone->ownerId = pBrush->owner->entityId;\r
-       //save the old undo ID for previous undos\r
-       pClone->undoId = pBrush->undoId;\r
-       Brush_AddToList (pClone, &g_lastundo->brushlist);\r
-       //\r
-       g_undoMemorySize += Brush_MemorySize(pClone);\r
-}\r
-\r
-/*\r
-=============\r
-Undo_AddBrushList\r
-TTimo: some brushes are just there for UI, and the information is somewhere else\r
-for patches it's in the patchMesh_t structure, so when we clone the brush we get that information (brush_t::pPatch)\r
-but: models are stored in pBrush->owner->md3Class, and owner epairs and origin parameters are important\r
-  so, we detect models and push the entity in the undo session (as well as it's BBox brush)\r
-       same for other items like weapons and ammo etc.\r
-=============\r
-*/\r
-void Undo_AddBrushList(brush_t *brushlist)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_AddBrushList: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-       \r
-       brush_t *pBrush;\r
-\r
-       if (!g_lastundo)\r
-       {\r
-               Sys_Printf("Undo_AddBrushList: no last undo.\n");\r
-               return;\r
-       }\r
-       if (g_lastundo->entitylist.next != &g_lastundo->entitylist)\r
-       {\r
-               Sys_Printf("Undo_AddBrushList: WARNING adding brushes after entity.\n");\r
-       }\r
-       //copy the brushes to the undo\r
-       for (pBrush = brushlist->next ; pBrush != NULL && pBrush != brushlist; pBrush=pBrush->next)\r
-       {\r
-               //if the brush is already in the undo\r
-               //++timo FIXME: when does this happen?\r
-               if (Undo_BrushInUndo(g_lastundo, pBrush))\r
-                       continue;\r
-               // do we need to store this brush's entity in the undo?\r
-               // if it's a fixed size entity, the brush that reprents it is not really relevant, it's used for selecting and moving around\r
-               // what we want to store for undo is the owner entity, epairs and origin/angle stuff\r
-               //++timo FIXME: if the entity is not fixed size I don't know, so I don't do it yet\r
-               if (pBrush->owner->eclass->fixedsize == 1)\r
-                       Undo_AddEntity( pBrush->owner );\r
-               // clone the brush\r
-               brush_t* pClone = Brush_FullClone(pBrush);\r
-               // save the ID of the owner entity\r
-               pClone->ownerId = pBrush->owner->entityId;\r
-               // save the old undo ID from previous undos\r
-               pClone->undoId = pBrush->undoId;\r
-               Brush_AddToList (pClone, &g_lastundo->brushlist);\r
-               // track memory size used by undo\r
-               g_undoMemorySize += Brush_MemorySize(pClone);\r
-       }\r
-}\r
-\r
-/*\r
-=============\r
-Undo_EndBrush\r
-=============\r
-*/\r
-void Undo_EndBrush(brush_t *pBrush)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_EndBrush: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-       \r
-\r
-       if (!g_lastundo)\r
-       {\r
-               //Sys_Printf("Undo_End: no last undo.\n");\r
-               return;\r
-       }\r
-       if (g_lastundo->done)\r
-       {\r
-               //Sys_Printf("Undo_End: last undo already finished.\n");\r
-               return;\r
-       }\r
-       pBrush->undoId = g_lastundo->id;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_EndBrushList\r
-=============\r
-*/\r
-void Undo_EndBrushList(brush_t *brushlist)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_EndBrushList: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-       \r
-\r
-       if (!g_lastundo)\r
-       {\r
-               //Sys_Printf("Undo_End: no last undo.\n");\r
-               return;\r
-       }\r
-       if (g_lastundo->done)\r
-       {\r
-               //Sys_Printf("Undo_End: last undo already finished.\n");\r
-               return;\r
-       }\r
-       for (brush_t* pBrush = brushlist->next; pBrush != NULL && pBrush != brushlist; pBrush=pBrush->next)\r
-       {\r
-               pBrush->undoId = g_lastundo->id;\r
-       }\r
-}\r
-\r
-/*\r
-=============\r
-Undo_AddEntity\r
-=============\r
-*/\r
-void Undo_AddEntity(entity_t *entity)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_AddEntity: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-       \r
-\r
-       entity_t* pClone;\r
-\r
-       if (!g_lastundo)\r
-       {\r
-               Sys_Printf("Undo_AddEntity: no last undo.\n");\r
-               return;\r
-       }\r
-       //if the entity is already in the undo\r
-       if (Undo_EntityInUndo(g_lastundo, entity))\r
-               return;\r
-       //clone the entity\r
-       pClone = Entity_Clone(entity);\r
-       //save the old undo ID for previous undos\r
-       pClone->undoId = entity->undoId;\r
-       //save the entity ID (we need a full clone)\r
-       pClone->entityId = entity->entityId;\r
-       //\r
-       Entity_AddToList(pClone, &g_lastundo->entitylist);\r
-       //\r
-       g_undoMemorySize += Entity_MemorySize(pClone);\r
-}\r
-\r
-/*\r
-=============\r
-Undo_EndEntity\r
-=============\r
-*/\r
-void Undo_EndEntity(entity_t *entity)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_EndEntity: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-\r
-\r
-       if (!g_lastundo)\r
-       {\r
-#ifdef _DEBUG\r
-               Sys_Printf("Undo_End: no last undo.\n");\r
-#endif\r
-               return;\r
-       }\r
-       if (g_lastundo->done)\r
-       {\r
-#ifdef _DEBUG\r
-               Sys_Printf("Undo_End: last undo already finished.\n");\r
-#endif\r
-               return;\r
-       }\r
-       if (entity == world_entity)\r
-       {\r
-               //Sys_Printf("Undo_AddEntity: undo on world entity.\n");\r
-               //NOTE: we never delete the world entity when undoing an operation\r
-               //              we only transfer the epairs\r
-               return;\r
-       }\r
-       entity->undoId = g_lastundo->id;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_End\r
-=============\r
-*/\r
-void Undo_End(void)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-#ifdef DBG_UNDO\r
-               Sys_Printf("Undo_End: undo is disabled.\n");\r
-#endif\r
-               return;\r
-       }\r
-       \r
-\r
-       if (!g_lastundo)\r
-       {\r
-               //Sys_Printf("Undo_End: no last undo.\n");\r
-               return;\r
-       }\r
-       if (g_lastundo->done)\r
-       {\r
-               //Sys_Printf("Undo_End: last undo already finished.\n");\r
-               return;\r
-       }\r
-       g_lastundo->done = true;\r
-\r
-       //undo memory size is bound to a max\r
-       while (g_undoMemorySize > g_undoMaxMemorySize)\r
-       {\r
-               //always keep one undo\r
-               if (g_undolist == g_lastundo) break;\r
-               Undo_FreeFirstUndo();\r
-       }\r
-       //\r
-       //Sys_Printf("undo size = %d, undo memory = %d\n", g_undoSize, g_undoMemorySize);\r
-}\r
-\r
-/*\r
-=============\r
-Undo_Undo\r
-=============\r
-*/\r
-void Undo_Undo(boolean bSilent)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-               Sys_Printf("Undo_Undo: undo is disabled.\n");\r
-               return;\r
-       }\r
-       \r
-       undo_t *undo, *redo;\r
-       brush_t *pBrush, *pNextBrush;\r
-       entity_t *pEntity, *pNextEntity, *pUndoEntity;\r
-\r
-       if (!g_lastundo)\r
-       {\r
-               Sys_Printf("Nothing left to undo.\n");\r
-               return;\r
-       }\r
-       if (!g_lastundo->done)\r
-       {\r
-               Sys_Printf("Undo_Undo: WARNING: last undo not yet finished!\n");\r
-       }\r
-       // get the last undo\r
-       undo = g_lastundo;\r
-       if (g_lastundo->prev) g_lastundo->prev->next = NULL;\r
-       else g_undolist = NULL;\r
-       g_lastundo = g_lastundo->prev;\r
-\r
-       //allocate a new redo\r
-       redo = (undo_t *) malloc(sizeof(undo_t));\r
-       if (!redo) return;\r
-       memset(redo, 0, sizeof(undo_t));\r
-       redo->brushlist.next = &redo->brushlist;\r
-       redo->brushlist.prev = &redo->brushlist;\r
-       redo->entitylist.next = &redo->entitylist;\r
-       redo->entitylist.prev = &redo->entitylist;\r
-       if (g_lastredo) g_lastredo->next = redo;\r
-       else g_redolist = redo;\r
-       redo->prev = g_lastredo;\r
-       redo->next = NULL;\r
-       g_lastredo = redo;\r
-       redo->time = Sys_DoubleTime();\r
-       redo->id = g_redoId++;\r
-       redo->done = true;\r
-       redo->operation = undo->operation;\r
-\r
-       //reset the redo IDs of all brushes using the new ID\r
-       for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush = pBrush->next)\r
-       {\r
-               if (pBrush->redoId == redo->id)\r
-               {\r
-                       pBrush->redoId = 0;\r
-               }\r
-       }\r
-       for (pBrush = selected_brushes.next; pBrush != NULL && pBrush != &selected_brushes; pBrush = pBrush->next)\r
-       {\r
-               if (pBrush->redoId == redo->id)\r
-               {\r
-                       pBrush->redoId = 0;\r
-               }\r
-       }\r
-       //reset the redo IDs of all entities using thew new ID\r
-       for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pEntity->next)\r
-       {\r
-               if (pEntity->redoId == redo->id)\r
-               {\r
-                       pEntity->redoId = 0;\r
-               }\r
-       }\r
-\r
-       // deselect current sutff\r
-       Select_Deselect();\r
-       // move "created" brushes to the redo\r
-       for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush=pNextBrush)\r
-       {\r
-               pNextBrush = pBrush->next;\r
-               if (pBrush->undoId == undo->id)\r
-               {\r
-                       //Brush_Free(pBrush);\r
-                       //move the brush to the redo\r
-                       Brush_RemoveFromList(pBrush);\r
-                       Brush_AddToList(pBrush, &redo->brushlist);\r
-                       //make sure the ID of the owner is stored\r
-                       pBrush->ownerId = pBrush->owner->entityId;\r
-                       //unlink the brush from the owner entity\r
-                       Entity_UnlinkBrush(pBrush);\r
-               }\r
-       }\r
-       // move "created" entities to the redo\r
-       for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pNextEntity)\r
-       {\r
-               pNextEntity = pEntity->next;\r
-               if (pEntity->undoId == undo->id)\r
-               {\r
-                       // check if this entity is in the undo\r
-                       for (pUndoEntity = undo->entitylist.next; pUndoEntity != NULL && pUndoEntity != &undo->entitylist; pUndoEntity = pUndoEntity->next)\r
-                       {\r
-                               // move brushes to the undo entity\r
-                               if (pUndoEntity->entityId == pEntity->entityId)\r
-                               {\r
-                                       pUndoEntity->brushes.next = pEntity->brushes.next;\r
-                                       pUndoEntity->brushes.prev = pEntity->brushes.prev;\r
-                                       pEntity->brushes.next = &pEntity->brushes;\r
-                                       pEntity->brushes.prev = &pEntity->brushes;\r
-                               }\r
-                       }\r
-                       //\r
-                       //Entity_Free(pEntity);\r
-                       //move the entity to the redo\r
-                       Entity_RemoveFromList(pEntity);\r
-                       Entity_AddToList(pEntity, &redo->entitylist);\r
-               }\r
-       }\r
-       // add the undo entities back into the entity list\r
-       for (pEntity = undo->entitylist.next; pEntity != NULL && pEntity != &undo->entitylist; pEntity = undo->entitylist.next)\r
-       {\r
-               g_undoMemorySize -= Entity_MemorySize(pEntity);\r
-               //if this is the world entity\r
-               if (pEntity->entityId == world_entity->entityId)\r
-               {\r
-                       epair_t* tmp = world_entity->epairs;\r
-                       world_entity->epairs = pEntity->epairs;\r
-      pEntity->epairs = tmp;\r
-                       Entity_Free(pEntity);\r
-               }\r
-               else\r
-               {\r
-                       Entity_RemoveFromList(pEntity);\r
-                       Entity_AddToList(pEntity, &entities);\r
-                       pEntity->redoId = redo->id;\r
-               }\r
-       }\r
-       // add the undo brushes back into the selected brushes\r
-       for (pBrush = undo->brushlist.next; pBrush != NULL && pBrush != &undo->brushlist; pBrush = undo->brushlist.next)\r
-       {\r
-               //Sys_Printf("Owner ID: %i\n",pBrush->ownerId);\r
-               g_undoMemorySize -= Brush_MemorySize(pBrush);\r
-               Brush_RemoveFromList(pBrush);\r
-       Brush_AddToList(pBrush, &active_brushes);\r
-               for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pEntity->next) // fixes broken undo on entities\r
-               {\r
-                       //Sys_Printf("Entity ID: %i\n",pEntity->entityId);\r
-                       if (pEntity->entityId == pBrush->ownerId)\r
-                       {\r
-                               Entity_LinkBrush(pEntity, pBrush);\r
-                               break;\r
-                       }\r
-               }\r
-               //if the brush is not linked then it should be linked into the world entity\r
-               //++timo FIXME: maybe not, maybe we've lost this entity's owner!\r
-               if (pEntity == NULL || pEntity == &entities)\r
-               {\r
-                       Entity_LinkBrush(world_entity, pBrush);\r
-               }\r
-               //build the brush\r
-               //Brush_Build(pBrush);\r
-               Select_Brush(pBrush);\r
-               pBrush->redoId = redo->id;\r
-    }\r
-       if (!bSilent)\r
-               Sys_Printf("%s undone.\n", undo->operation);\r
-       // free the undo\r
-       g_undoMemorySize -= sizeof(undo_t);\r
-       free(undo);\r
-       g_undoSize--;\r
-       g_undoId--;\r
-       if (g_undoId <= 0) g_undoId = 2 * g_undoMaxSize;\r
-       //\r
-    g_bScreenUpdates = true; \r
-    UpdateSurfaceDialog();\r
-    Sys_UpdateWindows(W_ALL);\r
-}\r
-\r
-/*\r
-=============\r
-Undo_Redo\r
-=============\r
-*/\r
-void Undo_Redo(void)\r
-{\r
-       // spog - disable undo if undo levels = 0\r
-       if (g_PrefsDlg.m_nUndoLevels == 0)\r
-       {\r
-               Sys_Printf("Undo_Redo: undo is disabled.\n");\r
-               return;\r
-       }\r
-\r
-       undo_t *redo;\r
-       brush_t *pBrush, *pNextBrush;\r
-       entity_t *pEntity, *pNextEntity, *pRedoEntity;\r
-\r
-       if (!g_lastredo)\r
-       {\r
-               Sys_Printf("Nothing left to redo.\n");\r
-               return;\r
-       }\r
-       if (g_lastundo)\r
-       {\r
-               if (!g_lastundo->done)\r
-               {\r
-                       Sys_Printf("WARNING: last undo not finished.\n");\r
-               }\r
-       }\r
-       // get the last redo\r
-       redo = g_lastredo;\r
-       if (g_lastredo->prev) g_lastredo->prev->next = NULL;\r
-       else g_redolist = NULL;\r
-       g_lastredo = g_lastredo->prev;\r
-       //\r
-       Undo_GeneralStart(redo->operation);\r
-       // remove current selection\r
-       Select_Deselect();\r
-       // move "created" brushes back to the last undo\r
-       for (pBrush = active_brushes.next; pBrush != NULL && pBrush != &active_brushes; pBrush = pNextBrush)\r
-       {\r
-               pNextBrush = pBrush->next;\r
-               if (pBrush->redoId == redo->id)\r
-               {\r
-                       //move the brush to the undo\r
-                       Brush_RemoveFromList(pBrush);\r
-                       Brush_AddToList(pBrush, &g_lastundo->brushlist);\r
-                       g_undoMemorySize += Brush_MemorySize(pBrush);\r
-                       pBrush->ownerId = pBrush->owner->entityId;\r
-                       Entity_UnlinkBrush(pBrush);\r
-               }\r
-       }\r
-       // move "created" entities back to the last undo\r
-       for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pNextEntity)\r
-       {\r
-               pNextEntity = pEntity->next;\r
-               if (pEntity->redoId == redo->id)\r
-               {\r
-                       // check if this entity is in the redo\r
-                       for (pRedoEntity = redo->entitylist.next; pRedoEntity != NULL && pRedoEntity != &redo->entitylist; pRedoEntity = pRedoEntity->next)\r
-                       {\r
-                               // move brushes to the redo entity\r
-                               if (pRedoEntity->entityId == pEntity->entityId)\r
-                               {\r
-                                       pRedoEntity->brushes.next = pEntity->brushes.next;\r
-                                       pRedoEntity->brushes.prev = pEntity->brushes.prev;\r
-                                       pEntity->brushes.next = &pEntity->brushes;\r
-                                       pEntity->brushes.prev = &pEntity->brushes;\r
-                               }\r
-                       }\r
-                       //\r
-                       //Entity_Free(pEntity);\r
-                       //move the entity to the redo\r
-                       Entity_RemoveFromList(pEntity);\r
-                       Entity_AddToList(pEntity, &g_lastundo->entitylist);\r
-                       g_undoMemorySize += Entity_MemorySize(pEntity);\r
-               }\r
-       }\r
-       // add the undo entities back into the entity list\r
-       for (pEntity = redo->entitylist.next; pEntity != NULL && pEntity != &redo->entitylist; pEntity = redo->entitylist.next)\r
-       {\r
-               //if this is the world entity\r
-               if (pEntity->entityId == world_entity->entityId)\r
-               {\r
-      epair_t* tmp = world_entity->epairs;\r
-                       world_entity->epairs = pEntity->epairs;\r
-      pEntity->epairs = tmp;\r
-                       Entity_Free(pEntity);\r
-               }\r
-               else\r
-               {\r
-                       Entity_RemoveFromList(pEntity);\r
-                       Entity_AddToList(pEntity, &entities);\r
-               }\r
-       }\r
-       // add the redo brushes back into the selected brushes\r
-       for (pBrush = redo->brushlist.next; pBrush != NULL && pBrush != &redo->brushlist; pBrush = redo->brushlist.next)\r
-       {\r
-               Brush_RemoveFromList(pBrush);\r
-    Brush_AddToList(pBrush, &active_brushes);\r
-               for (pEntity = entities.next; pEntity != NULL && pEntity != &entities; pEntity = pEntity->next) // fixes broken undo on entities\r
-               {\r
-                       if (pEntity->entityId == pBrush->ownerId)\r
-                       {\r
-                               Entity_LinkBrush(pEntity, pBrush);\r
-                               break;\r
-                       }\r
-               }\r
-               //if the brush is not linked then it should be linked into the world entity\r
-               if (pEntity == NULL || pEntity == &entities)\r
-               {\r
-                       Entity_LinkBrush(world_entity, pBrush);\r
-               }\r
-               //build the brush\r
-               //Brush_Build(pBrush);\r
-               Select_Brush(pBrush);\r
-    }\r
-       //\r
-       Undo_End();\r
-       //\r
-       Sys_Printf("%s redone.\n", redo->operation);\r
-       //\r
-       g_redoId--;\r
-       // free the undo\r
-       free(redo);\r
-       //\r
-    g_bScreenUpdates = true; \r
-    UpdateSurfaceDialog();\r
-    Sys_UpdateWindows(W_ALL);\r
-}\r
-\r
-/*\r
-=============\r
-Undo_RedoAvailable\r
-=============\r
-*/\r
-int Undo_RedoAvailable(void)\r
-{\r
-       if (g_lastredo) return true;\r
-       return false;\r
-}\r
-\r
-int Undo_GetUndoId(void)\r
-{\r
-       if (g_lastundo)\r
-               return g_lastundo->id;\r
-       return 0;\r
-}\r
-\r
-/*\r
-=============\r
-Undo_UndoAvailable\r
-=============\r
-*/\r
-int Undo_UndoAvailable(void)\r
-{\r
-       if (g_lastundo)\r
-       {\r
-               if (g_lastundo->done)\r
-                       return true;\r
-       }\r
-       return false;\r
-}\r
+/*
+   Copyright (C) 2001-2006, William Joseph.
+   All Rights Reserved.
+
+   This file is part of GtkRadiant.
+
+   GtkRadiant is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   GtkRadiant is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GtkRadiant; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "undo.h"
+
+#include "debugging/debugging.h"
+#include "warnings.h"
+
+#include "iundo.h"
+#include "preferencesystem.h"
+#include "string/string.h"
+#include "generic/callback.h"
+#include "preferences.h"
+#include "stringio.h"
+
+#include <list>
+#include <map>
+#include <set>
+
+#include "timer.h"
+
+class DebugScopeTimer {
+    Timer m_timer;
+    const char *m_operation;
+public:
+    DebugScopeTimer(const char *operation)
+            : m_operation(operation)
+    {
+        m_timer.start();
+    }
+
+    ~DebugScopeTimer()
+    {
+        unsigned int elapsed = m_timer.elapsed_msec();
+        if (elapsed > 0) {
+            globalOutputStream() << m_operation << ": " << elapsed << " msec\n";
+        }
+    }
+};
+
+
+class RadiantUndoSystem : public UndoSystem {
+    UINT_CONSTANT(MAX_UNDO_LEVELS, 1024);
+
+    class Snapshot {
+        class StateApplicator {
+        public:
+            Undoable *m_undoable;
+        private:
+            UndoMemento *m_data;
+        public:
+
+            StateApplicator(Undoable *undoable, UndoMemento *data)
+                    : m_undoable(undoable), m_data(data)
+            {
+            }
+
+            void restore()
+            {
+                m_undoable->importState(m_data);
+            }
+
+            void release()
+            {
+                m_data->release();
+            }
+        };
+
+        typedef std::list<StateApplicator> states_t;
+        states_t m_states;
+
+    public:
+        bool empty() const
+        {
+            return m_states.empty();
+        }
+
+        std::size_t size() const
+        {
+            return m_states.size();
+        }
+
+        void save(Undoable *undoable)
+        {
+            m_states.push_front(StateApplicator(undoable, undoable->exportState()));
+        }
+
+        void restore()
+        {
+            for (states_t::iterator i = m_states.begin(); i != m_states.end(); ++i) {
+                (*i).restore();
+            }
+        }
+
+        void release()
+        {
+            for (states_t::iterator i = m_states.begin(); i != m_states.end(); ++i) {
+                (*i).release();
+            }
+        }
+    };
+
+    struct Operation {
+        Snapshot m_snapshot;
+        CopiedString m_command;
+
+        Operation(const char *command)
+                : m_command(command)
+        {
+        }
+
+        ~Operation()
+        {
+            m_snapshot.release();
+        }
+    };
+
+
+    class UndoStack {
+//! Note: using std::list instead of vector/deque, to avoid copying of undos
+        typedef std::list<Operation *> Operations;
+
+        Operations m_stack;
+        Operation *m_pending;
+
+    public:
+        UndoStack() : m_pending(0)
+        {
+        }
+
+        ~UndoStack()
+        {
+            clear();
+        }
+
+        bool empty() const
+        {
+            return m_stack.empty();
+        }
+
+        std::size_t size() const
+        {
+            return m_stack.size();
+        }
+
+        Operation *back()
+        {
+            return m_stack.back();
+        }
+
+        const Operation *back() const
+        {
+            return m_stack.back();
+        }
+
+        Operation *front()
+        {
+            return m_stack.front();
+        }
+
+        const Operation *front() const
+        {
+            return m_stack.front();
+        }
+
+        void pop_front()
+        {
+            delete m_stack.front();
+            m_stack.pop_front();
+        }
+
+        void pop_back()
+        {
+            delete m_stack.back();
+            m_stack.pop_back();
+        }
+
+        void clear()
+        {
+            if (!m_stack.empty()) {
+                for (Operations::iterator i = m_stack.begin(); i != m_stack.end(); ++i) {
+                    delete *i;
+                }
+                m_stack.clear();
+            }
+        }
+
+        void start(const char *command)
+        {
+            if (m_pending != 0) {
+                delete m_pending;
+            }
+            m_pending = new Operation(command);
+        }
+
+        bool finish(const char *command)
+        {
+            if (m_pending != 0) {
+                delete m_pending;
+                m_pending = 0;
+                return false;
+            } else {
+                ASSERT_MESSAGE(!m_stack.empty(), "undo stack empty");
+                m_stack.back()->m_command = command;
+                return true;
+            }
+        }
+
+        void save(Undoable *undoable)
+        {
+            if (m_pending != 0) {
+                m_stack.push_back(m_pending);
+                m_pending = 0;
+            }
+            back()->m_snapshot.save(undoable);
+        }
+    };
+
+    UndoStack m_undo_stack;
+    UndoStack m_redo_stack;
+
+    class UndoStackFiller : public UndoObserver {
+        UndoStack *m_stack;
+    public:
+
+        UndoStackFiller()
+                : m_stack(0)
+        {
+        }
+
+        void save(Undoable *undoable)
+        {
+            ASSERT_NOTNULL(undoable);
+
+            if (m_stack != 0) {
+                m_stack->save(undoable);
+                m_stack = 0;
+            }
+        }
+
+        void setStack(UndoStack *stack)
+        {
+            m_stack = stack;
+        }
+    };
+
+    typedef std::map<Undoable *, UndoStackFiller> undoables_t;
+    undoables_t m_undoables;
+
+    void mark_undoables(UndoStack *stack)
+    {
+        for (undoables_t::iterator i = m_undoables.begin(); i != m_undoables.end(); ++i) {
+            (*i).second.setStack(stack);
+        }
+    }
+
+    std::size_t m_undo_levels;
+
+    typedef std::set<UndoTracker *> Trackers;
+    Trackers m_trackers;
+public:
+    RadiantUndoSystem()
+            : m_undo_levels(64)
+    {
+    }
+
+    ~RadiantUndoSystem()
+    {
+        clear();
+    }
+
+    UndoObserver *observer(Undoable *undoable)
+    {
+        ASSERT_NOTNULL(undoable);
+
+        return &m_undoables[undoable];
+    }
+
+    void release(Undoable *undoable)
+    {
+        ASSERT_NOTNULL(undoable);
+
+        m_undoables.erase(undoable);
+    }
+
+    void setLevels(std::size_t levels)
+    {
+        if (levels > MAX_UNDO_LEVELS()) {
+            levels = MAX_UNDO_LEVELS();
+        }
+
+        while (m_undo_stack.size() > levels) {
+            m_undo_stack.pop_front();
+        }
+        m_undo_levels = levels;
+    }
+
+    std::size_t getLevels() const
+    {
+        return m_undo_levels;
+    }
+
+    std::size_t size() const
+    {
+        return m_undo_stack.size();
+    }
+
+    void startUndo()
+    {
+        m_undo_stack.start("unnamedCommand");
+        mark_undoables(&m_undo_stack);
+    }
+
+    bool finishUndo(const char *command)
+    {
+        bool changed = m_undo_stack.finish(command);
+        mark_undoables(0);
+        return changed;
+    }
+
+    void startRedo()
+    {
+        m_redo_stack.start("unnamedCommand");
+        mark_undoables(&m_redo_stack);
+    }
+
+    bool finishRedo(const char *command)
+    {
+        bool changed = m_redo_stack.finish(command);
+        mark_undoables(0);
+        return changed;
+    }
+
+    void start()
+    {
+        m_redo_stack.clear();
+        if (m_undo_stack.size() == m_undo_levels) {
+            m_undo_stack.pop_front();
+        }
+        startUndo();
+        trackersBegin();
+    }
+
+    void finish(const char *command)
+    {
+        if (finishUndo(command)) {
+            globalOutputStream() << command << '\n';
+        }
+    }
+
+    void undo()
+    {
+        if (m_undo_stack.empty()) {
+            globalOutputStream() << "Undo: no undo available\n";
+        } else {
+            Operation *operation = m_undo_stack.back();
+            globalOutputStream() << "Undo: " << operation->m_command.c_str() << "\n";
+
+            startRedo();
+            trackersUndo();
+            operation->m_snapshot.restore();
+            finishRedo(operation->m_command.c_str());
+            m_undo_stack.pop_back();
+        }
+    }
+
+    void redo()
+    {
+        if (m_redo_stack.empty()) {
+            globalOutputStream() << "Redo: no redo available\n";
+        } else {
+            Operation *operation = m_redo_stack.back();
+            globalOutputStream() << "Redo: " << operation->m_command.c_str() << "\n";
+
+            startUndo();
+            trackersRedo();
+            operation->m_snapshot.restore();
+            finishUndo(operation->m_command.c_str());
+            m_redo_stack.pop_back();
+        }
+    }
+
+    void clear()
+    {
+        mark_undoables(0);
+        m_undo_stack.clear();
+        m_redo_stack.clear();
+        trackersClear();
+    }
+
+    void trackerAttach(UndoTracker &tracker)
+    {
+        ASSERT_MESSAGE(m_trackers.find(&tracker) == m_trackers.end(), "undo tracker already attached");
+        m_trackers.insert(&tracker);
+    }
+
+    void trackerDetach(UndoTracker &tracker)
+    {
+        ASSERT_MESSAGE(m_trackers.find(&tracker) != m_trackers.end(), "undo tracker cannot be detached");
+        m_trackers.erase(&tracker);
+    }
+
+    void trackersClear() const
+    {
+        for (Trackers::const_iterator i = m_trackers.begin(); i != m_trackers.end(); ++i) {
+            (*i)->clear();
+        }
+    }
+
+    void trackersBegin() const
+    {
+        for (Trackers::const_iterator i = m_trackers.begin(); i != m_trackers.end(); ++i) {
+            (*i)->begin();
+        }
+    }
+
+    void trackersUndo() const
+    {
+        for (Trackers::const_iterator i = m_trackers.begin(); i != m_trackers.end(); ++i) {
+            (*i)->undo();
+        }
+    }
+
+    void trackersRedo() const
+    {
+        for (Trackers::const_iterator i = m_trackers.begin(); i != m_trackers.end(); ++i) {
+            (*i)->redo();
+        }
+    }
+};
+
+
+void UndoLevels_importString(RadiantUndoSystem &undo, const char *value)
+{
+    int levels;
+    PropertyImpl<int, const char *>::Import(levels, value);
+    undo.setLevels(levels);
+}
+
+typedef ReferenceCaller<RadiantUndoSystem, void(const char *), UndoLevels_importString> UndoLevelsImportStringCaller;
+
+void UndoLevels_exportString(const RadiantUndoSystem &undo, const Callback<void(const char *)> &importer)
+{
+    PropertyImpl<int, const char *>::Export(static_cast<int>( undo.getLevels()), importer);
+}
+
+typedef ConstReferenceCaller<RadiantUndoSystem, void(
+        const Callback<void(const char *)> &), UndoLevels_exportString> UndoLevelsExportStringCaller;
+
+#include "generic/callback.h"
+
+struct UndoLevels {
+    static void Export(const RadiantUndoSystem &self, const Callback<void(int)> &returnz)
+    {
+        returnz(static_cast<int>(self.getLevels()));
+    }
+
+    static void Import(RadiantUndoSystem &self, int value)
+    {
+        self.setLevels(value);
+    }
+};
+
+void Undo_constructPreferences(RadiantUndoSystem &undo, PreferencesPage &page)
+{
+    page.appendSpinner("Undo Queue Size", 64, 0, 1024, make_property<UndoLevels>(undo));
+}
+
+void Undo_constructPage(RadiantUndoSystem &undo, PreferenceGroup &group)
+{
+    PreferencesPage page(group.createPage("Undo", "Undo Queue Settings"));
+    Undo_constructPreferences(undo, page);
+}
+
+void Undo_registerPreferencesPage(RadiantUndoSystem &undo)
+{
+    PreferencesDialog_addSettingsPage(
+            ReferenceCaller<RadiantUndoSystem, void(PreferenceGroup &), Undo_constructPage>(undo));
+}
+
+class UndoSystemDependencies : public GlobalPreferenceSystemModuleRef {
+};
+
+class UndoSystemAPI {
+    RadiantUndoSystem m_undosystem;
+public:
+    typedef UndoSystem Type;
+
+    STRING_CONSTANT(Name, "*");
+
+    UndoSystemAPI()
+    {
+        GlobalPreferenceSystem().registerPreference("UndoLevels", make_property_string<UndoLevels>(m_undosystem));
+
+        Undo_registerPreferencesPage(m_undosystem);
+    }
+
+    UndoSystem *getTable()
+    {
+        return &m_undosystem;
+    }
+};
+
+#include "modulesystem/singletonmodule.h"
+#include "modulesystem/moduleregistry.h"
+
+typedef SingletonModule<UndoSystemAPI, UndoSystemDependencies> UndoSystemModule;
+typedef Static<UndoSystemModule> StaticUndoSystemModule;
+StaticRegisterModule staticRegisterUndoSystem(StaticUndoSystemModule::instance());
+
+
+class undoable_test : public Undoable {
+    struct state_type : public UndoMemento {
+        state_type() : test_data(0)
+        {
+        }
+
+        state_type(const state_type &other) : UndoMemento(other), test_data(other.test_data)
+        {
+        }
+
+        void release()
+        {
+            delete this;
+        }
+
+        int test_data;
+    };
+
+    state_type m_state;
+    UndoObserver *m_observer;
+public:
+    undoable_test()
+            : m_observer(GlobalUndoSystem().observer(this))
+    {
+    }
+
+    ~undoable_test()
+    {
+        GlobalUndoSystem().release(this);
+    }
+
+    UndoMemento *exportState() const
+    {
+        return new state_type(m_state);
+    }
+
+    void importState(const UndoMemento *state)
+    {
+        ASSERT_NOTNULL(state);
+
+        m_observer->save(this);
+        m_state = *(static_cast<const state_type *>( state ));
+    }
+
+    void mutate(unsigned int data)
+    {
+        m_observer->save(this);
+        m_state.test_data = data;
+    }
+};
+
+#if 0
+
+class TestUndo
+{
+public:
+TestUndo(){
+    undoable_test test;
+    GlobalUndoSystem().begin( "bleh" );
+    test.mutate( 3 );
+    GlobalUndoSystem().begin( "blah" );
+    test.mutate( 4 );
+    GlobalUndoSystem().undo();
+    GlobalUndoSystem().undo();
+    GlobalUndoSystem().redo();
+    GlobalUndoSystem().redo();
+}
+};
+
+TestUndo g_TestUndo;
+
+#endif