summaryrefslogtreecommitdiff
path: root/plugins/YAPP/src/yapp_history_dlg.cpp
blob: 534af1fe0c91c54d497a5b10c20edb3c9b3a99fe (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
#include "common.h"
#include "resource.h"
#include "yapp_history.h"

//************ Some helper resize stuff ******************

#define MIN_HISTORY_WIDTH  540
#define MIN_HISTORY_HEIGHT 300

#define GAP_SIZE 2

#define ANCHOR_LEFT     0x000001
#define ANCHOR_RIGHT		0x000002
#define ANCHOR_TOP      0x000004
#define ANCHOR_BOTTOM   0x000008
#define ANCHOR_ALL      ANCHOR_LEFT | ANCHOR_RIGHT | ANCHOR_TOP | ANCHOR_BOTTOM

WNDPROC oldPopupsListProc = NULL;

HWND hHistoryWindow = 0; //the history window
HICON hiPopupHistory; //popup history icon
PopupHistoryList lstPopupHistory; //defined in main.cpp

const TCHAR *szHistoryColumns[] = {_T("Title"), _T("Message"), _T("Timestamp")}; //need to make sure that the string and size vectors have the same number of elements
const int cxHistoryColumns[] = {100, 450, 115};
const int cHistoryColumns = sizeof(szHistoryColumns) / sizeof(szHistoryColumns[0]);

struct PopupHistoryWindowData{
	HWND hIEView;
};

void ScreenToClient(HWND hWnd, LPRECT rect)
{
	POINT pt;
	int cx = rect->right - rect->left;
	int cy = rect->bottom - rect->top;
	pt.x = rect->left;
	pt.y = rect->top;
	ScreenToClient(hWnd, &pt);
	rect->left = pt.x;
	rect->top = pt.y;
	rect->right = pt.x + cx;
	rect->bottom = pt.y + cy;
}

RECT AnchorCalcPos(HWND window, const RECT *rParent, const WINDOWPOS *parentPos, int anchors)
{
	RECT rChild;
	RECT rTmp;

	GetWindowRect(window, &rChild);
	ScreenToClient(parentPos->hwnd, &rChild);

	int cx = rParent->right - rParent->left;
	int cy = rParent->bottom - rParent->top;
	if ((cx == parentPos->cx) && (cy == parentPos->cy))
		{
			return rChild;
		}
	if (parentPos->flags & SWP_NOSIZE)
		{
			return rChild;
		}

	rTmp.left = parentPos->x - rParent->left;
	rTmp.right = (parentPos->x + parentPos->cx) - rParent->right;
	rTmp.bottom = (parentPos->y + parentPos->cy) - rParent->bottom;
	rTmp.top = parentPos->y - rParent->top;
	
	cx = (rTmp.left) ? -rTmp.left : rTmp.right;
	cy = (rTmp.top) ? -rTmp.top : rTmp.bottom;
	
	rChild.right += cx;
	rChild.bottom += cy;
	//expanded the window accordingly, now we need to enforce the anchors
	if ((anchors & ANCHOR_LEFT) && (!(anchors & ANCHOR_RIGHT)))
		{
			rChild.right -= cx;
		}
	if ((anchors & ANCHOR_TOP) && (!(anchors & ANCHOR_BOTTOM)))
		{
			rChild.bottom -= cy;
		}
	if ((anchors & ANCHOR_RIGHT) && (!(anchors & ANCHOR_LEFT)))
		{
			rChild.left += cx;
		}
	if ((anchors & ANCHOR_BOTTOM) && (!(anchors & ANCHOR_TOP)))
		{
			rChild.top += cy;
		}
	return rChild;
}

void AnchorMoveWindow(HWND window, const WINDOWPOS *parentPos, int anchors)
{
	RECT rParent;
	RECT rChild;
	
	if (parentPos->flags & SWP_NOSIZE)
		{
			return;
		}
	GetWindowRect(parentPos->hwnd, &rParent);
	rChild = AnchorCalcPos(window, &rParent, parentPos, anchors);
	MoveWindow(window, rChild.left, rChild.top, rChild.right - rChild.left, rChild.bottom - rChild.top, FALSE);
}

void AddAnchorWindowToDeferList(HDWP &hdWnds, HWND window, RECT *rParent, WINDOWPOS *wndPos, int anchors)
{
	RECT rChild = AnchorCalcPos(window, rParent, wndPos, anchors);
	hdWnds = DeferWindowPos(hdWnds, window, HWND_NOTOPMOST, rChild.left, rChild.top, rChild.right - rChild.left, rChild.bottom - rChild.top, SWP_NOZORDER);
}
//************************************************************

//Stucture passed to list sort function
struct SortParams{
	HWND hList;
	int column;
};

static int lastColumn = -1; //last sort column

int MatchesFilterCS(const TCHAR *filter, const PopupHistoryData *popupItem) //case sensitive
{
	if (_tcslen(filter) <= 0)	{ return 1;	} //if no filter is set then the popup item matches the filter
	int match = 0;
	
	match = (_tcsstr(popupItem->messageT, filter)) ? 1 : match; //check message part
	
	if (!match) //check title part of no match has been found
	{
		match = (_tcsstr(popupItem->titleT, filter)) ? 1 : match;
	}
	
	if (!match) //if no match has been found yet try to match the timestamp
	{
		TCHAR buffer[1024];
		struct tm *myTime = localtime(&popupItem->timestamp);
		_tcsftime(buffer, 1024, _T("%c"), myTime);
		match = (_tcsstr(buffer, filter)) ? 1 : match;
	}
	
	return match;
}

__inline void ConvertCase(TCHAR *dest, const TCHAR *source, size_t size)
{
	_tcsncpy(dest, source, size);
	_tcslwr(dest);
}

int MatchesFilterCI(const TCHAR *filterS, const PopupHistoryData *popupItem)
{
	if (_tcslen(filterS) <= 0)	{ return 1;	} //if no filter is set then the popup item matches the filter
	int match = 0;
	const int BUFFER_SIZE = 1024;
	TCHAR buffer[BUFFER_SIZE];
	TCHAR filterI[BUFFER_SIZE];
	
	ConvertCase(filterI, filterS, BUFFER_SIZE);
	
	ConvertCase(buffer, popupItem->messageT, BUFFER_SIZE); //check message part
	match = (_tcsstr(buffer, filterI)) ? 1 : match;
	
	if (!match) // check title part of no match has been found
	{
		ConvertCase(buffer, popupItem->titleT, BUFFER_SIZE);
		match = (_tcsstr(buffer, filterI)) ? 1 : match;
	}
	
	if (!match) //if no match has been found yet try to match the timestamp
	{
		struct tm *myTime = localtime(&popupItem->timestamp);
		_tcsftime(buffer, 1024, _T("%c"), myTime);
		match = (_tcsstr(buffer, filterI)) ? 1 : match;
	}
	
	return match;
}

int CALLBACK PopupsCompare(LPARAM lParam1, LPARAM lParam2, LPARAM myParam)
{
	SortParams params = *(SortParams *) myParam;
	const int MAX_SIZE = 512;
	TCHAR text1[MAX_SIZE];
	TCHAR text2[MAX_SIZE];
	int res;
	
	ListView_GetItemText(params.hList, (int) lParam1, params.column, text1, MAX_SIZE);
	ListView_GetItemText(params.hList, (int) lParam2, params.column, text2, MAX_SIZE);
	
	res = _tcsicmp(text1, text2);
	
	res = (params.column == lastColumn) ? -res : res; //do reverse search on second click on same column
	
	return res;
}



int CalcCustomControlPos(IEVIEWWINDOW *ieWnd, HWND hMainWindow)
{
	RECT rect;
	GetWindowRect(GetDlgItem(hMainWindow, IDC_LST_HISTORY), &rect);
	ScreenToClient(hMainWindow, &rect);
	//GetClientRect(hMainWindow, &rect);

	ieWnd->x = rect.left + GAP_SIZE;
	ieWnd->y = rect.top + GAP_SIZE;
	ieWnd->cx = rect.right - rect.left - (2 * GAP_SIZE);
	ieWnd->cy = rect.bottom - rect.top - (2 * GAP_SIZE);
	return 0;
}

void MoveCustomControl(HWND hWnd, int renderer)
{
	switch (renderer)
	{
		case RENDER_HISTORYPP:
		case RENDER_IEVIEW:
		{
			PopupHistoryWindowData *data = (PopupHistoryWindowData *) GetWindowLongPtr(hWnd, GWLP_USERDATA);
			if (data)
			{
					IEVIEWWINDOW ieWnd = {0};
					ieWnd.cbSize = sizeof(ieWnd);
					ieWnd.parent = hWnd;
					ieWnd.hwnd = data->hIEView;
					ieWnd.iType = IEW_SETPOS;
					CalcCustomControlPos(&ieWnd, hWnd);
					
					CallService((renderer == RENDER_HISTORYPP) ? MS_HPP_EG_WINDOW : MS_IEVIEW_WINDOW, 0, (LPARAM) &ieWnd);
			}
			
			break;
		}
	}
}

void LoadRenderer(HWND hWnd, int renderer)
{
	switch (renderer)
	{
		case RENDER_HISTORYPP:
		case RENDER_IEVIEW:
		{
			IEVIEWWINDOW ieWnd = {0};
			
			ieWnd.cbSize = sizeof(ieWnd);
			ieWnd.iType = IEW_CREATE;
			ieWnd.dwMode = IEWM_HISTORY;
			ieWnd.dwFlags = 0;
			ieWnd.parent = hWnd;
			CalcCustomControlPos(&ieWnd, hWnd);
			
			CallService((renderer == RENDER_HISTORYPP) ? MS_HPP_EG_WINDOW : MS_IEVIEW_WINDOW, 0, (LPARAM) &ieWnd); //create the IeView or H++ control.
			
			PopupHistoryWindowData *data = (PopupHistoryWindowData *) mir_alloc(sizeof(PopupHistoryWindowData)); //create custom control data
			data->hIEView = ieWnd.hwnd;
			ShowWindow(data->hIEView, SW_SHOW);
			SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) data); //set it as the window's user data
			ShowWindow(GetDlgItem(hWnd, IDC_LST_HISTORY), SW_HIDE);
			//SetWindowPos(GetDlgItem(hWnd, IDC_LST_HISTORY), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
			
			break;
		}
	}
}

