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