From 3169a8e8373815a6929fd7abdd26122b5194e37b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 11 Dec 2008 22:16:38 +0000 Subject: [PATCH] forward SetColPos() to the header window git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57263 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/generic/grid.cpp | 59 +++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 739fedfcac..03204f839b 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -6779,42 +6779,41 @@ void wxGrid::DoEndDragMoveCol() SetColPos( m_dragRowOrCol, newPos ); } -void wxGrid::SetColPos( int colID, int newPos ) +void wxGrid::SetColPos(int idx, int pos) { - if ( m_colAt.IsEmpty() ) + if ( m_colAt.empty() ) { - m_colAt.Alloc( m_numCols ); - - int i; - for ( i = 0; i < m_numCols; i++ ) - { - m_colAt.Add( i ); - } + // we're going to need m_colAt now, initialize it + m_colAt.reserve(m_numCols); + for ( int i = 0; i < m_numCols; i++ ) + m_colAt.push_back(i); } - int oldPos = GetColPos( colID ); + // create the updated copy of m_colAt + const unsigned count = m_colAt.size(); - //Reshuffle the m_colAt array - if ( newPos > oldPos ) - { - int i; - for ( i = oldPos; i < newPos; i++ ) - { - m_colAt[i] = m_colAt[i+1]; - } - } - else + wxArrayInt colAt; + colAt.reserve(count); + for ( unsigned n = 0; n < count; n++ ) { - int i; - for ( i = oldPos; i > newPos; i-- ) - { - m_colAt[i] = m_colAt[i-1]; - } + // NB: order of checks is important for this to work when the new + // column position is the same as the old one + + // insert the column at its new position + if ( colAt.size() == static_cast(pos) ) + colAt.push_back(idx); + + // delete the column from its old position + const int idxOld = m_colAt[n]; + if ( idxOld == idx ) + continue; + + colAt.push_back(idxOld); } - m_colAt[newPos] = colID; + m_colAt = colAt; - //Recalculate the column rights + // also recalculate the column rights if ( !m_colWidths.IsEmpty() ) { int colRight = 0; @@ -6828,7 +6827,11 @@ void wxGrid::SetColPos( int colID, int newPos ) } } - m_colWindow->Refresh(); + // and make the changes visible + if ( m_useNativeHeader ) + GetColHeader()->SetColumnsOrder(m_colAt); + else + m_colWindow->Refresh(); m_gridWin->Refresh(); } -- 2.45.2