void UnloadRenderer(HWND hWnd, int renderer)
{
	switch (renderer)
	{
		case RENDER_HISTORYPP:
		case RENDER_IEVIEW:
		{
			PopupHistoryWindowData *data = (PopupHistoryWindowData *) GetWindowLongPtr(hWnd, GWLP_USERDATA);
			
			if (data)
			{
				IEVIEWWINDOW ieWnd = {0};
				ieWnd.cbSize = sizeof(ieWnd);
				ieWnd.parent = hWnd;
				ieWnd.hwnd = data->hIEView;
				ieWnd.iType = IEW_DESTROY;
				CallService((renderer == RENDER_HISTORYPP) ? MS_HPP_EG_WINDOW : MS_IEVIEW_WINDOW, 0, (LPARAM) &ieWnd);		
				
				mir_free(data);
			}
			
			break;
		}
	}
}

void DeleteOldEvents(HWND hWnd, int renderer)
{
	switch (renderer)
	{
		case RENDER_HISTORYPP:
		case RENDER_IEVIEW:
		{
			PopupHistoryWindowData *data = (PopupHistoryWindowData *) GetWindowLongPtr(hWnd, GWLP_USERDATA);
			if (data)
			{
				IEVIEWEVENT ieEvent = {0};
				ieEvent.cbSize = sizeof(IEVIEWEVENT);
				ieEvent.hwnd = data->hIEView;
				ieEvent.iType = IEE_CLEAR_LOG;
				
				CallService((renderer == RENDER_HISTORYPP) ? MS_HPP_EG_EVENT : MS_IEVIEW_EVENT, 0, (LPARAM) &ieEvent);
			}
			
			break;
		}
		
		case RENDER_DEFAULT:
		default:
		{
			ListView_DeleteAllItems(GetDlgItem(hWnd, IDC_LST_HISTORY));
			
			break;
		}
		
	}
}

