summaryrefslogtreecommitdiff
path: root/plugins/FloatingContacts/src/main.cpp
blob: 900e6e7c7b86c8b42b3bd0daed6bd640ee377984 (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
/*
Miranda Floating Contacts plugin, created by Iavor Vajarov ( ivajarov@code.bg )
http://miranda-icq.sourceforge.net/

Miranda fonts and colors settings by Ranger.
Extended bonts and backgrounds settings by Oleksiy Shurubura


This file is placed in the public domain. Anybody is free to use or
modify it as they wish with no restriction.

This plugin has been tested to work under Windows ME, 2000 and XP.

No warranty for any misbehaviour.
*/

#include "stdhdr.h"

BOOL (WINAPI *pSetLayeredWindowAttributes)(HWND, COLORREF, BYTE, DWORD);
BOOL (WINAPI *pUpdateLayeredWindow)
	(HWND hwnd, HDC hdcDST, POINT *pptDst, SIZE *psize, HDC hdcSrc, POINT *pptSrc,
	 COLORREF crKey, BLENDFUNCTION *pblend, DWORD dwFlags);

// Globals

// TODO: move to some more approriate place, probably part of Thumbs manager
static void	LoadContacts			( void );
static void LoadContact				( HANDLE hContact );

// Internal funcs
static void	RepaintWindow			( HWND hwnd, HDC hdc );
static void	LoadMenus				();
static void	CreateThumbWnd			( TCHAR *ptszName, HANDLE hContact, int nX, int nY );
static void	RegisterWindowClass		( void );
static void	UnregisterWindowClass	( void );
static void	CleanUp					( void );
static BOOL GetOSPlatform			( void );
static void LoadDBSettings			( void );
static void CreateThumbsFont		( void );
static void CreateBackgroundBrush	( void );
static int	GetContactStatus		( HANDLE hContact );
static void	GetScreenRect			( void );
extern void SetThumbsOpacity		( BYTE btAlpha );
static int	ClcStatusToPf2			( int status );

static LRESULT __stdcall CommWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
static LRESULT __stdcall newMirandaWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

static int OnContactDeleted			( WPARAM wParam, LPARAM lParam );
static int OnContactIconChanged		( WPARAM wParam, LPARAM lParam );
static int OnContactDrag			( WPARAM wParam, LPARAM lParam );
static int OnContactDrop			( WPARAM wParam, LPARAM lParam );
static int OnContactDragStop		( WPARAM wParam, LPARAM lParam );
static int OnSkinIconsChanged		( WPARAM wParam, LPARAM lParam );
static int OnContactSettingChanged	( WPARAM wParam, LPARAM lParam );
static int OnStatusModeChange		( WPARAM wParam, LPARAM lParam );
static int OnModulesLoded			( WPARAM wParam, LPARAM lParam );
static int OnPrebuildContactMenu	( WPARAM wParam, LPARAM lParam );

static INT_PTR OnContactMenu_Remove		( WPARAM wParam, LPARAM lParam );
static INT_PTR OnMainMenu_HideAll		( WPARAM wParam, LPARAM lParam );
static INT_PTR OnHotKey_HideWhenCListShow( WPARAM wParam, LPARAM lParam );
static VOID CALLBACK ToTopTimerProc ( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);

WNDPROC oldMirandaWndProc;


HINSTANCE	hInst				 =  NULL;
HMODULE		hUserDll			 =  NULL;
HFONT		hFont[FLT_FONTIDS]	 =  {NULL};
COLORREF	tColor[FLT_FONTIDS]	 =  {0};
HIMAGELIST	himl				 =  NULL;
HANDLE		hevContactIcon		 =  NULL;
HANDLE		hevContactDrop		 =  NULL;
HANDLE		hevContactDragStop	 =  NULL;
HANDLE		hevSkinIcons		 =  NULL;
HANDLE		hevContactDrag		 =  NULL;
HANDLE		hevContactSetting	 =  NULL;
HANDLE		hevContactDeleted	 =  NULL;
HANDLE		hevOptionsInit		 =  NULL;
HANDLE		hevStatusMode		 =  NULL;
HANDLE		hevModules			 =  NULL;
HANDLE		hevPrebuildMenu		 =  NULL;
HANDLE		hNewContact			 =  NULL;

HPEN		hLTEdgesPen			 =  NULL;
HPEN		hRBEdgesPen			 =  NULL;
HBRUSH		hBkBrush			 =  NULL;
DWORD		bkColor				 =  0;
HBITMAP		hBmpBackground		 =  NULL;
WORD		nBackgroundBmpUse	 =  CLB_STRETCH;

HWND		hwndMiranda			 =  NULL;
BOOL		bVersionOK			 =  FALSE;
BOOL		bNT					 =  FALSE;
BOOL		bDockHorz			 =  TRUE;
//UINT		nStatus				 =  0;
HMENU		hContactMenu		 =  NULL;
HANDLE		hMenuItemRemove		 =  NULL;
HANDLE		hMenuItemHideAll	 =  NULL;
HANDLE		hMainMenuItemHideAll	 =  NULL;
RECT		rcScreen;
DWORD		dwOfflineModes		 =  0;
BOOL		bEnableTip			 =  FALSE;
UINT_PTR	ToTopTimerID		 =  0;
BOOL		bIsCListShow		 =  TRUE;

HANDLE		hRemoveThumb		 =  NULL;
HANDLE		hMainHideAllThumbs	 =  NULL;
HANDLE		hHideWhenCListShow	 =  NULL;
int hLangpack;

//Options

FCOptions fcOpt = {0};

static void InitOptions() {
	fcOpt.thumbAlpha			 =  255;
	fcOpt.bHideOffline			 =  FALSE;
	fcOpt.bHideAll				 =  FALSE;
	fcOpt.bHideWhenFullscreen	 =  FALSE;
	fcOpt.bMoveTogether			 =  FALSE;
	fcOpt.bFixedWidth			 =  FALSE;
	fcOpt.nThumbWidth			 =  0;
	fcOpt.bShowTip				 =  TRUE;
	fcOpt.TimeIn				 =  0;
	fcOpt.bToTop				 =  TRUE;
	fcOpt.ToTopTime				 =  TOTOPTIME_DEF;
	fcOpt.bHideWhenCListShow	 =  FALSE;
	fcOpt.bUseSingleClick		 =  FALSE;
	fcOpt.bShowIdle				 =  TRUE;
}

PLUGININFOEX pluginInfoEx  = {
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {53C715A8-EB01-4136-A73C-441868610074}
	{0x53c715a8, 0xeb01, 0x4136, {0xa7, 0x3c, 0x44, 0x18, 0x68, 0x61, 0x0, 0x74}}
};

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD mirandaVersion)
{
	return &pluginInfoEx;
}

