comparison snippets/shell/Snippet134.d @ 78:4a04b6759f98

Clean up directory names
author John Reimer <terminal.node@gmail.com>
date Sat, 10 May 2008 13:32:45 -0700
parents
children 1f0a7a472680
comparison
equal deleted inserted replaced
76:04f122e90b0a 78:4a04b6759f98
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 snippets.shell.Snippet134;
14
15 /*
16 * Shell example snippet: create a non-rectangular window
17 *
18 * For a list of all SWT example snippets see
19 * http://www.eclipse.org/swt/snippets/
20 *
21 * @since 3.0
22 */
23 import dwt.DWT;
24 import dwt.graphics.Region;
25 import dwt.graphics.Point;
26 import dwt.graphics.Rectangle;
27 import dwt.widgets.Display;
28 import dwt.widgets.Shell;
29 import dwt.widgets.Button;
30 import dwt.widgets.Listener;
31 import dwt.widgets.Event;
32
33 import dwt.dwthelper.utils;
34
35 version(JIVE){
36 import jive.stacktrace;
37 }
38
39 int[] circle(int r, int offsetX, int offsetY) {
40 int[] polygon = new int[8 * r + 4];
41 //x^2 + y^2 = r^2
42 for (int i = 0; i < 2 * r + 1; i++) {
43 int x = i - r;
44 int y = cast(int)Math.sqrt( cast(float)(r*r - x*x));
45 polygon[2*i] = offsetX + x;
46 polygon[2*i+1] = offsetY + y;
47 polygon[8*r - 2*i - 2] = offsetX + x;
48 polygon[8*r - 2*i - 1] = offsetY - y;
49 }
50 return polygon;
51 }
52
53 Display display;
54 Shell shell;
55
56 void main(char[][] args) {
57 display = new Display();
58 //Shell must be created with style SWT.NO_TRIM
59 shell = new Shell(display, DWT.NO_TRIM | DWT.ON_TOP);
60 shell.setBackground(display.getSystemColor(DWT.COLOR_RED));
61 //define a region that looks like a key hole
62 Region region = new Region();
63 region.add(circle(67, 67, 67));
64 region.subtract(circle(20, 67, 50));
65 region.subtract([67, 50, 55, 105, 79, 105]);
66 //define the shape of the shell using setRegion
67 shell.setRegion(region);
68 Rectangle size = region.getBounds();
69 shell.setSize(size.width, size.height);
70 //add ability to move shell around
71 Listener l = new class Listener {
72 Point origin;
73 public void handleEvent(Event e) {
74 switch (e.type) {
75 case DWT.MouseDown:
76 origin = new Point(e.x, e.y);
77 break;
78 case DWT.MouseUp:
79 origin = null;
80 break;
81 case DWT.MouseMove:
82 if (origin !is null) {
83 Point p = display.map(shell, null, e.x, e.y);
84 shell.setLocation(p.x - origin.x, p.y - origin.y);
85 }
86 break;
87 }
88 }
89 };
90 shell.addListener(DWT.MouseDown, l);
91 shell.addListener(DWT.MouseUp, l);
92 shell.addListener(DWT.MouseMove, l);
93 //add ability to close shell
94 Button b = new Button(shell, DWT.PUSH);
95 b.setBackground(shell.getBackground());
96 b.setText("close");
97 b.pack();
98 b.setLocation(10, 68);
99 b.addListener(DWT.Selection, new class Listener {
100 public void handleEvent(Event e) {
101 shell.close();
102 }
103 });
104 shell.open();
105 while (!shell.isDisposed()) {
106 if (!display.readAndDispatch())
107 display.sleep();
108 }
109 region.dispose();
110 display.dispose();
111 }