typedef int (*SIG_MATCHESFILTER)(const TCHAR *filter, const PopupHistoryData *popupItem);
typedef void (*SIG_ADDEVENTS)(HWND hWnd, int renderer, TCHAR *filter, SIG_MATCHESFILTER MatchesFilter);

IEVIEWEVENTDATA *CreateAndFillEventData(PopupHistoryData *popupItem)
{
	IEVIEWEVENTDATA *eventData = (IEVIEWEVENTDATA *) mir_calloc(sizeof(IEVIEWEVENTDATA));
	if (eventData)
	{
		eventData->cbSize = sizeof(IEVIEWEVENTDATA);
		eventData->iType = IEED_EVENT_MESSAGE;

		eventData->dwFlags = IEEDF_UNICODE_NICK | IEEDF_UNICODE_TEXT | IEEDF_UNICODE_TEXT2;
		eventData->pszNickW = popupItem->titleT;
		eventData->pszTextW = popupItem->messageT;

		eventData->time = (DWORD) popupItem->timestamp;
		eventData->next = NULL;
	}
	
	return eventData;
}

void AddEventsCustomControl(HWND hWnd, int renderer, TCHAR *filter, SIG_MATCHESFILTER MatchesFilter)
{
	PopupHistoryWindowData *pwData = (PopupHistoryWindowData *) GetWindowLongPtr(hWnd, GWLP_USERDATA);
	if (pwData)
	{
		IEVIEWEVENT ieEvent = {0};
		ieEvent.cbSize = sizeof(IEVIEWEVENT);
		ieEvent.hwnd = pwData->hIEView;
		ieEvent.codepage = CP_ACP;
		ieEvent.iType = IEE_LOG_MEM_EVENTS;
		ieEvent.hContact = NULL;//(HANDLE) CallService(MS_DB_CONTACT_FINDFIRST, 0, 0); IEVIEW needs a contact handle !!
		
		IEVIEWEVENTDATA *eventData = NULL;
		IEVIEWEVENTDATA *cED = NULL;
		IEVIEWEVENTDATA *prevED = NULL;
		
		int i;
		int count = 0;
		int size = lstPopupHistory.Count();
		PopupHistoryData *popupItem;
		
		for (i = 0; i < size; i++)
		{
			popupItem = lstPopupHistory.Get(i);
			if (MatchesFilter(filter, popupItem))
			{
				cED = CreateAndFillEventData(popupItem);
				if (cED)
				{
					count++;
					if (!eventData)
					{
						eventData = cED;
					}
					else{
						prevED->next = cED;
					}
					
					prevED = cED;
				}
			}
		}
		ieEvent.count = count;
		ieEvent.eventData = eventData;
		
		CallService((renderer == RENDER_HISTORYPP) ? MS_HPP_EG_EVENT : MS_IEVIEW_EVENT, 0, (LPARAM) &ieEvent);
		
		while (eventData)
		{
			cED = eventData;
			eventData = eventData->next;
			mir_free(cED);
		}
	}
}

