2000-12-31 16:12:15 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-12-31 16:12:15 -08:00
|
|
|
|
|
|
|
#include "XULSelectControlAccessible.h"
|
|
|
|
|
|
|
|
#include "nsAccessibilityService.h"
|
2012-05-27 02:01:40 -07:00
|
|
|
#include "DocAccessible.h"
|
2000-12-31 16:12:15 -08:00
|
|
|
|
|
|
|
#include "nsIDOMXULContainerElement.h"
|
|
|
|
#include "nsIDOMXULSelectCntrlItemEl.h"
|
|
|
|
#include "nsIDOMXULMultSelectCntrlEl.h"
|
|
|
|
#include "nsIDOMKeyEvent.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMXULElement.h"
|
|
|
|
#include "nsIMutableArray.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::a11y;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// XULSelectControlAccessible
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
XULSelectControlAccessible::
|
2012-05-27 02:01:40 -07:00
|
|
|
XULSelectControlAccessible(nsIContent* aContent, DocAccessible* aDoc) :
|
2012-05-28 18:18:45 -07:00
|
|
|
AccessibleWrap(aContent, aDoc)
|
2000-12-31 16:12:15 -08:00
|
|
|
{
|
2012-12-17 21:22:26 -08:00
|
|
|
mGenericTypes |= eSelect;
|
2000-12-31 16:12:15 -08:00
|
|
|
mSelectControl = do_QueryInterface(aContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-10-28 20:30:55 -07:00
|
|
|
// XULSelectControlAccessible: Accessible
|
2000-12-31 16:12:15 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
XULSelectControlAccessible::Shutdown()
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mSelectControl = nullptr;
|
2012-05-28 18:18:45 -07:00
|
|
|
AccessibleWrap::Shutdown();
|
2000-12-31 16:12:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// XULSelectControlAccessible: SelectAccessible
|
|
|
|
|
|
|
|
already_AddRefed<nsIArray>
|
|
|
|
XULSelectControlAccessible::SelectedItems()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMutableArray> selectedItems =
|
|
|
|
do_CreateInstance(NS_ARRAY_CONTRACTID);
|
2012-02-07 14:38:54 -08:00
|
|
|
if (!selectedItems || !mDoc)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2000-12-31 16:12:15 -08:00
|
|
|
|
|
|
|
// For XUL multi-select control
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> xulMultiSelect =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
if (xulMultiSelect) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t length = 0;
|
2000-12-31 16:12:15 -08:00
|
|
|
xulMultiSelect->GetSelectedCount(&length);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t index = 0; index < length; index++) {
|
2000-12-31 16:12:15 -08:00
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm;
|
2012-08-07 00:06:29 -07:00
|
|
|
xulMultiSelect->MultiGetSelectedItem(index, getter_AddRefs(itemElm));
|
2000-12-31 16:12:15 -08:00
|
|
|
nsCOMPtr<nsINode> itemNode(do_QueryInterface(itemElm));
|
2012-05-28 18:18:45 -07:00
|
|
|
Accessible* item = mDoc->GetAccessible(itemNode);
|
2000-12-31 16:12:15 -08:00
|
|
|
if (item)
|
|
|
|
selectedItems->AppendElement(static_cast<nsIAccessible*>(item),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
} else { // Single select?
|
2012-08-07 00:06:29 -07:00
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm;
|
|
|
|
mSelectControl->GetSelectedItem(getter_AddRefs(itemElm));
|
|
|
|
nsCOMPtr<nsINode> itemNode(do_QueryInterface(itemElm));
|
|
|
|
if (itemNode) {
|
|
|
|
Accessible* item = mDoc->GetAccessible(itemNode);
|
|
|
|
if (item)
|
|
|
|
selectedItems->AppendElement(static_cast<nsIAccessible*>(item),
|
|
|
|
false);
|
|
|
|
}
|
2012-02-07 14:38:54 -08:00
|
|
|
}
|
2000-12-31 16:12:15 -08:00
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
return selectedItems.forget();
|
2000-12-31 16:12:15 -08:00
|
|
|
}
|
|
|
|
|
2012-05-28 18:18:45 -07:00
|
|
|
Accessible*
|
2012-08-22 08:56:38 -07:00
|
|
|
XULSelectControlAccessible::GetSelectedItem(uint32_t aIndex)
|
2000-12-31 16:12:15 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm;
|
|
|
|
if (multiSelectControl)
|
2012-08-07 00:06:29 -07:00
|
|
|
multiSelectControl->MultiGetSelectedItem(aIndex, getter_AddRefs(itemElm));
|
2000-12-31 16:12:15 -08:00
|
|
|
else if (aIndex == 0)
|
|
|
|
mSelectControl->GetSelectedItem(getter_AddRefs(itemElm));
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> itemNode(do_QueryInterface(itemElm));
|
2012-07-30 07:20:58 -07:00
|
|
|
return itemNode && mDoc ? mDoc->GetAccessible(itemNode) : nullptr;
|
2000-12-31 16:12:15 -08:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2000-12-31 16:12:15 -08:00
|
|
|
XULSelectControlAccessible::SelectedItemCount()
|
|
|
|
{
|
|
|
|
// For XUL multi-select control
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
if (multiSelectControl) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = 0;
|
2000-12-31 16:12:15 -08:00
|
|
|
multiSelectControl->GetSelectedCount(&count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For XUL single-select control/menulist
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index;
|
2000-12-31 16:12:15 -08:00
|
|
|
mSelectControl->GetSelectedIndex(&index);
|
|
|
|
return (index >= 0) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
XULSelectControlAccessible::AddItemToSelection(uint32_t aIndex)
|
2000-12-31 16:12:15 -08:00
|
|
|
{
|
2012-05-28 18:18:45 -07:00
|
|
|
Accessible* item = GetChildAt(aIndex);
|
2000-12-31 16:12:15 -08:00
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm =
|
|
|
|
do_QueryInterface(item->GetContent());
|
|
|
|
if (!itemElm)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isItemSelected = false;
|
|
|
|
itemElm->GetSelected(&isItemSelected);
|
|
|
|
if (isItemSelected)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
|
|
|
|
if (multiSelectControl)
|
|
|
|
multiSelectControl->AddItemToSelection(itemElm);
|
|
|
|
else
|
|
|
|
mSelectControl->SetSelectedItem(itemElm);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
XULSelectControlAccessible::RemoveItemFromSelection(uint32_t aIndex)
|
2000-12-31 16:12:15 -08:00
|
|
|
{
|
2012-05-28 18:18:45 -07:00
|
|
|
Accessible* item = GetChildAt(aIndex);
|
2000-12-31 16:12:15 -08:00
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm =
|
|
|
|
do_QueryInterface(item->GetContent());
|
|
|
|
if (!itemElm)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isItemSelected = false;
|
|
|
|
itemElm->GetSelected(&isItemSelected);
|
|
|
|
if (!isItemSelected)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
|
|
|
|
if (multiSelectControl)
|
|
|
|
multiSelectControl->RemoveItemFromSelection(itemElm);
|
|
|
|
else
|
2012-07-30 07:20:58 -07:00
|
|
|
mSelectControl->SetSelectedItem(nullptr);
|
2000-12-31 16:12:15 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
XULSelectControlAccessible::IsItemSelected(uint32_t aIndex)
|
2000-12-31 16:12:15 -08:00
|
|
|
{
|
2012-05-28 18:18:45 -07:00
|
|
|
Accessible* item = GetChildAt(aIndex);
|
2000-12-31 16:12:15 -08:00
|
|
|
if (!item)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm =
|
|
|
|
do_QueryInterface(item->GetContent());
|
|
|
|
if (!itemElm)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isItemSelected = false;
|
|
|
|
itemElm->GetSelected(&isItemSelected);
|
|
|
|
return isItemSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XULSelectControlAccessible::UnselectAll()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
multiSelectControl ?
|
|
|
|
multiSelectControl->ClearSelection() : mSelectControl->SetSelectedIndex(-1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
XULSelectControlAccessible::SelectAll()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
if (multiSelectControl) {
|
|
|
|
multiSelectControl->SelectAll();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, don't support this method
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// XULSelectControlAccessible: Widgets
|
|
|
|
|
2012-05-28 18:18:45 -07:00
|
|
|
Accessible*
|
2000-12-31 16:12:15 -08:00
|
|
|
XULSelectControlAccessible::CurrentItem()
|
|
|
|
{
|
|
|
|
if (!mSelectControl)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2000-12-31 16:12:15 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> currentItemElm;
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
if (multiSelectControl)
|
|
|
|
multiSelectControl->GetCurrentItem(getter_AddRefs(currentItemElm));
|
|
|
|
else
|
|
|
|
mSelectControl->GetSelectedItem(getter_AddRefs(currentItemElm));
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> DOMNode;
|
|
|
|
if (currentItemElm)
|
|
|
|
DOMNode = do_QueryInterface(currentItemElm);
|
|
|
|
|
|
|
|
if (DOMNode) {
|
2012-05-27 02:01:40 -07:00
|
|
|
DocAccessible* document = Document();
|
2000-12-31 16:12:15 -08:00
|
|
|
if (document)
|
|
|
|
return document->GetAccessible(DOMNode);
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2000-12-31 16:12:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-05-28 18:18:45 -07:00
|
|
|
XULSelectControlAccessible::SetCurrentItem(Accessible* aItem)
|
2000-12-31 16:12:15 -08:00
|
|
|
{
|
|
|
|
if (!mSelectControl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm =
|
|
|
|
do_QueryInterface(aItem->GetContent());
|
|
|
|
nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl =
|
|
|
|
do_QueryInterface(mSelectControl);
|
|
|
|
if (multiSelectControl)
|
|
|
|
multiSelectControl->SetCurrentItem(itemElm);
|
|
|
|
else
|
|
|
|
mSelectControl->SetSelectedItem(itemElm);
|
|
|
|
}
|