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.Snippet220;
|
|
14
|
|
15 /*
|
|
16 * Tree example snippet: Images on the right side of the TreeItem
|
|
17 *
|
|
18 * For a detailed explanation of this snippet see
|
|
19 * http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_example5
|
|
20 *
|
|
21 * For a list of all SWT example snippets see
|
|
22 * http://www.eclipse.org/swt/snippets/
|
|
23 *
|
|
24 * @since 3.2
|
|
25 */
|
|
26
|
|
27 import org.eclipse.swt.SWT;
|
|
28 import org.eclipse.swt.graphics.Image;
|
|
29 import org.eclipse.swt.graphics.GC;
|
|
30 import org.eclipse.swt.widgets.Display;
|
|
31 import org.eclipse.swt.widgets.Shell;
|
|
32 import org.eclipse.swt.widgets.Listener;
|
|
33 import org.eclipse.swt.widgets.Event;
|
|
34 import org.eclipse.swt.widgets.Tree;
|
|
35 import org.eclipse.swt.widgets.TreeItem;
|
112
|
36 import java.lang.all;
|
28
|
37
|
112
|
38 version(Tango){
|
|
39 import tango.util.Convert;
|
|
40 } else { // Phobos
|
|
41 import std.conv;
|
|
42 }
|
28
|
43
|
|
44 version(JIVE){
|
|
45 import jive.stacktrace;
|
|
46 }
|
|
47
|
|
48 void main() {
|
|
49 const int IMAGE_MARGIN = 2;
|
|
50 void handleEventMeasureItem(Event event) {
|
|
51 TreeItem item = cast(TreeItem)event.item;
|
|
52 Image trailingImage = cast(Image)item.getData();
|
|
53 if (trailingImage !is null) {
|
|
54 int w1 = event.width;
|
|
55 event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
|
|
56 int w2 = event.width;
|
|
57 }
|
|
58 }
|
|
59 void handleEventPaintItem(Event event, Tree tree ) {
|
|
60 TreeItem item = cast(TreeItem)event.item;
|
|
61 Image trailingImage = cast(Image)item.getData();
|
|
62 if (trailingImage !is null) {
|
|
63 int x = event.x + event.width + IMAGE_MARGIN;
|
|
64 int itemHeight = tree.getItemHeight();
|
|
65 int imageHeight = trailingImage.getBounds().height;
|
|
66 int y = event.y + (itemHeight - imageHeight) / 2;
|
|
67 event.gc.drawImage(trailingImage, x, y);
|
|
68 }
|
|
69 }
|
|
70 Display display = new Display();
|
|
71 Shell shell = new Shell(display);
|
|
72 shell.setBounds(10, 10, 350, 200);
|
|
73 Image xImage = new Image (display, 16, 16);
|
|
74 GC gc = new GC(xImage);
|
|
75 gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
|
|
76 gc.drawLine(1, 1, 14, 14);
|
|
77 gc.drawLine(1, 14, 14, 1);
|
|
78 gc.drawOval(2, 2, 11, 11);
|
|
79 gc.dispose();
|
|
80 Tree tree = new Tree(shell, SWT.CHECK);
|
|
81 tree.setBounds(10, 10, 300, 150);
|
|
82 TreeItem item = new TreeItem(tree, SWT.NONE);
|
|
83 item.setText("root item");
|
|
84 for (int i = 0; i < 4; i++) {
|
|
85 TreeItem newItem = new TreeItem(item, SWT.NONE);
|
112
|
86 newItem.setText("descendent " ~ to!(String)(i));
|
28
|
87 if (i % 2 == 0) newItem.setData(xImage);
|
|
88 item.setExpanded(true);
|
|
89 item = newItem;
|
|
90 }
|
|
91
|
|
92 /*
|
|
93 * NOTE: MeasureItem and PaintItem are called repeatedly. Therefore it is
|
|
94 * critical for performance that these methods be as efficient as possible.
|
|
95 */
|
|
96 tree.addListener(SWT.MeasureItem, dgListener( &handleEventMeasureItem ));
|
|
97 tree.addListener(SWT.PaintItem,dgListener( &handleEventPaintItem, tree ));
|
|
98
|
|
99 shell.open();
|
|
100 while (!shell.isDisposed()) {
|
|
101 if (!display.readAndDispatch()) display.sleep();
|
|
102 }
|
|
103 xImage.dispose();
|
|
104 display.dispose();
|
|
105 }
|