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.
22 #if ENABLE(PROGRESS_TAG)
23 #include "HTMLProgressElement.h"
25 #include "Attribute.h"
26 #include "EventNames.h"
27 #include "ExceptionCode.h"
28 #include "FormDataList.h"
29 #include "HTMLDivElement.h"
30 #include "HTMLFormElement.h"
31 #include "HTMLNames.h"
32 #include "HTMLParserIdioms.h"
33 #include "ProgressShadowElement.h"
34 #include "RenderProgress.h"
35 #include "ShadowRoot.h"
36 #include <wtf/StdLibExtras.h>
40 using namespace HTMLNames;
42 const double HTMLProgressElement::IndeterminatePosition = -1;
43 const double HTMLProgressElement::InvalidPosition = -2;
45 HTMLProgressElement::HTMLProgressElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
46 : HTMLFormControlElement(tagName, document, form)
48 ASSERT(hasTagName(progressTag));
51 HTMLProgressElement::~HTMLProgressElement()
55 PassRefPtr<HTMLProgressElement> HTMLProgressElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
57 RefPtr<HTMLProgressElement> progress = adoptRef(new HTMLProgressElement(tagName, document, form));
58 progress->createShadowSubtree();
62 RenderObject* HTMLProgressElement::createRenderer(RenderArena* arena, RenderStyle*)
64 return new (arena) RenderProgress(this);
67 bool HTMLProgressElement::supportsFocus() const
69 return Node::supportsFocus() && !disabled();
72 const AtomicString& HTMLProgressElement::formControlType() const
74 DEFINE_STATIC_LOCAL(const AtomicString, progress, ("progress"));
78 void HTMLProgressElement::parseMappedAttribute(Attribute* attribute)
80 if (attribute->name() == valueAttr)
81 didElementStateChange();
82 else if (attribute->name() == maxAttr)
83 didElementStateChange();
85 HTMLFormControlElement::parseMappedAttribute(attribute);
88 void HTMLProgressElement::attach()
90 HTMLFormControlElement::attach();
91 didElementStateChange();
94 double HTMLProgressElement::value() const
97 bool ok = parseToDoubleForNumberType(fastGetAttribute(valueAttr), &value);
100 return (value > max()) ? max() : value;
103 void HTMLProgressElement::setValue(double value, ExceptionCode& ec)
105 if (!isfinite(value)) {
106 ec = NOT_SUPPORTED_ERR;
109 setAttribute(valueAttr, String::number(value >= 0 ? value : 0));
112 double HTMLProgressElement::max() const
115 bool ok = parseToDoubleForNumberType(getAttribute(maxAttr), &max);
121 void HTMLProgressElement::setMax(double max, ExceptionCode& ec)
123 if (!isfinite(max)) {
124 ec = NOT_SUPPORTED_ERR;
127 setAttribute(maxAttr, String::number(max > 0 ? max : 1));
130 double HTMLProgressElement::position() const
132 if (!isDeterminate())
133 return HTMLProgressElement::IndeterminatePosition;
134 return value() / max();
137 bool HTMLProgressElement::isDeterminate() const
139 return fastHasAttribute(valueAttr);
142 void HTMLProgressElement::didElementStateChange()
144 m_value->setWidthPercentage(position() * 100);
146 RenderProgress* render = toRenderProgress(renderer());
147 bool wasDeterminate = render->isDeterminate();
148 renderer()->updateFromElement();
149 if (wasDeterminate != isDeterminate())
150 setNeedsStyleRecalc();
154 void HTMLProgressElement::createShadowSubtree()
156 RefPtr<ProgressBarElement> bar = ProgressBarElement::create(document());
157 m_value = ProgressValueElement::create(document());
158 ExceptionCode ec = 0;
159 bar->appendChild(m_value, ec);
160 ensureShadowRoot()->appendChild(bar, ec);