class HistoryText(Pmw
.ScrolledText
):
def __init__(self
, parent
= None, **kw
):
# Define the megawidget options.
('compressany', 1, None),
('compresstail', 1, None),
('historycommand', None, None),
self
.defineoptions(kw
, optiondefs
)
# Initialise the base class (after defining the options).
Pmw
.ScrolledText
.__init
__(self
, parent
)
# Initialise instance variables.
self
._lastIndex
= 0 # pointer to end of history list
# Check keywords and initialise options.
# This is the first history entry. Add it.
self
._list
.append([text
, text
, _MODIFIED
])
currentEntry
= self
._list
[self
._currIndex
]
if text
== currentEntry
[_ORIGINAL
]:
# The current history entry has not been modified. Check if
# we need to add it again.
if self
['compresstail'] and self
._currIndex
== self
._lastIndex
:
# Undo any changes for the current history entry, since they
# will now be available in the new entry.
currentEntry
[_MODIFIED
] = currentEntry
[_ORIGINAL
]
historycommand
= self
['historycommand']
if self
._currIndex
== self
._lastIndex
:
# The last history entry is currently being displayed,
# so disable the special meaning of the 'Next' button.
# A previous history entry is currently being displayed,
# so allow the 'Next' button to go to the entry after this one.
self
._pastIndex
= self
._currIndex
if callable(historycommand
):
historycommand('normal', nextState
)
# Create the new history entry.
self
._list
.append([text
, text
, _MODIFIED
])
# Move the pointer into the history entry list to the end.
self
._lastIndex
= self
._lastIndex
+ 1
self
._currIndex
= self
._lastIndex
if self
._currIndex
== self
._lastIndex
and self
._pastIndex
is None:
self
._modifyDisplay
('next')
self
._modifyDisplay
('prev')
self
._modifyDisplay
('undo')
self
._modifyDisplay
('redo')
def _modifyDisplay(self
, command
):
# Modify the display to show either the next or previous
# history entry (next, prev) or the original or modified
# version of the current history entry (undo, redo).
# Save the currently displayed text.
if currentText
[-1] == '\n':
currentText
= currentText
[:-1]
currentEntry
= self
._list
[self
._currIndex
]
if currentEntry
[_DISPLAY
] == _MODIFIED
:
currentEntry
[_MODIFIED
] = currentText
elif currentEntry
[_ORIGINAL
] != currentText
:
currentEntry
[_MODIFIED
] = currentText
if command
in ('next', 'prev'):
currentEntry
[_DISPLAY
] = _MODIFIED
if command
in ('next', 'prev'):
if self
._pastIndex
is not None:
self
._currIndex
= self
._pastIndex
self
._currIndex
= self
._currIndex
+ 1
if self
._currIndex
== self
._lastIndex
:
self
._currIndex
= self
._currIndex
- 1
historycommand
= self
['historycommand']
if callable(historycommand
):
historycommand(prevstate
, nextstate
)
currentEntry
= self
._list
[self
._currIndex
]
currentEntry
[_DISPLAY
] = _ORIGINAL
currentEntry
[_DISPLAY
] = _MODIFIED
self
.delete('1.0', 'end')
self
.insert('end', currentEntry
[currentEntry
[_DISPLAY
]])