///////////////////////////////////////////////////////
// Load / unload
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	hInst = hinstDLL;
	return TRUE;
}

extern "C" int __declspec(dllexport) Load()
{
	SetLastError(0);
	InitOptions();

	mir_getLP(&pluginInfoEx);

	hevModules = HookEvent( ME_SYSTEM_MODULESLOADED,  OnModulesLoded );
	bNT = GetOSPlatform();

	if (hUserDll = LoadLibrary(_T("user32.dll"))) {
		pSetLayeredWindowAttributes  =
			(BOOL (WINAPI *)(HWND, COLORREF, BYTE, DWORD))
			GetProcAddress(hUserDll, "SetLayeredWindowAttributes");
		pUpdateLayeredWindow  =
			(BOOL (WINAPI *)(HWND, HDC, POINT *, SIZE *, HDC, POINT *, COLORREF, BLENDFUNCTION *, DWORD))
			GetProcAddress(hUserDll, "UpdateLayeredWindow");
	}
	else {
		pSetLayeredWindowAttributes = NULL;
		pUpdateLayeredWindow = NULL;
	}

	return 0;
}

extern "C" int __declspec(dllexport) Unload()
{
	CleanUp();
	return 0;
}

static void CleanUp()
{
	int nFontId;

	UnhookEvent( hevContactIcon );
	UnhookEvent( hevContactDrag );
	UnhookEvent( hevContactDrop );
	UnhookEvent( hevContactDragStop );
	UnhookEvent( hevSkinIcons );
	UnhookEvent( hevContactDeleted );
	UnhookEvent( hevContactSetting );
	UnhookEvent( hevOptionsInit );
	UnhookEvent( hevStatusMode );
	UnhookEvent( hevModules );
	UnhookEvent( hevPrebuildMenu );

	if (hRemoveThumb)
		DestroyServiceFunction(hRemoveThumb);
	if (hMainHideAllThumbs)
		DestroyServiceFunction(hMainHideAllThumbs);
	if (hHideWhenCListShow)
		DestroyServiceFunction(hHideWhenCListShow);

	if (ServiceExists(MS_HOTKEY_UNREGISTER)) {
		CallService(MS_HOTKEY_UNREGISTER, 0, (LPARAM)(sModule "/MainHideAllThumbs"));
		CallService(MS_HOTKEY_UNREGISTER, 0, (LPARAM)(sModule "/HideWhenCListShow"));
	}

	if (NULL != hLTEdgesPen)
		DeleteObject(hLTEdgesPen);
	if (NULL != hRBEdgesPen)
		DeleteObject(hRBEdgesPen);
	if (NULL != hBmpBackground)
		DeleteObject(hBmpBackground);
	if (NULL != hBkBrush) {
		SetClassLong((HWND)WND_CLASS, GCLP_HBRBACKGROUND, (LONG)NULL);
		DeleteObject(hBkBrush);
		hBkBrush = NULL;
	}

	for (nFontId = 0; nFontId < FLT_FONTIDS; nFontId++)
		if (NULL != hFont[nFontId])
			DeleteObject(hFont[nFontId]);

	UnregisterWindowClass();

	FreeFileDropping();

	if ( hUserDll != NULL )
		FreeLibrary( hUserDll );
}



///////////////////////////////////////////////////////
// Hooked events
static int OnModulesLoded( WPARAM wParam, LPARAM lParam )
{
	hevContactIcon		 =  HookEvent( ME_CLIST_CONTACTICONCHANGED,  OnContactIconChanged );
	hevSkinIcons		 =  HookEvent( ME_SKIN_ICONSCHANGED,  OnSkinIconsChanged );
	hevContactDrag		 =  HookEvent( ME_CLUI_CONTACTDRAGGING,  OnContactDrag );
	hevContactDrop		 =  HookEvent( ME_CLUI_CONTACTDROPPED,  OnContactDrop );
	hevContactDragStop	 =  HookEvent( ME_CLUI_CONTACTDRAGSTOP,  OnContactDragStop );
	hevContactSetting	 =  HookEvent( ME_DB_CONTACT_SETTINGCHANGED, OnContactSettingChanged );
	hevContactDeleted	 =  HookEvent( ME_DB_CONTACT_DELETED, OnContactDeleted );
	hevOptionsInit		 =  HookEvent( ME_OPT_INITIALISE, OnOptionsInitialize );
	hevStatusMode		 =  HookEvent( ME_CLIST_STATUSMODECHANGE, OnStatusModeChange );
	hevPrebuildMenu		 =  HookEvent( ME_CLIST_PREBUILDCONTACTMENU, OnPrebuildContactMenu );
	hwndMiranda			 =  (HWND)CallService( MS_CLUI_GETHWND, 0, 0 );

	oldMirandaWndProc	 =  (WNDPROC)SetWindowLongPtr( hwndMiranda, GWLP_WNDPROC, (LONG)newMirandaWndProc);



	// No thumbs yet
//	pThumbsList	 =  NULL;
	bEnableTip = ServiceExists("mToolTip/ShowTip");

	RegisterWindowClass();
	InitFileDropping();
	GetScreenRect();
	LoadDBSettings();
	CreateBackgroundBrush();
	CreateThumbsFont();
	LoadContacts();
	LoadMenus();

	if (fcOpt.bToTop){
		fcOpt.ToTopTime = (fcOpt.ToTopTime<1)?1:fcOpt.ToTopTime;
		fcOpt.ToTopTime = (fcOpt.ToTopTime>TOTOPTIME_MAX)?TOTOPTIME_MAX:fcOpt.ToTopTime;
		ToTopTimerID = SetTimer(NULL, 0, fcOpt.ToTopTime*TOTOPTIME_P, ToTopTimerProc);
	}
	return( 0 );
}


