aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/commandLink.cpp
blob: 69f18189087fb95ed8e7309fa9b1ff3444cb8099 (plain) (blame)
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
#include "main.h"
#include "./commandLink.h"
#include "./api.h"
//#include "./guiObjects.h"

#include <windows.h>
#include <shlwapi.h>
#include <commctrl.h>
#include <strsafe.h>

#ifndef LONGX86
#ifdef _WIN64
  #define LONGX86	LONG_PTR
#else /*_WIN64*/
  #define LONGX86	 LONG	
#endif  /*_WIN64*/
#endif // LONGX86


#define MARGIN_LEFT		2
#define MARGIN_TOP		0
#define MARGIN_RIGHT		2
#define MARGIN_BOTTOM	1

typedef struct __COMMANDLINK
{
	COLORREF	rgbBk;
	COLORREF	rgbText;
	COLORREF	rgbTextVisited;
	COLORREF	rgbTextHighlight;
	HCURSOR		cursorHot;
	HFONT		textFont;
	UINT		state;
	RECT		margins;
} COMMANDLINK;

#define GetCommandLink(__hwnd) ((COMMANDLINK*)(LONG_PTR)(LONGX86)GetWindowLongPtrW((__hwnd), 0))

static LRESULT CALLBACK CommandLink_WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


EXTERN_C BOOL CommandLink_RegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEXW  wc;
	ATOM klassAtom;

	if (GetClassInfoExW(hInstance, NWC_COMMANDLINKW, &wc)) 
		return TRUE;

	ZeroMemory(&wc, sizeof(WNDCLASSEXW));

	wc.cbSize        = sizeof(WNDCLASSEXW);
	wc.hInstance     = hInstance;
	wc.lpszClassName = NWC_COMMANDLINKW;
	wc.lpfnWndProc   = CommandLink_WindowProc;
	wc.style         = CS_GLOBALCLASS;
	wc.hIcon         = NULL;
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = NULL;
	wc.cbWndExtra    = sizeof(COMMANDLINK*);
	
	klassAtom = RegisterClassExW(&wc);
	if (0 == klassAtom)
		return FALSE;

	if (NULL != WASABI_API_APP)
		WASABI_API_APP->DirectMouseWheel_RegisterSkipClass(klassAtom);

	return TRUE;
}


static COLORREF CommandLink_BlendColors(COLORREF rgbTop, COLORREF rgbBottom, INT alpha)
{
	if (alpha > 254) return rgbTop;
	if (alpha < 0) return rgbBottom;

	INT k = (((255 - alpha)*255 + 127)/255);
	
	return RGB( (GetRValue(rgbTop)*alpha + k*GetRValue(rgbBottom) + 127)/255, 
				(GetGValue(rgbTop)*alpha + k*GetGValue(rgbBottom) + 127)/255, 
				(GetBValue(rgbTop)*alpha + k*GetBValue(rgbBottom) + 127)/255);
}

static HBRUSH CommandLink_GetPaintColors(HWND hwnd, HDC hdc, COLORREF *rgbBkOut, COLORREF *rgbFgOut)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return NULL;
	
	COLORREF rgbBk, rgbFg;
	UINT windowStyle = GetWindowLongPtrW(hwnd, GWL_STYLE);
	BOOL defaultColors = (0 != (CLS_DEFAULTCOLORS & windowStyle));

	rgbBk = (defaultColors) ? GetSysColor(COLOR_3DFACE) : link->rgbBk;
	if (NULL != rgbBkOut) *rgbBkOut = rgbBk;

	if (0 != (WS_DISABLED & windowStyle))
		rgbFg = GetSysColor(COLOR_GRAYTEXT);
	else
	{	
		if (0 != (CLIS_VISITED & link->state))
			rgbFg = (defaultColors) ? RGB(85, 26, 139) : link->rgbTextVisited;
		else
			rgbFg = (defaultColors) ? GetSysColor(COLOR_HOTLIGHT) : link->rgbText;

		if (0 != ((CLIS_HOT | CLIS_PRESSED) & link->state))
		{
			if (0 == (CLS_HIGHLIGHTCOLOR & windowStyle))
				rgbFg = CommandLink_BlendColors(rgbFg, rgbBk, 160);
			else
				rgbFg = link->rgbTextHighlight;
		}
	}

	if (NULL != rgbFgOut)
		*rgbFgOut = rgbFg;
	

	HBRUSH backBrush = NULL;
	if (FALSE != defaultColors)
	{
		HWND hParent = GetParent(hwnd);
		if (NULL != hParent)
		{
			COLORREF backBk = SetBkColor(hdc, rgbBk);
			COLORREF backFg = SetBkColor(hdc, rgbFg);
			backBrush = (HBRUSH)SendMessageW(hParent, WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwnd);
			rgbBk = SetBkColor(hdc, backBk);
			SetBkColor(hdc, backFg);
		}
	}
	return backBrush;
}

