~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/html/HTMLProgressElement.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "HTMLProgressElement.h"
 
23
 
 
24
#if ENABLE(PROGRESS_ELEMENT)
 
25
#include "Attribute.h"
 
26
#include "EventNames.h"
 
27
#include "ExceptionCode.h"
 
28
#include "NodeRenderingContext.h"
 
29
#include "HTMLDivElement.h"
 
30
#include "HTMLNames.h"
 
31
#include "HTMLParserIdioms.h"
 
32
#include "ProgressShadowElement.h"
 
33
#include "RenderProgress.h"
 
34
#include "SelectRuleFeatureSet.h"
 
35
#include "ShadowRoot.h"
 
36
#include <wtf/StdLibExtras.h>
 
37
 
 
38
namespace WebCore {
 
39
 
 
40
using namespace HTMLNames;
 
41
 
 
42
const double HTMLProgressElement::IndeterminatePosition = -1;
 
43
const double HTMLProgressElement::InvalidPosition = -2;
 
44
 
 
45
HTMLProgressElement::HTMLProgressElement(const QualifiedName& tagName, Document* document)
 
46
    : LabelableElement(tagName, document)
 
47
    , m_value(0)
 
48
    , m_hasAuthorShadowRoot(false)
 
49
{
 
50
    ASSERT(hasTagName(progressTag));
 
51
}
 
52
 
 
53
HTMLProgressElement::~HTMLProgressElement()
 
54
{
 
55
}
 
56
 
 
57
PassRefPtr<HTMLProgressElement> HTMLProgressElement::create(const QualifiedName& tagName, Document* document)
 
58
{
 
59
    RefPtr<HTMLProgressElement> progress = adoptRef(new HTMLProgressElement(tagName, document));
 
60
    progress->createShadowSubtree();
 
61
    return progress;
 
62
}
 
63
 
 
64
RenderObject* HTMLProgressElement::createRenderer(RenderArena* arena, RenderStyle* style)
 
65
{
 
66
    if (!style->hasAppearance() || hasAuthorShadowRoot())
 
67
        return RenderObject::createObject(this, style);
 
68
 
 
69
    return new (arena) RenderProgress(this);
 
70
}
 
71
 
 
72
bool HTMLProgressElement::childShouldCreateRenderer(const NodeRenderingContext& childContext) const
 
73
{
 
74
    return childContext.isOnUpperEncapsulationBoundary() && HTMLElement::childShouldCreateRenderer(childContext);
 
75
}
 
76
 
 
77
RenderProgress* HTMLProgressElement::renderProgress() const
 
78
{
 
79
    if (renderer() && renderer()->isProgress())
 
80
        return static_cast<RenderProgress*>(renderer());
 
81
 
 
82
    RenderObject* renderObject = userAgentShadowRoot()->firstChild()->renderer();
 
83
    ASSERT(!renderObject || renderObject->isProgress());
 
84
    return static_cast<RenderProgress*>(renderObject);
 
85
}
 
86
 
 
87
void HTMLProgressElement::willAddAuthorShadowRoot()
 
88
{
 
89
    m_hasAuthorShadowRoot = true;
 
90
}
 
91
 
 
92
bool HTMLProgressElement::supportsFocus() const
 
93
{
 
94
    return Node::supportsFocus() && !disabled();
 
95
}
 
96
 
 
97
void HTMLProgressElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
 
98
{
 
99
    if (name == valueAttr)
 
100
        didElementStateChange();
 
101
    else if (name == maxAttr)
 
102
        didElementStateChange();
 
103
    else
 
104
        LabelableElement::parseAttribute(name, value);
 
105
}
 
106
 
 
107
void HTMLProgressElement::attach()
 
108
{
 
109
    LabelableElement::attach();
 
110
    if (RenderProgress* render = renderProgress())
 
111
        render->updateFromElement();
 
112
}
 
113
 
 
114
double HTMLProgressElement::value() const
 
115
{
 
116
    double value = parseToDoubleForNumberType(fastGetAttribute(valueAttr));
 
117
    return !isfinite(value) || value < 0 ? 0 : std::min(value, max());
 
118
}
 
119
 
 
120
void HTMLProgressElement::setValue(double value, ExceptionCode& ec)
 
121
{
 
122
    if (!isfinite(value)) {
 
123
        ec = NOT_SUPPORTED_ERR;
 
124
        return;
 
125
    }
 
126
    setAttribute(valueAttr, String::number(value >= 0 ? value : 0));
 
127
}
 
128
 
 
129
double HTMLProgressElement::max() const
 
130
{
 
131
    double max = parseToDoubleForNumberType(getAttribute(maxAttr));
 
132
    return !isfinite(max) || max <= 0 ? 1 : max;
 
133
}
 
134
 
 
135
void HTMLProgressElement::setMax(double max, ExceptionCode& ec)
 
136
{
 
137
    if (!isfinite(max)) {
 
138
        ec = NOT_SUPPORTED_ERR;
 
139
        return;
 
140
    }
 
141
    setAttribute(maxAttr, String::number(max > 0 ? max : 1));
 
142
}
 
143
 
 
144
double HTMLProgressElement::position() const
 
145
{
 
146
    if (!isDeterminate())
 
147
        return HTMLProgressElement::IndeterminatePosition;
 
148
    return value() / max();
 
149
}
 
150
 
 
151
bool HTMLProgressElement::isDeterminate() const
 
152
{
 
153
    return fastHasAttribute(valueAttr);
 
154
}
 
155
    
 
156
void HTMLProgressElement::didElementStateChange()
 
157
{
 
158
    m_value->setWidthPercentage(position() * 100);
 
159
    if (RenderProgress* render = renderProgress()) {
 
160
        bool wasDeterminate = render->isDeterminate();
 
161
        render->updateFromElement();
 
162
        if (wasDeterminate != isDeterminate()) {
 
163
            setNeedsStyleRecalc();
 
164
            invalidateParentDistributionIfNecessary(this, SelectRuleFeatureSet::RuleFeatureIndeterminate);
 
165
        }
 
166
    }
 
167
}
 
168
 
 
169
void HTMLProgressElement::createShadowSubtree()
 
170
{
 
171
    ASSERT(!userAgentShadowRoot());
 
172
    ASSERT(!m_value);
 
173
           
 
174
    RefPtr<ShadowRoot> root = ShadowRoot::create(this, ShadowRoot::UserAgentShadowRoot, ASSERT_NO_EXCEPTION);
 
175
 
 
176
    RefPtr<ProgressInnerElement> inner = ProgressInnerElement::create(document());
 
177
    root->appendChild(inner);
 
178
 
 
179
    RefPtr<ProgressBarElement> bar = ProgressBarElement::create(document());
 
180
    RefPtr<ProgressValueElement> value = ProgressValueElement::create(document());
 
181
    m_value = value.get();
 
182
    m_value->setWidthPercentage(HTMLProgressElement::IndeterminatePosition * 100);
 
183
    bar->appendChild(m_value, ASSERT_NO_EXCEPTION);
 
184
 
 
185
    inner->appendChild(bar, ASSERT_NO_EXCEPTION);
 
186
}
 
187
 
 
188
} // namespace
 
189
#endif