Discussion:
NSTextView doesn't have a focus ring
(too old to reply)
Jøhnny Fävòrítê (it means "Wanton Sezhuan")
2005-07-11 04:39:01 UTC
Permalink
the main window of the app i'm working on is primarily composed of two
NSOutlineViews and an NSTextView. as you tab from view to view, the outline
views display a focus ring, but the text view does not. it seems kind of
disorienting: tab -> blue focus ring, tab -> blue focus ring, tab -> ...
(nothing).

as far as i can see, there's no way to make NSTextView display a focus ring
when it has the input focus. is that correct, or have i missed something
obvious? i tried every flavor of [NSView setFocusRingType:] call i could
think of, but none of them made the view display a focus ring. does anybody
have a link to example code that would give this ability to the view?

alternately, is there some HIG-type argument for why i shouldn't be trying to
do this?
Michael Ash
2005-07-11 06:22:08 UTC
Permalink
Post by Jøhnny Fävòrítê (it means "Wanton Sezhuan")
the main window of the app i'm working on is primarily composed of two
NSOutlineViews and an NSTextView. as you tab from view to view, the outline
views display a focus ring, but the text view does not. it seems kind of
disorienting: tab -> blue focus ring, tab -> blue focus ring, tab -> ...
(nothing).
as far as i can see, there's no way to make NSTextView display a focus ring
when it has the input focus. is that correct, or have i missed something
obvious? i tried every flavor of [NSView setFocusRingType:] call i could
think of, but none of them made the view display a focus ring. does anybody
have a link to example code that would give this ability to the view?
alternately, is there some HIG-type argument for why i shouldn't be trying to
do this?
I don't know if there's a reason why you shouldn't do it, but I can help
you do it.

Your text view is embedded in a scroll view, so you almost certainly want
te draw the focus ring around the scroll view, not the text view. Just
knowing that is probably enough to get it working.

If setFocusRingType: on the scroll view doesn't work (I no longer remember
if it does), then you'll have to subclass NSScrollView and put some code
like this in it:

- (void)drawRect:(NSRect rect)
[super drawRect:rect];

NSSetFocusRingStyle(NSFocusRingOnly);
NSRectFill([self bounds]);
}

Of course, you should conditionalize that drawing based on whether your
text view is first responder, whether the window and app are frontmost,
etc.
Jøhnny Fävòrítê (it means "Wanton Sezhuan")
2005-07-11 11:28:01 UTC
Permalink
Post by Michael Ash
If setFocusRingType: on the scroll view doesn't work (I no longer
remember if it does),
it doesn't, i tried that.
Post by Michael Ash
then you'll have to subclass NSScrollView and put some code
- (void)drawRect:(NSRect rect)
[super drawRect:rect];
NSSetFocusRingStyle(NSFocusRingOnly);
NSRectFill([self bounds]);
}
Of course, you should conditionalize that drawing based on whether
your text view is first responder, whether the window and app are
frontmost, etc.
i finally got it to work. for those following along at home, it was one of
those problems that just won't lay down and die, it keeps popping up new
details everywhere. to wit:

- override [NSTextView becomeFirstResponder] and
[NSTextView resignFirstResponder], so you can invalidate the focus
area at those times

- set up the scroll view to receive notifications when its parent window gains
and loses key status, so you can invalidate the focus area

- override [NSScrollView resizeSubviewsWithOldSize:], again so you can
invalidate the focus ring if the view size should happen to change while
you're focused

dang, why didn't apple just give NSTextView the ability to draw focus rings,
the way NSOutlineView can? harrumph.
Michael Ash
2005-07-11 17:09:15 UTC
Permalink
Post by Jøhnny Fävòrítê (it means "Wanton Sezhuan")
Post by Michael Ash
If setFocusRingType: on the scroll view doesn't work (I no longer
remember if it does),
it doesn't, i tried that.
Ah, of course not, because it never becomes first responder, and it
probably doesn't have any special code to check for stuff inside it. I
didn't think of that before.
Post by Jøhnny Fävòrítê (it means "Wanton Sezhuan")
i finally got it to work. for those following along at home, it was one of
those problems that just won't lay down and die, it keeps popping up new
- override [NSTextView becomeFirstResponder] and
[NSTextView resignFirstResponder], so you can invalidate the focus
area at those times
- set up the scroll view to receive notifications when its parent window gains
and loses key status, so you can invalidate the focus area
- override [NSScrollView resizeSubviewsWithOldSize:], again so you can
invalidate the focus ring if the view size should happen to change while
you're focused
dang, why didn't apple just give NSTextView the ability to draw focus rings,
the way NSOutlineView can? harrumph.
Well, I'm glad you got it working. Did you file an enhancement request
with Apple? This stuff *does* get changed, as evidenced by the fact that
NSTableView's focus ring support wasn't around until Panther.

Loading...