Skip navigation.
 
mlRe: Out-of-order Undos and NSTextField [SOLVED, well sort of]
FROM : James Bucanek
DATE : Tue Jul 11 21:05:53 2006

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

Related mailsAuthorDate
mlOut-of-order Undos and NSTextField James Bucanek Jul 8, 18:48
mlRe: Out-of-order Undos and NSTextField James Bucanek Jul 10, 01:32
mlRe: Out-of-order Undos and NSTextField Ken Victor Jul 10, 19:08
mlRe: Out-of-order Undos and NSTextField Bill Cheeseman Jul 10, 19:57
mlRe: Out-of-order Undos and NSTextField Ken Victor Jul 10, 21:29
mlRe: Out-of-order Undos and NSTextField Bill Cheeseman Jul 10, 21:53
mlRe: Out-of-order Undos and NSTextField James Bucanek Jul 10, 22:58
mlRe: Out-of-order Undos and NSTextField [SOLVED, well sort of] James Bucanek Jul 11, 21:05
mlRe: Out-of-order Undos and NSTextField [SOLVED, well sort of] Ken Victor Jul 11, 21:37
mlRe: Out-of-order Undos and NSTextField [SOLVED, well sort of] James Bucanek Jul 12, 00:04