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
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
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.
34 #include "SliderThumbElement.h"
36 #include "CSSValueKeywords.h"
37 #include "ElementShadow.h"
40 #include "HTMLInputElement.h"
41 #include "HTMLParserIdioms.h"
42 #include "MouseEvent.h"
43 #include "RenderDeprecatedFlexibleBox.h"
44 #include "RenderSlider.h"
45 #include "RenderTheme.h"
46 #include "ShadowRoot.h"
47 #include "StepRange.h"
48 #include <wtf/MathExtras.h>
54 inline static InputNumber sliderPosition(HTMLInputElement* element)
56 const StepRange stepRange(element->createStepRange(RejectAny));
57 const InputNumber oldValue = parseToDecimalForNumberType(element->value(), stepRange.defaultValue());
58 return stepRange.proportionFromValue(stepRange.clampValue(oldValue));
61 inline static bool hasVerticalAppearance(HTMLInputElement* input)
63 ASSERT(input->renderer());
64 RenderStyle* sliderStyle = input->renderer()->style();
65 return sliderStyle->appearance() == SliderVerticalPart || sliderStyle->appearance() == MediaVolumeSliderPart;
68 SliderThumbElement* sliderThumbElementOf(Node* node)
71 ShadowRoot* shadow = node->toInputElement()->shadow()->oldestShadowRoot();
73 Node* thumb = shadow->firstChild()->firstChild()->firstChild();
75 return toSliderThumbElement(thumb);
78 // --------------------------------
80 RenderSliderThumb::RenderSliderThumb(Node* node)
85 void RenderSliderThumb::updateAppearance(RenderStyle* parentStyle)
87 if (parentStyle->appearance() == SliderVerticalPart)
88 style()->setAppearance(SliderThumbVerticalPart);
89 else if (parentStyle->appearance() == SliderHorizontalPart)
90 style()->setAppearance(SliderThumbHorizontalPart);
91 else if (parentStyle->appearance() == MediaSliderPart)
92 style()->setAppearance(MediaSliderThumbPart);
93 else if (parentStyle->appearance() == MediaVolumeSliderPart)
94 style()->setAppearance(MediaVolumeSliderThumbPart);
95 else if (parentStyle->appearance() == MediaFullScreenVolumeSliderPart)
96 style()->setAppearance(MediaFullScreenVolumeSliderThumbPart);
97 if (style()->hasAppearance())
98 theme()->adjustSliderThumbSize(style(), toElement(node()));
101 bool RenderSliderThumb::isSliderThumb() const
106 void RenderSliderThumb::layout()
108 // Do not cast node() to SliderThumbElement. This renderer is used for
109 // TrackLimitElement too.
110 HTMLInputElement* input = node()->shadowAncestorNode()->toInputElement();
111 bool isVertical = style()->appearance() == SliderThumbVerticalPart || style()->appearance() == MediaVolumeSliderThumbPart;
113 double fraction = convertInputNumberToDouble(sliderPosition(input) * 100);
115 style()->setTop(Length(100 - fraction, Percent));
116 else if (style()->isLeftToRightDirection())
117 style()->setLeft(Length(fraction, Percent));
119 style()->setRight(Length(fraction, Percent));
121 RenderBlock::layout();
124 // --------------------------------
126 // FIXME: Find a way to cascade appearance and adjust heights, and get rid of this class.
127 // http://webkit.org/b/62535
128 class RenderSliderContainer : public RenderDeprecatedFlexibleBox {
130 RenderSliderContainer(Node* node)
131 : RenderDeprecatedFlexibleBox(node) { }
134 virtual void layout();
137 void RenderSliderContainer::layout()
139 HTMLInputElement* input = node()->shadowAncestorNode()->toInputElement();
140 bool isVertical = hasVerticalAppearance(input);
141 style()->setBoxOrient(isVertical ? VERTICAL : HORIZONTAL);
142 // Sets the concrete height if the height of the <input> is not fixed or a
143 // percentage value because a percentage height value of this box won't be
144 // based on the <input> height in such case.
145 Length inputHeight = input->renderer()->style()->height();
146 RenderObject* trackRenderer = node()->firstChild()->renderer();
147 if (!isVertical && input->renderer()->isSlider() && !inputHeight.isFixed() && !inputHeight.isPercent()) {
148 RenderObject* thumbRenderer = input->shadow()->oldestShadowRoot()->firstChild()->firstChild()->firstChild()->renderer();
150 style()->setHeight(thumbRenderer->style()->height());
152 trackRenderer->style()->setHeight(thumbRenderer->style()->height());
155 style()->setHeight(Length(100, Percent));
157 trackRenderer->style()->setHeight(Length());
160 RenderDeprecatedFlexibleBox::layout();
162 // Percentage 'top' for the thumb doesn't work if the parent style has no
164 Node* track = node()->firstChild();
165 if (track && track->renderer()->isBox()) {
166 RenderBox* trackBox = track->renderBox();
167 trackBox->style()->setHeight(Length(trackBox->height() - trackBox->borderAndPaddingHeight(), Fixed));
171 // --------------------------------
173 void SliderThumbElement::setPositionFromValue()
175 // Since the code to calculate position is in the RenderSliderThumb layout
176 // path, we don't actually update the value here. Instead, we poke at the
177 // renderer directly to trigger layout.
179 renderer()->setNeedsLayout(true);
182 RenderObject* SliderThumbElement::createRenderer(RenderArena* arena, RenderStyle*)
184 return new (arena) RenderSliderThumb(this);
187 bool SliderThumbElement::isEnabledFormControl() const
189 return hostInput()->isEnabledFormControl();
192 bool SliderThumbElement::isReadOnlyFormControl() const
194 return hostInput()->isReadOnlyFormControl();
197 Node* SliderThumbElement::focusDelegate()
202 void SliderThumbElement::dragFrom(const LayoutPoint& point)
204 setPositionFromPoint(point);
208 void SliderThumbElement::setPositionFromPoint(const LayoutPoint& point)
210 HTMLInputElement* input = hostInput();
212 if (!input->renderer() || !renderer())
215 LayoutPoint offset = roundedLayoutPoint(input->renderer()->absoluteToLocal(point, false, true));
216 bool isVertical = hasVerticalAppearance(input);
217 LayoutUnit trackSize;
219 LayoutUnit currentPosition;
220 // We need to calculate currentPosition from absolute points becaue the
221 // renderer for this node is usually on a layer and renderBox()->x() and
223 // FIXME: This should probably respect transforms.
224 LayoutPoint absoluteThumbOrigin = renderBox()->absoluteBoundingBoxRectIgnoringTransforms().location();
225 LayoutPoint absoluteSliderContentOrigin = roundedLayoutPoint(input->renderer()->localToAbsolute());
227 trackSize = input->renderBox()->contentHeight() - renderBox()->height();
228 position = offset.y() - renderBox()->height() / 2;
229 currentPosition = absoluteThumbOrigin.y() - absoluteSliderContentOrigin.y();
231 trackSize = input->renderBox()->contentWidth() - renderBox()->width();
232 position = offset.x() - renderBox()->width() / 2;
233 currentPosition = absoluteThumbOrigin.x() - absoluteSliderContentOrigin.x();
235 position = max<LayoutUnit>(0, min(position, trackSize));
236 if (position == currentPosition)
239 const InputNumber ratio = convertDoubleToInputNumber(static_cast<double>(position) / trackSize);
240 const InputNumber fraction = isVertical || !renderBox()->style()->isLeftToRightDirection() ? InputNumber(1) - ratio : ratio;
241 StepRange stepRange(input->createStepRange(RejectAny));
242 const InputNumber value = stepRange.clampValue(stepRange.valueFromProportion(fraction));
244 // FIXME: This is no longer being set from renderer. Consider updating the method name.
245 input->setValueFromRenderer(serializeForNumberType(value));
246 renderer()->setNeedsLayout(true);
247 input->dispatchFormControlChangeEvent();
250 void SliderThumbElement::startDragging()
252 if (Frame* frame = document()->frame()) {
253 frame->eventHandler()->setCapturingMouseEventsNode(this);
258 void SliderThumbElement::stopDragging()
263 if (Frame* frame = document()->frame())
264 frame->eventHandler()->setCapturingMouseEventsNode(0);
265 m_inDragMode = false;
267 renderer()->setNeedsLayout(true);
270 void SliderThumbElement::defaultEventHandler(Event* event)
272 if (!event->isMouseEvent()) {
273 HTMLDivElement::defaultEventHandler(event);
277 // FIXME: Should handle this readonly/disabled check in more general way.
278 // Missing this kind of check is likely to occur elsewhere if adding it in each shadow element.
279 HTMLInputElement* input = hostInput();
280 if (!input || input->isReadOnlyFormControl() || !input->isEnabledFormControl()) {
282 HTMLDivElement::defaultEventHandler(event);
286 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
287 bool isLeftButton = mouseEvent->button() == LeftButton;
288 const AtomicString& eventType = event->type();
290 // We intentionally do not call event->setDefaultHandled() here because
291 // MediaControlTimelineElement::defaultEventHandler() wants to handle these
293 if (eventType == eventNames().mousedownEvent && isLeftButton) {
296 } else if (eventType == eventNames().mouseupEvent && isLeftButton) {
299 } else if (eventType == eventNames().mousemoveEvent) {
301 setPositionFromPoint(mouseEvent->absoluteLocation());
305 HTMLDivElement::defaultEventHandler(event);
308 void SliderThumbElement::detach()
311 if (Frame* frame = document()->frame())
312 frame->eventHandler()->setCapturingMouseEventsNode(0);
314 HTMLDivElement::detach();
317 HTMLInputElement* SliderThumbElement::hostInput() const
319 // Only HTMLInputElement creates SliderThumbElement instances as its shadow nodes.
320 // So, shadowAncestorNode() must be an HTMLInputElement.
321 return shadowAncestorNode()->toInputElement();
324 const AtomicString& SliderThumbElement::shadowPseudoId() const
326 DEFINE_STATIC_LOCAL(AtomicString, sliderThumb, ("-webkit-slider-thumb"));
330 // --------------------------------
332 inline TrackLimiterElement::TrackLimiterElement(Document* document)
333 : HTMLDivElement(HTMLNames::divTag, document)
337 PassRefPtr<TrackLimiterElement> TrackLimiterElement::create(Document* document)
339 RefPtr<TrackLimiterElement> element = adoptRef(new TrackLimiterElement(document));
341 element->setInlineStyleProperty(CSSPropertyVisibility, CSSValueHidden);
342 element->setInlineStyleProperty(CSSPropertyPosition, CSSValueStatic);
344 return element.release();
347 RenderObject* TrackLimiterElement::createRenderer(RenderArena* arena, RenderStyle*)
349 return new (arena) RenderSliderThumb(this);
352 const AtomicString& TrackLimiterElement::shadowPseudoId() const
354 DEFINE_STATIC_LOCAL(AtomicString, sliderThumb, ("-webkit-slider-thumb"));
358 TrackLimiterElement* trackLimiterElementOf(Node* node)
361 ASSERT(node->toInputElement()->shadow());
362 ShadowRoot* shadow = node->toInputElement()->shadow()->oldestShadowRoot();
364 Node* limiter = shadow->firstChild()->lastChild();
366 return static_cast<TrackLimiterElement*>(limiter);
369 // --------------------------------
371 inline SliderContainerElement::SliderContainerElement(Document* document)
372 : HTMLDivElement(HTMLNames::divTag, document)
376 PassRefPtr<SliderContainerElement> SliderContainerElement::create(Document* document)
378 return adoptRef(new SliderContainerElement(document));
381 RenderObject* SliderContainerElement::createRenderer(RenderArena* arena, RenderStyle*)
383 return new (arena) RenderSliderContainer(this);
386 const AtomicString& SliderContainerElement::shadowPseudoId() const
388 DEFINE_STATIC_LOCAL(AtomicString, sliderThumb, ("-webkit-slider-container"));