1 // Aseprite
2 // Copyright (C) 2001-2018  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifndef APP_DOCUMENT_UNDO_H_INCLUDED
8 #define APP_DOCUMENT_UNDO_H_INCLUDED
9 #pragma once
10 
11 #include "app/doc_range.h"
12 #include "app/sprite_position.h"
13 #include "base/disable_copying.h"
14 #include "base/unique_ptr.h"
15 #include "obs/observable.h"
16 #include "undo/undo_history.h"
17 
18 #include <iosfwd>
19 #include <string>
20 
21 namespace app {
22   using namespace doc;
23 
24   class Cmd;
25   class CmdTransaction;
26   class Context;
27   class DocUndoObserver;
28 
29   class DocUndo : public obs::observable<DocUndoObserver>,
30                   public undo::UndoHistoryDelegate {
31   public:
32     DocUndo();
33 
totalUndoSize()34     size_t totalUndoSize() const { return m_totalUndoSize; }
35 
36     void setContext(Context* ctx);
37 
38     void add(CmdTransaction* cmd);
39 
40     bool canUndo() const;
41     bool canRedo() const;
42     void undo();
43     void redo();
44 
45     void clearRedo();
46 
47     bool isSavedState() const;
48     void markSavedState();
49     void impossibleToBackToSavedState();
50 
51     std::string nextUndoLabel() const;
52     std::string nextRedoLabel() const;
53 
54     SpritePosition nextUndoSpritePosition() const;
55     SpritePosition nextRedoSpritePosition() const;
56     std::istream* nextUndoDocRange() const;
57     std::istream* nextRedoDocRange() const;
58 
59     Cmd* lastExecutedCmd() const;
60 
savedCounter()61     int* savedCounter() { return &m_savedCounter; }
62 
firstState()63     const undo::UndoState* firstState() const { return m_undoHistory.firstState(); }
currentState()64     const undo::UndoState* currentState() const { return m_undoHistory.currentState(); }
65 
66     void moveToState(const undo::UndoState* state);
67 
68   private:
69     const undo::UndoState* nextUndo() const;
70     const undo::UndoState* nextRedo() const;
71 
72     // undo::UndoHistoryDelegate impl
73     void onDeleteUndoState(undo::UndoState* state) override;
74 
75     undo::UndoHistory m_undoHistory;
76     Context* m_ctx;
77     size_t m_totalUndoSize;
78 
79     // This counter is equal to 0 if we are in the "saved state", i.e.
80     // the document on memory is equal to the document on disk. This
81     // value is less than 0 if we're in a past version of the document
82     // (due undoes), or greater than 0 if we are in a future version
83     // (due redoes).
84     int m_savedCounter;
85 
86     // True if the saved state was invalidated/corrupted/lost in some
87     // way. E.g. If the save process fails.
88     bool m_savedStateIsLost;
89 
90     DISABLE_COPYING(DocUndo);
91   };
92 
93 } // namespace app
94 
95 #endif
96