blob: 1e5c53ccc09bfbcab82487392fb5edd884d17f7d [file] [log] [blame]
cfleizach@apple.come9dbd312008-05-06 23:27:40 +00001/*
2 * Copyright (C) 2008 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 "AccessibilityListBox.h"
31
32#include "AXObjectCache.h"
33#include "AccessibilityListBoxOption.h"
cfleizach@apple.come9dbd312008-05-06 23:27:40 +000034#include "HTMLNames.h"
35#include "HTMLSelectElement.h"
cfleizach@apple.comf534eb22009-11-03 07:01:20 +000036#include "HitTestResult.h"
cfleizach@apple.come9dbd312008-05-06 23:27:40 +000037#include "RenderListBox.h"
38#include "RenderObject.h"
39
40using namespace std;
41
42namespace WebCore {
43
44using namespace HTMLNames;
45
46AccessibilityListBox::AccessibilityListBox(RenderObject* renderer)
47 : AccessibilityRenderObject(renderer)
48{
49}
50
51AccessibilityListBox::~AccessibilityListBox()
52{
53}
54
55PassRefPtr<AccessibilityListBox> AccessibilityListBox::create(RenderObject* renderer)
56{
dmazzoni@google.com36019062013-01-24 08:16:00 +000057 return adoptRef(new AccessibilityListBox(renderer));
cfleizach@apple.come9dbd312008-05-06 23:27:40 +000058}
59
cfleizach@apple.com39027b72008-06-05 22:17:56 +000060bool AccessibilityListBox::canSetSelectedChildrenAttribute() const
61{
62 Node* selectNode = m_renderer->node();
63 if (!selectNode)
64 return false;
65
darin@apple.com318f8892011-10-15 00:30:43 +000066 return !toHTMLSelectElement(selectNode)->disabled();
cfleizach@apple.com39027b72008-06-05 22:17:56 +000067}
68
cfleizach@apple.come9dbd312008-05-06 23:27:40 +000069void AccessibilityListBox::addChildren()
70{
71 Node* selectNode = m_renderer->node();
72 if (!selectNode)
73 return;
74
75 m_haveChildren = true;
76
darin@apple.com318f8892011-10-15 00:30:43 +000077 const Vector<HTMLElement*>& listItems = toHTMLSelectElement(selectNode)->listItems();
cfleizach@apple.come9dbd312008-05-06 23:27:40 +000078 unsigned length = listItems.size();
79 for (unsigned i = 0; i < length; i++) {
zimmermann@webkit.orgde5f1ac2009-05-23 21:25:22 +000080 // The cast to HTMLElement below is safe because the only other possible listItem type
dbates@webkit.orgef42d382010-01-25 19:20:06 +000081 // would be a WMLElement, but WML builds don't use accessibility features at all.
darin@apple.com318f8892011-10-15 00:30:43 +000082 AccessibilityObject* listOption = listBoxOptionAccessibilityObject(listItems[i]);
cfleizach@apple.com6f3a5692010-03-11 00:36:02 +000083 if (listOption && !listOption->accessibilityIsIgnored())
cfleizach@apple.come9dbd312008-05-06 23:27:40 +000084 m_children.append(listOption);
85 }
86}
87
cfleizach@apple.com39027b72008-06-05 22:17:56 +000088void AccessibilityListBox::setSelectedChildren(AccessibilityChildrenVector& children)
89{
90 if (!canSetSelectedChildrenAttribute())
91 return;
92
93 Node* selectNode = m_renderer->node();
94 if (!selectNode)
95 return;
96
97 // disable any selected options
98 unsigned length = m_children.size();
99 for (unsigned i = 0; i < length; i++) {
100 AccessibilityListBoxOption* listBoxOption = static_cast<AccessibilityListBoxOption*>(m_children[i].get());
101 if (listBoxOption->isSelected())
102 listBoxOption->setSelected(false);
103 }
104
105 length = children.size();
106 for (unsigned i = 0; i < length; i++) {
107 AccessibilityObject* obj = children[i].get();
108 if (obj->roleValue() != ListBoxOptionRole)
109 continue;
110
111 static_cast<AccessibilityListBoxOption*>(obj)->setSelected(true);
112 }
113}
114
115void AccessibilityListBox::selectedChildren(AccessibilityChildrenVector& result)
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000116{
alice.liu@apple.comcac21ca2008-05-16 03:27:24 +0000117 ASSERT(result.isEmpty());
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000118
119 if (!hasChildren())
120 addChildren();
121
122 unsigned length = m_children.size();
123 for (unsigned i = 0; i < length; i++) {
124 if (static_cast<AccessibilityListBoxOption*>(m_children[i].get())->isSelected())
alice.liu@apple.comcac21ca2008-05-16 03:27:24 +0000125 result.append(m_children[i]);
126 }
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000127}
128
cfleizach@apple.com39027b72008-06-05 22:17:56 +0000129void AccessibilityListBox::visibleChildren(AccessibilityChildrenVector& result)
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000130{
alice.liu@apple.comcac21ca2008-05-16 03:27:24 +0000131 ASSERT(result.isEmpty());
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000132
133 if (!hasChildren())
134 addChildren();
135
136 unsigned length = m_children.size();
137 for (unsigned i = 0; i < length; i++) {
darin@apple.comb6cb2562009-08-05 21:25:09 +0000138 if (toRenderListBox(m_renderer)->listIndexIsVisible(i))
alice.liu@apple.comcac21ca2008-05-16 03:27:24 +0000139 result.append(m_children[i]);
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000140 }
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000141}
142
143AccessibilityObject* AccessibilityListBox::listBoxOptionAccessibilityObject(HTMLElement* element) const
144{
145 // skip hr elements
146 if (!element || element->hasTagName(hrTag))
147 return 0;
148
cfleizach@apple.com70b144c2009-02-26 00:27:38 +0000149 AccessibilityObject* listBoxObject = m_renderer->document()->axObjectCache()->getOrCreate(ListBoxOptionRole);
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000150 static_cast<AccessibilityListBoxOption*>(listBoxObject)->setHTMLElement(element);
151
152 return listBoxObject;
153}
cfleizach@apple.com6f3a5692010-03-11 00:36:02 +0000154
dmazzoni@google.com3b5179d2013-02-09 23:06:00 +0000155bool AccessibilityListBox::computeAccessibilityIsIgnored() const
cfleizach@apple.com6f3a5692010-03-11 00:36:02 +0000156{
cfleizach@apple.com14f57ce2010-03-11 22:24:33 +0000157 AccessibilityObjectInclusion decision = accessibilityIsIgnoredBase();
158 if (decision == IncludeObject)
159 return false;
160 if (decision == IgnoreObject)
cfleizach@apple.com6f3a5692010-03-11 00:36:02 +0000161 return true;
162
163 return false;
164}
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000165
leviw@chromium.org3be076e2012-03-22 15:07:02 +0000166AccessibilityObject* AccessibilityListBox::elementAccessibilityHitTest(const IntPoint& point) const
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000167{
168 // the internal HTMLSelectElement methods for returning a listbox option at a point
169 // ignore optgroup elements.
170 if (!m_renderer)
171 return 0;
172
hyatt@apple.com7e032532009-02-11 22:06:32 +0000173 Node* node = m_renderer->node();
174 if (!node)
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000175 return 0;
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000176
leviw@chromium.orgc3f41a02011-08-18 01:18:55 +0000177 LayoutRect parentRect = boundingBoxRect();
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000178
cfleizach@apple.com6f3a5692010-03-11 00:36:02 +0000179 AccessibilityObject* listBoxOption = 0;
180 unsigned length = m_children.size();
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000181 for (unsigned i = 0; i < length; i++) {
leviw@chromium.orgc3f41a02011-08-18 01:18:55 +0000182 LayoutRect rect = toRenderListBox(m_renderer)->itemBoundingBoxRect(parentRect.location(), i);
zimmermann@webkit.orgde5f1ac2009-05-23 21:25:22 +0000183 // The cast to HTMLElement below is safe because the only other possible listItem type
dbates@webkit.orgef42d382010-01-25 19:20:06 +0000184 // would be a WMLElement, but WML builds don't use accessibility features at all.
cfleizach@apple.com6f3a5692010-03-11 00:36:02 +0000185 if (rect.contains(point)) {
186 listBoxOption = m_children[i].get();
187 break;
188 }
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000189 }
190
cfleizach@apple.com6f3a5692010-03-11 00:36:02 +0000191 if (listBoxOption && !listBoxOption->accessibilityIsIgnored())
192 return listBoxOption;
193
cfleizach@apple.com70b144c2009-02-26 00:27:38 +0000194 return axObjectCache()->getOrCreate(m_renderer);
cfleizach@apple.come9dbd312008-05-06 23:27:40 +0000195}
196
197} // namespace WebCore