34 var _CPLevelIndicatorBezelColor = nil,
35 _CPLevelIndicatorSegmentEmptyColor = nil,
36 _CPLevelIndicatorSegmentNormalColor = nil,
37 _CPLevelIndicatorSegmentWarningColor = nil,
38 _CPLevelIndicatorSegmentCriticalColor = nil,
40 _CPLevelIndicatorSpacing = 1;
54 double _criticalValue;
55 CPTickMarkPosition _tickMarkPosition;
56 int _numberOfTickMarks;
57 int _numberOfMajorTickMarks;
117 - (id)initWithFrame:(CGRect)aFrame
138 - (void)layoutSubviews
144 [bezelView setBackgroundColor:_CPLevelIndicatorBezelColor];
146 var segmentCount = _maxValue - _minValue;
148 if (segmentCount <= 0)
151 var filledColor = _CPLevelIndicatorSegmentNormalColor,
154 if (value <= _criticalValue)
155 filledColor = _CPLevelIndicatorSegmentCriticalColor;
156 else if (value <= _warningValue)
157 filledColor = _CPLevelIndicatorSegmentWarningColor;
159 for (var i = 0; i < segmentCount; i++)
165 [segmentView setBackgroundColor:(_minValue + i) < value ? filledColor : _CPLevelIndicatorSegmentEmptyColor];
171 return [[
CPView alloc] initWithFrame:_CGRectMakeZero()];
174 - (CGRect)rectForEphemeralSubviewNamed:(
CPString)aViewName
177 var bezelHeight = 18,
179 bounds = _CGRectCreateCopy([
self bounds]);
181 if (aViewName ==
"bezel")
183 bounds.origin.y = (_CGRectGetHeight(bounds) - bezelHeight) / 2.0;
184 bounds.size.height = bezelHeight;
187 else if (aViewName.indexOf(
"segment-bezel") === 0)
189 var segment = parseInt(aViewName.substring(
"segment-bezel-".length), 10),
190 segmentCount = _maxValue - _minValue;
192 if (segment >= segmentCount)
193 return _CGRectMakeZero();
195 var basicSegmentWidth = bounds.size.width / segmentCount,
196 segmentFrame = CGRectCreateCopy([
self bounds]);
198 segmentFrame.origin.y = (_CGRectGetHeight(bounds) - bezelHeight) / 2.0;
199 segmentFrame.origin.x = FLOOR(segment * basicSegmentWidth);
200 segmentFrame.size.width = (segment == segmentCount - 1) ? bounds.size.width - segmentFrame.origin.x : FLOOR(((segment + 1) * basicSegmentWidth)) - FLOOR((segment * basicSegmentWidth)) - _CPLevelIndicatorSpacing;
201 segmentFrame.size.height = segmentHeight;
206 return _CGRectMakeZero();
212 - (void)setEditable:(BOOL)shouldBeEditable
214 if (_isEditable === shouldBeEditable)
217 _isEditable = shouldBeEditable;
231 if (![
self isEditable])
239 if (![
self isEditable] || ![
self isEnabled])
242 [
self _trackMouse:anEvent];
245 - (void)_trackMouse:(
CPEvent)anEvent
247 var type = [anEvent
type];
251 var segmentCount = _maxValue - _minValue;
253 if (segmentCount <= 0)
257 bounds = [
self bounds],
258 oldValue = [
self doubleValue],
266 newValue = _minValue;
270 newValue = _maxValue;
274 for (var i = 0; i < segmentCount; i++)
276 var rect = [
self rectForEphemeralSubviewNamed:"segment-bezel-" + i];
279 if (location.x >= CGRectGetMinX(rect) && location.x < CGRectGetMaxX(rect))
281 newValue = (_minValue + i + 1);
287 if (newValue != oldValue)
288 [
self setDoubleValue:newValue];
292 [CPApp setTarget:self selector:@selector(_trackMouse:) forNextEventMatchingMask:CPLeftMouseDraggedMask | CPLeftMouseUpMask untilDate:nil inMode:nil dequeue:YES];
294 if ([
self isContinuous])
295 [
self sendAction:[
self action] to:[
self target]];
297 else if (_isTracking)
300 [
self sendAction:[
self action] to:[
self target]];
309 - (void)setMinValue:(
double)minValue
311 if (_minValue === minValue)
313 _minValue = minValue;
318 - (void)setMaxValue:(
double)maxValue
320 if (_maxValue === maxValue)
322 _maxValue = maxValue;
327 - (void)setWarningValue:(
double)warningValue;
329 if (_warningValue === warningValue)
331 _warningValue = warningValue;
336 - (void)setCriticalValue:(
double)criticalValue;
338 if (_criticalValue === criticalValue)
340 _criticalValue = criticalValue;
379 _levelIndicatorStyle = [aCoder decodeIntForKey:CPLevelIndicatorStyleKey];
380 _minValue = [aCoder decodeDoubleForKey:CPLevelIndicatorMinValueKey];
381 _maxValue = [aCoder decodeDoubleForKey:CPLevelIndicatorMaxValueKey];
382 _warningValue = [aCoder decodeDoubleForKey:CPLevelIndicatorWarningValueKey];
383 _criticalValue = [aCoder decodeDoubleForKey:CPLevelIndicatorCriticalValueKey];
384 _tickMarkPosition = [aCoder decodeIntForKey:CPLevelIndicatorTickMarkPositionKey];
385 _numberOfTickMarks = [aCoder decodeIntForKey:CPLevelIndicatorNumberOfTickMarksKey];
386 _numberOfMajorTickMarks = [aCoder decodeIntForKey:CPLevelIndicatorNumberOfMajorTickMarksKey];
388 _isEditable = [aCoder decodeBoolForKey:CPLevelIndicatorIsEditableKey];
403 [aCoder encodeInt:_levelIndicatorStyle forKey:CPLevelIndicatorStyleKey];
404 [aCoder encodeDouble:_minValue forKey:CPLevelIndicatorMinValueKey];
405 [aCoder encodeDouble:_maxValue forKey:CPLevelIndicatorMaxValueKey];
406 [aCoder encodeDouble:_warningValue forKey:CPLevelIndicatorWarningValueKey];
407 [aCoder encodeDouble:_criticalValue forKey:CPLevelIndicatorCriticalValueKey];
408 [aCoder encodeInt:_tickMarkPosition forKey:CPLevelIndicatorTickMarkPositionKey];
409 [aCoder encodeInt:_numberOfTickMarks forKey:CPLevelIndicatorNumberOfTickMarksKey];
410 [aCoder encodeInt:_numberOfMajorTickMarks forKey:CPLevelIndicatorNumberOfMajorTickMarksKey];
411 [aCoder encodeBool:_isEditable forKey:CPLevelIndicatorIsEditableKey];
423 return _levelIndicatorStyle;
431 _levelIndicatorStyle = aValue;
445 - (void)setMinValue:(
double)aValue
461 - (void)setMaxValue:(
double)aValue
469 - (double)warningValue
471 return _warningValue;
477 - (void)setWarningValue:(
double)aValue
479 _warningValue = aValue;
485 - (double)criticalValue
487 return _criticalValue;
493 - (void)setCriticalValue:(
double)aValue
495 _criticalValue = aValue;
501 - (CPTickMarkPosition)tickMarkPosition
503 return _tickMarkPosition;
509 - (void)setTickMarkPosition:(CPTickMarkPosition)aValue
511 _tickMarkPosition = aValue;
517 - (int)numberOfTickMarks
519 return _numberOfTickMarks;
525 - (void)setNumberOfTickMarks:(
int)aValue
527 _numberOfTickMarks = aValue;
533 - (int)numberOfMajorTickMarks
535 return _numberOfMajorTickMarks;
541 - (void)setNumberOfMajorTickMarks:(
int)aValue
543 _numberOfMajorTickMarks = aValue;