Undo Stack Change Notification

SyntaxEditor for Windows Forms Forum

Posted 14 years ago by Greg Shaffer - Boulder
Avatar
Hi,

I need to somehow link your undo capabilities to my undo system. All I need to do this is to be notified when your Undo / Redo stacks change in size. Is there a way to do this? If not, it would be nice to provide an event in your UndoStack and RedoStack classes.

Also, it would be nice if some sort of editing hint was provided to help describe the action that occurred.

Thanks,
Greg

Comments (14)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Greg,

The DocumentModification class has a HasFlag method that you can use to determine if the modification is from an Undo or Redo operation or from normal editing (based on the flag). Use that and you'll know that caused it.

Also, DocumentModification.Type tells you the type of action.

You can get the modification from the DocumentTextChanged event.


Actipro Software Support

Posted 14 years ago by Greg Shaffer - Boulder
Avatar
It sounds like the DocumentModification tells me when the Document is Modified by either Undo, Redo, user typing, etc.

What I want to track is when an undoable action is added to your undo / redo stack.

For instance, typing a line of code is treated as one undoable action. I need to know when you track such events so that I can add those events to my stack which also contains non-syntax editor actions.

Does DocumentModification fire for Undo / Redo events when you place undoable actions on your stack?

Thanks,
Greg
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Well multiple modifications can be combined into a single "undoable action" on the undo stack. This occurs when typing several chars in a row. In that case the undo stack isn't really changed but the new character is added to the previously added modification's InsertText.

So essentially a DocumentModification occurs whenever something happens to the document. Each DocumentModification is added to the undo stack (or redo stack when undoing) however if it is a typing modification, it has the potential of being combined with another typing modification already on the stack.


Actipro Software Support

Posted 14 years ago by Greg Shaffer - Boulder
Avatar
So, I cannot use the DocumentModification.

Back to my main question. Can I observe the undo stack? It still would be useful to know when the UndoStack's count changes. Even if you merge on the top of the stack, I still need to know when you add a new entry to the stack.

Is this possible?

Thanks,
Greg
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Where's my head... it's been a long day. :) There is a SyntaxEditor.DocumentUndoRedoStateChanged event that I think is exactly what you want. It fires whenever the undo stack is modified.


Actipro Software Support

Posted 14 years ago by Greg Shaffer - Boulder
Avatar
Hey No Problem.

That is Fantastic! Good job. Very Happy!

Thanks,
Greg

[Modified at 06/23/2005 03:46 PM]
Posted 14 years ago by Greg Shaffer - Boulder
Avatar
Actually,

It appears that the event does not fire often enough. If I enter a multi-line document, each line is an undoable event, but the DocumentUndoRedoStateChanged does not get fired for each line.

I think that if I enter the text:

line1
line2
line3

Then there should be five items (3 identifiers and 2 newlines) on the undo stack, so the event should fire at least five times. It doesn't, even though there are 5 items on the undo stack. It fires one time, when I type the first char of "line1".

Here's a suggestion: Add an event to the UndoRedo that fires when a new item is added to the stack. Make it provide the stack that changed, a hint describing the modification, the document or code editor that changed, and if something was added / removed from the stack.

Thanks,
Greg
Posted 14 years ago by Boyd - Automated Software Testing Consultant / Developer, Patterson Consulting, LLC
Avatar
This is a great topic, and might help with this previous issue I posted:

http://www.actiprosoftware.com/Support/Forums/ViewForumTopic.aspx?ForumTopicID=849#2817

The problem is that Undo/Redo exists on the Document level, and I want to be able to track selection information as well. I had planned to look into a method of implementing my own undo/redo stack to help with this as well as other operations not specific to a document (like a Replace All in all documents). In order to be able to keep track of selection state, I'd need to be notified BEFORE an undo/redo operation so that I could record the selection before the operation is executed. Could you possibly add two events... one before the undo/redo operation, and one after? Perhaps even make the "Before" event cancellable so that we can override the default behavior?
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
After looking at the code, the event only fires if either the undo/redo stack gets its first item or if one of them is cleared. The intent is to allow you to update the enabled state of an undo/redo toolbar button when the event fires.

Do you think this should be changed to just fire after any change whatsoever or should we add a second event that is raised more often? What name should that be?


Actipro Software Support

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Oh Boyd, your item is on the TODO list too.


Actipro Software Support

Posted 14 years ago by Boyd - Automated Software Testing Consultant / Developer, Patterson Consulting, LLC
Avatar
I would say provide the events that would allow us to build our own stack. This should include notifications for when an item is added to undo/redo as well as when an item is removed from undo/redo. There is also the instance you mentioned where a series of characters being typed gets appended to the last undo item. In this case, we'd need to know when an undo item was modified.

How about...

UndoItemAdded
UndoItemRemoved
UndoItemModified
RedoItemAdded
RedoItemRemoved

Where you pass a reference to the item that was added/removed/modified?
Posted 14 years ago by Greg Shaffer - Boulder
Avatar
That is a good idea from Boyd.

Some sort of mechanism to do that would be great. Another possibility is to combine all of this into one event, and provide a hint that describes what happened. If there was an "Added" and "Removed", programmers could easily check the size of the stack, which would mean that DocumentUndoRedoStateChanged would not be necessary as a means to determine when you can undo or redo.

Thanks,
Greg
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
The DocumentUndoRedoStateChanged event has been greatly enhanced for the next maintenance release. It will fire after any undo/redo state change, and indicates the modification that caused it and what type of state change occurred. It also indicates a boolean flag to tell whether you need to update the enabled states of toolbar buttons for undo/redo commands.


Actipro Software Support

Posted 14 years ago by Greg Shaffer - Boulder
Avatar
Thanks!

This is going to help a ton. I'm very impressed with how fast you responded to our needs.

-Greg
The latest build of this product (v2018.1 build 0341) was released 7 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.