2 #include <CommCtrl.h>
\r
4 #include "../res/resource.h"
\r
8 #include <functional>
\r
9 #include <unordered_map>
\r
23 Hwnd(Window *window, LPCSTR className, LPCSTR windowName, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave);
\r
25 void setStyle(DWORD style)
\r
27 SetWindowLongPtrA(hwnd, GWL_STYLE, style);
\r
31 return GetWindowLongPtrA(hwnd, GWL_STYLE);
\r
33 void addStyle(DWORD style)
\r
35 SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() | style);
\r
37 void removeStyle(DWORD style)
\r
39 SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() & (~style));
\r
41 void setActive(bool active)
\r
43 EnableWindow(hwnd, active);
\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
100 std::function<void()> f;
\r
102 std::vector<Timer> timers;
\r
104 Window(std::string title, std::string className, HINSTANCE hInstance)
\r
107 wc.cbSize = sizeof(WNDCLASSEX);
\r
109 wc.lpfnWndProc = WndProc;
\r
111 wc.cbWndExtra = sizeof(Window*);
\r
112 wc.hInstance = hInstance;
\r
113 wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE));
\r
114 wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
\r
115 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
\r
116 wc.lpszMenuName = nullptr;
\r
117 wc.lpszClassName = className.c_str();
\r
118 wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE));
\r
119 RegisterClassExA(&wc);
\r
121 lay_init_context(&ctx);
\r
122 lId = lay_item(&ctx);
\r
123 lay_set_contain(&ctx, lId, LAY_COLUMN);
\r
125 hwnd = CreateWindowA(className.c_str(),
\r
127 WS_OVERLAPPEDWINDOW,
\r
137 SetWindowLongPtrA(hwnd, 0, (LONG_PTR)this);
\r
142 if (GetMessage(&msg, nullptr, 0, 0) > 0) {
\r
143 TranslateMessage(&msg);
\r
144 DispatchMessage(&msg);
\r
151 ShowWindow(hwnd, true);
\r
153 void setDefaultFont()
\r
157 [](HWND hwnd, LPARAM lParam) -> BOOL {
\r
158 HFONT guiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
\r
159 SendMessage(hwnd, WM_SETFONT, (WPARAM)guiFont, MAKELPARAM(TRUE, 0));
\r
164 void setTimer(UINT interval, std::function<void()> cb)
\r
166 SetTimer(this->hwnd, timers.size() + 1000, interval, [](HWND hwnd, UINT uMsg, UINT_PTR uIdEvent, DWORD dwTime) {
\r
167 Window *window = (Window*)GetWindowLongPtrA(hwnd, 0);
\r
168 if (window == nullptr)
\r
171 window->timers[uIdEvent-1000].f();
\r
175 timers.push_back(t);
\r
179 struct Button : Hwnd
\r
181 Button(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
182 : Hwnd(window, WC_BUTTONA, title.c_str(), parent, w, h, contain, behave)
\r
184 window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
185 if ((HWND)lParam == this->hwnd && HIWORD(wParam) == BN_CLICKED)
\r
186 for (auto handler : this->onClickHandlers)
\r
191 void onClick(std::function<void()> cb)
\r
193 onClickHandlers.push_back(cb);
\r
196 std::vector<std::function<void()>> onClickHandlers;
\r
199 struct CheckBox : Hwnd
\r
201 CheckBox(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
202 : Hwnd(window, WC_BUTTONA, title.c_str(), parent, w, h, contain, behave)
\r
204 addStyle(BS_CHECKBOX);
\r
205 window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
206 if ((HWND)lParam == this->hwnd && HIWORD(wParam) == BN_CLICKED)
\r
207 SendMessageA(this->hwnd, BM_SETCHECK, SendMessageA(this->hwnd, BM_GETCHECK, 0, 0) ? BST_UNCHECKED : BST_CHECKED, 0);
\r
212 struct ListBox : Hwnd
\r
214 ListBox(Window *window, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
215 : Hwnd(window, WC_LISTBOXA, "", parent, w, h, contain, behave)
\r
217 addStyle(WS_BORDER);
\r
218 //addStyle(WS_VSCROLL);
\r
221 void addString(std::string str)
\r
223 SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)str.c_str());
\r
226 int getSelectedIndex()
\r
228 return SendMessage(hwnd, LB_GETCURSEL, 0, 0);
\r
231 int findString(std::string str)
\r
233 return SendMessageA(hwnd, LB_FINDSTRINGEXACT, -1, (LPARAM)str.c_str());
\r
236 std::string getText(int index)
\r
238 int len = SendMessageA(hwnd, LB_GETTEXTLEN, index, 0);
\r
239 std::string result(len, 0);
\r
240 SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)result.data());
\r
246 SendMessageA(hwnd, LB_RESETCONTENT, 0, 0);
\r
249 void remove(int index)
\r
251 SendMessageA(hwnd, LB_DELETESTRING, index, 0);
\r
255 lay_id createLayId(lay_context *ctx, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)
\r
257 lay_id lId = lay_item(ctx);
\r
258 lay_insert(ctx, parent, lId);
\r
259 lay_set_size_xy(ctx, lId, w, h);
\r
260 lay_set_contain(ctx, lId, contain);
\r
261 lay_set_behave(ctx, lId, behave);
\r
269 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
271 this->window = window;
\r
273 lId = createLayId(&window->ctx, parent, w, h, contain, behave);
\r
275 hwnd = CreateWindowExA(0,
\r
278 WS_VISIBLE | WS_CHILD,
\r
288 window->handlers[WM_SIZE].push_back([this](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
\r
289 lay_vec4 rect = lay_get_rect(&this->window->ctx, this->lId);
\r
290 SetWindowPos(this->hwnd, HWND_TOP,
\r