Implementing an Undo routine

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Eric
Posts: 14
Joined: Thu Oct 01, 2009 9:16 am

Implementing an Undo routine

Post by Eric » Fri Jul 23, 2010 4:06 pm

I have a facility that enables users to delete fields from my main stack, using the list in a palette:

on deleteKey
` get the value of the selectedLine
` cut field it of card 1 of stack "organiser 4"
` send mouseup to button "List Fields" of card 1 of stack "organiser 4" -- updates the field list palette
end deleteKey

Now I'd like to add an undo routine that enables them to reinstate the field if they change their mind. Something along the lines of:

on undo
-- paste the recently cut field back into stack "organiser 4"
-- (update the list palette) send mouseup to button "List Fields" of card 1 of stack "organiser 4"
end undo

How do I modify the Rev menus so that choosing Edit:Undo, or typing Cmd-Z, will run this routine?

Thanks for your help,


Eric

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Implementing an Undo routine

Post by Mark » Sat Jul 24, 2010 9:55 am

Eric,

There is no reliable way to do this. If it hasn't been reported yet, report it as a bug to the QA center. It happens to me every day, that I accidentally delete an object and can't undo. It happens, because the property inspector appears to have focus, while in fact the stack window has focus. If I press the backspace button to delete the name of an object in the property inspector, the entire object is often deleted :-(

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Eric
Posts: 14
Joined: Thu Oct 01, 2009 9:16 am

Re: Implementing an Undo routine

Post by Eric » Tue Jul 27, 2010 2:33 pm

Thanks, Mark.

That's a bit remiss, don't you think?

It seems I CAN intercept the undoKey *- which, surely, is always Cmd-Z (or Ctrl+Z on Windows);

and it seems I can even intercept a message (undoChanged) when the action to be undone updates to some new action.

So why wouldn't Rev developers want me to intercept the menu item Edit:Undo?

Please explain, oh Rev team. Why not give out a message when the Undo menu command has been called?
Any particular reason? Or is this a simple oversight which you now intend to address?

_______________________________________________________________
*Here's my undo routine, anyway.

Code: Select all

global FieldJustDeleted

on deleteKey
   get the value of the selectedLine
   cut field it of card 1 of stack "organiser 4"
   send mouseup to button "List Fields" of card 1 of stack "organiser 4" -- updates the field list palette
end deleteKey

on UndoCmd -- (IF THE REV TEAM WOULD ONLY ALLOW IT)
   if FieldJustDeleted is in the clipboardData["objects"] then
      if the name of this stack is not "Organiser 4" then go to stack "Organiser 4"
      paste
   end if
   send mouseup to button "List Fields" of card 1 of stack "organiser 4"
end UndoCmd

on undoKey                          -- NB THIS KEY ONLY CALLED WHEN NOT IN DEVELOPMENT ENVIRONMENT
   undoCmd
end undoKey

on commandKeyDown theKey -- WITHIN DEVELOPMENT ENVIRONMENT, HOLD DOWN SHIFT KEY AND TYPE CMD-SHIFT-Z
   if theKey is "Z" then
      undoCmd
   else pass commandKeyDown
end commandKeyDown
Last edited by Eric on Tue Jul 27, 2010 11:08 pm, edited 2 times in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10045
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Implementing an Undo routine

Post by FourthWorld » Tue Jul 27, 2010 4:23 pm

Eric wrote:So why wouldn't Rev developers want me to intercept the menu item Edit:Undo?

Please explain, oh Rev team. Why not release a message when the Undo menu command is called?
Testing here my card gets a commandKeyDown message when I type Cmd-Z in the Rev 4.0 IDE.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Implementing an Undo routine

Post by Mark » Tue Jul 27, 2010 4:31 pm

Hi Eric,

You have posted an interesting script, but I wouldn't want to use it because you mess around with the clipboard. Sometimes, I keep valuable data in the clipboard and I would be rather upset if the undo command deleted the data in the clipboard.

I understood that you wanted to undo the deletion of fields in the IDE. Everything related to undo is unreliable in the IDE. However, you're free to write a script to undo changes in a standalone and it should be possible to do this reliably in a standalone.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Eric
Posts: 14
Joined: Thu Oct 01, 2009 9:16 am

Re: Implementing an Undo routine

Post by Eric » Tue Jul 27, 2010 11:07 pm

Well, I peek at the clipboard data - but I don't "mess around" with it beyond that. By convention, when you paste from the clipboard, the clipboard data remains unchanged so you might even paste again (a second, third or fourth copy - even if the original instruction had only been to "cut").

But you're right. I posted hastily, without fully implementing my alternative to pasting when the clipboard data may already have changed.

In this version, if the name of the deleted field (global FieldJustDeleted) is not found in the clipboard objects list, then it doesn't paste but instead reverts to the last version saved. I believe reverting still leaves your clipboard data intact?

Code: Select all

global FieldJustDeleted

on deleteKey
   get the value of the selectedLine
   put it into FieldJustDeleted
   save stack "organiser 4"
   cut field it of card 1 of stack "organiser 4"
   send mouseup to button "List Fields" of card 1 of stack "organiser 4" -- updates the field list palette
end deleteKey

on UndoCmd -- (IF THE REV TEAM WOULD ONLY ALLOW IT)
   if FieldJustDeleted is empty then pass UndoCmd

   if the name of this stack is not "Organiser 4" then go to stack "Organiser 4"
   if FieldJustDeleted is in the clipboardData["objects"] then
      paste
   else revert
   put empty into FieldJustDeleted
   send mouseup to button "List Fields" of card 1 of stack "organiser 4" -- updates the field list palette
end UndoCmd

on undoKey                          -- NB THIS KEY ONLY CALLED WHEN NOT IN DEVELOPMENT ENVIRONMENT
   undoCmd
end undoKey

--on commandKeyDown theKey -- WITHIN DEVELOPMENT ENVIRONMENT, HOLD DOWN SHIFT KEY AND TYPE CMD-SHIFT-Z
--   if theKey is "Z" then
--      undoCmd
--   else pass commandKeyDown
--end commandKeyDown

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: Implementing an Undo routine

Post by Curry » Wed Jul 28, 2010 2:28 am

By storing some info (properties) about the deleted field, you could later restore it with a new field using that info.
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

Eric
Posts: 14
Joined: Thu Oct 01, 2009 9:16 am

Re: Implementing an Undo routine

Post by Eric » Thu Jul 29, 2010 3:36 pm

Good idea.

But, however the routine were implemented, I would still prefer for it to be called when the user selects Edit:Undo from the menus.

Surely selecting any menu item ought to trigger a system message that one could intercept?

Alternatively, does anyone know how I could modify the Edit Menu and replace Undo with a custom command, say: "Edit:Undo Field Delete"?

Curry
Posts: 111
Joined: Mon Oct 15, 2007 11:34 pm
Contact:

Re: Implementing an Undo routine

Post by Curry » Fri Jul 30, 2010 7:21 am

You probably already know this, but just in case: You can make all your own menus for a stack/app and do whatever you want with them.

Or is your goal to modify the IDE?
Best wishes,

Curry Kenworthy

LiveCode Development, Training & Consulting
http://livecodeconsulting.com/

WordLib: Conquer MS Word & OpenOffice
SpreadLib: "Excel-lent" spreadsheet import/export
http://livecodeaddons.com/

Post Reply