Cursors using NSTrackingArea

Christiaan Hofman <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
I am having trouble setting a custom cursor for a rectangle in a custom view using the new(er) NSTrackingArea API.

What I want is simply the same result I got with the old cursor rect API, which is a custom cursor whenever the cursor is over a given rect in my subview (and the standard or appropriate cursor outside it). 

Here's the relevant part of my implementation, which is partly based on Apple's sample in the event handling guide (-cursorRect : returns an appropriate subrect of the view's bounds):

- (void)updateTrackingAreas {
	[super updateTrackingAreas];
	if (cursorArea) {
		[self removeTrackingArea:cursorArea];
		[cursorArea release];
	}
	cursorArea = [[NSTrackingArea alloc] initWithRect:[self cursorRect]
		options:NSTrackingCursorUpdate|NSTrackingActiveInActiveApp
		owner:self userInfo:nil];
	[self addTrackingArea:cursorArea];
}

- (void)cursorUpdate:(NSEvent *)event {
	if ([[event trackingArea] isEqual:cursorArea]) {
		[[NSCursor resizeLeftRightCursor] set];
	} else {
		[super cursorUpdate:event];
	}
}

This does not seem to work. I have two problems:
1. When I hover over the cursorRect, I often don't see the resize cursor. I seem to first have to click the view before it works, anyway it works very erratically.
2. When I leave the cursorRect (after it finally succeeded to set the cursor), the resize cursor remains and does not revert to the appropriate cursor.

What am I doing wrong? The documentation on the subject ( Cocoa Event Handling Guide, NSTrackingArea, NSView, NSWindow) does not give me sufficient info to figure it out. In fact, in their samples  the cursorUpdate: implementation is even simpler than this, as it does not check for the trackingArea (and that's wrong,. because then I really get the resize cursor for my whole view).

thanks,
Christiaan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.