Bug 1647875 [wpt PR 24320] - [AspectRatio] Add an in-flow test for computing a block...
[gecko.git] / accessible / xpcom / xpcAccessibleSelectable.cpp
blob9f1d2ce1a8b37b68f90b7fd5d480de72304a157c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "Accessible-inl.h"
9 #include "nsIAccessible.h"
10 #include "nsIMutableArray.h"
11 #include "xpcAccessibleDocument.h"
12 #include "xpcAccessibleSelectable.h"
14 using namespace mozilla::a11y;
16 NS_IMETHODIMP
17 xpcAccessibleSelectable::GetSelectedItems(nsIArray** aSelectedItems) {
18 NS_ENSURE_ARG_POINTER(aSelectedItems);
19 *aSelectedItems = nullptr;
21 if (!Intl()) return NS_ERROR_FAILURE;
22 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
24 AutoTArray<Accessible*, 10> items;
25 Intl()->SelectedItems(&items);
27 uint32_t itemCount = items.Length();
28 if (itemCount == 0) return NS_OK;
30 nsresult rv = NS_OK;
31 nsCOMPtr<nsIMutableArray> xpcItems =
32 do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
33 NS_ENSURE_SUCCESS(rv, rv);
35 for (uint32_t idx = 0; idx < itemCount; idx++)
36 xpcItems->AppendElement(static_cast<nsIAccessible*>(ToXPC(items[idx])));
38 NS_ADDREF(*aSelectedItems = xpcItems);
39 return NS_OK;
42 NS_IMETHODIMP
43 xpcAccessibleSelectable::GetSelectedItemCount(uint32_t* aSelectionCount) {
44 NS_ENSURE_ARG_POINTER(aSelectionCount);
45 *aSelectionCount = 0;
47 if (!Intl()) return NS_ERROR_FAILURE;
48 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
50 *aSelectionCount = Intl()->SelectedItemCount();
51 return NS_OK;
54 NS_IMETHODIMP
55 xpcAccessibleSelectable::GetSelectedItemAt(uint32_t aIndex,
56 nsIAccessible** aSelected) {
57 NS_ENSURE_ARG_POINTER(aSelected);
58 *aSelected = nullptr;
60 if (!Intl()) return NS_ERROR_FAILURE;
61 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
63 *aSelected = ToXPC(Intl()->GetSelectedItem(aIndex));
64 if (*aSelected) {
65 NS_ADDREF(*aSelected);
66 return NS_OK;
69 return NS_ERROR_INVALID_ARG;
72 NS_IMETHODIMP
73 xpcAccessibleSelectable::IsItemSelected(uint32_t aIndex, bool* aIsSelected) {
74 NS_ENSURE_ARG_POINTER(aIsSelected);
75 *aIsSelected = false;
77 if (!Intl()) return NS_ERROR_FAILURE;
78 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
80 *aIsSelected = Intl()->IsItemSelected(aIndex);
81 return NS_OK;
84 NS_IMETHODIMP
85 xpcAccessibleSelectable::AddItemToSelection(uint32_t aIndex) {
86 if (!Intl()) return NS_ERROR_FAILURE;
87 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
89 return Intl()->AddItemToSelection(aIndex) ? NS_OK : NS_ERROR_INVALID_ARG;
92 NS_IMETHODIMP
93 xpcAccessibleSelectable::RemoveItemFromSelection(uint32_t aIndex) {
94 if (!Intl()) return NS_ERROR_FAILURE;
95 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
97 return Intl()->RemoveItemFromSelection(aIndex) ? NS_OK : NS_ERROR_INVALID_ARG;
100 NS_IMETHODIMP
101 xpcAccessibleSelectable::SelectAll(bool* aIsMultiSelect) {
102 NS_ENSURE_ARG_POINTER(aIsMultiSelect);
103 *aIsMultiSelect = false;
105 if (!Intl()) return NS_ERROR_FAILURE;
106 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
108 *aIsMultiSelect = Intl()->SelectAll();
109 return NS_OK;
112 NS_IMETHODIMP
113 xpcAccessibleSelectable::UnselectAll() {
114 if (!Intl()) return NS_ERROR_FAILURE;
115 MOZ_ASSERT(Intl()->IsSelect(), "Called on non selectable widget!");
117 Intl()->UnselectAll();
118 return NS_OK;