AccessibilityListBox.cpp [plain text]
#include "config.h"
#include "AccessibilityListBox.h"
#include "AXObjectCache.h"
#include "AccessibilityListBoxOption.h"
#include "HTMLNames.h"
#include "HTMLSelectElement.h"
#include "HitTestResult.h"
#include "RenderListBox.h"
#include "RenderObject.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
AccessibilityListBox::AccessibilityListBox(RenderObject* renderer)
: AccessibilityRenderObject(renderer)
{
}
AccessibilityListBox::~AccessibilityListBox()
{
}
PassRefPtr<AccessibilityListBox> AccessibilityListBox::create(RenderObject* renderer)
{
return adoptRef(new AccessibilityListBox(renderer));
}
bool AccessibilityListBox::canSetSelectedChildrenAttribute() const
{
Node* selectNode = m_renderer->node();
if (!selectNode)
return false;
return !toHTMLSelectElement(selectNode)->disabled();
}
void AccessibilityListBox::addChildren()
{
Node* selectNode = m_renderer->node();
if (!selectNode)
return;
m_haveChildren = true;
const Vector<HTMLElement*>& listItems = toHTMLSelectElement(selectNode)->listItems();
unsigned length = listItems.size();
for (unsigned i = 0; i < length; i++) {
AccessibilityObject* listOption = listBoxOptionAccessibilityObject(listItems[i]);
if (listOption && !listOption->accessibilityIsIgnored())
m_children.append(listOption);
}
}
void AccessibilityListBox::setSelectedChildren(AccessibilityChildrenVector& children)
{
if (!canSetSelectedChildrenAttribute())
return;
Node* selectNode = m_renderer->node();
if (!selectNode)
return;
unsigned length = m_children.size();
for (unsigned i = 0; i < length; i++) {
AccessibilityListBoxOption* listBoxOption = static_cast<AccessibilityListBoxOption*>(m_children[i].get());
if (listBoxOption->isSelected())
listBoxOption->setSelected(false);
}
length = children.size();
for (unsigned i = 0; i < length; i++) {
AccessibilityObject* obj = children[i].get();
if (obj->roleValue() != ListBoxOptionRole)
continue;
static_cast<AccessibilityListBoxOption*>(obj)->setSelected(true);
}
}
void AccessibilityListBox::selectedChildren(AccessibilityChildrenVector& result)
{
ASSERT(result.isEmpty());
if (!hasChildren())
addChildren();
unsigned length = m_children.size();
for (unsigned i = 0; i < length; i++) {
if (static_cast<AccessibilityListBoxOption*>(m_children[i].get())->isSelected())
result.append(m_children[i]);
}
}
void AccessibilityListBox::visibleChildren(AccessibilityChildrenVector& result)
{
ASSERT(result.isEmpty());
if (!hasChildren())
addChildren();
unsigned length = m_children.size();
for (unsigned i = 0; i < length; i++) {
if (toRenderListBox(m_renderer)->listIndexIsVisible(i))
result.append(m_children[i]);
}
}
AccessibilityObject* AccessibilityListBox::listBoxOptionAccessibilityObject(HTMLElement* element) const
{
if (!element || element->hasTagName(hrTag))
return 0;
AccessibilityObject* listBoxObject = m_renderer->document()->axObjectCache()->getOrCreate(ListBoxOptionRole);
static_cast<AccessibilityListBoxOption*>(listBoxObject)->setHTMLElement(element);
return listBoxObject;
}
bool AccessibilityListBox::accessibilityIsIgnored() const
{
AccessibilityObjectInclusion decision = accessibilityIsIgnoredBase();
if (decision == IncludeObject)
return false;
if (decision == IgnoreObject)
return true;
return false;
}
AccessibilityObject* AccessibilityListBox::elementAccessibilityHitTest(const IntPoint& point) const
{
if (!m_renderer)
return 0;
Node* node = m_renderer->node();
if (!node)
return 0;
LayoutRect parentRect = boundingBoxRect();
AccessibilityObject* listBoxOption = 0;
unsigned length = m_children.size();
for (unsigned i = 0; i < length; i++) {
LayoutRect rect = toRenderListBox(m_renderer)->itemBoundingBoxRect(parentRect.location(), i);
if (rect.contains(point)) {
listBoxOption = m_children[i].get();
break;
}
}
if (listBoxOption && !listBoxOption->accessibilityIsIgnored())
return listBoxOption;
return axObjectCache()->getOrCreate(m_renderer);
}
}