Hillegass page 275

Discussion in 'Mac Programming' started by mdeh, Jan 18, 2010.

  1. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    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.
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    Could this be it?
     
  3. mdeh thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #3
    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];
    		}
    	}
    	
    }
    
    
     

Share This Page