summaryrefslogtreecommitdiffstats
path: root/accessible/xpcom/xpcAccessibleSelectable.cpp
blob: df63e116c85bb58e6004908bf68ecd8a0eb6aa87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* -*- 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 "Accessible-inl.h"
#include "xpcAccessibleDocument.h"

#include "nsIMutableArray.h"

using namespace mozilla::a11y;

NS_IMETHODIMP
xpcAccessibleSelectable::GetSelectedItems(nsIArray** aSelectedItems)
{
  NS_ENSURE_ARG_POINTER(aSelectedItems);
  *aSelectedItems = nullptr;

  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  AutoTArray<Accessible*, 10> items;
  Intl()->SelectedItems(&items);

  uint32_t itemCount = items.Length();
  if (itemCount == 0)
    return NS_OK;

  nsresult rv = NS_OK;
  nsCOMPtr<nsIMutableArray> xpcItems =
    do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  for (uint32_t idx = 0; idx < itemCount; idx++)
    xpcItems->AppendElement(static_cast<nsIAccessible*>(ToXPC(items[idx])), false);

  NS_ADDREF(*aSelectedItems = xpcItems);
  return NS_OK;
}

NS_IMETHODIMP
xpcAccessibleSelectable::GetSelectedItemCount(uint32_t* aSelectionCount)
{
  NS_ENSURE_ARG_POINTER(aSelectionCount);
  *aSelectionCount = 0;

  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  *aSelectionCount = Intl()->SelectedItemCount();
  return NS_OK;
}

NS_IMETHODIMP
xpcAccessibleSelectable::GetSelectedItemAt(uint32_t aIndex,
                                           nsIAccessible** aSelected)
{
  NS_ENSURE_ARG_POINTER(aSelected);
  *aSelected = nullptr;

  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  *aSelected = ToXPC(Intl()->GetSelectedItem(aIndex));
  if (*aSelected) {
    NS_ADDREF(*aSelected);
    return NS_OK;
  }

  return NS_ERROR_INVALID_ARG;
}

NS_IMETHODIMP
xpcAccessibleSelectable::IsItemSelected(uint32_t aIndex, bool* aIsSelected)
{
  NS_ENSURE_ARG_POINTER(aIsSelected);
  *aIsSelected = false;

  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  *aIsSelected = Intl()->IsItemSelected(aIndex);
  return NS_OK;
}

NS_IMETHODIMP
xpcAccessibleSelectable::AddItemToSelection(uint32_t aIndex)
{
  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  return Intl()->AddItemToSelection(aIndex) ? NS_OK : NS_ERROR_INVALID_ARG;
}

NS_IMETHODIMP
xpcAccessibleSelectable::RemoveItemFromSelection(uint32_t aIndex)
{
  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  return Intl()->RemoveItemFromSelection(aIndex) ? NS_OK : NS_ERROR_INVALID_ARG;
}

NS_IMETHODIMP
xpcAccessibleSelectable::SelectAll(bool* aIsMultiSelect)
{
  NS_ENSURE_ARG_POINTER(aIsMultiSelect);
  *aIsMultiSelect = false;

  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  *aIsMultiSelect = Intl()->SelectAll();
  return NS_OK;
}

NS_IMETHODIMP
xpcAccessibleSelectable::UnselectAll()
{
  if (!Intl())
    return NS_ERROR_FAILURE;
  NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");

  Intl()->UnselectAll();
  return NS_OK;
}