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
This commit is contained in:
Vadim Zeitlin
2008-12-11 22:27:02 +00:00
parent 3169a8e837
commit 1bb7462687
5 changed files with 63 additions and 46 deletions

View File

@@ -98,6 +98,14 @@ public:
// get the position at which this column is currently displayed // get the position at which this column is currently displayed
unsigned int GetColumnPos(unsigned int idx) const; 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 // implementation only from now on
// ------------------------------- // -------------------------------

View File

@@ -272,6 +272,26 @@ public:
*/ */
unsigned int GetColumnPos(unsigned int idx) const; 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: protected:
/** /**
Method to be implemented by the derived classes to return the Method to be implemented by the derived classes to return the

View File

@@ -147,6 +147,38 @@ unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const
return wxNO_COLUMN; 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 // wxHeaderCtrlSimple implementation
// ============================================================================ // ============================================================================

View File

@@ -6781,37 +6781,15 @@ void wxGrid::DoEndDragMoveCol()
void wxGrid::SetColPos(int idx, int pos) void wxGrid::SetColPos(int idx, int pos)
{ {
// we're going to need m_colAt now, initialize it if needed
if ( m_colAt.empty() ) if ( m_colAt.empty() )
{ {
// we're going to need m_colAt now, initialize it
m_colAt.reserve(m_numCols); m_colAt.reserve(m_numCols);
for ( int i = 0; i < m_numCols; i++ ) for ( int i = 0; i < m_numCols; i++ )
m_colAt.push_back(i); m_colAt.push_back(i);
} }
// create the updated copy of m_colAt wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
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<unsigned>(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;
// also recalculate the column rights // also recalculate the column rights
if ( !m_colWidths.IsEmpty() ) if ( !m_colWidths.IsEmpty() )

View File

@@ -473,28 +473,7 @@ wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const
void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos) void wxHeaderCtrl::DoMoveCol(unsigned int idx, unsigned int pos)
{ {
const unsigned count = m_colIndices.size(); MoveColumnInOrderArray(m_colIndices, idx, pos);
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;
Refresh(); Refresh();
} }