summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/ctrl_button.cpp
blob: 534bf8edc2e9b5141723eb5fa1f01729778a4bb3 (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
/*
UserinfoEx plugin for Miranda IM

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

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "stdafx.h"

// Used for our own cheap TrackMouseEvent
#define BUTTON_POLLID			 100
#define BUTTON_POLLDELAY		50

typedef struct TMBCtrl{
	HWND		hwnd;
	HANDLE		hThemeButton;
	HANDLE		hThemeToolbar;

	HICON		hIcon;
	HICON		arrow;			// uses down arrow
	HBITMAP		hBitmap;
	HFONT		hFont;			// font

	DWORD		dwStyle;	
	BYTE		bFocus;	
	
	int			stateId;		// button state
	int			defbutton;		// default button
	int			pbState;
	wchar_t		cHot;
} BTNCTRL, *LPBTNCTRL;

// External theme methods and properties
static mir_cs csTips;
static HWND hwndToolTips = nullptr;

/**
 * name:	DestroyTheme
 * desc:	destroys theme data for buttons
 * param:	ctl - BTNCTRL structure with the information about the theme to close
 * return:	nothing
 **/
static void __fastcall DestroyTheme(BTNCTRL *ctl) {
	if (ctl->hThemeButton) {
		CloseThemeData(ctl->hThemeButton);
		ctl->hThemeButton = nullptr;
	}
	if (ctl->hThemeToolbar) {
		CloseThemeData(ctl->hThemeToolbar);
		ctl->hThemeToolbar = nullptr;
	}
}

/**
 * name:	LoadTheme
 * desc:	load theme data for buttons if supported by os
 * param:	ctl - BTNCTRL structure with the information about the theme to load
 * return:	nothing
 **/
static void __fastcall LoadTheme(BTNCTRL *ctl) {
	DestroyTheme(ctl);
	ctl->hThemeButton = OpenThemeData(ctl->hwnd,L"BUTTON");
	ctl->hThemeToolbar = OpenThemeData(ctl->hwnd,L"TOOLBAR");
}

/**
 * name:	TBStateConvert2Flat
 * desc:	convert button stateIDs
 * param:	state - state id for the normal theme button
 * return:	stateID for the flat theme button
 **/
static int __fastcall TBStateConvert2Flat(int state) {
	switch (state) {
		case PBS_NORMAL:		return TS_NORMAL;
		case PBS_HOT:			 return TS_HOT;
		case PBS_PRESSED:	 return TS_PRESSED;
		case PBS_DISABLED:	return TS_DISABLED;
		case PBS_DEFAULTED: return TS_NORMAL;
	}
	return TS_NORMAL;
}

/**
 * name:	PaintIcon
 * desc:	Draws the Icon of the button
 * param:	ctl			- BTNCTRL structure for the button
 *			hdcMem		- device context to draw to
 *			ccText		- character count of the text of the button
 *			rcClient	- rectangle of the whole button
 *			rcText		- rectangle of the text to draw later on
 * return:	nothing
 **/
static void __fastcall PaintIcon(BTNCTRL *ctl, HDC hdcMem, LPWORD ccText, LPRECT rcClient, LPRECT rcText)
{
	RECT rcImage;

	// draw icon on the left of the button
	if (ctl->hIcon) {
		rcImage.right = GetSystemMetrics(SM_CXSMICON);
		rcImage.bottom = GetSystemMetrics(SM_CYSMICON);
		rcImage.left = (rcClient->right - rcClient->left) / 2 - ((rcImage.right + rcText->right + (*ccText > 0 ? 4 : 0) + (ctl->arrow ? rcImage.right : 0)) / 2);
		rcImage.top = (rcClient->bottom - rcClient->top - rcImage.bottom) / 2;
		rcImage.right += rcImage.left;
		rcImage.bottom += rcImage.top;
		
		OffsetRect(rcText, rcImage.right + 4, 0);
		if (ctl->stateId == PBS_PRESSED)	OffsetRect(&rcImage, 1, 1);

		DrawState(hdcMem, nullptr, nullptr, (LPARAM)ctl->hIcon, 0, 
			rcImage.left, rcImage.top, 
			rcImage.right - rcImage.left, rcImage.bottom - rcImage.top,
			IsWindowEnabled(ctl->hwnd) ? DST_ICON | DSS_NORMAL : DST_ICON | DSS_DISABLED);
	}

	// draw arrow on the right of the button
	if (ctl->arrow) {
		rcImage.right = GetSystemMetrics(SM_CXSMICON);
		rcImage.left = (*ccText > 0 || ctl->hIcon) 
					 ? rcClient->right - GetSystemMetrics(SM_CXSMICON) 
					 : (rcClient->right - rcClient->left - rcImage.right) / 2;
		rcImage.right += rcImage.left;
		rcImage.bottom = GetSystemMetrics(SM_CYSMICON);
		rcImage.top = (rcClient->bottom - rcClient->top - rcImage.bottom) / 2;
		if (ctl->stateId == PBS_PRESSED)	OffsetRect(&rcImage, 1, 1);

		DrawState(hdcMem, nullptr, nullptr, (LPARAM)ctl->arrow, 0, 
			rcImage.left, rcImage.top, 
			rcImage.right - rcImage.left, rcImage.bottom - rcImage.top,
			IsWindowEnabled(ctl->hwnd) ? DST_ICON | DSS_NORMAL : DST_ICON | DSS_DISABLED);
	}
}

/**
 * name:	PaintThemeButton
 * desc:	Draws the themed button
 * param:	ctl			- BTNCTRL structure for the button
 *			hdcMem		- device context to draw to
 *			rcClient	- rectangle of the whole button
 * return:	nothing
 **/
static void __fastcall PaintThemeButton(BTNCTRL *ctl, HDC hdcMem, LPRECT rcClient)
{
	RECT rcText = { 0, 0, 0, 0 };
	WCHAR wszText[MAX_PATH] = { 0 };
	WORD ccText;

	// Draw the flat button
	if ((ctl->dwStyle & MBS_FLAT) && ctl->hThemeToolbar) {
		int state = IsWindowEnabled(ctl->hwnd)
				? (ctl->stateId == PBS_NORMAL && ctl->defbutton 
					? PBS_DEFAULTED
					: ctl->stateId)
				: PBS_DISABLED;
		if (IsThemeBackgroundPartiallyTransparent(ctl->hThemeToolbar, TP_BUTTON, TBStateConvert2Flat(state))) {
			if (SUCCEEDED(DrawThemeParentBackground(ctl->hwnd, hdcMem, rcClient)))
				DrawThemeParentBackground(GetParent(ctl->hwnd), hdcMem, rcClient);
		}
		DrawThemeBackground(ctl->hThemeToolbar, hdcMem, TP_BUTTON, TBStateConvert2Flat(state), rcClient, rcClient);
	}
	else {
		// draw themed button background
		if (ctl->hThemeButton) {
			int state = IsWindowEnabled(ctl->hwnd)
				? (ctl->stateId == PBS_NORMAL && ctl->defbutton 
					? PBS_DEFAULTED
					: ctl->stateId)
				: PBS_DISABLED;
			if (IsThemeBackgroundPartiallyTransparent(ctl->hThemeButton, BP_PUSHBUTTON, state)) {
				if (SUCCEEDED(DrawThemeParentBackground(ctl->hwnd, hdcMem, rcClient)))
					DrawThemeParentBackground(GetParent(ctl->hwnd), hdcMem, rcClient);
			}
			DrawThemeBackground(ctl->hThemeButton, hdcMem, BP_PUSHBUTTON, state, rcClient, rcClient);
		}
	}
	
	// calculate text rect
	{
		RECT	sizeText;
		HFONT	hOldFont;

		ccText = GetWindowTextW(ctl->hwnd, wszText, _countof(wszText));

		if (ccText > 0) {
			hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont);
			
			GetThemeTextExtent(
				ctl->hThemeButton,
				hdcMem,
				BP_PUSHBUTTON,
				IsWindowEnabled(ctl->hwnd) ? ctl->stateId : PBS_DISABLED,
				wszText,
				ccText,
				DST_PREFIXTEXT,
				nullptr,
				&sizeText);
			
			if (ctl->cHot) {
				RECT rcHot;
				
				GetThemeTextExtent(ctl->hThemeButton,
					hdcMem,
					BP_PUSHBUTTON,
					IsWindowEnabled(ctl->hwnd) ? ctl->stateId : PBS_DISABLED,
					L"&",
					1,
					DST_PREFIXTEXT,
					nullptr,
					&rcHot);
				
				sizeText.right -= (rcHot.right - rcHot.left);
			}
			SelectObject(hdcMem, hOldFont);

			rcText.left = (ctl->hIcon) ? 0 : (rcClient->right - rcClient->left - (sizeText.right - sizeText.left)) / 2;
			rcText.top = (rcClient->bottom - rcClient->top - (sizeText.bottom - sizeText.top)) / 2;
			rcText.right = rcText.left + (sizeText.right - sizeText.left);
			rcText.bottom = rcText.top + (sizeText.bottom - sizeText.top);
			if (ctl->stateId == PBS_PRESSED) {
				OffsetRect(&rcText, 1, 1);
			}
		}
	}
	PaintIcon(ctl, hdcMem, &ccText, rcClient, &rcText);
	// draw text
	if (ccText > 0 && ctl->hThemeButton) { 
		HFONT hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont);
		DrawThemeText(ctl->hThemeButton, hdcMem, BP_PUSHBUTTON, IsWindowEnabled(ctl->hwnd) ? ctl->stateId : PBS_DISABLED, wszText, ccText, DST_PREFIXTEXT, 0, &rcText);
		SelectObject(hdcMem, hOldFont);
	}
}

