X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=OSX%2FInvertedSlider.m;h=6ac0b4196bb31f0d28cb9d1c88c9fc990485a9b1;hb=aa75c7476aeaa84cf3abc192b376a8b03c325213;hp=c4fd5e3740fd3ce28472a35a76dede73a743864c;hpb=f8cf5ac7b2f53510f80a0eaf286a25298be17bfe;p=xscreensaver diff --git a/OSX/InvertedSlider.m b/OSX/InvertedSlider.m index c4fd5e37..6ac0b419 100644 --- a/OSX/InvertedSlider.m +++ b/OSX/InvertedSlider.m @@ -1,16 +1,16 @@ -/* 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 -* the above copyright notice appear in all copies and that both that -* copyright notice and this permission notice appear in supporting -* documentation. No representations are made about the suitability of this -* software for any purpose. It is provided "as is" without express or -* implied warranty. -* -* This is a subclass of NSSlider that is flipped horizontally: -* the high value is on the left and the low value is on the right. -*/ +/* xscreensaver, Copyright (c) 2006-2015 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 + * the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation. No representations are made about the suitability of this + * software for any purpose. It is provided "as is" without express or + * implied warranty. + * + * This is a subclass of NSSlider that is flipped horizontally: + * the high value is on the left and the low value is on the right. + */ #import "InvertedSlider.h" @@ -49,111 +49,103 @@ return v2; } --(double) doubleValue; +#ifndef USE_IPHONE + +/* On MacOS, we have to transform the value on every entry and exit point + to this class. So, we implement doubleValue and setDoubleValue to + transform the value; and we then have to re-implement every getter and + setter in terms of those. There's no way to simply change how the + slider is displayed without mucking with the value inside of it. + */ + +-(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 - +-(float)floatValue { return (float) [self doubleValue]; } +-(int)intValue { return (int) [self doubleValue]; } +-(NSInteger)integerValue { return (NSInteger) [self doubleValue]; } +-(id)objectValue { return [NSNumber numberWithDouble:[self doubleValue]]; } - -/* Implement all accessor methods in terms of "doubleValue" above. - (Note that these normally exist only on MacOS, not on iOS.) - */ - --(float) floatValue; +-(NSString *)stringValue { - return (float) [self doubleValue]; + if (integers) + return [NSString stringWithFormat:@"%d", [self intValue]]; + else + return [NSString stringWithFormat:@"%f", [self doubleValue]]; } --(int) intValue; -{ - return (int) [self doubleValue]; -} - --(id) objectValue; +- (NSAttributedString *)attributedStringValue; { - return [NSNumber numberWithDouble:[self doubleValue]]; + return [[[NSAttributedString alloc] initWithString:[self stringValue]] + autorelease]; +} + +-(void)setFloatValue:(float)v { [self setDoubleValue: (double) v]; } +-(void)setIntValue: (int)v { [self setDoubleValue: (double) v]; } +-(void)setIntegerValue:(NSInteger)v { [self setDoubleValue: (double) v]; } +-(void)setStringValue:(NSString *)v { [self setDoubleValue: [v doubleValue]]; } +-(void)takeIntValueFrom:(id)f { [self setIntValue: [f intValue]]; } +-(void)takeFloatValueFrom:(id)f { [self setFloatValue: [f floatValue]]; } +-(void)takeDoubleValueFrom:(id)f { [self setDoubleValue: [f doubleValue]]; } +-(void)takeStringValueFrom:(id)f { [self setStringValue: [f stringValue]]; } +-(void)takeObjectValueFrom:(id)f { [self setObjectValue: [f objectValue]]; } +-(void)takeIntegerValueFrom:(id)f { [self setIntegerValue:[f integerValue]];} +-(void) setAttributedStringValue:(NSAttributedString *)v { + [self setStringValue:[v string]]; } --(NSString *) stringValue; +-(void) setObjectValue:(id )v { - return [NSString stringWithFormat:@"%f", [self floatValue]]; + NSAssert2((v == nil) || + [(NSObject *) v respondsToSelector:@selector(doubleValue)], + @"argument %@ to %s does not respond to doubleValue", + v, __PRETTY_FUNCTION__); + [self setDoubleValue:[((NSNumber *) v) doubleValue]]; } -- (NSAttributedString *)attributedStringValue; -{ - // #### "Build and Analyze" says this leaks. Unsure whether this is true. - return [[NSAttributedString alloc] initWithString:[self stringValue]]; -} +#else // USE_IPHONE +/* On iOS, we have control over how the value is displayed, but there's no + way to transform the value on input and output: if we wrap 'value' and + 'setValue' analagously to what we do on MacOS, things fail in weird + ways. Presumably some parts of the system are accessing the value + instance variable directly instead of going through the methods. -/* Implment all setter methods in terms of "setDoubleValue", above. + So the only way around this is to enforce that all of our calls into + this object use a new API: 'transformedValue' and 'setTransformedValue'. + The code in XScreenSaverConfigSheet uses that instead. */ --(void) setFloatValue:(float)v +- (CGRect)thumbRectForBounds:(CGRect)bounds + trackRect:(CGRect)rect + value:(float)value { - [self setDoubleValue:(double)v]; + CGRect thumb = [super thumbRectForBounds: bounds + trackRect: rect + value: [self transformValue:value]]; + if (inverted) + thumb.origin.x = rect.size.width - thumb.origin.x - thumb.size.width; + return thumb; } --(void) setIntValue:(int)v +-(double) transformedValue { - [self setDoubleValue:(double)v]; + return [self transformValue: [self value]]; } --(void) setObjectValue:(id )v +-(void) setTransformedValue:(double)v { - NSAssert2((v == nil) || - [(NSObject *) v respondsToSelector:@selector(doubleValue)], - @"argument %@ to %s does not respond to doubleValue", - v, __PRETTY_FUNCTION__); - [self setDoubleValue:[((NSNumber *) v) doubleValue]]; + [self setValue: [self transformValue: v]]; } --(void) setStringValue:(NSString *)v -{ - [self setDoubleValue:[v doubleValue]]; -} +#endif // USE_IPHONE --(void) setAttributedStringValue:(NSAttributedString *)v -{ - [self setStringValue:[v string]]; -} @end