static int OnContactDeleted( WPARAM wParam, LPARAM lParam )
{
	HANDLE		hContact	 =  ( HANDLE )wParam;
	ThumbInfo	*pThumb		 =  thumbList.FindThumbByContact( hContact );
	if (!pThumb) return 0;

	pThumb->DeleteContactPos();
	thumbList.RemoveThumb(pThumb);

	return 0;
}


static int OnContactIconChanged( WPARAM wParam, LPARAM lParam )
{
	HANDLE		hContact	 =  ( HANDLE )wParam;
	ThumbInfo	*pThumb		 =  thumbList.FindThumbByContact( hContact );

	if ( pThumb )
	{
		pThumb->RefreshContactIcon( ( int )lParam );
		//pThumb->ThumbSelect( FALSE );
		//SetTimer( pThumb->hwnd, TIMERID_SELECT_T, 1000, NULL );
	}

	return 0;
}


static int OnContactDrag( WPARAM wParam, LPARAM lParam )
{
	ThumbInfo	*pNewThumb	 =  NULL;
	HANDLE		hContact	 =  ( HANDLE )wParam;
	ThumbInfo	*pThumb		 =  thumbList.FindThumbByContact( hContact );
	TCHAR		*ptName		 =  (TCHAR*)CallService( MS_CLIST_GETCONTACTDISPLAYNAME, wParam, (LPARAM)GCDNF_TCHAR );
	int			idStatus	 =  ID_STATUS_OFFLINE;
	POINT pt;
	GetCursorPos(&pt);

	if ( pThumb == NULL ) {
		idStatus = GetContactStatus( hContact );

		if ( !fcOpt.bHideAll && !HideOnFullScreen() && ( !fcOpt.bHideOffline || IsStatusVisible( idStatus ))  && (!fcOpt.bHideWhenCListShow || !bIsCListShow)) {
			CreateThumbWnd( ptName, hContact, pt.x, pt.y );
			pNewThumb = thumbList.FindThumbByContact( hContact );
			ShowWindow( pNewThumb->hwnd, SW_SHOWNA );
			hNewContact = hContact;

			OnStatusChanged();
		}
	}
	else if ( hContact == hNewContact )
		pThumb->PositionThumb( (short)( pt.x - 5 ), (short)( pt.y - 5 ));

	return( hNewContact != NULL ? 1 : 0 );
}



static int OnContactDrop( WPARAM wParam, LPARAM lParam )
{
	RECT	rcMiranda;
	RECT	rcThumb;
//	RECT	rcOverlap;

	HANDLE	hContact	 =  ( HANDLE )wParam;
	ThumbInfo *pThumb	 =  thumbList.FindThumbByContact( hContact );

	if ( ( hNewContact == hContact ) && ( pThumb != NULL ))
	{
		hNewContact = NULL;

		GetWindowRect( hwndMiranda, &rcMiranda );
		pThumb->GetThumbRect( &rcThumb );

		//if ( IntersectRect( &rcOverlap, &rcThumb, &rcMiranda ))
		//{
		//	pThumb->OnLButtonDown( (short)(rcThumb.left + 5), (short)(rcThumb.top + 5));
		//}
	}

	SaveContactsPos();

	return( 1 );
}


static int OnContactDragStop( WPARAM wParam, LPARAM lParam )
{
	HANDLE	hContact = ( HANDLE )wParam;
	ThumbInfo *pThumb = thumbList.FindThumbByContact( hContact );

	if ( ( pThumb != NULL ) && ( hNewContact == hContact ))
	{
		thumbList.RemoveThumb( pThumb );
		hNewContact = NULL;
	}

	return( 0 );
}


static int OnSkinIconsChanged( WPARAM wParam, LPARAM lParam )
{
	// Get handle to the image list
	himl = ( HIMAGELIST )CallService( MS_CLIST_GETICONSIMAGELIST, 0, 0 );

	// Update thumbs
	for (int i = 0; i < thumbList.getCount(); ++i)
		thumbList[i]->UpdateContent();

	return( 0 );
}



static int OnContactSettingChanged( WPARAM wParam, LPARAM lParam )
{
	HANDLE		hContact	 =  ( HANDLE )wParam;
	ThumbInfo	*pThumb		 =  thumbList.FindThumbByContact( hContact );
	int			idStatus	 =  ID_STATUS_OFFLINE;
	BOOL		bRefresh	 =  TRUE;
	DBCONTACTWRITESETTING* pdbcws = ( DBCONTACTWRITESETTING* )lParam;


	if ( hContact == NULL )
	{
		if (( 0 == _stricmp( pdbcws->szModule, "CLC" )) || ( 0 == _stricmp( pdbcws->szModule, sModule )) ) {
			LoadDBSettings();
			ApplyOptionsChanges();
		}

		return( 0 );
	}

	if ( pThumb == NULL ) return( 0 );

	// Only on these 2 events we need to refresh
	if ( 0 == _stricmp( pdbcws->szSetting, "Status" ))
	{
		idStatus = pdbcws->value.wVal;
	}
	else if ( 0 == _stricmp( pdbcws->szSetting, "Nick" ))
	{
		idStatus = GetContactStatus( hContact );
	}
	else if ( 0 == _stricmp( pdbcws->szSetting, "MyHandle" ))
	{
		idStatus = GetContactStatus( hContact );
	}
	else if ( fcOpt.bShowIdle && 0 == _stricmp( pdbcws->szSetting, "IdleTS" ))
	{
		idStatus = GetContactStatus( hContact );
	}
	else
	{
		bRefresh = FALSE;
	}

	if ( bRefresh )
	{
		// Detach call
		PostMessage( pThumb->hwnd, WM_REFRESH_CONTACT, 0, idStatus );
	}

	return( 0 );
}


