Undo/Redo in TRichViewEdit

<< Click to display table of contents >>

Undo/Redo in TRichViewEdit

(Introduced in version 1.3)

All methods for document editing that introduced in editor (TRichViewEdit) can be undone/redone (see editor-style methods).

All methods for document modification that introduced in TRichView can't be undone/redone (see viewer-style methods). If you wish to modify document with these methods, you must clear undo buffer before.

All methods and properties described in this topic are methods and properties of TRichViewEdit.

property UndoLimit: Integer

Property UndoLimit sets the capacity of undo buffer. It can be used to disable undo/redo feature.

Basic undo/redo

Undo/redo is available in editor even without any effort from your side.

User can undo the last operation by pressing  Ctrl + Z  or  Alt + Backspace .

User can redo the last undone operation by pressing  Shift + Ctrl + Z  or  Shift + Alt + Backspace .

Undo/redo buffers will be cleared automatically when you call Clear method, or when you use methods for loading documents (which call Clear inside).

You can clear undo and redo buffer yourself using method ClearUndo.

Implementing menu/toolbar buttons for undo/redo

You can undo/redo operations from the application code, using methods Undo and Redo.

You can change enabled/disabled state of menu items or buttons comparing the returned values of functions UndoAction and RedoAction with rvutNone (rvutNone menu for undo/redo should be disabled; other values enabled).

Call these methods inside OnChange event.

You can modify caption/hint of menu item or button to display the name of action that will be undone/redone next. A value identifying this undo/redo action can be obtained using UndoAction/RedoAction.

There are no predefined names for standard undo/redo actions. The Editor demo (Demos\*\Editors\Editor 1\) has the suggested names of actions in RVUndoStr.pas.

rvutCustom constant is a special case, see below. For this value, use UndoName/RedoName methods.

Grouping operations for undo

You can set the mode so that all subsequent operations will be considered by the editor as a whole, and they will be undone (and redone) in one step.

First, you need to give name to this undo action. Name can be standard (not a name really, but a type of undo action) or custom.

Use BeginUndoGroup to give a standard name, or BeginUndoCustomGroup to give a custom name.

Next, you need to call SetUndoGroupMode.

procedure TRichViewEdit.BeginUndoGroup(UndoType: TRVUndoType);

begins a new undo action. It must be followed by SetUndoGroupMode(True) ... SetUndoGroupMode(False).

UndoType can be any value of type TRVUndoType, except for rvutNone and rvutCustom.
This value will be returned by UndoAction/RedoAction when undo/redo.

procedure TRichViewEdit.BeginUndoCustomGroup(const Name: TRVUnicodeString);

the same as BeginUndoGroup, but allows to set your own name for the undo action. The undo action will have type = rvutCustom.

Name of it will be returned by UndoName/RedoName.

procedure TRichViewEdit.SetUndoGroupMode(GroupUndo: Boolean);

sets mode where all subsequent actions will be undone and redone as one. It must be preceded by either BeginUndoGroup or BeginUndoCustomGroup. Typing can not be grouped. Operations in different cell inplace editors cannot be grouped.

These methods must be called for the editor where these operations are performed (usually TopLevelEditor)

hmtoggle_arrow1Example (inserting picture and resizing it 100x100):

Each call of SetUndoGroupMode(True) should be followed by exactly one call of SetUndoGroupMode(False).

Calls can be nested.

Grouping operations for undo, an alternative way

There is one more way to group operation for undo. It is less efficient, but allows grouping operations in multiple table cells.

Use BeginUndoGroup2..EndUndoGroup2 to give a standard name to the group, or use BeginUndoCustomGroup2..EndUndoCustomGroup2 to give a custom name. Unlike the methods described in the previous section, these methods should be called for the root editor, not for cell inplace editors.

hmtoggle_arrow1Example (replace all occurrences of FindText ro ReplaceText):

See also...

Methods introduced in TRichView and undo: see Viewer vs Editor