49
49
*/
50
50
public final class Cursor extends Resource {
51
51
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 ;
64
52
/**
65
53
* Attribute to cache current native zoom level
66
54
*/
@@ -74,6 +62,8 @@ public final class Cursor extends Resource {
74
62
private final ImageData mask ;
75
63
private final int hotspotX ;
76
64
private final int hotspotY ;
65
+ private boolean isDestroyed ;
66
+ private Integer style ;
77
67
/**
78
68
* Prevents uninitialized instances from being created outside the package.
79
69
*/
@@ -84,7 +74,6 @@ public final class Cursor extends Resource {
84
74
this .mask = null ;
85
75
this .hotspotX = -1 ;
86
76
this .hotspotY = -1 ;
87
- this .device .registerResourceWithZoomSupport (this );
88
77
}
89
78
90
79
/**
@@ -135,8 +124,9 @@ public final class Cursor extends Resource {
135
124
*/
136
125
public Cursor (Device device , int style ) {
137
126
this (device );
138
- this .handle = setupCursorFromStyle ( style ) ;
127
+ this .style = style ;
139
128
init ();
129
+ this .device .registerResourceWithZoomSupport (this );
140
130
}
141
131
142
132
private static long setupCursorFromStyle (int style ) {
@@ -212,7 +202,6 @@ public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int
212
202
this .hotspotX = hotspotX ;
213
203
this .hotspotY = hotspotY ;
214
204
this .imageDataProvider = null ;
215
- this .handle = setupCursorFromImageData (source , mask , hotspotX , hotspotY );
216
205
init ();
217
206
this .device .registerResourceWithZoomSupport (this );
218
207
}
@@ -285,7 +274,6 @@ public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) {
285
274
this .hotspotX = hotspotX ;
286
275
this .hotspotY = hotspotY ;
287
276
this .imageDataProvider = null ;
288
- this .handle = setupCursorFromImageData (device , source , hotspotX , hotspotY );
289
277
isIcon = true ;
290
278
init ();
291
279
this .device .registerResourceWithZoomSupport (this );
@@ -402,7 +390,6 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
402
390
this .mask = null ;
403
391
this .hotspotX = hotspotX ;
404
392
this .hotspotY = hotspotY ;
405
- this .handle = setupCursorFromImageData (device , this .source , hotspotX , hotspotY );
406
393
isIcon = true ;
407
394
init ();
408
395
this .device .registerResourceWithZoomSupport (this );
@@ -425,24 +412,26 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
425
412
*/
426
413
public static Long win32_getHandle (Cursor cursor , int zoom ) {
427
414
if (cursor .isDisposed ()) {
428
- return cursor . handle ;
415
+ return 0L ;
429
416
}
430
417
if (cursor .zoomLevelToHandle .get (zoom ) != null ) {
431
418
return cursor .zoomLevelToHandle .get (zoom );
432
419
}
433
420
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 );
436
425
} else {
437
426
ImageData source ;
438
427
if (cursor .imageDataProvider != null ) {
439
428
Image tempImage = new Image (cursor .getDevice (), cursor .imageDataProvider );
440
429
source = tempImage .getImageData (zoom );
441
430
tempImage .dispose ();
431
+ } else {
432
+ source = DPIUtil .scaleImageData (cursor .getDevice (), cursor .source , zoom , DEFAULT_ZOOM );
442
433
}
443
- else {
444
- source = DPIUtil .scaleImageData (cursor .device , cursor .source , zoom , DEFAULT_ZOOM );
445
- }
434
+
446
435
if (cursor .isIcon ) {
447
436
long handle = setupCursorFromImageData (cursor .getDevice (), source , Win32DPIUtils .pointToPixel (cursor .hotspotX , zoom ), Win32DPIUtils .pointToPixel (cursor .hotspotY , zoom ));
448
437
cursor .setHandleForZoomLevel (handle , zoom );
@@ -456,9 +445,6 @@ public static Long win32_getHandle (Cursor cursor, int zoom) {
456
445
}
457
446
458
447
private void setHandleForZoomLevel (long handle , Integer zoom ) {
459
- if (this .handle == 0 ) {
460
- this .handle = handle ; // Set handle for default zoom level
461
- }
462
448
if (zoom != null && !zoomLevelToHandle .containsKey (zoom )) {
463
449
zoomLevelToHandle .put (zoom , handle );
464
450
}
@@ -480,14 +466,14 @@ void destroy () {
480
466
// }
481
467
device .deregisterResourceWithZoomSupport (this );
482
468
destroyHandle ();
469
+ this .isDestroyed = true ;
483
470
}
484
471
485
472
private void destroyHandle () {
486
473
for (Long handle : zoomLevelToHandle .values ()) {
487
474
destroyHandle (handle );
488
475
}
489
476
zoomLevelToHandle .clear ();
490
- handle = 0 ;
491
477
}
492
478
493
479
private void destroyHandle (long handle ) {
@@ -521,7 +507,7 @@ public boolean equals (Object object) {
521
507
if (object == this ) return true ;
522
508
if (!(object instanceof Cursor )) return false ;
523
509
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 ) ;
525
511
}
526
512
527
513
/**
@@ -536,7 +522,7 @@ public boolean equals (Object object) {
536
522
*/
537
523
@ Override
538
524
public int hashCode () {
539
- return ( int ) handle ;
525
+ return win32_getHandle ( this , DEFAULT_ZOOM ). intValue () ;
540
526
}
541
527
542
528
/**
@@ -551,7 +537,7 @@ public int hashCode () {
551
537
*/
552
538
@ Override
553
539
public boolean isDisposed () {
554
- return handle == 0 ;
540
+ return isDestroyed ;
555
541
}
556
542
557
543
/**
@@ -563,7 +549,7 @@ public boolean isDisposed() {
563
549
@ Override
564
550
public String toString () {
565
551
if (isDisposed ()) return "Cursor {*DISPOSED*}" ;
566
- return "Cursor {" + handle + "}" ;
552
+ return "Cursor {" + zoomLevelToHandle + "}" ;
567
553
}
568
554
569
555
@ Override
0 commit comments