static int OnStatusModeChange( WPARAM wParam, LPARAM lParam )
{
	int			idStatus;

	//nStatus = (int)wParam;

	for (int i = 0; i < thumbList.getCount(); ++i)
	{
		idStatus = GetContactStatus( thumbList[i]->hContact );
		thumbList[i]->RefreshContactStatus( idStatus );
	}
	if ( wParam == ID_STATUS_OFFLINE )
	{
		// Floating status window will use this
	}

	return( 0 );
}



static int OnPrebuildContactMenu( WPARAM wParam, LPARAM lParam )
{
	ThumbInfo *pThumb = thumbList.FindThumbByContact( (HANDLE) wParam );

	CLISTMENUITEM clmi = { sizeof(clmi) };
	clmi.flags = ( pThumb == NULL ) ? CMIM_FLAGS | CMIF_HIDDEN : CMIM_FLAGS &~CMIF_HIDDEN;
	CallService( MS_CLIST_MODIFYMENUITEM, (WPARAM)hMenuItemRemove, (LPARAM)&clmi );

	clmi.flags = fcOpt.bHideAll ? CMIM_FLAGS | CMIF_HIDDEN : CMIM_FLAGS &~CMIF_HIDDEN;
	CallService( MS_CLIST_MODIFYMENUITEM, (WPARAM)hMenuItemHideAll, (LPARAM)&clmi );

	return( 0 );
}




///////////////////////////////////////////////////////
// General functions

static void LoadDBSettings()
{
	fcOpt.thumbAlpha			 =  (BYTE)((double)DBGetContactSettingByte( NULL, sModule, "Opacity", 100 ) * 2.55 );
	fcOpt.bHideOffline			 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "HideOffline", 0 );
	fcOpt.bHideAll				 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "HideAll", 0 );
	fcOpt.bHideWhenFullscreen	 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "HideWhenFullscreen", 0 );
	fcOpt.bMoveTogether			 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "MoveTogether", 0 );
	fcOpt.bFixedWidth			 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "FixedWidth", 0 );
	fcOpt.nThumbWidth			 =  (DWORD)DBGetContactSettingDword( NULL, sModule, "Width", 0 );
	dwOfflineModes				 =  DBGetContactSettingDword( NULL, "CLC", "OfflineModes", CLCDEFAULT_OFFLINEMODES );
	fcOpt.bShowTip				 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "ShowTip", 1 );
	fcOpt.TimeIn				 =  (WORD)DBGetContactSettingWord( NULL, sModule, "TimeIn", 0 );
	fcOpt.bToTop				 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "ToTop", 0 );
	fcOpt.ToTopTime				 =  (WORD)DBGetContactSettingWord( NULL, sModule, "ToTopTime", TOTOPTIME_DEF );
	fcOpt.bHideWhenCListShow	 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "HideWhenCListShow", 0 );
	fcOpt.bUseSingleClick		 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "UseSingleClick", 0 );
	fcOpt.bShowIdle				 =  (BOOL)DBGetContactSettingByte( NULL, sModule, "ShowIdle", 0 );

	if (DBGetContactSettingByte(NULL, "ModernData", "HideBehind", 0))
		bIsCListShow = (DBGetContactSettingByte(NULL, "ModernData", "BehindEdge", 0) == 0);
	else
		bIsCListShow = (DBGetContactSettingByte(NULL, "CList", "State", 0) == 2);
}

void SendMsgDialog( HWND hwnd, TCHAR *pText )
{
	ThumbInfo *pThumb = thumbList.FindThumb( hwnd );

	if ( pThumb != NULL )
		CallService( MS_MSG_SENDMESSAGET, (WPARAM)pThumb->hContact, (LPARAM)pText );
}


static void ShowContactMenu( HWND hwnd, POINT pt )
{
	ThumbInfo	*pThumb		 =  thumbList.FindThumb( hwnd );

	if ( pThumb != NULL )
	{
		hContactMenu = (HMENU)CallService( MS_CLIST_MENUBUILDCONTACT, (WPARAM)pThumb->hContact, 0 );

		if ( hContactMenu == NULL ) return;

		int idCommand = TrackPopupMenu( hContactMenu, TPM_RIGHTALIGN | TPM_TOPALIGN | TPM_RETURNCMD, pt.x, pt.y, 0 , hwnd, NULL );
		CallService( MS_CLIST_MENUPROCESSCOMMAND, MAKEWPARAM( idCommand , MPCF_CONTACTMENU), (LPARAM)pThumb->hContact );
	}
}

