00001 #ifndef __GUNDO_H__ 00002 #define __GUNDO_H__ 00003 00004 class GUndoEvent 00005 { 00006 public: 00007 virtual ~GUndoEvent() {} 00008 virtual void ApplyChange() {} 00009 virtual void RemoveChange() {} 00010 }; 00011 00012 class GUndo 00013 { 00014 int Pos; 00015 List<GUndoEvent> Events; 00016 00017 public: 00018 GUndo() { Pos = 0; } 00019 ~GUndo() { Empty(); } 00020 00021 bool CanUndo() { return (Pos > 0) AND (Events.Length() > 0); } 00022 bool CanRedo() { return (Pos < Events.Length()) AND (Events.Length() > 0); } 00023 void Empty() { Events.DeleteObjects(); Pos = 0; } 00024 00025 GUndo &operator +=(GUndoEvent *e) 00026 { 00027 while (Events.Length() > Pos) 00028 { 00029 GUndoEvent *u = Events.Last(); 00030 Events.Delete(u); 00031 DeleteObj(u); 00032 } 00033 00034 Events.Insert(e); 00035 Pos++; 00036 return *this; 00037 } 00038 00039 void Undo() 00040 { 00041 if (CanUndo()) 00042 { 00043 GUndoEvent *e = Events.ItemAt(Pos-1); 00044 if (e) 00045 { 00046 e->RemoveChange(); 00047 Pos--; 00048 } 00049 } 00050 } 00051 00052 void Redo() 00053 { 00054 if (CanRedo()) 00055 { 00056 GUndoEvent *e = Events.ItemAt(Pos); 00057 if (e) 00058 { 00059 e->ApplyChange(); 00060 Pos++; 00061 } 00062 } 00063 } 00064 }; 00065 00066 #endif