From 1bb7462687e4ce250372f6201a92a155a6f67af6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 11 Dec 2008 22:27:02 +0000 Subject: [PATCH] don't duplicate the column reordering in generic wxHeaderCtrl and wxGrid, extract it into a (public) helper function git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57264 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/headerctrl.h | 8 ++++++++ interface/wx/headerctrl.h | 20 ++++++++++++++++++++ src/common/headerctrlcmn.cpp | 32 ++++++++++++++++++++++++++++++++ src/generic/grid.cpp | 26 ++------------------------ src/generic/headerctrlg.cpp | 23 +---------------------- 5 files changed, 63 insertions(+), 46 deletions(-) diff --git a/include/wx/headerctrl.h b/include/wx/headerctrl.h index b242b9c920..512171a3db 100644 --- a/include/wx/headerctrl.h +++ b/include/wx/headerctrl.h @@ -98,6 +98,14 @@ public: // get the position at which this column is currently displayed unsigned int GetColumnPos(unsigned int idx) const; + // helper function used by the generic version of this control and also + // wxGrid: reshuffles the array of column indices indexed by positions + // (i.e. using the same convention as for SetColumnsOrder()) so that the + // column with the given index is found at the specified position + static void MoveColumnInOrderArray(wxArrayInt& order, + unsigned int idx, + unsigned int pos); + // implementation only from now on // ------------------------------- diff --git a/interface/wx/headerctrl.h b/interface/wx/headerctrl.h index 1feaa0d01f..4bf92248e4 100644 --- a/interface/wx/headerctrl.h +++ b/interface/wx/headerctrl.h @@ -272,6 +272,26 @@ public: */ unsigned int GetColumnPos(unsigned int idx) const; + /** + Helper function to manipulate the array of column indices. + + This function reshuffles the array of column indices indexed by + positions (i.e. using the same convention as for SetColumnsOrder()) so + that the column with the given index is found at the specified + position. + + @param order + Array containing the indices of columns in order of their + positions. + @param idx + The index of the column to move. + @param pos + The new position for the column @a idx. + */ + static void MoveColumnInOrderArray(wxArrayInt& order, + unsigned int idx, + unsigned int pos); + protected: /** Method to be implemented by the derived classes to return the diff --git a/src/common/headerctrlcmn.cpp b/src/common/headerctrlcmn.cpp index 03e2c35cdd..66548a1ff9 100644 --- a/src/common/headerctrlcmn.cpp +++ b/src/common/headerctrlcmn.cpp @@ -147,6 +147,38 @@ unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const return wxNO_COLUMN; } +/* static */ +void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt& order, + unsigned int idx, + unsigned int pos) +{ + const unsigned count = order.size(); + + wxArrayInt orderNew; + orderNew.reserve(count); + for ( unsigned n = 0; ; n++ ) + { + // 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 ( orderNew.size() == pos ) + orderNew.push_back(idx); + + if ( n == count ) + break; + + // delete the column from its old position + const unsigned idxOld = order[n]; + if ( idxOld == idx ) + continue; + + orderNew.push_back(idxOld); + } + + order.swap(orderNew); +} + // ============================================================================ // wxHeaderCtrlSimple implementation // ============================================================================ diff --git a/src/generic/grid.cpp b/src/generic/grid.cpp index 03204f839b..8d2bf5c278 100644 --- a/src/generic/grid.cpp +++ b/src/generic/grid.cpp @@ -6781,37 +6781,15 @@ void wxGrid::DoEndDragMoveCol() void wxGrid::SetColPos(int idx, int pos) { + // we're going to need m_colAt now, initialize it if needed if ( m_colAt.empty() ) { - // 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); } - // create the updated copy of m_colAt - const unsigned count = m_colAt.size(); - - wxArrayInt colAt; - colAt.reserve(count); - for ( unsigned n = 0; n < count; n++ ) - { - // 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 = colAt; + wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos); // also recalculate the column rights if ( !m_colWidths.IsEmpty() ) diff --git a/src/generic/headerctrlg.cpp b/src/generic/headerctrlg.cpp index f1a98083c2..4ec2a61120 100644 --- a/src/generic/headerctrlg.cpp +++ b/src/generic/headerctrlg.cpp @@ -473,28 +473,7 @@ wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos) { - const unsigned count = m_colIndices.size(); - - wxArrayInt colIndices; - colIndices.reserve(count); - for ( unsigned n = 0; n < count; n++ ) - { - // 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 ( colIndices.size() == pos ) - colIndices.push_back(idx); - - // delete the column from its old position - const unsigned idxOld = m_colIndices[n]; - if ( idxOld == idx ) - continue; - - colIndices.push_back(idxOld); - } - - m_colIndices = colIndices; + MoveColumnInOrderArray(m_colIndices, idx, pos); Refresh(); } -- 2.45.2