/*
 iPhly: Radio Control on the iPhone
 
 Copyright (C) 2010-2011 Ari Krupnik & Associates
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License V2
 as published by the Free Software Foundation.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#import "MMSlider.h"

@implementation MMSlider

@synthesize minimumValue;
@synthesize maximumValue;

- (id)initWithFrame:(CGRect)frame min:(float)n max:(float)x {
    self = [super initWithFrame:frame];
    if (self) {
        minimumValue = n;
        maximumValue = x;
        
        track = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        CGRect trackFrame=[track frame];
        trackFrame.size.width = frame.size.width - THUMB_WIDTH;
        trackFrame.origin.x = THUMB_WIDTH / 2;
        trackFrame.origin.y = (frame.size.height-trackFrame.size.height)/2;
        [track setFrame:trackFrame];
        [self setBackgroundColor:[UIColor clearColor]];
        [self addSubview:track];
        
        [self addSubview:lowerLimit=[[[MMSliderThumb alloc] initWithParent:self andOrientation:MMSliderThumbOrientationAbove] autorelease]];
        [lowerLimit setValue:minimumValue];
        [self addSubview:upperLimit=[[[MMSliderThumb alloc] initWithParent:self andOrientation:MMSliderThumbOrientationAbove] autorelease]];
        [upperLimit setValue:maximumValue];
        [self addSubview:center=[[[MMSliderThumb alloc] initWithParent:self andOrientation:MMSliderThumbOrientationBelow] autorelease]];
        [center setValue:(minimumValue+maximumValue)/2];
        
        [lowerLimit setUpperBound:center];
        [center setLowerBound:lowerLimit];
        [center setUpperBound:upperLimit];
        [upperLimit setLowerBound:center];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    return [self initWithFrame:frame min:0 max:1];
}

- (void)setFormat:(NSString*(*)(float value)) format {
    [lowerLimit setFormat:format];
    [center setFormat:format];
    [upperLimit setFormat:format];
}

- (float)lowerLimit {
    return [lowerLimit value];
}
- (void)setLowerLimit:(float)v {
    [lowerLimit setValue:v];
}
- (float)value {
    return [center value];
}
- (void)setValue:(float)v {
    [center setValue:v];
}
- (float)upperLimit {
    return [upperLimit value];
}
- (void)setUpperLimit:(float)v {
    [upperLimit setValue:v];
}


- (void)dealloc {
    [track release];
    [super dealloc];
}

@end
