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
36namespace WebCore {
37
38using namespace HTMLNames;
39
40AccessibilityMenuListPopup::AccessibilityMenuListPopup()
41    : m_menuList(0)
42{
43}
44
45bool AccessibilityMenuListPopup::isVisible() const
46{
47    return false;
48}
49
50bool AccessibilityMenuListPopup::isOffScreen() const
51{
52    return m_menuList->isCollapsed();
53}
54
55AccessibilityObject* AccessibilityMenuListPopup::parentObject() const
56{
57    return m_menuList;
58}
59
60bool AccessibilityMenuListPopup::isEnabled() const
61{
62    return m_menuList->isEnabled();
63}
64
65AccessibilityMenuListOption* 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
79bool AccessibilityMenuListPopup::press() const
80{
81    m_menuList->press();
82    return true;
83}
84
85void 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        // The cast to HTMLElement below is safe because the only other possible listItem type
99        // would be a WMLElement, but WML builds don't use accessbility features at all.
100        AccessibilityMenuListOption* option = menuListOptionAccessibilityObject(toHTMLElement(listItems[i]));
101        if (option) {
102            option->setParent(this);
103            m_children.append(option);
104        }
105    }
106}
107
108void AccessibilityMenuListPopup::childrenChanged()
109{
110    for (size_t i = m_children.size(); i > 0 ; --i) {
111        AccessibilityObject* child = m_children[i - 1].get();
112        if (child->actionElement() && !child->actionElement()->attached()) {
113            m_menuList->renderer()->document()->axObjectCache()->remove(child->axObjectID());
114            m_children.remove(i - 1);
115        }
116    }
117}
118
119void AccessibilityMenuListPopup::setMenuList(AccessibilityMenuList* menuList)
120{
121    ASSERT_ARG(menuList, menuList);
122    ASSERT(!m_menuList);
123    m_menuList = menuList;
124}
125
126} // namespace WebCore
127