Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet288.d @ 112:9f4c18c268b2
Update to compile and execute with dmd 2.052.
author | kntroh |
---|---|
date | Wed, 16 Mar 2011 21:53:53 +0900 |
parents | 8ae65ae167f5 |
children | 536e43f63c81 |
rev | line source |
---|---|
28 | 1 /******************************************************************************* |
2 * Copyright (c) 2008 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 *******************************************************************************/ | |
11 module org.eclipse.swt.snippets.Snippet288; | |
12 | |
13 /* | |
14 * Create a ToolBar containing animated GIFs | |
15 * | |
16 * For a list of all SWT example snippets see | |
17 * http://www.eclipse.org/swt/snippets/ | |
18 */ | |
19 import org.eclipse.swt.SWT; | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
20 import org.eclipse.swt.SWTException; |
28 | 21 import org.eclipse.swt.graphics.GC; |
22 import org.eclipse.swt.graphics.Color; | |
23 import org.eclipse.swt.graphics.Image; | |
24 import org.eclipse.swt.graphics.ImageLoader; | |
25 import org.eclipse.swt.graphics.ImageData; | |
26 import org.eclipse.swt.widgets.Display; | |
27 import org.eclipse.swt.widgets.Shell; | |
28 import org.eclipse.swt.widgets.ToolBar; | |
29 import org.eclipse.swt.widgets.ToolItem; | |
30 import org.eclipse.swt.widgets.FileDialog; | |
31 | |
30
93b0e7382fd5
SWT Snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
32 import java.lang.all; |
93b0e7382fd5
SWT Snippets build on windows
Frank Benoit <benoit@tionex.de>
parents:
29
diff
changeset
|
33 |
112 | 34 version(Tango){ |
35 import tango.io.FilePath; | |
36 import tango.io.model.IFile; | |
37 //import tango.core.Thread; | |
38 import tango.io.Stdout; | |
39 import tango.util.Convert; | |
40 import tango.core.Exception; | |
41 } else { // Phobos | |
42 import std.path; | |
43 import std.stream; | |
44 import std.stdio; | |
45 import std.conv; | |
46 import core.exception; | |
47 import core.thread : ThreadException; | |
48 struct FileConst { | |
49 static const PathSeparatorChar = sep; | |
50 } | |
51 } | |
28 | 52 |
53 static Display display; | |
54 static Shell shell; | |
55 static GC shellGC; | |
56 static Color shellBackground; | |
57 static ImageLoader[] loader; | |
58 static ImageData[][] imageDataArray; | |
59 static Thread[] animateThread; | |
60 static Image[][] image; | |
61 private static ToolItem[] item; | |
112 | 62 static const bool useGIFBackground = false; |
28 | 63 |
64 void main () { | |
65 display = new Display(); | |
66 Shell shell = new Shell (display); | |
67 shellBackground = shell.getBackground(); | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
68 FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI); |
28 | 69 dialog.setText("Select Multiple Animated GIFs"); |
70 dialog.setFilterExtensions(["*.gif"]); | |
112 | 71 String filename = dialog.open(); |
72 String[] filenames = dialog.getFileNames(); | |
28 | 73 int numToolBarItems = filenames.length; |
74 if (numToolBarItems > 0) { | |
112 | 75 version(Tango){ |
76 try { | |
77 loadAllImages((new FilePath(filename)).parent, filenames); | |
78 } catch (SWTException e) { | |
79 Stdout.print("There was an error loading an image.").newline; | |
80 e.printStackTrace(); | |
81 } | |
82 } else { // Phobos | |
83 try { | |
84 loadAllImages(filename.getDirName, filenames); | |
85 } catch (SWTException e) { | |
86 writeln("There was an error loading an image."); | |
87 e.printStackTrace(); | |
88 } | |
28 | 89 } |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
90 ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER | SWT.WRAP); |
28 | 91 item = new ToolItem[numToolBarItems]; |
92 for (int i = 0; i < numToolBarItems; i++) { | |
93 item[i] = new ToolItem (toolBar, SWT.PUSH); | |
94 item[i].setImage(image[i][0]); | |
95 } | |
96 toolBar.pack (); | |
97 shell.open (); | |
98 | |
99 startAnimationThreads(); | |
100 | |
101 while (!shell.isDisposed()) { | |
102 if (!display.readAndDispatch ()) display.sleep (); | |
103 } | |
104 | |
105 for (int i = 0; i < numToolBarItems; i++) { | |
106 for (int j = 0; j < image[i].length; j++) { | |
107 image[i][j].dispose(); | |
108 } | |
109 } | |
110 display.dispose (); | |
111 } | |
109
8ae65ae167f5
Updated swt win32 for DMD 1.063 and latest Tango trunk.
Jacob Carlborg <doob@me.com>
parents:
30
diff
changeset
|
112 Thread.joinAll(); |
28 | 113 } |
114 | |
112 | 115 private static void loadAllImages(String directory, String[] filenames) { |
28 | 116 int numItems = filenames.length; |
117 loader.length = numItems; | |
118 imageDataArray.length = numItems; | |
119 image.length = numItems; | |
120 for (int i = 0; i < numItems; i++) { | |
121 loader[i] = new ImageLoader(); | |
122 int fullWidth = loader[i].logicalScreenWidth; | |
123 int fullHeight = loader[i].logicalScreenHeight; | |
124 imageDataArray[i] = loader[i].load(directory ~ FileConst.PathSeparatorChar ~ filenames[i]); | |
125 int numFramesOfAnimation = imageDataArray[i].length; | |
126 image[i] = new Image[numFramesOfAnimation]; | |
127 for (int j = 0; j < numFramesOfAnimation; j++) { | |
128 if (j == 0) { | |
129 //for the first frame of animation, just draw the first frame | |
130 image[i][j] = new Image(display, imageDataArray[i][j]); | |
131 fullWidth = imageDataArray[i][j].width; | |
132 fullHeight = imageDataArray[i][j].height; | |
133 } | |
134 else { | |
135 //after the first frame of animation, draw the background or previous frame first, then the new image data | |
136 image[i][j] = new Image(display, fullWidth, fullHeight); | |
137 GC gc = new GC(image[i][j]); | |
138 gc.setBackground(shellBackground); | |
139 gc.fillRectangle(0, 0, fullWidth, fullHeight); | |
140 ImageData imageData = imageDataArray[i][j]; | |
141 switch (imageData.disposalMethod) { | |
142 case SWT.DM_FILL_BACKGROUND: | |
143 /* Fill with the background color before drawing. */ | |
144 Color bgColor = null; | |
145 if (useGIFBackground && loader[i].backgroundPixel != -1) { | |
146 bgColor = new Color(display, imageData.palette.getRGB(loader[i].backgroundPixel)); | |
147 } | |
148 gc.setBackground(bgColor !is null ? bgColor : shellBackground); | |
149 gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height); | |
150 if (bgColor !is null) bgColor.dispose(); | |
151 break; | |
152 default: | |
153 /* Restore the previous image before drawing. */ | |
154 gc.drawImage( | |
155 image[i][j-1], | |
156 0, | |
157 0, | |
158 fullWidth, | |
159 fullHeight, | |
160 0, | |
161 0, | |
162 fullWidth, | |
163 fullHeight); | |
164 break; | |
165 } | |
166 Image newFrame = new Image(display, imageData); | |
167 gc.drawImage(newFrame, | |
168 0, | |
169 0, | |
170 imageData.width, | |
171 imageData.height, | |
172 imageData.x, | |
173 imageData.y, | |
174 imageData.width, | |
175 imageData.height); | |
176 newFrame.dispose(); | |
177 gc.dispose(); | |
178 } | |
179 } | |
180 } | |
181 } | |
182 | |
183 private static void startAnimationThreads() { | |
184 animateThread = new Thread[image.length]; | |
185 for (int ii = 0; ii < image.length; ii++) { | |
186 animateThread[ii] = new class(ii) Thread { | |
187 int imageDataIndex = 0; | |
188 int id = 0; | |
189 this(int _id) { | |
190 id = _id; | |
109
8ae65ae167f5
Updated swt win32 for DMD 1.063 and latest Tango trunk.
Jacob Carlborg <doob@me.com>
parents:
30
diff
changeset
|
191 //name = "Animation "~to!(char[])(ii); |
8ae65ae167f5
Updated swt win32 for DMD 1.063 and latest Tango trunk.
Jacob Carlborg <doob@me.com>
parents:
30
diff
changeset
|
192 //isDaemon = true; |
28 | 193 super(&run); |
194 } | |
195 void run() { | |
196 try { | |
197 int repeatCount = loader[id].repeatCount; | |
198 while (loader[id].repeatCount == 0 || repeatCount > 0) { | |
199 imageDataIndex = (imageDataIndex + 1) % imageDataArray[id].length; | |
200 if (!display.isDisposed()) { | |
201 display.asyncExec(new class Runnable { | |
202 public void run() { | |
203 if (!item[id].isDisposed()) | |
204 item[id].setImage(image[id][imageDataIndex]); | |
205 } | |
206 }); | |
207 } | |
208 | |
209 /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */ | |
210 try { | |
211 int ms = imageDataArray[id][imageDataIndex].delayTime * 10; | |
212 if (ms < 20) ms += 30; | |
213 if (ms < 30) ms += 10; | |
109
8ae65ae167f5
Updated swt win32 for DMD 1.063 and latest Tango trunk.
Jacob Carlborg <doob@me.com>
parents:
30
diff
changeset
|
214 Thread.sleep(1 * ms); |
28 | 215 } catch (ThreadException e) { |
216 } | |
217 | |
218 /* If we have just drawn the last image, decrement the repeat count and start again. */ | |
219 if (imageDataIndex == imageDataArray[id].length - 1) repeatCount--; | |
220 } | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
221 } catch (SWTException ex) { |
112 | 222 version(Tango){ |
223 Stdout.print("There was an error animating the GIF").newline; | |
224 } else { // Phobos | |
225 writeln("There was an error animating the GIF"); | |
226 } | |
28 | 227 ex.printStackTrace(); |
228 } | |
229 } | |
230 }; | |
231 animateThread[ii].start(); | |
232 } | |
233 } | |
234 |