2

I need a control similar to NSLevelIndicator in NSContinuousCapacityLevelIndicatorStyle, but with reverse coloring. With NSLevelIndicator, the colors are like this: Green up to the warning level, yellow from warning to critical level, red from critical level onwards. This is fine for, say, a volume control. But I have a value that corresponds to the filling of a gas tank: I want green for a full tank, yellow for warning and red for empty. I have not found any means to change the colors of NSLevelIndicator.

So, before I start to write my own custom control, is there a NSControl available somewhere that already does what I want (of course I googled before asking, but to no avail)?

Thanks for reading.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
Dirk
  • 2,335
  • 24
  • 36

2 Answers2

1

Subclass NSLevelIndicator and write your own - (void)drawRect:(NSRect)theRect

#import <Cocoa/Cocoa.h>


@interface PBLevelIndicator : NSLevelIndicator {

    unsigned int mPercent;
}


@end  

#import "PBLevelIndicator.h"


@implementation PBLevelIndicator
- (void)drawRect:(NSRect)theRect
{
    NSRect fillingRect = theRect;
    fillingRect.size.width = theRect.size.width*mPercent/100;   
    NSColor *indicatorColor;

    if( mPercent >= 99 )
    {
        indicatorColor = [NSColor greenColor];
    }
    else if (mPercent >50) 
    {
        indicatorColor = [NSColor yellowColor];
    }
    else
    {
        indicatorColor = [NSColor redColor];
    }
    [indicatorColor set];
    NSRectFill(fillingRect);
}

-(void) setPercentage:(unsigned int) inPercent
{
    mPercent = inPercent;
    [self setNeedsDisplay:YES];
}
@end
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • That is a good idea, thank you. I tried it, but the new class is not key value coding-compliant. I will look deeper into this and see if I get it fixed. – Dirk Feb 08 '13 at 09:13
1

NSLevelIndicator (at least nowadays) does this automatically for you if you set a warning level higher than the critical level!

andyvn22
  • 14,696
  • 1
  • 52
  • 74