Merge "[CherryPick] [WEBGL] Rename WEBKIT_WEBGL_lose_context to WEBGL_lose_context...
[framework/web/webkit-efl.git] / Source / WebCore / accessibility / AccessibilityListBox.cpp
1 /*
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"
34 #include "HTMLNames.h"
35 #include "HTMLSelectElement.h"
36 #include "HitTestResult.h"
37 #include "RenderListBox.h"
38 #include "RenderObject.h"
39
40 using namespace std;
41
42 namespace WebCore {
43
44 using namespace HTMLNames;
45
46 AccessibilityListBox::AccessibilityListBox(RenderObject* renderer)
47     : AccessibilityRenderObject(renderer)
48 {
49 }
50
51 AccessibilityListBox::~AccessibilityListBox()
52 {
53 }
54     
55 PassRefPtr<AccessibilityListBox> AccessibilityListBox::create(RenderObject* renderer)
56 {
57     AccessibilityListBox* obj = new AccessibilityListBox(renderer);
58     obj->init();
59     return adoptRef(obj);
60 }
61     
62 bool AccessibilityListBox::canSetSelectedChildrenAttribute() const
63 {
64     Node* selectNode = m_renderer->node();
65     if (!selectNode)
66         return false;
67     
68     return !toHTMLSelectElement(selectNode)->disabled();
69 }
70
71 void AccessibilityListBox::addChildren()
72 {
73     Node* selectNode = m_renderer->node();
74     if (!selectNode)
75         return;
76     
77     m_haveChildren = true;
78     
79     const Vector<HTMLElement*>& listItems = toHTMLSelectElement(selectNode)->listItems();
80     unsigned length = listItems.size();
81     for (unsigned i = 0; i < length; i++) {
82         // The cast to HTMLElement below is safe because the only other possible listItem type
83         // would be a WMLElement, but WML builds don't use accessibility features at all.
84         AccessibilityObject* listOption = listBoxOptionAccessibilityObject(listItems[i]);
85         if (listOption && !listOption->accessibilityIsIgnored())
86             m_children.append(listOption);
87     }
88 }
89
90 void AccessibilityListBox::setSelectedChildren(AccessibilityChildrenVector& children)
91 {
92     if (!canSetSelectedChildrenAttribute())
93         return;
94     
95     Node* selectNode = m_renderer->node();
96     if (!selectNode)
97         return;
98     
99     // disable any selected options
100     unsigned length = m_children.size();
101     for (unsigned i = 0; i < length; i++) {
102         AccessibilityListBoxOption* listBoxOption = static_cast<AccessibilityListBoxOption*>(m_children[i].get());
103         if (listBoxOption->isSelected())
104             listBoxOption->setSelected(false);
105     }
106     
107     length = children.size();
108     for (unsigned i = 0; i < length; i++) {
109         AccessibilityObject* obj = children[i].get();
110         if (obj->roleValue() != ListBoxOptionRole)
111             continue;
112                 
113         static_cast<AccessibilityListBoxOption*>(obj)->setSelected(true);
114     }
115 }
116     
117 void AccessibilityListBox::selectedChildren(AccessibilityChildrenVector& result)
118 {
119     ASSERT(result.isEmpty());
120
121     if (!hasChildren())
122         addChildren();
123         
124     unsigned length = m_children.size();
125     for (unsigned i = 0; i < length; i++) {
126         if (static_cast<AccessibilityListBoxOption*>(m_children[i].get())->isSelected())
127             result.append(m_children[i]);
128     }    
129 }
130
131 void AccessibilityListBox::visibleChildren(AccessibilityChildrenVector& result)
132 {
133     ASSERT(result.isEmpty());
134     
135     if (!hasChildren())
136         addChildren();
137     
138     unsigned length = m_children.size();
139     for (unsigned i = 0; i < length; i++) {
140         if (toRenderListBox(m_renderer)->listIndexIsVisible(i))
141             result.append(m_children[i]);
142     }
143 }
144
145 AccessibilityObject* AccessibilityListBox::listBoxOptionAccessibilityObject(HTMLElement* element) const
146 {
147     // skip hr elements
148     if (!element || element->hasTagName(hrTag))
149         return 0;
150     
151     AccessibilityObject* listBoxObject = m_renderer->document()->axObjectCache()->getOrCreate(ListBoxOptionRole);
152     static_cast<AccessibilityListBoxOption*>(listBoxObject)->setHTMLElement(element);
153     
154     return listBoxObject;
155 }
156     
157 bool AccessibilityListBox::accessibilityIsIgnored() const
158 {
159     AccessibilityObjectInclusion decision = accessibilityIsIgnoredBase();
160     if (decision == IncludeObject)
161         return false;
162     if (decision == IgnoreObject)
163         return true;
164     
165     return false;
166 }
167
168 AccessibilityObject* AccessibilityListBox::elementAccessibilityHitTest(const IntPoint& point) const
169 {
170     // the internal HTMLSelectElement methods for returning a listbox option at a point
171     // ignore optgroup elements.
172     if (!m_renderer)
173         return 0;
174     
175     Node* node = m_renderer->node();
176     if (!node)
177         return 0;
178     
179     LayoutRect parentRect = boundingBoxRect();
180     
181     AccessibilityObject* listBoxOption = 0;
182     unsigned length = m_children.size();
183     for (unsigned i = 0; i < length; i++) {
184         LayoutRect rect = toRenderListBox(m_renderer)->itemBoundingBoxRect(parentRect.location(), i);
185         // The cast to HTMLElement below is safe because the only other possible listItem type
186         // would be a WMLElement, but WML builds don't use accessibility features at all.
187         if (rect.contains(point)) {
188             listBoxOption = m_children[i].get();
189             break;
190         }
191     }
192     
193     if (listBoxOption && !listBoxOption->accessibilityIsIgnored())
194         return listBoxOption;
195     
196     return axObjectCache()->getOrCreate(m_renderer);
197 }
198
199 } // namespace WebCore