]> gitweb.ps.run Git - autorec/blobdiff - src/win.h
Why didnt I commit these
[autorec] / src / win.h
index fe58ebe8e6acbf730458c55ba3728716f338ab4d..0a734c95da3d4c2dadfa725d8211003dfbba9362 100644 (file)
--- a/src/win.h
+++ b/src/win.h
@@ -1,7 +1,3 @@
-#pragma comment(linker, "\"/manifestdependency:type='win32' \\r
-name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \\r
-processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")\r
-\r
 #include <windows.h>\r
 #include <CommCtrl.h>\r
 \r
@@ -13,321 +9,6 @@ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
 #include <unordered_map>\r
 #include <string>\r
 \r
-// namespace wnin {\r
-// namespace _ {\r
-// using CallbackFn = std::function<void()>;\r
-// std::map<HWND, std::map<WORD, CallbackFn>> handlers;\r
-// std::map<HWND, lay_id> lIds;\r
-\r
-// NOTIFYICONDATA niData = { 0 };\r
-// void\r
-// ShowNotificationIcon()\r
-// {\r
-//   Shell_NotifyIconA(NIM_ADD, &_::niData);\r
-//   Shell_NotifyIconA(NIM_SETVERSION, &_::niData);\r
-// }\r
-\r
-// void\r
-// HideNotificationIcon()\r
-// {\r
-//   Shell_NotifyIconA(NIM_DELETE, &_::niData);\r
-// }\r
-\r
-// lay_context ctx;\r
-// lay_id root;\r
-\r
-// LRESULT CALLBACK\r
-// WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\r
-// {\r
-//   switch (msg) {\r
-//     case WM_CLOSE:\r
-//       DestroyWindow(hwnd);\r
-//       break;\r
-//     case WM_DESTROY:\r
-//       Shell_NotifyIconA(NIM_DELETE, &niData);\r
-//       lay_destroy_context(&ctx);\r
-//       PostQuitMessage(0);\r
-//       break;\r
-//     case WM_SIZE:\r
-//       if (wParam == SIZE_MINIMIZED) {\r
-//         ShowNotificationIcon();\r
-//         ShowWindow(hwnd, false);\r
-//       }\r
-//       else {\r
-//         lay_set_size_xy(&_::ctx, _::root, LOWORD(lParam), HIWORD(lParam));\r
-//         lay_run_context(&_::ctx);\r
-\r
-//         for (auto &lId : lIds) {\r
-//           lay_vec4 rect = lay_get_rect(&_::ctx, lId.second);\r
-//           SetWindowPos(lId.first, HWND_TOP,\r
-//             rect[0],\r
-//             rect[1],\r
-//             rect[2],\r
-//             rect[3],\r
-//             SWP_NOZORDER\r
-//           );\r
-//         }\r
-//         RedrawWindow(hwnd, 0, 0, RDW_ERASE | RDW_INVALIDATE | RDW_ALLCHILDREN);\r
-//       }\r
-//       break;\r
-//     case WM_COMMAND:\r
-//       if (handlers.find((HWND)lParam) != handlers.end()) {\r
-//         auto handler = handlers[(HWND)lParam];\r
-//         if (handler.find(HIWORD(wParam)) != handler.end()) {\r
-//           auto cb = handler[HIWORD(wParam)];\r
-//           cb();\r
-//         }\r
-//       }\r
-//       break;\r
-//     case WM_NOTIFY:\r
-//       break;\r
-//     case WM_APP + 1:\r
-//       if (LOWORD(lParam) == NIN_SELECT) {\r
-//         HideNotificationIcon();\r
-//         ShowWindow(hwnd, true);\r
-//         SetForegroundWindow(hwnd);\r
-//         SetActiveWindow(hwnd);\r
-//       }\r
-//       break;\r
-//     case WM_CTLCOLORSTATIC:\r
-//       return (LONG)GetStockObject(WHITE_BRUSH);\r
-//     case WM_GETMINMAXINFO: {\r
-//       MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;\r
-//       mmInfo->ptMinTrackSize.x = 400;\r
-//       mmInfo->ptMinTrackSize.y = 200;\r
-//       break;\r
-//     }\r
-//     default:\r
-//       return DefWindowProc(hwnd, msg, wParam, lParam);\r
-//   }\r
-//   return 0;\r
-// }\r
-// }\r
-\r
-\r
-// void\r
-// Callback(HWND hwnd, WORD ev, std::function<void()> cb)\r
-// {\r
-//   _::handlers[hwnd][ev] = cb;\r
-// }\r
-\r
-// HWND\r
-// Window(string title, string className, HINSTANCE hInstance)\r
-// {\r
-//   WNDCLASSEX wc;\r
-//   wc.cbSize = sizeof(WNDCLASSEX);\r
-//   wc.style = 0;\r
-//   wc.lpfnWndProc = _::WndProc;\r
-//   wc.cbClsExtra = 0;\r
-//   wc.cbWndExtra = 0;\r
-//   wc.hInstance = hInstance;\r
-//   wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);\r
-//   wc.hCursor = LoadCursor(nullptr, IDC_ARROW);\r
-//   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);\r
-//   wc.lpszMenuName = nullptr;\r
-//   wc.lpszClassName = className.c_str();\r
-//   wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);\r
-//   RegisterClassEx(&wc);\r
-\r
-//   lay_init_context(&_::ctx);\r
-//   _::root = lay_item(&_::ctx);\r
-//   lay_set_contain(&_::ctx, _::root, LAY_COLUMN);\r
-\r
-//   HWND result = CreateWindowA(className.c_str(),\r
-//                        title.c_str(),\r
-//                        WS_OVERLAPPEDWINDOW,\r
-//                        CW_USEDEFAULT,\r
-//                        CW_USEDEFAULT,\r
-//                        CW_USEDEFAULT,\r
-//                        CW_USEDEFAULT,\r
-//                        nullptr,\r
-//                        nullptr,\r
-//                        hInstance,\r
-//                        nullptr);\r
-                       \r
-//   _::niData.cbSize = sizeof(_::niData);\r
-//   _::niData.uID = 12345;\r
-//   _::niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;\r
-//   _::niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_MY_ICON));\r
-//   _::niData.hWnd = result;\r
-//   _::niData.uCallbackMessage = WM_APP+1;\r
-//   _::niData.uVersion = NOTIFYICON_VERSION_4;\r
-\r
-//   return result;\r
-// }\r
-\r
-// bool\r
-// UpdateWindow(HWND hwnd)\r
-// {\r
-//   MSG msg;\r
-//   if (GetMessage(&msg, nullptr, 0, 0) > 0) {\r
-//     TranslateMessage(&msg);\r
-//     DispatchMessage(&msg);\r
-//     return true;\r
-//   }\r
-//   return false;\r
-// }\r
-\r
-// void\r
-// ShowWindow(HWND hwnd)\r
-// {\r
-//   ShowWindow(hwnd, true);\r
-\r
-//   EnumChildWindows(\r
-//     hwnd,\r
-//     [](HWND hwnd, LPARAM lParam) -> BOOL {\r
-//       HFONT guiFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);\r
-//       SendMessage(hwnd, WM_SETFONT, (WPARAM)guiFont, MAKELPARAM(TRUE, 0));\r
-//       return TRUE;\r
-//     },\r
-//     0);\r
-// }\r
-\r
-// HWND\r
-// Button(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
-// {\r
-//   lay_id lId = lay_item(&_::ctx);\r
-//   lay_insert(&_::ctx, parent, lId);\r
-//   lay_set_size_xy(&_::ctx, lId, w, h);\r
-//   lay_set_contain(&_::ctx, lId, contain);\r
-//   lay_set_behave(&_::ctx, lId, behave);\r
-\r
-//   HWND result = CreateWindowExA(0,\r
-//                                 WC_BUTTONA,\r
-//                                 title.c_str(),\r
-//                                 WS_VISIBLE | WS_CHILD,\r
-//                                 0, 0, 0, 0,\r
-//                                 hwnd,\r
-//                                 nullptr,\r
-//                                 nullptr,\r
-//                                 nullptr);\r
-//   _::lIds[result] = lId;\r
-//   return result;\r
-// }\r
-\r
-// HWND\r
-// ListBox(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
-// {\r
-//   lay_id lId = lay_item(&_::ctx);\r
-//   lay_insert(&_::ctx, parent, lId);\r
-//   lay_set_size_xy(&_::ctx, lId, w, h);\r
-//   lay_set_contain(&_::ctx, lId, contain);\r
-//   lay_set_behave(&_::ctx, lId, behave);\r
-\r
-//   HWND result = CreateWindowExA(0,\r
-//                          WC_LISTBOXA,\r
-//                          "",\r
-//                          WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,\r
-//                          0, 0, 0, 0,\r
-//                          hwnd,\r
-//                          nullptr,\r
-//                          nullptr,\r
-//                          nullptr);\r
-//   _::lIds[result] = lId;\r
-//   return result;\r
-// }\r
-\r
-// void\r
-// ListAddString(HWND hwnd, string str)\r
-// {\r
-//   SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM)str.c_str());\r
-// }\r
-\r
-// int\r
-// ListGetSelectedIndex(HWND hwnd)\r
-// {\r
-//   int sel = SendMessage(hwnd, LB_GETCURSEL, 0, 0);\r
-//   return sel;\r
-// }\r
-\r
-// int ListFindString(HWND hwnd, string str)\r
-// {\r
-//   return SendMessageA(hwnd, LB_FINDSTRINGEXACT, -1, (LPARAM)str.c_str());\r
-// }\r
-\r
-// string\r
-// ListGetText(HWND hwnd, int index)\r
-// {\r
-//   char buffer[1024];\r
-//   SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)buffer);\r
-//   return string(buffer);\r
-// }\r
-\r
-// void\r
-// ListClear(HWND hwnd)\r
-// {\r
-//   SendMessageA(hwnd, LB_RESETCONTENT, 0, 0);\r
-// }\r
-\r
-// void ListRemove(HWND hwnd, int index)\r
-// {\r
-//   SendMessageA(hwnd, LB_DELETESTRING, index, 0);\r
-// }\r
-\r
-// HWND\r
-// ListView(HWND hwnd, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
-// {\r
-//   lay_id lId = lay_item(&_::ctx);\r
-//   lay_insert(&_::ctx, parent, lId);\r
-//   lay_set_size_xy(&_::ctx, lId, w, h);\r
-//   lay_set_contain(&_::ctx, lId, contain);\r
-//   lay_set_behave(&_::ctx, lId, behave);\r
-\r
-//   HWND result = CreateWindowExA(0,\r
-//                          WC_LISTVIEWA,\r
-//                          "",\r
-//                          WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,\r
-//                          0, 0, 0, 0,\r
-//                          hwnd,\r
-//                          nullptr,\r
-//                          nullptr,\r
-//                          nullptr);\r
-//   _::lIds[result] = lId;\r
-//   return result;\r
-// }\r
-\r
-// HWND\r
-// CheckBox(HWND hwnd, string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
-// {\r
-//   lay_id lId = lay_item(&_::ctx);\r
-//   lay_insert(&_::ctx, parent, lId);\r
-//   lay_set_size_xy(&_::ctx, lId, w, h);\r
-//   lay_set_contain(&_::ctx, lId, contain);\r
-//   lay_set_behave(&_::ctx, lId, behave);\r
-\r
-//   HWND result = CreateWindowExA(0,\r
-//                          WC_BUTTONA,\r
-//                          title.c_str(),\r
-//                          WS_VISIBLE | WS_CHILD | BS_CHECKBOX,\r
-//                          0, 0, 0, 0,\r
-//                          hwnd,\r
-//                          nullptr,\r
-//                          nullptr,\r
-//                          nullptr);\r
-//   _::lIds[result] = lId;\r
-//   return result;\r
-// }\r
-\r
-// void SetStyle(HWND hwnd, DWORD style)\r
-// {\r
-//   SetWindowLongPtrA(hwnd, GWL_STYLE, style);\r
-// }\r
-// DWORD GetStyle(HWND hwnd)\r
-// {\r
-//   return GetWindowLongPtrA(hwnd, GWL_STYLE);\r
-// }\r
-// void AddStyle(HWND hwnd, DWORD style)\r
-// {\r
-//   SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) | style);\r
-// }\r
-// void RemoveStyle(HWND hwnd, DWORD style)\r
-// {\r
-//   SetWindowLongPtrA(hwnd, GWL_STYLE, GetStyle(hwnd) & (~style));\r
-// }\r
-// }\r
-\r
-\r
-\r
 \r
 namespace win\r
 {\r
@@ -339,7 +20,7 @@ namespace win
     Window *window;\r
 \r
     Hwnd() {}\r
-    Hwnd(Window *window, Hwnd *parent, LPCSTR className, LPCSTR windowName, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave);\r
+    Hwnd(Window *window, LPCSTR className, LPCSTR windowName, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave);\r
 \r
     void setStyle(DWORD style)\r
     {\r
@@ -357,11 +38,14 @@ namespace win
     {\r
       SetWindowLongPtrA(hwnd, GWL_STYLE, getStyle() & (~style));\r
     }\r
+    void setActive(bool active)\r
+    {\r
+      EnableWindow(hwnd, active);\r
+    }\r
   };\r
   struct Window : Hwnd\r
   {\r
   private:\r
-    NOTIFYICONDATAA niData = { 0 };\r
 \r
     static LRESULT CALLBACK\r
     WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)\r
@@ -377,17 +61,11 @@ namespace win
           DestroyWindow(hwnd);\r
           break;\r
         case WM_DESTROY:\r
-          Shell_NotifyIconA(NIM_DELETE, &window->niData);\r
           lay_destroy_context(&window->ctx);\r
           PostQuitMessage(0);\r
           break;\r
         case WM_SIZE:\r
-          if (wParam == SIZE_MINIMIZED) {\r
-            //TODO: auslagen\r
-            //ShowNotificationIcon();\r
-            ShowWindow(hwnd, false);\r
-          }\r
-          else {\r
+          if (wParam != SIZE_MINIMIZED) {\r
             lay_set_size_xy(&window->ctx, window->lId, LOWORD(lParam), HIWORD(lParam));\r
             lay_run_context(&window->ctx);\r
 \r
@@ -396,23 +74,8 @@ namespace win
           break;\r
         case WM_NOTIFY:\r
           break;\r
-        case WM_APP + 1:\r
-          if (LOWORD(lParam) == NIN_SELECT) {\r
-            //TODO: auslagern\r
-            //HideNotificationIcon();\r
-            ShowWindow(hwnd, true);\r
-            SetForegroundWindow(hwnd);\r
-            SetActiveWindow(hwnd);\r
-          }\r
-          break;\r
         case WM_CTLCOLORSTATIC:\r
-          return (LONG)GetStockObject(WHITE_BRUSH);\r
-        case WM_GETMINMAXINFO: {\r
-          MINMAXINFO *mmInfo = (MINMAXINFO*)lParam;\r
-          mmInfo->ptMinTrackSize.x = 400;\r
-          mmInfo->ptMinTrackSize.y = 200;\r
-          break;\r
-        }\r
+          return (LONG_PTR)GetStockObject(WHITE_BRUSH);\r
         default:\r
           defaultHandler = true;\r
           break;\r
@@ -432,6 +95,12 @@ namespace win
       std::vector<\r
         std::function<void(HWND, UINT, WPARAM, LPARAM)>>> handlers;\r
 \r
+    struct Timer {\r
+      bool active = true;\r
+      std::function<void()> f;\r
+    };\r
+    std::vector<Timer> timers;\r
+\r
     Window(std::string title, std::string className, HINSTANCE hInstance)\r
     {\r
       WNDCLASSEXA wc;\r
@@ -441,12 +110,12 @@ namespace win
       wc.cbClsExtra = 0;\r
       wc.cbWndExtra = sizeof(Window*);\r
       wc.hInstance = hInstance;\r
-      wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);\r
-      wc.hCursor = LoadCursor(nullptr, IDC_ARROW);\r
+      wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE));\r
+      wc.hCursor = LoadCursor(hInstance, IDC_ARROW);\r
       wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);\r
       wc.lpszMenuName = nullptr;\r
       wc.lpszClassName = className.c_str();\r
