From bae51a17cc742e74172a3b9ae7c96730a7c558e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= <john@ankarstrom.se>
Date: Mon, 18 Jul 2022 02:33:16 +0200
Subject: Formatting.

---
 c/main.cpp | 122 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 66 insertions(+), 56 deletions(-)

diff --git a/c/main.cpp b/c/main.cpp
index 3e2c7e7..68fe4b3 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -11,57 +11,66 @@
 #include "main.h"
 #include "pl.h"
 
-DataListView* g_pDlv = nullptr;
-EpisodeListView* g_pElv = nullptr;
-
+/* Application state. */
 atom_t g_aThread;
-char g_szLimitScreenwriter[64] = {0};
+int g_bThread;
+
+/* Looked-up constants. */
+int g_bThemes;
+int g_iDPI = 96;
+
+/* Fonts. */
 HFONT g_hfNormal;
 HFONT g_fBold;
+
+/* Menus. */
 HMENU g_hPopupMenu;
+
+/* Windows. */
 HWND g_hFocus;
 HWND g_hWnd;
 HWND g_hWndStatus;
-int g_bThemes = 0;
-int g_bViewTVOriginal = 1;
+
+/* Child window objects. */
+DataListView* g_pDlv;
+EpisodeListView* g_pElv;
+
+/* View settings. */
 int g_bViewWatched = 1;
-int g_bThread = 0;
-int g_iDPI = 96;
-static int g_cxVScroll;
-static void OnTerminate();
+int g_bViewTVOriginal = 1;
+char g_szLimitScreenwriter[64];
+
 static LRESULT CALLBACK CBTProc(int, WPARAM, LPARAM);
 static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
 static HWND CreateStatusBar(HWND, HINSTANCE);
 static void UpdateTheme();
 
+void OnTerminate()
+{
+	try {
+		std::rethrow_exception(std::current_exception());
+        } catch (const term_t& t) {
+		TCHAR* tsz;
+		if (PL_get_tchars(t, &tsz, CVT_WRITE)) {
+			MessageBox(NULL, tsz, TEXT("Fatal Error"), MB_ICONERROR);
+		} else
+			MessageBoxA(NULL, "The program was terminated due to a Prolog exception.",
+			    "Fatal Error", MB_ICONERROR);
+	} catch (std::exception& e) {
+		MessageBoxA(NULL, e.what(), "Fatal Error", MB_ICONERROR);
+	} catch (...) {
+		MessageBoxA(NULL, "The program was terminated due to an exception.",
+		    "Fatal Error", MB_ICONERROR);
+	}
+	_Exit(1);
+}
+
 int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, const int nCmdShow)
 {
 	/* Exit gracefully on uncaught exception. */
 	std::set_terminate(OnTerminate);
 
-	/* Set constant values. */
-	if (auto opLib = try_make<Library>(TEXT("uxtheme.dll")))
-		if (opLib->GetProcAddress("SetWindowTheme"))
-			g_bThemes = 1;
-	g_cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
-
-	/* Setup fonts. */
-	if (auto opLib = try_make<Library>(TEXT("User32.dll"))) {
-		if (opLib->GetProcAddress("SystemParametersInfo" WA)) {
-			NONCLIENTMETRICS m;
-			m.cbSize = sizeof(NONCLIENTMETRICS);
-			SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &m, 0);
-			g_hfNormal = CreateFontIndirect(&m.lfMessageFont);
-		}
-	} else
-		g_hfNormal = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
-
-	LOGFONT lf;
-	GetObject(g_hfNormal, sizeof(LOGFONT), &lf);
-	lf.lfWeight = FW_BOLD;
-	g_fBold = CreateFontIndirect(&lf);
-
 	/* Initialize Prolog. */
 	char* argv[] = { (char*)"EpisodeBrowser", NULL };
 	if (!PL_initialise(1, argv))
@@ -130,27 +139,7 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons
 	return 0;
 }
 
