Re: Controlling selection in NSTextFields
Re: Controlling selection in NSTextFields
- Subject: Re: Controlling selection in NSTextFields
- From: Rainer Brockerhoff <email@hidden>
- Date: Sat, 10 Nov 2001 23:02:41 -0200
At 17:56 -0600 10/11/2001, Erik M. Buck wrote:
> Well, the embedded NSText object is damnably difficult to get ahold
of, but I succeeded after subclassing both NSTextField AND
NSTextFieldCell and capturing the NSText object in some critical call
- but the process struck me as extraordinarily kludgy and cumbersome,
especially as IB seems to have no provision to change a field's cell
class.
[[myTextField window] fieldEditor:YES forObject:myTextField ]; // this is
not damnably difficult
...
There is also NSControl's
currentEditor
- (NSText *)currentEditor
Erik, thanks for the fast reply!
Oh boy, or rather, $%^&*. Over two hours of reading the NSControl
header AND html, and that one snuck right by me. Even after your
e-mail, I couldn't find it by eye and was already composing a pointed
response, when it occurred to me to search for it with PB's Find...
and there it was. I plead both non compos mentis and excess
hemoglobin in my caffeine circulation.
Anyway, why is it called a "field editor" in some contexts and a
"text object" in others? Just to trip me up? :-)
> So, I'm stuck. Is there any way of intercepting mouse
selection/insertion point movement while it happens, or will I be
forced to reimplement my own NSTextField (and NSTextFieldCell)
completely from scratch? Some other methods come to mind but are
equally distasteful.
I like
- (BOOL)textShouldBeginEditing:(NSText *)aTextObject
and/or
- (void)textDidBeginEditing:(NSNotification *)aNotification
and/or
acceptsFirstResponder
- (BOOL)acceptsFirstResponder
Those actually were the first I've tried. acceptsFirstResponder gets
called before the insertion point is set in the field... and I need
to get the insertion point _afterwards_ to correct it if necessary.
BTW, for whoever's coming in late to the discussion, I need to check
if the user has set the insertion point or the selection where I
don't want them, and to correct this immediately.
Both textShouldBeginEditing and textDidBeginEditing get called only
if the field's contents will actually be _modified_, not if the user
just clicks in the field or selects text in it. Frankly, I think they
should be called in that case, too - the names are misleading. The
way it stands they ought to be named textShouldBeModified and
textWasModified.
I also tried to implement:
- (void)mouseUp:(NSEvent*)theEvent
in my NSTextField subclass, this would of course allow me to check
the selection after the user finishes messing around with it, but it
turns out mouseUp is not called on a mouse click. The NSView docs
just mention mouse-down events, never mouse-up events; I at first
supposed they're disabled by default as they were in previous Mac
OS's, and needed to be turned on by some obscure API.
Before sending this in I tried implementing mouseDown. This actually
gets called when the left mouse button goes down (surprise!), but
checking the selection at that point shows the whole field selected -
it seems the insertion point is indeed set later on, by whoever
swallows the mouse-up event.
If mouseUp actually worked it would be the ideal (and simple)
solution. But, on reflection, it seems that the mouseUp event is
probably swallowed by the field editor/text object, and not passed on
to its superiors. This might be a bug, but I need to code around it
anyway - since I do want my app to run on 10.1, and not on some
future release.
So the solution might be to subclass NSText (or NSTextView, actually)
and convince my NSTextField to use my NSTextView subclass as "field
editor". I'll try to find out how tomorrow... now for some sleep...
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"Originality is the art of concealing your sources."
http://www.brockerhoff.net/ (updated Oct. 2001)