1 #pragma comment(linker, "\"/manifestdependency:type='win32' \
\r
2 name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
\r
3 processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
\r
6 #include <CommCtrl.h>
\r
10 #include <functional>
\r
18 using CallbackFn = std::function<void()>;
\r
19 std::map<HWND, std::map<WORD, CallbackFn>> handlers;
\r
20 std::map<HWND, lay_id> lIds;
\r
22 NOTIFYICONDATA niData = { 0 };
\r
24 ShowNotificationIcon()
\r
26 Shell_NotifyIconA(NIM_ADD, &_::niData);
\r
27 Shell_NotifyIconA(NIM_SETVERSION, &_::niData);
\r
31 HideNotificationIcon()
\r
33 Shell_NotifyIconA(NIM_DELETE, &_::niData);
\r
40 WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
\r
44 DestroyWindow(hwnd);
\r
47 Shell_NotifyIconA(NIM_DELETE, &niData);
\r
48 lay_destroy_context(&ctx);
\r
52 if (wParam == SIZE_MINIMIZED) {
\r
53 ShowNotificationIcon();
\r
54 ShowWindow(hwnd, false);
\r
57 lay_set_size_xy(&_::ctx, _::root, LOWORD(lParam), HIWORD(lParam));
\r
58 lay_run_context(&_::ctx);
\r
60 for (auto &lId : lIds) {
\r
61 lay_vec4 rect = lay_get_rect(&_::ctx, lId.second);
\r
62 SetWindowPos(lId.first, HWND_TOP,
\r
70 RedrawWindow(hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN);
\r
74 if (handlers.find((HWND)lParam) != handlers.end()) {
\r
75 auto handler = handlers[(HWND)lParam];
\r
76 if (handler.find(HIWORD(wParam)) != handler.end()) {
\r
77 auto cb = handler[HIWORD(wParam)];
\r
85 if (LOWORD(lParam) == NIN_SELECT) {
\r
86 HideNotificationIcon();
\r
87 ShowWindow(hwnd, true);
\r
88 SetForegroundWindow(hwnd);
\r
89 SetActiveWindow(hwnd);
\r
92 case WM_CTLCOLORSTATIC:
\r
93 return (LONG)GetStockObject(WHITE_BRUSH);
\r
94 case WM_GETMINMAXINFO: {
\r
95 MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;
\r
96 mmInfo->ptMinTrackSize.x = 400;
\r
97 mmInfo->ptMinTrackSize.y = 200;
\r
101 return DefWindowProc(hwnd, msg, wParam, lParam);
\r
109 Callback(HWND hwnd, WORD ev, std::function<void()> cb)
\r
111 _::handlers[hwnd][ev] = cb;
\r
115 Window(string title, string className, HINSTANCE hInstance)
\r
118 wc.cbSize = sizeof(WNDCLASSEX);
\r
120 wc.lpfnWndProc = _::WndProc;
\r
123 wc.hInstance = hInstance;
\r
124 wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
\r
125 wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
\r
126 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
\r
127 wc.lpszMenuName = nullptr;
\r
128 wc.lpszClassName = className.c_str();
\r
129 wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
\r
130 RegisterClassEx(&wc);
\r
132 lay_init_context(&_::ctx);
\r
133 _::root = lay_item(&_::ctx);
\r
134 lay_set_contain(&_::ctx, _::root, LAY_COLUMN);
\r
136 HWND result = CreateWindowA(className.c_str(),
\r
138 WS_OVERLAPPEDWINDOW,
\r
148 _::niData.cbSize = sizeof(_::niData);
\r
149 _::niData.uID = 12345;
\r
150 _::niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
\r
151 _::niData.hIcon = LoadIconA(nullptr, IDI_WINLOGO);
\r
152 _::niData.hWnd = result;
\r
153 _::niData.uCallbackMessage = WM_APP+1;
\r
154 _::niData.uVersion = NOTIFYICON_VERSION_4;
\r
160 UpdateWindow(HWND hwnd)
\r
163 if (GetMessage(&msg, nullptr, 0, 0) > 0) {
\r
164 TranslateMessage(&msg);
\r
165 DispatchMessage(&msg);
\r
172 ShowWindow(HWND hwnd)
\r
174 ShowWindow(hwnd, true);
\r
178 [](HWND hwnd, LPARAM lParam) -> BOOL {
\r
179 HFONT guiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
\r
180 SendMessage(hwnd, WM_SETFONT, (WPARAM)guiFont, MAKELPARAM(TRUE, 0));
\r
187 Button(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
189 lay_id lId = lay_item(&_::ctx);
\r
190 lay_insert(&_::ctx, parent, lId);
\r
191 lay_set_size_xy(&_::ctx, lId, w, h);
\r
192 lay_set_contain(&_::ctx, lId, contain);
\r
193 lay_set_behave(&_::ctx, lId, behave);
\r
195 HWND result = CreateWindowExA(0,
\r
198 WS_VISIBLE | WS_CHILD,
\r
204 _::lIds[result] = lId;
\r
209 ListBox(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
211 lay_id lId = lay_item(&_::ctx);
\r
212 lay_insert(&_::ctx, parent, lId);
\r
213 lay_set_size_xy(&_::ctx, lId, w, h);
\r
214 lay_set_contain(&_::ctx, lId, contain);
\r
215 lay_set_behave(&_::ctx, lId, behave);
\r
217 HWND result = CreateWindowExA(0,
\r
220 WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,
\r
226 _::lIds[result] = lId;
\r
231 ListAddString(HWND hwnd, string str)
\r
233 SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)str.c_str());
\r
237 ListGetSelectedIndex(HWND hwnd)
\r
239 int sel = SendMessage(hwnd, LB_GETCURSEL, 0, 0);
\r
243 int ListFindString(HWND hwnd, string str)
\r
245 return SendMessageA(hwnd, LB_FINDSTRINGEXACT, -1, (LPARAM)str.c_str());
\r
249 ListGetText(HWND hwnd, int index)
\r
252 SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)buffer);
\r
253 return string(buffer);
\r
257 ListClear(HWND hwnd)
\r
259 SendMessageA(hwnd, LB_RESETCONTENT, 0, 0);
\r
262 void ListRemove(HWND hwnd, int index)
\r
264 SendMessageA(hwnd, LB_DELETESTRING, index, 0);
\r
268 ListView(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
270 lay_id lId = lay_item(&_::ctx);
\r
271 lay_insert(&_::ctx, parent, lId);
\r
272 lay_set_size_xy(&_::ctx, lId, w, h);
\r
273 lay_set_contain(&_::ctx, lId, contain);
\r
274 lay_set_behave(&_::ctx, lId, behave);
\r
276 HWND result = CreateWindowExA(0,
\r
279 WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,
\r
285 _::lIds[result] = lId;
\r
290 CheckBox(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
292 lay_id lId = lay_item(&_::ctx);
\r
293 lay_insert(&_::ctx, parent, lId);
\r
294 lay_set_size_xy(&_::ctx, lId, w, h);
\r
295 lay_set_contain(&_::ctx, lId, contain);
\r
296 lay_set_behave(&_::ctx, lId, behave);
\r
298 HWND result = CreateWindowExA(0,
\r
301 WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
\r
307 _::lIds[result] = lId;
\r
311 void SetStyle(HWND hwnd, DWORD style)
\r
313 SetWindowLongPtrA(hwnd, GWL_STYLE, style);
\r
315 DWORD GetStyle(HWND hwnd)
\r
317 return GetWindowLongPtrA(hwnd, GWL_STYLE);
\r
319 void AddStyle(HWND hwnd, DWORD style)
\r
321 SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) | style);
\r
323 void RemoveStyle(HWND hwnd, DWORD style)
\r
325 SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) & (~style));
\r