Add GetUpdatedAscendingSortIndicator() helper function

It seems like this function will need to be used in every implementation
of EVT_LIST_COL_CLICK handler when using sorting, so it makes sense to
provide it in the library itself.
This commit is contained in:
Vadim Zeitlin 2021-12-14 14:22:08 +00:00
parent 0a8e82b010
commit c834c0b8b7
3 changed files with 32 additions and 8 deletions

View File

@ -451,6 +451,12 @@ public:
void RemoveSortIndicator() { ShowSortIndicator(-1); }
virtual int GetSortIndicator() const { return -1; }
virtual bool IsAscendingSortIndicator() const { return true; }
bool GetUpdatedAscendingSortIndicator(int col) const
{
// If clicking on the same column by which we already sort, toggle the sort
// direction, otherwise use ascending sort by default.
return col == GetSortIndicator() ? !IsAscendingSortIndicator() : true;
}
protected:
// Return pointer to the corresponding m_imagesXXX.

View File

@ -1460,6 +1460,31 @@ public:
*/
int GetSortIndicator() const;
/**
Returns the new value to use for sort indicator after clicking a
column.
This helper function can be useful in the EVT_LIST_COL_CLICK handler
when it updates the sort indicator after the user clicked on a column.
For example:
@code
void MyListCtrl::OnColClick(wxListEvent& event)
{
int col = event.GetColumn();
if ( col == -1 )
return; // clicked outside any column.
const bool ascending = GetUpdatedAscendingSortIndicator(col);
SortItems(MyCompareFunction, ascending);
ShowSortIndicator(col, ascending);
}
@endcode
@since 3.1.6
*/
bool GetUpdatedAscendingSortIndicator(int col) const;
/**
Returns @true if the sort indicator direction is ascending,
@false when the direction is descending.

View File

@ -1089,15 +1089,8 @@ void MyListCtrl::OnColClick(wxListEvent& event)
return; // clicked outside any column.
}
// If clicking on the same column by which we already sort, toggle the sort
// direction, otherwise use ascending sort by default.
bool ascending;
if ( col == GetSortIndicator() )
ascending = !IsAscendingSortIndicator();
else
ascending = true;
// sort on item data (SetItemData)
const bool ascending = GetUpdatedAscendingSortIndicator(col);
if ( SortItems(MyCompareFunction, ascending) )
{
ShowSortIndicator(col, ascending);