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