aboutsummaryrefslogtreecommitdiff
path: root/c/common.h
blob: f011565f3351ba634114e0ddc3a30ee1b60bd7f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef COMMON_H
#define COMMON_H

#include <memory>
#include <optional>
#include <windows.h>

int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType);
int GetRelativeCursorPos(HWND hWnd, POINT* pt);

struct wchar_ptr
{
	static wchar_ptr from_narrow(const char* buf, int cp = CP_UTF8);
	static wchar_ptr copy(const wchar_t* s);
	wchar_ptr();
	wchar_ptr(wchar_t* s);
	wchar_ptr& operator=(wchar_t* s);
	wchar_ptr(wchar_ptr& other) = delete;
	wchar_ptr& operator=(wchar_ptr& other) = delete;
	wchar_ptr(wchar_ptr&& other) noexcept;
	wchar_ptr& operator=(wchar_ptr&& other) noexcept;
	operator wchar_t*() noexcept;
	wchar_t *release();
	~wchar_ptr();
private:
	wchar_t* m_p = nullptr;
};

struct Win32Error : public std::exception
{
	Win32Error();
	Win32Error(DWORD code);
	~Win32Error();
	const char* what() const noexcept;
	const wchar_t* WhatW() const noexcept;
	DWORD code;
private:
	char* m_szMsg = NULL;
	wchar_t* m_wszMsg = NULL;
};

struct Library
{
	static std::optional<Library> Maybe(const wchar_t* lib);
	Library(const wchar_t* lib);
	~Library();
	template <class T> T* GetProcAddress(const char* szProc);
private:
	Library(HMODULE hModule);
	HMODULE m_hModule;
};

template <typename T>
T* Library::GetProcAddress(const char* const szProc)
{
	return (T*)(void*)::GetProcAddress(m_hModule, szProc);
}

template<size_t N, typename... T>
inline int wszf(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 <int I>
auto Metric = GetSystemMetrics(I);

/* Check result of Windows API call, throwing error on NULL. */
template <typename T>
inline T require(const T x)
{
	if (!x) throw Win32Error();
	return x;
}

/* Check result of Windows API call, showing a warning on NULL. */
template <typename T>
inline T prefer(const T x)
{
	if (!x) {
		EBMessageBox(Win32Error().WhatW(), 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);
}

#endif