Simplify wxGrid::SetSelectionMode() selection updating logic
Don't try to extend the existing selected blocks to rows/columns, this contradicts the documented behaviour which is to discard the selected blocks that become invalid in the new mode. Do handle switching to wxGridSelectRowsOrColumns mode, as there doesn't seem to be any reason not to. Update the tests to check for the expected selection update behaviour.
This commit is contained in:
@@ -81,8 +81,6 @@ void wxGridSelection::SetSelectionMode( wxGrid::wxGridSelectionModes selmode )
|
|||||||
if (selmode == m_selectionMode)
|
if (selmode == m_selectionMode)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// TODO: wxGridSelectRowsOrColumns?
|
|
||||||
|
|
||||||
if ( m_selectionMode != wxGrid::wxGridSelectCells )
|
if ( m_selectionMode != wxGrid::wxGridSelectCells )
|
||||||
{
|
{
|
||||||
// if changing form row to column selection
|
// if changing form row to column selection
|
||||||
@@ -94,6 +92,11 @@ void wxGridSelection::SetSelectionMode( wxGrid::wxGridSelectionModes selmode )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
// Preserve only fully selected rows/columns when switching from cell
|
||||||
|
// selection mode and discard the selected blocks that are invalid in
|
||||||
|
// the new selection mode.
|
||||||
|
const int lastCol = m_grid->GetNumberCols() - 1;
|
||||||
|
const int lastRow = m_grid->GetNumberRows() - 1;
|
||||||
for ( size_t n = m_selection.size(); n > 0; )
|
for ( size_t n = m_selection.size(); n > 0; )
|
||||||
{
|
{
|
||||||
n--;
|
n--;
|
||||||
@@ -103,26 +106,29 @@ void wxGridSelection::SetSelectionMode( wxGrid::wxGridSelectionModes selmode )
|
|||||||
const int bottomRow = block.GetBottomRow();
|
const int bottomRow = block.GetBottomRow();
|
||||||
const int rightCol = block.GetRightCol();
|
const int rightCol = block.GetRightCol();
|
||||||
|
|
||||||
if (selmode == wxGrid::wxGridSelectRows)
|
bool valid = false;
|
||||||
|
switch ( selmode )
|
||||||
{
|
{
|
||||||
if (leftCol != 0 || rightCol != m_grid->GetNumberCols() - 1 )
|
case wxGrid::wxGridSelectCells:
|
||||||
{
|
wxFAIL_MSG("unreachable");
|
||||||
m_selection.erase(m_selection.begin() + n);
|
break;
|
||||||
SelectBlockNoEvent(
|
|
||||||
wxGridBlockCoords(topRow, 0,
|
case wxGrid::wxGridSelectRows:
|
||||||
bottomRow, m_grid->GetNumberCols() - 1));
|
valid = leftCol == 0 && rightCol == lastCol;
|
||||||
}
|
break;
|
||||||
}
|
|
||||||
else // selmode == wxGridSelectColumns)
|
case wxGrid::wxGridSelectColumns:
|
||||||
{
|
valid = topRow == 0 && bottomRow == lastRow;
|
||||||
if (topRow != 0 || bottomRow != m_grid->GetNumberRows() - 1 )
|
break;
|
||||||
{
|
|
||||||
m_selection.erase(m_selection.begin() + n);
|
case wxGrid::wxGridSelectRowsOrColumns:
|
||||||
SelectBlockNoEvent(
|
valid = (leftCol == 0 && rightCol == lastCol) ||
|
||||||
wxGridBlockCoords(0, leftCol,
|
(topRow == 0 && bottomRow == lastRow);
|
||||||
m_grid->GetNumberRows() - 1, rightCol));
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( !valid )
|
||||||
|
m_selection.erase(m_selection.begin() + n);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_selectionMode = selmode;
|
m_selectionMode = selmode;
|
||||||
|
@@ -862,9 +862,20 @@ TEST_CASE_METHOD(GridTestCase, "Grid::SelectionMode", "[grid]")
|
|||||||
//We already test this mode in Select
|
//We already test this mode in Select
|
||||||
CHECK(m_grid->GetSelectionMode() == wxGrid::wxGridSelectCells);
|
CHECK(m_grid->GetSelectionMode() == wxGrid::wxGridSelectCells);
|
||||||
|
|
||||||
|
// Select an individual cell and an entire row.
|
||||||
|
m_grid->SelectBlock(3, 1, 3, 1);
|
||||||
|
m_grid->SelectRow(5, true /* add to selection */);
|
||||||
|
|
||||||
|
// Test that after switching to row selection mode only the row remains
|
||||||
|
// selected.
|
||||||
|
m_grid->SetSelectionMode(wxGrid::wxGridSelectRows);
|
||||||
|
CHECK( m_grid->IsInSelection(5, 0) );
|
||||||
|
CHECK( m_grid->IsInSelection(5, 1) );
|
||||||
|
CHECK( !m_grid->IsInSelection(3, 1) );
|
||||||
|
|
||||||
//Test row selection be selecting a single cell and checking the whole
|
//Test row selection be selecting a single cell and checking the whole
|
||||||
//row is selected
|
//row is selected
|
||||||
m_grid->SetSelectionMode(wxGrid::wxGridSelectRows);
|
m_grid->ClearSelection();
|
||||||
m_grid->SelectBlock(3, 1, 3, 1);
|
m_grid->SelectBlock(3, 1, 3, 1);
|
||||||
|
|
||||||
wxArrayInt selectedRows = m_grid->GetSelectedRows();
|
wxArrayInt selectedRows = m_grid->GetSelectedRows();
|
||||||
|
Reference in New Issue
Block a user