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