Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / layout / LayoutSliderContainer.cpp
blob23e8e0fd1eaf7389740a4fb7e30fafe2f9b57277
1 /*
2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
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.
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.
32 #include "config.h"
33 #include "core/layout/LayoutSliderContainer.h"
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/layout/LayoutFlexibleBox.h"
41 #include "core/layout/LayoutSlider.h"
42 #include "core/layout/LayoutTheme.h"
44 namespace blink {
46 LayoutSliderContainer::LayoutSliderContainer(SliderContainerElement* element)
47 : LayoutFlexibleBox(element)
51 inline static Decimal sliderPosition(HTMLInputElement* element)
53 const StepRange stepRange(element->createStepRange(RejectAny));
54 const Decimal oldValue = parseToDecimalForNumberType(element->value(), stepRange.defaultValue());
55 return stepRange.proportionFromValue(stepRange.clampValue(oldValue));
58 inline static bool hasVerticalAppearance(HTMLInputElement* input)
60 ASSERT(input->layoutObject());
61 const ComputedStyle& sliderStyle = input->layoutObject()->styleRef();
63 return sliderStyle.appearance() == SliderVerticalPart;
66 void LayoutSliderContainer::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
68 HTMLInputElement* input = toHTMLInputElement(node()->shadowHost());
69 bool isVertical = hasVerticalAppearance(input);
71 if (input->layoutObject()->isSlider() && !isVertical && input->list()) {
72 int offsetFromCenter = LayoutTheme::theme().sliderTickOffsetFromTrackCenter();
73 LayoutUnit trackHeight = 0;
74 if (offsetFromCenter < 0) {
75 trackHeight = -2 * offsetFromCenter;
76 } else {
77 int tickLength = LayoutTheme::theme().sliderTickSize().height();
78 trackHeight = 2 * (offsetFromCenter + tickLength);
80 float zoomFactor = style()->effectiveZoom();
81 if (zoomFactor != 1.0)
82 trackHeight *= zoomFactor;
84 // FIXME: The trackHeight should have been added before updateLogicalHeight was called to avoid this hack.
85 setIntrinsicContentLogicalHeight(trackHeight);
87 LayoutBox::computeLogicalHeight(trackHeight, logicalTop, computedValues);
88 return;
90 if (isVertical)
91 logicalHeight = LayoutSlider::defaultTrackLength;
93 // FIXME: The trackHeight should have been added before updateLogicalHeight was called to avoid this hack.
94 setIntrinsicContentLogicalHeight(logicalHeight);
96 LayoutBox::computeLogicalHeight(logicalHeight, logicalTop, computedValues);
99 void LayoutSliderContainer::layout()
101 HTMLInputElement* input = toHTMLInputElement(node()->shadowHost());
102 bool isVertical = hasVerticalAppearance(input);
103 mutableStyleRef().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 mutableStyleRef().setDirection(LTR);
112 Element* thumbElement = input->userAgentShadowRoot()->getElementById(ShadowElementNames::sliderThumb());
113 Element* trackElement = input->userAgentShadowRoot()->getElementById(ShadowElementNames::sliderTrack());
114 LayoutBox* thumb = thumbElement ? thumbElement->layoutBox() : 0;
115 LayoutBox* track = trackElement ? trackElement->layoutBox() : 0;
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 layout class for the track and move the thumb positioning code there.
120 if (track)
121 layoutScope.setChildNeedsLayout(track);
123 LayoutFlexibleBox::layout();
125 mutableStyleRef().setDirection(oldTextDirection);
126 // These should always exist, unless someone mutates the shadow DOM (e.g., in the inspector).
127 if (!thumb || !track)
128 return;
130 double percentageOffset = sliderPosition(input).toDouble();
131 LayoutUnit availableExtent = isVertical ? track->contentHeight() : track->contentWidth();
132 availableExtent -= isVertical ? thumb->size().height() : thumb->size().width();
133 LayoutUnit offset = percentageOffset * availableExtent;
134 LayoutPoint thumbLocation = thumb->location();
135 if (isVertical)
136 thumbLocation.setY(thumbLocation.y() + track->contentHeight() - thumb->size().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);
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();
149 } // namespace blink