Skip to content

Commit

Permalink
[win32] Support scaling of ImageLists with null images
Browse files Browse the repository at this point in the history
This commit properly creates scaled variants of an ImageList if there a null images contained in the source ImageList

Contributes to #62 and #131
  • Loading branch information
akoch-yatta authored and HeikoKlare committed Aug 4, 2024
1 parent f68bc9b commit 47638e4
Showing 1 changed file with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,20 +334,31 @@ public long getHandle(int targetZoom) {
if (!zoomToHandle.containsKey(targetZoom)) {
int scaledWidth = DPIUtil.scaleUp(DPIUtil.scaleDown(width, this.zoom), targetZoom);
int scaledHeight = DPIUtil.scaleUp(DPIUtil.scaleDown(height, this.zoom), targetZoom);
long handle = OS.ImageList_Create(scaledWidth, scaledHeight, flags, 16, 16);
int count = OS.ImageList_GetImageCount(handle);
for (int i = 0; i < images.length; i++) {
long newImageListHandle = OS.ImageList_Create(scaledWidth, scaledHeight, flags, 16, 16);
int count = OS.ImageList_GetImageCount (handle);
for (int i = 0; i < count; i++) {
Image image = images[i];
if (image != null) {
set(i, image, count, handle, targetZoom);
count++;
set(i, image, i, newImageListHandle, targetZoom);
} else {
addPlaceholderImageToImageList(newImageListHandle, scaledWidth, scaledHeight);
}
}
zoomToHandle.put(targetZoom, handle);
zoomToHandle.put(targetZoom, newImageListHandle);
}
return zoomToHandle.get(targetZoom);
}

private void addPlaceholderImageToImageList(long imageListHandle, int bitmapWidth, int bitmapHeight) {
long hDC = OS.GetDC (0);
if (hDC == 0) SWT.error(SWT.ERROR_NO_HANDLES);
long placeholderBitmapHandle = OS.CreateCompatibleBitmap(hDC, bitmapWidth, bitmapHeight);
if (placeholderBitmapHandle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
OS.ImageList_Add(imageListHandle, placeholderBitmapHandle, placeholderBitmapHandle);
OS.DeleteObject(placeholderBitmapHandle);
OS.ReleaseDC(0, hDC);
}

public Point getImageSize() {
int [] cx = new int [1], cy = new int [1];
OS.ImageList_GetIconSize (handle, cx, cy);
Expand Down Expand Up @@ -389,10 +400,10 @@ private void setForAllHandles(int index, Image image, int count) {
zoomToHandle.forEach((zoom, handle) -> set(index, image, count, handle, zoom));
}

void set (int index, Image image, int count, long handle, int zoom) {
void set (int index, Image image, int count, long listHandle, int zoom) {
long hImage = Image.win32_getHandle(image, zoom);
int [] cx = new int [1], cy = new int [1];
OS.ImageList_GetIconSize (handle, cx, cy);
OS.ImageList_GetIconSize (listHandle, cx, cy);
switch (image.type) {
case SWT.BITMAP: {
/*
Expand Down Expand Up @@ -443,18 +454,18 @@ void set (int index, Image image, int count, long handle, int zoom) {
break;
}
if (index == count) {
OS.ImageList_Add (handle, hBitmap, hMask);
OS.ImageList_Add (listHandle, hBitmap, hMask);
} else {
/* Note that the mask must always be replaced even for TRANSPARENCY_NONE */
OS.ImageList_Replace (handle, index, hBitmap, hMask);
OS.ImageList_Replace (listHandle, index, hBitmap, hMask);
}
if (hMask != 0) OS.DeleteObject (hMask);
if (hBitmap != hImage) OS.DeleteObject (hBitmap);
break;
}
case SWT.ICON: {
long hIcon = copyIcon (hImage, cx [0], cy [0]);
OS.ImageList_ReplaceIcon (handle, index == count ? -1 : index, hIcon);
OS.ImageList_ReplaceIcon (listHandle, index == count ? -1 : index, hIcon);
OS.DestroyIcon (hIcon);
break;
}
Expand Down

0 comments on commit 47638e4

Please sign in to comment.