Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderSliderContainer.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "core/rendering/RenderSliderContainer.h"
34
35 #include "core/dom/shadow/ShadowRoot.h"
36 #include "core/html/HTMLInputElement.h"
37 #include "core/html/parser/HTMLParserIdioms.h"
38 #include "core/html/shadow/ShadowElementNames.h"
39 #include "core/html/shadow/SliderThumbElement.h"
40 #include "core/rendering/RenderFlexibleBox.h"
41 #include "core/rendering/RenderSlider.h"
42 #include "core/rendering/RenderTheme.h"
43
44 namespace blink {
45
46 RenderSliderContainer::RenderSliderContainer(SliderContainerElement* element)
47     : RenderFlexibleBox(element)
48 {
49 }
50
51 inline static Decimal sliderPosition(HTMLInputElement* element)
52 {
53     const StepRange stepRange(element->createStepRange(RejectAny));
54     const Decimal oldValue = parseToDecimalForNumberType(element->value(), stepRange.defaultValue());
55     return stepRange.proportionFromValue(stepRange.clampValue(oldValue));
56 }
57
58 inline static bool hasVerticalAppearance(HTMLInputElement* input)
59 {
60     ASSERT(input->renderer());
61     RenderStyle* sliderStyle = input->renderer()->style();
62
63     return sliderStyle->appearance() == SliderVerticalPart;
64 }
65
66 void RenderSliderContainer::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
67 {
68     HTMLInputElement* input = toHTMLInputElement(node()->shadowHost());
69     bool isVertical = hasVerticalAppearance(input);
70
71     if (input->renderer()->isSlider() && !isVertical && input->list()) {
72         int offsetFromCenter = RenderTheme::theme().sliderTickOffsetFromTrackCenter();
73         LayoutUnit trackHeight = 0;
74         if (offsetFromCenter < 0) {
75             trackHeight = -2 * offsetFromCenter;
76         } else {
77             int tickLength = RenderTheme::theme().sliderTickSize().height();
78             trackHeight = 2 * (offsetFromCenter + tickLength);
79         }
80         float zoomFactor = style()->effectiveZoom();
81         if (zoomFactor != 1.0)
82             trackHeight *= zoomFactor;
83
84         // FIXME: The trackHeight should have been added before updateLogicalHeight was called to avoid this hack.
85         updateIntrinsicContentLogicalHeight(trackHeight);
86
87         RenderBox::computeLogicalHeight(trackHeight, logicalTop, computedValues);
88         return;
89     }
90     if (isVertical)
91         logicalHeight = RenderSlider::defaultTrackLength;
92
93     // FIXME: The trackHeight should have been added before updateLogicalHeight was called to avoid this hack.
94     updateIntrinsicContentLogicalHeight(logicalHeight);
95
96     RenderBox::computeLogicalHeight(logicalHeight, logicalTop, computedValues);
97 }
98
99 void RenderSliderContainer::layout()
100 {
101     HTMLInputElement* input = toHTMLInputElement(node()->shadowHost());
102     bool isVertical = hasVerticalAppearance(input);
103     style()->setFlexDirection(isVertical ? FlowColumn : FlowRow);
104     TextDirection oldTextDirection = style()->direction();
105     if (isVertical) {
106         // FIXME: Work around rounding issues in RTL vertical sliders. We want them to
107         // render identically to LTR vertical sliders. We can remove this work around when
108         // subpixel rendering is enabled on all ports.
109         style()->setDirection(LTR);
110     }
111
112     Element* thumbElement = input->userAgentShadowRoot()->getElementById(ShadowElementNames::sliderThumb());
113     Element* trackElement = input->userAgentShadowRoot()->getElementById(ShadowElementNames::sliderTrack());
114     RenderBox* thumb = thumbElement ? thumbElement->renderBox() : 0;
115     RenderBox* track = trackElement ? trackElement->renderBox() : 0;
116
117     SubtreeLayoutScope layoutScope(*this);
118     // Force a layout to reset the position of the thumb so the code below doesn't move the thumb to the wrong place.
119     // FIXME: Make a custom Render class for the track and move the thumb positioning code there.
120     if (track)
121         layoutScope.setChildNeedsLayout(track);
122
123     RenderFlexibleBox::layout();
124
125     style()->setDirection(oldTextDirection);
126     // These should always exist, unless someone mutates the shadow DOM (e.g., in the inspector).
127     if (!thumb || !track)
128         return;
129
130     double percentageOffset = sliderPosition(input).toDouble();
131     LayoutUnit availableExtent = isVertical ? track->contentHeight() : track->contentWidth();
132     availableExtent -= isVertical ? thumb->height() : thumb->width();
133     LayoutUnit offset = percentageOffset * availableExtent;
134     LayoutPoint thumbLocation = thumb->location();
135     if (isVertical)
136         thumbLocation.setY(thumbLocation.y() + track->contentHeight() - thumb->height() - offset);
137     else if (style()->isLeftToRightDirection())
138         thumbLocation.setX(thumbLocation.x() + offset);
139     else
140         thumbLocation.setX(thumbLocation.x() - offset);
141     thumb->setLocation(thumbLocation);
142
143     // We need one-off invalidation code here because painting of the timeline element does not go through style.
144     // Instead it has a custom implementation in C++ code.
145     // Therefore the style system cannot understand when it needs to be paint invalidated.
146     setShouldDoFullPaintInvalidation();
147 }
148
149 } // namespace blink