summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/history_control.cpp
blob: f7225ef1535119a068e20fcc1715782067cda39b (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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
#include "stdafx.h"

#define AVERAGE_ITEM_HEIGHT 100

HANDLE htuLog = 0;

static WNDPROC OldEditWndProc;
static wchar_t wszDelete[] = LPGENW("Are you sure to remove all events from history?");

static LRESULT CALLBACK HistoryEditWndProc(HWND, UINT, WPARAM, LPARAM);

/////////////////////////////////////////////////////////////////////////
// Control utilities, types and constants

struct NewstoryListData : public MZeroedObject
{
	NewstoryListData(HWND _1) :
		hwnd(_1),
		redrawTimer(Miranda_GetSystemWindow(), (LPARAM)this)
	{
		redrawTimer.OnEvent = Callback(this, &NewstoryListData::OnTimer);
	}

	HistoryArray items;

	int scrollTopItem; // topmost item
	int scrollTopPixel; // y coord of topmost item, this should be negative or zero
	int caret;
	int cachedWindowHeight;
	int cachedMaxTopItem; // the largest ID of top item to avoid empty space
	int cachedMaxTopPixel;

	RECT rcLastPaint;
	bool repaint;

	HWND hwnd;
	HWND hwndEditBox;

	CTimer redrawTimer;
	CSrmmBaseDialog *pMsgDlg = nullptr;

	void OnContextMenu(int index, POINT pt)
	{
		ItemData* item = items[index];
		if (item == nullptr)
			return;
		
		HMENU hMenu = LoadMenu(g_plugin.getInst(), MAKEINTRESOURCE(IDR_CONTEXTMENU));
		TranslateMenu(hMenu);

		HMENU hSubMenu = GetSubMenu(hMenu, 0);
		UINT ret;
		if (pMsgDlg != nullptr && pMsgDlg->isChat()) {
			EnableMenuItem(hSubMenu, 2, MF_BYPOSITION | MF_GRAYED);

			pMsgDlg->m_bInMenu = true;
			ret = Chat_CreateMenu(hwnd, hSubMenu, pt, pMsgDlg->getChat(), nullptr);
			pMsgDlg->m_bInMenu = false;
		}
		else ret = TrackPopupMenu(hSubMenu, TPM_RETURNCMD, pt.x, pt.y, 0, hwnd, nullptr);

		switch(ret) {
		case ID_CONTEXT_COPY:
			SendMessage(hwnd, NSM_COPY, 0, 0);
			break;

		case ID_CONTEXT_EDIT:
			BeginEditItem(index);
			break;

		case ID_CONTEXT_DELETE:
			break;

		case ID_CONTEXT_SELECTALL:
			SendMessage(hwnd, NSM_SELECTITEMS, 0, items.getCount() - 1);
			break;

		default:
			if (pMsgDlg != nullptr) {
				PostMessage(pMsgDlg->GetHwnd(), WM_MOUSEACTIVATE, 0, 0);
				Chat_DoEventHook(pMsgDlg->getChat(), GC_USER_LOGMENU, nullptr, nullptr, ret);
			}
		}

		DestroyMenu(hMenu);
	}

	void OnTimer(CTimer *pTimer)
	{
		pTimer->Stop();

		scrollTopItem = items.getCount();
		FixScrollPosition();
		InvalidateRect(hwnd, 0, FALSE);
	}

	void BeginEditItem(int index)
	{
		if (hwndEditBox)
			EndEditItem();

		if (scrollTopItem > index)
			return;

		RECT rc; GetClientRect(hwnd, &rc);
		int height = rc.bottom - rc.top;

		int top = scrollTopPixel;
		int idx = scrollTopItem;
		int itemHeight = LayoutItem(idx);
		while (top < height) {
			if (idx == index) {
				ItemData *item = items[index];

				int tpl;
				int fontid;
				int colorid;
				switch (item->dbe.eventType) {
				case EVENTTYPE_MESSAGE:
					tpl = TPL_COPY_MESSAGE;
					fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INMSG : FONT_OUTMSG;
					colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INMSG : COLOR_OUTMSG;
					break;

				case EVENTTYPE_FILE:
					tpl = TPL_COPY_FILE;
					fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INFILE : FONT_OUTFILE;
					colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INFILE : COLOR_OUTFILE;
					break;

				case EVENTTYPE_STATUSCHANGE:
					tpl = TPL_COPY_SIGN;
					fontid = FONT_STATUS;
					colorid = COLOR_STATUS;
					break;

				case EVENTTYPE_AUTHREQUEST:
					tpl = TPL_COPY_AUTH;
					fontid = FONT_INOTHER;
					colorid = COLOR_INOTHER;
					break;

				case EVENTTYPE_ADDED:
					tpl = TPL_COPY_ADDED;
					fontid = FONT_INOTHER;
					colorid = COLOR_INOTHER;
					break;

				case EVENTTYPE_JABBER_PRESENCE:
					tpl = TPL_COPY_PRESENCE;
					fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INOTHER : FONT_OUTOTHER;
					colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INOTHER : COLOR_OUTOTHER;
					break;

				default:
					tpl = TPL_COPY_OTHER;
					fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INOTHER : FONT_OUTOTHER;
					colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INOTHER : COLOR_OUTOTHER;
					break;
				}

				ptrW text(TplFormatString(tpl, item->hContact, item));
				hwndEditBox = CreateWindow(L"EDIT", text, WS_CHILD | WS_BORDER | ES_READONLY | ES_MULTILINE | ES_AUTOVSCROLL, 0, top, rc.right - rc.left, itemHeight, hwnd, NULL, g_plugin.getInst(), NULL);
				OldEditWndProc = (WNDPROC)SetWindowLongPtr(hwndEditBox, GWLP_WNDPROC, (LONG_PTR)HistoryEditWndProc);
				SendMessage(hwndEditBox, WM_SETFONT, (WPARAM)g_fontTable[fontid].hfnt, 0);
				SendMessage(hwndEditBox, EM_SETMARGINS, EC_RIGHTMARGIN, 100);
				SendMessage(hwndEditBox, EM_SETSEL, 0, (LPARAM)(-1));
				ShowWindow(hwndEditBox, SW_SHOW);
				SetFocus(hwndEditBox);
				break;
			}
			top += itemHeight;
			idx++;
			itemHeight = LayoutItem(idx);
		}
	}

	void EndEditItem()
	{
		DestroyWindow(hwndEditBox);
		hwndEditBox = 0;
	}

	void EnsureVisible(int item)
	{
		if (scrollTopItem >= item) {
			scrollTopItem = item;
			scrollTopPixel = 0;
		}
		else {
			RECT rc;
			GetClientRect(hwnd, &rc);
			int height = rc.bottom - rc.top;
			int top = scrollTopPixel;
			int idx = scrollTopItem;
			int itemHeight = LayoutItem(idx);
			bool found = false;
			while (top < height) {
				if (idx == item) {
					itemHeight = LayoutItem(idx);
					if (top + itemHeight > height)
						ScrollListBy(0, height - top - itemHeight);
					found = true;
					break;
				}
				top += itemHeight;
				idx++;
				itemHeight = LayoutItem(idx);
			}
			if (!found) {
				scrollTopItem = item;
				scrollTopPixel = 0;
			}
		}
		FixScrollPosition();
	}

	void FixScrollPosition()
	{
		EndEditItem();

		RECT rc;
		GetWindowRect(hwnd, &rc);
		int windowHeight = rc.bottom - rc.top;

		if (windowHeight != cachedWindowHeight || cachedMaxTopItem != scrollTopItem) {
			int maxTopItem = 0;
			int tmp = 0;
			for (maxTopItem = items.getCount(); (maxTopItem > 0) && (tmp < windowHeight); maxTopItem--)
				tmp += LayoutItem(maxTopItem - 1);
			cachedMaxTopItem = maxTopItem;
			cachedWindowHeight = windowHeight;
			cachedMaxTopPixel = (windowHeight < tmp) ? windowHeight - tmp : 0;
		}

		if (scrollTopItem < 0)
			scrollTopItem = 0;

		if ((scrollTopItem > cachedMaxTopItem) ||
			((scrollTopItem == cachedMaxTopItem) && (scrollTopPixel < cachedMaxTopPixel))) {
			scrollTopItem = cachedMaxTopItem;
			scrollTopPixel = cachedMaxTopPixel;
		}

		RecalcScrollBar();
	}

	int LayoutItem(int index)
	{
		HDC hdc = GetDC(hwnd);
		RECT rc; GetClientRect(hwnd, &rc);
		int width = rc.right - rc.left;

		ItemData *item = items[index];
		if (!item) {
			DeleteDC(hdc);
			return 0;
		}

		int tpl;
		int fontid;
		switch (item->dbe.eventType) {
		case EVENTTYPE_MESSAGE:
			tpl = item->isGrouped() ? TPL_MSG_GRP : TPL_MESSAGE;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INMSG : FONT_OUTMSG;
			break;

		case EVENTTYPE_FILE:
			tpl = TPL_FILE;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INFILE : FONT_OUTFILE;
			break;

		case EVENTTYPE_STATUSCHANGE:
			tpl = TPL_SIGN;
			fontid = FONT_STATUS;
			break;

		case EVENTTYPE_AUTHREQUEST:
			tpl = TPL_AUTH;
			fontid = FONT_INOTHER;
			break;

		case EVENTTYPE_ADDED:
			tpl = TPL_ADDED;
			fontid = FONT_INOTHER;
			break;

		case EVENTTYPE_JABBER_PRESENCE:
			tpl = TPL_PRESENCE;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INOTHER : FONT_OUTOTHER;
			break;

		default:
			tpl = TPL_OTHER;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INOTHER : FONT_OUTOTHER;
			break;
		}

		HFONT hfnt = (HFONT)SelectObject(hdc, g_fontTable[fontid].hfnt);
		if (!item->data)
			item->data = MTextCreateW(htuLog, ptrW(TplFormatString(tpl, item->hContact, item)));

		SIZE sz;
		sz.cx = width - 6;
		MTextMeasure(hdc, &sz, (HANDLE)item->data);

		SelectObject(hdc, hfnt);

		ReleaseDC(hwnd, hdc);
		return sz.cy + 5;
	}

	int PaintItem(HDC hdc, int index, int top, int width)
	{
		auto *item = items[index];

		//	LOGFONT lfText;
		COLORREF clText, clBack, clLine;
		int tpl;
		int fontid;
		int colorid;
		switch (item->dbe.eventType) {
		case EVENTTYPE_MESSAGE:
			tpl = item->isGrouped() ? TPL_MSG_GRP : TPL_MESSAGE;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INMSG : FONT_OUTMSG;
			colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INMSG : COLOR_OUTMSG;
			break;

		case EVENTTYPE_FILE:
			tpl = TPL_FILE;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INFILE : FONT_OUTFILE;
			colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INFILE : COLOR_OUTFILE;
			break;

		case EVENTTYPE_STATUSCHANGE:
			tpl = TPL_SIGN;
			fontid = FONT_STATUS;
			colorid = COLOR_STATUS;
			break;

		case EVENTTYPE_AUTHREQUEST:
			tpl = TPL_AUTH;
			fontid = FONT_INOTHER;
			colorid = COLOR_INOTHER;
			break;

		case EVENTTYPE_ADDED:
			tpl = TPL_ADDED;
			fontid = FONT_INOTHER;
			colorid = COLOR_INOTHER;
			break;

		case EVENTTYPE_JABBER_PRESENCE:
			tpl = TPL_PRESENCE;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INOTHER : FONT_OUTOTHER;
			colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INOTHER : COLOR_OUTOTHER;
			break;

		default:
			tpl = TPL_OTHER;
			fontid = !(item->dbe.flags & DBEF_SENT) ? FONT_INOTHER : FONT_OUTOTHER;
			colorid = !(item->dbe.flags & DBEF_SENT) ? COLOR_INOTHER : COLOR_OUTOTHER;
			break;
		}
		clText = g_fontTable[fontid].cl;
		if (item->bSelected) {
			MTextSendMessage(0, item->data, EM_SETSEL, 0, -1);
			clText = g_colorTable[COLOR_SELTEXT].cl;
			clBack = g_colorTable[COLOR_SELBACK].cl;
			clLine = g_colorTable[COLOR_SELFRAME].cl;
		}
		else {
			MTextSendMessage(0, item->data, EM_SETSEL, 0, 0);
			clLine = g_colorTable[COLOR_FRAME].cl;
			clBack = g_colorTable[colorid].cl;
		}

		if (!item->data) {
			item->data = MTextCreateW(htuLog, ptrW(TplFormatString(tpl, item->hContact, item)));
			if (!item->data)
				return 0;
		}

		SIZE sz;
		sz.cx = width - 6;
		HFONT hfnt = (HFONT)SelectObject(hdc, g_fontTable[fontid].hfnt);
		MTextMeasure(hdc, &sz, (HANDLE)item->data);
		SelectObject(hdc, hfnt);
		int height = sz.cy + 5;

		RECT rc;
		SetRect(&rc, 0, top, width, top + height);

		HBRUSH hbr;
		hbr = CreateSolidBrush(clBack);
		FillRect(hdc, &rc, hbr);

		SetTextColor(hdc, clText);
		SetBkMode(hdc, TRANSPARENT);

		POINT pos;
		pos.x = 3;
		pos.y = top + 2;
		hfnt = (HFONT)SelectObject(hdc, g_fontTable[fontid].hfnt);
		MTextDisplay(hdc, pos, sz, (HANDLE)item->data);
		SelectObject(hdc, hfnt);

		DeleteObject(hbr);

		HPEN hpn = (HPEN)SelectObject(hdc, CreatePen(PS_SOLID, 1, clLine));
		MoveToEx(hdc, rc.left, rc.bottom - 1, 0);
		LineTo(hdc, rc.right, rc.bottom - 1);
		DeleteObject(SelectObject(hdc, hpn));
		return height;
	}

	void RecalcScrollBar()
	{
		SCROLLINFO si = { 0 };
		RECT clRect;
		GetClientRect(hwnd, &clRect);
		si.cbSize = sizeof(si);
		si.fMask = SIF_ALL;
		si.nMin = 0;
		si.nMax = items.getCount() * AVERAGE_ITEM_HEIGHT;
		si.nPage = clRect.bottom;
		si.nPos = scrollTopItem * AVERAGE_ITEM_HEIGHT;
		SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
	}

	void ScrollListBy(int scrollItems, int scrollPixels)
	{
		if (scrollItems) {
			scrollTopItem += scrollItems;
			scrollTopPixel = 0;
		}
		else if (scrollPixels) {
			scrollTopPixel += scrollPixels;
			if (scrollTopPixel > 0) {
				while ((scrollTopPixel > 0) && scrollTopItem) {
					scrollTopItem--;
					int itemHeight = LayoutItem(scrollTopItem);
					scrollTopPixel -= itemHeight;
				}

				if (scrollTopPixel > 0) {
					scrollTopPixel = 0;
				}
			}
			else if (scrollTopPixel < 0) {
				int maxItem = items.getCount();
				int itemHeight = LayoutItem(scrollTopItem);
				while ((-scrollTopPixel > itemHeight) && (scrollTopItem < maxItem)) {
					scrollTopPixel += itemHeight;
					scrollTopItem++;
					itemHeight = LayoutItem(scrollTopItem);
				}
			}
		}

		FixScrollPosition();
	}
};

// Edit box
static LRESULT CALLBACK HistoryEditWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	auto *pData = (NewstoryListData *)GetWindowLongPtr(GetParent(hwnd), 0);

	switch (msg) {
	case WM_KEYDOWN:
		switch (wParam) {
		case VK_ESCAPE:
			pData->EndEditItem();
			return 0;
		}
		break;

	case WM_GETDLGCODE:
		if (lParam) {
			MSG *msg2 = (MSG *)lParam;
			if (msg2->message == WM_KEYDOWN && msg2->wParam == VK_TAB)
				return 0;
			if (msg2->message == WM_CHAR && msg2->wParam == '\t')
				return 0;
		}
		return DLGC_WANTMESSAGE;

	case WM_KILLFOCUS:
		pData->EndEditItem();
		return 0;
	}

	return CallWindowProc(OldEditWndProc, hwnd, msg, wParam, lParam);
}

