1// Aseprite
2// Copyright (C) 2022 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_DOCUMENT_UNDO_H_INCLUDED
9#define APP_DOCUMENT_UNDO_H_INCLUDED
10#pragma once
11
12#include "app/doc_range.h"
13#include "app/sprite_position.h"
14#include "base/disable_copying.h"
15#include "obs/observable.h"
16#include "undo/undo_history.h"
17
18#include <iosfwd>
19#include <string>
20
21namespace 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
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
61 int* savedCounter() { return &m_savedCounter; }
62
63 const undo::UndoState* firstState() const { return m_undoHistory.firstState(); }
64 const undo::UndoState* lastState() const { return m_undoHistory.lastState(); }
65 const undo::UndoState* currentState() const { return m_undoHistory.currentState(); }
66
67 void moveToState(const undo::UndoState* state);
68
69 private:
70 const undo::UndoState* nextUndo() const;
71 const undo::UndoState* nextRedo() const;
72
73 // undo::UndoHistoryDelegate impl
74 void onDeleteUndoState(undo::UndoState* state) override;
75
76 undo::UndoHistory m_undoHistory;
77 Context* m_ctx;
78 size_t m_totalUndoSize;
79
80 // This counter is equal to 0 if we are in the "saved state", i.e.
81 // the document on memory is equal to the document on disk. This
82 // value is less than 0 if we're in a past version of the document
83 // (due undoes), or greater than 0 if we are in a future version
84 // (due redoes).
85 int m_savedCounter;
86
87 // True if the saved state was invalidated/corrupted/lost in some
88 // way. E.g. If the save process fails.
89 bool m_savedStateIsLost;
90
91 DISABLE_COPYING(DocUndo);
92 };
93
94} // namespace app
95
96#endif
97