/
TTCheckBtnGroup.cpp
565 lines (446 loc) · 13.5 KB
/
TTCheckBtnGroup.cpp
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
#include "stdafx.h"
#include "TTCheckBtnGroup.h"
#include "CommonDrawing.h"
#include "TTButton.h"
CTTCheckBtnGroup::CTTCheckBtnGroup()
{
SetDrawingProperties(1, 5);
m_bTracking = false;
m_Buttons.clear();
m_defaultButtonWidth = 150;
}
CTTCheckBtnGroup::~CTTCheckBtnGroup()
{
}
void CTTCheckBtnGroup::SetDrawingProperties(int borderPenWidth, int cornerRadius)
{
m_BorderPenWidth = borderPenWidth;
m_CornerRadius = cornerRadius;
}
void CTTCheckBtnGroup::AddButton(CString text, bool isChecked)
{
BtnInfo btnInfo;
btnInfo.m_text = text;
btnInfo.m_checked = isChecked;
btnInfo.m_howered = false;
btnInfo.m_rect.SetRectEmpty();
m_Buttons.push_back(btnInfo);
RecalcLayout();
Invalidate(FALSE);
}
void CTTCheckBtnGroup::SetPressColor(COLORREF cl)
{
m_ColorMap.SetColor(Press, BackgroundTopGradientStart, cl);
m_ColorMap.SetColor(Press, BackgroundTopGradientFinish, cl);
m_ColorMap.SetColor(Press, BackgroundBottomGradientStart, cl);
m_ColorMap.SetColor(Press, BackgroundBottomGradientFinish, cl);
Invalidate(FALSE);
}
BEGIN_MESSAGE_MAP(CTTCheckBtnGroup, CButton)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
ON_CONTROL_REFLECT_EX(BN_CLICKED, &CTTCheckBtnGroup::OnBnClicked)
END_MESSAGE_MAP()
void CTTCheckBtnGroup::RecalcLayout()
{
if (GetSafeHwnd() == NULL)
return;
CDC* pDC = GetDC();
int totalWidth = 0;
vector<int> buttonsWidth;
for (int btnId = 0; btnId < m_Buttons.size(); ++btnId)
{
int textExt = pDC->GetTextExtent(m_Buttons[btnId].m_text).cx + 10;
int desiredWidth = max(textExt, m_defaultButtonWidth);
buttonsWidth.push_back(desiredWidth);
totalWidth += desiredWidth;
}
CRect clientRect;
GetClientRect(clientRect);
clientRect.DeflateRect(1, 1);
for (int btnId = 0; btnId < m_Buttons.size(); ++btnId)
{
int left = btnId == 0 ? clientRect.left : (m_Buttons[btnId - 1].m_rect.right);
int right = (btnId == m_Buttons.size() - 1) ? clientRect.right : (left + (int)((float)buttonsWidth[btnId] / (float)totalWidth * (float)clientRect.Width()));
m_Buttons[btnId].m_rect.SetRect(left, clientRect.top, right, clientRect.bottom);
}
}
void CTTCheckBtnGroup::SetProperWidth(int maxWidth)
{
if (GetSafeHwnd() == NULL)
return;
CDC* pDC = GetDC();
int totalWidth = 0;
for (int btnId = 0; btnId < m_Buttons.size(); ++btnId)
{
int textExt = pDC->GetTextExtent(m_Buttons[btnId].m_text).cx + 10;
int desiredWidth = max(textExt, m_defaultButtonWidth);
totalWidth += desiredWidth;
}
CRect clientRect;
GetClientRect(clientRect);
int newWidth = min(maxWidth, totalWidth);
SetWindowPos(NULL, 0, 0, newWidth, clientRect.Height(), SWP_NOSENDCHANGING | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);
Invalidate();
}
bool CTTCheckBtnGroup::IsChecked(int btnId)
{
if (btnId < 0 || btnId >= m_Buttons.size())
return false;
return m_Buttons[btnId].m_checked;
}
void CTTCheckBtnGroup::InvalidateButtonRect(BtnInfo* pBtnInfo)
{
CRect invalidRect(pBtnInfo->m_rect);
invalidRect.right++;
invalidRect.bottom++;
InvalidateRect(invalidRect, FALSE);
}
ControlState CTTCheckBtnGroup::GetButtonState(BtnInfo* pBtnInfo)
{
ControlState state = Normal;
if (pBtnInfo->m_checked)
state = Press;
else if (pBtnInfo->m_howered)
state = Mouseover;
return state;
}
void CTTCheckBtnGroup::OnPaint()
{
CPaintDC dc(this);
CRect clientRect;
GetClientRect(clientRect);
if (clientRect.Width() <= 0 || clientRect.Height() <= 0)
return;
CMemDC memDC(dc, clientRect);
CDC* pDC = &memDC.GetDC();
DrawThemeParentBackground(GetSafeHwnd(), memDC.GetDC().GetSafeHdc(), clientRect);
Graphics graphics(pDC->GetSafeHdc());
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
graphics.SetTextRenderingHint(TextRenderingHintSingleBitPerPixel);
// clear background
CRgn bkgndRgn;
CRect bkgndRect(clientRect);
bkgndRect.left += 1;
bkgndRect.top += 1;
CreateRectRgnInDevicePoints(pDC, &bkgndRgn, bkgndRect, m_CornerRadius);
pDC->SelectClipRgn(&bkgndRgn);
pDC->FillSolidRect(&clientRect, RGB(255, 255, 255));
CRgn clientRgn;
CreateRectRgnInDevicePoints(pDC, &clientRgn, clientRect);
pDC->SelectClipRgn(&clientRgn);
// Whole border
Gdiplus::Rect borderRect(clientRect.left, clientRect.top, clientRect.Width(), clientRect.Height());
borderRect.Inflate(-1, -1);
if (m_Buttons.size() == 0)
{
COLORREF borderColor = m_ColorMap.GetColor(Normal, Border);
DrawRectArea(borderRect, graphics, borderColor, m_CornerRadius, m_BorderPenWidth);
}
for (int btnId = 0; btnId < m_Buttons.size(); ++btnId)
{
BtnInfo* pBtnInfo = &m_Buttons[btnId];
CRgn btnRgn;
CRect t(pBtnInfo->m_rect);
t.right += 1; t.bottom += 1;
CreateRectRgnInDevicePoints(pDC, &btnRgn, t);
btnRgn.CombineRgn(&btnRgn, &bkgndRgn, RGN_AND);
pDC->SelectClipRgn(&btnRgn);
ControlState buttonState = GetButtonState(pBtnInfo);
// Highlight
if (buttonState != Press)
pDC->FillSolidRect(pBtnInfo->m_rect, m_ColorMap.GetColor(buttonState, BorderLight));
// Border
graphics.SetClip(btnRgn);
COLORREF borderColor = m_ColorMap.GetColor(buttonState, Border);
DrawRectArea(borderRect, graphics, borderColor, m_CornerRadius, m_BorderPenWidth);
graphics.ResetClip();
CRgn offsetRgn1;
offsetRgn1.CreateRectRgn(0, 0, 0, 0);
offsetRgn1.CopyRgn(&btnRgn);
offsetRgn1.OffsetRgn(m_BorderPenWidth + 1, m_BorderPenWidth + 1);
CRgn offsetRgn2;
offsetRgn2.CreateRectRgn(0, 0, 0, 0);
offsetRgn2.CopyRgn(&btnRgn);
offsetRgn2.OffsetRgn(-(m_BorderPenWidth + 1), -(m_BorderPenWidth + 1));
CRgn backRgn;
backRgn.CreateRectRgn(0, 0, 0, 0);
backRgn.CombineRgn(&offsetRgn1, &offsetRgn2, RGN_AND);
pDC->SelectClipRgn(&backRgn);
if (buttonState != Mouseover)
{
DrawGradient(pDC, t);
/*Draw4ColorsGradientRect(t, *pDC,
m_ColorMap.GetColor(buttonState, BackgroundTopGradientStart),
m_ColorMap.GetColor(buttonState, BackgroundTopGradientFinish),
m_ColorMap.GetColor(buttonState, BackgroundBottomGradientStart),
m_ColorMap.GetColor(buttonState, BackgroundBottomGradientFinish),
m_CornerRadius, TRUE);*/
}
COLORREF textColor = RGB(80,80,80);//COLOR_GRAYTEXT;//COLOR_BTNTEXT;
/*if (buttonState == Press)
textColor = RGB(0, 122, 224);*/
pDC->SelectClipRgn(&btnRgn);
CFont* pFont = GetFont();
int nFormat = DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS;
CTTButton::DrawCaptionText(pDC, &pBtnInfo->m_rect, &pBtnInfo->m_rect, pBtnInfo->m_text, nFormat, pFont, textColor);
}
pDC->SelectClipRgn(&clientRgn);
for (int btnId = 1; btnId < m_Buttons.size(); ++btnId)
{
BtnInfo* pPrevBtnInfo = &m_Buttons[btnId - 1];
BtnInfo* pCurBtnInfo = &m_Buttons[btnId];
ControlState prevBtnState = GetButtonState(pPrevBtnInfo);
ControlState curBtnState = GetButtonState(pCurBtnInfo);
ControlState state = Normal;
if (prevBtnState == Press || curBtnState == Press)
state = Press;
else if (prevBtnState == Mouseover || curBtnState == Mouseover)
state = Mouseover;
COLORREF borderColor = m_ColorMap.GetColor(state, Border);
CPen borderPen(PS_SOLID, m_BorderPenWidth, borderColor);
pDC->SelectObject(borderPen);
pDC->MoveTo(pPrevBtnInfo->m_rect.right, pPrevBtnInfo->m_rect.top);
pDC->LineTo(pPrevBtnInfo->m_rect.right, pPrevBtnInfo->m_rect.bottom);
}
}
BOOL CTTCheckBtnGroup::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CTTCheckBtnGroup::OnSize(UINT nType, int cx, int cy)
{
CButton::OnSize(nType, cx, cy);
RecalcLayout();
Invalidate(FALSE);
}
void CTTCheckBtnGroup::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = GetSafeHwnd();
m_bTracking = ::_TrackMouseEvent(&tme);
}
bool foundedHower = false;
for (int btnId = 0; btnId < m_Buttons.size(); ++btnId)
{
BtnInfo* pBtnInfo = &m_Buttons[btnId];
bool oldHower = pBtnInfo->m_howered;
pBtnInfo->m_howered = !foundedHower && pBtnInfo->m_rect.PtInRect(point);
if (oldHower != pBtnInfo->m_howered)
InvalidateButtonRect(pBtnInfo);
foundedHower = foundedHower || pBtnInfo->m_howered;
}
CButton::OnMouseMove(nFlags, point);
}
void CTTCheckBtnGroup::OnMouseLeave()
{
m_bTracking = false;
for (int btnId = 0; btnId < m_Buttons.size(); ++btnId)
{
BtnInfo* pBtnInfo = &m_Buttons[btnId];
if (pBtnInfo->m_howered)
{
pBtnInfo->m_howered = false;
InvalidateButtonRect(pBtnInfo);
}
}
CButton::OnMouseLeave();
}
BOOL CTTCheckBtnGroup::OnBnClicked()
{
POINT pt;
if (!GetCursorPos(&pt))
return TRUE;
ScreenToClient(&pt);
for (int btnId = 0; btnId < m_Buttons.size(); ++btnId)
{
BtnInfo* pBtnInfo = &m_Buttons[btnId];
if (pBtnInfo->m_rect.PtInRect(pt))
{
pBtnInfo->m_howered = false;
pBtnInfo->m_checked = !pBtnInfo->m_checked;
InvalidateButtonRect(pBtnInfo);
return FALSE;
}
}
return TRUE;
}
void CTTCheckBtnGroup::DrawGradient(CDC* pDC, CRect rect)
{
Color gdipBaseColor, gdipLightColor;
COLORREF baseColor = m_ColorMap.GetColor(Press, BackgroundTopGradientStart);
HLSColor hlsBaseColor = RgbToHls(baseColor);
if (hlsBaseColor.L > 0.8)
{
COLORREF darkColor = GetLumColor(baseColor, -0.3);
gdipBaseColor.SetFromCOLORREF(baseColor);
gdipLightColor.SetFromCOLORREF(darkColor);
}
else
{
COLORREF lightColor = GetLumColor(baseColor, 0.3);
gdipBaseColor.SetFromCOLORREF(baseColor);
gdipLightColor.SetFromCOLORREF(lightColor);
}
Graphics graphics(pDC->GetSafeHdc());
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
// Draw frag rectangle
Rect boundsRect(rect.left, rect.top, rect.Width(), rect.Height());
GraphicsPath path;
GetRoundRectPath(&path, boundsRect, 8);
Rect upperRect(boundsRect);
upperRect.Height = boundsRect.Height / 2;
Rect lowerRect(boundsRect);
lowerRect.Y = upperRect.GetBottom() - 1;
lowerRect.Height = boundsRect.Height - upperRect.Height;
Rect upGradRect(upperRect);
upGradRect.Inflate(0, 1);
Rect loGradRect(lowerRect);
loGradRect.Inflate(0, 1);
LinearGradientBrush brush1(upGradRect, gdipLightColor, gdipBaseColor, LinearGradientMode::LinearGradientModeVertical);
LinearGradientBrush brush2(loGradRect, gdipBaseColor, gdipBaseColor, LinearGradientMode::LinearGradientModeVertical);
//SolidBrush brush(baseColor);
//graphics.FillRectangle(&brush, upperRect);
//graphics.FillPath(&brush1, &path);
Region wholeRgn(&path);
Region upperRgn(upperRect);
upperRgn.Intersect(&wholeRgn);
Region lowerRgn(lowerRect);
lowerRgn.Intersect(&wholeRgn);
graphics.FillRegion(&brush1, &upperRgn);
graphics.FillRegion(&brush2, &lowerRgn);
//SolidBrush brush(gdipFragColor);
//graphics.FillPath(&brush, &path);
}
COLORREF CTTCheckBtnGroup::GetLumColor(COLORREF baseCl, double lumOffset, double minLum, double maxLum)
{
HLSColor hlsResult = RgbToHls(baseCl);
hlsResult.L += lumOffset;
if (hlsResult.L > maxLum)
hlsResult.L = maxLum;
if (hlsResult.L < minLum)
hlsResult.L = minLum;
return HLSToRGB(hlsResult);
}
// Converts a HSL color to an RGB color
// Returns an RGB color object equivalent to the HSL color object passed in
COLORREF CTTCheckBtnGroup::HLSToRGB(HLSColor hls)
{
int R, G, B, A = 0;
/* If S = 0, that means we are dealing with a shade
* of gray. So, we set R, G, and B to L and exit. */
// Special case: Gray
if (hls.S == 0)
{
R = (int)(hls.L * 255);
G = (int)(hls.L * 255);
B = (int)(hls.L * 255);
A = (int)(hls.A * 255);
return RGB(R, G, B);
}
double t1;
if (hls.L < 0.5)
{
t1 = hls.L*(1.0 + hls.S);
}
else
{
t1 = hls.L + hls.S - (hls.L * hls.S);
}
double t2 = 2.0*hls.L - t1;
// Convert H from degrees to a percentage
double h = hls.H / 360;
// Set colors as percentage values
double tR = h + (1.0 / 3.0);
double r = SetColor(t1, t2, tR);
double tG = h;
double g = SetColor(t1, t2, tG);
double tB = h - (1.0 / 3.0);
double b = SetColor(t1, t2, tB);
R = (int)(r * 255);
G = (int)(g * 255);
B = (int)(b * 255);
A = (int)(A * 255);
return RGB(R, G, B);
}
double CTTCheckBtnGroup::SetColor(double t1, double t2, double t3)
{
if (t3 < 0) t3 += 1.0;
if (t3 > 1) t3 -= 1.0;
double color;
if (6.0 * t3 < 1)
{
color = t2 + (t1 - t2) * 6.0 * t3;
}
else if (2.0 * t3 < 1)
{
color = t1;
}
else if (3.0*t3 < 2)
{
color = t2 + (t1 - t2) * ((2.0 / 3.0) - t3) * 6.0;
}
else
{
color = t2;
}
return color;
}
// Converts a WPF RGB color to an HSL color
// Returns an HSL color object equivalent to the RGB color object passed in
HLSColor CTTCheckBtnGroup::RgbToHls(COLORREF rgbColor)
{
// Initialize result
HLSColor hls;
// Convert RGB values to percentages
double r = (double)GetRValue(rgbColor) / 255;
double g = (double)GetGValue(rgbColor) / 255;
double b = (double)GetBValue(rgbColor) / 255;
double a = (double)(LOBYTE((rgbColor) >> 24)) / 255;
// Find min and max RGB values
double min = __min(r, __min(g, b));
double max = __max(r, __max(g, b));
double delta = max - min;
/* If max and min are equal, that means we are dealing with
* a shade of gray. So we set H and S to zero, and L to either
* max or min (it doesn't matter which), and then we exit. */
//Special case: Gray
if (max == min)
{
hls.H = 0;
hls.S = 0;
hls.L = max;
return hls;
}
/* If we get to this point, we know we don't have a shade of gray. */
// Set L
hls.L = (min + max) / 2;
// Set S
if (hls.L < 0.5)
{
hls.S = delta / (max + min);
}
else
{
hls.S = delta / (2.0 - max - min);
}
// Set H
if (r == max) hls.H = (g - b) / delta;
if (g == max) hls.H = 2.0 + (b - r) / delta;
if (b == max) hls.H = 4.0 + (r - g) / delta;
hls.H *= 60;
if (hls.H < 0) hls.H += 360;
// Set A
hls.A = a;
// Set return value
return hls;
}