2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include "core/html/HTMLMeterElement.h"
25 #include "bindings/core/v8/ExceptionMessages.h"
26 #include "bindings/core/v8/ExceptionState.h"
27 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
28 #include "core/HTMLNames.h"
29 #include "core/dom/ExceptionCode.h"
30 #include "core/dom/shadow/ShadowRoot.h"
31 #include "core/frame/UseCounter.h"
32 #include "core/html/parser/HTMLParserIdioms.h"
33 #include "core/html/shadow/MeterShadowElement.h"
34 #include "core/rendering/RenderMeter.h"
35 #include "core/rendering/RenderTheme.h"
39 using namespace HTMLNames;
41 HTMLMeterElement::HTMLMeterElement(Document& document)
42 : LabelableElement(meterTag, document)
44 ScriptWrappable::init(this);
45 UseCounter::count(document, UseCounter::MeterElement);
48 HTMLMeterElement::~HTMLMeterElement()
52 PassRefPtrWillBeRawPtr<HTMLMeterElement> HTMLMeterElement::create(Document& document)
54 RefPtrWillBeRawPtr<HTMLMeterElement> meter = adoptRefWillBeNoop(new HTMLMeterElement(document));
55 meter->ensureUserAgentShadowRoot();
56 return meter.release();
59 RenderObject* HTMLMeterElement::createRenderer(RenderStyle* style)
61 if (hasAuthorShadowRoot() || !RenderTheme::theme().supportsMeter(style->appearance()))
62 return RenderObject::createObject(this, style);
64 return new RenderMeter(this);
67 void HTMLMeterElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
69 if (name == valueAttr || name == minAttr || name == maxAttr || name == lowAttr || name == highAttr || name == optimumAttr)
70 didElementStateChange();
72 LabelableElement::parseAttribute(name, value);
75 double HTMLMeterElement::value() const
77 double value = getFloatingPointAttribute(valueAttr, 0);
78 return std::min(std::max(value, min()), max());
81 void HTMLMeterElement::setValue(double value)
83 setFloatingPointAttribute(valueAttr, value);
86 double HTMLMeterElement::min() const
88 return getFloatingPointAttribute(minAttr, 0);
91 void HTMLMeterElement::setMin(double min)
93 setFloatingPointAttribute(minAttr, min);
96 double HTMLMeterElement::max() const
98 return std::max(getFloatingPointAttribute(maxAttr, std::max(1.0, min())), min());
101 void HTMLMeterElement::setMax(double max)
103 setFloatingPointAttribute(maxAttr, max);
106 double HTMLMeterElement::low() const
108 double low = getFloatingPointAttribute(lowAttr, min());
109 return std::min(std::max(low, min()), max());
112 void HTMLMeterElement::setLow(double low)
114 setFloatingPointAttribute(lowAttr, low);
117 double HTMLMeterElement::high() const
119 double high = getFloatingPointAttribute(highAttr, max());
120 return std::min(std::max(high, low()), max());
123 void HTMLMeterElement::setHigh(double high)
125 setFloatingPointAttribute(highAttr, high);
128 double HTMLMeterElement::optimum() const
130 double optimum = getFloatingPointAttribute(optimumAttr, (max() + min()) / 2);
131 return std::min(std::max(optimum, min()), max());
134 void HTMLMeterElement::setOptimum(double optimum)
136 setFloatingPointAttribute(optimumAttr, optimum);
139 HTMLMeterElement::GaugeRegion HTMLMeterElement::gaugeRegion() const
141 double lowValue = low();
142 double highValue = high();
143 double theValue = value();
144 double optimumValue = optimum();
146 if (optimumValue < lowValue) {
147 // The optimum range stays under low
148 if (theValue <= lowValue)
149 return GaugeRegionOptimum;
150 if (theValue <= highValue)
151 return GaugeRegionSuboptimal;
152 return GaugeRegionEvenLessGood;
155 if (highValue < optimumValue) {
156 // The optimum range stays over high
157 if (highValue <= theValue)
158 return GaugeRegionOptimum;
159 if (lowValue <= theValue)
160 return GaugeRegionSuboptimal;
161 return GaugeRegionEvenLessGood;
164 // The optimum range stays between high and low.
165 // According to the standard, <meter> never show GaugeRegionEvenLessGood in this case
166 // because the value is never less or greater than min or max.
167 if (lowValue <= theValue && theValue <= highValue)
168 return GaugeRegionOptimum;
169 return GaugeRegionSuboptimal;
172 double HTMLMeterElement::valueRatio() const
174 double min = this->min();
175 double max = this->max();
176 double value = this->value();
180 return (value - min) / (max - min);
183 void HTMLMeterElement::didElementStateChange()
185 m_value->setWidthPercentage(valueRatio()*100);
186 m_value->updatePseudo();
187 if (RenderMeter* render = renderMeter())
188 render->updateFromElement();
191 RenderMeter* HTMLMeterElement::renderMeter() const
193 if (renderer() && renderer()->isMeter())
194 return toRenderMeter(renderer());
196 RenderObject* renderObject = userAgentShadowRoot()->firstChild()->renderer();
197 return toRenderMeter(renderObject);
200 void HTMLMeterElement::didAddUserAgentShadowRoot(ShadowRoot& root)
204 RefPtrWillBeRawPtr<MeterInnerElement> inner = MeterInnerElement::create(document());
205 root.appendChild(inner);
207 RefPtrWillBeRawPtr<MeterBarElement> bar = MeterBarElement::create(document());
208 m_value = MeterValueElement::create(document());
209 m_value->setWidthPercentage(0);
210 m_value->updatePseudo();
211 bar->appendChild(m_value);
213 inner->appendChild(bar);
216 void HTMLMeterElement::trace(Visitor* visitor)
218 visitor->trace(m_value);
219 LabelableElement::trace(visitor);