[pygtk] Undo and Redo

Brett Viren bv at bnl.gov
Sun Sep 4 06:01:15 WST 2005


Hi Jens,

Jens Geiregat <jens.geiregat at gmail.com> writes:

> I would like to add undo and redo buttons to my program. The problem
> is I do not have any idea how to implement the undo and redo-actions.
> Are there some (easy) examples on this subject? Or other (small)
> pygtk-programs that support undo and redo?

What you want to do is google "memento design pattern".  It's well
discussed in the GOF's Software Design Patterns book.

Basically, just before your application data is modified, it must
bundle up any information it needs in order to undo this mod.  This
bundle is the memento.  It then pushes this memento object onto an
undo stack and makes whatever change.  When you click the "undo"
button the top memento is popped from the undo stack and applied to
your application data (and its UI reflects this change).  By requiring
the the application to produce a memento at the time of applying the
undo memento, you can keep a redo stack as well.

All this is very application specific so must be written to fit.  It
works best when you abstract your application data away from the UI
(ie, as a model in MVC) and have the model present and accept
mementos.  In principle, it would be possible to allow automatic
undo/redo in an MVC system, but I've not seen it in the few I've
looked at.

Luck,
-Brett.



More information about the pygtk mailing list