Skip to content

Commit

Permalink
eclipse-platform#1130: [Win32]: Program.getImageData is broken for ".…
Browse files Browse the repository at this point in the history
…exe" extension

Before commit 97ca656 `SHGetFileInfo` was used initially to get the
icon. This caused problems for some icons (eclipse-platform#715). The fix in commit
97ca656 removed the `SHGetFileInfo` invocation which caused a new
problem that for some file extensions, e.g. `.exe`, no icon was returned
any more.

This fix re-introduces the `SHGetFileInfo`, but only uses it if
`ExtractIconEx` did not return an icon.

See:
eclipse-platform#1030: eclipse-platform#1130
eclipse-platform#715: eclipse-platform#715
  • Loading branch information
Thomas Singer committed Mar 21, 2024
1 parent 103b10e commit 5500a22
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class Program {
String name;
String command;
String iconName;
String extension;
static final String [] ARGUMENTS = new String [] {"%1", "%l", "%L"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

/**
Expand Down Expand Up @@ -101,6 +102,7 @@ public static Program findProgram (String extension) {
program.name = name;
program.command = command;
program.iconName = iconName;
program.extension = extension;
}
return program;
}
Expand Down Expand Up @@ -182,7 +184,7 @@ static String getKeyValue (String string, boolean expand) {
return result;
}

static Program getProgram (String key) {
static Program getProgram (String key, String extension) {

/* Name */
String name = getKeyValue (key, false);
Expand All @@ -208,6 +210,7 @@ static Program getProgram (String key) {
program.name = name;
program.command = command;
program.iconName = iconName;
program.extension = extension;
return program;
}

Expand All @@ -233,7 +236,7 @@ static Program getProgram (String key) {
//map paths to programs in parallel which takes now ~ 4/5 of time:
ConcurrentHashMap<String, Program> programs = new ConcurrentHashMap<>(paths.size());
paths.stream().parallel().forEach(path -> {
Program program = getProgram(path); // getProgram takes most time
Program program = getProgram(path, null); // getProgram takes most time
if (program != null) {
programs.put(path, program);
}
Expand Down Expand Up @@ -396,8 +399,22 @@ public ImageData getImageData (int zoom) {
TCHAR lpszFile = new TCHAR (0, fileName, true);
long [] phiconSmall = new long[1], phiconLarge = null;
OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1);
if (phiconSmall [0] == 0) return null;
Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0]);

long hIcon = phiconSmall [0];

if (hIcon == 0) {
SHFILEINFO shfi = new SHFILEINFO ();
int flags = OS.SHGFI_ICON | OS.SHGFI_SMALLICON | OS.SHGFI_USEFILEATTRIBUTES;
TCHAR pszPath = new TCHAR (0, extension, true);
OS.SHGetFileInfo (pszPath.chars, OS.FILE_ATTRIBUTE_NORMAL, shfi, SHFILEINFO.sizeof, flags);

hIcon = shfi.hIcon;

if (hIcon == 0) return null;
}


Image image = Image.win32_new (null, SWT.ICON, hIcon);
// Windows API returns image data according to primary monitor zoom factor
// rather than at original scaling
int nativeZoomFactor = 100 * Display.getCurrent().getPrimaryMonitor().getZoom() / DPIUtil.getDeviceZoom();
Expand Down

0 comments on commit 5500a22

Please sign in to comment.