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