Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet282.d @ 29:4e5843b771cc
First snippets buildable on windows
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 22 Mar 2009 19:55:00 +0100 |
parents | 69b1fa94a4a8 |
children | 536e43f63c81 |
rev | line source |
---|---|
28 | 1 /******************************************************************************* |
2 * Copyright (c) 2007 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 * Thomas Demmer <t_demmer AT web DOT de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.snippets.Snippets282; | |
14 | |
15 /* | |
16 * Copy/paste image to/from clipboard. | |
17 * | |
18 * For a list of all SWT example snippets see | |
19 * http://www.eclipse.org/swt/snippets/ | |
20 */ | |
21 | |
22 import org.eclipse.swt.SWT; | |
23 import org.eclipse.swt.dnd.Clipboard; | |
24 import org.eclipse.swt.dnd.ImageTransfer; | |
25 import org.eclipse.swt.dnd.Transfer; | |
26 import org.eclipse.swt.graphics.Image; | |
27 import org.eclipse.swt.graphics.ImageData; | |
28 import org.eclipse.swt.layout.GridLayout; | |
29 import org.eclipse.swt.layout.GridData; | |
30 | |
31 import org.eclipse.swt.widgets.Button; | |
32 import org.eclipse.swt.widgets.Composite; | |
33 import org.eclipse.swt.widgets.Display; | |
34 import org.eclipse.swt.widgets.Event; | |
35 import org.eclipse.swt.widgets.FileDialog; | |
36 import org.eclipse.swt.widgets.Listener; | |
37 import org.eclipse.swt.widgets.Shell; | |
38 | |
39 import java.lang.all; | |
40 | |
41 public static void main(String[] args) { | |
42 Display display = new Display(); | |
43 Clipboard clipboard = new Clipboard(display); | |
44 Shell shell = new Shell(display, SWT.SHELL_TRIM); | |
45 shell.setLayout(new GridLayout()); | |
46 shell.setText("Clipboard ImageTransfer"); | |
47 | |
48 Button imageButton = new Button(shell, SWT.NONE ); | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
49 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); |
28 | 50 gd.minimumHeight = 400; |
51 gd.minimumWidth = 600; | |
52 imageButton.setLayoutData(gd); | |
53 | |
54 Composite buttons = new Composite(shell, SWT.NONE); | |
55 buttons.setLayout(new GridLayout(4, true)); | |
56 Button button = new Button(buttons, SWT.PUSH); | |
57 button.setText("Open"); | |
58 button.addListener(SWT.Selection, new class() Listener { | |
59 public void handleEvent(Event event) { | |
60 FileDialog dialog = new FileDialog (shell, SWT.OPEN); | |
61 dialog.setText("Open an image file or cancel"); | |
62 String string = dialog.open (); | |
63 if (string !is null) { | |
64 Image image = imageButton.getImage(); | |
65 if (image !is null) image.dispose(); | |
66 image = new Image(display, string); | |
67 imageButton.setImage(image); | |
68 } | |
69 } | |
70 }); | |
71 | |
72 button = new Button(buttons, SWT.PUSH); | |
73 button.setText("Copy"); | |
74 button.addListener(SWT.Selection, new class() Listener { | |
75 public void handleEvent(Event event) { | |
76 Image image = imageButton.getImage(); | |
77 if (image !is null) { | |
78 ImageTransfer transfer = ImageTransfer.getInstance(); | |
79 | |
80 Transfer[] xfer = [ transfer ]; | |
81 Object[] td = [ image.getImageData ]; | |
82 | |
83 clipboard.setContents(td,xfer); | |
84 } | |
85 } | |
86 }); | |
87 button = new Button(buttons, SWT.PUSH); | |
88 button.setText("Paste"); | |
89 button.addListener(SWT.Selection, new class() Listener { | |
90 public void handleEvent(Event event) { | |
91 ImageTransfer transfer = ImageTransfer.getInstance(); | |
92 ImageData imageData = cast(ImageData)clipboard.getContents(transfer); | |
93 if (imageData !is null) { | |
94 imageButton.setText(""); | |
95 Image image = imageButton.getImage(); | |
96 if (image !is null) image.dispose(); | |
97 image = new Image(display, imageData); | |
98 imageButton.setImage(image); | |
99 } else { | |
100 imageButton.setText("No image"); | |
101 } | |
102 } | |
103 }); | |
104 | |
105 button = new Button(buttons, SWT.PUSH); | |
106 button.setText("Clear"); | |
107 button.addListener(SWT.Selection, new class() Listener { | |
108 public void handleEvent(Event event) { | |
109 imageButton.setText(""); | |
110 Image image = imageButton.getImage(); | |
111 if (image !is null) image.dispose(); | |
112 imageButton.setImage(null); | |
113 } | |
114 }); | |
115 shell.pack(); | |
116 shell.open(); | |
117 while (!shell.isDisposed()) { | |
118 if (!display.readAndDispatch()) | |
119 display.sleep(); | |
120 } | |
121 display.dispose(); | |
122 } | |
123 |