-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUNDO.PAS
More file actions
231 lines (197 loc) · 5.23 KB
/
UNDO.PAS
File metadata and controls
231 lines (197 loc) · 5.23 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
{$I COMPILER.INC}
unit Undo;
interface
uses
AplObj,
AplTypes,
Lists;
type
PUndoStack = ^TUndoStack;
PUndoItem = ^TUndoItem;
PUndoAction = ^TUndoAction;
PUndoActionList = ^TUndoActionList;
TUndoAction = object(TObject)
private
public
ActionType: word;
OnUndo: PEventProc;
OnRedo: PEventProc;
constructor Create(AActionType: word; ACode: word);
constructor CreateEvents(AActionType: word; ACode: word; AOnUndo, AOnRedo: PEventProc);
procedure Init; virtual;
end;
TUndoItem = object(TObject)
private
public
Code: word;
ActionType: word;
Data: pointer;
constructor Create(AActionType: word; ACode: word);
constructor CreateData(AActionType: word; ACode: word; AData: pointer);
procedure Init; virtual;
end;
TUndoActionList = object(TList)
private
public
procedure Init; virtual;
function Add(AItem: PUndoAction): integer;
function GetItem(AIndex: integer): PUndoAction;
function GetItemByType(AActionType: word): PUndoAction;
function IndexOf(AAction: PUndoAction): integer;
function IndexOfType(AActionType: word): integer;
function HasType(AActionType: word): boolean;
procedure SetItem(AIndex: integer; AAction: PUndoAction);
procedure Insert(AIndex: integer; AAction: PUndoAction);
end;
TUndoStack = object(TObjectStack)
private
public
RedoIndex: integer;
Actions: PUndoActionList;
destructor Free; virtual;
procedure Init; virtual;
function Add(AItem: PUndoItem): integer;
function GetItem(AIndex: integer): PUndoItem;
function IndexOf(AItem: PUndoItem): integer;
procedure SetItem(AIndex: integer; AItem: PUndoItem);
procedure Insert(AIndex: integer; AItem: PUndoItem);
function Push(AItem: PUndoItem): integer;
function Pop: PUndoItem;
function Peek: PUndoItem;
end;
implementation
procedure TUndoStack.Init;
begin
inherited Init;
RedoIndex := -1;
Actions := New(PUndoActionList, Create);
end;
destructor TUndoStack.Free;
begin
FreeAndNil(Actions);
inherited Free;
end;
function TUndoStack.Add(AItem: PUndoItem): integer;
begin
Add := inherited Add(AItem);
end;
function TUndoStack.GetItem(AIndex: integer): PUndoItem;
begin
GetItem := PUndoItem(inherited GetItem(AIndex));
end;
function TUndoStack.IndexOf(AItem: PUndoItem): integer;
begin
IndexOf := inherited IndexOf(AItem);
end;
procedure TUndoStack.SetItem(AIndex: integer; AItem: PUndoItem);
begin
inherited SetItem(AIndex, AItem);
end;
procedure TUndoStack.Insert(AIndex: integer; AItem: PUndoItem);
begin
inherited Insert(AIndex, AItem);
end;
function TUndoStack.Push(AItem: PUndoItem): integer;
begin
Push := inherited Push(AItem);
end;
function TUndoStack.Pop: PUndoItem;
begin
Pop := PUndoItem(inherited Pop);
end;
function TUndoStack.Peek: PUndoItem;
begin
Peek := PUndoItem(inherited Peek);
end;
procedure TUndoItem.Init;
begin
inherited Init;
end;
constructor TUndoItem.Create(AActionType: word; ACode: word);
begin
inherited Create;
ActionType := AActionType;
Code := ACode;
end;
constructor TUndoItem.CreateData(AActionType: word; ACode: word; AData: pointer);
begin
Create(AActionType, ACode);
Data := AData;
end;
procedure TUndoActionList.Init;
begin
inherited Init;
end;
function TUndoActionList.Add(AItem: PUndoAction): integer;
var
index: integer;
item: PUndoAction;
begin
if HasType(AItem^.ActionType) then begin
index := IndexOfType(AItem^.ActionType);
item := GetItem(index);
FreeAndNil(item);
SetItem(index, AItem);
end;
Add := inherited Add(AItem);
end;
function TUndoActionList.GetItem(AIndex: integer): PUndoAction;
begin
GetItem := PUndoAction(inherited GetItem(AIndex));
end;
function TUndoActionList.IndexOf(AAction: PUndoAction): integer;
begin
IndexOf := inherited IndexOf(AAction);
end;
procedure TUndoActionList.SetItem(AIndex: integer; AAction: PUndoAction);
begin
inherited SetItem(AIndex, AAction);
end;
procedure TUndoActionList.Insert(AIndex: integer; AAction: PUndoAction);
begin
inherited Insert(AIndex, AAction);
end;
function TUndoActionList.HasType(AActionType: word): boolean;
begin
HasType := IndexOfType(AActionType) >= 0;
end;
function TUndoActionList.IndexOfType(AActionType: word): integer;
var
index: integer;
begin
for index := 0 to Count - 1 do
if GetItem(index)^.ActionType = AActionType then begin
IndexOfType := index;
exit;
end;
IndexOfType := -1;
end;
function TUndoActionList.GetItemByType(AActionType: word): PUndoAction;
var
index: integer;
begin
GetItemByType := nil;
index := IndexOfType(AActionType);
if index >= 0 then
GetItemByType := GetItem(index);
end;
constructor TUndoAction.Create(AActionType: word; ACode: word);
begin
inherited Create;
ActionType := AActionType;
end;
constructor TUndoAction.CreateEvents(AActionType: word; ACode: word; AOnUndo, AOnRedo: PEventProc);
begin
Create(AActionType, ACode);
OnUndo := AOnUndo;
OnRedo := AOnRedo;
end;
procedure TUndoAction.Init;
begin
inherited Init;
OnUndo := nil;
OnRedo := nil;
ActionType := 0;
end;
begin
end.