Skip to content

Instantly share code, notes, and snippets.

@dchest
Created April 18, 2009 23:17
Show Gist options
  • Save dchest/97823 to your computer and use it in GitHub Desktop.
Save dchest/97823 to your computer and use it in GitHub Desktop.
Detect clicked segment in NSSegmentedCell subclass
// -------------------- 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
@panupan
Copy link

panupan commented Dec 12, 2011

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment