Mercurial > projects > dwt-samples
comparison user/dragdrop/texttolabel.d @ 32:200a229be5ca
Added menu and sash Snippets. textdragdrop to the user section.
author | Jesse Phillips <Jesse.K.Phillips+D@gmail.com> |
---|---|
date | Tue, 04 Mar 2008 15:57:21 -0800 |
parents | |
children | 8ee0dfe6f685 |
comparison
equal
deleted
inserted
replaced
31:5478bda2ae7f | 32:200a229be5ca |
---|---|
1 module user.dragdrop.texttolabel; | |
2 | |
3 /** | |
4 * Original | |
5 * http://www.java2s.com/Tutorial/Java/0280__SWT/DragselectedtextinTexttoLabel.htm | |
6 * | |
7 * Drag sellected text to a label. | |
8 * | |
9 * Port to the D programming language: | |
10 * Jesse Phillips <Jesse.K.Phillips+D> gmail.com | |
11 * | |
12 */ | |
13 | |
14 import dwt.DWT; | |
15 import dwt.DWTException; | |
16 import dwt.dnd.DND; | |
17 import dwt.dnd.DragSource; | |
18 import dwt.dnd.DragSourceAdapter; | |
19 import dwt.dnd.DragSourceEvent; | |
20 import dwt.dnd.DropTarget; | |
21 import dwt.dnd.DropTargetAdapter; | |
22 import dwt.dnd.DropTargetEvent; | |
23 import dwt.dnd.TextTransfer; | |
24 import dwt.dnd.Transfer; | |
25 import dwt.widgets.Display; | |
26 import dwt.widgets.Label; | |
27 import dwt.widgets.Shell; | |
28 import dwt.widgets.Text; | |
29 | |
30 import tango.io.Stdout; | |
31 | |
32 void main() { | |
33 auto display = new Display(); | |
34 auto shell = new Shell(display); | |
35 | |
36 auto text = new Text(shell, DWT.BORDER|DWT.SINGLE); | |
37 | |
38 int i = 0; | |
39 | |
40 auto types = new Transfer[1]; | |
41 types[0] = TextTransfer.getInstance(); | |
42 auto source = new DragSource(text, DND.DROP_MOVE | DND.DROP_COPY); | |
43 source.setTransfer(types); | |
44 | |
45 source.addDragListener(new class DragSourceAdapter { | |
46 public void dragSetData(DragSourceEvent event) { | |
47 // Get the selected items in the drag source | |
48 DragSource ds = cast(DragSource) event.widget; | |
49 Text text = cast(Text) ds.getControl(); | |
50 | |
51 event.data = new ArrayWrapperString(text.getSelectionText()); | |
52 } | |
53 }); | |
54 | |
55 auto label = new Label(shell, DWT.BORDER); | |
56 // Create the drop target | |
57 auto target = new DropTarget(label, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT); | |
58 target.setTransfer(types); | |
59 target.addDropListener(new class DropTargetAdapter { | |
60 public void dragEnter(DropTargetEvent event) { | |
61 if (event.detail == DND.DROP_DEFAULT) { | |
62 event.detail = (event.operations & DND.DROP_COPY) != 0 ? DND.DROP_COPY : DND.DROP_NONE; | |
63 } | |
64 | |
65 // Allow dropping text only | |
66 foreach (dataType; event.dataTypes) { | |
67 if (TextTransfer.getInstance().isSupportedType(dataType)) { | |
68 event.currentDataType = dataType; | |
69 } | |
70 } | |
71 } | |
72 | |
73 public void dragOver(DropTargetEvent event) { | |
74 event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL; | |
75 } | |
76 public void drop(DropTargetEvent event) { | |
77 if (TextTransfer.getInstance().isSupportedType(event.currentDataType)) { | |
78 // Get the dropped data | |
79 DropTarget target = cast(DropTarget) event.widget; | |
80 Label label = cast(Label) target.getControl(); | |
81 auto data = cast(ArrayWrapperString) event.data; | |
82 | |
83 label.setText(data.array); | |
84 label.redraw(); | |
85 } | |
86 } | |
87 }); | |
88 | |
89 text.setBounds(10,10,100,25); | |
90 label.setBounds(10,55,100,25); | |
91 shell.setSize(300, 200); | |
92 shell.open(); | |
93 | |
94 while (!shell.isDisposed()) { | |
95 if (!display.readAndDispatch()) { | |
96 display.sleep(); | |
97 } | |
98 } | |
99 display.dispose(); | |
100 } |