void AddEventsDefault(HWND hWnd, int renderer, TCHAR *filter, SIG_MATCHESFILTER MatchesFilter)
{
	HWND hHistoryList = GetDlgItem(hWnd, IDC_LST_HISTORY);
	TCHAR buffer[1024];
	struct tm *myTime;
	
	LVITEM item = {0};
	item.mask = LVIF_TEXT;

	int i, lIndex;
	lIndex = 0;
	PopupHistoryData *popupItem;
	for (i = 0; i < lstPopupHistory.Count(); i++)
	{
		item.iItem = lIndex;
		popupItem = lstPopupHistory.Get(i);
		if (MatchesFilter(filter, popupItem))
		{
			item.pszText = popupItem->titleT;
			ListView_InsertItem(hHistoryList, &item);
			ListView_SetItemText(hHistoryList, lIndex, 1, popupItem->messageT);
			myTime = localtime(&popupItem->timestamp);
			_tcsftime(buffer, 1024, _T("%c"), myTime);
			ListView_SetItemText(hHistoryList, lIndex++, 2, buffer);
		}
	}
}

void RefreshPopupHistory(HWND hWnd, int renderer)
{
	if (!hWnd) { return; }
	const int MAX_FILTER_SIZE = 1024;
	SIG_MATCHESFILTER MatchesFilter = (IsDlgButtonChecked(hWnd, IDC_HISTORY_FILTER_CASESENSITIVE)) ? MatchesFilterCS : MatchesFilterCI; //case sensitive compare or not ?
	
	SIG_ADDEVENTS AddEvents = (renderer == RENDER_DEFAULT) ? AddEventsDefault : AddEventsCustomControl;

	TCHAR filter[MAX_FILTER_SIZE];
	DeleteOldEvents(hWnd, renderer); //delete events
		
	GetWindowText(GetDlgItem(hWnd, IDC_HISTORY_FILTER), filter, MAX_FILTER_SIZE); //get filter text
	
	AddEvents(hWnd, renderer, filter, MatchesFilter);
	
	if (renderer == RENDER_DEFAULT)
	{
		HWND hHistoryList = GetDlgItem(hWnd, IDC_LST_HISTORY);
		SortParams params = {0};
		params.hList = hHistoryList;
		params.column = lastColumn;
		
		ListView_SortItemsEx(hHistoryList, PopupsCompare, &params);
	}
}

