1 /* xscreensaver, Copyright (c) 2006-2012 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * This is a subclass of NSSlider that is flipped horizontally:
12 * the high value is on the left and the low value is on the right.
15 #import "InvertedSlider.h"
17 @implementation InvertedSlider
19 - (id) initWithFrame:(NSRect)r
21 self = [super initWithFrame:r];
28 - (id) initWithFrame:(NSRect)r inverted:(BOOL)_inv integers:(BOOL)_int
30 self = [self initWithFrame:r];
37 -(double) transformValue:(double) value
40 ? (int) (value + (value < 0 ? -0.5 : 0.5))
42 double low = [self minValue];
43 double high = [self maxValue];
44 double range = high - low;
45 double off = v2 - low;
47 v2 = low + (range - off);
48 // NSLog (@" ... %.1f -> %.1f [%.1f - %.1f]", value, v2, low, high);
52 -(double) doubleValue;
55 return [self transformValue:[self value]];
57 return [self transformValue:[super doubleValue]];
61 -(void) setDoubleValue:(double)v
64 return [super setValue:[self transformValue:v]];
66 return [super setDoubleValue:[self transformValue:v]];
73 - (void)setValue:(float)v animated:(BOOL)a
75 return [super setValue:[self transformValue:v] animated:a];
79 /* Draw the thumb in the right place by also inverting its position
80 relative to the track.
82 - (CGRect)thumbRectForBounds:(CGRect)bounds
83 trackRect:(CGRect)rect
86 CGRect thumb = [super thumbRectForBounds:bounds trackRect:rect value:value];
88 thumb.origin.x = rect.size.width - thumb.origin.x - thumb.size.width;
96 /* Implement all accessor methods in terms of "doubleValue" above.
97 (Note that these normally exist only on MacOS, not on iOS.)
102 return (float) [self doubleValue];
107 return (int) [self doubleValue];
112 return [NSNumber numberWithDouble:[self doubleValue]];
115 -(NSString *) stringValue;
117 return [NSString stringWithFormat:@"%f", [self floatValue]];
120 - (NSAttributedString *)attributedStringValue;
122 // #### "Build and Analyze" says this leaks. Unsure whether this is true.
123 return [[NSAttributedString alloc] initWithString:[self stringValue]];
127 /* Implment all setter methods in terms of "setDoubleValue", above.
130 -(void) setFloatValue:(float)v
132 [self setDoubleValue:(double)v];
135 -(void) setIntValue:(int)v
137 [self setDoubleValue:(double)v];
140 -(void) setObjectValue:(id <NSCopying>)v
142 NSAssert2((v == nil) ||
143 [(NSObject *) v respondsToSelector:@selector(doubleValue)],
144 @"argument %@ to %s does not respond to doubleValue",
145 v, __PRETTY_FUNCTION__);
146 [self setDoubleValue:[((NSNumber *) v) doubleValue]];
149 -(void) setStringValue:(NSString *)v
151 [self setDoubleValue:[v doubleValue]];
154 -(void) setAttributedStringValue:(NSAttributedString *)v
156 [self setStringValue:[v string]];