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