Mercurial > projects > dwt2
comparison org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/custom/AnimatedProgress.d @ 25:f713da8bc051
Added SWT Linux GTK
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 20 Mar 2009 23:03:58 +0100 |
parents | |
children | fb3aa8075988 |
comparison
equal
deleted
inserted
replaced
24:b7a1d02a0e1f | 25:f713da8bc051 |
---|---|
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 */ | |
123 public synchronized void clear(){ | |
124 checkWidget(); | |
125 if (active) stop(); | |
126 showStripes = false; | |
127 redraw(); | |
128 } | |
129 public override Point computeSize(int wHint, int hHint, bool changed) { | |
130 checkWidget(); | |
131 Point size = null; | |
132 if (orientation is SWT.HORIZONTAL) { | |
133 size = new Point(DEFAULT_WIDTH, DEFAULT_HEIGHT); | |
134 } else { | |
135 size = new Point(DEFAULT_HEIGHT, DEFAULT_WIDTH); | |
136 } | |
137 if (wHint !is SWT.DEFAULT) size.x = wHint; | |
138 if (hHint !is SWT.DEFAULT) size.y = hHint; | |
139 | |
140 return size; | |
141 } | |
142 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) { | |
143 gc.setForeground(topleft); | |
144 gc.drawLine(x, y, x+w-1, y); | |
145 gc.drawLine(x, y, x, y+h-1); | |
146 | |
147 gc.setForeground(bottomright); | |
148 gc.drawLine(x+w, y, x+w, y+h); | |
149 gc.drawLine(x, y+h, x+w, y+h); | |
150 } | |
151 void paint(PaintEvent event) { | |
152 GC gc = event.gc; | |
153 Display disp= getDisplay(); | |
154 | |
155 Rectangle rect= getClientArea(); | |
156 gc.fillRectangle(rect); | |
157 if (showBorder) { | |
158 drawBevelRect(gc, rect.x, rect.y, rect.width-1, rect.height-1, | |
159 disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW), | |
160 disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW)); | |
161 } | |
162 | |
163 paintStripes(gc); | |
164 } | |
165 void paintStripes(GC gc) { | |
166 | |
167 if (!showStripes) return; | |
168 | |
169 Rectangle rect= getClientArea(); | |
170 // Subtracted border painted by paint. | |
171 rect = new Rectangle(rect.x+2, rect.y+2, rect.width-4, rect.height-4); | |
172 | |
173 gc.setLineWidth(2); | |
174 gc.setClipping(rect); | |
175 Color color = getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION); | |
176 gc.setBackground(color); | |
177 gc.fillRectangle(rect); | |
178 gc.setForeground(this.getBackground()); | |
179 int step = 12; | |
180 int foregroundValue = value is 0 ? step - 2 : value - 2; | |
181 if (orientation is SWT.HORIZONTAL) { | |
182 int y = rect.y - 1; | |
183 int w = rect.width; | |
184 int h = rect.height + 2; | |
185 for (int i= 0; i < w; i+= step) { | |
186 int x = i + foregroundValue; | |
187 gc.drawLine(x, y, x, h); | |
188 } | |
189 } else { | |
190 int x = rect.x - 1; | |
191 int w = rect.width + 2; | |
192 int h = rect.height; | |
193 | |
194 for (int i= 0; i < h; i+= step) { | |
195 int y = i + foregroundValue; | |
196 gc.drawLine(x, y, w, y); | |
197 } | |
198 } | |
199 | |
200 if (active) { | |
201 value = (value + 2) % step; | |
202 } | |
203 } | |
204 /** | |
205 * Start the animation. | |
206 * | |
207 * @exception SWTException <ul> | |
208 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
209 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
210 * </ul> | |
211 */ | |
212 public synchronized void start() { | |
213 checkWidget(); | |
214 if (active) return; | |
215 | |
216 active = true; | |
217 showStripes = true; | |
218 | |
219 Display display = getDisplay(); | |
220 Runnable [] timer = new Runnable [1]; | |
221 | |
222 timer [0] = new class( display, timer ) Runnable { | |
223 Display disp; | |
224 Runnable [] runs; | |
225 this( Display disp, Runnable[] runs ){ | |
226 this.disp = disp; | |
227 this.runs = runs; | |
228 } | |
229 public void run () { | |
230 if (!active) return; | |
231 GC gc = new GC(this.outer); | |
232 paintStripes(gc); | |
233 gc.dispose(); | |
234 disp.timerExec (SLEEP, runs [0]); | |
235 } | |
236 }; | |
237 display.timerExec (SLEEP, timer [0]); | |
238 } | |
239 /** | |
240 * Stop the animation. Freeze the presentation at its current appearance. | |
241 */ | |
242 public synchronized void stop() { | |
243 //checkWidget(); | |
244 active = false; | |
245 } | |
246 } |