X-Git-Url: https://gitweb.ps.run/autorec/blobdiff_plain/3ec9dfbe56f54f699f2d5efb4e3a4be96e157bc0..ed07fa5aa45759ad962bd4dcef85d53b65070424:/src/win.h diff --git a/src/win.h b/src/win.h index fe58ebe..0a734c9 100644 --- a/src/win.h +++ b/src/win.h @@ -1,7 +1,3 @@ -#pragma comment(linker, "\"/manifestdependency:type='win32' \ -name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \ -processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") - #include #include @@ -13,321 +9,6 @@ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") #include #include -// namespace wnin { -// namespace _ { -// using CallbackFn = std::function; -// std::map> handlers; -// std::map 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 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 { @@ -339,7 +20,7 @@ namespace win Window *window; Hwnd() {} - Hwnd(Window *window, Hwnd *parent, LPCSTR className, LPCSTR windowName, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave); + Hwnd(Window *window, LPCSTR className, LPCSTR windowName, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave); void setStyle(DWORD style) { @@ -357,11 +38,14 @@ namespace win { SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() & (~style)); } + void setActive(bool active) + { + EnableWindow(hwnd, active); + } }; struct Window : Hwnd { private: - NOTIFYICONDATAA niData = { 0 }; static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) @@ -377,17 +61,11 @@ namespace win 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 { + if (wParam != SIZE_MINIMIZED) { lay_set_size_xy(&window->ctx, window->lId, LOWORD(lParam), HIWORD(lParam)); lay_run_context(&window->ctx); @@ -396,23 +74,8 @@ namespace win 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; - } + return (LONG_PTR)GetStockObject(WHITE_BRUSH); default: defaultHandler = true; break; @@ -432,6 +95,12 @@ namespace win std::vector< std::function>> handlers; + struct Timer { + bool active = true; + std::function f; + }; + std::vector timers; + Window(std::string title, std::string className, HINSTANCE hInstance) { WNDCLASSEXA wc; @@ -441,12 +110,12 @@ namespace win wc.cbClsExtra = 0; wc.cbWndExtra = sizeof(Window*); wc.hInstance = hInstance; - wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION); - wc.hCursor = LoadCursor(nullptr, IDC_ARROW); + wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE)); + wc.hCursor = LoadCursor(hInstance, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = nullptr; wc.lpszClassName = className.c_str(); - wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION); + wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE)); RegisterClassExA(&wc); lay_init_context(&ctx); @@ -466,15 +135,6 @@ namespace win 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() { @@ -489,7 +149,9 @@ namespace win void show() { ShowWindow(hwnd, true); - + } + void setDefaultFont() + { EnumChildWindows( hwnd, [](HWND hwnd, LPARAM lParam) -> BOOL { @@ -499,12 +161,25 @@ namespace win }, 0); } + void setTimer(UINT interval, std::function cb) + { + SetTimer(this->hwnd, timers.size() + 1000, interval, [](HWND hwnd, UINT uMsg, UINT_PTR uIdEvent, DWORD dwTime) { + Window *window = (Window*)GetWindowLongPtrA(hwnd, 0); + if (window == nullptr) + return; + + window->timers[uIdEvent-1000].f(); + }); + Timer t; + t.f = cb; + timers.push_back(t); + } }; 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) + Button(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave) + : Hwnd(window, WC_BUTTONA, title.c_str(), parent, 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) @@ -523,8 +198,8 @@ namespace win 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) + CheckBox(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave) + : Hwnd(window, WC_BUTTONA, title.c_str(), parent, w, h, contain, behave) { addStyle(BS_CHECKBOX); window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { @@ -536,11 +211,11 @@ namespace win 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) + ListBox(Window *window, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave) + : Hwnd(window, WC_LISTBOXA, "", parent, w, h, contain, behave) { addStyle(WS_BORDER); - addStyle(WS_VSCROLL); + //addStyle(WS_VSCROLL); } void addString(std::string str) @@ -561,8 +236,7 @@ namespace win std::string getText(int index) { int len = SendMessageA(hwnd, LB_GETTEXTLEN, index, 0); - std::string result; - result.reserve(len); + std::string result(len, 0); SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)result.data()); return result; } @@ -577,17 +251,26 @@ namespace win SendMessageA(hwnd, LB_DELETESTRING, index, 0); } }; + + lay_id createLayId(lay_context *ctx, 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); + return lId; + } } -win::Hwnd::Hwnd(Window *window, Hwnd *parent, LPCSTR className, LPCSTR windowName, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave) + + + +win::Hwnd::Hwnd(Window *window, LPCSTR className, LPCSTR windowName, lay_id parent, 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); + lId = createLayId(&window->ctx, parent, w, h, contain, behave); hwnd = CreateWindowExA(0, className,