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: James Bucanek <email@hidden>
- Date: Tue, 11 Jul 2006 15:04:03 -0700
Ken Victor wrote on Tuesday, July 11, 2006:
>you might consider registering for one or more of the following notifications:
> NSTextDidBeginEditingNotification
> NSTextDidChangeNotification
> NSTextDidEndEditingNotification
I originally thought of that and rejected it as a chicken-and-egg problem: How do you register for notifcations from an object (the field editor) before you know what that object is?
As I was about to compose my reply, it dawned on me that in Cocoa you don't need to know what the object is, you can register to receive all notifications of that type then compare the object to the one you're looking for after the fact. (Sorry, I still carry around a lot of Java patterns in my head.)
So, I tried it and it works! Much nicer solution, even if it turns out to be a lot more code. Let me know what you think:
------------- /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.
//
// 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 value and we don't need the field
// editor registering a superfluous bunch of text edit changes at the same time.
//
// Notes:
// The object works by listening for the did-begin-editing and did-end-editing notifications
// fired by NSTextViews being used as field editors. If one of these field editors begins
// editing with this text field as its delegate, the handler disables undo support
// until a matching did-end-editing notification is fired.
@interface UndoableTextField : NSTextField
{
@private
BOOL _undoDisabled;
BOOL _saveAllowsUndo;
}
- (id)initWithFrame:(NSRect)frameRect;
- (id)initWithCoder:(NSCoder*)decoder;
- (void)dealloc;
- (void)listenForEditingSessions;
- (void)textViewDidBeginEditing:(NSNotification*)notification;
- (void)textViewDidEndEditing:(NSNotification*)notification;
@end
------------- /Users/james/Desktop/UndoableTextField.m
//
// UndoableTextField.m
// QRecall
//
// Created by James Bucanek on 7/11/06.
//
#import "UndoableTextField.h"
@implementation UndoableTextField
- (id)initWithFrame:(NSRect)frameRect
{
if ( (self=[super initWithFrame:frameRect]) != nil )
{
[self listenForEditingSessions];
}
return (self);
}
- (id)initWithCoder:(NSCoder*)decoder;
{
if ( (self=[super initWithCoder:decoder]) != nil )
{
[self listenForEditingSessions];
}
return (self);
}
- (void)dealloc
{
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self
name:NSTextDidBeginEditingNotification
object:nil];
[notificationCenter removeObserver:self
name:NSTextDidEndEditingNotification
object:nil];
[super dealloc];
}
- (void)listenForEditingSessions
{
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(textViewDidBeginEditing:)
name:NSTextDidBeginEditingNotification
object:nil];
[notificationCenter addObserver:self
selector:@selector(textViewDidEndEditing:)
name:NSTextDidEndEditingNotification
object:nil];
}
- (void)textViewDidBeginEditing:(NSNotification*)notification
{
id object = [notification object];
if (!_undoDisabled
&& [object respondsToSelector:@selector(delegate)]
&& [object delegate]==self
&& [object respondsToSelector:@selector(allowsUndo)]
&& [object respondsToSelector:@selector(setAllowsUndo:)])
{
// NSLog(@"disabling undo on field editor");
_saveAllowsUndo = [object allowsUndo];
[object setAllowsUndo:NO];
_undoDisabled = YES;
}
}
- (void)textViewDidEndEditing:(NSNotification*)notification
{
id object = [notification object];
if (_undoDisabled
&& [object respondsToSelector:@selector(delegate)]
&& [object delegate]==self
&& [object respondsToSelector:@selector(setAllowsUndo:)])
{
// NSLog(@"reenabling undo on field editor");
[object setAllowsUndo:_saveAllowsUndo];
_undoDisabled = NO;
}
}
@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