4949 */
5050public final class Cursor extends Resource {
5151
52- /**
53- * the handle to the OS cursor resource
54- * (Warning: This field is platform dependent)
55- * <p>
56- * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
57- * public API. It is marked public only so that it can be shared
58- * within the packages provided by SWT. It is not available on all
59- * platforms and should never be accessed from application code.
60- * </p>
61- *
62- */
63- private long handle ;
6452 /**
6553 * Attribute to cache current native zoom level
6654 */
@@ -74,6 +62,8 @@ public final class Cursor extends Resource {
7462 private final ImageData mask ;
7563 private final int hotspotX ;
7664 private final int hotspotY ;
65+ private boolean isDestroyed ;
66+ private Integer style ;
7767/**
7868 * Prevents uninitialized instances from being created outside the package.
7969 */
@@ -84,7 +74,6 @@ public final class Cursor extends Resource {
8474 this .mask = null ;
8575 this .hotspotX = -1 ;
8676 this .hotspotY = -1 ;
87- this .device .registerResourceWithZoomSupport (this );
8877}
8978
9079/**
@@ -135,8 +124,9 @@ public final class Cursor extends Resource {
135124 */
136125public Cursor (Device device , int style ) {
137126 this (device );
138- this .handle = setupCursorFromStyle ( style ) ;
127+ this .style = style ;
139128 init ();
129+ this .device .registerResourceWithZoomSupport (this );
140130}
141131
142132private static long setupCursorFromStyle (int style ) {
@@ -212,7 +202,6 @@ public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int
212202 this .hotspotX = hotspotX ;
213203 this .hotspotY = hotspotY ;
214204 this .imageDataProvider = null ;
215- this .handle = setupCursorFromImageData (source , mask , hotspotX , hotspotY );
216205 init ();
217206 this .device .registerResourceWithZoomSupport (this );
218207}
@@ -285,7 +274,6 @@ public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) {
285274 this .hotspotX = hotspotX ;
286275 this .hotspotY = hotspotY ;
287276 this .imageDataProvider = null ;
288- this .handle = setupCursorFromImageData (device , source , hotspotX , hotspotY );
289277 isIcon = true ;
290278 init ();
291279 this .device .registerResourceWithZoomSupport (this );
@@ -402,7 +390,6 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
402390 this .mask = null ;
403391 this .hotspotX = hotspotX ;
404392 this .hotspotY = hotspotY ;
405- this .handle = setupCursorFromImageData (device , this .source , hotspotX , hotspotY );
406393 isIcon = true ;
407394 init ();
408395 this .device .registerResourceWithZoomSupport (this );
@@ -425,40 +412,39 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
425412 */
426413public static Long win32_getHandle (Cursor cursor , int zoom ) {
427414 if (cursor .isDisposed ()) {
428- return cursor . handle ;
415+ return 0L ;
429416 }
430417 if (cursor .zoomLevelToHandle .get (zoom ) != null ) {
431418 return cursor .zoomLevelToHandle .get (zoom );
432419 }
433420
434- if (cursor .source == null ) {
435- cursor .setHandleForZoomLevel (cursor .handle , zoom );
421+ if (cursor .style != null ) {
422+ // we don't need to pass zoom in this case. LoadCursor will always return scaled cursor even though handle value will be same for all zoom levels.
423+ long handle = setupCursorFromStyle (cursor .style );
424+ cursor .setHandleForZoomLevel (handle , zoom );
436425 } else {
437426 ImageData source ;
438427 if (cursor .imageDataProvider != null ) {
439428 Image tempImage = new Image (cursor .getDevice (), cursor .imageDataProvider );
440429 source = tempImage .getImageData (zoom );
441430 tempImage .dispose ();
431+ } else {
432+ source = DPIUtil .scaleImageData (cursor .getDevice (), cursor .source , zoom , DEFAULT_ZOOM );
442433 }
443- else {
444- source = DPIUtil .scaleImageData (cursor .device , cursor .source , zoom , DEFAULT_ZOOM );
445- }
434+
446435 if (cursor .isIcon ) {
447- Cursor newCursor = new Cursor (cursor .device , source , Win32DPIUtils .pointToPixel (cursor .hotspotX , zoom ), Win32DPIUtils .pointToPixel (cursor .hotspotY , zoom ));
448- cursor .setHandleForZoomLevel (newCursor . handle , zoom );
436+ long handle = setupCursorFromImageData (cursor .getDevice () , source , Win32DPIUtils .pointToPixel (cursor .hotspotX , zoom ), Win32DPIUtils .pointToPixel (cursor .hotspotY , zoom ));
437+ cursor .setHandleForZoomLevel (handle , zoom );
449438 } else {
450- ImageData mask = DPIUtil .scaleImageData (cursor .device , cursor .mask , zoom , DEFAULT_ZOOM );
451- Cursor newCursor = new Cursor ( cursor . device , source , mask , Win32DPIUtils .pointToPixel (cursor .hotspotX , zoom ), Win32DPIUtils .pointToPixel (cursor .hotspotY , zoom ));
452- cursor .setHandleForZoomLevel (newCursor . handle , zoom );
439+ ImageData mask = DPIUtil .scaleImageData (cursor .getDevice () , cursor .mask , zoom , DEFAULT_ZOOM );
440+ long handle = setupCursorFromImageData ( source , mask , Win32DPIUtils .pointToPixel (cursor .hotspotX , zoom ), Win32DPIUtils .pointToPixel (cursor .hotspotY , zoom ));
441+ cursor .setHandleForZoomLevel (handle , zoom );
453442 }
454443 }
455444 return cursor .zoomLevelToHandle .get (zoom );
456445}
457446
458447private void setHandleForZoomLevel (long handle , Integer zoom ) {
459- if (this .handle == 0 ) {
460- this .handle = handle ; // Set handle for default zoom level
461- }
462448 if (zoom != null && !zoomLevelToHandle .containsKey (zoom )) {
463449 zoomLevelToHandle .put (zoom , handle );
464450 }
@@ -480,14 +466,14 @@ void destroy () {
480466// }
481467 device .deregisterResourceWithZoomSupport (this );
482468 destroyHandle ();
469+ this .isDestroyed = true ;
483470}
484471
485472private void destroyHandle () {
486473 for (Long handle : zoomLevelToHandle .values ()) {
487474 destroyHandle (handle );
488475 }
489476 zoomLevelToHandle .clear ();
490- handle = 0 ;
491477}
492478
493479private void destroyHandle (long handle ) {
@@ -521,7 +507,7 @@ public boolean equals (Object object) {
521507 if (object == this ) return true ;
522508 if (!(object instanceof Cursor )) return false ;
523509 Cursor cursor = (Cursor ) object ;
524- return device == cursor .device && handle == cursor . handle ;
510+ return device == cursor .device && win32_getHandle ( this , DEFAULT_ZOOM ) == win32_getHandle ( cursor , DEFAULT_ZOOM ) ;
525511}
526512
527513/**
@@ -536,7 +522,7 @@ public boolean equals (Object object) {
536522 */
537523@ Override
538524public int hashCode () {
539- return ( int ) handle ;
525+ return win32_getHandle ( this , DEFAULT_ZOOM ). intValue () ;
540526}
541527
542528/**
@@ -551,7 +537,7 @@ public int hashCode () {
551537 */
552538@ Override
553539public boolean isDisposed () {
554- return handle == 0 ;
540+ return isDestroyed ;
555541}
556542
557543/**
@@ -563,7 +549,7 @@ public boolean isDisposed() {
563549@ Override
564550public String toString () {
565551 if (isDisposed ()) return "Cursor {*DISPOSED*}" ;
566- return "Cursor {" + handle + "}" ;
552+ return "Cursor {" + zoomLevelToHandle + "}" ;
567553}
568554
569555@ Override
0 commit comments