summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/psp_profile.cpp
blob: 098fc8ad720a305cf82510f7c2125a7e4030c927 (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
/*
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.
*/

/**
 * system & local includes
 **/

#include "commonheaders.h"

#define LVF_EDITLABEL	8

#define CELIF_CUSTOM	8		// entry is userdefined (e.g. MyPhoneXX)
#define CELIF_SMS		16		// phone with ability to receive sms
#define LIF_TIPVISIBLE	32		// set if infotip is visible

typedef struct TCECListItem : CTRL {
	LPIDSTRLIST	idstrList;
	int			idstrListCount;
	int			iListItem;
	LPTSTR		pszText[2];
} LCITEM, *LPLCITEM;

typedef struct TListCtrl : CTRL {
	HWND		hList;
	HWND		hTip;
	POINT		ptTip;
	int			iHotItem;
	int			iHotSubItem;
	HFONT		hFont;

	struct {
		HWND		hEdit;		// handle to edit window
		HWND		hBtn;		// button to open dropdown list
		RECT		rcCombo;
		struct {
			HWND	hDrop;		// dropdown list
			int		iItem;		// currently selected item of the dropdown
		} dropDown; 
		LPLCITEM	pItem;		// the item beiing edited
		int			iItem;		// zero based index to item in the listview
		int			iSubItem;	// zero based index to subitem
		int			iTopIndex;	// zero based index to first visible item on list
	} labelEdit;
} LISTCTRL, *LPLISTCTRL;

typedef int (*MISERVICE)(WPARAM wParam, LPARAM lParam);

typedef struct TProfileEntries {
	LPTSTR	szGroup;
	LPCSTR	szCatFmt;
	LPCSTR	szValFmt;
	MIRANDASERVICE	GetList;
} PROFILEENTRY, *LPPROFILEENTRY;

static const PROFILEENTRY pFmt[3] = {
	{ LPGENT("Past"),			 "Past%d",				 "Past%dText",				 (MIRANDASERVICE)GetPastList				 },
	{ LPGENT("Affiliation"),"Affiliation%d",	"Affiliation%dText",	(MIRANDASERVICE)GetAffiliationsList },
	{ LPGENT("Interest"),	 "Interest%dCat",	"Interest%dText",		 (MIRANDASERVICE)GetInterestsList		}
};

static LRESULT CALLBACK ProfileList_LabelEditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

extern COLORREF clrBoth;
extern COLORREF clrChanged;
extern COLORREF clrCustom;
extern COLORREF clrNormal;
extern COLORREF clrMeta;


/**
 * name:	ProfileList_AddGroup
 * desc:	add a group to the listview
 * param:	hList	- handle to listview control
 *			pszText	- text of new group
 * return:	index the where group was added
 **/
static int ProfileList_AddGroup(HWND hList, LPTSTR pszText, int iItem)
{
	LVITEM lvi;
	lvi.mask = LVIF_TEXT|LVIF_PARAM;
	lvi.iItem = iItem;
	lvi.iSubItem = 0;
	lvi.pszText = pszText;
	lvi.lParam = NULL;
	return ListView_InsertItem(hList, &lvi);
}

/**
 * name:	ProfileList_GetItemText
 * desc:	returns the text of a listview item
 * param:	hList	 - handle to listview control
 *			iItem	 - item index
 *			iSubItem - subitem (column) index
 *			pszText	 - pointer of a buffer to retrieve the text
 *			ccText	 - number of maximal characters pszText can take
 * return:	number of read characters
 **/
static int ProfileList_GetItemText(HWND hList, int iItem, int iSubItem, LPTSTR pszText, int ccText)
{
	LVITEM lvi;
	TCHAR szNull[2];

	lvi.mask = LVIF_TEXT;
	lvi.iItem = iItem;
	lvi.iSubItem = iSubItem;
	lvi.cchTextMax = ccText ? ccText : 2;
	lvi.pszText = pszText ? pszText : szNull;
	return SNDMSG(hList, LVM_GETITEMTEXT, iItem, (LPARAM)&lvi);
}

/**
 * name:	ProfileList_GetItemData
 * desc:	return the infostructure associated with the desired item
 * param:	hList	- handle to listview control
 *			iItem	- item index
 * return:	LPLCITEM structure
 **/
static LPLCITEM ProfileList_GetItemData(HWND hList, int iItem)
{
	LVITEM lvi;

	if (iItem < 0) return NULL;
	lvi.mask = LVIF_PARAM;
	lvi.iSubItem = 0;
	lvi.iItem = iItem;
	return (SendMessage(hList, LVM_GETITEM, NULL, (LPARAM)&lvi)) ? (LPLCITEM)lvi.lParam : NULL;
}

/**
 * name:	ProfileList_DeleteItem
 * desc:	delete an item from the listview
 * param:	hList	- handle to listview control
 *			iItem	- item index
 *	
 * return:	nothing
 **/
static void ProfileList_DeleteItem(HWND hList, int iItem)
{
	LPLCITEM pItem = ProfileList_GetItemData(hList, iItem);
	
	if (PtrIsValid(pItem)) {
		if (pItem->pszText[0])
			mir_free(pItem->pszText[0]);
		if (pItem->pszText[1])
			mir_free(pItem->pszText[1]);
		mir_free(pItem);
	}
	ListView_DeleteItem(hList, iItem);
}

/**
 * name:	ProfileList_Clear
 * desc:	delete all list items and their data
 *
 **/
static void ProfileList_Clear(HWND hList)
{
	LVITEM lvi;

	lvi.iItem = 0;
	lvi.iSubItem = 0;
	lvi.mask = LVIF_PARAM;

	while (ListView_GetItem(hList, &lvi)) {
		if (PtrIsValid(lvi.lParam)) {
			if (((LPLCITEM)lvi.lParam)->pszText[0])
				mir_free(((LPLCITEM)lvi.lParam)->pszText[0]);
			if (((LPLCITEM)lvi.lParam)->pszText[1])
				mir_free(((LPLCITEM)lvi.lParam)->pszText[1]);
			mir_free((LPVOID)lvi.lParam);
		}
		lvi.iItem++;
	}
	ListView_DeleteAllItems(hList);
}

/**
 * name:	ProfileList_EndLabelEdit
 * desc:	destroys the edit control and saves the text to the list control if desired
 * param:	hList	- handle to listview control
 *			bSave	- tells, whether to save changes (TRUE) or not (FALSE)
 * return:	returns 0 on success or nonzero
 **/

static int ProfileList_EndLabelEdit(LPLISTCTRL pList, BYTE bSave)
{
	HWND hEdit;

	// check if labeledit is enabled
	if (!PtrIsValid(pList) || !pList->hList || !pList->labelEdit.hEdit)
		return 1;
	// set hEdit NULL to indicate the endlabeledit call and prevent other calls
	hEdit = pList->labelEdit.hEdit;
	pList->labelEdit.hEdit = NULL;

	if (bSave != FALSE && pList->labelEdit.pItem) {
		WORD ccText;
		LPTSTR	szEdit = NULL;
		BYTE bChanged = FALSE;

		// an list element was selected
		if (pList->labelEdit.iSubItem && pList->labelEdit.dropDown.iItem != pList->labelEdit.pItem->iListItem && pList->labelEdit.dropDown.iItem >= 0 && pList->labelEdit.dropDown.iItem < pList->labelEdit.pItem->idstrListCount) {
			pList->labelEdit.pItem->iListItem = pList->labelEdit.dropDown.iItem;
			if (pList->labelEdit.pItem->pszText[0]) {
				mir_free(pList->labelEdit.pItem->pszText[0]);
				pList->labelEdit.pItem->pszText[0] = NULL;
			}
			bChanged = TRUE;
		}
		// value was edited
		else {
			if ((ccText = GetWindowTextLength(hEdit)) > 0 && (szEdit = (LPTSTR)mir_alloc((ccText + 2) * sizeof(TCHAR)))) {
				GetWindowText(hEdit, szEdit, ccText + 1);
				szEdit[ccText + 1] = 0;

				if (!pList->labelEdit.pItem->pszText[pList->labelEdit.iSubItem]) {
					pList->labelEdit.pItem->pszText[pList->labelEdit.iSubItem] = szEdit;
					bChanged = TRUE;
				}
				else
				if (_tcscmp(pList->labelEdit.pItem->pszText[pList->labelEdit.iSubItem], szEdit)) {
					mir_free(pList->labelEdit.pItem->pszText[pList->labelEdit.iSubItem]);
					pList->labelEdit.pItem->pszText[pList->labelEdit.iSubItem] = szEdit;
					bChanged = TRUE;
				}
				else
					mir_free(szEdit);
			}
		}
		if (bChanged) {
			pList->labelEdit.pItem->wFlags |= CTRLF_CHANGED;
			pList->wFlags |= CTRLF_CHANGED;
			SendMessage(GetParent(GetParent(pList->hList)), PSM_CHANGED, 0, 0);
		}
	}
	if (pList->labelEdit.hBtn) DestroyWindow(pList->labelEdit.hBtn);
	if (pList->labelEdit.dropDown.hDrop) DestroyWindow(pList->labelEdit.dropDown.hDrop);
	DestroyWindow(hEdit);
	ListView_RedrawItems(pList->hList, pList->labelEdit.iItem, pList->labelEdit.iItem);
	ZeroMemory(&pList->labelEdit, sizeof(pList->labelEdit));
	SetFocus(pList->hList);
	return 0;
}

static int ProfileList_EndLabelEdit(HWND hList, BYTE bSave)
{
	return ProfileList_EndLabelEdit((LPLISTCTRL)GetUserData(hList), bSave);
}

/**
 * name:	ProfileList_BeginLabelEdit
 * desc:	create an edit control to edit the label of the selected item
 * param:	pList	 - handle to listview control's info structure
 *			iItem	 - item index
 *			iSubItem - subitem (column) index
 * return:	handle to the edit control
 **/

static HWND ProfileList_BeginLabelEdit(LPLISTCTRL pList, int iItem, int iSubItem)
{
	LVITEM lvi;
	LPLCITEM pItem;
	HCONTACT hContact;
	RECT rcList;
	
	if (!PtrIsValid(pList)) 
		return NULL;
	if (pList->labelEdit.hEdit)
		ProfileList_EndLabelEdit(pList, FALSE);

	lvi.mask = LVIF_PARAM|LVIF_STATE;
	lvi.stateMask = 0xFFFFFFFF;
	lvi.iItem = iItem;
	lvi.iSubItem = iSubItem;

	if (!ListView_GetItem(pList->hList, &lvi))
		return NULL;

	pItem = (LPLCITEM)lvi.lParam;

	PSGetContact(GetParent(pList->hList), hContact);
			
	// do not edit deviders or protocol based contact information
	if (!(lvi.state & LVIS_SELECTED) || !PtrIsValid(pItem) || (hContact && (pItem->wFlags & CTRLF_HASPROTO))) 
		return NULL;

	ListView_EnsureVisible(pList->hList, iItem, FALSE);
	ListView_GetSubItemRect(pList->hList, iItem, iSubItem, LVIR_BOUNDS, &pList->labelEdit.rcCombo);
		
	if (lvi.iSubItem == 0) {
		RECT rc2;
		
		ListView_GetSubItemRect(pList->hList, iItem, 1, LVIR_BOUNDS, &rc2);
		pList->labelEdit.rcCombo.right = rc2.left;
	}
	GetClientRect(pList->hList, &rcList);
	pList->labelEdit.rcCombo.right = min(pList->labelEdit.rcCombo.right, rcList.right);
	pList->labelEdit.rcCombo.left = max(pList->labelEdit.rcCombo.left, rcList.left);
	InflateRect(&pList->labelEdit.rcCombo, -1, -1);

	// create the button control for the combobox
	if (!iSubItem && pItem->idstrList) {
		pList->labelEdit.hBtn = CreateWindowEx(WS_EX_NOPARENTNOTIFY, UINFOBUTTONCLASS, NULL, 
					WS_VISIBLE|WS_CHILD|MBS_DOWNARROW,
					pList->labelEdit.rcCombo.right - (pList->labelEdit.rcCombo.bottom - pList->labelEdit.rcCombo.top), pList->labelEdit.rcCombo.top,
					pList->labelEdit.rcCombo.bottom - pList->labelEdit.rcCombo.top,
					pList->labelEdit.rcCombo.bottom - pList->labelEdit.rcCombo.top,
					pList->hList, NULL, ghInst, NULL);
		if (pList->labelEdit.hBtn) {
			SetWindowLongPtr(pList->labelEdit.hBtn, GWLP_ID, BTN_EDIT);
			pList->labelEdit.rcCombo.right -= pList->labelEdit.rcCombo.bottom - pList->labelEdit.rcCombo.top;
		}
	}
	else {
		pList->labelEdit.rcCombo.bottom = 3 * pList->labelEdit.rcCombo.bottom - 2 * pList->labelEdit.rcCombo.top;
		if (rcList.bottom < pList->labelEdit.rcCombo.bottom) {
			OffsetRect(&pList->labelEdit.rcCombo, 0, rcList.bottom - pList->labelEdit.rcCombo.bottom - 2);
		}
	}
	// create the edit control
	pList->labelEdit.hEdit = CreateWindowEx(WS_EX_NOPARENTNOTIFY|WS_EX_CLIENTEDGE,
				_T("EDIT"),
				(!iSubItem && pItem->idstrList && pItem->iListItem > 0 && pItem->iListItem < pItem->idstrListCount) 
					? pItem->idstrList[pItem->iListItem].ptszTranslated
					: (iSubItem >= 0 && iSubItem < 2 && pItem->pszText[iSubItem] && *pItem->pszText[iSubItem])
						? pItem->pszText[iSubItem] 
						: _T(""), 
						WS_VISIBLE|WS_CHILD|(iSubItem ? (WS_VSCROLL|ES_MULTILINE|ES_AUTOVSCROLL) : ES_AUTOHSCROLL),
				pList->labelEdit.rcCombo.left, pList->labelEdit.rcCombo.top,
				pList->labelEdit.rcCombo.right - pList->labelEdit.rcCombo.left,
				pList->labelEdit.rcCombo.bottom - pList->labelEdit.rcCombo.top,
				pList->hList, NULL, ghInst, NULL);
	if (!pList->labelEdit.hEdit)
		return NULL;
	SendMessage(pList->labelEdit.hEdit, WM_SETFONT, (WPARAM)(pList->hFont), 0);
	SendMessage(pList->labelEdit.hEdit, EM_SETSEL, 0, (LPARAM)-1);
	SetUserData(pList->labelEdit.hEdit, pList);
	pList->labelEdit.dropDown.iItem = pItem->iListItem;
	pList->labelEdit.iItem = iItem;
	pList->labelEdit.iSubItem = iSubItem;
	pList->labelEdit.iTopIndex = ListView_GetTopIndex(pList->hList);
	pList->labelEdit.pItem = pItem;
	SetFocus(pList->labelEdit.hEdit);
	mir_subclassWindow(pList->labelEdit.hEdit, ProfileList_LabelEditProc);
	return pList->labelEdit.hEdit;
}

/**
 * name:	ProfileList_BeginLabelEdit
 * desc:	create an edit control to edit the label of the selected item
 * param:	hList	 - handle to listview control
 *			iItem	 - item index
 *			iSubItem - subitem (column) index
 * return:	handle to the edit control
 **/
static HWND ProfileList_BeginLabelEdit(HWND hList, int iItem, int iSubItem)
{
	return ProfileList_BeginLabelEdit((LPLISTCTRL)GetUserData(hList), iItem, iSubItem);
}

/**
 * name:	ProfileList_GetInsertIndex
 * desc:	finds index to add the new item to and adds an devider if necessary
 * param:	hList	- handle to listcontrol to search for the index in
 *			pszList	- database settings string, that identifies this category
 * 
 * return:	zero based index for the new item or -1 on failure
 **/
static int ProfileList_GetInsertIndex(HWND hList, LPTSTR pszGroup)
{
	LVFINDINFO	lfi;
	int			iDevider, 
				iItem;
	
	// search for the devider to add the new item under
	lfi.flags = LVFI_STRING;
	lfi.psz = pszGroup;
	iDevider = SendMessage(hList, LVM_FINDITEM, (WPARAM)-1, (LPARAM)&lfi);
	
	// devider does not exist yet, add it!
	if (iDevider == -1) {
		LVITEM	lvi;

		lvi.mask = LVIF_PARAM|LVIF_TEXT;
		lvi.iSubItem = 0;
		lvi.iItem = 0xFFFF;
		lvi.lParam = NULL;
		lvi.pszText = (LPTSTR)lfi.psz;
		if ((iItem = ListView_InsertItem(hList, &lvi)) == -1) {
			return -1;
		}
		iItem++;
	}
	else {
		// search next devider to add new item just before
		lfi.flags = LVFI_PARAM;
		lfi.lParam = NULL;
		if ((iItem = ListView_FindItem(hList, iDevider, &lfi)) == -1)
			iItem = 0xFFFF;
	}
	return iItem;
}

/**
 * name:	ProfileList_AddNewItem
 * desc:	Ask's user for a type and adds new item to the list view
 * param:	pList	- pointer to the listview's data structure
 *			pszList	- database settings string, that identifies this category
 *
 * return:	TRUE or FALSE
 **/
static BYTE ProfileList_AddNewItem(HWND hDlg, LPLISTCTRL pList, const PROFILEENTRY *pEntry)
{
	LPLCITEM pItem;
	LVITEM lvi;
	HCONTACT hContact;

	if (PtrIsValid(pList) && (pItem = (LPLCITEM)mir_alloc(sizeof(LCITEM)))) {
		PSGetContact(hDlg, hContact);
		pItem->nType = CTRL_LIST_ITEM;
		pItem->wFlags = hContact ? CTRLF_HASCUSTOM : 0;
		pItem->iListItem = 0;
		pItem->pszText[0] = NULL;
		pItem->pszText[1] = NULL;
		// get category list
		pEntry->GetList((WPARAM)&pItem->idstrListCount, (LPARAM)&pItem->idstrList);

		lvi.mask = LVIF_PARAM|LVIF_STATE;
		lvi.stateMask = 0xFFFFFFFF;
		lvi.state = LVIS_FOCUSED|LVIS_SELECTED;
		lvi.iItem = ProfileList_GetInsertIndex(pList->hList, pEntry->szGroup);
		lvi.iSubItem = 0;
		lvi.lParam = (LPARAM)pItem;
		if ((lvi.iItem = ListView_InsertItem(pList->hList, &lvi)) >= 0) {
			ProfileList_BeginLabelEdit(pList, lvi.iItem, 0);
			return TRUE;
		}
		mir_free(pItem);
		MsgErr(hDlg, LPGENT("Sorry, but there is a problem with adding a new item of type \"%s\""), pEntry->szGroup);
	}	
	return FALSE;
}

/**
 * name:	ProfileList_AddItemlistFromDB
 * desc:	reads an zero based indexed szList from the database of hContacts pszModule and fills a hList
 * param:	hList		- HANDLE to the list to fill with two columns
 *			iItem		- index of new listviewitem
 *			iImage		- image to draw from a imagelist associated with the listview
 *			hContact	- handle to the contact, whose information are read
 *			pszModule	- the module the information are stored in
 *			szCatFormat	- name of a database setting that holds the categorytext
 *			szValFormat - name of a database setting that holds the valuetext
 *			wFlags		- flags to set for a new allocated item
 *
 * return	number of added rows or -1 if listview's changed flag is set
 **/
static int ProfileList_AddItemlistFromDB(
				LPLISTCTRL pList,
				int &iItem,
				LPIDSTRLIST idList,
				UINT nList,
				HCONTACT hContact,
				LPCSTR pszModule,
				LPCSTR szCatFormat,
				LPCSTR szValFormat,
				WORD wFlags)
{
	DBVARIANT	dbvVal, dbvCat;
	LPLCITEM	pItem;
	LVITEM		lvi;
	UINT		i, j = 0;
	CHAR		pszSetting[MAXSETTING];

	lvi.iSubItem = 0;
	lvi.mask = LVIF_PARAM;

	for (i = 0, lvi.iItem = iItem; ; i++) {
		// read the setting from db
		mir_snprintf(pszSetting, MAXSETTING, szValFormat, i);
		if (DB::Setting::GetTString(hContact, pszModule, pszSetting, &dbvVal)) break;
		if (dbvVal.type != DBVT_TCHAR) continue;
		mir_snprintf(pszSetting, MAXSETTING, szCatFormat, i);
		DB::Setting::GetAString(hContact, pszModule, pszSetting, &dbvCat);
		// create the itemobject
		if (!(pItem = (LPLCITEM)mir_alloc(sizeof(LCITEM)))) {
			db_free(&dbvCat);
			db_free(&dbvVal);
			break;
		}
		// fill item struct
		pItem->nType = CTRL_LIST_ITEM;
		pItem->idstrList = idList;
		pItem->idstrListCount = nList;
		pItem->iListItem = 0;
		pItem->pszText[0] = NULL;
		pItem->pszText[1] = dbvVal.ptszVal;
		pItem->wFlags = wFlags;
		lvi.lParam = (LPARAM)pItem;
		
		// get id-str-list-item for the category string
		if (idList != NULL) {
			for (j = 0; j < nList; j++) {
				switch (dbvCat.type) {
					case DBVT_BYTE:
						if (dbvCat.bVal != (BYTE)idList[j].nID)
							continue;
						break;
					case DBVT_WORD:
						if (dbvCat.wVal != (WORD)idList[j].nID)
							continue;
						break;
					case DBVT_DWORD:
						if (dbvCat.dVal != (DWORD)idList[j].nID)
							continue;
						break;
					case DBVT_ASCIIZ:			
						if (strcmp(dbvCat.pszVal, idList[j].pszText)) 
							continue;
						break;
				}
				pItem->iListItem = j;
				break;
			}
		}
		// item not found in the predefined category list?
		if ((idList == NULL || j == nList) && dbvCat.type == DBVT_ASCIIZ) {
			pItem->pszText[0] = mir_a2t(dbvCat.pszVal);
			db_free(&dbvCat);
		}
		if ((lvi.iItem = ListView_InsertItem(pList->hList, &lvi)) < 0) {
			mir_free(pItem);
			db_free(&dbvCat);
			db_free(&dbvVal);
			break;
		}
		lvi.iItem++;
		dbvCat.type = dbvVal.type = DBVT_DELETED;
	}
	iItem = lvi.iItem;
	return i;
}

/**
 * name:	ProfileList_DropdownProc
 * desc:	procedure to catch messages for a listbox control for my own combobox
 * param:	hwnd	- handle to the listview control's window
 *			msg		- message sent to the control
 *			wParam	- message specific parameter
 *			lParam	- message specific parameter
 * return:	message specific
**/
 
static LRESULT CALLBACK ProfileList_DropdownProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{
	LPLISTCTRL pList;

	switch (msg) {
	case WM_KEYDOWN:
		switch (wParam) {
		case VK_ESCAPE:
			SetFocus(GetParent(hwnd));
			return 0;

		case VK_RETURN:
		case VK_F4:
			{
				LPIDSTRLIST pItem;

				if (!PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) return CB_ERR;
				pList->labelEdit.dropDown.iItem = ListBox_GetCurSel(hwnd);
				if (pList->labelEdit.dropDown.iItem >= 0 && PtrIsValid(pItem = (LPIDSTRLIST)ListBox_GetItemData(hwnd, pList->labelEdit.dropDown.iItem)))
					SetWindowText(pList->labelEdit.hEdit, pItem->ptszTranslated);
				else
					pList->labelEdit.dropDown.iItem = -1;
				SetFocus(pList->labelEdit.hEdit);
				return 0;
			}
		}
		break;
	case WM_LBUTTONUP:
		{
			POINT pt;
			LPIDSTRLIST pItem;

			if (!PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) return CB_ERR;
			mir_callNextSubclass(hwnd, ProfileList_DropdownProc, msg, wParam, lParam);

			pt.x = (short)LOWORD(lParam);
			pt.y = (short)HIWORD(lParam);
			ClientToScreen(hwnd, &pt);

			if (SendMessage(hwnd, WM_NCHITTEST, 0, MAKELPARAM(pt.x, pt.y)) == HTVSCROLL) 
				return CB_ERR;

			pList->labelEdit.dropDown.iItem = SendMessage(hwnd, LB_GETCURSEL, 0, 0);

			if (pList->labelEdit.dropDown.iItem >= 0 && PtrIsValid(pItem = (LPIDSTRLIST)ListBox_GetItemData(hwnd, pList->labelEdit.dropDown.iItem)))
				SetWindowText(pList->labelEdit.hEdit, pItem->ptszTranslated);
			else
				pList->labelEdit.dropDown.iItem = -1;

			ProfileList_EndLabelEdit(pList->hList, TRUE);
			return 0;
		}
	case WM_KILLFOCUS:
		if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) {
			if (GetFocus() == pList->labelEdit.hEdit) {
				ShowWindow(hwnd, SW_HIDE);
				return 0;
			}
			ProfileList_EndLabelEdit(pList, FALSE);
		}
		return 0;
	}
	return mir_callNextSubclass(hwnd, ProfileList_DropdownProc, msg, wParam, lParam);
 }

