Skip to content

Instantly share code, notes, and snippets.

@arufian
Created September 4, 2012 05:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arufian/3616801 to your computer and use it in GitHub Desktop.
Save arufian/3616801 to your computer and use it in GitHub Desktop.
Click detection on button inside NSCell
- (BOOL) trackMouse:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag
{
// Check if the button was hit.
int hitType = [self hitTestForEvent:[NSApp currentEvent] inRect:cellFrame ofView:controlView];
BOOL isButtonClicked = hitType == (NSCellHitContentArea | NSCellHitTrackableArea);
if (!isButtonClicked) return YES;
// Ignore events other than mouse down.
if ([event type] != NSLeftMouseDown) return YES;
// Grab all events until a mouse up event.
while ([event type] != NSLeftMouseUp) {
// Check if link arrow was hit.
hitType = [self hitTestForEvent:[NSApp currentEvent] inRect:cellFrame ofView:controlView];
isButtonClicked = hitType == (NSCellHitContentArea | NSCellHitTrackableArea);
// Update the cell's display.
[controlView setNeedsDisplayInRect:cellFrame];
// Pass event if not hit.
if (!isButtonClicked) {
[NSApp sendEvent:event];
}
// Grab next event.
event = [[controlView window] nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSMouseEnteredMask | NSMouseExitedMask)];
}
// Perform click only if the button was hit.
if (isButtonClicked) {
[aButton performClick:self];
}
return YES;
}
- (IBAction)clickButton:(id)sender
{
NSLog(@"here ... button was clicked");
}
[aButton setAction:@selector(clickButton:)]; // assign callback method after button clicked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment