Repository navigation
Expand file tree
/
Copy pathPathBar.cpp
More file actions
449 lines (408 loc) · 13.6 KB
/
Copy pathPathBar.cpp
File metadata and controls
449 lines (408 loc) · 13.6 KB
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
#include "stdafx.h"
#include "spacemonger.h"
#include "PathBar.h"
#include "FolderView.h"
#include "FileTypes.h"
#include "TextFormat.h"
#include "Theme.h"
#include "Lang.h"
#include <uxtheme.h>
#pragma comment(lib, "uxtheme.lib")
namespace {
const UINT kSearchId = 1;
const UINT_PTR kSearchTimer = 1;
const UINT kSearchDelayMs = 180; // search once typing pauses, not per key
const wchar_t kSeparator[] = L" › "; // single right-pointing angle quotation mark
const wchar_t kEllipsis[] = L"…";
CFolderView *View()
{
return (CFolderView *)theApp.m_view;
}
// The edit's own cue banner is nearly invisible in dark mode, so the hint is
// drawn here, after the edit has painted, while the box is empty.
LRESULT CALLBACK SearchHintProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR)
{
LRESULT result = ::DefSubclassProc(hwnd, msg, wp, lp);
if (msg == WM_PAINT && ::GetWindowTextLengthW(hwnd) == 0) {
HDC dc = ::GetDC(hwnd);
HGDIOBJ oldFont = ::SelectObject(dc, (HFONT)::SendMessageW(hwnd, WM_GETFONT, 0, 0));
const SM_Theme &theme = SM_CurrentTheme();
::SetTextColor(dc, theme.dark ? RGB(165, 165, 165) : theme.grayText);
::SetBkMode(dc, TRANSPARENT);
RECT rc;
::GetClientRect(hwnd, &rc);
rc.left += LOWORD(::SendMessageW(hwnd, EM_GETMARGINS, 0, 0)) + 1;
::DrawTextW(dc, CurLang->search_cue, -1, &rc, DT_LEFT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX | DT_END_ELLIPSIS);
::SelectObject(dc, oldFont);
::ReleaseDC(hwnd, dc);
}
return result;
}
int TextWidth(CDC &dc, const std::wstring &text)
{
SIZE size = { 0, 0 };
::GetTextExtentPoint32W(dc.m_hDC, text.c_str(), (int)text.size(), &size);
return size.cx;
}
}
BEGIN_MESSAGE_MAP(CPathBar, CWnd)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_WM_MOUSEMOVE()
ON_WM_MOUSELEAVE()
ON_WM_LBUTTONUP()
ON_WM_SETCURSOR()
ON_WM_CTLCOLOR()
ON_EN_CHANGE(kSearchId, OnSearchChanged)
ON_EN_SETFOCUS(kSearchId, OnSearchFocusChanged)
ON_EN_KILLFOCUS(kSearchId, OnSearchFocusChanged)
ON_WM_TIMER()
END_MESSAGE_MAP()
CPathBar::CPathBar()
: m_searchBox(0, 0, 0, 0), m_font(NULL), m_iconFont(NULL), m_dpi(96), m_hot(-1),
m_tracking(false), m_searching(false), m_matches(0)
{
}
CPathBar::~CPathBar()
{
if (m_iconFont != NULL) ::DeleteObject(m_iconFont);
}
BOOL CPathBar::Create(CWnd *parent)
{
CString cls = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, ::LoadCursor(NULL, IDC_ARROW));
if (!CWnd::Create(cls, L"", WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN, CRect(0, 0, 1, 1), parent, 0x5101))
return FALSE;
if (!m_search.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
CRect(0, 0, 1, 1), this, kSearchId))
return FALSE;
::SetWindowSubclass(m_search.m_hWnd, SearchHintProc, 0, 0);
Relabel();
ApplyTheme();
return TRUE;
}
void CPathBar::Relabel(void)
{
if (::IsWindow(m_search.m_hWnd)) m_search.Invalidate(); // the hint follows the language
Invalidate();
}
void CPathBar::ApplyTheme(void)
{
if (::IsWindow(m_search.m_hWnd)) {
::SetWindowTheme(m_search.m_hWnd, SM_CurrentTheme().dark ? L"DarkMode_CFD" : NULL, NULL);
m_search.Invalidate();
}
Invalidate();
}
void CPathBar::SetUiFont(HFONT font, UINT dpi)
{
m_font = font;
m_dpi = dpi != 0 ? dpi : 96;
if (m_iconFont != NULL) ::DeleteObject(m_iconFont);
m_iconFont = ::CreateFontW(-Scale(14), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
DEFAULT_PITCH, SM_IconFontFace());
if (::IsWindow(m_search.m_hWnd)) m_search.SendMessage(WM_SETFONT, (WPARAM)font, TRUE);
LayoutChildren();
Invalidate();
}
int CPathBar::GetDesiredHeight(void) const
{
TEXTMETRICW tm = {};
HDC dc = ::GetDC(NULL);
HGDIOBJ old = ::SelectObject(dc, m_font != NULL ? m_font : ::GetStockObject(DEFAULT_GUI_FONT));
::GetTextMetricsW(dc, &tm);
::SelectObject(dc, old);
::ReleaseDC(NULL, dc);
return tm.tmHeight + MulDiv(20, (int)m_dpi, 96);
}
void CPathBar::SetSegments(const std::vector<Segment> &segments)
{
bool same = segments.size() == m_segments.size();
for (size_t i = 0; same && i < segments.size(); ++i)
same = segments[i].folder == m_segments[i].folder && segments[i].label == m_segments[i].label;
if (same) return;
m_segments = segments;
m_hot = -1;
Invalidate();
}
void CPathBar::SetMatchCount(bool searching, ui64 matches)
{
m_searching = searching;
m_matches = matches;
Invalidate();
}
void CPathBar::FocusSearch(void)
{
if (!::IsWindow(m_search.m_hWnd)) return;
m_search.SetFocus();
m_search.SetSel(0, -1);
}
void CPathBar::ClearSearch(void)
{
if (::IsWindow(m_search.m_hWnd)) m_search.SetWindowText(L"");
}
void CPathBar::LayoutChildren(void)
{
if (!::IsWindow(m_search.m_hWnd)) return;
CRect rect;
GetClientRect(&rect);
int boxHeight = max(1, rect.Height() - Scale(8));
int boxWidth = min(Scale(340), max(Scale(160), rect.Width() / 3));
m_searchBox.SetRect(rect.right - boxWidth - Scale(8), (rect.Height() - boxHeight) / 2,
rect.right - Scale(8), (rect.Height() - boxHeight) / 2 + boxHeight);
// The edit itself is one text line high, centred in the field right of
// the magnifier.
TEXTMETRICW tm = {};
HDC dc = ::GetDC(NULL);
HGDIOBJ old = ::SelectObject(dc, m_font != NULL ? m_font : ::GetStockObject(DEFAULT_GUI_FONT));
::GetTextMetricsW(dc, &tm);
::SelectObject(dc, old);
::ReleaseDC(NULL, dc);
int editHeight = min(boxHeight - 2, (int)tm.tmHeight + Scale(2));
int editLeft = m_searchBox.left + Scale(30);
m_search.SetWindowPos(NULL, editLeft, m_searchBox.top + (boxHeight - editHeight) / 2,
max(1, m_searchBox.right - Scale(8) - editLeft), editHeight, SWP_NOZORDER | SWP_NOACTIVATE);
}
void CPathBar::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
LayoutChildren();
Invalidate();
}
BOOL CPathBar::OnEraseBkgnd(CDC *pDC)
{
return TRUE;
}
void CPathBar::OnPaint()
{
CPaintDC paint(this);
CRect rect;
GetClientRect(&rect);
if (rect.IsRectEmpty()) return;
const SM_Theme &theme = SM_CurrentTheme();
CDC dc;
dc.CreateCompatibleDC(&paint);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&paint, rect.Width(), rect.Height());
CBitmap *oldBitmap = dc.SelectObject(&bitmap);
dc.FillSolidRect(&rect, theme.face);
dc.FillSolidRect(0, rect.bottom - 1, rect.Width(), 1, theme.line);
HGDIOBJ oldFont = ::SelectObject(dc.m_hDC, m_font != NULL ? m_font : ::GetStockObject(DEFAULT_GUI_FONT));
dc.SetBkMode(TRANSPARENT);
TEXTMETRICW tm = {};
::GetTextMetricsW(dc.m_hDC, &tm);
int textTop = (rect.Height() - tm.tmHeight) / 2;
// The search field: rounded, filled like an edit, with a magnifier; the
// frame turns to the accent colour while it has the focus.
int right = rect.right;
if (!m_searchBox.IsRectEmpty()) {
const bool focused = ::IsWindow(m_search.m_hWnd) && ::GetFocus() == m_search.m_hWnd;
COLORREF frame = focused ? ::GetSysColor(COLOR_HIGHLIGHT)
: theme.dark ? RGB(105, 105, 105) : RGB(138, 138, 138);
CBrush fill(theme.editBack);
CPen pen(PS_SOLID, focused ? Scale(2) : max(1, Scale(1)), frame);
CBrush *oldBrush = dc.SelectObject(&fill);
CPen *oldPen = dc.SelectObject(&pen);
dc.RoundRect(&m_searchBox, CPoint(Scale(8), Scale(8)));
dc.SelectObject(oldPen);
dc.SelectObject(oldBrush);
if (m_iconFont != NULL) {
HGDIOBJ textFont = ::SelectObject(dc.m_hDC, m_iconFont);
const wchar_t magnifier = 0xE721; // Search
CRect icon(m_searchBox.left + Scale(8), m_searchBox.top, m_searchBox.left + Scale(28), m_searchBox.bottom);
dc.SetTextColor(focused ? ::GetSysColor(COLOR_HIGHLIGHT) : theme.text);
::DrawTextW(dc.m_hDC, &magnifier, 1, &icon, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX);
::SelectObject(dc.m_hDC, textFont);
}
right = m_searchBox.left - Scale(10);
}
// Match count left of the search box.
if (m_searching) {
CString count;
count.Format(CurLang->search_result_format, SM_FormatCount(m_matches).c_str());
int w = TextWidth(dc, (LPCWSTR)count);
dc.SetTextColor(theme.grayText);
::TextOutW(dc.m_hDC, right - w, textTop, count, count.GetLength());
right -= w + Scale(16);
}
// Legend for "By file type", if it fits beside a short path.
if (theApp.m_settings.color_scheme == 3) {
int box = Scale(10), gap = Scale(5), spacing = Scale(12);
int legendWidth = 0;
for (int i = 0; i < SM_TYPE_COUNT; ++i)
legendWidth += box + gap + TextWidth(dc, CurLang->filetype_labels[i]) + spacing;
if (legendWidth < right - Scale(200)) {
int x = right - legendWidth;
for (int i = 0; i < SM_TYPE_COUNT; ++i) {
COLORREF color, bright, dark;
SM_FileTypeColors((SM_FileType)i, color, bright, dark);
int top = (rect.Height() - box) / 2;
dc.FillSolidRect(x, top, box, box, dark);
dc.FillSolidRect(x + 1, top + 1, box - 2, box - 2, color);
x += box + gap;
dc.SetTextColor(theme.text);
const wchar_t *label = CurLang->filetype_labels[i];
::TextOutW(dc.m_hDC, x, textTop, label, (int)wcslen(label));
x += TextWidth(dc, label) + spacing;
}
right -= legendWidth + Scale(8);
}
}
// The path; when it is too long the first segment stays and the middle
// collapses into "…".
m_segmentRects.assign(m_segments.size(), CRect(0, 0, 0, 0));
int left = Scale(10);
int sepWidth = TextWidth(dc, kSeparator);
std::vector<int> widths;
int total = 0;
for (const Segment &s : m_segments) {
widths.push_back(TextWidth(dc, s.label) + Scale(8));
total += widths.back();
}
total += sepWidth * (int)max((size_t)1, m_segments.size()) - sepWidth;
size_t skipTo = 1; // first segment shown after the root
int ellipsisWidth = TextWidth(dc, kEllipsis) + sepWidth;
while (m_segments.size() > 2 && skipTo < m_segments.size() - 1 && left + total > right) {
total -= widths[skipTo] + sepWidth;
if (skipTo == 1) total += ellipsisWidth;
++skipTo;
}
int x = left;
for (size_t i = 0; i < m_segments.size(); ++i) {
if (i > 0 && i < skipTo && skipTo > 1) {
if (i == 1) {
dc.SetTextColor(theme.grayText);
::TextOutW(dc.m_hDC, x, textTop, kSeparator, (int)wcslen(kSeparator));
x += sepWidth;
::TextOutW(dc.m_hDC, x, textTop, kEllipsis, 1);
x += TextWidth(dc, kEllipsis);
}
continue;
}
if (i > 0) {
dc.SetTextColor(theme.grayText);
::TextOutW(dc.m_hDC, x, textTop, kSeparator, (int)wcslen(kSeparator));
x += sepWidth;
}
CRect segmentRect(x, Scale(4), min(x + widths[i], right), rect.bottom - Scale(4));
bool last = i + 1 == m_segments.size();
if ((int)i == m_hot && !last) {
CBrush hot(theme.dark ? RGB(70, 70, 70) : RGB(218, 218, 218));
CPen none(PS_NULL, 0, RGB(0, 0, 0));
CBrush *oldBrush = dc.SelectObject(&hot);
CPen *oldPen = dc.SelectObject(&none);
dc.RoundRect(&segmentRect, CPoint(Scale(6), Scale(6)));
dc.SelectObject(oldPen);
dc.SelectObject(oldBrush);
}
dc.SetTextColor(last ? theme.text : theme.grayText);
CRect textRect(segmentRect.left + Scale(4), textTop, segmentRect.right, textTop + tm.tmHeight);
::DrawTextW(dc.m_hDC, m_segments[i].label.c_str(), (int)m_segments[i].label.size(), &textRect,
DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_END_ELLIPSIS);
m_segmentRects[i] = segmentRect;
x += widths[i];
}
::SelectObject(dc.m_hDC, oldFont);
paint.BitBlt(0, 0, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);
dc.SelectObject(oldBitmap);
}
int CPathBar::HitSegment(CPoint point) const
{
// The last segment is where you are; only the ones above it are links.
for (size_t i = 0; i + 1 < m_segmentRects.size(); ++i)
if (!m_segmentRects[i].IsRectEmpty() && m_segmentRects[i].PtInRect(point)) return (int)i;
return -1;
}
void CPathBar::OnMouseMove(UINT flags, CPoint point)
{
if (!m_tracking) {
TRACKMOUSEEVENT tme = { sizeof(tme), TME_LEAVE, m_hWnd, 0 };
m_tracking = ::TrackMouseEvent(&tme) != FALSE;
}
int hot = HitSegment(point);
if (hot != m_hot) {
m_hot = hot;
Invalidate();
}
CWnd::OnMouseMove(flags, point);
}
void CPathBar::OnMouseLeave()
{
m_tracking = false;
if (m_hot != -1) {
m_hot = -1;
Invalidate();
}
}
BOOL CPathBar::OnSetCursor(CWnd *wnd, UINT hitTest, UINT message)
{
if (wnd == this && hitTest == HTCLIENT) {
CPoint point;
::GetCursorPos(&point);
ScreenToClient(&point);
::SetCursor(::LoadCursor(NULL, HitSegment(point) >= 0 ? IDC_HAND : IDC_ARROW));
return TRUE;
}
return CWnd::OnSetCursor(wnd, hitTest, message);
}
void CPathBar::OnLButtonUp(UINT flags, CPoint point)
{
int hit = HitSegment(point);
if (hit >= 0 && View() != NULL) View()->ZoomTo(m_segments[hit].folder);
else if (m_searchBox.PtInRect(point)) FocusSearch(); // the magnifier or the frame
CWnd::OnLButtonUp(flags, point);
}
HBRUSH CPathBar::OnCtlColor(CDC *pDC, CWnd *wnd, UINT ctlColor)
{
const SM_Theme &theme = SM_CurrentTheme();
if (ctlColor == CTLCOLOR_EDIT) {
pDC->SetTextColor(theme.text);
pDC->SetBkColor(theme.editBack);
return theme.editBrush;
}
return CWnd::OnCtlColor(pDC, wnd, ctlColor);
}
void CPathBar::OnSearchChanged()
{
// Clearing the box ends the search at once; typing waits for a pause.
m_search.Invalidate(); // the hint appears or goes as a whole
if (m_search.GetWindowTextLength() == 0) {
KillTimer(kSearchTimer);
if (View() != NULL) View()->SetSearch(L"");
return;
}
SetTimer(kSearchTimer, kSearchDelayMs, NULL);
}
void CPathBar::OnSearchFocusChanged()
{
InvalidateRect(&m_searchBox, FALSE);
}
void CPathBar::OnTimer(UINT_PTR id)
{
if (id != kSearchTimer) {
CWnd::OnTimer(id);
return;
}
KillTimer(kSearchTimer);
CString text;
m_search.GetWindowText(text);
text.Trim();
if (View() != NULL) View()->SetSearch((LPCWSTR)text);
}
BOOL CPathBar::PreTranslateMessage(MSG *msg)
{
if (msg->message == WM_KEYDOWN && msg->hwnd == m_search.m_hWnd) {
if (msg->wParam == VK_ESCAPE) {
ClearSearch();
if (View() != NULL) View()->SetFocus();
return TRUE;
}
if (msg->wParam == VK_RETURN) {
OnTimer(kSearchTimer); // search now
if (View() != NULL) View()->SetFocus();
return TRUE;
}
}
return CWnd::PreTranslateMessage(msg);
}