Mercurial > projects > dwt-linux
annotate dwt/widgets/ProgressBar.d @ 240:ce446666f5a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 12 May 2008 19:13:01 +0200 |
parents | efba9cd3ee94 |
children | c0d810de7093 |
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 |
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> | |
44 */ | |
45 public class ProgressBar : Control { | |
46 CallbackData callbackData; | |
47 int timerId, minimum = 0, maximum = 100, selection = 0; | |
48 static const int DELAY = 100; | |
49 | |
50 /** | |
51 * Constructs a new instance of this class given its parent | |
52 * and a style value describing its behavior and appearance. | |
53 * <p> | |
54 * The style value is either one of the style constants defined in | |
55 * class <code>DWT</code> which is applicable to instances of this | |
56 * class, or must be built by <em>bitwise OR</em>'ing together | |
57 * (that is, using the <code>int</code> "|" operator) two or more | |
58 * of those <code>DWT</code> style constants. The class description | |
59 * lists the style constants that are applicable to the class. | |
60 * Style bits are also inherited from superclasses. | |
61 * </p> | |
62 * | |
63 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
64 * @param style the style of control to construct | |
65 * | |
66 * @exception IllegalArgumentException <ul> | |
67 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
68 * </ul> | |
69 * @exception DWTException <ul> | |
70 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
71 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
72 * </ul> | |
73 * | |
74 * @see DWT#SMOOTH | |
75 * @see DWT#HORIZONTAL | |
76 * @see DWT#VERTICAL | |
77 * @see Widget#checkSubclass | |
78 * @see Widget#getStyle | |
79 */ | |
80 public this (Composite parent, int style) { | |
81 super (parent, checkStyle(style)); | |
82 } | |
83 | |
84 static int checkStyle (int style) { | |
85 style |= DWT.NO_FOCUS; | |
86 return checkBits (style, DWT.HORIZONTAL, DWT.VERTICAL, 0, 0, 0, 0); | |
87 } | |
88 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
89 override void createHandle (int index) { |
84 | 90 state |= HANDLE; |
91 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); | |
92 if (fixedHandle is null) error (DWT.ERROR_NO_HANDLES); | |
93 OS.gtk_fixed_set_has_window (fixedHandle, true); | |
94 handle = OS.gtk_progress_bar_new (); | |
95 if (handle is null) error (DWT.ERROR_NO_HANDLES); | |
96 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
|
97 int orientation = ((style & DWT.VERTICAL) !is 0 ) ? OS.GTK_PROGRESS_BOTTOM_TO_TOP : OS.GTK_PROGRESS_LEFT_TO_RIGHT; |
84 | 98 OS.gtk_progress_bar_set_orientation (handle, orientation); |
99 if ((style & DWT.INDETERMINATE) !is 0) { | |
100 timerId = display.doWindowTimerAdd( &callbackData, DELAY, handle ); | |
101 } | |
102 } | |
103 | |
104 /** | |
105 * Returns the maximum value which the receiver will allow. | |
106 * | |
107 * @return the maximum | |
108 * | |
109 * @exception DWTException <ul> | |
110 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
111 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
112 * </ul> | |
113 */ | |
114 public int getMaximum () { | |
115 checkWidget (); | |
116 return maximum; | |
117 } | |
118 | |
119 /** | |
120 * Returns the minimum value which the receiver will allow. | |
121 * | |
122 * @return the minimum | |
123 * | |
124 * @exception DWTException <ul> | |
125 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
126 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
127 * </ul> | |
128 */ | |
129 public int getMinimum () { | |
159
efba9cd3ee94
removed debugging outputs from progressbar
Frank Benoit <benoit@tionex.de>
parents:
158
diff
changeset
|
130 checkWidget (); |
84 | 131 return minimum; |
132 } | |
133 | |
134 /** | |
135 * Returns the single 'selection' that is the receiver's position. | |
136 * | |
137 * @return the selection | |
138 * | |
139 * @exception DWTException <ul> | |
140 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
141 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
142 * </ul> | |
143 */ | |
144 public int getSelection () { | |
145 checkWidget (); | |
146 return selection; | |
147 } | |
148 | |
240 | 149 public int getState () { |
150 checkWidget (); | |
151 return DWT.NORMAL; | |
152 } | |
153 | |
84 | 154 override int gtk_realize (GtkWidget* widget) { |
155 int result = super.gtk_realize (widget); | |
156 if (result !is 0) return result; | |
157 /* | |
158 * Bug in GTK. When a progress bar has been unrealized after being | |
159 * realized at least once, gtk_progress_bar_set_fraction() GP's. The | |
160 * fix is to update the progress bar state only when realized and restore | |
161 * the state when the progress bar becomes realized. | |
162 */ | |
163 updateBar (selection, minimum, maximum); | |
164 return 0; | |
165 } | |
166 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
167 override void releaseWidget () { |
84 | 168 super.releaseWidget (); |
169 if (timerId !is 0) OS.gtk_timeout_remove (timerId); | |
170 timerId = 0; | |
171 } | |
172 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
173 override void setParentBackground () { |
84 | 174 /* |
175 * Bug in GTK. For some reason, some theme managers will crash | |
176 * when the progress bar is inheriting the background from a parent. | |
177 * The fix is to stop inheriting the background. This is acceptable | |
178 * since progress bars do not use the inherited background. | |
179 */ | |
180 } | |
181 | |
182 /** | |
183 * Sets the maximum value that the receiver will allow. This new | |
184 * value will be ignored if it is not greater than the receiver's current | |
185 * minimum value. If the new maximum is applied then the receiver's | |
186 * selection value will be adjusted if necessary to fall within its new range. | |
187 * | |
188 * @param value the new maximum, which must be greater than the current minimum | |
189 * | |
190 * @exception DWTException <ul> | |
191 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
192 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
193 * </ul> | |
194 */ | |
195 public void setMaximum (int value) { | |
196 checkWidget (); | |
197 if (value <= minimum) return; | |
198 maximum = value; | |
199 selection = Math.min (selection, maximum); | |
200 updateBar (selection, minimum, maximum); | |
201 } | |
202 | |
203 /** | |
204 * Sets the minimum value that the receiver will allow. This new | |
205 * value will be ignored if it is negative or is not less than the receiver's | |
206 * current maximum value. If the new minimum 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 minimum, which must be nonnegative and less than the current maximum | |
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 setMinimum (int value) { | |
217 checkWidget (); | |
218 if (value < 0 || value >= maximum) return; | |
219 minimum = value; | |
220 selection = Math.max (selection, minimum); | |
221 updateBar (selection, minimum, maximum); | |
222 } | |
223 | |
224 /** | |
225 * Sets the single 'selection' that is the receiver's | |
226 * position to the argument which must be greater than or equal | |
227 * to zero. | |
228 * | |
229 * @param value the new selection (must be zero or greater) | |
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 setSelection (int value) { | |
237 checkWidget (); | |
238 selection = Math.max (minimum, Math.min (maximum, value)); | |
239 updateBar (selection, minimum, maximum); | |
240 } | |
241 | |
240 | 242 public void setState (int state) { |
243 checkWidget (); | |
244 //NOT IMPLEMENTED | |
245 } | |
246 | |
152
17f8449522fd
overloads second walkthrough
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
247 override int /*long*/ timerProc (GtkWidget* widget) { |
84 | 248 if (isVisible ()) OS.gtk_progress_bar_pulse (handle); |
249 return 1; | |
250 } | |
251 | |
252 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
|
253 /* |
84 | 254 * Bug in GTK. When a progress bar has been unrealized after being |
255 * realized at least once, gtk_progress_bar_set_fraction() GP's. The | |
256 * fix is to update the progress bar state only when realized and restore | |
257 * the state when the progress bar becomes realized. | |
258 */ | |
259 if ((OS.GTK_WIDGET_FLAGS (handle) & OS.GTK_REALIZED) is 0) return; | |
260 | |
261 double fraction = minimum is maximum ? 1 : cast(double)(selection - minimum) / (maximum - minimum); | |
262 OS.gtk_progress_bar_set_fraction (handle, fraction); | |
263 /* | |
264 * Feature in GTK. The progress bar does | |
265 * not redraw right away when a value is | |
266 * changed. This is not strictly incorrect | |
267 * but unexpected. The fix is to force all | |
268 * outstanding redraws to be delivered. | |
269 */ | |
270 auto window = paintWindow (); | |
271 OS.gdk_window_process_updates (window, false); | |
272 OS.gdk_flush (); | |
273 } | |
274 } |