michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "xpcAccessibleTable.h" michael@0: michael@0: #include "Accessible.h" michael@0: #include "TableAccessible.h" michael@0: michael@0: #include "nsIMutableArray.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: michael@0: using namespace mozilla::a11y; michael@0: michael@0: static const uint32_t XPC_TABLE_DEFAULT_SIZE = 40; michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetCaption(nsIAccessible** aCaption) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCaption); michael@0: *aCaption = nullptr; michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: NS_IF_ADDREF(*aCaption = mTable->Caption()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetColumnCount(int32_t* aColumnCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aColumnCount); michael@0: *aColumnCount = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *aColumnCount = mTable->ColCount(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetRowCount(int32_t* aRowCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aRowCount); michael@0: *aRowCount = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *aRowCount = mTable->RowCount(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetCellAt(int32_t aRowIdx, int32_t aColIdx, michael@0: nsIAccessible** aCell) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCell); michael@0: *aCell = nullptr; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount() || michael@0: aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: NS_IF_ADDREF(*aCell = mTable->CellAt(aRowIdx, aColIdx)); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetCellIndexAt(int32_t aRowIdx, int32_t aColIdx, michael@0: int32_t* aCellIdx) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCellIdx); michael@0: *aCellIdx = -1; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount() || michael@0: aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aCellIdx = mTable->CellIndexAt(aRowIdx, aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetColumnExtentAt(int32_t aRowIdx, int32_t aColIdx, michael@0: int32_t* aColumnExtent) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aColumnExtent); michael@0: *aColumnExtent = -1; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount() || michael@0: aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aColumnExtent = mTable->ColExtentAt(aRowIdx, aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetRowExtentAt(int32_t aRowIdx, int32_t aColIdx, michael@0: int32_t* aRowExtent) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aRowExtent); michael@0: *aRowExtent = -1; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount() || michael@0: aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aRowExtent = mTable->RowExtentAt(aRowIdx, aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetColumnDescription(int32_t aColIdx, michael@0: nsAString& aDescription) michael@0: { michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsAutoString description; michael@0: mTable->ColDescription(aColIdx, description); michael@0: aDescription.Assign(description); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetRowDescription(int32_t aRowIdx, nsAString& aDescription) michael@0: { michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: nsAutoString description; michael@0: mTable->RowDescription(aRowIdx, description); michael@0: aDescription.Assign(description); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::IsColumnSelected(int32_t aColIdx, bool* aIsSelected) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aIsSelected); michael@0: *aIsSelected = false; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aIsSelected = mTable->IsColSelected(aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::IsRowSelected(int32_t aRowIdx, bool* aIsSelected) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aIsSelected); michael@0: *aIsSelected = false; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aIsSelected = mTable->IsRowSelected(aRowIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::IsCellSelected(int32_t aRowIdx, int32_t aColIdx, michael@0: bool* aIsSelected) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aIsSelected); michael@0: *aIsSelected = false; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount() || michael@0: aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aIsSelected = mTable->IsCellSelected(aRowIdx, aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSelectedCellCount(uint32_t* aSelectedCellCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aSelectedCellCount); michael@0: *aSelectedCellCount = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *aSelectedCellCount = mTable->SelectedCellCount(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSelectedColumnCount(uint32_t* aSelectedColumnCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aSelectedColumnCount); michael@0: *aSelectedColumnCount = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *aSelectedColumnCount = mTable->SelectedColCount(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSelectedRowCount(uint32_t* aSelectedRowCount) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aSelectedRowCount); michael@0: *aSelectedRowCount = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *aSelectedRowCount = mTable->SelectedRowCount(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSelectedCells(nsIArray** aSelectedCells) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aSelectedCells); michael@0: *aSelectedCells = nullptr; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsresult rv = NS_OK; michael@0: nsCOMPtr selCells = michael@0: do_CreateInstance(NS_ARRAY_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsAutoTArray cellsArray; michael@0: mTable->SelectedCells(&cellsArray); michael@0: michael@0: uint32_t totalCount = cellsArray.Length(); michael@0: for (uint32_t idx = 0; idx < totalCount; idx++) { michael@0: Accessible* cell = cellsArray.ElementAt(idx); michael@0: selCells -> AppendElement(static_cast(cell), false); michael@0: } michael@0: michael@0: NS_ADDREF(*aSelectedCells = selCells); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSelectedCellIndices(uint32_t* aCellsArraySize, michael@0: int32_t** aCellsArray) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aCellsArraySize); michael@0: *aCellsArraySize = 0; michael@0: michael@0: NS_ENSURE_ARG_POINTER(aCellsArray); michael@0: *aCellsArray = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsAutoTArray cellsArray; michael@0: mTable->SelectedCellIndices(&cellsArray); michael@0: michael@0: *aCellsArraySize = cellsArray.Length(); michael@0: *aCellsArray = static_cast michael@0: (moz_xmalloc(*aCellsArraySize * sizeof(int32_t))); michael@0: memcpy(*aCellsArray, cellsArray.Elements(), michael@0: *aCellsArraySize * sizeof(int32_t)); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSelectedColumnIndices(uint32_t* aColsArraySize, michael@0: int32_t** aColsArray) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aColsArraySize); michael@0: *aColsArraySize = 0; michael@0: michael@0: NS_ENSURE_ARG_POINTER(aColsArray); michael@0: *aColsArray = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsAutoTArray colsArray; michael@0: mTable->SelectedColIndices(&colsArray); michael@0: michael@0: *aColsArraySize = colsArray.Length(); michael@0: *aColsArray = static_cast michael@0: (moz_xmalloc(*aColsArraySize * sizeof(int32_t))); michael@0: memcpy(*aColsArray, colsArray.Elements(), michael@0: *aColsArraySize * sizeof(int32_t)); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSelectedRowIndices(uint32_t* aRowsArraySize, michael@0: int32_t** aRowsArray) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aRowsArraySize); michael@0: *aRowsArraySize = 0; michael@0: michael@0: NS_ENSURE_ARG_POINTER(aRowsArray); michael@0: *aRowsArray = 0; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsAutoTArray rowsArray; michael@0: mTable->SelectedRowIndices(&rowsArray); michael@0: michael@0: *aRowsArraySize = rowsArray.Length(); michael@0: *aRowsArray = static_cast michael@0: (moz_xmalloc(*aRowsArraySize * sizeof(int32_t))); michael@0: memcpy(*aRowsArray, rowsArray.Elements(), michael@0: *aRowsArraySize * sizeof(int32_t)); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetColumnIndexAt(int32_t aCellIdx, int32_t* aColIdx) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aColIdx); michael@0: *aColIdx = -1; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aCellIdx < 0 michael@0: || static_cast(aCellIdx) michael@0: >= mTable->RowCount() * mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aColIdx = mTable->ColIndexAt(aCellIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetRowIndexAt(int32_t aCellIdx, int32_t* aRowIdx) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aRowIdx); michael@0: *aRowIdx = -1; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aCellIdx < 0 michael@0: || static_cast(aCellIdx) michael@0: >= mTable->RowCount() * mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: *aRowIdx = mTable->RowIndexAt(aCellIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetRowAndColumnIndicesAt(int32_t aCellIdx, int32_t* aRowIdx, michael@0: int32_t* aColIdx) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aRowIdx); michael@0: *aRowIdx = -1; michael@0: NS_ENSURE_ARG_POINTER(aColIdx); michael@0: *aColIdx = -1; michael@0: michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aCellIdx < 0 michael@0: || static_cast(aCellIdx) michael@0: >= mTable->RowCount() * mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: mTable->RowAndColIndicesAt(aCellIdx, aRowIdx, aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::GetSummary(nsAString& aSummary) michael@0: { michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: nsAutoString summary; michael@0: mTable->Summary(summary); michael@0: aSummary.Assign(summary); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::IsProbablyForLayout(bool* aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: *aResult = false; michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: *aResult = mTable->IsProbablyLayoutTable(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::SelectColumn(int32_t aColIdx) michael@0: { michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: mTable->SelectCol(aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::SelectRow(int32_t aRowIdx) michael@0: { michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: mTable->SelectRow(aRowIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::UnselectColumn(int32_t aColIdx) michael@0: { michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aColIdx < 0 || static_cast(aColIdx) >= mTable->ColCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: mTable->UnselectCol(aColIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: xpcAccessibleTable::UnselectRow(int32_t aRowIdx) michael@0: { michael@0: if (!mTable) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (aRowIdx < 0 || static_cast(aRowIdx) >= mTable->RowCount()) michael@0: return NS_ERROR_INVALID_ARG; michael@0: michael@0: mTable->UnselectRow(aRowIdx); michael@0: return NS_OK; michael@0: } michael@0: michael@0: