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 "RenderProgress.h"
23
24#include "HTMLNames.h"
25#include "HTMLProgressElement.h"
26#include "PaintInfo.h"
27#include "RenderTheme.h"
28#include <wtf/CurrentTime.h>
29#include <wtf/RefPtr.h>
30
31namespace WebCore {
32
33RenderProgress::RenderProgress(HTMLElement& element, PassRef<RenderStyle> style)
34    : RenderBlockFlow(element, WTF::move(style))
35    , m_position(HTMLProgressElement::InvalidPosition)
36    , m_animationStartTime(0)
37    , m_animationRepeatInterval(0)
38    , m_animationDuration(0)
39    , m_animating(false)
40    , m_animationTimer(this, &RenderProgress::animationTimerFired)
41{
42}
43
44RenderProgress::~RenderProgress()
45{
46}
47
48void RenderProgress::updateFromElement()
49{
50    HTMLProgressElement* element = progressElement();
51    if (m_position == element->position())
52        return;
53    m_position = element->position();
54
55    updateAnimationState();
56    repaint();
57    RenderBlockFlow::updateFromElement();
58}
59
60void RenderProgress::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
61{
62    RenderBox::computeLogicalHeight(logicalHeight, logicalTop, computedValues);
63
64    LayoutRect frame = frameRect();
65    if (isHorizontalWritingMode())
66        frame.setHeight(computedValues.m_extent);
67    else
68        frame.setWidth(computedValues.m_extent);
69    IntSize frameSize = theme().progressBarRectForBounds(*this, pixelSnappedIntRect(frame)).size();
70    computedValues.m_extent = isHorizontalWritingMode() ? frameSize.height() : frameSize.width();
71}
72
73double RenderProgress::animationProgress() const
74{
75    return m_animating ? (fmod((monotonicallyIncreasingTime() - m_animationStartTime), m_animationDuration) / m_animationDuration) : 0;
76}
77
78bool RenderProgress::isDeterminate() const
79{
80    return (HTMLProgressElement::IndeterminatePosition != position()
81            && HTMLProgressElement::InvalidPosition != position());
82}
83
84void RenderProgress::animationTimerFired(Timer<RenderProgress>&)
85{
86    repaint();
87    if (!m_animationTimer.isActive() && m_animating)
88        m_animationTimer.startOneShot(m_animationRepeatInterval);
89}
90
91void RenderProgress::updateAnimationState()
92{
93    m_animationDuration = theme().animationDurationForProgressBar(*this);
94    m_animationRepeatInterval = theme().animationRepeatIntervalForProgressBar(*this);
95
96    bool animating = style().hasAppearance() && m_animationDuration > 0;
97    if (animating == m_animating)
98        return;
99
100    m_animating = animating;
101    if (m_animating) {
102        m_animationStartTime = monotonicallyIncreasingTime();
103        m_animationTimer.startOneShot(m_animationRepeatInterval);
104    } else
105        m_animationTimer.stop();
106}
107
108HTMLProgressElement* RenderProgress::progressElement() const
109{
110    if (!element())
111        return 0;
112
113    if (isHTMLProgressElement(element()))
114        return toHTMLProgressElement(element());
115
116    ASSERT(element()->shadowHost());
117    return toHTMLProgressElement(element()->shadowHost());
118}
119
120} // namespace WebCore
121
122