X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=OSX%2FInvertedSlider.m;h=c4fd5e3740fd3ce28472a35a76dede73a743864c;hb=f8cf5ac7b2f53510f80a0eaf286a25298be17bfe;hp=d291ffec27779d275e45bc9e85c7ec5507ea1a85;hpb=49f5b54f312fe4ac2e9bc47581a72451bd0e8439;p=xscreensaver diff --git a/OSX/InvertedSlider.m b/OSX/InvertedSlider.m index d291ffec..c4fd5e37 100644 --- a/OSX/InvertedSlider.m +++ b/OSX/InvertedSlider.m @@ -1,4 +1,4 @@ -/* xscreensaver, Copyright (c) 2006 Jamie Zawinski +/* xscreensaver, Copyright (c) 2006-2012 Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -16,29 +16,85 @@ @implementation InvertedSlider +- (id) initWithFrame:(NSRect)r +{ + self = [super initWithFrame:r]; + if (! self) return 0; + inverted = YES; + integers = NO; + return self; +} + +- (id) initWithFrame:(NSRect)r inverted:(BOOL)_inv integers:(BOOL)_int +{ + self = [self initWithFrame:r]; + inverted = _inv; + integers = _int; + return self; +} + + -(double) transformValue:(double) value { + double v2 = (integers + ? (int) (value + (value < 0 ? -0.5 : 0.5)) + : value); double low = [self minValue]; double high = [self maxValue]; double range = high - low; - double off = value - low; - double trans = low + (range - off); - // NSLog (@" ... %.1f -> %.1f [%.1f - %.1f]\n", value, trans, low, high); - return trans; + double off = v2 - low; + if (inverted) + v2 = low + (range - off); + // NSLog (@" ... %.1f -> %.1f [%.1f - %.1f]", value, v2, low, high); + return v2; } -(double) doubleValue; { +# ifdef USE_IPHONE + return [self transformValue:[self value]]; +# else return [self transformValue:[super doubleValue]]; +# endif } -(void) setDoubleValue:(double)v { +# ifdef USE_IPHONE + return [super setValue:[self transformValue:v]]; +# else return [super setDoubleValue:[self transformValue:v]]; +# endif } +#ifdef USE_IPHONE + +- (void)setValue:(float)v animated:(BOOL)a +{ + return [super setValue:[self transformValue:v] animated:a]; +} + + +/* Draw the thumb in the right place by also inverting its position + relative to the track. + */ +- (CGRect)thumbRectForBounds:(CGRect)bounds + trackRect:(CGRect)rect + value:(float)value +{ + CGRect thumb = [super thumbRectForBounds:bounds trackRect:rect value:value]; + if (inverted) + thumb.origin.x = rect.size.width - thumb.origin.x - thumb.size.width; + return thumb; +} + +#endif // !USE_IPHONE + + + /* Implement all accessor methods in terms of "doubleValue" above. + (Note that these normally exist only on MacOS, not on iOS.) */ -(float) floatValue; @@ -63,6 +119,7 @@ - (NSAttributedString *)attributedStringValue; { + // #### "Build and Analyze" says this leaks. Unsure whether this is true. return [[NSAttributedString alloc] initWithString:[self stringValue]]; } @@ -70,7 +127,7 @@ /* Implment all setter methods in terms of "setDoubleValue", above. */ --(void) setFloatValue:(double)v +-(void) setFloatValue:(float)v { [self setDoubleValue:(double)v]; }