2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "AccessibilityListBox.h"
32 #include "AXObjectCache.h"
33 #include "AccessibilityListBoxOption.h"
34 #include "HTMLNames.h"
35 #include "HTMLSelectElement.h"
36 #include "HitTestResult.h"
37 #include "RenderListBox.h"
38 #include "RenderObject.h"
44 using namespace HTMLNames;
46 AccessibilityListBox::AccessibilityListBox(RenderObject* renderer)
47 : AccessibilityRenderObject(renderer)
51 AccessibilityListBox::~AccessibilityListBox()
55 PassRefPtr<AccessibilityListBox> AccessibilityListBox::create(RenderObject* renderer)
57 return adoptRef(new AccessibilityListBox(renderer));
60 bool AccessibilityListBox::canSetSelectedChildrenAttribute() const
62 Node* selectNode = m_renderer->node();
66 return !static_cast<HTMLSelectElement*>(selectNode)->disabled();
69 void AccessibilityListBox::addChildren()
71 Node* selectNode = m_renderer->node();
75 m_haveChildren = true;
77 const Vector<Element*>& listItems = static_cast<HTMLSelectElement*>(selectNode)->listItems();
78 unsigned length = listItems.size();
79 for (unsigned i = 0; i < length; i++) {
80 // The cast to HTMLElement below is safe because the only other possible listItem type
81 // would be a WMLElement, but WML builds don't use accessibility features at all.
82 AccessibilityObject* listOption = listBoxOptionAccessibilityObject(toHTMLElement(listItems[i]));
83 if (listOption && !listOption->accessibilityIsIgnored())
84 m_children.append(listOption);
88 void AccessibilityListBox::setSelectedChildren(AccessibilityChildrenVector& children)
90 if (!canSetSelectedChildrenAttribute())
93 Node* selectNode = m_renderer->node();
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);
105 length = children.size();
106 for (unsigned i = 0; i < length; i++) {
107 AccessibilityObject* obj = children[i].get();
108 if (obj->roleValue() != ListBoxOptionRole)
111 static_cast<AccessibilityListBoxOption*>(obj)->setSelected(true);
115 void AccessibilityListBox::selectedChildren(AccessibilityChildrenVector& result)
117 ASSERT(result.isEmpty());
122 unsigned length = m_children.size();
123 for (unsigned i = 0; i < length; i++) {
124 if (static_cast<AccessibilityListBoxOption*>(m_children[i].get())->isSelected())
125 result.append(m_children[i]);
129 void AccessibilityListBox::visibleChildren(AccessibilityChildrenVector& result)
131 ASSERT(result.isEmpty());
136 unsigned length = m_children.size();
137 for (unsigned i = 0; i < length; i++) {
138 if (toRenderListBox(m_renderer)->listIndexIsVisible(i))
139 result.append(m_children[i]);
143 AccessibilityObject* AccessibilityListBox::listBoxOptionAccessibilityObject(HTMLElement* element) const
146 if (!element || element->hasTagName(hrTag))
149 AccessibilityObject* listBoxObject = m_renderer->document()->axObjectCache()->getOrCreate(ListBoxOptionRole);
150 static_cast<AccessibilityListBoxOption*>(listBoxObject)->setHTMLElement(element);
152 return listBoxObject;
155 bool AccessibilityListBox::accessibilityIsIgnored() const
157 AccessibilityObjectInclusion decision = accessibilityIsIgnoredBase();
158 if (decision == IncludeObject)
160 if (decision == IgnoreObject)
166 AccessibilityObject* AccessibilityListBox::elementAccessibilityHitTest(const LayoutPoint& point) const
168 // the internal HTMLSelectElement methods for returning a listbox option at a point
169 // ignore optgroup elements.
173 Node* node = m_renderer->node();
177 LayoutRect parentRect = boundingBoxRect();
179 AccessibilityObject* listBoxOption = 0;
180 unsigned length = m_children.size();
181 for (unsigned i = 0; i < length; i++) {
182 LayoutRect rect = toRenderListBox(m_renderer)->itemBoundingBoxRect(parentRect.location(), i);
183 // The cast to HTMLElement below is safe because the only other possible listItem type
184 // would be a WMLElement, but WML builds don't use accessibility features at all.
185 if (rect.contains(point)) {
186 listBoxOption = m_children[i].get();
191 if (listBoxOption && !listBoxOption->accessibilityIsIgnored())
192 return listBoxOption;
194 return axObjectCache()->getOrCreate(m_renderer);
197 } // namespace WebCore