xpcAccessibleSelectable.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "Accessible-inl.h"
  6. #include "xpcAccessibleDocument.h"
  7. #include "nsIMutableArray.h"
  8. using namespace mozilla::a11y;
  9. NS_IMETHODIMP
  10. xpcAccessibleSelectable::GetSelectedItems(nsIArray** aSelectedItems)
  11. {
  12. NS_ENSURE_ARG_POINTER(aSelectedItems);
  13. *aSelectedItems = nullptr;
  14. if (!Intl())
  15. return NS_ERROR_FAILURE;
  16. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  17. AutoTArray<Accessible*, 10> items;
  18. Intl()->SelectedItems(&items);
  19. uint32_t itemCount = items.Length();
  20. if (itemCount == 0)
  21. return NS_OK;
  22. nsresult rv = NS_OK;
  23. nsCOMPtr<nsIMutableArray> xpcItems =
  24. do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
  25. NS_ENSURE_SUCCESS(rv, rv);
  26. for (uint32_t idx = 0; idx < itemCount; idx++)
  27. xpcItems->AppendElement(static_cast<nsIAccessible*>(ToXPC(items[idx])), false);
  28. NS_ADDREF(*aSelectedItems = xpcItems);
  29. return NS_OK;
  30. }
  31. NS_IMETHODIMP
  32. xpcAccessibleSelectable::GetSelectedItemCount(uint32_t* aSelectionCount)
  33. {
  34. NS_ENSURE_ARG_POINTER(aSelectionCount);
  35. *aSelectionCount = 0;
  36. if (!Intl())
  37. return NS_ERROR_FAILURE;
  38. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  39. *aSelectionCount = Intl()->SelectedItemCount();
  40. return NS_OK;
  41. }
  42. NS_IMETHODIMP
  43. xpcAccessibleSelectable::GetSelectedItemAt(uint32_t aIndex,
  44. nsIAccessible** aSelected)
  45. {
  46. NS_ENSURE_ARG_POINTER(aSelected);
  47. *aSelected = nullptr;
  48. if (!Intl())
  49. return NS_ERROR_FAILURE;
  50. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  51. *aSelected = ToXPC(Intl()->GetSelectedItem(aIndex));
  52. if (*aSelected) {
  53. NS_ADDREF(*aSelected);
  54. return NS_OK;
  55. }
  56. return NS_ERROR_INVALID_ARG;
  57. }
  58. NS_IMETHODIMP
  59. xpcAccessibleSelectable::IsItemSelected(uint32_t aIndex, bool* aIsSelected)
  60. {
  61. NS_ENSURE_ARG_POINTER(aIsSelected);
  62. *aIsSelected = false;
  63. if (!Intl())
  64. return NS_ERROR_FAILURE;
  65. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  66. *aIsSelected = Intl()->IsItemSelected(aIndex);
  67. return NS_OK;
  68. }
  69. NS_IMETHODIMP
  70. xpcAccessibleSelectable::AddItemToSelection(uint32_t aIndex)
  71. {
  72. if (!Intl())
  73. return NS_ERROR_FAILURE;
  74. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  75. return Intl()->AddItemToSelection(aIndex) ? NS_OK : NS_ERROR_INVALID_ARG;
  76. }
  77. NS_IMETHODIMP
  78. xpcAccessibleSelectable::RemoveItemFromSelection(uint32_t aIndex)
  79. {
  80. if (!Intl())
  81. return NS_ERROR_FAILURE;
  82. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  83. return Intl()->RemoveItemFromSelection(aIndex) ? NS_OK : NS_ERROR_INVALID_ARG;
  84. }
  85. NS_IMETHODIMP
  86. xpcAccessibleSelectable::SelectAll(bool* aIsMultiSelect)
  87. {
  88. NS_ENSURE_ARG_POINTER(aIsMultiSelect);
  89. *aIsMultiSelect = false;
  90. if (!Intl())
  91. return NS_ERROR_FAILURE;
  92. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  93. *aIsMultiSelect = Intl()->SelectAll();
  94. return NS_OK;
  95. }
  96. NS_IMETHODIMP
  97. xpcAccessibleSelectable::UnselectAll()
  98. {
  99. if (!Intl())
  100. return NS_ERROR_FAILURE;
  101. NS_PRECONDITION(Intl()->IsSelect(), "Called on non selectable widget!");
  102. Intl()->UnselectAll();
  103. return NS_OK;
  104. }