Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet111.d @ 44:ed96ea2a2764
First swt win snippets are buiding with d2+phobos
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 25 Mar 2009 18:31:01 +0100 |
parents | 69b1fa94a4a8 |
children |
rev | line source |
---|---|
28 | 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 org.eclipse.swt.snippets.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 org.eclipse.swt.SWT; | |
22 import org.eclipse.swt.graphics.Color; | |
23 import org.eclipse.swt.graphics.Rectangle; | |
24 import org.eclipse.swt.graphics.GC; | |
25 import org.eclipse.swt.widgets.Display; | |
26 import org.eclipse.swt.widgets.Shell; | |
27 import org.eclipse.swt.widgets.Widget; | |
28 import org.eclipse.swt.widgets.Composite; | |
29 import org.eclipse.swt.widgets.Tree; | |
30 import org.eclipse.swt.widgets.TreeItem; | |
31 import org.eclipse.swt.widgets.Text; | |
32 import org.eclipse.swt.widgets.Listener; | |
33 import org.eclipse.swt.widgets.Event; | |
34 import org.eclipse.swt.layout.FillLayout; | |
35 import org.eclipse.swt.custom.TreeEditor; | |
36 | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
37 import java.lang.all; |
28 | 38 |
39 version(JIVE){ | |
40 import jive.stacktrace; | |
41 } | |
42 | |
43 void main () { | |
44 | |
45 Tree tree; | |
46 Color black; | |
47 void handleResize (Event e, Composite composite, Text text, int inset ) { | |
48 Rectangle rect = composite.getClientArea (); | |
49 text.setBounds (rect.x + inset, rect.y + inset, rect.width - inset * 2, rect.height - inset * 2); | |
50 } | |
51 void handleTextEvent (Event e, Composite composite, TreeItem item, TreeEditor editor,Text text, int inset ) { | |
52 switch (e.type) { | |
53 case SWT.FocusOut: { | |
54 item.setText (text.getText ()); | |
55 composite.dispose (); | |
56 } | |
57 break; | |
58 case SWT.Verify: { | |
59 String newText = text.getText (); | |
60 String leftText = newText.substring (0, e.start); | |
61 String rightText = newText.substring (e.end, newText.length); | |
62 GC gc = new GC (text); | |
63 Point size = gc.textExtent (leftText ~ e.text ~ rightText); | |
64 gc.dispose (); | |
65 size = text.computeSize (size.x, SWT.DEFAULT); | |
66 editor.horizontalAlignment = SWT.LEFT; | |
67 Rectangle itemRect = item.getBounds (), rect = tree.getClientArea (); | |
68 editor.minimumWidth = Math.max (size.x, itemRect.width) + inset* 2; | |
69 int left = itemRect.x, right = rect.x + rect.width; | |
70 editor.minimumWidth = Math.min (editor.minimumWidth, right - left); | |
71 editor.minimumHeight = size.y + inset* 2; | |
72 editor.layout (); | |
73 } | |
74 break; | |
75 case SWT.Traverse: { | |
76 switch (e.detail) { | |
77 case SWT.TRAVERSE_RETURN: | |
78 item.setText (text.getText ()); | |
79 //FALL THROUGH | |
80 case SWT.TRAVERSE_ESCAPE: | |
81 composite.dispose (); | |
82 e.doit = false; | |
83 default: | |
84 //no-op | |
85 } | |
86 break; | |
87 } | |
88 default: | |
89 // no-op | |
90 } | |
91 } | |
92 void handleSelection (Event event, TreeItem[] lastItem, TreeEditor editor ) { | |
93 TreeItem item = cast(TreeItem) event.item; | |
94 if (item !is null && item is lastItem [0]) { | |
95 bool showBorder = true; | |
96 Composite composite = new Composite (tree, SWT.NONE); | |
97 if (showBorder) composite.setBackground (black); | |
98 Text text = new Text (composite, SWT.NONE); | |
99 int inset = showBorder ? 1 : 0; | |
100 composite.addListener (SWT.Resize, dgListener( &handleResize, composite, text, inset )); | |
101 Listener textListener = dgListener( &handleTextEvent, composite, item, editor, text, inset); | |
102 text.addListener (SWT.FocusOut, textListener); | |
103 text.addListener (SWT.Traverse, textListener); | |
104 text.addListener (SWT.Verify, textListener); | |
105 editor.setEditor (composite, item); | |
106 text.setText (item.getText ()); | |
107 text.selectAll (); | |
108 text.setFocus (); | |
109 } | |
110 lastItem [0] = item; | |
111 } | |
112 | |
113 Display display = new Display (); | |
114 black = display.getSystemColor (SWT.COLOR_BLACK); | |
115 Shell shell = new Shell (display); | |
116 shell.setLayout (new FillLayout ()); | |
117 tree = new Tree (shell, SWT.BORDER); | |
118 for (int i=0; i<16; i++) { | |
119 TreeItem itemI = new TreeItem (tree, SWT.NONE); | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
120 itemI.setText (Format("Item {}", i)); |
28 | 121 for (int j=0; j<16; j++) { |
122 TreeItem itemJ = new TreeItem (itemI, SWT.NONE); | |
44
ed96ea2a2764
First swt win snippets are buiding with d2+phobos
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
123 itemJ.setText ( Format("Item {}", j) ); |
28 | 124 } |
125 } | |
126 TreeItem [] lastItem = new TreeItem [1]; | |
127 TreeEditor editor = new TreeEditor (tree); | |
128 tree.addListener (SWT.Selection, dgListener( &handleSelection, lastItem, editor )); | |
129 shell.pack (); | |
130 shell.open (); | |
131 while (!shell.isDisposed()) { | |
132 if (!display.readAndDispatch ()) display.sleep (); | |
133 } | |
134 display.dispose (); | |
135 } | |
136 | |
137 |