Bug 1009776 - part 4, [css-grid] Add a cell map to keep track of which grid cells are occupied. r=dholbert

This commit is contained in:
Mats Palmgren 2015-03-18 09:02:32 +00:00
parent 4cd97099d0
commit fe1d940300
2 changed files with 75 additions and 0 deletions

View File

@ -502,7 +502,56 @@ nsGridContainerFrame::GetFrameName(nsAString& aResult) const
}
#endif
void
nsGridContainerFrame::CellMap::Fill(const GridArea& aGridArea)
{
MOZ_ASSERT(aGridArea.IsDefinite());
MOZ_ASSERT(aGridArea.mRows.mStart < aGridArea.mRows.mEnd);
MOZ_ASSERT(aGridArea.mRows.mStart > 0);
MOZ_ASSERT(aGridArea.mCols.mStart < aGridArea.mCols.mEnd);
MOZ_ASSERT(aGridArea.mCols.mStart > 0);
// Line numbers are 1-based so convert them to a zero-based index.
const auto numRows = aGridArea.mRows.mEnd - 1;
const auto numCols = aGridArea.mCols.mEnd - 1;
mCells.EnsureLengthAtLeast(numRows);
for (auto i = aGridArea.mRows.mStart - 1; i < numRows; ++i) {
nsTArray<Cell>& cellsInRow = mCells[i];
cellsInRow.EnsureLengthAtLeast(numCols);
for (auto j = aGridArea.mCols.mStart - 1; j < numCols; ++j) {
cellsInRow[j].mIsOccupied = true;
}
}
}
void
nsGridContainerFrame::CellMap::ClearOccupied()
{
const size_t numRows = mCells.Length();
for (size_t i = 0; i < numRows; ++i) {
nsTArray<Cell>& cellsInRow = mCells[i];
const size_t numCols = cellsInRow.Length();
for (size_t j = 0; j < numCols; ++j) {
cellsInRow[j].mIsOccupied = false;
}
}
}
#ifdef DEBUG
void
nsGridContainerFrame::CellMap::Dump() const
{
const size_t numRows = mCells.Length();
for (size_t i = 0; i < numRows; ++i) {
const nsTArray<Cell>& cellsInRow = mCells[i];
const size_t numCols = cellsInRow.Length();
printf("%lu:\t", (unsigned long)i + 1);
for (size_t j = 0; j < numCols; ++j) {
printf(cellsInRow[j].mIsOccupied ? "X " : ". ");
}
printf("\n");
}
}
static bool
FrameWantsToBeInAnonymousGridItem(nsIFrame* aFrame)
{

View File

@ -82,6 +82,27 @@ protected:
LineRange mRows;
};
/**
* A CellMap holds state for each cell in the grid.
* It's row major. It's sparse in the sense that it only has enough rows to
* cover the last row that has a grid item. Each row only has enough entries
* to cover columns that are occupied *on that row*, i.e. it's not a full
* matrix covering the entire implicit grid. An absent Cell means that it's
* unoccupied by any grid item.
*/
struct CellMap {
struct Cell {
Cell() : mIsOccupied(false) {}
bool mIsOccupied : 1;
};
void Fill(const GridArea& aGridArea);
void ClearOccupied();
#if DEBUG
void Dump() const;
#endif
nsTArray<nsTArray<Cell>> mCells;
};
enum LineRangeSide {
eLineRangeSideStart, eLineRangeSideEnd
};
@ -217,6 +238,11 @@ protected:
#endif // DEBUG
private:
/**
* State for each cell in the grid.
*/
CellMap mCellMap;
/**
* The last column grid line (1-based) in the explicit grid.
* (i.e. the number of explicit columns + 1)