beautypg.com

Using undo actions 17 – Apple Newton Programmer’s Newton 2.0 (for Newton 2.0) User Manual

Page 624

background image

C H A P T E R 1 7

Additional System Services

17-8

Using Additional System Services

Using Undo Actions

17

The following code example shows how to provide undo capability in a view.
Imagine you have a view that uses cards. The view has a particular method,

DeleteCard

, that it uses to delete a card. Within the

DeleteCard

method, you

call the

AddUndoAction

method, passing as its arguments the name of the card

deleted and a different method that will add the card (thereby undoing the delete
operation). Your call to

view

:

AddUndoAction

would look like the one in the

following code fragment:

DeleteCard: func(theCard)

begin

// statements that delete a card

...

// call AddCard as an undo action

:AddUndoAction ('AddCard, [theCard])

end,

You also need to supply the

AddCard

method, which would look similar to the

following example. It too provides an undo action—one that calls the original
action, thereby completing the Undo/Redo cycle.

AddCard: func(theCard)

begin

// statements that add a card

. . .

// call DeleteCard as an undo action

:AddUndoAction ('DeleteCard, [theCard])

end,

The Various Undo Methods

17

The

AddUndoAction

method ties an undo action to a specific view. If that view is

no longer open when the user taps Undo, the action does not take place. Because
an undo action should generally cause a visible change, it is often desirable to tie
the undo actions to views.

When it is not desirable or feasible to tie an undo action to an open view, you can
use the

AddUndoCall

or

AddUndoSend

functions to execute the undo action

unconditionally. For example, view methods usually use the

AddUndoAction

view method to post their undo action; however, if the view will not be open when
the user taps Undo, you may need to use the

AddUndoCall

or

AddUndoSend

functions to post the undo action. If your action relies on the view being open or
some other condition, it must test that condition explicitly.