static LRESULT __stdcall CommWndProc(	HWND	hwnd,
										UINT	uMsg,
										WPARAM	wParam,
										LPARAM  lParam	)
{
	LRESULT		lResult		 =  0;
	ThumbInfo	*pThumb		 =  thumbList.FindThumb( hwnd );

	switch( uMsg )
	{
	case WM_RBUTTONUP:
		{
			POINT pt;
			pt.x = LOWORD( lParam );
			pt.y = HIWORD( lParam );

			if (pThumb) pThumb->ThumbDeselect( TRUE );

			ClientToScreen(hwnd, &pt);
			ShowContactMenu( hwnd, pt );
		}

		break;

//#if 0
	case WM_NCPAINT:
		if (pThumb){
			HDC hdc = GetWindowDC( hwnd );
			BitBlt(hdc, 0, 0, pThumb->bmpContent.getWidth(), pThumb->bmpContent.getHeight(), pThumb->bmpContent.getDC(), 0, 0, SRCCOPY);
			//RepaintWindow( hwnd, hdc );
			ReleaseDC( hwnd, hdc );
			ValidateRect( hwnd, NULL );
			return 0;
		}

	case WM_PAINT:
		if (pThumb){
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hwnd, &ps);
			BitBlt(hdc, 0, 0, pThumb->bmpContent.getWidth(), pThumb->bmpContent.getHeight(), pThumb->bmpContent.getDC(), 0, 0, SRCCOPY);
			//RepaintWindow( hwnd, hdc );
			EndPaint(hwnd, &ps);
			break;
		}

	case WM_PRINT:
	case WM_PRINTCLIENT:
		if (pThumb){
			BitBlt((HDC)wParam, 0, 0, pThumb->bmpContent.getWidth(), pThumb->bmpContent.getHeight(), pThumb->bmpContent.getDC(), 0, 0, SRCCOPY);
			//RepaintWindow(hwnd, (HDC)wParam);
			break;
		}
//#endif

	case WM_MEASUREITEM:
		lResult = CallService( MS_CLIST_MENUMEASUREITEM, wParam, lParam );
		break;

	case WM_DRAWITEM:
		lResult = CallService( MS_CLIST_MENUDRAWITEM, wParam, lParam );
		break;

	case WM_LBUTTONDOWN:
		if (pThumb) pThumb->OnLButtonDown( (short)LOWORD( lParam ), (short)HIWORD( lParam ));
		break;

	case WM_MOUSEMOVE:
		if (pThumb) pThumb->OnMouseMove( (short)LOWORD( lParam ), (short)HIWORD( lParam ), wParam);
		break;

	case WM_LBUTTONUP:
		if (pThumb) pThumb->OnLButtonUp();
		//if (bMouseMoved || !DBGetContactSettingByte(NULL, "CList", "Tray1Click", SETTING_TRAY1CLICK_DEFAULT))
		break;
		// FALL THRU

	case WM_LBUTTONDBLCLK:
		// Popup message dialog
		//if (pThumb) pThumb->ThumbDeselect( TRUE );
		if (!fcOpt.bUseSingleClick && pThumb)
			pThumb->PopUpMessageDialog();
		break;

	case WM_RBUTTONDOWN:
		if (bEnableTip && fcOpt.bShowTip && pThumb) pThumb->KillTip();
		break;
	case WM_REFRESH_CONTACT:
		if (pThumb)
		{
			_tcsncpy( pThumb->ptszName, (TCHAR*)CallService( MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)pThumb->hContact, (LPARAM)GCDNF_TCHAR ), USERNAME_LEN - 1 );
			pThumb->RefreshContactStatus( (int)lParam );
			pThumb->ResizeThumb();
		}
		break;

	case WM_TIMER:
		if (pThumb)pThumb->OnTimer(wParam);
		break;

	case WM_HOTKEY:
		{
			ThumbInfo *pThumb = thumbList.FindThumb( ( HWND )wParam );

			if (pThumb)	pThumb->PopUpMessageDialog();
		}

	default:
		break;
	}

	lResult = DefWindowProc( hwnd, uMsg, wParam, lParam );

	return( lResult );
}



extern void SetThumbsOpacity( BYTE btAlpha )
{
	for (int i = 0; i < thumbList.getCount(); ++i)
		thumbList[i]->SetThumbOpacity( btAlpha );
}



static void GetScreenRect()
{
	rcScreen.left	 =  GetSystemMetrics( SM_XVIRTUALSCREEN );
	rcScreen.top	 =  GetSystemMetrics( SM_YVIRTUALSCREEN );
	rcScreen.right	 =  GetSystemMetrics( SM_CXVIRTUALSCREEN ) + rcScreen.left;
	rcScreen.bottom	 =  GetSystemMetrics( SM_CYVIRTUALSCREEN ) + rcScreen.top;
}



void OnStatusChanged()
{
	int			idStatus	 =  ID_STATUS_OFFLINE;

	for (int i = 0; i < thumbList.getCount(); ++i)
	{
		idStatus = GetContactStatus( thumbList[i]->hContact );
		thumbList[i]->RefreshContactStatus( idStatus );
	}
}



void ApplyOptionsChanges()
{
	CreateThumbsFont();
	CreateBackgroundBrush();

//	dwOfflineModes	 =  DBGetContactSettingDword( NULL, "CLC", "OfflineModes", CLCDEFAULT_OFFLINEMODES );
	if (!fcOpt.bToTop && ToTopTimerID){
		KillTimer(NULL, ToTopTimerID);
		ToTopTimerID = 0;
	}
	if (fcOpt.bToTop){
		if (ToTopTimerID) KillTimer(NULL, ToTopTimerID);
		fcOpt.ToTopTime = (fcOpt.ToTopTime<1)?1:fcOpt.ToTopTime;
		fcOpt.ToTopTime = (fcOpt.ToTopTime>TOTOPTIME_MAX)?TOTOPTIME_MAX:fcOpt.ToTopTime;
		ToTopTimerID = SetTimer(NULL, 0, fcOpt.ToTopTime*TOTOPTIME_P, ToTopTimerProc);
	}

	OnStatusChanged();

	for (int i = 0; i < thumbList.getCount(); ++i)
		thumbList[i]->ResizeThumb();
}



///////////////////////////////////////////////////////
// Window creation
static void RegisterWindowClass()
{
	WNDCLASSEX	wcx;
	ZeroMemory( &wcx, sizeof( wcx ));

	wcx.cbSize			 = 	sizeof( WNDCLASSEX );
	wcx.style			 = 	CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
	wcx.lpfnWndProc		 = 	CommWndProc;
	wcx.cbClsExtra		 = 	0;
	wcx.cbWndExtra		 = 	0;
	wcx.hInstance		 = 	hInst;
	wcx.hIcon			 = 	NULL;
	wcx.hCursor			 = 	NULL;
	wcx.hbrBackground	 = 	GetSysColorBrush(COLOR_3DFACE);
	wcx.lpszMenuName	 = 	NULL;
	wcx.lpszClassName	 = 	WND_CLASS;
	wcx.hIconSm			 = 	NULL;

	SetLastError( 0 );

	RegisterClassEx( &wcx );
}



