Mercurial > projects > dwt-samples
comparison snippets/tree/Snippet220.d @ 86:4095b64d16f5
Fixed in dwt-win, so removed the debugging outputs.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 18 May 2008 20:58:10 +0200 |
parents | fa286c85e7b8 |
children | 1f0a7a472680 |
comparison
equal
deleted
inserted
replaced
85:fa286c85e7b8 | 86:4095b64d16f5 |
---|---|
10 * D Port: | 10 * D Port: |
11 * Bill Baxter <wbaxter@gmail.com> | 11 * Bill Baxter <wbaxter@gmail.com> |
12 *******************************************************************************/ | 12 *******************************************************************************/ |
13 module dwtsnippets.tree.Snippet220; | 13 module dwtsnippets.tree.Snippet220; |
14 | 14 |
15 /* | 15 /* |
16 * Tree example snippet: Images on the right side of the TreeItem | 16 * Tree example snippet: Images on the right side of the TreeItem |
17 * | 17 * |
18 * For a detailed explanation of this snippet see | 18 * For a detailed explanation of this snippet see |
19 * http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_example5 | 19 * http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_example5 |
20 * | 20 * |
21 * For a list of all SWT example snippets see | 21 * For a list of all SWT example snippets see |
22 * http://www.eclipse.org/swt/snippets/ | 22 * http://www.eclipse.org/swt/snippets/ |
23 * | 23 * |
24 * @since 3.2 | 24 * @since 3.2 |
25 */ | 25 */ |
26 | 26 |
27 import dwt.DWT; | 27 import dwt.DWT; |
28 import dwt.graphics.Image; | 28 import dwt.graphics.Image; |
34 import dwt.widgets.Tree; | 34 import dwt.widgets.Tree; |
35 import dwt.widgets.TreeItem; | 35 import dwt.widgets.TreeItem; |
36 | 36 |
37 import tango.util.Convert; | 37 import tango.util.Convert; |
38 | 38 |
39 import tango.io.Stdout; | |
40 version(JIVE){ | 39 version(JIVE){ |
41 import jive.stacktrace; | 40 import jive.stacktrace; |
42 } | 41 } |
43 | 42 |
44 void main() { | 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 } | |
45 Display display = new Display(); | 65 Display display = new Display(); |
46 Shell shell = new Shell(display); | 66 Shell shell = new Shell(display); |
47 shell.setBounds(10, 10, 350, 200); | 67 shell.setBounds(10, 10, 350, 200); |
48 Image xImage = new Image (display, 16, 16); | 68 Image xImage = new Image (display, 16, 16); |
49 GC gc = new GC(xImage); | 69 GC gc = new GC(xImage); |
50 gc.setForeground(display.getSystemColor(DWT.COLOR_RED)); | 70 gc.setForeground(display.getSystemColor(DWT.COLOR_RED)); |
51 gc.drawLine(1, 1, 14, 14); | 71 gc.drawLine(1, 1, 14, 14); |
52 gc.drawLine(1, 14, 14, 1); | 72 gc.drawLine(1, 14, 14, 1); |
53 gc.drawOval(2, 2, 11, 11); | 73 gc.drawOval(2, 2, 11, 11); |
54 gc.dispose(); | 74 gc.dispose(); |
55 const int IMAGE_MARGIN = 2; | 75 Tree tree = new Tree(shell, DWT.CHECK); |
56 final Tree tree = new Tree(shell, DWT.CHECK); | |
57 tree.setBounds(10, 10, 300, 150); | 76 tree.setBounds(10, 10, 300, 150); |
58 TreeItem item = new TreeItem(tree, DWT.NONE); | 77 TreeItem item = new TreeItem(tree, DWT.NONE); |
59 item.setText("root item"); | 78 item.setText("root item"); |
60 for (int i = 0; i < 4; i++) { | 79 for (int i = 0; i < 4; i++) { |
61 TreeItem newItem = new TreeItem(item, DWT.NONE); | 80 TreeItem newItem = new TreeItem(item, DWT.NONE); |
67 | 86 |
68 /* | 87 /* |
69 * NOTE: MeasureItem and PaintItem are called repeatedly. Therefore it is | 88 * NOTE: MeasureItem and PaintItem are called repeatedly. Therefore it is |
70 * critical for performance that these methods be as efficient as possible. | 89 * critical for performance that these methods be as efficient as possible. |
71 */ | 90 */ |
72 tree.addListener(DWT.MeasureItem, new class Listener { | 91 tree.addListener(DWT.MeasureItem, dgListener( &handleEventMeasureItem )); |
73 public void handleEvent(Event event) { | 92 tree.addListener(DWT.PaintItem,dgListener( &handleEventPaintItem, tree )); |
74 Stdout("measure").newline; | |
75 TreeItem item = cast(TreeItem)event.item; | |
76 Image trailingImage = cast(Image)item.getData(); | |
77 if (trailingImage !is null) { | |
78 Stdout(" - measure image").newline; | |
79 event.width += trailingImage.getBounds().width + IMAGE_MARGIN; | |
80 } | |
81 } | |
82 }); | |
83 tree.addListener(DWT.PaintItem, new class Listener { | |
84 public void handleEvent(Event event) { | |
85 Stdout("paint").newline; | |
86 TreeItem item = cast(TreeItem)event.item; | |
87 Image trailingImage = cast(Image)item.getData(); | |
88 if (trailingImage !is null) { | |
89 Stdout(" - draw image").newline; | |
90 int x = event.x + event.width + IMAGE_MARGIN; | |
91 int itemHeight = tree.getItemHeight(); | |
92 int imageHeight = trailingImage.getBounds().height; | |
93 int y = event.y + (itemHeight - imageHeight) / 2; | |
94 event.gc.drawImage(trailingImage, x, y); | |
95 } | |
96 } | |
97 }); | |
98 | 93 |
99 shell.open(); | 94 shell.open(); |
100 while (!shell.isDisposed()) { | 95 while (!shell.isDisposed()) { |
101 if (!display.readAndDispatch()) display.sleep(); | 96 if (!display.readAndDispatch()) display.sleep(); |
102 } | 97 } |