[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UnDo/ ReDo Buffer ?


Le 20/08/2025 à 14:17, Claus Dietrich a écrit :
Hi

I developed an app for scanning documents. It depicts the scans in a ListView with a thumbnail preview. The order of the scans in the ListView can be changed and scans can also be deleted. What was missing so far is an UnDo function. So I asked an AI about a suitable concept and got a reasonable proposal based on a class for scanned document and a collection of these classes and a history stack. I tested it in a pilot project and it turned out to be feasible and efficient.

In parallel I found this in the Gambas-wiki: https://gambaswiki.org/ edit/snippets/undoredobuffer

I don't understand this chapter. It seems to tell the reader, that there is a generic solution for any thinkable undo/redo action in an app. As this is beyond my imagination I kindly ask for some guidance resp. concepts for UnDo/ReDo functions und sample projects if available.

Best regards

Claus



I will try to explain that concept the best I can.

But please stop doing using this AI shit. Or only for questions you already know the answer. :-)

To implement undo/redo, you have to list all the actions in your program that are undoable. These actions must be reversible to be able to be redoable.

Then you create a stack of "actions", each action being an object or a structure that includes all the needed information for undoing or redoing the action.

Then you have the following methods:

- Add a new action to the stack.
- Undo the last action.
- Redo the action.
- Begin a group of action that will be undone or redone globally.
- End a group of action that will be undone or redone globally.

You have an example of that with the 'CUndo' class in the IDE source code. It is used for implementing undo/redo in the form editor.

Now the question is: how an action can be executed (inverted or not) in a generic way?

This way: in the 'CUndo' class, I put the name of the public method in the action data, and I use 'Object.Call()' to dynamically call the method in the object passed to the Undo() or the Redo() method.

So everything is entirely generic! The 'CUndo' class can be used for almost anything.

When a "undoable" method is called, it calls the Add() method of the 'CUndo' object, passing as argument the name of the method that will do the reverse action (with the arguments if needed).

To see an example, look at the 'FForm.RaiseControl()' and 'FForm.LowerControl()' method. They indirectly call methods in the 'CControl' class that implement what I described above.

The only real hard thing is being careful when describing the action, the reverse action, and implementing their respective methods. They must be the exact contrary!

I hope it was clear enough. If you have any question, tell me.

Regards,

--
Benoît Minisini.


Follow-Ups:
Re: UnDo/ ReDo Buffer ?Claus Dietrich <claus.dietrich@xxxxxxxxxx>
References:
UnDo/ ReDo Buffer ?Claus Dietrich <claus.dietrich@xxxxxxxxxx>