summaryrefslogtreecommitdiff
path: root/plugins/Clist_mw/src/CLUIFrames/statusbar.cpp
blob: c8c62c1bd40175e54a1c6853b1842b2fa68a9b73 (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
#include "../commonheaders.h"

extern HINSTANCE g_hInst;
HANDLE hStatusBarShowToolTipEvent,hStatusBarHideToolTipEvent;
boolean canloadstatusbar = FALSE;
HWND helperhwnd = 0;
HANDLE hFrameHelperStatusBar;

void CluiProtocolStatusChanged(int parStatus, const char* szProto);

INT_PTR GetConnectingIconService (WPARAM wParam,LPARAM lParam);

int RecreateStatusBar(HWND);
int UseOwnerDrawStatusBar;

#define TM_STATUSBAR 23435234
#define TM_STATUSBARHIDE 23435235
boolean tooltipshoing;
WNDPROC OldWindowProc = NULL;


POINT lastpnt;
RECT OldRc = {0};
static	HBITMAP hBmpBackground;
static int backgroundBmpUse;
static COLORREF bkColour;
extern BYTE showOpts;
int extraspace;

int OnStatusBarBackgroundChange()
{
	showOpts = db_get_b(NULL,"CLUI","SBarShow",1);		
	bkColour = DBGetContactSettingDword(NULL,"StatusBar","BkColour",CLCDEFAULT_BKCOLOUR);
	if (hBmpBackground)
		DeleteObject(hBmpBackground), hBmpBackground = NULL;

	if ( db_get_b(NULL,"StatusBar","UseBitmap",CLCDEFAULT_USEBITMAP)) {
		DBVARIANT dbv;
		if ( !DBGetContactSettingTString(NULL, "StatusBar", "BkBitmap", &dbv)) {
			hBmpBackground = (HBITMAP)CallService(MS_UTILS_LOADBITMAPT, 0, (LPARAM)dbv.ptszVal);
			db_free(&dbv);
		}
	}

	backgroundBmpUse = DBGetContactSettingWord(NULL,"StatusBar","BkBmpUse",CLCDEFAULT_BKBMPUSE);
	extraspace = DBGetContactSettingDword(NULL,"StatusBar","BkExtraSpace",0);

	RecreateStatusBar(pcli->hwndContactList);
	if (pcli->hwndStatus) InvalidateRect(pcli->hwndStatus,NULL,TRUE);
	return 0;
}


void DrawDataForStatusBar(LPDRAWITEMSTRUCT dis)
{
	ProtocolData *PD = (ProtocolData *)dis->itemData;
	SIZE textSize;
	boolean NeedDestroy = FALSE;

	if (PD == NULL || dis->hDC == NULL)
		return;

	//clip it
	HRGN hrgn = CreateRectRgn(dis->rcItem.left, dis->rcItem.top, dis->rcItem.right, dis->rcItem.bottom); 
	SelectClipRgn(dis->hDC, hrgn);

	char *szProto = PD->RealName;
#ifdef _DEBUG
	{
		//char buf[512];
		//sprintf(buf,"proto: %s draw at pos: %d\r\n",szProto,dis->rcItem.left);
		//OutputDebugStringA(buf);
	}
#endif

	int status = CallProtoService(szProto,PS_GETSTATUS,0,0);
	SetBkMode(dis->hDC,TRANSPARENT);
	int x = dis->rcItem.left+extraspace;

	if (showOpts & 1) {
		HICON hIcon;
		if ( db_get_b(NULL, "CLUI", "UseConnectingIcon", 1) == 1 && status < ID_STATUS_OFFLINE) {
			hIcon = (HICON)GetConnectingIconService((WPARAM)szProto, 0);
			if (hIcon)
				NeedDestroy = TRUE;
			else
				hIcon = LoadSkinnedProtoIcon(szProto,status);
		}
		else hIcon = LoadSkinnedProtoIcon(szProto,status);

		DrawIconEx(dis->hDC,x,(dis->rcItem.top+dis->rcItem.bottom-GetSystemMetrics(SM_CYSMICON))>>1,hIcon,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),0,NULL,DI_NORMAL);
		if (NeedDestroy) DestroyIcon(hIcon);
		x += GetSystemMetrics(SM_CXSMICON)+2;
	}
	else x += 2;

	if (showOpts & 2) {
		PROTOACCOUNT* pa = ProtoGetAccount(szProto);
		TCHAR szName[64];
		mir_sntprintf(szName, SIZEOF(szName), _T("%s%s"), pa->tszAccountName, showOpts&4 ? _T(" ") : _T(""));
		GetTextExtentPoint32(dis->hDC, szName, (int)_tcslen(szName), &textSize);

		TextOut(dis->hDC,x,(dis->rcItem.top+dis->rcItem.bottom-textSize.cy)>>1,szName,lstrlen(szName));
		x += textSize.cx;
	}

	if (showOpts & 4) {
		TCHAR *szStatus = pcli->pfnGetStatusModeDescription(status, 0);
		if (!szStatus)
			szStatus = _T("");
		GetTextExtentPoint32(dis->hDC,szStatus,lstrlen(szStatus),&textSize);
		TextOut(dis->hDC,x,(dis->rcItem.top+dis->rcItem.bottom-textSize.cy)>>1,szStatus,lstrlen(szStatus));
	}
	SelectClipRgn(dis->hDC, NULL);
	DeleteObject(hrgn);
}

