Action History'�Undo and Redo

Environment: VC6, VC7.NET, WinXP, Win2000, Win98, NT4 SP3

Not so long ago I decided that I needed to add undo and redo functionality to my program. I thought about it for a while and decided that it would become easier if I created some kind of interface which could be easily derived from. Every time the user does anything a new action is added to a std::vector list and these instructions can be interpreted by your program. I would recommend keeping the amount of data stored about an instruction as small as possible.

Basically for everything your program can do (that you would like to be undone or redone) you create a class derived from the interface IActionBase. This interface contains a run function which should contain the code to complete the action, and then an undo function which should contain the code to undo the action again.

The demo project's main window contains a box which you can drag around the screen. Every time you complete a drag cycle (released the mouse) you will then be able to click the undo button. I have also inserted an option under the "Edit" menu which enables you to clear the undo/redo history.

Technically with this system you could implement some kind of history feature like that in Adobe Photoshop so that you can revert to a specific point in history.

To use this system declare an instance of the class CActionHistory class in the CMainFrame class (if you are using MFC). I would recommend that you declare it publicly so that it is easy to access, especially from the CView derived class. Then for every action your program does derive a class from the interface IActionBase. You then only need to create and add the action to the interface. Please note that the AddAction member function of CActionHistory does not run the action. You have to run the action separately. Here is a small example of its usage:

// create a new action for dragging the box
CActionDragBox* pNewAction = new CActionDragBox(&m_rtBox, this);
// you can run the action whenever you want using the pointer
// add this actions to the history list

The CActionHistory class will take care of cleaning up instances of your action classes created with the new operator.

If you are interested in using this I would suggest that you firstly take a close look at the demo.

I cannot see any problems with this method, but I am no expert and there may be a better method. I hope that this article will be of some benefit to you.


Download demo project - 47 Kb
Download source - 3 Kb

About the Author

Lea Hayes

I have always enjoyed learning new concepts which is one of the things which suits me to programming, because in programming you are always learning new and interesting concepts. I have been programming since about 1995 and have become reasonably experienced with Microsoft Visual Basic 4,5,6 and Microsoft Visual C++ 5,6,7,7.1. Having achieved a double A grade in AVCE ICT I have begun to study BEng Games and Entertainment Systems Engineering at the University of Greenwich at Medway. I have now completed my first year with the kind of results expected from a first-class degree.


  • not bad solution, but maybe anyone has a better idea?

    Posted by Legacy on 09/02/2003 12:00am

    Originally posted by: Archi

    not bad solution, but maybe anyone has a better idea?
    I'm just starting to think about Undo/Redo policy for my programs and would like to hear some ideas...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date