1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet162"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * D Port:
46  *     Thomas Demmer <t_demmer AT web DOT de>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet162;
49 
50 /*
51  * Adding an accessible listener to provide state information
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  */
56 import org.eclipse.swt.SWT;
57 import org.eclipse.swt.accessibility.ACC;
58 import org.eclipse.swt.accessibility.Accessible;
59 import org.eclipse.swt.accessibility.AccessibleAdapter;
60 import org.eclipse.swt.accessibility.AccessibleControlListener;
61 import org.eclipse.swt.accessibility.AccessibleControlAdapter;
62 import org.eclipse.swt.accessibility.AccessibleControlEvent;
63 import org.eclipse.swt.accessibility.AccessibleEvent;
64 import org.eclipse.swt.graphics.GC;
65 import org.eclipse.swt.graphics.Image;
66 import org.eclipse.swt.graphics.Point;
67 
68 import org.eclipse.swt.layout.FillLayout;
69 import org.eclipse.swt.widgets.Display;
70 import org.eclipse.swt.widgets.Shell;
71 import org.eclipse.swt.widgets.Table;
72 import org.eclipse.swt.widgets.TableColumn;
73 import org.eclipse.swt.widgets.TableItem;
74 
75 import java.lang.all;
76 
77 
78 const STATE = "CheckedIndices";
79 
80 void main () {
81     Display display = new Display ();
82     Image checkedImage = getCheckedImage (display);
83     Image uncheckedImage = getUncheckedImage (display);
84     Shell shell = new Shell (display);
85     shell.setLayout (new FillLayout ());
86     Table table = new Table (shell, SWT.BORDER);
87     TableColumn column1 = new TableColumn (table, SWT.NONE);
88     TableColumn column2 = new TableColumn (table, SWT.NONE);
89     TableColumn column3 = new TableColumn (table, SWT.NONE);
90     TableItem item1 = new TableItem (table, SWT.NONE);
91     item1.setText ( ["first item", "a", "b"]);
92     item1.setImage (1, uncheckedImage);
93     item1.setImage (2, uncheckedImage);
94     item1.setData (STATE, null);
95     TableItem item2 = new TableItem (table, SWT.NONE);
96     item2.setText ( ["second item", "c", "d"]);
97     item2.setImage (1, uncheckedImage);
98     item2.setImage (2, checkedImage);
99     item2.setData (STATE,  new ArrayWrapperInt([2]));
100     TableItem item3 = new TableItem (table, SWT.NONE);
101     item3.setText ( ["third", "e", "f"]);
102     item3.setImage (1, checkedImage);
103     item3.setImage (2, checkedImage);
104     item3.setData (STATE, new ArrayWrapperInt( [1, 2]));
105     column1.pack ();
106     column2.pack ();
107     column3.pack ();
108 
109     Accessible accessible = table.getAccessible ();
110     accessible.addAccessibleListener( new class AccessibleAdapter  {
111         override
112         public void getName (AccessibleEvent e) {
113             super.getName (e);
114             if (e.childID >= 0 && e.childID < table.getItemCount ()) {
115                 TableItem item = table.getItem (e.childID);
116                 Point pt = display.getCursorLocation ();
117                 pt = display.map (null, table, pt);
118                 for (int i = 0; i < table.getColumnCount (); i++) {
119                     if (item.getBounds (i).contains (pt)) {
120                         int [] data = (cast(ArrayWrapperInt)item.getData (STATE)).array;
121                         bool checked = false;
122                         if (data !is null) {
123                             for (int j = 0; j < data.length; j++) {
124                                 if (data [j] == i) {
125                                     checked = true;
126                                     break;
127                                 }
128                             }
129                         }
130                         e.result = item.getText (i) ~ " " ~ (checked ? "checked" : "unchecked");
131                         break;
132                     }
133                 }
134             }
135         }
136     });
137 
138     accessible.addAccessibleControlListener (new class AccessibleControlAdapter  {
139         override
140         public void getState (AccessibleControlEvent e) {
141             super.getState (e);
142             if (e.childID >= 0 && e.childID < table.getItemCount ()) {
143                 TableItem item = table.getItem (e.childID);
144                 int [] data =(cast(ArrayWrapperInt)item.getData (STATE)).array;
145                 if (data !is null) {
146                     Point pt = display.getCursorLocation ();
147                     pt = display.map (null, table, pt);
148                     for (int i = 0; i < data.length; i++) {
149                         if (item.getBounds (data [i]).contains (pt)) {
150                             e.detail |= ACC.STATE_CHECKED;
151                             break;
152                         }
153                     }
154                 }
155             }
156         }
157     });
158     shell.open ();
159     while (!shell.isDisposed ()) {
160         if (!display.readAndDispatch ()) display.sleep ();
161     }
162     checkedImage.dispose ();
163     uncheckedImage.dispose ();
164     display.dispose ();
165 }
166 
167 Image getCheckedImage (Display display) {
168     Image image = new Image (display, 16, 16);
169     GC gc = new GC (image);
170     gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW));
171     gc.fillOval (0, 0, 16, 16);
172     gc.setForeground (display.getSystemColor (SWT.COLOR_DARK_GREEN));
173     gc.drawLine (0, 0, 16, 16);
174     gc.drawLine (16, 0, 0, 16);
175     gc.dispose ();
176     return image;
177 }
178 
179 Image getUncheckedImage (Display display) {
180     Image image = new Image (display, 16, 16);
181     GC gc = new GC (image);
182     gc.setBackground (display.getSystemColor (SWT.COLOR_YELLOW));
183     gc.fillOval (0, 0, 16, 16);
184     gc.dispose ();
185     return image;
186 }