1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet119"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * D Port:
46  *     Thomas Demmer <t_demmer AT web DOT de>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet119;
49 
50 /*
51  * Cursor example snippet: create a color cursor from a source and a mask
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  *
56  * @since 3.0
57  */
58 import org.eclipse.swt.SWT;
59 import org.eclipse.swt.events.PaintListener;
60 import org.eclipse.swt.graphics.Color;
61 import org.eclipse.swt.graphics.Cursor;
62 import org.eclipse.swt.graphics.GC;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.graphics.ImageData;
65 import org.eclipse.swt.graphics.PaletteData;
66 import org.eclipse.swt.widgets.Display;
67 import org.eclipse.swt.widgets.Shell;
68 
69 import java.lang.all;
70 
71 
72 
73 auto srcData = [
74         cast(byte)0x11, cast(byte)0x11, cast(byte)0x11, cast(byte)0x00, cast(byte)0x00, cast(byte)0x11, cast(byte)0x11, cast(byte)0x11,
75         cast(byte)0x11, cast(byte)0x10, cast(byte)0x00, cast(byte)0x01, cast(byte)0x10, cast(byte)0x00, cast(byte)0x01, cast(byte)0x11,
76         cast(byte)0x11, cast(byte)0x00, cast(byte)0x22, cast(byte)0x01, cast(byte)0x10, cast(byte)0x33, cast(byte)0x00, cast(byte)0x11,
77         cast(byte)0x10, cast(byte)0x02, cast(byte)0x22, cast(byte)0x01, cast(byte)0x10, cast(byte)0x33, cast(byte)0x30, cast(byte)0x01,
78         cast(byte)0x10, cast(byte)0x22, cast(byte)0x22, cast(byte)0x01, cast(byte)0x10, cast(byte)0x33, cast(byte)0x33, cast(byte)0x01,
79         cast(byte)0x10, cast(byte)0x22, cast(byte)0x22, cast(byte)0x01, cast(byte)0x10, cast(byte)0x33, cast(byte)0x33, cast(byte)0x01,
80         cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00,
81         cast(byte)0x01, cast(byte)0x11, cast(byte)0x11, cast(byte)0x01, cast(byte)0x10, cast(byte)0x11, cast(byte)0x11, cast(byte)0x10,
82         cast(byte)0x01, cast(byte)0x11, cast(byte)0x11, cast(byte)0x01, cast(byte)0x10, cast(byte)0x11, cast(byte)0x11, cast(byte)0x10,
83         cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00, cast(byte)0x00,
84         cast(byte)0x10, cast(byte)0x44, cast(byte)0x44, cast(byte)0x01, cast(byte)0x10, cast(byte)0x55, cast(byte)0x55, cast(byte)0x01,
85         cast(byte)0x10, cast(byte)0x44, cast(byte)0x44, cast(byte)0x01, cast(byte)0x10, cast(byte)0x55, cast(byte)0x55, cast(byte)0x01,
86         cast(byte)0x10, cast(byte)0x04, cast(byte)0x44, cast(byte)0x01, cast(byte)0x10, cast(byte)0x55, cast(byte)0x50, cast(byte)0x01,
87         cast(byte)0x11, cast(byte)0x00, cast(byte)0x44, cast(byte)0x01, cast(byte)0x10, cast(byte)0x55, cast(byte)0x00, cast(byte)0x11,
88         cast(byte)0x11, cast(byte)0x10, cast(byte)0x00, cast(byte)0x01, cast(byte)0x10, cast(byte)0x00, cast(byte)0x01, cast(byte)0x11,
89         cast(byte)0x11, cast(byte)0x11, cast(byte)0x11, cast(byte)0x00, cast(byte)0x00, cast(byte)0x11, cast(byte)0x11, cast(byte)0x11,
90 ];
91 
92 auto mskData = [
93         cast(byte)0x03, cast(byte)0xc0,
94         cast(byte)0x1f, cast(byte)0xf8,
95         cast(byte)0x3f, cast(byte)0xfc,
96         cast(byte)0x7f, cast(byte)0xfe,
97         cast(byte)0x7f, cast(byte)0xfe,
98         cast(byte)0x7f, cast(byte)0xfe,
99         cast(byte)0xff, cast(byte)0xff,
100         cast(byte)0xfe, cast(byte)0x7f,
101         cast(byte)0xfe, cast(byte)0x7f,
102         cast(byte)0xff, cast(byte)0xff,
103         cast(byte)0x7f, cast(byte)0xfe,
104         cast(byte)0x7f, cast(byte)0xfe,
105         cast(byte)0x7f, cast(byte)0xfe,
106         cast(byte)0x3f, cast(byte)0xfc,
107         cast(byte)0x1f, cast(byte)0xf8,
108         cast(byte)0x03, cast(byte)0xc0
109 ];
110 
111 void main (String [] args) {
112     Display display = new Display();
113     Color white = display.getSystemColor (SWT.COLOR_WHITE);
114     Color black = display.getSystemColor (SWT.COLOR_BLACK);
115     Color yellow = display.getSystemColor (SWT.COLOR_YELLOW);
116     Color red = display.getSystemColor (SWT.COLOR_RED);
117     Color green = display.getSystemColor (SWT.COLOR_GREEN);
118     Color blue = display.getSystemColor (SWT.COLOR_BLUE);
119 
120     //Create a source ImageData of depth 4
121     PaletteData palette = new PaletteData ([black.getRGB(), white.getRGB(), yellow.getRGB(),
122                                             red.getRGB(), blue.getRGB(), green.getRGB()]);
123     ImageData sourceData = new ImageData (16, 16, 4, palette, 1, srcData[]);
124 
125     //Create a mask ImageData of depth 1 (monochrome)
126     palette = new PaletteData ([black.getRGB(), white.getRGB()]);
127     ImageData maskData = new ImageData (16, 16, 1, palette, 1, mskData[]);
128 
129     //Set mask
130     sourceData.maskData = maskData.data;
131     sourceData.maskPad = maskData.scanlinePad;
132 
133     //Create cursor
134     Cursor cursor = new Cursor(display, sourceData, 10, 10);
135 
136     //Remove mask to draw them separately just to show what they look like
137     sourceData.maskData = null;
138     sourceData.maskPad = -1;
139 
140     Shell shell = new Shell(display);
141     Image source = new Image (display,sourceData);
142     Image mask = new Image (display, maskData);
143     shell.addPaintListener(new class PaintListener{
144         public void paintControl(PaintEvent e) {
145             GC gc = e.gc;
146             int x = 10, y = 10;
147             String stringSource = "source: ";
148             String stringMask = "mask: ";
149             gc.drawString(stringSource, x, y);
150             gc.drawString(stringMask, x, y + 30);
151             x += Math.max(gc.stringExtent(stringSource).x, gc.stringExtent(stringMask).x);
152             gc.drawImage(source, x, y);
153             gc.drawImage(mask, x, y + 30);
154         }
155     });
156     shell.setSize(150, 150);
157     shell.open();
158     shell.setCursor(cursor);
159 
160     while (!shell.isDisposed()) {
161         if (!display.readAndDispatch())
162             display.sleep();
163     }
164     cursor.dispose();
165     source.dispose();
166     mask.dispose();
167     display.dispose();
168 }