Mercurial > projects > dwt-linux
annotate dwt/widgets/ProgressBar.d @ 158:de2578a843a7
Tango update to rev 3158, TracedException>Exception, fromUtf8z>fromStringz,Fix Bug in MenuItem Thanx to nascent for the report.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 10 Feb 2008 04:19:19 +0100 |
parents | 17f8449522fd |
children | efba9cd3ee94 |
rev | line source |
---|---|
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
84 | 2 * Copyright (c) 2000, 2007 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 | |
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 |
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
|
28 void trc( int line ){ |
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
|
29 Stdout.formatln( "ProgressBar {}", line ).flush; |
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
|
30 } |
84 | 31 |
32 /** | |
33 * Instances of the receiver represent an unselectable | |
34 * user interface object that is used to display progress, | |
35 * typically in the form of a bar. | |
36 * <dl> | |
37 * <dt><b>Styles:</b></dt> | |
38 * <dd>SMOOTH, HORIZONTAL, VERTICAL, INDETERMINATE</dd> | |
39 * <dt><b>Events:</b></dt> | |
40 * <dd>(none)</dd> | |
41 * </dl> | |
42 * <p> | |
43 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified. | |
44 * </p><p> | |
45 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
46 * within the DWT implementation. | |
47 * </p> | |
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) { |
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
|
94 trc( __LINE__ ); |
84 | 95 state |= HANDLE; |
96 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); | |
97 if (fixedHandle is null) error (DWT.ERROR_NO_HANDLES); | |
98 OS.gtk_fixed_set_has_window (fixedHandle, true); | |
99 handle = OS.gtk_progress_bar_new (); | |
100 if (handle is null) error (DWT.ERROR_NO_HANDLES); | |
101 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
|
102 int orientation = ((style & DWT.VERTICAL) !is 0 ) ? OS.GTK_PROGRESS_BOTTOM_TO_TOP : OS.GTK_PROGRESS_LEFT_TO_RIGHT; |
84 | 103 OS.gtk_progress_bar_set_orientation (handle, orientation); |
104 if ((style & DWT.INDETERMINATE) !is 0) { | |
105 timerId = display.doWindowTimerAdd( &callbackData, DELAY, handle ); | |
106 } | |
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
|
107 trc( __LINE__ ); |
84 | 108 } |
109 | |
110 /** | |
111 * Returns the maximum value which the receiver will allow. | |
112 * | |
113 * @return the maximum | |
114 * | |
115 * @exception DWTException <ul> | |
116 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
117 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
118 * </ul> | |
119 */ | |
120 public int getMaximum () { | |
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
|
121 trc( __LINE__ ); |
84 | 122 checkWidget (); |
123 return maximum; | |
124 } | |
125 | |
126 /** | |
127 * Returns the minimum value which the receiver will allow. | |
128 * | |
129 * @return the minimum | |
130 * | |
131 * @exception DWTException <ul> | |
132 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
133 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
134 * </ul> | |
135 */ | |
136 public int getMinimum () { | |
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
|
137 trc( __LINE__ ); |
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
|
138 checkWidget (); |
84 | 139 return minimum; |
140 } | |
141 | |
142 /** | |
143 * Returns the single 'selection' that is the receiver's position. | |
144 * | |
145 * @return the selection | |
146 * | |
147 * @exception DWTException <ul> | |
148 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
149 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
150 * </ul> | |
151 */ | |
152 public int getSelection () { | |
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
|
153 trc( __LINE__ ); |
84 | 154 checkWidget (); |
155 return selection; | |
156 } | |
157 | |
158 override int gtk_realize (GtkWidget* widget) { | |
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
|
159 trc( __LINE__ ); |
84 | 160 int result = super.gtk_realize (widget); |
161 if (result !is 0) return result; | |
162 /* | |
163 * Bug in GTK. When a progress bar has been unrealized after being | |
164 * realized at least once, gtk_progress_bar_set_fraction() GP's. The | |
165 * fix is to update the progress bar state only when realized and restore | |
166 * the state when the progress bar becomes realized. | |
167 */ | |
168 updateBar (selection, minimum, maximum); | |
169 return 0; | |
170 } | |
171 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
172 override void releaseWidget () { |
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
|
173 trc( __LINE__ ); |
84 | 174 super.releaseWidget (); |
175 if (timerId !is 0) OS.gtk_timeout_remove (timerId); | |
176 timerId = 0; | |
177 } | |
178 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
179 override void setParentBackground () { |
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
|
180 trc( __LINE__ ); |
84 | 181 /* |
182 * Bug in GTK. For some reason, some theme managers will crash | |
183 * when the progress bar is inheriting the background from a parent. | |
184 * The fix is to stop inheriting the background. This is acceptable | |
185 * since progress bars do not use the inherited background. | |
186 */ | |
187 } | |
188 | |
189 /** | |
190 * Sets the maximum value that the receiver will allow. This new | |
191 * value will be ignored if it is not greater than the receiver's current | |
192 * minimum value. If the new maximum is applied then the receiver's | |
193 * selection value will be adjusted if necessary to fall within its new range. | |
194 * | |
195 * @param value the new maximum, which must be greater than the current minimum | |
196 * | |
197 * @exception DWTException <ul> | |
198 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
199 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
200 * </ul> | |
201 */ | |
202 public void setMaximum (int value) { | |
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
|
203 trc( __LINE__ ); |
84 | 204 checkWidget (); |
205 if (value <= minimum) return; | |
206 maximum = value; | |
207 selection = Math.min (selection, maximum); | |
208 updateBar (selection, minimum, maximum); | |
209 } | |
210 | |
211 /** | |
212 * Sets the minimum value that the receiver will allow. This new | |
213 * value will be ignored if it is negative or is not less than the receiver's | |
214 * current maximum value. If the new minimum is applied then the receiver's | |
215 * selection value will be adjusted if necessary to fall within its new range. | |
216 * | |
217 * @param value the new minimum, which must be nonnegative and less than the current maximum | |
218 * | |
219 * @exception DWTException <ul> | |
220 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
221 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
222 * </ul> | |
223 */ | |
224 public void setMinimum (int value) { | |
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
|
225 trc( __LINE__ ); |
84 | 226 checkWidget (); |
227 if (value < 0 || value >= maximum) return; | |
228 minimum = value; | |
229 selection = Math.max (selection, minimum); | |
230 updateBar (selection, minimum, maximum); | |
231 } | |
232 | |
233 /** | |
234 * Sets the single 'selection' that is the receiver's | |
235 * position to the argument which must be greater than or equal | |
236 * to zero. | |
237 * | |
238 * @param value the new selection (must be zero or greater) | |
239 * | |
240 * @exception DWTException <ul> | |
241 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
242 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
243 * </ul> | |
244 */ | |
245 public void setSelection (int value) { | |
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
|
246 trc( __LINE__ ); |
84 | 247 checkWidget (); |
248 selection = Math.max (minimum, Math.min (maximum, value)); | |
249 updateBar (selection, minimum, 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
|
250 trc( __LINE__ ); |
84 | 251 } |
252 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
253 override int /*long*/ timerProc (GtkWidget* widget) { |
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
|
254 trc( __LINE__ ); |
84 | 255 if (isVisible ()) OS.gtk_progress_bar_pulse (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
|
256 trc( __LINE__ ); |
84 | 257 return 1; |
258 } | |
259 | |
260 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
|
261 trc( __LINE__ ); |
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
|
262 /* |
84 | 263 * Bug in GTK. When a progress bar has been unrealized after being |
264 * realized at least once, gtk_progress_bar_set_fraction() GP's. The | |
265 * fix is to update the progress bar state only when realized and restore | |
266 * the state when the progress bar becomes realized. | |
267 */ | |
268 if ((OS.GTK_WIDGET_FLAGS (handle) & OS.GTK_REALIZED) is 0) return; | |
269 | |
270 double fraction = minimum is maximum ? 1 : cast(double)(selection - minimum) / (maximum - minimum); | |
271 OS.gtk_progress_bar_set_fraction (handle, fraction); | |
272 /* | |
273 * Feature in GTK. The progress bar does | |
274 * not redraw right away when a value is | |
275 * changed. This is not strictly incorrect | |
276 * but unexpected. The fix is to force all | |
277 * outstanding redraws to be delivered. | |
278 */ | |
279 auto window = paintWindow (); | |
280 OS.gdk_window_process_updates (window, false); | |
281 OS.gdk_flush (); | |
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
|
282 trc( __LINE__ ); |
84 | 283 } |
284 } |