Skip navigation.
 
mlRe:Making undo work with complicated NSTextView subclass
FROM : Shripada Hebbar
DATE : Mon Jan 07 07:13:10 2008

>
> I'm working on a core data document-based application. For it I 
> have created
> a very complicated NSTextView subclass (about 700 lines of code), 
> which
> allows for proper outlining (i.e., nested lists). The most 
> complicated part
> of this subclass is the keyDown method, which changes the NSTextStore
> primarily by changing the selection and calling [super keyDown], 
> setting
> attributes using setAttributes:range: on the text store, using several
> string changing methods on the NSTextStore's mutableString method 
> and in a
> few cases setting a new NSAttributedString using the  text store's
> setAttributedString method.
>
> All of these modifications muck with the text view's undo system, 
> and I've
> been unable to make it act nicely with them, with the result that 
> some undos
> throw errors (generally -[NSBigMutableString characterAtIndex:]: 
> Range or
> index out of bounds) while others proceed but restore the text 
> completely
> wrong. The apple documentation on subclassing
> NSTextView<http://developer.apple.com/documentation/Cocoa/

> Conceptual/TextEditing/Tasks/Subclassing.html>says:
> however calling these methods doesn't seem to fix my issues.
>
> My basic question is this: is there a way I can make NSTextView's 
> built-in
> undo manager work for me, or am I doomed to the unpleasant task of 
> handling
> it manually?
>
> Thanks for any help.
>
> --
> Micah Wylde

You need to take care  of undo and redoing of the additional behavior 
that you induce
into the text view.  You may have to consider grouping of undo 
operations as well. For instance
as you  mentioned you are doing lots of things in the subclass and 
later also happen to call on super class.
Now that the Undo manger asssociated with the view will have only the 
invocations of the super class and not at all your
subclass and thats the exact reason why it cant restore the states 
properly upon undoing.

Now what I think would solve this issue is by grouping together your 
custom invocations with that of the super class. Thus in the
case of say  'keydown' I would do something like this. Please note 
that this is just a pseudo  code and I am just passing some ideas.

===
-(void)keyDown:(NSEvent*)event
{
   [undoMgr beginUndoGrouping];
   
   //register here my undo invocation which would undo the operations I 
do in this method.

   //Do whatever is appropriate for this subclass here -any custom 
behavior

   //call on the super if needed.

   [undoMgr endUndoGrouping]

}
===


Regards
Shripada

Related mailsAuthorDate
mlMaking undo work with complicated NSTextView subclass Micah Wylde Jan 6, 11:12
mlRe:Making undo work with complicated NSTextView subclass Shripada Hebbar Jan 7, 07:13