initial import
[vuplus_webkit] / Source / WebCore / rendering / RenderSlider.cpp
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 "RenderSlider.h"
23
24 #include "CSSPropertyNames.h"
25 #include "CSSStyleSelector.h"
26 #include "Document.h"
27 #include "Event.h"
28 #include "EventHandler.h"
29 #include "EventNames.h"
30 #include "Frame.h"
31 #include "HTMLInputElement.h"
32 #include "HTMLNames.h"
33 #include "HTMLParserIdioms.h"
34 #include "MediaControlElements.h"
35 #include "MouseEvent.h"
36 #include "Node.h"
37 #include "RenderLayer.h"
38 #include "RenderTheme.h"
39 #include "RenderView.h"
40 #include "ShadowRoot.h"
41 #include "SliderThumbElement.h"
42 #include "StepRange.h"
43 #include <wtf/MathExtras.h>
44
45 using std::min;
46
47 namespace WebCore {
48
49 static const int defaultTrackLength = 129;
50
51 RenderSlider::RenderSlider(HTMLInputElement* element)
52     : RenderBlock(element)
53 {
54     // We assume RenderSlider works only with <input type=range>.
55     ASSERT(element->isRangeControl());
56 }
57
58 RenderSlider::~RenderSlider()
59 {
60 }
61
62 int RenderSlider::baselinePosition(FontBaseline, bool /*firstLine*/, LineDirectionMode, LinePositionMode) const
63 {
64     // FIXME: Patch this function for writing-mode.
65     return height() + marginTop();
66 }
67
68 void RenderSlider::computePreferredLogicalWidths()
69 {
70     m_minPreferredLogicalWidth = 0;
71     m_maxPreferredLogicalWidth = 0;
72
73     if (style()->width().isFixed() && style()->width().value() > 0)
74         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeContentBoxLogicalWidth(style()->width().value());
75     else
76         m_maxPreferredLogicalWidth = defaultTrackLength * style()->effectiveZoom();
77
78     if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
79         m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
80         m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
81     } else if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent()))
82         m_minPreferredLogicalWidth = 0;
83     else
84         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth;
85     
86     if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength) {
87         m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
88         m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
89     }
90
91     LayoutUnit toAdd = borderAndPaddingWidth();
92     m_minPreferredLogicalWidth += toAdd;
93     m_maxPreferredLogicalWidth += toAdd;
94
95     setPreferredLogicalWidthsDirty(false); 
96 }
97
98 void RenderSlider::layout()
99 {
100     // FIXME: Find a way to cascade appearance.
101     // http://webkit.org/b/62535
102     RenderBox* thumbBox = sliderThumbElementOf(node())->renderBox();
103     if (thumbBox && thumbBox->isSliderThumb())
104         static_cast<RenderSliderThumb*>(thumbBox)->updateAppearance(style());
105     if (RenderObject* limiterRenderer = trackLimiterElementOf(node())->renderer()) {
106         if (limiterRenderer->isSliderThumb())
107           static_cast<RenderSliderThumb*>(limiterRenderer)->updateAppearance(style());
108     }
109
110     RenderBlock::layout();
111
112     if (!thumbBox)
113         return;
114     LayoutUnit heightDiff = thumbBox->height() - contentHeight();
115     if (heightDiff > 0)
116         thumbBox->setY(thumbBox->y() - (heightDiff / 2));
117 }
118
119 bool RenderSlider::inDragMode() const
120 {
121     return sliderThumbElementOf(node())->active();
122 }
123
124 } // namespace WebCore