static BOOL CommandLink_GetTextRect(HDC hdc, LPCWSTR pszText, INT cchText, const RECT *prcClient, RECT *prcTextOut, const RECT *margins)
{
	if (NULL == prcClient || NULL == prcTextOut)
		return FALSE;

	SIZE textSize;
	if (0 == cchText || !GetTextExtentPoint32W(hdc, pszText, cchText, &textSize))
		return SetRectEmpty(prcTextOut);
		
	prcTextOut->left = ((prcClient->right - prcClient->left) - textSize.cx) / 2;
	if (prcTextOut->left < (prcClient->left + margins->left)) 
		prcTextOut->left = prcClient->left + margins->left;
	
	prcTextOut->top = ((prcClient->bottom - prcClient->top) - textSize.cy) / 2;
	if (prcTextOut->top < (prcClient->top + margins->top)) 
		prcTextOut->top = prcClient->top + margins->top;

	prcTextOut->right = prcTextOut->left + textSize.cx;
	if (prcTextOut->right > (prcClient->right - margins->right))
		prcTextOut->right = prcClient->right - margins->right;

	prcTextOut->bottom = prcTextOut->top + textSize.cy;
	if (prcTextOut->bottom > (prcClient->bottom - margins->bottom))
		prcTextOut->bottom = prcClient->bottom - margins->bottom;
	
	return TRUE;
}

static BOOL CommandLink_GetIdealSizeReal(HWND hwnd, SIZE *sizeOut)
{
	if (NULL == sizeOut)
		return FALSE;
	
	ZeroMemory(sizeOut, sizeof(SIZE));

	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return FALSE;

	WCHAR szText[256] = {0};
	INT cchText = GetWindowTextW(hwnd, szText, ARRAYSIZE(szText));
	
	HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_WINDOW | DCX_NORESETATTRS);
	if (NULL == hdc) return FALSE;

	HFONT originalFont = (HFONT)SelectObject(hdc, link->textFont);
	BOOL resultOk;

	if (0 == cchText)
	{
		TEXTMETRICW tm;
		resultOk = GetTextMetricsW(hdc, &tm);
		if (resultOk) sizeOut->cy = tm.tmHeight;
	}
	else
		resultOk = GetTextExtentPoint32W(hdc, szText, cchText, sizeOut);
	
	if (originalFont != link->textFont) SelectObject(hdc, originalFont);
	ReleaseDC(hwnd, hdc);

	if (resultOk)
	{
		sizeOut->cx += (link->margins.left + link->margins.right);
		sizeOut->cy += (link->margins.top + link->margins.bottom);
	}
	return resultOk;
}

static BOOL CommandLink_SetStateEx(HWND hwnd, UINT newState, UINT stateMask, BOOL fRedraw)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return FALSE;
	
	UINT oldState = link->state;
	link->state = (link->state & ~stateMask) | (newState & stateMask); 

	if (oldState != link->state && fRedraw)
		InvalidateRect(hwnd, NULL, TRUE);
	
	return TRUE;
}

