00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPControl.j"
00024
00025
00026 var CPSliderHorizontalKnobImage = nil,
00027 CPSliderHorizontalBarLeftImage = nil,
00028 CPSliderHorizontalBarRightImage = nil,
00029 CPSliderHorizontalBarCenterImage = nil;
00030
00035 @implementation CPSlider : CPControl
00036 {
00037 double _minValue;
00038 double _maxValue;
00039 double _altIncrementValue;
00040 BOOL _isVertical;
00041
00042 CPView _bar;
00043 CPView _knob;
00044
00045 CPImageView _standardKnob;
00046 CPView _standardVerticalBar;
00047 CPView _standardHorizontalBar;
00048 }
00049
00050
00051
00052
00053 + (void)initialize
00054 {
00055 if (self != [CPSlider class])
00056 return;
00057
00058 var bundle = [CPBundle bundleForClass:self];
00059
00060 CPSliderKnobImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:"CPSlider/CPSliderKnobRegular.png"] size:CPSizeMake(11.0, 11.0)],
00061 CPSliderKnobPushedImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:"CPSlider/CPSliderKnobRegularPushed.png"] size:CPSizeMake(11.0, 11.0)],
00062 CPSliderHorizontalBarLeftImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:"CPSlider/CPSliderTrackHorizontalLeft.png"] size:CPSizeMake(2.0, 4.0)],
00063 CPSliderHorizontalBarRightImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:"CPSlider/CPSliderTrackHorizontalRight.png"] size:CPSizeMake(2.0, 4.0)],
00064 CPSliderHorizontalBarCenterImage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:"CPSlider/CPSliderTrackHorizontalCenter.png"] size:CPSizeMake(1.0, 4.0)];
00065 }
00066
00067 - (id)initWithFrame:(CGRect)aFrame
00068 {
00069 self = [super initWithFrame:aFrame];
00070
00071 if (self)
00072 {
00073 _value = 50.0;
00074 _minValue = 0.0;
00075 _maxValue = 100.0;
00076
00077 _bar = [self bar];
00078 _knob = [self knob];
00079 _knobSize = [[self knobImage] size];
00080 _isVertical = [self isVertical];
00081
00082 [self setContinuous:YES];
00083
00084 [_knob setFrameOrigin:[self knobPosition]];
00085
00086 [self addSubview:_bar];
00087 [self addSubview:_knob];
00088 }
00089
00090 return self;
00091 }
00092
00093 - (void)setFrameSize:(CGSize)aSize
00094 {
00095 if (aSize.height > 21.0)
00096 aSize.height = 21.0;
00097
00098 if (_isVertical != [self isVertical])
00099 {
00100 _isVertical = [self isVertical];
00101
00102 var bar = [self bar],
00103 knob = [self knob];
00104
00105 if (_bar != bar)
00106 [self replaceSubview:_bar = bar withView:_bar];
00107
00108 if (_knob != knob)
00109 {
00110 [self replaceSubview:knob withView:_knob];
00111
00112 _knob = knob;
00113 [_knob setFrameOrigin:[self knobPosition]];
00114 }
00115 }
00116
00117 [super setFrameSize:aSize];
00118
00119 [_knob setFrameOrigin:[self knobPosition]];
00120 }
00121
00126 - (double)altIncrementValue
00127 {
00128 return _altIncrementValue;
00129 }
00130
00134 - (void)setAltIncrementValue:(double)anIncrementValue
00135 {
00136 _altIncrementValue = anIncrementValue;
00137 }
00138
00142 - (BOOL)isContinuous
00143 {
00144 return (_sendActionOn & CPLeftMouseDraggedMask) != 0;
00145 }
00146
00150 - (void)setContinuous:(BOOL)flag
00151 {
00152 if (flag)
00153 _sendActionOn |= CPLeftMouseDraggedMask;
00154 else
00155 _sendActionOn &= ~CPLeftMouseDraggedMask;
00156 }
00157
00162 - (float)knobThickness
00163 {
00164 return CPRectGetWidth([_knob frame]);
00165 }
00166
00167
00168
00169
00170 - (CPImage)leftTrackImage
00171 {
00172 return CPSliderHorizontalBarLeftImage;
00173 }
00174
00175
00176
00177
00178 - (CPImage)rightTrackImage
00179 {
00180 return CPSliderHorizontalBarRightImage;
00181 }
00182
00183
00184
00185
00186 - (CPImage)centerTrackImage
00187 {
00188 return CPSliderHorizontalBarCenterImage
00189 }
00190
00191
00192
00193
00194 - (CPImage)knobImage
00195 {
00196 return CPSliderKnobImage;
00197 }
00198
00199
00200
00201
00202 - (CPImage)pushedKnobImage
00203 {
00204 return CPSliderKnobPushedImage;
00205 }
00206
00210 - (CPView)knob
00211 {
00212 if (!_standardKnob)
00213 {
00214 var knobImage = [self knobImage],
00215 knobSize = [knobImage size];
00216
00217 _standardKnob = [[CPImageView alloc] initWithFrame:CPRectMake(0.0, 0.0, knobSize.width, knobSize.height)];
00218
00219 [_standardKnob setImage:knobImage];
00220 }
00221
00222 return _standardKnob;
00223 }
00224
00228 - (CPView)bar
00229 {
00230
00231 if ([self isVertical])
00232 return nil;
00233 else
00234 {
00235 if (!_standardHorizontalBar)
00236 {
00237 var frame = [self frame],
00238 barFrame = CPRectMake(0.0, 0.0, CPRectGetWidth(frame), 4.0);
00239
00240 _standardHorizontalBar = [[CPView alloc] initWithFrame:barFrame];
00241
00242 [_standardHorizontalBar setBackgroundColor:[CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:
00243 [[self leftTrackImage], [self centerTrackImage], [self rightTrackImage]] isVertical:NO]]];
00244
00245 [_standardHorizontalBar setFrame:CPRectMake(0.0, (CPRectGetHeight(frame) - CPRectGetHeight(barFrame)) / 2.0, CPRectGetWidth(_isVertical ? barFrame : frame), CPRectGetHeight(_isVertical ? frame : barFrame))];
00246 [_standardHorizontalBar setAutoresizingMask:_isVertical ? CPViewHeightSizable : CPViewWidthSizable];
00247 }
00248
00249 return _standardHorizontalBar;
00250 }
00251 }
00252
00256 - (BOOL)isVertical
00257 {
00258 var frame = [self frame];
00259
00260 if (CPRectGetWidth(frame) == CPRectGetHeight(frame))
00261 return -1;
00262
00263 return CPRectGetWidth(frame) < CPRectGetHeight(frame);
00264 }
00265
00269 - (double)maxValue
00270 {
00271 return _maxValue;
00272 }
00273
00277 - (double)minValue
00278 {
00279 return _minValue;
00280 }
00281
00286 - (void)setMaxValue:(double)aMaxValue
00287 {
00288 _maxValue = aMaxValue;
00289 }
00290
00295 - (void)setMinValue:(double)aMinValue
00296 {
00297 _minValue = aMinValue;
00298 }
00299
00305 - (void)setValue:(double)aValue
00306 {
00307 [self setObjectValue:aValue];
00308 }
00309
00314 - (double)value
00315 {
00316 return [self floatValue];
00317 }
00318
00319 - (void)setObjectValue:(id)anObject
00320 {
00321 [super setObjectValue:anObject];
00322
00323 if (_knob)
00324 [_knob setFrameOrigin:[self knobPosition]];
00325 }
00326
00327
00328
00329
00330
00331 - (CGPoint)knobPosition
00332 {
00333 if ([self isVertical])
00334 return CPPointMake(0.0, 0.0);
00335 else
00336 return CPPointMake(
00337 (([self floatValue] - _minValue) / (_maxValue - _minValue)) * (CPRectGetWidth([self frame]) - CPRectGetWidth([_knob frame])),
00338 (CPRectGetHeight([self frame]) - CPRectGetHeight([_knob frame])) / 2.0);
00339 }
00340
00341
00342
00343
00344 - (float)valueForKnobPosition:(CGPoint)aPoint
00345 {
00346 if ([self isVertical])
00347 return 0.0;
00348 else
00349 return MAX(MIN((aPoint.x) * (_maxValue - _minValue) / ( CPRectGetWidth([self frame]) - CPRectGetWidth([_knob frame]) ) + _minValue, _maxValue), _minValue);
00350 }
00351
00352 - (CGPoint)constrainKnobPosition:(CGPoint)aPoint
00353 {
00354
00355 aPoint.x -= _knobSize.width / 2.0;
00356 return CPPointMake(MAX(MIN(CPRectGetWidth([self bounds]) - _knobSize.width, aPoint.x), 0.0), (CPRectGetHeight([self bounds]) - CPRectGetHeight([_knob frame])) / 2.0);
00357 }
00358
00359 - (void)mouseUp:(CPEvent)anEvent
00360 {
00361 [[self knob] setImage:[self knobImage]];
00362
00363 if ([_target respondsToSelector:@selector(sliderDidFinish:)])
00364 [_target sliderDidFinish:self];
00365
00366 if (_sendActionOn & CPLeftMouseUpMask)
00367 [self sendAction:_action to:_target];
00368
00369 }
00370
00371 - (void)mouseDown:(CPEvent)anEvent
00372 {
00373 [_knob setFrameOrigin:[self constrainKnobPosition:[self convertPoint:[anEvent locationInWindow] fromView:nil]]];
00374
00375 [super setObjectValue:[self valueForKnobPosition:[_knob frame].origin]];
00376
00377 [[self knob] setImage:[self pushedKnobImage]];
00378
00379 if (_sendActionOn & CPLeftMouseDraggedMask)
00380 [self sendAction:_action to:_target];
00381
00382 }
00383
00384 - (void)mouseDragged:(CPEvent)anEvent
00385 {
00386 [_knob setFrameOrigin:[self constrainKnobPosition:[self convertPoint:[anEvent locationInWindow] fromView:nil]]];
00387
00388 [super setObjectValue:[self valueForKnobPosition:[_knob frame].origin]];
00389
00390 if (_sendActionOn & CPLeftMouseDraggedMask)
00391 [self sendAction:_action to:_target];
00392
00393 }
00394
00395 @end
00396
00397 var CPSliderMinValueKey = "CPSliderMinValueKey",
00398 CPSliderMaxValueKey = "CPSliderMaxValueKey",
00399 CPSliderAltIncrValueKey = "CPSliderAltIncrValueKey",
00400 CPSliderIsVerticalKey = "CPSliderIsVerticalKey";
00401
00402 @implementation CPSlider (CPCoding)
00403
00409 - (id)initWithCoder:(CPCoder)aCoder
00410 {
00411 self = [super initWithCoder:aCoder];
00412
00413 if (self)
00414 {
00415 _minValue = [aCoder decodeDoubleForKey:CPSliderMinValueKey];
00416 _maxValue = [aCoder decodeDoubleForKey:CPSliderMaxValueKey];
00417 _altIncrementValue = [aCoder decodeDoubleForKey:CPSliderAltIncrValueKey];
00418 _isVertical = [aCoder decodeDoubleForKey:CPSliderIsVerticalKey];
00419
00420 _bar = [self bar];
00421 _knob = [self knob];
00422 _knobSize = [[self knobImage] size];
00423 _isVertical = [self isVertical];
00424
00425 [_knob setFrameOrigin:[self knobPosition]];
00426
00427 [self addSubview:_bar];
00428 [self addSubview:_knob];
00429 }
00430
00431 return self;
00432 }
00433
00438 - (void)encodeWithCoder:(CPCoder)aCoder
00439 {
00440 var subviews = _subviews;
00441
00442 _subviews = [];
00443
00444 [super encodeWithCoder:aCoder];
00445
00446 _subviews = subviews;
00447
00448 [aCoder encodeDouble:_minValue forKey:CPSliderMinValueKey];
00449 [aCoder encodeDouble:_maxValue forKey:CPSliderMaxValueKey];
00450 [aCoder encodeDouble:_altIncrementValue forKey:CPSliderAltIncrValueKey];
00451 [aCoder encodeBool:_isVertical forKey:CPSliderIsVerticalKey];
00452 }
00453
00454 @end