void DrawBackGround(HWND hwnd,HDC mhdc)
{
	HDC hdcMem,hdc;
	RECT clRect,*rcPaint;

	int yScroll = 0;
	int y;
	PAINTSTRUCT paintst = {0};
	HBITMAP hBmpOsb,holdbmp;
	DWORD style = GetWindowLongPtr(hwnd,GWL_STYLE);
	int grey = 0;
	HFONT oFont;
	HBRUSH hBrushAlternateGrey = NULL;

	HFONT hFont = (HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
	if (mhdc) {
		hdc = mhdc;
		rcPaint = NULL;
	}
	else {
		hdc = BeginPaint(hwnd,&paintst);
		rcPaint = &(paintst.rcPaint);
	}

	GetClientRect(hwnd,&clRect);
	if (rcPaint == NULL) rcPaint = &clRect;
	if (rcPaint->right-rcPaint->left == 0||rcPaint->top-rcPaint->bottom == 0) rcPaint = &clRect;
	y = -yScroll;
	hdcMem = CreateCompatibleDC(hdc);
	hBmpOsb = CreateBitmap(clRect.right,clRect.bottom,1,GetDeviceCaps(hdc,BITSPIXEL),NULL);
	holdbmp = (HBITMAP)SelectObject(hdcMem,hBmpOsb);
	oFont = (HFONT)SelectObject(hdcMem,hFont);
	SetBkMode(hdcMem,TRANSPARENT);
	{
		HBRUSH hBrush = CreateSolidBrush(bkColour);
		HBRUSH hoBrush = (HBRUSH)SelectObject(hdcMem,hBrush);
		FillRect(hdcMem,rcPaint,hBrush);
		SelectObject(hdcMem,hoBrush);
		DeleteObject(hBrush);
		if (hBmpBackground) {
			BITMAP bmp;
			int x,y;
			int maxx,maxy;
			int destw,desth;

			GetObject(hBmpBackground,sizeof(bmp),&bmp);
			HDC hdcBmp = CreateCompatibleDC(hdcMem);
			HDC holdbackbmp = (HDC)SelectObject(hdcBmp,hBmpBackground);
			y = backgroundBmpUse&CLBF_SCROLL?-yScroll:0;
			maxx = backgroundBmpUse&CLBF_TILEH?clRect.right:1;
			maxy = backgroundBmpUse&CLBF_TILEV?maxy = rcPaint->bottom:y+1;
			switch(backgroundBmpUse&CLBM_TYPE) {
			case CLB_STRETCH:
				if (backgroundBmpUse&CLBF_PROPORTIONAL) {
					if (clRect.right*bmp.bmHeight<clRect.bottom*bmp.bmWidth) {
						desth = clRect.bottom;
						destw = desth*bmp.bmWidth/bmp.bmHeight;
					}
					else {
						destw = clRect.right;
						desth = destw*bmp.bmHeight/bmp.bmWidth;
					}
				}
				else {
					destw = clRect.right;
					desth = clRect.bottom;
				}
				break;

			case CLB_STRETCHH:
				if (backgroundBmpUse&CLBF_PROPORTIONAL) {
					destw = clRect.right;
					desth = destw*bmp.bmHeight/bmp.bmWidth;
				}
				else {
					destw = clRect.right;
					desth = bmp.bmHeight;
				}
				break;

			case CLB_STRETCHV:
				if (backgroundBmpUse&CLBF_PROPORTIONAL) {
					desth = clRect.bottom;
					destw = desth*bmp.bmWidth/bmp.bmHeight;
				}
				else {
					destw = bmp.bmWidth;
					desth = clRect.bottom;
				}
				break;

			default:    //clb_topleft
				destw = bmp.bmWidth;
				desth = bmp.bmHeight;
				break;
			}
			desth = clRect.bottom -clRect.top;
			for (;y<maxy;y += desth) {
				if (y<rcPaint->top-desth) continue;
				for (x = 0;x<maxx;x += destw)
					StretchBlt(hdcMem,x,y,destw,desth,hdcBmp,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
			}
			SelectObject(hdcBmp,holdbackbmp);
			DeleteDC(hdcBmp);
		}
	}

	//call to draw icons
	{
		DRAWITEMSTRUCT ds = { 0 };
		ds.hwndItem = hwnd;
		ds.hDC = hdcMem;

		int startoffset = DBGetContactSettingDword(NULL,"StatusBar","FirstIconOffset",0);
		int extraspace = DBGetContactSettingDword(NULL,"StatusBar","BkExtraSpace",0);
		int nParts = SendMessage(hwnd,SB_GETPARTS,0,0);

		RECT rc = { 0 }, clrc;
		GetClientRect(hwnd,&clrc);
		clrc.right -= clrc.left;
		clrc.right -= startoffset;
		int sectwidth = clrc.right/nParts;

		for (int nPanel = 0; nPanel < nParts; nPanel++) {
			ProtocolData *PD = (ProtocolData *)SendMessage(pcli->hwndStatus,SB_GETTEXT,(WPARAM)nPanel,0);
			if (PD == NULL)
				continue;

			SendMessage(hwnd,SB_GETRECT,(WPARAM)nPanel,(LPARAM)&rc);

			rc.top = 0;
			rc.left = nPanel*sectwidth+startoffset;
			rc.right = rc.left+sectwidth-1;
			ds.rcItem = rc;
			ds.itemData = (ULONG_PTR)PD;
			ds.itemID = nPanel;

			DrawDataForStatusBar(&ds);
	}	}

	BitBlt(hdc,rcPaint->left,rcPaint->top,rcPaint->right-rcPaint->left,rcPaint->bottom-rcPaint->top,hdcMem,rcPaint->left,rcPaint->top,SRCCOPY);

	SelectObject(hdcMem,holdbmp);
	SelectObject(hdcMem,oFont);
	DeleteObject(hBmpOsb);
	DeleteDC(hdcMem);
	paintst.fErase = FALSE;
	if (!mhdc)
		EndPaint(hwnd,&paintst);	
}

LRESULT CALLBACK StatusBarOwnerDrawProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (UseOwnerDrawStatusBar) {
		switch(uMsg) {
		case WM_ERASEBKGND:
			return 0;

		case WM_PRINT:
			DrawBackGround(hwnd,(HDC)wParam);
			return 0;

		case WM_PAINT:
			DrawBackGround(hwnd,0);
			return 0;
		}
	}
	return CallWindowProc(OldWindowProc, hwnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK StatusHelperProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg) {
	case WM_CREATE:
		tooltipshoing = FALSE;
		return FALSE;

	case WM_GETMINMAXINFO:
		if (pcli->hwndStatus) {
			RECT rct;
			GetWindowRect(pcli->hwndStatus,&rct);
			memset((LPMINMAXINFO)lParam, 0, sizeof(MINMAXINFO));
			((LPMINMAXINFO)lParam)->ptMinTrackSize.x = 5;
			((LPMINMAXINFO)lParam)->ptMinTrackSize.y = rct.bottom-rct.top;
			((LPMINMAXINFO)lParam)->ptMaxTrackSize.x = 1600;
			((LPMINMAXINFO)lParam)->ptMaxTrackSize.y = rct.bottom-rct.top;
			return 0;
		}
		break;

	case WM_NCHITTEST:
	case WM_SHOWWINDOW:
		if (hFrameHelperStatusBar) {
			int res = CallService(MS_CLIST_FRAMES_GETFRAMEOPTIONS, MAKEWPARAM(FO_FLAGS,hFrameHelperStatusBar),0);
			DBWriteContactSettingByte(0, "CLUI", "ShowSBar", (res & F_VISIBLE) != 0);
		}

		if (tooltipshoing){
			NotifyEventHooks(hStatusBarHideToolTipEvent, 0, 0);
			tooltipshoing = FALSE;
		}
		return 0;

	case WM_TIMER:
		if (wParam == TM_STATUSBARHIDE) {
			KillTimer(hwnd,TM_STATUSBARHIDE);

			if (tooltipshoing){
				NotifyEventHooks(hStatusBarHideToolTipEvent,0,0);
				tooltipshoing = FALSE;
			}
		}

		if (wParam == TM_STATUSBAR) {
			KillTimer(hwnd,TM_STATUSBAR);

			POINT pt;
			GetCursorPos(&pt);
			if (pt.x == lastpnt.x&&pt.y == lastpnt.y) {
				RECT rc;
				ScreenToClient(pcli->hwndStatus, &pt);
				int nParts = SendMessage(pcli->hwndStatus,SB_GETPARTS,0,0);
				for (int i = 0;i<nParts;i++) {
					SendMessage(pcli->hwndStatus,SB_GETRECT,i,(LPARAM)&rc);
					if (PtInRect(&rc,pt)) {							
						ProtocolData *PD = (ProtocolData *)SendMessage(pcli->hwndStatus,SB_GETTEXT,i,0);
						if (PD == NULL)
							return 0;

						NotifyEventHooks(hStatusBarShowToolTipEvent,(WPARAM)PD->RealName,0);
						SetTimer(hwnd,TM_STATUSBARHIDE,DBGetContactSettingWord(NULL,"CLUIFrames","HideToolTipTime",5000),0);
						tooltipshoing = TRUE;
						break;
					}
				}
			}
		}
		return 0;
	
	case WM_SETCURSOR:
		{		
			POINT pt;
			GetCursorPos(&pt);
			if (pt.x == lastpnt.x&&pt.y == lastpnt.y)
				return 0;

			lastpnt = pt;
			if (tooltipshoing){
				KillTimer(hwnd,TM_STATUSBARHIDE);				
				NotifyEventHooks(hStatusBarHideToolTipEvent,0,0);
				tooltipshoing = FALSE;		
			}
			KillTimer(hwnd,TM_STATUSBAR);
			SetTimer(hwnd,TM_STATUSBAR,DBGetContactSettingWord(NULL,"CLC","InfoTipHoverTime",750),0);
		}			
		return 0;

	case WM_NOTIFY:
		if (lParam == 0)
			return 0;

		if (((LPNMHDR)lParam)->hwndFrom == pcli->hwndStatus)
			if (((LPNMHDR)lParam)->code == WM_NCHITTEST)
				LPNMMOUSE lpnmmouse = (LPNMMOUSE) lParam;

	case WM_CONTEXTMENU:
		KillTimer(hwnd,TM_STATUSBARHIDE);

		if (tooltipshoing)
			NotifyEventHooks(hStatusBarHideToolTipEvent,0,0);

		tooltipshoing = FALSE;

	case WM_MEASUREITEM:
	case WM_DRAWITEM:
		//parent do all work for us
		return(SendMessage(pcli->hwndContactList,msg,wParam,lParam));

	case WM_MOVE:
		PostMessage(pcli->hwndStatus,WM_MOVE,wParam,lParam);

	case WM_SIZE:
		{
			RECT rc;
			int b;
			if (pcli->hwndStatus != 0) {
				GetClientRect(hwnd,&rc);

				b = LOWORD(lParam);
				if (b != 0&&(rc.right-rc.left) != (OldRc.right-OldRc.left)) {
					OldRc = rc;
					if (canloadstatusbar) 	
						if ( db_get_b(NULL,"CLUI","UseOwnerDrawStatusBar",0)||db_get_b(NULL,"CLUI","EqualSections",1)) 
							CluiProtocolStatusChanged(0,0);
				}
				if (msg == WM_SIZE) PostMessage(pcli->hwndStatus,WM_SIZE,wParam,lParam);
				if (pcli->hwndStatus != 0) InvalidateRect(pcli->hwndStatus,NULL,TRUE);
				return 0;
			}
		}

	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);

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

HWND CreateStatusHelper(HWND parent)
{
	WNDCLASS wndclass = {0};
	TCHAR pluginname[] = _T("Statushelper");

	if (GetClassInfo(g_hInst,pluginname,&wndclass) == 0 ) {
		wndclass.style         = 0;
		wndclass.lpfnWndProc   = StatusHelperProc;
		wndclass.cbClsExtra    = 0;
		wndclass.cbWndExtra    = 0;
		wndclass.hInstance     = g_hInst;
		wndclass.hIcon         = NULL;
		wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
		wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
		wndclass.lpszMenuName  = NULL;
		wndclass.lpszClassName = pluginname;
		RegisterClass(&wndclass);
	}

	return(CreateWindow(pluginname,pluginname,
		/*WS_THICKFRAME|*/WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,
		0,0,0,0,parent,NULL,g_hInst,NULL));
}

HANDLE CreateStatusBarFrame()
{
	RECT rc;
	GetWindowRect(helperhwnd,&rc);
	int h = rc.bottom-rc.top;

	CLISTFrame Frame = { 0 };
	Frame.cbSize = sizeof(CLISTFrame);
	Frame.hWnd = helperhwnd;
	Frame.align = alBottom;
	Frame.hIcon = LoadSkinnedIcon (SKINICON_OTHER_MIRANDA);
	Frame.Flags = ( db_get_b(NULL,"CLUI","ShowSBar",1)?F_VISIBLE:0)|F_LOCKED|F_NOBORDER|F_TCHAR;
	Frame.height = (h == 0) ? 20 : h;
	Frame.tname = _T("Status");
	Frame.TBtname = TranslateT("Status Bar");
	hFrameHelperStatusBar = (HANDLE)CallService(MS_CLIST_FRAMES_ADDFRAME,(WPARAM)&Frame,0);

	return hFrameHelperStatusBar;
}

int RecreateStatusBar(HWND parent)
{
	if (pcli->hwndStatus) {
		FreeProtocolData();
		DestroyWindow(pcli->hwndStatus);
	}
	pcli->hwndStatus = 0;
	if (hFrameHelperStatusBar) CallService(MS_CLIST_FRAMES_REMOVEFRAME,(WPARAM)hFrameHelperStatusBar,0);

	helperhwnd = CreateStatusHelper(parent);
	UseOwnerDrawStatusBar = db_get_b(NULL,"CLUI","UseOwnerDrawStatusBar",0);

	//create the status wnd
	pcli->hwndStatus = CreateStatusWindow(
		( db_get_b(0,"CLUI","SBarUseSizeGrip",TRUE) && (!UseOwnerDrawStatusBar)?SBARS_SIZEGRIP:0)|
		WS_CHILD | ( db_get_b(NULL,"CLUI","ShowSBar",1)?WS_VISIBLE:0), _T(""), helperhwnd, 0);

	OldWindowProc = (WNDPROC)GetWindowLongPtr(pcli->hwndStatus,GWLP_WNDPROC);
	SetWindowLongPtr(pcli->hwndStatus,GWLP_WNDPROC,(LONG_PTR)&StatusBarOwnerDrawProc);
	CreateStatusBarFrame();

	SetWindowPos(helperhwnd,NULL,1,1,1,1,SWP_NOZORDER);
	CluiProtocolStatusChanged(0,0);
	CallService(MS_CLIST_FRAMES_UPDATEFRAME,-1,0);
	return 0;
}

HWND CreateStatusBarhWnd(HWND parent)
{	
	RecreateStatusBar(parent);
	OnStatusBarBackgroundChange();

	hStatusBarShowToolTipEvent = CreateHookableEvent(ME_CLIST_FRAMES_SB_SHOW_TOOLTIP);
	hStatusBarHideToolTipEvent = CreateHookableEvent(ME_CLIST_FRAMES_SB_HIDE_TOOLTIP);
	return pcli->hwndStatus;
}