annotate snippets/tree/Snippet220.d @ 85:fa286c85e7b8

Fix Snippet111 and add jive to Snippet220
author Frank Benoit <benoit@tionex.de>
date Sat, 17 May 2008 23:26:35 +0200
parents 398aa64a7243
children 4095b64d16f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
1 /*******************************************************************************
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
3 * All rights reserved. This program and the accompanying materials
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
5 * which accompanies this distribution, and is available at
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
7 *
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
8 * Contributors:
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
9 * IBM Corporation - initial API and implementation
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
10 * D Port:
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
11 * Bill Baxter <wbaxter@gmail.com>
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
12 *******************************************************************************/
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
13 module dwtsnippets.tree.Snippet220;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
14
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
15 /*
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
16 * Tree example snippet: Images on the right side of the TreeItem
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
17 *
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
18 * For a detailed explanation of this snippet see
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
19 * http://www.eclipse.org/articles/Article-CustomDrawingTableAndTreeItems/customDraw.htm#_example5
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
20 *
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
21 * For a list of all SWT example snippets see
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
22 * http://www.eclipse.org/swt/snippets/
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
23 *
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
24 * @since 3.2
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
25 */
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
26
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
27 import dwt.DWT;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
28 import dwt.graphics.Image;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
29 import dwt.graphics.GC;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
30 import dwt.widgets.Display;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
31 import dwt.widgets.Shell;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
32 import dwt.widgets.Listener;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
33 import dwt.widgets.Event;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
34 import dwt.widgets.Tree;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
35 import dwt.widgets.TreeItem;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
36
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
37 import tango.util.Convert;
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
38
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
39 import tango.io.Stdout;
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
40 version(JIVE){
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
41 import jive.stacktrace;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
42 }
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
43
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
44 void main() {
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
45 Display display = new Display();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
46 Shell shell = new Shell(display);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
47 shell.setBounds(10, 10, 350, 200);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
48 Image xImage = new Image (display, 16, 16);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
49 GC gc = new GC(xImage);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
50 gc.setForeground(display.getSystemColor(DWT.COLOR_RED));
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
51 gc.drawLine(1, 1, 14, 14);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
52 gc.drawLine(1, 14, 14, 1);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
53 gc.drawOval(2, 2, 11, 11);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
54 gc.dispose();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
55 const int IMAGE_MARGIN = 2;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
56 final Tree tree = new Tree(shell, DWT.CHECK);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
57 tree.setBounds(10, 10, 300, 150);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
58 TreeItem item = new TreeItem(tree, DWT.NONE);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
59 item.setText("root item");
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
60 for (int i = 0; i < 4; i++) {
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
61 TreeItem newItem = new TreeItem(item, DWT.NONE);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
62 newItem.setText("descendent " ~ to!(char[])(i));
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
63 if (i % 2 == 0) newItem.setData(xImage);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
64 item.setExpanded(true);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
65 item = newItem;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
66 }
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
67
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
68 /*
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
69 * NOTE: MeasureItem and PaintItem are called repeatedly. Therefore it is
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
70 * critical for performance that these methods be as efficient as possible.
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
71 */
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
72 tree.addListener(DWT.MeasureItem, new class Listener {
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
73 public void handleEvent(Event event) {
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
74 Stdout("measure").newline;
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
75 TreeItem item = cast(TreeItem)event.item;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
76 Image trailingImage = cast(Image)item.getData();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
77 if (trailingImage !is null) {
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
78 Stdout(" - measure image").newline;
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
79 event.width += trailingImage.getBounds().width + IMAGE_MARGIN;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
80 }
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
81 }
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
82 });
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
83 tree.addListener(DWT.PaintItem, new class Listener {
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
84 public void handleEvent(Event event) {
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
85 Stdout("paint").newline;
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
86 TreeItem item = cast(TreeItem)event.item;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
87 Image trailingImage = cast(Image)item.getData();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
88 if (trailingImage !is null) {
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
89 Stdout(" - draw image").newline;
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
90 int x = event.x + event.width + IMAGE_MARGIN;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
91 int itemHeight = tree.getItemHeight();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
92 int imageHeight = trailingImage.getBounds().height;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
93 int y = event.y + (itemHeight - imageHeight) / 2;
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
94 event.gc.drawImage(trailingImage, x, y);
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
95 }
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
96 }
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
97 });
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
98
85
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
99 shell.open();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
100 while (!shell.isDisposed()) {
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
101 if (!display.readAndDispatch()) display.sleep();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
102 }
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
103 xImage.dispose();
fa286c85e7b8 Fix Snippet111 and add jive to Snippet220
Frank Benoit <benoit@tionex.de>
parents: 84
diff changeset
104 display.dispose();
84
398aa64a7243 Two new snippets -- don't quite work yet though.
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
105 }