28
|
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 * D Port:
|
|
11 * Thomas Demmer <t_demmer AT web DOT de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.snippets.Snippet294;
|
|
14
|
|
15 /* Port OK */
|
|
16 import org.eclipse.swt.SWT;
|
|
17 import org.eclipse.swt.graphics.Image;
|
|
18 import org.eclipse.swt.graphics.Region;
|
|
19 import org.eclipse.swt.layout.FillLayout;
|
|
20 import org.eclipse.swt.widgets.Button;
|
|
21 import org.eclipse.swt.widgets.Display;
|
|
22 import org.eclipse.swt.widgets.Event;
|
|
23 import org.eclipse.swt.widgets.Listener;
|
|
24 import org.eclipse.swt.widgets.Shell;
|
|
25
|
|
26 import java.lang.all;
|
112
|
27 version(Tango){
|
|
28 import tango.util.Convert;
|
|
29 } else { // Phobos
|
|
30 import std.conv;
|
|
31 }
|
28
|
32
|
|
33 void main(String[] args){
|
|
34 Snippet294.main(args);
|
|
35 }
|
|
36
|
|
37 /*
|
|
38 * Region on a control: create a non-rectangular button
|
|
39 *
|
|
40 * For a list of all SWT example snippets see
|
|
41 * http://www.eclipse.org/swt/snippets/
|
|
42 *
|
|
43 * @since 3.4
|
|
44 */
|
|
45
|
|
46 public class Snippet294 {
|
|
47
|
|
48 static int[] circle(int r, int offsetX, int offsetY) {
|
|
49 int[] polygon = new int[8 * r + 4];
|
|
50 // x^2 + y^2 = r^2
|
|
51 for (int i = 0; i < 2 * r + 1; i++) {
|
|
52 int x = i - r;
|
|
53 int y = cast(int)Math.sqrt(cast(real)(r*r - x*x));
|
|
54 polygon[2*i] = offsetX + x;
|
|
55 polygon[2*i+1] = offsetY + y;
|
|
56 polygon[8*r - 2*i - 2] = offsetX + x;
|
|
57 polygon[8*r - 2*i - 1] = offsetY - y;
|
|
58 }
|
|
59 return polygon;
|
|
60 }
|
|
61
|
|
62 public static void main(String[] args) {
|
|
63 Display display = new Display();
|
|
64 Shell shell = new Shell(display);
|
|
65 shell.setText("Regions on a Control");
|
|
66 shell.setLayout(new FillLayout());
|
|
67 shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_RED));
|
|
68
|
|
69 Button b2 = new Button(shell, SWT.PUSH);
|
|
70 b2.setText("Button with Regions");
|
|
71
|
|
72 // define a region that looks like a circle with two holes in ot
|
|
73 Region region = new Region();
|
|
74 region.add(circle(67, 87, 77));
|
|
75 region.subtract(circle(20, 87, 47));
|
|
76 region.subtract(circle(20, 87, 113));
|
|
77
|
|
78 // define the shape of the button using setRegion
|
|
79 b2.setRegion(region);
|
|
80 b2.setLocation(100,50);
|
|
81
|
|
82 b2.addListener(SWT.Selection, new class() Listener {
|
|
83 public void handleEvent(Event e) {
|
|
84 shell.close();
|
|
85 }
|
|
86 });
|
|
87
|
|
88 shell.setSize(200,200);
|
|
89 shell.open();
|
|
90
|
|
91 while (!shell.isDisposed()) {
|
|
92 if (!display.readAndDispatch())
|
|
93 display.sleep();
|
|
94 }
|
|
95 region.dispose();
|
|
96 display.dispose();
|
|
97 }
|
|
98 }
|