-
Notifications
You must be signed in to change notification settings - Fork 0
/
UNDO.CPP
490 lines (416 loc) · 12 KB
/
UNDO.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Undo.cpp - implementation for UndoStack objects
//
// Copyright (C) 1993-1994 George Mills and Softronics, Inc. Corporation
// All rights reserved.
//
#include "stdafx.h"
////////////////////////////////////////////////////////////////////////////
// CUndoStack
///////////////////////////
// Constructs a CUndoStack.
CUndoStack::CUndoStack()
{
m_iCurrentState = NotInProgress;
m_iTransactionCookie = 0;
}
///////////////////////////////////////////////////////
// Begins a transaction, which is a sequence of actions
// performed as a group. When undo/redo encounters
// a transaction, it executes the entire transaction.
// EndTransaction must be called after all actions
// to be grouped are Pushed. Transactions may be nested.
//
// For example, if the user highlights several objects
// then chooses cut, a single transaction will contain
// all of the delete actions performed.
//
// Parameters: description - a string which describes
// the transaction to the user.
int CUndoStack::BeginTransaction(LPCTSTR description)
{
m_iTransactionCookie++;
CUndoElement *pUndo = new CUndoElement();
pUndo->m_iTransactionCookie = m_iTransactionCookie;
pUndo->m_iAction = CUndoElement::StartOfTransaction;
pUndo->m_str = description;
InternalPush(pUndo);
return 0;
}
/////////////////////////////////////////////////////
// Ends a transaction started with BeginTransaction.
// The description parameter should be the same
// string passed to the call to BeginTransaction.
//
// Parameters: description - a string which describes
// the transaction to the user.
void CUndoStack::EndTransaction(LPCTSTR description)
{
CUndoElement *pUndo = new CUndoElement();
pUndo->m_iTransactionCookie = m_iTransactionCookie;
pUndo->m_iAction = CUndoElement::EndOfTransaction;
pUndo->m_str = description;
InternalPush(pUndo);
m_iTransactionCookie--;
}
///////////////////////////////////////////////////////
// Pushes an action onto the stack. An action describes
// a command that can be undone/redone.
//
// Parameters: pLogiObj - pointer to CLogiObj involved.
//
// iActionType - the action performed.
// From CUndoElement::enum
//
// str - (default: NULL) a string; the
// meaning depends on iActionType.
void CUndoStack::Push(CLogiObj* pLogiObj, int iActionType, LPCTSTR str)
{
CUndoElement *pUndo = new CUndoElement();
pUndo->m_pLogiObj = pLogiObj;
pUndo->m_iAction = iActionType;
pUndo->m_str = str;
InternalPush(pUndo);
}
///////////////////////////////////////////////////////
// Pushes a Move action onto the stack.
//
// Parameters: pLogiObj - pointer to CLogiObj involved.
//
// rect - the rectangle describing
// the object's previous position.
void CUndoStack::PushMove(CLogiObj* pLogiObj, CRect &rect)
{
CUndoElement *pUndo = new CUndoElement();
pUndo->m_pLogiObj = pLogiObj;
pUndo->m_iAction = CUndoElement::Move;
pUndo->m_rect = rect;
InternalPush(pUndo);
}
///////////////////////////////////////////////////////
// Pushes an Edit action onto the stack.
// PushEdit is called prior to making changes
// to the object state. The state is saved
// through serialization.
//
// Parameters: pLogiObj - pointer to CLogiObj involved.
void CUndoStack::PushEdit(CLogiObj* pLogiObj)
{
CUndoElement *pUndo = new CUndoElement();
pUndo->m_pLogiObj = pLogiObj;
pUndo->m_iAction = CUndoElement::Edit;
CMemFile memFile(4);
CArchive ar(&memFile, CArchive::store);
ASSERT(pLogiObj->m_pDocument != NULL);
ar.m_pDocument = pLogiObj->m_pDocument;
pLogiObj->Serialize(ar);
ar.Close();
pUndo->m_dwLen = (DWORD) memFile.GetLength();
pUndo->m_pVar = memFile.Detach();
InternalPush(pUndo);
}
////////////////////////////////////////////////////////
// Private method called by public PushXXX() methods.
// If an undo is not in progress, pushes pUndo to the
// undo stack, otherwise pushes pUndo to the redo stack.
// Whenever a push is made to the undo stack, the
// redo stack is cleared.
//
// Parameters: pUndo - the CUndoElement describing
// the action to be pushed.
void CUndoStack::InternalPush(CUndoElement *pUndo)
{
pUndo->Init();
if (m_iCurrentState == NotInProgress)
InternalReset(&m_redo);
if (m_iCurrentState == UndoInProgress)
m_redo.AddHead(pUndo);
else
{
m_undo.AddHead(pUndo);
}
}
///////////////////////////////////////////
// Reverts the action at the top
// of the undo stack.
//
// Parameters: pDoc - the current CLogiDoc.
void CUndoStack::Undo(CLogiDoc* pDoc)
{
m_iCurrentState = UndoInProgress;
InternalUndo(pDoc, &m_undo);
m_iCurrentState = NotInProgress;
}
///////////////////////////////////////////
// Reverts the action at the top
// of the redo stack.
//
// Parameters: pDoc - the current CLogiDoc.
void CUndoStack::Redo(CLogiDoc* pDoc)
{
m_iCurrentState = RedoInProgress;
InternalUndo(pDoc, &m_redo);
m_iCurrentState = NotInProgress;
}
///////////////////////////////////////////////////////////////
// Private method called by Undo() and Redo() to
// revert an action on either the undo or the redo stack.
// If the element at the top of the stack is a transaction,
// all actions in the transaction (and any nested transactions)
// are reverted.
//
// Parameters: pDoc - the current CLogiDoc.
// pUndoList - which stack (undo/redo).
void CUndoStack::InternalUndo(CLogiDoc* pDoc, CObList *pUndoList)
{
CUndoElement* pUndo;
int i_WhichTransaction = -1;
if (pUndoList->IsEmpty()) return;
BOOL bKeepGoing = FALSE;
do
{
pUndo = (CUndoElement*) pUndoList->RemoveHead();
if (i_WhichTransaction == -1)
{
if (pUndo->m_iAction == CUndoElement::EndOfTransaction)
{
i_WhichTransaction = pUndo->m_iTransactionCookie;
BeginTransaction(pUndo->m_str);
bKeepGoing = TRUE;
}
}
else if (pUndo->m_iAction == CUndoElement::StartOfTransaction)
{
if (i_WhichTransaction == pUndo->m_iTransactionCookie)
{
EndTransaction(pUndo->m_str);
bKeepGoing = FALSE;
}
}
pUndo->Undo(pDoc);
delete pUndo;
pUndo = NULL;
}
while (bKeepGoing);
pDoc->UpdateAllViews(NULL);
}
////////////////////////////////////////
// Clears both the undo and redo stacks.
void CUndoStack::Reset()
{
InternalReset(&m_redo);
InternalReset(&m_undo);
}
//////////////////////////////////////////////////
// Private method called by Reset() and other
// methods to clear either the undo or redo stack.
//
// Parameters: pList - which stack (undo/redo).
void CUndoStack::InternalReset(CObList *pList)
{
POSITION pos;
CUndoElement *und;
int pass;
// Two passes. Delete wires first, other objects second.
for (pass=0; pass<=1; pass++)
{
//TEDO: this seems like an odd for loop, would a while be better?
for (pos = pList->GetHeadPosition(); pos != NULL; )
{
und = (CUndoElement *) pList->GetNext(pos);
und->Cleanup(pass);
// On last pass, delete CUndoElement too
if (pass == 1)
{
delete und;
und = NULL;
}
}
}
pList->RemoveAll();
}
/////////////////////////////////////////
// Checks status of undo stack.
BOOL CUndoStack::CanUndo()
{
return !m_undo.IsEmpty();
}
/////////////////////////////////////////
// Checks status of redo stack.
BOOL CUndoStack::CanRedo()
{
return !m_redo.IsEmpty();
}
////////////////////////////////////////////////
// Retrieves the description associated with
// the item on the top of the undo stack.
CString CUndoStack::GetUndoDescription()
{
if (m_undo.IsEmpty())
return "";
return ((CUndoElement *) m_undo.GetHead())->GetUndoDescription();
}
////////////////////////////////////////////////
// Retrieves the description associated with
// the item on the top of the redo stack.
CString CUndoStack::GetRedoDescription()
{
if (m_redo.IsEmpty())
return "";
return ((CUndoElement *) m_redo.GetHead())->GetRedoDescription();
}
//////////////////////////
// CUndoStack desctructor.
CUndoStack::~CUndoStack()
{
Reset();
}
////////////////////////////////////////////////////////////////////////////
// CUndoElement
/////////////////////////////////////////
// Returns the description describing the
// element in the "undo" context.
//
// Therefore, the string describes the
// action that was performed.
CString CUndoElement::GetUndoDescription()
{
switch (m_iAction)
{
case CUndoElement::StartOfTransaction:
case CUndoElement::EndOfTransaction:
return m_str;
case CUndoElement::Delete:
return "Delete";
case CUndoElement::Add:
return "Add";
case CUndoElement::Move:
return "Move";
case CUndoElement::Rename:
return "Rename";
case CUndoElement::Edit:
return "Edit";
default:
ASSERT(FALSE);
return "";
}
}
/////////////////////////////////////////
// Returns the description describing the
// element in the "redo" context.
//
// Therefore, the string describes the
// INVERSE operation of the action that
// was performed.
CString CUndoElement::GetRedoDescription()
{
switch (m_iAction)
{
case CUndoElement::StartOfTransaction:
case CUndoElement::EndOfTransaction:
return m_str;
case CUndoElement::Delete:
return "Add";
case CUndoElement::Add:
return "Delete";
case CUndoElement::Move:
return "Move";
case CUndoElement::Rename:
return "Rename";
case CUndoElement::Edit:
return "Edit";
default:
ASSERT(FALSE);
return "";
}
}
////////////////////////////////////////////////
// Init is called when new item pushed on stack.
void CUndoElement::Init()
{
switch (m_iAction)
{
case CUndoElement::Delete:
if (m_pLogiObj->IsKindOf(RUNTIME_CLASS(CLogiWire)))
((CLogiWire *) m_pLogiObj)->CleanupReferences();
else
((CLogiGate *) m_pLogiObj)->CleanupReferences();
break;
}
}
///////////////////////////////////////
// Undo is called to reverse the action
// described by the CUndoElement.
void CUndoElement::Undo(CLogiDoc* pDoc)
{
switch (m_iAction)
{
case CUndoElement::Delete:
pDoc->Add(m_pLogiObj);
if (m_pLogiObj->IsKindOf(RUNTIME_CLASS(CLogiWire)))
pDoc->ReWire(m_pLogiObj->m_position, (CLogiWire*) m_pLogiObj, m_pLogiObj->m_iPage);
break;
case CUndoElement::Add:
pDoc->Remove(m_pLogiObj);
break;
case CUndoElement::Move:
pDoc->Move(m_pLogiObj, m_rect);
break;
case CUndoElement::Rename:
pDoc->Rename(m_pLogiObj, m_str);
break;
case CUndoElement::Edit:
CMemFile memFile((BYTE *) m_pVar, m_dwLen, 0);
CArchive ar(&memFile, CArchive::load);
ASSERT(m_pLogiObj->m_pDocument != NULL);
ar.m_pDocument = m_pLogiObj->m_pDocument;
pDoc->Edit(m_pLogiObj, ar);
ar.Close();
memFile.Close();
delete m_pVar;
m_pVar = NULL;
break;
}
}
////////////////////////////////////////////////
// Cleanup is called when a stack containing
// the object is reset.
//
// Currently, the cleanup has two passes, where
// wires are deleted on pass 0 and everything
// else is deleted on pass 1.
//
// Parameters: pass - the pass number,
// which is 0 or 1.
void CUndoElement::Cleanup(int pass)
{
if (pass == 0)
{
// Delete wires on first pass
if (m_iAction == CUndoElement::Delete)
{
if (m_pLogiObj->IsKindOf(RUNTIME_CLASS(CLogiWire)))
{
delete m_pLogiObj;
m_pLogiObj = NULL;
}
}
else if (m_iAction == CUndoElement::Edit)
{
delete m_pVar;
m_pVar = NULL;
}
}
else if (pass == 1)
{
// Delete everything else on second pass
if (m_iAction == CUndoElement::Delete)
{
if (m_pLogiObj != NULL)
{
delete m_pLogiObj;
m_pLogiObj = NULL;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////