/**
 * name:	ProfileList_LabelEditProc
 * desc:	procedure to catch messages for an edit control for my own combobox
 * param:	hwnd	- handle to the listview control's window
 *			msg		- message sent to the control
 *			wParam	- message specific parameter
 *			lParam	- message specific parameter
 * return:	message specific
 **/
static LRESULT CALLBACK ProfileList_LabelEditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	LPLISTCTRL pList;

	switch (msg) {
		case WM_KEYDOWN:
			switch (wParam) {
				case VK_ESCAPE:
					if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd)))
						ProfileList_EndLabelEdit(pList, FALSE);
					return 0;
				case VK_F3:
					if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd)) && pList->labelEdit.hBtn)
						SendMessage(pList->hList, WM_COMMAND, MAKEWPARAM(BTN_EDIT, BN_CLICKED), (LPARAM)pList->labelEdit.hBtn);
					return 0;
				case VK_RETURN:
				{
					BYTE bEditNext;
					int iItem;
		
					if (GetWindowLongPtr(hwnd, GWL_STYLE) & ES_WANTRETURN && !(GetKeyState(VK_CONTROL) & 0x8000))
						break;
					if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) {
						bEditNext = !pList->labelEdit.iSubItem && !ProfileList_GetItemText(pList->hList, pList->labelEdit.iItem, 1, NULL, NULL);
						iItem = pList->labelEdit.iItem;
						ProfileList_EndLabelEdit(pList->hList, TRUE);
						if (bEditNext) ProfileList_BeginLabelEdit(pList->hList, pList->labelEdit.iItem, 1);
					}
					return 0;
				}
				case VK_TAB:
					if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) {
						LVITEM lvi;

						lvi.mask = LVIF_STATE;
						lvi.stateMask = LVIS_FOCUSED|LVIS_SELECTED;
						lvi.iItem = pList->labelEdit.iItem;

						if (wParam == VK_TAB && !pList->labelEdit.iSubItem) {
							lvi.iSubItem = 1;
							lvi.state = LVIS_FOCUSED|LVIS_SELECTED;
							ProfileList_EndLabelEdit(pList->hList, TRUE);
						}
						else {
							UINT iSubItem = (wParam == VK_TAB) ? 0 : pList->labelEdit.iSubItem;

							lvi.iSubItem = 0;
							lvi.state = 0;

							ProfileList_EndLabelEdit(pList, TRUE);

							// unselect current list item
							if (!ListView_SetItem(pList->hList, &lvi))
								return 0;

							// search for next valid list item (skip deviders)
							lvi.iSubItem = iSubItem;
							lvi.mask = LVIF_PARAM;
							do {
								if (wParam == VK_UP) 
									lvi.iItem--;
								else
									lvi.iItem++;

								if (lvi.iItem == -1 || !ListView_GetItem(pList->hList, &lvi)) {
									return 0;
								}
							} while (!lvi.lParam);

							// select new list item
							lvi.mask = LVIF_STATE;
							lvi.state = LVIS_FOCUSED|LVIS_SELECTED;
							if (!ListView_SetItem(pList->hList, &lvi)) {
								return 0;
							}
						}
						ProfileList_BeginLabelEdit(pList->hList, lvi.iItem, lvi.iSubItem);
						return 0;
					}
					return 1;
			}
			break;
		case WM_GETDLGCODE:
			return DLGC_WANTALLKEYS | mir_callNextSubclass(hwnd, ProfileList_LabelEditProc, msg, wParam, lParam);
		case WM_KILLFOCUS:
		{
			HWND hwndFocus = GetFocus();

			if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd)) && 
				hwndFocus != pList->labelEdit.dropDown.hDrop && 
				hwndFocus != pList->labelEdit.hEdit &&
				hwndFocus != pList->labelEdit.hBtn)
				ProfileList_EndLabelEdit(pList, hwndFocus == pList->hList);
			return 0;
		}
	}
	return mir_callNextSubclass(hwnd, ProfileList_LabelEditProc, msg, wParam, lParam);
}

