Add wxGrid::RefreshBlock() helper

This simple function combines BlockToDeviceRect() and
wxWindow::Refresh() calls and allows to avoid the ugly casts in
wxGridSelection code as well as making the code slightly shorter and
more clear.

No real changes.
This commit is contained in:
lucian-rotariu
2019-07-11 01:13:54 +02:00
committed by Vadim Zeitlin
parent cf3709147a
commit 00224e3f30
3 changed files with 40 additions and 78 deletions

View File

@@ -1112,6 +1112,11 @@ public:
const wxArrayString& lines, const wxArrayString& lines,
long *width, long *height ) const; long *width, long *height ) const;
void RefreshBlock(const wxGridCellCoords& topLeft,
const wxGridCellCoords& bottomRight);
void RefreshBlock(int topRow, int leftCol,
int bottomRow, int rightCol);
// ------ // ------
// Code that does a lot of grid modification can be enclosed // Code that does a lot of grid modification can be enclosed

View File

@@ -4913,6 +4913,22 @@ void wxGrid::Refresh(bool eraseb, const wxRect* rect)
} }
} }
void wxGrid::RefreshBlock(const wxGridCellCoords& topLeft,
const wxGridCellCoords& bottomRight)
{
RefreshBlock(topLeft.GetRow(), topLeft.GetCol(),
bottomRight.GetRow(), bottomRight.GetCol());
}
void wxGrid::RefreshBlock(int topRow, int leftCol,
int bottomRow, int rightCol)
{
const wxRect rect = BlockToDeviceRect(wxGridCellCoords(topRow, leftCol),
wxGridCellCoords(bottomRow, rightCol));
if ( !rect.IsEmpty() )
m_gridWin->Refresh(false, &rect);
}
void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event)) void wxGrid::OnSize(wxSizeEvent& WXUNUSED(event))
{ {
if (m_targetWindow != this) // check whether initialisation has been done if (m_targetWindow != this) // check whether initialisation has been done
@@ -5317,10 +5333,7 @@ wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
if ( m_selectedBlockTopLeft == wxGridNoCellCoords || if ( m_selectedBlockTopLeft == wxGridNoCellCoords ||
m_selectedBlockBottomRight == wxGridNoCellCoords ) m_selectedBlockBottomRight == wxGridNoCellCoords )
{ {
wxRect rect; RefreshBlock(topRow, leftCol, bottomRow, rightCol);
rect = BlockToDeviceRect( wxGridCellCoords ( topRow, leftCol ),
wxGridCellCoords ( bottomRow, rightCol ) );
m_gridWin->Refresh( false, &rect );
} }
// Now handle changing an existing selection area. // Now handle changing an existing selection area.
@@ -5330,13 +5343,6 @@ wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
// Compute two optimal update rectangles: // Compute two optimal update rectangles:
// Either one rectangle is a real subset of the // Either one rectangle is a real subset of the
// other, or they are (almost) disjoint! // other, or they are (almost) disjoint!
wxRect rect[4];
bool need_refresh[4];
need_refresh[0] =
need_refresh[1] =
need_refresh[2] =
need_refresh[3] = false;
int i;
// Store intermediate values // Store intermediate values
wxCoord oldLeft = m_selectedBlockTopLeft.GetCol(); wxCoord oldLeft = m_selectedBlockTopLeft.GetCol();
@@ -5358,46 +5364,29 @@ wxGrid::UpdateBlockBeingSelected(int topRow, int leftCol,
{ {
// Refresh the newly selected or deselected // Refresh the newly selected or deselected
// area to the left of the old or new selection. // area to the left of the old or new selection.
need_refresh[0] = true; RefreshBlock(oldTop, oldLeft, oldBottom, leftCol - 1);
rect[0] = BlockToDeviceRect(
wxGridCellCoords( oldTop, oldLeft ),
wxGridCellCoords( oldBottom, leftCol - 1 ) );
} }
if ( oldTop < topRow ) if ( oldTop < topRow )
{ {
// Refresh the newly selected or deselected // Refresh the newly selected or deselected
// area above the old or new selection. // area above the old or new selection.
need_refresh[1] = true; RefreshBlock(oldTop, leftCol, topRow - 1, rightCol);
rect[1] = BlockToDeviceRect(
wxGridCellCoords( oldTop, leftCol ),
wxGridCellCoords( topRow - 1, rightCol ) );
} }
if ( oldRight > rightCol ) if ( oldRight > rightCol )
{ {
// Refresh the newly selected or deselected // Refresh the newly selected or deselected
// area to the right of the old or new selection. // area to the right of the old or new selection.
need_refresh[2] = true; RefreshBlock(oldTop, rightCol + 1, oldBottom, oldRight);
rect[2] = BlockToDeviceRect(
wxGridCellCoords( oldTop, rightCol + 1 ),
wxGridCellCoords( oldBottom, oldRight ) );
} }
if ( oldBottom > bottomRow ) if ( oldBottom > bottomRow )
{ {
// Refresh the newly selected or deselected // Refresh the newly selected or deselected
// area below the old or new selection. // area below the old or new selection.
need_refresh[3] = true; RefreshBlock(bottomRow + 1, leftCol, oldBottom, rightCol);
rect[3] = BlockToDeviceRect(
wxGridCellCoords( bottomRow + 1, leftCol ),
wxGridCellCoords( oldBottom, rightCol ) );
} }
// various Refresh() calls
for (i = 0; i < 4; i++ )
if ( need_refresh[i] && rect[i] != wxGridNoCellRect )
m_gridWin->Refresh( false, &(rect[i]) );
} }
// change selection // change selection
@@ -9077,20 +9066,13 @@ wxArrayInt wxGrid::GetSelectedCols() const
void wxGrid::ClearSelection() void wxGrid::ClearSelection()
{ {
wxRect r1 = BlockToDeviceRect(m_selectedBlockTopLeft, RefreshBlock(m_selectedBlockTopLeft, m_selectedBlockBottomRight);
m_selectedBlockBottomRight); RefreshBlock(m_currentCellCoords, m_selectedBlockCorner);
wxRect r2 = BlockToDeviceRect(m_currentCellCoords,
m_selectedBlockCorner);
m_selectedBlockTopLeft = m_selectedBlockTopLeft =
m_selectedBlockBottomRight = m_selectedBlockBottomRight =
m_selectedBlockCorner = wxGridNoCellCoords; m_selectedBlockCorner = wxGridNoCellCoords;
if ( !r1.IsEmpty() )
RefreshRect(r1, false);
if ( !r2.IsEmpty() )
RefreshRect(r2, false);
if ( m_selection ) if ( m_selection )
m_selection->ClearSelection(); m_selection->ClearSelection();
} }

View File

@@ -259,9 +259,7 @@ void wxGridSelection::SelectRow(int row, const wxKeyboardState& kbd)
// Update View: // Update View:
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
wxRect r = m_grid->BlockToDeviceRect( wxGridCellCoords( row, 0 ), m_grid->RefreshBlock(row, 0, row, m_grid->GetNumberCols() - 1);
wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
// Send Event // Send Event
@@ -353,9 +351,7 @@ void wxGridSelection::SelectCol(int col, const wxKeyboardState& kbd)
// Update View: // Update View:
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
wxRect r = m_grid->BlockToDeviceRect( wxGridCellCoords( 0, col ), m_grid->RefreshBlock(0, col, m_grid->GetNumberRows() - 1, col);
wxGridCellCoords( m_grid->GetNumberRows() - 1, col ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
// Send Event // Send Event
@@ -572,9 +568,7 @@ void wxGridSelection::SelectBlock( int topRow, int leftCol,
// Update View: // Update View:
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
wxRect r = m_grid->BlockToDeviceRect( wxGridCellCoords( topRow, leftCol ), m_grid->RefreshBlock(topRow, leftCol, bottomRow, rightCol);
wxGridCellCoords( bottomRow, rightCol ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
// Send Event, if not disabled. // Send Event, if not disabled.
@@ -621,10 +615,7 @@ void wxGridSelection::SelectCell( int row, int col,
// Update View: // Update View:
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
wxRect r = m_grid->BlockToDeviceRect( m_grid->RefreshBlock(selectedTopLeft, selectedBottomRight);
selectedTopLeft,
selectedBottomRight );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
// Send event // Send event
@@ -674,8 +665,7 @@ wxGridSelection::ToggleCellSelection(int row, int col,
m_cellSelection.RemoveAt(n); m_cellSelection.RemoveAt(n);
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
wxRect r = m_grid->BlockToDeviceRect( coords, coords ); m_grid->RefreshBlock(coords, coords);
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
// Send event // Send event
@@ -810,10 +800,7 @@ wxGridSelection::ToggleCellSelection(int row, int col,
{ {
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
r = m_grid->BlockToDeviceRect( m_grid->RefreshBlock(row, col, row, col);
wxGridCellCoords( row, col ),
wxGridCellCoords( row, col ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
wxGridRangeSelectEvent gridEvt( m_grid->GetId(), wxGridRangeSelectEvent gridEvt( m_grid->GetId(),
@@ -839,10 +826,7 @@ wxGridSelection::ToggleCellSelection(int row, int col,
{ {
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
r = m_grid->BlockToDeviceRect( m_grid->RefreshBlock(row, colFrom, row, colTo - 1);
wxGridCellCoords( row, colFrom ),
wxGridCellCoords( row, colTo-1 ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
wxGridRangeSelectEvent gridEvt( m_grid->GetId(), wxGridRangeSelectEvent gridEvt( m_grid->GetId(),
@@ -872,10 +856,7 @@ wxGridSelection::ToggleCellSelection(int row, int col,
{ {
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
r = m_grid->BlockToDeviceRect( m_grid->RefreshBlock(rowFrom, col, rowTo - 1, col);
wxGridCellCoords( rowFrom, col ),
wxGridCellCoords( rowTo - 1, col ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
} }
wxGridRangeSelectEvent gridEvt( m_grid->GetId(), wxGridRangeSelectEvent gridEvt( m_grid->GetId(),
@@ -911,8 +892,7 @@ void wxGridSelection::ClearSelection()
m_cellSelection.RemoveAt(n); m_cellSelection.RemoveAt(n);
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
r = m_grid->BlockToDeviceRect( coords1, coords1 ); m_grid->RefreshBlock(coords1, coords1);
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
#ifdef __WXMAC__ #ifdef __WXMAC__
m_grid->UpdateGridWindows(); m_grid->UpdateGridWindows();
@@ -931,8 +911,7 @@ void wxGridSelection::ClearSelection()
m_blockSelectionBottomRight.RemoveAt(n); m_blockSelectionBottomRight.RemoveAt(n);
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
r = m_grid->BlockToDeviceRect( coords1, coords2 ); m_grid->RefreshBlock(coords1, coords2);
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
#ifdef __WXMAC__ #ifdef __WXMAC__
m_grid->UpdateGridWindows(); m_grid->UpdateGridWindows();
@@ -950,9 +929,7 @@ void wxGridSelection::ClearSelection()
m_rowSelection.RemoveAt(n); m_rowSelection.RemoveAt(n);
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
r = m_grid->BlockToDeviceRect( wxGridCellCoords( row, 0 ), m_grid->RefreshBlock(row, 0, row, m_grid->GetNumberCols() - 1);
wxGridCellCoords( row, m_grid->GetNumberCols() - 1 ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
#ifdef __WXMAC__ #ifdef __WXMAC__
m_grid->UpdateGridWindows(); m_grid->UpdateGridWindows();
@@ -971,9 +948,7 @@ void wxGridSelection::ClearSelection()
m_colSelection.RemoveAt(n); m_colSelection.RemoveAt(n);
if ( !m_grid->GetBatchCount() ) if ( !m_grid->GetBatchCount() )
{ {
r = m_grid->BlockToDeviceRect( wxGridCellCoords( 0, col ), m_grid->RefreshBlock(0, col, m_grid->GetNumberRows() - 1, col);
wxGridCellCoords( m_grid->GetNumberRows() - 1, col ) );
((wxWindow *)m_grid->m_gridWin)->Refresh( false, &r );
#ifdef __WXMAC__ #ifdef __WXMAC__
m_grid->UpdateGridWindows(); m_grid->UpdateGridWindows();