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 "AccessibilitySlider.h"
31
32#include "AXObjectCache.h"
33#include "HTMLInputElement.h"
34#include "HTMLNames.h"
35#include "RenderObject.h"
36#include "RenderSlider.h"
37
38namespace WebCore {
39
40using namespace HTMLNames;
41
42AccessibilitySlider::AccessibilitySlider(RenderObject* renderer)
43    : AccessibilityRenderObject(renderer)
44{
45}
46
47PassRefPtr<AccessibilitySlider> AccessibilitySlider::create(RenderObject* renderer)
48{
49    return adoptRef(new AccessibilitySlider(renderer));
50}
51
52const AccessibilityObject::AccessibilityChildrenVector& AccessibilitySlider::children()
53{
54    if (!m_haveChildren)
55        addChildren();
56    return m_children;
57}
58
59AccessibilityOrientation AccessibilitySlider::orientation() const
60{
61    // Default to horizontal in the unknown case.
62    if (!m_renderer)
63        return AccessibilityOrientationHorizontal;
64
65    RenderStyle* style = m_renderer->style();
66    if (!style)
67        return AccessibilityOrientationHorizontal;
68
69    ControlPart styleAppearance = style->appearance();
70    switch (styleAppearance) {
71    case SliderThumbHorizontalPart:
72    case SliderHorizontalPart:
73    case MediaSliderPart:
74        return AccessibilityOrientationHorizontal;
75
76    case SliderThumbVerticalPart:
77    case SliderVerticalPart:
78    case MediaVolumeSliderPart:
79        return AccessibilityOrientationVertical;
80
81    default:
82        return AccessibilityOrientationHorizontal;
83    }
84}
85
86void AccessibilitySlider::addChildren()
87{
88    ASSERT(!m_haveChildren);
89
90    m_haveChildren = true;
91
92    AXObjectCache* cache = m_renderer->document()->axObjectCache();
93
94    AccessibilitySliderThumb* thumb = static_cast<AccessibilitySliderThumb*>(cache->getOrCreate(SliderThumbRole));
95    thumb->setParentObject(this);
96
97    // Before actually adding the value indicator to the hierarchy,
98    // allow the platform to make a final decision about it.
99    if (thumb->accessibilityIsIgnored())
100        cache->remove(thumb->axObjectID());
101    else
102        m_children.append(thumb);
103}
104
105const AtomicString& AccessibilitySlider::getAttribute(const QualifiedName& attribute) const
106{
107    return element()->getAttribute(attribute);
108}
109
110AccessibilityObject* AccessibilitySlider::elementAccessibilityHitTest(const IntPoint& point) const
111{
112    if (m_children.size()) {
113        ASSERT(m_children.size() == 1);
114        if (m_children[0]->elementRect().contains(point))
115            return m_children[0].get();
116    }
117
118    return axObjectCache()->getOrCreate(m_renderer);
119}
120
121bool AccessibilitySlider::accessibilityIsIgnored() const
122{
123    AccessibilityObjectInclusion decision = accessibilityIsIgnoredBase();
124    if (decision == IncludeObject)
125        return false;
126    if (decision == IgnoreObject)
127        return true;
128
129    return false;
130}
131
132float AccessibilitySlider::valueForRange() const
133{
134    return element()->value().toFloat();
135}
136
137float AccessibilitySlider::maxValueForRange() const
138{
139    return static_cast<float>(element()->maximum());
140}
141
142float AccessibilitySlider::minValueForRange() const
143{
144    return static_cast<float>(element()->minimum());
145}
146
147void AccessibilitySlider::setValue(const String& value)
148{
149    HTMLInputElement* input = element();
150
151    if (input->value() == value)
152        return;
153
154    input->setValue(value);
155
156    // Fire change event manually, as RenderSlider::setValueForPosition does.
157    input->dispatchFormControlChangeEvent();
158}
159
160HTMLInputElement* AccessibilitySlider::element() const
161{
162    return static_cast<HTMLInputElement*>(m_renderer->node());
163}
164
165
166AccessibilitySliderThumb::AccessibilitySliderThumb()
167    : m_parentSlider(0)
168{
169}
170
171PassRefPtr<AccessibilitySliderThumb> AccessibilitySliderThumb::create()
172{
173    return adoptRef(new AccessibilitySliderThumb());
174}
175
176IntRect AccessibilitySliderThumb::elementRect() const
177{
178    if (!m_parentSlider->renderer())
179        return IntRect();
180
181    IntRect intRect = toRenderSlider(m_parentSlider->renderer())->thumbRect();
182    FloatQuad floatQuad = m_parentSlider->renderer()->localToAbsoluteQuad(FloatRect(intRect));
183
184    return floatQuad.enclosingBoundingBox();
185}
186
187IntSize AccessibilitySliderThumb::size() const
188{
189    return elementRect().size();
190}
191
192bool AccessibilitySliderThumb::accessibilityIsIgnored() const
193{
194    AccessibilityObjectInclusion decision = accessibilityPlatformIncludesObject();
195    if (decision == IncludeObject)
196        return false;
197    if (decision == IgnoreObject)
198        return true;
199
200    return false;
201}
202
203} // namespace WebCore
204