Re: How to draw keyboard focus ring around a custom control?
Re: How to draw keyboard focus ring around a custom control?
- Subject: Re: How to draw keyboard focus ring around a custom control?
- From: Ricky Sharp <email@hidden>
- Date: Wed, 11 May 2005 15:21:05 -0500
On May 11, 2005, at 3:06 PM, Sean McBride wrote:
Hi all,
I have a custom NSView subclass and want to draw a focus ring
around it
when it's first responder. I've searched the archives and web and
found
many conflicting ways of doing it. Most involve a C function
NSSetFocusRingStyle(), which I find weird. I see new in 10.3 is
setFocusRingType: but I don't see anything like drawFocusRing:.
(For the
life of me, I can't find any definitive Apple docs on this subject.
Grr.
Must, resist, ugre to rant :) )
In Carbon there is a nice function, DrawThemeFocusRect(), that you
give a
rect and it draws the focus ring.
Surely there is an easy way in Cocoa? :)
I cheated a bit with all my custom controls/views. The size of my
items always take into account the focus ring area (and thus I didn't
need to worry about extending the clipping area, etc.)
Anyhow, rendering it is pretty simple.
Here's what I do in NSCell subclasses:
- (void)drawInteriorWithFrame:(NSRect)aFrame inView:(NSView*)aView
{
... // draw the cell
// draw the focus ring
if ([self showsFirstResponder])
{
NSBezierPath* theFocusRingPath = [self focusRingPath];
[[self focusRingColor] set];
[theFocusRingPath stroke];
}
}
Here, I use a cached NSBezierPath and color. That's basically all
you need to do for rendering. Then, in your cell and control
subclasses, make sure provide an appropriate implementation for
acceptsFirstResponder.
My controls' impl looks like this:
- (BOOL)acceptsFirstResponder
{
return [[self cell] acceptsFirstResponder];
}
The cell's impl:
- (BOOL)acceptsFirstResponder
{
return [self allowsFocus] && [self isEnabled];
}
Basically, figure out if it's appropriate to show the focus. In my
case, my cell has 'enabled' and 'allowsFocus' attributes that I query.
Finally, add an appropriate impl of needsPanelToBecomeKey
For example:
- (BOOL)needsPanelToBecomeKey
{
return NO;
}
This will handle cases where you want the control to become the one
with focus upon a mouse click. I don't do this for my custom
buttons, but do it for things like lists, edit fields.
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
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