/**
 * name:	ListSubclassProc
 * desc:	procedure to catch messages for a listview control, to handle tooltips
 * param:	hwnd	- handle to the listview control's window
 *			msg		- message sent to the control
 *			wParam	- message specific parameter
 *			lParam	- message specific parameter
 * return:	message specific
 **/
static LRESULT CALLBACK ProfileList_SubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{
	LPLISTCTRL pList;

	switch (msg) {
	case WM_KEYDOWN:
		{
			int nCurSel, newSel;
			LVITEM lvi;

			switch (wParam) {
			case VK_F2:
				nCurSel = ListView_GetSelectionMark(hwnd);
				if (nCurSel == -1) break;
				ProfileList_BeginLabelEdit(hwnd, nCurSel, 0);
				return 0;
			case VK_F3:
				nCurSel = ListView_GetSelectionMark(hwnd);
				if (nCurSel == -1) break;
				ProfileList_BeginLabelEdit(hwnd, nCurSel, 1);
				return 0;
			case VK_UP:
			case VK_DOWN:
				lvi.iItem = nCurSel = ListView_GetSelectionMark(hwnd);
				lvi.iSubItem = 0;

				// find next valid item to select
				lvi.mask = LVIF_PARAM;
				do {
					if (wParam == VK_UP) lvi.iItem--;
					else lvi.iItem++;
					if (lvi.iItem == -1 || !ListView_GetItem(hwnd, &lvi)) {
						return 0;
					}
				} while (!lvi.lParam);

				ListView_EnsureVisible(hwnd, lvi.iItem, FALSE);
				newSel = lvi.iItem;
				lvi.iItem = nCurSel;
				lvi.mask = LVIF_STATE;
				lvi.stateMask = LVIS_FOCUSED|LVIS_SELECTED;
				lvi.state = 0;
				ListView_SetItem(hwnd, &lvi);
				lvi.iItem = newSel;
				lvi.state = LVIS_FOCUSED|LVIS_SELECTED;
				ListView_SetItem(hwnd, &lvi);
				ListView_SetSelectionMark(hwnd, lvi.iItem);
				return 0;
			}
			break;
		}
	case WM_MOUSEMOVE:
		if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) {
			HDC hDC;
			RECT rchWnd, rcItem;
			SIZE textSize;
			LVHITTESTINFO hi;
			TOOLINFO ti;
			BYTE bReposition;
			LPLCITEM pItem;

			hi.pt.x = GET_X_LPARAM(lParam);
			hi.pt.y = GET_Y_LPARAM(lParam);
			ListView_SubItemHitTest(hwnd, &hi);

			// show tip only if pointer is over an item
			if (pList->iHotItem != hi.iItem || pList->iHotSubItem != hi.iSubItem) {
				bReposition = pList->iHotItem != -1 || pList->iHotSubItem != -1;
				pList->iHotItem = hi.iItem;
				pList->iHotSubItem = hi.iSubItem;

				if ((hi.flags & LVHT_ONITEMLABEL) && PtrIsValid(pItem = ProfileList_GetItemData(hwnd, hi.iItem))) {
					GetWindowRect(hwnd, &rchWnd);
					ListView_GetSubItemRect(hwnd, hi.iItem, hi.iSubItem, LVIR_BOUNDS, &rcItem);
					// calculate size of text on the screen
					if ((hDC = GetDC(GetParent(hwnd)))) {
						SelectObject(hDC, (HFONT)SendMessage(GetParent(hwnd), WM_GETFONT, NULL, NULL));
						GetTextExtentPoint32(hDC, pItem->pszText[hi.iSubItem], lstrlen(pItem->pszText[hi.iSubItem]), &textSize);
						ReleaseDC(GetParent(hwnd), hDC);
					}
					// show tip only for text that is larger than te listview can display
					if (textSize.cx > rchWnd.right - rchWnd.left || textSize.cx > rcItem.right - rcItem.left) {
						ZeroMemory(&ti, sizeof(TOOLINFO));
						ti.cbSize = sizeof(TOOLINFO);
						ti.uFlags = TTF_IDISHWND|TTF_SUBCLASS|TTF_TRANSPARENT;
						ti.hinst = ghInst;
						ti.hwnd = hwnd;
						ti.uId = (UINT_PTR)hwnd;
						ti.lpszText = pItem->pszText[hi.iSubItem];
						ti.rect = rcItem;
						SendMessage(pList->hTip, TTM_SETMAXTIPWIDTH, 0, 300);
						SendMessage(pList->hTip, TTM_SETTOOLINFO, NULL, (LPARAM)&ti);
						if (pList->iHotSubItem > 0) {
							SendMessage(pList->hTip, TTM_SETTITLE, 1, (LPARAM)
								((pItem->idstrList && pItem->iListItem > 0 && pItem->iListItem < pItem->idstrListCount)
								? pItem->idstrList[pItem->iListItem].ptszTranslated
								: (pItem->pszText[0] && *pItem->pszText[0])
								? pItem->pszText[0]
							: TranslateT("<empty>"))
								);
							InvalidateRect(pList->hTip, NULL, TRUE);
						}
						else
							SendMessage(pList->hTip, TTM_SETTITLE, 0, (LPARAM)"");
						SendMessage(pList->hTip, TTM_ACTIVATE, TRUE, (LPARAM)&ti);
						pList->ptTip.x = rchWnd.left + GET_X_LPARAM(lParam) - 16;
						pList->ptTip.y = rchWnd.top + rcItem.top;
						// no TTN_SHOW is called if bReposition is TRUE, so repose here!
						if (bReposition) {
							RECT rcTip;
							GetClientRect(pList->hTip, &rcTip);
							SetWindowPos(pList->hTip, hwnd, pList->ptTip.x, pList->ptTip.y - rcTip.bottom, 0, 0, SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
						}
						pList->wFlags |= LIF_TIPVISIBLE;
						return 0;
					}
				}
				if (pList->wFlags & LIF_TIPVISIBLE) {
					SendMessage(pList->hTip, TTM_ACTIVATE, FALSE, (LPARAM)&ti);
					pList->wFlags &= ~LIF_TIPVISIBLE;
				}
			}
		}
		return 0;

		// begin label edit
	case WM_LBUTTONDBLCLK:
		{
			LVHITTESTINFO hi;

			hi.pt.x = GET_X_LPARAM(lParam);
			hi.pt.y = GET_Y_LPARAM(lParam);
			if (ListView_SubItemHitTest(hwnd, &hi)) {
				ProfileList_BeginLabelEdit(hwnd, hi.iItem, hi.iSubItem);
			}
			return TRUE;
		}

	case WM_NOTIFY:
		if (!PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd)))
			break;

		// ensure position of tooltip is on the topline of the item
		if (((LPNMHDR)lParam)->hwndFrom == pList->hTip) {
			RECT rcTip;
			GetClientRect(pList->hTip, &rcTip);

			switch (((LPNMHDR)lParam)->code) {
			case TTN_SHOW:
				SetWindowPos(pList->hTip, hwnd, pList->ptTip.x, pList->ptTip.y - rcTip.bottom, 0, 0, SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
				return TRUE;
			}
		}
		break;
	case WM_COMMAND:
		switch (LOWORD(wParam)) {

			// show dropdown menu for category list
		case BTN_EDIT:
			{
				int i;
				TCHAR szEdit[MAX_PATH];

				if (!PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) break;
				GetWindowText(pList->labelEdit.hEdit, szEdit, MAX_PATH);

				// need to create the dropdown list?
				if (pList->labelEdit.dropDown.hDrop == NULL) {
					const int listHeight = 120;
					RECT rc, rcList;
					int add;

					// dropdown rect
					GetClientRect(pList->hList, &rcList);
					rc.left = pList->labelEdit.rcCombo.left;
					rc.right = pList->labelEdit.rcCombo.right + pList->labelEdit.rcCombo.bottom - pList->labelEdit.rcCombo.top; 

					if (rcList.bottom < pList->labelEdit.rcCombo.bottom + listHeight) {
						rc.bottom = pList->labelEdit.rcCombo.bottom - 7; // don't ask me why!
						rc.top = rc.bottom - listHeight;
					}
					else {
						rc.top = pList->labelEdit.rcCombo.bottom;
						rc.bottom = rc.top + listHeight;
					}

					pList->labelEdit.dropDown.hDrop = CreateWindowEx(0,
						_T("LISTBOX"), NULL, WS_CHILD|WS_BORDER|WS_VSCROLL|LBS_COMBOBOX|LBS_HASSTRINGS,
						rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
						hwnd, NULL, ghInst, NULL);
					if (!pList->labelEdit.dropDown.hDrop) return FALSE;
					SetUserData(pList->labelEdit.dropDown.hDrop, pList);
					mir_subclassWindow(pList->labelEdit.dropDown.hDrop, ProfileList_DropdownProc);
					SetWindowLongPtr(pList->labelEdit.dropDown.hDrop, GWLP_ID, LIST_DROPDOWN);
					SendMessage(pList->labelEdit.dropDown.hDrop, WM_SETFONT, (WPARAM)SendMessage(GetParent(pList->hList), WM_GETFONT, 0, 0), 0);

					// add items
					for (i = 0; i < pList->labelEdit.pItem->idstrListCount; i++) {
						add = ListBox_AddString(pList->labelEdit.dropDown.hDrop, pList->labelEdit.pItem->idstrList[i].ptszTranslated);
						ListBox_SetItemData(pList->labelEdit.dropDown.hDrop, add, pList->labelEdit.pItem->idstrList + i);
						if (!_tcscmp(szEdit, pList->labelEdit.pItem->idstrList[i].ptszTranslated))
							ListBox_SetCurSel(pList->labelEdit.dropDown.hDrop, add);
					}
				}
				else {
					LPIDSTRLIST lpidList;

					i = 0;
					while (PtrIsValid(lpidList = (LPIDSTRLIST)ListBox_GetItemData(pList->labelEdit.dropDown.hDrop, i))) {
						if (!_tcscmp(szEdit, lpidList->ptszTranslated)) {
							ListBox_SetCurSel(pList->labelEdit.dropDown.hDrop, i);
							break;
						}
						i++;
					}
					if (i == pList->labelEdit.pItem->idstrListCount) 
						ListBox_SetCurSel(pList->labelEdit.dropDown.hDrop, -1);
				}
				if (IsWindowVisible(pList->labelEdit.dropDown.hDrop)) {
					SetFocus(pList->labelEdit.hEdit);
				}
				else {
					ShowWindow(pList->labelEdit.dropDown.hDrop, SW_SHOW);
					//SetFocus(pList->labelEdit.dropDown.hDrop);
				}
				break;
			}
		}
		break;

	case WM_MOUSEWHEEL:
	case WM_VSCROLL:
	case WM_HSCROLL:
		if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd)))
			ProfileList_EndLabelEdit(pList, FALSE);
		break;

	case WM_KILLFOCUS:
		{
			HWND hwndFocus = GetFocus();

			if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd)) &&
				pList->labelEdit.hEdit != hwndFocus && 
				pList->labelEdit.dropDown.hDrop != hwndFocus && 
				pList->labelEdit.hBtn != hwndFocus)
				ProfileList_EndLabelEdit(pList, FALSE);
			break;
		}

	case WM_DESTROY:
		if (PtrIsValid(pList = (LPLISTCTRL)GetUserData(hwnd))) {
			HFONT hFont;

			ProfileList_EndLabelEdit(pList, FALSE);
			ProfileList_Clear(hwnd);
			if (PtrIsValid(hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0)) && hFont != pList->hFont)
				DeleteObject(hFont);
			DestroyWindow(pList->hTip);
			mir_free(pList);
		}
		break;
	}
	return mir_callNextSubclass(hwnd, ProfileList_SubclassProc, msg, wParam, lParam);
}

