aboutsummaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-22 23:52:22 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-22 23:52:22 +0200
commit21e96c692595f204b91431a90123419e4a1780c4 (patch)
treefbd273dc24fa590626283dac09bc7671f0562d59 /c
parentc1bbba366067f27881195f74cee5d063d1b270b0 (diff)
downloadEpisodeBrowser-21e96c692595f204b91431a90123419e4a1780c4.tar.gz
Cache GetSystemMetrics values with variable template.
The variable template could be generalized like this: template <auto F, auto... A> const auto cache = F(A...); and instantiated like: cache<GetSystemMetrics, SM_CXVSCROLL> It would still be limited to constant function arguments, which usually isn't a problem for GetSystemMetrics, but might be for other functions.
Diffstat (limited to 'c')
-rw-r--r--c/common.h14
-rw-r--r--c/datalistview.cpp3
-rw-r--r--c/episodelistview.cpp3
-rw-r--r--c/main.cpp3
4 files changed, 11 insertions, 12 deletions
diff --git a/c/common.h b/c/common.h
index 2c0be13..c6c7dbd 100644
--- a/c/common.h
+++ b/c/common.h
@@ -54,7 +54,11 @@ std::optional<T> maybe_make(U... xs)
}
}
-/* Call Windows API function, throwing error on NULL. */
+/* Variable template for caching values from GetSystemMetrics. */
+template <int I>
+const auto Metric = GetSystemMetrics(I);
+
+/* Check result of Windows API call, throwing error on NULL. */
template <typename T>
inline T require(const T x)
{
@@ -62,7 +66,7 @@ inline T require(const T x)
return x;
}
-/* Call Windows API function, showing a warning on NULL. */
+/* Check result of Windows API call, showing a warning on NULL. */
template <typename T>
inline T prefer(const T x)
{
@@ -75,7 +79,7 @@ inline T prefer(const T x)
}
/* Conditionally choose between two values. This template is necessary
- * because the ternary conditional operator chooses only between
+ * because the ternary conditional operator can choose only between
* values of the same type. */
template <bool B, typename X, typename Y>
constexpr std::enable_if_t<B == true, X> choose(X x, Y) { return x; }
@@ -83,10 +87,10 @@ template <bool B, typename X, typename Y>
constexpr std::enable_if_t<B == false, Y> choose(X, Y y) { return y; }
/* Conditionally choose between ANSI and wide string literal. */
-#define AWTEXT(t, s) choose<std::is_same_v<t, char>>(s, L"" s)
+#define AWTEXT(t, s) choose<std::is_same_v<t, wchar_t>>(L"" s, s)
/* Conditionally choose between ANSI and wide Windows API function. */
-#define AWFUN(t, f) choose<std::is_same_v<t, char>>(f##A, f##W)
+#define AWFUN(t, f) choose<std::is_same_v<t, wchar_t>>(f##W, f##A)
/* Return integer scaled for current DPI. */
inline int Dpi(const int i)
diff --git a/c/datalistview.cpp b/c/datalistview.cpp
index c191901..9ff4659 100644
--- a/c/datalistview.cpp
+++ b/c/datalistview.cpp
@@ -31,12 +31,11 @@ DataListView::DataListView(const HWND hWndParent)
void DataListView::ResizeColumns(RECT& rcParent)
{
- extern int g_cxVScroll;
ListView_SetColumnWidth(hWnd, DLVSIKEY, LVSCW_AUTOSIZE);
const int cxColumn = ListView_GetColumnWidth(hWnd, 0)+4;
ListView_SetColumnWidth(hWnd, DLVSIKEY, cxColumn);
- ListView_SetColumnWidth(hWnd, DLVSIVALUE, rcParent.right-cxColumn-g_cxVScroll-4);
+ ListView_SetColumnWidth(hWnd, DLVSIVALUE, rcParent.right-cxColumn-Metric<SM_CXVSCROLL>-4);
}
void DataListView::ShowEpisode(const int iEpisode)
diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp
index 6f12a0f..17ae373 100644
--- a/c/episodelistview.cpp
+++ b/c/episodelistview.cpp
@@ -143,14 +143,13 @@ void EpisodeListView::Redraw()
void EpisodeListView::ResizeColumns(RECT& rcParent)
{
- extern int g_cxVScroll;
ListView_SetColumnWidth(hWnd, ELVSIEPISODE, LVSCW_AUTOSIZE);
int cxColumn = ListView_GetColumnWidth(hWnd, ELVSIEPISODE)+4;
ListView_SetColumnWidth(hWnd, ELVSIEPISODE, cxColumn);
cxColumn += ListView_GetColumnWidth(hWnd, ELVSIRATING);
- ListView_SetColumnWidth(hWnd, ELVSITITLE, rcParent.right-cxColumn-g_cxVScroll-4);
+ ListView_SetColumnWidth(hWnd, ELVSITITLE, rcParent.right-cxColumn-Metric<SM_CXVSCROLL>-4);
}
/* Select previously focused episode. */
diff --git a/c/main.cpp b/c/main.cpp
index 5a33405..c04890d 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -18,7 +18,6 @@ int g_bThread;
/* Looked-up constants. */
int g_bThemes;
-int g_cxVScroll;
int g_iDPI = 96;
/* Fonts. */
@@ -166,8 +165,6 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR
g_hWnd = (HWND)wParam;
/* Look up constants. */
- g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
-
if (auto opLib = maybe_make<Library>(TEXT("User32.dll")))
if (auto GetDpiForWindow = opLib->GetProcAddress<UINT(HWND)>("GetDpiForWindow"))
g_iDPI = GetDpiForWindow(g_hWnd);