48 double _criticalValue;
49 CPTickMarkPosition _tickMarkPosition;
50 int _numberOfTickMarks;
51 int _numberOfMajorTickMarks;
60 return "level-indicator";
75 - (id)initWithFrame:(CGRect)aFrame
90 - (void)layoutSubviews
92 var bezelView = [
self layoutEphemeralSubviewNamed:"bezel"
93 positioned:CPWindowBelow
94 relativeToEphemeralSubviewNamed:nil];
96 [bezelView setBackgroundColor:[
self valueForThemeAttribute:@"bezel-color"]];
98 var segmentCount = _maxValue - _minValue;
100 if (segmentCount <= 0)
103 var filledColor = [
self valueForThemeAttribute:@"color-normal"],
106 if (value <= _criticalValue)
107 filledColor = [
self valueForThemeAttribute:@"color-critical"];
108 else if (value <= _warningValue)
109 filledColor = [
self valueForThemeAttribute:@"color-warning"];
111 for (var i = 0; i < segmentCount; i++)
113 var segmentView = [
self layoutEphemeralSubviewNamed:"segment-bezel-" + i
114 positioned:CPWindowAbove
115 relativeToEphemeralSubviewNamed:bezelView];
117 [segmentView setBackgroundColor:(_minValue + i) < value ? filledColor : [
self valueForThemeAttribute:@"color-empty"]];
123 return [[
CPView alloc] initWithFrame:CGRectMakeZero()];
126 - (CGRect)rectForEphemeralSubviewNamed:(
CPString)aViewName
129 var bezelHeight = 18,
131 bounds = CGRectCreateCopy([
self bounds]);
133 if (aViewName ==
"bezel")
135 bounds.origin.y = (CGRectGetHeight(bounds) - bezelHeight) / 2.0;
136 bounds.size.height = bezelHeight;
139 else if (aViewName.indexOf(
"segment-bezel") === 0)
141 var segment = parseInt(aViewName.substring(
"segment-bezel-".length), 10),
142 segmentCount = _maxValue - _minValue;
144 if (segment >= segmentCount)
145 return CGRectMakeZero();
147 var basicSegmentWidth = bounds.size.width / segmentCount,
148 segmentFrame = CGRectCreateCopy([
self bounds]),
149 spacing = [
self valueForThemeAttribute:@"spacing"];
151 segmentFrame.origin.y = (CGRectGetHeight(bounds) - bezelHeight) / 2.0;
152 segmentFrame.origin.x = FLOOR(segment * basicSegmentWidth);
153 segmentFrame.size.width = (segment == segmentCount - 1) ? bounds.size.width - segmentFrame.origin.x : FLOOR(((segment + 1) * basicSegmentWidth)) - FLOOR((segment * basicSegmentWidth)) - spacing;
154 segmentFrame.size.height = segmentHeight;
159 return CGRectMakeZero();
165 - (void)setEditable:(BOOL)shouldBeEditable
167 if (_isEditable === shouldBeEditable)
170 _isEditable = shouldBeEditable;
184 if (![
self isEditable])
187 return [
super hitTest:aPoint];
192 if (![
self isEditable] || ![
self isEnabled])
195 [
self _trackMouse:anEvent];
198 - (void)_trackMouse:(
CPEvent)anEvent
200 var type = [anEvent type];
204 var segmentCount = _maxValue - _minValue;
206 if (segmentCount <= 0)
209 var location = [
self convertPoint:[anEvent locationInWindow] fromView:nil],
210 bounds = [
self bounds],
211 oldValue = [
self doubleValue],
219 newValue = _minValue;
223 newValue = _maxValue;
227 for (var i = 0; i < segmentCount; i++)
229 var rect = [
self rectForEphemeralSubviewNamed:"segment-bezel-" + i];
232 if (location.x >= CGRectGetMinX(rect) && location.x < CGRectGetMaxX(rect))
234 newValue = (_minValue + i + 1);
240 if (newValue != oldValue)
241 [
self setDoubleValue:newValue];
245 [CPApp setTarget:self selector:@selector(_trackMouse:) forNextEventMatchingMask:CPLeftMouseDraggedMask | CPLeftMouseUpMask untilDate:nil inMode:nil dequeue:YES];
247 if ([
self isContinuous])
248 [
self sendAction:[
self action] to:[
self target]];
250 else if (_isTracking)
253 [
self sendAction:[
self action] to:[
self target]];
262 - (void)setMinValue:(
double)minValue
264 if (_minValue === minValue)
266 _minValue = minValue;
268 [
self setNeedsLayout];
271 - (void)setMaxValue:(
double)maxValue
273 if (_maxValue === maxValue)
275 _maxValue = maxValue;
277 [
self setNeedsLayout];
280 - (void)setWarningValue:(
double)warningValue;
282 if (_warningValue === warningValue)
284 _warningValue = warningValue;
286 [
self setNeedsLayout];
289 - (void)setCriticalValue:(
double)criticalValue;
291 if (_criticalValue === criticalValue)
293 _criticalValue = criticalValue;
295 [
self setNeedsLayout];
332 _levelIndicatorStyle = [aCoder decodeIntForKey:CPLevelIndicatorStyleKey];
333 _minValue = [aCoder decodeDoubleForKey:CPLevelIndicatorMinValueKey];
334 _maxValue = [aCoder decodeDoubleForKey:CPLevelIndicatorMaxValueKey];
335 _warningValue = [aCoder decodeDoubleForKey:CPLevelIndicatorWarningValueKey];
336 _criticalValue = [aCoder decodeDoubleForKey:CPLevelIndicatorCriticalValueKey];
337 _tickMarkPosition = [aCoder decodeIntForKey:CPLevelIndicatorTickMarkPositionKey];
338 _numberOfTickMarks = [aCoder decodeIntForKey:CPLevelIndicatorNumberOfTickMarksKey];
339 _numberOfMajorTickMarks = [aCoder decodeIntForKey:CPLevelIndicatorNumberOfMajorTickMarksKey];
341 _isEditable = [aCoder decodeBoolForKey:CPLevelIndicatorIsEditableKey];
343 [
self setNeedsLayout];
344 [
self setNeedsDisplay:YES];
354 [aCoder encodeInt:_levelIndicatorStyle forKey:CPLevelIndicatorStyleKey];
355 [aCoder encodeDouble:_minValue forKey:CPLevelIndicatorMinValueKey];
356 [aCoder encodeDouble:_maxValue forKey:CPLevelIndicatorMaxValueKey];
357 [aCoder encodeDouble:_warningValue forKey:CPLevelIndicatorWarningValueKey];
358 [aCoder encodeDouble:_criticalValue forKey:CPLevelIndicatorCriticalValueKey];
359 [aCoder encodeInt:_tickMarkPosition forKey:CPLevelIndicatorTickMarkPositionKey];
360 [aCoder encodeInt:_numberOfTickMarks forKey:CPLevelIndicatorNumberOfTickMarksKey];
361 [aCoder encodeInt:_numberOfMajorTickMarks forKey:CPLevelIndicatorNumberOfMajorTickMarksKey];
362 [aCoder encodeBool:_isEditable forKey:CPLevelIndicatorIsEditableKey];
374 return _levelIndicatorStyle;
382 _levelIndicatorStyle = aValue;
396 - (void)setMinValue:(
double)aValue
412 - (void)setMaxValue:(
double)aValue
420 - (double)warningValue
422 return _warningValue;
428 - (void)setWarningValue:(
double)aValue
430 _warningValue = aValue;
436 - (double)criticalValue
438 return _criticalValue;
444 - (void)setCriticalValue:(
double)aValue
446 _criticalValue = aValue;
452 - (CPTickMarkPosition)tickMarkPosition
454 return _tickMarkPosition;
460 - (void)setTickMarkPosition:(CPTickMarkPosition)aValue
462 _tickMarkPosition = aValue;
468 - (int)numberOfTickMarks
470 return _numberOfTickMarks;
476 - (void)setNumberOfTickMarks:(
int)aValue
478 _numberOfTickMarks = aValue;
484 - (int)numberOfMajorTickMarks
486 return _numberOfMajorTickMarks;
492 - (void)setNumberOfMajorTickMarks:(
int)aValue
494 _numberOfMajorTickMarks = aValue;