-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListView.cpp
531 lines (433 loc) · 13.5 KB
/
ListView.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "ListView.h"
#include "locale"
class SortList
{
public:
explicit SortList(int column, bool idec){}
bool operator()(const std::vector<std::wstring>& cData1, const std::vector<std::wstring>& cData2)
{
if(dec)
return std::lexicographical_compare(cData1[clumn].begin(), cData1[clumn].end(), cData2[clumn].begin(), cData2[clumn].end(), [](wchar_t ai, wchar_t bi) {
return towlower(ai) > towlower(bi);
});
else
{
return std::lexicographical_compare(cData1[clumn].begin(), cData1[clumn].end(), cData2[clumn].begin(), cData2[clumn].end(), [](wchar_t ai, wchar_t bi) {
return towlower(ai) < towlower(bi);
});
}
}
private:
int clumn;
bool dec;
};
void InitializeListView(HINSTANCE hInstance, HBRUSH backgroundcolor)
{
Theme::RClass(CS_HREDRAW | CS_VREDRAW, ListViewButtonProc, hInstance, LoadCursor(nullptr, IDC_ARROW), NULL,
backgroundcolor, NULL, L"LVBUTTON", sizeof(Theme::ListViewButton));
Theme::RClass(CS_HREDRAW | CS_VREDRAW, (WNDPROC)ListViewProc, hInstance, LoadCursor(nullptr, IDC_ARROW), NULL,
backgroundcolor, NULL, L"LV", sizeof(Theme::ListView));
}
bool compClientData(std::vector<std::wstring> cData1, std::vector<std::wstring> cData2)
{
int retVal = 0;
if (cData1.size() != cData2.size())
return FALSE;
for (int i = 0; i < cData1.size(); i++)
{
retVal += _wcsnicmp(cData1[i].c_str(), cData2[i].c_str(), 99);
}
return retVal;
}
static void
refreshButtons(HWND hWnd, Theme::ListView*& LV)
{
int ActualScrollPosition = GetScrollPos(hWnd, SB_VERT);
int tmpCounterScrollPosition = ActualScrollPosition;
for (int i = LV->numOfPinned; i < LV->vLVButton.size(); i++)
{
for (int j = 0; j < LV->numOfPinned; j++)
{
if (tmpCounterScrollPosition >= LV->vListData.size())
break;
if (!compClientData(LV->vListData[tmpCounterScrollPosition], LV->vLVButton[j]->cData)) {
tmpCounterScrollPosition++; // NEED TO WORK ON THIS TO FIX SCROLLING
}
}
if (tmpCounterScrollPosition >= LV->vListData.size())
break;
//if(LV->vLVButton[i]->)
LV->vLVButton[i]->cData = LV->vListData[tmpCounterScrollPosition];
tmpCounterScrollPosition++;
}
RECT r = Theme::GetRect(hWnd);
r.top = r.top + (LV->ButtonHeight * (LV->numOfPinned -1));
InvalidateRect(hWnd, &r, TRUE);
}
Theme::ListViewButton*
addAndCreate(std::vector<std::wstring> cData, HWND parent, int y, int width, int height)
{
HWND hWnd = CreateWindow(L"LVBUTTON", NULL, WS_CHILD, 0, y, width, height, parent, NULL, (HINSTANCE)GetModuleHandle(NULL), nullptr);
if (!hWnd) {
MessageBox(parent, L"LVBUTTON", L"FailedToCreateWindow", NULL);
}
Theme::ListViewButton* tmpLVButton = (Theme::ListViewButton*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
tmpLVButton->cData = cData;
return tmpLVButton;
}
LRESULT ListViewProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
Theme::ListView* LV = (Theme::ListView*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
switch (message)
{
case IDB_REFRESH:
case IDB_REMOVE:
{
if (wParam) {
//std::vector<int>* invalidClients = (std::vector<int>*)(wParam);
//std::vector<int> vInt;
for (int i : *(std::vector<int>*)(wParam)) {
if (i < LV->vListData.size()) {
for (int j = 0; j < LV->vLVButton.size(); j++)
{
if (!compClientData(LV->vListData[i], LV->vLVButton[j]->cData)) {
if(LV->vLVButton[j]->Pinned)
SendMessage(hWnd, IDB_PIN, NULL, lParam);
break;
}
}
}
}
/*if ((*(std::vector<int>*)(wParam))[0] == 0)
{
HWND rHWND = (HWND)SendMessage(GetParent(hWnd), IDB_GETREMOTEHANDLE, NULL, NULL);
LPSTREAM iStream = (LPSTREAM)SendMessage(rHWND, M_GETSTREAM, NULL, NULL);
while (iStream != nullptr)
Sleep(1);
}*/
int j = 0;
int p = NULL;
for (int i : *(std::vector<int>*)(wParam)) {
p = i - j;
if (p < 0) break;
LV->vListData.erase(LV->vListData.begin() + p);
j++;
}
SendMessage(hWnd, IDC_CLEAR, TRUE, NULL);
}
else
{
if (LV->vLVButton[lParam]->Pinned)
SendMessage(hWnd, IDB_PIN, NULL, lParam);
LV->vListData.erase(LV->vListData.begin() + lParam);
SendMessage(hWnd, IDC_CLEAR, TRUE, NULL);
}
for (auto cData = LV->vListData.begin() + LV->numOfPinned; cData != LV->vListData.end(); cData++)
{
Theme::ListViewButton* tmpLVButton = addAndCreate(cData[0], hWnd, (LV->vLVButton.size()) * LV->ButtonHeight, LV->ButtonWidth, LV->ButtonHeight);
tmpLVButton->ButtonNumber = LV->vLVButton.size();
LV->vLVButton.push_back(tmpLVButton);
ShowWindow(tmpLVButton->pData->hWnd, SW_SHOW);
}
//refreshButtons(hWnd, LV);
}
break;
case IDR_GETLIST:
{
if(LV)
return (LRESULT)&LV->vListData;
}
break;
case IDC_CLEAR:
{
if (wParam != 1) {
LV->vListData.erase(LV->vListData.begin(), LV->vListData.end());
LV->numOfPinned = 0;
}
for (int i = (wParam == 1 ? LV->numOfPinned : 0 ); i < LV->vLVButton.size(); i++)
DestroyWindow(LV->vLVButton[i]->pData->hWnd);
LV->vLVButton.erase(LV->vLVButton.begin() + (wParam == 1 ? LV->numOfPinned : 0), LV->vLVButton.end());
ShowScrollBar(hWnd, SB_VERT, 0);
LV->VSCROLL = FALSE;
InvalidateRect(hWnd, 0, TRUE);
}
break;
case IDB_PIN:
{
if (!LV->vLVButton[lParam]->Pinned) {
LV->vLVButton[LV->numOfPinned]->cData = LV->vLVButton[lParam]->cData; // swap pinned item to first slot after the last pinned item;
LV->vLVButton[LV->numOfPinned]->Pinned = TRUE;
LV->numOfPinned++; //to be pinned item was placed. so increase this counter/
}
else
{
if (lParam < LV->numOfPinned - 1)
for (int i = lParam; i < LV->numOfPinned; i++)
LV->vLVButton[i]->cData = LV->vLVButton[i+1]->cData;
LV->numOfPinned--;
LV->vLVButton[LV->numOfPinned]->Pinned = FALSE;
}
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE;
si.nMin = 0;
si.nMax = ((LV->vListData.size() + LV->numOfPinned) - (LV->ControlHeight / LV->ButtonHeight));
SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
refreshButtons(hWnd, LV);
SetFocus(hWnd);
break;
}
case WM_NCCREATE:
{
LV = new Theme::ListView();
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)LV);
RECT r = Theme::GetRect(hWnd);
LV->counter = 0;
LV->numOfPinned = 0;
LV->ButtonHeight = 15;
LV->ButtonWidth = r.right;
LV->ControlHeight = r.bottom;
}
return TRUE;
case WM_MOUSEWHEEL:
{
SendMessage(hWnd, WM_VSCROLL, WM_MOUSEWHEEL, GET_WHEEL_DELTA_WPARAM(wParam));
return 0;
}
case WM_VSCROLL:
{
// Get all the vertical scroll bar information.
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hWnd, SB_VERT, &si);
// Save the position for comparison later on.
int yPos = si.nPos;
switch (LOWORD(wParam))
{
case WM_MOUSEWHEEL:
si.nPos += (-lParam) / (LV->ControlHeight / LV->ButtonHeight);
break;
// User clicked the top arrow.
case SB_LINEUP:
si.nPos -= 1;
break;
// User clicked the bottom arrow.
case SB_LINEDOWN:
si.nPos += 1;
break;
// User clicked the scroll bar shaft above the scroll box.
case SB_PAGEUP:
si.nPos -= 3;
break;
// User clicked the scroll bar shaft below the scroll box.
case SB_PAGEDOWN:
si.nPos += 3;
break;
// User dragged the scroll box.
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
default:
break;
}
// Set the position and then retrieve it. Due to adjustments
// by Windows it may not be the same as the value set.
si.fMask = SIF_POS;
SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
GetScrollInfo(hWnd, SB_VERT, &si);
// If the position has changed, scroll window and update it.
if (si.nPos != yPos)
{
RECT r = Theme::GetRect(hWnd);
r.top = r.top + (LV->ButtonHeight * LV->numOfPinned);
ScrollWindowEx(hWnd, 0, (yPos - si.nPos) * 18,
&r, &r, NULL, NULL, SW_ERASE | SW_INVALIDATE);
//ScrollWindow(hWnd, 0, 18 * (yPos - si.nPos), NULL, NULL);
//UpdateWindow(hWnd);
refreshButtons(hWnd, LV);
}
return 0;
}
case IDB_ADD:
{
if (LV == NULL) {
MessageBox(hWnd, L"LV == NULL", NULL, NULL);
return FALSE;
}
std::vector<std::wstring> tmpClientData = *(std::vector<std::wstring>*)lParam;
if ((LV->vLVButton.size() * LV->ButtonHeight) < LV->ControlHeight + 3) {
Theme::ListViewButton* tmpLVButton = addAndCreate(tmpClientData, hWnd, (LV->vLVButton.size())*LV->ButtonHeight, LV->ButtonWidth, LV->ButtonHeight);
tmpLVButton->ButtonNumber = LV->vLVButton.size();
LV->vLVButton.push_back(tmpLVButton);
ShowWindow(tmpLVButton->pData->hWnd, SW_SHOW);
}
LV->vListData.push_back(tmpClientData);
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE;
si.nMin = 0;
si.nMax = (LV->vListData.size() - (LV->ControlHeight / LV->ButtonHeight));
SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
if ((LV->vListData.size() * LV->ButtonHeight) > LV->ControlHeight + 3 && !LV->VSCROLL) {
ShowScrollBar(hWnd, SB_VERT, 1);
LV->VSCROLL = TRUE;
}
else if ((LV->vListData.size() * LV->ButtonHeight) < LV->ControlHeight + 3 && LV->VSCROLL) {
ShowScrollBar(hWnd, SB_VERT, 0);
LV->VSCROLL = FALSE;
}
}
break;
case IDB_SORTDEC:
std::sort(LV->vListData.begin(), LV->vListData.end(), SortList(lParam, TRUE));
refreshButtons(hWnd, LV);
break;
case IDB_SORTACE:
std::sort(LV->vListData.begin(), LV->vListData.end(), SortList(lParam, FALSE));
refreshButtons(hWnd, LV);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT __stdcall ListViewButtonProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL LBD = FALSE;
Theme::ListViewButton* cData = (Theme::ListViewButton*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
HWND focus = GetFocus();
if (cData)
{
if (focus == hWnd)
cData->pData->FOCUS = TRUE;
else
cData->pData->FOCUS = FALSE;
}
switch (message)
{
case WM_NCCREATE:
{
Theme::ListViewButton* LVB = new Theme::ListViewButton();
Theme::CData* pData = new Theme::CData();
RECT rect = Theme::GetRect(hWnd);
pData->hWnd = hWnd;
pData->FOCUS = FALSE;
pData->parent = GetParent(hWnd);
pData->csize.right = rect.right;
pData->csize.bottom = rect.bottom;
pData->BMT = FALSE;
pData->font = Theme::CreateCFont({ 14,0,0,0 }, L"Segoe UI");
LVB->pData = pData;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)LVB);
return TRUE;
}
case GETFOCUS:
{
BOOL IB = (!cData->pData->FOCUS ? 0 : 1);
return IB;
}
case WM_KILLFOCUS:
{
cData->pData->FOCUS = FALSE;
InvalidateRect(hWnd, 0, TRUE);
return DefWindowProc(hWnd, message, wParam, lParam);
}
case WM_ERASEBKGND:
{
if (cData->pData->BMT || cData->pData->FOCUS || cData->Pinned)
{
RECT rc;
HDC hdc = (HDC)wParam;
GetClientRect(hWnd, &rc);
FillRect(hdc, &rc, CreateSolidBrush(cData->Pinned ? cData->pData->FOCUS ? RGB(0x6C, 0xC, 0x8C) : RGB(0x2C, 0x4C, 0x6C) : RGB(0x2C, 0x4C, 0x6C)));
break;
}
else return DefWindowProc(hWnd, message, wParam, lParam);
}
case WM_MOUSEACTIVATE:
{
if (!cData->pData->FOCUS)
SetFocus(hWnd);
else
SendMessage(hWnd, WM_KILLFOCUS, 0, 0);
InvalidateRect(hWnd, 0, TRUE);
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam)) {
case LV_REFRESH:
SendMessage(GetParent(hWnd), IDB_REFRESH, 0, cData->ButtonNumber);
break;
case LV_REMOVE:
SendMessage(GetParent(hWnd),IDB_REMOVE, 0, cData->ButtonNumber);
break;
case LV_PIN:
SendMessage(GetParent(hWnd), IDB_PIN, 0, cData->ButtonNumber);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
}
case WM_RBUTTONDOWN:
{
POINT cursor = { 0 };
GetCursorPos(&cursor);
HMENU popupMenu = CreatePopupMenu();
InsertMenu(popupMenu, NULL, MF_STRING, LV_PIN, cData->Pinned ? L"UnPin" : L"Pin");
InsertMenu(popupMenu, NULL, MF_STRING, LV_REMOVE, L"Remove");
TrackPopupMenu(popupMenu, TPM_LEFTALIGN, cursor.x, cursor.y, 0, hWnd, NULL);
break;
}
case WM_MOUSEMOVE:
{
if (!cData->pData->BMT)
{
TRACKMOUSEEVENT TME = { NULL };
TME.cbSize = sizeof(TRACKMOUSEEVENT);
TME.dwFlags = TME_LEAVE;
TME.hwndTrack = hWnd;
TME.dwHoverTime = NULL;
cData->pData->BMT = TrackMouseEvent(&TME);
InvalidateRect(hWnd, 0, TRUE);
}
//
break;
}
case WM_MOUSELEAVE:
{
cData->pData->BMT = FALSE; //hover == false
InvalidateRect(hWnd, 0, TRUE);
}
break;
case WM_PAINT:
{
if (cData->pData == NULL)
return FALSE;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT r = Theme::GetRect(hWnd);
HPEN hp;
int menu = (int)GetMenu(hWnd);
SetTextColor(hdc, RGB(200, 200, 200));
SelectObject(hdc, cData->pData->font);
SetBkColor(hdc, cData->pData->BMT || cData->pData->FOCUS || cData->Pinned ? cData->Pinned ? cData->pData->FOCUS ? RGB(0x6C, 0xC, 0x8C) : RGB(0x2C, 0x4C, 0x6C) : RGB(0x2C, 0x4C, 0x6C) : RGB(0x2C, 0x2C, 0x2C));
int size = cData->cData.size();
RECT tmpr = Theme::DrawTextC(r, hdc, &cData->cData[0][0], wcslen(&cData->cData[0][0]), 0);
int spacing = (r.right - (tmpr.right / 2)) / size;
r.left += spacing + (tmpr.right / 2);
for (int i = 1; i < size; i++)
{
Theme::DrawTextC(r, hdc, &cData->cData[i][0], wcslen(&cData->cData[i][0]), 1);
r.left += spacing;
}
EndPaint(hWnd, &ps);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}