From 19230604c4a41c9a058e22a1719f4919c7f2dd37 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Tue, 14 Aug 2001 05:35:09 +0000 Subject: [PATCH] Moved fix from 2.2 branch that allows the attributes in MSW wxListCtrl to move with their items when the list is sorted. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@11372 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/hash.cpp | 11 +++++--- src/msw/listctrl.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/common/hash.cpp b/src/common/hash.cpp index 900219c6d1..2909e48183 100644 --- a/src/common/hash.cpp +++ b/src/common/hash.cpp @@ -681,11 +681,14 @@ void wxHashTable::DeleteContents (bool flag) void wxHashTable::Clear () { - int i; - for (i = 0; i < n; i++) + int i; + if (hash_table) { - if (hash_table[i]) - hash_table[i]->Clear (); + for (i = 0; i < n; i++) + { + if (hash_table[i]) + hash_table[i]->Clear (); + } } m_count = 0; } diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index 5b030dd304..904294c968 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -1376,11 +1376,74 @@ bool wxListCtrl::ScrollList(int dx, int dy) // or zero if the two items are equivalent. // data is arbitrary data to be passed to the sort function. + +// FIXME: this is horrible and MT-unsafe and everything else but I don't have +// time for anything better right now (VZ) +static long gs_sortData = 0; +static wxListCtrl *gs_sortCtrl = NULL; +static wxListCtrlCompare gs_sortFunction = NULL; + +int wxCMPFUNC_CONV wxListCtrlCompareFn(const void *arg1, const void *arg2) +{ + int n1 = *(const int *)arg1, + n2 = *(const int *)arg2; + + return gs_sortFunction(gs_sortCtrl->GetItemData(n1), + gs_sortCtrl->GetItemData(n2), + gs_sortData); +} + bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data) { - return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE) fn, data) != 0); + // sort the attributes too + if ( m_hasAnyAttr ) + { + int n, + count = GetItemCount(); + int *aItems = new int[count]; + for ( n = 0; n < count; n++ ) + { + aItems[n] = n; + } + + gs_sortData = data; + gs_sortCtrl = this; + gs_sortFunction = fn; + + qsort(aItems, count, sizeof(int), wxListCtrlCompareFn); + + gs_sortData = 0; + gs_sortCtrl = NULL; + gs_sortFunction = NULL; + + wxHashTable attrsNew(wxKEY_INTEGER, 1000); + for ( n = 0; n < count; n++ ) + { + wxObject *attr = m_attrs.Delete(n); + if ( attr ) + { + attrsNew.Put(aItems[n], attr); + } + } + + m_attrs.Destroy(); + m_attrs = attrsNew; + + delete [] aItems; + } + + if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE)fn, data) ) + { + wxLogDebug(_T("ListView_SortItems() failed")); + + return FALSE; + } + + return TRUE; } + + // ---------------------------------------------------------------------------- // message processing // ---------------------------------------------------------------------------- -- 2.45.2