summaryrefslogtreecommitdiff
path: root/plugins/WhenWasIt/src/dlg_handlers.cpp
blob: 1083287cc28c27401e4e937fc1be6e282678cbd1 (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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
/*
WhenWasIt (birthday reminder) plugin for Miranda IM

Copyright © 2006 Cristian Libotean
Copyright (C) 2014 Rozhuk Ivan

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"

#define COLOR_USERINFO RGB(138, 190, 160)
#define COLOR_MBIRTHDAY RGB(222, 222, 88)
#define COLOR_BIRTHDAYREMINDER RGB(200, 120, 240)
#define COLOR_PROTOCOL RGB(255, 153, 153)
#define COLOR_MICQBIRTHDAY RGB(88, 88, 240)

#define UPCOMING_TIMER_ID 1002

const TCHAR *szShowAgeMode[] = { LPGENT("Upcoming age"), LPGENT("Current age") };
const int cShowAgeMode = sizeof(szShowAgeMode) / sizeof(szShowAgeMode[0]);

const TCHAR *szSaveModule[] = { LPGENT("UserInfo module"), LPGENT("Protocol module"), LPGENT("mBirthday module") };
const int cSaveModule = sizeof(szSaveModule) / sizeof(szSaveModule[0]);

const TCHAR *szPopupClick[] = { LPGENT("Nothing"), LPGENT("Dismiss"), LPGENT("Message window") };
const int cPopupClick = sizeof(szPopupClick) / sizeof(szPopupClick[0]);

const TCHAR *szNotifyFor[] = { LPGENT("All contacts"), LPGENT("All contacts except hidden ones"), LPGENT("All contacts except ignored ones"), LPGENT("All contacts except hidden and ignored ones") };
const int cNotifyFor = sizeof(szNotifyFor) / sizeof(szNotifyFor[0]);

#define MIN_BIRTHDAYS_WIDTH 200
#define MIN_BIRTHDAYS_HEIGHT 200

#include "commctrl.h"
void CreateToolTip(HWND target, TCHAR* tooltip, LPARAM width)
{
	HWND hwndToolTip = CreateWindow(TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT, target, NULL, NULL, NULL);
	if (hwndToolTip) {
		TOOLINFO ti = { 0 };
		ti.cbSize = sizeof(ti);
		ti.uFlags = TTF_TRANSPARENT | TTF_SUBCLASS;
		ti.hwnd = target;
		ti.uId = 0;
		ti.hinst = NULL;
		ti.lpszText = tooltip;
		GetClientRect(target, &ti.rect);
		SendMessage(hwndToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
		SendMessage(hwndToolTip, TTM_SETMAXTIPWIDTH, 0, width);
		SendMessage(hwndToolTip, TTM_SETDELAYTIME, TTDT_AUTOPOP, 20000);
	}
}

int EnablePopupsGroup(HWND hWnd, int enable)
{
	EnableWindow(GetDlgItem(hWnd, IDC_POPUPS_STATIC), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_POPUP_TIMEOUT), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_FOREGROUND), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_BACKGROUND), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_NOBIRTHDAYS_POPUP), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_IGNORE_SUBCONTACTS), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_PREVIEW), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_LEFT_CLICK), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_RIGHT_CLICK), enable);

	return enable;
}

int EnableClistGroup(HWND hWnd, int enable)
{
	EnableWindow(GetDlgItem(hWnd, IDC_CLIST_STATIC), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_ADVANCED_ICON), enable);
	return enable;
}

int EnableDialogGroup(HWND hWnd, int enable)
{
	EnableWindow(GetDlgItem(hWnd, IDC_DLG_TIMEOUT), enable);
	EnableWindow(GetDlgItem(hWnd, IDC_OPENINBACKGROUND), enable);

	return enable;
}

int AddInfoToComboBoxes(HWND hWnd)
{
	int i;

	for (i = 0; i < cShowAgeMode; i++)
		SendDlgItemMessage(hWnd, IDC_AGE_COMBOBOX, CB_ADDSTRING, 0, (LPARAM)TranslateTS(szShowAgeMode[i]));

	for (i = 0; i < cSaveModule; i++)
		SendDlgItemMessage(hWnd, IDC_DEFAULT_MODULE, CB_ADDSTRING, 0, (LPARAM)TranslateTS(szSaveModule[i]));

	for (i = 0; i < cPopupClick; i++) {
		SendDlgItemMessage(hWnd, IDC_LEFT_CLICK, CB_ADDSTRING, 0, (LPARAM)TranslateTS(szPopupClick[i]));
		SendDlgItemMessage(hWnd, IDC_RIGHT_CLICK, CB_ADDSTRING, 0, (LPARAM)TranslateTS(szPopupClick[i]));
	}

	for (i = 0; i < cNotifyFor; i++)
		SendDlgItemMessage(hWnd, IDC_NOTIFYFOR, CB_ADDSTRING, 0, (LPARAM)TranslateTS(szNotifyFor[i]));

	return i;
}

SIZE GetControlTextSize(HWND hCtrl)
{
	HDC hDC = GetDC(hCtrl);
	HFONT font = (HFONT)SendMessage(hCtrl, WM_GETFONT, 0, 0);
	HFONT oldFont = (HFONT)SelectObject(hDC, font);
	const size_t maxSize = 2048;
	TCHAR buffer[maxSize];
	SIZE size;
	GetWindowText(hCtrl, buffer, _countof(buffer));
	GetTextExtentPoint32(hDC, buffer, (int)mir_tstrlen(buffer), &size);
	SelectObject(hDC, oldFont);
	ReleaseDC(hCtrl, hDC);
	return size;
}

int EnlargeControl(HWND hCtrl, HWND, SIZE oldSize)
{
	SIZE size = GetControlTextSize(hCtrl);
	RECT rect;
	GetWindowRect(hCtrl, &rect);
	int offset = (rect.right - rect.left) - oldSize.cx;
	SetWindowPos(hCtrl, HWND_TOP, 0, 0, size.cx + offset, oldSize.cy, SWP_NOMOVE);
	SetWindowPos(hCtrl, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
	return 0;
}

TCHAR *strtrim(TCHAR *str)
{
	size_t i = 0;
	size_t len = mir_tstrlen(str);
	while ((i < len) && (str[i] == _T(' '))) { i++; }
	if (i) {
		memmove(str, str + i, len - i + 1);
		len -= i;
	}

	while ((len > 0) && (str[--len] == _T(' ')))
		str[len] = 0;

	return str;
}

INT_PTR CALLBACK DlgProcOptions(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int bInitializing = 0;
	switch (msg) {
	case WM_INITDIALOG:
		{
			bInitializing = 1;

			SIZE oldPopupsSize = GetControlTextSize(GetDlgItem(hWnd, IDC_USE_POPUPS));
			SIZE oldClistIconSize = GetControlTextSize(GetDlgItem(hWnd, IDC_USE_CLISTICON));
			SIZE oldDialogSize = GetControlTextSize(GetDlgItem(hWnd, IDC_USE_DIALOG));

			TranslateDialogDefault(hWnd);

			EnlargeControl(GetDlgItem(hWnd, IDC_USE_POPUPS), GetDlgItem(hWnd, IDC_POPUPS_STATIC), oldPopupsSize);
			EnlargeControl(GetDlgItem(hWnd, IDC_USE_CLISTICON), GetDlgItem(hWnd, IDC_CLIST_STATIC), oldClistIconSize);
			EnlargeControl(GetDlgItem(hWnd, IDC_USE_DIALOG), GetDlgItem(hWnd, IDC_DIALOG_STATIC), oldDialogSize);

			AddInfoToComboBoxes(hWnd);

			SendDlgItemMessage(hWnd, IDC_FOREGROUND, CPM_SETDEFAULTCOLOUR, 0, FOREGROUND_COLOR);
			SendDlgItemMessage(hWnd, IDC_BACKGROUND, CPM_SETDEFAULTCOLOUR, 0, BACKGROUND_COLOR);

			SendDlgItemMessage(hWnd, IDC_FOREGROUND, CPM_SETCOLOUR, 0, commonData.foreground);
			SendDlgItemMessage(hWnd, IDC_BACKGROUND, CPM_SETCOLOUR, 0, commonData.background);

			SendDlgItemMessage(hWnd, IDC_DEFAULT_MODULE, CB_SETCURSEL, commonData.cDefaultModule, 0);
			SendDlgItemMessage(hWnd, IDC_LEFT_CLICK, CB_SETCURSEL, commonData.lPopupClick, 0);
			SendDlgItemMessage(hWnd, IDC_RIGHT_CLICK, CB_SETCURSEL, commonData.rPopupClick, 0);
			SendDlgItemMessage(hWnd, IDC_NOTIFYFOR, CB_SETCURSEL, commonData.notifyFor, 0);

			CreateToolTip(GetDlgItem(hWnd, IDC_POPUP_TIMEOUT), TranslateT("Set popup delay when notifying of upcoming birthdays.\nFormat: default delay [ | delay for birthdays occurring today]"), 400);

			TCHAR buffer[1024];
			_itot(commonData.daysInAdvance, buffer, 10);
			SetDlgItemText(hWnd, IDC_DAYS_IN_ADVANCE, buffer);
			_itot(commonData.checkInterval, buffer, 10);
			SetDlgItemText(hWnd, IDC_CHECK_INTERVAL, buffer);
			mir_sntprintf(buffer, _countof(buffer), _T("%d|%d"), commonData.popupTimeout, commonData.popupTimeoutToday);
			SetDlgItemText(hWnd, IDC_POPUP_TIMEOUT, buffer);
			_itot(commonData.cSoundNearDays, buffer, 10);
			SetDlgItemText(hWnd, IDC_SOUND_NEAR_DAYS_EDIT, buffer);
			_itot(commonData.cDlgTimeout, buffer, 10);
			SetDlgItemText(hWnd, IDC_DLG_TIMEOUT, buffer);
			_itot(commonData.daysAfter, buffer, 10);
			SetDlgItemText(hWnd, IDC_DAYS_AFTER, buffer);

			CheckDlgButton(hWnd, IDC_OPENINBACKGROUND, (commonData.bOpenInBackground) ? BST_CHECKED : BST_UNCHECKED);

			CheckDlgButton(hWnd, IDC_NOBIRTHDAYS_POPUP, (commonData.bNoBirthdaysPopup) ? BST_CHECKED : BST_UNCHECKED);
			SendDlgItemMessage(hWnd, IDC_AGE_COMBOBOX, CB_SETCURSEL, commonData.cShowAgeMode, 0);

			CheckDlgButton(hWnd, IDC_IGNORE_SUBCONTACTS, (commonData.bIgnoreSubcontacts) ? BST_CHECKED : BST_UNCHECKED);

			CheckDlgButton(hWnd, IDC_ONCE_PER_DAY, (commonData.bOncePerDay) ? BST_CHECKED : BST_UNCHECKED);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK_INTERVAL), !commonData.bOncePerDay);

			CheckDlgButton(hWnd, IDC_USE_DIALOG, (commonData.bUseDialog) ? BST_CHECKED : BST_UNCHECKED);
			EnableDialogGroup(hWnd, commonData.bUseDialog);

			if (ServiceExists(MS_POPUP_ADDPOPUPT)) {
				CheckDlgButton(hWnd, IDC_USE_POPUPS, commonData.bUsePopups ? BST_CHECKED : BST_UNCHECKED);
				EnablePopupsGroup(hWnd, commonData.bUsePopups);
			}
			else {
				EnableWindow(GetDlgItem(hWnd, IDC_USE_POPUPS), FALSE);
				EnablePopupsGroup(hWnd, FALSE);
			}

			CheckDlgButton(hWnd, IDC_USE_CLISTICON, BST_CHECKED);
			EnableWindow(GetDlgItem(hWnd, IDC_USE_CLISTICON), FALSE);
			EnableClistGroup(hWnd, FALSE);
		}
		bInitializing = 0;
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_CHECK_INTERVAL:
		case IDC_POPUP_TIMEOUT:
		case IDC_DAYS_IN_ADVANCE:
		case IDC_DLG_TIMEOUT:
		case IDC_SOUND_NEAR_DAYS_EDIT:
		case IDC_DAYS_AFTER:
			if ((HIWORD(wParam) == EN_CHANGE) && (!bInitializing))// || (HIWORD(wParam) == CBN_SELENDOK))
				SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0);
			break;

		case IDC_USE_POPUPS:
		case IDC_USE_CLISTICON:
		case IDC_USE_DIALOG:
			EnablePopupsGroup(hWnd, IsDlgButtonChecked(hWnd, IDC_USE_POPUPS));
			EnableClistGroup(hWnd, IsDlgButtonChecked(hWnd, IDC_USE_CLISTICON));
			EnableDialogGroup(hWnd, IsDlgButtonChecked(hWnd, IDC_USE_DIALOG));
			RedrawWindow(GetDlgItem(hWnd, IDC_USE_POPUPS), NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
			RedrawWindow(GetDlgItem(hWnd, IDC_USE_CLISTICON), NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
			RedrawWindow(GetDlgItem(hWnd, IDC_USE_DIALOG), NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
			//fallthrough

		case IDC_FOREGROUND:
		case IDC_BACKGROUND:
		case IDC_ADVANCED_ICON:
		case IDC_IGNORE_SUBCONTACTS:
		case IDC_AGE_COMBOBOX:
		case IDC_NOBIRTHDAYS_POPUP:
		case IDC_DEFAULT_MODULE:
		case IDC_LEFT_CLICK:
		case IDC_RIGHT_CLICK:
		case IDC_ONCE_PER_DAY:
		case IDC_NOTIFYFOR:
			SendMessage(GetParent(hWnd), PSM_CHANGED, 0, 0);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK_INTERVAL), BST_UNCHECKED == IsDlgButtonChecked(hWnd, IDC_ONCE_PER_DAY));
			break;

		case IDC_PREVIEW:
			{
				MCONTACT hContact = db_find_first();
				int dtb = rand() % 11; //0..10
				int age = rand() % 50 + 1; //1..50
				PopupNotifyBirthday(hContact, dtb, age);
				break;
			}
		}
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom) {
		case 0:
			switch (((LPNMHDR)lParam)->code) {
			case PSN_APPLY:
				commonData.foreground = SendDlgItemMessage(hWnd, IDC_FOREGROUND, CPM_GETCOLOUR, 0, 0);
				commonData.background = SendDlgItemMessage(hWnd, IDC_BACKGROUND, CPM_GETCOLOUR, 0, 0);
				commonData.popupTimeout = POPUP_TIMEOUT;
				commonData.popupTimeoutToday = POPUP_TIMEOUT;
				commonData.bUsePopups = IsDlgButtonChecked(hWnd, IDC_USE_POPUPS);
				commonData.bUseDialog = IsDlgButtonChecked(hWnd, IDC_USE_DIALOG);
				commonData.bIgnoreSubcontacts = IsDlgButtonChecked(hWnd, IDC_IGNORE_SUBCONTACTS);
				commonData.bNoBirthdaysPopup = IsDlgButtonChecked(hWnd, IDC_NOBIRTHDAYS_POPUP);
				commonData.cShowAgeMode = SendDlgItemMessage(hWnd, IDC_AGE_COMBOBOX, CB_GETCURSEL, 0, 0);
				commonData.cDefaultModule = SendDlgItemMessage(hWnd, IDC_DEFAULT_MODULE, CB_GETCURSEL, 0, 0);
				commonData.lPopupClick = SendDlgItemMessage(hWnd, IDC_LEFT_CLICK, CB_GETCURSEL, 0, 0);
				commonData.rPopupClick = SendDlgItemMessage(hWnd, IDC_RIGHT_CLICK, CB_GETCURSEL, 0, 0);
				commonData.bOncePerDay = IsDlgButtonChecked(hWnd, IDC_ONCE_PER_DAY);
				commonData.notifyFor = SendDlgItemMessage(hWnd, IDC_NOTIFYFOR, CB_GETCURSEL, 0, 0);
				commonData.bOpenInBackground = IsDlgButtonChecked(hWnd, IDC_OPENINBACKGROUND);
				{
					const int maxSize = 1024;
					TCHAR buffer[maxSize];

					GetDlgItemText(hWnd, IDC_DAYS_IN_ADVANCE, buffer, _countof(buffer));
					TCHAR *stop = NULL;
					commonData.daysInAdvance = _tcstol(buffer, &stop, 10);

					if (*stop) { commonData.daysInAdvance = DAYS_TO_NOTIFY; }

					GetDlgItemText(hWnd, IDC_DAYS_AFTER, buffer, _countof(buffer));
					commonData.daysAfter = _tcstol(buffer, &stop, 10);

					if (*stop) { commonData.daysAfter = DAYS_TO_NOTIFY_AFTER; }

					GetDlgItemText(hWnd, IDC_CHECK_INTERVAL, buffer, _countof(buffer));
					commonData.checkInterval = _ttol(buffer);
					if (!commonData.checkInterval) { commonData.checkInterval = CHECK_INTERVAL; }

					GetDlgItemText(hWnd, IDC_POPUP_TIMEOUT, buffer, _countof(buffer));
					TCHAR *pos;
					pos = _tcschr(buffer, _T('|'));
					if (pos) {
						TCHAR tmp[128];
						*pos = 0;
						mir_tstrcpy(tmp, buffer);
						strtrim(tmp);
						commonData.popupTimeout = _ttol(tmp);

						mir_tstrcpy(tmp, pos + 1);
						strtrim(tmp);
						commonData.popupTimeoutToday = _ttol(tmp);

					}
					else commonData.popupTimeout = commonData.popupTimeoutToday = _ttol(buffer);

					GetDlgItemText(hWnd, IDC_SOUND_NEAR_DAYS_EDIT, buffer, _countof(buffer));
					//cSoundNearDays = _ttol(buffer);
					commonData.cSoundNearDays = _tcstol(buffer, &stop, 10);
					if (*stop) { commonData.cSoundNearDays = BIRTHDAY_NEAR_DEFAULT_DAYS; }

					GetDlgItemText(hWnd, IDC_DLG_TIMEOUT, buffer, _countof(buffer));
					commonData.cDlgTimeout = _tcstol(buffer, &stop, 10);
					if (*stop) { commonData.cDlgTimeout = POPUP_TIMEOUT; }

					db_set_b(NULL, ModuleName, "IgnoreSubcontacts", commonData.bIgnoreSubcontacts);
					db_set_b(NULL, ModuleName, "UsePopups", commonData.bUsePopups);
					db_set_b(NULL, ModuleName, "UseDialog", commonData.bUseDialog);
					db_set_w(NULL, ModuleName, "CheckInterval", commonData.checkInterval);
					db_set_w(NULL, ModuleName, "DaysInAdvance", commonData.daysInAdvance);
					db_set_w(NULL, ModuleName, "DaysAfter", commonData.daysAfter);
					db_set_w(NULL, ModuleName, "PopupTimeout", commonData.popupTimeout);
					db_set_w(NULL, ModuleName, "PopupTimeoutToday", commonData.popupTimeoutToday);

					db_set_b(NULL, ModuleName, "ShowCurrentAge", commonData.cShowAgeMode);
					db_set_b(NULL, ModuleName, "NoBirthdaysPopup", commonData.bNoBirthdaysPopup);

					db_set_b(NULL, ModuleName, "OpenInBackground", commonData.bOpenInBackground);

					db_set_b(NULL, ModuleName, "SoundNearDays", commonData.cSoundNearDays);

					db_set_b(NULL, ModuleName, "DefaultModule", commonData.cDefaultModule);

					db_set_b(NULL, ModuleName, "PopupLeftClick", commonData.lPopupClick);
					db_set_b(NULL, ModuleName, "PopupRightClick", commonData.rPopupClick);

					db_set_dw(NULL, ModuleName, "Foreground", commonData.foreground);
					db_set_dw(NULL, ModuleName, "Background", commonData.background);

					db_set_b(NULL, ModuleName, "OncePerDay", commonData.bOncePerDay);

					db_set_w(NULL, ModuleName, "DlgTimeout", commonData.cDlgTimeout);

					db_set_b(NULL, ModuleName, "NotifyFor", commonData.notifyFor);

					RefreshAllContactListIcons();

					UpdateTimers(); //interval might get changed
				}
				bInitializing = 0;
			}
			break;
		}
		break;
	}
	return 0;
}

INT_PTR CALLBACK DlgProcAddBirthday(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	MCONTACT hContact = (MCONTACT)GetWindowLongPtr(hWnd, GWLP_USERDATA);

	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWnd);
		SetWindowLongPtr(hWnd, GWLP_USERDATA, lParam);
		{
			MCONTACT hContact = lParam;
			WindowList_Add(hAddBirthdayWndsList, hWnd, hContact);
			Utils_RestoreWindowPositionNoSize(hWnd, hContact, ModuleName, "BirthdayWnd");
		}
		SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)IcoLib_GetIconByHandle(hAddBirthdayContact, 1));
		{
			for (int i = 0; i < cSaveModule; i++)
				SendDlgItemMessage(hWnd, IDC_COMPATIBILITY, CB_ADDSTRING, 0, (LPARAM)TranslateTS(szSaveModule[i]));

			SendDlgItemMessage(hWnd, IDC_COMPATIBILITY, CB_SETCURSEL, commonData.cDefaultModule, 0);
		}
		break;

	case WM_SHOWWINDOW:
		{
			TCHAR *szTooltipText = TranslateT("Please select the module where you want the date of birth to be saved.\r\n\"UserInfo\" is the default location.\r\nUse \"Protocol module\" to make the data visible in User Details.\n\"mBirthday module\" uses the same module as mBirthday plugin.");
			TCHAR *szCurrentModuleTooltip = NULL;
			char *szProto = GetContactProto(hContact);

			TCHAR buffer[2048];
			mir_sntprintf(buffer, _countof(buffer), TranslateT("Set birthday for %s:"), pcli->pfnGetContactDisplayName(hContact, 0));
			SetWindowText(hWnd, buffer);

			HWND hDate = GetDlgItem(hWnd, IDC_DATE);

			int year, month, day;
			int loc = GetContactDOB(hContact, year, month, day);
			if (IsDOBValid(year, month, day)) {
				SYSTEMTIME st = { 0 };
				st.wDay = day;
				st.wMonth = month;
				st.wYear = year;
				DateTime_SetSystemtime(hDate, GDT_VALID, &st);
			}
			else DateTime_SetSystemtime(hDate, GDT_NONE, NULL);

			switch (loc) {
			case DOB_MBIRTHDAY:
				DateTime_SetMonthCalColor(hDate, MCSC_TITLEBK, COLOR_MBIRTHDAY);
				szCurrentModuleTooltip = _T("mBirthday");
				break;

			case DOB_PROTOCOL:
				DateTime_SetMonthCalColor(hDate, MCSC_TITLEBK, COLOR_PROTOCOL);
				mir_sntprintf(buffer, _countof(buffer), TranslateT("%S protocol"), szProto);
				szCurrentModuleTooltip = buffer;
				break;

			case DOB_BIRTHDAYREMINDER:
				DateTime_SetMonthCalColor(hDate, MCSC_TITLEBK, COLOR_BIRTHDAYREMINDER);
				szCurrentModuleTooltip = _T("Birthday Reminder");
				break;

			case DOB_USERINFO:
				DateTime_SetMonthCalColor(hDate, MCSC_TITLEBK, COLOR_USERINFO);
				szCurrentModuleTooltip = _T("UserInfo");
				break;

			case DOB_MICQBIRTHDAY:
				DateTime_SetMonthCalColor(hDate, MCSC_TITLEBK, COLOR_MICQBIRTHDAY);
				szCurrentModuleTooltip = _T("mICQBirthday");
				break;

			default:
				szCurrentModuleTooltip = NULL;
				break;
			}

			CreateToolTip(GetDlgItem(hWnd, IDC_COMPATIBILITY), szTooltipText, 500);
			if (szCurrentModuleTooltip)
				CreateToolTip(hDate, szCurrentModuleTooltip, 400);
		}
		break;

	case WM_DESTROY:
		RefreshContactListIcons(hContact); //the birthday might be changed, refresh icon.
		IcoLib_ReleaseIcon((HICON)SendMessage(hWnd, WM_GETICON, ICON_BIG, 0));
		Utils_SaveWindowPosition(hWnd, hContact, ModuleName, "BirthdayWnd");
		WindowList_Remove(hAddBirthdayWndsList, hWnd);
		break;

	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
			SYSTEMTIME st;
			MCONTACT hContact = (MCONTACT)GetWindowLongPtr(hWnd, GWLP_USERDATA);
			HWND hDate = GetDlgItem(hWnd, IDC_DATE);
			if (DateTime_GetSystemtime(hDate, &st) == GDT_VALID) {
				int mode = SendDlgItemMessage(hWnd, IDC_COMPATIBILITY, CB_GETCURSEL, 0, 0); //SAVE modes  in date_utils.h are synced
				SaveBirthday(hContact, st.wYear, st.wMonth, st.wDay, mode);
			}
			else SaveBirthday(hContact, 0, 0, 0, SAVE_MODE_DELETEALL);

			if (hBirthdaysDlg != NULL)
				SendMessage(hBirthdaysDlg, WWIM_UPDATE_BIRTHDAY, hContact, NULL);

			SendMessage(hWnd, WM_CLOSE, 0, 0);
		}
		break;
	}

	return FALSE;
}

void AddAnchorWindowToDeferList(HDWP &hdWnds, HWND window, RECT *rParent, WINDOWPOS *wndPos, int anchors)
{
	if (NULL == window) /* Wine fix. */
		return;
	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);
}


