1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet111"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * D Port:
46  *     Bill Baxter <wbaxter@gmail.com>
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet111;
49 
50 /*
51  * TreeEditor example snippet: edit the text of a tree item (in place, fancy)
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  */
56 import org.eclipse.swt.SWT;
57 import org.eclipse.swt.graphics.Color;
58 import org.eclipse.swt.graphics.Rectangle;
59 import org.eclipse.swt.graphics.GC;
60 import org.eclipse.swt.widgets.Display;
61 import org.eclipse.swt.widgets.Shell;
62 import org.eclipse.swt.widgets.Widget;
63 import org.eclipse.swt.widgets.Composite;
64 import org.eclipse.swt.widgets.Tree;
65 import org.eclipse.swt.widgets.TreeItem;
66 import org.eclipse.swt.widgets.Text;
67 import org.eclipse.swt.widgets.Listener;
68 import org.eclipse.swt.widgets.Event;
69 import org.eclipse.swt.layout.FillLayout;
70 import org.eclipse.swt.custom.TreeEditor;
71 
72 import java.lang.all;
73 
74 version(JIVE){
75     import jive.stacktrace;
76 }
77 
78 void main () {
79 
80     Tree tree;
81     Color black;
82     void handleResize (Event e, Composite composite, Text text, int inset ) {
83         Rectangle rect = composite.getClientArea ();
84         text.setBounds (rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2);
85     }
86     void handleTextEvent (Event e, Composite composite, TreeItem item, TreeEditor editor,Text text, int inset ) {
87         switch (e.type) {
88         case SWT.FocusOut: {
89             item.setText (text.getText ());
90             composite.dispose ();
91         }
92         break;
93         case SWT.Verify: {
94             String newText = text.getText ();
95             String leftText = newText.substring (0, e.start);
96             String rightText = newText.substring (e.end, cast(int)newText.length);
97             GC gc = new GC (text);
98             Point size = gc.textExtent (leftText ~ e.text ~ rightText);
99             gc.dispose ();
100             size = text.computeSize (size.x, SWT.DEFAULT);
101             editor.horizontalAlignment = SWT.LEFT;
102             Rectangle itemRect = item.getBounds (), rect = tree.getClientArea ();
103             editor.minimumWidth = Math.max (size.x, itemRect.width) + inset* 2;
104             int left = itemRect.x, right = rect.x + rect.width;
105             editor.minimumWidth = Math.min (editor.minimumWidth, right - left);
106             editor.minimumHeight = size.y + inset* 2;
107             editor.layout ();
108         }
109         break;
110         case SWT.Traverse: {
111             switch (e.detail) {
112             case SWT.TRAVERSE_RETURN:
113                 item.setText (text.getText ());
114                 goto case SWT.TRAVERSE_ESCAPE;
115             case SWT.TRAVERSE_ESCAPE:
116                 composite.dispose ();
117                 e.doit = false;
118                 break;
119             default:
120                 //no-op
121                 break;
122             }
123             break;
124         }
125         default:
126         // no-op
127         }
128     }
129     void handleSelection (Event event, TreeItem[] lastItem, TreeEditor editor ) {
130         TreeItem item = cast(TreeItem) event.item;
131         if (item !is null && item is lastItem [0]) {
132             bool showBorder = true;
133             Composite composite = new Composite (tree, SWT.NONE);
134             if (showBorder) composite.setBackground (black);
135             Text text = new Text (composite, SWT.NONE);
136             int inset = showBorder ? 1 : 0;
137             composite.addListener (SWT.Resize, dgListener( &handleResize, composite, text, inset ));
138             Listener textListener = dgListener( &handleTextEvent, composite, item, editor, text, inset);
139             text.addListener (SWT.FocusOut, textListener);
140             text.addListener (SWT.Traverse, textListener);
141             text.addListener (SWT.Verify, textListener);
142             editor.setEditor (composite, item);
143             text.setText (item.getText ());
144             text.selectAll ();
145             text.setFocus ();
146         }
147         lastItem [0] = item;
148     }
149 
150     Display display = new Display ();
151     black = display.getSystemColor (SWT.COLOR_BLACK);
152     Shell shell = new Shell (display);
153     shell.setLayout (new FillLayout ());
154     tree = new Tree (shell, SWT.BORDER);
155     for (int i=0; i<16; i++) {
156         TreeItem itemI = new TreeItem (tree, SWT.NONE);
157         itemI.setText (Format("Item {}", i));
158         for (int j=0; j<16; j++) {
159             TreeItem itemJ = new TreeItem (itemI, SWT.NONE);
160             itemJ.setText ( Format("Item {}", j) );
161         }
162     }
163     TreeItem [] lastItem = new TreeItem [1];
164     TreeEditor editor = new TreeEditor (tree);
165     tree.addListener (SWT.Selection, dgListener( &handleSelection, lastItem, editor ));
166     shell.pack ();
167     shell.open ();
168     while (!shell.isDisposed()) {
169         if (!display.readAndDispatch ()) display.sleep ();
170     }
171     display.dispose ();
172 }
173 
174