Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sortinglistcontrol.cpp « Controls « windirstat - github.com/windirstat/windirstat.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f17115cb2909570ca525e488986cf6c7b1c7ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// sortinglistcontrol.cpp - Implementation of CSortingListItem and CSortingListControl
//
// WinDirStat - Directory Statistics
// Copyright (C) 2003-2005 Bernhard Seifert
// Copyright (C) 2004-2017 WinDirStat Team (windirstat.net)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//

#include "stdafx.h"
#include "windirstat.h"
#include "options.h"
#include "sortinglistcontrol.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/////////////////////////////////////////////////////////////////////////////

CString CSortingListItem::GetText(int subitem) const
{
    // Dummy implementation
    CString s;
    s.Format(_T("subitem %d"), subitem);
    return s;
}

int CSortingListItem::GetImage() const
{
    // Dummy implementation
    return 0;
}

// Return value:
// <= -2:   this is less than other regardless of ascending flag
// -1:      this is less than other
// 0:       this equals other
// +1:      this is greater than other
// >= +1:   this is greater than other regardless of ascending flag.
//
int CSortingListItem::Compare(const CSortingListItem *other, int subitem) const
{
    // Default implementation compares strings
    return signum(GetText(subitem).CompareNoCase(other->GetText(subitem)));
}

// TODO: check how suitable those are for Unicode-only strings (say Russian)
int CSortingListItem::CompareS(const CSortingListItem *other, const SSorting& sorting) const
{
    int r = Compare(other, sorting.column1);
    if(abs(r) < 2 && !sorting.ascending1)
    {
        r = -r;
    }

    if(r == 0 && sorting.column2 != sorting.column1)
    {
        r = Compare(other, sorting.column2);
        if(abs(r) < 2 && !sorting.ascending2)
        {
            r = -r;
        }
    }
    return r;
}



/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CSortingListControl, CListCtrl)

CSortingListControl::CSortingListControl(LPCTSTR name)
{
    m_name = name;
    m_indicatedColumn = -1;
}

CSortingListControl::~CSortingListControl()
{
}

void CSortingListControl::LoadPersistentAttributes()
{
    int i = 0;
    CArray<int, int> arr;
    arr.SetSize(GetHeaderCtrl()->GetItemCount());

    GetColumnOrderArray(arr.GetData(), static_cast<int>(arr.GetSize()));
    CPersistence::GetColumnOrder(m_name, arr);
    SetColumnOrderArray(int(arr.GetSize()), arr.GetData());

    for(i = 0; i < arr.GetSize(); i++)
    {
        arr[i]= GetColumnWidth(i);
    }
    CPersistence::GetColumnWidths(m_name, arr);
    for(i = 0; i < arr.GetSize(); i++)
    {
        // To avoid "insane" settings we set the column width to
        // maximal twice the default width.
        int maxWidth = GetColumnWidth(i) * 2;
        int w = min(arr[i], maxWidth);
        SetColumnWidth(i, w);
    }

    // Not so good: CPersistence::GetSorting(m_name, GetHeaderCtrl()->GetItemCount(), m_sorting.column1, m_sorting.ascending1, m_sorting.column2, m_sorting.ascending2);
    // We refrain from saving the sorting because it is too likely, that
    // users start up with insane settings and don't get it.
}

void CSortingListControl::SavePersistentAttributes()
{
    CArray<int, int> arr;
    arr.SetSize(GetHeaderCtrl()->GetItemCount());

    GetColumnOrderArray(arr.GetData(), static_cast<int>(arr.GetSize()));
    CPersistence::SetColumnOrder(m_name, arr);

    for(int i = 0; i < arr.GetSize(); i++)
    {
        arr[i]= GetColumnWidth(i);
    }
    CPersistence::SetColumnWidths(m_name, arr);

    // Not so good: CPersistence::SetSorting(m_name, m_sorting.column1, m_sorting.ascending1, m_sorting.column2, m_sorting.ascending2);
}

void CSortingListControl::AddExtendedStyle(DWORD exStyle)
{
    SetExtendedStyle(GetExtendedStyle() | exStyle);
}

void CSortingListControl::RemoveExtendedStyle(DWORD exStyle)
{
    SetExtendedStyle(GetExtendedStyle() & ~exStyle);
}


const SSorting& CSortingListControl::GetSorting()
{
    return m_sorting;
}

void CSortingListControl::SetSorting(const SSorting& sorting)
{
    m_sorting = sorting;
}

void CSortingListControl::SetSorting(int sortColumn1, bool ascending1, int sortColumn2, bool ascending2)
{
    m_sorting.column1 = sortColumn1;
    m_sorting.ascending1 = ascending1;
    m_sorting.column2 = sortColumn2;
    m_sorting.ascending2 = ascending2;
}

