comparison snippets/treeeditor/Snippet111.d @ 84:398aa64a7243

Two new snippets -- don't quite work yet though.
author Bill Baxter <bill@billbaxter.com>
date Sat, 17 May 2008 13:06:43 +0900
parents
children fa286c85e7b8
comparison
equal deleted inserted replaced
83:b7e2d67d5efa 84:398aa64a7243
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 dwtsnippets.treeeditor.Snippet111;
14
15 /*
16 * TreeEditor example snippet: edit the text of a tree item (in place, fancy)
17 *
18 * For a list of all SWT example snippets see
19 * http://www.eclipse.org/swt/snippets/
20 */
21 import dwt.DWT;
22 import dwt.graphics.Color;
23 import dwt.graphics.Rectangle;
24 import dwt.graphics.GC;
25 import dwt.widgets.Display;
26 import dwt.widgets.Shell;
27 import dwt.widgets.Widget;
28 import dwt.widgets.Composite;
29 import dwt.widgets.Tree;
30 import dwt.widgets.TreeItem;
31 import dwt.widgets.Text;
32 import dwt.widgets.Listener;
33 import dwt.widgets.Event;
34 import dwt.layout.FillLayout;
35 import dwt.custom.TreeEditor;
36
37 import dwt.dwthelper.utils : String, substring, Math;
38 import std.stdio;
39
40 import tango.util.Convert;
41
42 void main () {
43 final Display display = new Display ();
44 final Color black = display.getSystemColor (DWT.COLOR_BLACK);
45 Shell shell = new Shell (display);
46 shell.setLayout (new FillLayout ());
47 final Tree tree = new Tree (shell, DWT.BORDER);
48 for (int i=0; i<16; i++) {
49 TreeItem itemI = new TreeItem (tree, DWT.NONE);
50 itemI.setText ("Item " ~ to!(char[])(i));
51 for (int j=0; j<16; j++) {
52 TreeItem itemJ = new TreeItem (itemI, DWT.NONE);
53 itemJ.setText ("Item " ~ to!(char[])(j) );
54 }
55 }
56 final TreeItem [] lastItem = new TreeItem [1];
57 final TreeEditor editor = new TreeEditor (tree);
58 tree.addListener (DWT.Selection, new class Listener {
59 public void handleEvent (Event event) {
60 final TreeItem item = cast(TreeItem) event.item;
61 if (item !is null && item is lastItem [0]) {
62 bool showBorder = true;
63 final Composite composite = new Composite (tree, DWT.NONE);
64 if (showBorder) composite.setBackground (black);
65 final Text text = new Text (composite, DWT.NONE);
66 final int inset = showBorder ? 1 : 0;
67 composite.addListener (DWT.Resize, new class Listener {
68 public void handleEvent (Event e) {
69 Rectangle rect = composite.getClientArea ();
70 text.setBounds (rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2);
71 }
72 });
73 Listener textListener = new class Listener {
74 public void handleEvent (Event e) {
75 switch (e.type) {
76 case DWT.FocusOut: {
77 item.setText (text.getText ());
78 composite.dispose ();
79 }
80 break;
81 case DWT.Verify: {
82 String newText = text.getText ();
83 String leftText = newText.substring (0, e.start);
84 String rightText = newText.substring (e.end, newText.length);
85 GC gc = new GC (text);
86 Point size = gc.textExtent (leftText ~ e.text ~ rightText);
87 gc.dispose ();
88 size = text.computeSize (size.x, DWT.DEFAULT);
89 editor.horizontalAlignment = DWT.LEFT;
90 Rectangle itemRect = item.getBounds (), rect = tree.getClientArea ();
91 editor.minimumWidth = Math.max (size.x, itemRect.width) + inset * 2;
92 int left = itemRect.x, right = rect.x + rect.width;
93 editor.minimumWidth = Math.min (editor.minimumWidth, right - left);
94 editor.minimumHeight = size.y + inset * 2;
95 editor.layout ();
96 }
97 break;
98 case DWT.Traverse: {
99 switch (e.detail) {
100 case DWT.TRAVERSE_RETURN:
101 item.setText (text.getText ());
102 //FALL THROUGH
103 case DWT.TRAVERSE_ESCAPE:
104 composite.dispose ();
105 e.doit = false;
106 default:
107 //no-op
108 }
109 break;
110 }
111 default:
112 // no-op
113 }
114 }
115 };
116 text.addListener (DWT.FocusOut, textListener);
117 text.addListener (DWT.Traverse, textListener);
118 text.addListener (DWT.Verify, textListener);
119 editor.setEditor (composite, item);
120 text.setText (item.getText ());
121 text.selectAll ();
122 text.setFocus ();
123 }
124 lastItem [0] = item;
125 }
126 });
127 shell.pack ();
128 shell.open ();
129 while (!shell.isDisposed()) {
130 if (!display.readAndDispatch ()) display.sleep ();
131 }
132 display.dispose ();
133 }
134