diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /accessible/xul | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'accessible/xul')
21 files changed, 5294 insertions, 0 deletions
diff --git a/accessible/xul/XULAlertAccessible.cpp b/accessible/xul/XULAlertAccessible.cpp new file mode 100644 index 0000000000..d6fbea4cbc --- /dev/null +++ b/accessible/xul/XULAlertAccessible.cpp @@ -0,0 +1,44 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULAlertAccessible.h" + +#include "Accessible-inl.h" +#include "Role.h" +#include "States.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULAlertAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULAlertAccessible::XULAlertAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) { + mGenericTypes |= eAlert; +} + +XULAlertAccessible::~XULAlertAccessible() {} + +role XULAlertAccessible::NativeRole() const { return roles::ALERT; } + +uint64_t XULAlertAccessible::NativeState() const { + return Accessible::NativeState() | states::ALERT; +} + +ENameValueFlag XULAlertAccessible::Name(nsString& aName) const { + // Screen readers need to read contents of alert, not the accessible name. + // If we have both some screen readers will read the alert twice. + aName.Truncate(); + return eNameOK; +} + +//////////////////////////////////////////////////////////////////////////////// +// Widgets + +bool XULAlertAccessible::IsWidget() const { return true; } + +Accessible* XULAlertAccessible::ContainerWidget() const { return nullptr; } diff --git a/accessible/xul/XULAlertAccessible.h b/accessible/xul/XULAlertAccessible.h new file mode 100644 index 0000000000..9795bd834d --- /dev/null +++ b/accessible/xul/XULAlertAccessible.h @@ -0,0 +1,40 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULAlertAccessible_h__ +#define mozilla_a11y_XULAlertAccessible_h__ + +#include "AccessibleWrap.h" + +namespace mozilla { +namespace a11y { + +/** + * Accessible for supporting XUL alerts. + */ + +class XULAlertAccessible : public AccessibleWrap { + public: + XULAlertAccessible(nsIContent* aContent, DocAccessible* aDoc); + + NS_INLINE_DECL_REFCOUNTING_INHERITED(XULAlertAccessible, AccessibleWrap) + + // Accessible + virtual mozilla::a11y::ENameValueFlag Name(nsString& aName) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // Widgets + virtual bool IsWidget() const override; + virtual Accessible* ContainerWidget() const override; + + protected: + ~XULAlertAccessible(); +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULComboboxAccessible.cpp b/accessible/xul/XULComboboxAccessible.cpp new file mode 100644 index 0000000000..035433195f --- /dev/null +++ b/accessible/xul/XULComboboxAccessible.cpp @@ -0,0 +1,138 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULComboboxAccessible.h" + +#include "Accessible-inl.h" +#include "nsAccessibilityService.h" +#include "DocAccessible.h" +#include "nsCoreUtils.h" +#include "Role.h" +#include "States.h" + +#include "mozilla/dom/Element.h" +#include "nsIDOMXULMenuListElement.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULComboboxAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULComboboxAccessible::XULComboboxAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) { + mGenericTypes |= eCombobox; +} + +role XULComboboxAccessible::NativeRole() const { return roles::COMBOBOX; } + +uint64_t XULComboboxAccessible::NativeState() const { + // As a nsComboboxAccessible we can have the following states: + // STATE_FOCUSED + // STATE_FOCUSABLE + // STATE_HASPOPUP + // STATE_EXPANDED + // STATE_COLLAPSED + + // Get focus status from base class + uint64_t state = Accessible::NativeState(); + + nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList(); + if (menuList) { + bool isOpen = false; + menuList->GetOpen(&isOpen); + if (isOpen) + state |= states::EXPANDED; + else + state |= states::COLLAPSED; + } + + return state | states::HASPOPUP; +} + +void XULComboboxAccessible::Description(nsString& aDescription) { + aDescription.Truncate(); + // Use description of currently focused option + nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = Elm()->AsXULMenuList(); + if (!menuListElm) return; + + nsCOMPtr<dom::Element> focusedOptionItem; + menuListElm->GetSelectedItem(getter_AddRefs(focusedOptionItem)); + if (focusedOptionItem && mDoc) { + Accessible* focusedOptionAcc = mDoc->GetAccessible(focusedOptionItem); + if (focusedOptionAcc) focusedOptionAcc->Description(aDescription); + } +} + +void XULComboboxAccessible::Value(nsString& aValue) const { + aValue.Truncate(); + + // The value is the option or text shown entered in the combobox. + nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList(); + if (menuList) menuList->GetLabel(aValue); +} + +uint8_t XULComboboxAccessible::ActionCount() const { + // Just one action (click). + return 1; +} + +bool XULComboboxAccessible::DoAction(uint8_t aIndex) const { + if (aIndex != XULComboboxAccessible::eAction_Click) return false; + + // Programmaticaly toggle the combo box. + nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList(); + if (!menuList) return false; + + bool isDroppedDown = false; + menuList->GetOpen(&isDroppedDown); + menuList->SetOpen(!isDroppedDown); + return true; +} + +void XULComboboxAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + aName.Truncate(); + if (aIndex != XULComboboxAccessible::eAction_Click) return; + + nsCOMPtr<nsIDOMXULMenuListElement> menuList = Elm()->AsXULMenuList(); + if (!menuList) return; + + bool isDroppedDown = false; + menuList->GetOpen(&isDroppedDown); + if (isDroppedDown) + aName.AssignLiteral("close"); + else + aName.AssignLiteral("open"); +} + +//////////////////////////////////////////////////////////////////////////////// +// Widgets + +bool XULComboboxAccessible::IsActiveWidget() const { + if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::editable, + nsGkAtoms::_true, eIgnoreCase)) { + int32_t childCount = mChildren.Length(); + for (int32_t idx = 0; idx < childCount; idx++) { + Accessible* child = mChildren[idx]; + if (child->Role() == roles::ENTRY) + return FocusMgr()->HasDOMFocus(child->GetContent()); + } + return false; + } + + return FocusMgr()->HasDOMFocus(mContent); +} + +bool XULComboboxAccessible::AreItemsOperable() const { + nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = Elm()->AsXULMenuList(); + if (menuListElm) { + bool isOpen = false; + menuListElm->GetOpen(&isOpen); + return isOpen; + } + + return false; +} diff --git a/accessible/xul/XULComboboxAccessible.h b/accessible/xul/XULComboboxAccessible.h new file mode 100644 index 0000000000..2674a268cc --- /dev/null +++ b/accessible/xul/XULComboboxAccessible.h @@ -0,0 +1,42 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULComboboxAccessible_h__ +#define mozilla_a11y_XULComboboxAccessible_h__ + +#include "XULMenuAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * Used for XUL comboboxes like xul:menulist and autocomplete textbox. + */ +class XULComboboxAccessible : public AccessibleWrap { + public: + enum { eAction_Click = 0 }; + + XULComboboxAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual void Description(nsString& aDescription) override; + virtual void Value(nsString& aValue) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + // Widgets + virtual bool IsActiveWidget() const override; + MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual bool AreItemsOperable() const override; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULElementAccessibles.cpp b/accessible/xul/XULElementAccessibles.cpp new file mode 100644 index 0000000000..d2d200a7d1 --- /dev/null +++ b/accessible/xul/XULElementAccessibles.cpp @@ -0,0 +1,228 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULElementAccessibles.h" + +#include "Accessible-inl.h" +#include "BaseAccessibles.h" +#include "DocAccessible-inl.h" +#include "nsAccUtils.h" +#include "nsCoreUtils.h" +#include "nsTextEquivUtils.h" +#include "Relation.h" +#include "Role.h" +#include "States.h" +#include "TextUpdater.h" + +#ifdef A11Y_LOG +# include "Logging.h" +#endif + +#include "nsNameSpaceManager.h" +#include "nsNetUtil.h" +#include "nsString.h" +#include "nsTextBoxFrame.h" +#include "nsXULElement.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULLabelAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULLabelAccessible::XULLabelAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : HyperTextAccessibleWrap(aContent, aDoc) { + mType = eXULLabelType; + + // If @value attribute is given then it's rendered instead text content. In + // this case we need to create a text leaf accessible to make @value attribute + // accessible. + // XXX: text interface doesn't let you get the text by words. + nsTextBoxFrame* textBoxFrame = do_QueryFrame(mContent->GetPrimaryFrame()); + if (textBoxFrame) { + mValueTextLeaf = new XULLabelTextLeafAccessible(mContent, mDoc); + mDoc->BindToDocument(mValueTextLeaf, nullptr); + + nsAutoString text; + textBoxFrame->GetCroppedTitle(text); + mValueTextLeaf->SetText(text); + AppendChild(mValueTextLeaf); + } +} + +void XULLabelAccessible::Shutdown() { + mValueTextLeaf = nullptr; + HyperTextAccessibleWrap::Shutdown(); +} + +void XULLabelAccessible::DispatchClickEvent(nsIContent* aContent, + uint32_t aActionIndex) const { + // Bug 1578140: For labels inside buttons, The base implementation of + // DispatchClickEvent doesn't fire a command event on the button. + RefPtr<nsXULElement> el = nsXULElement::FromNodeOrNull(aContent); + if (el) { + el->Click(mozilla::dom::CallerType::System); + } +} + +ENameValueFlag XULLabelAccessible::NativeName(nsString& aName) const { + // if the value attr doesn't exist, the screen reader must get the accessible + // text from the accessible text interface or from the children + if (mValueTextLeaf) return mValueTextLeaf->Name(aName); + + return Accessible::NativeName(aName); +} + +role XULLabelAccessible::NativeRole() const { return roles::LABEL; } + +uint64_t XULLabelAccessible::NativeState() const { + // Labels and description have read only state + // They are not focusable or selectable + return HyperTextAccessibleWrap::NativeState() | states::READONLY; +} + +Relation XULLabelAccessible::RelationByType(RelationType aType) const { + Relation rel = HyperTextAccessibleWrap::RelationByType(aType); + + // The label for xul:groupbox is generated from the first xul:label + if (aType == RelationType::LABEL_FOR) { + Accessible* parent = Parent(); + if (parent && parent->Role() == roles::GROUPING && + parent->GetChildAt(0) == this) { + nsIContent* parentContent = parent->GetContent(); + if (parentContent && parentContent->IsXULElement(nsGkAtoms::groupbox)) { + rel.AppendTarget(parent); + } + } + } + + return rel; +} + +void XULLabelAccessible::UpdateLabelValue(const nsString& aValue) { +#ifdef A11Y_LOG + if (logging::IsEnabled(logging::eText)) { + logging::MsgBegin("TEXT", "text may be changed (xul:label @value update)"); + logging::Node("container", mContent); + logging::MsgEntry("old text '%s'", + NS_ConvertUTF16toUTF8(mValueTextLeaf->Text()).get()); + logging::MsgEntry("new text: '%s'", NS_ConvertUTF16toUTF8(aValue).get()); + logging::MsgEnd(); + } +#endif + + TextUpdater::Run(mDoc, mValueTextLeaf, aValue); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULLabelTextLeafAccessible +//////////////////////////////////////////////////////////////////////////////// + +role XULLabelTextLeafAccessible::NativeRole() const { return roles::TEXT_LEAF; } + +uint64_t XULLabelTextLeafAccessible::NativeState() const { + return TextLeafAccessibleWrap::NativeState() | states::READONLY; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTooltipAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTooltipAccessible::XULTooltipAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) { + mType = eXULTooltipType; +} + +uint64_t XULTooltipAccessible::NativeState() const { + return LeafAccessible::NativeState() | states::READONLY; +} + +role XULTooltipAccessible::NativeRole() const { return roles::TOOLTIP; } + +//////////////////////////////////////////////////////////////////////////////// +// XULLinkAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULLinkAccessible::XULLinkAccessible(nsIContent* aContent, DocAccessible* aDoc) + : XULLabelAccessible(aContent, aDoc) {} + +XULLinkAccessible::~XULLinkAccessible() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULLinkAccessible: Accessible + +void XULLinkAccessible::Value(nsString& aValue) const { + aValue.Truncate(); + + mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::href, aValue); +} + +ENameValueFlag XULLinkAccessible::NativeName(nsString& aName) const { + mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::value, aName); + if (!aName.IsEmpty()) return eNameOK; + + nsTextEquivUtils::GetNameFromSubtree(this, aName); + return aName.IsEmpty() ? eNameOK : eNameFromSubtree; +} + +role XULLinkAccessible::NativeRole() const { return roles::LINK; } + +uint64_t XULLinkAccessible::NativeLinkState() const { return states::LINKED; } + +uint8_t XULLinkAccessible::ActionCount() const { return 1; } + +void XULLinkAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + aName.Truncate(); + + if (aIndex == eAction_Jump) aName.AssignLiteral("jump"); +} + +bool XULLinkAccessible::DoAction(uint8_t aIndex) const { + if (aIndex != eAction_Jump) return false; + + DoCommand(); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULLinkAccessible: HyperLinkAccessible + +bool XULLinkAccessible::IsLink() const { + // Expose HyperLinkAccessible unconditionally. + return true; +} + +uint32_t XULLinkAccessible::StartOffset() { + // If XUL link accessible is not contained by hypertext accessible then + // start offset matches index in parent because the parent doesn't contains + // a text. + // XXX: accessible parent of XUL link accessible should be a hypertext + // accessible. + if (Accessible::IsLink()) return Accessible::StartOffset(); + return IndexInParent(); +} + +uint32_t XULLinkAccessible::EndOffset() { + if (Accessible::IsLink()) return Accessible::EndOffset(); + return IndexInParent() + 1; +} + +already_AddRefed<nsIURI> XULLinkAccessible::AnchorURIAt( + uint32_t aAnchorIndex) const { + if (aAnchorIndex != 0) return nullptr; + + nsAutoString href; + mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::href, href); + + dom::Document* document = mContent->OwnerDoc(); + + nsCOMPtr<nsIURI> anchorURI; + NS_NewURI(getter_AddRefs(anchorURI), href, + document->GetDocumentCharacterSet(), mContent->GetBaseURI()); + + return anchorURI.forget(); +} diff --git a/accessible/xul/XULElementAccessibles.h b/accessible/xul/XULElementAccessibles.h new file mode 100644 index 0000000000..6c5bd58f73 --- /dev/null +++ b/accessible/xul/XULElementAccessibles.h @@ -0,0 +1,109 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULElementAccessibles_h__ +#define mozilla_a11y_XULElementAccessibles_h__ + +#include "HyperTextAccessibleWrap.h" +#include "TextLeafAccessibleWrap.h" + +namespace mozilla { +namespace a11y { + +class XULLabelTextLeafAccessible; + +/** + * Used for XUL description and label elements. + */ +class XULLabelAccessible : public HyperTextAccessibleWrap { + public: + XULLabelAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual void Shutdown() override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual Relation RelationByType(RelationType aType) const override; + + void UpdateLabelValue(const nsString& aValue); + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + virtual void DispatchClickEvent(nsIContent* aContent, + uint32_t aActionIndex) const override; + + private: + RefPtr<XULLabelTextLeafAccessible> mValueTextLeaf; +}; + +inline XULLabelAccessible* Accessible::AsXULLabel() { + return IsXULLabel() ? static_cast<XULLabelAccessible*>(this) : nullptr; +} + +/** + * Used to implement text interface on XUL label accessible in case when text + * is provided by @value attribute (no underlying text frame). + */ +class XULLabelTextLeafAccessible final : public TextLeafAccessibleWrap { + public: + XULLabelTextLeafAccessible(nsIContent* aContent, DocAccessible* aDoc) + : TextLeafAccessibleWrap(aContent, aDoc) { + mStateFlags |= eSharedNode; + } + + virtual ~XULLabelTextLeafAccessible() {} + + // Accessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; +}; + +/** + * Used for XUL tooltip element. + */ +class XULTooltipAccessible : public LeafAccessible { + public: + XULTooltipAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; +}; + +class XULLinkAccessible : public XULLabelAccessible { + public: + XULLinkAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual void Value(nsString& aValue) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeLinkState() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + // HyperLinkAccessible + virtual bool IsLink() const override; + virtual uint32_t StartOffset() override; + virtual uint32_t EndOffset() override; + virtual already_AddRefed<nsIURI> AnchorURIAt( + uint32_t aAnchorIndex) const override; + + protected: + virtual ~XULLinkAccessible(); + + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + + enum { eAction_Jump = 0 }; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULFormControlAccessible.cpp b/accessible/xul/XULFormControlAccessible.cpp new file mode 100644 index 0000000000..9ae0abea08 --- /dev/null +++ b/accessible/xul/XULFormControlAccessible.cpp @@ -0,0 +1,439 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULFormControlAccessible.h" + +#include "Accessible-inl.h" +#include "HTMLFormControlAccessible.h" +#include "nsAccUtils.h" +#include "DocAccessible.h" +#include "Relation.h" +#include "Role.h" +#include "States.h" +#include "TreeWalker.h" +#include "XULMenuAccessible.h" + +#include "nsIDOMXULButtonElement.h" +#include "nsIDOMXULMenuListElement.h" +#include "nsIDOMXULRadioGroupElement.h" +#include "nsIDOMXULSelectCntrlItemEl.h" +#include "nsIFrame.h" +#include "nsITextControlFrame.h" +#include "nsMenuPopupFrame.h" +#include "nsNameSpaceManager.h" +#include "mozilla/dom/Element.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULButtonAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULButtonAccessible::XULButtonAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) { + if (ContainsMenu()) { + mGenericTypes |= eMenuButton; + } else { + mGenericTypes |= eButton; + } +} + +XULButtonAccessible::~XULButtonAccessible() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULButtonAccessible: nsISupports + +//////////////////////////////////////////////////////////////////////////////// +// XULButtonAccessible: nsIAccessible + +uint8_t XULButtonAccessible::ActionCount() const { return 1; } + +void XULButtonAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Click) aName.AssignLiteral("press"); +} + +bool XULButtonAccessible::DoAction(uint8_t aIndex) const { + if (aIndex != 0) return false; + + DoCommand(); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULButtonAccessible: Accessible + +role XULButtonAccessible::NativeRole() const { return roles::PUSHBUTTON; } + +uint64_t XULButtonAccessible::NativeState() const { + // Possible states: focused, focusable, unavailable(disabled). + + // get focus and disable status from base class + uint64_t state = Accessible::NativeState(); + + // Buttons can be checked -- they simply appear pressed in rather than checked + nsCOMPtr<nsIDOMXULButtonElement> xulButtonElement = Elm()->AsXULButton(); + if (xulButtonElement) { + nsAutoString type; + xulButtonElement->GetType(type); + if (type.EqualsLiteral("checkbox") || type.EqualsLiteral("radio")) { + state |= states::CHECKABLE; + } + // Some buttons can have their checked state set without being of type + // checkbox or radio. Expose the pressed state unconditionally. + bool checked = false; + xulButtonElement->GetChecked(&checked); + if (checked) { + state |= states::PRESSED; + } + } + + if (ContainsMenu()) state |= states::HASPOPUP; + + if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::_default)) + state |= states::DEFAULT; + + return state; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULButtonAccessible: Widgets + +bool XULButtonAccessible::IsWidget() const { return true; } + +bool XULButtonAccessible::IsActiveWidget() const { + return FocusMgr()->HasDOMFocus(mContent); +} + +bool XULButtonAccessible::AreItemsOperable() const { + if (IsMenuButton()) { + Accessible* menuPopup = mChildren.SafeElementAt(0, nullptr); + if (menuPopup) { + nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(menuPopup->GetFrame()); + return menuPopupFrame->IsOpen(); + } + } + return false; // no items +} + +Accessible* XULButtonAccessible::ContainerWidget() const { + if (IsMenuButton() && mParent && mParent->IsAutoComplete()) return mParent; + return nullptr; +} + +bool XULButtonAccessible::IsAcceptableChild(nsIContent* aEl) const { + // In general XUL button has not accessible children. Nevertheless menu + // buttons can have popup accessibles (@type="menu" or columnpicker). + return aEl->IsXULElement(nsGkAtoms::menupopup) || + aEl->IsXULElement(nsGkAtoms::popup); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULButtonAccessible protected + +bool XULButtonAccessible::ContainsMenu() const { + return mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + nsGkAtoms::menu, eCaseMatters); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULDropmarkerAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULDropmarkerAccessible::XULDropmarkerAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) {} + +uint8_t XULDropmarkerAccessible::ActionCount() const { return 1; } + +bool XULDropmarkerAccessible::DropmarkerOpen(bool aToggleOpen) const { + bool isOpen = false; + + nsIContent* parent = mContent->GetFlattenedTreeParent(); + + while (parent) { + nsCOMPtr<nsIDOMXULButtonElement> parentButtonElement = + parent->AsElement()->AsXULButton(); + if (parentButtonElement) { + parentButtonElement->GetOpen(&isOpen); + if (aToggleOpen) parentButtonElement->SetOpen(!isOpen); + return isOpen; + } + + nsCOMPtr<nsIDOMXULMenuListElement> parentMenuListElement = + parent->AsElement()->AsXULMenuList(); + if (parentMenuListElement) { + parentMenuListElement->GetOpen(&isOpen); + if (aToggleOpen) parentMenuListElement->SetOpen(!isOpen); + return isOpen; + } + parent = parent->GetFlattenedTreeParent(); + } + + return isOpen; +} + +void XULDropmarkerAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + aName.Truncate(); + if (aIndex == eAction_Click) { + if (DropmarkerOpen(false)) + aName.AssignLiteral("close"); + else + aName.AssignLiteral("open"); + } +} + +bool XULDropmarkerAccessible::DoAction(uint8_t index) const { + if (index == eAction_Click) { + DropmarkerOpen(true); // Reverse the open attribute + return true; + } + return false; +} + +role XULDropmarkerAccessible::NativeRole() const { return roles::PUSHBUTTON; } + +uint64_t XULDropmarkerAccessible::NativeState() const { + return DropmarkerOpen(false) ? states::PRESSED : 0; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULGroupboxAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULGroupboxAccessible::XULGroupboxAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) {} + +role XULGroupboxAccessible::NativeRole() const { return roles::GROUPING; } + +ENameValueFlag XULGroupboxAccessible::NativeName(nsString& aName) const { + // XXX: we use the first related accessible only. + Accessible* label = RelationByType(RelationType::LABELLED_BY).Next(); + if (label) return label->Name(aName); + + return eNameOK; +} + +Relation XULGroupboxAccessible::RelationByType(RelationType aType) const { + Relation rel = AccessibleWrap::RelationByType(aType); + + // The label for xul:groupbox is generated from the first xul:label + if (aType == RelationType::LABELLED_BY && ChildCount() > 0) { + Accessible* childAcc = GetChildAt(0); + if (childAcc->Role() == roles::LABEL && + childAcc->GetContent()->IsXULElement(nsGkAtoms::label)) { + rel.AppendTarget(childAcc); + } + } + + return rel; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULRadioButtonAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULRadioButtonAccessible::XULRadioButtonAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : RadioButtonAccessible(aContent, aDoc) {} + +uint64_t XULRadioButtonAccessible::NativeState() const { + uint64_t state = LeafAccessible::NativeState(); + state |= states::CHECKABLE; + + nsCOMPtr<nsIDOMXULSelectControlItemElement> radioButton = + Elm()->AsXULSelectControlItem(); + if (radioButton) { + bool selected = false; // Radio buttons can be selected + radioButton->GetSelected(&selected); + if (selected) { + state |= states::CHECKED; + } + } + + return state; +} + +uint64_t XULRadioButtonAccessible::NativeInteractiveState() const { + return NativelyUnavailable() ? states::UNAVAILABLE : states::FOCUSABLE; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULRadioButtonAccessible: Widgets + +Accessible* XULRadioButtonAccessible::ContainerWidget() const { + return mParent; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULRadioGroupAccessible +//////////////////////////////////////////////////////////////////////////////// + +/** + * XUL Radio Group + * The Radio Group proxies for the Radio Buttons themselves. The Group gets + * focus whereas the Buttons do not. So we only have an accessible object for + * this for the purpose of getting the proper RadioButton. Need this here to + * avoid circular reference problems when navigating the accessible tree and + * for getting to the radiobuttons. + */ + +XULRadioGroupAccessible::XULRadioGroupAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : XULSelectControlAccessible(aContent, aDoc) {} + +role XULRadioGroupAccessible::NativeRole() const { return roles::RADIO_GROUP; } + +uint64_t XULRadioGroupAccessible::NativeInteractiveState() const { + // The radio group is not focusable. Sometimes the focus controller will + // report that it is focused. That means that the actual selected radio button + // should be considered focused. + return NativelyUnavailable() ? states::UNAVAILABLE : 0; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULRadioGroupAccessible: Widgets + +bool XULRadioGroupAccessible::IsWidget() const { return true; } + +bool XULRadioGroupAccessible::IsActiveWidget() const { + return FocusMgr()->HasDOMFocus(mContent); +} + +bool XULRadioGroupAccessible::AreItemsOperable() const { return true; } + +Accessible* XULRadioGroupAccessible::CurrentItem() const { + if (!mSelectControl) { + return nullptr; + } + + RefPtr<dom::Element> currentItemElm; + nsCOMPtr<nsIDOMXULRadioGroupElement> group = + mSelectControl->AsXULRadioGroup(); + if (group) { + group->GetFocusedItem(getter_AddRefs(currentItemElm)); + } + + if (currentItemElm) { + DocAccessible* document = Document(); + if (document) { + return document->GetAccessible(currentItemElm); + } + } + + return nullptr; +} + +void XULRadioGroupAccessible::SetCurrentItem(const Accessible* aItem) { + if (!mSelectControl) { + return; + } + + nsCOMPtr<dom::Element> itemElm = aItem->Elm(); + nsCOMPtr<nsIDOMXULRadioGroupElement> group = + mSelectControl->AsXULRadioGroup(); + if (group) { + group->SetFocusedItem(itemElm); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// XULStatusBarAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULStatusBarAccessible::XULStatusBarAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) {} + +role XULStatusBarAccessible::NativeRole() const { return roles::STATUSBAR; } + +//////////////////////////////////////////////////////////////////////////////// +// XULToolbarButtonAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULToolbarButtonAccessible::XULToolbarButtonAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : XULButtonAccessible(aContent, aDoc) {} + +void XULToolbarButtonAccessible::GetPositionAndSizeInternal(int32_t* aPosInSet, + int32_t* aSetSize) { + int32_t setSize = 0; + int32_t posInSet = 0; + + Accessible* parent = Parent(); + if (!parent) return; + + uint32_t childCount = parent->ChildCount(); + for (uint32_t childIdx = 0; childIdx < childCount; childIdx++) { + Accessible* child = parent->GetChildAt(childIdx); + if (IsSeparator(child)) { // end of a group of buttons + if (posInSet) break; // we've found our group, so we're done + + setSize = 0; // not our group, so start a new group + + } else { + setSize++; // another button in the group + + if (child == this) posInSet = setSize; // we've found our button + } + } + + *aPosInSet = posInSet; + *aSetSize = setSize; +} + +bool XULToolbarButtonAccessible::IsSeparator(Accessible* aAccessible) { + nsIContent* content = aAccessible->GetContent(); + return content && content->IsAnyOfXULElements(nsGkAtoms::toolbarseparator, + nsGkAtoms::toolbarspacer, + nsGkAtoms::toolbarspring); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULToolbarButtonAccessible: Widgets + +bool XULToolbarButtonAccessible::IsAcceptableChild(nsIContent* aEl) const { + // In general XUL button has not accessible children. Nevertheless menu + // buttons can have popup accessibles (@type="menu" or columnpicker). + // Also: Toolbar buttons can have labels as children. + // But only if the label attribute is not present. + return aEl->IsXULElement(nsGkAtoms::menupopup) || + aEl->IsXULElement(nsGkAtoms::popup) || + (aEl->IsXULElement(nsGkAtoms::label) && + !mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::label)); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULToolbarAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULToolbarAccessible::XULToolbarAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) {} + +role XULToolbarAccessible::NativeRole() const { return roles::TOOLBAR; } + +ENameValueFlag XULToolbarAccessible::NativeName(nsString& aName) const { + if (mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::toolbarname, + aName)) + aName.CompressWhitespace(); + + return eNameOK; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULToolbarAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULToolbarSeparatorAccessible::XULToolbarSeparatorAccessible( + nsIContent* aContent, DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) {} + +role XULToolbarSeparatorAccessible::NativeRole() const { + return roles::SEPARATOR; +} + +uint64_t XULToolbarSeparatorAccessible::NativeState() const { return 0; } diff --git a/accessible/xul/XULFormControlAccessible.h b/accessible/xul/XULFormControlAccessible.h new file mode 100644 index 0000000000..9b8f7a8070 --- /dev/null +++ b/accessible/xul/XULFormControlAccessible.h @@ -0,0 +1,186 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef MOZILLA_A11Y_XULFormControlAccessible_H_ +#define MOZILLA_A11Y_XULFormControlAccessible_H_ + +// NOTE: alphabetically ordered +#include "AccessibleWrap.h" +#include "FormControlAccessible.h" +#include "HyperTextAccessibleWrap.h" +#include "XULSelectControlAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * Used for XUL button. + * + * @note Don't inherit from LeafAccessible - it doesn't allow children + * and a button can have a dropmarker child. + */ +class XULButtonAccessible : public AccessibleWrap { + public: + enum { eAction_Click = 0 }; + XULButtonAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // nsISupports + NS_INLINE_DECL_REFCOUNTING_INHERITED(XULButtonAccessible, AccessibleWrap) + + // Accessible + virtual mozilla::a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + // Widgets + virtual bool IsWidget() const override; + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + virtual Accessible* ContainerWidget() const override; + + virtual bool IsAcceptableChild(nsIContent* aEl) const override; + + protected: + virtual ~XULButtonAccessible(); + + // XULButtonAccessible + bool ContainsMenu() const; +}; + +/** + * Used for XUL dropmarker element. + */ +class XULDropmarkerAccessible : public LeafAccessible { + public: + enum { eAction_Click = 0 }; + XULDropmarkerAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual mozilla::a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + private: + bool DropmarkerOpen(bool aToggleOpen) const; +}; + +/** + * Used for XUL groupbox element. + */ +class XULGroupboxAccessible final : public AccessibleWrap { + public: + XULGroupboxAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual mozilla::a11y::role NativeRole() const override; + virtual Relation RelationByType(RelationType aType) const override; + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * Used for XUL radio element (radio button). + */ +class XULRadioButtonAccessible : public RadioButtonAccessible { + public: + XULRadioButtonAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + + // Widgets + virtual Accessible* ContainerWidget() const override; +}; + +/** + * Used for XUL radiogroup element. + */ +class XULRadioGroupAccessible : public XULSelectControlAccessible { + public: + XULRadioGroupAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual mozilla::a11y::role NativeRole() const override; + virtual uint64_t NativeInteractiveState() const override; + + // Widgets + virtual bool IsWidget() const override; + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + virtual Accessible* CurrentItem() const override; + virtual void SetCurrentItem(const Accessible* aItem) override; +}; + +/** + * Used for XUL statusbar element. + */ +class XULStatusBarAccessible : public AccessibleWrap { + public: + XULStatusBarAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual mozilla::a11y::role NativeRole() const override; +}; + +/** + * Used for XUL toolbarbutton element. + */ +class XULToolbarButtonAccessible : public XULButtonAccessible { + public: + XULToolbarButtonAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual void GetPositionAndSizeInternal(int32_t* aPosInSet, + int32_t* aSetSize) override; + + // nsXULToolbarButtonAccessible + static bool IsSeparator(Accessible* aAccessible); + + // Widgets + virtual bool IsAcceptableChild(nsIContent* aEl) const override; +}; + +/** + * Used for XUL toolbar element. + */ +class XULToolbarAccessible : public AccessibleWrap { + public: + XULToolbarAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual mozilla::a11y::role NativeRole() const override; + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * Used for XUL toolbarseparator element. + */ +class XULToolbarSeparatorAccessible : public LeafAccessible { + public: + XULToolbarSeparatorAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual mozilla::a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULListboxAccessible.cpp b/accessible/xul/XULListboxAccessible.cpp new file mode 100644 index 0000000000..c985f5232e --- /dev/null +++ b/accessible/xul/XULListboxAccessible.cpp @@ -0,0 +1,505 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULListboxAccessible.h" + +#include "Accessible-inl.h" +#include "nsAccessibilityService.h" +#include "nsAccUtils.h" +#include "DocAccessible.h" +#include "Role.h" +#include "States.h" + +#include "nsComponentManagerUtils.h" +#include "nsIAutoCompletePopup.h" +#include "nsIDOMXULMenuListElement.h" +#include "nsIDOMXULMultSelectCntrlEl.h" +#include "nsIDOMXULSelectCntrlItemEl.h" +#include "nsINodeList.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULColumAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULColumAccessible::XULColumAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) {} + +role XULColumAccessible::NativeRole() const { return roles::LIST; } + +uint64_t XULColumAccessible::NativeState() const { return states::READONLY; } + +//////////////////////////////////////////////////////////////////////////////// +// XULColumnItemAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULColumnItemAccessible::XULColumnItemAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : LeafAccessible(aContent, aDoc) {} + +role XULColumnItemAccessible::NativeRole() const { return roles::COLUMNHEADER; } + +uint64_t XULColumnItemAccessible::NativeState() const { + return states::READONLY; +} + +uint8_t XULColumnItemAccessible::ActionCount() const { return 1; } + +void XULColumnItemAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Click) aName.AssignLiteral("click"); +} + +bool XULColumnItemAccessible::DoAction(uint8_t aIndex) const { + if (aIndex != eAction_Click) return false; + + DoCommand(); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListboxAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULListboxAccessible::XULListboxAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : XULSelectControlAccessible(aContent, aDoc) { + nsIContent* parentContent = mContent->GetFlattenedTreeParent(); + if (parentContent) { + nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm = + parentContent->AsElement()->AsAutoCompletePopup(); + if (autoCompletePopupElm) mGenericTypes |= eAutoCompletePopup; + } + + if (IsMulticolumn()) mGenericTypes |= eTable; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListboxAccessible: Accessible + +uint64_t XULListboxAccessible::NativeState() const { + // As a XULListboxAccessible we can have the following states: + // FOCUSED, READONLY, FOCUSABLE + + // Get focus status from base class + uint64_t states = Accessible::NativeState(); + + // see if we are multiple select if so set ourselves as such + + if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::seltype, + nsGkAtoms::multiple, eCaseMatters)) { + states |= states::MULTISELECTABLE | states::EXTSELECTABLE; + } + + return states; +} + +role XULListboxAccessible::NativeRole() const { + // A richlistbox is used with the new autocomplete URL bar, and has a parent + // popup <panel>. + if (mContent->GetParent() && + mContent->GetParent()->IsXULElement(nsGkAtoms::panel)) + return roles::COMBOBOX_LIST; + + return IsMulticolumn() ? roles::TABLE : roles::LISTBOX; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListboxAccessible: Table + +uint32_t XULListboxAccessible::ColCount() const { return 0; } + +uint32_t XULListboxAccessible::RowCount() { + nsCOMPtr<nsIDOMXULSelectControlElement> element = Elm()->AsXULSelectControl(); + + uint32_t itemCount = 0; + if (element) element->GetItemCount(&itemCount); + + return itemCount; +} + +Accessible* XULListboxAccessible::CellAt(uint32_t aRowIndex, + uint32_t aColumnIndex) { + nsCOMPtr<nsIDOMXULSelectControlElement> control = Elm()->AsXULSelectControl(); + NS_ENSURE_TRUE(control, nullptr); + + RefPtr<dom::Element> element; + control->GetItemAtIndex(aRowIndex, getter_AddRefs(element)); + if (!element) return nullptr; + + Accessible* row = mDoc->GetAccessible(element); + NS_ENSURE_TRUE(row, nullptr); + + return row->GetChildAt(aColumnIndex); +} + +bool XULListboxAccessible::IsColSelected(uint32_t aColIdx) { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + int32_t selectedrowCount = 0; + nsresult rv = control->GetSelectedCount(&selectedrowCount); + NS_ENSURE_SUCCESS(rv, false); + + return selectedrowCount == static_cast<int32_t>(RowCount()); +} + +bool XULListboxAccessible::IsRowSelected(uint32_t aRowIdx) { + nsCOMPtr<nsIDOMXULSelectControlElement> control = Elm()->AsXULSelectControl(); + NS_ASSERTION(control, "Doesn't implement nsIDOMXULSelectControlElement."); + + RefPtr<dom::Element> element; + nsresult rv = control->GetItemAtIndex(aRowIdx, getter_AddRefs(element)); + NS_ENSURE_SUCCESS(rv, false); + if (!element) { + return false; + } + + nsCOMPtr<nsIDOMXULSelectControlItemElement> item = + element->AsXULSelectControlItem(); + + bool isSelected = false; + item->GetSelected(&isSelected); + return isSelected; +} + +bool XULListboxAccessible::IsCellSelected(uint32_t aRowIdx, uint32_t aColIdx) { + return IsRowSelected(aRowIdx); +} + +uint32_t XULListboxAccessible::SelectedCellCount() { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + nsCOMPtr<nsINodeList> selectedItems; + control->GetSelectedItems(getter_AddRefs(selectedItems)); + if (!selectedItems) return 0; + + uint32_t selectedItemsCount = selectedItems->Length(); + + return selectedItemsCount * ColCount(); +} + +uint32_t XULListboxAccessible::SelectedColCount() { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + int32_t selectedRowCount = 0; + nsresult rv = control->GetSelectedCount(&selectedRowCount); + NS_ENSURE_SUCCESS(rv, 0); + + return selectedRowCount > 0 && + selectedRowCount == static_cast<int32_t>(RowCount()) + ? ColCount() + : 0; +} + +uint32_t XULListboxAccessible::SelectedRowCount() { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + int32_t selectedRowCount = 0; + nsresult rv = control->GetSelectedCount(&selectedRowCount); + NS_ENSURE_SUCCESS(rv, 0); + + return selectedRowCount >= 0 ? selectedRowCount : 0; +} + +void XULListboxAccessible::SelectedCells(nsTArray<Accessible*>* aCells) { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + nsCOMPtr<nsINodeList> selectedItems; + control->GetSelectedItems(getter_AddRefs(selectedItems)); + if (!selectedItems) return; + + uint32_t selectedItemsCount = selectedItems->Length(); + + for (uint32_t index = 0; index < selectedItemsCount; index++) { + nsIContent* itemContent = selectedItems->Item(index); + Accessible* item = mDoc->GetAccessible(itemContent); + + if (item) { + uint32_t cellCount = item->ChildCount(); + for (uint32_t cellIdx = 0; cellIdx < cellCount; cellIdx++) { + Accessible* cell = mChildren[cellIdx]; + if (cell->Role() == roles::CELL) aCells->AppendElement(cell); + } + } + } +} + +void XULListboxAccessible::SelectedCellIndices(nsTArray<uint32_t>* aCells) { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + nsCOMPtr<nsINodeList> selectedItems; + control->GetSelectedItems(getter_AddRefs(selectedItems)); + if (!selectedItems) return; + + uint32_t selectedItemsCount = selectedItems->Length(); + + uint32_t colCount = ColCount(); + aCells->SetCapacity(selectedItemsCount * colCount); + aCells->AppendElements(selectedItemsCount * colCount); + + for (uint32_t selItemsIdx = 0, cellsIdx = 0; selItemsIdx < selectedItemsCount; + selItemsIdx++) { + nsIContent* itemContent = selectedItems->Item(selItemsIdx); + + nsCOMPtr<nsIDOMXULSelectControlItemElement> item = + itemContent->AsElement()->AsXULSelectControlItem(); + if (item) { + int32_t itemIdx = -1; + control->GetIndexOfItem(item, &itemIdx); + if (itemIdx >= 0) + for (uint32_t colIdx = 0; colIdx < colCount; colIdx++, cellsIdx++) + aCells->ElementAt(cellsIdx) = itemIdx * colCount + colIdx; + } + } +} + +void XULListboxAccessible::SelectedColIndices(nsTArray<uint32_t>* aCols) { + uint32_t selColCount = SelectedColCount(); + aCols->SetCapacity(selColCount); + + for (uint32_t colIdx = 0; colIdx < selColCount; colIdx++) + aCols->AppendElement(colIdx); +} + +void XULListboxAccessible::SelectedRowIndices(nsTArray<uint32_t>* aRows) { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + nsCOMPtr<nsINodeList> selectedItems; + control->GetSelectedItems(getter_AddRefs(selectedItems)); + if (!selectedItems) return; + + uint32_t rowCount = selectedItems->Length(); + + if (!rowCount) return; + + aRows->SetCapacity(rowCount); + aRows->AppendElements(rowCount); + + for (uint32_t rowIdx = 0; rowIdx < rowCount; rowIdx++) { + nsIContent* itemContent = selectedItems->Item(rowIdx); + nsCOMPtr<nsIDOMXULSelectControlItemElement> item = + itemContent->AsElement()->AsXULSelectControlItem(); + + if (item) { + int32_t itemIdx = -1; + control->GetIndexOfItem(item, &itemIdx); + if (itemIdx >= 0) aRows->ElementAt(rowIdx) = itemIdx; + } + } +} + +void XULListboxAccessible::SelectRow(uint32_t aRowIdx) { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + RefPtr<dom::Element> item; + control->GetItemAtIndex(aRowIdx, getter_AddRefs(item)); + if (!item) { + return; + } + + nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm = + item->AsXULSelectControlItem(); + control->SelectItem(itemElm); +} + +void XULListboxAccessible::UnselectRow(uint32_t aRowIdx) { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> control = + Elm()->AsXULMultiSelectControl(); + NS_ASSERTION(control, + "Doesn't implement nsIDOMXULMultiSelectControlElement."); + + RefPtr<dom::Element> item; + control->GetItemAtIndex(aRowIdx, getter_AddRefs(item)); + if (!item) { + return; + } + + nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm = + item->AsXULSelectControlItem(); + control->RemoveItemFromSelection(itemElm); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListboxAccessible: Widgets + +bool XULListboxAccessible::IsWidget() const { return true; } + +bool XULListboxAccessible::IsActiveWidget() const { + if (IsAutoCompletePopup()) { + nsIContent* parentContent = mContent->GetParent(); + if (parentContent) { + nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm = + parentContent->AsElement()->AsAutoCompletePopup(); + if (autoCompletePopupElm) { + bool isOpen = false; + autoCompletePopupElm->GetPopupOpen(&isOpen); + return isOpen; + } + } + } + return FocusMgr()->HasDOMFocus(mContent); +} + +bool XULListboxAccessible::AreItemsOperable() const { + if (IsAutoCompletePopup()) { + nsIContent* parentContent = mContent->GetParent(); + if (parentContent) { + nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm = + parentContent->AsElement()->AsAutoCompletePopup(); + if (autoCompletePopupElm) { + bool isOpen = false; + autoCompletePopupElm->GetPopupOpen(&isOpen); + return isOpen; + } + } + } + return true; +} + +Accessible* XULListboxAccessible::ContainerWidget() const { + if (IsAutoCompletePopup() && mContent->GetParent()) { + // This works for XUL autocompletes. It doesn't work for HTML forms + // autocomplete because of potential crossprocess calls (when autocomplete + // lives in content process while popup lives in chrome process). If that's + // a problem then rethink Widgets interface. + nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = + mContent->GetParent()->AsElement()->AsXULMenuList(); + if (menuListElm) { + RefPtr<mozilla::dom::Element> inputElm; + menuListElm->GetInputField(getter_AddRefs(inputElm)); + if (inputElm) { + Accessible* input = mDoc->GetAccessible(inputElm); + return input ? input->ContainerWidget() : nullptr; + } + } + } + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListitemAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULListitemAccessible::XULListitemAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : XULMenuitemAccessible(aContent, aDoc) { + mIsCheckbox = mContent->AsElement()->AttrValueIs( + kNameSpaceID_None, nsGkAtoms::type, nsGkAtoms::checkbox, eCaseMatters); + mType = eXULListItemType; +} + +XULListitemAccessible::~XULListitemAccessible() {} + +Accessible* XULListitemAccessible::GetListAccessible() const { + if (IsDefunct()) return nullptr; + + nsCOMPtr<nsIDOMXULSelectControlItemElement> listItem = + Elm()->AsXULSelectControlItem(); + if (!listItem) return nullptr; + + RefPtr<dom::Element> listElement; + listItem->GetControl(getter_AddRefs(listElement)); + if (!listElement) return nullptr; + + return mDoc->GetAccessible(listElement); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListitemAccessible Accessible + +void XULListitemAccessible::Description(nsString& aDesc) { + AccessibleWrap::Description(aDesc); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListitemAccessible: Accessible + +/** + * Get the name from GetXULName. + */ +ENameValueFlag XULListitemAccessible::NativeName(nsString& aName) const { + return Accessible::NativeName(aName); +} + +role XULListitemAccessible::NativeRole() const { + Accessible* list = GetListAccessible(); + if (!list) { + NS_ERROR("No list accessible for listitem accessible!"); + return roles::NOTHING; + } + + if (list->Role() == roles::TABLE) return roles::ROW; + + if (mIsCheckbox) return roles::CHECK_RICH_OPTION; + + if (mParent && mParent->Role() == roles::COMBOBOX_LIST) + return roles::COMBOBOX_OPTION; + + return roles::RICH_OPTION; +} + +uint64_t XULListitemAccessible::NativeState() const { + if (mIsCheckbox) return XULMenuitemAccessible::NativeState(); + + uint64_t states = NativeInteractiveState(); + + nsCOMPtr<nsIDOMXULSelectControlItemElement> listItem = + Elm()->AsXULSelectControlItem(); + if (listItem) { + bool isSelected; + listItem->GetSelected(&isSelected); + if (isSelected) states |= states::SELECTED; + + if (FocusMgr()->IsFocused(this)) states |= states::FOCUSED; + } + + return states; +} + +uint64_t XULListitemAccessible::NativeInteractiveState() const { + return NativelyUnavailable() || (mParent && mParent->NativelyUnavailable()) + ? states::UNAVAILABLE + : states::FOCUSABLE | states::SELECTABLE; +} + +void XULListitemAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Click && mIsCheckbox) { + uint64_t states = NativeState(); + if (states & states::CHECKED) + aName.AssignLiteral("uncheck"); + else + aName.AssignLiteral("check"); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// XULListitemAccessible: Widgets + +Accessible* XULListitemAccessible::ContainerWidget() const { return Parent(); } diff --git a/accessible/xul/XULListboxAccessible.h b/accessible/xul/XULListboxAccessible.h new file mode 100644 index 0000000000..e9f3a70721 --- /dev/null +++ b/accessible/xul/XULListboxAccessible.h @@ -0,0 +1,140 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULListboxAccessible_h__ +#define mozilla_a11y_XULListboxAccessible_h__ + +#include "BaseAccessibles.h" +#include "TableAccessible.h" +#include "TableCellAccessible.h" +#include "XULMenuAccessible.h" +#include "XULSelectControlAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * XULColumAccessible are accessible for list and tree columns elements + * (xul:treecols and xul:listheader). + */ +class XULColumAccessible : public AccessibleWrap { + public: + XULColumAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; +}; + +/** + * XULColumnItemAccessible are accessibles for list and tree column elements + * (xul:treecol). + */ +class XULColumnItemAccessible : public LeafAccessible { + public: + XULColumnItemAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + enum { eAction_Click = 0 }; +}; + +/* + * A class the represents the XUL Listbox widget. + */ +class XULListboxAccessible : public XULSelectControlAccessible, + public TableAccessible { + public: + XULListboxAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // TableAccessible + virtual uint32_t ColCount() const override; + virtual uint32_t RowCount() override; + virtual Accessible* CellAt(uint32_t aRowIndex, + uint32_t aColumnIndex) override; + virtual bool IsColSelected(uint32_t aColIdx) override; + virtual bool IsRowSelected(uint32_t aRowIdx) override; + virtual bool IsCellSelected(uint32_t aRowIdx, uint32_t aColIdx) override; + virtual uint32_t SelectedCellCount() override; + virtual uint32_t SelectedColCount() override; + virtual uint32_t SelectedRowCount() override; + virtual void SelectedCells(nsTArray<Accessible*>* aCells) override; + virtual void SelectedCellIndices(nsTArray<uint32_t>* aCells) override; + virtual void SelectedColIndices(nsTArray<uint32_t>* aCols) override; + virtual void SelectedRowIndices(nsTArray<uint32_t>* aRows) override; + virtual void SelectRow(uint32_t aRowIdx) override; + virtual void UnselectRow(uint32_t aRowIdx) override; + virtual Accessible* AsAccessible() override { return this; } + + // Accessible + virtual TableAccessible* AsTable() override { return this; } + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // Widgets + virtual bool IsWidget() const override; + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + + virtual Accessible* ContainerWidget() const override; + + protected: + virtual ~XULListboxAccessible() {} + + bool IsMulticolumn() const { return ColCount() > 1; } +}; + +/** + * Listitems -- used in listboxes + */ +class XULListitemAccessible : public XULMenuitemAccessible { + public: + enum { eAction_Click = 0 }; + + NS_INLINE_DECL_REFCOUNTING_INHERITED(XULListitemAccessible, + XULMenuitemAccessible) + + XULListitemAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual void Description(nsString& aDesc) override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + + // Actions + virtual void ActionNameAt(uint8_t index, nsAString& aName) override; + + // Widgets + virtual Accessible* ContainerWidget() const override; + + protected: + virtual ~XULListitemAccessible(); + + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; + + // XULListitemAccessible + + /** + * Return listbox accessible for the listitem. + */ + Accessible* GetListAccessible() const; + + private: + bool mIsCheckbox; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULMenuAccessible.cpp b/accessible/xul/XULMenuAccessible.cpp new file mode 100644 index 0000000000..a0d98d1aaf --- /dev/null +++ b/accessible/xul/XULMenuAccessible.cpp @@ -0,0 +1,513 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULMenuAccessible.h" + +#include "Accessible-inl.h" +#include "nsAccessibilityService.h" +#include "nsAccUtils.h" +#include "DocAccessible.h" +#include "Role.h" +#include "States.h" +#include "XULFormControlAccessible.h" + +#include "nsIDOMXULContainerElement.h" +#include "nsIDOMXULSelectCntrlEl.h" +#include "nsIDOMXULSelectCntrlItemEl.h" +#include "nsIContent.h" +#include "nsMenuBarFrame.h" +#include "nsMenuPopupFrame.h" + +#include "mozilla/Preferences.h" +#include "mozilla/LookAndFeel.h" +#include "mozilla/dom/Document.h" +#include "mozilla/dom/Element.h" +#include "mozilla/dom/KeyboardEventBinding.h" + +using namespace mozilla; +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULMenuitemAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULMenuitemAccessible::XULMenuitemAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) {} + +uint64_t XULMenuitemAccessible::NativeState() const { + uint64_t state = Accessible::NativeState(); + + // Has Popup? + if (mContent->NodeInfo()->Equals(nsGkAtoms::menu, kNameSpaceID_XUL)) { + state |= states::HASPOPUP; + if (mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::open)) + state |= states::EXPANDED; + else + state |= states::COLLAPSED; + } + + // Checkable/checked? + static dom::Element::AttrValuesArray strings[] = { + nsGkAtoms::radio, nsGkAtoms::checkbox, nullptr}; + + if (mContent->AsElement()->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type, + strings, eCaseMatters) >= 0) { + // Checkable? + state |= states::CHECKABLE; + + // Checked? + if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, + nsGkAtoms::checked, nsGkAtoms::_true, + eCaseMatters)) + state |= states::CHECKED; + } + + // Combo box listitem + bool isComboboxOption = (Role() == roles::COMBOBOX_OPTION); + if (isComboboxOption) { + // Is selected? + bool isSelected = false; + nsCOMPtr<nsIDOMXULSelectControlItemElement> item = + Elm()->AsXULSelectControlItem(); + NS_ENSURE_TRUE(item, state); + item->GetSelected(&isSelected); + + // Is collapsed? + bool isCollapsed = false; + Accessible* parent = Parent(); + if (parent && parent->State() & states::INVISIBLE) isCollapsed = true; + + if (isSelected) { + state |= states::SELECTED; + + // Selected and collapsed? + if (isCollapsed) { + // Set selected option offscreen/invisible according to combobox state + Accessible* grandParent = parent->Parent(); + if (!grandParent) return state; + NS_ASSERTION(grandParent->IsCombobox(), + "grandparent of combobox listitem is not combobox"); + uint64_t grandParentState = grandParent->State(); + state &= ~(states::OFFSCREEN | states::INVISIBLE); + state |= (grandParentState & states::OFFSCREEN) | + (grandParentState & states::INVISIBLE) | + (grandParentState & states::OPAQUE1); + } // isCollapsed + } // isSelected + } // ROLE_COMBOBOX_OPTION + + return state; +} + +uint64_t XULMenuitemAccessible::NativeInteractiveState() const { + if (NativelyUnavailable()) { + // Note: keep in sinc with nsXULPopupManager::IsValidMenuItem() logic. + bool skipNavigatingDisabledMenuItem = true; + nsMenuFrame* menuFrame = do_QueryFrame(GetFrame()); + if (!menuFrame || !menuFrame->IsOnMenuBar()) { + skipNavigatingDisabledMenuItem = + LookAndFeel::GetInt( + LookAndFeel::IntID::SkipNavigatingDisabledMenuItem, 0) != 0; + } + + if (skipNavigatingDisabledMenuItem) return states::UNAVAILABLE; + + return states::UNAVAILABLE | states::FOCUSABLE | states::SELECTABLE; + } + + return states::FOCUSABLE | states::SELECTABLE; +} + +ENameValueFlag XULMenuitemAccessible::NativeName(nsString& aName) const { + mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, aName); + return eNameOK; +} + +void XULMenuitemAccessible::Description(nsString& aDescription) { + mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::description, + aDescription); +} + +KeyBinding XULMenuitemAccessible::AccessKey() const { + // Return menu accesskey: N or Alt+F. + static int32_t gMenuAccesskeyModifier = + -1; // magic value of -1 indicates unitialized state + + // We do not use nsCoreUtils::GetAccesskeyFor() because accesskeys for + // menu are't registered by EventStateManager. + nsAutoString accesskey; + mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey, + accesskey); + if (accesskey.IsEmpty()) return KeyBinding(); + + uint32_t modifierKey = 0; + + Accessible* parentAcc = Parent(); + if (parentAcc) { + if (parentAcc->NativeRole() == roles::MENUBAR) { + // If top level menu item, add Alt+ or whatever modifier text to string + // No need to cache pref service, this happens rarely + if (gMenuAccesskeyModifier == -1) { + // Need to initialize cached global accesskey pref + gMenuAccesskeyModifier = Preferences::GetInt("ui.key.menuAccessKey", 0); + } + + switch (gMenuAccesskeyModifier) { + case dom::KeyboardEvent_Binding::DOM_VK_CONTROL: + modifierKey = KeyBinding::kControl; + break; + case dom::KeyboardEvent_Binding::DOM_VK_ALT: + modifierKey = KeyBinding::kAlt; + break; + case dom::KeyboardEvent_Binding::DOM_VK_META: + modifierKey = KeyBinding::kMeta; + break; + case dom::KeyboardEvent_Binding::DOM_VK_WIN: + modifierKey = KeyBinding::kOS; + break; + } + } + } + + return KeyBinding(accesskey[0], modifierKey); +} + +KeyBinding XULMenuitemAccessible::KeyboardShortcut() const { + nsAutoString keyElmId; + mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::key, keyElmId); + if (keyElmId.IsEmpty()) return KeyBinding(); + + dom::Element* keyElm = mContent->OwnerDoc()->GetElementById(keyElmId); + if (!keyElm) return KeyBinding(); + + uint32_t key = 0; + + nsAutoString keyStr; + keyElm->GetAttr(kNameSpaceID_None, nsGkAtoms::key, keyStr); + if (keyStr.IsEmpty()) { + nsAutoString keyCodeStr; + keyElm->GetAttr(kNameSpaceID_None, nsGkAtoms::keycode, keyCodeStr); + nsresult errorCode; + key = keyStr.ToInteger(&errorCode, /* aRadix = */ 10); + if (NS_FAILED(errorCode)) { + key = keyStr.ToInteger(&errorCode, /* aRadix = */ 16); + } + } else { + key = keyStr[0]; + } + + nsAutoString modifiersStr; + keyElm->GetAttr(kNameSpaceID_None, nsGkAtoms::modifiers, modifiersStr); + + uint32_t modifierMask = 0; + if (modifiersStr.Find("shift") != -1) modifierMask |= KeyBinding::kShift; + if (modifiersStr.Find("alt") != -1) modifierMask |= KeyBinding::kAlt; + if (modifiersStr.Find("meta") != -1) modifierMask |= KeyBinding::kMeta; + if (modifiersStr.Find("os") != -1) modifierMask |= KeyBinding::kOS; + if (modifiersStr.Find("control") != -1) modifierMask |= KeyBinding::kControl; + if (modifiersStr.Find("accel") != -1) { + modifierMask |= KeyBinding::AccelModifier(); + } + + return KeyBinding(key, modifierMask); +} + +role XULMenuitemAccessible::NativeRole() const { + nsCOMPtr<nsIDOMXULContainerElement> xulContainer = Elm()->AsXULContainer(); + if (xulContainer) return roles::PARENT_MENUITEM; + + Accessible* widget = ContainerWidget(); + if (widget && widget->Role() == roles::COMBOBOX_LIST) { + return roles::COMBOBOX_OPTION; + } + + if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + nsGkAtoms::radio, eCaseMatters)) + return roles::RADIO_MENU_ITEM; + + if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type, + nsGkAtoms::checkbox, eCaseMatters)) + return roles::CHECK_MENU_ITEM; + + return roles::MENUITEM; +} + +int32_t XULMenuitemAccessible::GetLevelInternal() { + return nsAccUtils::GetLevelForXULContainerItem(mContent); +} + +bool XULMenuitemAccessible::DoAction(uint8_t index) const { + if (index == eAction_Click) { // default action + DoCommand(); + return true; + } + + return false; +} + +void XULMenuitemAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Click) aName.AssignLiteral("click"); +} + +uint8_t XULMenuitemAccessible::ActionCount() const { return 1; } + +//////////////////////////////////////////////////////////////////////////////// +// XULMenuitemAccessible: Widgets + +bool XULMenuitemAccessible::IsActiveWidget() const { + // Parent menu item is a widget, it's active when its popup is open. + // Typically the <menupopup> is included in the document markup, and + // <menu> prepends content in front of it. + nsIContent* menuPopupContent = mContent->GetLastChild(); + if (menuPopupContent) { + nsMenuPopupFrame* menuPopupFrame = + do_QueryFrame(menuPopupContent->GetPrimaryFrame()); + return menuPopupFrame && menuPopupFrame->IsOpen(); + } + return false; +} + +bool XULMenuitemAccessible::AreItemsOperable() const { + // Parent menu item is a widget, its items are operable when its popup is + // open. + nsIContent* menuPopupContent = mContent->GetLastChild(); + if (menuPopupContent) { + nsMenuPopupFrame* menuPopupFrame = + do_QueryFrame(menuPopupContent->GetPrimaryFrame()); + return menuPopupFrame && menuPopupFrame->IsOpen(); + } + return false; +} + +Accessible* XULMenuitemAccessible::ContainerWidget() const { + nsMenuFrame* menuFrame = do_QueryFrame(GetFrame()); + if (menuFrame) { + nsMenuParent* menuParent = menuFrame->GetMenuParent(); + if (menuParent) { + nsBoxFrame* frame = nullptr; + if (menuParent->IsMenuBar()) { // menubar menu + frame = static_cast<nsMenuBarFrame*>(menuParent); + } else if (menuParent->IsMenu()) { // a menupopup or parent menu item + frame = static_cast<nsMenuPopupFrame*>(menuParent); + } + if (frame) { + nsIContent* content = frame->GetContent(); + if (content) { + MOZ_ASSERT(mDoc); + // We use GetAccessibleOrContainer instead of just GetAccessible + // because we strip menupopups from the tree for ATK. + return mDoc->GetAccessibleOrContainer(content); + } + } + + // otherwise it's different kind of popups (like panel or tooltip), it + // shouldn't be a real case. + } + } + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULMenuSeparatorAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULMenuSeparatorAccessible::XULMenuSeparatorAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : XULMenuitemAccessible(aContent, aDoc) {} + +uint64_t XULMenuSeparatorAccessible::NativeState() const { + // Isn't focusable, but can be offscreen/invisible -- only copy those states + return XULMenuitemAccessible::NativeState() & + (states::OFFSCREEN | states::INVISIBLE); +} + +ENameValueFlag XULMenuSeparatorAccessible::NativeName(nsString& aName) const { + return eNameOK; +} + +role XULMenuSeparatorAccessible::NativeRole() const { return roles::SEPARATOR; } + +bool XULMenuSeparatorAccessible::DoAction(uint8_t index) const { return false; } + +void XULMenuSeparatorAccessible::ActionNameAt(uint8_t aIndex, + nsAString& aName) { + aName.Truncate(); +} + +uint8_t XULMenuSeparatorAccessible::ActionCount() const { return 0; } + +//////////////////////////////////////////////////////////////////////////////// +// XULMenupopupAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULMenupopupAccessible::XULMenupopupAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : XULSelectControlAccessible(aContent, aDoc) { + nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetFrame()); + if (menuPopupFrame && menuPopupFrame->IsMenu()) mType = eMenuPopupType; + + // May be the anonymous <menupopup> inside <menulist> (a combobox) + auto* parent = mContent->GetParentElement(); + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + parent ? parent->AsXULSelectControl() : nullptr; + if (selectControl) { + mSelectControl = parent; + } else { + mSelectControl = nullptr; + mGenericTypes &= ~eSelect; + } +} + +uint64_t XULMenupopupAccessible::NativeState() const { + uint64_t state = Accessible::NativeState(); + +#ifdef DEBUG + // We are onscreen if our parent is active + bool isActive = + mContent->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::menuactive); + if (!isActive) { + Accessible* parent = Parent(); + if (parent) { + nsIContent* parentContent = parent->GetContent(); + if (parentContent && parentContent->IsElement()) + isActive = parentContent->AsElement()->HasAttr(kNameSpaceID_None, + nsGkAtoms::open); + } + } + + NS_ASSERTION(isActive || (state & states::INVISIBLE), + "XULMenupopup doesn't have INVISIBLE when it's inactive"); +#endif + + if (state & states::INVISIBLE) state |= states::OFFSCREEN | states::COLLAPSED; + + return state; +} + +ENameValueFlag XULMenupopupAccessible::NativeName(nsString& aName) const { + nsIContent* content = mContent; + while (content && aName.IsEmpty()) { + if (content->IsElement()) { + content->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, aName); + } + content = content->GetFlattenedTreeParent(); + } + + return eNameOK; +} + +role XULMenupopupAccessible::NativeRole() const { + nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetFrame()); + if (menuPopupFrame && menuPopupFrame->IsContextMenu()) { + return roles::MENUPOPUP; + } + + if (mParent) { + if (mParent->IsCombobox() || mParent->IsAutoComplete()) + return roles::COMBOBOX_LIST; + + if (mParent->Role() == roles::PUSHBUTTON) { + // Some widgets like the search bar have several popups, owned by buttons. + Accessible* grandParent = mParent->Parent(); + if (grandParent && grandParent->IsAutoComplete()) + return roles::COMBOBOX_LIST; + } + } + + // If accessible is not bound to the tree (this happens while children are + // cached) return general role. + return roles::MENUPOPUP; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULMenupopupAccessible: Widgets + +bool XULMenupopupAccessible::IsWidget() const { return true; } + +bool XULMenupopupAccessible::IsActiveWidget() const { + // If menupopup is a widget (the case of context menus) then active when open. + nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetFrame()); + return menuPopupFrame && menuPopupFrame->IsOpen(); +} + +bool XULMenupopupAccessible::AreItemsOperable() const { + nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetFrame()); + return menuPopupFrame && menuPopupFrame->IsOpen(); +} + +Accessible* XULMenupopupAccessible::ContainerWidget() const { + DocAccessible* document = Document(); + + nsMenuPopupFrame* menuPopupFrame = do_QueryFrame(GetFrame()); + while (menuPopupFrame) { + Accessible* menuPopup = + document->GetAccessible(menuPopupFrame->GetContent()); + if (!menuPopup) // shouldn't be a real case + return nullptr; + + nsMenuFrame* menuFrame = do_QueryFrame(menuPopupFrame->GetParent()); + if (!menuFrame) // context menu or popups + return nullptr; + + nsMenuParent* menuParent = menuFrame->GetMenuParent(); + if (!menuParent) // menulist or menubutton + return menuPopup->Parent(); + + if (menuParent->IsMenuBar()) { // menubar menu + nsMenuBarFrame* menuBarFrame = static_cast<nsMenuBarFrame*>(menuParent); + return document->GetAccessible(menuBarFrame->GetContent()); + } + + // different kind of popups like panel or tooltip + if (!menuParent->IsMenu()) return nullptr; + + menuPopupFrame = static_cast<nsMenuPopupFrame*>(menuParent); + } + + MOZ_ASSERT_UNREACHABLE("Shouldn't be a real case."); + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULMenubarAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULMenubarAccessible::XULMenubarAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) {} + +ENameValueFlag XULMenubarAccessible::NativeName(nsString& aName) const { + aName.AssignLiteral("Application"); + return eNameOK; +} + +role XULMenubarAccessible::NativeRole() const { return roles::MENUBAR; } + +//////////////////////////////////////////////////////////////////////////////// +// XULMenubarAccessible: Widgets + +bool XULMenubarAccessible::IsActiveWidget() const { + nsMenuBarFrame* menuBarFrame = do_QueryFrame(GetFrame()); + return menuBarFrame && menuBarFrame->IsActive(); +} + +bool XULMenubarAccessible::AreItemsOperable() const { return true; } + +Accessible* XULMenubarAccessible::CurrentItem() const { + nsMenuBarFrame* menuBarFrame = do_QueryFrame(GetFrame()); + if (menuBarFrame) { + nsMenuFrame* menuFrame = menuBarFrame->GetCurrentMenuItem(); + if (menuFrame) { + nsIContent* menuItemNode = menuFrame->GetContent(); + return mDoc->GetAccessible(menuItemNode); + } + } + return nullptr; +} + +void XULMenubarAccessible::SetCurrentItem(const Accessible* aItem) { + NS_ERROR("XULMenubarAccessible::SetCurrentItem not implemented"); +} diff --git a/accessible/xul/XULMenuAccessible.h b/accessible/xul/XULMenuAccessible.h new file mode 100644 index 0000000000..e2891af752 --- /dev/null +++ b/accessible/xul/XULMenuAccessible.h @@ -0,0 +1,116 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULMenuAccessible_h__ +#define mozilla_a11y_XULMenuAccessible_h__ + +#include "AccessibleWrap.h" +#include "XULSelectControlAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * Used for XUL menu, menuitem elements. + */ +class XULMenuitemAccessible : public AccessibleWrap { + public: + enum { eAction_Click = 0 }; + + XULMenuitemAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual void Description(nsString& aDescription) override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + virtual int32_t GetLevelInternal() override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + virtual KeyBinding AccessKey() const override; + virtual KeyBinding KeyboardShortcut() const override; + + // Widgets + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + virtual Accessible* ContainerWidget() const override; + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * Used for XUL menuseparator element. + */ +class XULMenuSeparatorAccessible : public XULMenuitemAccessible { + public: + XULMenuSeparatorAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * Used for XUL menupopup and panel. + */ +class XULMenupopupAccessible : public XULSelectControlAccessible { + public: + XULMenupopupAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + + // Widgets + virtual bool IsWidget() const override; + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + + virtual Accessible* ContainerWidget() const override; + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * Used for XUL menubar element. + */ +class XULMenubarAccessible : public AccessibleWrap { + public: + XULMenubarAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + + // Widget + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + virtual Accessible* CurrentItem() const override; + virtual void SetCurrentItem(const Accessible* aItem) override; + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULSelectControlAccessible.cpp b/accessible/xul/XULSelectControlAccessible.cpp new file mode 100644 index 0000000000..95854c7d48 --- /dev/null +++ b/accessible/xul/XULSelectControlAccessible.cpp @@ -0,0 +1,253 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* 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/. */ + +#include "XULSelectControlAccessible.h" + +#include "nsAccessibilityService.h" +#include "DocAccessible.h" + +#include "nsIDOMXULSelectCntrlItemEl.h" +#include "nsIDOMXULMultSelectCntrlEl.h" + +#include "mozilla/dom/Element.h" +#include "mozilla/dom/KeyboardEventBinding.h" + +using namespace mozilla; +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULSelectControlAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULSelectControlAccessible::XULSelectControlAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) { + mGenericTypes |= eSelect; + mSelectControl = aContent->AsElement(); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULSelectControlAccessible: Accessible + +void XULSelectControlAccessible::Shutdown() { + mSelectControl = nullptr; + AccessibleWrap::Shutdown(); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULSelectControlAccessible: SelectAccessible + +void XULSelectControlAccessible::SelectedItems(nsTArray<Accessible*>* aItems) { + // For XUL multi-select control + nsCOMPtr<nsIDOMXULMultiSelectControlElement> xulMultiSelect = + mSelectControl->AsXULMultiSelectControl(); + if (xulMultiSelect) { + int32_t length = 0; + xulMultiSelect->GetSelectedCount(&length); + for (int32_t index = 0; index < length; index++) { + RefPtr<dom::Element> element; + xulMultiSelect->MultiGetSelectedItem(index, getter_AddRefs(element)); + Accessible* item = mDoc->GetAccessible(element); + if (item) aItems->AppendElement(item); + } + } else { // Single select? + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + RefPtr<dom::Element> element; + selectControl->GetSelectedItem(getter_AddRefs(element)); + if (element) { + Accessible* item = mDoc->GetAccessible(element); + if (item) aItems->AppendElement(item); + } + } +} + +Accessible* XULSelectControlAccessible::GetSelectedItem(uint32_t aIndex) { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + mSelectControl->AsXULMultiSelectControl(); + + RefPtr<dom::Element> element; + if (multiSelectControl) { + multiSelectControl->MultiGetSelectedItem(aIndex, getter_AddRefs(element)); + } else if (aIndex == 0) { + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + if (selectControl) { + selectControl->GetSelectedItem(getter_AddRefs(element)); + } + } + + return element && mDoc ? mDoc->GetAccessible(element) : nullptr; +} + +uint32_t XULSelectControlAccessible::SelectedItemCount() { + // For XUL multi-select control + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + mSelectControl->AsXULMultiSelectControl(); + if (multiSelectControl) { + int32_t count = 0; + multiSelectControl->GetSelectedCount(&count); + return count; + } + + // For XUL single-select control/menulist + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + if (selectControl) { + int32_t index = -1; + selectControl->GetSelectedIndex(&index); + return (index >= 0) ? 1 : 0; + } + + return 0; +} + +bool XULSelectControlAccessible::AddItemToSelection(uint32_t aIndex) { + Accessible* item = GetChildAt(aIndex); + if (!item || !item->GetContent()) return false; + + nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm = + item->GetContent()->AsElement()->AsXULSelectControlItem(); + if (!itemElm) return false; + + bool isItemSelected = false; + itemElm->GetSelected(&isItemSelected); + if (isItemSelected) return true; + + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + mSelectControl->AsXULMultiSelectControl(); + + if (multiSelectControl) { + multiSelectControl->AddItemToSelection(itemElm); + } else { + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + if (selectControl) { + selectControl->SetSelectedItem(item->Elm()); + } + } + + return true; +} + +bool XULSelectControlAccessible::RemoveItemFromSelection(uint32_t aIndex) { + Accessible* item = GetChildAt(aIndex); + if (!item || !item->GetContent()) return false; + + nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm = + item->GetContent()->AsElement()->AsXULSelectControlItem(); + if (!itemElm) return false; + + bool isItemSelected = false; + itemElm->GetSelected(&isItemSelected); + if (!isItemSelected) return true; + + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + mSelectControl->AsXULMultiSelectControl(); + + if (multiSelectControl) { + multiSelectControl->RemoveItemFromSelection(itemElm); + } else { + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + if (selectControl) { + selectControl->SetSelectedItem(nullptr); + } + } + + return true; +} + +bool XULSelectControlAccessible::IsItemSelected(uint32_t aIndex) { + Accessible* item = GetChildAt(aIndex); + if (!item || !item->GetContent()) return false; + + nsCOMPtr<nsIDOMXULSelectControlItemElement> itemElm = + item->GetContent()->AsElement()->AsXULSelectControlItem(); + if (!itemElm) return false; + + bool isItemSelected = false; + itemElm->GetSelected(&isItemSelected); + return isItemSelected; +} + +bool XULSelectControlAccessible::UnselectAll() { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + mSelectControl->AsXULMultiSelectControl(); + if (multiSelectControl) { + multiSelectControl->ClearSelection(); + } else { + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + if (selectControl) { + selectControl->SetSelectedIndex(-1); + } + } + + return true; +} + +bool XULSelectControlAccessible::SelectAll() { + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + mSelectControl->AsXULMultiSelectControl(); + if (multiSelectControl) { + multiSelectControl->SelectAll(); + return true; + } + + // otherwise, don't support this method + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULSelectControlAccessible: Widgets + +Accessible* XULSelectControlAccessible::CurrentItem() const { + // aria-activedescendant should override. + Accessible* current = AccessibleWrap::CurrentItem(); + if (current) { + return current; + } + + if (!mSelectControl) return nullptr; + + RefPtr<dom::Element> currentItemElm; + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + mSelectControl->AsXULMultiSelectControl(); + if (multiSelectControl) { + multiSelectControl->GetCurrentItem(getter_AddRefs(currentItemElm)); + } else { + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + if (selectControl) { + selectControl->GetSelectedItem(getter_AddRefs(currentItemElm)); + } + } + + if (currentItemElm) { + DocAccessible* document = Document(); + if (document) return document->GetAccessible(currentItemElm); + } + + return nullptr; +} + +void XULSelectControlAccessible::SetCurrentItem(const Accessible* aItem) { + if (!mSelectControl) return; + + nsCOMPtr<dom::Element> itemElm = aItem->Elm(); + nsCOMPtr<nsIDOMXULMultiSelectControlElement> multiSelectControl = + itemElm->AsXULMultiSelectControl(); + if (multiSelectControl) { + multiSelectControl->SetCurrentItem(itemElm); + } else { + nsCOMPtr<nsIDOMXULSelectControlElement> selectControl = + mSelectControl->AsXULSelectControl(); + if (selectControl) { + selectControl->SetSelectedItem(itemElm); + } + } +} diff --git a/accessible/xul/XULSelectControlAccessible.h b/accessible/xul/XULSelectControlAccessible.h new file mode 100644 index 0000000000..607000904b --- /dev/null +++ b/accessible/xul/XULSelectControlAccessible.h @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* 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/. */ + +#ifndef mozilla_a11y_XULSelectControlAccessible_h__ +#define mozilla_a11y_XULSelectControlAccessible_h__ + +#include "AccessibleWrap.h" + +namespace mozilla { +namespace a11y { + +/** + * The basic implementation of accessible selection for XUL select controls. + */ +class XULSelectControlAccessible : public AccessibleWrap { + public: + XULSelectControlAccessible(nsIContent* aContent, DocAccessible* aDoc); + virtual ~XULSelectControlAccessible() {} + + // Accessible + virtual void Shutdown() override; + + // SelectAccessible + virtual void SelectedItems(nsTArray<Accessible*>* aItems) override; + virtual uint32_t SelectedItemCount() override; + virtual Accessible* GetSelectedItem(uint32_t aIndex) override; + virtual bool IsItemSelected(uint32_t aIndex) override; + virtual bool AddItemToSelection(uint32_t aIndex) override; + virtual bool RemoveItemFromSelection(uint32_t aIndex) override; + virtual bool SelectAll() override; + virtual bool UnselectAll() override; + + // Widgets + virtual Accessible* CurrentItem() const override; + virtual void SetCurrentItem(const Accessible* aItem) override; + + protected: + RefPtr<dom::Element> mSelectControl; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULTabAccessible.cpp b/accessible/xul/XULTabAccessible.cpp new file mode 100644 index 0000000000..513aedc302 --- /dev/null +++ b/accessible/xul/XULTabAccessible.cpp @@ -0,0 +1,216 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULTabAccessible.h" + +#include "ARIAMap.h" +#include "nsAccUtils.h" +#include "Relation.h" +#include "Role.h" +#include "States.h" + +// NOTE: alphabetically ordered +#include "mozilla/dom/Document.h" +#include "nsIDOMXULSelectCntrlItemEl.h" +#include "nsIDOMXULRelatedElement.h" +#include "nsXULElement.h" + +#include "mozilla/dom/BindingDeclarations.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULTabAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTabAccessible::XULTabAccessible(nsIContent* aContent, DocAccessible* aDoc) + : HyperTextAccessibleWrap(aContent, aDoc) {} + +//////////////////////////////////////////////////////////////////////////////// +// XULTabAccessible: Accessible + +uint8_t XULTabAccessible::ActionCount() const { return 1; } + +void XULTabAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Switch) aName.AssignLiteral("switch"); +} + +bool XULTabAccessible::DoAction(uint8_t index) const { + if (index == eAction_Switch) { + // XXXbz Could this just FromContent? + RefPtr<nsXULElement> tab = nsXULElement::FromNodeOrNull(mContent); + if (tab) { + tab->Click(mozilla::dom::CallerType::System); + return true; + } + } + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTabAccessible: Accessible + +role XULTabAccessible::NativeRole() const { return roles::PAGETAB; } + +uint64_t XULTabAccessible::NativeState() const { + // Possible states: focused, focusable, unavailable(disabled), offscreen. + + // get focus and disable status from base class + uint64_t state = AccessibleWrap::NativeState(); + + // Check whether the tab is selected and/or pinned + nsCOMPtr<nsIDOMXULSelectControlItemElement> tab = + Elm()->AsXULSelectControlItem(); + if (tab) { + bool selected = false; + if (NS_SUCCEEDED(tab->GetSelected(&selected)) && selected) + state |= states::SELECTED; + + if (mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::pinned, + nsGkAtoms::_true, eCaseMatters)) + state |= states::PINNED; + } + + return state; +} + +uint64_t XULTabAccessible::NativeInteractiveState() const { + uint64_t state = Accessible::NativeInteractiveState(); + return (state & states::UNAVAILABLE) ? state : state | states::SELECTABLE; +} + +Relation XULTabAccessible::RelationByType(RelationType aType) const { + Relation rel = AccessibleWrap::RelationByType(aType); + if (aType != RelationType::LABEL_FOR) return rel; + + // Expose 'LABEL_FOR' relation on tab accessible for tabpanel accessible. + ErrorResult rv; + nsIContent* parent = mContent->AsElement()->Closest("tabs"_ns, rv); + if (!parent) return rel; + + nsCOMPtr<nsIDOMXULRelatedElement> tabsElm = + parent->AsElement()->AsXULRelated(); + if (!tabsElm) return rel; + + RefPtr<mozilla::dom::Element> tabpanelElement; + tabsElm->GetRelatedElement(GetNode(), getter_AddRefs(tabpanelElement)); + if (!tabpanelElement) return rel; + + rel.AppendTarget(mDoc, tabpanelElement); + return rel; +} + +void XULTabAccessible::ApplyARIAState(uint64_t* aState) const { + HyperTextAccessibleWrap::ApplyARIAState(aState); + // XUL tab has an implicit ARIA role of tab, so support aria-selected. + // Don't use aria::MapToState because that will set the SELECTABLE state + // even if the tab is disabled. + if (nsAccUtils::IsARIASelected(this)) { + *aState |= states::SELECTED; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTabsAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTabsAccessible::XULTabsAccessible(nsIContent* aContent, DocAccessible* aDoc) + : XULSelectControlAccessible(aContent, aDoc) {} + +role XULTabsAccessible::NativeRole() const { return roles::PAGETABLIST; } + +uint8_t XULTabsAccessible::ActionCount() const { return 0; } + +void XULTabsAccessible::Value(nsString& aValue) const { aValue.Truncate(); } + +ENameValueFlag XULTabsAccessible::NativeName(nsString& aName) const { + // no name + return eNameOK; +} + +void XULTabsAccessible::ApplyARIAState(uint64_t* aState) const { + XULSelectControlAccessible::ApplyARIAState(aState); + // XUL tabs has an implicit ARIA role of tablist, so support + // aria-multiselectable. + MOZ_ASSERT(Elm()); + aria::MapToState(aria::eARIAMultiSelectable, Elm(), aState); +} + +// XUL tabs is a single selection control and doesn't allow ARIA selection. +// However, if aria-multiselectable is used, it becomes a multiselectable +// control, where both native and ARIA markup are used to set selection. +// Therefore, if aria-multiselectable is set, use the base implementation of +// the selection retrieval methods in order to support ARIA selection. +// We don't bother overriding the selection setting methods because +// current front-end code using XUL tabs doesn't support setting of +// aria-selected by the a11y engine and we still want to be able to set the +// primary selected item according to XUL. + +void XULTabsAccessible::SelectedItems(nsTArray<Accessible*>* aItems) { + if (nsAccUtils::IsARIAMultiSelectable(this)) { + AccessibleWrap::SelectedItems(aItems); + } else { + XULSelectControlAccessible::SelectedItems(aItems); + } +} + +Accessible* XULTabsAccessible::GetSelectedItem(uint32_t aIndex) { + if (nsAccUtils::IsARIAMultiSelectable(this)) { + return AccessibleWrap::GetSelectedItem(aIndex); + } + + return XULSelectControlAccessible::GetSelectedItem(aIndex); +} + +uint32_t XULTabsAccessible::SelectedItemCount() { + if (nsAccUtils::IsARIAMultiSelectable(this)) { + return AccessibleWrap::SelectedItemCount(); + } + + return XULSelectControlAccessible::SelectedItemCount(); +} + +bool XULTabsAccessible::IsItemSelected(uint32_t aIndex) { + if (nsAccUtils::IsARIAMultiSelectable(this)) { + return AccessibleWrap::IsItemSelected(aIndex); + } + + return XULSelectControlAccessible::IsItemSelected(aIndex); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTabpanelsAccessible +//////////////////////////////////////////////////////////////////////////////// + +role XULTabpanelsAccessible::NativeRole() const { return roles::PANE; } + +//////////////////////////////////////////////////////////////////////////////// +// XULTabpanelAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTabpanelAccessible::XULTabpanelAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) {} + +role XULTabpanelAccessible::NativeRole() const { return roles::PROPERTYPAGE; } + +Relation XULTabpanelAccessible::RelationByType(RelationType aType) const { + Relation rel = AccessibleWrap::RelationByType(aType); + if (aType != RelationType::LABELLED_BY) return rel; + + // Expose 'LABELLED_BY' relation on tabpanel accessible for tab accessible. + if (!mContent->GetParent()) return rel; + + nsCOMPtr<nsIDOMXULRelatedElement> tabpanelsElm = + mContent->GetParent()->AsElement()->AsXULRelated(); + if (!tabpanelsElm) return rel; + + RefPtr<mozilla::dom::Element> tabElement; + tabpanelsElm->GetRelatedElement(GetNode(), getter_AddRefs(tabElement)); + if (!tabElement) return rel; + + rel.AppendTarget(mDoc, tabElement); + return rel; +} diff --git a/accessible/xul/XULTabAccessible.h b/accessible/xul/XULTabAccessible.h new file mode 100644 index 0000000000..9f0e9cf01e --- /dev/null +++ b/accessible/xul/XULTabAccessible.h @@ -0,0 +1,98 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULTabAccessible_h__ +#define mozilla_a11y_XULTabAccessible_h__ + +// NOTE: alphabetically ordered +#include "HyperTextAccessibleWrap.h" +#include "XULMenuAccessible.h" +#include "XULSelectControlAccessible.h" + +namespace mozilla { +namespace a11y { + +/** + * An individual tab, xul:tab element. + */ +class XULTabAccessible : public HyperTextAccessibleWrap { + public: + enum { eAction_Switch = 0 }; + + XULTabAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + virtual Relation RelationByType(RelationType aType) const override; + virtual void ApplyARIAState(uint64_t* aState) const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; +}; + +/** + * A container of tab objects, xul:tabs element. + */ +class XULTabsAccessible : public XULSelectControlAccessible { + public: + XULTabsAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual void Value(nsString& aValue) const override; + virtual a11y::role NativeRole() const override; + virtual void ApplyARIAState(uint64_t* aState) const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + + // SelectAccessible + virtual void SelectedItems(nsTArray<Accessible*>* aItems) override; + virtual uint32_t SelectedItemCount() override; + virtual Accessible* GetSelectedItem(uint32_t aIndex) override; + virtual bool IsItemSelected(uint32_t aIndex) override; + + protected: + // Accessible + virtual ENameValueFlag NativeName(nsString& aName) const override; +}; + +/** + * A container of tab panels, xul:tabpanels element. + */ +class XULTabpanelsAccessible : public AccessibleWrap { + public: + XULTabpanelsAccessible(nsIContent* aContent, DocAccessible* aDoc) + : AccessibleWrap(aContent, aDoc) { + mType = eXULTabpanelsType; + } + + // Accessible + virtual a11y::role NativeRole() const override; +}; + +/** + * A tabpanel object, child elements of xul:tabpanels element. + * + * XXX: we need to move the class logic into generic class since + * for example we do not create instance of this class for XUL textbox used as + * a tabpanel. + */ +class XULTabpanelAccessible : public AccessibleWrap { + public: + XULTabpanelAccessible(nsIContent* aContent, DocAccessible* aDoc); + + // Accessible + virtual a11y::role NativeRole() const override; + virtual Relation RelationByType(RelationType aType) const override; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULTreeAccessible.cpp b/accessible/xul/XULTreeAccessible.cpp new file mode 100644 index 0000000000..8b69622394 --- /dev/null +++ b/accessible/xul/XULTreeAccessible.cpp @@ -0,0 +1,997 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* 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/. */ + +#include "XULTreeAccessible.h" + +#include "Accessible-inl.h" +#include "DocAccessible-inl.h" +#include "nsAccCache.h" +#include "nsAccUtils.h" +#include "nsCoreUtils.h" +#include "nsEventShell.h" +#include "DocAccessible.h" +#include "Relation.h" +#include "Role.h" +#include "States.h" +#include "XULTreeGridAccessible.h" +#include "nsQueryObject.h" + +#include "nsComponentManagerUtils.h" +#include "nsIAutoCompletePopup.h" +#include "nsIDOMXULMenuListElement.h" +#include "nsITreeSelection.h" +#include "nsTreeBodyFrame.h" +#include "nsTreeColumns.h" +#include "nsTreeUtils.h" +#include "mozilla/PresShell.h" +#include "mozilla/dom/XULTreeElementBinding.h" + +using namespace mozilla::a11y; + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTreeAccessible::XULTreeAccessible(nsIContent* aContent, DocAccessible* aDoc, + nsTreeBodyFrame* aTreeFrame) + : AccessibleWrap(aContent, aDoc), + mAccessibleCache(kDefaultTreeCacheLength) { + mType = eXULTreeType; + mGenericTypes |= eSelect; + + nsCOMPtr<nsITreeView> view = aTreeFrame->GetExistingView(); + mTreeView = view; + + mTree = nsCoreUtils::GetTree(aContent); + NS_ASSERTION(mTree, "Can't get mTree!\n"); + + nsIContent* parentContent = mContent->GetParent(); + if (parentContent) { + nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm = + do_QueryInterface(parentContent); + if (autoCompletePopupElm) mGenericTypes |= eAutoCompletePopup; + } +} + +XULTreeAccessible::~XULTreeAccessible() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: nsISupports and cycle collection implementation + +NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeAccessible, Accessible, mTree, + mAccessibleCache) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XULTreeAccessible) +NS_INTERFACE_MAP_END_INHERITING(Accessible) + +NS_IMPL_ADDREF_INHERITED(XULTreeAccessible, Accessible) +NS_IMPL_RELEASE_INHERITED(XULTreeAccessible, Accessible) + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: Accessible implementation + +uint64_t XULTreeAccessible::NativeState() const { + // Get focus status from base class. + uint64_t state = Accessible::NativeState(); + + // readonly state + state |= states::READONLY; + + // multiselectable state. + if (!mTreeView) return state; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + NS_ENSURE_TRUE(selection, state); + + bool isSingle = false; + nsresult rv = selection->GetSingle(&isSingle); + NS_ENSURE_SUCCESS(rv, state); + + if (!isSingle) state |= states::MULTISELECTABLE; + + return state; +} + +void XULTreeAccessible::Value(nsString& aValue) const { + aValue.Truncate(); + if (!mTreeView) return; + + // Return the value is the first selected child. + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (!selection) return; + + int32_t currentIndex; + selection->GetCurrentIndex(¤tIndex); + if (currentIndex >= 0) { + RefPtr<nsTreeColumn> keyCol; + + RefPtr<nsTreeColumns> cols = mTree->GetColumns(); + if (cols) keyCol = cols->GetKeyColumn(); + + mTreeView->GetCellText(currentIndex, keyCol, aValue); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: Accessible implementation + +void XULTreeAccessible::Shutdown() { + if (mDoc && !mDoc->IsDefunct()) { + UnbindCacheEntriesFromDocument(mAccessibleCache); + } + + mTree = nullptr; + mTreeView = nullptr; + + AccessibleWrap::Shutdown(); +} + +role XULTreeAccessible::NativeRole() const { + // No primary column means we're in a list. In fact, history and mail turn off + // the primary flag when switching to a flat view. + + nsIContent* child = + nsTreeUtils::GetDescendantChild(mContent, nsGkAtoms::treechildren); + NS_ASSERTION(child, "tree without treechildren!"); + nsTreeBodyFrame* treeFrame = do_QueryFrame(child->GetPrimaryFrame()); + NS_ASSERTION(treeFrame, "xul tree accessible for tree without a frame!"); + if (!treeFrame) return roles::LIST; + + RefPtr<nsTreeColumns> cols = treeFrame->Columns(); + nsTreeColumn* primaryCol = cols->GetPrimaryColumn(); + + return primaryCol ? roles::OUTLINE : roles::LIST; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: Accessible implementation (DON'T put methods here) + +Accessible* XULTreeAccessible::ChildAtPoint(int32_t aX, int32_t aY, + EWhichChildAtPoint aWhichChild) { + nsIFrame* frame = GetFrame(); + if (!frame) return nullptr; + + nsPresContext* presContext = frame->PresContext(); + PresShell* presShell = presContext->PresShell(); + + nsIFrame* rootFrame = presShell->GetRootFrame(); + NS_ENSURE_TRUE(rootFrame, nullptr); + + CSSIntRect rootRect = rootFrame->GetScreenRect(); + + int32_t clientX = presContext->DevPixelsToIntCSSPixels(aX) - rootRect.X(); + int32_t clientY = presContext->DevPixelsToIntCSSPixels(aY) - rootRect.Y(); + + ErrorResult rv; + dom::TreeCellInfo cellInfo; + mTree->GetCellAt(clientX, clientY, cellInfo, rv); + + // If we failed to find tree cell for the given point then it might be + // tree columns. + if (cellInfo.mRow == -1 || !cellInfo.mCol) + return AccessibleWrap::ChildAtPoint(aX, aY, aWhichChild); + + Accessible* child = GetTreeItemAccessible(cellInfo.mRow); + if (aWhichChild == eDeepestChild && child) { + // Look for accessible cell for the found item accessible. + RefPtr<XULTreeItemAccessibleBase> treeitem = do_QueryObject(child); + + Accessible* cell = treeitem->GetCellAccessible(cellInfo.mCol); + if (cell) child = cell; + } + + return child; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: SelectAccessible + +Accessible* XULTreeAccessible::CurrentItem() const { + if (!mTreeView) return nullptr; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + int32_t currentIndex = -1; + selection->GetCurrentIndex(¤tIndex); + if (currentIndex >= 0) return GetTreeItemAccessible(currentIndex); + } + + return nullptr; +} + +void XULTreeAccessible::SetCurrentItem(const Accessible* aItem) { + NS_ERROR("XULTreeAccessible::SetCurrentItem not implemented"); +} + +void XULTreeAccessible::SelectedItems(nsTArray<Accessible*>* aItems) { + if (!mTreeView) return; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (!selection) return; + + int32_t rangeCount = 0; + selection->GetRangeCount(&rangeCount); + for (int32_t rangeIdx = 0; rangeIdx < rangeCount; rangeIdx++) { + int32_t firstIdx = 0, lastIdx = -1; + selection->GetRangeAt(rangeIdx, &firstIdx, &lastIdx); + for (int32_t rowIdx = firstIdx; rowIdx <= lastIdx; rowIdx++) { + Accessible* item = GetTreeItemAccessible(rowIdx); + if (item) aItems->AppendElement(item); + } + } +} + +uint32_t XULTreeAccessible::SelectedItemCount() { + if (!mTreeView) return 0; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + int32_t count = 0; + selection->GetCount(&count); + return count; + } + + return 0; +} + +bool XULTreeAccessible::AddItemToSelection(uint32_t aIndex) { + if (!mTreeView) return false; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + bool isSelected = false; + selection->IsSelected(aIndex, &isSelected); + if (!isSelected) selection->ToggleSelect(aIndex); + + return true; + } + return false; +} + +bool XULTreeAccessible::RemoveItemFromSelection(uint32_t aIndex) { + if (!mTreeView) return false; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + bool isSelected = false; + selection->IsSelected(aIndex, &isSelected); + if (isSelected) selection->ToggleSelect(aIndex); + + return true; + } + return false; +} + +bool XULTreeAccessible::IsItemSelected(uint32_t aIndex) { + if (!mTreeView) return false; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + bool isSelected = false; + selection->IsSelected(aIndex, &isSelected); + return isSelected; + } + return false; +} + +bool XULTreeAccessible::UnselectAll() { + if (!mTreeView) return false; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (!selection) return false; + + selection->ClearSelection(); + return true; +} + +Accessible* XULTreeAccessible::GetSelectedItem(uint32_t aIndex) { + if (!mTreeView) return nullptr; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (!selection) return nullptr; + + uint32_t selCount = 0; + int32_t rangeCount = 0; + selection->GetRangeCount(&rangeCount); + for (int32_t rangeIdx = 0; rangeIdx < rangeCount; rangeIdx++) { + int32_t firstIdx = 0, lastIdx = -1; + selection->GetRangeAt(rangeIdx, &firstIdx, &lastIdx); + for (int32_t rowIdx = firstIdx; rowIdx <= lastIdx; rowIdx++) { + if (selCount == aIndex) return GetTreeItemAccessible(rowIdx); + + selCount++; + } + } + + return nullptr; +} + +bool XULTreeAccessible::SelectAll() { + // see if we are multiple select if so set ourselves as such + if (!mTreeView) return false; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + bool single = false; + selection->GetSingle(&single); + if (!single) { + selection->SelectAll(); + return true; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: Accessible implementation + +Accessible* XULTreeAccessible::GetChildAt(uint32_t aIndex) const { + uint32_t childCount = Accessible::ChildCount(); + if (aIndex < childCount) return Accessible::GetChildAt(aIndex); + + return GetTreeItemAccessible(aIndex - childCount); +} + +uint32_t XULTreeAccessible::ChildCount() const { + // Tree's children count is row count + treecols count. + uint32_t childCount = Accessible::ChildCount(); + if (!mTreeView) return childCount; + + int32_t rowCount = 0; + mTreeView->GetRowCount(&rowCount); + childCount += rowCount; + + return childCount; +} + +Relation XULTreeAccessible::RelationByType(RelationType aType) const { + if (aType == RelationType::NODE_PARENT_OF) { + if (mTreeView) + return Relation(new XULTreeItemIterator(this, mTreeView, -1)); + + return Relation(); + } + + return Accessible::RelationByType(aType); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: Widgets + +bool XULTreeAccessible::IsWidget() const { return true; } + +bool XULTreeAccessible::IsActiveWidget() const { + if (IsAutoCompletePopup()) { + nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm = + do_QueryInterface(mContent->GetParent()); + + if (autoCompletePopupElm) { + bool isOpen = false; + autoCompletePopupElm->GetPopupOpen(&isOpen); + return isOpen; + } + } + return FocusMgr()->HasDOMFocus(mContent); +} + +bool XULTreeAccessible::AreItemsOperable() const { + if (IsAutoCompletePopup()) { + nsCOMPtr<nsIAutoCompletePopup> autoCompletePopupElm = + do_QueryInterface(mContent->GetParent()); + + if (autoCompletePopupElm) { + bool isOpen = false; + autoCompletePopupElm->GetPopupOpen(&isOpen); + return isOpen; + } + } + return true; +} + +Accessible* XULTreeAccessible::ContainerWidget() const { + if (IsAutoCompletePopup() && mContent->GetParent()) { + // This works for XUL autocompletes. It doesn't work for HTML forms + // autocomplete because of potential crossprocess calls (when autocomplete + // lives in content process while popup lives in chrome process). If that's + // a problem then rethink Widgets interface. + nsCOMPtr<nsIDOMXULMenuListElement> menuListElm = + mContent->GetParent()->AsElement()->AsXULMenuList(); + if (menuListElm) { + RefPtr<mozilla::dom::Element> inputElm; + menuListElm->GetInputField(getter_AddRefs(inputElm)); + if (inputElm) { + Accessible* input = mDoc->GetAccessible(inputElm); + return input ? input->ContainerWidget() : nullptr; + } + } + } + return nullptr; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: public implementation + +Accessible* XULTreeAccessible::GetTreeItemAccessible(int32_t aRow) const { + if (aRow < 0 || IsDefunct() || !mTreeView) return nullptr; + + int32_t rowCount = 0; + nsresult rv = mTreeView->GetRowCount(&rowCount); + if (NS_FAILED(rv) || aRow >= rowCount) return nullptr; + + void* key = reinterpret_cast<void*>(intptr_t(aRow)); + Accessible* cachedTreeItem = mAccessibleCache.GetWeak(key); + if (cachedTreeItem) return cachedTreeItem; + + RefPtr<Accessible> treeItem = CreateTreeItemAccessible(aRow); + if (treeItem) { + mAccessibleCache.Put(key, RefPtr{treeItem}); + Document()->BindToDocument(treeItem, nullptr); + return treeItem; + } + + return nullptr; +} + +void XULTreeAccessible::InvalidateCache(int32_t aRow, int32_t aCount) { + if (IsDefunct()) return; + + if (!mTreeView) { + UnbindCacheEntriesFromDocument(mAccessibleCache); + return; + } + + // Do not invalidate the cache if rows have been inserted. + if (aCount > 0) return; + + DocAccessible* document = Document(); + + // Fire destroy event for removed tree items and delete them from caches. + for (int32_t rowIdx = aRow; rowIdx < aRow - aCount; rowIdx++) { + void* key = reinterpret_cast<void*>(intptr_t(rowIdx)); + Accessible* treeItem = mAccessibleCache.GetWeak(key); + + if (treeItem) { + RefPtr<AccEvent> event = + new AccEvent(nsIAccessibleEvent::EVENT_HIDE, treeItem); + nsEventShell::FireEvent(event); + + // Unbind from document, shutdown and remove from tree cache. + document->UnbindFromDocument(treeItem); + mAccessibleCache.Remove(key); + } + } + + // We dealt with removed tree items already however we may keep tree items + // having row indexes greater than row count. We should remove these dead tree + // items silently from caches. + int32_t newRowCount = 0; + nsresult rv = mTreeView->GetRowCount(&newRowCount); + if (NS_FAILED(rv)) return; + + int32_t oldRowCount = newRowCount - aCount; + + for (int32_t rowIdx = newRowCount; rowIdx < oldRowCount; ++rowIdx) { + void* key = reinterpret_cast<void*>(intptr_t(rowIdx)); + Accessible* treeItem = mAccessibleCache.GetWeak(key); + + if (treeItem) { + // Unbind from document, shutdown and remove from tree cache. + document->UnbindFromDocument(treeItem); + mAccessibleCache.Remove(key); + } + } +} + +void XULTreeAccessible::TreeViewInvalidated(int32_t aStartRow, int32_t aEndRow, + int32_t aStartCol, + int32_t aEndCol) { + if (IsDefunct()) return; + + if (!mTreeView) { + UnbindCacheEntriesFromDocument(mAccessibleCache); + return; + } + + int32_t endRow = aEndRow; + + nsresult rv; + if (endRow == -1) { + int32_t rowCount = 0; + rv = mTreeView->GetRowCount(&rowCount); + if (NS_FAILED(rv)) return; + + endRow = rowCount - 1; + } + + RefPtr<nsTreeColumns> treeColumns = mTree->GetColumns(); + if (!treeColumns) return; + + int32_t endCol = aEndCol; + + if (endCol == -1) { + // We need to make sure to cast to int32_t before we do the subtraction, in + // case the column count is 0. + endCol = static_cast<int32_t>(treeColumns->Count()) - 1; + } + + for (int32_t rowIdx = aStartRow; rowIdx <= endRow; ++rowIdx) { + void* key = reinterpret_cast<void*>(intptr_t(rowIdx)); + Accessible* accessible = mAccessibleCache.GetWeak(key); + + if (accessible) { + RefPtr<XULTreeItemAccessibleBase> treeitemAcc = + do_QueryObject(accessible); + NS_ASSERTION(treeitemAcc, "Wrong accessible at the given key!"); + + treeitemAcc->RowInvalidated(aStartCol, endCol); + } + } +} + +void XULTreeAccessible::TreeViewChanged(nsITreeView* aView) { + if (IsDefunct()) return; + + // Fire reorder event on tree accessible on accessible tree (do not fire + // show/hide events on tree items because it can be expensive to fire them for + // each tree item. + RefPtr<AccReorderEvent> reorderEvent = new AccReorderEvent(this); + Document()->FireDelayedEvent(reorderEvent); + + // Clear cache. + UnbindCacheEntriesFromDocument(mAccessibleCache); + + mTreeView = aView; + Accessible* item = CurrentItem(); + if (item) { + FocusMgr()->ActiveItemChanged(item, true); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeAccessible: protected implementation + +already_AddRefed<Accessible> XULTreeAccessible::CreateTreeItemAccessible( + int32_t aRow) const { + RefPtr<Accessible> accessible = new XULTreeItemAccessible( + mContent, mDoc, const_cast<XULTreeAccessible*>(this), mTree, mTreeView, + aRow); + + return accessible.forget(); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessibleBase +//////////////////////////////////////////////////////////////////////////////// + +XULTreeItemAccessibleBase::XULTreeItemAccessibleBase( + nsIContent* aContent, DocAccessible* aDoc, Accessible* aParent, + dom::XULTreeElement* aTree, nsITreeView* aTreeView, int32_t aRow) + : AccessibleWrap(aContent, aDoc), + mTree(aTree), + mTreeView(aTreeView), + mRow(aRow) { + mParent = aParent; + mStateFlags |= eSharedNode; +} + +XULTreeItemAccessibleBase::~XULTreeItemAccessibleBase() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessibleBase: nsISupports implementation + +NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeItemAccessibleBase, Accessible, mTree) + +NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(XULTreeItemAccessibleBase, + Accessible, + XULTreeItemAccessibleBase) + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessibleBase: Accessible + +Accessible* XULTreeItemAccessibleBase::FocusedChild() { + return FocusMgr()->FocusedAccessible() == this ? this : nullptr; +} + +nsIntRect XULTreeItemAccessibleBase::BoundsInCSSPixels() const { + // Get x coordinate and width from treechildren element, get y coordinate and + // height from tree cell. + + RefPtr<nsTreeColumn> column = nsCoreUtils::GetFirstSensibleColumn(mTree); + + nsresult rv; + nsIntRect rect = mTree->GetCoordsForCellItem(mRow, column, u"cell"_ns, rv); + if (NS_FAILED(rv)) { + return nsIntRect(); + } + + RefPtr<dom::Element> bodyElement = mTree->GetTreeBody(); + if (!bodyElement || !bodyElement->IsXULElement()) { + return nsIntRect(); + } + + rect.width = bodyElement->ClientWidth(); + + nsIFrame* bodyFrame = bodyElement->GetPrimaryFrame(); + if (!bodyFrame) { + return nsIntRect(); + } + + CSSIntRect screenRect = bodyFrame->GetScreenRect(); + rect.x = screenRect.x; + rect.y += screenRect.y; + return rect; +} + +nsRect XULTreeItemAccessibleBase::BoundsInAppUnits() const { + nsIntRect bounds = BoundsInCSSPixels(); + nsPresContext* presContext = mDoc->PresContext(); + return nsRect(presContext->CSSPixelsToAppUnits(bounds.X()), + presContext->CSSPixelsToAppUnits(bounds.Y()), + presContext->CSSPixelsToAppUnits(bounds.Width()), + presContext->CSSPixelsToAppUnits(bounds.Height())); +} + +void XULTreeItemAccessibleBase::SetSelected(bool aSelect) { + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + bool isSelected = false; + selection->IsSelected(mRow, &isSelected); + if (isSelected != aSelect) selection->ToggleSelect(mRow); + } +} + +void XULTreeItemAccessibleBase::TakeFocus() const { + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) selection->SetCurrentIndex(mRow); + + // focus event will be fired here + Accessible::TakeFocus(); +} + +Relation XULTreeItemAccessibleBase::RelationByType(RelationType aType) const { + switch (aType) { + case RelationType::NODE_CHILD_OF: { + int32_t parentIndex = -1; + if (!NS_SUCCEEDED(mTreeView->GetParentIndex(mRow, &parentIndex))) + return Relation(); + + if (parentIndex == -1) return Relation(mParent); + + XULTreeAccessible* treeAcc = mParent->AsXULTree(); + return Relation(treeAcc->GetTreeItemAccessible(parentIndex)); + } + + case RelationType::NODE_PARENT_OF: { + bool isTrue = false; + if (NS_FAILED(mTreeView->IsContainerEmpty(mRow, &isTrue)) || isTrue) + return Relation(); + + if (NS_FAILED(mTreeView->IsContainerOpen(mRow, &isTrue)) || !isTrue) + return Relation(); + + XULTreeAccessible* tree = mParent->AsXULTree(); + return Relation(new XULTreeItemIterator(tree, mTreeView, mRow)); + } + + default: + return Relation(); + } +} + +uint8_t XULTreeItemAccessibleBase::ActionCount() const { + // "activate" action is available for all treeitems, "expand/collapse" action + // is avaible for treeitem which is container. + return IsExpandable() ? 2 : 1; +} + +void XULTreeItemAccessibleBase::ActionNameAt(uint8_t aIndex, nsAString& aName) { + if (aIndex == eAction_Click) { + aName.AssignLiteral("activate"); + return; + } + + if (aIndex == eAction_Expand && IsExpandable()) { + bool isContainerOpen = false; + mTreeView->IsContainerOpen(mRow, &isContainerOpen); + if (isContainerOpen) + aName.AssignLiteral("collapse"); + else + aName.AssignLiteral("expand"); + } +} + +bool XULTreeItemAccessibleBase::DoAction(uint8_t aIndex) const { + if (aIndex != eAction_Click && (aIndex != eAction_Expand || !IsExpandable())) + return false; + + DoCommand(nullptr, aIndex); + return true; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessibleBase: Accessible implementation + +void XULTreeItemAccessibleBase::Shutdown() { + mTree = nullptr; + mTreeView = nullptr; + mRow = -1; + mParent = nullptr; // null-out to prevent base class's shutdown ops + + AccessibleWrap::Shutdown(); +} + +GroupPos XULTreeItemAccessibleBase::GroupPosition() { + GroupPos groupPos; + + int32_t level; + nsresult rv = mTreeView->GetLevel(mRow, &level); + NS_ENSURE_SUCCESS(rv, groupPos); + + int32_t topCount = 1; + for (int32_t index = mRow - 1; index >= 0; index--) { + int32_t lvl = -1; + if (NS_SUCCEEDED(mTreeView->GetLevel(index, &lvl))) { + if (lvl < level) break; + + if (lvl == level) topCount++; + } + } + + int32_t rowCount = 0; + rv = mTreeView->GetRowCount(&rowCount); + NS_ENSURE_SUCCESS(rv, groupPos); + + int32_t bottomCount = 0; + for (int32_t index = mRow + 1; index < rowCount; index++) { + int32_t lvl = -1; + if (NS_SUCCEEDED(mTreeView->GetLevel(index, &lvl))) { + if (lvl < level) break; + + if (lvl == level) bottomCount++; + } + } + + groupPos.level = level + 1; + groupPos.setSize = topCount + bottomCount; + groupPos.posInSet = topCount; + + return groupPos; +} + +uint64_t XULTreeItemAccessibleBase::NativeState() const { + // focusable and selectable states + uint64_t state = NativeInteractiveState(); + + // expanded/collapsed state + if (IsExpandable()) { + bool isContainerOpen; + mTreeView->IsContainerOpen(mRow, &isContainerOpen); + state |= isContainerOpen ? states::EXPANDED : states::COLLAPSED; + } + + // selected state + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + bool isSelected; + selection->IsSelected(mRow, &isSelected); + if (isSelected) state |= states::SELECTED; + } + + // focused state + if (FocusMgr()->IsFocused(this)) state |= states::FOCUSED; + + // invisible state + int32_t firstVisibleRow = mTree->GetFirstVisibleRow(); + int32_t lastVisibleRow = mTree->GetLastVisibleRow(); + if (mRow < firstVisibleRow || mRow > lastVisibleRow) + state |= states::INVISIBLE; + + return state; +} + +uint64_t XULTreeItemAccessibleBase::NativeInteractiveState() const { + return states::FOCUSABLE | states::SELECTABLE; +} + +int32_t XULTreeItemAccessibleBase::IndexInParent() const { + return mParent ? mParent->ContentChildCount() + mRow : -1; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessibleBase: Widgets + +Accessible* XULTreeItemAccessibleBase::ContainerWidget() const { + return mParent; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessibleBase: Accessible protected methods + +void XULTreeItemAccessibleBase::DispatchClickEvent( + nsIContent* aContent, uint32_t aActionIndex) const { + if (IsDefunct()) return; + + RefPtr<nsTreeColumns> columns = mTree->GetColumns(); + if (!columns) return; + + // Get column and pseudo element. + RefPtr<nsTreeColumn> column; + nsAutoString pseudoElm; + + if (aActionIndex == eAction_Click) { + // Key column is visible and clickable. + column = columns->GetKeyColumn(); + } else { + // Primary column contains a twisty we should click on. + column = columns->GetPrimaryColumn(); + pseudoElm = u"twisty"_ns; + } + + if (column) { + RefPtr<dom::XULTreeElement> tree = mTree; + nsCoreUtils::DispatchClickEvent(tree, mRow, column, pseudoElm); + } +} + +Accessible* XULTreeItemAccessibleBase::GetSiblingAtOffset( + int32_t aOffset, nsresult* aError) const { + if (aError) *aError = NS_OK; // fail peacefully + + return mParent->GetChildAt(IndexInParent() + aOffset); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessibleBase: protected implementation + +bool XULTreeItemAccessibleBase::IsExpandable() const { + bool isContainer = false; + mTreeView->IsContainer(mRow, &isContainer); + if (isContainer) { + bool isEmpty = false; + mTreeView->IsContainerEmpty(mRow, &isEmpty); + if (!isEmpty) { + RefPtr<nsTreeColumns> columns = mTree->GetColumns(); + if (columns) { + nsTreeColumn* primaryColumn = columns->GetPrimaryColumn(); + if (primaryColumn && !nsCoreUtils::IsColumnHidden(primaryColumn)) + return true; + } + } + } + + return false; +} + +void XULTreeItemAccessibleBase::GetCellName(nsTreeColumn* aColumn, + nsAString& aName) const { + mTreeView->GetCellText(mRow, aColumn, aName); + + // If there is still no name try the cell value: + // This is for graphical cells. We need tree/table view implementors to + // implement FooView::GetCellValue to return a meaningful string for cases + // where there is something shown in the cell (non-text) such as a star icon; + // in which case GetCellValue for that cell would return "starred" or + // "flagged" for example. + if (aName.IsEmpty()) mTreeView->GetCellValue(mRow, aColumn, aName); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTreeItemAccessible::XULTreeItemAccessible( + nsIContent* aContent, DocAccessible* aDoc, Accessible* aParent, + dom::XULTreeElement* aTree, nsITreeView* aTreeView, int32_t aRow) + : XULTreeItemAccessibleBase(aContent, aDoc, aParent, aTree, aTreeView, + aRow) { + mStateFlags |= eNoKidsFromDOM; + mColumn = nsCoreUtils::GetFirstSensibleColumn(mTree, FlushType::None); + GetCellName(mColumn, mCachedName); +} + +XULTreeItemAccessible::~XULTreeItemAccessible() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessible: nsISupports implementation + +NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeItemAccessible, + XULTreeItemAccessibleBase, mColumn) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XULTreeItemAccessible) +NS_INTERFACE_MAP_END_INHERITING(XULTreeItemAccessibleBase) +NS_IMPL_ADDREF_INHERITED(XULTreeItemAccessible, XULTreeItemAccessibleBase) +NS_IMPL_RELEASE_INHERITED(XULTreeItemAccessible, XULTreeItemAccessibleBase) + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessible: nsIAccessible implementation + +ENameValueFlag XULTreeItemAccessible::Name(nsString& aName) const { + aName.Truncate(); + + GetCellName(mColumn, aName); + return eNameOK; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessible: Accessible implementation + +void XULTreeItemAccessible::Shutdown() { + mColumn = nullptr; + XULTreeItemAccessibleBase::Shutdown(); +} + +role XULTreeItemAccessible::NativeRole() const { + RefPtr<nsTreeColumns> columns = mTree->GetColumns(); + if (!columns) { + NS_ERROR("No tree columns object in the tree!"); + return roles::NOTHING; + } + + nsTreeColumn* primaryColumn = columns->GetPrimaryColumn(); + + return primaryColumn ? roles::OUTLINEITEM : roles::LISTITEM; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeItemAccessible: XULTreeItemAccessibleBase implementation + +void XULTreeItemAccessible::RowInvalidated(int32_t aStartColIdx, + int32_t aEndColIdx) { + nsAutoString name; + Name(name); + + if (name != mCachedName) { + nsEventShell::FireEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, this); + mCachedName = name; + } +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeColumAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTreeColumAccessible::XULTreeColumAccessible(nsIContent* aContent, + DocAccessible* aDoc) + : XULColumAccessible(aContent, aDoc) {} + +Accessible* XULTreeColumAccessible::GetSiblingAtOffset(int32_t aOffset, + nsresult* aError) const { + if (aOffset < 0) + return XULColumAccessible::GetSiblingAtOffset(aOffset, aError); + + if (aError) *aError = NS_OK; // fail peacefully + + RefPtr<dom::XULTreeElement> tree = nsCoreUtils::GetTree(mContent); + if (tree) { + nsCOMPtr<nsITreeView> treeView = tree->GetView(FlushType::None); + if (treeView) { + int32_t rowCount = 0; + treeView->GetRowCount(&rowCount); + if (rowCount > 0 && aOffset <= rowCount) { + XULTreeAccessible* treeAcc = Parent()->AsXULTree(); + + if (treeAcc) return treeAcc->GetTreeItemAccessible(aOffset - 1); + } + } + } + + return nullptr; +} diff --git a/accessible/xul/XULTreeAccessible.h b/accessible/xul/XULTreeAccessible.h new file mode 100644 index 0000000000..e0e3990933 --- /dev/null +++ b/accessible/xul/XULTreeAccessible.h @@ -0,0 +1,274 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULTreeAccessible_h__ +#define mozilla_a11y_XULTreeAccessible_h__ + +#include "nsITreeView.h" +#include "XULListboxAccessible.h" +#include "mozilla/dom/XULTreeElement.h" + +class nsTreeBodyFrame; +class nsTreeColumn; + +namespace mozilla { +namespace a11y { + +class XULTreeGridCellAccessible; + +/* + * A class the represents the XUL Tree widget. + */ +const uint32_t kMaxTreeColumns = 100; +const uint32_t kDefaultTreeCacheLength = 128; + +/** + * Accessible class for XUL tree element. + */ + +class XULTreeAccessible : public AccessibleWrap { + public: + XULTreeAccessible(nsIContent* aContent, DocAccessible* aDoc, + nsTreeBodyFrame* aTreeframe); + + // nsISupports and cycle collection + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(XULTreeAccessible, Accessible) + + // Accessible + virtual void Shutdown() override; + virtual void Value(nsString& aValue) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual Accessible* ChildAtPoint(int32_t aX, int32_t aY, + EWhichChildAtPoint aWhichChild) override; + + virtual Accessible* GetChildAt(uint32_t aIndex) const override; + virtual uint32_t ChildCount() const override; + virtual Relation RelationByType(RelationType aType) const override; + + // SelectAccessible + virtual void SelectedItems(nsTArray<Accessible*>* aItems) override; + virtual uint32_t SelectedItemCount() override; + virtual Accessible* GetSelectedItem(uint32_t aIndex) override; + virtual bool IsItemSelected(uint32_t aIndex) override; + virtual bool AddItemToSelection(uint32_t aIndex) override; + virtual bool RemoveItemFromSelection(uint32_t aIndex) override; + virtual bool SelectAll() override; + virtual bool UnselectAll() override; + + // Widgets + virtual bool IsWidget() const override; + virtual bool IsActiveWidget() const override; + virtual bool AreItemsOperable() const override; + virtual Accessible* CurrentItem() const override; + virtual void SetCurrentItem(const Accessible* aItem) override; + + virtual Accessible* ContainerWidget() const override; + + // XULTreeAccessible + + /** + * Return tree item accessible at the givem row. If accessible doesn't exist + * in the cache then create and cache it. + * + * @param aRow [in] the given row index + */ + Accessible* GetTreeItemAccessible(int32_t aRow) const; + + /** + * Invalidates the number of cached treeitem accessibles. + * + * @param aRow [in] row index the invalidation starts from + * @param aCount [in] the number of treeitem accessibles to invalidate, + * the number sign specifies whether rows have been + * inserted (plus) or removed (minus) + */ + void InvalidateCache(int32_t aRow, int32_t aCount); + + /** + * Fires name change events for invalidated area of tree. + * + * @param aStartRow [in] row index invalidation starts from + * @param aEndRow [in] row index invalidation ends, -1 means last row index + * @param aStartCol [in] column index invalidation starts from + * @param aEndCol [in] column index invalidation ends, -1 mens last column + * index + */ + void TreeViewInvalidated(int32_t aStartRow, int32_t aEndRow, + int32_t aStartCol, int32_t aEndCol); + + /** + * Invalidates children created for previous tree view. + */ + void TreeViewChanged(nsITreeView* aView); + + protected: + virtual ~XULTreeAccessible(); + + /** + * Creates tree item accessible for the given row index. + */ + virtual already_AddRefed<Accessible> CreateTreeItemAccessible( + int32_t aRow) const; + + RefPtr<dom::XULTreeElement> mTree; + nsITreeView* mTreeView; + mutable AccessibleHashtable mAccessibleCache; +}; + +/** + * Base class for tree item accessibles. + */ + +#define XULTREEITEMBASEACCESSIBLE_IMPL_CID \ + { /* 1ab79ae7-766a-443c-940b-b1e6b0831dfc */ \ + 0x1ab79ae7, 0x766a, 0x443c, { \ + 0x94, 0x0b, 0xb1, 0xe6, 0xb0, 0x83, 0x1d, 0xfc \ + } \ + } + +class XULTreeItemAccessibleBase : public AccessibleWrap { + public: + XULTreeItemAccessibleBase(nsIContent* aContent, DocAccessible* aDoc, + Accessible* aParent, dom::XULTreeElement* aTree, + nsITreeView* aTreeView, int32_t aRow); + + // nsISupports and cycle collection + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(XULTreeItemAccessibleBase, + AccessibleWrap) + + // Accessible + virtual void Shutdown() override; + virtual nsRect BoundsInAppUnits() const override; + MOZ_CAN_RUN_SCRIPT_BOUNDARY + virtual nsIntRect BoundsInCSSPixels() const override; + virtual GroupPos GroupPosition() override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + virtual int32_t IndexInParent() const override; + virtual Relation RelationByType(RelationType aType) const override; + virtual Accessible* FocusedChild() override; + virtual void SetSelected(bool aSelect) override; + virtual void TakeFocus() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + // Widgets + virtual Accessible* ContainerWidget() const override; + + // XULTreeItemAccessibleBase + NS_DECLARE_STATIC_IID_ACCESSOR(XULTREEITEMBASEACCESSIBLE_IMPL_CID) + + /** + * Return row index associated with the accessible. + */ + int32_t GetRowIndex() const { return mRow; } + + /** + * Return cell accessible for the given column. If XUL tree accessible is not + * accessible table then return null. + */ + virtual XULTreeGridCellAccessible* GetCellAccessible( + nsTreeColumn* aColumn) const { + return nullptr; + } + + /** + * Proccess row invalidation. Used to fires name change events. + */ + virtual void RowInvalidated(int32_t aStartColIdx, int32_t aEndColIdx) = 0; + + protected: + virtual ~XULTreeItemAccessibleBase(); + + enum { eAction_Click = 0, eAction_Expand = 1 }; + + // Accessible + MOZ_CAN_RUN_SCRIPT + virtual void DispatchClickEvent(nsIContent* aContent, + uint32_t aActionIndex) const override; + virtual Accessible* GetSiblingAtOffset( + int32_t aOffset, nsresult* aError = nullptr) const override; + + // XULTreeItemAccessibleBase + + /** + * Return true if the tree item accessible is expandable (contains subrows). + */ + bool IsExpandable() const; + + /** + * Return name for cell at the given column. + */ + void GetCellName(nsTreeColumn* aColumn, nsAString& aName) const; + + RefPtr<dom::XULTreeElement> mTree; + nsITreeView* mTreeView; + int32_t mRow; +}; + +NS_DEFINE_STATIC_IID_ACCESSOR(XULTreeItemAccessibleBase, + XULTREEITEMBASEACCESSIBLE_IMPL_CID) + +/** + * Accessible class for items for XUL tree. + */ +class XULTreeItemAccessible : public XULTreeItemAccessibleBase { + public: + XULTreeItemAccessible(nsIContent* aContent, DocAccessible* aDoc, + Accessible* aParent, dom::XULTreeElement* aTree, + nsITreeView* aTreeView, int32_t aRow); + + // nsISupports and cycle collection + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(XULTreeItemAccessible, + XULTreeItemAccessibleBase) + + // Accessible + virtual void Shutdown() override; + virtual ENameValueFlag Name(nsString& aName) const override; + virtual a11y::role NativeRole() const override; + + // XULTreeItemAccessibleBase + virtual void RowInvalidated(int32_t aStartColIdx, + int32_t aEndColIdx) override; + + protected: + virtual ~XULTreeItemAccessible(); + + // XULTreeItemAccessible + RefPtr<nsTreeColumn> mColumn; + nsString mCachedName; +}; + +/** + * Accessible class for columns element of XUL tree. + */ +class XULTreeColumAccessible : public XULColumAccessible { + public: + XULTreeColumAccessible(nsIContent* aContent, DocAccessible* aDoc); + + protected: + // Accessible + virtual Accessible* GetSiblingAtOffset( + int32_t aOffset, nsresult* aError = nullptr) const override; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Accessible downcasting method + +inline XULTreeAccessible* Accessible::AsXULTree() { + return IsXULTree() ? static_cast<XULTreeAccessible*>(this) : nullptr; +} + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/XULTreeGridAccessible.cpp b/accessible/xul/XULTreeGridAccessible.cpp new file mode 100644 index 0000000000..1bd87c1419 --- /dev/null +++ b/accessible/xul/XULTreeGridAccessible.cpp @@ -0,0 +1,664 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#include "XULTreeGridAccessibleWrap.h" + +#include "nsAccCache.h" +#include "nsAccessibilityService.h" +#include "nsAccUtils.h" +#include "DocAccessible.h" +#include "nsEventShell.h" +#include "Relation.h" +#include "Role.h" +#include "States.h" +#include "nsQueryObject.h" +#include "nsTreeColumns.h" + +#include "nsPersistentProperties.h" +#include "nsITreeSelection.h" +#include "nsComponentManagerUtils.h" +#include "mozilla/PresShell.h" +#include "mozilla/dom/Element.h" +#include "mozilla/dom/TreeColumnBinding.h" +#include "mozilla/dom/XULTreeElementBinding.h" + +using namespace mozilla::a11y; +using namespace mozilla; + +XULTreeGridAccessible::~XULTreeGridAccessible() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridAccessible: Table + +uint32_t XULTreeGridAccessible::ColCount() const { + return nsCoreUtils::GetSensibleColumnCount(mTree); +} + +uint32_t XULTreeGridAccessible::RowCount() { + if (!mTreeView) return 0; + + int32_t rowCount = 0; + mTreeView->GetRowCount(&rowCount); + return rowCount >= 0 ? rowCount : 0; +} + +uint32_t XULTreeGridAccessible::SelectedCellCount() { + return SelectedRowCount() * ColCount(); +} + +uint32_t XULTreeGridAccessible::SelectedColCount() { + // If all the row has been selected, then all the columns are selected, + // because we can't select a column alone. + + uint32_t selectedRowCount = SelectedItemCount(); + return selectedRowCount > 0 && selectedRowCount == RowCount() ? ColCount() + : 0; +} + +uint32_t XULTreeGridAccessible::SelectedRowCount() { + return SelectedItemCount(); +} + +void XULTreeGridAccessible::SelectedCells(nsTArray<Accessible*>* aCells) { + uint32_t colCount = ColCount(), rowCount = RowCount(); + + for (uint32_t rowIdx = 0; rowIdx < rowCount; rowIdx++) { + if (IsRowSelected(rowIdx)) { + for (uint32_t colIdx = 0; colIdx < colCount; colIdx++) { + Accessible* cell = CellAt(rowIdx, colIdx); + aCells->AppendElement(cell); + } + } + } +} + +void XULTreeGridAccessible::SelectedCellIndices(nsTArray<uint32_t>* aCells) { + uint32_t colCount = ColCount(), rowCount = RowCount(); + + for (uint32_t rowIdx = 0; rowIdx < rowCount; rowIdx++) + if (IsRowSelected(rowIdx)) + for (uint32_t colIdx = 0; colIdx < colCount; colIdx++) + aCells->AppendElement(rowIdx * colCount + colIdx); +} + +void XULTreeGridAccessible::SelectedColIndices(nsTArray<uint32_t>* aCols) { + if (RowCount() != SelectedRowCount()) return; + + uint32_t colCount = ColCount(); + aCols->SetCapacity(colCount); + for (uint32_t colIdx = 0; colIdx < colCount; colIdx++) + aCols->AppendElement(colIdx); +} + +void XULTreeGridAccessible::SelectedRowIndices(nsTArray<uint32_t>* aRows) { + uint32_t rowCount = RowCount(); + for (uint32_t rowIdx = 0; rowIdx < rowCount; rowIdx++) + if (IsRowSelected(rowIdx)) aRows->AppendElement(rowIdx); +} + +Accessible* XULTreeGridAccessible::CellAt(uint32_t aRowIndex, + uint32_t aColumnIndex) { + Accessible* row = GetTreeItemAccessible(aRowIndex); + if (!row) return nullptr; + + RefPtr<nsTreeColumn> column = + nsCoreUtils::GetSensibleColumnAt(mTree, aColumnIndex); + if (!column) return nullptr; + + RefPtr<XULTreeItemAccessibleBase> rowAcc = do_QueryObject(row); + if (!rowAcc) return nullptr; + + return rowAcc->GetCellAccessible(column); +} + +void XULTreeGridAccessible::ColDescription(uint32_t aColIdx, + nsString& aDescription) { + aDescription.Truncate(); + + Accessible* treeColumns = Accessible::GetChildAt(0); + if (treeColumns) { + Accessible* treeColumnItem = treeColumns->GetChildAt(aColIdx); + if (treeColumnItem) treeColumnItem->Name(aDescription); + } +} + +bool XULTreeGridAccessible::IsColSelected(uint32_t aColIdx) { + // If all the row has been selected, then all the columns are selected. + // Because we can't select a column alone. + return SelectedItemCount() == RowCount(); +} + +bool XULTreeGridAccessible::IsRowSelected(uint32_t aRowIdx) { + if (!mTreeView) return false; + + nsCOMPtr<nsITreeSelection> selection; + nsresult rv = mTreeView->GetSelection(getter_AddRefs(selection)); + NS_ENSURE_SUCCESS(rv, false); + + bool isSelected = false; + selection->IsSelected(aRowIdx, &isSelected); + return isSelected; +} + +bool XULTreeGridAccessible::IsCellSelected(uint32_t aRowIdx, uint32_t aColIdx) { + return IsRowSelected(aRowIdx); +} + +void XULTreeGridAccessible::SelectRow(uint32_t aRowIdx) { + if (!mTreeView) return; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + NS_ASSERTION(selection, "GetSelection() Shouldn't fail!"); + + selection->Select(aRowIdx); +} + +void XULTreeGridAccessible::UnselectRow(uint32_t aRowIdx) { + if (!mTreeView) return; + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + + if (selection) selection->ClearRange(aRowIdx, aRowIdx); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridAccessible: Accessible implementation + +role XULTreeGridAccessible::NativeRole() const { + RefPtr<nsTreeColumns> treeColumns = mTree->GetColumns(); + if (!treeColumns) { + NS_ERROR("No treecolumns object for tree!"); + return roles::NOTHING; + } + + nsTreeColumn* primaryColumn = treeColumns->GetPrimaryColumn(); + + return primaryColumn ? roles::TREE_TABLE : roles::TABLE; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridAccessible: XULTreeAccessible implementation + +already_AddRefed<Accessible> XULTreeGridAccessible::CreateTreeItemAccessible( + int32_t aRow) const { + RefPtr<Accessible> accessible = new XULTreeGridRowAccessible( + mContent, mDoc, const_cast<XULTreeGridAccessible*>(this), mTree, + mTreeView, aRow); + + return accessible.forget(); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridRowAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTreeGridRowAccessible::XULTreeGridRowAccessible( + nsIContent* aContent, DocAccessible* aDoc, Accessible* aTreeAcc, + dom::XULTreeElement* aTree, nsITreeView* aTreeView, int32_t aRow) + : XULTreeItemAccessibleBase(aContent, aDoc, aTreeAcc, aTree, aTreeView, + aRow), + mAccessibleCache(kDefaultTreeCacheLength) { + mGenericTypes |= eTableRow; + mStateFlags |= eNoKidsFromDOM; +} + +XULTreeGridRowAccessible::~XULTreeGridRowAccessible() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridRowAccessible: nsISupports and cycle collection implementation + +NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeGridRowAccessible, + XULTreeItemAccessibleBase, mAccessibleCache) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XULTreeGridRowAccessible) +NS_INTERFACE_MAP_END_INHERITING(XULTreeItemAccessibleBase) + +NS_IMPL_ADDREF_INHERITED(XULTreeGridRowAccessible, XULTreeItemAccessibleBase) +NS_IMPL_RELEASE_INHERITED(XULTreeGridRowAccessible, XULTreeItemAccessibleBase) + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridRowAccessible: Accessible implementation + +void XULTreeGridRowAccessible::Shutdown() { + if (mDoc && !mDoc->IsDefunct()) { + UnbindCacheEntriesFromDocument(mAccessibleCache); + } + + XULTreeItemAccessibleBase::Shutdown(); +} + +role XULTreeGridRowAccessible::NativeRole() const { return roles::ROW; } + +ENameValueFlag XULTreeGridRowAccessible::Name(nsString& aName) const { + aName.Truncate(); + + // XXX: the row name sholdn't be a concatenation of cell names (bug 664384). + RefPtr<nsTreeColumn> column = nsCoreUtils::GetFirstSensibleColumn(mTree); + while (column) { + if (!aName.IsEmpty()) aName.Append(' '); + + nsAutoString cellName; + GetCellName(column, cellName); + aName.Append(cellName); + + column = nsCoreUtils::GetNextSensibleColumn(column); + } + + return eNameOK; +} + +Accessible* XULTreeGridRowAccessible::ChildAtPoint( + int32_t aX, int32_t aY, EWhichChildAtPoint aWhichChild) { + nsIFrame* frame = GetFrame(); + if (!frame) return nullptr; + + nsPresContext* presContext = frame->PresContext(); + PresShell* presShell = presContext->PresShell(); + + nsIFrame* rootFrame = presShell->GetRootFrame(); + NS_ENSURE_TRUE(rootFrame, nullptr); + + CSSIntRect rootRect = rootFrame->GetScreenRect(); + + int32_t clientX = presContext->DevPixelsToIntCSSPixels(aX) - rootRect.X(); + int32_t clientY = presContext->DevPixelsToIntCSSPixels(aY) - rootRect.Y(); + + ErrorResult rv; + dom::TreeCellInfo cellInfo; + mTree->GetCellAt(clientX, clientY, cellInfo, rv); + + // Return if we failed to find tree cell in the row for the given point. + if (cellInfo.mRow != mRow || !cellInfo.mCol) return nullptr; + + return GetCellAccessible(cellInfo.mCol); +} + +Accessible* XULTreeGridRowAccessible::GetChildAt(uint32_t aIndex) const { + if (IsDefunct()) return nullptr; + + RefPtr<nsTreeColumn> column = nsCoreUtils::GetSensibleColumnAt(mTree, aIndex); + if (!column) return nullptr; + + return GetCellAccessible(column); +} + +uint32_t XULTreeGridRowAccessible::ChildCount() const { + return nsCoreUtils::GetSensibleColumnCount(mTree); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridRowAccessible: XULTreeItemAccessibleBase implementation + +XULTreeGridCellAccessible* XULTreeGridRowAccessible::GetCellAccessible( + nsTreeColumn* aColumn) const { + MOZ_ASSERT(aColumn, "No tree column!"); + + void* key = static_cast<void*>(aColumn); + XULTreeGridCellAccessible* cachedCell = mAccessibleCache.GetWeak(key); + if (cachedCell) return cachedCell; + + RefPtr<XULTreeGridCellAccessible> cell = new XULTreeGridCellAccessibleWrap( + mContent, mDoc, const_cast<XULTreeGridRowAccessible*>(this), mTree, + mTreeView, mRow, aColumn); + mAccessibleCache.Put(key, RefPtr{cell}); + Document()->BindToDocument(cell, nullptr); + return cell; +} + +void XULTreeGridRowAccessible::RowInvalidated(int32_t aStartColIdx, + int32_t aEndColIdx) { + RefPtr<nsTreeColumns> treeColumns = mTree->GetColumns(); + if (!treeColumns) return; + + bool nameChanged = false; + for (int32_t colIdx = aStartColIdx; colIdx <= aEndColIdx; ++colIdx) { + nsTreeColumn* column = treeColumns->GetColumnAt(colIdx); + if (column && !nsCoreUtils::IsColumnHidden(column)) { + XULTreeGridCellAccessible* cell = GetCellAccessible(column); + if (cell) nameChanged |= cell->CellInvalidated(); + } + } + + if (nameChanged) + nsEventShell::FireEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, this); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible +//////////////////////////////////////////////////////////////////////////////// + +XULTreeGridCellAccessible::XULTreeGridCellAccessible( + nsIContent* aContent, DocAccessible* aDoc, + XULTreeGridRowAccessible* aRowAcc, dom::XULTreeElement* aTree, + nsITreeView* aTreeView, int32_t aRow, nsTreeColumn* aColumn) + : LeafAccessible(aContent, aDoc), + mTree(aTree), + mTreeView(aTreeView), + mRow(aRow), + mColumn(aColumn) { + mParent = aRowAcc; + mStateFlags |= eSharedNode; + mGenericTypes |= eTableCell; + + NS_ASSERTION(mTreeView, "mTreeView is null"); + + if (mColumn->Type() == dom::TreeColumn_Binding::TYPE_CHECKBOX) + mTreeView->GetCellValue(mRow, mColumn, mCachedTextEquiv); + else + mTreeView->GetCellText(mRow, mColumn, mCachedTextEquiv); +} + +XULTreeGridCellAccessible::~XULTreeGridCellAccessible() {} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible: nsISupports implementation + +NS_IMPL_CYCLE_COLLECTION_INHERITED(XULTreeGridCellAccessible, LeafAccessible, + mTree, mColumn) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XULTreeGridCellAccessible) +NS_INTERFACE_MAP_END_INHERITING(LeafAccessible) +NS_IMPL_ADDREF_INHERITED(XULTreeGridCellAccessible, LeafAccessible) +NS_IMPL_RELEASE_INHERITED(XULTreeGridCellAccessible, LeafAccessible) + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible: Accessible + +void XULTreeGridCellAccessible::Shutdown() { + mTree = nullptr; + mTreeView = nullptr; + mRow = -1; + mColumn = nullptr; + mParent = nullptr; // null-out to prevent base class's shutdown ops + + LeafAccessible::Shutdown(); +} + +Accessible* XULTreeGridCellAccessible::FocusedChild() { return nullptr; } + +ENameValueFlag XULTreeGridCellAccessible::Name(nsString& aName) const { + aName.Truncate(); + + if (!mTreeView) return eNameOK; + + mTreeView->GetCellText(mRow, mColumn, aName); + + // If there is still no name try the cell value: + // This is for graphical cells. We need tree/table view implementors to + // implement FooView::GetCellValue to return a meaningful string for cases + // where there is something shown in the cell (non-text) such as a star icon; + // in which case GetCellValue for that cell would return "starred" or + // "flagged" for example. + if (aName.IsEmpty()) mTreeView->GetCellValue(mRow, mColumn, aName); + + return eNameOK; +} + +nsIntRect XULTreeGridCellAccessible::BoundsInCSSPixels() const { + // Get bounds for tree cell and add x and y of treechildren element to + // x and y of the cell. + nsresult rv; + nsIntRect rect = mTree->GetCoordsForCellItem(mRow, mColumn, u"cell"_ns, rv); + if (NS_FAILED(rv)) { + return nsIntRect(); + } + + RefPtr<dom::Element> bodyElement = mTree->GetTreeBody(); + if (!bodyElement || !bodyElement->IsXULElement()) { + return nsIntRect(); + } + + nsIFrame* bodyFrame = bodyElement->GetPrimaryFrame(); + if (!bodyFrame) { + return nsIntRect(); + } + + CSSIntRect screenRect = bodyFrame->GetScreenRect(); + rect.x += screenRect.x; + rect.y += screenRect.y; + return rect; +} + +nsRect XULTreeGridCellAccessible::BoundsInAppUnits() const { + nsIntRect bounds = BoundsInCSSPixels(); + nsPresContext* presContext = mDoc->PresContext(); + return nsRect(presContext->CSSPixelsToAppUnits(bounds.X()), + presContext->CSSPixelsToAppUnits(bounds.Y()), + presContext->CSSPixelsToAppUnits(bounds.Width()), + presContext->CSSPixelsToAppUnits(bounds.Height())); +} + +uint8_t XULTreeGridCellAccessible::ActionCount() const { + if (mColumn->Cycler()) return 1; + + if (mColumn->Type() == dom::TreeColumn_Binding::TYPE_CHECKBOX && IsEditable()) + return 1; + + return 0; +} + +void XULTreeGridCellAccessible::ActionNameAt(uint8_t aIndex, nsAString& aName) { + aName.Truncate(); + + if (aIndex != eAction_Click || !mTreeView) return; + + if (mColumn->Cycler()) { + aName.AssignLiteral("cycle"); + return; + } + + if (mColumn->Type() == dom::TreeColumn_Binding::TYPE_CHECKBOX && + IsEditable()) { + nsAutoString value; + mTreeView->GetCellValue(mRow, mColumn, value); + if (value.EqualsLiteral("true")) + aName.AssignLiteral("uncheck"); + else + aName.AssignLiteral("check"); + } +} + +bool XULTreeGridCellAccessible::DoAction(uint8_t aIndex) const { + if (aIndex != eAction_Click) return false; + + if (mColumn->Cycler()) { + DoCommand(); + return true; + } + + if (mColumn->Type() == dom::TreeColumn_Binding::TYPE_CHECKBOX && + IsEditable()) { + DoCommand(); + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible: TableCell + +TableAccessible* XULTreeGridCellAccessible::Table() const { + Accessible* grandParent = mParent->Parent(); + if (grandParent) return grandParent->AsTable(); + + return nullptr; +} + +uint32_t XULTreeGridCellAccessible::ColIdx() const { + uint32_t colIdx = 0; + RefPtr<nsTreeColumn> column = mColumn; + while ((column = nsCoreUtils::GetPreviousSensibleColumn(column))) colIdx++; + + return colIdx; +} + +uint32_t XULTreeGridCellAccessible::RowIdx() const { return mRow; } + +void XULTreeGridCellAccessible::ColHeaderCells( + nsTArray<Accessible*>* aHeaderCells) { + dom::Element* columnElm = mColumn->Element(); + + Accessible* headerCell = mDoc->GetAccessible(columnElm); + if (headerCell) aHeaderCells->AppendElement(headerCell); +} + +bool XULTreeGridCellAccessible::Selected() { + nsCOMPtr<nsITreeSelection> selection; + nsresult rv = mTreeView->GetSelection(getter_AddRefs(selection)); + NS_ENSURE_SUCCESS(rv, false); + + bool selected = false; + selection->IsSelected(mRow, &selected); + return selected; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible: Accessible public implementation + +already_AddRefed<nsIPersistentProperties> +XULTreeGridCellAccessible::NativeAttributes() { + RefPtr<nsPersistentProperties> attributes = new nsPersistentProperties(); + + // "table-cell-index" attribute + TableAccessible* table = Table(); + if (!table) return attributes.forget(); + + nsAutoString stringIdx; + stringIdx.AppendInt(table->CellIndexAt(mRow, ColIdx())); + nsAccUtils::SetAccAttr(attributes, nsGkAtoms::tableCellIndex, stringIdx); + + // "cycles" attribute + if (mColumn->Cycler()) + nsAccUtils::SetAccAttr(attributes, nsGkAtoms::cycles, u"true"_ns); + + return attributes.forget(); +} + +role XULTreeGridCellAccessible::NativeRole() const { return roles::GRID_CELL; } + +uint64_t XULTreeGridCellAccessible::NativeState() const { + if (!mTreeView) return states::DEFUNCT; + + // selectable/selected state + uint64_t states = + states::SELECTABLE; // keep in sync with NativeInteractiveState + + nsCOMPtr<nsITreeSelection> selection; + mTreeView->GetSelection(getter_AddRefs(selection)); + if (selection) { + bool isSelected = false; + selection->IsSelected(mRow, &isSelected); + if (isSelected) states |= states::SELECTED; + } + + // checked state + if (mColumn->Type() == dom::TreeColumn_Binding::TYPE_CHECKBOX) { + states |= states::CHECKABLE; + nsAutoString checked; + mTreeView->GetCellValue(mRow, mColumn, checked); + if (checked.EqualsIgnoreCase("true")) states |= states::CHECKED; + } + + return states; +} + +uint64_t XULTreeGridCellAccessible::NativeInteractiveState() const { + return states::SELECTABLE; +} + +int32_t XULTreeGridCellAccessible::IndexInParent() const { return ColIdx(); } + +Relation XULTreeGridCellAccessible::RelationByType(RelationType aType) const { + return Relation(); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible: public implementation + +bool XULTreeGridCellAccessible::CellInvalidated() { + nsAutoString textEquiv; + + if (mColumn->Type() == dom::TreeColumn_Binding::TYPE_CHECKBOX) { + mTreeView->GetCellValue(mRow, mColumn, textEquiv); + if (mCachedTextEquiv != textEquiv) { + bool isEnabled = textEquiv.EqualsLiteral("true"); + RefPtr<AccEvent> accEvent = + new AccStateChangeEvent(this, states::CHECKED, isEnabled); + nsEventShell::FireEvent(accEvent); + + mCachedTextEquiv = textEquiv; + return true; + } + + return false; + } + + mTreeView->GetCellText(mRow, mColumn, textEquiv); + if (mCachedTextEquiv != textEquiv) { + nsEventShell::FireEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, this); + mCachedTextEquiv = textEquiv; + return true; + } + + return false; +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible: Accessible protected implementation + +Accessible* XULTreeGridCellAccessible::GetSiblingAtOffset( + int32_t aOffset, nsresult* aError) const { + if (aError) *aError = NS_OK; // fail peacefully + + RefPtr<nsTreeColumn> columnAtOffset(mColumn), column; + if (aOffset < 0) { + for (int32_t index = aOffset; index < 0 && columnAtOffset; index++) { + column = nsCoreUtils::GetPreviousSensibleColumn(columnAtOffset); + column.swap(columnAtOffset); + } + } else { + for (int32_t index = aOffset; index > 0 && columnAtOffset; index--) { + column = nsCoreUtils::GetNextSensibleColumn(columnAtOffset); + column.swap(columnAtOffset); + } + } + + if (!columnAtOffset) return nullptr; + + RefPtr<XULTreeItemAccessibleBase> rowAcc = do_QueryObject(Parent()); + return rowAcc->GetCellAccessible(columnAtOffset); +} + +void XULTreeGridCellAccessible::DispatchClickEvent( + nsIContent* aContent, uint32_t aActionIndex) const { + if (IsDefunct()) return; + + RefPtr<dom::XULTreeElement> tree = mTree; + RefPtr<nsTreeColumn> column = mColumn; + nsCoreUtils::DispatchClickEvent(tree, mRow, column); +} + +//////////////////////////////////////////////////////////////////////////////// +// XULTreeGridCellAccessible: protected implementation + +bool XULTreeGridCellAccessible::IsEditable() const { + // XXX: logic corresponds to tree.xml, it's preferable to have interface + // method to check it. + bool isEditable = false; + nsresult rv = mTreeView->IsEditable(mRow, mColumn, &isEditable); + if (NS_FAILED(rv) || !isEditable) return false; + + dom::Element* columnElm = mColumn->Element(); + + if (!columnElm->AttrValueIs(kNameSpaceID_None, nsGkAtoms::editable, + nsGkAtoms::_true, eCaseMatters)) + return false; + + return mContent->AsElement()->AttrValueIs( + kNameSpaceID_None, nsGkAtoms::editable, nsGkAtoms::_true, eCaseMatters); +} diff --git a/accessible/xul/XULTreeGridAccessible.h b/accessible/xul/XULTreeGridAccessible.h new file mode 100644 index 0000000000..2e4be63dde --- /dev/null +++ b/accessible/xul/XULTreeGridAccessible.h @@ -0,0 +1,186 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef mozilla_a11y_XULTreeGridAccessible_h__ +#define mozilla_a11y_XULTreeGridAccessible_h__ + +#include "XULTreeAccessible.h" +#include "TableAccessible.h" +#include "TableCellAccessible.h" + +namespace mozilla { +namespace a11y { + +class XULTreeGridCellAccessible; + +/** + * Represents accessible for XUL tree in the case when it has multiple columns. + */ +class XULTreeGridAccessible : public XULTreeAccessible, public TableAccessible { + public: + XULTreeGridAccessible(nsIContent* aContent, DocAccessible* aDoc, + nsTreeBodyFrame* aTreeFrame) + : XULTreeAccessible(aContent, aDoc, aTreeFrame) { + mGenericTypes |= eTable; + } + + // TableAccessible + virtual uint32_t ColCount() const override; + virtual uint32_t RowCount() override; + virtual Accessible* CellAt(uint32_t aRowIndex, + uint32_t aColumnIndex) override; + virtual void ColDescription(uint32_t aColIdx, + nsString& aDescription) override; + virtual bool IsColSelected(uint32_t aColIdx) override; + virtual bool IsRowSelected(uint32_t aRowIdx) override; + virtual bool IsCellSelected(uint32_t aRowIdx, uint32_t aColIdx) override; + virtual uint32_t SelectedCellCount() override; + virtual uint32_t SelectedColCount() override; + virtual uint32_t SelectedRowCount() override; + virtual void SelectedCells(nsTArray<Accessible*>* aCells) override; + virtual void SelectedCellIndices(nsTArray<uint32_t>* aCells) override; + virtual void SelectedColIndices(nsTArray<uint32_t>* aCols) override; + virtual void SelectedRowIndices(nsTArray<uint32_t>* aRows) override; + virtual void SelectRow(uint32_t aRowIdx) override; + virtual void UnselectRow(uint32_t aRowIdx) override; + virtual Accessible* AsAccessible() override { return this; } + + // Accessible + virtual TableAccessible* AsTable() override { return this; } + virtual a11y::role NativeRole() const override; + + protected: + virtual ~XULTreeGridAccessible(); + + // XULTreeAccessible + virtual already_AddRefed<Accessible> CreateTreeItemAccessible( + int32_t aRow) const override; +}; + +/** + * Represents accessible for XUL tree item in the case when XUL tree has + * multiple columns. + */ +class XULTreeGridRowAccessible final : public XULTreeItemAccessibleBase { + public: + using Accessible::GetChildAt; + + XULTreeGridRowAccessible(nsIContent* aContent, DocAccessible* aDoc, + Accessible* aParent, dom::XULTreeElement* aTree, + nsITreeView* aTreeView, int32_t aRow); + + // nsISupports and cycle collection + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(XULTreeGridRowAccessible, + XULTreeItemAccessibleBase) + + // Accessible + virtual void Shutdown() override; + virtual a11y::role NativeRole() const override; + virtual ENameValueFlag Name(nsString& aName) const override; + virtual Accessible* ChildAtPoint(int32_t aX, int32_t aY, + EWhichChildAtPoint aWhichChild) override; + + virtual Accessible* GetChildAt(uint32_t aIndex) const override; + virtual uint32_t ChildCount() const override; + + // XULTreeItemAccessibleBase + XULTreeGridCellAccessible* GetCellAccessible( + nsTreeColumn* aColumn) const final; + virtual void RowInvalidated(int32_t aStartColIdx, + int32_t aEndColIdx) override; + + protected: + virtual ~XULTreeGridRowAccessible(); + + // XULTreeItemAccessibleBase + mutable nsRefPtrHashtable<nsPtrHashKey<const void>, XULTreeGridCellAccessible> + mAccessibleCache; +}; + +/** + * Represents an accessible for XUL tree cell in the case when XUL tree has + * multiple columns. + */ + +class XULTreeGridCellAccessible : public LeafAccessible, + public TableCellAccessible { + public: + XULTreeGridCellAccessible(nsIContent* aContent, DocAccessible* aDoc, + XULTreeGridRowAccessible* aRowAcc, + dom::XULTreeElement* aTree, nsITreeView* aTreeView, + int32_t aRow, nsTreeColumn* aColumn); + + // nsISupports + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(XULTreeGridCellAccessible, + LeafAccessible) + + // Accessible + virtual void Shutdown() override; + virtual TableCellAccessible* AsTableCell() override { return this; } + virtual nsRect BoundsInAppUnits() const override; + virtual nsIntRect BoundsInCSSPixels() const override; + virtual ENameValueFlag Name(nsString& aName) const override; + virtual Accessible* FocusedChild() override; + virtual already_AddRefed<nsIPersistentProperties> NativeAttributes() override; + virtual int32_t IndexInParent() const override; + virtual Relation RelationByType(RelationType aType) const override; + virtual a11y::role NativeRole() const override; + virtual uint64_t NativeState() const override; + virtual uint64_t NativeInteractiveState() const override; + + // ActionAccessible + virtual uint8_t ActionCount() const override; + virtual void ActionNameAt(uint8_t aIndex, nsAString& aName) override; + virtual bool DoAction(uint8_t aIndex) const override; + + // TableCellAccessible + virtual TableAccessible* Table() const override; + virtual uint32_t ColIdx() const override; + virtual uint32_t RowIdx() const override; + virtual void ColHeaderCells(nsTArray<Accessible*>* aHeaderCells) override; + virtual void RowHeaderCells(nsTArray<Accessible*>* aCells) override {} + virtual bool Selected() override; + + /** + * Fire name or state change event if the accessible text or value has been + * changed. + * @return true if name has changed + */ + bool CellInvalidated(); + + protected: + virtual ~XULTreeGridCellAccessible(); + + // Accessible + virtual Accessible* GetSiblingAtOffset( + int32_t aOffset, nsresult* aError = nullptr) const override; + MOZ_CAN_RUN_SCRIPT + virtual void DispatchClickEvent(nsIContent* aContent, + uint32_t aActionIndex) const override; + + // XULTreeGridCellAccessible + + /** + * Return true if value of cell can be modified. + */ + bool IsEditable() const; + + enum { eAction_Click = 0 }; + + RefPtr<dom::XULTreeElement> mTree; + nsITreeView* mTreeView; + + int32_t mRow; + RefPtr<nsTreeColumn> mColumn; + + nsString mCachedTextEquiv; +}; + +} // namespace a11y +} // namespace mozilla + +#endif diff --git a/accessible/xul/moz.build b/accessible/xul/moz.build new file mode 100644 index 0000000000..72a55cccea --- /dev/null +++ b/accessible/xul/moz.build @@ -0,0 +1,59 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +UNIFIED_SOURCES += [ + "XULAlertAccessible.cpp", + "XULComboboxAccessible.cpp", + "XULElementAccessibles.cpp", + "XULFormControlAccessible.cpp", + "XULListboxAccessible.cpp", + "XULMenuAccessible.cpp", + "XULSelectControlAccessible.cpp", + "XULTabAccessible.cpp", + "XULTreeAccessible.cpp", + "XULTreeGridAccessible.cpp", +] + +LOCAL_INCLUDES += [ + "/accessible/base", + "/accessible/generic", + "/accessible/html", + "/accessible/xpcom", + "/dom/base", + "/dom/xul", + "/layout/generic", + "/layout/xul", + "/layout/xul/tree", +] + +if CONFIG["MOZ_WIDGET_TOOLKIT"] == "gtk": + LOCAL_INCLUDES += [ + "/accessible/atk", + ] +elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "windows": + LOCAL_INCLUDES += [ + "/accessible/windows/ia2", + "/accessible/windows/msaa", + ] +elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa": + LOCAL_INCLUDES += [ + "/accessible/mac", + ] +elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": + LOCAL_INCLUDES += [ + "/accessible/android", + ] +else: + LOCAL_INCLUDES += [ + "/accessible/other", + ] + +include("/ipc/chromium/chromium-config.mozbuild") + +FINAL_LIBRARY = "xul" + +if CONFIG["CC_TYPE"] in ("clang", "gcc"): + CXXFLAGS += ["-Wno-error=shadow"] |