From f2a01d8193e0d37c47d0ac2ecc1a5de96cc5349f Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Thu, 8 Feb 2024 08:23:32 +0100 Subject: [PATCH] Adapt calls for image bounds to consider the actual zoom level of the containing control Contributes to https://github.com/eclipse-platform/eclipse.platform.swt/issues/62 and https://github.com/eclipse-platform/eclipse.platform.swt/issues/127 --- .../win32/org/eclipse/swt/internal/ImageList.java | 4 ++-- .../win32/org/eclipse/swt/widgets/Button.java | 9 +++++---- .../win32/org/eclipse/swt/widgets/Display.java | 4 ++-- .../win32/org/eclipse/swt/widgets/Label.java | 4 ++-- .../win32/org/eclipse/swt/widgets/TabFolder.java | 2 +- .../win32/org/eclipse/swt/widgets/ToolItem.java | 4 ++-- .../win32/org/eclipse/swt/widgets/Tree.java | 11 ++++++----- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ImageList.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ImageList.java index 34ccb923d23..c5e0fee69fb 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ImageList.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ImageList.java @@ -46,7 +46,7 @@ public int add (Image image) { index++; } if (count == 0) { - Rectangle rect = image.getBoundsInPixels (); + Rectangle rect = image.getBoundsInPixels(); OS.ImageList_SetIconSize (handle, rect.width, rect.height); } set (index, image, count); @@ -369,7 +369,7 @@ void set (int index, Image image, int count) { * Note that the image size has to match the image list icon size. */ long hBitmap = 0, hMask = 0; - ImageData data = image.getImageData (DPIUtil.getDeviceZoom ()); + ImageData data = image.getImageDataAtCurrentZoom(); switch (data.getTransparencyType ()) { case SWT.TRANSPARENCY_ALPHA: /* diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java index f46fdc8332a..60511ea33b2 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java @@ -283,7 +283,7 @@ int computeLeftMargin () { if ((style & (SWT.PUSH | SWT.TOGGLE)) == 0) return MARGIN; int margin = 0; if (image != null && text.length () != 0) { - Rectangle bounds = image.getBoundsInPixels (); + Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), this.getZoom(), 100); margin += bounds.width + MARGIN * 2; long oldFont = 0; long hDC = OS.GetDC (handle); @@ -345,7 +345,7 @@ else if ((style & SWT.RIGHT) != 0) { boolean hasImage = image != null, hasText = true; if (hasImage) { if (image != null) { - Rectangle rect = image.getBoundsInPixels (); + Rectangle rect = DPIUtil.autoScaleBounds(image.getBounds(), this.getZoom(), 100); width = rect.width; if (hasText && text.length () != 0) { width += MARGIN * 2; @@ -1378,11 +1378,12 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) { GC gc = GC.win32_new (nmcd.hdc, data); int margin = computeLeftMargin(); - int imageWidth = image.getBoundsInPixels().width; + Rectangle imageBounds = DPIUtil.autoScaleBounds(image.getBounds(), this.getZoom(), 100); + int imageWidth = imageBounds.width; left += (imageWidth + (isRadioOrCheck() ? 2 * MARGIN : MARGIN)); // for SWT.RIGHT_TO_LEFT right and left are inverted int x = margin + (isRadioOrCheck() ? radioOrCheckTextPadding : 3); - int y = Math.max (0, (nmcd.bottom - image.getBoundsInPixels().height) / 2); + int y = Math.max (0, (nmcd.bottom - imageBounds.height) / 2); gc.drawImage (image, DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(y)); gc.dispose (); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java index 9fd9570658f..66fea169019 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java @@ -1161,7 +1161,7 @@ static long create32bitDIB (long hBitmap, int alpha, byte [] alphaData, int tran static Image createIcon (Image image) { Device device = image.getDevice (); - ImageData data = image.getImageData (DPIUtil.getDeviceZoom ()); + ImageData data = image.getImageDataAtCurrentZoom(); if (data.alpha == -1 && data.alphaData == null) { ImageData mask = data.getTransparencyMask (); return new Image (device, data, mask); @@ -2464,7 +2464,7 @@ Font getSystemFont (int zoom) { this.systemFont = systemFont; if (systemFont != null) { this.lfSystemFont = systemFont.getFontData()[0].data; - } + } } return systemFont; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java index 40eb777075f..4265a2eb6a4 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Label.java @@ -147,7 +147,7 @@ static int checkStyle (int style) { return new Point (width, height); } if (isImageMode) { - Rectangle rect = image.getBoundsInPixels(); + Rectangle rect = DPIUtil.autoScaleBounds(image.getBounds(), this.getZoom(), 100); width += rect.width; height += rect.height; } else { @@ -553,7 +553,7 @@ void wmDrawChildImage(DRAWITEMSTRUCT struct) { int height = struct.bottom - struct.top; if (width == 0 || height == 0) return; - Rectangle imageRect = image.getBoundsInPixels (); + Rectangle imageRect = DPIUtil.autoScaleBounds(image.getBounds(), getZoom(), 100); int x = 0; if ((style & SWT.CENTER) != 0) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java index 5789bd3a3cc..29cd9b58ea1 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java @@ -455,7 +455,7 @@ int imageIndex (Image image) { */ if (image == null) return -1; if (imageList == null) { - Rectangle bounds = image.getBoundsInPixels (); + Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), this.getZoom(), 100); imageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height); int index = imageList.add (image); long hImageList = imageList.getHandle (); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolItem.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolItem.java index 807e3c30900..f99adecca7b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolItem.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolItem.java @@ -1107,7 +1107,7 @@ void updateImages (boolean enabled) { ImageList hotImageList = parent.getHotImageList (); ImageList disabledImageList = parent.getDisabledImageList(); if (info.iImage == OS.I_IMAGENONE) { - Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), 100, getParent().getZoom()); + Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), getParent().getZoom(), 100); int listStyle = parent.style & SWT.RIGHT_TO_LEFT; if (imageList == null) { imageList = display.getImageListToolBar (listStyle, bounds.width, bounds.height); @@ -1229,7 +1229,7 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac Display display = item.getDisplay(); int listStyle = parent.style & SWT.RIGHT_TO_LEFT; - Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), 100, newZoom); + Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), newZoom, 100); if (parent.getImageList() == null) { parent.setImageList (display.getImageListToolBar (listStyle, bounds.width, bounds.height)); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java index 873fdb6ddf4..faeb117ac77 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java @@ -3739,7 +3739,7 @@ boolean hitTestSelection (long hItem, int x, int y) { int imageIndex (Image image, int index) { if (image == null) return OS.I_IMAGENONE; if (imageList == null) { - Rectangle bounds = image.getBoundsInPixels (); + Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), this.getZoom(), 100); imageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height); } int imageIndex = imageList.indexOf (image); @@ -3763,7 +3763,7 @@ int imageIndex (Image image, int index) { int imageIndexHeader (Image image) { if (image == null) return OS.I_IMAGENONE; if (headerImageList == null) { - Rectangle bounds = image.getBoundsInPixels (); + Rectangle bounds = DPIUtil.autoScaleBounds(image.getBounds(), this.getZoom(), 100); headerImageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height); int index = headerImageList.indexOf (image); if (index == -1) index = headerImageList.add (image); @@ -7540,7 +7540,7 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) { } case OS.NM_CUSTOMDRAW: { if (hdr.hwndFrom == hwndHeader) break; - if (hooks (SWT.MeasureItem)) { + if (hooks (SWT.MeasureItem)) { if (hwndHeader == 0) createParent (); } if (!customDraw && findImageControl () == null) { @@ -7926,9 +7926,10 @@ LRESULT wmNotifyHeader (NMHDR hdr, long wParam, long lParam) { GCData data = new GCData(); data.device = display; GC gc = GC.win32_new (nmcd.hdc, data); - int y = Math.max (0, (nmcd.bottom - columns[i].image.getBoundsInPixels().height) / 2); + Rectangle imageBounds = DPIUtil.autoScaleBounds(columns[i].image.getBounds(), this.getZoom(), 100); + int y = Math.max (0, (nmcd.bottom - imageBounds.height) / 2); gc.drawImage (columns[i].image, DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(y)); - x += columns[i].image.getBoundsInPixels().width + 12; + x += imageBounds.width + 12; gc.dispose (); }