comparison snippets/button/Snippet294.d @ 114:0de3dab4d6e1

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