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
8 #include "../res/resource.h"
\r
12 #include <functional>
\r
13 #include <unordered_map>
\r
27 Hwnd(Window *window, LPCSTR className, LPCSTR windowName, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave);
\r
29 void setStyle(DWORD style)
\r
31 SetWindowLongPtrA(hwnd, GWL_STYLE, style);
\r
35 return GetWindowLongPtrA(hwnd, GWL_STYLE);
\r
37 void addStyle(DWORD style)
\r
39 SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() | style);
\r
41 void removeStyle(DWORD style)
\r
43 SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() & (~style));
\r
46 struct Window : Hwnd
\r
50 static LRESULT CALLBACK
\r
51 WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
\r
53 Window *window = (Window*)GetWindowLongPtrA(hwnd, 0);
\r
54 if (window == nullptr)
\r
55 return DefWindowProc(hwnd, msg, wParam, lParam);
\r
57 bool defaultHandler = false;
\r
61 DestroyWindow(hwnd);
\r
64 lay_destroy_context(&window->ctx);
\r
68 if (wParam != SIZE_MINIMIZED) {
\r
69 lay_set_size_xy(&window->ctx, window->lId, LOWORD(lParam), HIWORD(lParam));
\r
70 lay_run_context(&window->ctx);
\r
72 //RedrawWindow(hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN);
\r
77 case WM_CTLCOLORSTATIC:
\r
78 return (LONG_PTR)GetStockObject(WHITE_BRUSH);
\r
80 defaultHandler = true;
\r
84 for (auto handler : window->handlers[msg])
\r
85 handler(hwnd, msg, wParam, lParam);
\r
88 return DefWindowProc(hwnd, msg, wParam, lParam);
\r
94 std::unordered_map<UINT,
\r
96 std::function<void(HWND, UINT, WPARAM, LPARAM)>>> handlers;
\r
98 Window(std::string title, std::string className, HINSTANCE hInstance)
\r
101 wc.cbSize = sizeof(WNDCLASSEX);
\r
103 wc.lpfnWndProc = WndProc;
\r
105 wc.cbWndExtra = sizeof(Window*);
\r
106 wc.hInstance = hInstance;
\r
107 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE));
\r
108 wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
\r
109 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
\r
110 wc.lpszMenuName = nullptr;
\r
111 wc.lpszClassName = className.c_str();
\r
112 wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE));
\r
113 RegisterClassExA(&wc);
\r
115 lay_init_context(&ctx);
\r
116 lId = lay_item(&ctx);
\r
117 lay_set_contain(&ctx, lId, LAY_COLUMN);
\r
119 hwnd = CreateWindowA(className.c_str(),
\r
121 WS_OVERLAPPEDWINDOW,
\r
131 SetWindowLongPtrA(hwnd, 0, (LONG_PTR)this);
\r
136 if (GetMessage(&msg, nullptr, 0, 0) > 0) {
\r
137 TranslateMessage(&msg);
\r
138 DispatchMessage(&msg);
\r
145 ShowWindow(hwnd, true);
\r
147 void setDefaultFont()
\r
151 [](HWND hwnd, LPARAM lParam) -> BOOL {
\r
152 HFONT guiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
\r
153 SendMessage(hwnd, WM_SETFONT, (WPARAM)guiFont, MAKELPARAM(TRUE, 0));
\r
160 struct Button : Hwnd
\r
162 Button(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
163 : Hwnd(window, WC_BUTTONA, title.c_str(), parent, w, h, contain, behave)
\r
165 window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
166 if ((HWND)lParam == this->hwnd && HIWORD(wParam) == BN_CLICKED)
\r
167 for (auto handler : this->onClickHandlers)
\r
172 void onClick(std::function<void()> cb)
\r
174 onClickHandlers.push_back(cb);
\r
177 std::vector<std::function<void()>> onClickHandlers;
\r
180 struct CheckBox : Hwnd
\r
182 CheckBox(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
183 : Hwnd(window, WC_BUTTONA, title.c_str(), parent, w, h, contain, behave)
\r
185 addStyle(BS_CHECKBOX);
\r
186 window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
187 if ((HWND)lParam == this->hwnd && HIWORD(wParam) == BN_CLICKED)
\r
188 SendMessageA(this->hwnd, BM_SETCHECK, SendMessageA(this->hwnd, BM_GETCHECK, 0, 0) ? BST_UNCHECKED : BST_CHECKED, 0);
\r
193 struct ListBox : Hwnd
\r
195 ListBox(Window *window, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
196 : Hwnd(window, WC_LISTBOXA, "", parent, w, h, contain, behave)
\r
198 addStyle(WS_BORDER);
\r
199 //addStyle(WS_VSCROLL);
\r
202 void addString(std::string str)
\r
204 SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)str.c_str());
\r
207 int getSelectedIndex()
\r
209 return SendMessage(hwnd, LB_GETCURSEL, 0, 0);
\r
212 int findString(std::string str)
\r
214 return SendMessageA(hwnd, LB_FINDSTRINGEXACT, -1, (LPARAM)str.c_str());
\r
217 std::string getText(int index)
\r
219 int len = SendMessageA(hwnd, LB_GETTEXTLEN, index, 0);
\r
220 std::string result(len, 0);
\r
221 SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)result.data());
\r
227 SendMessageA(hwnd, LB_RESETCONTENT, 0, 0);
\r
230 void remove(int index)
\r
232 SendMessageA(hwnd, LB_DELETESTRING, index, 0);
\r
236 lay_id createLayId(lay_context *ctx, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
238 lay_id lId = lay_item(ctx);
\r
239 lay_insert(ctx, parent, lId);
\r
240 lay_set_size_xy(ctx, lId, w, h);
\r
241 lay_set_contain(ctx, lId, contain);
\r
242 lay_set_behave(ctx, lId, behave);
\r
250 win::Hwnd::Hwnd(Window *window, LPCSTR className, LPCSTR windowName, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
252 this->window = window;
\r
254 lId = createLayId(&window->ctx, parent, w, h, contain, behave);
\r
256 hwnd = CreateWindowExA(0,
\r
259 WS_VISIBLE | WS_CHILD,
\r
269 window->handlers[WM_SIZE].push_back([this](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
270 lay_vec4 rect = lay_get_rect(&this->window->ctx, this->lId);
\r
271 SetWindowPos(this->hwnd, HWND_TOP,
\r