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