-void OnTerminate()
-{
-	try {
-		std::rethrow_exception(std::current_exception());
-        } catch (const term_t& t) {
-		TCHAR* tsz;
-		if (PL_get_tchars(t, &tsz, CVT_WRITE)) {
-			MessageBox(NULL, tsz, TEXT("Fatal Error"), MB_ICONERROR);
-		} else
-			MessageBoxA(NULL, "The program was terminated due to a Prolog exception.",
-			    "Fatal Error", MB_ICONERROR);
-	} catch (std::exception& e) {
-		MessageBoxA(NULL, e.what(), "Fatal Error", MB_ICONERROR);
-	} catch (...) {
-		MessageBoxA(NULL, "The program was terminated due to an exception.",
-		    "Fatal Error", MB_ICONERROR);
-	}
-	_Exit(1);
-}
-
-static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
+static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPARAM)
 {
 	if (nCode != HCBT_CREATEWND) return 0;
 	if (g_hWnd) return 0;
@@ -161,11 +150,31 @@ static LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
 
 	g_hWnd = (HWND)wParam;
 
-	/* Get DPI. */
+	/* Look up constants. */
 	if (auto opLib = try_make<Library>(TEXT("User32.dll")))
 		if (auto GetDpiForWindow = (UINT (*)(HWND))opLib->GetProcAddress("GetDpiForWindow"))
 			g_iDPI = GetDpiForWindow(g_hWnd);
 
+	if (auto opLib = try_make<Library>(TEXT("uxtheme.dll")))
+		if (opLib->GetProcAddress("SetWindowTheme"))
+			g_bThemes = 1;
+
+	LOGFONT lf;
+	if (auto opLib = try_make<Library>(TEXT("User32.dll"))) {
+		if (opLib->GetProcAddress("SystemParametersInfo" WA)) {
+			NONCLIENTMETRICS m;
+			m.cbSize = sizeof(NONCLIENTMETRICS);
+			SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &m, 0);
+			g_hfNormal = CreateFontIndirect(&m.lfMessageFont);
+		}
+	} else
+		g_hfNormal = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
+	GetObject(g_hfNormal, sizeof(LOGFONT), &lf);
+	lf.lfWeight = FW_BOLD;
+	g_fBold = CreateFontIndirect(&lf);
+
+	/* Get DPI. */
+
 	/* Create child windows. */
 	g_pDlv = new DataListView(g_hWnd);
 	g_pElv = new EpisodeListView(g_hWnd);
@@ -502,6 +511,7 @@ void UpdateLayout()
 {
 	int cxColumn, cyDlv, yStatus;
 	RECT rc, rcStatus;
+	static int cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
 
 	GetClientRect(g_hWnd, &rc);
 	GetClientRect(g_hWndStatus, &rcStatus);
@@ -515,7 +525,7 @@ void UpdateLayout()
 	ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIKEY, LVSCW_AUTOSIZE);
 	cxColumn = ListView_GetColumnWidth(g_pDlv->hWnd, 0)+4;
 	ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIKEY, cxColumn);
-	ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIVALUE, rc.right-cxColumn-g_cxVScroll-4);
+	ListView_SetColumnWidth(g_pDlv->hWnd, DLVSIVALUE, rc.right-cxColumn-cxVScroll-4);
 
 	/* Resize episode list view. */
 	MoveWindow(g_pElv->hWnd, 0, 0, rc.right, cyDlv+1, TRUE);
@@ -523,7 +533,7 @@ void UpdateLayout()
 	cxColumn = ListView_GetColumnWidth(g_pElv->hWnd, ELVSIEPISODE)+4;
 	ListView_SetColumnWidth(g_pElv->hWnd, ELVSIEPISODE, cxColumn);
 	cxColumn += ListView_GetColumnWidth(g_pElv->hWnd, ELVSIRATING);
-	ListView_SetColumnWidth(g_pElv->hWnd, ELVSITITLE, rc.right-cxColumn-g_cxVScroll-4);
+	ListView_SetColumnWidth(g_pElv->hWnd, ELVSITITLE, rc.right-cxColumn-cxVScroll-4);
 	SendMessage(g_pElv->hWnd, WM_SETREDRAW, TRUE, 0);
 	SendMessage(g_pDlv->hWnd, WM_SETREDRAW, TRUE, 0);
 
-- 
cgit v1.2.3