Skip to content

Commit 47638e4

Browse files
akoch-yattaHeikoKlare
authored andcommitted
[win32] Support scaling of ImageLists with null images
This commit properly creates scaled variants of an ImageList if there a null images contained in the source ImageList Contributes to #62 and #131
1 parent f68bc9b commit 47638e4

File tree

1 file changed

+22
-11
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal

1 file changed

+22
-11
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ImageList.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,20 +334,31 @@ public long getHandle(int targetZoom) {
334334
if (!zoomToHandle.containsKey(targetZoom)) {
335335
int scaledWidth = DPIUtil.scaleUp(DPIUtil.scaleDown(width, this.zoom), targetZoom);
336336
int scaledHeight = DPIUtil.scaleUp(DPIUtil.scaleDown(height, this.zoom), targetZoom);
337-
long handle = OS.ImageList_Create(scaledWidth, scaledHeight, flags, 16, 16);
338-
int count = OS.ImageList_GetImageCount(handle);
339-
for (int i = 0; i < images.length; i++) {
337+
long newImageListHandle = OS.ImageList_Create(scaledWidth, scaledHeight, flags, 16, 16);
338+
int count = OS.ImageList_GetImageCount (handle);
339+
for (int i = 0; i < count; i++) {
340340
Image image = images[i];
341341
if (image != null) {
342-
set(i, image, count, handle, targetZoom);
343-
count++;
342+
set(i, image, i, newImageListHandle, targetZoom);
343+
} else {
344+
addPlaceholderImageToImageList(newImageListHandle, scaledWidth, scaledHeight);
344345
}
345346
}
346-
zoomToHandle.put(targetZoom, handle);
347+
zoomToHandle.put(targetZoom, newImageListHandle);
347348
}
348349
return zoomToHandle.get(targetZoom);
349350
}
350351

352+
private void addPlaceholderImageToImageList(long imageListHandle, int bitmapWidth, int bitmapHeight) {
353+
long hDC = OS.GetDC (0);
354+
if (hDC == 0) SWT.error(SWT.ERROR_NO_HANDLES);
355+
long placeholderBitmapHandle = OS.CreateCompatibleBitmap(hDC, bitmapWidth, bitmapHeight);
356+
if (placeholderBitmapHandle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
357+
OS.ImageList_Add(imageListHandle, placeholderBitmapHandle, placeholderBitmapHandle);
358+
OS.DeleteObject(placeholderBitmapHandle);
359+
OS.ReleaseDC(0, hDC);
360+
}
361+
351362
public Point getImageSize() {
352363
int [] cx = new int [1], cy = new int [1];
353364
OS.ImageList_GetIconSize (handle, cx, cy);
@@ -389,10 +400,10 @@ private void setForAllHandles(int index, Image image, int count) {
389400
zoomToHandle.forEach((zoom, handle) -> set(index, image, count, handle, zoom));
390401
}
391402

392-
void set (int index, Image image, int count, long handle, int zoom) {
403+
void set (int index, Image image, int count, long listHandle, int zoom) {
393404
long hImage = Image.win32_getHandle(image, zoom);
394405
int [] cx = new int [1], cy = new int [1];
395-
OS.ImageList_GetIconSize (handle, cx, cy);
406+
OS.ImageList_GetIconSize (listHandle, cx, cy);
396407
switch (image.type) {
397408
case SWT.BITMAP: {
398409
/*
@@ -443,18 +454,18 @@ void set (int index, Image image, int count, long handle, int zoom) {
443454
break;
444455
}
445456
if (index == count) {
446-
OS.ImageList_Add (handle, hBitmap, hMask);
457+
OS.ImageList_Add (listHandle, hBitmap, hMask);
447458
} else {
448459
/* Note that the mask must always be replaced even for TRANSPARENCY_NONE */
449-
OS.ImageList_Replace (handle, index, hBitmap, hMask);
460+
OS.ImageList_Replace (listHandle, index, hBitmap, hMask);
450461
}
451462
if (hMask != 0) OS.DeleteObject (hMask);
452463
if (hBitmap != hImage) OS.DeleteObject (hBitmap);
453464
break;
454465
}
455466
case SWT.ICON: {
456467
long hIcon = copyIcon (hImage, cx [0], cy [0]);
457-
OS.ImageList_ReplaceIcon (handle, index == count ? -1 : index, hIcon);
468+
OS.ImageList_ReplaceIcon (listHandle, index == count ? -1 : index, hIcon);
458469
OS.DestroyIcon (hIcon);
459470
break;
460471
}

0 commit comments

Comments
 (0)