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