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