1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.widgets.ProgressBar;
14 
15 import java.lang.all;
16 
17 
18 
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.SWTException;
21 import org.eclipse.swt.internal.gtk.OS;
22 
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
26 
27 
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 SWT implementation.
43  * </p>
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">SWT Example: ControlExample</a>
47  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
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>SWT</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>SWT</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 SWTException <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 SWT#SMOOTH
79  * @see SWT#HORIZONTAL
80  * @see SWT#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 |= SWT.NO_FOCUS;
90     return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
91 }
92 
93 override void createHandle (int index) {
94     state |= HANDLE;
95     fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null);
96     if (fixedHandle is null) error (SWT.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 (SWT.ERROR_NO_HANDLES);
100     OS.gtk_container_add (fixedHandle, handle);
101     int orientation = ((style & SWT.VERTICAL) !is 0 ) ? OS.GTK_PROGRESS_BOTTOM_TO_TOP : OS.GTK_PROGRESS_LEFT_TO_RIGHT;
102     OS.gtk_progress_bar_set_orientation (handle, orientation);
103     if ((style & SWT.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 SWTException <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 SWTException <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 () {
134     checkWidget ();
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 SWTException <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 
153 /**
154  * Returns the state of the receiver. The value will be one of:
155  * <ul>
156  *  <li>{@link SWT#NORMAL}</li>
157  *  <li>{@link SWT#ERROR}</li>
158  *  <li>{@link SWT#PAUSED}</li>
159  * </ul>
160  *
161  * @return the state 
162  *
163  * @exception SWTException <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  */
170 public int getState () {
171     checkWidget ();
172     return SWT.NORMAL;
173 }
174 
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 
188 override void releaseWidget () {
189     super.releaseWidget ();
190     if (timerId !is 0) OS.gtk_timeout_remove (timerId);
191     timerId = 0;
192 }
193 
194 override void setParentBackground () {
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 SWTException <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 SWTException <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 SWTException <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 
263 /**
264  * Sets the state of the receiver. The state must be one of these values:
265  * <ul>
266  *  <li>{@link SWT#NORMAL}</li>
267  *  <li>{@link SWT#ERROR}</li>
268  *  <li>{@link SWT#PAUSED}</li>
269  * </ul>
270  *
271  * @param state the new state
272  *
273  * @exception SWTException <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  */
280 public void setState (int state) {
281     checkWidget ();
282     //NOT IMPLEMENTED
283 }
284 
285 override int timerProc (GtkWidget* widget) {
286     if (isVisible ()) OS.gtk_progress_bar_pulse (handle);
287     return 1;
288 }
289 
290 void updateBar (int selection, int minimum, int maximum) {
291    /*
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 }