1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet94" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2004 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * D Port: 46 * Thomas Demmer <t_demmer AT web DOT de> 47 *******************************************************************************/ 48 module org.eclipse.swt.snippets.Snippet94; 49 /* 50 * Clipboard example snippet: copy and paste data with the clipboard 51 * 52 * For a list of all SWT example snippets see 53 * http://www.eclipse.org/swt/snippets/ 54 */ 55 import org.eclipse.swt.SWT; 56 import org.eclipse.swt.dnd.Clipboard; 57 import org.eclipse.swt.dnd.Transfer; 58 import org.eclipse.swt.dnd.TextTransfer; 59 60 import org.eclipse.swt.layout.FormAttachment; 61 import org.eclipse.swt.layout.FormData; 62 import org.eclipse.swt.layout.FormLayout; 63 64 import org.eclipse.swt.widgets.Button; 65 import org.eclipse.swt.widgets.Display; 66 import org.eclipse.swt.widgets.Event; 67 import org.eclipse.swt.widgets.Listener; 68 import org.eclipse.swt.widgets.Shell; 69 import org.eclipse.swt.widgets.Text; 70 71 import java.lang.all; 72 73 74 void main() { 75 Display display = new Display (); 76 Clipboard cb = new Clipboard(display); 77 Shell shell = new Shell (display); 78 shell.setLayout(new FormLayout()); 79 Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); 80 81 Button copy = new Button(shell, SWT.PUSH); 82 copy.setText("Copy"); 83 copy.addListener (SWT.Selection, new class Listener { 84 public void handleEvent (Event e) { 85 String textData = text.getSelectionText(); 86 if (textData.length > 0) { 87 TextTransfer textTransfer = TextTransfer.getInstance(); 88 // this is ugly, but works. 89 Transfer[] xfer = [ textTransfer]; 90 Object[] td = [ new ArrayWrapperString(textData) ]; 91 cb.setContents(td,xfer); 92 } 93 } 94 }); 95 96 Button paste = new Button(shell, SWT.PUSH); 97 paste.setText("Paste"); 98 paste.addListener (SWT.Selection, new class Listener { 99 public void handleEvent (Event e) { 100 TextTransfer transfer = TextTransfer.getInstance(); 101 String data = stringcast(cb.getContents(transfer)); 102 if (data !is null) { 103 text.insert(data); 104 } 105 } 106 }); 107 108 FormData data = new FormData(); 109 data.left = new FormAttachment(paste, 0, SWT.LEFT); 110 data.right = new FormAttachment(100, -5); 111 data.top = new FormAttachment(0, 5); 112 copy.setLayoutData(data); 113 114 data = new FormData(); 115 data.right = new FormAttachment(100, -5); 116 data.top = new FormAttachment(copy, 5); 117 paste.setLayoutData(data); 118 119 data = new FormData(); 120 data.left = new FormAttachment(0, 5); 121 data.top = new FormAttachment(0, 5); 122 data.right = new FormAttachment(paste, -5); 123 data.bottom = new FormAttachment(100, -5); 124 text.setLayoutData(data); 125 126 shell.setSize(200, 200); 127 shell.open(); 128 while (!shell.isDisposed ()) { 129 if (!display.readAndDispatch ()) display.sleep (); 130 } 131 cb.dispose(); 132 display.dispose(); 133 }