Custom NSCell mouse events
Custom NSCell mouse events
- Subject: Custom NSCell mouse events
- From: Michael Hanna <email@hidden>
- Date: Thu, 10 Jun 2010 12:43:29 -0400
OK I'm really stumped on this one. I want to make a checkbox with a
NSTextFieldCell combined together. It's important that the checkbox
goes ON if the mouse hits the box, NOT the text. I've accomplished
this, more or less, but the issue is receiving the mouse event because
I click one checkbox in a row, but ALL of them turn to NSOnState. I
will show what I've done and my various failed attempts in order to
get this to work.
So this is how I've done it so far:
header:
@interface MyCheckboxCellToo : NSTextFieldCell {
NSButtonCell *_checkboxCell;
}
implementation:
- (NSUInteger)hitTestForEvent:(NSEvent *)event
inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
NSPoint point = [controlView convertPoint:[event locationInWindow]
fromView:nil];
NSRect checkFrame;
NSDivideRect(cellFrame, &checkFrame, &cellFrame,
cellFrame.size.height, NSMinXEdge);
if (NSMouseInRect(point, checkFrame, [controlView isFlipped])) {
// the checkbox, or the small region around it, was hit. so
let's flip the state
NSCellStateValue checkState = ([_checkboxCell state] ==
NSOnState) ? NSOffState:NSOnState;
[self setState:checkState];
[_checkboxCell setState:checkState];
[controlView setNeedsDisplay:YES];
return NSCellHitTrackableArea;
}
return [super hitTestForEvent:event inRect:cellFrame
ofView:controlView];
}
I know I probably shouldn't be doing:
[self setState:checkState];
[_checkboxCell setState:checkState];
[controlView setNeedsDisplay:YES];
in there... because the result is that EVERY checkbox in every goes to
NSOnState.
How do I handle the mouse event?
I have tried:
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame
ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp {
NSLog(@"%s %@", _cmd, theEvent);
return [_checkboxCell trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:untilMouseUp];
// return YES;
// return [super trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:untilMouseUp];
}
- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView {
NSLog(@"%s %@", _cmd, NSStringFromPoint(startPoint));
return [super startTrackingAt:startPoint inView:controlView];
}
- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint
inView:(NSView *)controlView {
NSLog(@"%s %@", _cmd, NSStringFromPoint(currentPoint));
return [super continueTracking:lastPoint at:currentPoint
inView:controlView];
}
- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint
inView:(NSView *)controlView mouseIsUp:(BOOL)flag {
NSLog(@"%s %d %@", _cmd, flag, NSStringFromPoint(stopPoint));
}
trackMouse: ... DOES gets called
but
startTrackingAt:..., continueTracking:..., and stopTracking:.... DO
NOT get called when I click on the checkbox "hit area"
in trackMouse:... I have tried
return [_checkboxCell trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:untilMouseUp];
and
return [super trackMouse:theEvent inRect:cellFrame
ofView:controlView untilMouseUp:untilMouseUp];
and neither seems to result in the mouse event being handled by the checkbox.
How do I get that single checkbox to go NSOnState? I know I'm pretty
close but after a lot of doc reading and google searching I haven't
been successful at solving this.
suggestions and comments welcome,
Michael
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden