-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadMenu.cpp
566 lines (499 loc) · 16.3 KB
/
RadMenu.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
566
#include "Rad/Window.h"
#include "Rad/Dialog.h"
#include "Rad/Windowxx.h"
#include "Rad/Log.h"
#include "Rad/Format.h"
//#include <tchar.h>
//#include <strsafe.h>
#include "resource.h"
#include <Shlwapi.h>
#include "EditPlus.h"
#include "ListBoxPlus.h"
#include "StrUtils.h"
#include "resource.h"
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <memory>
#include <thread>
Theme g_Theme;
#define UM_ADDITEM (WM_USER + 127)
extern HINSTANCE g_hInstance;
extern HWND g_hWndDlg;
template <class R, class F, typename... Ps>
R CallWinApi(F f, Ps... args)
{
R r = {};
if (!f(args..., &r))
throw WinError({ GetLastError(), nullptr, TEXT("CallWinApi") });
return r;
}
#define IDC_EDIT 100
#define IDC_LIST 101
const int Border = 10;
LRESULT CALLBACK BuddyProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (uMsg)
{
case WM_KEYDOWN:
case WM_KEYUP:
{
const UINT vk = (UINT) (wParam);
const int cRepeat = (int) (short) LOWORD(lParam);
UINT flags = (UINT) HIWORD(lParam);
if ((GetKeyState(VK_SHIFT) & 0x8000) == 0)
{
switch (vk)
{
case VK_UP: case VK_DOWN:
case VK_HOME: case VK_END:
case VK_NEXT: case VK_PRIOR:
{
HWND hWndBuddy = GetWindow(hWnd, GW_HWNDNEXT);
SendMessage(hWndBuddy, uMsg, wParam, lParam);
return TRUE;
}
}
}
}
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
inline std::tstring GetName(const std::tstring& line)
{
LPCTSTR filename = PathFindFileName(line.c_str());
if (filename)
{
LPCTSTR fileext = PathFindExtension(filename);
if (fileext)
return std::tstring(filename, fileext - filename);
else
return filename;
}
else
return {};
}
enum class DisplayMode { LINE, FNAME };
struct Options
{
int icon_mode = -1;
DisplayMode dm = DisplayMode::LINE;
LPCTSTR file = nullptr;
LPCTSTR elements = nullptr;
bool sort = false;
bool blur = true;
void ParseCommandLine(const int argc, const LPCTSTR* argv);
};
class RootWindow : public Window
{
friend WindowManager<RootWindow>;
~RootWindow()
{
for (auto& t : m_threads)
t.join();
}
public:
static ATOM Register() { return WindowManager<RootWindow>::Register(); }
static RootWindow* Create(const Options& options) { return WindowManager<RootWindow>::Create(reinterpret_cast<LPVOID>(const_cast<Options*>(&options))); }
protected:
static void GetCreateWindow(CREATESTRUCT& cs);
static void GetWndClass(WNDCLASS& wc);
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override;
private:
BOOL OnCreate(LPCREATESTRUCT lpCreateStruct);
void OnDestroy();
void OnSetFocus(HWND hwndOldFocus);
void OnSize(UINT state, int cx, int cy);
void OnActivate(UINT state, HWND hWndActDeact, BOOL fMinimized);
UINT OnNCHitTest(int x, int y);
void OnCommand(int id, HWND hWndCtl, UINT codeNotify);
HBRUSH OnCtlColor(HDC hdc, HWND hWndChild, int type);
void OnDrawItem(const DRAWITEMSTRUCT* lpDrawItem);
static LPCTSTR ClassName() { return TEXT("RadMenu"); }
static void LoadItemsFomFileThread(std::wistream* is, const DisplayMode dm, HWND hWnd)
{
std::tstring line;
while (std::getline(*is, line))
if (!line.empty())
SendMessage(hWnd, UM_ADDITEM, WPARAM(dm), LPARAM(line.c_str()));
if (is != &std::wcin)
delete is;
}
void LoadItemsFomFile(std::wistream* is, const DisplayMode dm)
{
#if 0
std::tstring line;
while (std::getline(*is, line))
if (!line.empty())
AddItem(line.c_str(), dm);
if (is != &std::wcin)
delete is;
#else
std::thread t(LoadItemsFomFileThread, is, dm, HWND(*this));
m_threads.push_back(std::move(t));
#endif
}
HWND m_hEdit = NULL;
ListBoxOwnerDrawnFixed m_ListBox;
struct Item
{
std::shared_ptr<std::tstring> line;
std::shared_ptr<std::tstring> name;
int iIcon = -1;
};
std::vector<Item> m_items;
std::vector<std::thread> m_threads;
std::vector<std::tstring> GetSearchTerms() const;
std::tstring GetSelectedText() const;
static bool Matches(const std::vector<std::tstring>& search, const std::tstring& text);
void FillList();
void AddItemToList(const Item& i, const size_t j, const std::vector<std::tstring>& search);
Item& AddItem(const LPCTSTR line, const DisplayMode dm)
{
std::tstring name;
if (dm == DisplayMode::FNAME)
name = GetName(line);
m_items.push_back({ std::make_shared<std::tstring>(line), std::make_shared<std::tstring>(name) });
return m_items.back();
}
};
void RootWindow::GetCreateWindow(CREATESTRUCT& cs)
{
Window::GetCreateWindow(cs);
cs.lpszName = TEXT("Rad Menu");
cs.style = WS_POPUP | WS_BORDER | WS_VISIBLE;
cs.dwExStyle = WS_EX_TOPMOST | WS_EX_TOOLWINDOW;
cs.dwExStyle |= WS_EX_CONTROLPARENT;
cs.x = 100;
cs.y = 100;
cs.cx = 500;
cs.cy = 500;
}
void RootWindow::GetWndClass(WNDCLASS& wc)
{
Window::GetWndClass(wc);
wc.hbrBackground = g_Theme.brWindow;
wc.hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON1));
}
void Options::ParseCommandLine(const int argc, const LPCTSTR* argv)
{
for (int argn = 1; argn < argc; ++argn)
{
LPCTSTR arg = argv[argn];
if (lstrcmpi(arg, TEXT("/f")) == 0)
file = argv[++argn];
else if (lstrcmpi(arg, TEXT("/is")) == 0)
icon_mode = ICON_SMALL;
else if (lstrcmpi(arg, TEXT("/il")) == 0)
icon_mode = ICON_BIG;
else if (lstrcmpi(arg, TEXT("/dm")) == 0)
{
LPCTSTR mode = argv[++argn];
if (lstrcmpi(mode, TEXT("fname")) == 0)
dm = DisplayMode::FNAME;
}
else if (lstrcmpi(arg, TEXT("/e")) == 0)
elements = argv[++argn];
else if (lstrcmpi(arg, TEXT("/sort")) == 0)
sort = true;
else if (lstrcmpi(arg, TEXT("/noblur")) == 0)
blur = false;
else if (lstrcmpi(arg, TEXT("/?")) == 0)
MessageBox(NULL,
TEXT("RadMenu <options>\n")
TEXT("Where <options> are:\n")
TEXT(" /is\t\t- use small icons\n")
TEXT(" /il\t\t- use large icons\n")
TEXT(" /dm <mode>\t- display mode\n")
TEXT(" Where <mode> is one of:\n")
TEXT(" fname\t\t- display file name\n")
TEXT(" /e <elements>\t- list of options\n")
TEXT(" /sort\t\t- sort items\n")
TEXT(" /noblur\t\t- remove blur effect"),
TEXT("Rad Menu"), MB_OK | MB_ICONERROR);
else
MessageBox(NULL, Format(TEXT("Unknown argument: %s"), arg).c_str(), TEXT("Rad Menu"), MB_OK | MB_ICONERROR);
}
}
BOOL RootWindow::OnCreate(const LPCREATESTRUCT lpCreateStruct)
{
const Options& options = *reinterpret_cast<Options*>(lpCreateStruct->lpCreateParams);
SetWindowBlur(*this);
const RECT rcClient = CallWinApi<RECT>(GetClientRect, HWND(*this));
const HANDLE hFont = GetStockObject(DEFAULT_GUI_FONT);
const SIZE TextSize = GetFontSize(*this, hFont, TEXT("Mg"), 2);
RECT rc = { Border, Border, Width(rcClient) - Border, Border + TextSize.cy + Border };
m_hEdit = Edit_Create(*this, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP | ES_LEFT, rc, IDC_EDIT);
SetWindowSubclass(m_hEdit, BuddyProc, 0, 0);
Edit_SetCueBannerTextFocused(m_hEdit, TEXT("Search"), TRUE);
rc.top = rc.bottom + Border;
rc.bottom = rcClient.bottom - Border;
if (options.icon_mode != -1)
{
m_ListBox.SetIconMode(options.icon_mode);
const SIZE szIcon = m_ListBox.GetIconSize();
ImageList_Destroy(m_ListBox.SetImageList(ImageList_Create(szIcon.cx, szIcon.cy, ILC_COLOR32, 0, 100)));
}
m_ListBox.Create(*this, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_TABSTOP | LBS_NOTIFY | (options.sort ? LBS_SORT : 0), rc, IDC_LIST);
if (options.file != nullptr)
{
auto f = std::make_unique<std::wifstream>(options.file);
if (*f)
LoadItemsFomFile(f.release(), options.dm);
else
MessageBox(*this, Format(TEXT("Error opening file: %s"), options.file).c_str(), TEXT("Rad Menu"), MB_OK | MB_ICONERROR);
}
if (options.elements != nullptr)
{
const std::vector<std::tstring> a = split_unquote(options.elements, TEXT(','));
for (const auto& s : a)
if (!s.empty())
{
auto ss = std::make_shared<std::tstring>(s);
m_items.push_back({ ss, ss });
}
}
LoadItemsFomFile(&std::wcin, options.dm);
FillList();
return TRUE;
}
void RootWindow::OnDestroy()
{
ImageList_Destroy(m_ListBox.SetImageList(NULL));
PostQuitMessage(0);
}
void RootWindow::OnSetFocus(const HWND hwndOldFocus)
{
if (m_hEdit)
SetFocus(m_hEdit);
}
void RootWindow::OnSize(UINT state, int cx, int cy)
{
RECT rc;
GetWindowRect(m_hEdit, &rc);
ScreenToClient(*this, &rc);
rc.right = cx - Border;
SetWindowPos(m_hEdit, NULL, rc.left, rc.top, Width(rc), Height(rc), SWP_NOOWNERZORDER | SWP_NOZORDER);
InvalidateRect(m_hEdit, nullptr, FALSE);;
rc.top = rc.bottom + Border;
rc.bottom = cy - Border;
SetWindowPos(m_ListBox, NULL, rc.left, rc.top, Width(rc), Height(rc), SWP_NOOWNERZORDER | SWP_NOZORDER);
InvalidateRect(m_ListBox, nullptr, FALSE);;
}
void RootWindow::OnActivate(UINT state, HWND hWndActDeact, BOOL fMinimized)
{
#ifndef _DEBUG
if (state == WA_INACTIVE)
SendMessage(*this, WM_CLOSE, 0, 0);
#endif
}
UINT RootWindow::OnNCHitTest(int x, int y)
{
POINT pt = { x, y };
ScreenToClient(*this, &pt);
RECT rc;
GetClientRect(*this, &rc);
InflateRect(&rc, -5, -5);
if (pt.x > rc.right and pt.x <= rc.top)
return HTTOPRIGHT;
else if (pt.x <= rc.left and pt.x <= rc.top)
return HTTOPLEFT;
else if (pt.x > rc.right and pt.y > rc.bottom)
return HTBOTTOMRIGHT;
else if (pt.x <= rc.left and pt.y > rc.bottom)
return HTBOTTOMLEFT;
else if (pt.x > rc.right)
return HTRIGHT;
else if (pt.x <= rc.left)
return HTLEFT;
else if (pt.y > rc.bottom)
return HTBOTTOM;
else if (pt.x <= rc.top)
return HTTOP;
else
return HTCAPTION;
}
void RootWindow::OnCommand(int id, HWND hWndCtl, UINT codeNotify)
{
switch (id)
{
case IDC_EDIT:
switch (codeNotify)
{
case EN_CHANGE:
FillList();
break;
}
break;
case IDC_LIST:
switch (codeNotify)
{
case LBN_DBLCLK:
SendMessage(*this, WM_COMMAND, IDOK, 0);
break;
}
break;
case IDCANCEL:
if (Edit_GetTextLength(m_hEdit) > 0)
Edit_SetText(m_hEdit, TEXT(""));
else
SendMessage(*this, WM_CLOSE, 0, 0);
break;
case IDOK:
{
const int sel = m_ListBox.GetCurSel();
if (sel >= 0)
{
if ((GetKeyState(VK_CONTROL) & 0x8000))
std::wcout << TEXT('!');
if ((GetKeyState(VK_SHIFT) & 0x8000))
std::wcout << TEXT('+');
const int j = (int) m_ListBox.GetItemData(sel);
std::wcout << *m_items[j].line << std::endl;
SendMessage(*this, WM_CLOSE, 0, 0);
}
break;
}
}
}
HBRUSH RootWindow::OnCtlColor(HDC hDC, HWND hWndChild, int type)
{
SetTextColor(hDC, g_Theme.clrWindowText);
SetBkColor(hDC, g_Theme.clrWindow);
return g_Theme.brWindow;
}
void RootWindow::OnDrawItem(const DRAWITEMSTRUCT* lpDrawItem)
{
if (lpDrawItem->hwndItem == m_ListBox and m_ListBox.GetIconMode() >= 0 and lpDrawItem->itemID >= 0)
{
const int j = (int) m_ListBox.GetItemData(lpDrawItem->itemID);
Item& item = m_items[j];
if (m_ListBox.GetItemIconIndex(lpDrawItem->itemID) == -1)
{
if (item.iIcon == -1)
{
HICON hIcon = GetIconWithoutShortcutOverlay(item.line->c_str(), m_ListBox.GetIconMode() == ICON_BIG);
if (!hIcon)
item.iIcon = -2;
else
{
HIMAGELIST hImageList = m_ListBox.GetImageList();
item.iIcon = ImageList_AddIcon(hImageList, hIcon);
DestroyIcon(hIcon);
}
}
m_ListBox.SetItemIconIndex(lpDrawItem->itemID, item.iIcon);
}
}
SetHandled(false);
}
std::vector<std::tstring> RootWindow::GetSearchTerms() const
{
TCHAR text[1024];
Edit_GetText(m_hEdit, text, ARRAYSIZE(text));
return split_unquote(text, TEXT(' '));
}
std::tstring RootWindow::GetSelectedText() const
{
const int sel = m_ListBox.GetCurSel();
TCHAR buf[1024] = TEXT("");
if (sel >= 0)
m_ListBox.GetText(sel, buf);
return buf;
}
bool RootWindow::Matches(const std::vector<std::tstring>& search, const std::tstring& text)
{
bool found = true;
for (const auto& s : search)
if (!StrFindI(text.c_str(), s.c_str()))
{
found = false;
break;
}
return found;
}
void RootWindow::FillList()
{
SetWindowRedraw(m_ListBox, FALSE);
const std::tstring seltext = GetSelectedText();
const std::vector<std::tstring> search = GetSearchTerms();
m_ListBox.ResetContent();
size_t j = 0;
for (const auto& sp : m_items)
AddItemToList(sp, j++, search);
const int sel = m_ListBox.FindStringExact(-1, seltext.c_str());
m_ListBox.SetCurSel(sel >= 0 ? sel : 0);
SetWindowRedraw(m_ListBox, TRUE);
}
void RootWindow::AddItemToList(const Item& sp, const size_t j, const std::vector<std::tstring>& search)
{
const std::tstring& text = sp.name->empty() ? *sp.line : *sp.name;
if (Matches(search, text))
{
SendMessage(m_ListBox, WM_SETREDRAW, FALSE, 0);
const int n = m_ListBox.AddString(text.c_str());
m_ListBox.SetItemData(n, j);
m_ListBox.SetItemIconIndex(n, sp.iIcon);
SendMessage(m_ListBox, WM_SETREDRAW, TRUE, 0);
}
}
LRESULT RootWindow::HandleMessage(const UINT uMsg, const WPARAM wParam, const LPARAM lParam)
{
LRESULT ret = 0;
switch (uMsg)
{
HANDLE_MSG(WM_CREATE, OnCreate);
HANDLE_MSG(WM_DESTROY, OnDestroy);
HANDLE_MSG(WM_SETFOCUS, OnSetFocus);
HANDLE_MSG(WM_SIZE, OnSize);
HANDLE_MSG(WM_ACTIVATE, OnActivate);
HANDLE_MSG(WM_NCHITTEST, OnNCHitTest);
HANDLE_MSG(WM_COMMAND, OnCommand);
HANDLE_MSG(WM_CTLCOLOREDIT, OnCtlColor);
HANDLE_MSG(WM_CTLCOLORLISTBOX, OnCtlColor);
HANDLE_MSG(WM_DRAWITEM, OnDrawItem);
case UM_ADDITEM:
{
SetHandled(true);
const DisplayMode dm = DisplayMode(wParam);
const LPCTSTR line = reinterpret_cast<LPCTSTR>(lParam);
const size_t j = m_items.size();
const std::vector<std::tstring> search = GetSearchTerms();
AddItemToList(AddItem(line, dm), j, search);
break;
}
}
if (!IsHandled())
{
bool bHandled = false;
ret = m_ListBox.ProcessMessage(*this, uMsg, wParam, lParam, bHandled);
if (bHandled)
SetHandled(true);
}
if (!IsHandled())
ret = Window::HandleMessage(uMsg, wParam, lParam);
return ret;
}
bool Run(_In_ const LPCTSTR lpCmdLine, _In_ const int nShowCmd)
{
RadLogInitWnd(NULL, "RadMenu", L"RadMenu");
if (true)
{
g_Theme.clrWindow = RGB(0, 0, 0);
g_Theme.clrHighlight = RGB(61, 61, 61);
g_Theme.clrWindowText = RGB(250, 250, 250);
g_Theme.clrHighlightText = g_Theme.clrWindowText;
g_Theme.clrGrayText = RGB(128, 128, 128);
}
Options options;
options.ParseCommandLine(__argc, __wargv);
InitTheme();
CHECK_LE_RET(RootWindow::Register(), false);
RootWindow* prw = RootWindow::Create(options);
CHECK_LE_RET(prw != nullptr, false);
RadLogInitWnd(*prw, "RadMenu", L"RadMenu");
g_hWndDlg = *prw;
ShowWindow(*prw, nShowCmd);
return true;
}