1/*
2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "core/accessibility/AXSlider.h"
31
32#include "core/accessibility/AXObjectCache.h"
33#include "core/dom/shadow/ShadowRoot.h"
34#include "core/html/HTMLInputElement.h"
35#include "core/html/shadow/ShadowElementNames.h"
36#include "core/rendering/RenderObject.h"
37
38namespace WebCore {
39
40using namespace HTMLNames;
41
42AXSlider::AXSlider(RenderObject* renderer)
43    : AXRenderObject(renderer)
44{
45}
46
47PassRefPtr<AXSlider> AXSlider::create(RenderObject* renderer)
48{
49    return adoptRef(new AXSlider(renderer));
50}
51
52AccessibilityOrientation AXSlider::orientation() const
53{
54    // Default to horizontal in the unknown case.
55    if (!m_renderer)
56        return AccessibilityOrientationHorizontal;
57
58    RenderStyle* style = m_renderer->style();
59    if (!style)
60        return AccessibilityOrientationHorizontal;
61
62    ControlPart styleAppearance = style->appearance();
63    switch (styleAppearance) {
64    case SliderThumbHorizontalPart:
65    case SliderHorizontalPart:
66    case MediaSliderPart:
67    case MediaFullScreenVolumeSliderPart:
68        return AccessibilityOrientationHorizontal;
69
70    case SliderThumbVerticalPart:
71    case SliderVerticalPart:
72    case MediaVolumeSliderPart:
73        return AccessibilityOrientationVertical;
74
75    default:
76        return AccessibilityOrientationHorizontal;
77    }
78}
79
80void AXSlider::addChildren()
81{
82    ASSERT(!m_haveChildren);
83
84    m_haveChildren = true;
85
86    AXObjectCache* cache = m_renderer->document().axObjectCache();
87
88    AXSliderThumb* thumb = static_cast<AXSliderThumb*>(cache->getOrCreate(SliderThumbRole));
89    thumb->setParent(this);
90
91    // Before actually adding the value indicator to the hierarchy,
92    // allow the platform to make a final decision about it.
93    if (thumb->accessibilityIsIgnored())
94        cache->remove(thumb->axObjectID());
95    else
96        m_children.append(thumb);
97}
98
99const AtomicString& AXSlider::getAttribute(const QualifiedName& attribute) const
100{
101    return element()->getAttribute(attribute);
102}
103
104AXObject* AXSlider::elementAccessibilityHitTest(const IntPoint& point) const
105{
106    if (m_children.size()) {
107        ASSERT(m_children.size() == 1);
108        if (m_children[0]->elementRect().contains(point))
109            return m_children[0].get();
110    }
111
112    return axObjectCache()->getOrCreate(m_renderer);
113}
114
115void AXSlider::setValue(const String& value)
116{
117    HTMLInputElement* input = element();
118
119    if (input->value() == value)
120        return;
121
122    input->setValue(value);
123
124    // Fire change event manually, as RenderSlider::setValueForPosition does.
125    input->dispatchFormControlChangeEvent();
126}
127
128HTMLInputElement* AXSlider::element() const
129{
130    return toHTMLInputElement(m_renderer->node());
131}
132
133
134AXSliderThumb::AXSliderThumb()
135{
136}
137
138PassRefPtr<AXSliderThumb> AXSliderThumb::create()
139{
140    return adoptRef(new AXSliderThumb());
141}
142
143LayoutRect AXSliderThumb::elementRect() const
144{
145    if (!m_parent)
146        return LayoutRect();
147
148    RenderObject* sliderRenderer = m_parent->renderer();
149    if (!sliderRenderer || !sliderRenderer->isSlider())
150        return LayoutRect();
151    return toElement(sliderRenderer->node())->userAgentShadowRoot()->getElementById(ShadowElementNames::sliderThumb())->boundingBox();
152}
153
154bool AXSliderThumb::computeAccessibilityIsIgnored() const
155{
156    return accessibilityIsIgnoredByDefault();
157}
158
159} // namespace WebCore
160