Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
Could anyone help understanding this issue.

Problem: Set up a tracking area in an existing view.


Hillegass Solution. In the -(void)viewDidMoveToWindow, the following code.


Code:
-(void) viewDidMoveToWindow
{
	int options = NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect ;
	NSTrackingArea *ta;
	ta = [[NSTrackingArea alloc] initWithRect:NSZeroRect
									  options:options
										owner:self
									 userInfo:nil];
	[self addTrackingArea:ta];
	[ta release];
}

Running this code using NSLog statements in -mouseEntered and -mouseExited methods, as soon as the mouse enters the view, and on leaving the view, I get the appropriate console messages. **Even** when changing the init to, for example, "initWithRect: NSMakeRect(0,0,10,10), I get the same response from the view ie the **entire** view is tracked.

So, my question is what is the purpose of the Rect in the init, OR why does NSZeroRect work at all?

Thanks, in advance, for any help.
 
Documentation: NSTrackingArea method -rect said:
Discussion
The rectangle is specified in the local coordinate system of the associated view. If the NSTrackingInVisibleRect option is specified, the receiver is automatically synchronized with changes in the view’s visible area (visibleRect) and the value returned from this method is ignored.
Could this be it?
 
Could this be it?

Yes!

Thanks....that's what happens if you don't read the entire documentation.

Commenting out this option does indeed set the area of tracking to the area specified in the rect parameter.

So, this works in drawRect if the option "NSTrackingInVisibleRect" is omitted.


Code:
- (void)drawRect:(NSRect)dirtyRect {
    
	[[self bgColor] set];
	
	NSRect bounds = [ self bounds];
	[NSBezierPath fillRect: bounds];
	
	if ( [[ self window] firstResponder] == self )
	{
		NSLog(@"View is first responder");
		[[NSColor keyboardFocusIndicatorColor] set];
		[NSBezierPath setDefaultLineWidth:4];
		[NSBezierPath strokeRect:bounds];
	}
	
	if ([self isHighlighted] == YES )
	{
		[[NSColor greenColor] set];
		[NSBezierPath setDefaultLineWidth:2];
		NSArray * trackingArea = [ self trackingAreas];
		if (trackingArea.count > 0)
		{
			NSTrackingArea * area = [trackingArea objectAtIndex:0];
			NSRect highlightArea = [area rect];
			[NSBezierPath strokeRect:highlightArea];
		}
	}
	
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.