Skip to content

Refactor fillGradientRectangle to avoid DPIUtil.autoScaleDown() #2284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -1256,8 +1256,8 @@ int handleContextMenuRequested(long pView, long pArgs) {
// to PIXEL coordinates with the real native zoom value
// independent from the swt.autoScale property:
Point pt = new Point( //
Win32DPIUtils.pointToPixel(win32Point.x, DPIUtil.getNativeDeviceZoom()), //
Win32DPIUtils.pointToPixel(win32Point.y, DPIUtil.getNativeDeviceZoom()));
DPIUtil.pointToPixel(win32Point.x, DPIUtil.getNativeDeviceZoom()), //
DPIUtil.pointToPixel(win32Point.y, DPIUtil.getNativeDeviceZoom()));
// - then, scale back down from PIXEL to DISPLAY coordinates, taking
// swt.autoScale property into account
// which is also later considered in Menu#setLocation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ private void drag(Event dragEvent) {
int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
OS.RedrawWindow (topControl.handle, null, 0, flags);
POINT pt = new POINT ();
pt.x = Win32DPIUtils.pointToPixel(dragEvent.x, zoom);// To Pixels
pt.y = Win32DPIUtils.pointToPixel(dragEvent.y, zoom);// To Pixels
pt.x = DPIUtil.pointToPixel(dragEvent.x, zoom);// To Pixels
pt.y = DPIUtil.pointToPixel(dragEvent.y, zoom);// To Pixels
OS.MapWindowPoints (control.handle, 0, pt, 1);
RECT rect = new RECT ();
OS.GetWindowRect (hwndDrag, rect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2616,9 +2616,14 @@ static void fillGradientRectangle(GC gc, Device device,
RGB fromRGB, RGB toRGB,
int redBits, int greenBits, int blueBits, int zoom) {
/* Create the bitmap and tile it */
ImageData band = createGradientBand(width, height, vertical,
fromRGB, toRGB, redBits, greenBits, blueBits);
Image image = new Image(device, band);
ImageDataProvider imageDataProvider = imageZoom -> {
int scaledWidth = DPIUtil.pointToPixel(width, imageZoom);
int scaledHeight = DPIUtil.pointToPixel(height, imageZoom);
return createGradientBand(scaledWidth, scaledHeight, vertical, fromRGB, toRGB, redBits, greenBits,
blueBits);
};
Image image = new Image(device, imageDataProvider);
ImageData band = image.getImageData(zoom);
if ((band.width == 1) || (band.height == 1)) {
gc.drawImage(image, 0, 0, DPIUtil.pixelToPoint(band.width, zoom), DPIUtil.pixelToPoint(band.height, zoom),
DPIUtil.pixelToPoint(x, zoom), DPIUtil.pixelToPoint(y, zoom), DPIUtil.pixelToPoint(width, zoom),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ public static int mapZoomToDPI (int zoom) {
return roundedDpi;
}

/**
* Auto-scale up int dimensions to match the given zoom level
*/
public static int pointToPixel(int size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor(zoom);
return Math.round (size * scaleFactor);
}

public static float pointToPixel(float size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor(zoom);
return (size * scaleFactor);
}

/**
* Represents an element, such as some image data, at a specific zoom level.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,11 @@ public static Long win32_getHandle (Cursor cursor, int zoom) {
source = DPIUtil.scaleImageData(cursor.device, cursor.source, zoom, DEFAULT_ZOOM);
}
if (cursor.isIcon) {
long handle = setupCursorFromImageData(cursor.getDevice(), source, Win32DPIUtils.pointToPixel(cursor.hotspotX, zoom), Win32DPIUtils.pointToPixel(cursor.hotspotY, zoom));
long handle = setupCursorFromImageData(cursor.getDevice(), source, DPIUtil.pointToPixel(cursor.hotspotX, zoom), DPIUtil.pointToPixel(cursor.hotspotY, zoom));
cursor.setHandleForZoomLevel(handle, zoom);
} else {
ImageData mask = DPIUtil.scaleImageData(cursor.getDevice(), cursor.mask, zoom, DEFAULT_ZOOM);
long handle = setupCursorFromImageData(source, mask, Win32DPIUtils.pointToPixel(cursor.hotspotX, zoom), Win32DPIUtils.pointToPixel(cursor.hotspotY, zoom));
long handle = setupCursorFromImageData(source, mask, DPIUtil.pointToPixel(cursor.hotspotX, zoom), DPIUtil.pointToPixel(cursor.hotspotY, zoom));
cursor.setHandleForZoomLevel(handle, zoom);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2199,8 +2199,8 @@ private ImageHandle createHandle(int zoom) {

private long initHandle(int zoom) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
int scaledWidth = Win32DPIUtils.pointToPixel (width, zoom);
int scaledHeight = Win32DPIUtils.pointToPixel (height, zoom);
int scaledWidth = DPIUtil.pointToPixel (width, zoom);
int scaledHeight = DPIUtil.pointToPixel (height, zoom);
long hDC = device.internal_new_GC(null);
long newHandle = OS.CreateCompatibleBitmap(hDC, scaledWidth, scaledHeight);
/*
Expand Down Expand Up @@ -2598,8 +2598,8 @@ protected ImageHandle newImageHandle(int zoom) {
int gcStyle = drawer.getGcStyle();
Image image;
if ((gcStyle & SWT.TRANSPARENT) != 0) {
int scaledHeight = Win32DPIUtils.pointToPixel(height, zoom);
int scaledWidth = Win32DPIUtils.pointToPixel(width, zoom);
int scaledHeight = DPIUtil.pointToPixel(height, zoom);
int scaledWidth = DPIUtil.pointToPixel(width, zoom);
/* Create a 24 bit image data with alpha channel */
final ImageData resultData = new ImageData (scaledWidth, scaledHeight, 24, new PaletteData (0xFF, 0xFF00, 0xFF0000));
resultData.alphaData = new byte [scaledWidth * scaledHeight];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ public BasePatternHandle(int zoom) {
@Override
long createHandle(int zoom) {
long handle;
float x1 = Win32DPIUtils.pointToPixel(baseX1, zoom);
float y1 = Win32DPIUtils.pointToPixel(baseY1, zoom);
float x2 = Win32DPIUtils.pointToPixel(baseX2, zoom);
float y2 = Win32DPIUtils.pointToPixel(baseY2, zoom);
float x1 = DPIUtil.pointToPixel(baseX1, zoom);
float y1 = DPIUtil.pointToPixel(baseY1, zoom);
float x2 = DPIUtil.pointToPixel(baseX2, zoom);
float y2 = DPIUtil.pointToPixel(baseY2, zoom);
if (color1 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (color1.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
if (color2 == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ public boolean contains (int x, int y) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
return applyUsingAnyHandle(regionHandle -> {
int zoom = regionHandle.zoom();
int xInPixels = Win32DPIUtils.pointToPixel(x, zoom);
int yInPixels = Win32DPIUtils.pointToPixel(y, zoom);
int xInPixels = DPIUtil.pointToPixel(x, zoom);
int yInPixels = DPIUtil.pointToPixel(y, zoom);
return containsInPixels(regionHandle.handle(), xInPixels, yInPixels);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ void computeRuns (GC gc) {
}
SCRIPT_LOGATTR logAttr = new SCRIPT_LOGATTR();
SCRIPT_PROPERTIES properties = new SCRIPT_PROPERTIES();
int wrapIndentInPixels = Win32DPIUtils.pointToPixel(wrapIndent, getZoom(gc));
int indentInPixels = Win32DPIUtils.pointToPixel(indent, getZoom(gc));
int wrapWidthInPixels = Win32DPIUtils.pointToPixel(wrapWidth, getZoom(gc));
int wrapIndentInPixels = DPIUtil.pointToPixel(wrapIndent, getZoom(gc));
int indentInPixels = DPIUtil.pointToPixel(indent, getZoom(gc));
int wrapWidthInPixels = DPIUtil.pointToPixel(wrapWidth, getZoom(gc));
int[] tabsInPixels = Win32DPIUtils.pointToPixel(tabs, getZoom(gc));
int lineWidth = indentInPixels, lineStart = 0, lineCount = 1;
for (int i=0; i<allRuns.length - 1; i++) {
Expand Down Expand Up @@ -1896,8 +1896,8 @@ Rectangle getBoundsInPixels (int start, int end) {
}
left = Math.min(left, runLead);
right = Math.max(right, runTrail);
top = Math.min(top, Win32DPIUtils.pointToPixel(lineY[lineIndex], getZoom(null)));
bottom = Math.max(bottom, Win32DPIUtils.pointToPixel(lineY[lineIndex + 1] - lineSpacingInPoints, getZoom(null)));
top = Math.min(top, DPIUtil.pointToPixel(lineY[lineIndex], getZoom(null)));
bottom = Math.max(bottom, DPIUtil.pointToPixel(lineY[lineIndex + 1] - lineSpacingInPoints, getZoom(null)));
}
return new Rectangle(left, top, right - left, bottom - top + getScaledVerticalIndent());
}
Expand Down Expand Up @@ -2057,14 +2057,14 @@ int getLineIndent(int lineIndex) {
}

int getLineIndentInPixel (int lineIndex) {
int lineIndent = Win32DPIUtils.pointToPixel(wrapIndent, getZoom());
int lineIndent = DPIUtil.pointToPixel(wrapIndent, getZoom());
if (lineIndex == 0) {
lineIndent = Win32DPIUtils.pointToPixel(indent, getZoom());
lineIndent = DPIUtil.pointToPixel(indent, getZoom());
} else {
StyleItem[] previousLine = runs[lineIndex - 1];
StyleItem previousRun = previousLine[previousLine.length - 1];
if (previousRun.lineBreak && !previousRun.softBreak) {
lineIndent = Win32DPIUtils.pointToPixel(indent, getZoom());
lineIndent = DPIUtil.pointToPixel(indent, getZoom());
}
}
if (wrapWidth != -1) {
Expand All @@ -2078,8 +2078,8 @@ int getLineIndentInPixel (int lineIndex) {
if (partialLine) {
int lineWidth = this.lineWidthInPixels[lineIndex] + lineIndent;
switch (alignment) {
case SWT.CENTER: lineIndent += (Win32DPIUtils.pointToPixel(wrapWidth, getZoom()) - lineWidth) / 2; break;
case SWT.RIGHT: lineIndent += Win32DPIUtils.pointToPixel(wrapWidth, getZoom()) - lineWidth; break;
case SWT.CENTER: lineIndent += (DPIUtil.pointToPixel(wrapWidth, getZoom()) - lineWidth) / 2; break;
case SWT.RIGHT: lineIndent += DPIUtil.pointToPixel(wrapWidth, getZoom()) - lineWidth; break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public MultiplyOperation(Transform matrix) {
public void apply(TransformHandle transformHandle) {
long handle = transformHandle.handle;
int zoom = transformHandle.zoom;
long newHandle = Gdip.Matrix_new(elements[0], elements[1], elements[2], elements[3], Win32DPIUtils.pointToPixel(elements[4], zoom), Win32DPIUtils.pointToPixel(elements[5], zoom));
long newHandle = Gdip.Matrix_new(elements[0], elements[1], elements[2], elements[3], DPIUtil.pointToPixel(elements[4], zoom), DPIUtil.pointToPixel(elements[5], zoom));
if (newHandle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
try {
Gdip.Matrix_Multiply(handle, newHandle, Gdip.MatrixOrderPrepend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ public int getStyle () {

public long getHandle(int targetZoom) {
if (!zoomToHandle.containsKey(targetZoom)) {
int scaledWidth = Win32DPIUtils.pointToPixel(DPIUtil.pixelToPoint(width, this.zoom), targetZoom);
int scaledHeight = Win32DPIUtils.pointToPixel(DPIUtil.pixelToPoint(height, this.zoom), targetZoom);
int scaledWidth = DPIUtil.pointToPixel(DPIUtil.pixelToPoint(width, this.zoom), targetZoom);
int scaledHeight = DPIUtil.pointToPixel(DPIUtil.pixelToPoint(height, this.zoom), targetZoom);
long newImageListHandle = OS.ImageList_Create(scaledWidth, scaledHeight, flags, 16, 16);
int count = OS.ImageList_GetImageCount (handle);
for (int i = 0; i < count; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,29 +190,14 @@ public static int[] pointToPixel(Drawable drawable, int[] pointArray, int zoom)
return pointToPixel (pointArray, zoom);
}

/**
* Auto-scale up int dimensions to match the given zoom level
*/
public static int pointToPixel(int size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = DPIUtil.getScalingFactor(zoom);
return Math.round (size * scaleFactor);
}

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

public static float pointToPixel(float size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = DPIUtil.getScalingFactor(zoom);
return (size * scaleFactor);
return DPIUtil.pointToPixel (size, zoom);
}

public static float pointToPixel(Drawable drawable, float size, int zoom) {
if (drawable != null && !drawable.isAutoScalable()) return size;
return pointToPixel (size, zoom);
return DPIUtil.pointToPixel (size, zoom);
}

public static Point pointToPixel(Point point, int zoom) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ else if ((style & SWT.RIGHT) != 0) {
Rectangle rect = Win32DPIUtils.scaleBounds(image.getBounds(), this.getZoom(), 100);
width = rect.width;
if (hasText && text.length () != 0) {
width += Win32DPIUtils.pointToPixel(MARGIN * 2, getZoom());;
width += DPIUtil.pointToPixel(MARGIN * 2, getZoom());;
}
height = rect.height;
extra = Win32DPIUtils.pointToPixel(MARGIN * 2, getZoom());;
extra = DPIUtil.pointToPixel(MARGIN * 2, getZoom());;
}
}
if (hasText) {
Expand All @@ -379,7 +379,7 @@ else if ((style & SWT.RIGHT) != 0) {
if (length == 0) {
height = Math.max (height, lptm.tmHeight);
} else {
extra = Math.max (Win32DPIUtils.pointToPixel(MARGIN * 2, getZoom()), lptm.tmAveCharWidth);
extra = Math.max (DPIUtil.pointToPixel(MARGIN * 2, getZoom()), lptm.tmAveCharWidth);
char [] buffer = text.toCharArray ();
RECT rect = new RECT ();
int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
Expand Down Expand Up @@ -1315,7 +1315,7 @@ private int getCheckboxTextOffset(long hdc) {
OS.GetThemePartSize(display.hButtonTheme(nativeZoom), hdc, OS.BP_CHECKBOX, OS.CBS_UNCHECKEDNORMAL, null, OS.TS_TRUE, size);
result += size.cx;
} else {
result += Win32DPIUtils.pointToPixel(13, nativeZoom);
result += DPIUtil.pointToPixel(13, nativeZoom);
}

// Windows uses half width of '0' as checkbox-to-text distance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ void reskinChildren (int flags) {
public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
checkWidget ();
int zoom = getZoom();
destX = Win32DPIUtils.pointToPixel(destX, zoom);
destY = Win32DPIUtils.pointToPixel(destY, zoom);
destX = DPIUtil.pointToPixel(destX, zoom);
destY = DPIUtil.pointToPixel(destY, zoom);
Rectangle rectangle = Win32DPIUtils.pointToPixel(new Rectangle(x, y, width, height), zoom);
scrollInPixels(destX, destY, rectangle.x, rectangle.y, rectangle.width, rectangle.height, all);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,19 @@ Point getSizeInPixels () {
}

private int getWidthInPixels() {
return Win32DPIUtils.pointToPixel(width, getZoom());
return DPIUtil.pointToPixel(width, getZoom());
}

private int getHeightInPixels() {
return Win32DPIUtils.pointToPixel(height, getZoom());
return DPIUtil.pointToPixel(height, getZoom());
}

private int getXInPixels() {
return Win32DPIUtils.pointToPixel(x, getZoom());
return DPIUtil.pointToPixel(x, getZoom());
}

private int getYInPixels() {
return Win32DPIUtils.pointToPixel(y, getZoom());
return DPIUtil.pointToPixel(y, getZoom());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ public void drawBackground (GC gc, int x, int y, int width, int height, int offs
checkWidget ();
int zoom = getZoom();
Rectangle rectangle = Win32DPIUtils.pointToPixel(new Rectangle(x, y, width, height), zoom);
offsetX = Win32DPIUtils.pointToPixel(offsetX, zoom);
offsetY = Win32DPIUtils.pointToPixel(offsetY, zoom);
offsetX = DPIUtil.pointToPixel(offsetX, zoom);
offsetY = DPIUtil.pointToPixel(offsetY, zoom);
drawBackgroundInPixels(gc, rectangle.x, rectangle.y, rectangle.width, rectangle.height, offsetX, offsetY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ public Point computeSize (int wHint, int hHint) {
public Point computeSize (int wHint, int hHint, boolean changed){
checkWidget ();
int zoom = getZoom();
wHint = (wHint != SWT.DEFAULT ? Win32DPIUtils.pointToPixel(wHint, zoom) : wHint);
hHint = (hHint != SWT.DEFAULT ? Win32DPIUtils.pointToPixel(hHint, zoom) : hHint);
wHint = (wHint != SWT.DEFAULT ? DPIUtil.pointToPixel(wHint, zoom) : wHint);
hHint = (hHint != SWT.DEFAULT ? DPIUtil.pointToPixel(hHint, zoom) : hHint);
return Win32DPIUtils.pixelToPoint(computeSizeInPixels(wHint, hHint, changed), zoom);
}

Expand Down Expand Up @@ -782,7 +782,7 @@ public boolean dragDetect (Event event) {
if (event == null) error (SWT.ERROR_NULL_ARGUMENT);
Point loc = event.getLocation();
int zoom = getZoom();
return dragDetect (event.button, event.count, event.stateMask, Win32DPIUtils.pointToPixel(loc.x, zoom), Win32DPIUtils.pointToPixel(loc.y, zoom));
return dragDetect (event.button, event.count, event.stateMask, DPIUtil.pointToPixel(loc.x, zoom), DPIUtil.pointToPixel(loc.y, zoom));
}

/**
Expand Down Expand Up @@ -825,7 +825,7 @@ public boolean dragDetect (MouseEvent event) {
checkWidget ();
if (event == null) error (SWT.ERROR_NULL_ARGUMENT);
int zoom = getZoom();
return dragDetect (event.button, event.count, event.stateMask, Win32DPIUtils.pointToPixel(event.x, zoom), Win32DPIUtils.pointToPixel(event.y, zoom)); // To Pixels
return dragDetect (event.button, event.count, event.stateMask, DPIUtil.pointToPixel(event.x, zoom), DPIUtil.pointToPixel(event.y, zoom)); // To Pixels
}

boolean dragDetect (int button, int count, int stateMask, int x, int y) {
Expand Down Expand Up @@ -1987,8 +1987,8 @@ void mapEvent (long hwnd, Event event) {
POINT point = new POINT ();
Point loc = event.getLocation();
int zoom = getZoom();
point.x = Win32DPIUtils.pointToPixel(loc.x, zoom);
point.y = Win32DPIUtils.pointToPixel(loc.y, zoom);
point.x = DPIUtil.pointToPixel(loc.x, zoom);
point.y = DPIUtil.pointToPixel(loc.y, zoom);
OS.MapWindowPoints (hwnd, handle, point, 1);
event.setLocation(DPIUtil.pixelToPoint(point.x, zoom), DPIUtil.pixelToPoint(point.y, zoom));
}
Expand Down Expand Up @@ -3522,8 +3522,8 @@ public void setLayoutData (Object layoutData) {
public void setLocation (int x, int y) {
checkWidget ();
int zoom = getZoom();
x = Win32DPIUtils.pointToPixel(x, zoom);
y = Win32DPIUtils.pointToPixel(y, zoom);
x = DPIUtil.pointToPixel(x, zoom);
y = DPIUtil.pointToPixel(y, zoom);
setLocationInPixels(x, y);
}

Expand Down Expand Up @@ -3779,8 +3779,8 @@ public void setRegion (Region region) {
public void setSize (int width, int height) {
checkWidget ();
int zoom = getZoom();
width = Win32DPIUtils.pointToPixel(width, zoom);
height = Win32DPIUtils.pointToPixel(height, zoom);
width = DPIUtil.pointToPixel(width, zoom);
height = DPIUtil.pointToPixel(height, zoom);
setSizeInPixels(width, height);
}

Expand Down Expand Up @@ -4090,7 +4090,7 @@ public Point toControl (Point point) {
public Point toDisplay (int x, int y) {
checkWidget ();
int zoom = getZoom();
Point displayPointInPixels = toDisplayInPixels(Win32DPIUtils.pointToPixel(x, zoom), Win32DPIUtils.pointToPixel(y, zoom));
Point displayPointInPixels = toDisplayInPixels(DPIUtil.pointToPixel(x, zoom), DPIUtil.pointToPixel(y, zoom));
return getDisplay().translateFromDisplayCoordinates(displayPointInPixels, zoom);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ int getMargin (int index) {
}
if ((style & SWT.FLAT) == 0) {
if (!isLastItemOfRow (index)) {
margin += Win32DPIUtils.pointToPixel(SEPARATOR_WIDTH, getZoom());
margin += DPIUtil.pointToPixel(SEPARATOR_WIDTH, getZoom());
}
}
return margin;
Expand Down
Loading
Loading