summaryrefslogtreecommitdiffstats
path: root/WebCore/rendering/RenderMeter.cpp
blob: f60e350391ab9e9e3ecdd2ca4c501ca8d634a6b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#if ENABLE(METER_TAG)

#include "RenderMeter.h"

#include "HTMLMeterElement.h"
#include "HTMLNames.h"
#include "RenderTheme.h"
#include "ShadowElement.h"

using namespace std;

namespace WebCore {

using namespace HTMLNames;

RenderMeter::RenderMeter(HTMLMeterElement* element)
    : RenderIndicator(element)
{
}

RenderMeter::~RenderMeter()
{
    if (m_valuePart)
        m_valuePart->detach();
    if (m_barPart)
        m_barPart->detach();
}

void RenderMeter::computeLogicalWidth()
{
    RenderBox::computeLogicalWidth();
    setWidth(theme()->meterSizeForBounds(this, frameRect()).width());
}

void RenderMeter::computeLogicalHeight()
{
    RenderBox::computeLogicalHeight();
    setHeight(theme()->meterSizeForBounds(this, frameRect()).height());
}

void RenderMeter::layoutParts()
{
    // We refresh shadow node here because the state can depend
    // on the frame size of this render object.
    updatePartsState();
    if (m_valuePart)
        m_valuePart->layoutAsPart(valuePartRect());
    if (m_barPart)
        m_barPart->layoutAsPart(barPartRect());
}

bool RenderMeter::shouldHaveParts() const
{
    bool hasTheme = theme()->supportsMeter(style()->appearance(), isHorizontal());
    if (!hasTheme)
        return true;
    bool shadowsHaveStyle = ShadowBlockElement::partShouldHaveStyle(this, barPseudoId()) || ShadowBlockElement::partShouldHaveStyle(this, valuePseudoId());
    if (shadowsHaveStyle)
        return true;
    return false;
}

double RenderMeter::valueRatio() const
{
    HTMLMeterElement* element = static_cast<HTMLMeterElement*>(node());
    double min = element->min();
    double max = element->max();
    double value = element->value();

    if (max <= min)
        return 0;
    return (value - min) / (max - min);
}

IntRect RenderMeter::barPartRect() const
{
    return IntRect(borderLeft() + paddingLeft(), borderTop() + paddingTop(), lround(width() - borderLeft() - paddingLeft() - borderRight() - paddingRight()), height()  - borderTop() - paddingTop() - borderBottom() - paddingBottom());
}

IntRect RenderMeter::valuePartRect() const
{
    IntRect rect = barPartRect();
    
    if (rect.height() <= rect.width()) {
        int width = static_cast<int>(rect.width()*valueRatio());
        if (!style()->isLeftToRightDirection()) {
            rect.setX(rect.x() + (rect.width() - width));
            rect.setWidth(width);
        } else
            rect.setWidth(width);
    } else {
        int height = static_cast<int>(rect.height()*valueRatio());
        rect.setY(rect.y() + (rect.height() - height));
        rect.setHeight(height);
    }

    return rect;
}

bool RenderMeter::isHorizontal() const
{
    IntRect rect = barPartRect();
    return rect.height() <= rect.width();
}

PseudoId RenderMeter::valuePseudoId() const
{
    HTMLMeterElement* element = static_cast<HTMLMeterElement*>(node());

    if (isHorizontal()) {
        switch (element->gaugeRegion()) {
        case HTMLMeterElement::GaugeRegionOptimum:
            return METER_HORIZONTAL_OPTIMUM;
        case HTMLMeterElement::GaugeRegionSuboptimal:
            return METER_HORIZONTAL_SUBOPTIMAL;
        case HTMLMeterElement::GaugeRegionEvenLessGood:
            return METER_HORIZONTAL_EVEN_LESS_GOOD;
        }
    } else {
        switch (element->gaugeRegion()) {
        case HTMLMeterElement::GaugeRegionOptimum:
            return METER_VERTICAL_OPTIMUM;
        case HTMLMeterElement::GaugeRegionSuboptimal:
            return METER_VERTICAL_SUBOPTIMAL;
        case HTMLMeterElement::GaugeRegionEvenLessGood:
            return METER_VERTICAL_EVEN_LESS_GOOD;
        }
    }

    ASSERT_NOT_REACHED();
    return NOPSEUDO;
}

PseudoId RenderMeter::barPseudoId() const
{
    return isHorizontal() ? METER_HORIZONTAL_BAR : METER_VERTICAL_BAR;
}

void RenderMeter::updatePartsState()
{
    if (shouldHaveParts() && !m_barPart) {
        ASSERT(!m_valuePart);
        m_barPart = ShadowBlockElement::createForPart(static_cast<HTMLElement*>(node()), barPseudoId());
        addChild(m_barPart->renderer());
        m_valuePart = ShadowBlockElement::createForPart(static_cast<HTMLElement*>(node()), valuePseudoId());
        addChild(m_valuePart->renderer());
    } else if (!shouldHaveParts() && m_barPart) {
        ASSERT(m_valuePart);
        m_barPart->detach();
        m_barPart = 0;
        m_valuePart->detach();
        m_valuePart = 0;
    }

    if (m_barPart) {
        ASSERT(m_valuePart);
        m_barPart->updateStyleForPart(barPseudoId());
        m_valuePart->updateStyleForPart(valuePseudoId());
    }
}

} // namespace WebCore

#endif