ReactOS 0.4.16-dev-2284-g3529151
CSortableHeader.hpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Certificate Manager
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: CSortableHeader definition
5 * COPYRIGHT: Copyright 2025 Mark Jansen <mark.jansen@reactos.org>
6 */
7
9{
10 private:
12 bool m_Ascending = true;
13
14 public:
16 {
20 };
21
22 static INT CALLBACK
23 s_CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
24 {
25 SortContext *ctx = ((SortContext *)lParamSort);
26 return ctx->hdr->CompareFunc(lParam1, lParam2, ctx);
27 }
28
29 INT
31 {
32 const INT iSubItem = ctx->iSubItem;
33#define MAX_STR_LEN 256
34
35 CStringW Item1, Item2;
36 LVFINDINFOW IndexInfo;
37 INT Index;
38
39 IndexInfo.flags = LVFI_PARAM;
40
41 IndexInfo.lParam = lParam1;
42 Index = ctx->lvw->FindItem(-1, &IndexInfo);
43 ctx->lvw->GetItemText(Index, iSubItem, Item1.GetBuffer(MAX_STR_LEN), MAX_STR_LEN);
44 Item1.ReleaseBuffer();
45
46 IndexInfo.lParam = lParam2;
47 Index = ctx->lvw->FindItem(-1, &IndexInfo);
48 ctx->lvw->GetItemText(Index, iSubItem, Item2.GetBuffer(MAX_STR_LEN), MAX_STR_LEN);
49 Item2.ReleaseBuffer();
50
51 return m_Ascending ? Item1.Compare(Item2) : Item2.Compare(Item1);
52 }
53
54 void
56 {
57 if ((listView.GetWindowLongPtr(GWL_STYLE) & ~LVS_NOSORTHEADER) == 0)
58 return;
59
60 ApplySorting(listView, pnmv->iSubItem);
61
62 /* Save new values */
65 }
66
67 void
69 {
70 if (m_LastHeaderID != -1)
71 {
73 }
74 }
75
76 void
77 ApplySorting(CListView &listView, INT nHeaderID)
78 {
79 HWND hHeader = (HWND)listView.SendMessage(LVM_GETHEADER, 0, 0);
80 HDITEMW hColumn;
81 ZeroMemory(&hColumn, sizeof(hColumn));
82
83 /* If the sorting column changed, remove the sorting style from the old column */
84 if ((m_LastHeaderID != -1) && (m_LastHeaderID != nHeaderID))
85 {
86 m_Ascending = TRUE; // also reset sorting method to ascending
87 hColumn.mask = HDI_FORMAT;
88 Header_GetItem(hHeader, m_LastHeaderID, &hColumn);
89 hColumn.fmt &= ~(HDF_SORTUP | HDF_SORTDOWN);
90 Header_SetItem(hHeader, m_LastHeaderID, &hColumn);
91 }
92
93 /* Set the sorting style to the new column */
94 hColumn.mask = HDI_FORMAT;
95 Header_GetItem(hHeader, nHeaderID, &hColumn);
96
97 hColumn.fmt &= ~(HDF_SORTDOWN | HDF_SORTUP);
98 hColumn.fmt |= (m_Ascending ? HDF_SORTUP : HDF_SORTDOWN);
99 Header_SetItem(hHeader, nHeaderID, &hColumn);
100
101 /* Sort the list, using the current values of nHeaderID and bIsAscending */
102 SortContext ctx = {this, &listView, nHeaderID};
103 listView.SortItems(s_CompareFunc, &ctx);
104 }
105};
#define MAX_STR_LEN
BOOL SortItems(PFNLVCOMPARE pfnCompare, PVOID lParam)
Definition: rosctrls.h:141
#define TRUE
Definition: types.h:120
HANDLE HWND
Definition: compat.h:19
#define CALLBACK
Definition: compat.h:35
#define ZeroMemory
Definition: minwinbase.h:31
LONG_PTR LPARAM
Definition: minwindef.h:175
#define Header_GetItem(hwndHD, i, phdi)
Definition: commctrl.h:751
#define LVM_GETHEADER
Definition: commctrl.h:2655
#define Header_SetItem(hwndHD, i, phdi)
Definition: commctrl.h:758
#define LVFI_PARAM
Definition: commctrl.h:2441
#define HDF_SORTUP
Definition: commctrl.h:724
#define HDI_FORMAT
Definition: commctrl.h:705
#define LVS_NOSORTHEADER
Definition: commctrl.h:2290
#define HDF_SORTDOWN
Definition: commctrl.h:725
void ReapplySorting(CListView &listView)
void ApplySorting(CListView &listView, INT nHeaderID)
void ColumnClick(CListView &listView, LPNMLISTVIEW pnmv)
INT CompareFunc(LPARAM lParam1, LPARAM lParam2, SortContext *ctx)
static INT CALLBACK s_CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
UINT mask
Definition: commctrl.h:684
int fmt
Definition: commctrl.h:689
LPARAM lParam
Definition: commctrl.h:2463
int32_t INT
Definition: typedefs.h:58
_In_ WDFCOLLECTION _In_ ULONG Index
#define GWL_STYLE
Definition: winuser.h:863