1/*
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
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 "core/rendering/RenderSlider.h"
23
24#include "core/InputTypeNames.h"
25#include "core/dom/shadow/ShadowRoot.h"
26#include "core/html/HTMLInputElement.h"
27#include "core/html/shadow/ShadowElementNames.h"
28#include "core/html/shadow/SliderThumbElement.h"
29#include "core/rendering/RenderSliderThumb.h"
30#include "wtf/MathExtras.h"
31
32using std::min;
33
34namespace blink {
35
36const int RenderSlider::defaultTrackLength = 129;
37
38RenderSlider::RenderSlider(HTMLInputElement* element)
39    : RenderFlexibleBox(element)
40{
41    // We assume RenderSlider works only with <input type=range>.
42    ASSERT(element->type() == InputTypeNames::range);
43}
44
45RenderSlider::~RenderSlider()
46{
47}
48
49int RenderSlider::baselinePosition(FontBaseline, bool /*firstLine*/, LineDirectionMode, LinePositionMode linePositionMode) const
50{
51    ASSERT(linePositionMode == PositionOnContainingLine);
52    // FIXME: Patch this function for writing-mode.
53    return height() + marginTop();
54}
55
56void RenderSlider::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
57{
58    maxLogicalWidth = defaultTrackLength * style()->effectiveZoom();
59    if (!style()->width().isPercent())
60        minLogicalWidth = maxLogicalWidth;
61}
62
63void RenderSlider::computePreferredLogicalWidths()
64{
65    m_minPreferredLogicalWidth = 0;
66    m_maxPreferredLogicalWidth = 0;
67    RenderStyle* styleToUse = style();
68
69    if (styleToUse->width().isFixed() && styleToUse->width().value() > 0)
70        m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(styleToUse->width().value());
71    else
72        computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
73
74    if (styleToUse->minWidth().isFixed() && styleToUse->minWidth().value() > 0) {
75        m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->minWidth().value()));
76        m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->minWidth().value()));
77    }
78
79    if (styleToUse->maxWidth().isFixed()) {
80        m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->maxWidth().value()));
81        m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->maxWidth().value()));
82    }
83
84    LayoutUnit toAdd = borderAndPaddingWidth();
85    m_minPreferredLogicalWidth += toAdd;
86    m_maxPreferredLogicalWidth += toAdd;
87
88    clearPreferredLogicalWidthsDirty();
89}
90
91inline SliderThumbElement* RenderSlider::sliderThumbElement() const
92{
93    return toSliderThumbElement(toElement(node())->userAgentShadowRoot()->getElementById(ShadowElementNames::sliderThumb()));
94}
95
96void RenderSlider::layout()
97{
98    // FIXME: Find a way to cascade appearance.
99    // http://webkit.org/b/62535
100    RenderBox* thumbBox = sliderThumbElement()->renderBox();
101    if (thumbBox && thumbBox->isSliderThumb())
102        static_cast<RenderSliderThumb*>(thumbBox)->updateAppearance(style());
103
104    RenderFlexibleBox::layout();
105}
106
107bool RenderSlider::inDragMode() const
108{
109    return sliderThumbElement()->active();
110}
111
112} // namespace blink
113