static void CommandLink_Paint(HWND hwnd, HDC hdc, const RECT *prcPaint, BOOL fErase)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return;

	RECT clientRect, partRect;
	if (!GetClientRect(hwnd, &clientRect)) return;

	DWORD windowStyle = GetWindowLongPtrW(hwnd, GWL_STYLE);

	COLORREF rgbBk, rgbFg;
	HBRUSH backBrush = CommandLink_GetPaintColors(hwnd, hdc, &rgbBk, &rgbFg);

	COLORREF originalBk = SetBkColor(hdc, rgbBk);
	COLORREF originalFg = SetTextColor(hdc, rgbFg);

	HFONT originalFont = (HFONT)SelectObject(hdc, link->textFont);

	WCHAR szText[256] = {0};
	INT cchText = GetWindowTextW(hwnd, szText, ARRAYSIZE(szText));

	RECT textRect;
	CommandLink_GetTextRect(hdc, szText, cchText, &clientRect, &textRect, &link->margins);

	if (fErase)
	{
		if(NULL != backBrush)
		{
			FillRect(hdc, prcPaint, backBrush);
		}
		else
		{
			ExtTextOut(hdc, 0, 0, ETO_OPAQUE, prcPaint, NULL, 0, NULL);
		}
	}

	if (0 != ((CLIS_HOT | CLIS_PRESSED) & link->state) ||
		0 != (CLS_ALWAYSUNDERLINE & windowStyle))
	{		
		TEXTMETRIC tm;
		if (GetTextMetrics(hdc, &tm))
		{
			CopyRect(&partRect, &textRect);
			partRect.top = partRect.top + tm.tmAscent + 1;
			partRect.bottom = partRect.top + 1;
				
			COLORREF originalColor = SetBkColor(hdc, rgbFg);
			ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &partRect, NULL, 0, NULL);
			if (originalColor != rgbFg) SetBkColor(hdc, originalColor);
		}
	}

	if (0 != cchText)
	{	
		INT originalMode = SetBkMode(hdc, TRANSPARENT);
		UINT originalAlign = SetTextAlign(hdc, TA_LEFT | TA_TOP);
		ExtTextOutW(hdc, textRect.left, textRect.top, ETO_CLIPPED, &textRect, szText, cchText, NULL);
		if (TRANSPARENT != originalMode) SetBkMode(hdc, originalMode);
		if ((TA_LEFT | TA_TOP) != originalAlign) SetTextAlign(hdc, originalAlign);
	}

	if (CLIS_FOCUSED == ((CLIS_FOCUSED | CLIS_HIDEFOCUS) & link->state))
	{
		SetBkColor(hdc, 0x00FFFFFF);
		SetTextColor(hdc, 0x000000000);
		DrawFocusRect(hdc, &clientRect);
	}

	SelectObject(hdc, originalFont);
	if (originalBk != rgbBk) SetBkColor(hdc, originalBk);
	if (originalFg != rgbFg) SetTextColor(hdc, originalFg);

}

static void CommandLink_NotifyParent(HWND hwnd, UINT notificationId)
{
	NMHDR nmhdr;
	nmhdr.code = notificationId;
	nmhdr.hwndFrom = hwnd;
	nmhdr.idFrom = GetDlgCtrlID(hwnd);

	HWND hParent = GetParent(hwnd);
	if (NULL != hParent)
	{
		SendMessageW(hParent, WM_NOTIFY, (WPARAM)nmhdr.idFrom, (LPARAM)&nmhdr);
	}
}

static void CommandLink_Click(HWND hwnd)
{
	CommandLink_NotifyParent(hwnd, NM_CLICK);
	
	DWORD windowStyle = GetWindowLongPtrW(hwnd, GWL_STYLE);
	if (0 != (CLS_TRACKVISITED & windowStyle))
		CommandLink_SetStateEx(hwnd, CLIS_VISITED, CLIS_VISITED, TRUE);

}

static void CommandLink_QueryUiState(HWND hwnd)
{
	UINT uiState = (UINT)DefWindowProcW(hwnd, WM_QUERYUISTATE, 0, 0L);
	CommandLink_SetStateEx(hwnd, (0 != (UISF_HIDEFOCUS & uiState)) ? CLIS_HIDEFOCUS : 0, CLIS_HIDEFOCUS, TRUE);
}
static LRESULT CommandLink_OnCreateWindow(HWND hwnd, CREATESTRUCT *pcs)
{	
	UNREFERENCED_PARAMETER(pcs);

	COMMANDLINK *link;

	link = (COMMANDLINK*)calloc(1, sizeof(COMMANDLINK));
	if (NULL == link)
	{
		DestroyWindow(hwnd);
		return -1;
	}

	SetLastError(ERROR_SUCCESS);
	if (!SetWindowLongPtrW(hwnd, 0, (LONGX86)(LONG_PTR)link) && ERROR_SUCCESS != GetLastError())
	{
		free(link);
		DestroyWindow(hwnd);
		return -1;
	}

	link->rgbBk = GetSysColor(COLOR_3DFACE);
	link->rgbText = GetSysColor(COLOR_HOTLIGHT);
	link->rgbTextVisited = (0x00FFFFFF & (~link->rgbText));
	link->rgbTextHighlight = GetSysColor(COLOR_HOTLIGHT);
	link->cursorHot = LoadCursor(NULL, IDC_HAND);
	SetRect(&link->margins, MARGIN_LEFT, MARGIN_TOP, MARGIN_RIGHT, MARGIN_BOTTOM);

	CommandLink_QueryUiState(hwnd);
	return FALSE;
}

