NSSegmentedControl shading ... how?

I have NSSegmentedControl

one that must be selected before the user can proceed to the next step in the wizard. I can disable the next button, making the user guess that something is missing, but I would like to skip a red control, something like a tint or border, to get attention if the user presses NEXT without selecting an option on the control.

I have not found a single page on Google telling how to do this. Is it possible to do this in cocoa? how can i do this in one NSSegmentedControl

?

+3


source to share


1 answer


I am using a rendered segmented control in Tembo . The goal here is not to draw attention to the control, but to insert it into a colored navbar. In the upper right corner of the screenshot .

Instead of drawing a segmented control from scratch, I have a standard implementation in NSImage, which I then tint before drawing to the view.



The same principle can be used to draw attention to governance. You will need to call setNeedsDisplay whenever you change the color of the tints

@interface TintedSegmentedCell : NSSegmentedCell
{
    NSMutableDictionary *_frames;
}

@end


@implementation TintedSegmentedControl

@synthesize tintColor = _tintColor;

- (void)dealloc
{
    [_tintColor release], _tintColor = nil;

    [super dealloc];
}

+ (Class)cellClass
{
    return [TintedSegmentedCell class];
}

@end


@implementation TintedSegmentedCell

- (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)controlView
{
    [_frames setObject:[NSValue valueWithRect:frame] forKey:[NSNumber numberWithInteger:segment]];

    [super drawSegment:segment inFrame:frame withView:controlView];
}

- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view
{
    if ([view isKindOfClass:[TintedSegmentedControl class]]) {
        NSColor *tintColor = [(TintedSegmentedControl*)view tintColor];

        if (tintColor != nil) {
            NSRect bounds = frame;

            bounds.origin.x = 0;
            bounds.origin.y = 0;

            NSSize size = bounds.size;
            NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];

            NSInteger segmentCount = [self segmentCount];
            NSMutableDictionary *frames = [NSMutableDictionary dictionaryWithCapacity:segmentCount];

            _frames = frames;

            [image lockFocus];
            {
                [super drawWithFrame:bounds inView:view];
            }
            [image unlockFocus];

            NSImage *tintedImage = [[image hh_imageTintedWithColor:[NSColor blackColor]] hh_imageTintedWithColor:tintColor];

            [tintedImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

            NSImage *overlayImage = [[[NSImage alloc] initWithSize:size] autorelease];

            [overlayImage lockFocus];
            {
                _frames = nil;

                for (NSInteger segment = 0; segment < segmentCount; segment++) {
                    NSRect frameRect = [[frames objectForKey:[NSNumber numberWithInteger:segment]] rectValue];

                    [self drawSegment:segment inFrame:frameRect withView:view];
                }
            }
            [overlayImage unlockFocus];

            [overlayImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

            return;
        }
    }

    [super drawWithFrame:frame inView:view];
}

@end

      

+4


source







All Articles