28
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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 * Bill Baxter <wbaxter@gmail.com>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.snippets.Snippet226;
|
|
14
|
|
15 /*
|
|
16 * Tree example snippet: Draw a custom gradient selection
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 *
|
|
21 * @since 3.3
|
|
22 */
|
|
23 import org.eclipse.swt.SWT;
|
|
24 import org.eclipse.swt.graphics.GC;
|
|
25 import org.eclipse.swt.graphics.Rectangle;
|
|
26 import org.eclipse.swt.graphics.Region;
|
|
27 import org.eclipse.swt.graphics.Color;
|
|
28 import org.eclipse.swt.layout.FillLayout;
|
|
29 import org.eclipse.swt.widgets.Display;
|
|
30 import org.eclipse.swt.widgets.Shell;
|
|
31 import org.eclipse.swt.widgets.Tree;
|
|
32 import org.eclipse.swt.widgets.TreeItem;
|
|
33 import org.eclipse.swt.widgets.TreeColumn;
|
|
34 import org.eclipse.swt.widgets.Listener;
|
|
35 import org.eclipse.swt.widgets.Event;
|
|
36
|
112
|
37 version(Tango){
|
|
38 import tango.util.Convert;
|
|
39 } else { // Phobos
|
|
40 import std.conv;
|
|
41 }
|
28
|
42
|
|
43 import java.lang.all;
|
|
44
|
|
45 void main()
|
|
46 {
|
112
|
47 Display display = new Display();
|
28
|
48 Shell shell = new Shell(display);
|
|
49 shell.setText("Custom gradient selection for Tree");
|
|
50 shell.setLayout(new FillLayout());
|
112
|
51 Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION);
|
28
|
52 tree.setHeaderVisible(true);
|
|
53 tree.setLinesVisible(true);
|
|
54 int columnCount = 4;
|
|
55 for (int i=0; i<columnCount; i++) {
|
|
56 auto istr = to!(String)(i);
|
|
57 TreeColumn column = new TreeColumn(tree, SWT.NONE);
|
|
58 column.setText("Column " ~ istr);
|
|
59 }
|
|
60 int itemCount = 3;
|
|
61 for (int i=0; i<itemCount; i++) {
|
|
62 auto istr = to!(String)(i);
|
|
63 TreeItem item1 = new TreeItem(tree, SWT.NONE);
|
|
64 item1.setText("item "~istr);
|
|
65 for (int c=1; c < columnCount; c++) {
|
|
66 auto cstr = to!(String)(c);
|
|
67 item1.setText(c, "item ["~istr~"-"~cstr~"]");
|
|
68 }
|
|
69 for (int j=0; j<itemCount; j++) {
|
|
70 auto jstr = to!(String)(j);
|
|
71 TreeItem item2 = new TreeItem(item1, SWT.NONE);
|
|
72 item2.setText("item ["~istr~" "~jstr~"]");
|
|
73 for (int c=1; c<columnCount; c++) {
|
|
74 auto cstr = to!(String)(c);
|
|
75 item2.setText(c, "item ["~istr~" "~jstr~"-"~cstr~"]");
|
|
76 }
|
|
77 for (int k=0; k<itemCount; k++) {
|
|
78 auto kstr = to!(String)(k);
|
|
79 TreeItem item3 = new TreeItem(item2, SWT.NONE);
|
|
80 item3.setText("item ["~istr~" "~jstr~" "~kstr~"]");
|
|
81 for (int c=1; c<columnCount; c++) {
|
|
82 auto cstr = to!(String)(c);
|
|
83 item3.setText(c, "item ["~istr~" "~jstr~" "~kstr~"-"~cstr~"]");
|
|
84 }
|
|
85 }
|
|
86 }
|
|
87 }
|
|
88
|
|
89 /*
|
|
90 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
|
|
91 * Therefore, it is critical for performance that these methods be
|
|
92 * as efficient as possible.
|
|
93 */
|
|
94 tree.addListener(SWT.EraseItem, new class Listener {
|
|
95 public void handleEvent(Event event) {
|
|
96 event.detail &= ~SWT.HOT;
|
|
97 if ((event.detail & SWT.SELECTED) != 0) {
|
|
98 GC gc = event.gc;
|
|
99 Rectangle area = tree.getClientArea();
|
|
100 /*
|
|
101 * If you wish to paint the selection beyond the end of
|
|
102 * last column, you must change the clipping region.
|
|
103 */
|
|
104 int columnCount = tree.getColumnCount();
|
|
105 if (event.index == columnCount - 1 || columnCount == 0) {
|
|
106 int width = area.x + area.width - event.x;
|
|
107 if (width > 0) {
|
|
108 Region region = new Region();
|
|
109 gc.getClipping(region);
|
|
110 region.add(event.x, event.y, width, event.height);
|
|
111 gc.setClipping(region);
|
|
112 region.dispose();
|
|
113 }
|
|
114 }
|
|
115 gc.setAdvanced(true);
|
|
116 if (gc.getAdvanced()) gc.setAlpha(127);
|
|
117 Rectangle rect = event.getBounds();
|
|
118 Color foreground = gc.getForeground();
|
|
119 Color background = gc.getBackground();
|
|
120 gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
|
|
121 gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
|
|
122 gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
|
|
123 // restore colors for subsequent drawing
|
|
124 gc.setForeground(foreground);
|
|
125 gc.setBackground(background);
|
|
126 event.detail &= ~SWT.SELECTED;
|
|
127 }
|
|
128 }
|
|
129 });
|
|
130 for (int i=0; i<columnCount; i++) {
|
|
131 tree.getColumn(i).pack();
|
|
132 }
|
|
133 tree.setSelection(tree.getItem(0));
|
|
134 shell.setSize(500, 200);
|
|
135 shell.open();
|
|
136 while (!shell.isDisposed()) {
|
|
137 if (!display.readAndDispatch()) display.sleep();
|
|
138 }
|
|
139 display.dispose();
|
|
140 }
|