static void CommandLink_OnDestroy(HWND hwnd)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	SetWindowLongPtrW(hwnd, 0, 0L);
	if (!link) return;
	
	free(link);
}

static void CommandLink_OnPaint(HWND hwnd)
{
	PAINTSTRUCT ps;
	if (BeginPaint(hwnd, &ps))
	{
		if (ps.rcPaint.left != ps.rcPaint.right)
			CommandLink_Paint(hwnd, ps.hdc, &ps.rcPaint, ps.fErase);
		EndPaint(hwnd, &ps);
	}
}

static void CommandLink_OnPrintClient(HWND hwnd, HDC hdc, UINT options)
{	
	RECT clientRect;
	if (GetClientRect(hwnd, &clientRect))
	CommandLink_Paint(hwnd, hdc, &clientRect, 0 != (PRF_ERASEBKGND & options));
}

static void CommandLink_OnSetFocus(HWND hwnd, HWND lostFocus)
{
	UNREFERENCED_PARAMETER(lostFocus);
	CommandLink_SetStateEx(hwnd, CLIS_FOCUSED, CLIS_FOCUSED, TRUE);
	CommandLink_NotifyParent(hwnd, NM_SETFOCUS);
}

static void CommandLink_OnKillFocus(HWND hwnd, HWND receiveFocus)
{
	UNREFERENCED_PARAMETER(receiveFocus);
	CommandLink_SetStateEx(hwnd, 0, CLIS_FOCUSED, TRUE);
	CommandLink_NotifyParent(hwnd, NM_KILLFOCUS);
}

static void CommandLink_OnMouseMove(HWND hwnd, UINT mouseFlags, POINTS pts)
{
	UNREFERENCED_PARAMETER(mouseFlags);
	UNREFERENCED_PARAMETER(pts);

	UINT windowStyle = GetWindowLongPtrW(hwnd, GWL_STYLE);

	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL != link)
	{
		if (0 == (CLIS_HOT & link->state) && 0 != (CLS_HOTTRACK & windowStyle))
		{
			link->state |= CLIS_HOT;
			InvalidateRect(hwnd, NULL, FALSE);

			TRACKMOUSEEVENT tm;
			tm.cbSize = sizeof(TRACKMOUSEEVENT);
			tm.dwFlags = TME_LEAVE;
			tm.hwndTrack = hwnd;
			TrackMouseEvent(&tm);
		}
	}
}

static void CommandLink_OnMouseLeave(HWND hwnd)
{
	CommandLink_SetStateEx(hwnd, 0, CLIS_HOT, TRUE);
}

static void CommandLink_OnLButtonDown(HWND hwnd, UINT mouseFlags, POINTS pts)
{
	UNREFERENCED_PARAMETER(mouseFlags);
	UNREFERENCED_PARAMETER(pts);

	if (IsWindowEnabled(hwnd) && hwnd != GetFocus())
		SetFocus(hwnd);
	
	CommandLink_SetStateEx(hwnd, CLIS_PRESSED, CLIS_PRESSED, TRUE);
	if (hwnd != GetCapture())
		SetCapture(hwnd);

}

static void CommandLink_OnLButtonUp(HWND hwnd, UINT mouseFlags, POINTS pts)
{
	UNREFERENCED_PARAMETER(mouseFlags);

	RECT clientRect;
	POINT pt;
	POINTSTOPOINT(pt, pts);

	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL != link && 0 != (CLIS_PRESSED & link->state))
	{
		if (GetClientRect(hwnd, &clientRect) && PtInRect(&clientRect, pt))
			CommandLink_Click(hwnd);
		CommandLink_SetStateEx(hwnd, 0, CLIS_PRESSED, TRUE);
	}

	if (hwnd == GetCapture())
		SetCapture(NULL);
}

