xpcAccessibleTable.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 "xpcAccessibleTable.h"
  6. #include "Accessible.h"
  7. #include "TableAccessible.h"
  8. #include "xpcAccessibleDocument.h"
  9. #include "nsIMutableArray.h"
  10. #include "nsComponentManagerUtils.h"
  11. using namespace mozilla::a11y;
  12. static const uint32_t XPC_TABLE_DEFAULT_SIZE = 40;
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // nsISupports
  15. NS_IMPL_ISUPPORTS_INHERITED(xpcAccessibleTable,
  16. xpcAccessibleGeneric,
  17. nsIAccessibleTable)
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // nsIAccessibleTable
  20. NS_IMETHODIMP
  21. xpcAccessibleTable::GetCaption(nsIAccessible** aCaption)
  22. {
  23. NS_ENSURE_ARG_POINTER(aCaption);
  24. *aCaption = nullptr;
  25. if (!Intl())
  26. return NS_ERROR_FAILURE;
  27. NS_IF_ADDREF(*aCaption = ToXPC(Intl()->Caption()));
  28. return NS_OK;
  29. }
  30. NS_IMETHODIMP
  31. xpcAccessibleTable::GetColumnCount(int32_t* aColumnCount)
  32. {
  33. NS_ENSURE_ARG_POINTER(aColumnCount);
  34. *aColumnCount = 0;
  35. if (!Intl())
  36. return NS_ERROR_FAILURE;
  37. *aColumnCount = Intl()->ColCount();
  38. return NS_OK;
  39. }
  40. NS_IMETHODIMP
  41. xpcAccessibleTable::GetRowCount(int32_t* aRowCount)
  42. {
  43. NS_ENSURE_ARG_POINTER(aRowCount);
  44. *aRowCount = 0;
  45. if (!Intl())
  46. return NS_ERROR_FAILURE;
  47. *aRowCount = Intl()->RowCount();
  48. return NS_OK;
  49. }
  50. NS_IMETHODIMP
  51. xpcAccessibleTable::GetCellAt(int32_t aRowIdx, int32_t aColIdx,
  52. nsIAccessible** aCell)
  53. {
  54. NS_ENSURE_ARG_POINTER(aCell);
  55. *aCell = nullptr;
  56. if (!Intl())
  57. return NS_ERROR_FAILURE;
  58. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount() ||
  59. aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  60. return NS_ERROR_INVALID_ARG;
  61. NS_IF_ADDREF(*aCell = ToXPC(Intl()->CellAt(aRowIdx, aColIdx)));
  62. return NS_OK;
  63. }
  64. NS_IMETHODIMP
  65. xpcAccessibleTable::GetCellIndexAt(int32_t aRowIdx, int32_t aColIdx,
  66. int32_t* aCellIdx)
  67. {
  68. NS_ENSURE_ARG_POINTER(aCellIdx);
  69. *aCellIdx = -1;
  70. if (!Intl())
  71. return NS_ERROR_FAILURE;
  72. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount() ||
  73. aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  74. return NS_ERROR_INVALID_ARG;
  75. *aCellIdx = Intl()->CellIndexAt(aRowIdx, aColIdx);
  76. return NS_OK;
  77. }
  78. NS_IMETHODIMP
  79. xpcAccessibleTable::GetColumnExtentAt(int32_t aRowIdx, int32_t aColIdx,
  80. int32_t* aColumnExtent)
  81. {
  82. NS_ENSURE_ARG_POINTER(aColumnExtent);
  83. *aColumnExtent = -1;
  84. if (!Intl())
  85. return NS_ERROR_FAILURE;
  86. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount() ||
  87. aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  88. return NS_ERROR_INVALID_ARG;
  89. *aColumnExtent = Intl()->ColExtentAt(aRowIdx, aColIdx);
  90. return NS_OK;
  91. }
  92. NS_IMETHODIMP
  93. xpcAccessibleTable::GetRowExtentAt(int32_t aRowIdx, int32_t aColIdx,
  94. int32_t* aRowExtent)
  95. {
  96. NS_ENSURE_ARG_POINTER(aRowExtent);
  97. *aRowExtent = -1;
  98. if (!Intl())
  99. return NS_ERROR_FAILURE;
  100. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount() ||
  101. aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  102. return NS_ERROR_INVALID_ARG;
  103. *aRowExtent = Intl()->RowExtentAt(aRowIdx, aColIdx);
  104. return NS_OK;
  105. }
  106. NS_IMETHODIMP
  107. xpcAccessibleTable::GetColumnDescription(int32_t aColIdx,
  108. nsAString& aDescription)
  109. {
  110. if (!Intl())
  111. return NS_ERROR_FAILURE;
  112. if (aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  113. return NS_ERROR_INVALID_ARG;
  114. nsAutoString description;
  115. Intl()->ColDescription(aColIdx, description);
  116. aDescription.Assign(description);
  117. return NS_OK;
  118. }
  119. NS_IMETHODIMP
  120. xpcAccessibleTable::GetRowDescription(int32_t aRowIdx, nsAString& aDescription)
  121. {
  122. if (!Intl())
  123. return NS_ERROR_FAILURE;
  124. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->ColCount())
  125. return NS_ERROR_INVALID_ARG;
  126. nsAutoString description;
  127. Intl()->RowDescription(aRowIdx, description);
  128. aDescription.Assign(description);
  129. return NS_OK;
  130. }
  131. NS_IMETHODIMP
  132. xpcAccessibleTable::IsColumnSelected(int32_t aColIdx, bool* aIsSelected)
  133. {
  134. NS_ENSURE_ARG_POINTER(aIsSelected);
  135. *aIsSelected = false;
  136. if (!Intl())
  137. return NS_ERROR_FAILURE;
  138. if (aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  139. return NS_ERROR_INVALID_ARG;
  140. *aIsSelected = Intl()->IsColSelected(aColIdx);
  141. return NS_OK;
  142. }
  143. NS_IMETHODIMP
  144. xpcAccessibleTable::IsRowSelected(int32_t aRowIdx, bool* aIsSelected)
  145. {
  146. NS_ENSURE_ARG_POINTER(aIsSelected);
  147. *aIsSelected = false;
  148. if (!Intl())
  149. return NS_ERROR_FAILURE;
  150. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount())
  151. return NS_ERROR_INVALID_ARG;
  152. *aIsSelected = Intl()->IsRowSelected(aRowIdx);
  153. return NS_OK;
  154. }
  155. NS_IMETHODIMP
  156. xpcAccessibleTable::IsCellSelected(int32_t aRowIdx, int32_t aColIdx,
  157. bool* aIsSelected)
  158. {
  159. NS_ENSURE_ARG_POINTER(aIsSelected);
  160. *aIsSelected = false;
  161. if (!Intl())
  162. return NS_ERROR_FAILURE;
  163. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount() ||
  164. aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  165. return NS_ERROR_INVALID_ARG;
  166. *aIsSelected = Intl()->IsCellSelected(aRowIdx, aColIdx);
  167. return NS_OK;
  168. }
  169. NS_IMETHODIMP
  170. xpcAccessibleTable::GetSelectedCellCount(uint32_t* aSelectedCellCount)
  171. {
  172. NS_ENSURE_ARG_POINTER(aSelectedCellCount);
  173. *aSelectedCellCount = 0;
  174. if (!Intl())
  175. return NS_ERROR_FAILURE;
  176. *aSelectedCellCount = Intl()->SelectedCellCount();
  177. return NS_OK;
  178. }
  179. NS_IMETHODIMP
  180. xpcAccessibleTable::GetSelectedColumnCount(uint32_t* aSelectedColumnCount)
  181. {
  182. NS_ENSURE_ARG_POINTER(aSelectedColumnCount);
  183. *aSelectedColumnCount = 0;
  184. if (!Intl())
  185. return NS_ERROR_FAILURE;
  186. *aSelectedColumnCount = Intl()->SelectedColCount();
  187. return NS_OK;
  188. }
  189. NS_IMETHODIMP
  190. xpcAccessibleTable::GetSelectedRowCount(uint32_t* aSelectedRowCount)
  191. {
  192. NS_ENSURE_ARG_POINTER(aSelectedRowCount);
  193. *aSelectedRowCount = 0;
  194. if (!Intl())
  195. return NS_ERROR_FAILURE;
  196. *aSelectedRowCount = Intl()->SelectedRowCount();
  197. return NS_OK;
  198. }
  199. NS_IMETHODIMP
  200. xpcAccessibleTable::GetSelectedCells(nsIArray** aSelectedCells)
  201. {
  202. NS_ENSURE_ARG_POINTER(aSelectedCells);
  203. *aSelectedCells = nullptr;
  204. if (!Intl())
  205. return NS_ERROR_FAILURE;
  206. nsresult rv = NS_OK;
  207. nsCOMPtr<nsIMutableArray> selCells =
  208. do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
  209. NS_ENSURE_SUCCESS(rv, rv);
  210. AutoTArray<Accessible*, XPC_TABLE_DEFAULT_SIZE> cellsArray;
  211. Intl()->SelectedCells(&cellsArray);
  212. uint32_t totalCount = cellsArray.Length();
  213. for (uint32_t idx = 0; idx < totalCount; idx++) {
  214. Accessible* cell = cellsArray.ElementAt(idx);
  215. selCells->AppendElement(static_cast<nsIAccessible*>(ToXPC(cell)), false);
  216. }
  217. NS_ADDREF(*aSelectedCells = selCells);
  218. return NS_OK;
  219. }
  220. NS_IMETHODIMP
  221. xpcAccessibleTable::GetSelectedCellIndices(uint32_t* aCellsArraySize,
  222. int32_t** aCellsArray)
  223. {
  224. NS_ENSURE_ARG_POINTER(aCellsArraySize);
  225. *aCellsArraySize = 0;
  226. NS_ENSURE_ARG_POINTER(aCellsArray);
  227. *aCellsArray = 0;
  228. if (!Intl())
  229. return NS_ERROR_FAILURE;
  230. AutoTArray<uint32_t, XPC_TABLE_DEFAULT_SIZE> cellsArray;
  231. Intl()->SelectedCellIndices(&cellsArray);
  232. *aCellsArraySize = cellsArray.Length();
  233. *aCellsArray = static_cast<int32_t*>
  234. (moz_xmalloc(*aCellsArraySize * sizeof(int32_t)));
  235. memcpy(*aCellsArray, cellsArray.Elements(),
  236. *aCellsArraySize * sizeof(int32_t));
  237. return NS_OK;
  238. }
  239. NS_IMETHODIMP
  240. xpcAccessibleTable::GetSelectedColumnIndices(uint32_t* aColsArraySize,
  241. int32_t** aColsArray)
  242. {
  243. NS_ENSURE_ARG_POINTER(aColsArraySize);
  244. *aColsArraySize = 0;
  245. NS_ENSURE_ARG_POINTER(aColsArray);
  246. *aColsArray = 0;
  247. if (!Intl())
  248. return NS_ERROR_FAILURE;
  249. AutoTArray<uint32_t, XPC_TABLE_DEFAULT_SIZE> colsArray;
  250. Intl()->SelectedColIndices(&colsArray);
  251. *aColsArraySize = colsArray.Length();
  252. *aColsArray = static_cast<int32_t*>
  253. (moz_xmalloc(*aColsArraySize * sizeof(int32_t)));
  254. memcpy(*aColsArray, colsArray.Elements(),
  255. *aColsArraySize * sizeof(int32_t));
  256. return NS_OK;
  257. }
  258. NS_IMETHODIMP
  259. xpcAccessibleTable::GetSelectedRowIndices(uint32_t* aRowsArraySize,
  260. int32_t** aRowsArray)
  261. {
  262. NS_ENSURE_ARG_POINTER(aRowsArraySize);
  263. *aRowsArraySize = 0;
  264. NS_ENSURE_ARG_POINTER(aRowsArray);
  265. *aRowsArray = 0;
  266. if (!Intl())
  267. return NS_ERROR_FAILURE;
  268. AutoTArray<uint32_t, XPC_TABLE_DEFAULT_SIZE> rowsArray;
  269. Intl()->SelectedRowIndices(&rowsArray);
  270. *aRowsArraySize = rowsArray.Length();
  271. *aRowsArray = static_cast<int32_t*>
  272. (moz_xmalloc(*aRowsArraySize * sizeof(int32_t)));
  273. memcpy(*aRowsArray, rowsArray.Elements(),
  274. *aRowsArraySize * sizeof(int32_t));
  275. return NS_OK;
  276. }
  277. NS_IMETHODIMP
  278. xpcAccessibleTable::GetColumnIndexAt(int32_t aCellIdx, int32_t* aColIdx)
  279. {
  280. NS_ENSURE_ARG_POINTER(aColIdx);
  281. *aColIdx = -1;
  282. if (!Intl())
  283. return NS_ERROR_FAILURE;
  284. if (aCellIdx < 0 ||
  285. static_cast<uint32_t>(aCellIdx) >= Intl()->RowCount() * Intl()->ColCount())
  286. return NS_ERROR_INVALID_ARG;
  287. *aColIdx = Intl()->ColIndexAt(aCellIdx);
  288. return NS_OK;
  289. }
  290. NS_IMETHODIMP
  291. xpcAccessibleTable::GetRowIndexAt(int32_t aCellIdx, int32_t* aRowIdx)
  292. {
  293. NS_ENSURE_ARG_POINTER(aRowIdx);
  294. *aRowIdx = -1;
  295. if (!Intl())
  296. return NS_ERROR_FAILURE;
  297. if (aCellIdx < 0 ||
  298. static_cast<uint32_t>(aCellIdx) >= Intl()->RowCount() * Intl()->ColCount())
  299. return NS_ERROR_INVALID_ARG;
  300. *aRowIdx = Intl()->RowIndexAt(aCellIdx);
  301. return NS_OK;
  302. }
  303. NS_IMETHODIMP
  304. xpcAccessibleTable::GetRowAndColumnIndicesAt(int32_t aCellIdx, int32_t* aRowIdx,
  305. int32_t* aColIdx)
  306. {
  307. NS_ENSURE_ARG_POINTER(aRowIdx);
  308. *aRowIdx = -1;
  309. NS_ENSURE_ARG_POINTER(aColIdx);
  310. *aColIdx = -1;
  311. if (!Intl())
  312. return NS_ERROR_FAILURE;
  313. if (aCellIdx < 0 ||
  314. static_cast<uint32_t>(aCellIdx) >= Intl()->RowCount() * Intl()->ColCount())
  315. return NS_ERROR_INVALID_ARG;
  316. Intl()->RowAndColIndicesAt(aCellIdx, aRowIdx, aColIdx);
  317. return NS_OK;
  318. }
  319. NS_IMETHODIMP
  320. xpcAccessibleTable::GetSummary(nsAString& aSummary)
  321. {
  322. if (!Intl())
  323. return NS_ERROR_FAILURE;
  324. nsAutoString summary;
  325. Intl()->Summary(summary);
  326. aSummary.Assign(summary);
  327. return NS_OK;
  328. }
  329. NS_IMETHODIMP
  330. xpcAccessibleTable::IsProbablyForLayout(bool* aResult)
  331. {
  332. NS_ENSURE_ARG_POINTER(aResult);
  333. *aResult = false;
  334. if (!Intl())
  335. return NS_ERROR_FAILURE;
  336. *aResult = Intl()->IsProbablyLayoutTable();
  337. return NS_OK;
  338. }
  339. NS_IMETHODIMP
  340. xpcAccessibleTable::SelectColumn(int32_t aColIdx)
  341. {
  342. if (!Intl())
  343. return NS_ERROR_FAILURE;
  344. if (aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  345. return NS_ERROR_INVALID_ARG;
  346. Intl()->SelectCol(aColIdx);
  347. return NS_OK;
  348. }
  349. NS_IMETHODIMP
  350. xpcAccessibleTable::SelectRow(int32_t aRowIdx)
  351. {
  352. if (!Intl())
  353. return NS_ERROR_FAILURE;
  354. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount())
  355. return NS_ERROR_INVALID_ARG;
  356. Intl()->SelectRow(aRowIdx);
  357. return NS_OK;
  358. }
  359. NS_IMETHODIMP
  360. xpcAccessibleTable::UnselectColumn(int32_t aColIdx)
  361. {
  362. if (!Intl())
  363. return NS_ERROR_FAILURE;
  364. if (aColIdx < 0 || static_cast<uint32_t>(aColIdx) >= Intl()->ColCount())
  365. return NS_ERROR_INVALID_ARG;
  366. Intl()->UnselectCol(aColIdx);
  367. return NS_OK;
  368. }
  369. NS_IMETHODIMP
  370. xpcAccessibleTable::UnselectRow(int32_t aRowIdx)
  371. {
  372. if (!Intl())
  373. return NS_ERROR_FAILURE;
  374. if (aRowIdx < 0 || static_cast<uint32_t>(aRowIdx) >= Intl()->RowCount())
  375. return NS_ERROR_INVALID_ARG;
  376. Intl()->UnselectRow(aRowIdx);
  377. return NS_OK;
  378. }