Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void componentResized (ComponentEvent e) {
display.syncExec (() -> {
if (shell.isDisposed()) return;
Dimension dim = parent.getSize ();
shell.setSize(Win32DPIUtils.pixelToPoint(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points
shell.setSize(Win32DPIUtils.pixelToPointAsSize(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ void handleDOMEvent (OleEvent e) {
int screenY = pVarResult.getInt();
pVarResult.dispose();

Point position = Win32DPIUtils.pixelToPoint(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points
Point position = Win32DPIUtils.pixelToPointAsLocation(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points
position = browser.getDisplay().map(null, browser, position);
newEvent.x = position.x; newEvent.y = position.y;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ private Point convertPixelToPoint(int xInPixels, int yInPixels) {
if (this.control == null) {
// If there is no control for context, the behavior remains as before
int zoom = DPIUtil.getZoomForAutoscaleProperty(this.nativeZoom);
return Win32DPIUtils.pixelToPoint(new Point(xInPixels, yInPixels), zoom);
return Win32DPIUtils.pixelToPointAsLocation(new Point(xInPixels, yInPixels), zoom);
}
int zoom = DPIUtil.getZoomForAutoscaleProperty(this.control.nativeZoom);
// There is no API to convert absolute values in pixels to display relative
Expand All @@ -419,7 +419,7 @@ private Point convertPixelToPoint(int xInPixels, int yInPixels) {
POINT pt = new POINT ();
pt.x = xInPixels; pt.y = yInPixels;
OS.ScreenToClient (this.control.handle, pt);
Point p = Win32DPIUtils.pixelToPoint(new Point (pt.x, pt.y), zoom);
Point p = Win32DPIUtils.pixelToPointAsLocation(new Point (pt.x, pt.y), zoom);
return this.control.toDisplay(p);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void dragOver(DropTargetEvent event) {
int effect = checkEffect(event.feedback);
long handle = table.handle;
Point coordinates = new Point(event.x, event.y);
coordinates = Win32DPIUtils.pointToPixel(table.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(table.nativeZoom)); // To Pixels
coordinates = Win32DPIUtils.pointToPixelAsLocation(table.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(table.nativeZoom)); // To Pixels
LVHITTESTINFO pinfo = new LVHITTESTINFO();
pinfo.x = coordinates.x;
pinfo.y = coordinates.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void dragOver(DropTargetEvent event) {
int effect = checkEffect(event.feedback);
long handle = tree.handle;
Point coordinates = new Point(event.x, event.y);
coordinates = Win32DPIUtils.pointToPixel(tree.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(tree.nativeZoom)); // To Pixels
coordinates = Win32DPIUtils.pointToPixelAsLocation(tree.toControl(coordinates), DPIUtil.getZoomForAutoscaleProperty(tree.nativeZoom)); // To Pixels
TVHITTESTINFO lpht = new TVHITTESTINFO ();
lpht.x = coordinates.x;
lpht.y = coordinates.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ private int OnInPlaceDeactivate() {
return COM.S_OK;
}
private int OnPosRectChange(long lprcPosRect) {
Point size = Win32DPIUtils.pointToPixel(getSize(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
Point size = Win32DPIUtils.pointToPixelAsSize(getSize(), DPIUtil.getZoomForAutoscaleProperty(nativeZoom)); // To Pixels
setExtent(size.x, size.y);
return COM.S_OK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,20 @@ public static sealed class OfFloat extends Point permits Point.WithMonitor {
private static final long serialVersionUID = -1862062276431597053L;

public float residualX, residualY;
private final RoundingMode roundingMode;

public OfFloat(int x, int y) {
super(x, y);
this.roundingMode = null;
}

public OfFloat(float x, float y) {
super(Math.round(x), Math.round(y));
this(x, y, RoundingMode.ROUND);
}

public OfFloat(float x, float y, RoundingMode roundingMode) {
super(roundingMode.round(x), roundingMode.round(y));
this.roundingMode = roundingMode;
this.residualX = x - this.x;
this.residualY = y - this.y;
}
Expand All @@ -159,12 +166,12 @@ public float getY() {
}

public void setX(float x) {
this.x = Math.round(x);
this.x = roundingMode.round(x);
this.residualX = x - this.x;
}

public void setY(float y) {
this.y = Math.round(y);
this.y = roundingMode.round(y);
this.residualY = y - this.y;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.eclipse.swt.graphics;
/**
* @noreference This class is not intended to be referenced by clients
*/
public enum RoundingMode {
ROUND, UP;

public int round(float x) {
if (this == ROUND) {
return Math.round(x);
}
if (this == UP) {
return (int) Math.ceil(x);
}
return (int) x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public Point getDPI () {
int dpiX = OS.GetDeviceCaps (hDC, OS.LOGPIXELSX);
int dpiY = OS.GetDeviceCaps (hDC, OS.LOGPIXELSY);
internal_dispose_GC (hDC, null);
return Win32DPIUtils.pixelToPoint(new Point (dpiX, dpiY), DPIUtil.getZoomForAutoscaleProperty(getDeviceZoom()));
return Win32DPIUtils.pixelToPointAsLocation(new Point (dpiX, dpiY), DPIUtil.getZoomForAutoscaleProperty(getDeviceZoom()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ private class DrawImageOperation extends ImageOperation {

@Override
void apply() {
drawImageInPixels(getImage(), Win32DPIUtils.pointToPixel(drawable, this.location, getZoom()));
drawImageInPixels(getImage(), Win32DPIUtils.pointToPixelAsLocation(drawable, this.location, getZoom()));
}

private void drawImageInPixels(Image image, Point location) {
Expand Down Expand Up @@ -1868,8 +1868,8 @@ private class DrawLineOperation extends Operation {
@Override
void apply() {
int deviceZoom = getZoom();
Point startInPixels = Win32DPIUtils.pointToPixel (drawable, start, deviceZoom);
Point endInPixels = Win32DPIUtils.pointToPixel (drawable, end, deviceZoom);
Point startInPixels = Win32DPIUtils.pointToPixelAsLocation (drawable, start, deviceZoom);
Point endInPixels = Win32DPIUtils.pointToPixelAsLocation (drawable, end, deviceZoom);
drawLineInPixels(startInPixels.x, startInPixels.y, endInPixels.x, endInPixels.y);
}
}
Expand Down Expand Up @@ -2039,7 +2039,7 @@ private class DrawPointOperation extends Operation {

@Override
void apply() {
Point scaleUpLocation = Win32DPIUtils.pointToPixel(location, getZoom());
Point scaleUpLocation = Win32DPIUtils.pointToPixelAsLocation(location, getZoom());
drawPointInPixels(scaleUpLocation.x, scaleUpLocation.y);
}
}
Expand Down Expand Up @@ -2457,7 +2457,7 @@ private class DrawStringOperation extends Operation {

@Override
void apply() {
Point scaledLocation = Win32DPIUtils.pointToPixel(drawable, location, getZoom());
Point scaledLocation = Win32DPIUtils.pointToPixelAsLocation(drawable, location, getZoom());
drawStringInPixels(string, scaledLocation.x, scaledLocation.y, isTransparent);
}
}
Expand Down Expand Up @@ -2643,7 +2643,7 @@ private class DrawTextOperation extends Operation {

@Override
void apply() {
Point scaledLocation = Win32DPIUtils.pointToPixel(drawable, location, getZoom());
Point scaledLocation = Win32DPIUtils.pointToPixelAsLocation(drawable, location, getZoom());
drawTextInPixels(string, scaledLocation.x, scaledLocation.y, flags);
}
}
Expand Down Expand Up @@ -5736,7 +5736,7 @@ void apply() {
*/
public Point stringExtent (String string) {
if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
return Win32DPIUtils.pixelToPoint(drawable, stringExtentInPixels(string), getZoom());
return Win32DPIUtils.pixelToPointAsSize(drawable, stringExtentInPixels(string), getZoom());
}

Point stringExtentInPixels (String string) {
Expand Down Expand Up @@ -5781,7 +5781,7 @@ Point stringExtentInPixels (String string) {
* </ul>
*/
public Point textExtent (String string) {
return Win32DPIUtils.pixelToPoint(drawable, textExtentInPixels(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB), getZoom());
return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB), getZoom());
}

/**
Expand Down Expand Up @@ -5816,7 +5816,7 @@ public Point textExtent (String string) {
* </ul>
*/
public Point textExtent (String string, int flags) {
return Win32DPIUtils.pixelToPoint(drawable, textExtentInPixels(string, flags), getZoom());
return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, flags), getZoom());
}

Point textExtentInPixels(String string, int flags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public boolean contains (Point pt) {
if (pt == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
return applyUsingAnyHandle(regionHandle -> {
int zoom = regionHandle.zoom();
Point p = Win32DPIUtils.pointToPixel(pt, zoom);
Point p = Win32DPIUtils.pointToPixelAsLocation(pt, zoom);
return containsInPixels(regionHandle.handle(), p.x, p.y);
});
}
Expand Down Expand Up @@ -889,7 +889,7 @@ void intersect(long handle, int zoom) {

@Override
void translate(long handle, int zoom) {
Point pt = Win32DPIUtils.pointToPixel((Point) data, zoom);
Point pt = Win32DPIUtils.pointToPixelAsLocation((Point) data, zoom);
OS.OffsetRgn (handle, pt.x, pt.y);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2199,7 +2199,7 @@ public int[] getLineOffsets () {
*/
public Point getLocation (int offset, boolean trailing) {
checkLayout();
return Win32DPIUtils.pixelToPoint(getDevice(), getLocationInPixels(offset, trailing), getZoom());
return Win32DPIUtils.pixelToPointAsLocation(getDevice(), getLocationInPixels(offset, trailing), getZoom());
}

Point getLocationInPixels (int offset, boolean trailing) {
Expand Down Expand Up @@ -2409,7 +2409,7 @@ int _getOffset(int offset, int movement, boolean forward) {
*/
public int getOffset (Point point, int[] trailing) {
checkLayout();
if (point == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); return getOffsetInPixels(Win32DPIUtils.pointToPixel(getDevice(), point, getZoom()), trailing);
if (point == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); return getOffsetInPixels(Win32DPIUtils.pointToPixelAsLocation(getDevice(), point, getZoom()), trailing);
}

int getOffsetInPixels (Point point, int[] trailing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ private void addPlaceholderImageToImageList(long imageListHandle, int bitmapWidt
public Point getImageSize() {
int [] cx = new int [1], cy = new int [1];
OS.ImageList_GetIconSize (handle, cx, cy);
return Win32DPIUtils.pixelToPoint(new Point (cx [0], cy [0]), zoom);
return Win32DPIUtils.pixelToPointAsSize(new Point (cx [0], cy [0]), zoom);
}

public int indexOf (Image image) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,39 @@ public static float pixelToPoint(Drawable drawable, float size, int zoom) {
return DPIUtil.pixelToPoint (size, zoom);
}

public static Point pixelToPoint(Point point, int zoom) {
public static Point pixelToPointAsSize(Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return point;
return pixelToPointAsSize (point, zoom);
}

public static Point pixelToPointAsLocation(Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return point;
return pixelToPointAsLocation (point, zoom);
}

public static Point pixelToPointAsSize(Point point, int zoom) {
return pixelToPoint(point, zoom, RoundingMode.UP);
}

public static Point pixelToPointAsLocation(Point point, int zoom) {
return pixelToPoint(point, zoom, RoundingMode.ROUND);
}

private static Point pixelToPoint(Point point, int zoom, RoundingMode mode) {
if (zoom == 100 || point == null) return point;
Point.OfFloat fPoint = Point.OfFloat.from(point);
float scaleFactor = DPIUtil.getScalingFactor(zoom);
float scaledX = fPoint.getX() / scaleFactor;
float scaledY = fPoint.getY() / scaleFactor;
return new Point.OfFloat(scaledX, scaledY);
}

public static Point pixelToPoint(Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return point;
return pixelToPoint (point, zoom);
return new Point.OfFloat(scaledX, scaledY, mode);
}

public static Rectangle pixelToPoint(Rectangle rect, int zoom) {
if (zoom == 100 || rect == null) return rect;
if (rect instanceof Rectangle.OfFloat rectOfFloat) return pixelToPoint(rectOfFloat, zoom);
Rectangle scaledRect = new Rectangle.OfFloat (0,0,0,0);
Point scaledTopLeft = pixelToPoint(new Point (rect.x, rect.y), zoom);
Point scaledBottomRight = pixelToPoint(new Point (rect.x + rect.width, rect.y + rect.height), zoom);
Point scaledTopLeft = pixelToPointAsLocation(new Point (rect.x, rect.y), zoom);
Point scaledBottomRight = pixelToPointAsLocation(new Point (rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand Down Expand Up @@ -219,26 +232,39 @@ public static float pointToPixel(Drawable drawable, float size, int zoom) {
return pointToPixel (size, zoom);
}

public static Point pointToPixel(Point point, int zoom) {
private static Point pointToPixel(Point point, int zoom, RoundingMode mode) {
if (zoom == 100 || point == null) return point;
Point.OfFloat fPoint = Point.OfFloat.from(point);
float scaleFactor = DPIUtil.getScalingFactor(zoom);
float scaledX = fPoint.getX() * scaleFactor;
float scaledY = fPoint.getY() * scaleFactor;
return new Point.OfFloat(scaledX, scaledY);
return new Point.OfFloat(scaledX, scaledY, mode);
}

public static Point pointToPixelAsSize(Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return point;
return pointToPixelAsSize(point, zoom);
}

public static Point pointToPixel(Drawable drawable, Point point, int zoom) {
public static Point pointToPixelAsLocation(Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return point;
return pointToPixel (point, zoom);
return pointToPixelAsLocation(point, zoom);
}

public static Point pointToPixelAsSize(Point point, int zoom) {
return pointToPixel(point, zoom, RoundingMode.UP);
}

public static Point pointToPixelAsLocation(Point point, int zoom) {
return pointToPixel(point, zoom, RoundingMode.ROUND);
}

public static Rectangle pointToPixel(Rectangle rect, int zoom) {
if (zoom == 100 || rect == null) return rect;
if (rect instanceof Rectangle.OfFloat rectOfFloat) return pointToPixel(rectOfFloat, zoom);
Rectangle scaledRect = new Rectangle.OfFloat(0,0,0,0);
Point scaledTopLeft = pointToPixel (new Point(rect.x, rect.y), zoom);
Point scaledBottomRight = pointToPixel (new Point(rect.x + rect.width, rect.y + rect.height), zoom);
Point scaledTopLeft = pointToPixelAsLocation (new Point(rect.x, rect.y), zoom);
Point scaledBottomRight = pointToPixelAsLocation (new Point(rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public Canvas getParent () {
*/
public Point getSize () {
checkWidget();
return Win32DPIUtils.pixelToPoint(getSizeInPixels(), getZoom());
return Win32DPIUtils.pixelToPointAsSize(getSizeInPixels(), getZoom());
}

Point getSizeInPixels () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ boolean dragDetect (long hwnd, int x, int y, boolean filter, boolean [] detect,
*/
public Point getCaretLocation () {
checkWidget ();
return Win32DPIUtils.pixelToPoint(getCaretLocationInPixels(), getZoom());
return Win32DPIUtils.pixelToPointAsLocation(getCaretLocationInPixels(), getZoom());
}

Point getCaretLocationInPixels () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
changed |= (state & LAYOUT_CHANGED) != 0;
state &= ~LAYOUT_CHANGED;
int zoom = getZoom();
size = Win32DPIUtils.pointToPixel(layout.computeSize (this, DPIUtil.pixelToPoint(wHint, zoom), DPIUtil.pixelToPoint(hHint, zoom), changed), zoom);
size = Win32DPIUtils.pointToPixelAsSize(layout.computeSize (this, DPIUtil.pixelToPoint(wHint, zoom), DPIUtil.pixelToPoint(hHint, zoom), changed), zoom);
} else {
size = new Point (wHint, hHint);
}
Expand Down
Loading
Loading