summaryrefslogtreecommitdiff
path: root/plugins/Clist_modern/src/modern_aniavatars.cpp
blob: e515c57fa255b22956d0fb61eedcac544cb924d2 (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
/**************************************************************************\

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2008 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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.

****************************************************************************

Created: Mar 9, 2007

Author:  Artem Shpynov aka FYR:  ashpynov@gmail.com

****************************************************************************

File contains implementation of animated avatars in contact list

\**************************************************************************/

#include "hdr/modern_commonheaders.h"

#define IMMEDIATE_DRAW (!AniAva.bSeparateWindow)

void GDIPlus_ExtractAnimatedGIF (TCHAR * szName, int width, int height, HBITMAP  * pBmp, int ** pframesDelay, int * pframesCount, SIZE * sizeAvatar);
BOOL GDIPlus_IsAnimatedGif (TCHAR * szName);

/* Next is module */
#define ANIAVAWINDOWCLASS _T("MirandaModernAniAvatar")
#define aacheck if ( !AniAva.bModuleStarted) return
#define aalock EnterCriticalSection(&AniAva.CS)
#define aaunlock LeaveCriticalSection(&AniAva.CS)

#define AAO_HAS_BORDER	  0x01
#define AAO_ROUND_CORNERS 0x02
#define AAO_HAS_OVERLAY	  0x04
#define AAO_OPAQUE		  0x08
//messages
enum {
	AAM_FIRST = WM_USER,
	AAM_SETAVATAR ,					//sync		WPARAM: TCHAR * filename, LPARAM: SIZE * size, RESULT: actual size
	AAM_SETPOSITION,				//async		LPARAM: pointer to set pos info - the handler will empty it, RESULT: 0
	AAM_REDRAW,						//async
	AAM_STOP,						//async		stops animation, timer, hide window - prepeare for deleting
	AAM_PAUSE,						//sync		keep timer and window, but do not process painting -need before graphics change
	AAM_RESUME,						//async		remobe previous flag. repaints if required
	AAM_REMOVEAVATAR,				//sync		WPARAM: if y more then wParam, LPARAM: shift up to lParam( remove if values is same)
	AAM_SETPARENT,					//async	    WPARAM: handle of new parent window
	AAM_SELFDESTROY,				//sync
	AAM_RENDER,						//sync
	AAM_LAST,
};

typedef struct _tagAniAva_Object
{
	HANDLE  hContact;
	HWND	hWindow;
	BOOL	bInvalidPos;
	BOOL	bToBeDeleted;
	DWORD	dwAvatarUniqId;
	SIZE	ObjectSize;
} ANIAVA_OBJECT;

typedef struct _tagAniAva_Info
{
	DWORD	dwAvatarUniqId;
	TCHAR * tcsFilename;
	int		nRefCount;
	int		nStripTop;
	int		nFrameCount;
	int *	pFrameDelays;
	SIZE	FrameSize;
} ANIAVA_INFO;

typedef struct _tagAniAva_WindowInfo
{
	HWND	hWindow;
	RECT	rcPos;
	SIZE	sizeAvatar;
	BOOL	StopTimer;
	int		TimerId;
	int		nFramesCount;
	int *	delaysInterval;
	int		currentFrame;

	POINT   ptFromPoint;

	BOOL	bPlaying;
	int		overlayIconIdx;
	BYTE	bAlpha;
	BOOL	bOrderTop;

	BOOL	bPaused;			// was request do not draw
	BOOL	bPended;			// till do not draw - was painting - need to be repaint
} ANIAVA_WINDOWINFO;
typedef struct _tagAniAva_PosInfo
{
	RECT rcPos;
	int idxOverlay;
	BYTE bAlpha;
} ANIAVA_POSINFO;
typedef struct _tagAniAvaSyncCallItem
{
	WPARAM  wParam;
	LPARAM  lParam;
	INT_PTR nResult;
	HANDLE  hDoneEvent;
	PSYNCCALLBACKPROC pfnProc;
} ANIAVA_SYNCCALLITEM;
typedef struct _tagAniAvatarImageInfo
{
	POINT ptImagePos;
	int	nFramesCount;
	int * pFrameDelays;
	SIZE	szSize;
} ANIAVATARIMAGEINFO;

//main structure to handle global
typedef struct _tagAniAva
{
	//protection
	BOOL bModuleStarted;
	CRITICAL_SECTION CS;
	//options
	BYTE		bFlags;				// 0x1 has border, 0x2 has round corners, 0x4 has overlay, 0x8 background color
	COLORREF	borderColor;
	BYTE		cornerRadius;
	COLORREF	bkgColor;
	HIMAGELIST	overlayIconImageList;
	//animations
	HDC hAniAvaDC;
	HBITMAP hAniAvaBitmap;
	HBITMAP hAniAvaOldBitmap;
	int width;
	int height;
	SortedList * AniAvatarList;
	DWORD AnimationThreadID;
	HANDLE AnimationThreadHandle;
	HANDLE hExitEvent;
	//Objects
	SortedList * Objects;
	BOOL bSeparateWindow;
} ANIAVA;

//module static declarations
static void		__AniAva_DebugRenderStrip();

static void		_AniAva_DestroyAvatarWindow( HWND hwnd);
static void		_AniAva_Clear_ANIAVA_WINDOWINFO(ANIAVA_WINDOWINFO * pavwi );
static void     _AniAva_RenderAvatar(ANIAVA_WINDOWINFO * dat, HDC hdcParent = NULL, RECT * rcInParent = NULL );
static void		_AniAva_PausePainting();
static void		_AniAva_ResumePainting();
static void		_AniAva_LoadOptions();
static void		_AniAva_ReduceAvatarImages(int startY, int dY, BOOL bDestroyWindow);
static void		_AniAva_AnimationTreadProc(HANDLE hExitEvent);
static void		_AniAva_RemoveAniAvaDC(ANIAVA * pAniAva);
static void		_AniAva_RealRemoveAvatar(DWORD UniqueID);
static int		_AniAva_LoadAvatarFromImage(TCHAR * szFileName, int width, int height, ANIAVATARIMAGEINFO * pRetAII);
static int		_AniAva_SortAvatarInfo(void * first, void * last);
static BOOL		_AniAva_GetAvatarImageInfo(DWORD dwAvatarUniqId, ANIAVATARIMAGEINFO * avii);
static HWND		_AniAva_CreateAvatarWindowSync(TCHAR *szFileName);

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

//module variables
static ANIAVA AniAva = {0};

///	IMPLEMENTATION


int _AniAva_OnModulesUnload(WPARAM wParam,LPARAM lParam)
{
	SetEvent(AniAva.hExitEvent);
	return 0;
}


// Init AniAva module
int AniAva_InitModule()
{
	memset(&AniAva,0,sizeof(AniAva));
	if (g_CluiData.fGDIPlusFail) return 0;
	if ( !( db_get_b(NULL,"CList","AvatarsAnimated",(ServiceExists(MS_AV_GETAVATARBITMAP) && !g_CluiData.fGDIPlusFail))
		 &&  db_get_b(NULL,"CList","AvatarsShow",SETTINGS_SHOWAVATARS_DEFAULT))) return 0;
	{
		WNDCLASSEX wc;
		ZeroMemory(&wc, sizeof(wc));
		wc.cbSize         = sizeof(wc);
		wc.lpszClassName  = ANIAVAWINDOWCLASS;
		wc.lpfnWndProc    = _AniAva_WndProc;
		wc.hCursor        = LoadCursor(NULL, IDC_ARROW);
		wc.cbWndExtra     = sizeof(ANIAVA_WINDOWINFO*);
		wc.hbrBackground  = 0;
		wc.style          = CS_GLOBALCLASS;
		RegisterClassEx(&wc);
	}
	InitializeCriticalSection(&AniAva.CS);
	AniAva.Objects = List_Create(0,2);
	AniAva.AniAvatarList = List_Create(0,1);
	AniAva.AniAvatarList->sortFunc = _AniAva_SortAvatarInfo;
	AniAva.bModuleStarted = TRUE;
	AniAva.hExitEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
	AniAva.AnimationThreadID = (DWORD)mir_forkthread(_AniAva_AnimationTreadProc, (void*)AniAva.hExitEvent);
	HookEvent(ME_SYSTEM_PRESHUTDOWN,  _AniAva_OnModulesUnload);

	_AniAva_LoadOptions();

	return 1;
}

// Unload AniAva module
int AniAva_UnloadModule()
{
	aacheck 0;
	aalock;
	{
		int i;
		AniAva.bModuleStarted = FALSE;
		for (i=0; i < AniAva.Objects->realCount; i++)
		{
			if (AniAva.Objects->items[i])
				_AniAva_DestroyAvatarWindow(((ANIAVA_OBJECT*)AniAva.Objects->items[i])->hWindow);

			mir_free(AniAva.Objects->items[i]);
		}
		List_Destroy(AniAva.Objects);
		mir_free(AniAva.Objects);

		for (i=0; i < AniAva.AniAvatarList->realCount; i++)
		{
			ANIAVA_INFO * aai = (ANIAVA_INFO *)AniAva.AniAvatarList->items[i];
			mir_free(aai->tcsFilename);
			if (aai->pFrameDelays) free(aai->pFrameDelays);
			mir_free(aai);
		}
		List_Destroy(AniAva.AniAvatarList);
		mir_free(AniAva.AniAvatarList);
		_AniAva_RemoveAniAvaDC(&AniAva);
		SetEvent(AniAva.hExitEvent);
		CloseHandle(AniAva.hExitEvent);
	}
	aaunlock;
	DeleteCriticalSection(&AniAva.CS);
	return 1;
}
// Update options
int AniAva_UpdateOptions()
{
	BOOL bReloadAvatars = FALSE;
	BOOL bBeEnabled = (!g_CluiData.fGDIPlusFail
		 &&  db_get_b(NULL,"CList","AvatarsAnimated",(ServiceExists(MS_AV_GETAVATARBITMAP) && !g_CluiData.fGDIPlusFail))
		 &&  db_get_b(NULL,"CList","AvatarsShow",SETTINGS_SHOWAVATARS_DEFAULT));
	if (bBeEnabled && !AniAva.bModuleStarted)
	{
		AniAva_InitModule();
		bReloadAvatars = TRUE;
	}
	else if ( !bBeEnabled && AniAva.bModuleStarted)
	{
		AniAva_UnloadModule();
		bReloadAvatars = TRUE;
	}
	BOOL oldSeparate = AniAva.bSeparateWindow;
	_AniAva_LoadOptions();
	if ( oldSeparate != AniAva.bSeparateWindow )
	{
		AniAva_InvalidateAvatarPositions(NULL);
		AniAva_RemoveInvalidatedAvatars();
	}
	if ( bReloadAvatars ) PostMessage(pcli->hwndContactTree,INTM_AVATARCHANGED,0,0);
	else AniAva_RedrawAllAvatars(TRUE);
	return 0;
}
// adds avatars to be displayed
int AniAva_AddAvatar(HANDLE hContact, TCHAR * szFilename, int width, int heigth)
{
	int res = 0;
	aacheck 0;
	if ( !GDIPlus_IsAnimatedGif (szFilename))
		return 0;
	aalock;
	{
		//first try to find window for contact avatar
		HWND hwnd = NULL;
		int i;
		ANIAVA_OBJECT * pavi;
		ANIAVATARIMAGEINFO avii = {0};
		SIZE szAva = { width, heigth };
		for (i=0; i < AniAva.Objects->realCount; i++)
		{
			pavi = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
			if (pavi->hContact == hContact)
			{
				if (pavi->ObjectSize.cx == width && pavi->ObjectSize.cy == heigth)
				{
					hwnd = pavi->hWindow;
					break;
				}
				else
				{

					_AniAva_DestroyAvatarWindow(pavi->hWindow);
					pavi->hWindow = NULL;
					_AniAva_RealRemoveAvatar(pavi->dwAvatarUniqId);
					pavi->dwAvatarUniqId = 0;
					break;
				}
			}
		}
		if (i == AniAva.Objects->realCount)
		{
			pavi = (ANIAVA_OBJECT *) mir_calloc( sizeof(ANIAVA_OBJECT));
			pavi->hWindow		 = NULL;
			pavi->hContact		 = hContact;
			pavi->bInvalidPos	 = 0;
			List_Insert( AniAva.Objects, pavi, AniAva.Objects->realCount);
		}
		//change avatar
		pavi->bToBeDeleted = FALSE;
		pavi->bInvalidPos	 = 0;
		// now CreateAvatar
		if (pavi->dwAvatarUniqId)
			_AniAva_GetAvatarImageInfo(pavi->dwAvatarUniqId,&avii);
		else
			pavi->dwAvatarUniqId = _AniAva_LoadAvatarFromImage(szFilename, width, heigth, &avii);
		if (hwnd)
			SendMessage(hwnd, AAM_SETAVATAR, (WPARAM)&avii, (LPARAM) 0);
		pavi->ObjectSize = avii.szSize;
		res = MAKELONG(avii.szSize.cx, avii.szSize.cy);
	}
	aaunlock;
	return res;
}

// call windows to set they parent in order to ensure valid zorder
void AniAva_UpdateParent()
{
	aacheck;
	aalock;
	{
		int i;
		HWND parent = fnGetAncestor(pcli->hwndContactList,GA_PARENT);
		for (i=0; i < AniAva.Objects->realCount; i++)
		{
			ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
			SendMessage(pai->hWindow, AAM_SETPARENT, (WPARAM)parent,0);
		}
	}
	aaunlock;
}
ANIAVA_OBJECT * FindAvatarByContact( HANDLE hContact )
{
	for ( int i=0; i < AniAva.Objects->realCount; i++)
	{
		ANIAVA_OBJECT * pai = ((ANIAVA_OBJECT *)AniAva.Objects->items[i]);
		if (pai->hContact == hContact)
			return pai;
	}
	return NULL;
}

int AniAva_RenderAvatar( HANDLE hContact, HDC hdcMem, RECT * rc )
{
	aacheck 0;
	aalock;
	ANIAVA_OBJECT * pai = FindAvatarByContact( hContact );
	if ( pai )
		SendMessage(pai->hWindow, AAM_RENDER, (WPARAM)hdcMem, (LPARAM) rc);
	aaunlock;
	return 0;
}
// update avatars pos
int AniAva_SetAvatarPos(HANDLE hContact, RECT * rc, int overlayIdx, BYTE bAlpha)
{
	aacheck 0;
	aalock;
	if (AniAva.CS.LockCount>0)
	{
		aaunlock;
		return 0;
	}
	{
		ANIAVA_OBJECT * pai = FindAvatarByContact( hContact );
		if ( pai )
		{
			ANIAVA_POSINFO * api = (ANIAVA_POSINFO *)malloc(sizeof(ANIAVA_POSINFO));
			if ( !pai->hWindow)
				{
					HWND hwnd;
					HWND parent;
					ANIAVATARIMAGEINFO avii = {0};
					//not found -> create window
					char szName[150] = "AniAvaWnd_";
					TCHAR * tszName;
					_itoa((int)hContact,szName+10,16);

					tszName = mir_a2t( szName );
					hwnd = _AniAva_CreateAvatarWindowSync(tszName);
					mir_free( tszName );
					parent = fnGetAncestor(pcli->hwndContactList,GA_PARENT);
					pai->hWindow = hwnd;
					SendMessage(hwnd,AAM_SETPARENT,(WPARAM)parent,0);
					if (_AniAva_GetAvatarImageInfo(pai->dwAvatarUniqId,&avii))
						SendMessage(pai->hWindow, AAM_SETAVATAR, (WPARAM)&avii, (LPARAM) 0);
				}
			api->bAlpha = bAlpha;
			api->idxOverlay = overlayIdx;
			api->rcPos = *rc;
			SendNotifyMessage(pai->hWindow, AAM_SETPOSITION, 0, (LPARAM) api);
			// the AAM_SETPOSITION is responsible to destroy memory under api
			pai->bInvalidPos = FALSE;
			pai->bToBeDeleted = FALSE;
		}
	}
	aaunlock;
	return 1;
}
// remove avatar
int AniAva_RemoveAvatar(HANDLE hContact)
{
	aacheck 0;
	aalock;
	{
		int i;
		for (i=0; i < AniAva.Objects->realCount; i++)
		{
			ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
			if (pai->hContact == hContact)
			{
				pai->bToBeDeleted = TRUE;
				break;
			}
		}
	}
	aaunlock;
	return 1;
}
// reset positions of avatars to be drawn (still be painted at same place)
int AniAva_InvalidateAvatarPositions(HANDLE hContact)
{
	int i;
	aacheck 0;
	aalock;
	for (i=0; i < AniAva.Objects->realCount; i++)
	{
		ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
		if (pai->hContact == hContact || !hContact)
		{
			pai->bInvalidPos++;
			if (hContact) break;
		}
	}
	aaunlock;
	return 1;
}
// all avatars without validated position will be stop painted and probably removed
int AniAva_RemoveInvalidatedAvatars()
{
	BOOL keepAvatar = FALSE;
	aacheck 0;
	aalock;

	{
		int i;
		for (i=0; i < AniAva.Objects->realCount; i++)
		{
			ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
			if (pai->hWindow && (pai->bInvalidPos))
			{
				SendMessage(pai->hWindow,AAM_STOP,0,0);
				if (pai->bInvalidPos)//>3)
				{
					//keepAvatar = TRUE;
					//pai->bToBeDeleted = TRUE;
					pai->bInvalidPos = 0;
					_AniAva_DestroyAvatarWindow(pai->hWindow);
					pai->hWindow = NULL;
				}
			}
			if (pai->bToBeDeleted)
			{
				if (pai->hWindow) _AniAva_DestroyAvatarWindow(pai->hWindow);
				pai->hWindow = NULL;
				if ( !keepAvatar) _AniAva_RealRemoveAvatar(pai->dwAvatarUniqId);
				mir_free(pai);
				List_Remove(AniAva.Objects,i);
				i--;
			}
		}
	}
	aaunlock;
	return 1;
}

// repaint all avatars at positions (eg on main window movement)
int AniAva_RedrawAllAvatars(BOOL updateZOrder)
{
	int i;
	aacheck 0;
	aalock;
	updateZOrder = 1;
	for (i=0; i < AniAva.Objects->realCount; i++)
	{
		ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
		if (updateZOrder)
			SendMessage(pai->hWindow,AAM_REDRAW,(WPARAM)updateZOrder,0);
		else
			SendNotifyMessage(pai->hWindow,AAM_REDRAW,(WPARAM)updateZOrder,0);
	}
	aaunlock;
	return 1;
}

//Static procedures
static void CALLBACK _AniAva_SyncCallerUserAPCProc(DWORD_PTR dwParam)
{
	ANIAVA_SYNCCALLITEM* item = (ANIAVA_SYNCCALLITEM*) dwParam;
	item->nResult = item->pfnProc(item->wParam, item->lParam);
	SetEvent(item->hDoneEvent);
}
static INT_PTR _AniAva_CreateAvatarWindowSync_Worker(WPARAM tszName, LPARAM lParam)
{
	HWND hwnd = CreateWindowEx( WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_NOPARENTNOTIFY,ANIAVAWINDOWCLASS,(TCHAR*)tszName,WS_POPUP,
		0,0,1,1,pcli->hwndContactList, NULL, pcli->hInst, NULL );
	return (INT_PTR)hwnd;
}

static HWND _AniAva_CreateAvatarWindowSync(TCHAR *szFileName)
{
	ANIAVA_SYNCCALLITEM item = {0};
	int res = 0;
	if ( !AniAva.AnimationThreadHandle) return NULL;
	if (AniAva.AnimationThreadID == 0) return NULL;
	item.wParam = (WPARAM) szFileName;
	item.lParam = 0;
	item.pfnProc = _AniAva_CreateAvatarWindowSync_Worker;
	item.hDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	if (GetCurrentThreadId() != AniAva.AnimationThreadID)
		QueueUserAPC(_AniAva_SyncCallerUserAPCProc, AniAva.AnimationThreadHandle, (DWORD_PTR) &item);
	else
		_AniAva_SyncCallerUserAPCProc((DWORD_PTR) &item);
	WaitForSingleObject(item.hDoneEvent, INFINITE);
	CloseHandle(item.hDoneEvent);
	return (HWND)item.nResult;
}

static void _AniAva_RealRemoveAvatar(DWORD UniqueID)
{
	int j,k;
	for (j = 0; j < AniAva.AniAvatarList->realCount; j++)
	{
		ANIAVA_INFO * aai = (ANIAVA_INFO *) AniAva.AniAvatarList->items[j];
		if (aai->dwAvatarUniqId == UniqueID)
		{
			aai->nRefCount--;
			if (aai->nRefCount == 0)
			{
				_AniAva_PausePainting();
				#ifdef _DEBUG
					__AniAva_DebugRenderStrip();
				#endif
				mir_free(aai->tcsFilename);
				if (aai->pFrameDelays) free(aai->pFrameDelays);
				_AniAva_ReduceAvatarImages(aai->nStripTop,aai->FrameSize.cx*aai->nFrameCount, FALSE);
				for (k = 0; k < AniAva.AniAvatarList->realCount; k++)
					if (k != j)	{
						ANIAVA_INFO * taai = (ANIAVA_INFO *) AniAva.AniAvatarList->items[k];
						if (taai->nStripTop>aai->nStripTop)
							taai->nStripTop -= aai->FrameSize.cx*aai->nFrameCount;
					}
				if (AniAva.AniAvatarList->realCount>0)
				{
					//lets create hNewDC
					HDC	 hNewDC;
					HBITMAP	 hNewBmp, hNewOldBmp;

					int newWidth = AniAva.width-aai->FrameSize.cx*aai->nFrameCount;
					int newHeight = 0;
					int i;
					for (i=0; i < AniAva.AniAvatarList->realCount; i++)
						if (i != j)
						{
							newHeight = max(newHeight,((ANIAVA_INFO *) AniAva.AniAvatarList->items[i])->FrameSize.cy);
						}

					hNewDC = CreateCompatibleDC(NULL);
					hNewBmp = ske_CreateDIB32(newWidth,newHeight);
					hNewOldBmp = (HBITMAP)SelectObject(hNewDC,hNewBmp);
					// copy from old and from new strip
					if (aai->nStripTop>0)
						BitBlt(hNewDC,0,0,aai->nStripTop,newHeight,AniAva.hAniAvaDC,0,0, SRCCOPY);
					if (aai->nStripTop+aai->FrameSize.cx*aai->nFrameCount < AniAva.width)
						BitBlt(hNewDC,aai->nStripTop,0,AniAva.width-(aai->nStripTop+aai->FrameSize.cx*aai->nFrameCount),newHeight,AniAva.hAniAvaDC,aai->nStripTop+aai->FrameSize.cx*aai->nFrameCount,0, SRCCOPY);

					_AniAva_RemoveAniAvaDC(&AniAva);
					AniAva.hAniAvaDC		 = hNewDC;
					AniAva.hAniAvaBitmap	 = hNewBmp;
					AniAva.hAniAvaOldBitmap	 = hNewOldBmp;
					AniAva.width			 = newWidth;
					AniAva.height			 = newHeight;

				}
				else
				{
					_AniAva_RemoveAniAvaDC(&AniAva);
				}
				#ifdef _DEBUG
					__AniAva_DebugRenderStrip();
				#endif
				List_Remove(AniAva.AniAvatarList, j);
				mir_free(aai);
				_AniAva_ResumePainting();
				break;
			}
		}
	}
}
static void _AniAva_RemoveAniAvaDC(ANIAVA * pAniAva)
{
	if (pAniAva->hAniAvaDC)
	{
		SelectObject(pAniAva->hAniAvaDC, pAniAva->hAniAvaOldBitmap);
		DeleteObject(pAniAva->hAniAvaBitmap);
		DeleteDC(pAniAva->hAniAvaDC);
		pAniAva->hAniAvaDC = NULL;
		pAniAva->height = 0;
		pAniAva->width = 0;
		pAniAva->hAniAvaBitmap = NULL;
	}
};

static void _AniAva_DestroyAvatarWindow( HWND hwnd)
{
	SendMessage(hwnd,AAM_SELFDESTROY,0,0);
}
static int	_AniAva_LoadAvatarFromImage(TCHAR * szFileName, int width, int height, ANIAVATARIMAGEINFO * pRetAII)
{
	ANIAVA_INFO aai = {0};
	ANIAVA_INFO * paai = NULL;
	BOOL fNeedInsertToList = FALSE;
	int idx = 0;
	aai.tcsFilename = szFileName;
	aai.FrameSize.cx = width;
	aai.FrameSize.cy = height;

	if ( !List_GetIndex(AniAva.AniAvatarList,(void*)&aai,&idx)) idx = -1;
	if (idx == -1)	//item not present in list
	{
		HBITMAP hBitmap = NULL;
		HDC hTempDC;
		HBITMAP hOldBitmap;
		HDC hNewDC;
		HBITMAP hNewBmp;
		HBITMAP hNewOldBmp;
		int newWidth;
		int newHeight;

		paai = (ANIAVA_INFO *)mir_calloc(sizeof(ANIAVA_INFO));
		paai->tcsFilename = mir_tstrdup(szFileName);
		paai->dwAvatarUniqId = rand();
		fNeedInsertToList = TRUE;
		//get image strip
		GDIPlus_ExtractAnimatedGIF (szFileName, width, height, &hBitmap, &(paai->pFrameDelays), &(paai->nFrameCount), &(paai->FrameSize));

		//copy image to temp DC
		hTempDC = CreateCompatibleDC(NULL);
		hOldBitmap = (HBITMAP)SelectObject(hTempDC,hBitmap);

		//lets create hNewDC
		/*
		newWidth = max(paai->FrameSize.cx*paai->nFrameCount,AniAva.width);
		newHeight = AniAva.height+paai->FrameSize.cy;
		*/
		newWidth = AniAva.width+paai->FrameSize.cx*paai->nFrameCount;
		newHeight = max(paai->FrameSize.cy,AniAva.height);

		hNewDC = CreateCompatibleDC(NULL);
		hNewBmp = ske_CreateDIB32(newWidth,newHeight);
		hNewOldBmp = (HBITMAP)SelectObject(hNewDC,hNewBmp);

		_AniAva_PausePainting();
		GdiFlush();
		// copy from old and from new strip
		BitBlt(hNewDC,0,0,AniAva.width,AniAva.height,AniAva.hAniAvaDC,0,0, SRCCOPY);
		BitBlt(hNewDC,AniAva.width,0,paai->FrameSize.cx*paai->nFrameCount,paai->FrameSize.cy,hTempDC,0,0, SRCCOPY);
		
		paai->nStripTop = AniAva.width;
		
		GdiFlush();
		//remove temp DC
		SelectObject(hTempDC,hOldBitmap);
		DeleteObject(hNewBmp);
		DeleteDC(hTempDC);
        DeleteObject(hBitmap);


		//delete old
		_AniAva_RemoveAniAvaDC(&AniAva);
		//setNewDC;
		AniAva.hAniAvaDC		 = hNewDC;
		AniAva.hAniAvaBitmap	 = hNewBmp;
		AniAva.hAniAvaOldBitmap	 = hNewOldBmp;
		AniAva.width			 = newWidth;
		AniAva.height			 = newHeight;
		GdiFlush();
		_AniAva_ResumePainting();
	}
	else
	{
		paai = (ANIAVA_INFO *)AniAva.AniAvatarList->items[idx];
	}
	if (paai)
	{
		paai->nRefCount++;
		pRetAII->nFramesCount = paai->nFrameCount;
		pRetAII->pFrameDelays = paai->pFrameDelays;
		pRetAII->ptImagePos.x = paai->nStripTop;
		pRetAII->ptImagePos.y = 0;
		pRetAII->szSize = paai->FrameSize;
		if (fNeedInsertToList)
		{
			//add to list
			int idx = AniAva.AniAvatarList->realCount;
			List_GetIndex(AniAva.AniAvatarList, paai,&idx);
			List_Insert(AniAva.AniAvatarList, (void*)paai, idx);
		}
		return paai->dwAvatarUniqId;
	}
	return 0;
}
static BOOL _AniAva_GetAvatarImageInfo(DWORD dwAvatarUniqId, ANIAVATARIMAGEINFO * avii)
{
	int j;
	BOOL res = FALSE;
	for (j = 0; j < AniAva.AniAvatarList->realCount; j++)
	{
		ANIAVA_INFO * aai = (ANIAVA_INFO *) AniAva.AniAvatarList->items[j];
		if (aai->dwAvatarUniqId == dwAvatarUniqId)
		{
			avii->nFramesCount = aai->nFrameCount;
			avii->pFrameDelays = aai->pFrameDelays;
			avii->ptImagePos.x = aai->nStripTop;
			avii->ptImagePos.y = 0;
			avii->szSize = aai->FrameSize;
			res = TRUE;
			break;
		}
	}
	return res;
}
static void _AniAva_Clear_ANIAVA_WINDOWINFO(ANIAVA_WINDOWINFO * pavwi )
{
	pavwi->delaysInterval = NULL;
	pavwi->nFramesCount = 0;
	KillTimer(pavwi->hWindow,2);
	pavwi->bPlaying  = FALSE;
	pavwi->TimerId = 0;
}
static void __AniAva_DebugRenderStrip()
{	
	return;
	#ifdef _DEBUG
	{
		HDC hDC_debug = GetDC(NULL);
		BitBlt(hDC_debug,0,0,AniAva.width, AniAva.height,AniAva.hAniAvaDC,0,0,SRCCOPY);
		DeleteDC(hDC_debug);
	}
	#endif
}

static void _AniAva_RenderAvatar(ANIAVA_WINDOWINFO * dat, HDC hdcParent /*= NULL*/, RECT * rcInParent /*= NULL */ )
{
	if (dat->bPaused>0)	{	dat->bPended = TRUE;	return; 	}
	else dat->bPended = FALSE;
	
	if ( IMMEDIATE_DRAW && hdcParent == NULL ) return;
	GdiFlush();
#ifdef _DEBUG
	__AniAva_DebugRenderStrip();
#endif
	if (dat->bPlaying && IsWindowVisible(dat->hWindow))
	{
		POINT ptWnd = {0};
		SIZE szWnd = {dat->rcPos.right-dat->rcPos.left,dat->rcPos.bottom-dat->rcPos.top};
		BLENDFUNCTION bf = {AC_SRC_OVER, 0,g_CluiData.bCurrentAlpha*dat->bAlpha/256, AC_SRC_ALPHA };
		POINT pt_from = {0,0};
		HDC hDC_animation = GetDC(NULL);
		HDC copyFromDC;
		RECT clistRect;
		HDC tempDC = NULL;
		HBITMAP hBmp;
		HBITMAP hOldBmp;

		/*
		int x = bf.SourceConstantAlpha;
		x = (49152/(383-x))-129;
		x = min(x,255);	x = max(x,0);
		bf.SourceConstantAlpha = x;
		*/
		if ( AniAva.bFlags == 0 ) //simple and fastest method - no borders, round corners and etc. just copy
		{
			pt_from.x = dat->ptFromPoint.x+dat->currentFrame*dat->sizeAvatar.cx;
			pt_from.y = dat->ptFromPoint.y;
			copyFromDC = AniAva.hAniAvaDC;
		}
		else
		{
			// ... need to create additional hDC_animation
			HRGN hRgn = NULL;
			int cornerRadius = AniAva.cornerRadius;
			tempDC	 = CreateCompatibleDC( NULL );
			hBmp	 = ske_CreateDIB32( szWnd.cx, szWnd.cy );
			hOldBmp	 = (HBITMAP)SelectObject(tempDC,hBmp);
			if ( AniAva.bFlags & AAO_ROUND_CORNERS )
			{
				if ( !cornerRadius)  //auto radius
					cornerRadius = min(szWnd.cx, szWnd.cy )/5;
			}
			if ( AniAva.bFlags & AAO_HAS_BORDER )
			{
				// if has borders - create region (round corners) and fill it, remember internal as clipping
				HBRUSH hBrush = CreateSolidBrush( AniAva.borderColor );
				HBRUSH hOldBrush = (HBRUSH)SelectObject( tempDC, hBrush );
				HRGN rgnOutside = CreateRoundRectRgn( 0, 0, szWnd.cx+1, szWnd.cy+1, cornerRadius << 1, cornerRadius << 1);
				hRgn = CreateRoundRectRgn( 1, 1, szWnd.cx, szWnd.cy, cornerRadius << 1, cornerRadius << 1);
				CombineRgn( rgnOutside,rgnOutside,hRgn,RGN_DIFF);
				FillRgn( tempDC, rgnOutside, hBrush);
				ske_SetRgnOpaque( tempDC, rgnOutside, TRUE);
				SelectObject(tempDC, hOldBrush);
				DeleteObject(hBrush);
				DeleteObject(rgnOutside);
			} 
			else if ( cornerRadius > 0 )
			{
				// else create clipping area (round corners)
				hRgn = CreateRoundRectRgn(0, 0, szWnd.cx+1, szWnd.cy+1, cornerRadius << 1, cornerRadius << 1);
			}
			else
			{
				hRgn = CreateRectRgn(0, 0, szWnd.cx+1, szWnd.cy+1);
			}
			// select clip area
			if ( hRgn )
				ExtSelectClipRgn(tempDC, hRgn, RGN_AND);

			if ( AniAva.bFlags & AAO_OPAQUE)
			{
				// if back color - fill clipping area
				HBRUSH hBrush = CreateSolidBrush( AniAva.bkgColor );
				HBRUSH hOldBrush = (HBRUSH)SelectObject( tempDC, hBrush );
				FillRgn( tempDC, hRgn, hBrush );
				ske_SetRgnOpaque( tempDC, hRgn, TRUE );
			}
			// draw avatar
			if ( !(AniAva.bFlags & AAO_OPAQUE))
				BitBlt(tempDC,0, 0, szWnd.cx, szWnd.cy , AniAva.hAniAvaDC , dat->ptFromPoint.x+dat->sizeAvatar.cx*dat->currentFrame, dat->ptFromPoint.y, SRCCOPY);
			else
			{
				BLENDFUNCTION abf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
				ske_AlphaBlend(tempDC,0, 0, szWnd.cx, szWnd.cy , AniAva.hAniAvaDC, dat->ptFromPoint.x+dat->sizeAvatar.cx*dat->currentFrame, dat->ptFromPoint.y, szWnd.cx, szWnd.cy, abf);
			}
			// reset clip area
			if ( hRgn )
			{
				DeleteObject(hRgn);
				hRgn = CreateRectRgn(0, 0, szWnd.cx, szWnd.cy);
				SelectClipRgn(tempDC, hRgn);
				DeleteObject(hRgn);
			}

			if (( AniAva.bFlags & AAO_HAS_OVERLAY )
				  && ( dat->overlayIconIdx != -1 )
				  && ( AniAva.overlayIconImageList ))
			{
				// if overlay - draw overlay icon
				// position - on avatar
				int x = szWnd.cx - ICON_WIDTH;
				int y = szWnd.cy - ICON_HEIGHT;
				ske_ImageList_DrawEx(AniAva.overlayIconImageList,
					dat->overlayIconIdx & 0xFFFF,
					tempDC, x, y, ICON_WIDTH, ICON_HEIGHT,
					CLR_NONE, CLR_NONE, ILD_NORMAL);
			}
			copyFromDC = tempDC;
		}
		// intersect visible area
		// update layered window
		GetWindowRect(pcli->hwndContactTree, &clistRect);
		if (dat->rcPos.top < 0)
		{
			pt_from.y += -dat->rcPos.top;
			szWnd.cy += dat->rcPos.top;
		}
		if (dat->rcPos.bottom>clistRect.bottom-clistRect.top)
			szWnd.cy -= (dat->rcPos.bottom-(clistRect.bottom-clistRect.top));

		ptWnd.x = dat->rcPos.left+clistRect.left;
		ptWnd.y = (dat->rcPos.top>0 ? dat->rcPos.top :0)+clistRect.top;
		if (szWnd.cy>0)
		{
			if ( hdcParent && rcInParent && IMMEDIATE_DRAW )
			{
				if ( AniAva.bFlags & AAO_OPAQUE )
					BitBlt( hdcParent, rcInParent->left, rcInParent->top, szWnd.cx, szWnd.cy, copyFromDC, pt_from.x, pt_from.y, SRCCOPY);
				else
				{
					BLENDFUNCTION abf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
					ske_AlphaBlend( hdcParent, rcInParent->left, rcInParent->top, szWnd.cx, szWnd.cy, copyFromDC, pt_from.x, pt_from.y, szWnd.cx, szWnd.cy, abf);
				}
			}
			else if ( !g_proc_UpdateLayeredWindow(dat->hWindow, hDC_animation, &ptWnd, &szWnd, copyFromDC, &pt_from, RGB(0,0,0), &bf, ULW_ALPHA ))
			{
				LONG exStyle;
				exStyle = GetWindowLongPtr(dat->hWindow,GWL_EXSTYLE);
				exStyle |= WS_EX_LAYERED;
				SetWindowLongPtr(dat->hWindow,GWL_EXSTYLE,exStyle);
				if ( !IMMEDIATE_DRAW )
					SetWindowPos( pcli->hwndContactTree, dat->hWindow, 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOSENDCHANGING );
				g_proc_UpdateLayeredWindow(dat->hWindow, hDC_animation, &ptWnd, &szWnd, copyFromDC, &pt_from, RGB(0,0,0), &bf, ULW_ALPHA );
			}

			g_CluiData.fAeroGlass = false;
			CLUI_UpdateAeroGlass();
		}
		else
		{
			dat->bPlaying = FALSE;
		}
		ReleaseDC(NULL,hDC_animation);
		if (tempDC)
		{
			SelectObject(tempDC, hOldBmp);
			DeleteObject(hBmp);
			DeleteDC(tempDC);
		}
	}
	if ( !dat->bPlaying)
	{
		ShowWindow(dat->hWindow, SW_HIDE);
		KillTimer(dat->hWindow,2);  //stop animation till set pos will be called
	}
	GdiFlush();
}
static void _AniAva_PausePainting()
{
	int i;
	for (i=0; i < AniAva.Objects->realCount; i++)
	{
		ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
		SendMessage(pai->hWindow,AAM_PAUSE,0,0);
	}
}
static void _AniAva_ResumePainting()
{
	int i;
	for (i=0; i < AniAva.Objects->realCount; i++)
	{
		ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
		SendNotifyMessage(pai->hWindow,AAM_RESUME,0,0);
	}
}

static void _AniAva_ReduceAvatarImages(int startY, int dY, BOOL bDestroyWindow)
{
	int i;
	for (i=0; i < AniAva.Objects->realCount; i++)
	{
		ANIAVA_OBJECT * pai = (ANIAVA_OBJECT *)AniAva.Objects->items[i];
		int res = SendMessage(pai->hWindow,AAM_REMOVEAVATAR,(WPARAM)startY,(LPARAM)dY);
		if (res == 0xDEAD && bDestroyWindow)
		{
			_AniAva_DestroyAvatarWindow(pai->hWindow);
			mir_free(pai);
			List_Remove(AniAva.Objects,i);
			i--;
		}
	}
}


static void _AniAva_LoadOptions()
{
	aacheck;
	aalock;
	{
		AniAva.bFlags = (db_get_b(NULL,"CList","AvatarsDrawBorders",SETTINGS_AVATARDRAWBORDER_DEFAULT)?	AAO_HAS_BORDER		:0) |
			(db_get_b(NULL,"CList","AvatarsRoundCorners",SETTINGS_AVATARROUNDCORNERS_DEFAULT)?	AAO_ROUND_CORNERS	:0) |	
			(db_get_b(NULL,"CList","AvatarsDrawOverlay",SETTINGS_AVATARDRAWOVERLAY_DEFAULT)?	AAO_HAS_OVERLAY		:0) |
			((0) ? AAO_OPAQUE :0);

		if (AniAva.bFlags & AAO_HAS_BORDER)
			AniAva.borderColor = (COLORREF)db_get_dw(NULL,"CList","AvatarsBorderColor",SETTINGS_AVATARBORDERCOLOR_DEFAULT);;
		if (AniAva.bFlags & AAO_ROUND_CORNERS)
			AniAva.cornerRadius = db_get_b(NULL,"CList","AvatarsUseCustomCornerSize",SETTINGS_AVATARUSECUTOMCORNERSIZE_DEFAULT)? db_get_w(NULL,"CList","AvatarsCustomCornerSize",SETTINGS_AVATARCORNERSIZE_DEFAULT) : 0;
		if (AniAva.bFlags & AAO_HAS_OVERLAY)
		{
			//check image list
			BYTE type = db_get_b(NULL,"CList","AvatarsOverlayType",SETTINGS_AVATAROVERLAYTYPE_DEFAULT);
			switch(type)
			{
			case SETTING_AVATAR_OVERLAY_TYPE_NORMAL:
				AniAva.overlayIconImageList = hAvatarOverlays;
				break;
			case SETTING_AVATAR_OVERLAY_TYPE_PROTOCOL:
			case SETTING_AVATAR_OVERLAY_TYPE_CONTACT:
				AniAva.overlayIconImageList = g_himlCListClc;
				break;
			default:
				AniAva.overlayIconImageList = NULL;
			}
		}
		if (AniAva.bFlags & AAO_OPAQUE)
			AniAva.bkgColor = 0;
		AniAva.bSeparateWindow = db_get_b(NULL,"CList","AvatarsInSeparateWnd",SETTINGS_AVATARINSEPARATE_DEFAULT); 

	}
	aaunlock;
}
static void _AniAva_AnimationTreadProc(HANDLE hExitEvent)
{
	//wait forever till hExitEvent signalled
	DWORD rc;
	HANDLE hThread = 0;
	DuplicateHandle(GetCurrentProcess(),GetCurrentThread(),GetCurrentProcess(),&hThread,0,FALSE,DUPLICATE_SAME_ACCESS);
	AniAva.AnimationThreadHandle = hThread;
	SetThreadPriority(hThread,THREAD_PRIORITY_LOWEST);
	for (;;)
	{
		if ( fnMsgWaitForMultipleObjectsEx )
			rc = fnMsgWaitForMultipleObjectsEx(1,&hExitEvent, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE);
		else
			rc = MsgWaitForMultipleObjects(1,&hExitEvent, FALSE, INFINITE, QS_ALLINPUT);

		ResetEvent(hExitEvent);
		if ( rc == WAIT_OBJECT_0 + 1 )
		{
			MSG msg;
			while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
			{
				if ( IsDialogMessage(msg.hwnd, &msg)) continue;
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else if ( rc == WAIT_OBJECT_0 )
		{
			break;
		}
	}
	CloseHandle(AniAva.AnimationThreadHandle);
	AniAva.AnimationThreadHandle = NULL;
}

static int	_AniAva_SortAvatarInfo(void * first, void * last)
{
	int res = 0;
	ANIAVA_INFO * aai1 = (ANIAVA_INFO *)first;
	ANIAVA_INFO * aai2 = (ANIAVA_INFO *)last;
	if (aai1 && aai1->tcsFilename  && 
		aai2 && aai2->tcsFilename)
	{
		res = _tcsicmp(aai2->tcsFilename, aai1->tcsFilename);
	}
	else
	{
		int a1 = (aai1 && aai1->tcsFilename)? 1:0;
		int a2 = (aai2 && aai2->tcsFilename)? 1:0;
		res = a1-a2;
	}

	if (res == 0)
	{
		if ( aai1->FrameSize.cx == aai2->FrameSize.cx && aai1->FrameSize.cy == aai2->FrameSize.cy )
			return 0;
		else
			return 1;
	}
	else
		return res;
}

void _AniAva_InvalidateParent(ANIAVA_WINDOWINFO * dat)
{
	if ( !IMMEDIATE_DRAW ) return;
	HWND hwndParent = pcli->hwndContactTree;
	RECT rcPos = dat->rcPos;
	pcli->pfnInvalidateRect( hwndParent, &rcPos, FALSE );
}

static LRESULT CALLBACK _AniAva_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	ANIAVA_WINDOWINFO * dat = NULL;
	if (msg == WM_TIMER || msg == WM_DESTROY  || 	(msg>AAM_FIRST && msg < AAM_LAST))
		dat = (ANIAVA_WINDOWINFO *)GetWindowLongPtr(hwnd, GWLP_USERDATA);

	switch (msg)
	{
	case AAM_REMOVEAVATAR:
		if (dat->ptFromPoint.x == (int)wParam) return 0xDEAD;	 //need to destroy window
		else if (dat->ptFromPoint.x>(int)wParam) dat->ptFromPoint.x -= (int)lParam;
		return 0;

	case AAM_PAUSE:
		dat->bPaused++;
		return 0;

	case AAM_RESUME:
		dat->bPaused--;
		if (dat->bPaused) return 0;
		if (dat->bPended) 
		{
			if ( !IMMEDIATE_DRAW )
				_AniAva_RenderAvatar(dat);
		}
		dat->bPended = FALSE;
		return 0;

	case AAM_STOP:
		if (dat->bPlaying)
		{
			dat->bPlaying = FALSE;
			KillTimer(hwnd,2);
			ShowWindow(hwnd, SW_HIDE);
		}
		return 0;

	case AAM_SETAVATAR:
		{
			ANIAVATARIMAGEINFO *paaii = (ANIAVATARIMAGEINFO*)wParam;
			_AniAva_Clear_ANIAVA_WINDOWINFO(dat);
			dat->nFramesCount = paaii->nFramesCount;
			dat->delaysInterval = paaii->pFrameDelays;
			dat->sizeAvatar = paaii->szSize;
			dat->ptFromPoint = paaii->ptImagePos;
			dat->currentFrame = 0;
			dat->bPlaying = FALSE;
			return MAKELONG(dat->sizeAvatar.cx,dat->sizeAvatar.cy);
		}

	case AAM_SETPOSITION:
		{
			ANIAVA_POSINFO * papi = (ANIAVA_POSINFO *)lParam;
			if ( !dat->delaysInterval) return 0;
			if ( !papi) return 0;
			dat->rcPos = papi->rcPos;
			dat->overlayIconIdx = papi->idxOverlay;
			dat->bAlpha = papi->bAlpha;
			free(papi);
			if ( !dat->bPlaying)
			{
				dat->bPlaying = TRUE;
				ShowWindow(hwnd,SW_SHOWNA);
				dat->currentFrame = 0;
				KillTimer(hwnd,2);
				SetTimer(hwnd,2,dat->delaysInterval[0],NULL);
			}
			if ( !IMMEDIATE_DRAW )
				_AniAva_RenderAvatar(dat);
			return 0;
		}
	case AAM_SETPARENT:
		if ( IMMEDIATE_DRAW ) return 0;
		dat->bOrderTop = ((HWND)wParam != GetDesktopWindow());
		SetParent(hwnd,(HWND)wParam);
		if (dat->bOrderTop)
		{
			SetWindowPos(hwnd,HWND_TOP,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_ASYNCWINDOWPOS);
		}
		else
		{
			LONG exStyle;
			exStyle = GetWindowLongPtr(pcli->hwndContactList,GWL_EXSTYLE);
			SetWindowPos(pcli->hwndContactList,hwnd,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE/*|SWP_ASYNCWINDOWPOS*/);			
			if ( !(exStyle&WS_EX_TOPMOST))
				SetWindowPos(pcli->hwndContactList,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE/*|SWP_ASYNCWINDOWPOS*/);
		}
		return 0;

	case AAM_REDRAW:
		if ( IMMEDIATE_DRAW )
			return 0;
		if ( wParam )
		{
			if (dat->bOrderTop)
			{
				SetWindowPos(hwnd,HWND_TOP,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_ASYNCWINDOWPOS);
			}
			else
			{
				LONG exStyle;
				exStyle = GetWindowLongPtr(pcli->hwndContactList,GWL_EXSTYLE);
				SetWindowPos(pcli->hwndContactList,hwnd,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE/*|SWP_ASYNCWINDOWPOS*/);
				if ( !(exStyle&WS_EX_TOPMOST))
					SetWindowPos(pcli->hwndContactList,HWND_NOTOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE/*|SWP_ASYNCWINDOWPOS*/);
			}
		}

		_AniAva_RenderAvatar( dat );
		return 0;


	case AAM_RENDER:
		{
			HDC hdc = ( HDC )wParam;
			RECT* rect = ( RECT* )lParam;
			_AniAva_RenderAvatar( dat, hdc, rect );
		}
		return 0;

	case AAM_SELFDESTROY:
		return DestroyWindow(hwnd);

	case WM_CREATE:
		{
			LONG exStyle;
			ANIAVA_WINDOWINFO * dat = (ANIAVA_WINDOWINFO *) mir_calloc(sizeof (ANIAVA_WINDOWINFO));
			SetWindowLongPtr(hwnd,GWLP_USERDATA,(LONG_PTR)dat);
			dat->hWindow = hwnd;
			//ShowWindow(dat->hWindow,SW_SHOW);
			//change layered mode
			exStyle = GetWindowLongPtr(dat->hWindow,GWL_EXSTYLE);
			exStyle |= WS_EX_LAYERED;
			SetWindowLongPtr(dat->hWindow,GWL_EXSTYLE,exStyle);
			exStyle = GetWindowLongPtr(dat->hWindow,GWL_STYLE);
			exStyle &= ~WS_POPUP;
			exStyle |= WS_CHILD;
			SetWindowLongPtr(dat->hWindow,GWL_STYLE,exStyle);
			break;
		}
	case WM_TIMER:
		{
			if ( !IsWindowVisible(hwnd))
			{
				DestroyWindow(hwnd);
				return 0;
			}
			dat->currentFrame++;
			if (dat->currentFrame >= dat->nFramesCount)
				dat->currentFrame = 0;

			if ( !IMMEDIATE_DRAW ) 
				_AniAva_RenderAvatar( dat );
			else
				_AniAva_InvalidateParent( dat );

			KillTimer(hwnd,2);
			SetTimer(hwnd,2,dat->delaysInterval[dat->currentFrame]+1,NULL);
			return 0;
		}
	case WM_DESTROY:
		{
			_AniAva_Clear_ANIAVA_WINDOWINFO(dat);
			mir_free(dat);
			SetWindowLongPtr(hwnd,GWLP_USERDATA,(LONG_PTR)NULL);
			break;
		}

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

#undef aacheck
#undef aalock
#undef aaunlock

/////////////////////////////////////////////////////////////////
// some stub

HWND WINAPI MyGetAncestor( HWND hWnd, UINT option )
{
	if ( option == GA_PARENT )
		return GetParent( hWnd );

	if ( option == GA_ROOTOWNER ) {
		HWND result = hWnd;
		while( true ) {
			HWND hParent = GetParent( result );
			if ( !hParent )
				return result;

			result = hParent;
		}
	}

	return NULL;
}