//////////////////////////////////////////////////////////////////////////////// /*! @file Undo.h Механизм откатов для сети и вообще Undo-Redo. - Part of RANet - Research Assistant Net Library (based on ANSI C++). - Copyright(C) 2006-2010, Viktor E. Bursian, St.Petersburg, Russia. Viktor.Bursian@mail.ioffe.ru */////////////////////////////////////////////////////////////////////////////// #ifndef Undo_H #define Undo_H #include #include #include "General.h" #include "Strings.h" //#include "Storable.h" namespace RA { //------------------------------------------------------------------------------ ANNOUNCE_ABSTRACT_CLASS(sUndoItem) ANNOUNCE_ABSTRACT_CLASS(sRedoItem) ANNOUNCE_ABSTRACT_CLASS(sUndoRedoItem) ANNOUNCE_CLASS(sUndoList) ANNOUNCE_CLASS(sUndoGroupedList) ANNOUNCE_CLASS(sUndoRedo) ANNOUNCE_CLASS(sDummyUndoItem) ANNOUNCE_CLASS(sDummyRedoItem) using std::list; using std::stack; //---------------------------------------------------------------- sUndoItem --- class RANet_EXPORT sUndoItem //: virtual public sStorable { public: virtual ~sUndoItem () {}; sUndoItem () :TheOwner(NULL) {}; virtual sString Hint () { return sString(); }; virtual void UndoIt () =0; psUndoRedo Owner () const { return TheOwner; }; private: void SetOwner (psUndoRedo owner) { TheOwner=owner; }; //public: // fields //TimeStamp private: // fields psUndoRedo TheOwner; friend class sUndoRedo; }; //------------------------------------------------------------ sUndoRedoItem --- class RANet_EXPORT sRedoItem : public sUndoItem { public: virtual void RedoIt () =0; private: virtual void UndoIt () {}; }; //------------------------------------------------------------ sUndoRedoItem --- class RANet_EXPORT sUndoRedoItem : public sUndoItem { public: virtual void RedoIt () =0; }; //---------------------------------------------------------------- sUndoList --- class RANet_EXPORT sUndoList : public sUndoRedoItem { public: virtual ~sUndoList (); sUndoList () :RedoSubstitution(NULL) {}; sUndoList (rcsString hint) :HintText(hint),RedoSubstitution(NULL) {}; sUndoList (psRedoItem RS) :RedoSubstitution(RS) {}; sUndoList (rcsString hint ,psRedoItem RS) :HintText(hint),RedoSubstitution(RS) {}; virtual void UndoIt (); virtual void RedoIt (); virtual void Push (psUndoItem I); virtual psUndoItem Pop (); virtual psUndoItem Top (); virtual void Clear (); virtual bool Empty () { return Items.empty(); }; virtual int Size () { return Items.size(); }; virtual sString Hint () { return HintText; }; virtual void SetHintText (rcsString hint) { HintText=hint; }; virtual void SetRedoSubstitution (psRedoItem RS) { RedoSubstitution=RS; }; protected: private: // fields list Items; sString HintText; psRedoItem RedoSubstitution; friend class sUndoRedo; }; //--------------------------------------------------------- sUndoGroupedList --- class RANet_EXPORT sUndoGroupedList : public sUndoList { public: virtual void Push (psUndoItem I); virtual psUndoItem Pop (); virtual psUndoItem Top (); void GroupBegin (rcsString hint ,psRedoItem RS); void GroupEnd (); bool ThereIsUnclosedGroup () const { return ! OpenGroups.empty(); }; psUndoList GroupOnTop () const { return ( OpenGroups.empty() ? NULL : OpenGroups.top() ); }; private: // fields stack OpenGroups; }; //---------------------------------------------------------------- sUndoRedo --- class RANet_EXPORT sUndoRedo : public sUndoGroupedList { public: //typedefs typedef bool fSelected(psUndoItem); typedef fSelected * pfSelected; public: virtual ~sUndoRedo (); sUndoRedo (int limit = -1); virtual void Push (psUndoItem I); virtual void Clear (); void SetLimit (int limit); virtual void Undo (); virtual void Redo (); void GroupOpen () { GroupBegin(sString(),NULL); }; void GroupOpen (rcsString hint) { GroupBegin(hint,NULL); }; void GroupOpen (psRedoItem RS) { GroupBegin(sString(),RS); }; void GroupOpen (rcsString hint ,psRedoItem RS) { GroupBegin(hint,RS); }; void GroupClose (rcsString hint); void GroupClose (psRedoItem RS); void GroupClose (rcsString hint ,psRedoItem RS); void GroupClose (); virtual void GroupFlush (); virtual void RollBack (); // virtual void RollBackSelective (rcsTag class_type); virtual void RollBackSelective (pfSelected); virtual bool RedoEmpty () { return REDO.Empty(); }; virtual int RedoSize () { return REDO.Size(); }; virtual bool CanUndo (); virtual bool CanRedo (); virtual sString Hint (); virtual sString RedoHint (); private: void ExecuteUndoOn (psUndoItem I); void ExecuteRedoOn (psUndoItem I); //psRedoItem private: // fields sUndoList REDO; bool Undoing; bool Redoing; int UndoLimit; friend class sUndoList; }; //----------------------------------------------------------- sDummyUndoItem --- class RANet_EXPORT sDummyUndoItem : public sUndoItem { public: virtual void UndoIt () {}; }; //----------------------------------------------------------- sDummyRedoItem --- class RANet_EXPORT sDummyRedoItem : public sRedoItem { public: virtual void RedoIt () {}; }; //------------------------------------------------------------------------------ }; //namespace RA #endif