Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 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, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2023 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 @@ -3258,6 +3258,18 @@ JNIEXPORT jint JNICALL OS_NATIVE(GetSystemMetrics)
}
#endif

#ifndef NO_GetSystemMetricsForDpi
JNIEXPORT jint JNICALL OS_NATIVE(GetSystemMetricsForDpi)
(JNIEnv *env, jclass that, jint arg0, jint arg1)
{
jint rc = 0;
OS_NATIVE_ENTER(env, that, GetSystemMetricsForDpi_FUNC);
rc = (jint)GetSystemMetricsForDpi(arg0, arg1);
OS_NATIVE_EXIT(env, that, GetSystemMetricsForDpi_FUNC);
return rc;
}
#endif

#ifndef NO_GetTextColor
JNIEXPORT jint JNICALL OS_NATIVE(GetTextColor)
(JNIEnv *env, jclass that, jlong arg0)
Expand Down Expand Up @@ -9128,6 +9140,22 @@ JNIEXPORT jboolean JNICALL OS_NATIVE(SystemParametersInfo__II_3II)
}
#endif

#ifndef NO_SystemParametersInfoForDpi
JNIEXPORT jboolean JNICALL OS_NATIVE(SystemParametersInfoForDpi)
(JNIEnv *env, jclass that, jint arg0, jint arg1, jobject arg2, jint arg3, jint arg4)
{
NONCLIENTMETRICS _arg2, *lparg2=NULL;
jboolean rc = 0;
OS_NATIVE_ENTER(env, that, SystemParametersInfoForDpi_FUNC);
if (arg2) if ((lparg2 = getNONCLIENTMETRICSFields(env, arg2, &_arg2)) == NULL) goto fail;
rc = (jboolean)SystemParametersInfoForDpi(arg0, arg1, lparg2, arg3, arg4);
fail:
if (arg2 && lparg2) setNONCLIENTMETRICSFields(env, arg2, lparg2);
OS_NATIVE_EXIT(env, that, SystemParametersInfoForDpi_FUNC);
return rc;
}
#endif