static void UnregisterWindowClass()
{
	UnregisterClass( WND_CLASS, hInst );
}



static void CreateThumbWnd( TCHAR *ptszName, HANDLE hContact, int nX, int nY )
{
	HWND		hwnd		 =  NULL;
	ThumbInfo	*pThumb		 =  thumbList.FindThumbByContact( hContact );

	// Prepare for window creation
	if ( pThumb == NULL )
	{
		hwnd = CreateWindowEx(	WS_EX_TOOLWINDOW | WS_EX_TOPMOST,
								WND_CLASS,
								ptszName,
								WS_POPUP,
								nX,
								nY,
								50,
								20,
								NULL /*hwndMiranda*/,
								NULL,
								hInst,
								NULL
								 );

		if ( hwnd != NULL )
		{
			pThumb = thumbList.AddThumb( hwnd, ptszName, hContact );
			SetWindowLongPtr( hwnd, GWLP_USERDATA, (long)pThumb );
			pThumb->ResizeThumb();

			pThumb->SetThumbOpacity( fcOpt.thumbAlpha );
			ShowWindow( hwnd, ( fcOpt.bHideAll || HideOnFullScreen() || fcOpt.bHideOffline  || (fcOpt.bHideWhenCListShow && bIsCListShow))? SW_HIDE : SW_SHOWNA );
			pThumb->PositionThumb( (short)nX, (short)nY );

			// force repaint
			pThumb->UpdateContent();
		}
	}
}



static BOOL GetOSPlatform()
{
	OSVERSIONINFO	VersionInfo;

	// Get OS platform
	ZeroMemory( &VersionInfo, sizeof( VersionInfo ));
	VersionInfo.dwOSVersionInfoSize = sizeof( VersionInfo );

	GetVersionEx( &VersionInfo );
	return( VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT );
}



static void CreateThumbsFont()
{
	int nFontId;
	LOGFONTA lf;

	for (nFontId = 0; nFontId < FLT_FONTIDS; nFontId++)
	{
		if (NULL != hFont[nFontId])
		{
			DeleteObject(hFont[nFontId]);
			hFont[nFontId] = NULL;
		}
		GetFontSetting(TRUE, nFontId, &lf, &tColor[nFontId]);
		{
			LONG height;
			HDC hdc = GetDC(NULL);
			height = lf.lfHeight;
			lf.lfHeight = -MulDiv(lf.lfHeight, GetDeviceCaps(hdc, LOGPIXELSY), 72);
			ReleaseDC(NULL, hdc);

			hFont[nFontId] = CreateFontIndirectA(&lf);

			lf.lfHeight = height;
		}
	}
}



static void CreateBackgroundBrush()
{
//	LOGBRUSH	lb;
	bkColor = DBGetContactSettingDword(NULL, sModule, "BkColor", FLT_DEFAULT_BKGNDCOLOR);


	if (NULL != hLTEdgesPen)
	{
		DeleteObject(hLTEdgesPen);
		hLTEdgesPen = NULL;
	}
	if (NULL != hRBEdgesPen)
	{
		DeleteObject(hRBEdgesPen);
		hRBEdgesPen = NULL;
	}
	if (NULL != hBmpBackground)
	{
		DeleteObject(hBmpBackground);
		hBmpBackground = NULL;
	}
	if (NULL != hBkBrush)
	{
		SetClassLong((HWND)WND_CLASS, GCLP_HBRBACKGROUND, (LONG)NULL);
		DeleteObject( hBkBrush );
		hBkBrush = NULL;
	}

	if (DBGetContactSettingByte(NULL, sModule, "DrawBorder", FLT_DEFAULT_DRAWBORDER))
	{
		COLORREF cr;

		cr = (COLORREF)DBGetContactSettingDword(NULL, sModule, "LTEdgesColor", FLT_DEFAULT_LTEDGESCOLOR);
		hLTEdgesPen = CreatePen(PS_SOLID, 1, cr);
		cr = (COLORREF)DBGetContactSettingDword(NULL, sModule, "RBEdgesColor", FLT_DEFAULT_RBEDGESCOLOR);
		hRBEdgesPen = CreatePen(PS_SOLID, 1, cr);
	}
	if (DBGetContactSettingByte(NULL, sModule, "BkUseBitmap", FLT_DEFAULT_BKGNDUSEBITMAP))
	{
		DBVARIANT dbv;

		if (!DBGetContactSetting(NULL, sModule, "BkBitmap", &dbv))
		{
			hBmpBackground = (HBITMAP)CallService(MS_UTILS_LOADBITMAP, 0, (LPARAM)dbv.pszVal);
			DBFreeVariant(&dbv);
		}
	}
	nBackgroundBmpUse = (WORD)DBGetContactSettingWord(NULL, sModule, "BkBitmapOpt", FLT_DEFAULT_BKGNDBITMAPOPT);

	// Create brush
	hBkBrush	 =  CreateSolidBrush(bkColor);

	// Attach brush to the window
	SetClassLong((HWND)WND_CLASS, GCLP_HBRBACKGROUND, (LONG)hBkBrush);
}


static int GetContactStatus( HANDLE hContact )
{
	char	*szProto	 =  NULL;
	int		idStatus	 =  ID_STATUS_OFFLINE;

	if ( hContact == NULL )
	{
		assert( !"Contact is NULL" );
		return( 0 );
	}

	szProto	 =  ( char* )CallService( "Proto/GetContactBaseProto", (WPARAM)hContact, 0 );

	if ( NULL != szProto )
	{
		idStatus	 =  DBGetContactSettingWord( hContact, szProto, "Status", ID_STATUS_OFFLINE );
	}


	return( idStatus );
}


