Refactor common wxGridCellAttrData::UpdateAttrRows/Cols code

Move two symmetrical functions into one and clean up, preparing it for
multicell support.
This commit is contained in:
Dimitri Schoolwerth
2021-01-22 22:28:08 +01:00
parent 30de99470a
commit 04a3dda5c5

View File

@@ -806,72 +806,55 @@ wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const
return attr; return attr;
} }
void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) namespace
{ {
size_t count = m_attrs.GetCount();
void UpdateCellAttrRowsOrCols(wxGridCellWithAttrArray& attrs, int editPos,
int editRowCount, int editColCount)
{
wxASSERT( !editRowCount || !editColCount );
const bool isEditingRows = (editRowCount != 0);
const int editCount = (isEditingRows ? editRowCount : editColCount);
size_t count = attrs.GetCount();
for ( size_t n = 0; n < count; n++ ) for ( size_t n = 0; n < count; n++ )
{ {
wxGridCellCoords& coords = m_attrs[n].coords; wxGridCellCoords& coords = attrs[n].coords;
wxCoord row = coords.GetRow(); const wxCoord cellRow = coords.GetRow(),
if ((size_t)row >= pos) cellCol = coords.GetCol(),
cellPos = (isEditingRows ? cellRow : cellCol);
if ( cellPos < editPos )
continue;
if ( editCount < 0 && cellPos < editPos - editCount )
{ {
if (numRows > 0) // This row/col is deleted and the cell doesn't exist any longer:
{ // Remove the attribute.
// If rows inserted, increment row counter where necessary attrs.RemoveAt(n);
coords.SetRow(row + numRows);
}
else if (numRows < 0)
{
// If rows deleted ...
if ((size_t)row >= pos - numRows)
{
// ...either decrement row counter (if row still exists)...
coords.SetRow(row + numRows);
}
else
{
// ...or remove the attribute
m_attrs.RemoveAt(n);
n--; n--;
count--; count--;
continue;
}
// Rows/cols inserted or deleted (and this cell still exists):
// Adjust cell coords.
coords.Set(cellRow + editRowCount, cellCol + editColCount);
} }
} }
}
} } // anonymous namespace
void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows )
{
UpdateCellAttrRowsOrCols(m_attrs, static_cast<int>(pos), numRows, 0);
} }
void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols )
{ {
size_t count = m_attrs.GetCount(); UpdateCellAttrRowsOrCols(m_attrs, static_cast<int>(pos), 0, numCols);
for ( size_t n = 0; n < count; n++ )
{
wxGridCellCoords& coords = m_attrs[n].coords;
wxCoord col = coords.GetCol();
if ( (size_t)col >= pos )
{
if ( numCols > 0 )
{
// If cols inserted, increment col counter where necessary
coords.SetCol(col + numCols);
}
else if (numCols < 0)
{
// If cols deleted ...
if ((size_t)col >= pos - numCols)
{
// ...either decrement col counter (if col still exists)...
coords.SetCol(col + numCols);
}
else
{
// ...or remove the attribute
m_attrs.RemoveAt(n);
n--;
count--;
}
}
}
}
} }
int wxGridCellAttrData::FindIndex(int row, int col) const int wxGridCellAttrData::FindIndex(int row, int col) const