initial import
[vuplus_webkit] / Source / WebCore / html / shadow / SliderThumbElement.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
33 #include "config.h"
34 #include "SliderThumbElement.h"
35
36 #include "CSSValueKeywords.h"
37 #include "Event.h"
38 #include "Frame.h"
39 #include "HTMLInputElement.h"
40 #include "HTMLParserIdioms.h"
41 #include "MouseEvent.h"
42 #include "RenderDeprecatedFlexibleBox.h"
43 #include "RenderSlider.h"
44 #include "RenderTheme.h"
45 #include "ShadowRoot.h"
46 #include "StepRange.h"
47 #include <wtf/MathExtras.h>
48
49 using namespace std;
50
51 namespace WebCore {
52
53 inline static double sliderPosition(HTMLInputElement* element)
54 {
55     StepRange range(element);
56     return range.proportionFromValue(range.valueFromElement(element));
57 }
58
59 inline static bool hasVerticalAppearance(HTMLInputElement* input)
60 {
61     ASSERT(input->renderer());
62     RenderStyle* sliderStyle = input->renderer()->style();
63     return sliderStyle->appearance() == SliderVerticalPart || sliderStyle->appearance() == MediaVolumeSliderPart;
64 }
65
66 SliderThumbElement* sliderThumbElementOf(Node* node)
67 {
68     ASSERT(node);
69     ShadowRoot* shadow = node->toInputElement()->shadowRoot();
70     ASSERT(shadow);
71     Node* thumb = shadow->firstChild()->firstChild()->firstChild();
72     ASSERT(thumb);
73     return toSliderThumbElement(thumb);
74 }
75
76 // --------------------------------
77
78 RenderSliderThumb::RenderSliderThumb(Node* node)
79     : RenderBlock(node)
80 {
81 }
82
83 void RenderSliderThumb::updateAppearance(RenderStyle* parentStyle)
84 {
85     if (parentStyle->appearance() == SliderVerticalPart)
86         style()->setAppearance(SliderThumbVerticalPart);
87     else if (parentStyle->appearance() == SliderHorizontalPart)
88         style()->setAppearance(SliderThumbHorizontalPart);
89     else if (parentStyle->appearance() == MediaSliderPart)
90         style()->setAppearance(MediaSliderThumbPart);
91     else if (parentStyle->appearance() == MediaVolumeSliderPart)
92         style()->setAppearance(MediaVolumeSliderThumbPart);
93     if (style()->hasAppearance())
94         theme()->adjustSliderThumbSize(style());
95 }
96
97 bool RenderSliderThumb::isSliderThumb() const
98 {
99     return true;
100 }
101
102 void RenderSliderThumb::layout()
103 {
104     // Do not cast node() to SliderThumbElement. This renderer is used for
105     // TrackLimitElement too.
106     HTMLInputElement* input = node()->shadowAncestorNode()->toInputElement();
107     bool isVertical = style()->appearance() == SliderThumbVerticalPart || style()->appearance() == MediaVolumeSliderThumbPart;
108
109     double fraction = sliderPosition(input) * 100;
110     if (isVertical)
111         style()->setTop(Length(100 - fraction, Percent));
112     else if (style()->isLeftToRightDirection())
113         style()->setLeft(Length(fraction, Percent));
114     else
115         style()->setRight(Length(fraction, Percent));
116
117     RenderBlock::layout();
118 }
119
120 // --------------------------------
121
122 // FIXME: Find a way to cascade appearance and adjust heights, and get rid of this class.
123 // http://webkit.org/b/62535
124 class RenderSliderContainer : public RenderDeprecatedFlexibleBox {
125 public:
126     RenderSliderContainer(Node* node)
127         : RenderDeprecatedFlexibleBox(node) { }
128
129 private:
130     virtual void layout();
131 };
132
133 void RenderSliderContainer::layout()
134 {
135     HTMLInputElement* input = node()->shadowAncestorNode()->toInputElement();
136     bool isVertical = hasVerticalAppearance(input);
137     style()->setBoxOrient(isVertical ? VERTICAL : HORIZONTAL);
138     // Sets the concrete height if the height of the <input> is not fixed or a
139     // percentage value because a percentage height value of this box won't be
140     // based on the <input> height in such case.
141     Length inputHeight = input->renderer()->style()->height();
142     RenderObject* trackRenderer = node()->firstChild()->renderer();
143     if (!isVertical && input->renderer()->isSlider() && !inputHeight.isFixed() && !inputHeight.isPercent()) {
144         RenderObject* thumbRenderer = input->shadowRoot()->firstChild()->firstChild()->firstChild()->renderer();
145         if (thumbRenderer) {
146             style()->setHeight(thumbRenderer->style()->height());
147             if (trackRenderer)
148                 trackRenderer->style()->setHeight(thumbRenderer->style()->height());
149         }
150     } else {
151         style()->setHeight(Length(100, Percent));
152         if (trackRenderer)
153             trackRenderer->style()->setHeight(Length());
154     }
155
156     RenderDeprecatedFlexibleBox::layout();
157
158     // Percentage 'top' for the thumb doesn't work if the parent style has no
159     // concrete height.
160     Node* track = node()->firstChild();
161     if (track && track->renderer()->isBox()) {
162         RenderBox* trackBox = track->renderBox();
163         trackBox->style()->setHeight(Length(trackBox->height() - trackBox->borderAndPaddingHeight(), Fixed));
164     }
165 }
166
167 // --------------------------------
168
169 void SliderThumbElement::setPositionFromValue()
170 {
171     // Since the code to calculate position is in the RenderSliderThumb layout
172     // path, we don't actually update the value here. Instead, we poke at the
173     // renderer directly to trigger layout.
174     if (renderer())
175         renderer()->setNeedsLayout(true);
176 }
177
178 RenderObject* SliderThumbElement::createRenderer(RenderArena* arena, RenderStyle*)
179 {
180     return new (arena) RenderSliderThumb(this);
181 }
182
183 bool SliderThumbElement::isEnabledFormControl() const
184 {
185     return hostInput()->isEnabledFormControl();
186 }
187
188 bool SliderThumbElement::isReadOnlyFormControl() const
189 {
190     return hostInput()->isReadOnlyFormControl();
191 }
192
193 Node* SliderThumbElement::focusDelegate()
194 {
195     return hostInput();
196 }
197
198 void SliderThumbElement::dragFrom(const LayoutPoint& point)
199 {
200     setPositionFromPoint(point);
201     startDragging();
202 }
203
204 void SliderThumbElement::setPositionFromPoint(const LayoutPoint& point)
205 {
206     HTMLInputElement* input = hostInput();
207
208     if (!input->renderer() || !renderer())
209         return;
210
211     LayoutPoint offset = roundedLayoutPoint(input->renderer()->absoluteToLocal(point, false, true));
212     bool isVertical = hasVerticalAppearance(input);
213     LayoutUnit trackSize;
214     LayoutUnit position;
215     LayoutUnit currentPosition;
216     // We need to calculate currentPosition from absolute points becaue the
217     // renderer for this node is usually on a layer and renderBox()->x() and
218     // y() are unusable.
219     LayoutPoint absoluteThumbOrigin = renderBox()->absoluteBoundingBoxRect().location();
220     LayoutPoint absoluteSliderContentOrigin = roundedLayoutPoint(input->renderer()->localToAbsolute());
221     if (isVertical) {
222         trackSize = input->renderBox()->contentHeight() - renderBox()->height();
223         position = offset.y() - renderBox()->height() / 2;
224         currentPosition = absoluteThumbOrigin.y() - absoluteSliderContentOrigin.y();
225     } else {
226         trackSize = input->renderBox()->contentWidth() - renderBox()->width();
227         position = offset.x() - renderBox()->width() / 2;
228         currentPosition = absoluteThumbOrigin.x() - absoluteSliderContentOrigin.x();
229     }
230     position = max<LayoutUnit>(0, min(position, trackSize));
231     if (position == currentPosition)
232         return;
233
234     StepRange range(input);
235     double fraction = static_cast<double>(position) / trackSize;
236     if (isVertical || !renderBox()->style()->isLeftToRightDirection())
237         fraction = 1 - fraction;
238     double value = range.clampValue(range.valueFromProportion(fraction));
239
240     // FIXME: This is no longer being set from renderer. Consider updating the method name.
241     input->setValueFromRenderer(serializeForNumberType(value));
242     renderer()->setNeedsLayout(true);
243     input->dispatchFormControlChangeEvent();
244 }
245
246 void SliderThumbElement::startDragging()
247 {
248     if (Frame* frame = document()->frame()) {
249         frame->eventHandler()->setCapturingMouseEventsNode(this);
250         m_inDragMode = true;
251     }
252 }
253
254 void SliderThumbElement::stopDragging()
255 {
256     if (!m_inDragMode)
257         return;
258
259     if (Frame* frame = document()->frame())
260         frame->eventHandler()->setCapturingMouseEventsNode(0);
261     m_inDragMode = false;
262     if (renderer())
263         renderer()->setNeedsLayout(true);
264 }
265
266 void SliderThumbElement::defaultEventHandler(Event* event)
267 {
268     if (!event->isMouseEvent()) {
269         HTMLDivElement::defaultEventHandler(event);
270         return;
271     }
272
273     // FIXME: Should handle this readonly/disabled check in more general way.
274     // Missing this kind of check is likely to occur elsewhere if adding it in each shadow element.
275     HTMLInputElement* input = hostInput();
276     if (!input || input->isReadOnlyFormControl() || !input->isEnabledFormControl()) {
277         HTMLDivElement::defaultEventHandler(event);
278         return;
279     }
280
281     MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
282     bool isLeftButton = mouseEvent->button() == LeftButton;
283     const AtomicString& eventType = event->type();
284
285     // We intentionally do not call event->setDefaultHandled() here because
286     // MediaControlTimelineElement::defaultEventHandler() wants to handle these
287     // mouse events.
288     if (eventType == eventNames().mousedownEvent && isLeftButton) {
289         startDragging();
290         return;
291     } else if (eventType == eventNames().mouseupEvent && isLeftButton) {
292         stopDragging();
293         return;
294     } else if (eventType == eventNames().mousemoveEvent) {
295         if (m_inDragMode)
296             setPositionFromPoint(mouseEvent->absoluteLocation());
297         return;
298     }
299
300     HTMLDivElement::defaultEventHandler(event);
301 }
302
303 void SliderThumbElement::detach()
304 {
305     if (m_inDragMode) {
306         if (Frame* frame = document()->frame())
307             frame->eventHandler()->setCapturingMouseEventsNode(0);
308     }
309     HTMLDivElement::detach();
310 }
311
312 HTMLInputElement* SliderThumbElement::hostInput() const
313 {
314     // Only HTMLInputElement creates SliderThumbElement instances as its shadow nodes.
315     // So, shadowAncestorNode() must be an HTMLInputElement.
316     return shadowAncestorNode()->toInputElement();
317 }
318
319 const AtomicString& SliderThumbElement::shadowPseudoId() const
320 {
321     DEFINE_STATIC_LOCAL(AtomicString, sliderThumb, ("-webkit-slider-thumb"));
322     return sliderThumb;
323 }
324
325 // --------------------------------
326
327 inline TrackLimiterElement::TrackLimiterElement(Document* document)
328     : HTMLDivElement(HTMLNames::divTag, document)
329 {
330 }
331
332 PassRefPtr<TrackLimiterElement> TrackLimiterElement::create(Document* document)
333 {
334     RefPtr<TrackLimiterElement> element = adoptRef(new TrackLimiterElement(document));
335     element->getInlineStyleDecl()->setProperty(CSSPropertyVisibility, CSSValueHidden);
336     element->getInlineStyleDecl()->setProperty(CSSPropertyPosition, CSSValueStatic);
337     return element.release();
338 }
339
340 RenderObject* TrackLimiterElement::createRenderer(RenderArena* arena, RenderStyle*)
341 {
342     return new (arena) RenderSliderThumb(this);
343 }
344
345 const AtomicString& TrackLimiterElement::shadowPseudoId() const
346 {
347     DEFINE_STATIC_LOCAL(AtomicString, sliderThumb, ("-webkit-slider-thumb"));
348     return sliderThumb;
349 }
350
351 TrackLimiterElement* trackLimiterElementOf(Node* node)
352 {
353     ASSERT(node);
354     ShadowRoot* shadow = node->toInputElement()->shadowRoot();
355     ASSERT(shadow);
356     Node* limiter = shadow->firstChild()->lastChild();
357     ASSERT(limiter);
358     return static_cast<TrackLimiterElement*>(limiter);
359 }
360
361 // --------------------------------
362
363 inline SliderContainerElement::SliderContainerElement(Document* document)
364     : HTMLDivElement(HTMLNames::divTag, document)
365 {
366 }
367
368 PassRefPtr<SliderContainerElement> SliderContainerElement::create(Document* document)
369 {
370     return adoptRef(new SliderContainerElement(document));
371 }
372
373 RenderObject* SliderContainerElement::createRenderer(RenderArena* arena, RenderStyle*)
374 {
375     return new (arena) RenderSliderContainer(this);
376 }
377
378 const AtomicString& SliderContainerElement::shadowPseudoId() const
379 {
380     DEFINE_STATIC_LOCAL(AtomicString, sliderThumb, ("-webkit-slider-container"));
381     return sliderThumb;
382 }
383
384 }