00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <AppKit/CGGeometry.j>
00024 @import <AppKit/CPImageView.j>
00025 @import <AppKit/CPView.j>
00026
00027 #include "CoreGraphics/CGGeometry.h"
00028
00029
00030
00031
00032
00033
00034 CPProgressIndicatorBarStyle = 0;
00035
00036
00037
00038
00039 CPProgressIndicatorSpinningStyle = 1;
00040
00041
00042
00043
00044 CPProgressIndicatorHUDBarStyle = 2;
00045
00046 var CPProgressIndicatorSpinningStyleColors = nil,
00047
00048 CPProgressIndicatorClassName = nil,
00049 CPProgressIndicatorStyleIdentifiers = nil,
00050 CPProgressIndicatorStyleSizes = nil;
00051
00060 @implementation CPProgressIndicator : CPView
00061 {
00062 double _minValue;
00063 double _maxValue;
00064
00065 double _doubleValue;
00066
00067 CPControlSize _controlSize;
00068
00069 BOOL _isIndeterminate;
00070 CPProgressIndicatorStyle _style;
00071
00072 BOOL _isAnimating;
00073
00074 BOOL _isDisplayedWhenStoppedSet;
00075 BOOL _isDisplayedWhenStopped;
00076
00077 CPView _barView;
00078 }
00079
00080
00081
00082
00083 + (void)initialize
00084 {
00085 if (self != [CPProgressIndicator class])
00086 return;
00087
00088 var bundle = [CPBundle bundleForClass:self];
00089
00090 CPProgressIndicatorSpinningStyleColors = [];
00091
00092 CPProgressIndicatorSpinningStyleColors[CPMiniControlSize] = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:
00093 [bundle pathForResource:@"CPProgressIndicator/CPProgressIndicatorSpinningStyleRegular.gif"] size:CGSizeMake(64.0, 64.0)]];
00094 CPProgressIndicatorSpinningStyleColors[CPSmallControlSize] = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:
00095 [bundle pathForResource:@"CPProgressIndicator/CPProgressIndicatorSpinningStyleRegular.gif"] size:CGSizeMake(64.0, 64.0)]];
00096 CPProgressIndicatorSpinningStyleColors[CPRegularControlSize] = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:
00097 [bundle pathForResource:@"CPProgressIndicator/CPProgressIndicatorSpinningStyleRegular.gif"] size:CGSizeMake(64.0, 64.0)]];
00098
00099 CPProgressIndicatorBezelBorderViewPool = [];
00100
00101 var start = CPProgressIndicatorBarStyle,
00102 end = CPProgressIndicatorHUDBarStyle;
00103
00104 for (; start <= end; ++start)
00105 {
00106 CPProgressIndicatorBezelBorderViewPool[start] = [];
00107
00108 CPProgressIndicatorBezelBorderViewPool[start][CPMiniControlSize] = [];
00109 CPProgressIndicatorBezelBorderViewPool[start][CPSmallControlSize] = [];
00110 CPProgressIndicatorBezelBorderViewPool[start][CPRegularControlSize] = [];
00111 }
00112
00113 CPProgressIndicatorClassName = [self className];
00114
00115
00116 CPProgressIndicatorStyleIdentifiers = [];
00117
00118 CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle] = @"Bar";
00119 CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorSpinningStyle] = @"Spinny";
00120 CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle] = @"HUDBar";
00121
00122 var regularIdentifier = _CPControlIdentifierForControlSize(CPRegularControlSize),
00123 smallIdentifier = _CPControlIdentifierForControlSize(CPSmallControlSize),
00124 miniIdentifier = _CPControlIdentifierForControlSize(CPMiniControlSize);
00125
00126 CPProgressIndicatorStyleSizes = [];
00127
00128
00129 var prefix = CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle];
00130
00131 CPProgressIndicatorStyleSizes[prefix + regularIdentifier] = [_CGSizeMake(3.0, 15.0), _CGSizeMake(1.0, 15.0), _CGSizeMake(3.0, 15.0)];
00132 CPProgressIndicatorStyleSizes[prefix + smallIdentifier] = [_CGSizeMake(3.0, 15.0), _CGSizeMake(1.0, 15.0), _CGSizeMake(3.0, 15.0)];
00133 CPProgressIndicatorStyleSizes[prefix + miniIdentifier] = [_CGSizeMake(3.0, 15.0), _CGSizeMake(1.0, 15.0), _CGSizeMake(3.0, 15.0)];
00134
00135 prefix = CPProgressIndicatorClassName + @"Bar" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle];
00136
00137 CPProgressIndicatorStyleSizes[prefix + regularIdentifier] = _CGSizeMake(1.0, 9.0);
00138 CPProgressIndicatorStyleSizes[prefix + smallIdentifier] = _CGSizeMake(1.0, 9.0);
00139 CPProgressIndicatorStyleSizes[prefix + miniIdentifier] = _CGSizeMake(1.0, 9.0);
00140
00141
00142 prefix = CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle];
00143
00144 CPProgressIndicatorStyleSizes[prefix + regularIdentifier] = [_CGSizeMake(3.0, 15.0), _CGSizeMake(1.0, 15.0), _CGSizeMake(3.0, 15.0)];
00145 CPProgressIndicatorStyleSizes[prefix + smallIdentifier] = [_CGSizeMake(3.0, 15.0), _CGSizeMake(1.0, 15.0), _CGSizeMake(3.0, 15.0)];
00146 CPProgressIndicatorStyleSizes[prefix + miniIdentifier] = [_CGSizeMake(3.0, 15.0), _CGSizeMake(1.0, 15.0), _CGSizeMake(3.0, 15.0)];
00147
00148 prefix = CPProgressIndicatorClassName + @"Bar" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle];
00149
00150 CPProgressIndicatorStyleSizes[prefix + regularIdentifier] = _CGSizeMake(1.0, 9.0);
00151 CPProgressIndicatorStyleSizes[prefix + smallIdentifier] = _CGSizeMake(1.0, 9.0);
00152 CPProgressIndicatorStyleSizes[prefix + miniIdentifier] = _CGSizeMake(1.0, 9.0);
00153 }
00154
00155 - (id)initWithFrame:(CGRect)aFrame
00156 {
00157 self = [super initWithFrame:aFrame];
00158
00159 if (self)
00160 {
00161 _minValue = 0.0;
00162 _maxValue = 100.0;
00163
00164 _doubleValue = 0.0;
00165
00166 _style = CPProgressIndicatorBarStyle;
00167 _isDisplayedWhenStoppedSet = NO;
00168
00169 _controlSize = CPRegularControlSize;
00170
00171 [self updateBackgroundColor];
00172 [self drawBar];
00173 }
00174
00175 return self;
00176 }
00177
00178
00179
00180
00181 - (void)setUsesThreadedAnimation:(BOOL)aFlag
00182 {
00183 }
00184
00189 - (void)startAnimation:(id)aSender
00190 {
00191 _isAnimating = YES;
00192
00193 [self _hideOrDisplay];
00194 }
00195
00200 - (void)stopAnimation:(id)aSender
00201 {
00202 _isAnimating = NO;
00203
00204 [self _hideOrDisplay];
00205 }
00206
00210 - (BOOL)usesThreadedAnimation
00211 {
00212 return NO;
00213 }
00214
00215
00220 - (void)incrementBy:(double)aValue
00221 {
00222 [self setDoubleValue:_doubleValue + aValue];
00223 }
00224
00228 - (void)setDoubleValue:(double)aValue
00229 {
00230 _doubleValue = MIN(MAX(aValue, _minValue), _maxValue);
00231
00232 [self drawBar];
00233 }
00234
00238 - (double)doubleValue
00239 {
00240 return _doubleValue;
00241 }
00242
00247 - (void)setMinValue:(double)aValue
00248 {
00249 _minValue = aValue;
00250 }
00251
00255 - (double)minValue
00256 {
00257 return _minValue;
00258 }
00259
00264 - (void)setMaxValue:(double)aValue
00265 {
00266 _maxValue = aValue;
00267 }
00268
00272 - (double)maxValue
00273 {
00274 return _maxValue;
00275 }
00276
00277
00282 - (void)setControlSize:(CPControlSize)aControlSize
00283 {
00284 if (_controlSize == aControlSize)
00285 return;
00286
00287 _controlSize = aControlSize;
00288
00289 [self updateBackgroundColor];
00290 }
00291
00295 - (CPControlSize)controlSize
00296 {
00297 return _controlSize;
00298 }
00299
00300
00301
00302
00303 - (void)setControlTint:(CPControlTint)aControlTint
00304 {
00305 }
00306
00307
00308
00309
00310 - (CPControlTint)controlTint
00311 {
00312 return 0;
00313 }
00314
00315
00316
00317
00318 - (void)setBezeled:(BOOL)isBezeled
00319 {
00320 }
00321
00322
00323
00324
00325 - (BOOL)isBezeled
00326 {
00327 return YES;
00328 }
00329
00334 - (void)setIndeterminate:(BOOL)isIndeterminate
00335 {
00336 if (_isIndeterminate == isIndeterminate)
00337 return;
00338
00339 _isIndeterminate = isIndeterminate;
00340
00341 [self updateBackgroundColor];
00342 }
00343
00347 - (BOOL)isIndeterminate
00348 {
00349 return _isIndeterminate;
00350 }
00351
00356 - (void)setStyle:(CPProgressIndicatorStyle)aStyle
00357 {
00358 if (_style == aStyle)
00359 return;
00360
00361 _style = aStyle;
00362
00363 [self updateBackgroundColor];
00364 }
00365
00369 - (void)sizeToFit
00370 {
00371 if (_style == CPProgressIndicatorSpinningStyle)
00372 [self setFrameSize:[[CPProgressIndicatorSpinningStyleColors[_controlSize] patternImage] size]];
00373 else
00374 [self setFrameSize:CGSizeMake(CGRectGetWidth([self frame]), CPProgressIndicatorStyleSizes[
00375 CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle] +
00376 _CPControlIdentifierForControlSize(_controlSize)][0].height)];
00377 }
00378
00384 - (void)setDisplayedWhenStopped:(BOOL)isDisplayedWhenStopped
00385 {
00386 if (_isDisplayedWhenStoppedSet && _isDisplayedWhenStopped == isDisplayedWhenStopped)
00387 return;
00388
00389 _isDisplayedWhenStoppedSet = YES;
00390
00391 _isDisplayedWhenStopped = isDisplayedWhenStopped;
00392
00393 [self _hideOrDisplay];
00394 }
00395
00399 - (BOOL)isDisplayedWhenStopped
00400 {
00401 if (_isDisplayedWhenStoppedSet)
00402 return _isDisplayedWhenStopped;
00403
00404 if (_style == CPProgressIndicatorBarStyle || _style == CPProgressIndicatorHUDBarStyle)
00405 return YES;
00406
00407 return NO;
00408 }
00409
00410
00411 - (void)_hideOrDisplay
00412 {
00413 [self setHidden:!_isAnimating && ![self isDisplayedWhenStopped]];
00414 }
00415
00416 - (void)setFrameSize:(CGSize)aSize
00417 {
00418 [super setFrameSize:aSize];
00419
00420 [self drawBar];
00421 }
00422
00423
00424 - (void)drawBar
00425 {
00426 if (_style == CPProgressIndicatorSpinningStyle)
00427 return;
00428
00429 if (!_barView)
00430 {
00431 _barView = [[CPView alloc] initWithFrame:CGRectMake(2.0, 2.0, 0.0, 9.0)];
00432
00433 [_barView setBackgroundColor:[CPColor redColor]];
00434
00435 [self addSubview:_barView];
00436 }
00437
00438 [_barView setBackgroundColor:_CPControlColorWithPatternImage(
00439 CPProgressIndicatorStyleSizes,
00440 CPProgressIndicatorClassName,
00441 @"Bar",
00442 CPProgressIndicatorStyleIdentifiers[_style],
00443 _CPControlIdentifierForControlSize(_controlSize))];
00444
00445 [_barView setFrameSize:CGSizeMake(CGRectGetWidth([self bounds]) * (_doubleValue - _minValue) / (_maxValue - _minValue) - 4.0, 9.0)];
00446 }
00447
00448
00449 - (void)updateBackgroundColor
00450 {
00451 if (YES)
00452 {
00453 if (_style == CPProgressIndicatorSpinningStyle)
00454 {
00455 [_barView removeFromSuperview];
00456
00457 _barView = nil;
00458
00459 [self setBackgroundColor:CPProgressIndicatorSpinningStyleColors[_controlSize]];
00460 }
00461 else
00462 {
00463 [self setBackgroundColor:_CPControlThreePartImagePattern(
00464 NO,
00465 CPProgressIndicatorStyleSizes,
00466 CPProgressIndicatorClassName,
00467 @"BezelBorder",
00468 CPProgressIndicatorStyleIdentifiers[_style],
00469 _CPControlIdentifierForControlSize(_controlSize))];
00470
00471 [self drawBar];
00472 }
00473 }
00474 else
00475 [self setBackgroundColor:nil];
00476 }
00477
00478 @end