// 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
// -------------------------------
*/
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
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
// ============================================================================
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<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;
+ wxHeaderCtrl::MoveColumnInOrderArray(m_colAt, idx, pos);
// also recalculate the column rights
if ( !m_colWidths.IsEmpty() )
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();
}