author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 11:09:04 +0200 | |
changeset 5048 | 63f42cfbaf59 |
parent 4370 | a1ab0c18d19d |
permissions | -rw-r--r-- |
1522 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1454 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3820 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1454 | 16 |
Object subclass:#UndoSupport |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
17 |
instanceVariableNames:'actionPerformer transaction undoList redoList |
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
18 |
infoOfCurrentTransaction undoLimit' |
1454 | 19 |
classVariableNames:'' |
20 |
poolDictionaries:'' |
|
21 |
category:'Views-Text' |
|
22 |
! |
|
23 |
||
24 |
Object subclass:#CompoundAction |
|
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
25 |
instanceVariableNames:'actions userFriendlyInfo' |
1454 | 26 |
classVariableNames:'' |
27 |
poolDictionaries:'' |
|
28 |
privateIn:UndoSupport |
|
29 |
! |
|
30 |
||
31 |
!UndoSupport class methodsFor:'documentation'! |
|
32 |
||
1522 | 33 |
copyright |
34 |
" |
|
35 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
36 |
All Rights Reserved |
|
37 |
||
38 |
This software is furnished under a license and may be used |
|
39 |
only in accordance with the terms of that license and with the |
|
40 |
inclusion of the above copyright notice. This software may not |
|
41 |
be provided or otherwise made available to, or used by, any |
|
42 |
other person. No title to or ownership of the software is |
|
43 |
hereby transferred. |
|
44 |
" |
|
45 |
! |
|
46 |
||
1454 | 47 |
documentation |
48 |
" |
|
49 |
Framework for undo & redo. |
|
50 |
See concrete usage in EditTextView. |
|
51 |
||
52 |
[author:] |
|
53 |
(cg@betti) |
|
54 |
||
55 |
[instance variables:] |
|
56 |
||
57 |
[class variables:] |
|
58 |
||
59 |
[see also:] |
|
60 |
||
61 |
" |
|
62 |
! |
|
63 |
||
64 |
examples |
|
65 |
" |
|
66 |
See usage in EditTextView |
|
67 |
" |
|
68 |
! ! |
|
69 |
||
70 |
!UndoSupport class methodsFor:'instance creation'! |
|
71 |
||
72 |
for:anActionPerformer |
|
73 |
^ self new actionPerformer:anActionPerformer. |
|
74 |
! |
|
75 |
||
76 |
new |
|
77 |
^ self basicNew initialize. |
|
78 |
! ! |
|
79 |
||
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
80 |
!UndoSupport methodsFor:'accessing'! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
81 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
82 |
actionInfo:aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
83 |
infoOfCurrentTransaction := aString |
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
84 |
! |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
85 |
|
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
86 |
undoLimit |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
87 |
^ undoLimit |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
88 |
! |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
89 |
|
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
90 |
undoLimit:something |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
91 |
undoLimit := something. |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
92 |
! ! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
93 |
|
1454 | 94 |
!UndoSupport methodsFor:'initialization'! |
95 |
||
96 |
actionPerformer:something |
|
97 |
actionPerformer := something. |
|
98 |
! |
|
99 |
||
100 |
initialize |
|
101 |
self resetHistories. |
|
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
102 |
"/ undoLimit := 1000. |
1454 | 103 |
! |
104 |
||
105 |
resetHistories |
|
106 |
transaction := nil. |
|
107 |
undoList := OrderedCollection new. |
|
108 |
redoList := OrderedCollection new. |
|
109 |
! ! |
|
110 |
||
3820 | 111 |
!UndoSupport methodsFor:'queries'! |
112 |
||
113 |
isInTransaction |
|
114 |
^ transaction notNil |
|
115 |
||
116 |
"Created: / 30-04-2016 / 20:31:04 / cg" |
|
117 |
! |
|
118 |
||
119 |
transactionNotEmpty |
|
120 |
^ transaction notEmptyOrNil |
|
121 |
||
122 |
"Created: / 30-04-2016 / 20:47:53 / cg" |
|
123 |
! ! |
|
124 |
||
1454 | 125 |
!UndoSupport methodsFor:'undo & again'! |
126 |
||
127 |
addUndo:action |
|
128 |
transaction notNil ifTrue:[ |
|
129 |
transaction add:action |
|
130 |
]. |
|
131 |
! |
|
132 |
||
3820 | 133 |
addUndoFirst:action |
134 |
transaction notNil ifTrue:[ |
|
135 |
transaction addFirst:action |
|
136 |
]. |
|
137 |
||
138 |
"Created: / 30-04-2016 / 20:50:53 / cg" |
|
139 |
! |
|
140 |
||
3825 | 141 |
closeTransactionAndAddTo:anUndoList |
142 |
"at the end of an operation, check the individual undo actions |
|
143 |
and add them to the given undoList. |
|
144 |
(either individually or as a compound undo action). |
|
4370 | 145 |
Check if the new action(s) can be combined with the previous undo action into one |
3825 | 146 |
(this is done for individual keystrokes and character-deletions)" |
147 |
||
148 |
|anythingToUndo actionToAdd lastAction canCombine| |
|
149 |
||
150 |
anythingToUndo := false. |
|
1454 | 151 |
transaction notEmptyOrNil ifTrue:[ |
3825 | 152 |
anythingToUndo := transaction size > 1 |
153 |
or:[ transaction first isRestoreSelectionAndCursor not]. |
|
154 |
]. |
|
155 |
anythingToUndo ifTrue:[ |
|
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
156 |
canCombine := false. |
3825 | 157 |
|
158 |
anUndoList isEmpty ifTrue:[ |
|
159 |
"/ nothing yet |
|
160 |
anUndoList addAll:transaction |
|
161 |
] ifFalse:[ |
|
162 |
"/ append new actions |
|
163 |
(infoOfCurrentTransaction isNil and:[ transaction size == 1 ]) ifTrue:[ |
|
164 |
actionToAdd := transaction first. |
|
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
165 |
|
3825 | 166 |
lastAction := anUndoList last. |
1550
7710a4b44cc9
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1549
diff
changeset
|
167 |
canCombine := lastAction perform:#canCombineWithNext: with:actionToAdd ifNotUnderstood:false. |
3825 | 168 |
] ifFalse:[ |
169 |
(infoOfCurrentTransaction isNil |
|
170 |
and:[ transaction size == 2 |
|
171 |
and:[ transaction first isRestoreSelectionAndCursor |
|
172 |
and:[ undoList contains:[:op | op isRestoreSelectionAndCursor] ]]]) ifTrue:[ |
|
173 |
actionToAdd := transaction second. |
|
174 |
||
175 |
lastAction := anUndoList last. |
|
176 |
canCombine := lastAction perform:#canCombineWithNext: with:actionToAdd ifNotUnderstood:false. |
|
177 |
] ifFalse:[ |
|
178 |
actionToAdd := (CompoundAction new actions:transaction). |
|
179 |
actionToAdd info:infoOfCurrentTransaction. |
|
180 |
]. |
|
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
181 |
]. |
3825 | 182 |
canCombine ifTrue:[ |
183 |
lastAction combineWithNext:actionToAdd. |
|
184 |
] ifFalse:[ |
|
185 |
anUndoList add:actionToAdd. |
|
186 |
(undoLimit notNil and:[anUndoList size > undoLimit]) ifTrue:[ |
|
187 |
Transcript showCR:'forget old undo-action'. |
|
188 |
anUndoList removeFirst. |
|
189 |
]. |
|
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
190 |
]. |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
191 |
]. |
1454 | 192 |
]. |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
193 |
infoOfCurrentTransaction := nil. |
1454 | 194 |
transaction := nil |
4370 | 195 |
|
196 |
"Modified (comment): / 22-05-2017 / 12:48:07 / mawalch" |
|
1454 | 197 |
! |
198 |
||
199 |
executeActionFrom:doList addUndoTo:unDoList |
|
1588 | 200 |
|action previousTransaction| |
1454 | 201 |
|
202 |
doList notEmptyOrNil ifTrue:[ |
|
203 |
action := doList removeLast. |
|
204 |
||
1588 | 205 |
previousTransaction := transaction. |
206 |
[ |
|
207 |
transaction := OrderedCollection new. |
|
1454 | 208 |
|
1588 | 209 |
action executeIn:actionPerformer. |
1454 | 210 |
|
1588 | 211 |
self closeTransactionAndAddTo:unDoList. |
212 |
] ensure:[ |
|
213 |
transaction := previousTransaction. |
|
214 |
] |
|
1454 | 215 |
] |
216 |
! |
|
217 |
||
218 |
hasRedoAction |
|
219 |
^ redoList size > 0 |
|
220 |
! |
|
221 |
||
222 |
hasUndoAction |
|
223 |
^ undoList size > 0 |
|
224 |
! |
|
225 |
||
226 |
lastRedoAction |
|
227 |
^ redoList removeLast |
|
228 |
! |
|
229 |
||
230 |
lastUndoAction |
|
231 |
^ undoList removeLast |
|
232 |
! |
|
233 |
||
234 |
nonUndoableDo:aBlock |
|
235 |
|prev| |
|
236 |
||
237 |
prev := transaction. |
|
238 |
transaction := nil. |
|
239 |
aBlock |
|
240 |
ensure:[ |
|
241 |
transaction := prev. |
|
242 |
]. |
|
243 |
! |
|
244 |
||
245 |
redo |
|
246 |
self executeActionFrom:redoList addUndoTo:undoList |
|
247 |
! |
|
248 |
||
1932
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
249 |
redoActionInfo |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
250 |
^ redoList last info |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
251 |
! |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
252 |
|
1454 | 253 |
undo |
254 |
self executeActionFrom:undoList addUndoTo:redoList |
|
255 |
! |
|
256 |
||
1500 | 257 |
undoActionInfo |
258 |
^ undoList last info |
|
259 |
! |
|
260 |
||
1454 | 261 |
undoableDo:aBlock |
1526 | 262 |
self undoableDo:aBlock info:nil. |
263 |
! |
|
264 |
||
265 |
undoableDo:aBlock info:aString |
|
1454 | 266 |
transaction notNil ifTrue:[ |
1526 | 267 |
infoOfCurrentTransaction := infoOfCurrentTransaction ? aString. |
1454 | 268 |
aBlock value. |
269 |
] ifFalse:[ |
|
270 |
transaction := OrderedCollection new. |
|
1526 | 271 |
infoOfCurrentTransaction := aString. |
1454 | 272 |
aBlock |
273 |
ensure:[ |
|
274 |
self closeTransactionAndAddTo:undoList. |
|
275 |
]. |
|
276 |
]. |
|
277 |
! ! |
|
278 |
||
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
279 |
!UndoSupport::CompoundAction methodsFor:'accessing'! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
280 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
281 |
info |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
282 |
^ userFriendlyInfo |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
283 |
! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
284 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
285 |
info:aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
286 |
userFriendlyInfo := aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
287 |
! ! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
288 |
|
1454 | 289 |
!UndoSupport::CompoundAction methodsFor:'adding'! |
290 |
||
291 |
actions:aCollection |
|
292 |
actions := aCollection |
|
293 |
! |
|
294 |
||
295 |
add:action |
|
296 |
actions isNil ifTrue:[ |
|
297 |
actions := OrderedCollection new. |
|
298 |
]. |
|
299 |
actions add:action |
|
300 |
! ! |
|
301 |
||
302 |
!UndoSupport::CompoundAction methodsFor:'execution'! |
|
303 |
||
304 |
executeIn:editor |
|
305 |
actions reverseDo:[:each | each executeIn:editor ] |
|
306 |
! ! |
|
307 |
||
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
308 |
!UndoSupport::CompoundAction methodsFor:'queries'! |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
309 |
|
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
310 |
canCombineWithNext:nextAction |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
311 |
^ false. |
3825 | 312 |
! |
313 |
||
314 |
isRestoreSelectionAndCursor |
|
315 |
^ false. |
|
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
316 |
! ! |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
317 |
|
1454 | 318 |
!UndoSupport class methodsFor:'documentation'! |
319 |
||
320 |
version |
|
3820 | 321 |
^ '$Header$' |
1454 | 322 |
! ! |
3820 | 323 |