static int ClcStatusToPf2( int status )
{
	switch(status) {
		case ID_STATUS_ONLINE: return PF2_ONLINE;
		case ID_STATUS_AWAY: return PF2_SHORTAWAY;
		case ID_STATUS_DND: return PF2_HEAVYDND;
		case ID_STATUS_NA: return PF2_LONGAWAY;
		case ID_STATUS_OCCUPIED: return PF2_LIGHTDND;
		case ID_STATUS_FREECHAT: return PF2_FREECHAT;
		case ID_STATUS_INVISIBLE: return PF2_INVISIBLE;
		case ID_STATUS_ONTHEPHONE: return PF2_ONTHEPHONE;
		case ID_STATUS_OUTTOLUNCH: return PF2_OUTTOLUNCH;
		case ID_STATUS_OFFLINE: return MODEF_OFFLINE;
	}
	return 0;
}

BOOL IsStatusVisible( int status )
{
	return ( 0 == ( dwOfflineModes & ClcStatusToPf2( status )) );
}



void RegHotkey( HANDLE hContact, HWND hwnd )
{
	char szBuf[ MAX_PATH ] =  {0};
/*	char szPath[ MAX_PATH ] =  {0};

	GetModuleFileName( NULL, szPath, MAX_PATH );
	PathRemoveFileSpec( szPath );
	strcat( szPath, "\\Thumbs.ini" );
	GetPrivateProfileString( "Hotkeys", szName, "", szBuf, MAX_PATH - 1, szPath );*/
	DBVARIANT dbv;
	if (DBGetContactSettingString ( hContact, sModule, "Hotkey", &dbv)) return;
	strncpy(szBuf, dbv.pszVal, MAX_PATH - 1);
	DBFreeVariant( &dbv );

	if ( 0 != strlen( szBuf ))
	{
		UINT nModifiers			 =  0;
		char chKey				 =  0;
		char szMod[ 2 ][ 20 ]	 =  {0};
		char szKey[ 20 ]		 =  {0};
		int i = 0;

		sscanf( szBuf, "%[^'+']+%[^'+']+%[^'+']", szMod[ 0 ], szMod[ 1 ], szKey );

		for (  i = 0; i < 2; ++i )
		{
			if ( 0 == strncmp( szMod[ i ], "ALT", 19 ))
			{
				nModifiers = nModifiers | MOD_ALT;
			}
			else if ( 0 == strncmp( szMod[ i ], "CTRL", 19 ))
			{
				nModifiers = nModifiers | MOD_CONTROL;
			}
			else if ( 0 == strncmp( szMod[ i ], "SHIFT", 19 ))
			{
				nModifiers = nModifiers | MOD_SHIFT;
			}
		}

		chKey = szKey[ 0 ];

		RegisterHotKey( hwnd, (int)hwnd, nModifiers, VkKeyScan( chKey ));
	}
}



///////////////////////////////////////////////////////
// Contact sttings

void SaveContactsPos()
{
	RECT					rc;

	for (int i = 0; i < thumbList.getCount(); ++i)
	{
		SetLastError( 0 );

		thumbList[i]->GetThumbRect( &rc );

		if ( 0 == GetLastError())
			DBWriteContactSettingDword( thumbList[i]->hContact, sModule, "ThumbsPos", DB_POS_MAKE_XY(rc.left, rc.top));
	}
}


static void LoadContacts()
{
	HANDLE hContact = db_find_first() ;
	while( hContact != NULL ) {
		LoadContact( hContact );
		hContact = db_find_next(hContact);
	}
}




static void LoadMenus()
{
	// Remove thumb menu item
	hRemoveThumb = CreateServiceFunction( sModule "/RemoveThumb", OnContactMenu_Remove );

	CLISTMENUITEM mi = { sizeof(mi) };
	mi.position = 0xFFFFF;
	mi.flags = CMIF_TCHAR;
	mi.hIcon = LoadIcon( hInst, MAKEINTRESOURCE( IDI_HIDE ));
	mi.ptszName = LPGENT("Remove thumb");
	mi.pszService = sModule "/RemoveThumb";
	hMenuItemRemove = Menu_AddContactMenuItem(&mi);

	// Hide all thumbs main menu item
	hMainHideAllThumbs = CreateServiceFunction( sModule "/MainHideAllThumbs", OnMainMenu_HideAll );
	ZeroMemory( &mi, sizeof( mi ));

	mi.cbSize = sizeof( mi );
	mi.position = 0xFFFFF;
	mi.flags = CMIF_TCHAR;
	mi.hIcon = LoadIcon( hInst, MAKEINTRESOURCE( fcOpt.bHideAll ? IDI_SHOW : IDI_HIDE ));
	mi.ptszName = fcOpt.bHideAll ? LPGENT("Show all thumbs") : LPGENT("Hide all thumbs");
	mi.pszService = sModule "/MainHideAllThumbs";
	Menu_AddMainMenuItem(&mi);

	// Register hotkeys
	HOTKEYDESC hkd = {0};
	hkd.cbSize = sizeof(hkd);
	hkd.pszSection = "Floating Contacts";

	hkd.pszName = sModule "/MainHideAllThumbs";
	hkd.pszDescription = LPGEN("Show/Hide all thumbs");
	hkd.pszService = sModule "/MainHideAllThumbs";
	Hotkey_Register(&hkd);

	hHideWhenCListShow = CreateServiceFunction( sModule "/HideWhenCListShow", OnHotKey_HideWhenCListShow );
	hkd.pszName = sModule "/HideWhenCListShow";
	hkd.pszDescription = LPGEN("Hide when contact list is shown");
	hkd.pszService = sModule "/HideWhenCListShow";
	Hotkey_Register(&hkd);
}


static void LoadContact( HANDLE hContact )
{
	DWORD	dwPos		 =  (DWORD)-1;
	TCHAR	*ptName		 =  NULL;
	ThumbInfo	*pThumb	 =  thumbList.FindThumbByContact( hContact );
	int		nX, nY;


	if ( hContact == NULL ) return;

	dwPos = DBGetContactSettingDword( hContact, sModule, "ThumbsPos", (DWORD)-1 );

	if ( dwPos != -1 )
	{
		ptName = (TCHAR*)CallService( MS_CLIST_GETCONTACTDISPLAYNAME, ( WPARAM )hContact, (LPARAM)GCDNF_TCHAR );

		if ( ptName != NULL )
		{
			nX = DB_POS_GETX( dwPos );
			nY = DB_POS_GETY( dwPos );

			CreateThumbWnd( ptName, hContact, nX, nY );
			pThumb->PositionThumb( (short)nX, (short)nY );
		}
	}
}


