diff --git a/include/wx/generic/grid.h b/include/wx/generic/grid.h index c3d082ac38..611c36fa7d 100644 --- a/include/wx/generic/grid.h +++ b/include/wx/generic/grid.h @@ -1448,15 +1448,10 @@ public: if ( m_colAt.IsEmpty() ) return idx; - for ( int i = 0; i < m_numCols; i++ ) - { - if ( m_colAt[i] == idx ) - return i; - } + int pos = m_colAt.Index(idx); + wxASSERT_MSG( pos != wxNOT_FOUND, "invalid column index" ); - wxFAIL_MSG( "invalid column index" ); - - return wxNOT_FOUND; + return pos; } // reset the columns positions to the default order diff --git a/src/common/headerctrlcmn.cpp b/src/common/headerctrlcmn.cpp index 2dfbcc6bc7..1b8701595b 100644 --- a/src/common/headerctrlcmn.cpp +++ b/src/common/headerctrlcmn.cpp @@ -218,15 +218,10 @@ unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" ); const wxArrayInt order = GetColumnsOrder(); - for ( unsigned n = 0; n < count; n++ ) - { - if ( (unsigned)order[n] == idx ) - return n; - } + int pos = order.Index(idx); + wxCHECK_MSG( pos != wxNOT_FOUND, wxNO_COLUMN, "column unexpectedly not displayed at all" ); - wxFAIL_MSG( "column unexpectedly not displayed at all" ); - - return wxNO_COLUMN; + return (unsigned int)pos; } /* static */ @@ -234,31 +229,14 @@ void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt& order, unsigned int idx, unsigned int pos) { - const unsigned count = order.size(); + int posOld = order.Index(idx); + wxASSERT_MSG( posOld != wxNOT_FOUND, "invalid index" ); - wxArrayInt orderNew; - orderNew.reserve(count); - for ( unsigned n = 0; ; n++ ) + if ( pos != (unsigned int)posOld ) { - // 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.RemoveAt(posOld); + order.Insert(idx, pos); } - - order.swap(orderNew); } void