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 CPProgressIndicatorBezelBorderViewPool[start][CPMiniControlSize] = [];
00108 CPProgressIndicatorBezelBorderViewPool[start][CPSmallControlSize] = [];
00109 CPProgressIndicatorBezelBorderViewPool[start][CPRegularControlSize] = [];
00110 }
00111
00112 CPProgressIndicatorClassName = [self className];
00113 CPProgressIndicatorStyleIdentifiers = [];
00114
00115 CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle] = @"Bar";
00116 CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorSpinningStyle] = @"Spinny";
00117 CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle] = @"HUDBar";
00118
00119 var regularIdentifier = _CPControlIdentifierForControlSize(CPRegularControlSize),
00120 smallIdentifier = _CPControlIdentifierForControlSize(CPSmallControlSize),
00121 miniIdentifier = _CPControlIdentifierForControlSize(CPMiniControlSize);
00122
00123 CPProgressIndicatorStyleSizes = [];
00124
00125
00126 var prefixes = [
00127 CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle],
00128 CPProgressIndicatorClassName + @"Bar" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle],
00129 CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle],
00130 CPProgressIndicatorClassName + @"Bar" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle]
00131 ];
00132
00133 for (var i = 0, count = prefixes.length; i<count; i++)
00134 {
00135 var prefix = prefixes[i];
00136 CPProgressIndicatorStyleSizes[prefix + regularIdentifier] = [_CGSizeMake(3.0, 16.0), _CGSizeMake(1.0, 16.0), _CGSizeMake(3.0, 16.0)];
00137 CPProgressIndicatorStyleSizes[prefix + smallIdentifier] = [_CGSizeMake(3.0, 16.0), _CGSizeMake(1.0, 16.0), _CGSizeMake(3.0, 16.0)];
00138 CPProgressIndicatorStyleSizes[prefix + miniIdentifier] = [_CGSizeMake(3.0, 16.0), _CGSizeMake(1.0, 16.0), _CGSizeMake(3.0, 16.0)];
00139 }
00140 }
00141
00142 - (id)initWithFrame:(CGRect)aFrame
00143 {
00144 self = [super initWithFrame:aFrame];
00145
00146 if (self)
00147 {
00148 _minValue = 0.0;
00149 _maxValue = 100.0;
00150
00151 _doubleValue = 0.0;
00152
00153 _style = CPProgressIndicatorBarStyle;
00154 _isDisplayedWhenStoppedSet = NO;
00155
00156 _controlSize = CPRegularControlSize;
00157
00158 [self updateBackgroundColor];
00159 [self drawBar];
00160 }
00161
00162 return self;
00163 }
00164
00165
00166
00167
00168 - (void)setUsesThreadedAnimation:(BOOL)aFlag
00169 {
00170 }
00171
00176 - (void)startAnimation:(id)aSender
00177 {
00178 _isAnimating = YES;
00179
00180 [self _hideOrDisplay];
00181 }
00182
00187 - (void)stopAnimation:(id)aSender
00188 {
00189 _isAnimating = NO;
00190
00191 [self _hideOrDisplay];
00192 }
00193
00197 - (BOOL)usesThreadedAnimation
00198 {
00199 return NO;
00200 }
00201
00202
00207 - (void)incrementBy:(double)aValue
00208 {
00209 [self setDoubleValue:_doubleValue + aValue];
00210 }
00211
00215 - (void)setDoubleValue:(double)aValue
00216 {
00217 _doubleValue = MIN(MAX(aValue, _minValue), _maxValue);
00218
00219 [self drawBar];
00220 }
00221
00225 - (double)doubleValue
00226 {
00227 return _doubleValue;
00228 }
00229
00234 - (void)setMinValue:(double)aValue
00235 {
00236 _minValue = aValue;
00237 }
00238
00242 - (double)minValue
00243 {
00244 return _minValue;
00245 }
00246
00251 - (void)setMaxValue:(double)aValue
00252 {
00253 _maxValue = aValue;
00254 }
00255
00259 - (double)maxValue
00260 {
00261 return _maxValue;
00262 }
00263
00264
00269 - (void)setControlSize:(CPControlSize)aControlSize
00270 {
00271 if (_controlSize == aControlSize)
00272 return;
00273
00274 _controlSize = aControlSize;
00275
00276 [self updateBackgroundColor];
00277 }
00278
00282 - (CPControlSize)controlSize
00283 {
00284 return _controlSize;
00285 }
00286
00287
00288
00289
00290 - (void)setControlTint:(CPControlTint)aControlTint
00291 {
00292 }
00293
00294
00295
00296
00297 - (CPControlTint)controlTint
00298 {
00299 return 0;
00300 }
00301
00302
00303
00304
00305 - (void)setBezeled:(BOOL)isBezeled
00306 {
00307 }
00308
00309
00310
00311
00312 - (BOOL)isBezeled
00313 {
00314 return YES;
00315 }
00316
00321 - (void)setIndeterminate:(BOOL)isIndeterminate
00322 {
00323 if (_isIndeterminate == isIndeterminate)
00324 return;
00325
00326 _isIndeterminate = isIndeterminate;
00327
00328 [self updateBackgroundColor];
00329 }
00330
00334 - (BOOL)isIndeterminate
00335 {
00336 return _isIndeterminate;
00337 }
00338
00343 - (void)setStyle:(CPProgressIndicatorStyle)aStyle
00344 {
00345 if (_style == aStyle)
00346 return;
00347
00348 _style = aStyle;
00349
00350 [self updateBackgroundColor];
00351 }
00352
00356 - (void)sizeToFit
00357 {
00358 if (_style == CPProgressIndicatorSpinningStyle)
00359 [self setFrameSize:[[CPProgressIndicatorSpinningStyleColors[_controlSize] patternImage] size]];
00360 else
00361 [self setFrameSize:CGSizeMake(CGRectGetWidth([self frame]), CPProgressIndicatorStyleSizes[
00362 CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle] +
00363 _CPControlIdentifierForControlSize(_controlSize)][0].height)];
00364 }
00365
00371 - (void)setDisplayedWhenStopped:(BOOL)isDisplayedWhenStopped
00372 {
00373 if (_isDisplayedWhenStoppedSet && _isDisplayedWhenStopped == isDisplayedWhenStopped)
00374 return;
00375
00376 _isDisplayedWhenStoppedSet = YES;
00377
00378 _isDisplayedWhenStopped = isDisplayedWhenStopped;
00379
00380 [self _hideOrDisplay];
00381 }
00382
00386 - (BOOL)isDisplayedWhenStopped
00387 {
00388 if (_isDisplayedWhenStoppedSet)
00389 return _isDisplayedWhenStopped;
00390
00391 if (_style == CPProgressIndicatorBarStyle || _style == CPProgressIndicatorHUDBarStyle)
00392 return YES;
00393
00394 return NO;
00395 }
00396
00397
00398 - (void)_hideOrDisplay
00399 {
00400 [self setHidden:!_isAnimating && ![self isDisplayedWhenStopped]];
00401 }
00402
00403 - (void)setFrameSize:(CGSize)aSize
00404 {
00405 [super setFrameSize:aSize];
00406
00407 [self drawBar];
00408 }
00409
00410
00411 - (void)drawBar
00412 {
00413 if (_style == CPProgressIndicatorSpinningStyle)
00414 return;
00415
00416 if (!_barView)
00417 {
00418 _barView = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 16.0)];
00419 [self addSubview:_barView];
00420 }
00421
00422 [_barView setBackgroundColor:_CPControlThreePartImagePattern(
00423 NO,
00424 CPProgressIndicatorStyleSizes,
00425 CPProgressIndicatorClassName,
00426 @"Bar",
00427 CPProgressIndicatorStyleIdentifiers[_style],
00428 _CPControlIdentifierForControlSize(_controlSize))];
00429
00430 var width = CGRectGetWidth([self bounds]),
00431 barWidth = width * ((_doubleValue - _minValue) / (_maxValue - _minValue));
00432
00433 if (barWidth > 0.0 && barWidth < 4.0)
00434 barWidth = 4.0;
00435
00436 [_barView setFrameSize:CGSizeMake(barWidth, 16.0)];
00437 }
00438
00439
00440 - (void)updateBackgroundColor
00441 {
00442 if (YES)
00443 {
00444 if (_style == CPProgressIndicatorSpinningStyle)
00445 {
00446 [_barView removeFromSuperview];
00447
00448 _barView = nil;
00449
00450 [self setBackgroundColor:CPProgressIndicatorSpinningStyleColors[_controlSize]];
00451 }
00452 else
00453 {
00454 [self setBackgroundColor:_CPControlThreePartImagePattern(
00455 NO,
00456 CPProgressIndicatorStyleSizes,
00457 CPProgressIndicatorClassName,
00458 @"BezelBorder",
00459 CPProgressIndicatorStyleIdentifiers[_style],
00460 _CPControlIdentifierForControlSize(_controlSize))];
00461
00462 [self drawBar];
00463 }
00464 }
00465 else
00466 [self setBackgroundColor:nil];
00467 }
00468
00469 @end