#define NA TranslateT("N/A")

TCHAR* GetBirthdayModule(int module, MCONTACT)
{
	switch (module) {
	case DOB_MBIRTHDAY:        return _T("mBirthday");
	case DOB_PROTOCOL:         return TranslateT("Protocol module");
	case DOB_BIRTHDAYREMINDER: return _T("Birthday Reminder");
	case DOB_USERINFO:         return _T("UserInfo");
	case DOB_MICQBIRTHDAY:     return _T("mICQBirthday");
	}
	return NA;
}

static int lastColumn = -1;

struct BirthdaysSortParams
{
	HWND hList;
	int column;
};

INT_PTR CALLBACK BirthdaysCompare(LPARAM lParam1, LPARAM lParam2, LPARAM myParam)
{
	BirthdaysSortParams params = *(BirthdaysSortParams *)myParam;
	const int maxSize = 1024;
	TCHAR text1[maxSize];
	TCHAR text2[maxSize];
	long value1, value2;
	ListView_GetItemText(params.hList, (int)lParam1, params.column, text1, _countof(text1));
	ListView_GetItemText(params.hList, (int)lParam2, params.column, text2, _countof(text2));

	int res = 0;

	if ((params.column == 2) || (params.column == 4)) {
		TCHAR *err1, *err2;
		value1 = _tcstol(text1, &err1, 10);
		value2 = _tcstol(text2, &err2, 10);

		if ((err1[0]) || (err2[0]))
			res = (err1[0]) ? 1 : -1;
		else if (value1 < value2)
			res = -1;
		else
			res = (value1 != value2);
	}
	else res = mir_tstrcmpi(text1, text2);

	res = (params.column == lastColumn) ? -res : res;
	return res;
}

