1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet134"
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  * Port to the D programming language:
46  *     Frank Benoit <benoit@tionex.de>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet134;
49 
50 /*
51  * Shell example snippet: create a non-rectangular window
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.graphics.Region;
60 import org.eclipse.swt.graphics.Point;
61 import org.eclipse.swt.graphics.Rectangle;
62 import org.eclipse.swt.widgets.Display;
63 import org.eclipse.swt.widgets.Shell;
64 import org.eclipse.swt.widgets.Button;
65 import org.eclipse.swt.widgets.Listener;
66 import org.eclipse.swt.widgets.Event;
67 
68 import java.lang.all;
69 
70 version(JIVE){
71     import jive.stacktrace;
72 }
73 
74 int[] circle(int r, int offsetX, int offsetY) {
75     int[] polygon = new int[8 * r + 4];
76     //x^2 + y^2 = r^2
77     for (int i = 0; i < 2 * r + 1; i++) {
78         int x = i - r;
79         int y = cast(int)Math.sqrt( cast(float)(r*r - x*x));
80         polygon[2*i] = offsetX + x;
81         polygon[2*i+1] = offsetY + y;
82         polygon[8*r - 2*i - 2] = offsetX + x;
83         polygon[8*r - 2*i - 1] = offsetY - y;
84     }
85     return polygon;
86 }
87 
88 void main() {
89     auto display = new Display();
90     //Shell must be created with style SWT.NO_TRIM
91     auto shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
92     shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
93     //define a region that looks like a key hole
94     Region region = new Region();
95     region.add(circle(67, 67, 67));
96     region.subtract(circle(20, 67, 50));
97     region.subtract([67, 50, 55, 105, 79, 105]);
98     //define the shape of the shell using setRegion
99     shell.setRegion(region);
100     Rectangle size = region.getBounds();
101     shell.setSize(size.width, size.height);
102     //add ability to move shell around
103     Listener l = new class Listener {
104         Point origin;
105         public void handleEvent(Event e) {
106             switch (e.type) {
107                 case SWT.MouseDown:
108                     origin = new Point(e.x, e.y);
109                     break;
110                 case SWT.MouseUp:
111                     origin = null;
112                     break;
113                 case SWT.MouseMove:
114                     if (origin !is null) {
115                         Point p = display.map(shell, null, e.x, e.y);
116                         shell.setLocation(p.x - origin.x, p.y - origin.y);
117                     }
118                     break;
119                 default:
120             }
121         }
122     };
123     shell.addListener(SWT.MouseDown, l);
124     shell.addListener(SWT.MouseUp, l);
125     shell.addListener(SWT.MouseMove, l);
126     //add ability to close shell
127     Button b = new Button(shell, SWT.PUSH);
128     b.setBackground(shell.getBackground());
129     b.setText("close");
130     b.pack();
131     b.setLocation(10, 68);
132     b.addListener(SWT.Selection, new class Listener {
133         public void handleEvent(Event e) {
134             shell.close();
135         }
136     });
137     shell.open();
138     while (!shell.isDisposed()) {
139         if (!display.readAndDispatch())
140             display.sleep();
141     }
142     region.dispose();
143     display.dispose();
144 }