Graphics Reference
In-Depth Information
The key issue here is that we need to determine when the mouse has left the button.
However, tracking the mouse all the time throughout the entire window is processor-
intensive, so you don't want to do that; we want to limit mouse tracking as much as
possible. To accomplish this, add an NSTrackingArea to the contentView when the
mouseDown event is received, and we limit this NSTrackingArea to the rectangle containing
the button that was pressed.
The AppDelegate is set within the NSTrackingArea as the owner of the area, which tells
the area to notify AppDelegate whenever the mouse enters or exits the rectangle, but only
when we are the active application. We also tell NSTrackingArea to assume it is starting
within the rectangle so that the first event we receive is when the mouse exits.
In addition to adding an NSTrackingArea , we also keep a pointer to the button that was
pressed. This pointer is used in other methods to turn the selection of the button on and
off and to properly handle the mouseUp event.
This addition to the -mouseDown: method causes two other methods to be called:
-mouseExited: and -mouseEntered: ; both are defined in Listing 11-12.
LISTING 11-12
AppDelegate -mouseExited: Implementation
- ( void )mouseExited:( NSEvent *)theEvent
{
[ selectedButton setSelected : NO ];
}
- ( void )mouseEntered:( NSEvent *)theEvent
{
[ selectedButton setSelected : YES ];
}
After adding these methods, the button that was pressed selects and unselects as the
mouse enters and exits its rectangle. This gives the user visual feedback while the mouse
button is down so that they know the button click can still be canceled.
LISTING 11-13
AppDelegate -mouseUp: Implementation
- ( void )mouseUp:( NSEvent *)theEvent;
{
if (! selectedButton ) return ;
[[ window contentView ] removeTrackingArea : buttonDownTrackingArea ];
[ selectedButton setSelected : NO ];
LZButtonLayer *hitLayer = [ self buttonLayerHit ];
if (hitLayer != selectedButton ) {
selectedButton = nil ;
return ;
}
 
Search WWH ::




Custom Search