Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

root/snippets/treeeditor/Snippet111.d

Revision 85:fa286c85e7b8, 5.1 kB (checked in by Frank Benoit <benoit@tionex.de>, 17 years ago)

Fix Snippet111 and add jive to Snippet220

Line 
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
39 import tango.util.Convert;
40 import tango.util.log.Trace;
41 version(JIVE){
42     import jive.stacktrace;
43 }
44
45 void main () {
46
47     Tree tree;
48     Color black;
49     void handleResize (Event e, Composite composite, Text text, int inset ) {
50         Rectangle rect = composite.getClientArea ();
51         text.setBounds (rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2);
52     }
53     void handleTextEvent (Event e, Composite composite, TreeItem item, TreeEditor editor,Text text, int inset ) {
54         switch (e.type) {
55         case DWT.FocusOut: {
56             item.setText (text.getText ());
57             composite.dispose ();
58         }
59         break;
60         case DWT.Verify: {
61             String newText = text.getText ();
62             String leftText = newText.substring (0, e.start);
63             String rightText = newText.substring (e.end, newText.length);
64             GC gc = new GC (text);
65             Point size = gc.textExtent (leftText ~ e.text ~ rightText);
66             gc.dispose ();
67             size = text.computeSize (size.x, DWT.DEFAULT);
68             editor.horizontalAlignment = DWT.LEFT;
69             Rectangle itemRect = item.getBounds (), rect = tree.getClientArea ();
70             editor.minimumWidth = Math.max (size.x, itemRect.width) + inset* 2;
71             int left = itemRect.x, right = rect.x + rect.width;
72             editor.minimumWidth = Math.min (editor.minimumWidth, right - left);
73             editor.minimumHeight = size.y + inset* 2;
74             editor.layout ();
75         }
76         break;
77         case DWT.Traverse: {
78             switch (e.detail) {
79             case DWT.TRAVERSE_RETURN:
80                 item.setText (text.getText ());
81                 //FALL THROUGH
82             case DWT.TRAVERSE_ESCAPE:
83                 composite.dispose ();
84                 e.doit = false;
85             default:
86                 //no-op
87             }
88             break;
89         }
90         default:
91         // no-op
92         }
93     }
94     void handleSelection (Event event, TreeItem[] lastItem, TreeEditor editor ) {
95         TreeItem item = cast(TreeItem) event.item;
96         if (item !is null && item is lastItem [0]) {
97             bool showBorder = true;
98             Composite composite = new Composite (tree, DWT.NONE);
99             if (showBorder) composite.setBackground (black);
100             Text text = new Text (composite, DWT.NONE);
101             int inset = showBorder ? 1 : 0;
102             composite.addListener (DWT.Resize, dgListener( &handleResize, composite, text, inset ));
103             Listener textListener = dgListener( &handleTextEvent, composite, item, editor, text, inset);
104             text.addListener (DWT.FocusOut, textListener);
105             text.addListener (DWT.Traverse, textListener);
106             text.addListener (DWT.Verify, textListener);
107             editor.setEditor (composite, item);
108             text.setText (item.getText ());
109             text.selectAll ();
110             text.setFocus ();
111         }
112         lastItem [0] = item;
113     }
114
115     Display display = new Display ();
116     black = display.getSystemColor (DWT.COLOR_BLACK);
117     Shell shell = new Shell (display);
118     shell.setLayout (new FillLayout ());
119     tree = new Tree (shell, DWT.BORDER);
120     for (int i=0; i<16; i++) {
121         TreeItem itemI = new TreeItem (tree, DWT.NONE);
122         itemI.setText ("Item " ~ to!(char[])(i));
123         for (int j=0; j<16; j++) {
124             TreeItem itemJ = new TreeItem (itemI, DWT.NONE);
125             itemJ.setText ("Item " ~ to!(char[])(j) );
126         }
127     }
128     TreeItem [] lastItem = new TreeItem [1];
129     TreeEditor editor = new TreeEditor (tree);
130     tree.addListener (DWT.Selection, dgListener( &handleSelection, lastItem, editor ));
131     shell.pack ();
132     shell.open ();
133     while (!shell.isDisposed()) {
134         if (!display.readAndDispatch ()) display.sleep ();
135     }
136     display.dispose ();
137 }
Note: See TracBrowser for help on using the browser.