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
*/
@@ -70,6 +58,8 @@ public final class Cursor extends Resource {
70
58
71
59
private final CursorHandleProvider cursorHandleProvider ;
72
60
61
+ private boolean isDestroyed ;
62
+
73
63
/**
74
64
* Constructs a new cursor given a device and a style
75
65
* constant describing the desired cursor appearance.
@@ -119,7 +109,6 @@ public final class Cursor extends Resource {
119
109
public Cursor (Device device , int style ) {
120
110
super (device );
121
111
this .cursorHandleProvider = new StyleCursorHandleProvider (style );
122
- this .handle = this .cursorHandleProvider .createHandle (device , DEFAULT_ZOOM ).getHandle ();
123
112
init ();
124
113
this .device .registerResourceWithZoomSupport (this );
125
114
}
@@ -160,7 +149,6 @@ public Cursor(Device device, int style) {
160
149
public Cursor (Device device , ImageData source , ImageData mask , int hotspotX , int hotspotY ) {
161
150
super (device );
162
151
this .cursorHandleProvider = new ImageDataWithMaskCursorHandleProvider (source , mask , hotspotX , hotspotY );
163
- this .handle = this .cursorHandleProvider .createHandle (device , DEFAULT_ZOOM ).getHandle ();
164
152
init ();
165
153
this .device .registerResourceWithZoomSupport (this );
166
154
}
@@ -229,7 +217,6 @@ private static CursorHandle setupCursorFromImageData(ImageData source, ImageData
229
217
public Cursor (Device device , ImageData source , int hotspotX , int hotspotY ) {
230
218
super (device );
231
219
this .cursorHandleProvider = new ImageDataCursorHandleProvider (source , hotspotX , hotspotY );
232
- this .handle = this .cursorHandleProvider .createHandle (device , DEFAULT_ZOOM ).getHandle ();
233
220
init ();
234
221
this .device .registerResourceWithZoomSupport (this );
235
222
}
@@ -341,7 +328,6 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
341
328
super (device );
342
329
if (imageDataProvider == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
343
330
this .cursorHandleProvider = new ImageDataProviderCursorHandleProvider (imageDataProvider , hotspotX , hotspotY );
344
- this .handle = this .cursorHandleProvider .createHandle (device , DEFAULT_ZOOM ).getHandle ();
345
331
init ();
346
332
this .device .registerResourceWithZoomSupport (this );
347
333
}
@@ -363,7 +349,7 @@ public Cursor(Device device, ImageDataProvider imageDataProvider, int hotspotX,
363
349
*/
364
350
public static Long win32_getHandle (Cursor cursor , int zoom ) {
365
351
if (cursor .isDisposed ()) {
366
- return cursor . handle ;
352
+ return 0L ;
367
353
}
368
354
if (cursor .zoomLevelToHandle .get (zoom ) != null ) {
369
355
return cursor .zoomLevelToHandle .get (zoom ).getHandle ();
@@ -376,9 +362,6 @@ public static Long win32_getHandle (Cursor cursor, int zoom) {
376
362
}
377
363
378
364
private void setHandleForZoomLevel (CursorHandle handle , Integer zoom ) {
379
- if (this .handle == 0 ) {
380
- this .handle = handle .getHandle (); // Set handle for default zoom level
381
- }
382
365
if (zoom != null && !zoomLevelToHandle .containsKey (zoom )) {
383
366
zoomLevelToHandle .put (zoom , handle );
384
367
}
@@ -407,7 +390,7 @@ private void destroyHandle () {
407
390
handle .destroy ();
408
391
}
409
392
zoomLevelToHandle .clear ();
410
- handle = 0 ;
393
+ this . isDestroyed = true ;
411
394
}
412
395
413
396
/**
@@ -425,7 +408,7 @@ public boolean equals (Object object) {
425
408
if (object == this ) return true ;
426
409
if (!(object instanceof Cursor )) return false ;
427
410
Cursor cursor = (Cursor ) object ;
428
- return device == cursor .device && handle == cursor . handle ;
411
+ return device == cursor .device && win32_getHandle ( this , DEFAULT_ZOOM ) == win32_getHandle ( cursor , DEFAULT_ZOOM ) ;
429
412
}
430
413
431
414
/**
@@ -440,7 +423,7 @@ public boolean equals (Object object) {
440
423
*/
441
424
@ Override
442
425
public int hashCode () {
443
- return ( int ) handle ;
426
+ return win32_getHandle ( this , DEFAULT_ZOOM ). intValue () ;
444
427
}
445
428
446
429
/**
@@ -455,7 +438,7 @@ public int hashCode () {
455
438
*/
456
439
@ Override
457
440
public boolean isDisposed () {
458
- return handle == 0 ;
441
+ return isDestroyed ;
459
442
}
460
443
461
444
/**
@@ -467,7 +450,7 @@ public boolean isDisposed() {
467
450
@ Override
468
451
public String toString () {
469
452
if (isDisposed ()) return "Cursor {*DISPOSED*}" ;
470
- return "Cursor {" + handle + "}" ;
453
+ return "Cursor {" + zoomLevelToHandle + "}" ;
471
454
}
472
455
473
456
@ Override
@@ -531,19 +514,23 @@ private static interface CursorHandleProvider {
531
514
}
532
515
533
516
private static class StyleCursorHandleProvider implements CursorHandleProvider {
534
- private final int style ;
517
+ private final long lpCursorName ;
535
518
536
519
public StyleCursorHandleProvider (int style ) {
537
- this .style = style ;
520
+ this .lpCursorName = setupCursorFromStyle ( style ) ;
538
521
}
539
522
540
523
@ Override
541
524
public CursorHandle createHandle (Device device , int zoom ) {
542
525
// zoom ignored, LoadCursor handles scaling internally
543
- return setupCursorFromStyle (this .style );
526
+ long handle = OS .LoadCursor (0 , lpCursorName );
527
+ if (handle == 0 ) {
528
+ SWT .error (SWT .ERROR_NO_HANDLES );
529
+ }
530
+ return new CustomCursorHandle (handle );
544
531
}
545
532
546
- private static final CursorHandle setupCursorFromStyle (int style ) {
533
+ private static final long setupCursorFromStyle (int style ) {
547
534
long lpCursorName = 0 ;
548
535
switch (style ) {
549
536
case SWT .CURSOR_HAND :
@@ -615,11 +602,7 @@ private static final CursorHandle setupCursorFromStyle(int style) {
615
602
default :
616
603
SWT .error (SWT .ERROR_INVALID_ARGUMENT );
617
604
}
618
- long handle = OS .LoadCursor (0 , lpCursorName );
619
- if (handle == 0 ) {
620
- SWT .error (SWT .ERROR_NO_HANDLES );
621
- }
622
- return new CustomCursorHandle (handle );
605
+ return lpCursorName ;
623
606
}
624
607
}
625
608
@@ -646,6 +629,13 @@ private static class ImageDataProviderCursorHandleProvider extends HotspotAwareC
646
629
647
630
public ImageDataProviderCursorHandleProvider (ImageDataProvider provider , int hotspotX , int hotspotY ) {
648
631
super (hotspotX , hotspotY );
632
+ ImageData source = provider .getImageData (DEFAULT_ZOOM );
633
+ if (source == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
634
+ /* Check the hotspots */
635
+ if (hotspotX >= source .width || hotspotX < 0 ||
636
+ hotspotY >= source .height || hotspotY < 0 ) {
637
+ SWT .error (SWT .ERROR_INVALID_ARGUMENT );
638
+ }
649
639
this .provider = provider ;
650
640
}
651
641
@@ -668,6 +658,12 @@ private static class ImageDataCursorHandleProvider extends HotspotAwareCursorHan
668
658
669
659
public ImageDataCursorHandleProvider (ImageData source , int hotspotX , int hotspotY ) {
670
660
super (hotspotX , hotspotY );
661
+ if (source == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
662
+ /* Check the hotspots */
663
+ if (hotspotX >= source .width || hotspotX < 0 ||
664
+ hotspotY >= source .height || hotspotY < 0 ) {
665
+ SWT .error (SWT .ERROR_INVALID_ARGUMENT );
666
+ }
671
667
this .source = source ;
672
668
}
673
669
@@ -684,6 +680,16 @@ private static class ImageDataWithMaskCursorHandleProvider extends ImageDataCurs
684
680
685
681
public ImageDataWithMaskCursorHandleProvider (ImageData source , ImageData mask , int hotspotX , int hotspotY ) {
686
682
super (source , hotspotX , hotspotY );
683
+ if (mask == null ) {
684
+ if (source .getTransparencyType () != SWT .TRANSPARENCY_MASK ) {
685
+ SWT .error (SWT .ERROR_NULL_ARGUMENT );
686
+ }
687
+ mask = source .getTransparencyMask ();
688
+ }
689
+ /* Check the bounds. Mask must be the same size as source */
690
+ if (mask .width != source .width || mask .height != source .height ) {
691
+ SWT .error (SWT .ERROR_INVALID_ARGUMENT );
692
+ }
687
693
this .mask = mask ;
688
694
}
689
695
0 commit comments