Newer
Older
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
require 'fox16'
require 'fox16/undolist'
include Fox
# Undo record for text fragment
class FXTextCommand < FXCommand
def initialize(txt, change)
@text = txt
@buffer = nil
@pos = change.pos
@numCharsDeleted = change.ndel
@numCharsInserted = change.nins
end
def size
(@buffer != nil) ? @buffer.size : 0
end
end
# Insert command
class FXTextInsert < FXTextCommand
def undoName
"Undo insert"
end
def redoName
"Redo insert"
end
# Undoing an insert removes the previously inserted text
def undo
@buffer = @text.extractText(@pos, @numCharsInserted)
@text.removeText(@pos, @numCharsInserted)
@text.cursorPos = @pos
@text.makePositionVisible(@pos)
end
# Redoing an insert re-inserts the same text
def redo
@text.insertText(@pos, @buffer)
@text.cursorPos = @pos + @numCharsInserted
@text.makePositionVisible(@pos + @numCharsInserted)
@buffer = nil
end
end
# Delete command
class FXTextDelete < FXTextCommand
def initialize(txt, change)
super(txt, change)
@buffer = change.del
end
def undoName
"Undo delete"
end
def redoName
"Redo delete"
end
# Undoing a delete re-inserts the deleted text
def undo
@text.insertText(@pos, @buffer)
@text.cursorPos = @pos + @buffer.length
@text.makePositionVisible(@pos + @buffer.length)
@buffer = nil
end
# Redoing a delete removes it again
def redo
@buffer = @text.extractText(@pos, @numCharsDeleted)
@text.removeText(@pos, @buffer.length)
@text.cursorPos = @pos
@text.makePositionVisible(@pos)
end
end
# Replace command
class FXTextReplace < FXTextCommand
def initialize(txt, change)
super(txt, change)
@buffer = change.del
end
def undoName
"Undo replace"
end
def redoName
"Redo replace"
end
# Undoing a replace reinserts the old text
def undo
tmp = @text.extractText(@pos, @numCharsInserted)
@text.replaceText(@pos, @numCharsInserted, @buffer)
@text.cursorPos = @pos + @buffer.length
@text.makePositionVisible(@pos + @buffer.length)
@buffer = tmp
end
# Redo a replace reinserts the new text
def redo
tmp = @text.extractText(@pos, @numCharsDeleted)
@text.replaceText(@pos, @numCharsDeleted, @buffer)
@text.cursorPos = @pos + @numCharsInserted
@text.makePositionVisible(@pos + @numCharsInserted)
@buffer = tmp
end
end