summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/ctrl_contact.cpp
blob: 95abf7ae15e48a312e3436697465248d63b01b11 (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
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
/*
UserinfoEx plugin for Miranda IM

Copyright:
© 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

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 MAX_CAT			64

//#define BTN_MENU		(WM_USER+1)
//#define BTN_EDIT		(WM_USER+2)
#define EDIT_VALUE		(WM_USER+3)

#define CBEXM_MENIITEMFIRST (WM_USER+100)	// caution: after this message there must be room for all menuitem commnds
											// so it should be the last message with the highest value

typedef struct TCbExItem
{
	WORD	wMask;
	WORD	wFlags;
	DWORD	dwID;
	wchar_t	szCat[MAX_CAT];
	LPTSTR	pszVal;
	int   iIcon;
	HICON	hIcon;
} CBEXITEMINTERN, *LPCBEXITEMINTERN;

typedef struct TComboEx
{
	LPCBEXITEMINTERN	pItems;
	int			numItems;
	int			numOther;
	int			iSelectedItem;

	BYTE		bIsChanged;
	BYTE		bLocked;
	BYTE		bIsEditChanged;

	HINSTANCE	hInstance;
	HFONT		hFont;
	HWND		hEdit;
	HWND		hBtnMenu;
	HWND		hBtnEdit;
	HWND		hBtnAdd;
	HWND		hBtnDel;
	RECT		rect;
} CBEX, *LPCBEX;


static int compareProc(LPCVOID cbi1, LPCVOID cbi2)
{
	return mir_wstrcmp(((LPCBEXITEMINTERN)cbi1)->szCat, ((LPCBEXITEMINTERN)cbi2)->szCat);
}

static int CheckPhoneSyntax(LPTSTR pszSrc, LPTSTR szNumber, WORD cchNumber, int& errorPos)
{
	int lenNum = 0;
	BYTE	hasLeftBreaket = FALSE,
		hasRightBreaket = FALSE;

	if (!szNumber || !pszSrc || !*pszSrc || !cchNumber) return 0;
	*szNumber = 0;
	errorPos = -1;

	if (*pszSrc != '+') {
		errorPos = 2; // set cursor after first digit
		*(szNumber + lenNum++) = '+';
	}
	else
		*(szNumber + lenNum++) = *(pszSrc++);

	for (; lenNum < cchNumber - 1 && *pszSrc != 0; pszSrc++) {
		switch (*pszSrc) {
		case '(':
			if (hasLeftBreaket) {
				if (errorPos == -1) errorPos = lenNum;
				break;
			}
			if (*(szNumber + lenNum - 1) != ' ') {
				*(szNumber + lenNum++) = ' ';
				if (errorPos == -1) errorPos = lenNum + 1;
			}
			*(szNumber + lenNum++) = *pszSrc;
			hasLeftBreaket = TRUE;
			break;

		case ')':
			if (hasRightBreaket) {
				if (errorPos == -1) errorPos = lenNum;
				break;
			}
			*(szNumber + lenNum++) = *pszSrc;
			if (*(pszSrc + 1) != ' ') {
				*(szNumber + lenNum++) = ' ';
				if (errorPos == -1) errorPos = lenNum;
			}
			hasRightBreaket = TRUE;
			break;

		case ' ':
			if (*(szNumber + lenNum - 1) != ' ') {
				*(szNumber + lenNum++) = *pszSrc;
			}
			else
				if (errorPos == -1) errorPos = lenNum;
			break;

		default:
			if (*pszSrc >= '0' && *pszSrc <= '9' || *pszSrc == '-') {
				*(szNumber + lenNum++) = *pszSrc;
			}
			// remember first error position
			else if (errorPos == -1) errorPos = lenNum;
			break;
		}
	}
	*(szNumber + lenNum) = 0;
	return lenNum;
}

/**
 * name:	 DlgProc_EditEMail()
 * desc:	 dialog procedure
 *
 * return:	 0 or 1
 **/
static INT_PTR CALLBACK DlgProc_EMail(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	CBEXITEM *cbi = (CBEXITEM*)GetUserData(hDlg);

	switch (msg) {
	case WM_INITDIALOG:
		cbi = (CBEXITEM*)lParam;
		if (!cbi)
			return FALSE;
		SetUserData(hDlg, lParam);

		SendDlgItemMessage(hDlg, IDC_HEADERBAR, WM_SETICON, 0, (LPARAM)g_plugin.getIcon(IDI_DLG_EMAIL, true));
		if (g_plugin.getByte(SET_ICONS_BUTTONS, 1)) {
			SendDlgItemMessage(hDlg, IDOK, BM_SETIMAGE, IMAGE_ICON, (LPARAM)g_plugin.getIcon(IDI_BTN_OK));
			SendDlgItemMessage(hDlg, IDCANCEL, BM_SETIMAGE, IMAGE_ICON, (LPARAM)g_plugin.getIcon(IDI_BTN_CLOSE));
		}

		if (*cbi->pszVal)
			SetWindowText(hDlg, LPGENW("Edit e-mail"));
		TranslateDialogDefault(hDlg);
		SendDlgItemMessage(hDlg, EDIT_CATEGORY, EM_LIMITTEXT, cbi->ccCat - 1, 0);
		SendDlgItemMessage(hDlg, EDIT_EMAIL, EM_LIMITTEXT, cbi->ccVal - 1, 0);
		SetDlgItemText(hDlg, EDIT_CATEGORY, cbi->pszCat);
		SetDlgItemText(hDlg, EDIT_EMAIL, cbi->pszVal);
		EnableWindow(GetDlgItem(hDlg, EDIT_CATEGORY), !(cbi->wFlags & CBEXIF_CATREADONLY));
		EnableWindow(GetDlgItem(hDlg, IDOK), *cbi->pszVal);

		// translate Userinfo buttons
		{
			wchar_t szButton[MAX_PATH];
			HWND hBtn = GetDlgItem(hDlg, IDOK);
			GetWindowText(hBtn, szButton, _countof(szButton));
			SetWindowText(hBtn, TranslateW(szButton));

			hBtn = GetDlgItem(hDlg, IDCANCEL);
			GetWindowText(hBtn, szButton, _countof(szButton));
			SetWindowText(hBtn, TranslateW(szButton));
		}
		return TRUE;

	case WM_CTLCOLORSTATIC:
		SetBkColor((HDC)wParam, RGB(255, 255, 255));
		return (INT_PTR)GetStockObject(WHITE_BRUSH);

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
			{
				if (HIWORD(wParam) == BN_CLICKED) {
					if (cbi->pszVal && cbi->ccVal > 0)
						GetDlgItemText(hDlg, EDIT_EMAIL, cbi->pszVal, cbi->ccVal);
					if (cbi->pszCat && cbi->ccCat > 0)
						GetDlgItemText(hDlg, EDIT_CATEGORY, cbi->pszCat, cbi->ccCat);
				}
				break;
			}
		case IDCANCEL:
			{
				if (HIWORD(wParam) == BN_CLICKED)
					EndDialog(hDlg, LOWORD(wParam));
				break;
			}
		case EDIT_EMAIL:
			if (HIWORD(wParam) == EN_UPDATE) {
				wchar_t szText[MAXDATASIZE];
				LPTSTR pszAdd, pszDot;
				if (PtrIsValid(cbi)) {
					GetWindowText((HWND)lParam, szText, _countof(szText));
					EnableWindow(GetDlgItem(hDlg, IDOK),
						((pszAdd = wcschr(szText, '@')) &&
							*(pszAdd + 1) != '.' &&
							(pszDot = wcschr(pszAdd, '.')) &&
							*(pszDot + 1) &&
							mir_wstrcmp(szText, cbi->pszVal)));
				}
			}
			break;
		}
		break;
	}
	return FALSE;
}

/**
 * name:	 DlgProc_EditPhone()
 * desc:	 dialog procedure
 *
 * return:	 0 or 1
 **/
