initial import
[vuplus_webkit] / Source / WebCore / accessibility / AccessibilityMenuListPopup.cpp
1 /*
2  * Copyright (C) 2010 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "AccessibilityMenuListPopup.h"
28
29 #include "AXObjectCache.h"
30 #include "AccessibilityMenuList.h"
31 #include "AccessibilityMenuListOption.h"
32 #include "HTMLNames.h"
33 #include "HTMLSelectElement.h"
34 #include "RenderMenuList.h"
35
36 namespace WebCore {
37
38 using namespace HTMLNames;
39
40 AccessibilityMenuListPopup::AccessibilityMenuListPopup()
41     : m_menuList(0)
42 {
43 }
44
45 bool AccessibilityMenuListPopup::isVisible() const
46 {
47     return false;
48 }
49
50 bool AccessibilityMenuListPopup::isOffScreen() const
51 {
52     return m_menuList->isCollapsed();
53 }
54
55 AccessibilityObject* AccessibilityMenuListPopup::parentObject() const
56 {
57     return m_menuList;
58 }
59
60 bool AccessibilityMenuListPopup::isEnabled() const
61 {
62     return m_menuList->isEnabled();
63 }
64
65 AccessibilityMenuListOption* AccessibilityMenuListPopup::menuListOptionAccessibilityObject(HTMLElement* element) const
66 {
67     if (!element || !element->hasTagName(optionTag))
68         return 0;
69
70     AccessibilityObject* object = m_menuList->renderer()->document()->axObjectCache()->getOrCreate(MenuListOptionRole);
71     ASSERT(object->isMenuListOption());
72
73     AccessibilityMenuListOption* option = static_cast<AccessibilityMenuListOption*>(object);
74     option->setElement(element);
75
76     return option;
77 }
78
79 bool AccessibilityMenuListPopup::press() const
80 {
81     m_menuList->press();
82     return true;
83 }
84
85 void AccessibilityMenuListPopup::addChildren()
86 {
87     Node* selectNode = m_menuList->renderer()->node();
88     if (!selectNode)
89         return;
90
91     m_haveChildren = true;
92
93     ASSERT(selectNode->hasTagName(selectTag));
94
95     const Vector<Element*>& listItems = static_cast<HTMLSelectElement*>(selectNode)->listItems();
96     unsigned length = listItems.size();
97     for (unsigned i = 0; i < length; i++) {
98         AccessibilityMenuListOption* option = menuListOptionAccessibilityObject(toHTMLElement(listItems[i]));
99         if (option) {
100             option->setParent(this);
101             m_children.append(option);
102         }
103     }
104 }
105
106 void AccessibilityMenuListPopup::childrenChanged()
107 {
108     for (size_t i = m_children.size(); i > 0 ; --i) {
109         AccessibilityObject* child = m_children[i - 1].get();
110         if (child->actionElement() && !child->actionElement()->attached()) {
111             m_menuList->renderer()->document()->axObjectCache()->remove(child->axObjectID());
112         }
113     }
114     m_children.clear();
115     m_haveChildren = false;
116     addChildren();
117 }
118
119 void AccessibilityMenuListPopup::setMenuList(AccessibilityMenuList* menuList)
120 {
121     ASSERT_ARG(menuList, menuList);
122     ASSERT(!m_menuList);
123     m_menuList = menuList;
124 }
125
126 void AccessibilityMenuListPopup::didUpdateActiveOption(int optionIndex)
127 {
128     ASSERT_ARG(optionIndex, optionIndex >= 0);
129     ASSERT_ARG(optionIndex, optionIndex < static_cast<int>(m_children.size()));
130
131     RefPtr<Document> document = m_menuList->renderer()->document();
132     AXObjectCache* cache = document->axObjectCache();
133     RefPtr<AccessibilityObject> child = m_children[optionIndex].get();
134
135     cache->postNotification(child.get(), document.get(), AXObjectCache::AXFocusedUIElementChanged, true, PostSynchronously);
136     cache->postNotification(child.get(), document.get(), AXObjectCache::AXMenuListItemSelected, true, PostSynchronously);
137 }
138
139 } // namespace WebCore