From 06220b2621db853ca3387b963cb70e016b85bc21 Mon Sep 17 00:00:00 2001 From: Amartya Parijat Date: Thu, 27 Jun 2024 11:32:06 +0200 Subject: [PATCH] Removed the handle field from Region 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 --- .../org/eclipse/swt/graphics/Region.java | 56 ++++++++----------- .../Test_org_eclipse_swt_widgets_Shell.java | 3 +- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java index c202209cac9..51bf86b9f4b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Region.java @@ -36,20 +36,6 @@ */ public final class Region extends Resource { - /** - * the OS resource for the region - * (Warning: This field is platform dependent) - *

- * IMPORTANT: This field is not 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. - *

- * - * @noreference This field is not intended to be referenced by clients. - */ - public long handle; - private int initialZoom; private HashMap zoomToHandle = new HashMap<>(); @@ -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(); @@ -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) { @@ -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) { @@ -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); } /** @@ -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); } /** @@ -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(); } @@ -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); } /** @@ -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); } @@ -318,7 +304,7 @@ Rectangle getBoundsInPixels() { */ @Override public int hashCode () { - return (int)handle; + return (int)win32_getHandle(this, initialZoom); } /** @@ -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) { @@ -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); } /** @@ -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); } /** @@ -468,7 +455,7 @@ public boolean intersects (Rectangle rect) { */ @Override public boolean isDisposed() { - return handle == 0; + return zoomToHandle.isEmpty(); } /** @@ -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); } @@ -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) { @@ -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) { @@ -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); } /** @@ -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); } /** @@ -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 { diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Shell.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Shell.java index a900d17be0f..6a740fb5082 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Shell.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Shell.java @@ -713,6 +713,7 @@ private void logUnlessEquals(Appendable log, String message, Rectangle expected, } //TODO This test was not hooked for running with the runTest override. It fails on GTK/Cocoa. Investigate. +@Test public void a_test_setRegion() { Region region = new Region(); region.add(new Rectangle(10, 20, 100, 200)); @@ -727,7 +728,7 @@ public void a_test_setRegion() { Shell shell2 = new Shell(display, SWT.NO_TRIM); assertNull(":d:", shell2.getRegion()); shell2.setRegion(region); - assertEquals(":e:", region.handle, shell2.getRegion().handle); + assertTrue(":e:", region.equals(shell2.getRegion())); region.dispose(); assertTrue(":f:", shell2.getRegion().isDisposed()); shell2.setRegion(null);