+ hdi.cxy = col.GetWidth();
+ }
+
+ hdi.mask |= HDI_ORDER;
+ hdi.iOrder = MSWToNativeOrder(m_colIndices.Index(idx));
+
+ if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM,
+ MSWToNativeIdx(idx), (LPARAM)&hdi) == -1 )
+ {
+ wxLogLastError(wxT("Header_InsertItem()"));
+ }
+}
+
+void wxHeaderCtrl::DoSetColumnsOrder(const wxArrayInt& order)
+{
+ wxArrayInt orderShown;
+ orderShown.reserve(m_numColumns);
+
+ for ( unsigned n = 0; n < m_numColumns; n++ )
+ {
+ const int idx = order[n];
+ if ( GetColumn(idx).IsShown() )
+ orderShown.push_back(MSWToNativeIdx(idx));
+ }
+
+ if ( !Header_SetOrderArray(GetHwnd(), orderShown.size(), &orderShown[0]) )
+ {
+ wxLogLastError(wxT("Header_GetOrderArray"));
+ }
+
+ m_colIndices = order;
+}
+
+wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const
+{
+ // we don't use Header_GetOrderArray() here because it doesn't return
+ // information about the hidden columns, instead we just save the columns
+ // order array in DoSetColumnsOrder() and update it when they're reordered
+ return m_colIndices;
+}
+
+// ----------------------------------------------------------------------------
+// wxHeaderCtrl indexes and positions translation
+// ----------------------------------------------------------------------------
+
+int wxHeaderCtrl::MSWToNativeIdx(int idx)
+{
+ // don't check for GetColumn(idx).IsShown() as it could have just became
+ // false and we may be called from DoUpdate() to delete the old column
+ wxASSERT_MSG( !m_isHidden[idx],
+ "column must be visible to have an "
+ "index in the native control" );
+
+ int item = idx;
+ for ( int i = 0; i < idx; i++ )
+ {
+ if ( GetColumn(i).IsHidden() )
+ item--; // one less column the native control knows about
+ }
+
+ wxASSERT_MSG( item >= 0 && item <= GetShownColumnsCount(), "logic error" );
+
+ return item;
+}
+
+int wxHeaderCtrl::MSWFromNativeIdx(int item)
+{
+ wxASSERT_MSG( item >= 0 && item < GetShownColumnsCount(),
+ "column index out of range" );
+
+ // reverse the above function
+
+ unsigned idx = item;
+ for ( unsigned n = 0; n < m_numColumns; n++ )
+ {
+ if ( n > idx )
+ break;
+
+ if ( GetColumn(n).IsHidden() )
+ idx++;