comparison org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet21.d @ 28:69b1fa94a4a8

Added SWT snippets
author Frank Benoit <benoit@tionex.de>
date Sun, 22 Mar 2009 15:17:04 +0100
parents
children 4e5843b771cc
comparison
equal deleted inserted replaced
27:1bf55a6eb092 28:69b1fa94a4a8
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 * D Port:
11 * Bill Baxter <wbaxter> at gmail com
12 *******************************************************************************/
13 module org.eclipse.swt.snippets.Snippet21;
14
15 /*
16 * Canvas example snippet: implement tab traversal (behave like a tab group)
17 *
18 * For a list of all SWT example snippets see
19 * http://www.eclipse.org/swt/snippets/
20 */
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.Listener;
26 import org.eclipse.swt.widgets.Event;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.swt.widgets.Canvas;
30
31 import tango.io.Stdout;
32
33 import java.lang.all;
34
35 void main () {
36 Display display = new Display ();
37 final Color red = display.getSystemColor (SWT.COLOR_RED);
38 final Color blue = display.getSystemColor (SWT.COLOR_BLUE);
39 Shell shell = new Shell (display);
40 Button b = new Button (shell, SWT.PUSH);
41 b.setBounds (10, 10, 100, 32);
42 b.setText ("Button");
43 shell.setDefaultButton (b);
44 final Canvas c = new Canvas (shell, SWT.BORDER);
45 c.setBounds (10, 50, 100, 32);
46
47 void onTraverse(Event e, Canvas c) {
48 switch (e.detail) {
49 /* Do tab group traversal */
50 case SWT.TRAVERSE_ESCAPE:
51 case SWT.TRAVERSE_RETURN:
52 case SWT.TRAVERSE_TAB_NEXT:
53 case SWT.TRAVERSE_TAB_PREVIOUS:
54 case SWT.TRAVERSE_PAGE_NEXT:
55 case SWT.TRAVERSE_PAGE_PREVIOUS:
56 e.doit = true;
57 break;
58 }
59 }
60
61 void onFocusIn(Event e, Canvas c) {
62 c.setBackground (red);
63 }
64
65 void onFocusOut(Event e, Canvas c) {
66 c.setBackground (blue);
67 }
68
69 void onKeyDown (Event e, Canvas c) {
70 Stdout("KEY").newline;
71 for (int i=0; i<64; i++) {
72 Color c1 = red, c2 = blue;
73 if (c.isFocusControl ()) {
74 c1 = blue; c2 = red;
75 }
76 c.setBackground (c1);
77 c.update ();
78 c.setBackground (c2);
79 }
80 }
81
82 c.addListener (SWT.Traverse, dgListener(&onTraverse, c));
83 c.addListener (SWT.FocusIn, dgListener(&onFocusIn, c));
84 c.addListener (SWT.FocusOut, dgListener(&onFocusOut, c));
85 c.addListener (SWT.KeyDown, dgListener(&onKeyDown, c));
86
87 Text t = new Text (shell, SWT.SINGLE | DWT.BORDER);
88 t.setBounds (10, 85, 100, 32);
89
90 Text r = new Text (shell, SWT.MULTI | DWT.BORDER);
91 r.setBounds (10, 120, 100, 32);
92
93 c.setFocus ();
94 shell.setSize (200, 200);
95 shell.open ();
96 while (!shell.isDisposed()) {
97 if (!display.readAndDispatch ()) display.sleep ();
98 }
99 display.dispose ();
100 }