/**
 * name:	PaintThemeButton
 * desc:	Draws the none themed button
 * param:	ctl			- BTNCTRL structure for the button
 *			hdcMem		- device context to draw to
 *			rcClient	- rectangle of the whole button
 * return:	nothing
 **/
static void __fastcall PaintButton(BTNCTRL *ctl, HDC hdcMem, LPRECT rcClient)
{
	RECT rcText = { 0, 0, 0, 0 };
	wchar_t szText[MAX_PATH] = { 0 };
	WORD ccText;

	// Draw the flat button
	if (ctl->dwStyle & MBS_FLAT) {
		HBRUSH hbr = nullptr;
		
		if (ctl->stateId == PBS_PRESSED || ctl->stateId == PBS_HOT)
			hbr = GetSysColorBrush(COLOR_3DLIGHT);
		else {
			HDC dc;
			HWND hwndParent;

			hwndParent = GetParent(ctl->hwnd);
			if (dc = GetDC(hwndParent)) {
				hbr = (HBRUSH)SendMessage(hwndParent, WM_CTLCOLORDLG, (WPARAM)dc, (LPARAM)hwndParent);
				ReleaseDC(hwndParent, dc);
			}
		}
		if (hbr) {
			FillRect(hdcMem, rcClient, hbr);
			DeleteObject(hbr);
		}
		if (ctl->stateId == PBS_HOT || ctl->bFocus) {
			if (ctl->pbState) DrawEdge(hdcMem, rcClient, EDGE_ETCHED, BF_RECT|BF_SOFT);
			else DrawEdge(hdcMem, rcClient, BDR_RAISEDOUTER, BF_RECT|BF_SOFT|BF_FLAT);
		}
		else
		if (ctl->stateId == PBS_PRESSED)
			DrawEdge(hdcMem, rcClient, BDR_SUNKENOUTER, BF_RECT|BF_SOFT);
	}
	else {
		UINT uState = DFCS_BUTTONPUSH|((ctl->stateId == PBS_HOT) ? DFCS_HOT : 0)|((ctl->stateId == PBS_PRESSED) ? DFCS_PUSHED : 0);
		if (ctl->defbutton&&ctl->stateId==PBS_NORMAL) uState |= DLGC_DEFPUSHBUTTON;
		DrawFrameControl(hdcMem, rcClient, DFC_BUTTON, uState);
		// Draw focus rectangle if button has focus
		if (ctl->bFocus) {
			RECT focusRect = *rcClient;
			InflateRect(&focusRect, -3, -3);
			DrawFocusRect(hdcMem, &focusRect);
		}
	}
	// calculate text rect
	{
		SIZE	sizeText;
		HFONT	hOldFont;

		ccText = GetWindowText(ctl->hwnd, szText, _countof(szText));

		if (ccText > 0) {
			hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont);
			GetTextExtentPoint32(hdcMem, szText, ccText, &sizeText);
			if (ctl->cHot) {
				SIZE sizeHot;
				
				GetTextExtentPoint32A(hdcMem, "&", 1, &sizeHot);
				sizeText.cx -= sizeHot.cx;
			}
			SelectObject(hdcMem, hOldFont);

			rcText.left = (ctl->hIcon) ? 0 : (rcClient->right - rcClient->left - sizeText.cx) / 2;
			rcText.top = (rcClient->bottom - rcClient->top - sizeText.cy) / 2;
			rcText.right = rcText.left + sizeText.cx;
			rcText.bottom = rcText.top + sizeText.cy;
			if (ctl->stateId == PBS_PRESSED)
				OffsetRect(&rcText, 1, 1);
		}
	}
	PaintIcon(ctl, hdcMem, &ccText, rcClient, &rcText);

	// draw text
	if (ccText > 0) { 
		HFONT hOldFont;

		hOldFont = (HFONT)SelectObject(hdcMem, ctl->hFont);

		SetBkMode(hdcMem, TRANSPARENT);
		SetTextColor(hdcMem, 
			IsWindowEnabled(ctl->hwnd) || !ctl->hThemeButton 
			? ctl->stateId == PBS_HOT
					? GetSysColor(COLOR_HOTLIGHT)
					: GetSysColor(COLOR_BTNTEXT) 
				: GetSysColor(COLOR_GRAYTEXT));

		DrawState(hdcMem, nullptr, nullptr, (LPARAM)szText, 0, 
			rcText.left, rcText.top, rcText.right - rcText.left, rcText.bottom - rcText.top,
			IsWindowEnabled(ctl->hwnd) || ctl->hThemeButton ? DST_PREFIXTEXT | DSS_NORMAL : DST_PREFIXTEXT | DSS_DISABLED);
		SelectObject(hdcMem, hOldFont);
	}
}