/////////////////////////////////////////////////////////////////////////
// WndProc

LRESULT CALLBACK NewstoryListWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	NewstoryListData *data = (NewstoryListData *)GetWindowLongPtr(hwnd, 0);

	switch (msg) {
	case WM_CREATE:
		data = new NewstoryListData(hwnd);
		SetWindowLongPtr(hwnd, 0, (LONG_PTR)data);
		data->RecalcScrollBar();
		break;

	// History list control messages
	case NSM_ADDEVENTS:
		if (auto *p = (ADDEVENTS *)wParam)
			data->items.addEvent(p->hContact, p->hFirstEVent, p->eventCount);

		data->redrawTimer.Stop();
		data->redrawTimer.Start(100);
		break;

	case NSM_ADDCHATEVENT:
		data->items.addChatEvent((SESSION_INFO *)wParam, (LOGINFO*)lParam);

		data->redrawTimer.Stop();
		data->redrawTimer.Start(100);
		break;

	case NSM_GETARRAY:
		return (LRESULT)&data->items;

	case NSM_GETCOUNT:
		return data->items.getCount();

	case NSM_SELECTITEMS:
		{
			int start = min(data->items.getCount() - 1, wParam);
			int end = min(data->items.getCount() - 1, max(0, lParam));
			if (start > end)
				std::swap(start, end);

			for (int i = start; i <= end; ++i) {
				auto *p = data->items.get(i, false);
				p->bSelected = true;
			}
			
			InvalidateRect(hwnd, 0, FALSE);
			return 0;
		}

	case NSM_TOGGLEITEMS:
		{
			int start = min(data->items.getCount() - 1, wParam);
			int end = min(data->items.getCount() - 1, max(0, lParam));
			if (start > end)
				std::swap(start, end);

			for (int i = start; i <= end; ++i) {
				auto *p = data->items.get(i, false);
				p->bSelected = !p->bSelected;
			}
			
			InvalidateRect(hwnd, 0, FALSE);
			return 0;
		}

	case NSM_SELECTITEMS2:
		{
			int start = min(data->items.getCount() - 1, wParam);
			int end = min(data->items.getCount() - 1, max(0, lParam));
			if (start > end)
				std::swap(start, end);

			int count = data->items.getCount();
			for (int i = 0; i < count; ++i) {
				auto *p = data->items.get(i, false);
				if ((i >= start) && (i <= end)) 
					p->bSelected = true;
				else
					p->bSelected = false;
			}
			
			InvalidateRect(hwnd, 0, FALSE);
			return 0;
		}

	case NSM_DESELECTITEMS:
		{
			int start = min(data->items.getCount() - 1, wParam);
			int end = min(data->items.getCount() - 1, max(0, lParam));
			if (start > end) {
				start ^= end;
				end ^= start;
				start ^= end;
			}
			for (int i = start; i <= end; ++i) {
				auto *p = data->items.get(i, false);
				p->bSelected = false;
			}
			
			InvalidateRect(hwnd, 0, FALSE);
			return 0;
		}

	case NSM_ENSUREVISIBLE:
		data->EnsureVisible(wParam);
		return 0;

	case NSM_GETITEMFROMPIXEL:
		{
			RECT rc;
			GetClientRect(hwnd, &rc);
			int height = rc.bottom - rc.top;
			int count = data->items.getCount();
			int current = data->scrollTopItem;
			int top = data->scrollTopPixel;
			int bottom = top + data->LayoutItem(current);
			while (top <= height) {
				if ((lParam >= top) && (lParam <= bottom))
					return current;
				if (++current >= count)
					return -1;
				top = bottom;
				bottom = top + data->LayoutItem(current);
			}
			return -1;
		}

	case NSM_SETCARET:
		if (wParam < data->items.getCount()) {
			data->caret = wParam;
			if (lParam)
				SendMessage(hwnd, NSM_ENSUREVISIBLE, data->caret, 0);
		}

	case NSM_GETCARET:
		return data->caret;

	case NSM_FINDNEXT:
		{
			int id = data->items.FindNext(SendMessage(hwnd, NSM_GETCARET, 0, 0), Filter(Filter::EVENTONLY, (wchar_t *)wParam));
			if (id >= 0) {
				SendMessage(hwnd, NSM_SELECTITEMS2, id, id);
				SendMessage(hwnd, NSM_SETCARET, id, TRUE);
			}
			return id;
		}

	case NSM_FINDPREV:
		{
			int id = data->items.FindPrev(SendMessage(hwnd, NSM_GETCARET, 0, 0), Filter(Filter::EVENTONLY, (wchar_t *)wParam));
			if (id >= 0) {
				SendMessage(hwnd, NSM_SELECTITEMS2, id, id);
				SendMessage(hwnd, NSM_SETCARET, id, TRUE);
			}
			return id;
		}

	case NSM_SEEKTIME:
		{
			int eventCount = data->items.getCount();
			for (int i = 0; i < eventCount; i++) {
				auto *p = data->items.get(i, false);
				if (p->dbe.timestamp >= wParam) {
					SendMessage(hwnd, NSM_SELECTITEMS2, i, i);
					SendMessage(hwnd, NSM_SETCARET, i, TRUE);
					break;
				}

				if (i == eventCount - 1) {
					SendMessage(hwnd, NSM_SELECTITEMS2, i, i);
					SendMessage(hwnd, NSM_SETCARET, i, TRUE);
				}
			}
		}
		return TRUE;

	case NSM_SEEKEND:
		SendMessage(hwnd, NSM_SETCARET, data->items.getCount() - 1, 1);
		break;

	case NSM_SET_SRMM:
		data->pMsgDlg = (CSrmmBaseDialog *)lParam;
		break;

	case NSM_DELETE:
		if (IDYES == MessageBoxW(hwnd, TranslateW(wszDelete), _T(MODULETITLE), MB_YESNOCANCEL | MB_ICONQUESTION)) {
			db_set_safety_mode(false);

			int eventCount = data->items.getCount();
			for (int i = eventCount - 1; i >= 0; i--) {
				auto *p = data->items.get(i, false);
				if (p->hEvent && p->hContact)
					db_event_delete(p->hEvent);
			}
			db_set_safety_mode(true);

			data->items.reset();

			InvalidateRect(hwnd, 0, FALSE);
		}
		break;

	case NSM_COPY:
		{
			CMStringW res;

			int eventCount = data->items.getCount();
			for (int i = 0; i < eventCount; i++) {
				ItemData *p = data->items.get(i, false);
				if (p->bSelected)
					res.Append(ptrW(TplFormatString(TPL_COPY_MESSAGE, p->hContact, p)));
			}

			CopyText(hwnd, res);
		}
		__fallthrough;
		// End of history list control messages

	case WM_SIZE:
		InvalidateRect(hwnd, 0, FALSE);
		break;

	case WM_ERASEBKGND:
		return 1;

	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdcWindow = BeginPaint(hwnd, &ps);

			/* we get so many InvalidateRect()'s that there is no point painting,
			Windows in theory shouldn't queue up WM_PAINTs in this case but it does so
			we'll just ignore them */
			if (IsWindowVisible(hwnd)) {
				RECT rc;
				GetClientRect(hwnd, &rc);

				HDC hdc = CreateCompatibleDC(hdcWindow);
				HBITMAP hbmSave = (HBITMAP)SelectObject(hdc, CreateCompatibleBitmap(hdcWindow, rc.right - rc.left, rc.bottom - rc.top));

				GetClientRect(hwnd, &rc);
				int height = rc.bottom - rc.top;
				int width = rc.right - rc.left;
				int top = data->scrollTopPixel;
				int idx = data->scrollTopItem;
				while ((top < height) && (idx < data->items.getCount()))
					top += data->PaintItem(hdc, idx++, top, width);

				if (top <= height) {
					RECT rc2;
					SetRect(&rc2, 0, top, width, height);

					HBRUSH hbr;
					hbr = CreateSolidBrush(g_colorTable[COLOR_BACK].cl);
					FillRect(hdc, &rc2, hbr);
					DeleteObject(hbr);
				}

				GetWindowRect(hwnd, &rc);
				rc.right -= rc.left; rc.left = 0;
				rc.bottom -= rc.top; rc.top = 0;
				DrawEdge(hdc, &rc, BDR_SUNKENOUTER, BF_RECT);

				BitBlt(hdcWindow, 0, 0, rc.right, rc.bottom, hdc, 0, 0, SRCCOPY);
				DeleteObject(SelectObject(hdc, hbmSave));
				DeleteDC(hdc);
			}

			EndPaint(hwnd, &ps);
		}
		break;


	case WM_CONTEXTMENU:
		{
			POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
			if (pt.x == -1 && pt.y == -1)
				GetCursorPos(&pt);

			POINT pt2 = pt;
			ScreenToClient(hwnd, &pt2);

			int index = SendMessage(hwnd, NSM_GETITEMFROMPIXEL, pt2.x, pt2.y);
			if (index != -1)
				data->OnContextMenu(index, pt);
		}
		break;

	case WM_SETFOCUS:
		return 0;

	case WM_GETDLGCODE:
		if (lParam) {
			MSG *msg2 = (MSG *)lParam;
			if (msg2->message == WM_KEYDOWN) {
				if (msg2->wParam == VK_TAB)
					return 0;
				if (msg2->wParam == VK_ESCAPE && !data->hwndEditBox)
					return 0;
			}
			else if (msg2->message == WM_CHAR) {
				if (msg2->wParam == '\t')
					return 0;
				if (msg2->wParam == 27 && !data->hwndEditBox)
					return 0;
			}
		}
		return DLGC_WANTMESSAGE;

	case WM_KEYDOWN:
		{
			switch (wParam) {
			case VK_UP:
				SendMessage(hwnd, NSM_SELECTITEMS2, data->caret - 1, data->caret - 1);
				SendMessage(hwnd, NSM_SETCARET, data->caret - 1, TRUE);
				break;

			case VK_DOWN:
				SendMessage(hwnd, NSM_SELECTITEMS2, data->caret + 1, data->caret + 1);
				SendMessage(hwnd, NSM_SETCARET, data->caret + 1, TRUE);
				break;

			case VK_PRIOR:
				break;

			case VK_NEXT:
				break;

			case VK_HOME:
				break;

			case VK_END:
				break;

			case VK_F2:
				data->BeginEditItem(data->caret);
				break;
			}
			break;
		}

	case WM_SYSCHAR:
	case WM_CHAR:
		if (wParam == 27) {
			if (data->hwndEditBox)
				data->EndEditItem();
		}
		else {
			char ch = MapVirtualKey((lParam >> 16) & 0xff, 1);
			if (((ch == 'C') || (ch == VK_INSERT)) && (GetKeyState(VK_CONTROL) & 0x80)) {
				PostMessage(hwnd, NSM_COPY, 0, 0);
			}
			else if ((ch == 'A') && (GetKeyState(VK_CONTROL) & 0x80)) {
				SendMessage(hwnd, NSM_SELECTITEMS, 0, data->items.getCount());
			}
		}
		break;

	case WM_LBUTTONDOWN:
		{
			int item = SendMessage(hwnd, NSM_GETITEMFROMPIXEL, LOWORD(lParam), HIWORD(lParam));
			if (item >= 0) {
				if (data->caret != item)
					data->EndEditItem();

				if (wParam & MK_CONTROL) {
					SendMessage(hwnd, NSM_TOGGLEITEMS, item, item);
					SendMessage(hwnd, NSM_SETCARET, item, TRUE);
				}
				else if (wParam & MK_SHIFT) {
					SendMessage(hwnd, NSM_SELECTITEMS, data->caret, item);
					SendMessage(hwnd, NSM_SETCARET, item, TRUE);
				}
				else {
					if (data->caret == item) {
						data->BeginEditItem(item);
						return 0;
					}

					SendMessage(hwnd, NSM_SELECTITEMS2, item, item);
					SendMessage(hwnd, NSM_SETCARET, item, TRUE);
				}
			}
		}
		SetFocus(hwnd);
		return 0;

	case WM_MOUSEWHEEL:
		{
			int s_scrollTopItem = data->scrollTopItem;
			int s_scrollTopPixel = data->scrollTopPixel;

			UINT scrollLines;
			if (!SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &scrollLines, FALSE))
				scrollLines = 3;
			data->ScrollListBy(0, (short)HIWORD(wParam) * 10 * (signed)scrollLines / WHEEL_DELTA);

			if ((s_scrollTopItem != data->scrollTopItem) || (s_scrollTopPixel != data->scrollTopPixel))
				InvalidateRect(hwnd, 0, FALSE);
		}
		return TRUE;

	case WM_VSCROLL:
		{
			int s_scrollTopItem = data->scrollTopItem;
			int s_scrollTopPixel = data->scrollTopPixel;

			switch (LOWORD(wParam)) {
			case SB_LINEUP:
				data->ScrollListBy(0, 10);
				break;
			case SB_LINEDOWN:
				data->ScrollListBy(0, -10);
				break;
			case SB_PAGEUP:
				data->ScrollListBy(-10, 0);
				break;
			case SB_PAGEDOWN:
				data->ScrollListBy(10, 0);
				break;
			case SB_BOTTOM:
				data->scrollTopItem = data->items.getCount() - 1;
				data->scrollTopPixel = 0;
				break;
			case SB_TOP:
				data->scrollTopItem = 0;
				data->scrollTopPixel = 0;
				break;
			case SB_THUMBTRACK:
				{
					SCROLLINFO si;
					si.cbSize = sizeof(si);
					si.fMask = SIF_TRACKPOS | SIF_RANGE;
					GetScrollInfo(hwnd, SB_VERT, &si);
					int pos = si.nTrackPos;

					if (pos == si.nMax) {
						data->scrollTopItem = data->items.getCount();
						data->scrollTopPixel = -1000;
					}
					else {
						data->scrollTopItem = pos / AVERAGE_ITEM_HEIGHT;
						int itemHeight = data->LayoutItem(data->scrollTopItem);
						data->scrollTopPixel = -pos % AVERAGE_ITEM_HEIGHT * itemHeight / AVERAGE_ITEM_HEIGHT;
					}
					data->FixScrollPosition();
				}
				break;

			default:
				return 0;
			}

			if ((s_scrollTopItem != data->scrollTopItem) || (s_scrollTopPixel != data->scrollTopPixel))
				InvalidateRect(hwnd, 0, FALSE);
			break;
		}

	case WM_DESTROY:
		delete data;
		SetWindowLongPtr(hwnd, 0, 0);
		break;
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

void InitNewstoryControl()
{
	htuLog = MTextRegister("Newstory", MTEXT_FANCY_DEFAULT | MTEXT_SYSTEM_HICONS);

	WNDCLASS wndclass = {};
	wndclass.style = /*CS_HREDRAW | CS_VREDRAW | */CS_DBLCLKS | CS_GLOBALCLASS;
	wndclass.lpfnWndProc = NewstoryListWndProc;
	wndclass.cbWndExtra = sizeof(void *);
	wndclass.hInstance = g_plugin.getInst();
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.lpszClassName = _T(NEWSTORYLIST_CLASS);
	RegisterClass(&wndclass);
}