Mercurial > projects > dwt-samples
annotate examples/clipboard/ClipboardExample.d @ 109:be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 06 Jul 2008 14:21:59 +0200 |
parents | 4a04b6759f98 |
children | 698e70d0dca3 |
rev | line source |
---|---|
78 | 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 * Jesse Phillips <Jesse.K.Phillips+D> gmail.com | |
12 *******************************************************************************/ | |
13 module examples.clipboard.ClipboardExample; | |
14 | |
15 import dwt.DWT; | |
16 import dwt.custom.ScrolledComposite; | |
17 import dwt.custom.StyledText; | |
18 import dwt.dnd.Clipboard; | |
19 import dwt.dnd.FileTransfer; | |
20 import dwt.dnd.HTMLTransfer; | |
21 import dwt.dnd.RTFTransfer; | |
22 import dwt.dnd.TextTransfer; | |
23 import dwt.dnd.Transfer; | |
24 import dwt.events.SelectionAdapter; | |
25 import dwt.events.SelectionEvent; | |
26 import dwt.graphics.Point; | |
27 import dwt.graphics.Rectangle; | |
28 import dwt.layout.FillLayout; | |
29 import dwt.layout.GridData; | |
30 import dwt.layout.GridLayout; | |
31 import dwt.widgets.Button; | |
32 import dwt.widgets.Combo; | |
33 import dwt.widgets.Composite; | |
34 import dwt.widgets.DirectoryDialog; | |
35 import dwt.widgets.Display; | |
36 import dwt.widgets.FileDialog; | |
37 import dwt.widgets.Group; | |
38 import dwt.widgets.Label; | |
39 import dwt.widgets.List; | |
40 import dwt.widgets.Shell; | |
41 import dwt.widgets.Table; | |
42 import dwt.widgets.TableItem; | |
43 import dwt.widgets.Text; | |
44 import dwt.dwthelper.utils; | |
45 | |
46 import tango.io.Stdout; | |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
47 version(JIVE) import jive.stacktrace; |
78 | 48 |
49 class ClipboardExample { | |
50 static const int SIZE = 60; | |
51 Clipboard clipboard; | |
52 Shell shell; | |
53 Text copyText; | |
54 Text pasteText; | |
55 Text copyRtfText; | |
56 Text pasteRtfText; | |
57 Text copyHtmlText; | |
58 Text pasteHtmlText; | |
59 Table copyFileTable; | |
60 Table pasteFileTable; | |
61 Text text; | |
62 Combo combo; | |
63 StyledText styledText; | |
64 Label status; | |
65 | |
66 public void open(Display display) { | |
67 clipboard = new Clipboard(display); | |
68 shell = new Shell (display); | |
69 shell.setText("DWT Clipboard"); | |
70 shell.setLayout(new FillLayout()); | |
71 | |
72 ScrolledComposite sc = new ScrolledComposite(shell, DWT.H_SCROLL | DWT.V_SCROLL); | |
73 Composite parent = new Composite(sc, DWT.NONE); | |
74 sc.setContent(parent); | |
75 parent.setLayout(new GridLayout(2, true)); | |
76 | |
77 Group copyGroup = new Group(parent, DWT.NONE); | |
78 copyGroup.setText("Copy From:"); | |
79 GridData data = new GridData(GridData.FILL_BOTH); | |
80 copyGroup.setLayoutData(data); | |
81 copyGroup.setLayout(new GridLayout(3, false)); | |
82 | |
83 Group pasteGroup = new Group(parent, DWT.NONE); | |
84 pasteGroup.setText("Paste To:"); | |
85 data = new GridData(GridData.FILL_BOTH); | |
86 pasteGroup.setLayoutData(data); | |
87 pasteGroup.setLayout(new GridLayout(3, false)); | |
88 | |
89 Group controlGroup = new Group(parent, DWT.NONE); | |
90 controlGroup.setText("Control API:"); | |
91 data = new GridData(GridData.FILL_BOTH); | |
92 data.horizontalSpan = 2; | |
93 controlGroup.setLayoutData(data); | |
94 controlGroup.setLayout(new GridLayout(5, false)); | |
95 | |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
96 /* Enable with Available Types */ |
78 | 97 Group typesGroup = new Group(parent, DWT.NONE); |
98 typesGroup.setText("Available Types"); | |
99 data = new GridData(GridData.FILL_BOTH); | |
100 data.horizontalSpan = 2; | |
101 typesGroup.setLayoutData(data); | |
102 typesGroup.setLayout(new GridLayout(2, false)); | |
103 /**/ | |
104 | |
105 status = new Label(parent, DWT.BORDER); | |
106 data = new GridData(GridData.FILL_HORIZONTAL); | |
107 data.horizontalSpan = 2; | |
108 data.heightHint = 60; | |
109 status.setLayoutData(data); | |
110 | |
111 createTextTransfer(copyGroup, pasteGroup); | |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
112 createRTFTransfer(copyGroup, pasteGroup); |
78 | 113 createHTMLTransfer(copyGroup, pasteGroup); |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
114 createFileTransfer(copyGroup, pasteGroup); |
78 | 115 createMyTransfer(copyGroup, pasteGroup); |
116 createControlTransfer(controlGroup); | |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
117 createAvailableTypes(typesGroup); |
78 | 118 |
119 sc.setMinSize(parent.computeSize(DWT.DEFAULT, DWT.DEFAULT)); | |
120 sc.setExpandHorizontal(true); | |
121 sc.setExpandVertical(true); | |
122 | |
123 Point size = shell.computeSize(DWT.DEFAULT, DWT.DEFAULT); | |
124 Rectangle monitorArea = shell.getMonitor().getClientArea(); | |
125 shell.setSize(Math.min(size.x, monitorArea.width - 20), Math.min(size.y, monitorArea.height - 20)); | |
126 shell.open(); | |
127 while (!shell.isDisposed ()) { | |
128 if (!display.readAndDispatch ()) display.sleep (); | |
129 } | |
130 clipboard.dispose(); | |
131 } | |
132 void createTextTransfer(Composite copyParent, Composite pasteParent) { | |
133 | |
134 // TextTransfer | |
135 Label l = new Label(copyParent, DWT.NONE); | |
136 l.setText("TextTransfer:"); //$NON-NLS-1$ | |
137 copyText = new Text(copyParent, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL | DWT.H_SCROLL); | |
138 copyText.setText("some\nplain\ntext"); | |
139 GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
140 data.heightHint = data.widthHint = SIZE; | |
141 copyText.setLayoutData(data); | |
142 Button b = new Button(copyParent, DWT.PUSH); | |
143 b.setText("Copy"); | |
144 b.addSelectionListener(new class SelectionAdapter { | |
145 public void widgetSelected(SelectionEvent e) { | |
146 auto data = copyText.getText(); | |
147 if (data.length > 0) { | |
148 status.setText(""); | |
149 auto obj = new Object[1]; | |
150 auto trans = new TextTransfer[1]; | |
151 obj[0] = cast(Object) new ArrayWrapperString(data); | |
152 trans[0] = TextTransfer.getInstance(); | |
153 clipboard.setContents(obj, trans); | |
154 } else { | |
155 status.setText("nothing to copy"); | |
156 } | |
157 } | |
158 }); | |
159 | |
160 l = new Label(pasteParent, DWT.NONE); | |
161 l.setText("TextTransfer:"); //$NON-NLS-1$ | |
162 pasteText = new Text(pasteParent, DWT.READ_ONLY | DWT.MULTI | DWT.BORDER | DWT.V_SCROLL | DWT.H_SCROLL); | |
163 data = new GridData(GridData.FILL_HORIZONTAL); | |
164 data.heightHint = data.widthHint = SIZE; | |
165 pasteText.setLayoutData(data); | |
166 b = new Button(pasteParent, DWT.PUSH); | |
167 b.setText("Paste"); | |
168 b.addSelectionListener(new class SelectionAdapter { | |
169 public void widgetSelected(SelectionEvent e) { | |
170 auto data = cast(ArrayWrapperString) clipboard.getContents(TextTransfer.getInstance()); | |
171 if (data !is null) { | |
172 status.setText(""); | |
173 pasteText.setText("begin paste>"~data.array~"<end paste"); | |
174 } else { | |
175 status.setText("nothing to paste"); | |
176 } | |
177 } | |
178 }); | |
179 } | |
180 void createRTFTransfer(Composite copyParent, Composite pasteParent){ | |
181 // RTF Transfer | |
182 Label l = new Label(copyParent, DWT.NONE); | |
183 l.setText("RTFTransfer:"); //$NON-NLS-1$ | |
184 copyRtfText = new Text(copyParent, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL | DWT.H_SCROLL); | |
185 copyRtfText.setText("some\nrtf\ntext"); | |
186 GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
187 data.heightHint = data.widthHint = SIZE; | |
188 copyRtfText.setLayoutData(data); | |
189 Button b = new Button(copyParent, DWT.PUSH); | |
190 b.setText("Copy"); | |
191 b.addSelectionListener(new class SelectionAdapter { | |
192 public void widgetSelected(SelectionEvent e) { | |
193 auto data = copyRtfText.getText(); | |
194 if (data.length > 0) { | |
195 status.setText(""); | |
196 data = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i " ~ data ~ "}"; | |
197 auto obj = new Object[1]; | |
198 auto trans = new Transfer[1]; | |
199 obj[0] = cast(Object) new ArrayWrapperString(data); | |
200 trans[0] = RTFTransfer.getInstance(); | |
201 clipboard.setContents(obj, trans); | |
202 } else { | |
203 status.setText("nothing to copy"); | |
204 } | |
205 } | |
206 }); | |
207 | |
208 l = new Label(pasteParent, DWT.NONE); | |
209 l.setText("RTFTransfer:"); //$NON-NLS-1$ | |
210 pasteRtfText = new Text(pasteParent, DWT.READ_ONLY | DWT.MULTI | DWT.BORDER | DWT.V_SCROLL | DWT.H_SCROLL); | |
211 data = new GridData(GridData.FILL_HORIZONTAL); | |
212 data.heightHint = data.widthHint = SIZE; | |
213 pasteRtfText.setLayoutData(data); | |
214 b = new Button(pasteParent, DWT.PUSH); | |
215 b.setText("Paste"); | |
216 b.addSelectionListener(new class SelectionAdapter { | |
217 public void widgetSelected(SelectionEvent e) { | |
218 auto data = cast(ArrayWrapperString) clipboard.getContents(RTFTransfer.getInstance()); | |
219 if (data !is null) { | |
220 status.setText(""); | |
221 pasteRtfText.setText("begin paste>"~data.array~"<end paste"); | |
222 } else { | |
223 status.setText("nothing to paste"); | |
224 } | |
225 } | |
226 }); | |
227 } | |
228 void createHTMLTransfer(Composite copyParent, Composite pasteParent){ | |
229 // HTML Transfer | |
230 Label l = new Label(copyParent, DWT.NONE); | |
231 l.setText("HTMLTransfer:"); //$NON-NLS-1$ | |
232 copyHtmlText = new Text(copyParent, DWT.MULTI | DWT.BORDER | DWT.V_SCROLL | DWT.H_SCROLL); | |
233 copyHtmlText.setText("<b>Hello World</b>"); | |
234 GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
235 data.heightHint = data.widthHint = SIZE; | |
236 copyHtmlText.setLayoutData(data); | |
237 Button b = new Button(copyParent, DWT.PUSH); | |
238 b.setText("Copy"); | |
239 b.addSelectionListener(new class SelectionAdapter { | |
240 public void widgetSelected(SelectionEvent e) { | |
241 auto data = copyHtmlText.getText(); | |
242 if (data.length > 0) { | |
243 status.setText(""); | |
244 auto obj = new Object[1]; | |
245 auto trans = new Transfer[1]; | |
246 obj[0] = cast(Object) new ArrayWrapperString(data); | |
247 trans[0] = HTMLTransfer.getInstance(); | |
248 clipboard.setContents(obj, trans); | |
249 } else { | |
250 status.setText("nothing to copy"); | |
251 } | |
252 } | |
253 }); | |
254 | |
255 l = new Label(pasteParent, DWT.NONE); | |
256 l.setText("HTMLTransfer:"); //$NON-NLS-1$ | |
257 pasteHtmlText = new Text(pasteParent, DWT.READ_ONLY | DWT.MULTI | DWT.BORDER | DWT.V_SCROLL | DWT.H_SCROLL); | |
258 data = new GridData(GridData.FILL_HORIZONTAL); | |
259 data.heightHint = data.widthHint = SIZE; | |
260 pasteHtmlText.setLayoutData(data); | |
261 b = new Button(pasteParent, DWT.PUSH); | |
262 b.setText("Paste"); | |
263 b.addSelectionListener(new class SelectionAdapter { | |
264 public void widgetSelected(SelectionEvent e) { | |
265 auto data = cast(ArrayWrapperString) clipboard.getContents(HTMLTransfer.getInstance()); | |
266 if (data !is null) { | |
267 status.setText(""); | |
268 pasteHtmlText.setText("begin paste>"~data.array~"<end paste"); | |
269 } else { | |
270 status.setText("nothing to paste"); | |
271 } | |
272 } | |
273 }); | |
274 } | |
275 void createFileTransfer(Composite copyParent, Composite pasteParent){ | |
276 //File Transfer | |
277 Label l = new Label(copyParent, DWT.NONE); | |
278 l.setText("FileTransfer:"); //$NON-NLS-1$ | |
279 | |
280 Composite c = new Composite(copyParent, DWT.NONE); | |
281 c.setLayout(new GridLayout(2, false)); | |
282 GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
283 c.setLayoutData(data); | |
284 | |
285 copyFileTable = new Table(c, DWT.MULTI | DWT.BORDER); | |
286 data = new GridData(GridData.FILL_HORIZONTAL); | |
287 data.heightHint = data.widthHint = SIZE; | |
288 data.horizontalSpan = 2; | |
289 copyFileTable.setLayoutData(data); | |
290 | |
291 Button b = new Button(c, DWT.PUSH); | |
292 b.setText("Select file(s)"); | |
293 b.addSelectionListener(new class SelectionAdapter { | |
294 public void widgetSelected(SelectionEvent e) { | |
295 FileDialog dialog = new FileDialog(shell, DWT.OPEN | DWT.MULTI); | |
296 auto result = dialog.open(); | |
297 if (result !is null && result.length > 0){ | |
298 //copyFileTable.removeAll(); | |
299 //This cannot be used | |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
300 auto separator = tango.io.model.IFile.FileConst.PathSeparatorString; |
78 | 301 auto path = dialog.getFilterPath(); |
302 auto names = dialog.getFileNames(); | |
303 for (int i = 0; i < names.length; i++) { | |
304 TableItem item = new TableItem(copyFileTable, DWT.NONE); | |
305 item.setText(path~separator~names[i]); | |
306 } | |
307 } | |
308 } | |
309 }); | |
310 b = new Button(c, DWT.PUSH); | |
311 b.setText("Select directory"); | |
312 b.addSelectionListener(new class SelectionAdapter { | |
313 public void widgetSelected(SelectionEvent e) { | |
314 DirectoryDialog dialog = new DirectoryDialog(shell, DWT.OPEN); | |
315 auto result = dialog.open(); | |
316 if (result !is null && result.length > 0){ | |
317 //copyFileTable.removeAll(); | |
318 TableItem item = new TableItem(copyFileTable, DWT.NONE); | |
319 item.setText(result); | |
320 } | |
321 } | |
322 }); | |
323 | |
324 b = new Button(copyParent, DWT.PUSH); | |
325 b.setText("Copy"); | |
326 b.addSelectionListener(new class SelectionAdapter { | |
327 public void widgetSelected(SelectionEvent e) { | |
328 TableItem[] items = copyFileTable.getItems(); | |
329 if (items.length > 0){ | |
330 status.setText(""); | |
331 auto data = new char[][items.length]; | |
332 for (int i = 0; i < data.length; i++) { | |
333 data[i] = items[i].getText(); | |
334 } | |
335 auto obj = new Object[1]; | |
336 auto trans = new Transfer[1]; | |
337 obj[0] = cast(Object) new ArrayWrapperString2(data); | |
338 trans[0] = FileTransfer.getInstance(); | |
339 clipboard.setContents(obj, trans); | |
340 } else { | |
341 status.setText("nothing to copy"); | |
342 } | |
343 } | |
344 }); | |
345 | |
346 l = new Label(pasteParent, DWT.NONE); | |
347 l.setText("FileTransfer:"); //$NON-NLS-1$ | |
348 pasteFileTable = new Table(pasteParent, DWT.MULTI | DWT.BORDER); | |
349 data = new GridData(GridData.FILL_HORIZONTAL); | |
350 data.heightHint = data.widthHint = SIZE; | |
351 pasteFileTable.setLayoutData(data); | |
352 b = new Button(pasteParent, DWT.PUSH); | |
353 b.setText("Paste"); | |
354 b.addSelectionListener(new class SelectionAdapter { | |
355 public void widgetSelected(SelectionEvent e) { | |
356 auto data = cast(ArrayWrapperString2) clipboard.getContents(FileTransfer.getInstance()); | |
357 if (data !is null && data.array.length > 0) { | |
358 status.setText(""); | |
359 pasteFileTable.removeAll(); | |
360 foreach (s; data.array) { | |
361 TableItem item = new TableItem(pasteFileTable, DWT.NONE); | |
362 item.setText(s); | |
363 } | |
364 } else { | |
365 status.setText("nothing to paste"); | |
366 } | |
367 } | |
368 }); | |
369 } | |
370 void createMyTransfer(Composite copyParent, Composite pasteParent){ | |
371 // MyType Transfer | |
372 // TODO | |
373 } | |
374 void createControlTransfer(Composite parent){ | |
375 Label l = new Label(parent, DWT.NONE); | |
376 l.setText("Text:"); | |
377 Button b = new Button(parent, DWT.PUSH); | |
378 b.setText("Cut"); | |
379 b.addSelectionListener(new class SelectionAdapter { | |
380 public void widgetSelected(SelectionEvent e) { | |
381 text.cut(); | |
382 } | |
383 }); | |
384 b = new Button(parent, DWT.PUSH); | |
385 b.setText("Copy"); | |
386 b.addSelectionListener(new class SelectionAdapter { | |
387 public void widgetSelected(SelectionEvent e) { | |
388 text.copy(); | |
389 } | |
390 }); | |
391 b = new Button(parent, DWT.PUSH); | |
392 b.setText("Paste"); | |
393 b.addSelectionListener(new class SelectionAdapter { | |
394 public void widgetSelected(SelectionEvent e) { | |
395 text.paste(); | |
396 } | |
397 }); | |
398 text = new Text(parent, DWT.BORDER | DWT.MULTI | DWT.H_SCROLL | DWT.V_SCROLL); | |
399 GridData data = new GridData(GridData.FILL_HORIZONTAL); | |
400 data.heightHint = data.widthHint = SIZE; | |
401 text.setLayoutData(data); | |
402 | |
403 l = new Label(parent, DWT.NONE); | |
404 l.setText("Combo:"); | |
405 b = new Button(parent, DWT.PUSH); | |
406 b.setText("Cut"); | |
407 b.addSelectionListener(new class SelectionAdapter { | |
408 public void widgetSelected(SelectionEvent e) { | |
409 combo.cut(); | |
410 } | |
411 }); | |
412 b = new Button(parent, DWT.PUSH); | |
413 b.setText("Copy"); | |
414 b.addSelectionListener(new class SelectionAdapter { | |
415 public void widgetSelected(SelectionEvent e) { | |
416 combo.copy(); | |
417 } | |
418 }); | |
419 b = new Button(parent, DWT.PUSH); | |
420 b.setText("Paste"); | |
421 b.addSelectionListener(new class SelectionAdapter { | |
422 public void widgetSelected(SelectionEvent e) { | |
423 combo.paste(); | |
424 } | |
425 }); | |
426 combo = new Combo(parent, DWT.NONE); | |
427 char[][] str = new char[][4]; | |
428 str[0] = "Item 1"; | |
429 str[1] = "Item 2"; | |
430 str[2] = "Item 3"; | |
431 str[3] = "A longer Item"; | |
432 combo.setItems(str); | |
433 | |
434 l = new Label(parent, DWT.NONE); | |
435 l.setText("StyledText:"); | |
436 l = new Label(parent, DWT.NONE); | |
437 l.setVisible(false); | |
438 b = new Button(parent, DWT.PUSH); | |
439 b.setText("Copy"); | |
440 b.addSelectionListener(new class SelectionAdapter { | |
441 public void widgetSelected(SelectionEvent e) { | |
442 styledText.copy(); | |
443 } | |
444 }); | |
445 b = new Button(parent, DWT.PUSH); | |
446 b.setText("Paste"); | |
447 b.addSelectionListener(new class SelectionAdapter { | |
448 public void widgetSelected(SelectionEvent e) { | |
449 styledText.paste(); | |
450 } | |
451 }); | |
452 styledText = new StyledText(parent, DWT.BORDER | DWT.MULTI | DWT.H_SCROLL | DWT.V_SCROLL); | |
453 data = new GridData(GridData.FILL_HORIZONTAL); | |
454 data.heightHint = data.widthHint = SIZE; | |
455 styledText.setLayoutData(data); | |
456 } | |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
457 |
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
458 List list; |
78 | 459 void createAvailableTypes(Composite parent){ |
109
be3a48f1d596
ClipboardExample: reactivate comment functionality and fixed a bug.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
460 list = new List(parent, DWT.BORDER | DWT.H_SCROLL | DWT.V_SCROLL); |
78 | 461 GridData data = new GridData(GridData.FILL_BOTH); |
462 data.heightHint = 100; | |
463 list.setLayoutData(data); | |
464 Button b = new Button(parent, DWT.PUSH); | |
465 b.setText("Get Available Types"); | |
466 b.addSelectionListener(new class SelectionAdapter { | |
467 public void widgetSelected(SelectionEvent e) { | |
468 list.removeAll(); | |
469 auto names = clipboard.getAvailableTypeNames(); | |
470 for (int i = 0; i < names.length; i++) { | |
471 list.add(names[i]); | |
472 } | |
473 } | |
474 }); | |
475 } | |
476 } | |
477 void main() { | |
478 Display display = new Display(); | |
479 (new ClipboardExample()).open(display); | |
480 display.dispose(); | |
481 } | |
482 |