BOOL HideOnFullScreen()
{
	BOOL bFullscreen = FALSE;
	HWND hWnd = 0;

	if ( fcOpt.bHideWhenFullscreen )
	{
		int w = GetSystemMetrics(SM_CXSCREEN);
		int h = GetSystemMetrics(SM_CYSCREEN);

		hWnd = GetForegroundWindow();

		while (GetWindowLongPtr(hWnd, GWL_EXSTYLE) & WS_EX_TOPMOST)
		{
			RECT WindowRect;
			GetWindowRect(hWnd, &WindowRect);

			if ( (w == (WindowRect.right - WindowRect.left)) &&
				(h == (WindowRect.bottom - WindowRect.top)))
			{
				bFullscreen = TRUE;
				break;
			}

			hWnd = GetNextWindow( hWnd, GW_HWNDNEXT );
		}
	}

	return bFullscreen && fcOpt.bHideWhenFullscreen;
}


static INT_PTR OnContactMenu_Remove( WPARAM wParam, LPARAM lParam )
{
	HANDLE hContact = ( HANDLE )wParam;
	ThumbInfo *pThumb = thumbList.FindThumbByContact( hContact );

	if (pThumb) {
		pThumb->DeleteContactPos();
		thumbList.RemoveThumb(pThumb);
	}

	DestroyMenu( hContactMenu );
	return 0;
}

static INT_PTR OnHotKey_HideWhenCListShow( WPARAM wParam, LPARAM lParam )
{
	fcOpt.bHideWhenCListShow = !fcOpt.bHideWhenCListShow;
	DBWriteContactSettingByte(NULL, sModule, "HideWhenCListShow", (BYTE)fcOpt.bHideWhenCListShow);
	OnStatusChanged();
	return 0;
}


static INT_PTR OnMainMenu_HideAll( WPARAM wParam, LPARAM lParam )
{
	fcOpt.bHideAll = !fcOpt.bHideAll;
	DBWriteContactSettingByte(NULL, sModule, "HideAll", (BYTE)fcOpt.bHideAll);
	OnStatusChanged();

	CLISTMENUITEM clmi = { sizeof(clmi) };
	clmi.flags = CMIM_NAME | CMIM_ICON|CMIF_TCHAR;
	clmi.hIcon = LoadIcon( hInst, MAKEINTRESOURCE( fcOpt.bHideAll ? IDI_SHOW : IDI_HIDE ));
	clmi.ptszName = fcOpt.bHideAll ? LPGENT("Show all thumbs") : LPGENT("Hide all thumbs");
	CallService( MS_CLIST_MODIFYMENUITEM, ( WPARAM )hMainMenuItemHideAll, ( LPARAM )&clmi );
	return 0;
}

static VOID CALLBACK ToTopTimerProc ( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	for (int i = 0; i < thumbList.getCount(); ++i)
	{
		SetWindowPos(thumbList[i]->hwnd,
					HWND_TOPMOST,
					0,
					0,
					0,
					0,
					SWP_NOSIZE | SWP_NOMOVE | /*SWP_NOZORDER |*/ SWP_NOACTIVATE);
	}
}

void ShowThumbsOnHideCList()
{
	if (!fcOpt.bHideWhenCListShow || fcOpt.bHideAll || HideOnFullScreen())
		return;

	for (int i = 0; i < thumbList.getCount(); ++i)
		if ( !fcOpt.bHideOffline || IsStatusVisible( GetContactStatus(thumbList[i]->hContact)))
			ShowWindow( thumbList[i]->hwnd, SW_SHOWNA );
}


void HideThumbsOnShowCList()
{
	if (!fcOpt.bHideWhenCListShow || fcOpt.bHideAll || HideOnFullScreen())return;
	for (int i = 0; i < thumbList.getCount(); ++i)
		ShowWindow( thumbList[i]->hwnd, SW_HIDE );
}

static LRESULT __stdcall newMirandaWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	if ( uMsg == WM_WINDOWPOSCHANGED) {
		WINDOWPOS *wp = (WINDOWPOS *)lParam;
		if (wp->flags&SWP_HIDEWINDOW) {
			bIsCListShow = FALSE;
			ShowThumbsOnHideCList();
		}
		else if (wp->flags&SWP_SHOWWINDOW){
			bIsCListShow = TRUE;
			HideThumbsOnShowCList();
		}
		else if (!(wp->flags&SWP_NOMOVE)) {
			BYTE method = DBGetContactSettingByte(NULL, "ModernData", "HideBehind", 0);
			if (method) {
				WORD wBehindEdgeBorderSize = DBGetContactSettingWord(NULL, "ModernData", "HideBehindBorderSize", 0);
				RECT rc = {wp->x, wp->y, wp->x+wp->cx, wp->y+wp->cy};
				RECT rcScreen = {wBehindEdgeBorderSize*(2-method), 0, GetSystemMetrics(SM_CXSCREEN)-wBehindEdgeBorderSize*(method-1), GetSystemMetrics(SM_CYSCREEN)};
				RECT rcOverlap;
				BOOL isIntersect;

				isIntersect = IntersectRect( &rcOverlap, &rc, &rcScreen );
				if ( !isIntersect && bIsCListShow ) {
					bIsCListShow = FALSE;
					ShowThumbsOnHideCList();
				}
				else if ( isIntersect && !bIsCListShow ) {
					bIsCListShow = TRUE;
					HideThumbsOnShowCList();
				}
			}
		}
	}
	return( CallWindowProc(oldMirandaWndProc, hwnd, uMsg, wParam, lParam));
}