-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.hpp
60 lines (44 loc) · 1.68 KB
/
history.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef HISTORY_HPP_
#define HISTORY_HPP_
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <deque>
#define MAX_HISTORY 128
class History {
public:
History() { reset(NULL); }
~History() {
while (!history.empty()) {
free(history.back());
history.pop_back();
}
}
// Resets the undo/redo history and sets `str` as the first entry.
// `hEditor` is the handle to a edit control which this module tracks the history of.
// If `str` is NULL, the first entry will be an empty string.
void reset(HWND hEditor, const wchar_t *str = NULL);
// Adds the current edit control text to the undo/redo history.
// Redo history will be cleared.
void add();
// Changes back the edit control text to the previous state.
void undo();
// Changes the edit control text to the next state.
void redo();
bool canUndo() { return validHistory && historyIndex > 0; }
bool canRedo() { return validHistory && historyIndex < (int)history.size() - 1; }
// Marks the current edit control text as saved.
void setSaved();
// Returns whether the current edit control text is saved.
bool isSaved();
// Changes the edit control which this module tracks the history of, without modifing the history.
void changeEditor(HWND hEditor) { this->hEditor = hEditor; }
private:
int historyIndex; // Index to the current state in the undo/redo history.
int savedIndex; // Index to the saved state in the undo/redo history.
bool validHistory; // Whether the undo/redo history is valid.
HWND hEditor; // Handle to the edit control.
std::deque<wchar_t *> history; // Undo/Redo history.
};
#endif