Mercurial > projects > dwt-linux
annotate dwt/widgets/ProgressBar.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | ce446666f5a2 |
children | 4bffbf81e2d6 |
rev | line source |
---|---|
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
84 | 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
84 | 12 *******************************************************************************/ |
13 module dwt.widgets.ProgressBar; | |
14 | |
15 | |
16 | |
17 import dwt.DWT; | |
18 import dwt.DWTException; | |
19 import dwt.internal.gtk.OS; | |
20 | |
21 import dwt.widgets.Control; | |
22 import dwt.widgets.Composite; | |
23 import dwt.widgets.Display; | |
24 | |
25 import Math = tango.math.Math; | |
158
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
26 import tango.io.Stdout; |
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
27 |
84 | 28 /** |
29 * Instances of the receiver represent an unselectable | |
30 * user interface object that is used to display progress, | |
31 * typically in the form of a bar. | |
32 * <dl> | |
33 * <dt><b>Styles:</b></dt> | |
34 * <dd>SMOOTH, HORIZONTAL, VERTICAL, INDETERMINATE</dd> | |
35 * <dt><b>Events:</b></dt> | |
36 * <dd>(none)</dd> | |
37 * </dl> | |
38 * <p> | |
39 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified. | |
40 * </p><p> | |
41 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
42 * within the DWT implementation. | |
43 * </p> | |
259 | 44 * |
45 * @see <a href="http://www.eclipse.org/swt/snippets/#progressbar">ProgressBar snippets</a> | |
46 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a> | |
47 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
84 | 48 */ |
49 public class ProgressBar : Control { | |
50 CallbackData callbackData; | |
51 int timerId, minimum = 0, maximum = 100, selection = 0; | |
52 static const int DELAY = 100; | |
53 | |
54 /** | |
55 * Constructs a new instance of this class given its parent | |
56 * and a style value describing its behavior and appearance. | |
57 * <p> | |
58 * The style value is either one of the style constants defined in | |
59 * class <code>DWT</code> which is applicable to instances of this | |
60 * class, or must be built by <em>bitwise OR</em>'ing together | |
61 * (that is, using the <code>int</code> "|" operator) two or more | |
62 * of those <code>DWT</code> style constants. The class description | |
63 * lists the style constants that are applicable to the class. | |
64 * Style bits are also inherited from superclasses. | |
65 * </p> | |
66 * | |
67 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
68 * @param style the style of control to construct | |
69 * | |
70 * @exception IllegalArgumentException <ul> | |
71 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
72 * </ul> | |
73 * @exception DWTException <ul> | |
74 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
75 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
76 * </ul> | |
77 * | |
78 * @see DWT#SMOOTH | |
79 * @see DWT#HORIZONTAL | |
80 * @see DWT#VERTICAL | |
81 * @see Widget#checkSubclass | |
82 * @see Widget#getStyle | |
83 */ | |
84 public this (Composite parent, int style) { | |
85 super (parent, checkStyle(style)); | |
86 } | |
87 | |
88 static int checkStyle (int style) { | |
89 style |= DWT.NO_FOCUS; | |
90 return checkBits (style, DWT.HORIZONTAL, DWT.VERTICAL, 0, 0, 0, 0); | |
91 } | |
92 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
93 override void createHandle (int index) { |
84 | 94 state |= HANDLE; |
95 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); | |
96 if (fixedHandle is null) error (DWT.ERROR_NO_HANDLES); | |
97 OS.gtk_fixed_set_has_window (fixedHandle, true); | |
98 handle = OS.gtk_progress_bar_new (); | |
99 if (handle is null) error (DWT.ERROR_NO_HANDLES); | |
100 OS.gtk_container_add (fixedHandle, handle); | |
158
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
101 int orientation = ((style & DWT.VERTICAL) !is 0 ) ? OS.GTK_PROGRESS_BOTTOM_TO_TOP : OS.GTK_PROGRESS_LEFT_TO_RIGHT; |
84 | 102 OS.gtk_progress_bar_set_orientation (handle, orientation); |
103 if ((style & DWT.INDETERMINATE) !is 0) { | |
104 timerId = display.doWindowTimerAdd( &callbackData, DELAY, handle ); | |
105 } | |
106 } | |
107 | |
108 /** | |
109 * Returns the maximum value which the receiver will allow. | |
110 * | |
111 * @return the maximum | |
112 * | |
113 * @exception DWTException <ul> | |
114 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
115 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
116 * </ul> | |
117 */ | |
118 public int getMaximum () { | |
119 checkWidget (); | |
120 return maximum; | |
121 } | |
122 | |
123 /** | |
124 * Returns the minimum value which the receiver will allow. | |
125 * | |
126 * @return the minimum | |
127 * | |
128 * @exception DWTException <ul> | |
129 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
130 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
131 * </ul> | |
132 */ | |
133 public int getMinimum () { | |
159
efba9cd3ee94
removed debugging outputs from progressbar
Frank Benoit <benoit@tionex.de>
parents:
158
diff
changeset
|
134 checkWidget (); |
84 | 135 return minimum; |
136 } | |
137 | |
138 /** | |
139 * Returns the single 'selection' that is the receiver's position. | |
140 * | |
141 * @return the selection | |
142 * | |
143 * @exception DWTException <ul> | |
144 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
145 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
146 * </ul> | |
147 */ | |
148 public int getSelection () { | |
149 checkWidget (); | |
150 return selection; | |
151 } | |
152 | |
259 | 153 /** |
154 * Returns the state of the receiver. The value will be one of: | |
155 * <ul> | |
156 * <li>{@link DWT#NORMAL}</li> | |
157 * <li>{@link DWT#ERROR}</li> | |
158 * <li>{@link DWT#PAUSED}</li> | |
159 * </ul> | |
160 * | |
161 * @return the state | |
162 * | |
163 * @exception DWTException <ul> | |
164 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
165 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
166 * </ul> | |
167 * | |
168 * @since 3.4 | |
169 */ | |
240 | 170 public int getState () { |
171 checkWidget (); | |
172 return DWT.NORMAL; | |
173 } | |
174 | |
84 | 175 override int gtk_realize (GtkWidget* widget) { |
176 int result = super.gtk_realize (widget); | |
177 if (result !is 0) return result; | |
178 /* | |
179 * Bug in GTK. When a progress bar has been unrealized after being | |
180 * realized at least once, gtk_progress_bar_set_fraction() GP's. The | |
181 * fix is to update the progress bar state only when realized and restore | |
182 * the state when the progress bar becomes realized. | |
183 */ | |
184 updateBar (selection, minimum, maximum); | |
185 return 0; | |
186 } | |
187 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
188 override void releaseWidget () { |
84 | 189 super.releaseWidget (); |
190 if (timerId !is 0) OS.gtk_timeout_remove (timerId); | |
191 timerId = 0; | |
192 } | |
193 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
194 override void setParentBackground () { |
84 | 195 /* |
196 * Bug in GTK. For some reason, some theme managers will crash | |
197 * when the progress bar is inheriting the background from a parent. | |
198 * The fix is to stop inheriting the background. This is acceptable | |
199 * since progress bars do not use the inherited background. | |
200 */ | |
201 } | |
202 | |
203 /** | |
204 * Sets the maximum value that the receiver will allow. This new | |
205 * value will be ignored if it is not greater than the receiver's current | |
206 * minimum value. If the new maximum is applied then the receiver's | |
207 * selection value will be adjusted if necessary to fall within its new range. | |
208 * | |
209 * @param value the new maximum, which must be greater than the current minimum | |
210 * | |
211 * @exception DWTException <ul> | |
212 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
213 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
214 * </ul> | |
215 */ | |
216 public void setMaximum (int value) { | |
217 checkWidget (); | |
218 if (value <= minimum) return; | |
219 maximum = value; | |
220 selection = Math.min (selection, maximum); | |
221 updateBar (selection, minimum, maximum); | |
222 } | |
223 | |
224 /** | |
225 * Sets the minimum value that the receiver will allow. This new | |
226 * value will be ignored if it is negative or is not less than the receiver's | |
227 * current maximum value. If the new minimum is applied then the receiver's | |
228 * selection value will be adjusted if necessary to fall within its new range. | |
229 * | |
230 * @param value the new minimum, which must be nonnegative and less than the current maximum | |
231 * | |
232 * @exception DWTException <ul> | |
233 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
234 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
235 * </ul> | |
236 */ | |
237 public void setMinimum (int value) { | |
238 checkWidget (); | |
239 if (value < 0 || value >= maximum) return; | |
240 minimum = value; | |
241 selection = Math.max (selection, minimum); | |
242 updateBar (selection, minimum, maximum); | |
243 } | |
244 | |
245 /** | |
246 * Sets the single 'selection' that is the receiver's | |
247 * position to the argument which must be greater than or equal | |
248 * to zero. | |
249 * | |
250 * @param value the new selection (must be zero or greater) | |
251 * | |
252 * @exception DWTException <ul> | |
253 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
254 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
255 * </ul> | |
256 */ | |
257 public void setSelection (int value) { | |
258 checkWidget (); | |
259 selection = Math.max (minimum, Math.min (maximum, value)); | |
260 updateBar (selection, minimum, maximum); | |
261 } | |
262 | |
259 | 263 /** |
264 * Sets the state of the receiver. The state must be one of these values: | |
265 * <ul> | |
266 * <li>{@link DWT#NORMAL}</li> | |
267 * <li>{@link DWT#ERROR}</li> | |
268 * <li>{@link DWT#PAUSED}</li> | |
269 * </ul> | |
270 * | |
271 * @param state the new state | |
272 * | |
273 * @exception DWTException <ul> | |
274 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
275 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
276 * </ul> | |
277 * | |
278 * @since 3.4 | |
279 */ | |
240 | 280 public void setState (int state) { |
281 checkWidget (); | |
282 //NOT IMPLEMENTED | |
283 } | |
284 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
285 override int /*long*/ timerProc (GtkWidget* widget) { |
84 | 286 if (isVisible ()) OS.gtk_progress_bar_pulse (handle); |
287 return 1; | |
288 } | |
289 | |
290 void updateBar (int selection, int minimum, int maximum) { | |
158
de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
Frank Benoit <benoit@tionex.de>
parents:
152
diff
changeset
|
291 /* |
84 | 292 * Bug in GTK. When a progress bar has been unrealized after being |
293 * realized at least once, gtk_progress_bar_set_fraction() GP's. The | |
294 * fix is to update the progress bar state only when realized and restore | |
295 * the state when the progress bar becomes realized. | |
296 */ | |
297 if ((OS.GTK_WIDGET_FLAGS (handle) & OS.GTK_REALIZED) is 0) return; | |
298 | |
299 double fraction = minimum is maximum ? 1 : cast(double)(selection - minimum) / (maximum - minimum); | |
300 OS.gtk_progress_bar_set_fraction (handle, fraction); | |
301 /* | |
302 * Feature in GTK. The progress bar does | |
303 * not redraw right away when a value is | |
304 * changed. This is not strictly incorrect | |
305 * but unexpected. The fix is to force all | |
306 * outstanding redraws to be delivered. | |
307 */ | |
308 auto window = paintWindow (); | |
309 OS.gdk_window_process_updates (window, false); | |
310 OS.gdk_flush (); | |
311 } | |
312 } |