void CopyPopupDataToClipboard(HWND hList, int selection)
{
	if (!selection)
	{
		return;
	}
	
	if (!GetOpenClipboardWindow())
	{
		if (OpenClipboard(hList))
		{
			TCHAR buffer[2048];
			buffer[0] = _T('\0');
			TCHAR *clipboard;
			int i;
			int found = 0;
			int count = ListView_GetItemCount(hList);
			int textType;

			textType = CF_UNICODETEXT;

			
			for (i = 0; i < count; i++)
			{
				if (ListView_GetItemState(hList, i, LVIS_SELECTED))
				{
					ListView_GetItemText(hList, i, selection - 100, buffer, 2048);
					found = 1;
					break;
				}
			}
			if (found)
			{
				EmptyClipboard();
				int len = (int)_tcslen(buffer);
				
				HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, (len + 2) * sizeof(TCHAR));
				clipboard = (TCHAR *) GlobalLock(hData);
				_tcsncpy(clipboard, buffer, len);
				clipboard[len] = _T('\0');
				GlobalUnlock(hData);
				if (!SetClipboardData(textType, hData))
				{
					PUShowMessage("Could not set clipboard data", SM_WARNING);
				}
			}
			CloseClipboard();
		}
		else{
			PUShowMessage("Could not open clipboard", SM_WARNING);
		}
	}
	else{
		PUShowMessage("The clipboard is not available", SM_WARNING);
	}
}

//subclass proc for the list view
BOOL CALLBACK PopupsListSubclassProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_CONTEXTMENU:
		{
			int x = LOWORD(lParam);
			int y = HIWORD(lParam);
			int selection;		
			
			HMENU hMenu = CreatePopupMenu();
			AppendMenu(hMenu, MF_STRING, POPUPMENU_TITLE, TranslateT("Copy title to clipboard"));
			AppendMenu(hMenu, MF_STRING, POPUPMENU_MESSAGE, TranslateT("Copy message to clipboard"));
			AppendMenu(hMenu, MF_STRING, POPUPMENU_TIMESTAMP, TranslateT("Copy timestamp to clipboard"));
			selection = TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, x, y, 0, hWnd, NULL);
			DestroyMenu(hMenu);
			if (selection)
			{
				CopyPopupDataToClipboard(hWnd, selection);
			}
		
			break;
		}
		
		case WM_KEYUP:
		{
			switch (wParam)
			{
				case 'C':
				{
					if (GetKeyState(VK_CONTROL))
					{
						CopyPopupDataToClipboard(hWnd, POPUPMENU_MESSAGE);
					}
					
					break;
				}
				
				case VK_ESCAPE:
				{
					SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);
					
					break;
				}
				
			}
			 
			break;
		}
		
		case WM_SYSKEYDOWN:
		{
			if (wParam == 'X')
			{
				SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);
			}
			
			break;
		}
	}
	
	return CallWindowProc(oldPopupsListProc, hWnd, msg, wParam, lParam);
}

//load the columns
void LoadHistoryColumns(HWND hHistoryList)
{
	LVCOLUMN col;
	col.mask = LVCF_TEXT | LVCF_WIDTH;
	int i;
	
	for (i  = 0; i < cHistoryColumns; i++)
	{
		col.pszText = TranslateTS(szHistoryColumns[i]);
		col.cx = cxHistoryColumns[i];
		ListView_InsertColumn(hHistoryList, i, &col);
	}
}

