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