Undo/Redo in RichViewEdit
|Top Previous Next|
(Introduced in version 1.3)
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.
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 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\Delphi\Editors\Editor 1\) has the suggested names of actions in RVUndoStr.pas.
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.
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.
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)
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.
Methods introduced in TRichView and undo: see Viewer vs Editor
TRichView © trichview.com