diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java index 3e8434706ab..b948bf2d51e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/CoordinateSystemMapperTests.java @@ -218,6 +218,31 @@ void translateRectangleInPixelsForZeroSize(CoordinateSystemMapper mapper) { assertEquals(rectInPts, mapper.translateFromDisplayCoordinates(rectInPxs)); } + @ParameterizedTest + @MethodSource("provideCoordinateSystemMappers") + void translateBoundsToSecondMonitor(CoordinateSystemMapper mapper) { + final int OFFSET = 30; + final int SIZE = OFFSET * 3; + setupMonitors(mapper); + Rectangle currentPosition = new Rectangle.WithMonitor(monitors[1].getClientArea().x - OFFSET, 0, SIZE, SIZE, + monitors[1]); + Rectangle result = mapper.translateFromDisplayCoordinates(currentPosition); + assertTrue(monitors[1].getClientArea().intersects(result)); + } + + @ParameterizedTest + @MethodSource("provideCoordinateSystemMappers") + void translatePointsToSecondMonitor(CoordinateSystemMapper mapper) { + final int OFFSET = 30; + final int SIZE = OFFSET * 3; + setupMonitors(mapper); + Point currentPosition = new Point.WithMonitor(monitors[1].getClientArea().x - OFFSET, 0, monitors[1]); + Point size = new Point(SIZE, SIZE); + Point result = mapper.translateFromDisplayCoordinates(currentPosition); + Rectangle resultRect = new Rectangle(result.x, result.y, size.x, size.y); + assertTrue(monitors[1].getClientArea().intersects(resultRect)); + } + private Point createExpectedPoint(CoordinateSystemMapper mapper, int x, int y, Monitor monitor) { if (mapper instanceof SingleZoomCoordinateSystemMapper) { return new Point(x, y); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java index 006ac508d03..4004edff011 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/MultiZoomCoordinateSystemMapper.java @@ -93,7 +93,8 @@ public Rectangle mapMonitorBounds(Rectangle rect, int zoom) { @Override public Point translateFromDisplayCoordinates(Point point) { - return translateLocationInPixelsToPoints(point.x, point.y); + Monitor monitor = point instanceof Point.WithMonitor pointWithMonitor ? pointWithMonitor.getMonitor() : null; + return translateLocationInPixelsToPoints(point.x, point.y, monitor); } @Override @@ -117,7 +118,7 @@ public Rectangle translateToDisplayCoordinates(Rectangle rect) { @Override public Point getCursorLocation() { Point cursorLocationInPixels = display.getCursorLocationInPixels(); - return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y); + return translateLocationInPixelsToPoints(cursorLocationInPixels.x, cursorLocationInPixels.y, null); } @Override @@ -131,8 +132,10 @@ private Point translateLocationInPointsToPixels(int x, int y, Monitor monitor) { return getPixelsFromPoint(monitor, x, y); } - private Point translateLocationInPixelsToPoints(int x, int y) { - Monitor monitor = getContainingMonitorForPixels(x, y); + private Point translateLocationInPixelsToPoints(int x, int y, Monitor monitor) { + if (monitor == null) { + monitor = getContainingMonitorForPixels(x, y); + } return getPointFromPixels(monitor, x, y); } 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 2e64ca13f3a..5a3ba20fb4a 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 @@ -924,7 +924,7 @@ public int getAlpha () { OS.GetWindowRect (handle, rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; - return new Rectangle (rect.left, rect.top, width, height); + return new Rectangle.WithMonitor (rect.left, rect.top, width, height, getMonitor()); } ToolTip getCurrentToolTip () { @@ -1017,7 +1017,7 @@ public int getImeInputMode () { if (OS.IsIconic (handle)) return super.getLocationInPixels (); RECT rect = new RECT (); OS.GetWindowRect (handle, rect); - return new Point (rect.left, rect.top); + return new Point.WithMonitor (rect.left, rect.top, getMonitor()); } @Override