aboutsummaryrefslogtreecommitdiff
path: root/c/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'c/main.cpp')
-rw-r--r--c/main.cpp30
1 files changed, 13 insertions, 17 deletions
diff --git a/c/main.cpp b/c/main.cpp
index 758889f..e51e0f9 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -52,7 +52,7 @@ BOOL (*IsThemeActive)();
BOOL (*SetWindowTheme)(HWND, const wchar_t*, const wchar_t*);
/* Initialize important global state on parent window creation. */
-static LRESULT CALLBACK CBTProc(int, WPARAM, LPARAM);
+static void InitializeMainWindow(HWND);
/* Process parent window commands. */
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
/* Process main menu commands. */
@@ -66,7 +66,7 @@ static INT_PTR CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM);
/* Try to style application according to current Windows theme. */
static void UpdateTheme();
-void OnTerminate() noexcept
+static void OnTerminate() noexcept
{
const wchar_t* what = L"an exception";
WcharPtr why;
@@ -128,10 +128,10 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons
wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
Require(RegisterClassEx(&wc));
- /* Create window. A CBT hook is used to initialize important
- * global variables before any messages are sent to the new
- * window. It is vital that the hook is set up correctly. */
- const HHOOK hHook = Require(SetWindowsHookEx(WH_CBT, CBTProc, nullptr, GetCurrentThreadId()));
+ /* InitializeMainWindow is called before the first message is
+ * sent to WndProc. This is important, as it initializes
+ * global state on which WndProc relies. */
+ WithNextWindow(InitializeMainWindow);
const HWND hWnd = Require(CreateWindowEx(
0,
L"Episode Browser",
@@ -139,7 +139,6 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons
WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0,
nullptr, nullptr, hInstance, nullptr));
- Require(UnhookWindowsHookEx(hHook));
g_hWndStatus = Require(CreateWindowEx(
0,
@@ -167,21 +166,16 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons
return 0;
}
-static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPARAM lParam)
+void InitializeMainWindow(const HWND hWnd)
{
- if (nCode < 0 || nCode != HCBT_CREATEWND || g_hWnd)
- return CallNextHookEx(0, nCode, wParam, lParam);
-
/* This code is run ONCE, at the creation of the top-level
* window -- before WndProc! This is important, as it
* initializes global variables that are used by WndProc. */
- g_hWnd = reinterpret_cast<HWND>(wParam);
-
/* Look up DPI. */
if (auto lib = Library::Maybe(L"User32.dll");
auto GetDpiForWindow = lib->GetProcAddress<UINT(HWND)>("GetDpiForWindow"))
- g_dpi = GetDpiForWindow(g_hWnd);
+ g_dpi = GetDpiForWindow(hWnd);
/* Load normal font. */
if (auto lib = Library::Maybe(L"User32.dll");
@@ -205,8 +199,8 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR
}
/* Create child windows. */
- g_dlv = new DataListView(g_hWnd);
- g_elv = new EpisodeListView(g_hWnd);
+ g_dlv = new DataListView(hWnd);
+ g_elv = new EpisodeListView(hWnd);
/* Get saved view settings. */
Pl("cfg","get_view_watched",&g_bViewWatched);
@@ -221,7 +215,9 @@ static LRESULT CALLBACK CBTProc(const int nCode, const WPARAM wParam, const LPAR
Pl("cfg","get_dlv_height",&dlvHeight);
g_dlv->SetHeight(dlvHeight);
- return 0;
+ /* The global main window handle must only be set AFTER
+ * successful initialization. */
+ g_hWnd = hWnd;
}
LRESULT CALLBACK WndProc(const HWND hWnd, const UINT uMsg, const WPARAM wParam, const LPARAM lParam)