don't crash trying to dereference NULL m_selection (happens when showing a grid without any rows or columns)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15891 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2002-06-20 15:01:10 +00:00
parent bd9cd5343b
commit 3f3dc2efd0

View File

@@ -3604,7 +3604,8 @@ void wxGrid::Create()
// create the type registry // create the type registry
m_typeRegistry = new wxGridTypeRegistry; m_typeRegistry = new wxGridTypeRegistry;
m_selection = 0; m_selection = NULL;
// subwindow components that make up the wxGrid // subwindow components that make up the wxGrid
m_cornerLabelWin = new wxGridCornerLabelWindow( this, m_cornerLabelWin = new wxGridCornerLabelWindow( this,
-1, -1,
@@ -3671,8 +3672,8 @@ bool wxGrid::SetTable( wxGridTableBase *table, bool takeOwnership,
{ {
// RD: Actually, this should probably be allowed. I think it would be // RD: Actually, this should probably be allowed. I think it would be
// nice to be able to switch multiple Tables in and out of a single // nice to be able to switch multiple Tables in and out of a single
// View at runtime. Is there anything in the implmentation that would // View at runtime. Is there anything in the implementation that
// prevent this? // would prevent this?
// At least, you now have to cope with m_selection // At least, you now have to cope with m_selection
wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") ); wxFAIL_MSG( wxT("wxGrid::CreateGrid or wxGrid::SetTable called more than once") );
@@ -3977,6 +3978,8 @@ bool wxGrid::Redimension( wxGridTableMessage& msg )
// //
SetCurrentCell( 0, 0 ); SetCurrentCell( 0, 0 );
} }
if ( m_selection )
m_selection->UpdateRows( pos, numRows ); m_selection->UpdateRows( pos, numRows );
wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
if (attrProvider) if (attrProvider)
@@ -4060,6 +4063,8 @@ bool wxGrid::Redimension( wxGridTableMessage& msg )
if ( m_currentCellCoords.GetRow() >= m_numRows ) if ( m_currentCellCoords.GetRow() >= m_numRows )
m_currentCellCoords.Set( 0, 0 ); m_currentCellCoords.Set( 0, 0 );
} }
if ( m_selection )
m_selection->UpdateRows( pos, -((int)numRows) ); m_selection->UpdateRows( pos, -((int)numRows) );
wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
if (attrProvider) { if (attrProvider) {
@@ -4114,6 +4119,8 @@ bool wxGrid::Redimension( wxGridTableMessage& msg )
// //
SetCurrentCell( 0, 0 ); SetCurrentCell( 0, 0 );
} }
if ( m_selection )
m_selection->UpdateCols( pos, numCols ); m_selection->UpdateCols( pos, numCols );
wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
if (attrProvider) if (attrProvider)
@@ -4196,6 +4203,8 @@ bool wxGrid::Redimension( wxGridTableMessage& msg )
if ( m_currentCellCoords.GetCol() >= m_numCols ) if ( m_currentCellCoords.GetCol() >= m_numCols )
m_currentCellCoords.Set( 0, 0 ); m_currentCellCoords.Set( 0, 0 );
} }
if ( m_selection )
m_selection->UpdateCols( pos, -((int)numCols) ); m_selection->UpdateCols( pos, -((int)numCols) );
wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider(); wxGridCellAttrProvider * attrProvider = m_table->GetAttrProvider();
if (attrProvider) { if (attrProvider) {
@@ -4430,6 +4439,8 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
case WXGRID_CURSOR_SELECT_ROW: case WXGRID_CURSOR_SELECT_ROW:
if ( (row = YToRow( y )) >= 0 ) if ( (row = YToRow( y )) >= 0 )
{
if ( m_selection )
{ {
m_selection->SelectRow( row, m_selection->SelectRow( row,
event.ControlDown(), event.ControlDown(),
@@ -4437,6 +4448,7 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
} }
}
// default label to suppress warnings about "enumeration value // default label to suppress warnings about "enumeration value
// 'xxx' not handled in switch // 'xxx' not handled in switch
@@ -4480,7 +4492,10 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
{ {
if ( !event.ShiftDown() && !event.ControlDown() ) if ( !event.ShiftDown() && !event.ControlDown() )
ClearSelection(); ClearSelection();
else if ( m_selection )
{
if ( event.ShiftDown() ) if ( event.ShiftDown() )
{
m_selection->SelectBlock( m_currentCellCoords.GetRow(), m_selection->SelectBlock( m_currentCellCoords.GetRow(),
0, 0,
row, row,
@@ -4489,12 +4504,17 @@ void wxGrid::ProcessRowLabelMouseEvent( wxMouseEvent& event )
event.ShiftDown(), event.ShiftDown(),
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
else else
{
m_selection->SelectRow( row, m_selection->SelectRow( row,
event.ControlDown(), event.ControlDown(),
event.ShiftDown(), event.ShiftDown(),
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
}
ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin); ChangeCursorMode(WXGRID_CURSOR_SELECT_ROW, m_rowLabelWin);
} }
} }
@@ -4626,6 +4646,8 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
case WXGRID_CURSOR_SELECT_COL: case WXGRID_CURSOR_SELECT_COL:
if ( (col = XToCol( x )) >= 0 ) if ( (col = XToCol( x )) >= 0 )
{
if ( m_selection )
{ {
m_selection->SelectCol( col, m_selection->SelectCol( col,
event.ControlDown(), event.ControlDown(),
@@ -4633,6 +4655,7 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
} }
}
// default label to suppress warnings about "enumeration value // default label to suppress warnings about "enumeration value
// 'xxx' not handled in switch // 'xxx' not handled in switch
@@ -4676,7 +4699,10 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
{ {
if ( !event.ShiftDown() && !event.ControlDown() ) if ( !event.ShiftDown() && !event.ControlDown() )
ClearSelection(); ClearSelection();
if ( m_selection )
{
if ( event.ShiftDown() ) if ( event.ShiftDown() )
{
m_selection->SelectBlock( 0, m_selection->SelectBlock( 0,
m_currentCellCoords.GetCol(), m_currentCellCoords.GetCol(),
GetNumberRows() - 1, col, GetNumberRows() - 1, col,
@@ -4684,12 +4710,17 @@ void wxGrid::ProcessColLabelMouseEvent( wxMouseEvent& event )
event.ShiftDown(), event.ShiftDown(),
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
else else
{
m_selection->SelectCol( col, m_selection->SelectCol( col,
event.ControlDown(), event.ControlDown(),
event.ShiftDown(), event.ShiftDown(),
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
}
ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin); ChangeCursorMode(WXGRID_CURSOR_SELECT_COL, m_colLabelWin);
} }
} }
@@ -5020,6 +5051,8 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
if ( !event.ControlDown() ) if ( !event.ControlDown() )
ClearSelection(); ClearSelection();
if ( event.ShiftDown() ) if ( event.ShiftDown() )
{
if ( m_selection )
{ {
m_selection->SelectBlock( m_currentCellCoords.GetRow(), m_selection->SelectBlock( m_currentCellCoords.GetRow(),
m_currentCellCoords.GetCol(), m_currentCellCoords.GetCol(),
@@ -5030,6 +5063,7 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
} }
}
else if ( XToEdgeOfCol(x) < 0 && else if ( XToEdgeOfCol(x) < 0 &&
YToEdgeOfRow(y) < 0 ) YToEdgeOfRow(y) < 0 )
{ {
@@ -5057,6 +5091,8 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
else else
{ {
if ( event.ControlDown() ) if ( event.ControlDown() )
{
if ( m_selection )
{ {
m_selection->ToggleCellSelection( coords.GetRow(), m_selection->ToggleCellSelection( coords.GetRow(),
coords.GetCol(), coords.GetCol(),
@@ -5064,6 +5100,7 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
event.ShiftDown(), event.ShiftDown(),
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
m_selectingTopLeft = wxGridNoCellCoords; m_selectingTopLeft = wxGridNoCellCoords;
m_selectingBottomRight = wxGridNoCellCoords; m_selectingBottomRight = wxGridNoCellCoords;
m_selectingKeyboard = coords; m_selectingKeyboard = coords;
@@ -5071,10 +5108,15 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
else else
{ {
SetCurrentCell( coords ); SetCurrentCell( coords );
if ( m_selection->GetSelectionMode() if ( m_selection )
!= wxGrid::wxGridSelectCells) {
if ( m_selection->GetSelectionMode() !=
wxGrid::wxGridSelectCells )
{
HighlightBlock( coords, coords ); HighlightBlock( coords, coords );
} }
}
}
m_waitForSlowClick = TRUE; m_waitForSlowClick = TRUE;
} }
} }
@@ -5112,6 +5154,9 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
m_winCapture->ReleaseMouse(); m_winCapture->ReleaseMouse();
m_winCapture = NULL; m_winCapture = NULL;
} }
if ( m_selection )
{
m_selection->SelectBlock( m_selectingTopLeft.GetRow(), m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
m_selectingTopLeft.GetCol(), m_selectingTopLeft.GetCol(),
m_selectingBottomRight.GetRow(), m_selectingBottomRight.GetRow(),
@@ -5120,6 +5165,8 @@ void wxGrid::ProcessGridCellMouseEvent( wxMouseEvent& event )
event.ShiftDown(), event.ShiftDown(),
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
m_selectingTopLeft = wxGridNoCellCoords; m_selectingTopLeft = wxGridNoCellCoords;
m_selectingBottomRight = wxGridNoCellCoords; m_selectingBottomRight = wxGridNoCellCoords;
} }
@@ -5774,6 +5821,8 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
case WXK_SPACE: case WXK_SPACE:
if ( event.ControlDown() ) if ( event.ControlDown() )
{
if ( m_selection )
{ {
m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(), m_selection->ToggleCellSelection( m_currentCellCoords.GetRow(),
m_currentCellCoords.GetCol(), m_currentCellCoords.GetCol(),
@@ -5781,6 +5830,7 @@ void wxGrid::OnKeyDown( wxKeyEvent& event )
event.ShiftDown(), event.ShiftDown(),
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
break; break;
} }
if ( !IsEditable() ) if ( !IsEditable() )
@@ -5844,6 +5894,9 @@ void wxGrid::OnKeyUp( wxKeyEvent& event )
{ {
if ( m_selectingTopLeft != wxGridNoCellCoords && if ( m_selectingTopLeft != wxGridNoCellCoords &&
m_selectingBottomRight != wxGridNoCellCoords ) m_selectingBottomRight != wxGridNoCellCoords )
{
if ( m_selection )
{
m_selection->SelectBlock( m_selectingTopLeft.GetRow(), m_selection->SelectBlock( m_selectingTopLeft.GetRow(),
m_selectingTopLeft.GetCol(), m_selectingTopLeft.GetCol(),
m_selectingBottomRight.GetRow(), m_selectingBottomRight.GetRow(),
@@ -5852,6 +5905,9 @@ void wxGrid::OnKeyUp( wxKeyEvent& event )
TRUE, TRUE,
event.AltDown(), event.AltDown(),
event.MetaDown() ); event.MetaDown() );
}
}
m_selectingTopLeft = wxGridNoCellCoords; m_selectingTopLeft = wxGridNoCellCoords;
m_selectingBottomRight = wxGridNoCellCoords; m_selectingBottomRight = wxGridNoCellCoords;
m_selectingKeyboard = wxGridNoCellCoords; m_selectingKeyboard = wxGridNoCellCoords;
@@ -5913,6 +5969,8 @@ void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCo
int temp; int temp;
wxGridCellCoords updateTopLeft, updateBottomRight; wxGridCellCoords updateTopLeft, updateBottomRight;
if ( m_selection )
{
if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
{ {
leftCol = 0; leftCol = 0;
@@ -5923,6 +5981,8 @@ void wxGrid::HighlightBlock( int topRow, int leftCol, int bottomRow, int rightCo
topRow = 0; topRow = 0;
bottomRow = GetNumberRows() - 1; bottomRow = GetNumberRows() - 1;
} }
}
if ( topRow > bottomRow ) if ( topRow > bottomRow )
{ {
temp = topRow; temp = topRow;
@@ -8761,6 +8821,7 @@ void wxGrid::SelectRow( int row, bool addToSelected )
if ( IsSelection() && !addToSelected ) if ( IsSelection() && !addToSelected )
ClearSelection(); ClearSelection();
if ( m_selection )
m_selection->SelectRow( row, FALSE, addToSelected ); m_selection->SelectRow( row, FALSE, addToSelected );
} }
@@ -8770,6 +8831,7 @@ void wxGrid::SelectCol( int col, bool addToSelected )
if ( IsSelection() && !addToSelected ) if ( IsSelection() && !addToSelected )
ClearSelection(); ClearSelection();
if ( m_selection )
m_selection->SelectCol( col, FALSE, addToSelected ); m_selection->SelectCol( col, FALSE, addToSelected );
} }
@@ -8780,6 +8842,7 @@ void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
if ( IsSelection() && !addToSelected ) if ( IsSelection() && !addToSelected )
ClearSelection(); ClearSelection();
if ( m_selection )
m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol, m_selection->SelectBlock( topRow, leftCol, bottomRow, rightCol,
FALSE, addToSelected ); FALSE, addToSelected );
} }
@@ -8788,8 +8851,11 @@ void wxGrid::SelectBlock( int topRow, int leftCol, int bottomRow, int rightCol,
void wxGrid::SelectAll() void wxGrid::SelectAll()
{ {
if ( m_numRows > 0 && m_numCols > 0 ) if ( m_numRows > 0 && m_numCols > 0 )
{
if ( m_selection )
m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 ); m_selection->SelectBlock( 0, 0, m_numRows-1, m_numCols-1 );
} }
}
// //
// ------ Cell, row and col deselection // ------ Cell, row and col deselection
@@ -8797,6 +8863,9 @@ void wxGrid::SelectAll()
void wxGrid::DeselectRow( int row ) void wxGrid::DeselectRow( int row )
{ {
if ( !m_selection )
return;
if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows ) if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectRows )
{ {
if ( m_selection->IsInSelection(row, 0 ) ) if ( m_selection->IsInSelection(row, 0 ) )
@@ -8815,6 +8884,9 @@ void wxGrid::DeselectRow( int row )
void wxGrid::DeselectCol( int col ) void wxGrid::DeselectCol( int col )
{ {
if ( !m_selection )
return;
if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns ) if ( m_selection->GetSelectionMode() == wxGrid::wxGridSelectColumns )
{ {
if ( m_selection->IsInSelection(0, col ) ) if ( m_selection->IsInSelection(0, col ) )
@@ -8833,30 +8905,31 @@ void wxGrid::DeselectCol( int col )
void wxGrid::DeselectCell( int row, int col ) void wxGrid::DeselectCell( int row, int col )
{ {
if ( m_selection->IsInSelection(row, col) ) if ( m_selection && m_selection->IsInSelection(row, col) )
m_selection->ToggleCellSelection(row, col); m_selection->ToggleCellSelection(row, col);
} }
bool wxGrid::IsSelection() bool wxGrid::IsSelection()
{ {
return ( m_selection->IsSelection() || return ( m_selection && (m_selection->IsSelection() ||
( m_selectingTopLeft != wxGridNoCellCoords && ( m_selectingTopLeft != wxGridNoCellCoords &&
m_selectingBottomRight != wxGridNoCellCoords ) ); m_selectingBottomRight != wxGridNoCellCoords) ) );
} }
bool wxGrid::IsInSelection( int row, int col ) bool wxGrid::IsInSelection( int row, int col )
{ {
return ( m_selection->IsInSelection( row, col ) || return ( m_selection && (m_selection->IsInSelection( row, col ) ||
( row >= m_selectingTopLeft.GetRow() && ( row >= m_selectingTopLeft.GetRow() &&
col >= m_selectingTopLeft.GetCol() && col >= m_selectingTopLeft.GetCol() &&
row <= m_selectingBottomRight.GetRow() && row <= m_selectingBottomRight.GetRow() &&
col <= m_selectingBottomRight.GetCol() ) ); col <= m_selectingBottomRight.GetCol() )) );
} }
void wxGrid::ClearSelection() void wxGrid::ClearSelection()
{ {
m_selectingTopLeft = wxGridNoCellCoords; m_selectingTopLeft = wxGridNoCellCoords;
m_selectingBottomRight = wxGridNoCellCoords; m_selectingBottomRight = wxGridNoCellCoords;
if ( m_selection )
m_selection->ClearSelection(); m_selection->ClearSelection();
} }