Skip to content

Commit

Permalink
Use DPI-aware AdjustWindowRectExForDpi when available
Browse files Browse the repository at this point in the history
AdjustWindowRectEx does not take DPI into account, leading to dialog
windows being cut off. This change adjusts usages of that method to
AdjustWindowRectExForDpi, passing the zoom level to retrieve correct
coordinates and scaling accordingly.
  • Loading branch information
ShahzaibIbrahim authored and HeikoKlare committed Jan 10, 2025
1 parent 6104592 commit 17f652f
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 11 deletions.
27 changes: 26 additions & 1 deletion bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -124,6 +124,31 @@ JNIEXPORT jboolean JNICALL OS_NATIVE(AdjustWindowRectEx)
}
#endif

#ifndef NO_AdjustWindowRectExForDpi
JNIEXPORT jboolean JNICALL OS_NATIVE(AdjustWindowRectExForDpi)
(JNIEnv *env, jclass that, jobject arg0, jint arg1, jboolean arg2, jint arg3, jint arg4)
{
RECT _arg0, *lparg0=NULL;
jboolean rc = 0;

OS_NATIVE_ENTER(env, that, AdjustWindowRectExForDpi_FUNC);
if (arg0) if ((lparg0 = getRECTFields(env, arg0, &_arg0)) == NULL) goto fail;
/*
rc = (jboolean)AdjustWindowRectExForDpi(lparg0, arg1, arg2, arg3, arg4);
*/
{
OS_LOAD_FUNCTION(fp, AdjustWindowRectExForDpi)
if (fp) {
rc = (jboolean)((jboolean (CALLING_CONVENTION*)(RECT *, jint, jboolean, jint, jint))fp)(lparg0, arg1, arg2, arg3, arg4);
}
}
fail:
if (arg0 && lparg0) setRECTFields(env, arg0, lparg0);
OS_NATIVE_EXIT(env, that, AdjustWindowRectExForDpi_FUNC);
return rc;
}
#endif