void CSortingListControl::SetSorting(int sortColumn, bool ascending)
{
    m_sorting.column2 = m_sorting.column1;
    m_sorting.ascending2 = m_sorting.ascending1;
    m_sorting.column1 = sortColumn;
    m_sorting.ascending1 = ascending;
}

void CSortingListControl::InsertListItem(int i, CSortingListItem *item)
{
    LVITEM lvitem;
    ZeroMemory(&lvitem, sizeof(lvitem));

    lvitem.mask = LVIF_TEXT | LVIF_PARAM;
    if(HasImages())
    {
        lvitem.mask |= LVIF_IMAGE;
    }

    lvitem.iItem = i;
    lvitem.pszText = LPSTR_TEXTCALLBACK;
    lvitem.iImage = I_IMAGECALLBACK;
    lvitem.lParam = (LPARAM)item;

    VERIFY(i == CListCtrl::InsertItem(&lvitem));
}

CSortingListItem *CSortingListControl::GetSortingListItem(int i)
{
    return (CSortingListItem *)GetItemData(i);
}

void CSortingListControl::SortItems()
{
    VERIFY(CListCtrl::SortItems(&_CompareFunc, (DWORD_PTR)&m_sorting));

    HDITEM hditem;
    ZeroMemory(&hditem, sizeof(hditem));

    if(m_indicatedColumn != -1)
    {
        CString text;
        hditem.mask = HDI_TEXT;
        hditem.pszText = text.GetBuffer(256);
        hditem.cchTextMax = 256;
        GetHeaderCtrl()->GetItem(m_indicatedColumn, &hditem);
        text.ReleaseBuffer();
        text = text.Mid(2);
        hditem.pszText = (LPTSTR)(LPCTSTR)text;
        GetHeaderCtrl()->SetItem(m_indicatedColumn, &hditem);
    }

    CString text;
    hditem.mask = HDI_TEXT;
    hditem.pszText = text.GetBuffer(256);
    hditem.cchTextMax = 256;
    GetHeaderCtrl()->GetItem(m_sorting.column1, &hditem);
    text.ReleaseBuffer();
    text = (m_sorting.ascending1 ? _T("< ") : _T("> ")) + text;
    hditem.pszText = (LPTSTR)(LPCTSTR)text;
    GetHeaderCtrl()->SetItem(m_sorting.column1, &hditem);
    m_indicatedColumn = m_sorting.column1;
}

bool CSortingListControl::GetAscendingDefault(int /*column*/)
{
    return true;
}

bool CSortingListControl::HasImages()
{
    return false;
}

#   if (_MFC_VER <=0x0800)
BOOL CSortingListControl::GetColumnOrderArray(LPINT piArray, INT_PTR iCount)
{
    ASSERT(iCount <= INT_MAX);
    return dynamic_cast<CListCtrl*>(this)->GetColumnOrderArray(piArray, (int)iCount);
}
#   endif

int CALLBACK CSortingListControl::_CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
    CSortingListItem *item1 = (CSortingListItem *)lParam1;
    CSortingListItem *item2 = (CSortingListItem *)lParam2;
    SSorting *sorting = (SSorting *)lParamSort;

    return item1->CompareS(item2, *sorting);
}

BEGIN_MESSAGE_MAP(CSortingListControl, CListCtrl)
    ON_NOTIFY_REFLECT(LVN_GETDISPINFO, OnLvnGetdispinfo)
    ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHdnItemclick)
    ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHdnItemclick)
    ON_NOTIFY(HDN_ITEMDBLCLICKA, 0, OnHdnItemdblclick)
    ON_NOTIFY(HDN_ITEMDBLCLICKW, 0, OnHdnItemdblclick)
    ON_WM_DESTROY()
END_MESSAGE_MAP()

void CSortingListControl::OnLvnGetdispinfo(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVDISPINFO *di = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
    *pResult = 0;

    CSortingListItem *item = (CSortingListItem *)(di->item.lParam);

    if((di->item.mask & LVIF_TEXT) != 0)
    {
        _tcscpy_s(di->item.pszText, di->item.cchTextMax, item->GetText(di->item.iSubItem));
    }

    if((di->item.mask & LVIF_IMAGE) != 0)
    {
        di->item.iImage = item->GetImage();
    }
}

void CSortingListControl::OnHdnItemclick(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
    *pResult = 0;
    const int col = phdr->iItem;

    if(col == m_sorting.column1)
    {
        m_sorting.ascending1 = !m_sorting.ascending1;
    }
    else
    {
        SetSorting(col, GetAscendingDefault(col));
    }

    SortItems();
}


void CSortingListControl::OnHdnItemdblclick(NMHDR *pNMHDR, LRESULT *pResult)
{
    OnHdnItemclick(pNMHDR, pResult);
}

void CSortingListControl::OnDestroy()
{
    SavePersistentAttributes();
    CListCtrl::OnDestroy();
}