From 04daecdd21e86e7556a9462380096530228cd6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 3 Aug 2022 21:57:27 +0200 Subject: Split common.h to util.h, wcharptr.h and win.h. --- c/common.h | 146 ------------------------------------------------------------- 1 file changed, 146 deletions(-) delete mode 100644 c/common.h (limited to 'c/common.h') diff --git a/c/common.h b/c/common.h deleted file mode 100644 index 70c2c32..0000000 --- a/c/common.h +++ /dev/null @@ -1,146 +0,0 @@ -#ifndef COMMON_H -#define COMMON_H - -#include -#include -#include - -int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType); -int GetRelativeCursorPos(HWND hWnd, POINT* pt); - -/* wchar_ptr: Simple wrapper for wide C strings. */ -struct wchar_ptr -{ - /* Named copy constructors (expensive). */ - static wchar_ptr from_narrow(const char* buf, int cp = CP_UTF8); - static wchar_ptr copy(const wchar_t* s); - - /* Non-explicit copies are disabled. */ - wchar_ptr(wchar_ptr& other) = delete; - wchar_ptr& operator=(wchar_ptr& other) = delete; - - wchar_ptr() noexcept; - ~wchar_ptr(); - operator wchar_t*() noexcept; - - wchar_ptr(wchar_t* s) noexcept; - wchar_ptr& operator=(wchar_t* s) noexcept; - - wchar_ptr(wchar_ptr&& other) noexcept; - wchar_ptr& operator=(wchar_ptr&& other) noexcept; - - /* Return pointer, releasing ownership. */ - wchar_t *release() noexcept; -private: - wchar_t* m_p = nullptr; -}; - -/* Win32Error: Exception for Windows API errors. */ -struct Win32Error : public std::exception -{ - Win32Error(); - Win32Error(DWORD code); - ~Win32Error(); - const char* what() const noexcept override; - const wchar_t* What() const noexcept; - DWORD code; -private: - char* m_szMsg = NULL; - wchar_t* m_wszMsg = NULL; -}; - -/* Library: Wrapper for loading and freeing dynamically linked libraries. */ -struct Library -{ - static std::optional Maybe(const wchar_t* lib); - Library(const wchar_t* lib); - ~Library(); - template T* GetProcAddress(const char* szProc); -private: - Library(HMODULE hModule); - HMODULE m_hModule; -}; - -template -T* Library::GetProcAddress(const char* const szProc) -{ - return (T*)(void*)::GetProcAddress(m_hModule, szProc); -} - -template -inline int Swprintf(wchar_t (&buf)[N], const wchar_t* const fmt, T... xs) -{ - return _snwprintf_s(buf, N, _TRUNCATE, fmt, xs...); -} - -/* Variable template for caching values from GetSystemMetrics. */ -template -auto Metric = GetSystemMetrics(I); - -/* Check result of Windows API call, throwing error on NULL. */ -template -inline T require(const T x) -{ - if (!x) throw Win32Error(); - return x; -} - -/* Check result of Windows API call, showing a warning on NULL. */ -template -inline T prefer(const T x) -{ - if (!x) { - EBMessageBox(Win32Error().What(), L"System Error", MB_ICONWARNING); - return (T)NULL; - } - return x; -} - -/* Return integer scaled for current DPI. */ -inline int Dpi(const int i) -{ - extern int g_dpi; - return MulDiv(i, g_dpi, 96); -} - -inline int Cmp(const int a, const int b) -{ - if (a == b) return 0; - if (a > b) return 1; - return -1; -} - -/* Get window rectangle relative to parent. */ -inline BOOL GetRelativeRect(const HWND hWnd, RECT* const rr) -{ - if (!GetClientRect(hWnd, rr)) return 0; - return MapWindowPoints(hWnd, GetParent(hWnd), (POINT*)rr, 2); -} - -inline BOOL SetWindowRect(const HWND hWnd, - const long left, const long top, const long right, const long bottom) -{ - return MoveWindow(hWnd, - left, top, - right-left, bottom-top, - TRUE); -} - -inline BOOL SetWindowRect(const HWND hWnd, const RECT r) -{ - return SetWindowRect(hWnd, r.left, r.top, r.right, r.bottom); -} - -inline BOOL EBIsThemeActive() -{ - extern BOOL (*IsThemeActive)(); - return IsThemeActive? IsThemeActive(): 0; -} - -inline BOOL EBSetWindowTheme(HWND hWnd, LPCWSTR pszSubAppName, LPCWSTR pszSubIdList) -{ - extern BOOL (*SetWindowTheme)(HWND, LPCWSTR, LPCWSTR); - return SetWindowTheme? SetWindowTheme(hWnd, pszSubAppName, pszSubIdList): 0; -} - -#endif -- cgit v1.2.3