INT_PTR CALLBACK DlgProc_Phone(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	CBEXITEM *cbi = (CBEXITEM*)GetUserData(hDlg);
	static int noRecursion = 0;

	switch (msg) {
	case WM_INITDIALOG:
		{
			UINT i, item, countryCount;
			LPIDSTRLIST	pCountries;
			HWND hCombo = GetDlgItem(hDlg, EDIT_COUNTRY);

			cbi = (CBEXITEM*)lParam;
			if (!cbi) return FALSE;
			SetUserData(hDlg, lParam);

			SendDlgItemMessage(hDlg, IDC_HEADERBAR, WM_SETICON, 0, (LPARAM)g_plugin.getIcon(IDI_DLG_PHONE, TRUE));
			if (g_plugin.getByte(SET_ICONS_BUTTONS, 1)) {
				SendDlgItemMessage(hDlg, IDOK, BM_SETIMAGE, IMAGE_ICON, (LPARAM)g_plugin.getIcon(IDI_BTN_OK));
				SendDlgItemMessage(hDlg, IDCANCEL, BM_SETIMAGE, IMAGE_ICON, (LPARAM)g_plugin.getIcon(IDI_BTN_CLOSE));
			}

			// translate Userinfo buttons
			{
				wchar_t szButton[MAX_PATH];
				HWND hBtn;

				hBtn = GetDlgItem(hDlg, IDOK);
				GetWindowText(hBtn, szButton, _countof(szButton));
				SetWindowText(hBtn, TranslateW(szButton));
				hBtn = GetDlgItem(hDlg, IDCANCEL);
				GetWindowText(hBtn, szButton, _countof(szButton));
				SetWindowText(hBtn, TranslateW(szButton));
			}
			if (*cbi->pszVal) SetWindowText(hDlg, LPGENW("Edit phone number"));
			if (cbi->wFlags & CBEXIF_SMS) CheckDlgButton(hDlg, CHECK_SMS, BST_CHECKED);
			TranslateDialogDefault(hDlg);

			EnableWindow(GetDlgItem(hDlg, IDOK), *cbi->pszVal);
			SendDlgItemMessage(hDlg, EDIT_AREA, EM_LIMITTEXT, 31, 0);
			SendDlgItemMessage(hDlg, EDIT_NUMBER, EM_LIMITTEXT, 63, 0);
			SendDlgItemMessage(hDlg, EDIT_CATEGORY, EM_LIMITTEXT, cbi->ccCat - 1, 0);
			SendDlgItemMessage(hDlg, EDIT_PHONE, EM_LIMITTEXT, cbi->ccVal - 1, 0);

			GetCountryList(&countryCount, &pCountries);
			for (i = 0; i < countryCount; i++) {
				if (pCountries[i].nID == 0 || pCountries[i].nID == 0xFFFF) continue;
				item = SendMessage(hCombo, CB_ADDSTRING, NULL, (LPARAM)pCountries[i].ptszTranslated);
				SendMessage(hCombo, CB_SETITEMDATA, item, pCountries[i].nID);
			}

			SetDlgItemText(hDlg, EDIT_PHONE, cbi->pszVal);
			SetDlgItemText(hDlg, EDIT_CATEGORY, cbi->pszCat);
			EnableWindow(GetDlgItem(hDlg, EDIT_CATEGORY), !(cbi->wFlags & CBEXIF_CATREADONLY));
		}
		return TRUE;

	case WM_CTLCOLORSTATIC:
		SetBkColor((HDC)wParam, RGB(255, 255, 255));
		return (INT_PTR)GetStockObject(WHITE_BRUSH);

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDOK:
			if (HIWORD(wParam) == BN_CLICKED) {
				wchar_t szText[MAXDATASIZE];
				int errorPos;

				if (!GetDlgItemText(hDlg, EDIT_PHONE, szText, _countof(szText)) || !CheckPhoneSyntax(szText, cbi->pszVal, cbi->ccVal, errorPos) || errorPos > -1) {
					MsgErr(hDlg, TranslateT("The phone number should start with a + and consist of\nnumbers, spaces, brackets and hyphens only."));
					break;
				}
				// save category string
				GetDlgItemText(hDlg, EDIT_CATEGORY, cbi->pszCat, cbi->ccCat);

				// save SMS flag
				if ((int)IsDlgButtonChecked(hDlg, CHECK_SMS) != ((cbi->wFlags & CBEXIF_SMS) == CBEXIF_SMS))
					cbi->wFlags ^= CBEXIF_SMS;
			}
			__fallthrough;

		case IDCANCEL:
			if (HIWORD(wParam) == BN_CLICKED)
				EndDialog(hDlg, wParam);
			break;

		case EDIT_COUNTRY:
			if (HIWORD(wParam) != CBN_SELCHANGE)
				break;

		case EDIT_AREA:
		case EDIT_NUMBER:
			if (LOWORD(wParam) != EDIT_COUNTRY && HIWORD(wParam) != EN_CHANGE) break;
			if (noRecursion) break;
			EnableWindow(GetDlgItem(hDlg, IDOK), TRUE);
			{
				wchar_t szPhone[MAXDATASIZE], szArea[32], szData[64];
				int	 nCurSel = SendDlgItemMessage(hDlg, EDIT_COUNTRY, CB_GETCURSEL, 0, 0);
				UINT	nCountry = (nCurSel != CB_ERR) ? SendDlgItemMessage(hDlg, EDIT_COUNTRY, CB_GETITEMDATA, nCurSel, 0) : 0;

				GetDlgItemText(hDlg, EDIT_AREA, szArea, _countof(szArea));
				GetDlgItemText(hDlg, EDIT_NUMBER, szData, _countof(szData));
				mir_snwprintf(szPhone, L"+%u (%s) %s", nCountry, szArea, szData);
				noRecursion = 1;
				SetDlgItemText(hDlg, EDIT_PHONE, szPhone);
				noRecursion = 0;
			}
			break;

		case EDIT_PHONE:
			if (HIWORD(wParam) != EN_UPDATE) break;
			if (noRecursion) break;
			noRecursion = 1;
			{
				wchar_t szText[MAXDATASIZE], *pText = nullptr, *pArea, *pNumber;
				bool isValid = true;
				GetDlgItemText(hDlg, EDIT_PHONE, szText, _countof(szText));
				if (szText[0] != '+')
					isValid = false;
				if (isValid) {
					int country = wcstol(szText + 1, &pText, 10);
					if (pText - szText > 4)
						isValid = false;
					else {
						int i;
						for (i = SendDlgItemMessage(hDlg, EDIT_COUNTRY, CB_GETCOUNT, 0, 0) - 1; i >= 0; i--) {
							if (country == SendDlgItemMessage(hDlg, EDIT_COUNTRY, CB_GETITEMDATA, i, 0)) {
								SendDlgItemMessage(hDlg, EDIT_COUNTRY, CB_SETCURSEL, i, 0);
								break;
							}
						}
						if (i < 0)
							isValid = false;
					}
				}
				if (isValid) {
					pArea = pText + wcscspn(pText, L"0123456789");
					pText = pArea + _tcsspn(pArea, L"0123456789");
					if (*pText) {
						*pText = '\0';
						pNumber = pText + 1 + wcscspn(pText + 1, L"0123456789");
						SetDlgItemText(hDlg, EDIT_NUMBER, pNumber);
					}
					SetDlgItemText(hDlg, EDIT_AREA, pArea);
				}
				if (!isValid) {
					SendDlgItemMessage(hDlg, EDIT_COUNTRY, CB_SETCURSEL, -1, 0);
					SetDlgItemText(hDlg, EDIT_AREA, L"");
					SetDlgItemText(hDlg, EDIT_NUMBER, L"");
				}
			}
			noRecursion = 0;
			EnableWindow(GetDlgItem(hDlg, IDOK), GetWindowTextLength(GetDlgItem(hDlg, EDIT_PHONE)));
			break;
		}
		break;
	}
	return FALSE;
}

/**
 * name:	CtrlContactWndProc
 * desc:	window procedure for the extended combobox class
 * param:	hwnd	- handle to a extended combobox window
 *			msg		- message to handle
 *			wParam	- message specific
 *			lParam	- message specific
 * return:	message specific
 **/
