1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet226"
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.Snippet226;
49 
50 /* 
51  * Tree example snippet: Draw a custom gradient selection
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  * 
56  * @since 3.3
57  */
58 import org.eclipse.swt.SWT;
59 import org.eclipse.swt.graphics.GC;
60 import org.eclipse.swt.graphics.Rectangle;
61 import org.eclipse.swt.graphics.Region;
62 import org.eclipse.swt.graphics.Color;
63 import org.eclipse.swt.layout.FillLayout;
64 import org.eclipse.swt.widgets.Display;
65 import org.eclipse.swt.widgets.Shell;
66 import org.eclipse.swt.widgets.Tree;
67 import org.eclipse.swt.widgets.TreeItem;
68 import org.eclipse.swt.widgets.TreeColumn;
69 import org.eclipse.swt.widgets.Listener;
70 import org.eclipse.swt.widgets.Event;
71 
72 version(Tango){
73     import tango.util.Convert;
74 } else { // Phobos
75     import std.conv;
76 }
77 
78 import java.lang.all;
79 
80 void main() 
81 {
82 	Display display = new Display();
83 	Shell shell = new Shell(display);
84 	shell.setText("Custom gradient selection for Tree");
85 	shell.setLayout(new FillLayout());
86 	Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
87 	tree.setHeaderVisible(true);
88 	tree.setLinesVisible(true);
89 	int columnCount = 4;
90 	for (int i=0; i<columnCount; i++) {
91         auto istr = to!(String)(i);
92 		TreeColumn column = new TreeColumn(tree, SWT.NONE);
93 		column.setText("Column " ~ istr);	
94 	}
95 	int itemCount = 3;
96 	for (int i=0; i<itemCount; i++) {
97         auto istr = to!(String)(i);
98 		TreeItem item1 = new TreeItem(tree, SWT.NONE);
99 		item1.setText("item "~istr);
100 		for (int c=1; c < columnCount; c++) {
101             auto cstr = to!(String)(c);
102 			item1.setText(c, "item ["~istr~"-"~cstr~"]");
103 		}
104 		for (int j=0; j<itemCount; j++) {
105             auto jstr = to!(String)(j);
106 			TreeItem item2 = new TreeItem(item1, SWT.NONE);
107 			item2.setText("item ["~istr~" "~jstr~"]");
108 			for (int c=1; c<columnCount; c++) {
109                 auto cstr = to!(String)(c);
110 				item2.setText(c, "item ["~istr~" "~jstr~"-"~cstr~"]");
111 			}
112 			for (int k=0; k<itemCount; k++) {
113                 auto kstr = to!(String)(k);
114 				TreeItem item3 = new TreeItem(item2, SWT.NONE);
115 				item3.setText("item ["~istr~" "~jstr~" "~kstr~"]");
116 				for (int c=1; c<columnCount; c++) {
117                     auto cstr = to!(String)(c);
118 					item3.setText(c, "item ["~istr~" "~jstr~" "~kstr~"-"~cstr~"]");
119 				}
120 			}
121 		}
122 	}
123 
124 	/*
125 	 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
126 	 * Therefore, it is critical for performance that these methods be
127 	 * as efficient as possible.
128 	 */
129 	tree.addListener(SWT.EraseItem, new class Listener {
130 		public void handleEvent(Event event) {
131 			event.detail &= ~SWT.HOT;
132 			if ((event.detail & SWT.SELECTED) != 0) {
133 				GC gc = event.gc;
134 				Rectangle area = tree.getClientArea();
135 				/*
136 				 * If you wish to paint the selection beyond the end of
137 				 * last column, you must change the clipping region.
138 				 */
139 				int columnCount = tree.getColumnCount();
140 				if (event.index == columnCount - 1 || columnCount == 0) {
141 					int width = area.x + area.width - event.x;
142 					if (width > 0) {
143 						Region region = new Region();
144 						gc.getClipping(region);
145 						region.add(event.x, event.y, width, event.height); 
146 						gc.setClipping(region);
147 						region.dispose();
148 					}
149 				}
150 				gc.setAdvanced(true);
151 				if (gc.getAdvanced()) gc.setAlpha(127);								
152 				Rectangle rect = event.getBounds();
153 				Color foreground = gc.getForeground();
154 				Color background = gc.getBackground();
155 				gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
156 				gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
157 				gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
158 				// restore colors for subsequent drawing
159 				gc.setForeground(foreground);
160 				gc.setBackground(background);
161 				event.detail &= ~SWT.SELECTED;					
162 			}						
163 		}
164 	});		
165 	for (int i=0; i<columnCount; i++) {
166 		tree.getColumn(i).pack();
167 	}	
168 	tree.setSelection(tree.getItem(0));
169 	shell.setSize(500, 200);
170 	shell.open();
171 	while (!shell.isDisposed()) {
172 		if (!display.readAndDispatch()) display.sleep();
173 	}
174 	display.dispose();	
175 }