Re: understanding NSTextView undo?
Re: understanding NSTextView undo?
- Subject: Re: understanding NSTextView undo?
- From: Greg Titus <email@hidden>
- Date: Sat, 28 Dec 2002 14:17:21 -0800
On Saturday, December 28, 2002, at 12:30 PM, Jim Correia wrote:
I'm trying to understand the undo mechanism in NSTextView.
There doesn't seem to be much documentation about the mechanics. If
I've overlooked something important in the docs, can someone point me
at it?
Nope. It isn't documented. My comments are based on reverse engineering
what NSTextView does in order to do similar coalescing on my own.
Question 1:
It seems that undo of typing is coalesced. This presents a problem for
document applications because if you type, save, type, undo, it goes
back to past the save point. Is there a way to force the coalescing to
stop and start a new "group"? (This sounds like it should be a known
bug, but unless someone confirms that it is, I'll file a bug..)
Whenever you type the text system uses private methods on NSUndoManager
to examine the last undo invocation. If the last invocation is a typing
change on this text view (and you haven't changed the selection or
something in the meanwhile), then instead of adding a new undo
invocation, it simply does nothing - the existing topmost undo will
then undo back to when you first began typing.
So a couple possible ways to keep the text system from doing this is to:
1) Add your own undo invocation to the stack when you save (which
doesn't need to do anything). Then when you next make a typing change,
the text system looks at the top of the stack, sees it isn't a typing
change, and makes a new undo group. You could also use this "on save"
undo invocation call something that brings up a "you are about to go
back before this file was saved" warning dialog.
2) You could also do something when you save to simply convince the
text view that it should make a new typing "group". Probably the
easiest way is to simply change the selection (to anything different),
then change it back. NSTextView starts a new typing group if you change
the selection between typing characters.
Question 2:
How do I make transformations in NSTextView undoable? The docs just
say "This method must be invoked at the start of any sequence of
user-initiated editing changes...." but don't say that it handles the
undo for you. Poking around in the debugger, it appears that it does
(and further, if it returns yes, and I do not subsequently replace the
text, it puts the text view in an inconsistent state.)
So, is making an action undoable as simple as
[textView shouldChangeTextInRange: range replacementString: string];
// do replacement
[textView didChangeText];
It seems to be that simple, but I want to make sure I'm not missing
something important here.
Yep. It's that simple.
Also, without providing my own implementation of
shouldChangeTextInRange:replacementString: is it possible to affect
the range that will be selected on redo?
Imagine that a transform inserts
[macroStart] macroText [marcoEnd]
and leaves macroText selected so that the user can type a replacement
string.
But if I undo, then redo, the insertion point is left at the end of
the inserted text. It would be nice if the selection range were
preserved.
Yes, that would be nice.
I guess I would approach this by using your own controller object with
-insertMacro: and -deleteMacro: methods, and have them register each
other to undo each other's actions. You can set the selection range
appropriately in each instead of depending upon NSTextView's undo
management.
Hope this helps,
-Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.