-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.cpp
117 lines (103 loc) · 2.75 KB
/
history.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "history.hpp"
void History::reset(HWND hEditor, const wchar_t *str) {
SendMessageW(hEditor, EM_SETMODIFY, FALSE, 0);
while (!history.empty()) {
free(history.back());
history.pop_back();
}
wchar_t *newStr;
if (str) {
newStr = (wchar_t *)malloc((wcslen(str) + 1) * sizeof(wchar_t));
if (!newStr) {
validHistory = false;
historyIndex = 0;
savedIndex = -1;
return;
}
wcscpy(newStr, str);
} else {
newStr = (wchar_t *)malloc(sizeof(wchar_t));
if (!newStr) {
validHistory = false;
historyIndex = 0;
savedIndex = -1;
return;
}
newStr[0] = L'\0';
}
this->hEditor = hEditor;
history.push_back(newStr);
historyIndex = savedIndex = 0;
validHistory = true;
}
void History::add() {
if (!validHistory) return;
int editorSize = GetWindowTextLengthW(hEditor) + 1;
wchar_t *wcEditor = (wchar_t *)malloc(sizeof(wchar_t) * editorSize);
if (!wcEditor) {
while (!history.empty()) {
free(history.back());
history.pop_back();
}
historyIndex = 0;
savedIndex = -1;
validHistory = false;
return;
}
GetWindowTextW(hEditor, wcEditor, editorSize);
if (!history.empty() && !wcscmp(history[historyIndex], wcEditor)) {
free(wcEditor);
return;
}
while (historyIndex < (int)history.size() - 1) {
free(history.back());
history.pop_back();
}
if (history.size() >= MAX_HISTORY) {
free(history.front());
history.pop_front();
--historyIndex;
if (savedIndex >= 0) --savedIndex;
}
history.push_back(wcEditor);
++historyIndex;
}
void History::undo() {
if (canUndo()) {
DWORD selEnd;
--historyIndex;
SendMessageW(hEditor, EM_GETSEL, (WPARAM)NULL, (LPARAM)&selEnd);
SetWindowTextW(hEditor, history[historyIndex]);
SendMessageW(hEditor, EM_SETSEL, selEnd, selEnd);
}
}
void History::redo() {
if (canRedo()) {
DWORD selEnd;
++historyIndex;
SendMessageW(hEditor, EM_GETSEL, (WPARAM)NULL, (LPARAM)&selEnd);
SetWindowTextW(hEditor, history[historyIndex]);
SendMessageW(hEditor, EM_SETSEL, selEnd, selEnd);
}
}
void History::setSaved() {
if (validHistory) savedIndex = historyIndex;
SendMessageW(hEditor, EM_SETMODIFY, FALSE, 0);
}
bool History::isSaved() {
if (validHistory) {
if (historyIndex == savedIndex) return true;
if (savedIndex != -1) {
int editorSize = GetWindowTextLengthW(hEditor) + 1;
wchar_t *wcEditor = (wchar_t *)malloc(sizeof(wchar_t) * editorSize);
if (!wcEditor) return false;
GetWindowTextW(hEditor, wcEditor, editorSize);
bool result = !wcscmp(history[savedIndex], wcEditor);
free(wcEditor);
return result;
}
return false;
} else {
return SendMessageW(hEditor, EM_GETMODIFY, 0, 0) == FALSE;
}
}