static LRESULT CALLBACK CtrlContactWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	LPCBEX	cbex = (LPCBEX)GetWindowLongPtr(hwnd, 0);

	switch (msg) {

		/**
		* name:	WM_NCCREATE
		* desc:	is called to initiate the window creation
		* param:	wParam - not used
		*			lParam - pointer to a CREATESTRUCT
		*
		* return:	FALSE on error, TRUE if initialisation was ok
		**/
	case WM_NCCREATE:
		{
			LPCREATESTRUCT cs = (LPCREATESTRUCT)lParam;

			if (!(cbex = (LPCBEX)mir_calloc(1 * sizeof(CBEX))))
				return FALSE;
			SetWindowLongPtr(hwnd, 0, (LONG_PTR)cbex);
			cbex->bLocked = 1;
			cbex->hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			cbex->hInstance = cs->hInstance;
			cbex->iSelectedItem = -1;
			cbex->rect.left = cs->x;
			cbex->rect.top = cs->y;
			cbex->rect.right = cs->x + cs->cx;
			cbex->rect.bottom = cs->y + cs->cy;
			return TRUE;
		}

		/**
			* name:	WM_NCCREATE
			* desc:	is called to create all subitems
			* param:	wParam - not used
			*			lParam - not used
			*
			* return:	FALSE on error, TRUE if initialisation was ok
			**/
	case WM_CREATE:
		{
			WORD wHeight = (WORD)(cbex->rect.bottom - cbex->rect.top);
			WORD wWidth = 130;
			WORD x = 0;

			if (!(cbex->hBtnEdit = CreateWindowEx(WS_EX_NOPARENTNOTIFY,
				UINFOBUTTONCLASS,
				L"none",
				WS_VISIBLE | WS_CHILD | WS_TABSTOP, 0, 0,
				wWidth, wHeight,
				hwnd,
				nullptr,
				cbex->hInstance, nullptr))) {
				cbex->bLocked = 0;
				return FALSE;
			}
			x += wWidth + 2;
			wWidth = wHeight;
			if (!(cbex->hBtnMenu = CreateWindowEx(WS_EX_NOPARENTNOTIFY,
				UINFOBUTTONCLASS,
				nullptr,
				WS_VISIBLE | WS_CHILD | WS_TABSTOP | MBS_PUSHBUTTON | MBS_DOWNARROW,
				x, 0,
				wWidth, wHeight,
				hwnd,
				nullptr,
				cbex->hInstance, nullptr))) {
				DestroyWindow(cbex->hBtnEdit);
				cbex->bLocked = 0;
				return FALSE;
			}
			x += wWidth + 2;
			wWidth = (WORD)(cbex->rect.right - cbex->rect.left - x - (2 * (wHeight + 2)));
			if (!(cbex->hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
				L"Edit",
				nullptr,
				WS_VISIBLE | WS_CHILD | WS_TABSTOP | ES_AUTOHSCROLL,
				x, 1,
				wWidth, wHeight - 2,
				hwnd,
				nullptr,
				cbex->hInstance, nullptr))) {
				DestroyWindow(cbex->hBtnEdit);
				DestroyWindow(cbex->hBtnMenu);
				cbex->bLocked = 0;
				return FALSE;
			}
			x += wWidth + 2;
			wWidth = wHeight;
			if (!(cbex->hBtnAdd = CreateWindowEx(WS_EX_NOPARENTNOTIFY,
				UINFOBUTTONCLASS,
				nullptr,
				WS_VISIBLE | WS_CHILD | WS_TABSTOP | MBS_FLAT,
				x, 0,
				wWidth, wHeight,
				hwnd,
				nullptr,
				cbex->hInstance, nullptr))) {
				DestroyWindow(cbex->hBtnEdit);
				DestroyWindow(cbex->hBtnMenu);
				DestroyWindow(cbex->hEdit);
				cbex->bLocked = 0;
				return FALSE;
			}
			x += wWidth + 2;
			if (!(cbex->hBtnDel = CreateWindowEx(WS_EX_NOPARENTNOTIFY,
				UINFOBUTTONCLASS,
				nullptr,
				WS_VISIBLE | WS_CHILD | WS_TABSTOP | MBS_FLAT,
				x, 0,
				wWidth, wHeight,
				hwnd,
				nullptr,
				cbex->hInstance, nullptr))) {
				DestroyWindow(cbex->hBtnEdit);
				DestroyWindow(cbex->hBtnMenu);
				DestroyWindow(cbex->hEdit);
				DestroyWindow(cbex->hBtnAdd);
				cbex->bLocked = 0;
				return FALSE;
			}

			// set ids
			SetWindowLongPtr(cbex->hBtnEdit, GWLP_ID, BTN_EDIT);
			SetWindowLongPtr(cbex->hBtnMenu, GWLP_ID, BTN_MENU);
			SetWindowLongPtr(cbex->hEdit, GWLP_ID, EDIT_VALUE);
			SetWindowLongPtr(cbex->hBtnAdd, GWLP_ID, BTN_ADD);
			SetWindowLongPtr(cbex->hBtnDel, GWLP_ID, BTN_DEL);
			// set fonts & maximum edit control charachters
			SendMessage(cbex->hEdit, WM_SETFONT, (WPARAM)cbex->hFont, NULL);
			SendMessage(cbex->hEdit, EM_LIMITTEXT, (WPARAM)MAXDATASIZE, NULL);
			// add tooltips
			SendMessage(cbex->hBtnMenu, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Choose the item to display"), MBBF_TCHAR);
			SendMessage(cbex->hBtnEdit, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Edit the currently displayed item"), MBBF_TCHAR);
			SendMessage(cbex->hBtnAdd, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Add a new custom item"), MBBF_TCHAR);
			SendMessage(cbex->hBtnDel, BUTTONADDTOOLTIP, (WPARAM)TranslateT("Delete the selected item"), MBBF_TCHAR);
			// reload icons
			CtrlContactWndProc(hwnd, WM_SETICON, NULL, NULL);
			cbex->bLocked = 0;
			return TRUE;
		}

		/**
			* name:	WM_DESTROY
			* desc:	default destroy message, so clear up memory
			* param:	wParam - not used
			*			lParam - not used
			* return:	return value of DefWindowProc
			**/
	case WM_DESTROY:
		CtrlContactWndProc(hwnd, CBEXM_DELALLITEMS, NULL, NULL);
		DestroyWindow(cbex->hBtnEdit);
		DestroyWindow(cbex->hBtnMenu);
		DestroyWindow(cbex->hBtnAdd);
		DestroyWindow(cbex->hBtnDel);
		DestroyWindow(cbex->hEdit);
		MIR_FREE(cbex);
		break;

		/**
			* name:	WM_CTLCOLOREDIT
			* desc:	is called on a paint message for a dialog item to determine its colour scheme
			* param:	wParam - pointer to a HDC
			*			lParam - pointer to a HWND
			* return:	a brush
			**/
	case WM_CTLCOLOREDIT:
		if (!g_plugin.getByte(SET_PROPSHEET_SHOWCOLOURS, 1) || (HWND)lParam != cbex->hEdit || !cbex->pItems || cbex->iSelectedItem < 0)
			break;
		return Ctrl_SetTextColour((HDC)wParam, cbex->pItems[cbex->iSelectedItem].wFlags);

	case WM_CTLCOLORSTATIC:
		if ((HWND)lParam == cbex->hEdit)
			return (INT_PTR)GetSysColor(COLOR_WINDOW);
		return FALSE;
		/**
			* name:	WM_SETICON
			* desc:	updates the icons of this control
			* param:	wParam - not used
			*			lParam - not used
			* return:	always 0
			**/
	case WM_SETICON:
		{
			HICON hIcon = g_plugin.getIcon(IDI_BTN_ADD);
			SendMessage(cbex->hBtnAdd, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
			SetWindowText(cbex->hBtnAdd, (hIcon ? L"" : L"+"));

			hIcon = g_plugin.getIcon(IDI_BTN_DELETE);
			SendMessage(cbex->hBtnDel, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
			SetWindowText(cbex->hBtnDel, (hIcon ? L"" : L"-"));

			if (cbex->pItems && cbex->numItems > 0) {
				for (int i = 0; i < cbex->numItems; i++)
					cbex->pItems[i].hIcon = g_plugin.getIcon(cbex->pItems[i].iIcon);

				if (cbex->iSelectedItem >= 0 && cbex->iSelectedItem < cbex->numItems)
					SendMessage(cbex->hBtnEdit, BM_SETIMAGE, IMAGE_ICON, (LPARAM)cbex->pItems[cbex->iSelectedItem].hIcon);
			}
			return 0;
		}

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
			/**
			* name:	BTN_MENU
			* desc:	the button to dropdown the list to show all items is pressed
			**/
		case BTN_MENU:
			if (HIWORD(wParam) == BN_CLICKED) {
				POINT pt = { 0, 0 };
				RECT rc;
				int i, nItems;
				HMENU hMenu;

				if (!(hMenu = CreatePopupMenu())) return 0;
				SetFocus((HWND)lParam);

				MENUITEMINFO mii = { 0 };
				mii.cbSize = sizeof(mii);
				mii.fMask = MIIM_ID | MIIM_STRING | MIIM_FTYPE | MIIM_STATE;
				mii.fType = MFT_STRING;

				// insert the items
				for (i = nItems = 0; i < cbex->numItems; i++) {
					if ((cbex->pItems[i].wFlags & CBEXIF_DELETED) || *cbex->pItems[i].szCat == 0) continue;
					mii.fState = (cbex->pItems[i].pszVal && *cbex->pItems[i].pszVal) ? MFS_CHECKED : MFS_UNCHECKED;
					mii.wID = CBEXM_MENIITEMFIRST + i;
					mii.dwTypeData = cbex->pItems[i].szCat;
					if (!InsertMenuItem(hMenu, i, TRUE, &mii)) {
						DestroyMenu(hMenu);
						return 0;
					}
					nItems++;
				}
				// add separator between default and custom items
				if (nItems > 3) {
					mii.fMask = MIIM_FTYPE;
					mii.fType = MFT_SEPARATOR;
					mii.wID = 0;
					mii.dwItemData = 0;
					InsertMenuItem(hMenu, 3, TRUE, &mii);
				}
				ClientToScreen((HWND)lParam, &pt);
				GetClientRect((HWND)lParam, &rc);
				i = TrackPopupMenuEx(hMenu, TPM_RIGHTALIGN | TPM_RETURNCMD, pt.x + rc.right, pt.y + rc.bottom, hwnd, nullptr);
				SendMessage(cbex->hBtnMenu, BM_SETCHECK, NULL, NULL);
				if (i >= CBEXM_MENIITEMFIRST && i < CBEXM_MENIITEMFIRST + cbex->numItems) {
					CtrlContactWndProc(hwnd, CBEXM_SETCURSEL, (WPARAM)i - CBEXM_MENIITEMFIRST, NULL);
				}
				DestroyMenu(hMenu);
				return 0;
			}
			break;

			/**
				* name:	BTN_ADD
				* desc:	the button to add a new entry is pressed
				**/
		case BTN_ADD:
			if (HIWORD(wParam) == BN_CLICKED) {
				DLGPROC dlgProc;
				WORD dlgID;
				wchar_t szCat[MAX_CAT] = { 0 };
				wchar_t szVal[MAXDATASIZE] = { 0 };
				CBEXITEM cbi;
				HWND hDlgDetails;

				SetFocus((HWND)lParam);
				if (!(hDlgDetails = GetParent(GetParent(hwnd)))) return 1;
				if (SendMessage(hDlgDetails, PSM_ISLOCKED, NULL, NULL)) return 0;

				switch (GetWindowLongPtr(hwnd, GWLP_ID)) {
				case EDIT_PHONE:
					dlgID = IDD_ADDPHONE;
					dlgProc = DlgProc_Phone;
					cbi.iIcon = IDI_BTN_CUSTOMPHONE;
					break;
				case EDIT_EMAIL:
					dlgID = IDD_ADDEMAIL;
					dlgProc = DlgProc_EMail;
					cbi.iIcon = IDI_BTN_EMAIL;
					break;
				default:
					return 1;
				}

				cbi.iItem = -1;
				cbi.wMask = CBEXIM_CAT | CBEXIM_VAL | CBEXIM_FLAGS | CBEXIM_ICONTEXT;
				cbi.pszCat = szCat;
				cbi.pszVal = szVal;
				cbi.ccCat = MAX_CAT;
				cbi.ccVal = MAXDATASIZE;
				cbi.wFlags = 0;
				cbi.dwID = 0;

				if (DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(dlgID), GetParent(hwnd), dlgProc, (LPARAM)&cbi) == IDOK) {
					MCONTACT hContact = NULL;

					SendMessage(hDlgDetails, PSM_GETCONTACT, NULL, (LPARAM)&hContact);
					if (hContact) cbi.wFlags |= CTRLF_HASCUSTOM;
					cbi.wFlags |= CTRLF_CHANGED;
					if (SendMessage(hwnd, CBEXM_ADDITEM, NULL, (LPARAM)&cbi) > CB_ERR) {
						SendMessage(hDlgDetails, PSM_CHANGED, NULL, NULL);
						cbex->bIsChanged = TRUE;
						SendMessage(hwnd, CBEXM_SETCURSEL, cbex->numItems - 1, NULL);
					}
				}
				return 0;
			}
			break;

			/**
				* name:	BTN_EDIT
				* desc:	the button to edit an existing entry is pressed
				**/
		case BTN_EDIT:
			if (HIWORD(wParam) == BN_CLICKED) {
				DLGPROC dlgProc;
				WORD dlgID;
				wchar_t szCat[MAX_CAT] = { 0 };
				wchar_t szVal[MAXDATASIZE] = { 0 };
				CBEXITEM cbi;
				HWND hDlgDetails;

				SetFocus((HWND)lParam);
				if (!(hDlgDetails = GetParent(GetParent(hwnd)))) return 1;
				if (SendMessage(hDlgDetails, PSM_ISLOCKED, NULL, NULL)) return 0;
				if (!cbex->pItems || cbex->iSelectedItem == -1) return 0;

				switch (GetWindowLongPtr(hwnd, GWLP_ID)) {
				case EDIT_PHONE:
					dlgID = IDD_ADDPHONE;
					dlgProc = DlgProc_Phone;
					break;
				case EDIT_EMAIL:
					dlgID = IDD_ADDEMAIL;
					dlgProc = DlgProc_EMail;
					break;
				default:
					return 1;
				}
				cbi.iItem = cbex->iSelectedItem;
				cbi.dwID = 0;
				cbi.wMask = CBEXIM_CAT | CBEXIM_VAL | CBEXIM_FLAGS;
				cbi.pszCat = szCat;
				cbi.pszVal = szVal;
				cbi.ccCat = MAX_CAT;
				cbi.ccVal = MAXDATASIZE;
				if (!CtrlContactWndProc(hwnd, CBEXM_GETITEM, NULL, (LPARAM)&cbi)) {
					MsgErr(hwnd, LPGENW("CRITICAL: Unable to edit current entry!\nThis should not happen!"));
					return 1;
				}

				if (DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(dlgID), GetParent(hwnd), dlgProc, (LPARAM)&cbi) == IDOK) {
					MCONTACT hContact;

					SendMessage(hDlgDetails, PSM_GETCONTACT, NULL, (LPARAM)&hContact);
					if (hContact) cbi.wFlags |= CTRLF_HASCUSTOM;
					cbi.wFlags |= CTRLF_CHANGED;
					SendMessage(hwnd, CBEXM_SETITEM, NULL, (LPARAM)&cbi);
					SendMessage(hDlgDetails, PSM_CHANGED, NULL, NULL);
					cbex->bIsChanged = TRUE;
				}
				return 0;
			}
			break;

			/**
				* name:	BTN_DEL
				* desc:	the button to delete an existing entry is pressed
				**/
		case BTN_DEL:
			if (HIWORD(wParam) == BN_CLICKED) {
				HWND hDlgDetails;
				MSGBOX mBox;
				wchar_t szMsg[MAXDATASIZE];

				SetFocus((HWND)lParam);
				if (!(hDlgDetails = GetParent(GetParent(hwnd))) ||
					SendMessage(hDlgDetails, PSM_ISLOCKED, NULL, NULL) ||
					!cbex->pItems ||
					cbex->iSelectedItem < 0 ||
					cbex->iSelectedItem >= cbex->numItems ||
					FAILED(mir_snwprintf(szMsg, TranslateT("Do you really want to delete the current selected item?\n\t%s\n\t%s"),
						cbex->pItems[cbex->iSelectedItem].szCat, cbex->pItems[cbex->iSelectedItem].pszVal))
					) {
					return 1;
				}
				mBox.cbSize = sizeof(MSGBOX);
				mBox.hParent = hDlgDetails;
				mBox.hiLogo = g_plugin.getIcon(IDI_DLG_PHONE);
				mBox.uType = MB_YESNO | MB_ICON_QUESTION | MB_NOPOPUP;
				mBox.ptszTitle = TranslateT("Delete");
				mBox.ptszMsg = szMsg;
				if (IDYES == MsgBoxService(NULL, (LPARAM)&mBox)) {
					// clear value for standard entry
					if (cbex->pItems[cbex->iSelectedItem].wFlags & CBEXIF_CATREADONLY) {
						MIR_FREE(cbex->pItems[cbex->iSelectedItem].pszVal);
						SetWindowText(cbex->hEdit, L"");
						cbex->pItems[cbex->iSelectedItem].wFlags &= ~CBEXIF_SMS;
						cbex->pItems[cbex->iSelectedItem].wFlags |= CTRLF_CHANGED;
					}
					// clear values for customized database entry
					else
						if (cbex->pItems[cbex->iSelectedItem].dwID != 0) {
							MIR_FREE(cbex->pItems[cbex->iSelectedItem].pszVal);
							*cbex->pItems[cbex->iSelectedItem].szCat = 0;
							cbex->pItems[cbex->iSelectedItem].wFlags |= CTRLF_CHANGED | CBEXIF_DELETED;
							CtrlContactWndProc(hwnd, CBEXM_SETCURSEL, cbex->iSelectedItem - 1, FALSE);
						}
					// delete default entry
						else
							CtrlContactWndProc(hwnd, CBEXM_DELITEM, NULL, cbex->iSelectedItem);

					SendMessage(hDlgDetails, PSM_CHANGED, NULL, NULL);
					cbex->bIsChanged = TRUE;
				}
				return 0;
			}
			break;

			/**
				* name:	EDIT_VALUE
				* desc:	the edit control wants us to act
				**/
		case EDIT_VALUE:
			switch (HIWORD(wParam)) {
			case EN_UPDATE:
				{
					wchar_t szVal[MAXDATASIZE] = { 0 };
					int ccVal;
					MCONTACT hContact;
					HWND hDlgDetails = GetParent(GetParent(hwnd));

					EnableWindow(cbex->hBtnDel, GetWindowTextLength(cbex->hEdit) > 0);

					if (SendMessage(hDlgDetails, PSM_ISLOCKED, NULL, NULL) ||
						cbex->bLocked ||
						!cbex->pItems ||
						cbex->iSelectedItem < 0 ||
						cbex->iSelectedItem >= cbex->numItems) return 1;

					// get the edit control's text value and check it for syntax
					switch (GetWindowLongPtr(hwnd, GWLP_ID)) {
					case EDIT_PHONE:
						{
							int errorPos;
							wchar_t szEdit[MAXDATASIZE];

							if (ccVal = GetWindowText(cbex->hEdit, szEdit, _countof(szEdit))) {
								if (!(ccVal = CheckPhoneSyntax(szEdit, szVal, MAXDATASIZE, errorPos)) || errorPos > -1) {
									SetWindowText(cbex->hEdit, szVal);
									SendMessage(cbex->hEdit, EM_SETSEL, errorPos, errorPos);
								}
							}
							break;
						}
					case EDIT_EMAIL:
						ccVal = GetWindowText(cbex->hEdit, szVal, _countof(szVal));
						break;
					default:
						ccVal = GetWindowText(cbex->hEdit, szVal, _countof(szVal));
						break;
					}

					SendMessage(hDlgDetails, PSM_GETCONTACT, NULL, (LPARAM)&hContact);
					if ((cbex->pItems[cbex->iSelectedItem].wFlags & CTRLF_CHANGED) && !(hContact && (cbex->pItems[cbex->iSelectedItem].wFlags & CTRLF_HASCUSTOM))) return 0;

					if (*szVal == 0 || !cbex->pItems[cbex->iSelectedItem].pszVal || mir_wstrcmp(szVal, cbex->pItems[cbex->iSelectedItem].pszVal)) {
						cbex->pItems[cbex->iSelectedItem].wFlags |= CTRLF_CHANGED;
						cbex->pItems[cbex->iSelectedItem].wFlags |= (hContact ? CTRLF_HASCUSTOM : CTRLF_HASPROTO);
						cbex->bIsChanged = TRUE;
						InvalidateRect((HWND)lParam, nullptr, TRUE);
						SendMessage(hDlgDetails, PSM_CHANGED, NULL, NULL);
					}
					return 0;
				}
			case EN_KILLFOCUS:
				{
					int ccText;

					if (!cbex->pItems || cbex->iSelectedItem < 0 || cbex->iSelectedItem >= cbex->numItems) return 1;
					if (!(cbex->pItems[cbex->iSelectedItem].wFlags & CTRLF_CHANGED)) return 0;

					if ((ccText = GetWindowTextLength(cbex->hEdit)) <= 0) {
						if (cbex->pItems[cbex->iSelectedItem].wFlags & CBEXIF_CATREADONLY) {
							MIR_FREE(cbex->pItems[cbex->iSelectedItem].pszVal);
							SetWindowText(cbex->hEdit, L"");
							cbex->pItems[cbex->iSelectedItem].wFlags &= ~CBEXIF_SMS;
						}
						else
							CtrlContactWndProc(hwnd, CBEXM_DELITEM, NULL, cbex->iSelectedItem);
						SendMessage(GetParent(GetParent(hwnd)), PSM_CHANGED, NULL, NULL);
						cbex->bIsChanged = TRUE;
					}
					else
						if (cbex->pItems[cbex->iSelectedItem].pszVal = (LPTSTR)mir_realloc(cbex->pItems[cbex->iSelectedItem].pszVal, (ccText + 2) * sizeof(wchar_t))) {
							cbex->pItems[cbex->iSelectedItem].pszVal[ccText + 1] = 0;
							GetWindowText(cbex->hEdit, cbex->pItems[cbex->iSelectedItem].pszVal, ccText + 1);
						}
					return 0;
				}
			}
			break;
		}
		break;

		/**
			* name:	CBEXM_ADDITEM
			* desc:	add a item to the control
			* param:	wParam - not used
			*			lParam - (LPCBEXITEM)&item
			* return:	CB_ERR on failure, new item index if successful
			**/
	case CBEXM_ADDITEM:
		{
			CBEXITEM	*pItem = (CBEXITEM*)lParam;

			if (!pItem) return FALSE;

			// if an item with the id of pItem exists, change it instead of adding a new one
			// but only if it has not been changed by the user yet.
			if ((pItem->wMask & CBEXIM_ID) && cbex->pItems && pItem->dwID != 0) {
				int iIndex;

				for (iIndex = 0; iIndex < cbex->numItems; iIndex++) {
					if (cbex->pItems[iIndex].dwID == pItem->dwID) {
						pItem->iItem = iIndex;
						if (cbex->pItems[iIndex].wFlags & CTRLF_CHANGED)
							pItem->wFlags |= CTRLF_CHANGED;
						else
							CtrlContactWndProc(hwnd, CBEXM_SETITEM, 0, lParam);
						return iIndex;
					}
				}
			}

			// add a new item to the combobox
			if (!(cbex->pItems = (LPCBEXITEMINTERN)mir_realloc(cbex->pItems, (cbex->numItems + 1) * sizeof(CBEXITEMINTERN)))) {
				cbex->numItems = 0;
				return CB_ERR;
			}

			// set the ID
			cbex->pItems[cbex->numItems].dwID = (pItem->wMask & CBEXIM_ID) ? pItem->dwID : 0;

			// set category string
			if (!pItem->pszCat || !pItem->pszCat[0] || !mir_wstrncpy(cbex->pItems[cbex->numItems].szCat, pItem->pszCat, MAX_CAT)) {
				mir_snwprintf(cbex->pItems[cbex->numItems].szCat, L"%s %d", TranslateT("Other"), ++cbex->numOther);
			}

			// set value string
			if ((pItem->wMask & CBEXIM_VAL) && pItem->pszVal && pItem->pszVal[0])
				cbex->pItems[cbex->numItems].pszVal = mir_wstrdup(pItem->pszVal);
			else
				cbex->pItems[cbex->numItems].pszVal = nullptr;
			// set icon
			if ((pItem->wMask & CBEXIM_ICONTEXT) && pItem->iIcon) {
				cbex->pItems[cbex->numItems].iIcon = pItem->iIcon;
				cbex->pItems[cbex->numItems].hIcon = g_plugin.getIcon(pItem->iIcon);
			}
			// set flags
			cbex->pItems[cbex->numItems].wFlags = (pItem->wMask & CBEXIM_CAT) ? pItem->wFlags : 0;

			cbex->numItems++;
			return cbex->numItems;
		}

		/**
			* name:	CBEXM_SETITEM
			* desc:	Set an item's information of the control.
			*			If iItem member of CBEXITEM is -1, the currently selected item is changed.
			* param:	wParam - not used
			*			lParam - (LPCBEXITEM)&item
			* return:	CB_ERR on failure, new item index if successful
			**/
	case CBEXM_SETITEM:
		{
			CBEXITEM *pItem = (CBEXITEM *)lParam;

			if (!PtrIsValid(pItem) || !pItem->wMask || !PtrIsValid(cbex->pItems)) return FALSE;
			if (pItem->iItem == -1) pItem->iItem = cbex->iSelectedItem;
			if (pItem->iItem < 0 || pItem->iItem >= cbex->numItems) return FALSE;

			// set new category string
			if (pItem->wMask & CBEXIM_CAT) {
				// set category string
				if (!pItem->pszCat || !pItem->pszCat[0] || !mir_wstrncpy(cbex->pItems[pItem->iItem].szCat, pItem->pszCat, _countof(cbex->pItems[pItem->iItem].szCat)))
					mir_snwprintf(cbex->pItems[pItem->iItem].szCat, L"%s %d", TranslateT("Other"), ++cbex->numOther);
				if (pItem->iItem == cbex->iSelectedItem)
					SetWindowText(cbex->hBtnEdit, cbex->pItems[pItem->iItem].szCat);
			}
			// set new value
			if (pItem->wMask & CBEXIM_VAL) {
				MIR_FREE(cbex->pItems[pItem->iItem].pszVal);
				if (pItem->pszVal && pItem->pszVal[0])
					cbex->pItems[pItem->iItem].pszVal = mir_wstrdup(pItem->pszVal);
				if (pItem->iItem == cbex->iSelectedItem)
					SetWindowText(cbex->hEdit, cbex->pItems[pItem->iItem].pszVal ? cbex->pItems[pItem->iItem].pszVal : L"");
			}

			// set icon
			if ((pItem->wMask & CBEXIM_ICONTEXT) && pItem->iIcon) {
				cbex->pItems[pItem->iItem].iIcon = pItem->iIcon;
				cbex->pItems[pItem->iItem].hIcon = g_plugin.getIcon(pItem->iIcon);
				if (pItem->iItem == cbex->iSelectedItem)
					SendMessage(cbex->hBtnEdit, BM_SETIMAGE, IMAGE_ICON, (LPARAM)cbex->pItems[pItem->iItem].hIcon);
			}
			if (pItem->wMask & CBEXIM_FLAGS) {
				cbex->pItems[pItem->iItem].wFlags = pItem->wFlags;
				CtrlContactWndProc(hwnd, CBEXM_ENABLEITEM, NULL, NULL);
			}
			return TRUE;
		}

		/**
			* name:	CBEXM_GETITEM
			* desc:	Get an item from the control.
			*			If iItem member of CBEXITEM is -1, the currently selected item is returned.
			* param:	wParam - not used
			*			lParam - (LPCBEXITEM)&item
			* return:	CB_ERR on failure, new item index if successful
			**/
	case CBEXM_GETITEM:
		{
			CBEXITEM *pItem = (CBEXITEM *)lParam;

			if (!pItem || !cbex->pItems) return FALSE;

			// try to find item by id
			if ((pItem->wMask & CBEXIM_ID) && pItem->dwID != 0) {
				int i;

				for (i = 0; i < cbex->numItems; i++) {
					if (cbex->pItems[i].dwID == pItem->dwID)
						break;
				}
				pItem->iItem = i;
			}
			else
				if (pItem->iItem == -1) pItem->iItem = cbex->iSelectedItem;
			if (pItem->iItem < 0 || pItem->iItem >= cbex->numItems) return FALSE;

			// return only currently selected itemindex
			if (!pItem->wMask) return TRUE;
			// return the unique id
			if (pItem->wMask & CBEXIM_ID)
				pItem->dwID = cbex->pItems[pItem->iItem].dwID;
			// return category string
			if ((pItem->wMask & CBEXIM_CAT) && pItem->pszCat) {
				if (*cbex->pItems[pItem->iItem].szCat != 0)
					mir_wstrncpy(pItem->pszCat, cbex->pItems[pItem->iItem].szCat, pItem->ccCat - 1);
				else
					*pItem->pszCat = 0;
			}
			// return value string
			if ((pItem->wMask & CBEXIM_VAL) && pItem->pszVal) {
				if (cbex->pItems[pItem->iItem].pszVal)
					mir_wstrncpy(pItem->pszVal, cbex->pItems[pItem->iItem].pszVal, pItem->ccVal - 1);
				else
					*pItem->pszVal = 0;
			}
			// return the icon
			if (pItem->wMask & CBEXIM_ICONTEXT)
				pItem->iIcon = cbex->pItems[pItem->iItem].iIcon;
			// return the flags
			if (pItem->wMask & CBEXIM_FLAGS)
				pItem->wFlags = cbex->pItems[pItem->iItem].wFlags;
			return TRUE;
		}

		/**
			* name:	CBEXM_DELITEM
			* desc:	delete an item from the control
			* param:	wParam - not used
			*			lParam - item index
			* return:	CB_ERR on failure, new item index if successful
			**/
	case CBEXM_DELITEM:
		{
			if (!cbex->pItems || (int)lParam < 0 || (int)lParam >= cbex->numItems || (cbex->pItems[lParam].wFlags & CBEXIF_CATREADONLY))
				return FALSE;
			MIR_FREE(cbex->pItems[(int)lParam].pszVal);
			memmove(cbex->pItems + (int)lParam,
				cbex->pItems + (int)lParam + 1,
				(cbex->numItems - (int)lParam - 1) * sizeof(CBEXITEMINTERN));
			cbex->numItems--;
			memset((cbex->pItems + cbex->numItems), 0, sizeof(CBEXITEMINTERN));
			CtrlContactWndProc(hwnd, CBEXM_SETCURSEL, lParam - 1, FALSE);
			return TRUE;
		}

		/**
			* name:	CBEXM_DELITEM
			* desc:	delete an item from the control
			* param:	wParam - not used
			*			lParam - item index
			* return:	CB_ERR on failure, new item index if successful
			**/
	case CBEXM_DELALLITEMS:
		{
			int i;

			if (PtrIsValid(cbex)) {
				if (PtrIsValid(cbex->pItems)) {
					for (i = 0; i < cbex->numItems; i++) {
						MIR_FREE(cbex->pItems[i].pszVal);
					}
					MIR_FREE(cbex->pItems);
					cbex->pItems = nullptr;
				}
				cbex->numItems = 0;
				cbex->iSelectedItem = -1;
				SetWindowText(cbex->hEdit, L"");
				SetWindowText(cbex->hBtnEdit, L"");
				SendMessage(cbex->hBtnEdit, WM_SETICON, NULL, NULL);
			}
			return TRUE;
		}

		/**
			* name:	CBEXM_ENABLEITEM
			* desc:	enables or disables the current item
			* param:	wParam - not used
			*			lParam - not used
			* return:	always 0
			**/
	case CBEXM_ENABLEITEM:
		if (cbex->iSelectedItem >= 0 && cbex->iSelectedItem < cbex->numItems) {
			MCONTACT hContact;
			BYTE bEnabled;

			PSGetContact(GetParent(hwnd), hContact);

			bEnabled = !hContact ||
				(cbex->pItems[cbex->iSelectedItem].wFlags & CTRLF_HASCUSTOM) ||
				!(cbex->pItems[cbex->iSelectedItem].wFlags & (CTRLF_HASPROTO | CTRLF_HASMETA)) ||
				!g_plugin.getByte(SET_PROPSHEET_PCBIREADONLY, 0);

			EnableWindow(cbex->hBtnEdit, bEnabled);
			EnableWindow(cbex->hBtnDel, bEnabled && GetWindowTextLength(cbex->hEdit) > 0);
			EnableWindow(cbex->hEdit, bEnabled);
		}
		break;

		/**
			* name:	CBEXM_ISCHANGED
			* desc:	returns whether the control contains changed values or not
			* param:	wParam - not used
			*			lParam - not used
			* return:	TRUE if control was changed, FALSE if nothing was edited
			**/
	case CBEXM_ISCHANGED:
		return cbex->bIsChanged;

		/**
			* name:	CBEXM_RESETCHANGED
			* desc:	resets changed flag to FALSE
			* param:	wParam - not used
			*			lParam - not used
			* return:	always FALSE
			**/
	case CBEXM_RESETCHANGED:
		cbex->bIsChanged = 0;
		return 0;

		/**
			* name:	CBEXM_SETCURSEL
			* desc:	selects a certain item
			* param:	wParam - index of the item to select
			*			lParam - (BYTE)bValid - if TRUE, the next item with a value is selected
			* return:	always FALSE
			**/
	case CBEXM_SETCURSEL:
		{
			int i;

			if (!cbex->pItems) return 1;
			if ((int)wParam < 0 || (int)wParam >= cbex->numItems) wParam = max(cbex->iSelectedItem, 0);
			cbex->bLocked = 1;

			if ((BYTE)lParam == TRUE) {
				i = (int)wParam;

				cbex->iSelectedItem = (int)wParam;
				while (i < cbex->numItems) {
					if (cbex->pItems[i].pszVal && *cbex->pItems[i].pszVal) {
						cbex->iSelectedItem = i;
						break;
					}
					i++;
				}
			}
			else {
				// search for the next none deleted item
				for (i = (int)wParam; i < cbex->numItems && *cbex->pItems[i].szCat == 0; i++);
				if (i == cbex->numItems && (int)wParam > 0) {
					for (i = 0; i < (int)wParam && *cbex->pItems[i].szCat == 0; i++);
					cbex->iSelectedItem = i == (int)wParam ? 0 : i;
				}
				else
					cbex->iSelectedItem = i;

			}
			SetWindowText(cbex->hBtnEdit, cbex->pItems[cbex->iSelectedItem].szCat);
			SetWindowText(cbex->hEdit, cbex->pItems[cbex->iSelectedItem].pszVal ? cbex->pItems[cbex->iSelectedItem].pszVal : L"");
			SendMessage(cbex->hBtnEdit, BM_SETIMAGE, IMAGE_ICON, (LPARAM)cbex->pItems[cbex->iSelectedItem].hIcon);
			CtrlContactWndProc(hwnd, CBEXM_ENABLEITEM, NULL, NULL);
			cbex->bLocked = 0;
			return 0;
		}
	case CBEXM_SORT:
		if (cbex->numItems > 4) {
			qsort(cbex->pItems + 3, cbex->numItems - 3, sizeof(CBEXITEMINTERN), compareProc);
		}
		return 0;

	case WM_ERASEBKGND:
		return 1;

	case WM_SETFOCUS:
		SetFocus(cbex->hEdit);
		SendMessage(cbex->hEdit, EM_SETSEL, 0, (LPARAM)-1);
		return 0;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}

/**
 * name:	CtrlContactUnLoadModule
 * desc:	calls required operations to clean up used memory and objects
 * param:	wParam - not used
 *			lParam - not used
 * return:	always 0
 **/
int CtrlContactUnLoadModule()
{
	UnregisterClass(UINFOCOMBOEXCLASS, g_plugin.getInst());
	return 0;
}

/**
 * name:	CtrlContactLoadModule
 * desc:	registers window class and does some other initializations
 * param:	none
 * return:	always 0
 **/
int CtrlContactLoadModule()
{
	WNDCLASSEX wc;

	memset(&wc, 0, sizeof(wc));
	wc.cbSize = sizeof(wc);
	wc.lpszClassName = UINFOCOMBOEXCLASS;
	wc.lpfnWndProc = CtrlContactWndProc;
	wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wc.cbWndExtra = sizeof(LPCBEX);
	wc.hbrBackground = (HBRUSH)GetStockObject(COLOR_WINDOW);
	wc.style = CS_GLOBALCLASS;
	RegisterClassEx(&wc);
	return 0;
}


/**
 * name:	CtrlContactAddItemFromDB
 * desc:	add a item read from db to the combobox
 * param:	hCtrl			- windowhandle to extended combobox control
 *			hIcon			- icon to use for custom items
 *			szItem			- category text for the item
 *			wForcedFlags	- force flag for each new entry
 *			hContact		- handle to contact whose settings to add
 *			pszModule		- primary module to search the setting in
 *			pszProto		- contacts protocol module
 *			szSettingVal	- value holding setting
 * return:	TRUE	- if the item is not updated, because its changed flag is set
 *			FALSE	- if item is added or updated successfully
 **/
int CtrlContactAddItemFromDB(
	HWND hCtrl,
	int iIcon,
	LPTSTR szItem,
	MCONTACT hContact,
	LPCSTR pszModule,
	LPCSTR pszProto,
	LPCSTR szSettingVal)
{
	DBVARIANT dbv;
	CBEXITEM cbi;

	cbi.pszVal = nullptr;
	cbi.dwID = mir_hashstr(szSettingVal);
	cbi.wFlags = CBEXIF_CATREADONLY | DB::Setting::GetTStringCtrl(hContact, pszModule, pszModule, pszProto, szSettingVal, &dbv);
	if (dbv.type >= DBVT_WCHAR) {
		// no value read from database
		if (cbi.wFlags == CBEXIF_CATREADONLY)
			cbi.pszVal = nullptr;
		else { // check the database value
			cbi.pszVal = dbv.pwszVal;
			if (LPTSTR sms = wcsstr(cbi.pszVal, L" SMS")) {
				cbi.wFlags |= CBEXIF_SMS;
				*sms = 0;
			}
		}
	}
	cbi.pszCat = szItem;
	cbi.iItem = -1;
	cbi.wMask = CBEXIM_ALL;
	cbi.iIcon = iIcon;
	SendMessage(hCtrl, CBEXM_ADDITEM, NULL, (LPARAM)&cbi);
	db_free(&dbv);
	return (cbi.wFlags & CTRLF_CHANGED) == CTRLF_CHANGED;
}

/**
 * name:	CtrlContactAddMyItemsFromDB
 * desc:	add a item read from db to the combobox
 * param:	hCtrl			- windowhandle to extended combobox control
 *			hIcon			- icon to use for custom items
 *			wForcedFlags	- force flag for each new entry
 *			hContact		- handle to contact whose settings to add
 *			pszModule		- primary module to search the setting in
 *			pszProto		- contacts protocol module
 *			szFormatCat		- format for the category holding setting
 *			szFormatVal		- format for the value holding setting
 * return:	TRUE	- if one of the items was not updated, because its changed flag is set
 *			FALSE	- if all items were added or updated successfully
 **/
int CtrlContactAddMyItemsFromDB(
	HWND hCtrl,
	int iIcon,
	WORD wForcedFlags,
	MCONTACT hContact,
	LPCSTR pszModule,
	LPCSTR pszProto,
	LPCSTR szFormatCat,
	LPCSTR szFormatVal)
{
	CBEXITEM cbi;
	DBVARIANT dbv;
	CHAR pszSetting[MAXSETTING];
	WORD i;
	LPTSTR sms;
	int bAnyItemIsChanged = 0;

	memset(&cbi, 0, sizeof(cbi));
	cbi.iItem = -1;
	cbi.wMask = CBEXIM_ALL;
	cbi.iIcon = iIcon;

	for (i = 0;
		SUCCEEDED(mir_snprintf(pszSetting, szFormatVal, i)) &&
		(cbi.wFlags = DB::Setting::GetTStringCtrl(hContact, pszModule, pszModule, pszProto, pszSetting, &dbv));
		i++) {
		// read value
		cbi.dwID = mir_hashstr(pszSetting);
		cbi.pszVal = dbv.pwszVal;
		dbv.type = DBVT_DELETED;
		dbv.pwszVal = nullptr;

		// read category
		if (SUCCEEDED(mir_snprintf(pszSetting, szFormatCat, i))) {
			if (cbi.wFlags & CTRLF_HASCUSTOM) {
				if (DB::Setting::GetTString(hContact, pszModule, pszSetting, &dbv))
					dbv.type = DBVT_DELETED;
			}
			else
				if (cbi.wFlags & CTRLF_HASPROTO) {
					if (DB::Setting::GetTString(hContact, pszProto, pszSetting, &dbv))
						dbv.type = DBVT_DELETED;
				}

			if (dbv.type > DBVT_DELETED && dbv.pwszVal && *dbv.pwszVal) {
				cbi.pszCat = dbv.pwszVal;
				dbv.type = DBVT_DELETED;
				dbv.pwszVal = nullptr;
			}
		}
		if (sms = wcsstr(cbi.pszVal, L" SMS")) {
			cbi.wFlags |= CBEXIF_SMS;
			*sms = 0;
		}
		cbi.wFlags |= wForcedFlags;
		if (CB_ERR == SendMessage(hCtrl, CBEXM_ADDITEM, NULL, (LPARAM)&cbi))
			break;
		bAnyItemIsChanged |= (cbi.wFlags & CTRLF_CHANGED) == CTRLF_CHANGED;
		if (cbi.pszCat) {
			mir_free(cbi.pszCat);
			cbi.pszCat = nullptr;
		}
		if (cbi.pszVal) {
			mir_free(cbi.pszVal);
			cbi.pszVal = nullptr;
		}
	}
	SendMessage(hCtrl, CBEXM_SORT, NULL, NULL);
	return bAnyItemIsChanged;
}

/**
 * name:	CtrlContactWriteItemToDB
 * desc:	write a item from combobox to database
 * param:	none
 * return:	always 0
 **/
int CtrlContactWriteItemToDB(
	HWND hCtrl,
	MCONTACT hContact,
	LPCSTR pszModule,
	LPCSTR pszProto,
	LPCSTR pszSetting)
{
	wchar_t szVal[MAXDATASIZE];
	CBEXITEM cbi;

	if (!CtrlContactWndProc(hCtrl, CBEXM_ISCHANGED, NULL, NULL)) return 1;

	cbi.wMask = CBEXIM_ID | CBEXIM_VAL | CBEXIM_FLAGS;
	cbi.pszVal = szVal;
	cbi.ccVal = MAXDATASIZE - 4;
	cbi.iItem = 0;
	cbi.dwID = mir_hashstr(pszSetting);
	if (!CtrlContactWndProc(hCtrl, CBEXM_GETITEM, NULL, (LPARAM)&cbi)) return 1;
	if (!(cbi.wFlags & CTRLF_CHANGED)) return 0;
	if (!hContact && !(pszModule = pszProto)) return 1;
	if (!*szVal)
		db_unset(hContact, pszModule, pszSetting);
	else {
		if (cbi.wFlags & CBEXIF_SMS)
			mir_wstrncat(szVal, L" SMS", _countof(szVal) - mir_wstrlen(szVal));

		if (db_set_ws(hContact, pszModule, pszSetting, szVal)) return 1;
	}
	cbi.wFlags &= ~CTRLF_CHANGED;
	cbi.wMask = CBEXIM_FLAGS;
	CtrlContactWndProc(hCtrl, CBEXM_SETITEM, NULL, (LPARAM)&cbi);
	InvalidateRect(GetDlgItem(hCtrl, EDIT_VALUE), nullptr, TRUE);
	return 0;
}

/**
 * name:	CtrlContactWriteMyItemsToDB
 * desc:	write a list of custom items from combobox to database
 * param:	none
 * return:	always 0
 **/
int CtrlContactWriteMyItemsToDB(
	HWND hCtrl,
	int iFirstItem,
	MCONTACT hContact,
	LPCSTR pszModule,
	LPCSTR pszProto,
	LPCSTR szFormatCat,
	LPCSTR szFormatVal)
{
	CHAR pszSetting[MAXSETTING];
	wchar_t szCat[MAX_CAT];
	wchar_t szVal[MAXDATASIZE];
	LPTSTR pszOther;
	CBEXITEM cbi;
	INT_PTR ccOther;
	int i = 0;

	if (!CtrlContactWndProc(hCtrl, CBEXM_ISCHANGED, NULL, NULL)) return 1;
	if (!hContact && !(pszModule = pszProto)) return 1;

	pszOther = TranslateT("Other");
	ccOther = mir_wstrlen(pszOther);
	cbi.wMask = CBEXIM_CAT | CBEXIM_VAL | CBEXIM_FLAGS;
	cbi.pszCat = szCat;
	cbi.ccCat = MAX_CAT;
	cbi.pszVal = szVal;
	cbi.ccVal = MAXDATASIZE - 4;
	cbi.iItem = iFirstItem;
	cbi.dwID = 0;

	while (CtrlContactWndProc(hCtrl, CBEXM_GETITEM, NULL, (LPARAM)&cbi) && cbi.iItem < 50) {
		if (!(cbi.wFlags & CBEXIF_DELETED) && *szVal) {
			if (cbi.wFlags & CBEXIF_SMS) {
				mir_wstrncat(szVal, L" SMS", _countof(szVal) - mir_wstrlen(szVal));
			}
			mir_snprintf(pszSetting, szFormatCat, i);
			if (*szCat && wcsncmp(szCat, pszOther, ccOther)) {
				if (db_set_ws(hContact, pszModule, pszSetting, szCat)) return 1;
			}
			else
				db_unset(hContact, pszModule, pszSetting);
			mir_snprintf(pszSetting, szFormatVal, i);
			if (db_set_ws(hContact, pszModule, pszSetting, szVal)) return 1;
			cbi.wFlags &= ~CTRLF_CHANGED;
			cbi.wMask = CBEXIM_FLAGS;
			CtrlContactWndProc(hCtrl, CBEXM_SETITEM, NULL, (LPARAM)&cbi);
			cbi.wMask = CBEXIM_CAT | CBEXIM_VAL | CBEXIM_FLAGS;
			i++;
		}

		cbi.iItem++;
	}
	DB::Setting::DeleteArray(hContact, pszModule, szFormatCat, i);
	DB::Setting::DeleteArray(hContact, pszModule, szFormatVal, i);
	InvalidateRect(GetDlgItem(hCtrl, EDIT_VALUE), nullptr, TRUE);
	return 0;
}