//only updates the birthday part of the list view entry. Won't update the szProto and the contact name (those shouldn't change anyway :))
int UpdateBirthdayEntry(HWND hList, MCONTACT hContact, int entry, int bShowAll, int bShowCurrentAge, int bAdd)
{
	int currentMonth, currentDay;
	int res = entry;

	if (bShowCurrentAge) {
		time_t now = Today();
		struct tm *today = gmtime(&now);
		currentDay = today->tm_mday + 1;
		currentMonth = today->tm_mon + 1;
	}

	int year, month, day;
	int module = GetContactDOB(hContact, year, month, day);
	if (bShowAll || IsDOBValid(year, month, day)) {
		lastColumn = -1; //list isn't sorted anymore
		int dtb = DaysToBirthday(Today(), year, month, day);
		int age = GetContactAge(hContact);
		if (bShowCurrentAge)
			if (month > currentMonth || (month == currentMonth) && (day > currentDay)) // birthday still to come
				age--;

		char *szProto = GetContactProto(hContact);
		PROTOACCOUNT *pAcc = Proto_GetAccount(szProto);
		TCHAR *ptszAccName = (pAcc == NULL) ? TranslateT("Unknown") : pAcc->tszAccountName;

		LVITEM item = { 0 };
		item.mask = LVIF_TEXT | LVIF_PARAM;
		item.iItem = entry;
		item.lParam = hContact;
		item.pszText = ptszAccName;

		if (bAdd)
			ListView_InsertItem(hList, &item);
		else
			ListView_SetItemText(hList, entry, 0, ptszAccName);

		ListView_SetItemText(hList, entry, 1, pcli->pfnGetContactDisplayName(hContact, 0));

		TCHAR buffer[2048];
		if ((dtb <= 366) && (dtb >= 0))
			mir_sntprintf(buffer, _countof(buffer), _T("%d"), dtb);
		else
			mir_sntprintf(buffer, _countof(buffer), NA);

		ListView_SetItemText(hList, entry, 2, buffer);
		if ((month != 0) && (day != 0))
			mir_sntprintf(buffer, _countof(buffer), _T("%04d-%02d-%02d"), year, month, day);
		else
			mir_sntprintf(buffer, _countof(buffer), NA);

		ListView_SetItemText(hList, entry, 3, buffer);

		if (age < 400 && age > 0) //hopefully noone lives longer than this :)
			mir_sntprintf(buffer, _countof(buffer), _T("%d"), age);
		else
			mir_sntprintf(buffer, _countof(buffer), NA);

		ListView_SetItemText(hList, entry, 4, buffer);
		ListView_SetItemText(hList, entry, 5, GetBirthdayModule(module, hContact));

		res++;
	}
	else if (!bShowAll && !bAdd)
		ListView_DeleteItem(hList, entry);

	return res;
}

