Skip to content

Commit f5d50c9

Browse files
committed
Improve scaling of SystemImage #62
This commit contributes to better scaling of Images obtained from Display::getSystemImage using better windows API LoadIconWithScaleDown for scaling. The new API also changes the old system icons to the new Windows System Icons. Contributes to #62 and #127
1 parent df1c412 commit f5d50c9

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4725,6 +4725,22 @@ JNIEXPORT jint JNICALL OS_NATIVE(LoadIconMetric)
47254725
}
47264726
#endif
47274727

4728+
#ifndef NO_LoadIconWithScaleDown
4729+
JNIEXPORT jlong JNICALL OS_NATIVE(LoadIconWithScaleDown)
4730+
(JNIEnv *env, jclass that, jlong arg0, jlong arg1, jint arg2, jint arg3, jlongArray arg4)
4731+
{
4732+
jlong *lparg4=NULL;
4733+
jlong rc = 0;
4734+
OS_NATIVE_ENTER(env, that, LoadIconWithScaleDown_FUNC);
4735+
if (arg4) if ((lparg4 = (*env)->GetLongArrayElements(env, arg4, NULL)) == NULL) goto fail;
4736+
rc = (jlong)LoadIconWithScaleDown((HINSTANCE)arg0, (LPWSTR)arg1, arg2, arg3, (HICON *)lparg4);
4737+
fail:
4738+
if (arg4 && lparg4) (*env)->ReleaseLongArrayElements(env, arg4, lparg4, 0);
4739+
OS_NATIVE_EXIT(env, that, LoadIconWithScaleDown_FUNC);
4740+
return rc;
4741+
}
4742+
#endif
4743+
47284744
#ifndef NO_LoadImage
47294745
JNIEXPORT jlong JNICALL OS_NATIVE(LoadImage)
47304746
(JNIEnv *env, jclass that, jlong arg0, jlong arg1, jint arg2, jint arg3, jint arg4, jint arg5)

bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os_stats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ typedef enum {
365365
LoadCursor_FUNC,
366366
LoadIcon_FUNC,
367367
LoadIconMetric_FUNC,
368+
LoadIconWithScaleDown_FUNC,
368369
LoadImage_FUNC,
369370
LoadKeyboardLayout_FUNC,
370371
LocalFree_FUNC,

bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,6 +3298,12 @@ public static int HRESULT_FROM_WIN32(int x) {
32983298
* @param lpszName cast=(LPWSTR)
32993299
*/
33003300
public static final native long LoadImage (long hinst, long lpszName, int uType, int cxDesired, int cyDesired, int fuLoad);
3301+
/**
3302+
* @param hinst cast=(HINSTANCE)
3303+
* @param lpszName cast=(LPWSTR)
3304+
* @param phico cast=(HICON *)
3305+
*/
3306+
public static final native long LoadIconWithScaleDown (long hinst, long lpszName, int cxDesired, int cyDesired, long [] phico);
33013307
/**
33023308
* @param pwszKLID cast=(LPCWSTR)
33033309
* @param Flags cast=(UINT)

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ public class Display extends Device implements Executor {
530530
static int SWT_RESTORECARET;
531531
static int DI_GETDRAGIMAGE;
532532
static int SWT_OPENDOC;
533+
static int ICON_SIZE_AT_100;
533534

534535
/* Skinning support */
535536
Widget [] skinList = new Widget [GROW_SIZE];
@@ -554,9 +555,16 @@ public class Display extends Device implements Executor {
554555
}
555556

556557
static {
557-
CommonWidgetsDPIChangeHandlers.registerCommonHandlers();
558+
if (OS.WIN32_BUILD >= OS.WIN32_BUILD_WIN10_1607) {
559+
ICON_SIZE_AT_100 = OS.GetSystemMetricsForDpi(OS.SM_CXICON, 100);
560+
} else {
561+
ICON_SIZE_AT_100 = 16;
562+
}
558563
}
559564

565+
static {
566+
CommonWidgetsDPIChangeHandlers.registerCommonHandlers();
567+
}
560568

561569
/*
562570
* TEMPORARY CODE.
@@ -2543,33 +2551,40 @@ Font getSystemFont (int zoom) {
25432551
*/
25442552
public Image getSystemImage (int id) {
25452553
checkDevice ();
2546-
int primaryMonitorNativeZoom = getPrimaryMonitor().getZoom();
25472554
switch (id) {
25482555
case SWT.ICON_ERROR: {
25492556
if (errorImage != null) return errorImage;
2550-
long hIcon = OS.LoadImage (0, OS.OIC_HAND, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2551-
return errorImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2557+
return errorImage = new Image(this, getImageDataProviderForIcon(OS.OIC_HAND));
25522558
}
25532559
case SWT.ICON_WORKING:
25542560
case SWT.ICON_INFORMATION: {
25552561
if (infoImage != null) return infoImage;
2556-
long hIcon = OS.LoadImage (0, OS.OIC_INFORMATION, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2557-
return infoImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2562+
return infoImage = new Image(this, getImageDataProviderForIcon(OS.OIC_INFORMATION));
25582563
}
25592564
case SWT.ICON_QUESTION: {
25602565
if (questionImage != null) return questionImage;
2561-
long hIcon = OS.LoadImage (0, OS.OIC_QUES, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2562-
return questionImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2566+
return questionImage = new Image(this, getImageDataProviderForIcon(OS.OIC_QUES));
25632567
}
25642568
case SWT.ICON_WARNING: {
25652569
if (warningIcon != null) return warningIcon;
2566-
long hIcon = OS.LoadImage (0, OS.OIC_BANG, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2567-
return warningIcon = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2570+
return warningIcon = new Image(this, getImageDataProviderForIcon(OS.OIC_BANG));
25682571
}
25692572
}
25702573
return null;
25712574
}
25722575

2576+
private ImageDataProvider getImageDataProviderForIcon(int iconName) {
2577+
return zoom -> {
2578+
int scaledIconSize = DPIUtil.scaleUp(ICON_SIZE_AT_100, zoom);
2579+
long [] hIcon = new long [1];
2580+
OS.LoadIconWithScaleDown(0, iconName, scaledIconSize, scaledIconSize, hIcon);
2581+
Image image = Image.win32_new (this, SWT.ICON, hIcon[0], zoom);
2582+
ImageData imageData = image.getImageData(zoom);
2583+
image.dispose();
2584+
return imageData;
2585+
};
2586+
}
2587+
25732588
/**
25742589
* Returns the single instance of the system-provided menu for the application, or
25752590
* <code>null</code> on platforms where no menu is provided for the application.

0 commit comments

Comments
 (0)