1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet282" 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) 2007 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.Snippets282; 49 50 /* 51 * Copy/paste image to/from clipboard. 52 * 53 * For a list of all SWT example snippets see 54 * http://www.eclipse.org/swt/snippets/ 55 */ 56 57 import org.eclipse.swt.SWT; 58 import org.eclipse.swt.dnd.Clipboard; 59 import org.eclipse.swt.dnd.ImageTransfer; 60 import org.eclipse.swt.dnd.Transfer; 61 import org.eclipse.swt.graphics.Image; 62 import org.eclipse.swt.graphics.ImageData; 63 import org.eclipse.swt.layout.GridLayout; 64 import org.eclipse.swt.layout.GridData; 65 66 import org.eclipse.swt.widgets.Button; 67 import org.eclipse.swt.widgets.Composite; 68 import org.eclipse.swt.widgets.Display; 69 import org.eclipse.swt.widgets.Event; 70 import org.eclipse.swt.widgets.FileDialog; 71 import org.eclipse.swt.widgets.Listener; 72 import org.eclipse.swt.widgets.Shell; 73 74 import java.lang.all; 75 76 void main() { 77 //An org.eclipse.swt.graphics.Resource in DWT Windows (as against SWT) checks 78 //in destructor if it has been disposed, so we are to watch imageButton's 79 //image and dispose it after shell.isDisposed() or just disable this check 80 version(Windows) 81 Image.globalDisposeChecking = false; 82 83 Display display = new Display(); 84 Clipboard clipboard = new Clipboard(display); 85 Shell shell = new Shell(display, SWT.SHELL_TRIM); 86 shell.setLayout(new GridLayout()); 87 shell.setText("Clipboard ImageTransfer"); 88 89 Button imageButton = new Button(shell, SWT.NONE ); 90 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); 91 gd.minimumHeight = 400; 92 gd.minimumWidth = 600; 93 imageButton.setLayoutData(gd); 94 95 Composite buttons = new Composite(shell, SWT.NONE); 96 buttons.setLayout(new GridLayout(4, true)); 97 Button button = new Button(buttons, SWT.PUSH); 98 button.setText("Open"); 99 button.addListener(SWT.Selection, new class Listener { 100 public void handleEvent(Event event) { 101 FileDialog dialog = new FileDialog (shell, SWT.OPEN); 102 dialog.setText("Open an image file or cancel"); 103 String string = dialog.open (); 104 if (string !is null) { 105 Image image = imageButton.getImage(); 106 if (image !is null) image.dispose(); 107 image = new Image(display, string); 108 imageButton.setImage(image); 109 } 110 } 111 }); 112 113 button = new Button(buttons, SWT.PUSH); 114 button.setText("Copy"); 115 button.addListener(SWT.Selection, new class Listener { 116 public void handleEvent(Event event) { 117 Image image = imageButton.getImage(); 118 if (image !is null) { 119 ImageTransfer transfer = ImageTransfer.getInstance(); 120 121 Transfer[] xfer = [ transfer ]; 122 Object[] td = [ image.getImageData ]; 123 124 clipboard.setContents(td,xfer); 125 } 126 } 127 }); 128 button = new Button(buttons, SWT.PUSH); 129 button.setText("Paste"); 130 button.addListener(SWT.Selection, new class Listener { 131 public void handleEvent(Event event) { 132 ImageTransfer transfer = ImageTransfer.getInstance(); 133 ImageData imageData = cast(ImageData)clipboard.getContents(transfer); 134 if (imageData !is null) { 135 imageButton.setText(""); 136 Image image = imageButton.getImage(); 137 if (image !is null) image.dispose(); 138 image = new Image(display, imageData); 139 imageButton.setImage(image); 140 } else { 141 imageButton.setText("No image"); 142 } 143 } 144 }); 145 146 button = new Button(buttons, SWT.PUSH); 147 button.setText("Clear"); 148 button.addListener(SWT.Selection, new class Listener { 149 public void handleEvent(Event event) { 150 imageButton.setText(""); 151 Image image = imageButton.getImage(); 152 if (image !is null) image.dispose(); 153 imageButton.setImage(null); 154 } 155 }); 156 shell.pack(); 157 shell.open(); 158 while (!shell.isDisposed()) { 159 if (!display.readAndDispatch()) 160 display.sleep(); 161 } 162 display.dispose(); 163 } 164