Re: addTrackingRect fails?
Re: addTrackingRect fails?
- Subject: Re: addTrackingRect fails?
- From: Greg Titus <email@hidden>
- Date: Thu, 28 Jun 2001 11:09:49 -0700
On Thursday, June 28, 2001, at 03:36 AM, Stiphane Sudre wrote:
>
On jeudi, juin 28, 2001, at 07:21 AM, Gregory Weston wrote:
>
> Are there any preconditions on creating tracking rectangles? I've got a
>
> view that attempts to create several in its initWithFrame: method, but
>
> the return from every call to addTrackingRect... is 0 (which seems
>
> wrong) and my mouseEntered and mouseExited methods never get invoked
>
> (which seems to support the notion of wrong-ness). Any hints?
>
>
>
> [self addTrackingRect: someRect owner:self userData:NULL
>
> assumeInside:NO]
>
>
IIRC you can't add a tracking rect in the initWithFrame: method.
>
>
There is also a problem which is not described in the NSView
>
documentation: when a view is removed from its superview, the tracking
>
rect is lost. This is a bit problematic when you have a NSTabView and
>
some view inside that have a tracking rect.
More specifically: You can't add tracking rectangles to a view unless it
is in a window, because the window is the thing which is actually
keeping track of them. You want to set up your tracking rects in the
-resetCursorRects method instead.
Here's an excerpt from the NSView documentation:
Tracking rectangles, though created and used by NSViews, are actually
maintained by NSWindows. Because of this, a tracking rectangle is a
static entity; it doesn't move or change its size when the NSView does.
If you use tracking rectangles, you should be sure to remove and
reestablish them any time you change the frame rectangle of the NSView
that contains them. If you're using a custom subclass of NSView, you can
override the frame- and bounds-setting methods to do this. You can also
register an observer for the NSViewFrameDidChangeNotification (described
below), and have it reestablish the tracking rectangles on receiving the
notification.
[...]
Because cursor rectangles need to be reset often as the NSView's size
and graphic elements change, NSView defines a single method,
resetCursorRects, that's invoked any time its cursor rectangles need to
be reestablished. A concrete subclass overrides this method, invoking
addCursorRect:cursor: for each cursor rectangle it wishes to set.
Thereafter, the NSView's cursor rectangles can be rebuilt by invoking
NSWindow's invalidateCursorRectsForView: method. If you find you need to
temporarily remove a single cursor rectangle, you can do this with
removeCursorRect:cursor:. Be aware that resetCursorRects will
reestablish that rectangle, unless you implement it to do otherwise.
Hope this helps,
--Greg