static LRESULT CALLBACK BirthdaysListSubclassProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	LVITEM item = { 0 };
	MCONTACT hContact;
	int i, count;

	switch (msg) {
	case WM_KEYUP:
		if (wParam == VK_ESCAPE)
			SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);
		break;

	case WM_SYSKEYDOWN:
		if (wParam == 'X')
			SendMessage(GetParent(hWnd), WM_CLOSE, 0, 0);
		break;

	case WM_LBUTTONDBLCLK:
		count = ListView_GetItemCount(hWnd);
		item.mask = LVIF_PARAM;
		for (i = 0; i < count; i++) {
			if (ListView_GetItemState(hWnd, i, LVIS_SELECTED)) {
				item.iItem = i;
				ListView_GetItem(hWnd, &item);
				hContact = (MCONTACT)item.lParam;
				CallService(MS_WWI_ADD_BIRTHDAY, hContact, 0);
				break;
			}
		}
		break;

	case WM_CONTEXTMENU:
		POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
		count = ListView_GetItemCount(hWnd);
		item.mask = LVIF_PARAM;
		for (i = 0; i < count; i++) {
			if (ListView_GetItemState(hWnd, i, LVIS_SELECTED)) {
				item.iItem = i;
				ListView_GetItem(hWnd, &item);
				hContact = (MCONTACT)item.lParam;
				HMENU hMenu = Menu_BuildContactMenu(hContact);
				if (hMenu != NULL) {
					CallService(MS_CLIST_MENUPROCESSCOMMAND, MAKEWPARAM(TrackPopupMenu(hMenu, TPM_RIGHTBUTTON | TPM_RETURNCMD, pt.x, pt.y, 0, hWnd, NULL), MPCF_CONTACTMENU), hContact);
					DestroyMenu(hMenu);
				}
				break;
			}
		}
		break;
	}
	return mir_callNextSubclass(hWnd, BirthdaysListSubclassProc, msg, wParam, lParam);
}