/**
 * name:	DlgProcPspAbout()
 * desc:	dialog procedure
 *
 * return:	0 or 1
 **/
INT_PTR CALLBACK PSPProcContactProfile(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	HWND hList = GetDlgItem(hDlg, LIST_PROFILE);
	LPLISTCTRL pList;

	switch (uMsg) {
	case WM_INITDIALOG:
		{
			LVCOLUMN lvc;
			RECT rc;
			LOGFONT lf;
			HFONT hFont;
			TOOLINFO ti;

			if (!hList || !(pList = (LPLISTCTRL)mir_alloc(sizeof(LISTCTRL)))) 
				return FALSE;
			ZeroMemory(pList, sizeof(LISTCTRL));

			TranslateDialogDefault(hDlg);
			Ctrl_InitTextColours();

			// init info structure
			pList->hList = hList;
			pList->nType = CTRL_LIST_PROFILE;
			ZeroMemory(&pList->labelEdit, sizeof(pList->labelEdit));
			SetUserData(hList, pList);

			// set new window procedure
			mir_subclassWindow(hList, ProfileList_SubclassProc);

			// remove static edge in aero mode
			if (IsAeroMode())
				SetWindowLongPtr(hList, GWL_EXSTYLE, GetWindowLongPtr(hList, GWL_EXSTYLE)&~WS_EX_STATICEDGE);

			// insert columns into the listboxes					
			ListView_SetExtendedListViewStyle(hList, LVS_EX_FULLROWSELECT);


			PSGetBoldFont(hDlg, hFont);
			SendDlgItemMessage(hDlg, IDC_PAGETITLE, WM_SETFONT, (WPARAM)hFont, 0);

			// set listfont
			pList->hFont = (HFONT)SendMessage(hList, WM_GETFONT, 0, 0);
			pList->wFlags |= LVF_EDITLABEL;
			GetObject(pList->hFont, sizeof(lf), &lf);
			lf.lfHeight -= 6;
			hFont = CreateFontIndirect(&lf);
			SendMessage(hList, WM_SETFONT, (WPARAM)hFont, 0);

			GetClientRect(hList, &rc);
			rc.right -= GetSystemMetrics(SM_CXVSCROLL);

			// initiate the tooltips
			pList->hTip = CreateWindowEx(WS_EX_TOPMOST,	TOOLTIPS_CLASS, NULL,
				WS_POPUP|TTS_BALLOON|TTS_NOPREFIX|TTS_ALWAYSTIP,
				CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
				hList, NULL, ghInst, NULL);
			if (pList->hTip) {
				SetWindowPos(pList->hTip, HWND_TOPMOST,
					CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
					SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

				ZeroMemory(&ti, sizeof(TOOLINFO));
				ti.cbSize = sizeof(TOOLINFO);
				ti.uFlags = TTF_IDISHWND|TTF_SUBCLASS|TTF_TRANSPARENT;
				ti.hinst = ghInst;
				ti.hwnd = hList;
				ti.uId = (UINT_PTR)hList;
				SendMessage(pList->hTip, TTM_ADDTOOL, NULL, (LPARAM)&ti);
				SendMessage(pList->hTip, TTM_ACTIVATE, FALSE, (LPARAM)&ti);
			}		

			// insert columns into the listboxes					
			lvc.mask = LVCF_WIDTH;
			lvc.cx = rc.right / 8 * 3;
			ListView_InsertColumn(hList, 0, &lvc);
			lvc.cx = rc.right / 8 * 5;
			ListView_InsertColumn(hList, 1, &lvc);
			return TRUE;
		}

	case WM_CTLCOLORSTATIC:
	case WM_CTLCOLORDLG:
		if (IsAeroMode())
			return (INT_PTR)GetStockBrush(WHITE_BRUSH);
		break;

	case WM_NOTIFY:
		switch (((LPNMHDR)lParam)->idFrom) {
		case 0:
			{
				HCONTACT hContact = (HCONTACT)((LPPSHNOTIFY)lParam)->lParam;
				LPCSTR pszProto;

				if (!PtrIsValid(pList = (LPLISTCTRL)GetUserData(hList)))
					break;

				// some account data may have changed so reread database
				switch (((LPNMHDR)lParam)->code) {
				case PSN_INFOCHANGED:
					{
						BYTE msgResult = 0;
						LPIDSTRLIST idList;
						UINT nList;
						BYTE i;
						int iItem = 0,
							iGrp = 0,
							numProtoItems,
							numUserItems;

						if (!(pList->wFlags & CTRLF_CHANGED) && PSGetBaseProto(hDlg, pszProto) && *pszProto != 0) {
							ProfileList_Clear(hList);

							// insert the past information
							for (i = 0; i < 3; i++) {
								pFmt[i].GetList((WPARAM)&nList, (LPARAM)&idList);
								if ((numProtoItems = ProfileList_AddItemlistFromDB(pList, iItem, idList, nList, hContact, pszProto, pFmt[i].szCatFmt, pFmt[i].szValFmt, CTRLF_HASPROTO)) < 0)
									return FALSE;

								// scan all basic protocols for the subcontacts
								if (DB::Module::IsMetaAndScan(pszProto)) {
									int iDefault = CallService(MS_MC_GETDEFAULTCONTACTNUM, (WPARAM)hContact, NULL);
									HCONTACT hSubContact, hDefContact;
									LPCSTR pszSubBaseProto;
									int j, numSubs;

									if ((hDefContact = (HCONTACT)CallService(MS_MC_GETSUBCONTACT, (WPARAM)hContact, iDefault)) &&
										(pszSubBaseProto = DB::Contact::Proto(hDefContact)))
									{
										if ((numProtoItems += ProfileList_AddItemlistFromDB(pList, iItem, idList, nList, hDefContact, pszSubBaseProto, pFmt[i].szCatFmt, pFmt[i].szValFmt, CTRLF_HASMETA|CTRLF_HASPROTO)) < 0)
											return FALSE;

										// copy the missing settings from the other subcontacts
										numSubs = CallService(MS_MC_GETNUMCONTACTS, (WPARAM)hContact, NULL);
										for (j = 0; j < numSubs; j++) {
											if (j == iDefault) continue;
											if (!(hSubContact = (HCONTACT)CallService(MS_MC_GETSUBCONTACT, (WPARAM)hContact, j))) continue;
											if (!(pszSubBaseProto = DB::Contact::Proto(hSubContact))) continue;
											if ((numProtoItems += ProfileList_AddItemlistFromDB(pList, iItem, idList, nList, hSubContact, pszSubBaseProto, pFmt[i].szCatFmt, pFmt[i].szValFmt, CTRLF_HASMETA|CTRLF_HASPROTO)) < 0)
												return FALSE;
											//if ((numUserItems += ProfileList_AddItemlistFromDB(pList, iItem, idList, nList, hSubContact, USERINFO, pFmt[i].szCatFmt, pFmt[i].szValFmt, CTRLF_HASMETA|CTRLF_HASPROTO)) < 0)
											//	return FALSE;
										}
									}
								}
								if ((numUserItems = ProfileList_AddItemlistFromDB(pList, iItem, idList, nList, hContact, USERINFO, pFmt[i].szCatFmt, pFmt[i].szValFmt, CTRLF_HASCUSTOM)) < 0)
									return FALSE;
								if (numUserItems || numProtoItems) {
									msgResult = PSP_CHANGED;
									ProfileList_AddGroup(hList, pFmt[i].szGroup, iGrp);
									iGrp = ++iItem;
								}
							}
						}
						SetWindowLongPtr(hDlg, DWLP_MSGRESULT, msgResult);
						break;
					}
					// user swiches to another propertysheetpage
				case PSN_KILLACTIVE:
					ProfileList_EndLabelEdit(hList, TRUE);
					break;
					// user selected to apply settings to the database
				case PSN_APPLY:
					if (pList->wFlags & CTRLF_CHANGED) {
						BYTE iFmt = -1;
						int iItem;
						LVITEM lvi;
						TCHAR szGroup[MAX_PATH];
						CHAR pszSetting[MAXSETTING];
						LPLCITEM pItem;
						LPSTR pszModule = USERINFO;

						if (!hContact) PSGetBaseProto(hDlg, pszModule);

						*szGroup = 0;
						lvi.mask = LVIF_TEXT|LVIF_PARAM;
						lvi.pszText = szGroup;
						lvi.cchTextMax = MAX_PATH;

						for (iItem = lvi.iItem = lvi.iSubItem = 0; ListView_GetItem(hList, &lvi); lvi.iItem++) {
							if (!PtrIsValid(pItem = (LPLCITEM)lvi.lParam)) {
								// delete reluctant items
								if (iFmt >= 0 && iFmt < SIZEOF(pFmt)) {
									DB::Setting::DeleteArray(hContact, pszModule, pFmt[iFmt].szCatFmt, iItem);
									DB::Setting::DeleteArray(hContact, pszModule, pFmt[iFmt].szValFmt, iItem);
								}
								// find information about the group
								for (iFmt = 0; iFmt < SIZEOF(pFmt); iFmt++) {
									if (!_tcscmp(szGroup, pFmt[iFmt].szGroup)) {
										break;
									}
								}
								// indicate, no group was found. should not happen!!
								if (iFmt == SIZEOF(pFmt)) {
									*szGroup = 0;
									iFmt = -1;
								}
								iItem = 0;
							}
							else
								if (iFmt >= 0 && iFmt < SIZEOF(pFmt)) {
									// save value
									if (!pItem->pszText[1] || !*pItem->pszText[1])
										continue;
									if (!(pItem->wFlags & (CTRLF_HASPROTO|CTRLF_HASMETA))) {
										mir_snprintf(pszSetting, MAXSETTING, pFmt[iFmt].szValFmt, iItem);
										db_set_ts(hContact, pszModule, pszSetting, pItem->pszText[1]);
										// save category
										mir_snprintf(pszSetting, MAXSETTING, pFmt[iFmt].szCatFmt, iItem);
										if (pItem->idstrList && pItem->iListItem > 0 && pItem->iListItem < pItem->idstrListCount)
											db_set_s(hContact, pszModule, pszSetting, (LPSTR)pItem->idstrList[pItem->iListItem].pszText);
										else 
											if (pItem->pszText[0] && *pItem->pszText[0])
												db_set_ts(hContact, pszModule, pszSetting, (LPTSTR)pItem->pszText[0]);
											else									
												db_unset(hContact, pszModule, pszSetting);
										// redraw the item if required
										if (pItem->wFlags & CTRLF_CHANGED) {
											pItem->wFlags &= ~CTRLF_CHANGED;
											ListView_RedrawItems(hList, lvi.iItem, lvi.iItem);
										}
										iItem++;
									}
								}
						}
						// delete reluctant items
						if (iFmt >= 0 && iFmt < SIZEOF(pFmt)) {
							DB::Setting::DeleteArray(hContact, pszModule, pFmt[iFmt].szCatFmt, iItem);
							DB::Setting::DeleteArray(hContact, pszModule, pFmt[iFmt].szValFmt, iItem);
						}

						pList->wFlags &= ~CTRLF_CHANGED;
					}
				}
			}
			break;

		//
		// handle notification messages from the list control
		//
		case LIST_PROFILE:
			pList = (LPLISTCTRL)GetUserData(((LPNMHDR)lParam)->hwndFrom);

			switch (((LPNMHDR)lParam)->code) {
			case NM_RCLICK:
				{
					HMENU hMenu = CreatePopupMenu();
					MENUITEMINFO mii;
					HCONTACT hContact;
					LVHITTESTINFO hi;
					LPLCITEM pItem;
					POINT pt;

					if (!hMenu) return 1;
					PSGetContact(hDlg, hContact);
					GetCursorPos(&pt);
					hi.pt = pt;
					ScreenToClient(((LPNMHDR)lParam)->hwndFrom, &hi.pt);
					ListView_SubItemHitTest(((LPNMHDR)lParam)->hwndFrom, &hi);
					pItem = ProfileList_GetItemData(((LPNMHDR)lParam)->hwndFrom, hi.iItem);

					// insert menuitems
					ZeroMemory(&mii, sizeof(MENUITEMINFO));
					mii.cbSize = sizeof(MENUITEMINFO);
					mii.fMask = MIIM_ID|MIIM_STRING;
					// insert "Add" Menuitem
					mii.wID = BTN_ADD_intEREST;
					mii.dwTypeData = TranslateT("Add Interest");
					InsertMenuItem(hMenu, 0, TRUE, &mii);
					mii.wID = BTN_ADD_AFFLIATION;
					mii.dwTypeData = TranslateT("Add Affliation");
					InsertMenuItem(hMenu, 1, TRUE, &mii);
					mii.wID = BTN_ADD_PAST;
					mii.dwTypeData = TranslateT("Add Past");
					InsertMenuItem(hMenu, 2, TRUE, &mii);

					if (hi.iItem != -1 && PtrIsValid(pItem) && !(hContact && (pItem->wFlags & CTRLF_HASPROTO))) {
						// insert separator
						mii.fMask = MIIM_FTYPE;
						mii.fType = MFT_SEPARATOR;
						InsertMenuItem(hMenu, 3, TRUE, &mii);
						// insert "Delete" Menuitem
						mii.fMask = MIIM_ID|MIIM_STRING;
						mii.wID = BTN_EDIT_CAT;
						mii.dwTypeData = TranslateT("Edit Category");
						InsertMenuItem(hMenu, 4, TRUE, &mii);
						mii.wID = BTN_EDIT_VAL;
						mii.dwTypeData = TranslateT("Edit Value");
						InsertMenuItem(hMenu, 5, TRUE, &mii);
						mii.fMask = MIIM_FTYPE;
						mii.fType = MFT_SEPARATOR;
						InsertMenuItem(hMenu, 6, TRUE, &mii);
						// insert "Delete" Menuitem
						mii.fMask = MIIM_ID|MIIM_STRING;
						mii.wID = BTN_DEL;
						mii.dwTypeData = TranslateT("Delete");
						InsertMenuItem(hMenu, 7, TRUE, &mii);
					}
					TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hDlg, 0);
					DestroyMenu(hMenu);
				}
				return 0;

			case LVN_GETDISPINFO:
				if (pList->labelEdit.iTopIndex != ListView_GetTopIndex(hList))
					ProfileList_EndLabelEdit(((LPNMHDR)lParam)->hwndFrom, FALSE);
				break;

			case NM_CUSTOMDRAW:
				{
					LPNMLVCUSTOMDRAW cd = (LPNMLVCUSTOMDRAW)lParam;
					LPLCITEM pItem = (LPLCITEM)cd->nmcd.lItemlParam;
					RECT rc;

					switch (cd->nmcd.dwDrawStage) {
					case CDDS_PREPAINT:
						SetWindowLongPtr(hDlg, DWLP_MSGRESULT, CDRF_NOTIFYITEMDRAW);
						return TRUE;

					case CDDS_ITEMPREPAINT:
						ListView_GetItemRect(cd->nmcd.hdr.hwndFrom, cd->nmcd.dwItemSpec, &rc, LVIR_BOUNDS);
						if (!PtrIsValid(pItem)) {
							HFONT hBold, hFont;
							TCHAR szText[MAX_PATH];

							PSGetBoldFont(hDlg, hBold);
							hFont = (HFONT)SelectObject(cd->nmcd.hdc, hBold);
							SetTextColor(cd->nmcd.hdc, GetSysColor(COLOR_3DSHADOW));
							ProfileList_GetItemText(cd->nmcd.hdr.hwndFrom, cd->nmcd.dwItemSpec, 0, szText, MAX_PATH);
							rc.left += 6;
							DrawText(cd->nmcd.hdc, TranslateTS(szText), -1, &rc, DT_NOCLIP|DT_NOPREFIX|DT_SINGLELINE|DT_VCENTER);

							rc.bottom -= 2;
							rc.top = rc.bottom - 1;
							rc.left -= 6;
							DrawEdge(cd->nmcd.hdc, &rc, BDR_SUNKENOUTER, BF_RECT);

							SelectObject(cd->nmcd.hdc, hFont);
							SetWindowLongPtr(hDlg, DWLP_MSGRESULT, CDRF_SKIPDEFAULT);
							return TRUE;
						}
						// draw selected item
						if ((cd->nmcd.uItemState & CDIS_SELECTED) || (pList->labelEdit.iItem == cd->nmcd.dwItemSpec)) {
							SetTextColor(cd->nmcd.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT));
							FillRect(cd->nmcd.hdc, &rc, GetSysColorBrush(COLOR_HIGHLIGHT));
						}
						// draw background of unselected item
						else {
							SetTextColor(cd->nmcd.hdc, 
								(pItem->wFlags & CTRLF_CHANGED) 
								? clrChanged : (pItem->wFlags & CTRLF_HASMETA)
								? clrMeta : ((pItem->wFlags & (CTRLF_HASCUSTOM)) && (pItem->wFlags & CTRLF_HASPROTO))
								? clrBoth : (pItem->wFlags & CTRLF_HASCUSTOM)
								? clrCustom	: clrNormal);
							FillRect(cd->nmcd.hdc, &rc, GetSysColorBrush(COLOR_WINDOW));
						}
						SetWindowLongPtr(hDlg, DWLP_MSGRESULT, CDRF_NEWFONT|CDRF_NOTIFYSUBITEMDRAW);
						return TRUE;

					case CDDS_SUBITEM|CDDS_ITEMPREPAINT:
						{
							HFONT hoFont = (HFONT)SelectObject(cd->nmcd.hdc, pList->hFont);

							ListView_GetSubItemRect(cd->nmcd.hdr.hwndFrom, cd->nmcd.dwItemSpec, cd->iSubItem, LVIR_BOUNDS, &rc);
							if (cd->iSubItem == 0) {
								RECT rc2;
								ListView_GetSubItemRect(cd->nmcd.hdr.hwndFrom, cd->nmcd.dwItemSpec, 1, LVIR_BOUNDS, &rc2);
								rc.right = rc2.left;
							}
							rc.left += 3;
							DrawText(cd->nmcd.hdc,
								pItem->pszText[cd->iSubItem] 
							? pItem->pszText[cd->iSubItem] 
							: (cd->iSubItem == 0 && pItem->idstrList && pItem->iListItem > 0 && pItem->iListItem < pItem->idstrListCount)
								? pItem->idstrList[pItem->iListItem].ptszTranslated
								: TranslateT("<empty>"),
								-1, &rc, DT_END_ELLIPSIS|DT_NOCLIP|DT_NOPREFIX|DT_SINGLELINE|DT_VCENTER);
							SetWindowLongPtr(hDlg, DWLP_MSGRESULT, CDRF_SKIPDEFAULT);
							return TRUE;
						}
					} /* switch (cd->nmcd.dwDrawStage) */
					break;
				} /* case NM_CUSTOMDRAW: */
			} /* (((LPNMHDR)lParam)->code) */
			break;
		}
		break; /* case WM_NOTIFY: */

	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case BTN_ADD_intEREST:
			return ProfileList_AddNewItem(hDlg, (LPLISTCTRL)GetUserData(hList), &pFmt[2]);
		case BTN_ADD_AFFLIATION:
			return ProfileList_AddNewItem(hDlg, (LPLISTCTRL)GetUserData(hList), &pFmt[1]);
		case BTN_ADD_PAST:
			return ProfileList_AddNewItem(hDlg, (LPLISTCTRL)GetUserData(hList), &pFmt[0]);
		case BTN_EDIT_CAT:
			ProfileList_BeginLabelEdit(hList, ListView_GetSelectionMark(hList), 0);
			break;
		case BTN_EDIT_VAL:
			ProfileList_BeginLabelEdit(hList, ListView_GetSelectionMark(hList), 1);
			break;
		case BTN_DEL:
			if (IDYES == MsgBox(hDlg, MB_YESNO|MB_ICON_QUESTION, LPGENT("Question"), LPGENT("Delete an entry"), LPGENT("Do you really want to delete this entry?"))) {
				int iItem = ListView_GetSelectionMark(hList);
				pList = (LPLISTCTRL)GetUserData(hList);

				ProfileList_DeleteItem(hList, iItem);
				if (PtrIsValid(pList)) pList->wFlags |= CTRLF_CHANGED;
				SendMessage(GetParent(hDlg), PSM_CHANGED, NULL, NULL);
				// check if to delete any devider
				if (!ProfileList_GetItemData(hList, iItem--) && !ProfileList_GetItemData(hList, iItem))
					ListView_DeleteItem(hList, iItem);
			}
			break;
		}
		break;
	}
	return FALSE;
}