How Do I Add Undo/Redo Entries To the Edit Menu?

A Basic Example

Here is a very basic example of how you determine what to put in the Edit menu for Undo/Redo.

if undoCanUndo() then
   put "Undo" && undoUndoActionName() & "/Z|undo" into line 1 of theMenu
else
   -- Nothing to undo, disable the menu line
   put "(Undo/Z|undo" into line 1 of theMenu
end if

if undoCanRedo() then
   put "Redo" && undoRedoActionName() & "/cmd shift Z|redo" into line 2 of theMenu
else
    -- Nothing to redo, disable the menu line
   put "(Redo/cmd shift Z|redo" into line 2 of theMenu
end if

Let's look at how to use this in an actual edit menu.

Incorporating into an Edit Menu

Incorporating into an Edit Menu

To begin, edit the script of the menu group for your application (1).

Since Undo/Redo availability is constantly changing you will want to build your Edit menu dynamically when needed. LiveCode sends a mouseDown message to your menu group whenever it is going to be displayed to the user. This is an appropriate place to build your Edit menu (2).

Create Menu

Create Menu

When creating the Edit menu you can use the Undo Manager API to determine what should be displayed for Undo/Redo entries. The API provides four calls that will help you with this:

  • undoCanUndo()
  • undoCanRedo()
  • undoUndoActionName()
  • undoRedoActionName()

The first two simply return boolean values as to whether or not undo actions are in the undo or redo queue (1). Use these functions to determine if the menu items should be enabled or not.

The second two return the string that the developer passed in when adding the undo action to the queue (2). This is descriptive text that might say something like "Edit Text". You can add this to your menu items so that the menu shows something like "Undo Edit Text".

The CreateEditMenu Code

on CreateEditMenu
   local theMenu

   if undoCanUndo() then
      put "Undo" && undoUndoActionName() & "/Z|undo" & cr after theMenu
   else
      -- Nothing to undo, disable the menu line
      put "(Undo/Z|undo" & cr after theMenu
   end if

   if undoCanRedo() then
      put "Redo" && undoRedoActionName() & "/cmd shift Z|redo" & cr after theMenu
   else
      -- Nothing to redo, disable the menu line
      put "(Redo/cmd shift Z|redo" & cr after theMenu
   end if

   put "Cu&t/X" & cr & \
         "&Copy/C" & cr & \
         "&Paste/V" & cr & \
         "Clea&r" & cr  & \
         "-" & cr & \
         "Preferences" after theMenu

   set the text of button "Edit" of me to theMenu
end CreateEditMenu

Handle menuPick

on menuPick pItemName
   switch pItemName
      case "undo"
         if undoCanUndo() then
            undoUndo
         end if
         break
      case "redo"
         if undoCanRedo() then
            undoRedo
         end if
         break 
   end switch
end menuPick

In the script of the Edit button itself you will need to handle menuPick message. All that is required is to call undoUndo or undoRedo. Calling either one will fire off the appropriate messages for undoing an action.

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.