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
*/
@@ -135,7 +125,9 @@ public final class Cursor extends Resource {
135
125
*/
136
126
public Cursor (Device device , int style ) {
137
127
this (device );
138
- this .handle = setupCursorFromStyle (style );
128
+ this .style = style ;
129
+ long handle = setupCursorFromStyle (style );
130
+ zoomLevelToHandle .put (DEFAULT_ZOOM , handle );
139
131
init ();
140
132
}
141
133
@@ -212,7 +204,8 @@ public Cursor(Device device, ImageData source, ImageData mask, int hotspotX, int
212
204
this .hotspotX = hotspotX ;
213
205
this .hotspotY = hotspotY ;
214
206
this .imageDataProvider = null ;
215
- this .handle = setupCursorFromImageData (source , mask , hotspotX , hotspotY );
207
+ long handle = setupCursorFromImageData (source , mask , hotspotX , hotspotY );
208
+ zoomLevelToHandle .put (DEFAULT_ZOOM , handle );
216
209
init ();
217
210
this .device .registerResourceWithZoomSupport (this );
218
211
}
@@ -285,8 +278,9 @@ public Cursor(Device device, ImageData source, int hotspotX, int hotspotY) {
285
278
this .hotspotX = hotspotX ;
286
279
this .hotspotY = hotspotY ;
287
280
this .imageDataProvider = null ;
288
- this . handle = setupCursorFromImageData (device , source , hotspotX , hotspotY );
281
+ long handle = setupCursorFromImageData (device , source , hotspotX , hotspotY );
289
282
isIcon = true ;
283
+ zoomLevelToHandle .put (DEFAULT_ZOOM , handle );
290
284
init ();
291
285
this .device .registerResourceWithZoomSupport (this );
292
286
}
@@ -402,8 +396,9 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
402
396
this .mask = null ;
403
397
this .hotspotX = hotspotX ;
404
398
this .hotspotY = hotspotY ;
405
- this . handle = setupCursorFromImageData (device , this .source , hotspotX , hotspotY );
399
+ long handle = setupCursorFromImageData (device , this .source , hotspotX , hotspotY );
406
400
isIcon = true ;
401
+ zoomLevelToHandle .put (DEFAULT_ZOOM , handle );
407
402
init ();
408
403
this .device .registerResourceWithZoomSupport (this );
409
404
}
@@ -424,41 +419,40 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
424
419
* @noreference This method is not intended to be referenced by clients.
425
420
*/
426
421
public static Long win32_getHandle (Cursor cursor , int zoom ) {
427
- if (cursor .isDisposed () ) {
428
- return cursor . handle ;
422
+ if (cursor .zoomLevelToHandle . size () == 0 ) {
423
+ return 0L ;
429
424
}
430
425
if (cursor .zoomLevelToHandle .get (zoom ) != null ) {
431
426
return cursor .zoomLevelToHandle .get (zoom );
432
427
}
433
428
434
- if (cursor .source == null ) {
435
- cursor .setHandleForZoomLevel (cursor .handle , zoom );
429
+ if (cursor .style != null ) {
430
+ // 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.
431
+ long handle = setupCursorFromStyle (cursor .style );
432
+ cursor .setHandleForZoomLevel (handle , zoom );
436
433
} else {
437
434
ImageData source ;
438
435
if (cursor .imageDataProvider != null ) {
439
436
Image tempImage = new Image (cursor .getDevice (), cursor .imageDataProvider );
440
437
source = tempImage .getImageData (zoom );
441
438
tempImage .dispose ();
439
+ } else {
440
+ source = DPIUtil .scaleImageData (cursor .getDevice (), cursor .source , zoom , DEFAULT_ZOOM );
442
441
}
443
- else {
444
- source = DPIUtil .scaleImageData (cursor .device , cursor .source , zoom , DEFAULT_ZOOM );
445
- }
442
+
446
443
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 );
444
+ long handle = setupCursorFromImageData (cursor .getDevice () , source , Win32DPIUtils .pointToPixel (cursor .hotspotX , zoom ), Win32DPIUtils .pointToPixel (cursor .hotspotY , zoom ));
445
+ cursor .setHandleForZoomLevel (handle , zoom );
449
446
} 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 );
447
+ ImageData mask = DPIUtil .scaleImageData (cursor .getDevice () , cursor .mask , zoom , DEFAULT_ZOOM );
448
+ long handle = setupCursorFromImageData ( source , mask , Win32DPIUtils .pointToPixel (cursor .hotspotX , zoom ), Win32DPIUtils .pointToPixel (cursor .hotspotY , zoom ));
449
+ cursor .setHandleForZoomLevel (handle , zoom );
453
450
}
454
451
}
455
452
return cursor .zoomLevelToHandle .get (zoom );
456
453
}
457
454
458
455
private void setHandleForZoomLevel (long handle , Integer zoom ) {
459
- if (this .handle == 0 ) {
460
- this .handle = handle ; // Set handle for default zoom level
461
- }
462
456
if (zoom != null && !zoomLevelToHandle .containsKey (zoom )) {
463
457
zoomLevelToHandle .put (zoom , handle );
464
458
}
@@ -480,14 +474,14 @@ void destroy () {
480
474
// }
481
475
device .deregisterResourceWithZoomSupport (this );
482
476
destroyHandle ();
477
+ this .isDestroyed = true ;
483
478
}
484
479
485
480
private void destroyHandle () {
486
481
for (Long handle : zoomLevelToHandle .values ()) {
487
482
destroyHandle (handle );
488
483
}
489
484
zoomLevelToHandle .clear ();
490
- handle = 0 ;
491
485
}
492
486
493
487
private void destroyHandle (long handle ) {
@@ -521,7 +515,7 @@ public boolean equals (Object object) {
521
515
if (object == this ) return true ;
522
516
if (!(object instanceof Cursor )) return false ;
523
517
Cursor cursor = (Cursor ) object ;
524
- return device == cursor .device && handle == cursor . handle ;
518
+ return device == cursor .device && win32_getHandle ( this , DEFAULT_ZOOM ) == win32_getHandle ( cursor , DEFAULT_ZOOM ) ;
525
519
}
526
520
527
521
/**
@@ -536,7 +530,7 @@ public boolean equals (Object object) {
536
530
*/
537
531
@ Override
538
532
public int hashCode () {
539
- return ( int ) handle ;
533
+ return this . zoomLevelToHandle . get ( DEFAULT_ZOOM ) != null ? this . zoomLevelToHandle . get ( DEFAULT_ZOOM ). intValue () : 0 ;
540
534
}
541
535
542
536
/**
@@ -551,7 +545,7 @@ public int hashCode () {
551
545
*/
552
546
@ Override
553
547
public boolean isDisposed () {
554
- return handle == 0 ;
548
+ return isDestroyed ;
555
549
}
556
550
557
551
/**
@@ -563,7 +557,7 @@ public boolean isDisposed() {
563
557
@ Override
564
558
public String toString () {
565
559
if (isDisposed ()) return "Cursor {*DISPOSED*}" ;
566
- return "Cursor {" + handle + "}" ;
560
+ return "Cursor {" + zoomLevelToHandle + "}" ;
567
561
}
568
562
569
563
@ Override
0 commit comments