1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet3"
5     dependency "dwt" path="../../../../../../" version="*"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "gio-2.0" \
14       "glib-2.0" \
15       "gmodule-2.0" \
16       "gobject-2.0" \
17       "gthread-2.0" \
18       "gtk-x11-2.0" \
19       "pango-1.0" \
20       "pangocairo-1.0" \
21       "X11" \
22       "Xcomposite" \
23       "Xcursor" \
24       "Xdamage" \
25       "Xext" \
26       "Xfixes" \
27       "Xi" \
28       "Xinerama" \
29       "Xrandr" \
30       "Xrender" \
31       "Xtst" \
32       platform="linux"
33 +/
34 /*******************************************************************************
35  * Copyright (c) 2000, 2016 IBM Corporation and others.
36  * All rights reserved. This program and the accompanying materials
37  * are made available under the terms of the Eclipse Public License v1.0
38  * which accompanies this distribution, and is available at
39  * http://www.eclipse.org/legal/epl-v10.html
40  *
41  * Contributors:
42  *     IBM Corporation - initial API and implementation
43  * D Port:
44  *     alice <stigma@disroot.org>
45  *******************************************************************************/
46 module org.eclipse.swt.snippets.Snippet3;
47 
48 import std.conv : to;
49 import std.stdio : writeln;
50 
51 /*
52  * Table example snippet: find a table cell from mouse down (SWT.FULL_SELECTION)
53  *
54  * For a list of all SWT example snippets see
55  * http://www.eclipse.org/swt/snippets/
56  */
57 import org.eclipse.swt.all;
58 import org.eclipse.swt.graphics.all;
59 import org.eclipse.swt.widgets.all;
60 
61 void main(string[] args) {
62     Display display = new Display();
63     Shell shell = new Shell(display);
64     Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL |
65             SWT.FULL_SELECTION);
66     table.setHeaderVisible(true);
67     table.setLinesVisible(true);
68     const int rowCount = 64, columnCount = 4;
69     for (int i = 0; i < columnCount; i++) {
70         TableColumn column = new TableColumn(table, SWT.NONE);
71         column.setText("Column " ~ to!string(i));
72     }
73     for (int i = 0; i < rowCount; i++) {
74         TableItem item = new TableItem(table, SWT.NONE);
75         for (int j = 0; j < columnCount; j++) {
76             item.setText(j, "Item " ~ to!string(i) ~ "-" ~ to!string(j));
77         }
78     }
79     for (int i = 0; i < columnCount; i++) {
80         table.getColumn(i).pack();
81     }
82     Rectangle clientArea = shell.getClientArea();
83     table.setLocation(clientArea.x, clientArea.y);
84     Point size = table.computeSize(SWT.DEFAULT, 200);
85     table.setSize(size);
86     shell.pack();
87     table.addListener(SWT.MouseDown, new class Listener {
88         override void handleEvent(Event event) {
89             Point pt = new Point(event.x, event.y);
90             TableItem item = table.getItem(pt);
91             if (item is null)
92                 return;
93             for (int i = 0; i < columnCount; i++) {
94                 Rectangle rect = item.getBounds(i);
95                 if (rect.contains(pt)) {
96                     int index = table.indexOf(item);
97                     writeln("Item ", index, "-", i);
98                 }
99             }
100         }
101     });
102     shell.open();
103     while (!shell.isDisposed()) {
104         if (!display.readAndDispatch()) display.sleep();
105     }
106     display.dispose();
107 }