//this is the history list window handler
INT_PTR CALLBACK DlgProcHistLst(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int bInitializing;
	
	switch (msg)
	{
		case WM_INITDIALOG:
		{
			bInitializing = 1;
			int renderer = lstPopupHistory.GetRenderer();
			
			SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) hiPopupHistory);
			
			LoadRenderer(hWnd, renderer);
			
			TranslateDialogDefault(hWnd);
			HWND hHistoryList = GetDlgItem(hWnd, IDC_LST_HISTORY);

			ListView_SetExtendedListViewStyleEx(hHistoryList, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

			oldPopupsListProc = (WNDPROC) SetWindowLongPtr(hHistoryList, GWLP_WNDPROC, (LONG_PTR) PopupsListSubclassProc);
			
			LoadHistoryColumns(hHistoryList);
			
			RefreshPopupHistory(hWnd, renderer);
			
			bInitializing = 0;

			return TRUE;
		}		

		case WM_DESTROY:
		{
			UnloadRenderer(hWnd, lstPopupHistory.GetRenderer());
			hHistoryWindow = NULL;
			
			break;
		}
		
		case WM_CLOSE:
		{
			//Utils_SaveWindowPosition(hWnd, 0, MODULE, "history_dlg");
			DestroyWindow(hWnd);
	
			break;
		}

		case WM_WINDOWPOSCHANGING:
		{
			WINDOWPOS *wndPos = (WINDOWPOS *) lParam;
			
			if (wndPos->cx < MIN_HISTORY_WIDTH)
			{
				wndPos->cx = MIN_HISTORY_WIDTH;
			}
			if (wndPos->cy < MIN_HISTORY_HEIGHT)
			{
				wndPos->cy = MIN_HISTORY_HEIGHT;
			}

			RECT rParent;
			GetWindowRect(hWnd, &rParent);
			HDWP hdWnds = BeginDeferWindowPos(4);

			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_CLOSE), &rParent, wndPos, ANCHOR_RIGHT | ANCHOR_BOTTOM);
			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_HISTORY_FILTER), &rParent, wndPos, ANCHOR_LEFT | ANCHOR_BOTTOM);
			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_HISTORY_FILTER_CASESENSITIVE), &rParent, wndPos, ANCHOR_LEFT | ANCHOR_RIGHT | ANCHOR_BOTTOM);
			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_LST_HISTORY), &rParent, wndPos, ANCHOR_ALL);

			EndDeferWindowPos(hdWnds);
			MoveCustomControl(hWnd, lstPopupHistory.GetRenderer()); //move the custom control too, if any
			break;
		}

			
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_CLOSE:
				{
					SendMessage(hWnd, WM_CLOSE, 0, 0);
	
					break;
				}
				
				case IDC_HISTORY_FILTER_CASESENSITIVE:
				{
					if (HIWORD(wParam) == BN_CLICKED)
					{
						RefreshPopupHistory(hWnd, lstPopupHistory.GetRenderer());
					}
				}
				
				case IDC_HISTORY_FILTER:
				{
					if (HIWORD(wParam) == EN_CHANGE)
					{
						if (!bInitializing)
						{
							RefreshPopupHistory(hWnd, lstPopupHistory.GetRenderer());
						}
					}
					
					break;
				}
				
			}
	
			break;
		}
	
		case WM_NOTIFY:
		{
			switch(((LPNMHDR)lParam)->idFrom)
			{
				case IDC_LST_HISTORY:
				{
					switch (((LPNMHDR)lParam)->code)
					{
						case LVN_COLUMNCLICK:
						{
							LPNMLISTVIEW lv = (LPNMLISTVIEW) lParam;
							int column = lv->iSubItem;
							SortParams params = {0};
							params.hList = GetDlgItem(hWnd, IDC_LST_HISTORY);
							params.column = column;
							
							ListView_SortItemsEx(params.hList, PopupsCompare, (LPARAM) &params);
							lastColumn = (params.column == lastColumn) ? -1 : params.column;

							break;
						}
					}
				
					break;
				}
			}
				
			break;
		}
	}
	
	return 0;
}