void SetBirthdaysCount(HWND hWnd)
{
	int count = ListView_GetItemCount((GetDlgItem(hWnd, IDC_BIRTHDAYS_LIST)));
	TCHAR title[512];
	mir_sntprintf(title, _countof(title), TranslateT("Birthday list (%d)"), count);
	SetWindowText(hWnd, title);
}

int LoadBirthdays(HWND hWnd, int bShowAll)
{
	HWND hList = GetDlgItem(hWnd, IDC_BIRTHDAYS_LIST);
	ListView_DeleteAllItems(hList);

	int count = 0;
	for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact))
		count = UpdateBirthdayEntry(hList, hContact, count, bShowAll, commonData.cShowAgeMode, 1);

	SetBirthdaysCount(hWnd);
	return 0;
}

INT_PTR CALLBACK DlgProcBirthdays(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWnd);
		SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)IcoLib_GetIconByHandle(hListMenu));
		{
			HWND hList = GetDlgItem(hWnd, IDC_BIRTHDAYS_LIST);

			ListView_SetExtendedListViewStyleEx(hList, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

			mir_subclassWindow(hList, BirthdaysListSubclassProc);

			LVCOLUMN col;
			col.mask = LVCF_TEXT | LVCF_WIDTH;
			col.pszText = TranslateT("Protocol");
			col.cx = 80;
			ListView_InsertColumn(hList, 0, &col);
			col.pszText = TranslateT("Contact");
			col.cx = 180;
			ListView_InsertColumn(hList, 1, &col);
			col.pszText = TranslateT("DTB");
			col.cx = 50;
			ListView_InsertColumn(hList, 2, &col);
			col.pszText = TranslateT("Birthday");
			col.cx = 80;
			ListView_InsertColumn(hList, 3, &col);
			col.pszText = TranslateT("Age");
			col.cx = 50;
			ListView_InsertColumn(hList, 4, &col);
			col.pszText = TranslateT("Module");
			col.cx = 108;
			ListView_InsertColumn(hList, 5, &col);

			LoadBirthdays(hWnd, 0);
			int column = db_get_b(NULL, ModuleName, "SortColumn", 0);

			BirthdaysSortParams params = { 0 };
			params.hList = GetDlgItem(hWnd, IDC_BIRTHDAYS_LIST);
			params.column = column;
			ListView_SortItemsEx(params.hList, BirthdaysCompare, (LPARAM)&params);

			Utils_RestoreWindowPosition(hWnd, NULL, ModuleName, "BirthdayList");
		}
		return TRUE;

	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;

	case WWIM_UPDATE_BIRTHDAY:
		{
			MCONTACT hContact = wParam;
			HWND hList = GetDlgItem(hWnd, IDC_BIRTHDAYS_LIST);
			LVFINDINFO fi = { 0 };

			fi.flags = LVFI_PARAM;
			fi.lParam = (LPARAM)hContact;
			int idx = ListView_FindItem(hList, -1, &fi);
			if (-1 == idx)
				UpdateBirthdayEntry(hList, hContact, ListView_GetItemCount(hList), IsDlgButtonChecked(hWnd, IDC_SHOW_ALL), commonData.cShowAgeMode, 1);
			else
				UpdateBirthdayEntry(hList, hContact, idx, IsDlgButtonChecked(hWnd, IDC_SHOW_ALL), commonData.cShowAgeMode, 0);
			SetBirthdaysCount(hWnd);
		}
		break;

	case WM_WINDOWPOSCHANGING:
		{
			HDWP hdWnds = BeginDeferWindowPos(2);
			RECT rParent;
			WINDOWPOS *wndPos = (WINDOWPOS *)lParam;
			GetWindowRect(hWnd, &rParent);

			if (wndPos->cx < MIN_BIRTHDAYS_WIDTH)
				wndPos->cx = MIN_BIRTHDAYS_WIDTH;

			if (wndPos->cy < MIN_BIRTHDAYS_HEIGHT)
				wndPos->cy = MIN_BIRTHDAYS_HEIGHT;

			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_CLOSE), &rParent, wndPos, ANCHOR_RIGHT | ANCHOR_BOTTOM);
			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_SHOW_ALL), &rParent, wndPos, ANCHOR_LEFT | ANCHOR_BOTTOM);
			AddAnchorWindowToDeferList(hdWnds, GetDlgItem(hWnd, IDC_BIRTHDAYS_LIST), &rParent, wndPos, ANCHOR_ALL);

			EndDeferWindowPos(hdWnds);
		}
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_CLOSE:
			SendMessage(hWnd, WM_CLOSE, 0, 0);
			break;

		case IDC_SHOW_ALL:
			LoadBirthdays(hWnd, IsDlgButtonChecked(hWnd, IDC_SHOW_ALL));
			break;
		}
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom) {
		case IDC_BIRTHDAYS_LIST:
			switch (((LPNMHDR)lParam)->code) {
			case LVN_COLUMNCLICK:
				LPNMLISTVIEW lv = (LPNMLISTVIEW)lParam;
				int column = lv->iSubItem;
				db_set_b(NULL, ModuleName, "SortColumn", column);
				BirthdaysSortParams params = { 0 };
				params.hList = GetDlgItem(hWnd, IDC_BIRTHDAYS_LIST);
				params.column = column;
				ListView_SortItemsEx(params.hList, BirthdaysCompare, (LPARAM)&params);
				lastColumn = (params.column == lastColumn) ? -1 : params.column;
				break;
			}
		}
		break;

	case WM_DESTROY:
		hBirthdaysDlg = NULL;
		Utils_SaveWindowPosition(hWnd, NULL, ModuleName, "BirthdayList");
		IcoLib_ReleaseIcon((HICON)SendMessage(hWnd, WM_GETICON, ICON_BIG, 0));
		lastColumn = -1;
		break;

	}
	return 0;
}


