From e148e8f3646ae756254879f2df015431bbd036ed Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 3 Aug 2019 10:38:34 +0200 Subject: Windows: Fix class registration for multiple views --- pugl/detail/win.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'pugl') diff --git a/pugl/detail/win.c b/pugl/detail/win.c index 3a30814..42b9427 100644 --- a/pugl/detail/win.c +++ b/pugl/detail/win.c @@ -52,6 +52,9 @@ typedef BOOL (WINAPI *PFN_SetProcessDPIAware)(void); static const TCHAR* DEFAULT_CLASSNAME = "Pugl"; +LRESULT CALLBACK +wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); + static wchar_t* puglUtf8ToWideChar(const char* const utf8) { @@ -65,8 +68,25 @@ puglUtf8ToWideChar(const char* const utf8) return NULL; } -LRESULT CALLBACK -wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); +static bool +puglRegisterWindowClass(const char* name) +{ + WNDCLASSEX wc = { 0 }; + if (GetClassInfoEx(GetModuleHandle(NULL), name, &wc)) { + return true; // Already registered + } + + wc.cbSize = sizeof(wc); + wc.style = CS_OWNDC; + wc.lpfnWndProc = wndProc; + wc.hInstance = GetModuleHandle(NULL); + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wc.lpszClassName = name; + + return RegisterClassEx(&wc); +} PuglInternals* puglInitInternals(void) @@ -104,18 +124,8 @@ puglCreateWindow(PuglView* view, const char* title) EnumDisplaySettingsA(NULL, ENUM_CURRENT_SETTINGS, &devMode); view->impl->refreshRate = devMode.dmDisplayFrequency; - // Register window class - WNDCLASSEX wc; - memset(&wc, 0, sizeof(wc)); - wc.cbSize = sizeof(wc); - wc.style = CS_OWNDC; - wc.lpfnWndProc = wndProc; - wc.hInstance = GetModuleHandle(NULL); - wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // TODO: user-specified icon - wc.hCursor = LoadCursor(NULL, IDC_ARROW); - wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - wc.lpszClassName = className; - if (!RegisterClassEx(&wc)) { + // Register window class if necessary + if (!puglRegisterWindowClass(className)) { return 1; } -- cgit v1.2.1