-      wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);\r
+      wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_WHITE));\r
       RegisterClassExA(&wc);\r
 \r
       lay_init_context(&ctx);\r
@@ -466,15 +135,6 @@ namespace win
                           nullptr);\r
 \r
       SetWindowLongPtrA(hwnd, 0, (LONG_PTR)this);\r
-      \r
-                          \r
-      niData.cbSize = sizeof(niData);\r
-      niData.uID = 12345;\r
-      niData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;\r
-      niData.hIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(IDI_MY_ICON));\r
-      niData.hWnd = hwnd;\r
-      niData.uCallbackMessage = WM_APP+1;\r
-      niData.uVersion = NOTIFYICON_VERSION_4;\r
     }\r
     bool update()\r
     {\r
@@ -489,7 +149,9 @@ namespace win
     void show()\r
     {\r
       ShowWindow(hwnd, true);\r
-\r
+    }\r
+    void setDefaultFont()\r
+    {\r
       EnumChildWindows(\r
         hwnd,\r
         [](HWND hwnd, LPARAM lParam) -> BOOL {\r
@@ -499,12 +161,25 @@ namespace win
         },\r
         0);\r
     }\r
+    void setTimer(UINT interval, std::function<void()> cb)\r
+    {\r
+      SetTimer(this->hwnd, timers.size() + 1000, interval, [](HWND hwnd, UINT uMsg, UINT_PTR uIdEvent, DWORD dwTime) {\r
+        Window *window = (Window*)GetWindowLongPtrA(hwnd, 0);\r
+        if (window == nullptr)\r
+          return;\r
+\r
+        window->timers[uIdEvent-1000].f();\r
+      });\r
+      Timer t;\r
+      t.f = cb;\r
+      timers.push_back(t);\r
+    }\r
   };\r
 \r
   struct Button : Hwnd\r
   {\r
-    Button(Window *window, Hwnd *parent, std::string title, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
-      : Hwnd(window, parent, WC_BUTTONA, title.c_str(), w, h, contain, behave)\r
+    Button(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
+      : Hwnd(window, WC_BUTTONA, title.c_str(), parent, w, h, contain, behave)\r
     {\r
       window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {\r
         if ((HWND)lParam == this->hwnd && HIWORD(wParam) == BN_CLICKED)\r
@@ -523,8 +198,8 @@ namespace win
   \r
   struct CheckBox : Hwnd\r
   {\r
-    CheckBox(Window *window, Hwnd *parent, std::string title, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
-      : Hwnd(window, parent, WC_BUTTONA, title.c_str(), w, h, contain, behave)\r
+    CheckBox(Window *window, std::string title, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
+      : Hwnd(window, WC_BUTTONA, title.c_str(), parent, w, h, contain, behave)\r
     {\r
       addStyle(BS_CHECKBOX);\r
       window->handlers[WM_COMMAND].push_back([&](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {\r
@@ -536,11 +211,11 @@ namespace win
 \r
   struct ListBox : Hwnd\r
   {\r
-    ListBox(Window *window, Hwnd *parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
-      : Hwnd(window, parent, WC_LISTBOXA, "", w, h, contain, behave)\r
+    ListBox(Window *window, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
+      : Hwnd(window, WC_LISTBOXA, "", parent, w, h, contain, behave)\r
     {\r
       addStyle(WS_BORDER);\r
-      addStyle(WS_VSCROLL);\r
+      //addStyle(WS_VSCROLL);\r
     }\r
 \r
     void addString(std::string str)\r
@@ -561,8 +236,7 @@ namespace win
     std::string getText(int index)\r
     {\r
       int len = SendMessageA(hwnd, LB_GETTEXTLEN, index, 0);\r
-      std::string result;\r
-      result.reserve(len);\r
+      std::string result(len, 0);\r
       SendMessage(hwnd, LB_GETTEXT, index, (LPARAM)result.data());\r
       return result;\r
     }\r
@@ -577,17 +251,26 @@ namespace win
       SendMessageA(hwnd, LB_DELETESTRING, index, 0);\r
     }\r
   };\r
+\r
+  lay_id createLayId(lay_context *ctx, lay_id parent, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
+  {\r
+    lay_id lId = lay_item(ctx);\r
+    lay_insert(ctx, parent, lId);\r
+    lay_set_size_xy(ctx, lId, w, h);\r
+    lay_set_contain(ctx, lId, contain);\r
+    lay_set_behave(ctx, lId, behave);\r
+    return lId;\r
+  }\r
 }\r
 \r
-win::Hwnd::Hwnd(Window *window, Hwnd *parent, LPCSTR className, LPCSTR windowName, lay_scalar w, lay_scalar h, uint32_t contain, uint32_t behave)\r
+\r
+\r
+\r
+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
 {\r
   this->window = window;\r
   \r
-  lId = lay_item(&window->ctx);\r
-  lay_insert(&window->ctx, parent->lId, lId);\r
-  lay_set_size_xy(&window->ctx, lId, w, h);\r
-  lay_set_contain(&window->ctx, lId, contain);\r
-  lay_set_behave(&window->ctx, lId, behave);\r
+  lId = createLayId(&window->ctx, parent, w, h, contain, behave);\r
   \r
   hwnd = CreateWindowExA(0,\r
                           className,\r