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"
38 #include "HTMLInputElement.h"
39 #include "HTMLParserIdioms.h"
40 #include "MouseEvent.h"
41 #include "RenderSlider.h"
42 #include "RenderTheme.h"
43 #include "StepRange.h"
44 #include <wtf/MathExtras.h>
50 // FIXME: Find a way to cascade appearance (see the layout method) and get rid of this class.
51 class RenderSliderThumb : public RenderBlock {
53 RenderSliderThumb(Node*);
54 virtual void layout();
57 RenderSliderThumb::RenderSliderThumb(Node* node)
62 void RenderSliderThumb::layout()
64 // FIXME: Hard-coding this cascade of appearance is bad, because it's something
65 // that CSS usually does. We need to find a way to express this in CSS.
66 RenderStyle* parentStyle = parent()->style();
67 if (parentStyle->appearance() == SliderVerticalPart)
68 style()->setAppearance(SliderThumbVerticalPart);
69 else if (parentStyle->appearance() == SliderHorizontalPart)
70 style()->setAppearance(SliderThumbHorizontalPart);
71 else if (parentStyle->appearance() == MediaSliderPart)
72 style()->setAppearance(MediaSliderThumbPart);
73 else if (parentStyle->appearance() == MediaVolumeSliderPart)
74 style()->setAppearance(MediaVolumeSliderThumbPart);
76 if (style()->hasAppearance()) {
77 // FIXME: This should pass the style, not the renderer, to the theme.
78 theme()->adjustSliderThumbSize(this);
80 RenderBlock::layout();
83 RenderObject* SliderThumbElement::createRenderer(RenderArena* arena, RenderStyle*)
85 return new (arena) RenderSliderThumb(this);
88 void SliderThumbElement::dragFrom(const IntPoint& point)
94 void SliderThumbElement::setPosition(const IntPoint& point)
96 HTMLInputElement* input = static_cast<HTMLInputElement*>(shadowHost());
99 if (!input->renderer() || !renderer())
102 IntPoint offset = roundedIntPoint(input->renderer()->absoluteToLocal(point, false, true));
103 RenderStyle* sliderStyle = input->renderer()->style();
104 bool isVertical = sliderStyle->appearance() == SliderVerticalPart || sliderStyle->appearance() == MediaVolumeSliderPart;
110 trackSize = input->renderBox()->contentHeight() - renderBox()->height();
111 position = offset.y() - renderBox()->height() / 2;
112 currentPosition = renderBox()->y() - input->renderBox()->contentBoxRect().y();
114 trackSize = input->renderBox()->contentWidth() - renderBox()->width();
115 position = offset.x() - renderBox()->width() / 2;
116 currentPosition = renderBox()->x() - input->renderBox()->contentBoxRect().x();
118 position = max(0, min(position, trackSize));
119 if (position == currentPosition)
122 StepRange range(input);
123 double fraction = static_cast<double>(position) / trackSize;
125 fraction = 1 - fraction;
126 double value = range.clampValue(range.valueFromProportion(fraction));
128 // FIXME: This is no longer being set from renderer. Consider updating the method name.
129 input->setValueFromRenderer(serializeForNumberType(value));
130 renderer()->setNeedsLayout(true);
131 input->dispatchFormControlChangeEvent();
134 void SliderThumbElement::startDragging()
136 if (Frame* frame = document()->frame()) {
137 frame->eventHandler()->setCapturingMouseEventsNode(this);
142 void SliderThumbElement::stopDragging()
147 if (Frame* frame = document()->frame())
148 frame->eventHandler()->setCapturingMouseEventsNode(0);
149 m_inDragMode = false;
151 renderer()->setNeedsLayout(true);
154 void SliderThumbElement::defaultEventHandler(Event* event)
156 if (!event->isMouseEvent()) {
157 HTMLDivElement::defaultEventHandler(event);
161 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
162 bool isLeftButton = mouseEvent->button() == LeftButton;
163 const AtomicString& eventType = event->type();
165 if (eventType == eventNames().mousedownEvent && isLeftButton) {
168 } else if (eventType == eventNames().mouseupEvent && isLeftButton) {
171 } else if (eventType == eventNames().mousemoveEvent) {
173 setPosition(mouseEvent->absoluteLocation());
177 HTMLDivElement::defaultEventHandler(event);
180 void SliderThumbElement::detach()
183 if (Frame* frame = document()->frame())
184 frame->eventHandler()->setCapturingMouseEventsNode(0);
186 HTMLDivElement::detach();
189 const AtomicString& SliderThumbElement::shadowPseudoId() const
191 DEFINE_STATIC_LOCAL(AtomicString, sliderThumb, ("-webkit-slider-thumb"));