Created
April 18, 2009 23:17
-
-
Save dchest/97823 to your computer and use it in GitHub Desktop.
Detect clicked segment in NSSegmentedCell subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------------- CRSegmentedCell.h ------------------------------ | |
#import <Cocoa/Cocoa.h> | |
@interface CRSegmentedCell : NSSegmentedCell { | |
NSInteger highlightedSegment; | |
} | |
@property(assign) NSInteger highlightedSegment; | |
@end | |
// -------------------- CRSegmentedCell.m ------------------------------ | |
#import "CRSegmentedCell.h" | |
@implementation CRSegmentedCell | |
@synthesize highlightedSegment; | |
- (id)initWithCoder:(NSCoder *)decoder; | |
{ | |
if (![super initWithCoder:decoder]) | |
return nil; | |
[self setHighlightedSegment:-1]; | |
return self; | |
} | |
- (BOOL)trackMouse:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp; | |
{ | |
[self setHighlightedSegment:-1]; | |
NSPoint loc = [event locationInWindow]; | |
NSRect frame = cellFrame; | |
NSUInteger i = 0, count = [self segmentCount]; | |
loc = [controlView convertPoint:loc fromView:nil]; | |
while(i < count && frame.origin.x < cellFrame.size.width) { | |
frame.size.width = [self widthForSegment:i]; | |
if(NSMouseInRect(loc, frame, NO)) | |
{ | |
[self setHighlightedSegment:i]; | |
break; | |
} | |
frame.origin.x+=frame.size.width; | |
i++; | |
} | |
[controlView setNeedsDisplay:YES]; | |
return [super trackMouse:event inRect:cellFrame ofView:controlView untilMouseUp:untilMouseUp]; | |
} | |
- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag; | |
{ | |
[self setHighlightedSegment:-1]; | |
[super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!