Re: [Pyobjc-dev] Subclassing NSTextField
Brought to you by:
ronaldoussoren
From: <bb...@ma...> - 2002-12-17 20:08:14
|
On Tuesday, Dec 17, 2002, at 14:25 US/Eastern, Steven D. Arnold wrote: > Hi, > > I may be having several misconceptions here, but I've looked at several > sources without success, so I thought I'd ask. I want to examine the > keys > that are typed into an NSTextField, so I subclassed NSTextField in IB. > In > IB, I drew an NSTextField and set its custom class to my new class. > Then in > Main.py I defined the class as follows: > > class SearchTextField( AppKit.NSTextField ): > > #def init( self ): > # print "initializing SearchTextField" > # return self > > def keyDown_( self, the_event ): > the_keys = the_event.characters() > print "Received keys: ", the_keys > > It was my understanding that this should be sufficient; the new > SearchTextField should use the class I defined. But I don't see my > diagnostic message when I type. Editing of Text in the AppKit doesn't work quite like that. When a field becomes first responder & takes key, the window creates a field editor that is used to do the actual editing. The field editor more or less "floats above" the field. If you are looking for validation type behavior, you would want to create a custom Formatter. Given that your class is named 'SearchTextField', I'm guess you want to implement something like auto complettion and/or automatching as the user types? If that is the case, then have a look at the NSComboButton (or whatever it is called) as it support auto completion. If that doesn't address your needs, then you'll need to look at the text editing and control editing delegate protocols: @interface NSObject(NSControlSubclassDelegate) // These delegate and notification methods are sent from NSControl subclasses that allow text editing such as NSTextField and NSMatrix. The classes that need to send these have delegates. NSControl does not. - (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor; - (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor; - (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error; - (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error; - (BOOL)control:(NSControl *)control isValidObject:(id)obj; - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector; @end ...and.... @interface NSObject(NSTextDelegate) - (BOOL)textShouldBeginEditing:(NSText *)textObject; /* YES means do it */ - (BOOL)textShouldEndEditing:(NSText *)textObject; /* YES means do it */ - (void)textDidBeginEditing:(NSNotification *)notification; - (void)textDidEndEditing:(NSNotification *)notification; - (void)textDidChange:(NSNotification *)notification; /* Any keyDown or paste which changes the contents causes this */ The second only being applicable to NSText objects, I believe. In generaly, you wouldn't want to override -keyDown: on a NSTextField directly unless you really, really want to customize text entry behavior. > I also tried uncommenting the init function above, also with no effect > (i.e. > No diagnostic message when the app is starting up). I have it > commented out > because I assumed the standard init function the class inherits from > NSTextField was good enough. -init is not the designated initializer for NSView subclasses; generally, -initWithFrame: is. > On a related note, is there any mechanism to implement categories in > pyobjc? > I thought this question had been asked before, but I couldn't find it > in my > archive. Good question: Ronald? b.bum |