1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet122" 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.Snippet122; 49 /* 50 * Clipboard example snippet: enable/disable menu depending on clipboard content availability 51 * 52 * For a list of all SWT example snippets see 53 * http://www.eclipse.org/swt/snippets/ 54 * 55 * @since 3.0 56 */ 57 import org.eclipse.swt.SWT; 58 import org.eclipse.swt.dnd.Clipboard; 59 import org.eclipse.swt.dnd.TextTransfer; 60 import org.eclipse.swt.dnd.Transfer; 61 import org.eclipse.swt.dnd.TransferData; 62 import org.eclipse.swt.events.MenuAdapter; 63 import org.eclipse.swt.events.MenuEvent; 64 import org.eclipse.swt.events.SelectionAdapter; 65 import org.eclipse.swt.events.SelectionListener; 66 import org.eclipse.swt.layout.FillLayout; 67 import org.eclipse.swt.widgets.Display; 68 import org.eclipse.swt.widgets.Menu; 69 import org.eclipse.swt.widgets.MenuItem; 70 import org.eclipse.swt.widgets.Shell; 71 import org.eclipse.swt.widgets.Text; 72 73 import java.lang.all; 74 75 void main() { 76 Display display = new Display(); 77 Clipboard cb = new Clipboard(display); 78 Shell shell = new Shell(display); 79 shell.setLayout(new FillLayout()); 80 Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP); 81 Menu menu = new Menu(shell, SWT.POP_UP); 82 MenuItem copyItem = new MenuItem(menu, SWT.PUSH); 83 copyItem.setText("Copy"); 84 copyItem.addSelectionListener(new class SelectionAdapter{ 85 override 86 public void widgetSelected(SelectionEvent e) { 87 String selection = text.getSelectionText(); 88 if (selection.length == 0) return; 89 Object[] data = [ new ArrayWrapperString(selection) ]; 90 Transfer[] types = [ TextTransfer.getInstance() ]; 91 cb.setContents(data, types); 92 } 93 }); 94 MenuItem pasteItem = new MenuItem(menu, SWT.PUSH); 95 pasteItem.setText ("Paste"); 96 pasteItem.addSelectionListener(new class SelectionAdapter{ 97 override 98 public void widgetSelected(SelectionEvent e) { 99 String string = stringcast(cb.getContents(TextTransfer.getInstance())); 100 if (string !is null) text.insert(string); 101 } 102 }); 103 menu.addMenuListener(new class MenuAdapter{ 104 override 105 public void menuShown(MenuEvent e) { 106 // is copy valid? 107 String selection = text.getSelectionText(); 108 copyItem.setEnabled(selection.length > 0); 109 // is paste valid? 110 TransferData[] available = cb.getAvailableTypes(); 111 bool enabled = false; 112 for (int i = 0; i < available.length; i++) { 113 if (TextTransfer.getInstance().isSupportedType(available[i])) { 114 enabled = true; 115 break; 116 } 117 } 118 pasteItem.setEnabled(enabled); 119 } 120 }); 121 122 text.setMenu (menu); 123 shell.setSize(200, 200); 124 shell.open(); 125 while (!shell.isDisposed()) { 126 if (!display.readAndDispatch()) 127 display.sleep(); 128 } 129 cb.dispose(); 130 display.dispose(); 131 }