From a465c1c32e0754d3de56e01c59a0fef33202f04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Fri, 6 May 2022 17:58:42 +0200 Subject: [PATCH] Win32: Simplify window class handling By using window class atoms, we only need to mention each window class name once, also removing the need to define _GLFW_WNDCLASSNAME. It can still be defined by the user as before. --- src/win32_init.c | 17 ++++------ src/win32_platform.h | 9 ++--- src/win32_window.c | 78 +++++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 61 deletions(-) diff --git a/src/win32_init.c b/src/win32_init.c index c2dc1322..8704150c 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -378,7 +378,8 @@ static GLFWbool createHelperWindow(void) wc.hInstance = _glfw.win32.instance; wc.lpszClassName = L"GLFW3 Helper"; - if (!RegisterClassExW(&wc)) + _glfw.win32.helperWindowClass = RegisterClassExW(&wc); + if (!_glfw.win32.helperWindowClass) { _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, "WIn32: Failed to register helper window class"); @@ -387,7 +388,7 @@ static GLFWbool createHelperWindow(void) _glfw.win32.helperWindowHandle = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW, - L"GLFW3 Helper", + MAKEINTATOM(_glfw.win32.helperWindowClass), L"GLFW message window", WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 1, 1, @@ -695,9 +696,6 @@ int _glfwInitWin32(void) else if (IsWindowsVistaOrGreater()) SetProcessDPIAware(); - if (!_glfwRegisterWindowClassWin32()) - return GLFW_FALSE; - if (!createHelperWindow()) return GLFW_FALSE; @@ -711,12 +709,11 @@ void _glfwTerminateWin32(void) UnregisterDeviceNotification(_glfw.win32.deviceNotificationHandle); if (_glfw.win32.helperWindowHandle) - { DestroyWindow(_glfw.win32.helperWindowHandle); - UnregisterClassW(L"GLFW3 Helper", _glfw.win32.instance); - } - - _glfwUnregisterWindowClassWin32(); + if (_glfw.win32.helperWindowClass) + UnregisterClassW(MAKEINTATOM(_glfw.win32.helperWindowClass), _glfw.win32.instance); + if (_glfw.win32.mainWindowClass) + UnregisterClassW(MAKEINTATOM(_glfw.win32.mainWindowClass), _glfw.win32.instance); _glfw_free(_glfw.win32.clipboardString); _glfw_free(_glfw.win32.rawInput); diff --git a/src/win32_platform.h b/src/win32_platform.h index c9d96cd0..c2158943 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -358,10 +358,6 @@ typedef struct VkWin32SurfaceCreateInfoKHR typedef VkResult (APIENTRY *PFN_vkCreateWin32SurfaceKHR)(VkInstance,const VkWin32SurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*); typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)(VkPhysicalDevice,uint32_t); -#if !defined(_GLFW_WNDCLASSNAME) - #define _GLFW_WNDCLASSNAME L"GLFW30" -#endif - #define GLFW_WIN32_WINDOW_STATE _GLFWwindowWin32 win32; #define GLFW_WIN32_LIBRARY_WINDOW_STATE _GLFWlibraryWin32 win32; #define GLFW_WIN32_MONITOR_STATE _GLFWmonitorWin32 win32; @@ -444,6 +440,8 @@ typedef struct _GLFWlibraryWin32 { HINSTANCE instance; HWND helperWindowHandle; + ATOM helperWindowClass; + ATOM mainWindowClass; HDEVNOTIFY deviceNotificationHandle; int acquiredMonitorCount; char* clipboardString; @@ -527,9 +525,6 @@ GLFWbool _glfwConnectWin32(int platformID, _GLFWplatform* platform); int _glfwInitWin32(void); void _glfwTerminateWin32(void); -GLFWbool _glfwRegisterWindowClassWin32(void); -void _glfwUnregisterWindowClassWin32(void); - WCHAR* _glfwCreateWideStringFromUTF8Win32(const char* source); char* _glfwCreateUTF8FromWideStringWin32(const WCHAR* source); BOOL _glfwIsWindowsVersionOrGreaterWin32(WORD major, WORD minor, WORD sp); diff --git a/src/win32_window.c b/src/win32_window.c index cefb2cf6..8d9c95e7 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1249,6 +1249,39 @@ static int createNativeWindow(_GLFWwindow* window, DWORD style = getWindowStyle(window); DWORD exStyle = getWindowExStyle(window); + if (!_glfw.win32.mainWindowClass) + { + WNDCLASSEXW wc = { sizeof(wc) }; + wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; + wc.lpfnWndProc = windowProc; + wc.hInstance = _glfw.win32.instance; + wc.hCursor = LoadCursorW(NULL, IDC_ARROW); +#if defined(_GLFW_WNDCLASSNAME) + wc.lpszClassName = _GLFW_WNDCLASSNAME; +#else + wc.lpszClassName = L"GLFW30"; +#endif + // Load user-provided icon if available + wc.hIcon = LoadImageW(GetModuleHandleW(NULL), + L"GLFW_ICON", IMAGE_ICON, + 0, 0, LR_DEFAULTSIZE | LR_SHARED); + if (!wc.hIcon) + { + // No user-provided icon found, load default icon + wc.hIcon = LoadImageW(NULL, + IDI_APPLICATION, IMAGE_ICON, + 0, 0, LR_DEFAULTSIZE | LR_SHARED); + } + + _glfw.win32.mainWindowClass = RegisterClassExW(&wc); + if (!_glfw.win32.mainWindowClass) + { + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, + "Win32: Failed to register window class"); + return GLFW_FALSE; + } + } + if (window->monitor) { MONITORINFO mi = { sizeof(mi) }; @@ -1282,7 +1315,7 @@ static int createNativeWindow(_GLFWwindow* window, return GLFW_FALSE; window->win32.handle = CreateWindowExW(exStyle, - _GLFW_WNDCLASSNAME, + MAKEINTATOM(_glfw.win32.mainWindowClass), wideTitle, style, xpos, ypos, @@ -1387,49 +1420,6 @@ static int createNativeWindow(_GLFWwindow* window, return GLFW_TRUE; } -// Registers the GLFW window class -// -GLFWbool _glfwRegisterWindowClassWin32(void) -{ - WNDCLASSEXW wc; - - ZeroMemory(&wc, sizeof(wc)); - wc.cbSize = sizeof(wc); - wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; - wc.lpfnWndProc = windowProc; - wc.hInstance = _glfw.win32.instance; - wc.hCursor = LoadCursorW(NULL, IDC_ARROW); - wc.lpszClassName = _GLFW_WNDCLASSNAME; - - // Load user-provided icon if available - wc.hIcon = LoadImageW(GetModuleHandleW(NULL), - L"GLFW_ICON", IMAGE_ICON, - 0, 0, LR_DEFAULTSIZE | LR_SHARED); - if (!wc.hIcon) - { - // No user-provided icon found, load default icon - wc.hIcon = LoadImageW(NULL, - IDI_APPLICATION, IMAGE_ICON, - 0, 0, LR_DEFAULTSIZE | LR_SHARED); - } - - if (!RegisterClassExW(&wc)) - { - _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, - "Win32: Failed to register window class"); - return GLFW_FALSE; - } - - return GLFW_TRUE; -} - -// Unregisters the GLFW window class -// -void _glfwUnregisterWindowClassWin32(void) -{ - UnregisterClassW(_GLFW_WNDCLASSNAME, _glfw.win32.instance); -} - GLFWbool _glfwCreateWindowWin32(_GLFWwindow* window, const _GLFWwndconfig* wndconfig, const _GLFWctxconfig* ctxconfig,