API 0.9.5
AppKit/CPProgressIndicator.j
Go to the documentation of this file.
00001 /*
00002  * CPProgressIndicator.j
00003  * AppKit
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 /*
00026     @global
00027     @group CPProgressIndicatorStyle
00028 */
00029 CPProgressIndicatorBarStyle         = 0;
00030 /*
00031     @global
00032     @group CPProgressIndicatorStyle
00033 */
00034 CPProgressIndicatorSpinningStyle    = 1;
00035 /*
00036     @global
00037     @group CPProgressIndicatorStyle
00038 */
00039 CPProgressIndicatorHUDBarStyle      = 2;
00040 
00041 var CPProgressIndicatorSpinningStyleColors  = nil,
00042 
00043     CPProgressIndicatorClassName            = nil,
00044     CPProgressIndicatorStyleIdentifiers     = nil,
00045     CPProgressIndicatorStyleSizes           = nil;
00046 
00055 @implementation CPProgressIndicator : CPView
00056 {
00057     double                      _minValue;
00058     double                      _maxValue;
00059 
00060     double                      _doubleValue;
00061 
00062     CPControlSize               _controlSize;
00063 
00064     BOOL                        _isIndeterminate;
00065     CPProgressIndicatorStyle    _style;
00066 
00067     BOOL                        _isAnimating;
00068 
00069     BOOL                        _isDisplayedWhenStoppedSet;
00070     BOOL                        _isDisplayedWhenStopped;
00071 
00072     CPView                      _barView;
00073 }
00074 
00075 /*
00076     @ignore
00077 */
00078 + (void)initialize
00079 {
00080     if (self != [CPProgressIndicator class])
00081         return;
00082 
00083     var bundle = [CPBundle bundleForClass:self];
00084 
00085     CPProgressIndicatorSpinningStyleColors = [];
00086 
00087     CPProgressIndicatorSpinningStyleColors[CPMiniControlSize]       = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:
00088         [bundle pathForResource:@"CPProgressIndicator/CPProgressIndicatorSpinningStyleRegular.gif"] size:CGSizeMake(64.0, 64.0)]];
00089     CPProgressIndicatorSpinningStyleColors[CPSmallControlSize]      = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:
00090         [bundle pathForResource:@"CPProgressIndicator/CPProgressIndicatorSpinningStyleRegular.gif"] size:CGSizeMake(64.0, 64.0)]];
00091     CPProgressIndicatorSpinningStyleColors[CPRegularControlSize]    = [CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:
00092         [bundle pathForResource:@"CPProgressIndicator/CPProgressIndicatorSpinningStyleRegular.gif"] size:CGSizeMake(64.0, 64.0)]];
00093 
00094     CPProgressIndicatorBezelBorderViewPool = [];
00095 
00096     var start = CPProgressIndicatorBarStyle,
00097         end = CPProgressIndicatorHUDBarStyle;
00098 
00099     for (; start <= end; ++start)
00100     {
00101         CPProgressIndicatorBezelBorderViewPool[start] = [];
00102         CPProgressIndicatorBezelBorderViewPool[start][CPMiniControlSize]    = [];
00103         CPProgressIndicatorBezelBorderViewPool[start][CPSmallControlSize]   = [];
00104         CPProgressIndicatorBezelBorderViewPool[start][CPRegularControlSize]  = [];
00105     }
00106 
00107     CPProgressIndicatorClassName = [self className];
00108     CPProgressIndicatorStyleIdentifiers = [];
00109 
00110     CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle]        = @"Bar";
00111     CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorSpinningStyle]   = @"Spinny";
00112     CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle]     = @"HUDBar";
00113 
00114     var regularIdentifier = _CPControlIdentifierForControlSize(CPRegularControlSize),
00115         smallIdentifier = _CPControlIdentifierForControlSize(CPSmallControlSize),
00116         miniIdentifier = _CPControlIdentifierForControlSize(CPMiniControlSize);
00117 
00118     CPProgressIndicatorStyleSizes = [];
00119 
00120     // Bar Style
00121     var prefixes = [
00122         CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle],
00123         CPProgressIndicatorClassName + @"Bar" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle],
00124         CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle],
00125         CPProgressIndicatorClassName + @"Bar" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorHUDBarStyle]
00126     ];
00127 
00128     for (var i = 0, count = prefixes.length; i < count; i++)
00129     {
00130         var prefix = prefixes[i];
00131         CPProgressIndicatorStyleSizes[prefix + regularIdentifier] = [_CGSizeMake(3.0, 16.0), _CGSizeMake(1.0, 16.0), _CGSizeMake(3.0, 16.0)];
00132         CPProgressIndicatorStyleSizes[prefix + smallIdentifier] = [_CGSizeMake(3.0, 16.0), _CGSizeMake(1.0, 16.0), _CGSizeMake(3.0, 16.0)];
00133         CPProgressIndicatorStyleSizes[prefix + miniIdentifier] = [_CGSizeMake(3.0, 16.0), _CGSizeMake(1.0, 16.0), _CGSizeMake(3.0, 16.0)];
00134     }
00135 }
00136 
00137 - (id)initWithFrame:(CGRect)aFrame
00138 {
00139     self = [super initWithFrame:aFrame];
00140 
00141     if (self)
00142     {
00143         _minValue = 0.0;
00144         _maxValue = 100.0;
00145 
00146         _doubleValue = 0.0;
00147 
00148         _style = CPProgressIndicatorBarStyle;
00149         _isDisplayedWhenStoppedSet = NO;
00150 
00151         _controlSize = CPRegularControlSize;
00152 
00153         [self updateBackgroundColor];
00154         [self drawBar];
00155     }
00156 
00157     return self;
00158 }
00159 
00160 /*
00161     @ignore
00162 */
00163 - (void)setUsesThreadedAnimation:(BOOL)aFlag
00164 {
00165 }
00166 
00171 - (void)startAnimation:(id)aSender
00172 {
00173     _isAnimating = YES;
00174 
00175     [self _hideOrDisplay];
00176 }
00177 
00182 - (void)stopAnimation:(id)aSender
00183 {
00184     _isAnimating = NO;
00185 
00186     [self _hideOrDisplay];
00187 }
00188 
00192 - (BOOL)usesThreadedAnimation
00193 {
00194     return NO;
00195 }
00196 
00197 // Advancing the Progress Bar
00202 - (void)incrementBy:(double)aValue
00203 {
00204     [self setDoubleValue:_doubleValue + aValue];
00205 }
00206 
00210 - (void)setDoubleValue:(double)aValue
00211 {
00212     _doubleValue = MIN(MAX(aValue, _minValue), _maxValue);
00213 
00214     [self drawBar];
00215 }
00216 
00220 - (double)doubleValue
00221 {
00222     return _doubleValue;
00223 }
00224 
00229 - (void)setMinValue:(double)aValue
00230 {
00231     _minValue = aValue;
00232 }
00233 
00237 - (double)minValue
00238 {
00239     return _minValue;
00240 }
00241 
00246 - (void)setMaxValue:(double)aValue
00247 {
00248     _maxValue = aValue;
00249 }
00250 
00254 - (double)maxValue
00255 {
00256     return _maxValue;
00257 }
00258 
00259 // Setting the Appearance
00264 - (void)setControlSize:(CPControlSize)aControlSize
00265 {
00266     if (_controlSize == aControlSize)
00267         return;
00268 
00269     _controlSize = aControlSize;
00270 
00271     [self updateBackgroundColor];
00272 }
00273 
00277 - (CPControlSize)controlSize
00278 {
00279     return _controlSize;
00280 }
00281 
00282 /*
00283     Not yet implemented
00284 */
00285 - (void)setControlTint:(CPControlTint)aControlTint
00286 {
00287 }
00288 
00289 /*
00290     Not yet implemented.
00291 */
00292 - (CPControlTint)controlTint
00293 {
00294     return 0;
00295 }
00296 
00297 /*
00298     Not yet implemented.
00299 */
00300 - (void)setBezeled:(BOOL)isBezeled
00301 {
00302 }
00303 
00304 /*
00305     Not yet implemented.
00306 */
00307 - (BOOL)isBezeled
00308 {
00309     return YES;
00310 }
00311 
00316 - (void)setIndeterminate:(BOOL)isIndeterminate
00317 {
00318     if (_isIndeterminate == isIndeterminate)
00319         return;
00320 
00321     _isIndeterminate = isIndeterminate;
00322 
00323     [self updateBackgroundColor];
00324 }
00325 
00329 - (BOOL)isIndeterminate
00330 {
00331     return _isIndeterminate;
00332 }
00333 
00338 - (void)setStyle:(CPProgressIndicatorStyle)aStyle
00339 {
00340     if (_style == aStyle)
00341         return;
00342 
00343     _style = aStyle;
00344 
00345     [self updateBackgroundColor];
00346 }
00347 
00351 - (void)sizeToFit
00352 {
00353     if (_style == CPProgressIndicatorSpinningStyle)
00354         [self setFrameSize:[[CPProgressIndicatorSpinningStyleColors[_controlSize] patternImage] size]];
00355     else
00356         [self setFrameSize:CGSizeMake(CGRectGetWidth([self frame]), CPProgressIndicatorStyleSizes[
00357             CPProgressIndicatorClassName + @"BezelBorder" + CPProgressIndicatorStyleIdentifiers[CPProgressIndicatorBarStyle] + _CPControlIdentifierForControlSize(_controlSize)][0].height)];
00358 }
00359 
00365 - (void)setDisplayedWhenStopped:(BOOL)isDisplayedWhenStopped
00366 {
00367     if (_isDisplayedWhenStoppedSet && _isDisplayedWhenStopped == isDisplayedWhenStopped)
00368         return;
00369 
00370     _isDisplayedWhenStoppedSet = YES;
00371 
00372     _isDisplayedWhenStopped = isDisplayedWhenStopped;
00373 
00374     [self _hideOrDisplay];
00375 }
00376 
00380 - (BOOL)isDisplayedWhenStopped
00381 {
00382     if (_isDisplayedWhenStoppedSet)
00383         return _isDisplayedWhenStopped;
00384 
00385     if (_style == CPProgressIndicatorBarStyle || _style == CPProgressIndicatorHUDBarStyle)
00386         return YES;
00387 
00388     return NO;
00389 }
00390 
00391 /* @ignore */
00392 - (void)_hideOrDisplay
00393 {
00394     [self setHidden:!_isAnimating && ![self isDisplayedWhenStopped]];
00395 }
00396 
00397 - (void)setFrameSize:(CGSize)aSize
00398 {
00399     [super setFrameSize:aSize];
00400 
00401     [self drawBar];
00402 }
00403 
00404 /* @ignore */
00405 - (void)drawBar
00406 {
00407     if (_style == CPProgressIndicatorSpinningStyle)
00408         return;
00409 
00410     if (!_barView)
00411     {
00412         _barView = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 16.0)];
00413         [self addSubview:_barView];
00414     }
00415 
00416     [_barView setBackgroundColor:_CPControlThreePartImagePattern(
00417         NO,
00418         CPProgressIndicatorStyleSizes,
00419         CPProgressIndicatorClassName,
00420         @"Bar",
00421         CPProgressIndicatorStyleIdentifiers[_style],
00422         _CPControlIdentifierForControlSize(_controlSize))];
00423 
00424     var width = CGRectGetWidth([self bounds]),
00425         barWidth = width * ((_doubleValue - _minValue) / (_maxValue - _minValue));
00426 
00427     if (barWidth > 0.0 && barWidth < 4.0)
00428         barWidth = 4.0;
00429 
00430     [_barView setFrameSize:CGSizeMake(barWidth, 16.0)];
00431 }
00432 
00433 /* @ignore */
00434 - (void)updateBackgroundColor
00435 {
00436     if (YES)//_isBezeled)
00437     {
00438         if (_style == CPProgressIndicatorSpinningStyle)
00439         {
00440             [_barView removeFromSuperview];
00441 
00442             _barView = nil;
00443 
00444             [self setBackgroundColor:CPProgressIndicatorSpinningStyleColors[_controlSize]];
00445         }
00446         else
00447         {
00448             [self setBackgroundColor:_CPControlThreePartImagePattern(
00449                 NO,
00450                 CPProgressIndicatorStyleSizes,
00451                 CPProgressIndicatorClassName,
00452                 @"BezelBorder",
00453                 CPProgressIndicatorStyleIdentifiers[_style],
00454                 _CPControlIdentifierForControlSize(_controlSize))];
00455 
00456             [self drawBar];
00457         }
00458     }
00459     else
00460         [self setBackgroundColor:nil];
00461 }
00462 
00463 @end
00464 
00465 
00466 @implementation CPProgressIndicator (CPCoding)
00467 
00468 - (id)initWithCoder:(CPCoder)aCoder
00469 {
00470     if (self = [super initWithCoder:aCoder])
00471     {
00472         _minValue                   = [aCoder decodeObjectForKey:@"_minValue"];
00473         _maxValue                   = [aCoder decodeObjectForKey:@"_maxValue"];
00474         _doubleValue                = [aCoder decodeObjectForKey:@"_doubleValue"];
00475         _controlSize                = [aCoder decodeObjectForKey:@"_controlSize"];
00476         _isIndeterminate            = [aCoder decodeObjectForKey:@"_isIndeterminate"];
00477         _style                      = [aCoder decodeObjectForKey:@"_style"];
00478         _isAnimating                = [aCoder decodeObjectForKey:@"_isAnimating"];
00479         _isDisplayedWhenStoppedSet  = [aCoder decodeObjectForKey:@"_isDisplayedWhenStoppedSet"];
00480         _isDisplayedWhenStopped     = [aCoder decodeObjectForKey:@"_isDisplayedWhenStopped"];
00481     }
00482 
00483     return self;
00484 }
00485 
00486 - (void)encodeWithCoder:(CPCoder)aCoder
00487 {
00488     [super encodeWithCoder:aCoder];
00489 
00490     [aCoder encodeObject:_minValue forKey:@"_minValue"];
00491     [aCoder encodeObject:_maxValue forKey:@"_maxValue"];
00492     [aCoder encodeObject:_doubleValue forKey:@"_doubleValue"];
00493     [aCoder encodeObject:_controlSize forKey:@"_controlSize"];
00494     [aCoder encodeObject:_isIndeterminate forKey:@"_isIndeterminate"];
00495     [aCoder encodeObject:_style forKey:@"_style"];
00496     [aCoder encodeObject:_isAnimating forKey:@"_isAnimating"];
00497     [aCoder encodeObject:_isDisplayedWhenStoppedSet forKey:@"_isDisplayedWhenStoppedSet"];
00498     [aCoder encodeObject:_isDisplayedWhenStopped forKey:@"_isDisplayedWhenStopped"];
00499 }
00500 
00501 @end
 All Classes Files Functions Variables Defines