From fd0a9d811db874921a2d6bdaa3139c22f8b91b63 Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Wed, 4 Sep 2024 17:31:20 +0200 Subject: [PATCH] fix for coords --- .../org/eclipse/swt/widgets/Display.java | 40 +++++++++++-------- .../win32/org/eclipse/swt/widgets/Shell.java | 4 +- 2 files changed, 26 insertions(+), 18 deletions(-) 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 b54c7538c36..4c764cd1637 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 @@ -3031,11 +3031,11 @@ public Point map (Control from, Control to, int x, int y) { checkDevice (); Point mappedPointInPoints; if (from == null) { - Point mappedPointInpixels = mapInPixels(from, to, translateLocationInPixelsInDisplayCoordinateSystem(x, y)); + Point mappedPointInpixels = mapInPixels(from, to, getPixelsFromPoint(to.getMonitor(), x, y)); mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); } else if (to == null) { Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom())); - mappedPointInPoints = translateLocationInPointInDisplayCoordinateSystem(mappedPointInpixels.x, mappedPointInpixels.y); + mappedPointInPoints = getPointFromPixels(from.getMonitor(), mappedPointInpixels.x, mappedPointInpixels.y); } else { Point mappedPointInpixels = mapInPixels(from, to, DPIUtil.scaleUp(new Point(x, y), from.getZoom())); mappedPointInPoints = DPIUtil.scaleDown(mappedPointInpixels, to.getZoom()); @@ -3144,11 +3144,11 @@ public Rectangle map (Control from, Control to, int x, int y, int width, int hei checkDevice (); Rectangle mappedRectangleInPoints; if (from == null) { - Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height)); + Rectangle mappedRectangleInPixels = mapInPixels(from, to, translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, to.getMonitor())); mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); } else if (to == null) { Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom())); - mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height); + mappedRectangleInPoints = translateRectangleInPointsInDisplayCoordinateSystem(mappedRectangleInPixels.x, mappedRectangleInPixels.y, mappedRectangleInPixels.width, mappedRectangleInPixels.height, from.getMonitor()); } else { Rectangle mappedRectangleInPixels = mapInPixels(from, to, DPIUtil.scaleUp(new Rectangle(x, y, width, height), from.getZoom())); mappedRectangleInPoints = DPIUtil.scaleDown(mappedRectangleInPixels, to.getZoom()); @@ -3181,8 +3181,12 @@ Point translateLocationInPointInDisplayCoordinateSystem(int x, int y) { return getPointFromPixels(monitor, x, y); } -Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height) { +Rectangle translateRectangleInPixelsInDisplayCoordinateSystemByContainment(int x, int y, int width, int height) { Monitor monitor = getContainingMonitor(x, y, width, height); + return translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height, monitor); +} + +private Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int width, int height, Monitor monitor) { Point topLeft = getPixelsFromPoint(monitor, x, y); int zoom = DPIUtil.getZoomForAutoscaleProperty(monitor.zoom); int widthInPixels = DPIUtil.scaleUp(width, zoom); @@ -3190,8 +3194,12 @@ Rectangle translateRectangleInPixelsInDisplayCoordinateSystem(int x, int y, int return new Rectangle(topLeft.x, topLeft.y, widthInPixels, heightInPixels); } -Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels, int heightInPixels) { +Rectangle translateRectangleInPointsInDisplayCoordinateSystemByContainment(int x, int y, int widthInPixels, int heightInPixels) { Monitor monitor = getContainingMonitorInPixelsCoordiate(x, y, widthInPixels, heightInPixels); + return translateRectangleInPointsInDisplayCoordinateSystem(x, y, widthInPixels, heightInPixels, monitor); +} + +private Rectangle translateRectangleInPointsInDisplayCoordinateSystem(int x, int y, int widthInPixels, int heightInPixels, Monitor monitor) { Point topLeft = getPointFromPixels(monitor, x, y); int zoom = DPIUtil.getZoomForAutoscaleProperty(monitor.zoom); int width = DPIUtil.scaleDown(widthInPixels, zoom); @@ -5355,7 +5363,7 @@ private boolean setDPIAwareness(int desiredDpiAwareness) { return true; } -Monitor getContainingMonitor(int x, int y) { +private Monitor getContainingMonitor(int x, int y) { Monitor[] monitors = getMonitors(); for (Monitor currentMonitor : monitors) { Rectangle clientArea = currentMonitor.getClientArea(); @@ -5366,7 +5374,7 @@ Monitor getContainingMonitor(int x, int y) { return getPrimaryMonitor(); } -Monitor getContainingMonitor(int x, int y, int width, int height) { +private Monitor getContainingMonitor(int x, int y, int width, int height) { Rectangle rectangle = new Rectangle(x, y, width, height); Monitor[] monitors = getMonitors(); Monitor selectedMonitor = getPrimaryMonitor(); @@ -5383,7 +5391,7 @@ Monitor getContainingMonitor(int x, int y, int width, int height) { return selectedMonitor; } -Monitor getContainingMonitorInPixelsCoordiate(int xInPixels, int yInPixels) { +private Monitor getContainingMonitorInPixelsCoordiate(int xInPixels, int yInPixels) { Monitor[] monitors = getMonitors(); for (Monitor current : monitors) { Rectangle clientArea = getMonitorClientAreaInPixels(current); @@ -5394,7 +5402,7 @@ Monitor getContainingMonitorInPixelsCoordiate(int xInPixels, int yInPixels) { return getPrimaryMonitor(); } -Monitor getContainingMonitorInPixelsCoordiate(int xInPixels, int yInPixels, int widthInPixels, int heightInPixels) { +private Monitor getContainingMonitorInPixelsCoordiate(int xInPixels, int yInPixels, int widthInPixels, int heightInPixels) { Rectangle rectangleInPixels = new Rectangle(xInPixels, yInPixels, widthInPixels, heightInPixels); Monitor[] monitors = getMonitors(); Monitor selectedMonitor = getPrimaryMonitor(); @@ -5420,15 +5428,15 @@ private Rectangle getMonitorClientAreaInPixels(Monitor monitor) { private Point getPixelsFromPoint(Monitor monitor, int x, int y) { int zoom = DPIUtil.getZoomForAutoscaleProperty(monitor.zoom); - x = DPIUtil.scaleUp(x - monitor.clientX, zoom) + monitor.clientX; - y = DPIUtil.scaleUp(y - monitor.clientY, zoom) + monitor.clientY; - return new Point(x, y); + int mappedX = DPIUtil.scaleUp(x - monitor.clientX, zoom) + monitor.clientX; + int mappedY = DPIUtil.scaleUp(y - monitor.clientY, zoom) + monitor.clientY; + return new Point(mappedX, mappedY); } private Point getPointFromPixels(Monitor monitor, int x, int y) { int zoom = DPIUtil.getZoomForAutoscaleProperty(monitor.zoom); - x = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX; - y = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY; - return new Point(x, y); + int mappedX = DPIUtil.scaleDown(x - monitor.clientX, zoom) + monitor.clientX; + int mappedY = DPIUtil.scaleDown(y - monitor.clientY, zoom) + monitor.clientY; + return new Point(mappedX, mappedY); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java index 8141e2b2734..287646dad6a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Shell.java @@ -1569,7 +1569,7 @@ public void setAlpha (int alpha) { @Override public Rectangle getBounds() { Rectangle boundsInPixels = getBoundsInPixels(); - return display.translateRectangleInPointsInDisplayCoordinateSystem(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height); + return display.translateRectangleInPointsInDisplayCoordinateSystemByContainment(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height); } @Override @@ -1598,7 +1598,7 @@ public void setBounds(Rectangle rect) { @Override public void setBounds(int x, int y, int width, int height) { - Rectangle boundsInPixels = display.translateRectangleInPixelsInDisplayCoordinateSystem(x, y, width, height); + Rectangle boundsInPixels = display.translateRectangleInPixelsInDisplayCoordinateSystemByContainment(x, y, width, height); setBoundsInPixels(boundsInPixels.x, boundsInPixels.y, boundsInPixels.width, boundsInPixels.height); }