1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet220"
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, 2006 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  *     Bill Baxter <wbaxter@gmail.com>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet220;
49 
50 /*
51  * Tree example snippet: Images on the right side of the TreeItem
52  *
53  * For a detailed explanation of this snippet see
54  * http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_example5
55  *
56  * For a list of all SWT example snippets see
57  * http://www.eclipse.org/swt/snippets/
58  *
59  * @since 3.2
60  */
61 
62 import org.eclipse.swt.SWT;
63 import org.eclipse.swt.graphics.Image;
64 import org.eclipse.swt.graphics.GC;
65 import org.eclipse.swt.widgets.Display;
66 import org.eclipse.swt.widgets.Shell;
67 import org.eclipse.swt.widgets.Listener;
68 import org.eclipse.swt.widgets.Event;
69 import org.eclipse.swt.widgets.Tree;
70 import org.eclipse.swt.widgets.TreeItem;
71 import java.lang.all;
72 
73 version(Tango){
74     import tango.util.Convert;
75 } else { // Phobos
76     import std.conv;
77 }
78 
79 version(JIVE){
80     import jive.stacktrace;
81 }
82 
83 void main() {
84     const int IMAGE_MARGIN = 2;
85     void handleEventMeasureItem(Event event) {
86         TreeItem item = cast(TreeItem)event.item;
87         Image trailingImage = cast(Image)item.getData();
88         if (trailingImage !is null) {
89             int w1 = event.width;
90             event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
91             int w2 = event.width;
92         }
93     }
94     void handleEventPaintItem(Event event, Tree tree ) {
95         TreeItem item = cast(TreeItem)event.item;
96         Image trailingImage = cast(Image)item.getData();
97         if (trailingImage !is null) {
98             int x = event.x + event.width + IMAGE_MARGIN;
99             int itemHeight = tree.getItemHeight();
100             int imageHeight = trailingImage.getBounds().height;
101             int y = event.y + (itemHeight - imageHeight) / 2;
102             event.gc.drawImage(trailingImage, x, y);
103         }
104     }
105     Display display = new Display();
106     Shell shell = new Shell(display);
107     shell.setBounds(10, 10, 350, 200);
108     Image xImage = new Image (display, 16, 16);
109     GC gc = new GC(xImage);
110     gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
111     gc.drawLine(1, 1, 14, 14);
112     gc.drawLine(1, 14, 14, 1);
113     gc.drawOval(2, 2, 11, 11);
114     gc.dispose();
115     Tree tree = new Tree(shell, SWT.CHECK);
116     tree.setBounds(10, 10, 300, 150);
117     TreeItem item = new TreeItem(tree, SWT.NONE);
118     item.setText("root item");
119     for (int i = 0; i < 4; i++) {
120         TreeItem newItem = new TreeItem(item, SWT.NONE);
121         newItem.setText("descendent " ~ to!(String)(i));
122         if (i % 2 == 0) newItem.setData(xImage);
123         item.setExpanded(true);
124         item = newItem;
125     }
126 
127     /*
128      * NOTE: MeasureItem and PaintItem are called repeatedly.  Therefore it is
129      * critical for performance that these methods be as efficient as possible.
130      */
131     tree.addListener(SWT.MeasureItem, dgListener( &handleEventMeasureItem ));
132     tree.addListener(SWT.PaintItem,dgListener( &handleEventPaintItem, tree ));
133 
134     shell.open();
135     while (!shell.isDisposed()) {
136         if (!display.readAndDispatch()) display.sleep();
137     }
138     xImage.dispose();
139     display.dispose();
140 }