From 2e733ec74cdef0818ec171376f99049362b643b2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 14 Dec 2008 22:54:34 +0000 Subject: [PATCH] fix several bugs in index/position translation code between wx and MSW git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57343 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/headerctrl.h | 1 + src/msw/headerctrl.cpp | 69 ++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/include/wx/msw/headerctrl.h b/include/wx/msw/headerctrl.h index 38bf952f54..2d5603c332 100644 --- a/include/wx/msw/headerctrl.h +++ b/include/wx/msw/headerctrl.h @@ -90,6 +90,7 @@ private: int MSWFromNativeIdx(int item); // this is the same as above but for order, not index + int MSWToNativeOrder(int order); int MSWFromNativeOrder(int order); // get the event type corresponding to a click or double click event diff --git a/src/msw/headerctrl.cpp b/src/msw/headerctrl.cpp index ef512e0e74..2fb4dea8c1 100644 --- a/src/msw/headerctrl.cpp +++ b/src/msw/headerctrl.cpp @@ -319,7 +319,7 @@ void wxHeaderCtrl::DoInsertItem(const wxHeaderColumn& col, unsigned int idx) } hdi.mask |= HDI_ORDER; - hdi.iOrder = m_colIndices.Index(idx); + hdi.iOrder = MSWToNativeOrder(m_colIndices.Index(idx)); if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM, MSWToNativeIdx(idx), (LPARAM)&hdi) == -1 ) @@ -356,6 +356,10 @@ wxArrayInt wxHeaderCtrl::DoGetColumnsOrder() const 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 @@ -364,13 +368,16 @@ int wxHeaderCtrl::MSWToNativeIdx(int 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() ) - idx--; // one less column the native control knows about + item--; // one less column the native control knows about } - return idx; + wxASSERT_MSG( item >= 0 && item <= GetShownColumnsCount(), "logic error" ); + + return item; } int wxHeaderCtrl::MSWFromNativeIdx(int item) @@ -379,37 +386,59 @@ int wxHeaderCtrl::MSWFromNativeIdx(int item) "column index out of range" ); // reverse the above function - for ( int i = 0; i <= item; i++ ) + + unsigned idx = item; + for ( unsigned n = 0; n < m_numColumns; n++ ) { - if ( GetColumn(i).IsHidden() ) - item++; + if ( n > idx ) + break; + + if ( GetColumn(n).IsHidden() ) + idx++; } - return item; + wxASSERT_MSG( MSWToNativeIdx(idx) == item, "logic error" ); + + return idx; } -int wxHeaderCtrl::MSWFromNativeOrder(int order) +int wxHeaderCtrl::MSWToNativeOrder(int pos) { - wxASSERT_MSG( order >= 0 && order < GetShownColumnsCount(), + wxASSERT_MSG( pos >= 0 && static_cast(pos) < m_numColumns, "column position out of range" ); - // notice that the loop condition is inclusive because if the column - // exactly at position order is hidden we should go to the next one - for ( int pos = 0; pos <= order; pos++ ) + int order = pos; + for ( int n = 0; n < pos; n++ ) { - if ( GetColumn(m_colIndices[pos]).IsHidden() ) - { - // order can't become greater than m_numColumns here because it is - // less than the number of shown columns initially and it is going - // to be incremented at most once for every hidden column and - // m_numColumns is the total columns number - order++; - } + if ( GetColumn(m_colIndices[n]).IsHidden() ) + order--; } + wxASSERT_MSG( order >= 0 && order <= GetShownColumnsCount(), "logic error" ); + return order; } +int wxHeaderCtrl::MSWFromNativeOrder(int order) +{ + wxASSERT_MSG( order >= 0 && order < GetShownColumnsCount(), + "native column position out of range" ); + + unsigned pos = order; + for ( unsigned n = 0; n < m_numColumns; n++ ) + { + if ( n > pos ) + break; + + if ( GetColumn(m_colIndices[n]).IsHidden() ) + pos++; + } + + wxASSERT_MSG( MSWToNativeOrder(pos) == order, "logic error" ); + + return pos; +} + // ---------------------------------------------------------------------------- // wxHeaderCtrl events // ---------------------------------------------------------------------------- -- 2.47.2