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
|
#pragma comment(linker, "\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#include <windows.h>
#include <CommCtrl.h>
#include "../res/resource.h"
#include "layout.h"
#include <functional>
#include <unordered_map>
#include <string>
// namespace wnin {
// namespace _ {
// using CallbackFn = std::function<void()>;
// std::map<HWND, std::map<WORD, CallbackFn>> handlers;
// std::map<HWND, lay_id> lIds;
// NOTIFYICONDATA niData = { 0 };
// void
// ShowNotificationIcon()
// {
// Shell_NotifyIconA(NIM_ADD, &_::niData);
// Shell_NotifyIconA(NIM_SETVERSION, &_::niData);
// }
// void
// HideNotificationIcon()
// {
// Shell_NotifyIconA(NIM_DELETE, &_::niData);
// }
// lay_context ctx;
// lay_id root;
// LRESULT CALLBACK
// WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
// {
// switch (msg) {
// case WM_CLOSE:
// DestroyWindow(hwnd);
// break;
// case WM_DESTROY:
// Shell_NotifyIconA(NIM_DELETE, &niData);
// lay_destroy_context(&ctx);
// PostQuitMessage(0);
// break;
// case WM_SIZE:
// if (wParam == SIZE_MINIMIZED) {
// ShowNotificationIcon();
// ShowWindow(hwnd, false);
// }
// else {
// lay_set_size_xy(&_::ctx, _::root, LOWORD(lParam), HIWORD(lParam));
// lay_run_context(&_::ctx);
// for (auto &lId : lIds) {
// lay_vec4 rect = lay_get_rect(&_::ctx, lId.second);
// SetWindowPos(lId.first, HWND_TOP,
// rect[0],
// rect[1],
// rect[2],
// rect[3],
// SWP_NOZORDER
// );
// }
// RedrawWindow(hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN);
// }
// break;
// case WM_COMMAND:
// if (handlers.find((HWND)lParam) != handlers.end()) {
// auto handler = handlers[(HWND)lParam];
// if (handler.find(HIWORD(wParam)) != handler.end()) {
// auto cb = handler[HIWORD(wParam)];
// cb();
// }
// }
// break;
// case WM_NOTIFY:
// break;
// case WM_APP + 1:
// if (LOWORD(lParam) == NIN_SELECT) {
// HideNotificationIcon();
// ShowWindow(hwnd, true);
// SetForegroundWindow(hwnd);
// SetActiveWindow(hwnd);
// }
// break;
// case WM_CTLCOLORSTATIC:
// return (LONG)GetStockObject(WHITE_BRUSH);
// case WM_GETMINMAXINFO: {
// MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;
// mmInfo->ptMinTrackSize.x = 400;
// mmInfo->ptMinTrackSize.y = 200;
// break;
// }
// default:
// return DefWindowProc(hwnd, msg, wParam, lParam);
// }
// return 0;
// }
// }
// void
// Callback(HWND hwnd, WORD ev, std::function<void()> cb)
// {
// _::handlers[hwnd][ev] = cb;
// }
// HWND
// Window(string title, string className, HINSTANCE hInstance)
// {
// WNDCLASSEX wc;
// wc.cbSize = sizeof(WNDCLASSEX);
// wc.style = 0;
// wc.lpfnWndProc = _::WndProc;
// wc.cbClsExtra = 0;
// wc.cbWndExtra = 0;
// wc.hInstance = hInstance;
// wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
// wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
// wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
// wc.lpszMenuName = nullptr;
// wc.lpszClassName = className.c_str();
// wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
// RegisterClassEx(&wc);
// lay_init_context(&_::ctx);
// _::root = lay_item(&_::ctx);
// lay_set_contain(&_::ctx, _::root, LAY_COLUMN);
// HWND result = CreateWindowA(className.c_str(),
// title.c_str(),
// WS_OVERLAPPEDWINDOW,
// CW_USEDEFAULT,
// CW_USEDEFAULT,
// CW_USEDEFAULT,
// CW_USEDEFAULT,
// nullptr,
// nullptr,
// hInstance,
// nullptr);
// _::niData.cbSize = sizeof(_::niData);
// _::niData.uID = 12345;
// _::niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
// _::niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_MY_ICON));
// _::niData.hWnd = result;
// _::niData.uCallbackMessage = WM_APP+1;
// _::niData.uVersion = NOTIFYICON_VERSION_4;
// return result;
// }
// bool
// UpdateWindow(HWND hwnd)
// {
// MSG msg;
// if (GetMessage(&msg, nullptr, 0, 0) > 0) {
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// return true;
// }
// return false;
// }
// void
// ShowWindow(HWND hwnd)
// {
// ShowWindow(hwnd, true);
// EnumChildWindows(
// hwnd,
// [](HWND hwnd, LPARAM lParam) -> BOOL {
// HFONT guiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
// SendMessage(hwnd, WM_SETFONT, (WPARAM)guiFont, MAKELPARAM(TRUE, 0));
// return TRUE;
// },
// 0);
// }
// HWND
// Button(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
// {
// lay_id lId = lay_item(&_::ctx);
// lay_insert(&_::ctx, parent, lId);
// lay_set_size_xy(&_::ctx, lId, w, h);
// lay_set_contain(&_::ctx, lId, contain);
// lay_set_behave(&_::ctx, lId, behave);
// HWND result = CreateWindowExA(0,
// WC_BUTTONA,
// title.c_str(),
// WS_VISIBLE | WS_CHILD,
// 0, 0, 0, 0,
// hwnd,
// nullptr,
// nullptr,
// nullptr);
// _::lIds[result] = lId;
// return result;
// }
// HWND
// ListBox(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
// {
// lay_id lId = lay_item(&_::ctx);
// lay_insert(&_::ctx, parent, lId);
// lay_set_size_xy(&_::ctx, lId, w, h);
// lay_set_contain(&_::ctx, lId, contain);
// lay_set_behave(&_::ctx, lId, behave);
// HWND result = CreateWindowExA(0,
// WC_LISTBOXA,
// "",
// WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,
// 0, 0, 0, 0,
// hwnd,
// nullptr,
// nullptr,
// nullptr);
// _::lIds[result] = lId;
// return result;
// }
// void
// ListAddString(HWND hwnd, string str)
// {
// SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)str.c_str());
// }
// int
// ListGetSelectedIndex(HWND hwnd)
// {
// int sel = SendMessage(hwnd, LB_GETCURSEL, 0, 0);
// return sel;
// }
// int ListFindString(HWND hwnd, string str)
// {
// return SendMessageA(hwnd, LB_FINDSTRINGEXACT, -1, (LPARAM)str.c_str());
// }
// string
// ListGetText(HWND hwnd, int index)
// {
// char buffer[1024];
// SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)buffer);
// return string(buffer);
// }
// void
// ListClear(HWND hwnd)
// {
// SendMessageA(hwnd, LB_RESETCONTENT, 0, 0);
// }
// void ListRemove(HWND hwnd, int index)
// {
// SendMessageA(hwnd, LB_DELETESTRING, index, 0);
// }
// HWND
// ListView(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
// {
// lay_id lId = lay_item(&_::ctx);
// lay_insert(&_::ctx, parent, lId);
// lay_set_size_xy(&_::ctx, lId, w, h);
// lay_set_contain(&_::ctx, lId, contain);
// lay_set_behave(&_::ctx, lId, behave);
// HWND result = CreateWindowExA(0,
// WC_LISTVIEWA,
// "",
// WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,
// 0, 0, 0, 0,
// hwnd,
// nullptr,
// nullptr,
// nullptr);
// _::lIds[result] = lId;
// return result;
// }
// HWND
// CheckBox(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
// {
// lay_id lId = lay_item(&_::ctx);
// lay_insert(&_::ctx, parent, lId);
// lay_set_size_xy(&_::ctx, lId, w, h);
// lay_set_contain(&_::ctx, lId, contain);
// lay_set_behave(&_::ctx, lId, behave);
// HWND result = CreateWindowExA(0,
// WC_BUTTONA,
// title.c_str(),
// WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
// 0, 0, 0, 0,
// hwnd,
// nullptr,
// nullptr,
// nullptr);
// _::lIds[result] = lId;
// return result;
// }
// void SetStyle(HWND hwnd, DWORD style)
// {
// SetWindowLongPtrA(hwnd, GWL_STYLE, style);
// }
// DWORD GetStyle(HWND hwnd)
// {
// return GetWindowLongPtrA(hwnd, GWL_STYLE);
// }
// void AddStyle(HWND hwnd, DWORD style)
// {
// SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) | style);
// }
// void RemoveStyle(HWND hwnd, DWORD style)
// {
// SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) & (~style));
// }
// }
namespace win
{
struct Window;
struct Hwnd
{
HWND hwnd;
lay_id lId;
Window *window;
Hwnd() {}
Hwnd(Window *window, Hwnd *parent, LPCSTR className, LPCSTR windowName, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave);
void setStyle(DWORD style)
{
SetWindowLongPtrA(hwnd, GWL_STYLE, style);
}
DWORD getStyle()
{
return GetWindowLongPtrA(hwnd, GWL_STYLE);
}
void addStyle(DWORD style)
{
SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() | style);
}
void removeStyle(DWORD style)
{
SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() & (~style));
}
};
struct Window : Hwnd
{
private:
NOTIFYICONDATAA niData = { 0 };
static LRESULT CALLBACK
WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Window *window = (Window*)GetWindowLongPtrA(hwnd, 0);
if (window == nullptr)
return DefWindowProc(hwnd, msg, wParam, lParam);
bool defaultHandler = false;
switch (msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
Shell_NotifyIconA(NIM_DELETE, &window->niData);
lay_destroy_context(&window->ctx);
PostQuitMessage(0);
break;
case WM_SIZE:
if (wParam == SIZE_MINIMIZED) {
//TODO: auslagen
//ShowNotificationIcon();
ShowWindow(hwnd, false);
}
else {
lay_set_size_xy(&window->ctx, window->lId, LOWORD(lParam), HIWORD(lParam));
lay_run_context(&window->ctx);
//RedrawWindow(hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN);
}
break;
case WM_NOTIFY:
break;
case WM_APP + 1:
if (LOWORD(lParam) == NIN_SELECT) {
//TODO: auslagern
//HideNotificationIcon();
ShowWindow(hwnd, true);
SetForegroundWindow(hwnd);
SetActiveWindow(hwnd);
}
break;
case WM_CTLCOLORSTATIC:
return (LONG)GetStockObject(WHITE_BRUSH);
case WM_GETMINMAXINFO: {
MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;
mmInfo->ptMinTrackSize.x = 400;
mmInfo->ptMinTrackSize.y = 200;
break;
}
default:
defaultHandler = true;
break;
}
for (auto handler : window->handlers[msg])
handler(hwnd, msg, wParam, lParam);
if (defaultHandler)
return DefWindowProc(hwnd, msg, wParam, lParam);
else
return 0;
}
public:
lay_context ctx;
std::unordered_map<UINT,
std::vector<
std::function<void(HWND, UINT, WPARAM, LPARAM)>>> handlers;
Window(std::string title, std::string className, HINSTANCE hInstance)
{
WNDCLASSEXA wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(Window*);
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = nullptr;
wc.lpszClassName = className.c_str();
wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
RegisterClassExA(&wc);
lay_init_context(&ctx);
lId = lay_item(&ctx);
lay_set_contain(&ctx, lId, LAY_COLUMN);
hwnd = CreateWindowA(className.c_str(),
title.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
nullptr,
nullptr,
hInstance,
nullptr);
SetWindowLongPtrA(hwnd, 0, (LONG_PTR)this);
niData.cbSize = sizeof(niData);
niData.uID = 12345;
niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_MY_ICON));
niData.hWnd = hwnd;
niData.uCallbackMessage = WM_APP+1;
niData.uVersion = NOTIFYICON_VERSION_4;
}
bool update()
{
MSG msg;
if (GetMessage(&msg, nullptr, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
return true;
}
return false;
}
void show()
{
ShowWindow(hwnd, true);
EnumChildWindows(
hwnd,
[](HWND hwnd, LPARAM lParam) -> BOOL {
HFONT guiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hwnd, WM_SETFONT, (WPARAM)guiFont, MAKELPARAM(TRUE, 0));
return TRUE;
},
0);
}
};
struct Button : Hwnd
{
Button(Window *window, Hwnd *parent, std::string title, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
: Hwnd(window, parent, WC_BUTTONA, title.c_str(), w, h, contain, behave)
{
window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if ((HWND)lParam == this->hwnd && HIWORD(wParam) == BN_CLICKED)
for (auto handler : this->onClickHandlers)
handler();
});
}
void onClick(std::function<void()> cb)
{
onClickHandlers.push_back(cb);
}
private:
std::vector<std::function<void()>> onClickHandlers;
};
struct CheckBox : Hwnd
{
CheckBox(Window *window, Hwnd *parent, std::string title, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
: Hwnd(window, parent, WC_BUTTONA, title.c_str(), w, h, contain, behave)
{
addStyle(BS_CHECKBOX);
window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if ((HWND)lParam == this->hwnd && HIWORD(wParam) == BN_CLICKED)
SendMessageA(this->hwnd, BM_SETCHECK, SendMessageA(this->hwnd, BM_GETCHECK, 0, 0) ? BST_UNCHECKED : BST_CHECKED, 0);
});
}
};
struct ListBox : Hwnd
{
ListBox(Window *window, Hwnd *parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
: Hwnd(window, parent, WC_LISTBOXA, "", w, h, contain, behave)
{
addStyle(WS_BORDER);
addStyle(WS_VSCROLL);
}
void addString(std::string str)
{
SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)str.c_str());
}
int getSelectedIndex()
{
return SendMessage(hwnd, LB_GETCURSEL, 0, 0);
}
int findString(std::string str)
{
return SendMessageA(hwnd, LB_FINDSTRINGEXACT, -1, (LPARAM)str.c_str());
}
std::string getText(int index)
{
int len = SendMessageA(hwnd, LB_GETTEXTLEN, index, 0);
std::string result;
result.reserve(len);
SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)result.data());
return result;
}
void clear()
{
SendMessageA(hwnd, LB_RESETCONTENT, 0, 0);
}
void remove(int index)
{
SendMessageA(hwnd, LB_DELETESTRING, index, 0);
}
};
}
win::Hwnd::Hwnd(Window *window, Hwnd *parent, LPCSTR className, LPCSTR windowName, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
{
this->window = window;
lId = lay_item(&window->ctx);
lay_insert(&window->ctx, parent->lId, lId);
lay_set_size_xy(&window->ctx, lId, w, h);
lay_set_contain(&window->ctx, lId, contain);
lay_set_behave(&window->ctx, lId, behave);
hwnd = CreateWindowExA(0,
className,
windowName,
WS_VISIBLE | WS_CHILD,
0,
0,
0,
0,
window->hwnd,
nullptr,
nullptr,
nullptr);
window->handlers[WM_SIZE].push_back([this](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
lay_vec4 rect = lay_get_rect(&this->window->ctx, this->lId);
SetWindowPos(this->hwnd, HWND_TOP,
rect[0],
rect[1],
rect[2],
rect[3],
SWP_NOZORDER
);
});
}
|