comparison org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet220.d @ 28:69b1fa94a4a8

Added SWT snippets
author Frank Benoit <benoit@tionex.de>
date Sun, 22 Mar 2009 15:17:04 +0100
parents
children 9f4c18c268b2
comparison
equal deleted inserted replaced
27:1bf55a6eb092 28:69b1fa94a4a8
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;
36
37 import tango.util.Convert;
38
39 version(JIVE){
40 import jive.stacktrace;
41 }
42
43 void main() {
44 const int IMAGE_MARGIN = 2;
45 void handleEventMeasureItem(Event event) {
46 TreeItem item = cast(TreeItem)event.item;
47 Image trailingImage = cast(Image)item.getData();
48 if (trailingImage !is null) {
49 int w1 = event.width;
50 event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
51 int w2 = event.width;
52 }
53 }
54 void handleEventPaintItem(Event event, Tree tree ) {
55 TreeItem item = cast(TreeItem)event.item;
56 Image trailingImage = cast(Image)item.getData();
57 if (trailingImage !is null) {
58 int x = event.x + event.width + IMAGE_MARGIN;
59 int itemHeight = tree.getItemHeight();
60 int imageHeight = trailingImage.getBounds().height;
61 int y = event.y + (itemHeight - imageHeight) / 2;
62 event.gc.drawImage(trailingImage, x, y);
63 }
64 }
65 Display display = new Display();
66 Shell shell = new Shell(display);
67 shell.setBounds(10, 10, 350, 200);
68 Image xImage = new Image (display, 16, 16);
69 GC gc = new GC(xImage);
70 gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
71 gc.drawLine(1, 1, 14, 14);
72 gc.drawLine(1, 14, 14, 1);
73 gc.drawOval(2, 2, 11, 11);
74 gc.dispose();
75 Tree tree = new Tree(shell, SWT.CHECK);
76 tree.setBounds(10, 10, 300, 150);
77 TreeItem item = new TreeItem(tree, SWT.NONE);
78 item.setText("root item");
79 for (int i = 0; i < 4; i++) {
80 TreeItem newItem = new TreeItem(item, SWT.NONE);
81 newItem.setText("descendent " ~ to!(char[])(i));
82 if (i % 2 == 0) newItem.setData(xImage);
83 item.setExpanded(true);
84 item = newItem;
85 }
86
87 /*
88 * NOTE: MeasureItem and PaintItem are called repeatedly. Therefore it is
89 * critical for performance that these methods be as efficient as possible.
90 */
91 tree.addListener(SWT.MeasureItem, dgListener( &handleEventMeasureItem ));
92 tree.addListener(SWT.PaintItem,dgListener( &handleEventPaintItem, tree ));
93
94 shell.open();
95 while (!shell.isDisposed()) {
96 if (!display.readAndDispatch()) display.sleep();
97 }
98 xImage.dispose();
99 display.dispose();
100 }