Skip to content

Commit f48b2ad

Browse files
amartya4256HeikoKlare
authored andcommitted
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 9b1bcf4 commit f48b2ad

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
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
@@ -4745,6 +4745,22 @@ JNIEXPORT jint JNICALL OS_NATIVE(LoadIconMetric)
47454745
}
47464746
#endif
47474747

4748+
#ifndef NO_LoadIconWithScaleDown
4749+
JNIEXPORT jlong JNICALL OS_NATIVE(LoadIconWithScaleDown)
4750+
(JNIEnv *env, jclass that, jlong arg0, jlong arg1, jint arg2, jint arg3, jlongArray arg4)
4751+
{
4752+
jlong *lparg4=NULL;
4753+
jlong rc = 0;
4754+
OS_NATIVE_ENTER(env, that, LoadIconWithScaleDown_FUNC);
4755+
if (arg4) if ((lparg4 = (*env)->GetLongArrayElements(env, arg4, NULL)) == NULL) goto fail;
4756+
rc = (jlong)LoadIconWithScaleDown((HINSTANCE)arg0, (LPWSTR)arg1, arg2, arg3, (HICON *)lparg4);
4757+
fail:
4758+
if (arg4 && lparg4) (*env)->ReleaseLongArrayElements(env, arg4, lparg4, 0);
4759+
OS_NATIVE_EXIT(env, that, LoadIconWithScaleDown_FUNC);
4760+
return rc;
4761+
}
4762+
#endif
4763+
47484764
#ifndef NO_LoadImage
47494765
JNIEXPORT jlong JNICALL OS_NATIVE(LoadImage)
47504766
(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
@@ -366,6 +366,7 @@ typedef enum {
366366
LoadCursor_FUNC,
367367
LoadIcon_FUNC,
368368
LoadIconMetric_FUNC,
369+
LoadIconWithScaleDown_FUNC,
369370
LoadImage_FUNC,
370371
LoadKeyboardLayout_FUNC,
371372
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
@@ -3302,6 +3302,12 @@ public static int HRESULT_FROM_WIN32(int x) {
33023302
* @param lpszName cast=(LPWSTR)
33033303
*/
33043304
public static final native long LoadImage (long hinst, long lpszName, int uType, int cxDesired, int cyDesired, int fuLoad);
3305+
/**
3306+
* @param hinst cast=(HINSTANCE)
3307+
* @param lpszName cast=(LPWSTR)
3308+
* @param phico cast=(HICON *)
3309+
*/
3310+
public static final native long LoadIconWithScaleDown (long hinst, long lpszName, int cxDesired, int cyDesired, long [] phico);
33053311
/**
33063312
* @param pwszKLID cast=(LPCWSTR)
33073313
* @param Flags cast=(UINT)

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ public class Display extends Device implements Executor {
530530
static int SWT_RESTORECARET;
531531
static int DI_GETDRAGIMAGE;
532532
static int SWT_OPENDOC;
533+
private static int ICON_SIZE_AT_100 = retrieveDefaultIconSize();
534+
535+
private static int retrieveDefaultIconSize() {
536+
if (OS.WIN32_BUILD >= OS.WIN32_BUILD_WIN10_1607) {
537+
return OS.GetSystemMetricsForDpi(OS.SM_CXICON, DPIUtil.mapZoomToDPI(100));
538+
} else {
539+
return 32;
540+
}
541+
}
533542

534543
/* Skinning support */
535544
Widget [] skinList = new Widget [GROW_SIZE];
@@ -2552,33 +2561,44 @@ Font getSystemFont (int zoom) {
25522561
*/
25532562
public Image getSystemImage (int id) {
25542563
checkDevice ();
2555-
int primaryMonitorNativeZoom = getPrimaryMonitor().getZoom();
25562564
switch (id) {
25572565
case SWT.ICON_ERROR: {
25582566
if (errorImage != null) return errorImage;
2559-
long hIcon = OS.LoadImage (0, OS.OIC_HAND, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2560-
return errorImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2567+
errorImage = new Image(this, getImageDataProviderForIcon(OS.OIC_HAND));
2568+
return errorImage;
25612569
}
25622570
case SWT.ICON_WORKING:
25632571
case SWT.ICON_INFORMATION: {
25642572
if (infoImage != null) return infoImage;
2565-
long hIcon = OS.LoadImage (0, OS.OIC_INFORMATION, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2566-
return infoImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2573+
infoImage = new Image(this, getImageDataProviderForIcon(OS.OIC_INFORMATION));
2574+
return infoImage;
25672575
}
25682576
case SWT.ICON_QUESTION: {
25692577
if (questionImage != null) return questionImage;
2570-
long hIcon = OS.LoadImage (0, OS.OIC_QUES, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2571-
return questionImage = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2578+
questionImage = new Image(this, getImageDataProviderForIcon(OS.OIC_QUES));
2579+
return questionImage;
25722580
}
25732581
case SWT.ICON_WARNING: {
25742582
if (warningIcon != null) return warningIcon;
2575-
long hIcon = OS.LoadImage (0, OS.OIC_BANG, OS.IMAGE_ICON, 0, 0, OS.LR_SHARED);
2576-
return warningIcon = Image.win32_new (this, SWT.ICON, hIcon, primaryMonitorNativeZoom);
2583+
warningIcon = new Image(this, getImageDataProviderForIcon(OS.OIC_BANG));
2584+
return warningIcon;
25772585
}
25782586
}
25792587
return null;
25802588
}
25812589

2590+
private ImageDataProvider getImageDataProviderForIcon(int iconName) {
2591+
return zoom -> {
2592+
int scaledIconSize = DPIUtil.scaleUp(ICON_SIZE_AT_100, zoom);
2593+
long [] hIcon = new long [1];
2594+
OS.LoadIconWithScaleDown(0, iconName, scaledIconSize, scaledIconSize, hIcon);
2595+
Image image = Image.win32_new (this, SWT.ICON, hIcon[0], zoom);
2596+
ImageData imageData = image.getImageData(zoom);
2597+
image.dispose();
2598+
return imageData;
2599+
};
2600+
}
2601+
25822602
/**
25832603
* Returns the single instance of the system-provided menu for the application, or
25842604
* <code>null</code> on platforms where no menu is provided for the application.

0 commit comments

Comments
 (0)