Cocoa: Subclassing NSProgressIndicator on Lion

1.4k Views Asked by At

I have a problem that is driving me crazy. I want to subclass NSProgressIndicator in its "Bar" form to change the color of the progress bar based on a couple of logic states. For that i basically override the -drawRect: as usual. However a very strange thing happens on Lion. Even if my -drawRect just calls the superclass' implementation via [super drawRect:] and does nothing else at all the whole progress bar will be displayed using the style that was used before Lion. If you remember, with Lion the progress bar style has changes to a more flat and sleek look from the prior glassy one.

Again, just overriding -drawRect to do nothing but to call the superclass' implementation changes the style of the progress bar to Leopard's. Does anyone have a slightest idea of what is going on and how i can fix this?

2

There are 2 best solutions below

2
On

How about making the progress indicator layer-backed and applying a Core Image filter to recolour it?

0
On

CustomeProgressIndicator.h:

#import <Cocoa/Cocoa.h>

@interface CustomProgressIndicator : NSView {

    double m_minValue;
    double m_maxValue;
    double m_value;
}

- (void)setMaxValue:(double)newMaxValue;
- (void)setMinValue:(double)newMinValue;
- (void)setDoubleValue:(double)newDoubleValue;
- (void)drawRect:(NSRect)dirtyRect;   
@end

CustomProgressIndicator.m:

#import "CustomProgressIndicator.h"

@implementation CustomProgressIndicator

- (void)setMaxValue:(double)newMaxValue
{
    m_maxValue = newMaxValue;
}

- (void)setMinValue:(double)newMinValue
{
    m_minValue = newMinValue;
}

- (void)setDoubleValue:(double)newDoubleValue
{
    m_value = newDoubleValue;
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect
{
    [[NSColor grayColor] set];
    [NSBezierPath fillRect:dirtyRect];

    double width = ((m_value - m_minValue) / (m_maxValue - m_minValue));
    CGRect bar = dirtyRect;
    bar.size.width *= width;

    [[NSColor darkGrayColor] set];
    [NSBezierPath fillRect:bar];    
}