Demonstrate sort indicators in wxListCtrl sample

This commit is contained in:
Maarten Bent
2021-11-28 19:20:41 +01:00
parent d8ec0aa001
commit 366e3addf7

View File

@@ -68,13 +68,14 @@ const wxChar *SMALL_VIRTUAL_VIEW_ITEMS[][2] =
static const int NUM_ICONS = 9; static const int NUM_ICONS = 9;
int wxCALLBACK int wxCALLBACK
MyCompareFunction(wxIntPtr item1, wxIntPtr item2, wxIntPtr WXUNUSED(sortData)) MyCompareFunction(wxIntPtr item1, wxIntPtr item2, wxIntPtr sortData)
{ {
bool ascending = (sortData == 1);
// inverse the order // inverse the order
if (item1 < item2) if (item1 < item2)
return 1; return ascending ? -1 : 1;
if (item1 > item2) if (item1 > item2)
return -1; return ascending ? 1 : -1;
return 0; return 0;
} }
@@ -460,6 +461,8 @@ void MyFrame::RecreateList(long flags, bool withText)
flags | flags |
wxBORDER_THEME | wxLC_EDIT_LABELS); wxBORDER_THEME | wxLC_EDIT_LABELS);
m_listCtrl->EnableSortIndicator();
if ( old ) if ( old )
{ {
wxSizer* const sizer = m_panel->GetSizer(); wxSizer* const sizer = m_panel->GetSizer();
@@ -721,11 +724,15 @@ void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
{ {
wxStopWatch sw; wxStopWatch sw;
m_listCtrl->SortItems(MyCompareFunction, 0); static bool sortAsc = false;
sortAsc = !sortAsc;
m_listCtrl->SortItems(MyCompareFunction, sortAsc);
m_logWindow->WriteText(wxString::Format("Sorting %d items took %ld ms\n", m_logWindow->WriteText(wxString::Format("Sorting %d items took %ld ms\n",
m_listCtrl->GetItemCount(), m_listCtrl->GetItemCount(),
sw.Time())); sw.Time()));
m_listCtrl->ShowSortIndicator(0, sortAsc);
} }
void MyFrame::OnFind(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnFind(wxCommandEvent& WXUNUSED(event))
@@ -1084,6 +1091,12 @@ void MyListCtrl::OnColClick(wxListEvent& event)
return; // clicked outside any column. return; // clicked outside any column.
} }
if ( IsSortIndicatorEnabled() )
{
// sort on item data (SetItemData), disable when sorting fails
EnableSortIndicator( SortItems(MyCompareFunction, IsAscendingSortIndicator()) );
}
// set or unset image // set or unset image
static bool x = false; static bool x = false;
x = !x; x = !x;