]> git.saurik.com Git - wxWidgets.git/commitdiff
forward SetColPos() to the header window
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 11 Dec 2008 22:16:38 +0000 (22:16 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 11 Dec 2008 22:16:38 +0000 (22:16 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57263 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/generic/grid.cpp

index 739fedfcacf3d0e1a04dfb5fedc74794f34fbd2f..03204f839bf24998c090378fcdd4912d10acd574 100644 (file)
@@ -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<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[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();
 }