static void CommandLink_OnKeyDown(HWND hwnd, UINT virtualKey, UINT keyFlags)
{
	UNREFERENCED_PARAMETER(hwnd);
	UNREFERENCED_PARAMETER(virtualKey);
	UNREFERENCED_PARAMETER(keyFlags);
	
}

static void CommandLink_OnKeyUp(HWND hwnd, UINT virtualKey, UINT keyFlags)
{
	UNREFERENCED_PARAMETER(keyFlags);

	switch(virtualKey)
	{
		case VK_SPACE:
			CommandLink_Click(hwnd);
			break;
	}

}

static void CommandLink_OnEnable(HWND hwnd, BOOL fEnabled)
{
	UNREFERENCED_PARAMETER(fEnabled);
    InvalidateRect(hwnd, NULL, TRUE);
}


static void CommandLink_OnUpdateUiState(HWND hwnd, UINT actionId, UINT stateId)
{
	DefWindowProcW(hwnd, WM_UPDATEUISTATE, MAKEWPARAM(actionId, stateId), 0L);
	CommandLink_QueryUiState(hwnd);
}

static void CommandLink_OnSetFont(HWND hwnd, HFONT newFont, BOOL fRedraw)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL != link) link->textFont = newFont;

	if (FALSE != fRedraw)
		InvalidateRect(hwnd, NULL, TRUE);
}

static LRESULT CommandLink_OnGetFont(HWND hwnd)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	return (LRESULT)((NULL != link) ? link->textFont : NULL);
}

static LRESULT CommandLink_OnSetCursor(HWND hwnd, HWND cursorWindow, UINT hitTest, UINT uMsg)
{	
	UNREFERENCED_PARAMETER(cursorWindow);
	UNREFERENCED_PARAMETER(hitTest);
	UNREFERENCED_PARAMETER(uMsg);

	if (IsWindowEnabled(hwnd))
	{
		COMMANDLINK *link = GetCommandLink(hwnd);
		if (NULL != link && NULL != link->cursorHot)
		{
			SetCursor(link->cursorHot);
			return TRUE;
		}
	}
	return FALSE;
}

static LRESULT CommandLink_OnGetIdealHeight(HWND hwnd)
{
	SIZE windowSize;
	return (CommandLink_GetIdealSizeReal(hwnd, &windowSize)) ? windowSize.cy : 0;
}

static LRESULT CommandLink_OnGetIdealSize(HWND hwnd, SIZE *sizeOut)
{
	return CommandLink_GetIdealSizeReal(hwnd, sizeOut);
}

static void CommandLink_OnResetVisited(HWND hwnd)
{
	CommandLink_SetStateEx(hwnd, 0, CLIS_VISITED, TRUE);
}


static BOOL CommandLink_OnGetMargins(HWND hwnd, RECT *prc)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == prc || NULL == link) return FALSE;
	return CopyRect(prc, &link->margins);
}

static BOOL CommandLink_OnSetMargins(HWND hwnd, const RECT *prc)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == prc || NULL == link) return FALSE;
	return CopyRect(&link->margins, prc);
}

static BOOL CommandLink_OnSetBackColor(HWND hwnd, COLORREF rgb)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return FALSE;
	
	link->rgbBk = rgb;
	return TRUE;
}

static BOOL CommandLink_OnGetBackColor(HWND hwnd)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	return (NULL != link) ? link->rgbBk : RGB(255, 0, 255);
}

static BOOL CommandLink_OnSetTextColor(HWND hwnd, COLORREF rgb)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return FALSE;
	
	link->rgbText = rgb;
	return TRUE;
}

static BOOL CommandLink_OnGetTextColor(HWND hwnd)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	return (NULL != link) ? link->rgbText : RGB(255, 0, 255);
}

static BOOL CommandLink_OnSetVisitedColor(HWND hwnd, COLORREF rgb)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return FALSE;
	
	link->rgbTextVisited = rgb;
	return TRUE;
}

static BOOL CommandLink_OnGetVisitedColor(HWND hwnd)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	return (NULL != link) ? link->rgbTextVisited : RGB(255, 0, 255);
}

static BOOL CommandLink_OnSetHighlightColor(HWND hwnd, COLORREF rgb)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	if (NULL == link) return FALSE;
	
	link->rgbTextHighlight = rgb;
	return TRUE;
}

