comparison dwt/custom/AnimatedProgress.d @ 41:6337764516f1

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