Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLMeterElement.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
3  *
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.
8  *
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.
13  *
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.
18  *
19  */
20
21 #include "config.h"
22
23 #include "core/html/HTMLMeterElement.h"
24
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"
36
37 namespace blink {
38
39 using namespace HTMLNames;
40
41 HTMLMeterElement::HTMLMeterElement(Document& document)
42     : LabelableElement(meterTag, document)
43 {
44     ScriptWrappable::init(this);
45     UseCounter::count(document, UseCounter::MeterElement);
46 }
47
48 HTMLMeterElement::~HTMLMeterElement()
49 {
50 }
51
52 PassRefPtrWillBeRawPtr<HTMLMeterElement> HTMLMeterElement::create(Document& document)
53 {
54     RefPtrWillBeRawPtr<HTMLMeterElement> meter = adoptRefWillBeNoop(new HTMLMeterElement(document));
55     meter->ensureUserAgentShadowRoot();
56     return meter.release();
57 }
58
59 RenderObject* HTMLMeterElement::createRenderer(RenderStyle* style)
60 {
61     if (hasAuthorShadowRoot() || !RenderTheme::theme().supportsMeter(style->appearance()))
62         return RenderObject::createObject(this, style);
63
64     return new RenderMeter(this);
65 }
66
67 void HTMLMeterElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
68 {
69     if (name == valueAttr || name == minAttr || name == maxAttr || name == lowAttr || name == highAttr || name == optimumAttr)
70         didElementStateChange();
71     else
72         LabelableElement::parseAttribute(name, value);
73 }
74
75 double HTMLMeterElement::value() const
76 {
77     double value = getFloatingPointAttribute(valueAttr, 0);
78     return std::min(std::max(value, min()), max());
79 }
80
81 void HTMLMeterElement::setValue(double value)
82 {
83     setFloatingPointAttribute(valueAttr, value);
84 }
85
86 double HTMLMeterElement::min() const
87 {
88     return getFloatingPointAttribute(minAttr, 0);
89 }
90
91 void HTMLMeterElement::setMin(double min)
92 {
93     setFloatingPointAttribute(minAttr, min);
94 }
95
96 double HTMLMeterElement::max() const
97 {
98     return std::max(getFloatingPointAttribute(maxAttr, std::max(1.0, min())), min());
99 }
100
101 void HTMLMeterElement::setMax(double max)
102 {
103     setFloatingPointAttribute(maxAttr, max);
104 }
105
106 double HTMLMeterElement::low() const
107 {
108     double low = getFloatingPointAttribute(lowAttr, min());
109     return std::min(std::max(low, min()), max());
110 }
111
112 void HTMLMeterElement::setLow(double low)
113 {
114     setFloatingPointAttribute(lowAttr, low);
115 }
116
117 double HTMLMeterElement::high() const
118 {
119     double high = getFloatingPointAttribute(highAttr, max());
120     return std::min(std::max(high, low()), max());
121 }
122
123 void HTMLMeterElement::setHigh(double high)
124 {
125     setFloatingPointAttribute(highAttr, high);
126 }
127
128 double HTMLMeterElement::optimum() const
129 {
130     double optimum = getFloatingPointAttribute(optimumAttr, (max() + min()) / 2);
131     return std::min(std::max(optimum, min()), max());
132 }
133
134 void HTMLMeterElement::setOptimum(double optimum)
135 {
136     setFloatingPointAttribute(optimumAttr, optimum);
137 }
138
139 HTMLMeterElement::GaugeRegion HTMLMeterElement::gaugeRegion() const
140 {
141     double lowValue = low();
142     double highValue = high();
143     double theValue = value();
144     double optimumValue = optimum();
145
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;
153     }
154
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;
162     }
163
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;
170 }
171
172 double HTMLMeterElement::valueRatio() const
173 {
174     double min = this->min();
175     double max = this->max();
176     double value = this->value();
177
178     if (max <= min)
179         return 0;
180     return (value - min) / (max - min);
181 }
182
183 void HTMLMeterElement::didElementStateChange()
184 {
185     m_value->setWidthPercentage(valueRatio()*100);
186     m_value->updatePseudo();
187     if (RenderMeter* render = renderMeter())
188         render->updateFromElement();
189 }
190
191 RenderMeter* HTMLMeterElement::renderMeter() const
192 {
193     if (renderer() && renderer()->isMeter())
194         return toRenderMeter(renderer());
195
196     RenderObject* renderObject = userAgentShadowRoot()->firstChild()->renderer();
197     return toRenderMeter(renderObject);
198 }
199
200 void HTMLMeterElement::didAddUserAgentShadowRoot(ShadowRoot& root)
201 {
202     ASSERT(!m_value);
203
204     RefPtrWillBeRawPtr<MeterInnerElement> inner = MeterInnerElement::create(document());
205     root.appendChild(inner);
206
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);
212
213     inner->appendChild(bar);
214 }
215
216 void HTMLMeterElement::trace(Visitor* visitor)
217 {
218     visitor->trace(m_value);
219     LabelableElement::trace(visitor);
220 }
221
222 } // namespace