/**
 * name:	Button_WndProc
 * desc:	window procedure for the button class
 * param:	hwndBtn		- window handle to the button
 *			uMsg		- message to handle
 *			wParam		- message specific parameter
 *			lParam		- message specific parameter
 * return:	message specific
 **/
static LRESULT CALLBACK Button_WndProc(HWND hwndBtn, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	LPBTNCTRL bct = (LPBTNCTRL)GetWindowLongPtr(hwndBtn, 0);
	
	switch (uMsg) {
	case WM_NCCREATE:
		{
			LPCREATESTRUCT cs = (LPCREATESTRUCT)lParam;

			cs->style |= BS_OWNERDRAW;
			if (!(bct = (LPBTNCTRL)mir_alloc(sizeof(BTNCTRL))))
				return FALSE;
			memset(bct, 0, sizeof(BTNCTRL));
			bct->hwnd = hwndBtn;
			bct->stateId = PBS_NORMAL;
			bct->hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			bct->dwStyle = cs->style;
			if (cs->style & MBS_DOWNARROW)
				bct->arrow = IcoLib_GetIcon(ICO_BTN_DOWNARROW);
			LoadTheme(bct);
			SetWindowLongPtr(hwndBtn, 0, (LONG_PTR)bct);
			if (cs->lpszName) SetWindowText(hwndBtn, cs->lpszName);
			return TRUE;
		}
	case WM_DESTROY:
		if (bct) {
			mir_cslock lck(csTips);
			if (hwndToolTips) {
				TOOLINFO ti;

				memset(&ti, 0, sizeof(ti));
				ti.cbSize = sizeof(ti);
				ti.uFlags = TTF_IDISHWND;
				ti.hwnd = bct->hwnd;
				ti.uId = (UINT_PTR)bct->hwnd;
				if (SendMessage(hwndToolTips, TTM_GETTOOLINFO, 0, (LPARAM)&ti)) {
					SendMessage(hwndToolTips, TTM_DELTOOL, 0, (LPARAM)&ti);
				}
				if (SendMessage(hwndToolTips, TTM_GETTOOLCOUNT, 0, (LPARAM)&ti) == 0) {
					DestroyWindow(hwndToolTips);
					hwndToolTips = nullptr;
				}
			}
			DestroyTheme(bct);
			mir_free(bct);
		}
		SetWindowLongPtr(hwndBtn, 0, 0);
		break;
	case WM_SETTEXT:
		bct->cHot = 0;
		if ((LPTSTR)lParam) {
			LPTSTR tmp = (LPTSTR)lParam;

			while (*tmp) {
				if (*tmp == '&' && *(tmp + 1)) {
					bct->cHot = towlower(*(tmp + 1));
					break;
				}
				tmp++;
			}
			InvalidateRect(bct->hwnd, nullptr, TRUE);
		}
		break;
	case WM_SYSKEYUP:
		if (bct->stateId != PBS_DISABLED && bct->cHot && bct->cHot == towlower((wchar_t)wParam)) {
			if (bct->dwStyle & MBS_PUSHBUTTON) {
				if (bct->pbState) bct->pbState = 0;
				else bct->pbState = 1;
				InvalidateRect(bct->hwnd, nullptr, TRUE);
			}
			else
				SetFocus(hwndBtn);
			SendMessage(GetParent(hwndBtn), WM_COMMAND, MAKELONG(GetDlgCtrlID(hwndBtn), BN_CLICKED), (LPARAM)hwndBtn);
			return 0;
		}
		break;
	case WM_THEMECHANGED: 
		// themed changed, reload theme object
		LoadTheme(bct);
		InvalidateRect(bct->hwnd, nullptr, TRUE); // repaint it
		break;
	case WM_SETFONT: // remember the font so we can use it later
		bct->hFont = (HFONT)wParam; // maybe we should redraw?
		break;
	case WM_NCPAINT:
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdcPaint;
			HDC hdcMem;
			HBITMAP hbmMem;
			HDC hOld;
			RECT rcClient;
			
			if (hdcPaint = BeginPaint(hwndBtn, &ps)) {
				GetClientRect(bct->hwnd, &rcClient);
				hdcMem = CreateCompatibleDC(hdcPaint);
				hbmMem = CreateCompatibleBitmap(hdcPaint, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
				hOld = (HDC)SelectObject(hdcMem, hbmMem);

				// If its a push button, check to see if it should stay pressed
				if ((bct->dwStyle & MBS_PUSHBUTTON) && bct->pbState) bct->stateId = PBS_PRESSED;

				if ((bct->dwStyle & MBS_FLAT) && bct->hThemeToolbar || bct->hThemeButton)
					PaintThemeButton(bct, hdcMem, &rcClient);
				else
					PaintButton(bct, hdcMem, &rcClient);

				BitBlt(hdcPaint, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, hdcMem, 0, 0, SRCCOPY);
				SelectObject(hdcMem, hOld);
				DeleteObject(hbmMem);
				DeleteDC(hdcMem);				
				EndPaint(hwndBtn, &ps);
			}
		}
		return 0;
	case BM_SETIMAGE:
		if (wParam == IMAGE_ICON) {
			bct->hIcon = (HICON)lParam;
			bct->hBitmap = nullptr;
			InvalidateRect(bct->hwnd, nullptr, TRUE);
		}
		else if (wParam == IMAGE_BITMAP) {
			bct->hIcon = nullptr;
			bct->hBitmap = (HBITMAP)lParam;
			InvalidateRect(bct->hwnd, nullptr, TRUE);
		}
		break;
	case BM_SETCHECK:
		if (!(bct->dwStyle & MBS_PUSHBUTTON)) break;
		if (wParam == BST_CHECKED) {
			bct->pbState = 1;
			bct->stateId = PBS_PRESSED;
		}
		else if (wParam == BST_UNCHECKED) {
			bct->pbState = 0;
			bct->stateId = PBS_NORMAL;
		}
		InvalidateRect(bct->hwnd, nullptr, TRUE);
		break;
	case BM_GETCHECK:
		if (bct->dwStyle & MBS_PUSHBUTTON) return bct->pbState ? BST_CHECKED : BST_UNCHECKED;
		return 0;
	case BUTTONSETDEFAULT:
		bct->defbutton = (wParam != 0);
		InvalidateRect(bct->hwnd, nullptr, TRUE);
		break;
	case BUTTONADDTOOLTIP:
		if (wParam) {			
			mir_cslock lck(csTips);
			if (!hwndToolTips)
				hwndToolTips = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, nullptr, WS_POPUP, 0, 0, 0, 0, nullptr, nullptr, GetModuleHandleA("mir_app.mir"), nullptr);

			if (lParam == MBBF_UNICODE) {
				TOOLINFOW ti;

				memset(&ti, 0, sizeof(TOOLINFOW));
				ti.cbSize = sizeof(TOOLINFOW);
				ti.uFlags = TTF_IDISHWND;
				ti.hwnd = bct->hwnd;
				ti.uId = (UINT_PTR)bct->hwnd;
				if (SendMessage(hwndToolTips, TTM_GETTOOLINFOW, 0, (LPARAM)&ti)) {
					SendMessage(hwndToolTips, TTM_DELTOOLW, 0, (LPARAM)&ti);
				}
				ti.uFlags = TTF_IDISHWND|TTF_SUBCLASS;
				ti.uId = (UINT_PTR)bct->hwnd;
				ti.lpszText=(LPWSTR)wParam;
				SendMessage(hwndToolTips, TTM_ADDTOOLW, 0, (LPARAM)&ti);
			}
			else {
				TOOLINFOA ti;

				memset(&ti, 0, sizeof(TOOLINFOA));
				ti.cbSize = sizeof(TOOLINFOA);
				ti.uFlags = TTF_IDISHWND;
				ti.hwnd = bct->hwnd;
				ti.uId = (UINT_PTR)bct->hwnd;
				if (SendMessage(hwndToolTips, TTM_GETTOOLINFOA, 0, (LPARAM)&ti)) {
					SendMessage(hwndToolTips, TTM_DELTOOLA, 0, (LPARAM)&ti);
				}
				ti.uFlags = TTF_IDISHWND|TTF_SUBCLASS;
				ti.uId = (UINT_PTR)bct->hwnd;
				ti.lpszText=(LPSTR)wParam;
				SendMessage(hwndToolTips, TTM_ADDTOOLA, 0, (LPARAM)&ti);
			}
		}
		break;
	case BUTTONTRANSLATE:
		{
			wchar_t szButton[MAX_PATH];
			GetWindowText(bct->hwnd, szButton, _countof(szButton));
			SetWindowText(bct->hwnd, TranslateW(szButton));
		}
		break;
	case WM_SETFOCUS: // set keybord bFocus and redraw
		bct->bFocus = 1;
		InvalidateRect(bct->hwnd, nullptr, TRUE);
		break;
	case WM_KILLFOCUS: // kill bFocus and redraw
		bct->bFocus = 0;
		InvalidateRect(bct->hwnd, nullptr, TRUE);
		break;
	case WM_WINDOWPOSCHANGED:
		InvalidateRect(bct->hwnd, nullptr, TRUE);
		break;
	case WM_ENABLE: // windows tells us to enable/disable
		bct->stateId = wParam ? PBS_NORMAL : PBS_DISABLED;
		InvalidateRect(bct->hwnd, nullptr, TRUE);
		break;
	case WM_MOUSELEAVE: // faked by the WM_TIMER
		if (bct->stateId != PBS_DISABLED) { // don't change states if disabled
			bct->stateId = PBS_NORMAL;
			InvalidateRect(bct->hwnd, nullptr, TRUE);
		}
		break;
	case WM_LBUTTONDOWN:
		if (bct->stateId != PBS_DISABLED) { // don't change states if disabled
			bct->stateId = PBS_PRESSED;
			InvalidateRect(bct->hwnd, nullptr, TRUE);
		}
		break;
	case WM_LBUTTONUP:
		if (bct->stateId != PBS_DISABLED) { // don't change states if disabled
			BYTE bPressed = bct->stateId == PBS_PRESSED;

			if (bct->dwStyle & MBS_PUSHBUTTON) {
				if (bct->pbState) bct->pbState = 0;
				else bct->pbState = 1;
			}
			bct->stateId = PBS_HOT;

			// Tell your daddy you got clicked, if mouse is still over the button.
			if ((bct->dwStyle & MBS_PUSHBUTTON) || bPressed)
				SendMessage(GetParent(hwndBtn), WM_COMMAND, MAKELONG(GetDlgCtrlID(hwndBtn), BN_CLICKED), (LPARAM)hwndBtn);
			InvalidateRect(bct->hwnd, nullptr, TRUE);
		}
		break;
	case WM_MOUSEMOVE:
		if (bct->stateId == PBS_NORMAL) {
			bct->stateId = PBS_HOT;
			InvalidateRect(bct->hwnd, nullptr, TRUE);
		}
		// Call timer, used to start cheesy TrackMouseEvent faker
		SetTimer(hwndBtn, BUTTON_POLLID, BUTTON_POLLDELAY, nullptr);
		break;
	case WM_TIMER: // use a timer to check if they have did a mouseout
		if (wParam == BUTTON_POLLID) {
			RECT rc;
			POINT pt;

			GetWindowRect(hwndBtn, &rc);
			GetCursorPos(&pt);
			if (!PtInRect(&rc, pt)) { // mouse must be gone, trigger mouse leave
				PostMessage(hwndBtn, WM_MOUSELEAVE, 0, 0L);
				KillTimer(hwndBtn, BUTTON_POLLID);
			}
		}
		break;
	case WM_ERASEBKGND:
		return 1;
	}
	return DefWindowProc(hwndBtn, uMsg, wParam, lParam);
}

void CtrlButtonUnloadModule() 
{
	UnregisterClass(UINFOBUTTONCLASS, ghInst);
}

void CtrlButtonLoadModule()
{
	WNDCLASSEX wc;
	
	memset(&wc, 0, sizeof(wc));
	wc.cbSize				 = sizeof(wc);
	wc.lpszClassName	= UINFOBUTTONCLASS;
	wc.lpfnWndProc		= Button_WndProc;
	wc.hCursor				= LoadCursor(nullptr, IDC_ARROW);
	wc.cbWndExtra		 = sizeof(LPBTNCTRL);
	wc.style					= CS_GLOBALCLASS;
	RegisterClassEx(&wc);
}