Skip to content

Commit

Permalink
Added Shell to all the DPIUtil calls for classes from
Browse files Browse the repository at this point in the history
org.eclipse.swt.widgets + Refactored the classes from
org.eclipse.swt.graphics
  • Loading branch information
amartya4256 committed Feb 14, 2024
1 parent 8132e56 commit 0c2cc07
Show file tree
Hide file tree
Showing 38 changed files with 334 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,16 @@ public static Point autoScaleUp (Point point) {
return scaledPoint;
}

public static Point autoScaleUp (Point point, Shell shell) {
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor (shell);
Point scaledPoint = new Point (0,0);
scaledPoint.x = Math.round (point.x * scaleFactor);
scaledPoint.y = Math.round (point.y * scaleFactor);
return scaledPoint;
}

/**
* Returns a new scaled up Point if enabled for Drawable class.
*/
Expand All @@ -416,10 +426,15 @@ public static Point autoScaleUp (Drawable drawable, Point point) {
* Returns a new scaled up Rectangle.
*/
public static Rectangle autoScaleUp (Rectangle rect) {
if (deviceZoom == 100 || rect == null) return rect;
return autoScaleUp(rect, null);
}

public static Rectangle autoScaleUp (Rectangle rect, Shell shell) {
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || rect == null) return rect;
Rectangle scaledRect = new Rectangle (0,0,0,0);
Point scaledTopLeft = DPIUtil.autoScaleUp (new Point (rect.x, rect.y));
Point scaledBottomRight = DPIUtil.autoScaleUp (new Point (rect.x + rect.width, rect.y + rect.height));
Point scaledTopLeft = DPIUtil.autoScaleUp (new Point (rect.x, rect.y), shell);
Point scaledBottomRight = DPIUtil.autoScaleUp (new Point (rect.x + rect.width, rect.y + rect.height), shell);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void checkGC(int mask) {
if (data.gdipFgBrush != 0) Gdip.SolidBrush_delete(data.gdipFgBrush);
data.gdipFgBrush = 0;
long brush;
Pattern pattern = data.foregroundPattern.getScaledPattern(data.shell);
Pattern pattern = data.foregroundPattern != null ? data.foregroundPattern.getScaledPattern(data.shell) : null;
if (pattern != null) {
if(data.alpha == 0xFF) {
brush = pattern.handle;
Expand Down Expand Up @@ -286,7 +286,7 @@ void checkGC(int mask) {
if ((state & BACKGROUND) != 0) {
if (data.gdipBgBrush != 0) Gdip.SolidBrush_delete(data.gdipBgBrush);
data.gdipBgBrush = 0;
Pattern pattern = data.backgroundPattern.getScaledPattern(data.shell);
Pattern pattern = data.backgroundPattern != null ? data.backgroundPattern.getScaledPattern(data.shell) : null;
if (pattern != null) {
if(data.alpha == 0xFF) {
data.gdipBrush = pattern.handle;
Expand Down Expand Up @@ -1000,7 +1000,8 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei

void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
/* Refresh Image as per zoom level, if required. */
srcImage.refreshImageForZoom (data.shell.getCurrentDeviceZoom());
if (data.shell != null)
srcImage.refreshImageForZoom (data.shell.getCurrentDeviceZoom());

if (data.gdipGraphics != 0) {
//TODO - cache bitmap
Expand Down Expand Up @@ -1746,12 +1747,12 @@ void drawOvalInPixels (int x, int y, int width, int height) {
public void drawPath (Path path) {
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (path == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (path.handle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
if (path.getHandle(data.shell) == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
initGdip();
checkGC(DRAW);
long gdipGraphics = data.gdipGraphics;
Gdip.Graphics_TranslateTransform(gdipGraphics, data.gdipXOffset, data.gdipYOffset, Gdip.MatrixOrderPrepend);
Gdip.Graphics_DrawPath(gdipGraphics, data.gdipPen, path.handle);
Gdip.Graphics_DrawPath(gdipGraphics, data.gdipPen, path.getHandle(data.shell));
Gdip.Graphics_TranslateTransform(gdipGraphics, -data.gdipXOffset, -data.gdipYOffset, Gdip.MatrixOrderPrepend);
}

Expand Down Expand Up @@ -2919,12 +2920,12 @@ void fillOvalInPixels (int x, int y, int width, int height) {
public void fillPath (Path path) {
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (path == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (path.handle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
if (path.getHandle(data.shell) == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
initGdip();
checkGC(FILL);
int mode = OS.GetPolyFillMode(handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate;
Gdip.GraphicsPath_SetFillMode(path.handle, mode);
Gdip.Graphics_FillPath(data.gdipGraphics, data.gdipBrush, path.handle);
Gdip.GraphicsPath_SetFillMode(path.getHandle(data.shell), mode);
Gdip.Graphics_FillPath(data.gdipGraphics, data.gdipBrush, path.getHandle(data.shell));
}

/**
Expand Down Expand Up @@ -3371,7 +3372,7 @@ public void getClipping (Region region) {
Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeNone);
Gdip.Graphics_GetVisibleClipBounds(gdipGraphics, rect);
Gdip.Graphics_SetPixelOffsetMode(gdipGraphics, Gdip.PixelOffsetModeHalf);
OS.SetRectRgn(region.handle, rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height);
OS.SetRectRgn(region.getHandle(data.shell), rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height);
} else {
long matrix = Gdip.Matrix_new(1, 0, 0, 1, 0, 0);
long identity = Gdip.Matrix_new(1, 0, 0, 1, 0, 0);
Expand All @@ -3384,26 +3385,26 @@ public void getClipping (Region region) {
POINT pt = new POINT ();
OS.GetWindowOrgEx (handle, pt);
OS.OffsetRgn (hRgn, pt.x, pt.y);
OS.CombineRgn(region.handle, hRgn, 0, OS.RGN_COPY);
OS.CombineRgn(region.getHandle(data.shell), hRgn, 0, OS.RGN_COPY);
OS.DeleteObject(hRgn);
}
Gdip.Region_delete(rgn);
return;
}
POINT pt = new POINT ();
OS.GetWindowOrgEx (handle, pt);
int result = OS.GetClipRgn (handle, region.handle);
int result = OS.GetClipRgn (handle, region.getHandle(data.shell));
if (result != 1) {
RECT rect = new RECT();
OS.GetClipBox(handle, rect);
OS.SetRectRgn(region.handle, rect.left, rect.top, rect.right, rect.bottom);
OS.SetRectRgn(region.getHandle(data.shell), rect.left, rect.top, rect.right, rect.bottom);
} else {
OS.OffsetRgn (region.handle, pt.x, pt.y);
OS.OffsetRgn (region.getHandle(data.shell), pt.x, pt.y);
}
long metaRgn = OS.CreateRectRgn (0, 0, 0, 0);
if (OS.GetMetaRgn (handle, metaRgn) != 0) {
OS.OffsetRgn (metaRgn, pt.x, pt.y);
OS.CombineRgn (region.handle, metaRgn, region.handle, OS.RGN_AND);
OS.CombineRgn (region.getHandle(data.shell), metaRgn, region.getHandle(data.shell), OS.RGN_AND);
}
OS.DeleteObject(metaRgn);
long hwnd = data.hwnd;
Expand All @@ -3420,7 +3421,7 @@ public void getClipping (Region region) {
}
OS.MapWindowPoints (0, hwnd, pt, 1);
OS.OffsetRgn (sysRgn, pt.x, pt.y);
OS.CombineRgn (region.handle, sysRgn, region.handle, OS.RGN_AND);
OS.CombineRgn (region.getHandle(data.shell), sysRgn, region.getHandle(data.shell), OS.RGN_AND);
}
OS.DeleteObject(sysRgn);
}
Expand Down Expand Up @@ -3775,10 +3776,10 @@ public void getTransform(Transform transform) {
if (transform.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
long gdipGraphics = data.gdipGraphics;
if (gdipGraphics != 0) {
Gdip.Graphics_GetTransform(gdipGraphics, transform.handle);
Gdip.Graphics_GetTransform(gdipGraphics, transform.getHandle(data.shell));
long identity = identity();
Gdip.Matrix_Invert(identity);
Gdip.Matrix_Multiply(transform.handle, identity, Gdip.MatrixOrderAppend);
Gdip.Matrix_Multiply(transform.getHandle(data.shell), identity, Gdip.MatrixOrderAppend);
Gdip.Matrix_delete(identity);
} else {
transform.setElements(1, 0, 0, 1, 0, 0);
Expand Down Expand Up @@ -4294,8 +4295,8 @@ public void setClipping (Path path) {
if (path != null) {
initGdip();
int mode = OS.GetPolyFillMode(handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate;
Gdip.GraphicsPath_SetFillMode(path.handle, mode);
Gdip.Graphics_SetClipPath(data.gdipGraphics, path.handle);
Gdip.GraphicsPath_SetFillMode(path.getHandle(data.shell), mode);
Gdip.Graphics_SetClipPath(data.gdipGraphics, path.getHandle(data.shell));
}
}

Expand Down Expand Up @@ -4342,7 +4343,7 @@ public void setClipping (Rectangle rect) {
public void setClipping (Region region) {
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (region != null && region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
setClipping(region != null ? region.handle : 0);
setClipping(region != null ? region.getHandle(data.shell) : 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*******************************************************************************/
package org.eclipse.swt.graphics;

import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gdip.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.widgets.*;

/**
* Instances of this class represent paths through the two-dimensional
Expand Down Expand Up @@ -200,11 +203,6 @@ public Path (Device device, PathData data) {
*/
public void addArc (float x, float y, float width, float height, float startAngle, float arcAngle) {
if (width == 0 || height == 0 || arcAngle == 0) return;
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
width = DPIUtil.autoScaleUp(drawable, width);
height = DPIUtil.autoScaleUp(drawable, height);
addArcInPixels(x, y, width, height, startAngle, arcAngle);
}

Expand Down Expand Up @@ -270,11 +268,6 @@ public void addPath(Path path) {
* </ul>
*/
public void addRectangle (float x, float y, float width, float height) {
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
width = DPIUtil.autoScaleUp(drawable, width);
height = DPIUtil.autoScaleUp(drawable, height);
addRectangleInPixels(x, y, width, height);
}

Expand Down Expand Up @@ -308,9 +301,6 @@ void addRectangleInPixels(float x, float y, float width, float height) {
* </ul>
*/
public void addString (String string, float x, float y, Font font) {
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
addStringInPixels(string, x, y, font);
}

Expand Down Expand Up @@ -381,9 +371,6 @@ public void close() {
* </ul>
*/
public boolean contains (float x, float y, GC gc, boolean outline) {
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x, gc.data.shell);
y = DPIUtil.autoScaleUp(drawable, y, gc.data.shell);
return containsInPixels(x, y, gc, outline);
}

Expand Down Expand Up @@ -418,13 +405,6 @@ boolean containsInPixels(float x, float y, GC gc, boolean outline) {
* </ul>
*/
public void cubicTo (float cx1, float cy1, float cx2, float cy2, float x, float y) {
Drawable drawable = getDevice();
cx1 = DPIUtil.autoScaleUp(drawable, cx1);
cy1 = DPIUtil.autoScaleUp(drawable, cy1);
cx2 = DPIUtil.autoScaleUp(drawable, cx2);
cy2 = DPIUtil.autoScaleUp(drawable, cy2);
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
cubicToInPixels(cx1, cy1, cx2, cy2, x, y);
}

Expand All @@ -436,6 +416,7 @@ void cubicToInPixels(float cx1, float cy1, float cx2, float cy2, float x, float

@Override
void destroy() {
handleMap.values().forEach(handle -> Gdip.GraphicsPath_delete(handle));
Gdip.GraphicsPath_delete(handle);
handle = 0;
}
Expand All @@ -458,8 +439,6 @@ void destroy() {
public void getBounds (float[] bounds) {
if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
getBoundsInPixels(bounds);
float[] scaledbounds= DPIUtil.autoScaleDown(getDevice(), bounds);
System.arraycopy(scaledbounds, 0, bounds, 0, 4);
}

void getBoundsInPixels(float[] bounds) {
Expand Down Expand Up @@ -490,8 +469,6 @@ void getBoundsInPixels(float[] bounds) {
public void getCurrentPoint (float[] point) {
if (point == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
getCurrentPointInPixels(point);
float[] scaledpoint= DPIUtil.autoScaleDown(getDevice(), point);
System.arraycopy(scaledpoint, 0, point, 0, 2);
}

void getCurrentPointInPixels(float[] point) {
Expand All @@ -515,7 +492,6 @@ void getCurrentPointInPixels(float[] point) {
public PathData getPathData() {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
PathData result = getPathDataInPixels();
result.points = DPIUtil.autoScaleDown(getDevice(), result.points);
return result;
}

Expand Down Expand Up @@ -576,8 +552,7 @@ PathData getPathDataInPixels() {
* </ul>
*/
public void lineTo (float x, float y) {
Drawable drawable = getDevice();
lineToInPixels(DPIUtil.autoScaleUp(drawable, x), DPIUtil.autoScaleUp(drawable, y));
lineToInPixels(x, y);
}

void lineToInPixels(float x, float y) {
Expand Down Expand Up @@ -642,7 +617,7 @@ public boolean isDisposed() {
*/
public void moveTo (float x, float y) {
Drawable drawable = getDevice();
moveToInPixels(DPIUtil.autoScaleUp(drawable, x), DPIUtil.autoScaleUp(drawable, y));
moveToInPixels(x, y);
}

void moveToInPixels(float x, float y) {
Expand All @@ -665,11 +640,6 @@ void moveToInPixels(float x, float y) {
* </ul>
*/
public void quadTo (float cx, float cy, float x, float y) {
Drawable drawable = getDevice();
cx = DPIUtil.autoScaleUp(drawable, cx);
cy = DPIUtil.autoScaleUp(drawable, cy);
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
quadToInPixels(cx, cy, x, y);
}

Expand All @@ -683,6 +653,25 @@ void quadToInPixels(float cx, float cy, float x, float y) {
Gdip.GraphicsPath_GetLastPoint(handle, currentPoint);
}

private HashMap<Integer, Long> handleMap = new HashMap<>();

/**
* @since 3.125
*/
public long getHandle(Shell shell) {
if(shell.getCurrentDeviceZoom() == this.device.getDeviceZoom()) {
return this.handle;
}
if(this.handleMap.get(shell.getCurrentDeviceZoom()) == null) {
PathData pathData = this.getPathDataInPixels();
for (int index = 0; index < pathData.points.length; index++) {
pathData.points[index] = DPIUtil.autoScaleUp(getDevice(), pathData.points[index], shell);
}
handleMap.put(shell.getCurrentDeviceZoom(), new Path(getDevice(), pathData).handle);
}
return this.handleMap.get(shell.getCurrentDeviceZoom());
}

/**
* Returns a string containing a concise, human-readable
* description of the receiver.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ public class Pattern extends Resource {

private Runnable bitmapDestructor;

private float x1, y1, x2, y2;
private Color color1, color2;
private int alpha1, alpha2;
private final boolean isImagePattern;

private HashMap<Integer, Pattern> scaledpattern = new HashMap<>();

/**
* Constructs a new Pattern given an image. Drawing with the resulting
* pattern will cause the image to be tiled over the resulting area.
Expand Down Expand Up @@ -217,8 +224,6 @@ private Pattern(Shell shell, float x1, float y1, float x2, float y2, Color color
initializeSize(shell);
}

private HashMap<Integer, Pattern> scaledpattern = new HashMap<>();

Pattern getScaledPattern(Shell shell) {
if(shell.getCurrentDeviceZoom() == this.device.getDeviceZoom() || this.isImagePattern) {
return this;
Expand All @@ -230,16 +235,7 @@ Pattern getScaledPattern(Shell shell) {
return this.scaledpattern.get(shell.getCurrentDeviceZoom());
}

private float x1, y1, x2, y2;
private Color color1, color2;
private int alpha1, alpha2;
private final boolean isImagePattern;

//void initializeSize(float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) {
/**
* @since 3.125
*/
public void initializeSize(Shell shell) {
private void initializeSize(Shell shell) {
float x1, y1, x2, y2;
if(shell != null) {
x1 = DPIUtil.autoScaleUp(this.x1, shell);
Expand Down
Loading

0 comments on commit 0c2cc07

Please sign in to comment.