Skip to content

Commit

Permalink
Removed the handle field from Region
Browse files Browse the repository at this point in the history
This commit removes the public handle field from Region for win32 since
it is now replaced by win32_getHandle method for the clients and
internally the class uses a hashMap zoomToHandle to get the handle for
the particular zoomlevel. The default handle is stored in zoomToHandle
mapped to the initialZoom.

contributes #62 and #127
  • Loading branch information
amartya4256 committed Jun 27, 2024
1 parent 4ce5919 commit e2bd8c1
Showing 1 changed file with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@
*/
public final class Region extends Resource {

/**
* the OS resource for the region
* (Warning: This field is platform dependent)
* <p>
* <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
* public API. It is marked public only so that it can be shared
* within the packages provided by SWT. It is not available on all
* platforms and should never be accessed from application code.
* </p>
*
* @noreference This field is not intended to be referenced by clients.
*/
public long handle;

private int initialZoom;

private HashMap<Integer, Long> zoomToHandle = new HashMap<>();
Expand Down Expand Up @@ -94,7 +80,7 @@ public Region () {
public Region (Device device) {
super(device);
initialZoom = DPIUtil.getDeviceZoom();
handle = OS.CreateRectRgn (0, 0, 0, 0);
long handle = OS.CreateRectRgn (0, 0, 0, 0);
zoomToHandle.put(initialZoom, handle);
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
init();
Expand Down Expand Up @@ -123,7 +109,7 @@ public void add (int[] pointArray) {
}

void addInPixels (int[] pointArray) {
addInPixels(handle, pointArray);
addInPixels(win32_getHandle(this, initialZoom), pointArray);
}

private void addInPixels (long handle, int[] pointArray) {
Expand Down Expand Up @@ -179,7 +165,7 @@ public void add (int x, int y, int width, int height) {
}

void addInPixels (int x, int y, int width, int height) {
addInPixels(handle, x, y, width, height);
addInPixels(win32_getHandle(this, initialZoom), x, y, width, height);
}

private void addInPixels (long handle, int x, int y, int width, int height) {
Expand Down Expand Up @@ -209,7 +195,8 @@ public void add (Region region) {
if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
operations.add(new Operation(OperationType.ADD, ArgumentType.REGION, region));
OS.CombineRgn (handle, handle, region.handle, OS.RGN_OR);
long handle = win32_getHandle(this, initialZoom);
OS.CombineRgn (handle, handle, win32_getHandle(region, initialZoom), OS.RGN_OR);
}

/**
Expand All @@ -231,7 +218,7 @@ public boolean contains (int x, int y) {
}

boolean containsInPixels (int x, int y) {
return OS.PtInRegion (handle, x, y);
return OS.PtInRegion (win32_getHandle(this, initialZoom), x, y);
}

/**
Expand Down Expand Up @@ -259,8 +246,7 @@ public boolean contains (Point pt) {
@Override
void destroy () {
zoomToHandle.values().forEach(handle -> OS.DeleteObject(handle));
OS.DeleteObject(handle);
handle = 0;
zoomToHandle.clear();
operations.clear();
}

Expand All @@ -279,7 +265,7 @@ public boolean equals (Object object) {
if (this == object) return true;
if (!(object instanceof Region)) return false;
Region rgn = (Region)object;
return handle == rgn.handle;
return win32_getHandle(this, initialZoom) == win32_getHandle(rgn, initialZoom);
}

/**
Expand All @@ -302,7 +288,7 @@ public Rectangle getBounds () {

Rectangle getBoundsInPixels() {
RECT rect = new RECT();
OS.GetRgnBox(handle, rect);
OS.GetRgnBox(win32_getHandle(this, initialZoom), rect);
return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}

Expand All @@ -318,7 +304,7 @@ Rectangle getBoundsInPixels() {
*/
@Override
public int hashCode () {
return (int)handle;
return (int)win32_getHandle(this, initialZoom);
}

/**
Expand Down Expand Up @@ -370,7 +356,7 @@ public void intersect (int x, int y, int width, int height) {
}

void intersectInPixels (int x, int y, int width, int height) {
intersectInPixels(handle, x, y, width, height);
intersectInPixels(win32_getHandle(this, initialZoom), x, y, width, height);
}

private void intersectInPixels (long handle, int x, int y, int width, int height) {
Expand Down Expand Up @@ -401,7 +387,8 @@ public void intersect (Region region) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
OS.CombineRgn (handle, handle, region.handle, OS.RGN_AND);
long handle = win32_getHandle(this, initialZoom);
OS.CombineRgn (handle, handle, win32_getHandle(region, initialZoom), OS.RGN_AND);
}

/**
Expand Down Expand Up @@ -429,7 +416,7 @@ public boolean intersects (int x, int y, int width, int height) {
boolean intersectsInPixels (int x, int y, int width, int height) {
RECT r = new RECT ();
OS.SetRect (r, x, y, x + width, y + height);
return OS.RectInRegion (handle, r);
return OS.RectInRegion (win32_getHandle(this, initialZoom), r);
}

/**
Expand Down Expand Up @@ -468,7 +455,7 @@ public boolean intersects (Rectangle rect) {
*/
@Override
public boolean isDisposed() {
return handle == 0;
return zoomToHandle.isEmpty();
}

/**
Expand All @@ -485,7 +472,7 @@ public boolean isDisposed() {
public boolean isEmpty () {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
RECT rect = new RECT ();
int result = OS.GetRgnBox (handle, rect);
int result = OS.GetRgnBox (win32_getHandle(this, initialZoom), rect);
if (result == OS.NULLREGION) return true;
return ((rect.right - rect.left) <= 0) || ((rect.bottom - rect.top) <= 0);
}
Expand Down Expand Up @@ -513,7 +500,7 @@ public void subtract (int[] pointArray) {
}

void subtractInPixels (int[] pointArray) {
subtractInPixels(handle, pointArray);
subtractInPixels(win32_getHandle(this, initialZoom), pointArray);
}

private void subtractInPixels (long handle, int[] pointArray) {
Expand Down Expand Up @@ -571,7 +558,7 @@ public void subtract (int x, int y, int width, int height) {
}

void subtractInPixels (int x, int y, int width, int height) {
subtractInPixels(handle, x, y, width, height);
subtractInPixels(win32_getHandle(this, initialZoom), x, y, width, height);
}

private void subtractInPixels (long handle, int x, int y, int width, int height) {
Expand Down Expand Up @@ -602,7 +589,8 @@ public void subtract (Region region) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
OS.CombineRgn (handle, handle, region.handle, OS.RGN_DIFF);
long handle = win32_getHandle(this, initialZoom);
OS.CombineRgn (handle, handle, win32_getHandle(region, initialZoom), OS.RGN_DIFF);
}

/**
Expand All @@ -625,7 +613,7 @@ public void translate (int x, int y) {
}

void translateInPixels (int x, int y) {
OS.OffsetRgn (handle, x, y);
OS.OffsetRgn (win32_getHandle(this, initialZoom), x, y);
}

/**
Expand Down Expand Up @@ -688,7 +676,7 @@ public static long win32_getHandle(Region region, int zoom) {
@Override
public String toString () {
if (isDisposed()) return "Region {*DISPOSED*}";
return "Region {" + handle + "}";
return "Region " + zoomToHandle;
}

class Operation {
Expand Down

0 comments on commit e2bd8c1

Please sign in to comment.