static BOOL CommandLink_OnGetHighlightColor(HWND hwnd)
{
	COMMANDLINK *link = GetCommandLink(hwnd);
	return (NULL != link) ? link->rgbTextHighlight : RGB(255, 0, 255);
}

static LRESULT CALLBACK CommandLink_WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch ( uMsg )
	{
		case WM_CREATE:
			return CommandLink_OnCreateWindow( hwnd, (CREATESTRUCT *) lParam );
		case WM_DESTROY:
			CommandLink_OnDestroy( hwnd );
			return 0;
		case WM_PAINT:
			CommandLink_OnPaint( hwnd );
			return 0;
		case WM_PRINTCLIENT:
			CommandLink_OnPrintClient( hwnd, (HDC) wParam, (UINT) lParam );
			return 0;
		case WM_ERASEBKGND:
			return 0;
		case WM_SETFONT:
			CommandLink_OnSetFont( hwnd, (HFONT) wParam, (BOOL) LOWORD( lParam ) );
			return 0;
		case WM_GETFONT:
			return CommandLink_OnGetFont( hwnd );
		case WM_SETFOCUS:
			CommandLink_OnSetFocus( hwnd, (HWND) wParam );
			return 0;
		case WM_KILLFOCUS:
			CommandLink_OnKillFocus( hwnd, (HWND) wParam );
			return 0;
		case WM_MOUSEMOVE:
			CommandLink_OnMouseMove( hwnd, (UINT) wParam, MAKEPOINTS( lParam ) );
			return 0;
		case WM_MOUSELEAVE:
			CommandLink_OnMouseLeave( hwnd );
			return 0;
		case WM_LBUTTONDOWN:
			CommandLink_OnLButtonDown( hwnd, (UINT) wParam, MAKEPOINTS( lParam ) );
			return 0;
		case WM_LBUTTONUP:
			CommandLink_OnLButtonUp( hwnd, (UINT) wParam, MAKEPOINTS( lParam ) );
			return 0;
		case WM_KEYDOWN:
			CommandLink_OnKeyDown( hwnd, (UINT) wParam, (UINT) lParam );
			return 0;
		case WM_KEYUP:
			CommandLink_OnKeyUp( hwnd, (UINT) wParam, (UINT) lParam );
			return 0;
		case WM_ENABLE:
			CommandLink_OnEnable( hwnd, (BOOL) wParam );
			return 0;
		case WM_UPDATEUISTATE:
			CommandLink_OnUpdateUiState( hwnd, LOWORD( wParam ), HIWORD( wParam ) );
			return 0;
		case WM_SETCURSOR:
			return CommandLink_OnSetCursor( hwnd, (HWND) wParam, LOWORD( lParam ), HIWORD( lParam ) );

		case CLM_GETIDEALHEIGHT:
			return CommandLink_OnGetIdealHeight( hwnd );
		case CLM_GETIDEALSIZE:
			return CommandLink_OnGetIdealSize( hwnd, (SIZE *) lParam );
		case CLM_RESETVISITED:
			CommandLink_OnResetVisited( hwnd );
			return 0;
		case CLM_GETMARGINS:
			return CommandLink_OnGetMargins( hwnd, (RECT *) lParam );
		case CLM_SETMARGINS:
			return CommandLink_OnSetMargins( hwnd, (const RECT *) lParam );
		case CLM_SETBACKCOLOR:
			return CommandLink_OnSetBackColor( hwnd, (COLORREF) lParam );
		case CLM_GETBACKCOLOR:
			return CommandLink_OnGetBackColor( hwnd );
		case CLM_SETTEXTCOLOR:
			return CommandLink_OnSetTextColor( hwnd, (COLORREF) lParam );
		case CLM_GETTEXTCOLOR:
			return CommandLink_OnGetTextColor( hwnd );
		case CLM_SETVISITEDCOLOR:
			return CommandLink_OnSetVisitedColor( hwnd, (COLORREF) lParam );
		case CLM_GETVISITEDCOLOR:
			return CommandLink_OnGetVisitedColor( hwnd );
		case CLM_SETHIGHLIGHTCOLOR:
			return CommandLink_OnSetHighlightColor( hwnd, (COLORREF) lParam );
		case CLM_GETHIGHLIGHTCOLOR:
			return CommandLink_OnGetHighlightColor( hwnd );
	}

	return DefWindowProcW( hwnd, uMsg, wParam, lParam );
}