1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.graphics.Cursor;
14 
15 import java.lang.all;
16 
17 
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Resource;
20 import org.eclipse.swt.graphics.Device;
21 import org.eclipse.swt.graphics.ImageData;
22 import org.eclipse.swt.graphics.RGB;
23 import org.eclipse.swt.graphics.PaletteData;
24 
25 import org.eclipse.swt.internal.gtk.OS;
26 
27 version(Tango){
28     import tango.stdc..string;
29 } else {
30     import core.stdc..string;
31 }
32 
33 /**
34  * Instances of this class manage operating system resources that
35  * specify the appearance of the on-screen pointer. To create a
36  * cursor you specify the device and either a simple cursor style
37  * describing one of the standard operating system provided cursors
38  * or the image and mask data for the desired appearance.
39  * <p>
40  * Application code must explicitly invoke the <code>Cursor.dispose()</code>
41  * method to release the operating system resources managed by each instance
42  * when those instances are no longer required.
43  * </p>
44  * <dl>
45  * <dt><b>Styles:</b></dt>
46  * <dd>
47  *   CURSOR_ARROW, CURSOR_WAIT, CURSOR_CROSS, CURSOR_APPSTARTING, CURSOR_HELP,
48  *   CURSOR_SIZEALL, CURSOR_SIZENESW, CURSOR_SIZENS, CURSOR_SIZENWSE, CURSOR_SIZEWE,
49  *   CURSOR_SIZEN, CURSOR_SIZES, CURSOR_SIZEE, CURSOR_SIZEW, CURSOR_SIZENE, CURSOR_SIZESE,
50  *   CURSOR_SIZESW, CURSOR_SIZENW, CURSOR_UPARROW, CURSOR_IBEAM, CURSOR_NO, CURSOR_HAND
51  * </dd>
52  * </dl>
53  * <p>
54  * Note: Only one of the above styles may be specified.
55  * </p>
56  *
57  * @see <a href="http://www.eclipse.org/swt/snippets/#cursor">Cursor snippets</a>
58  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
59  */
60 public final class Cursor : Resource {
61     /**
62      * the handle to the OS cursor resource
63      * (Warning: This field is platform dependent)
64      * <p>
65      * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
66      * public API. It is marked public only so that it can be shared
67      * within the packages provided by SWT. It is not available on all
68      * platforms and should never be accessed from application code.
69      * </p>
70      */
71     public GdkCursor* handle;
72 
73     static const byte[] APPSTARTING_SRC = [ cast(byte)
74         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
75         0x0c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
76         0x7c, 0x00, 0x00, 0x00, cast(byte)0xfc, 0x00, 0x00, 0x00, cast(byte)0xfc, 0x01, 0x00, 0x00,
77         cast(byte)0xfc, 0x3b, 0x00, 0x00, 0x7c, 0x38, 0x00, 0x00, 0x6c, 0x54, 0x00, 0x00,
78         cast(byte)0xc4, cast(byte)0xdc, 0x00, 0x00, cast(byte)0xc0, 0x44, 0x00, 0x00, cast(byte)0x80, 0x39, 0x00, 0x00,
79         cast(byte)0x80, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
85 
86     static const byte[] APPSTARTING_MASK = [ cast(byte)
87         0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
88         0x1e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00,
89         cast(byte)0xfe, 0x00, 0x00, 0x00, cast(byte)0xfe, 0x01, 0x00, 0x00, cast(byte)0xfe, 0x3b, 0x00, 0x00,
90         cast(byte)0xfe, 0x7f, 0x00, 0x00, cast(byte)0xfe, 0x7f, 0x00, 0x00, cast(byte)0xfe, cast(byte)0xfe, 0x00, 0x00,
91         cast(byte)0xee, cast(byte)0xff, 0x01, 0x00, cast(byte)0xe4, cast(byte)0xff, 0x00, 0x00, cast(byte)0xc0, 0x7f, 0x00, 0x00,
92         cast(byte)0xc0, 0x7f, 0x00, 0x00, cast(byte)0x80, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
98 
99 this (Device device) {
100     super(device);
101 }
102 
103 /**
104  * Constructs a new cursor given a device and a style
105  * constant describing the desired cursor appearance.
106  * <p>
107  * You must dispose the cursor when it is no longer required.
108  * </p>
109  *
110  * @param device the device on which to allocate the cursor
111  * @param style the style of cursor to allocate
112  *
113  * @exception IllegalArgumentException <ul>
114  *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
115  *    <li>ERROR_INVALID_ARGUMENT - when an unknown style is specified</li>
116  * </ul>
117  * @exception SWTError <ul>
118  *    <li>ERROR_NO_HANDLES - if a handle could not be obtained for cursor creation</li>
119  * </ul>
120  *
121  * @see SWT#CURSOR_ARROW
122  * @see SWT#CURSOR_WAIT
123  * @see SWT#CURSOR_CROSS
124  * @see SWT#CURSOR_APPSTARTING
125  * @see SWT#CURSOR_HELP
126  * @see SWT#CURSOR_SIZEALL
127  * @see SWT#CURSOR_SIZENESW
128  * @see SWT#CURSOR_SIZENS
129  * @see SWT#CURSOR_SIZENWSE
130  * @see SWT#CURSOR_SIZEWE
131  * @see SWT#CURSOR_SIZEN
132  * @see SWT#CURSOR_SIZES
133  * @see SWT#CURSOR_SIZEE
134  * @see SWT#CURSOR_SIZEW
135  * @see SWT#CURSOR_SIZENE
136  * @see SWT#CURSOR_SIZESE
137  * @see SWT#CURSOR_SIZESW
138  * @see SWT#CURSOR_SIZENW
139  * @see SWT#CURSOR_UPARROW
140  * @see SWT#CURSOR_IBEAM
141  * @see SWT#CURSOR_NO
142  * @see SWT#CURSOR_HAND
143  */
144 public this(Device device, int style) {
145     super(device);
146     int shape = 0;
147     switch (style) {
148         case SWT.CURSOR_APPSTARTING:    break;
149         case SWT.CURSOR_ARROW:          shape = OS.GDK_LEFT_PTR; break;
150         case SWT.CURSOR_WAIT:           shape = OS.GDK_WATCH; break;
151         case SWT.CURSOR_CROSS:          shape = OS.GDK_CROSS; break;
152         case SWT.CURSOR_HAND:           shape = OS.GDK_HAND2; break;
153         case SWT.CURSOR_HELP:           shape = OS.GDK_QUESTION_ARROW; break;
154         case SWT.CURSOR_SIZEALL:        shape = OS.GDK_FLEUR; break;
155         case SWT.CURSOR_SIZENESW:       shape = OS.GDK_SIZING; break;
156         case SWT.CURSOR_SIZENS:         shape = OS.GDK_DOUBLE_ARROW; break;
157         case SWT.CURSOR_SIZENWSE:       shape = OS.GDK_SIZING; break;
158         case SWT.CURSOR_SIZEWE:         shape = OS.GDK_SB_H_DOUBLE_ARROW; break;
159         case SWT.CURSOR_SIZEN:          shape = OS.GDK_TOP_SIDE; break;
160         case SWT.CURSOR_SIZES:          shape = OS.GDK_BOTTOM_SIDE; break;
161         case SWT.CURSOR_SIZEE:          shape = OS.GDK_RIGHT_SIDE; break;
162         case SWT.CURSOR_SIZEW:          shape = OS.GDK_LEFT_SIDE; break;
163         case SWT.CURSOR_SIZENE:         shape = OS.GDK_TOP_RIGHT_CORNER; break;
164         case SWT.CURSOR_SIZESE:         shape = OS.GDK_BOTTOM_RIGHT_CORNER; break;
165         case SWT.CURSOR_SIZESW:         shape = OS.GDK_BOTTOM_LEFT_CORNER; break;
166         case SWT.CURSOR_SIZENW:         shape = OS.GDK_TOP_LEFT_CORNER; break;
167         case SWT.CURSOR_UPARROW:        shape = OS.GDK_SB_UP_ARROW; break;
168         case SWT.CURSOR_IBEAM:          shape = OS.GDK_XTERM; break;
169         case SWT.CURSOR_NO:             shape = OS.GDK_X_CURSOR; break;
170         default:
171             SWT.error(SWT.ERROR_INVALID_ARGUMENT);
172     }
173     if (shape is 0 && style is SWT.CURSOR_APPSTARTING) {
174         handle = createCursor(APPSTARTING_SRC, APPSTARTING_MASK, 32, 32, 2, 2, true);
175     } else {
176         handle = OS.gdk_cursor_new(shape);
177     }
178     if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
179     init_();
180 }
181 
182 /**
183  * Constructs a new cursor given a device, image and mask
184  * data describing the desired cursor appearance, and the x
185  * and y coordinates of the <em>hotspot</em> (that is, the point
186  * within the area covered by the cursor which is considered
187  * to be where the on-screen pointer is "pointing").
188  * <p>
189  * The mask data is allowed to be null, but in this case the source
190  * must be an ImageData representing an icon that specifies both
191  * color data and mask data.
192  * <p>
193  * You must dispose the cursor when it is no longer required.
194  * </p>
195  *
196  * @param device the device on which to allocate the cursor
197  * @param source the color data for the cursor
198  * @param mask the mask data for the cursor (or null)
199  * @param hotspotX the x coordinate of the cursor's hotspot
200  * @param hotspotY the y coordinate of the cursor's hotspot
201  *
202  * @exception IllegalArgumentException <ul>
203  *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
204  *    <li>ERROR_NULL_ARGUMENT - if the source is null</li>
205  *    <li>ERROR_NULL_ARGUMENT - if the mask is null and the source does not have a mask</li>
206  *    <li>ERROR_INVALID_ARGUMENT - if the source and the mask are not the same
207  *          size, or if the hotspot is outside the bounds of the image</li>
208  * </ul>
209  * @exception SWTError <ul>
210  *    <li>ERROR_NO_HANDLES - if a handle could not be obtained for cursor creation</li>
211  * </ul>
212  */
213 public this(Device device, ImageData source, ImageData mask, int hotspotX, int hotspotY) {
214     super(device);
215     if (source is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
216     if (mask is null) {
217         if (!(source.getTransparencyType() is SWT.TRANSPARENCY_MASK)) SWT.error(SWT.ERROR_NULL_ARGUMENT);
218         mask = source.getTransparencyMask();
219     }
220     /* Check the bounds. Mask must be the same size as source */
221     if (mask.width !is source.width || mask.height !is source.height) {
222         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
223     }
224     /* Check the hotspots */
225     if (hotspotX >= source.width || hotspotX < 0 ||
226         hotspotY >= source.height || hotspotY < 0) {
227         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
228     }
229     /* Convert depth to 1 */
230     source = ImageData.convertMask(source);
231     mask = ImageData.convertMask(mask);
232 
233     /* Swap the bits in each byte and convert to appropriate scanline pad */
234     byte[] sourceData = new byte[source.data.length];
235     byte[] maskData = new byte[mask.data.length];
236     byte[] data = source.data;
237     for (int i = 0; i < data.length; i++) {
238         byte s = data[i];
239         sourceData[i] = cast(byte)(((s & 0x80) >> 7) |
240             ((s & 0x40) >> 5) |
241             ((s & 0x20) >> 3) |
242             ((s & 0x10) >> 1) |
243             ((s & 0x08) << 1) |
244             ((s & 0x04) << 3) |
245             ((s & 0x02) << 5) |
246             ((s & 0x01) << 7));
247         sourceData[i] = cast(byte) ~cast(int)sourceData[i];
248     }
249     sourceData = ImageData.convertPad(sourceData, source.width, source.height, source.depth, source.scanlinePad, 1);
250     data = mask.data;
251     for (int i = 0; i < data.length; i++) {
252         byte s = data[i];
253         maskData[i] = cast(byte)(((s & 0x80) >> 7) |
254             ((s & 0x40) >> 5) |
255             ((s & 0x20) >> 3) |
256             ((s & 0x10) >> 1) |
257             ((s & 0x08) << 1) |
258             ((s & 0x04) << 3) |
259             ((s & 0x02) << 5) |
260             ((s & 0x01) << 7));
261         maskData[i] = cast(byte) ~cast(int)maskData[i];
262     }
263     maskData = ImageData.convertPad(maskData, mask.width, mask.height, mask.depth, mask.scanlinePad, 1);
264     handle = createCursor(maskData, sourceData, source.width, source.height, hotspotX, hotspotY, true);
265     if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
266     init_();
267 }
268 
269 /**
270  * Constructs a new cursor given a device, image data describing
271  * the desired cursor appearance, and the x and y coordinates of
272  * the <em>hotspot</em> (that is, the point within the area
273  * covered by the cursor which is considered to be where the
274  * on-screen pointer is "pointing").
275  * <p>
276  * You must dispose the cursor when it is no longer required.
277  * </p>
278  *
279  * @param device the device on which to allocate the cursor
280  * @param source the image data for the cursor
281  * @param hotspotX the x coordinate of the cursor's hotspot
282  * @param hotspotY the y coordinate of the cursor's hotspot
283  *
284  * @exception IllegalArgumentException <ul>
285  *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
286  *    <li>ERROR_NULL_ARGUMENT - if the image is null</li>
287  *    <li>ERROR_INVALID_ARGUMENT - if the hotspot is outside the bounds of the
288  *       image</li>
289  * </ul>
290  * @exception SWTError <ul>
291  *    <li>ERROR_NO_HANDLES - if a handle could not be obtained for cursor creation</li>
292  * </ul>
293  *
294  * @since 3.0
295  */
296 public this(Device device, ImageData source, int hotspotX, int hotspotY) {
297     super(device);
298     if (source is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
299     if (hotspotX >= source.width || hotspotX < 0 ||
300         hotspotY >= source.height || hotspotY < 0) {
301         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
302     }
303     GdkDisplay* display;
304     if (OS.GTK_VERSION >= OS.buildVERSION(2, 4, 0) && OS.gdk_display_supports_cursor_color(display = OS.gdk_display_get_default ())) {
305         int width = source.width;
306         int height = source.height;
307         PaletteData palette = source.palette;
308         auto pixbuf = OS.gdk_pixbuf_new(OS.GDK_COLORSPACE_RGB, true, 8, width, height);
309         if (pixbuf is null) SWT.error(SWT.ERROR_NO_HANDLES);
310         int stride = OS.gdk_pixbuf_get_rowstride(pixbuf);
311         auto data = OS.gdk_pixbuf_get_pixels(pixbuf);
312         byte[] buffer = source.data;
313         if (!palette.isDirect || source.depth !is 24 || stride !is source.bytesPerLine || palette.redMask !is 0xFF000000 || palette.greenMask !is 0xFF0000 || palette.blueMask !is 0xFF00) {
314             buffer = new byte[source.width * source.height * 4];
315             if (palette.isDirect) {
316                 ImageData.blit(ImageData.BLIT_SRC,
317                     source.data, source.depth, source.bytesPerLine, source.getByteOrder(), 0, 0, source.width, source.height, palette.redMask, palette.greenMask, palette.blueMask,
318                     ImageData.ALPHA_OPAQUE, null, 0, 0, 0,
319                     buffer, 32, source.width * 4, ImageData.MSB_FIRST, 0, 0, source.width, source.height, 0xFF000000, 0xFF0000, 0xFF00,
320                     false, false);
321             } else {
322                 RGB[] rgbs = palette.getRGBs();
323                 auto length = rgbs.length;
324                 byte[] srcReds = new byte[length];
325                 byte[] srcGreens = new byte[length];
326                 byte[] srcBlues = new byte[length];
327                 for (int i = 0; i < rgbs.length; i++) {
328                     RGB rgb = rgbs[i];
329                     if (rgb is null) continue;
330                     srcReds[i] = cast(byte)rgb.red;
331                     srcGreens[i] = cast(byte)rgb.green;
332                     srcBlues[i] = cast(byte)rgb.blue;
333                 }
334                 ImageData.blit(ImageData.BLIT_SRC,
335                     source.data, source.depth, source.bytesPerLine, source.getByteOrder(), 0, 0, source.width, source.height, srcReds, srcGreens, srcBlues,
336                     ImageData.ALPHA_OPAQUE, null, 0, 0, 0,
337                     buffer, 32, source.width * 4, ImageData.MSB_FIRST, 0, 0, source.width, source.height, 0xFF000000, 0xFF0000, 0xFF00,
338                     false, false);
339             }
340             if (source.maskData !is null || source.transparentPixel !is -1) {
341                 ImageData mask = source.getTransparencyMask();
342                 byte[] maskData = mask.data;
343                 int maskBpl = mask.bytesPerLine;
344                 int offset = 3, maskOffset = 0;
345                 for (int y = 0; y<source.height; y++) {
346                     for (int x = 0; x<source.width; x++) {
347                         buffer[offset] = ((maskData[maskOffset + (x >> 3)]) & (1 << (7 - (x & 0x7)))) !is 0 ? cast(byte)0xff : 0;
348                         offset += 4;
349                     }
350                     maskOffset += maskBpl;
351                 }
352             } else if (source.alpha !is -1) {
353                 byte alpha = cast(byte)source.alpha;
354                 for (int i=3; i<buffer.length; i+=4) {
355                     buffer[i] = alpha;
356                 }
357             } else if (source.alphaData !is null) {
358                 byte[] alphaData = source.alphaData;
359                 for (int i=3; i<buffer.length; i+=4) {
360                     buffer[i] = alphaData[i/4];
361                 }
362             }
363         }
364         memmove(data, buffer.ptr, stride * height);
365         handle = OS.gdk_cursor_new_from_pixbuf(display, pixbuf, hotspotX, hotspotY);
366         OS.g_object_unref(pixbuf);
367     } else {
368 
369         ImageData mask = source.getTransparencyMask();
370 
371         /* Ensure depth is equal to 1 */
372         if (source.depth > 1) {
373             /* Create a destination image with no data */
374             ImageData newSource = new ImageData(
375                 source.width, source.height, 1, ImageData.bwPalette(),
376                 1, null, 0, null, null, -1, -1, 0, 0, 0, 0, 0);
377 
378             byte[] newReds = [ cast(byte)0, cast(byte)255 ], newGreens = newReds, newBlues = newReds;
379 
380             /* Convert the source to a black and white image of depth 1 */
381             PaletteData palette = source.palette;
382             if (palette.isDirect) {
383                 ImageData.blit(ImageData.BLIT_SRC,
384                         source.data, source.depth, source.bytesPerLine, source.getByteOrder(), 0, 0, source.width, source.height, palette.redMask, palette.greenMask, palette.blueMask,
385                         ImageData.ALPHA_OPAQUE, null, 0, 0, 0,
386                         newSource.data, newSource.depth, newSource.bytesPerLine, newSource.getByteOrder(), 0, 0, newSource.width, newSource.height, newReds, newGreens, newBlues,
387                         false, false);
388             } else {
389                 RGB[] rgbs = palette.getRGBs();
390                 auto length = rgbs.length;
391                 byte[] srcReds = new byte[length];
392                 byte[] srcGreens = new byte[length];
393                 byte[] srcBlues = new byte[length];
394                 for (int i = 0; i < rgbs.length; i++) {
395                     RGB rgb = rgbs[i];
396                     if (rgb is null) continue;
397                     srcReds[i] = cast(byte)rgb.red;
398                     srcGreens[i] = cast(byte)rgb.green;
399                     srcBlues[i] = cast(byte)rgb.blue;
400                 }
401                 ImageData.blit(ImageData.BLIT_SRC,
402                         source.data, source.depth, source.bytesPerLine, source.getByteOrder(), 0, 0, source.width, source.height, srcReds, srcGreens, srcBlues,
403                         ImageData.ALPHA_OPAQUE, null, 0, 0, 0,
404                         newSource.data, newSource.depth, newSource.bytesPerLine, newSource.getByteOrder(), 0, 0, newSource.width, newSource.height, newReds, newGreens, newBlues,
405                         false, false);
406             }
407             source = newSource;
408         }
409 
410         /* Swap the bits in each byte and convert to appropriate scanline pad */
411         byte[] sourceData = new byte[source.data.length];
412         byte[] maskData = new byte[mask.data.length];
413         byte[] data = source.data;
414         for (int i = 0; i < data.length; i++) {
415             byte s = data[i];
416             sourceData[i] = cast(byte)(((s & 0x80) >> 7) |
417                 ((s & 0x40) >> 5) |
418                 ((s & 0x20) >> 3) |
419                 ((s & 0x10) >> 1) |
420                 ((s & 0x08) << 1) |
421                 ((s & 0x04) << 3) |
422                 ((s & 0x02) << 5) |
423                 ((s & 0x01) << 7));
424         }
425         sourceData = ImageData.convertPad(sourceData, source.width, source.height, source.depth, source.scanlinePad, 1);
426         data = mask.data;
427         for (int i = 0; i < data.length; i++) {
428             byte s = data[i];
429             maskData[i] = cast(byte)(((s & 0x80) >> 7) |
430                 ((s & 0x40) >> 5) |
431                 ((s & 0x20) >> 3) |
432                 ((s & 0x10) >> 1) |
433                 ((s & 0x08) << 1) |
434                 ((s & 0x04) << 3) |
435                 ((s & 0x02) << 5) |
436                 ((s & 0x01) << 7));
437         }
438         maskData = ImageData.convertPad(maskData, mask.width, mask.height, mask.depth, mask.scanlinePad, 1);
439         handle = createCursor(sourceData, maskData, source.width, source.height, hotspotX, hotspotY, false);
440     }
441     if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
442     init_();
443 }
444 
445 GdkCursor* createCursor(in byte[] sourceData, in byte[] maskData, int width, int height, int hotspotX, int hotspotY, bool reverse) {
446     auto sourcePixmap = OS.gdk_bitmap_create_from_data(null, cast(char*)sourceData.ptr, width, height);
447     auto maskPixmap = OS.gdk_bitmap_create_from_data(null, cast(char*)maskData.ptr, width, height);
448     GdkCursor* cursor = null;
449     if (sourcePixmap !is null && maskPixmap !is null) {
450         GdkColor* foreground = new GdkColor();
451         if (!reverse) foreground.red = foreground.green = foreground.blue = 0xFFFF;
452         GdkColor* background = new GdkColor();
453         if (reverse) background.red = background.green = background.blue = 0xFFFF;
454         cursor = OS.gdk_cursor_new_from_pixmap (cast(GdkPixmap*)sourcePixmap, cast(GdkPixmap*)maskPixmap, foreground, background, hotspotX, hotspotY);
455     }
456     if (sourcePixmap !is null) OS.g_object_unref (sourcePixmap);
457     if (maskPixmap !is null) OS.g_object_unref (maskPixmap);
458     return cursor;
459 }
460 
461 override
462 void destroy() {
463     OS.gdk_cursor_destroy(handle);
464     handle = null;
465 }
466 
467 /**
468  * Compares the argument to the receiver, and returns true
469  * if they represent the <em>same</em> object using a class
470  * specific comparison.
471  *
472  * @param object the object to compare with this object
473  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
474  *
475  * @see #hashCode
476  */
477 public override equals_t opEquals(Object object) {
478     if (object is this) return true;
479     if ( auto cursor = cast(Cursor)object ){
480         return device is cursor.device && handle is cursor.handle;
481     }
482     return false;
483 }
484 
485 /**
486  * Invokes platform specific functionality to allocate a new cursor.
487  * <p>
488  * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
489  * API for <code>Cursor</code>. It is marked public only so that it
490  * can be shared within the packages provided by SWT. It is not
491  * available on all platforms, and should never be called from
492  * application code.
493  * </p>
494  *
495  * @param device the device on which to allocate the color
496  * @param handle the handle for the cursor
497  *
498  * @private
499  */
500 public static Cursor gtk_new(Device device, GdkCursor* handle) {
501     Cursor cursor = new Cursor(device);
502     cursor.handle = handle;
503     return cursor;
504 }
505 
506 /**
507  * Returns an integer hash code for the receiver. Any two
508  * objects that return <code>true</code> when passed to
509  * <code>equals</code> must return the same value for this
510  * method.
511  *
512  * @return the receiver's hash
513  *
514  * @see #equals
515  */
516 public override hash_t toHash() {
517     return cast(hash_t)handle;
518 }
519 
520 /**
521  * Returns <code>true</code> if the cursor has been disposed,
522  * and <code>false</code> otherwise.
523  * <p>
524  * This method gets the dispose state for the cursor.
525  * When a cursor has been disposed, it is an error to
526  * invoke any other method using the cursor.
527  *
528  * @return <code>true</code> when the cursor is disposed and <code>false</code> otherwise
529  */
530 public override bool isDisposed() {
531     return handle is null;
532 }
533 
534 /**
535  * Returns a string containing a concise, human-readable
536  * description of the receiver.
537  *
538  * @return a string representation of the receiver
539  */
540 public override String toString () {
541     if (isDisposed()) return "Cursor {*DISPOSED*}";
542     return Format( "Cursor {{{}}", handle );
543 }
544 
545 }