comparison snippets/button/Snippet162.d @ 117:8cdaac0dc743

Added more snippets from TomD
author Frank Benoit <benoit@tionex.de>
date Sat, 12 Jul 2008 20:09:06 +0200
parents
children
comparison
equal deleted inserted replaced
116:f53c6274734f 117:8cdaac0dc743
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 * Thomas Demmer <t_demmer AT web DOT de>
12 *******************************************************************************/
13 module button.Snippet162;
14
15 /*
16 * Adding an accessible listener to provide state information
17 *
18 * For a list of all SWT example snippets see
19 * http://www.eclipse.org/swt/snippets/
20 */
21 import dwt.DWT;
22 import dwt.accessibility.ACC;
23 import dwt.accessibility.Accessible;
24 import dwt.accessibility.AccessibleAdapter;
25 import dwt.accessibility.AccessibleControlListener;
26 import dwt.accessibility.AccessibleControlAdapter;
27 import dwt.accessibility.AccessibleControlEvent;
28 import dwt.accessibility.AccessibleEvent;
29 import dwt.graphics.GC;
30 import dwt.graphics.Image;
31 import dwt.graphics.Point;
32
33 import dwt.layout.FillLayout;
34 import dwt.widgets.Display;
35 import dwt.widgets.Shell;
36 import dwt.widgets.Table;
37 import dwt.widgets.TableColumn;
38 import dwt.widgets.TableItem;
39
40 import dwt.dwthelper.utils;
41
42
43 void main(String[] args){
44 Snippet162.main(args);
45 }
46
47 public class Snippet162 {
48
49 final static String STATE = "CheckedIndices";
50
51 public static void main (String [] args) {
52 final Display display = new Display ();
53 Image checkedImage = getCheckedImage (display);
54 Image uncheckedImage = getUncheckedImage (display);
55 Shell shell = new Shell (display);
56 shell.setLayout (new FillLayout ());
57 final Table table = new Table (shell, DWT.BORDER);
58 TableColumn column1 = new TableColumn (table, DWT.NONE);
59 TableColumn column2 = new TableColumn (table, DWT.NONE);
60 TableColumn column3 = new TableColumn (table, DWT.NONE);
61 TableItem item1 = new TableItem (table, DWT.NONE);
62 item1.setText ( ["first item", "a", "b"]);
63 item1.setImage (1, uncheckedImage);
64 item1.setImage (2, uncheckedImage);
65 item1.setData (STATE, null);
66 TableItem item2 = new TableItem (table, DWT.NONE);
67 item2.setText ( ["second item", "c", "d"]);
68 item2.setImage (1, uncheckedImage);
69 item2.setImage (2, checkedImage);
70 item2.setData (STATE, new ArrayWrapperInt([2]));
71 TableItem item3 = new TableItem (table, DWT.NONE);
72 item3.setText ( ["third", "e", "f"]);
73 item3.setImage (1, checkedImage);
74 item3.setImage (2, checkedImage);
75 item3.setData (STATE, new ArrayWrapperInt( [1, 2]));
76 column1.pack ();
77 column2.pack ();
78 column3.pack ();
79
80 Accessible accessible = table.getAccessible ();
81 accessible.addAccessibleListener( new class() AccessibleAdapter {
82 public void getName (AccessibleEvent e) {
83 super.getName (e);
84 if (e.childID >= 0 && e.childID < table.getItemCount ()) {
85 TableItem item = table.getItem (e.childID);
86 Point pt = display.getCursorLocation ();
87 pt = display.map (null, table, pt);
88 for (int i = 0; i < table.getColumnCount (); i++) {
89 if (item.getBounds (i).contains (pt)) {
90 int [] data = (cast(ArrayWrapperInt)item.getData (STATE)).array;
91 bool checked = false;
92 if (data !is null) {
93 for (int j = 0; j < data.length; j++) {
94 if (data [j] == i) {
95 checked = true;
96 break;
97 }
98 }
99 }
100 e.result = item.getText (i) ~ " " ~ (checked ? "checked" : "unchecked");
101 break;
102 }
103 }
104 }
105 }
106 });
107
108 accessible.addAccessibleControlListener (new class() AccessibleControlAdapter {
109 public void getState (AccessibleControlEvent e) {
110 super.getState (e);
111 if (e.childID >= 0 && e.childID < table.getItemCount ()) {
112 TableItem item = table.getItem (e.childID);
113 int [] data =(cast(ArrayWrapperInt)item.getData (STATE)).array;
114 if (data !is null) {
115 Point pt = display.getCursorLocation ();
116 pt = display.map (null, table, pt);
117 for (int i = 0; i < data.length; i++) {
118 if (item.getBounds (data [i]).contains (pt)) {
119 e.detail |= ACC.STATE_CHECKED;
120 break;
121 }
122 }
123 }
124 }
125 }
126 });
127 shell.open ();
128 while (!shell.isDisposed ()) {
129 if (!display.readAndDispatch ()) display.sleep ();
130 }
131 checkedImage.dispose ();
132 uncheckedImage.dispose ();
133 display.dispose ();
134 }
135
136 static Image getCheckedImage (Display display) {
137 Image image = new Image (display, 16, 16);
138 GC gc = new GC (image);
139 gc.setBackground (display.getSystemColor (DWT.COLOR_YELLOW));
140 gc.fillOval (0, 0, 16, 16);
141 gc.setForeground (display.getSystemColor (DWT.COLOR_DARK_GREEN));
142 gc.drawLine (0, 0, 16, 16);
143 gc.drawLine (16, 0, 0, 16);
144 gc.dispose ();
145 return image;
146 }
147
148 static Image getUncheckedImage (Display display) {
149 Image image = new Image (display, 16, 16);
150 GC gc = new GC (image);
151 gc.setBackground (display.getSystemColor (DWT.COLOR_YELLOW));
152 gc.fillOval (0, 0, 16, 16);
153 gc.dispose ();
154 return image;
155 }
156 }