diff --git a/include/wx/listbase.h b/include/wx/listbase.h index df569db9ee..9b200d1412 100644 --- a/include/wx/listbase.h +++ b/include/wx/listbase.h @@ -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. diff --git a/interface/wx/listctrl.h b/interface/wx/listctrl.h index a2620d35fe..32aab20b5e 100644 --- a/interface/wx/listctrl.h +++ b/interface/wx/listctrl.h @@ -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. diff --git a/samples/listctrl/listtest.cpp b/samples/listctrl/listtest.cpp index 835b31e011..4b2aae9e57 100644 --- a/samples/listctrl/listtest.cpp +++ b/samples/listctrl/listtest.cpp @@ -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);