INT_PTR CALLBACK DlgProcUpcoming(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int timeout;

	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hWnd);
		{
			timeout = commonData.cDlgTimeout;
			SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)IcoLib_GetIconByHandle(hListMenu));
			HWND hList = GetDlgItem(hWnd, IDC_UPCOMING_LIST);

			mir_subclassWindow(hList, BirthdaysListSubclassProc);
			ListView_SetExtendedListViewStyleEx(hList, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

			LVCOLUMN col;
			col.mask = LVCF_TEXT | LVCF_WIDTH;
			col.pszText = TranslateT("Contact");
			col.cx = 300;
			ListView_InsertColumn(hList, 0, &col);
			col.pszText = TranslateT("Age");
			col.cx = 45;
			ListView_InsertColumn(hList, 1, &col);
			col.pszText = TranslateT("DTB");
			col.cx = 45;
			ListView_InsertColumn(hList, 2, &col);

			ListView_SetColumnWidth(hList, 0, LVSCW_AUTOSIZE);

			if (timeout > 0)
				SetTimer(hWnd, UPCOMING_TIMER_ID, 1000, NULL);
			Utils_RestoreWindowPosition(hWnd, NULL, ModuleName, "BirthdayListUpcoming");
		}
		return TRUE;

	case WM_TIMER:
		{
			const int MAX_SIZE = 512;
			TCHAR buffer[MAX_SIZE];
			timeout--;
			mir_sntprintf(buffer, _countof(buffer), (timeout != 2) ? TranslateT("Closing in %d seconds") : TranslateT("Closing in %d second"), timeout);
			SetDlgItemText(hWnd, IDC_CLOSE, buffer);

			if (timeout <= 0)
				SendMessage(hWnd, WM_CLOSE, 0, 0);
		}
		break;

	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;

	case WWIM_ADD_UPCOMING_BIRTHDAY:
		{
			PUpcomingBirthday data = (PUpcomingBirthday)wParam;

			HWND hList = GetDlgItem(hWnd, IDC_UPCOMING_LIST);
			LVITEM item = { 0 };
			LVFINDINFO fi = { 0 };

			fi.flags = LVFI_PARAM;
			fi.lParam = (LPARAM)data->hContact;
			if (-1 != ListView_FindItem(hList, -1, &fi))
				return 0; /* Allready in list. */

			int index = ListView_GetItemCount(hList);
			item.iItem = index;
			item.mask = LVIF_PARAM | LVIF_TEXT;
			item.lParam = (LPARAM)data->hContact;
			item.pszText = data->message;
			ListView_InsertItem(hList, &item);

			TCHAR buffer[512];
			mir_sntprintf(buffer, _countof(buffer), _T("%d"), data->age);
			ListView_SetItemText(hList, index, 1, buffer);
			mir_sntprintf(buffer, _countof(buffer), _T("%d"), data->dtb);
			ListView_SetItemText(hList, index, 2, buffer);

			BirthdaysSortParams params = { 0 };
			params.hList = hList;
			params.column = 2;
			ListView_SortItemsEx(hList, BirthdaysCompare, (LPARAM)&params);
		}
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDC_CLOSE:
			SendMessage(hWnd, WM_CLOSE, 0, 0);
			break;
		}
		break;

	case WM_GETMINMAXINFO:
		((LPMINMAXINFO)lParam)->ptMinTrackSize.x = 400;
		((LPMINMAXINFO)lParam)->ptMinTrackSize.y = 160;
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.x = 600;
		((LPMINMAXINFO)lParam)->ptMaxTrackSize.y = 530;
		break;

	case WM_SIZE:
		{
			HWND hList = GetDlgItem(hWnd, IDC_UPCOMING_LIST);

			RECT rcWin;
			GetWindowRect(hWnd, &rcWin);

			int cx = rcWin.right - rcWin.left;
			int cy = rcWin.bottom - rcWin.top;
			SetWindowPos(hList, NULL, 0, 0, (cx - 30), (cy - 80), (SWP_NOZORDER | SWP_NOMOVE));
			ListView_SetColumnWidth(hList, 0, (cx - 150));
			SetWindowPos(GetDlgItem(hWnd, IDC_CLOSE), NULL, ((cx / 2) - 95), (cy - 67), 0, 0, SWP_NOSIZE);
			RedrawWindow(hWnd, NULL, NULL, (RDW_FRAME | RDW_INVALIDATE));
		}
		break;

	case WM_DESTROY:
		hUpcomingDlg = NULL;
		Utils_SaveWindowPosition(hWnd, NULL, ModuleName, "BirthdayListUpcoming");
		IcoLib_ReleaseIcon((HICON)SendMessage(hWnd, WM_GETICON, ICON_BIG, 0));
		KillTimer(hWnd, UPCOMING_TIMER_ID);
		break;
	}

	return 0;
}

int HandlePopupClick(HWND hWnd, int action)
{
	MCONTACT hContact;

	switch (action) {
	case 2: //OPEN MESSAGE WINDOW
		hContact = (MCONTACT)PUGetContact(hWnd);
		if (hContact)
			CallServiceSync(MS_MSG_SENDMESSAGE, hContact, 0);

	case 1: //DISMISS
		PUDeletePopup(hWnd);
		break;
	}

	return 0;
}

LRESULT CALLBACK DlgProcPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_COMMAND:
		switch (HIWORD(wParam)) {
		case STN_CLICKED:
			HandlePopupClick(hWnd, commonData.lPopupClick);
			break;
		}
		break;

	case WM_CONTEXTMENU:
		HandlePopupClick(hWnd, commonData.rPopupClick);
		break;
	}

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