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/rendering/RenderProgress.h"
24
25#include "core/html/HTMLProgressElement.h"
26#include "core/rendering/RenderTheme.h"
27#include "wtf/CurrentTime.h"
28#include "wtf/RefPtr.h"
29
30namespace blink {
31
32RenderProgress::RenderProgress(HTMLElement* element)
33    : RenderBlockFlow(element)
34    , m_position(HTMLProgressElement::InvalidPosition)
35    , m_animationStartTime(0)
36    , m_animationRepeatInterval(0)
37    , m_animationDuration(0)
38    , m_animating(false)
39    , m_animationTimer(this, &RenderProgress::animationTimerFired)
40{
41}
42
43RenderProgress::~RenderProgress()
44{
45}
46
47void RenderProgress::destroy()
48{
49    if (m_animating) {
50        m_animationTimer.stop();
51        m_animating = false;
52    }
53    RenderBlockFlow::destroy();
54}
55
56void RenderProgress::updateFromElement()
57{
58    HTMLProgressElement* element = progressElement();
59    if (m_position == element->position())
60        return;
61    m_position = element->position();
62
63    updateAnimationState();
64    setShouldDoFullPaintInvalidation(true);
65    RenderBlockFlow::updateFromElement();
66}
67
68double RenderProgress::animationProgress() const
69{
70    return m_animating ? (fmod((currentTime() - m_animationStartTime), m_animationDuration) / m_animationDuration) : 0;
71}
72
73bool RenderProgress::isDeterminate() const
74{
75    return (HTMLProgressElement::IndeterminatePosition != position()
76            && HTMLProgressElement::InvalidPosition != position());
77}
78
79void RenderProgress::animationTimerFired(Timer<RenderProgress>*)
80{
81    setShouldDoFullPaintInvalidation(true);
82    if (!m_animationTimer.isActive() && m_animating)
83        m_animationTimer.startOneShot(m_animationRepeatInterval, FROM_HERE);
84}
85
86void RenderProgress::updateAnimationState()
87{
88    m_animationDuration = RenderTheme::theme().animationDurationForProgressBar(this);
89    m_animationRepeatInterval = RenderTheme::theme().animationRepeatIntervalForProgressBar(this);
90
91    bool animating = style()->hasAppearance() && m_animationDuration > 0;
92    if (animating == m_animating)
93        return;
94
95    m_animating = animating;
96    if (m_animating) {
97        m_animationStartTime = currentTime();
98        m_animationTimer.startOneShot(m_animationRepeatInterval, FROM_HERE);
99    } else
100        m_animationTimer.stop();
101}
102
103HTMLProgressElement* RenderProgress::progressElement() const
104{
105    if (!node())
106        return 0;
107
108    if (isHTMLProgressElement(*node()))
109        return toHTMLProgressElement(node());
110
111    ASSERT(node()->shadowHost());
112    return toHTMLProgressElement(node()->shadowHost());
113}
114
115} // namespace blink
116