From 359bcc31d07640963eea97c448ab82acf80f0185 Mon Sep 17 00:00:00 2001 From: Mohit Sethi Date: Fri, 25 Nov 2022 21:32:36 +0530 Subject: [PATCH 1/7] Fix imgui window resizing and dragging issue while undocking. --- src/win32_window.c | 94 ++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 57 deletions(-) diff --git a/src/win32_window.c b/src/win32_window.c index f0b7bcfd..3313a139 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -490,7 +490,9 @@ static void releaseMonitor(_GLFWwindow* window) static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { - static RECT border_thickness; + static RECT border_thickness = { 0, 0, 0, 0 }; + if (!_glfw.hints.window.titlebar) + SetRect(&border_thickness, 8, 8, 8, 8); _GLFWwindow* window = GetPropW(hWnd, L"GLFW"); if (!window) @@ -548,22 +550,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar) break; - //find border thickness - SetRectEmpty(&border_thickness); - if (GetWindowLongPtr(hWnd, GWL_STYLE) & WS_THICKFRAME) - { - AdjustWindowRectEx(&border_thickness, GetWindowLongPtr(hWnd, GWL_STYLE) & ~WS_CAPTION, FALSE, NULL); - border_thickness.left *= -1; - border_thickness.top *= -1; - } - else// if (GetWindowLongPtr(hWnd, GWL_STYLE) & WS_BORDER) - { - SetRect(&border_thickness, 4, 4, 4, 4); - } - MARGINS margins = { 0 }; DwmExtendFrameIntoClientArea(hWnd, &margins); - SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); break; } @@ -573,14 +561,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar) break; - // Extend the frame into the client area. MARGINS margins = { 0 }; - auto hr = DwmExtendFrameIntoClientArea(hWnd, &margins); - - if (!SUCCEEDED(hr)) - { - // Handle the error. - } + DwmExtendFrameIntoClientArea(hWnd, &margins); break; } @@ -1273,13 +1255,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, // Extend the frame into the client area. MARGINS margins = { 0 }; - auto hr = DwmExtendFrameIntoClientArea(hWnd, &margins); - - if (!SUCCEEDED(hr)) - { - // Handle the error. - } - + DwmExtendFrameIntoClientArea(hWnd, &margins); + break; } case WM_NCHITTEST: @@ -1287,38 +1264,41 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar) break; - POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; - ScreenToClient(hWnd, &pt); - RECT rc; - GetClientRect(hWnd, &rc); + if (GetWindowLongPtr(hWnd, GWL_STYLE) & WS_THICKFRAME) + { + POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; + ScreenToClient(hWnd, &pt); + RECT rc; + GetClientRect(hWnd, &rc); - int titlebarHittest = 0; - _glfwInputTitleBarHitTest(window, pt.x, pt.y, &titlebarHittest); + int titlebarHittest = 0; + _glfwInputTitleBarHitTest(window, pt.x, pt.y, &titlebarHittest); - if (titlebarHittest) - { - return HTCAPTION; - } - else - { - enum { left = 1, top = 2, right = 4, bottom = 8 }; - int hit = 0; - if (pt.x < border_thickness.left) hit |= left; - if (pt.x > rc.right - border_thickness.right) hit |= right; - if (pt.y < border_thickness.top) hit |= top; - if (pt.y > rc.bottom - border_thickness.bottom) hit |= bottom; + if (titlebarHittest) + { + return HTCAPTION; + } + else + { + enum { left = 1, top = 2, right = 4, bottom = 8 }; + int hit = 0; + if (pt.x < border_thickness.left) hit |= left; + if (pt.x > rc.right - border_thickness.right) hit |= right; + if (pt.y < border_thickness.top) hit |= top; + if (pt.y > rc.bottom - border_thickness.bottom) hit |= bottom; - if (hit & top && hit & left) return HTTOPLEFT; - if (hit & top && hit & right) return HTTOPRIGHT; - if (hit & bottom && hit & left) return HTBOTTOMLEFT; - if (hit & bottom && hit & right) return HTBOTTOMRIGHT; - if (hit & left) return HTLEFT; - if (hit & top) return HTTOP; - if (hit & right) return HTRIGHT; - if (hit & bottom) return HTBOTTOM; + if (hit & top && hit & left) return HTTOPLEFT; + if (hit & top && hit & right) return HTTOPRIGHT; + if (hit & bottom && hit & left) return HTBOTTOMLEFT; + if (hit & bottom && hit & right) return HTBOTTOMRIGHT; + if (hit & left) return HTLEFT; + if (hit & top) return HTTOP; + if (hit & right) return HTRIGHT; + if (hit & bottom) return HTBOTTOM; - return HTCLIENT; - } + return HTCLIENT; + } + } } } From 32af0691085ead064256925e60a68412c546f224 Mon Sep 17 00:00:00 2001 From: Mohit Sethi Date: Fri, 2 Dec 2022 20:34:26 +0530 Subject: [PATCH 2/7] Added Dist configuration + optimize for speed --- premake5.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/premake5.lua b/premake5.lua index ed7ce228..62dd8001 100644 --- a/premake5.lua +++ b/premake5.lua @@ -84,9 +84,9 @@ project "GLFW" filter "configurations:Release" runtime "Release" - optimize "on" + optimize "speed" - filter "configurations:Dist" + filter "configurations:Dist" runtime "Release" - optimize "on" - symbols "off" \ No newline at end of file + optimize "speed" + symbols "off" From db92674a9e0ee38344a6dcea71552a6be1f40d37 Mon Sep 17 00:00:00 2001 From: Mohit Sethi Date: Tue, 13 Dec 2022 14:57:46 +0530 Subject: [PATCH 3/7] Fix window clipping when maximized --- src/win32_window.c | 110 ++++++++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 37 deletions(-) diff --git a/src/win32_window.c b/src/win32_window.c index 3313a139..a2913d37 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -491,8 +491,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static RECT border_thickness = { 0, 0, 0, 0 }; - if (!_glfw.hints.window.titlebar) - SetRect(&border_thickness, 8, 8, 8, 8); + BOOL hasThickFrame = GetWindowLongPtr(hWnd, GWL_STYLE) & WS_THICKFRAME; _GLFWwindow* window = GetPropW(hWnd, L"GLFW"); if (!window) @@ -550,8 +549,21 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar) break; - MARGINS margins = { 0 }; - DwmExtendFrameIntoClientArea(hWnd, &margins); + if (hasThickFrame) + { + RECT size_rect; + GetWindowRect(hWnd, &size_rect); + + // Inform the application of the frame change to force redrawing with the new + // client area that is extended into the title bar + SetWindowPos( + hWnd, NULL, + size_rect.left, size_rect.top, + size_rect.right - size_rect.left, size_rect.bottom - size_rect.top, + SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE + ); + break; + } break; } @@ -561,9 +573,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar) break; - MARGINS margins = { 0 }; - DwmExtendFrameIntoClientArea(hWnd, &margins); - + RECT title_bar_rect = {0}; + InvalidateRect(hWnd, &title_bar_rect, FALSE); break; } } @@ -995,16 +1006,28 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, break; } - case WM_NCCALCSIZE: - { - if (_glfw.hints.window.titlebar) - break; + case WM_NCCALCSIZE: + { + if (_glfw.hints.window.titlebar || !hasThickFrame || !wParam) + break; - if (lParam) - return 0; + UINT dpi = GetDpiForWindow(hWnd); + + int frame_x = GetSystemMetricsForDpi(SM_CXFRAME, dpi); + int frame_y = GetSystemMetricsForDpi(SM_CYFRAME, dpi); + int padding = GetSystemMetricsForDpi(92, dpi); + + NCCALCSIZE_PARAMS* params = (NCCALCSIZE_PARAMS*)lParam; + RECT* requested_client_rect = params->rgrc; + + requested_client_rect->right -= frame_x + padding; + requested_client_rect->left += frame_x + padding; + requested_client_rect->bottom -= frame_y + padding; + requested_client_rect->top += frame_y + (window->win32.maximized ? 1.0f : -1.0f) * padding; + + return 0; + } - break; - } case WM_SIZE: { const int width = LOWORD(lParam); @@ -1045,6 +1068,18 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, window->win32.iconified = iconified; window->win32.maximized = maximized; + + RECT size_rect; + GetWindowRect(hWnd, &size_rect); + + // Inform the application of the frame change to force redrawing with the new + // client area that is extended into the title bar + SetWindowPos( + hWnd, NULL, + size_rect.left, size_rect.top, + size_rect.right - size_rect.left, size_rect.bottom - size_rect.top, + SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE + ); return 0; } @@ -1253,10 +1288,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar) break; - // Extend the frame into the client area. - MARGINS margins = { 0 }; - DwmExtendFrameIntoClientArea(hWnd, &margins); - + RECT title_bar_rect = { 0 }; + InvalidateRect(hWnd, &title_bar_rect, FALSE); break; } case WM_NCHITTEST: @@ -1264,28 +1297,26 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar) break; - if (GetWindowLongPtr(hWnd, GWL_STYLE) & WS_THICKFRAME) + if (hasThickFrame) { POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; ScreenToClient(hWnd, &pt); - RECT rc; - GetClientRect(hWnd, &rc); - - int titlebarHittest = 0; - _glfwInputTitleBarHitTest(window, pt.x, pt.y, &titlebarHittest); - - if (titlebarHittest) - { - return HTCAPTION; - } - else + + if (!window->win32.maximized) { + RECT rc; + GetClientRect(hWnd, &rc); + + UINT dpi = GetDpiForWindow(hWnd); + int frame_y = GetSystemMetricsForDpi(SM_CYFRAME, dpi); + int padding = GetSystemMetricsForDpi(92, dpi); + enum { left = 1, top = 2, right = 4, bottom = 8 }; int hit = 0; - if (pt.x < border_thickness.left) hit |= left; - if (pt.x > rc.right - border_thickness.right) hit |= right; - if (pt.y < border_thickness.top) hit |= top; - if (pt.y > rc.bottom - border_thickness.bottom) hit |= bottom; + if (pt.x < border_thickness.left) hit |= left; + if (pt.x > rc.right - border_thickness.right) hit |= right; + if (pt.y < border_thickness.top || pt.y < frame_y + padding) hit |= top; + if (pt.y > rc.bottom - border_thickness.bottom) hit |= bottom; if (hit & top && hit & left) return HTTOPLEFT; if (hit & top && hit & right) return HTTOPRIGHT; @@ -1295,9 +1326,14 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (hit & top) return HTTOP; if (hit & right) return HTRIGHT; if (hit & bottom) return HTBOTTOM; - - return HTCLIENT; } + + int titlebarHittest = 0; + _glfwInputTitleBarHitTest(window, pt.x, pt.y, &titlebarHittest); + if (titlebarHittest) + return HTCAPTION; + + return HTCLIENT; } } } From 8a96738c07a45fc1bad99d5babfdf8bd1ce195ee Mon Sep 17 00:00:00 2001 From: Mohit Sethi Date: Fri, 23 Dec 2022 17:42:38 +0530 Subject: [PATCH 4/7] Disable _CRT_SECURE_NO_WARNINGS --- premake5.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/premake5.lua b/premake5.lua index 62dd8001..62e29bd3 100644 --- a/premake5.lua +++ b/premake5.lua @@ -70,7 +70,6 @@ project "GLFW" defines { "_GLFW_WIN32", - "_CRT_SECURE_NO_WARNINGS" } links From a36f9a6a29cc373f86decfb5dd06b7f81eda061f Mon Sep 17 00:00:00 2001 From: Mohit Sethi Date: Sun, 25 Dec 2022 01:08:49 +0530 Subject: [PATCH 5/7] Turn off warnings --- premake5.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/premake5.lua b/premake5.lua index 62e29bd3..e4704969 100644 --- a/premake5.lua +++ b/premake5.lua @@ -2,6 +2,7 @@ project "GLFW" kind "StaticLib" language "C" staticruntime "off" + warnings "off" targetdir ("bin/" .. outputdir .. "/%{prj.name}") objdir ("bin-int/" .. outputdir .. "/%{prj.name}") From 07df3a26af0fdd1f13455a0c3de62bab1f12e157 Mon Sep 17 00:00:00 2001 From: Mohit Sethi Date: Sun, 25 Dec 2022 03:52:42 +0530 Subject: [PATCH 6/7] Remove dwmapi linking, compile posix_module.c as well for Linux --- premake5.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/premake5.lua b/premake5.lua index e4704969..7376fdd6 100644 --- a/premake5.lua +++ b/premake5.lua @@ -38,6 +38,7 @@ project "GLFW" "src/x11_monitor.c", "src/x11_window.c", "src/xkb_unicode.c", + "src/posix_module.c", "src/posix_time.c", "src/posix_thread.c", "src/glx_context.c", @@ -73,11 +74,6 @@ project "GLFW" "_GLFW_WIN32", } - links - { - "Dwmapi.lib" - } - filter "configurations:Debug" runtime "Debug" symbols "on" From f664c210a2c79474874df4329127e28689a5a8ca Mon Sep 17 00:00:00 2001 From: Mohit Sethi Date: Sat, 1 Apr 2023 04:39:03 +0530 Subject: [PATCH 7/7] Fix errors with GetSystemMetricsForDpi --- src/win32_window.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/win32_window.c b/src/win32_window.c index a2913d37..3a57c307 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1011,19 +1011,16 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, if (_glfw.hints.window.titlebar || !hasThickFrame || !wParam) break; - UINT dpi = GetDpiForWindow(hWnd); - - int frame_x = GetSystemMetricsForDpi(SM_CXFRAME, dpi); - int frame_y = GetSystemMetricsForDpi(SM_CYFRAME, dpi); - int padding = GetSystemMetricsForDpi(92, dpi); + const int frame_x = 2.0f * GetSystemMetrics(SM_CXFRAME); + const int frame_y = 2.0f * GetSystemMetrics(SM_CYFRAME); NCCALCSIZE_PARAMS* params = (NCCALCSIZE_PARAMS*)lParam; RECT* requested_client_rect = params->rgrc; - requested_client_rect->right -= frame_x + padding; - requested_client_rect->left += frame_x + padding; - requested_client_rect->bottom -= frame_y + padding; - requested_client_rect->top += frame_y + (window->win32.maximized ? 1.0f : -1.0f) * padding; + requested_client_rect->right -= frame_x; + requested_client_rect->left += frame_x; + requested_client_rect->bottom -= frame_y; + requested_client_rect->top += window->win32.maximized ? frame_y : 0.0f; return 0; } @@ -1307,15 +1304,13 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, RECT rc; GetClientRect(hWnd, &rc); - UINT dpi = GetDpiForWindow(hWnd); - int frame_y = GetSystemMetricsForDpi(SM_CYFRAME, dpi); - int padding = GetSystemMetricsForDpi(92, dpi); + int frame_y = 2.0f * GetSystemMetrics(SM_CYFRAME); enum { left = 1, top = 2, right = 4, bottom = 8 }; int hit = 0; if (pt.x < border_thickness.left) hit |= left; if (pt.x > rc.right - border_thickness.right) hit |= right; - if (pt.y < border_thickness.top || pt.y < frame_y + padding) hit |= top; + if (pt.y < border_thickness.top || pt.y < frame_y) hit |= top; if (pt.y > rc.bottom - border_thickness.bottom) hit |= bottom; if (hit & top && hit & left) return HTTOPLEFT;