[go: up one dir, main page]

Menu

[r19]: / trunk / UndoManager.cpp  Maximize  Restore  History

Download this file

289 lines (265 with data), 7.0 kB

  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyleft 2004 Chris Korda
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or any later version.
/*
chris korda
revision history:
rev date comments
00 22feb04 initial version
01 07mar04 add limit
02 08mar04 in SwapState, set busy before finding undoable
03 08mar04 in NotifyEdit, ctor isn't called when array shrinks
04 12mar04 if coalescing, remove states above current position
05 29sep04 cancel edit must update titles
06 19mar05 bump m_Edits regardless of number of undo levels
07 22nov06 rename strings to start with IDS_
08 25nov06 use CArrayishList instead of CList
09 23nov07 support Unicode
10 03jan08 replace CSmartBuf with CRefPtr
11 18mar08 remove key support
12 19feb09 in NotifyEdit, fix stack size limit case
13 28may10 support insignificant edits
14 05dec12 add UndoNoRedo
15 11feb13 add OnUpdateTitles and SetPos
16 01may14 widen CtrlID and Code to 32-bit
17 07may15 in DumpState, use address format for object pointer
18 23jun15 in SwapState, save to temporary in case restore throws
undoable edit interface
*/
#include "stdafx.h"
#include "Resource.h"
#include "UndoManager.h"
#include "Undoable.h"
#if UNDO_NATTER
#define UNDO_DUMP_STATE(Tag, Pos) DumpState(Tag, Pos);
#else
#define UNDO_DUMP_STATE(Tag, Pos)
#endif
CUndoManager::CUndoManager(CUndoable *Root) :
m_Root(Root)
{
m_CanUndo = FALSE;
m_CanRedo = FALSE;
m_Pos = 0;
m_Levels = INT_MAX;
m_Edits = 0;
m_Action = UA_NONE;
}
CUndoManager::~CUndoManager()
{
}
inline CString CUndoManager::GetTitle(int Pos)
{
return(m_Root->GetUndoTitle(m_List[Pos]));
}
int CUndoManager::FindUndoable() const
{
int pos = m_Pos;
while (pos > 0) {
pos--;
if (m_List[pos].IsSignificant())
return(pos);
}
return(-1);
}
int CUndoManager::FindRedoable() const
{
int pos = m_Pos;
while (pos < GetSize()) {
if (m_List[pos].IsSignificant())
return(pos);
pos++;
}
return(-1);
}
void CUndoManager::Undo()
{
int PrevUndo = FindUndoable();
if (PrevUndo >= 0) {
// undo previous significant edit, and all insignificant edits back to it
m_Action = UA_UNDO;
for (int i = m_Pos - 1; i >= PrevUndo; i--) {
UNDO_DUMP_STATE(_T("Undo"), i);
SwapState(i);
if (IsIdle())
return; // edits discarded
}
m_Pos = PrevUndo;
UpdateTitles();
m_Action = UA_NONE;
}
}
void CUndoManager::Redo()
{
int NextRedo = FindRedoable();
if (NextRedo >= 0) {
// redo next significant edit, and all insignificant edits up to it
m_Action = UA_REDO;
for (int i = m_Pos; i <= NextRedo; i++) {
UNDO_DUMP_STATE(_T("Redo"), i);
SwapState(i);
if (IsIdle())
return; // edits discarded
}
m_Pos = NextRedo + 1;
UpdateTitles();
m_Action = UA_NONE;
}
}
void CUndoManager::UndoNoRedo()
{
Undo();
m_List.SetSize(m_Pos);
UpdateTitles();
}
void CUndoManager::SwapState(int Pos)
{
ASSERT(Pos >= 0 && Pos < GetSize());
CUndoState PrevState = m_List[Pos];
CUndoable *uap = m_Root;
CUndoState NewState(PrevState); // initialize temporary
uap->SaveUndoState(NewState); // save to temporary in case restore throws
uap->RestoreUndoState(PrevState);
m_List[Pos] = NewState;
#if UNDO_NATTER
if (uap == NULL)
_tprintf(_T("Can't find instance.\n"));
#endif
}
void CUndoManager::NotifyEdit(int CtrlID, int Code, UINT Flags)
{
ASSERT(CtrlID != UNDO_CTRL_ID_INSIGNIFICANT); // reserved control ID
if (IsIdle()) {
// do insignificance test first; can modify CtrlID
if (Flags & CUndoable::UE_INSIGNIFICANT)
CtrlID = UNDO_CTRL_ID_INSIGNIFICANT;
else {
if (!m_Edits++) // if first modification
OnModify(TRUE); // call derived handler
}
// if coalesce requested and notifier's key matches top of stack
if ((Flags & CUndoable::UE_COALESCE) && m_Pos
&& m_List[m_Pos - 1].IsMatch(CtrlID, Code)) {
if (GetSize() > m_Pos) { // don't resize array needlessly
m_List.SetSize(m_Pos); // remove states above current position
UpdateTitles();
}
return;
}
if (m_Levels <= 0)
return;
if (m_Pos >= m_Levels) { // if stack size at limit
m_List.RemoveAt(0); // remove bottom state
m_Pos--;
}
m_List.SetSize(m_Pos); // array shrinks if we've undone
CUndoState us;
us.m_Val.i64 = 0;
us.m_CtrlID = CtrlID;
us.m_Code = Code;
m_List.Add(us);
CUndoState *usp = &m_List[m_Pos];
m_Root->SaveUndoState(*usp);
UNDO_DUMP_STATE(_T("Notify"), m_Pos);
m_Pos++;
UpdateTitles();
} else {
#if UNDO_NATTER
_tprintf(_T("Ignoring notify.\n"));
#endif
}
}
void CUndoManager::CancelEdit(int CtrlID, int Code)
{
#if UNDO_NATTER
_tprintf(_T("CancelEdit CtrlID=%d Code=%d\n"), CtrlID, Code);
#endif
int i;
for (i = m_Pos - 1; i >= 0; i--) {
if (m_List[i].IsMatch(CtrlID, Code))
break;
}
if (i >= 0) {
m_List.RemoveAt(i);
m_Pos--;
if (!--m_Edits) // if last modification
OnModify(FALSE); // call derived handler
UpdateTitles();
}
#if UNDO_NATTER
if (i < 0)
_tprintf(_T("Can't cancel edit.\n"));
#endif
}
void CUndoManager::DiscardAllEdits()
{
#if UNDO_NATTER
_tprintf(_T("DiscardAllEdits\n"));
#endif
m_List.SetSize(0);
m_CanUndo = FALSE;
m_CanRedo = FALSE;
m_Pos = 0;
m_Edits = 0;
m_Action = UA_NONE;
OnModify(FALSE); // call derived handler
UpdateTitles();
}
void CUndoManager::DumpState(LPCTSTR Tag, int Pos)
{
_tprintf(_T("%s '%s' Pos=%d %s Obj=%p\n"), Tag, GetTitle(Pos), Pos,
m_List[Pos].DumpState(), m_List[Pos].GetObj());
}
void CUndoManager::UpdateTitles()
{
int PrevUndo = FindUndoable();
m_CanUndo = PrevUndo >= 0;
if (m_CanUndo)
m_UndoTitle = GetTitle(PrevUndo);
else
m_UndoTitle.Empty();
int NextRedo = FindRedoable();
m_CanRedo = NextRedo >= 0;
if (m_CanRedo)
m_RedoTitle = GetTitle(NextRedo);
else
m_RedoTitle.Empty();
OnUpdateTitles();
}
void CUndoManager::SetLevels(int Levels)
{
if (Levels < 0)
Levels = INT_MAX;
if (Levels < GetSize()) { // if shrinking history
if (m_Pos < Levels) // if undo depth is below new size
DiscardAllEdits(); // history can't be saved
else { // trim excess history from the bottom
int Excess = GetSize() - Levels;
m_List.RemoveAt(0, Excess);
m_Pos = max(m_Pos - Excess, 0);
}
}
m_Levels = Levels;
}
void CUndoManager::SetPos(int Pos)
{
ASSERT(Pos >= 0 && Pos <= GetSize());
int Start = m_Pos;
if (Pos == Start)
return; // nothing to do
int steps = abs(Pos - Start);
bool undoing = Pos < Start;
for (int iStep = 0; iStep < steps; iStep++) {
if (undoing)
Undo();
else
Redo();
}
}
void CUndoManager::OnModify(bool Modified)
{
}
void CUndoManager::OnUpdateTitles()
{
}