#ifndef NO_TBBUTTONINFO_1sizeof
JNIEXPORT jint JNICALL OS_NATIVE(TBBUTTONINFO_1sizeof)
(JNIEnv *env, jclass that)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ typedef enum {
GetSysColorBrush_FUNC,
GetSystemMenu_FUNC,
GetSystemMetrics_FUNC,
GetSystemMetricsForDpi_FUNC,
GetTextColor_FUNC,
GetTextExtentPoint32_FUNC,
GetTextMetrics_FUNC,
Expand Down Expand Up @@ -680,6 +681,7 @@ typedef enum {
SystemParametersInfo__IILorg_eclipse_swt_internal_win32_NONCLIENTMETRICS_2I_FUNC,
SystemParametersInfo__IILorg_eclipse_swt_internal_win32_RECT_2I_FUNC,
SystemParametersInfo__II_3II_FUNC,
SystemParametersInfoForDpi_FUNC,
TBBUTTONINFO_1sizeof_FUNC,
TBBUTTON_1sizeof_FUNC,
TCHITTESTINFO_1sizeof_FUNC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class OS extends C {
/**
* Values taken from https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
*/
public static final int WIN32_BUILD_WIN10_1607 = 14393; // "Windows 10 August 2016 Update"
public static final int WIN32_BUILD_WIN10_1809 = 17763; // "Windows 10 October 2018 Update"
public static final int WIN32_BUILD_WIN10_2004 = 19041; // "Windows 10 May 2020 Update"
public static final int WIN32_BUILD_WIN11_21H2 = 22000; // Initial Windows 11 release
Expand Down Expand Up @@ -2975,6 +2976,7 @@ public static int HRESULT_FROM_WIN32(int x) {
/** @param hWnd cast=(HWND) */
public static final native long GetSystemMenu (long hWnd, boolean bRevert);
public static final native int GetSystemMetrics (int nIndex);
public static final native int GetSystemMetricsForDpi (int nIndex, int dpi);
/** @param hDC cast=(HDC) */
public static final native int GetTextColor (long hDC);
/**
Expand Down Expand Up @@ -4436,6 +4438,7 @@ public static int HRESULT_FROM_WIN32(int x) {
public static final native boolean SystemParametersInfo (int uiAction, int uiParam, RECT pvParam, int fWinIni);
public static final native boolean SystemParametersInfo (int uiAction, int uiParam, NONCLIENTMETRICS pvParam, int fWinIni);
public static final native boolean SystemParametersInfo (int uiAction, int uiParam, int [] pvParam, int fWinIni);
public static final native boolean SystemParametersInfoForDpi (int uiAction, int uiParam, NONCLIENTMETRICS pvParam, int fWinIni, int dpi);
/**
* @param lpKeyState cast=(PBYTE)
* @param pwszBuff cast=(LPWSTR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DPIUtil {
private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
private static AutoScaleMethod autoScaleMethodSetting = AutoScaleMethod.AUTO;
private static AutoScaleMethod autoScaleMethod = AutoScaleMethod.NEAREST;
private static boolean autoScaleOnRuntime = false;

private static String autoScaleValue;
private static boolean useCairoAutoScale = false;
Expand Down Expand Up @@ -84,6 +85,16 @@ private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
* <a href="https://bugs.eclipse.org/493455">bug 493455</a>.
*/
private static final String SWT_AUTOSCALE_METHOD = "swt.autoScale.method";

/**
* System property to enable to scale the applicaiton on runtime
* when a DPI change is detected.
* <ul>
* <li>"true": the application is scaled on DPI changes</li>
* <li>"false": the application will remain in its initial scaling</li>
* </ul>
*/
private static final String SWT_AUTOSCALE_UPDATE_ON_RUNTIME = "swt.autoScale.updateOnRuntime";
static {
autoScaleValue = System.getProperty (SWT_AUTOSCALE);

Expand All @@ -95,6 +106,9 @@ private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
autoScaleMethod = autoScaleMethodSetting = AutoScaleMethod.SMOOTH;
}
}

String updateOnRuntimeValue = System.getProperty (SWT_AUTOSCALE_UPDATE_ON_RUNTIME);
autoScaleOnRuntime = Boolean.parseBoolean(updateOnRuntimeValue);
}

/**
Expand Down Expand Up @@ -227,6 +241,11 @@ public static ImageData autoScaleImageData (Device device, final ImageData image
return autoScaleImageData(device, imageData, scaleFactor);
}


public static ImageData autoScaleImageData (Device device, final ElementAtZoom<ImageData> elementAtZoom, int targetZoom) {
return autoScaleImageData(device, elementAtZoom.element(), targetZoom, elementAtZoom.zoom());
}

private static ImageData autoScaleImageData (Device device, final ImageData imageData, float scaleFactor) {
// Guards are already implemented in callers: if (deviceZoom == 100 || imageData == null || scaleFactor == 1.0f) return imageData;
int width = imageData.width;
Expand Down Expand Up @@ -261,7 +280,7 @@ private static ImageData autoScaleImageData (Device device, final ImageData imag
* Returns a new rectangle as per the scaleFactor.
*/
public static Rectangle autoScaleBounds (Rectangle rect, int targetZoom, int currentZoom) {
if (deviceZoom == 100 || rect == null || targetZoom == currentZoom) return rect;
if (rect == null || targetZoom == currentZoom) return rect;
float scaleFactor = ((float)targetZoom) / (float)currentZoom;
Rectangle returnRect = new Rectangle (0,0,0,0);
returnRect.x = Math.round (rect.x * scaleFactor);
Expand All @@ -287,6 +306,10 @@ public static ImageData autoScaleUp (Device device, final ImageData imageData) {
return autoScaleImageData(device, imageData, 100);
}

public static ImageData autoScaleUp (Device device, final ElementAtZoom<ImageData> elementAtZoom) {
return autoScaleImageData(device, elementAtZoom.element(), elementAtZoom.zoom());
}

public static int[] autoScaleUp(int[] pointArray) {
if (deviceZoom == 100 || pointArray == null) return pointArray;
float scaleFactor = getScalingFactor ();
Expand All @@ -311,6 +334,14 @@ public static int autoScaleUp (int size) {
return Math.round (size * scaleFactor);
}

/**
* Auto-scale up int dimensions to match the given zoom level
*/
public static int autoScaleUp (int size, int zoom) {
float scaleFactor = getScalingFactor (zoom);
return Math.round (size * scaleFactor);
}

/**
* Auto-scale up int dimensions using Native DPI
*/
Expand Down Expand Up @@ -388,10 +419,18 @@ public static Rectangle autoScaleUp (Drawable drawable, Rectangle rect) {
* @return float scaling factor
*/
private static float getScalingFactor () {
return getScalingFactor(deviceZoom);
}

/**
* Returns scaling factor from the given device zoom
* @return float scaling factor
*/
private static float getScalingFactor (int shellDeviceZoom) {
if (useCairoAutoScale) {
return 1;
}
return deviceZoom / 100f;
return shellDeviceZoom / 100f;
}

/**
Expand All @@ -405,6 +444,17 @@ public static int mapDPIToZoom (int dpi) {
return roundedZoom;
}

/**
* Compute the DPI value value based on the zoom.
*
* @return DPI
*/
public static int mapZoomToDPI (int zoom) {
double dpi = (double) zoom / 100 * DPI_ZOOM_100;
int roundedDpi = (int) Math.round (dpi);
return roundedDpi;
}

/**
* Represents an element, such as some image data, at a specific zoom level.
*
Expand Down Expand Up @@ -479,6 +529,10 @@ private static <T> ElementAtZoom<T> getElementAtZoom(Function<Integer, T> elemen
return null;
}

public static int getNativeDeviceZoom() {
return nativeDeviceZoom;
}

public static int getDeviceZoom() {
return deviceZoom;
}
Expand Down Expand Up @@ -535,6 +589,10 @@ public static int getZoomForAutoscaleProperty (int nativeDeviceZoom) {
return zoom;
}

public static boolean isAutoScaleOnRuntimeActive() {
return autoScaleOnRuntime;
}

/**
* AutoScale ImageDataProvider.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ int computePixels(float height) {
}

float computePoints(LOGFONT logFont, long hFont) {
return computePoints(logFont, hFont, -1);
}

float computePoints(LOGFONT logFont, long hFont, int currentFontDPI) {
long hDC = internal_new_GC (null);
int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY);
int pixels = 0;
Expand All @@ -284,7 +288,14 @@ float computePoints(LOGFONT logFont, long hFont) {
pixels = -logFont.lfHeight;
}
internal_dispose_GC (hDC, null);
return pixels * 72f / logPixelsY;
float adjustedZoomFactor = 1.0f;
if (currentFontDPI > 0) {
// as Device::computePoints will always return point on the basis of the
// primary monitor zoom, a custom zoomFactor must be calculated if the font
// is used for a different zoom level
adjustedZoomFactor *= (float) logPixelsY / (float) currentFontDPI;
}
return adjustedZoomFactor * pixels * 72f / logPixelsY;
}

/**
Expand Down Expand Up @@ -905,6 +916,7 @@ protected void release () {
fontCollection = 0;
Gdip.GdiplusShutdown (gdipToken[0]);
}
SWTFontProvider.disposeFontRegistry(this);
gdipToken = null;
scripts = null;
logFonts = null;
Expand Down Expand Up @@ -946,5 +958,4 @@ void setEnableAutoScaling(boolean value) {
protected int getDeviceZoom () {
return DPIUtil.mapDPIToZoom ( _getDPIx ());
}

}
Loading