Re: Out-of-order Undos and NSTextField [SOLVED, well sort of]
Re: Out-of-order Undos and NSTextField [SOLVED, well sort of]
- Subject: Re: Out-of-order Undos and NSTextField [SOLVED, well sort of]
- From: Ken Victor <email@hidden>
- Date: Tue, 11 Jul 2006 12:37:49 -0700
you might consider registering for one or more of the following notifications:
NSTextDidBeginEditingNotification
NSTextDidChangeNotification
NSTextDidEndEditingNotification
ken
At 12:05 PM -0700 7/11/06, James Bucanek wrote:
After messing around with this problem, I found an almost acceptable
solution. This is a bit hacky, and I fear there are unpredicatble
side-effect, but it seems to be working in my application OK.
The hack is to sub-class NSTextField, then intercept one of the
delegate messages sent to NSTextField from the NSTextView that gets
created as the field's editor. I wish there was a
textViewWillBeginEditing:, but I settled for
textView:shouldChangeTextInRange:replacementString: which gets
invoked whenever the user tries to add/remove/replace any text.
Before the message is passed on to the real NSTextField, I simply
turn off undo support in the field editor.
Hopefully this will solve someone else's problem, or at least
encourage someone to find a better solution.
------------- /Users/james/Desktop/UndoableTextField.h
//
// UndoableTextField.h
// QRecall
//
// Created by James Bucanek on 7/11/06.
//
#import <Cocoa/Cocoa.h>
//
// An NSTextField that does not use the undo manager.
//
// NSTextField/NSTextView has this nasty habid of registering undo
actions out of order.
// This is used in windows where the text field is bound to a
property that is already
// registering undo actions based on the changes in its value and
we don't need the field
// editor registering superfluous text edit changes at the same time.
//
// Notes:
// The object works by intercepting the
textView:shouldChangeTextInRange:replacementString:
// delegate message sent to the text field whenever the user tries
to change the text.
// Before passing the message on, it disables undo on the
NSTextView that is being used
// as the field editor.
// This won't work for more complex cases where the user changes the font or
// other attributes of the text first. It also might mess up other
fields that reuse
// the NSTextView object.
@interface UndoableTextField : NSTextField
- (BOOL)textView:(NSTextView*)textView
shouldChangeTextInRange:(NSRange)affectedCharRange
replacementString:(NSString*)replacementString;
@end
------------- /Users/james/Desktop/UndoableTextField.m
//
// UndoableTextField.m
// QRecall
//
// Created by James Bucanek on 7/11/06.
//
#import "UndoableTextField.h"
@implementation UndoableTextField
- (BOOL)textView:(NSTextView*)textView
shouldChangeTextInRange:(NSRange)affectedCharRange
replacementString:(NSString*)replacementString
{
[textView setAllowsUndo:NO]; // turn off Undo before the user
changes anything
// NSTextField should implement
textView:shouldChangeTextInRange:replacementString:, but just to be
on the safe side...
if ([super
respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementString:)])
return ([super textView:textView
shouldChangeTextInRange:affectedCharRange
replacementString:replacementString]);
return (YES);
}
@end
--
James Bucanek
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden