forked from WinMerge/winmerge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTitleBarHelper.cpp
289 lines (263 loc) · 9.56 KB
/
TitleBarHelper.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
// Copyright (c) 2024 Takashi Sawanaka
// SPDX-License-Identifier: BSL-1.0
/**
* @file TitleBarHelper.cpp
*
* @brief Implementation of the CTitleBarHelper class
*/
#include "StdAfx.h"
#include "TitleBarHelper.h"
CTitleBarHelper::CTitleBarHelper()
: m_pWnd(nullptr)
, m_maximized(false)
, m_dpi(96)
, m_leftMargin(32.f)
, m_rightMargin(35.f * 3)
, m_bMouseTracking(false)
, m_nTrackingButton(-1)
, m_nHitTest(HTNOWHERE)
{
}
void CTitleBarHelper::Init(CWnd *pWnd)
{
m_pWnd = pWnd;
}
int CTitleBarHelper::GetTopMargin() const
{
return m_maximized ?
GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER) : 0;
}
void CTitleBarHelper::DrawIcon(CWnd* pWnd, CDC& dc)
{
HICON hIcon = (HICON)pWnd->SendMessage(WM_GETICON, ICON_SMALL2, 0);
if (hIcon == nullptr)
hIcon = (HICON)GetClassLongPtr(pWnd->m_hWnd, GCLP_HICONSM);
if (hIcon == nullptr)
return;
const int topMargin = GetTopMargin();
const int height = m_size.cy - topMargin;
const int cx = PointToPixel(12.f);
const int cy = PointToPixel(12.f);
const int x = (PointToPixel(m_leftMargin) - cx) / 2;
const int y = (height - cy) / 2 + topMargin;
DrawIconEx(dc.m_hDC, x, y, hIcon,
cx, cy, 0, nullptr, DI_NORMAL);
}
static void DrawTopRightEdgeWithCurve(Gdiplus::Graphics& graphics, Gdiplus::Pen& pen, Gdiplus::Rect rect, int cornerRadius)
{
Gdiplus::GraphicsPath path;
path.AddLine(rect.X, rect.Y, rect.X + rect.Width - cornerRadius, rect.Y);
path.AddArc(rect.X + rect.Width - cornerRadius, rect.Y, cornerRadius, cornerRadius, 270, 90);
path.AddLine(rect.X + rect.Width, rect.Y + cornerRadius, rect.X + rect.Width, rect.Y + rect.Height);
graphics.DrawPath(&pen, &path);
}
static void DrawRoundedRectangle(Gdiplus::Graphics& graphics, Gdiplus::Pen& pen, Gdiplus::Rect rect, int cornerRadius)
{
Gdiplus::GraphicsPath path;
path.AddArc(rect.X, rect.Y, cornerRadius, cornerRadius, 180, 90);
path.AddArc(rect.X + rect.Width - cornerRadius, rect.Y, cornerRadius, cornerRadius, 270, 90);
path.AddArc(rect.X + rect.Width - cornerRadius, rect.Y + rect.Height - cornerRadius, cornerRadius, cornerRadius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - cornerRadius, cornerRadius, cornerRadius, 90, 90);
path.CloseFigure();
graphics.DrawPath(&pen, &path);
}
void CTitleBarHelper::DrawButtons(CDC& dc, COLORREF textColor, COLORREF backColor)
{
Gdiplus::Graphics graphics(dc.m_hDC);
CRect rcIcons[3], rcButtons[3];
const float buttonWidth = PointToPixelF(m_rightMargin) / 3.f;
const int iconSize = PointToPixel(6.75);
for (int i = 0; i < 3; i++)
{
rcButtons[i] = GetButtonRect(i);
rcIcons[i] = rcButtons[i];
rcIcons[i].left = static_cast<int>(rcIcons[i].left + (buttonWidth - iconSize) / 2);
rcIcons[i].right = static_cast<int>(rcIcons[i].left + iconSize);
rcIcons[i].top = static_cast<int>(rcIcons[i].top + (rcButtons[i].Height() - iconSize) / 2);
rcIcons[i].bottom = static_cast<int>(rcIcons[i].top + iconSize);
COLORREF colorref;
Gdiplus::Color color;
if (m_nTrackingButton == i)
colorref = (i == 2) ? RGB(0xE9, 0x48, 0x56) : GetIntermediateColor(backColor, GetSysColor(COLOR_WINDOW), 0.66f);
else
colorref = backColor;
color.SetFromCOLORREF(colorref);
Gdiplus::SolidBrush brush(color);
graphics.FillRectangle(&brush, rcButtons[i].left, rcButtons[i].top, rcButtons[i].Width(), rcButtons[i].Height());
}
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
Gdiplus::Color penColor;
penColor.SetFromCOLORREF(textColor);
Gdiplus::Pen pen(penColor, PointToPixelF(0.75));
// minimize button
const int y = (rcIcons[0].top + rcIcons[0].bottom) / 2;
graphics.DrawLine(&pen, Gdiplus::Point(rcIcons[0].left, y), Gdiplus::Point(rcIcons[0].right, y));
const int r = PointToPixel(0.75);
if (m_maximized)
{
// maxmize button
DrawTopRightEdgeWithCurve(graphics, pen, Gdiplus::Rect(rcIcons[1].left + r, rcIcons[1].top - r, rcIcons[1].Width() - 2 * r, rcIcons[1].Height() - 2 * r), r * 3);
DrawRoundedRectangle(graphics, pen, Gdiplus::Rect(rcIcons[1].left - r, rcIcons[1].top + r, rcIcons[1].Width() - 2 * r, rcIcons[1].Height() - 2 * r), r);
}
else
{
// restore button
DrawRoundedRectangle(graphics, pen, Gdiplus::Rect(rcIcons[1].left, rcIcons[1].top, rcIcons[1].Width(), rcIcons[1].Height()), r);
}
// close button
penColor.SetFromCOLORREF(m_nTrackingButton != 2 ? textColor : RGB(255, 255, 255));
Gdiplus::Pen pen2(penColor, PointToPixelF(0.75));
graphics.DrawLine(&pen2, Gdiplus::Point(rcIcons[2].left, rcIcons[2].top), Gdiplus::Point(rcIcons[2].right, rcIcons[2].bottom));
graphics.DrawLine(&pen2, Gdiplus::Point(rcIcons[2].left, rcIcons[2].bottom), Gdiplus::Point(rcIcons[2].right, rcIcons[2].top));
}
CRect CTitleBarHelper::GetButtonRect(int button) const
{
CRect rcPart;
const float buttonWidth = PointToPixelF(m_rightMargin) / 3.f;
rcPart.top = GetTopMargin();
rcPart.bottom = m_size.cy;
rcPart.left = static_cast<int>(m_size.cx - (3 - button) * buttonWidth);
rcPart.right = static_cast<int>(rcPart.left + buttonWidth + 0.5);
return rcPart;
}
void CTitleBarHelper::SetSize(int cx, int cy)
{
m_size = CSize(cx, cy);
CClientDC dc(m_pWnd);
m_dpi = dc.GetDeviceCaps(LOGPIXELSX);
m_pWnd->GetWindowRect(&m_rc);
}
LRESULT CTitleBarHelper::OnNcHitTest(CPoint pt)
{
if (!m_pWnd)
return HTNOWHERE;
CClientDC dc(m_pWnd);
const int leftMargin = PointToPixel(m_leftMargin);
const int rightMargin = PointToPixel(m_rightMargin);
const int borderWidth = PointToPixel(6);
CRect rc;
m_pWnd->GetWindowRect(&rc);
if (pt.y < rc.top + borderWidth)
{
if (pt.x < rc.left + borderWidth)
return HTTOPLEFT;
else if (rc.right - borderWidth <= pt.x)
return HTTOPRIGHT;
return HTTOP;
}
if (!m_maximized)
{
if (pt.x < rc.left + borderWidth)
return HTLEFT;
if (rc.right - borderWidth <= pt.x)
return HTRIGHT;
}
if (pt.x < rc.left + leftMargin)
{
if (pt.x > rc.left + leftMargin * 2 / 3)
return HTCAPTION;
return HTSYSMENU;
}
for (int i = 0; i < 3; i++)
{
static const int htbuttons[]{ HTMINBUTTON, HTMAXBUTTON, HTCLOSE };
CRect rcButton = GetButtonRect(i);
m_pWnd->ClientToScreen(&rcButton);
if (PtInRect(&rcButton, pt))
return htbuttons[i];
}
return HTCAPTION;
}
void CTitleBarHelper::OnNcMouseMove(UINT nHitTest, CPoint point)
{
if (!m_bMouseTracking)
{
TRACKMOUSEEVENT tme = { sizeof TRACKMOUSEEVENT, TME_LEAVE | TME_NONCLIENT, m_pWnd->m_hWnd };
TrackMouseEvent(&tme);
m_bMouseTracking = true;
}
int i = -1;
if (nHitTest == HTMINBUTTON)
i = 0;
else if (nHitTest == HTMAXBUTTON)
i = 1;
else if (nHitTest == HTCLOSE)
i = 2;
for (int button : {i, m_nTrackingButton})
{
if (button != -1)
{
CRect rcPart = GetButtonRect(button);
m_pWnd->InvalidateRect(&rcPart, false);
}
}
m_nTrackingButton = i;
}
void CTitleBarHelper::OnNcMouseLeave()
{
TRACKMOUSEEVENT tme = { sizeof(TRACKMOUSEEVENT), TME_LEAVE | TME_CANCEL, m_pWnd->m_hWnd };
TrackMouseEvent(&tme);
m_bMouseTracking = false;
if (m_nTrackingButton >= 0)
{
CRect rcPart = GetButtonRect(m_nTrackingButton);
m_pWnd->InvalidateRect(&rcPart, false);
}
m_nTrackingButton = -1;
}
void CTitleBarHelper::OnNcLButtonDblClk(UINT nHitTest, CPoint point)
{
if (nHitTest != HTMINBUTTON && nHitTest != HTMAXBUTTON && nHitTest != HTCLOSE && nHitTest != HTSYSMENU)
AfxGetMainWnd()->SendMessage(WM_NCLBUTTONDBLCLK, nHitTest, MAKELPARAM(point.x, point.y));
else if (nHitTest == HTSYSMENU)
AfxGetMainWnd()->PostMessage(WM_CLOSE);
}
void CTitleBarHelper::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
if (nHitTest != HTMINBUTTON && nHitTest != HTMAXBUTTON && nHitTest != HTCLOSE && nHitTest != HTSYSMENU)
AfxGetMainWnd()->SendMessage(WM_NCLBUTTONDOWN, nHitTest, MAKELPARAM(point.x, point.y));
else if (nHitTest == HTSYSMENU)
ShowSysMenu(CPoint{ point.x + 1, point.y });
else if (nHitTest == HTMINBUTTON || nHitTest == HTMAXBUTTON || nHitTest == HTCLOSE)
m_nHitTest = nHitTest;
}
void CTitleBarHelper::OnNcLButtonUp(UINT nHitTest, CPoint point)
{
if (nHitTest != HTMINBUTTON && nHitTest != HTMAXBUTTON && nHitTest != HTCLOSE && nHitTest != HTSYSMENU)
AfxGetMainWnd()->SendMessage(WM_NCLBUTTONUP, nHitTest, MAKELPARAM(point.x, point.y));
else if (m_nHitTest != HTNOWHERE && m_nHitTest == nHitTest)
{
if (nHitTest == HTMINBUTTON)
AfxGetMainWnd()->SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
else if (nHitTest == HTMAXBUTTON)
AfxGetMainWnd()->SendMessage(WM_SYSCOMMAND, m_maximized ? SC_RESTORE : SC_MAXIMIZE, 0);
else if (nHitTest == HTCLOSE)
AfxGetMainWnd()->SendMessage(WM_SYSCOMMAND, SC_CLOSE, 0);
}
m_nHitTest = HTNOWHERE;
}
void CTitleBarHelper::OnNcRButtonDown(UINT nHitTest, CPoint point)
{
}
void CTitleBarHelper::OnNcRButtonUp(UINT nHitTest, CPoint point)
{
ShowSysMenu(point);
}
void CTitleBarHelper::ShowSysMenu(CPoint point)
{
CMenu* pSysMenu = AfxGetMainWnd()->GetSystemMenu(FALSE);
bool maximized = AfxGetMainWnd()->IsZoomed();
pSysMenu->EnableMenuItem(SC_MAXIMIZE,(!maximized ? MF_ENABLED : MF_DISABLED) | MF_BYCOMMAND);
pSysMenu->EnableMenuItem(SC_SIZE,(!maximized ? MF_ENABLED : MF_DISABLED) | MF_BYCOMMAND);
pSysMenu->EnableMenuItem(SC_RESTORE, (maximized ? MF_ENABLED : MF_DISABLED) | MF_BYCOMMAND);
BOOL cmd = pSysMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, point.x, point.y, AfxGetMainWnd(), nullptr);
if (cmd)
AfxGetMainWnd()->PostMessage(WM_SYSCOMMAND, cmd, 0);
}
COLORREF CTitleBarHelper::GetIntermediateColor(COLORREF a, COLORREF b, float ratio)
{
const uint8_t R = static_cast<int8_t>((GetRValue(a) - GetRValue(b)) * ratio) + GetRValue(b);
const uint8_t G = static_cast<int8_t>((GetGValue(a) - GetGValue(b)) * ratio) + GetGValue(b);
const uint8_t B = static_cast<int8_t>((GetBValue(a) - GetBValue(b)) * ratio) + GetBValue(b);
return RGB(R, G, B);
}