Mercurial > projects > dwt2
comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/custom/AnimatedProgress.d @ 0:6dd524f61e62
add dwt win and basic java stuff
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 02 Mar 2009 14:44:16 +0100 |
parents | |
children | 9f4c18c268b2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6dd524f61e62 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.custom.AnimatedProgress; | |
14 | |
15 | |
16 import org.eclipse.swt.SWT; | |
17 import org.eclipse.swt.SWTException; | |
18 import org.eclipse.swt.events.ControlAdapter; | |
19 import org.eclipse.swt.events.ControlEvent; | |
20 import org.eclipse.swt.events.DisposeEvent; | |
21 import org.eclipse.swt.events.DisposeListener; | |
22 import org.eclipse.swt.events.PaintEvent; | |
23 import org.eclipse.swt.events.PaintListener; | |
24 import org.eclipse.swt.graphics.Color; | |
25 import org.eclipse.swt.graphics.GC; | |
26 import org.eclipse.swt.graphics.Point; | |
27 import org.eclipse.swt.graphics.Rectangle; | |
28 import org.eclipse.swt.widgets.Canvas; | |
29 import org.eclipse.swt.widgets.Composite; | |
30 import org.eclipse.swt.widgets.Display; | |
31 import java.lang.Runnable; | |
32 | |
33 /** | |
34 * A control for showing progress feedback for a long running operation. | |
35 * | |
36 * @deprecated As of Eclipse 2.1, use ProgressBar with the style SWT.INDETERMINATE | |
37 * | |
38 * <dl> | |
39 * <dt><b>Styles:</b><dd>VERTICAL, HORIZONTAL, BORDER | |
40 * </dl> | |
41 * | |
42 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
43 */ | |
44 public class AnimatedProgress : Canvas { | |
45 | |
46 alias Canvas.computeSize computeSize; | |
47 | |
48 static const int SLEEP = 70; | |
49 static const int DEFAULT_WIDTH = 160; | |
50 static const int DEFAULT_HEIGHT = 18; | |
51 bool active = false; | |
52 bool showStripes = false; | |
53 int value; | |
54 int orientation = SWT.HORIZONTAL; | |
55 bool showBorder = false; | |
56 | |
57 /** | |
58 * Constructs a new instance of this class given its parent | |
59 * and a style value describing its behavior and appearance. | |
60 * <p> | |
61 * The style value is either one of the style constants defined in | |
62 * class <code>SWT</code> which is applicable to instances of this | |
63 * class, or must be built by <em>bitwise OR</em>'ing together | |
64 * (that is, using the <code>int</code> "|" operator) two or more | |
65 * of those <code>SWT</code> style constants. The class description | |
66 * lists the style constants that are applicable to the class. | |
67 * Style bits are also inherited from superclasses. | |
68 * </p> | |
69 * | |
70 * @param parent a widget which will be the parent of the new instance (cannot be null) | |
71 * @param style the style of widget to construct | |
72 * | |
73 * @exception IllegalArgumentException <ul> | |
74 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
75 * </ul> | |
76 * @exception SWTException <ul> | |
77 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
78 * </ul> | |
79 * | |
80 * @see SWT#VERTICAL | |
81 * @see SWT#HORIZONTAL | |
82 * @see SWT#BORDER | |
83 * @see #getStyle() | |
84 */ | |
85 public this(Composite parent, int style) { | |
86 super(parent, checkStyle(style)); | |
87 | |
88 if ((style & SWT.VERTICAL) !is 0) { | |
89 orientation = SWT.VERTICAL; | |
90 } | |
91 showBorder = (style & SWT.BORDER) !is 0; | |
92 | |
93 addControlListener(new class() ControlAdapter { | |
94 public void controlResized(ControlEvent e) { | |
95 redraw(); | |
96 } | |
97 }); | |
98 addPaintListener(new class() PaintListener { | |
99 public void paintControl(PaintEvent e) { | |
100 paint(e); | |
101 } | |
102 }); | |
103 addDisposeListener(new class() DisposeListener { | |
104 public void widgetDisposed(DisposeEvent e){ | |
105 stop(); | |
106 } | |
107 }); | |
108 } | |
109 private static int checkStyle (int style) { | |
110 int mask = SWT.NONE; | |
111 return style & mask; | |
112 } | |
113 /** | |
114 * Stop the animation if it is not already stopped and | |
115 * reset the presentation to a blank appearance. | |
116 * | |
117 * @exception SWTException <ul> | |
118 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
119 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
120 * </ul> | |
121 */ | |
122 public synchronized void clear(){ | |
123 checkWidget(); | |
124 if (active) stop(); | |
125 showStripes = false; | |
126 redraw(); | |
127 } | |
128 public override Point computeSize(int wHint, int hHint, bool changed) { | |
129 checkWidget(); | |
130 Point size = null; | |
131 if (orientation is SWT.HORIZONTAL) { | |
132 size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT); | |
133 } else { | |
134 size = new Point(DEFAULT_HEIGHT, DEFAULT_WIDTH); | |
135 } | |
136 if (wHint !is SWT.DEFAULT) size.x = wHint; | |
137 if (hHint !is SWT.DEFAULT) size.y = hHint; | |
138 | |
139 return size; | |
140 } | |
141 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) { | |
142 gc.setForeground(topleft); | |
143 gc.drawLine(x, y, x+w-1, y); | |
144 gc.drawLine(x, y, x, y+h-1); | |
145 | |
146 gc.setForeground(bottomright); | |
147 gc.drawLine(x+w, y, x+w, y+h); | |
148 gc.drawLine(x, y+h, x+w, y+h); | |
149 } | |
150 void paint(PaintEvent event) { | |
151 GC gc = event.gc; | |
152 Display disp= getDisplay(); | |
153 | |
154 Rectangle rect= getClientArea(); | |
155 gc.fillRectangle(rect); | |
156 if (showBorder) { | |
157 drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1, | |
158 disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW), | |
159 disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW)); | |
160 } | |
161 | |
162 paintStripes(gc); | |
163 } | |
164 void paintStripes(GC gc) { | |
165 | |
166 if (!showStripes) return; | |
167 | |
168 Rectangle rect= getClientArea(); | |
169 // Subtracted border painted by paint. | |
170 rect = new Rectangle(rect.x+2, rect.y+2, rect.width-4, rect.height-4); | |
171 | |
172 gc.setLineWidth(2); | |
173 gc.setClipping(rect); | |
174 Color color = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION); | |
175 gc.setBackground(color); | |
176 gc.fillRectangle(rect); | |
177 gc.setForeground(this.getBackground()); | |
178 int step = 12; | |
179 int foregroundValue = value is 0 ? step - 2 : value - 2; | |
180 if (orientation is SWT.HORIZONTAL) { | |
181 int y = rect.y - 1; | |
182 int w = rect.width; | |
183 int h = rect.height + 2; | |
184 for (int i= 0; i < w; i+= step) { | |
185 int x = i + foregroundValue; | |
186 gc.drawLine(x, y, x, h); | |
187 } | |
188 } else { | |
189 int x = rect.x - 1; | |
190 int w = rect.width + 2; | |
191 int h = rect.height; | |
192 | |
193 for (int i= 0; i < h; i+= step) { | |
194 int y = i + foregroundValue; | |
195 gc.drawLine(x, y, w, y); | |
196 } | |
197 } | |
198 | |
199 if (active) { | |
200 value = (value + 2) % step; | |
201 } | |
202 } | |
203 /** | |
204 * Start the animation. | |
205 * | |
206 * @exception SWTException <ul> | |
207 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
208 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
209 * </ul> | |
210 */ | |
211 public synchronized void start() { | |
212 checkWidget(); | |
213 if (active) return; | |
214 | |
215 active = true; | |
216 showStripes = true; | |
217 | |
218 Display display = getDisplay(); | |
219 Runnable [] timer = new Runnable [1]; | |
220 | |
221 timer [0] = new class( display, timer ) Runnable { | |
222 Display disp; | |
223 Runnable [] runs; | |
224 this( Display disp, Runnable[] runs ){ | |
225 this.disp = disp; | |
226 this.runs = runs; | |
227 } | |
228 public void run () { | |
229 if (!active) return; | |
230 GC gc = new GC(this.outer); | |
231 paintStripes(gc); | |
232 gc.dispose(); | |
233 disp.timerExec (SLEEP, runs [0]); | |
234 } | |
235 }; | |
236 display.timerExec (SLEEP, timer [0]); | |
237 } | |
238 /** | |
239 * Stop the animation. Freeze the presentation at its current appearance. | |
240 */ | |
241 public synchronized void stop() { | |
242 //checkWidget(); | |
243 active = false; | |
244 } | |
245 } |