#ifndef NO_AllowSetForegroundWindow
JNIEXPORT jboolean JNICALL OS_NATIVE(AllowSetForegroundWindow)
(JNIEnv *env, jclass that, jint arg0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
#define GetThreadDpiAwarenessContext_LIB "user32.dll"
#define SetThreadDpiAwarenessContext_LIB "user32.dll"
#define SystemParametersInfoForDpi_LIB "user32.dll"
#define AdjustWindowRectExForDpi_LIB "winuser.dll"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -30,6 +30,7 @@ typedef enum {
ActivateKeyboardLayout_FUNC,
AddFontResourceEx_FUNC,
AdjustWindowRectEx_FUNC,
AdjustWindowRectExForDpi_FUNC,
AllowDarkModeForWindow_FUNC,
AllowSetForegroundWindow_FUNC,
AlphaBlend_FUNC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,7 @@ public static int HRESULT_FROM_WIN32(int x) {
*/
public static final native int AddFontResourceEx(char[] lpszFilename, int fl, long pdv);
public static final native boolean AdjustWindowRectEx (RECT lpRect, int dwStyle, boolean bMenu, int dwExStyle);
public static final native boolean AdjustWindowRectExForDpi (RECT lpRect, int dwStyle, boolean bMenu, int dwExStyle, int dpi);
/** @method flags=no_gen */
public static final native boolean AllowDarkModeForWindow(long hWnd, boolean allow);
public static final native boolean AllowSetForegroundWindow (int dwProcessId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Control computeTabRoot () {
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
boolean hasMenu = OS.GetMenu (handle) != 0;
OS.AdjustWindowRectEx (rect, bits1, hasMenu, bits2);
adjustWindowRectEx(rect, bits1, hasMenu, bits2);

/* Get the size of the scroll bars */
if (horizontalBar != null) rect.bottom += getSystemMetrics (OS.SM_CYHSCROLL);
Expand Down Expand Up @@ -472,7 +472,7 @@ void fixDecorations (Decorations newDecorations, Control control, Menu [] menus)
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
boolean hasMenu = OS.GetMenu (handle) != 0;
OS.AdjustWindowRectEx (rect, bits1, hasMenu, bits2);
adjustWindowRectEx(rect, bits1, hasMenu, bits2);
width = Math.max (0, width - (rect.right - rect.left));
height = Math.max (0, height - (rect.bottom - rect.top));
return new Rectangle (0, 0, width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Rectangle computeTrimInPixels (int x, int y, int width, int height) {
OS.SetRect (rect, x, y, x + width, y + height);
int bits1 = OS.GetWindowLong (scrolledHandle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (scrolledHandle, OS.GWL_EXSTYLE);
OS.AdjustWindowRectEx (rect, bits1, false, bits2);
adjustWindowRectEx(rect, bits1, false, bits2);
if (horizontalBar != null) rect.bottom += getSystemMetrics (OS.SM_CYHSCROLL);
if (verticalBar != null) rect.right += getSystemMetrics (OS.SM_CXVSCROLL);
int nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ Point getMaximumSizeInPixels () {
RECT rect = new RECT ();
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
OS.AdjustWindowRectEx (rect, bits1, false, bits2);
adjustWindowRectEx(rect, bits1, false, bits2);
height = Math.min (height, rect.bottom - rect.top);
}
}
Expand Down Expand Up @@ -1109,7 +1109,7 @@ Point getMinimumSizeInPixels () {
RECT rect = new RECT ();
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
OS.AdjustWindowRectEx (rect, bits1, false, bits2);
adjustWindowRectEx(rect, bits1, false, bits2);
height = Math.max (height, rect.bottom - rect.top);
}
}
Expand Down Expand Up @@ -1815,7 +1815,7 @@ void setMaximumSizeInPixels (int width, int height) {
RECT rect = new RECT ();
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
OS.AdjustWindowRectEx (rect, bits1, false, bits2);
adjustWindowRectEx(rect, bits1, false, bits2);
heightLimit = rect.bottom - rect.top;
}
}
Expand Down Expand Up @@ -1861,7 +1861,7 @@ void setMinimumSizeInPixels (int width, int height) {
RECT rect = new RECT ();
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
OS.AdjustWindowRectEx (rect, bits1, false, bits2);
adjustWindowRectEx(rect, bits1, false, bits2);
heightLimit = rect.bottom - rect.top;
}
}
Expand Down Expand Up @@ -2671,7 +2671,7 @@ LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) {
RECT rect = new RECT ();
int bits1 = OS.GetWindowLong (handle, OS.GWL_STYLE);
int bits2 = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
OS.AdjustWindowRectEx (rect, bits1, false, bits2);
adjustWindowRectEx(rect, bits1, false, bits2);
lpwp.cy = Math.max (lpwp.cy, rect.bottom - rect.top);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void addVerifyListener (VerifyListener listener) {
bits0 &= ~OS.WS_BORDER;
bits1 |= OS.WS_EX_CLIENTEDGE;
}
OS.AdjustWindowRectEx (rect, bits0, false, bits1);
adjustWindowRectEx(rect, bits0, false, bits1);
width = rect.right - rect.left;
height = rect.bottom - rect.top;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2708,5 +2708,15 @@ int getSystemMetrics(int nIndex) {
return OS.GetSystemMetrics(nIndex);
}

boolean adjustWindowRectEx(RECT lpRect, int dwStyle, boolean bMenu, int dwExStyle) {
/*
* DPI-dependent version of the method was introduced with Windows 10 Version 1607
*/
if (OS.WIN32_BUILD >= OS.WIN32_BUILD_WIN10_1607) {
return OS.AdjustWindowRectExForDpi (lpRect, dwStyle, bMenu, dwExStyle, getZoom());
}
return OS.AdjustWindowRectEx(lpRect, dwStyle, bMenu, dwExStyle);
}


}

0 comments on commit 17f652f

Please sign in to comment.