]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/ProgressBar.java
c761b0a2f851b137ea95d0e8e7ae25b15e6b2721
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / ProgressBar.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.internal.win32.*;
18 import org.eclipse.swt.*;
19 import org.eclipse.swt.graphics.*;
20
21 /**
22  * Instances of the receiver represent an unselectable
23  * user interface object that is used to display progress,
24  * typically in the form of a bar.
25  * <dl>
26  * <dt><b>Styles:</b></dt>
27  * <dd>SMOOTH, HORIZONTAL, VERTICAL, INDETERMINATE</dd>
28  * <dt><b>Events:</b></dt>
29  * <dd>(none)</dd>
30  * </dl>
31  * <p>
32  * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
33  * </p><p>
34  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
35  * </p>
36  *
37  * @see <a href="http://www.eclipse.org/swt/snippets/#progressbar">ProgressBar snippets</a>
38  * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
39  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
40  * @noextend This class is not intended to be subclassed by clients.
41  */
42 public class ProgressBar extends Control {
43         static final int DELAY = 100;
44         static final int TIMER_ID = 100;
45         static final int MINIMUM_WIDTH = 100;
46         static final long ProgressBarProc;
47         static final TCHAR ProgressBarClass = new TCHAR (0, OS.PROGRESS_CLASS, true);
48         static {
49                 WNDCLASS lpWndClass = new WNDCLASS ();
50                 OS.GetClassInfo (0, ProgressBarClass, lpWndClass);
51                 ProgressBarProc = lpWndClass.lpfnWndProc;
52                 /*
53                 * Feature in Windows.  The progress bar window class
54                 * does not include CS_DBLCLKS.  This means that these
55                 * controls will not get double click messages such as
56                 * WM_LBUTTONDBLCLK.  The fix is to register a new
57                 * window class with CS_DBLCLKS.
58                 *
59                 * NOTE:  Screen readers look for the exact class name
60                 * of the control in order to provide the correct kind
61                 * of assistance.  Therefore, it is critical that the
62                 * new window class have the same name.  It is possible
63                 * to register a local window class with the same name
64                 * as a global class.  Since bits that affect the class
65                 * are being changed, it is possible that other native
66                 * code, other than SWT, could create a control with
67                 * this class name, and fail unexpectedly.
68                 */
69                 lpWndClass.hInstance = OS.GetModuleHandle (null);
70                 lpWndClass.style &= ~OS.CS_GLOBALCLASS;
71                 lpWndClass.style |= OS.CS_DBLCLKS;
72                 OS.RegisterClass (ProgressBarClass, lpWndClass);
73         }
74
75 /**
76  * Constructs a new instance of this class given its parent
77  * and a style value describing its behavior and appearance.
78  * <p>
79  * The style value is either one of the style constants defined in
80  * class <code>SWT</code> which is applicable to instances of this
81  * class, or must be built by <em>bitwise OR</em>'ing together
82  * (that is, using the <code>int</code> "|" operator) two or more
83  * of those <code>SWT</code> style constants. The class description
84  * lists the style constants that are applicable to the class.
85  * Style bits are also inherited from superclasses.
86  * </p>
87  *
88  * @param parent a composite control which will be the parent of the new instance (cannot be null)
89  * @param style the style of control to construct
90  *
91  * @exception IllegalArgumentException <ul>
92  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
93  * </ul>
94  * @exception SWTException <ul>
95  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
96  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
97  * </ul>
98  *
99  * @see SWT#SMOOTH
100  * @see SWT#HORIZONTAL
101  * @see SWT#VERTICAL
102  * @see SWT#INDETERMINATE
103  * @see Widget#checkSubclass
104  * @see Widget#getStyle
105  */
106 public ProgressBar (Composite parent, int style) {
107         super (parent, checkStyle (style));
108 }
109
110 @Override
111 long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
112         if (handle == 0) return 0;
113         return OS.CallWindowProc (ProgressBarProc, hwnd, msg, wParam, lParam);
114 }
115
116 static int checkStyle (int style) {
117         style |= SWT.NO_FOCUS;
118         return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
119 }
120
121 @Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
122         checkWidget ();
123         int border = getBorderWidthInPixels ();
124         int width = border * 2, height = border * 2;
125         if ((style & SWT.HORIZONTAL) != 0) {
126                 width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10;
127                 height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
128         } else {
129                 width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
130                 height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10;
131         }
132         if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
133         if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
134         return new Point (width, height);
135 }
136
137 @Override
138 void createHandle () {
139         super.createHandle ();
140         startTimer ();
141 }
142
143 @Override
144 int defaultForeground () {
145         return OS.GetSysColor (OS.COLOR_HIGHLIGHT);
146 }
147
148 /**
149  * Returns the maximum value which the receiver will allow.
150  *
151  * @return the maximum
152  *
153  * @exception SWTException <ul>
154  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
155  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
156  * </ul>
157  */
158 public int getMaximum () {
159         checkWidget ();
160         return (int)OS.SendMessage (handle, OS.PBM_GETRANGE, 0, 0);
161 }
162
163 /**
164  * Returns the minimum value which the receiver will allow.
165  *
166  * @return the minimum
167  *
168  * @exception SWTException <ul>
169  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
170  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
171  * </ul>
172  */
173 public int getMinimum () {
174         checkWidget ();
175         return (int)OS.SendMessage (handle, OS.PBM_GETRANGE, 1, 0);
176 }
177
178 /**
179  * Returns the single 'selection' that is the receiver's position.
180  *
181  * @return the selection
182  *
183  * @exception SWTException <ul>
184  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
185  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
186  * </ul>
187  */
188 public int getSelection () {
189         checkWidget ();
190         return (int)OS.SendMessage (handle, OS.PBM_GETPOS, 0, 0);
191 }
192
193 /**
194  * Returns the state of the receiver. The value will be one of:
195  * <ul>
196  *      <li>{@link SWT#NORMAL}</li>
197  *      <li>{@link SWT#ERROR}</li>
198  *      <li>{@link SWT#PAUSED}</li>
199  * </ul>
200  *
201  * @return the state
202  *
203  * @exception SWTException <ul>
204  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
205  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
206  * </ul>
207  *
208  * @since 3.4
209  */
210 public int getState () {
211         checkWidget ();
212         int state = (int)OS.SendMessage (handle, OS.PBM_GETSTATE, 0, 0);
213         switch (state) {
214                 case OS.PBST_NORMAL: return SWT.NORMAL;
215                 case OS.PBST_ERROR: return SWT.ERROR;
216                 case OS.PBST_PAUSED: return SWT.PAUSED;
217         }
218         return SWT.NORMAL;
219 }
220
221 @Override
222 void releaseWidget () {
223         super.releaseWidget ();
224         stopTimer ();
225 }
226
227 void startTimer () {
228         if ((style & SWT.INDETERMINATE) != 0) {
229                 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
230                 if ((bits & OS.PBS_MARQUEE) == 0) {
231                         OS.SetTimer (handle, TIMER_ID, DELAY, 0);
232                 } else {
233                         OS.SendMessage (handle, OS.PBM_SETMARQUEE, 1, DELAY);
234                 }
235         }
236 }
237
238 void stopTimer () {
239         if ((style & SWT.INDETERMINATE) != 0) {
240                 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
241                 if ((bits & OS.PBS_MARQUEE) == 0) {
242                         OS.KillTimer (handle, TIMER_ID);
243                 } else {
244                         OS.SendMessage (handle, OS.PBM_SETMARQUEE, 0, 0);
245                 }
246         }
247 }
248
249 @Override
250 void setBackgroundPixel (int pixel) {
251         if (pixel == -1) pixel = OS.CLR_DEFAULT;
252         OS.SendMessage (handle, OS.PBM_SETBKCOLOR, 0, pixel);
253 }
254
255 @Override
256 void setForegroundPixel (int pixel) {
257         if (pixel == -1) pixel = OS.CLR_DEFAULT;
258         OS.SendMessage (handle, OS.PBM_SETBARCOLOR, 0, pixel);
259 }
260
261 /**
262  * Sets the maximum value that the receiver will allow.  This new
263  * value will be ignored if it is not greater than the receiver's current
264  * minimum value.  If the new maximum is applied then the receiver's
265  * selection value will be adjusted if necessary to fall within its new range.
266  *
267  * @param value the new maximum, which must be greater than the current minimum
268  *
269  * @exception SWTException <ul>
270  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
271  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
272  * </ul>
273  */
274 public void setMaximum (int value) {
275         checkWidget ();
276         int minimum = (int)OS.SendMessage (handle, OS.PBM_GETRANGE, 1, 0);
277         if (0 <= minimum && minimum < value) {
278                 OS.SendMessage (handle, OS.PBM_SETRANGE32, minimum, value);
279         }
280 }
281
282 /**
283  * Sets the minimum value that the receiver will allow.  This new
284  * value will be ignored if it is negative or is not less than the receiver's
285  * current maximum value.  If the new minimum is applied then the receiver's
286  * selection value will be adjusted if necessary to fall within its new range.
287  *
288  * @param value the new minimum, which must be nonnegative and less than the current maximum
289  *
290  * @exception SWTException <ul>
291  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
292  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
293  * </ul>
294  */
295 public void setMinimum (int value) {
296         checkWidget ();
297         int maximum = (int)OS.SendMessage (handle, OS.PBM_GETRANGE, 0, 0);
298         if (0 <= value && value < maximum) {
299                 OS.SendMessage (handle, OS.PBM_SETRANGE32, value, maximum);
300         }
301 }
302
303 /**
304  * Sets the single 'selection' that is the receiver's
305  * position to the argument which must be greater than or equal
306  * to zero.
307  *
308  * @param value the new selection (must be zero or greater)
309  *
310  * @exception SWTException <ul>
311  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
312  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
313  * </ul>
314  */
315 public void setSelection (int value) {
316         checkWidget ();
317         OS.SendMessage (handle, OS.PBM_SETPOS, value, 0);
318
319         /*
320         * Bug in Vista.  For some reason, when the progress bar is not in
321         * a normal state, it shows the selection of previous call to
322         * PBM_SETPOS. This is undocumented. The fix is to call PBM_SETPOS
323         * a second time.
324         */
325         long state = OS.SendMessage (handle, OS.PBM_GETSTATE, 0, 0);
326         if (state != OS.PBST_NORMAL) {
327                 OS.SendMessage (handle, OS.PBM_SETPOS, value, 0);
328         }
329 }
330
331 /**
332  * Sets the state of the receiver. The state must be one of these values:
333  * <ul>
334  *      <li>{@link SWT#NORMAL}</li>
335  *      <li>{@link SWT#ERROR}</li>
336  *      <li>{@link SWT#PAUSED}</li>
337  * </ul>
338  * <p>
339  * Note: This operation is a hint and is not supported on
340  * platforms that do not have this concept.
341  * </p>
342  *
343  * @param state the new state
344  *
345  * @exception SWTException <ul>
346  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
347  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
348  * </ul>
349  *
350  * @since 3.4
351  */
352 public void setState (int state) {
353         checkWidget ();
354         switch (state) {
355                 case SWT.NORMAL:
356                         OS.SendMessage (handle, OS.PBM_SETSTATE, OS.PBST_NORMAL, 0);
357                         break;
358                 case SWT.ERROR:
359                         OS.SendMessage (handle, OS.PBM_SETSTATE, OS.PBST_ERROR, 0);
360                         break;
361                 case SWT.PAUSED:
362                         OS.SendMessage (handle, OS.PBM_SETSTATE, OS.PBST_PAUSED, 0);
363                         break;
364         }
365 }
366
367 @Override
368 int widgetStyle () {
369         int bits = super.widgetStyle ();
370         if ((style & SWT.SMOOTH) != 0) bits |= OS.PBS_SMOOTH;
371         if ((style & SWT.VERTICAL) != 0) bits |= OS.PBS_VERTICAL;
372         if ((style & SWT.INDETERMINATE) != 0) bits |= OS.PBS_MARQUEE;
373         return bits;
374 }
375
376 @Override
377 TCHAR windowClass () {
378         return ProgressBarClass;
379 }
380
381 @Override
382 long windowProc () {
383         return ProgressBarProc;
384 }
385
386 @Override
387 LRESULT WM_GETDLGCODE (long wParam, long lParam) {
388         LRESULT result = super.WM_GETDLGCODE (wParam, lParam);
389         if (result != null) return result;
390         /*
391         * Feature in Windows.  The progress bar does
392         * not implement WM_GETDLGCODE.  As a result,
393         * a progress bar takes focus and takes part
394         * in tab traversal.  This behavior, while
395         * unspecified, is unwanted.  The fix is to
396         * implement WM_GETDLGCODE to behave like a
397         * STATIC control.
398         */
399         return new LRESULT (OS.DLGC_STATIC);
400 }
401
402 @Override
403 LRESULT WM_SIZE (long wParam, long lParam) {
404         LRESULT result = super.WM_SIZE (wParam, lParam);
405         if (result != null) return result;
406         /*
407         * Feature in Windows.  When a progress bar with the style
408         * PBS_MARQUEE becomes too small, the animation (currently
409         * a small bar moving from right to left) does not have
410         * enough space to draw.  The result is that the progress
411         * bar does not appear to be moving.  The fix is to detect
412         * this case, clear the PBS_MARQUEE style and emulate the
413         * animation using PBM_STEPIT.
414         *
415         * NOTE:  This only happens on Window XP.
416         */
417         if ((style & SWT.INDETERMINATE) != 0) {
418                 if (!OS.IsAppThemed()) {
419                         forceResize ();
420                         RECT rect = new RECT ();
421                         OS.GetClientRect (handle, rect);
422                         int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE);
423                         int newBits = oldBits;
424                         if (rect.right - rect.left < MINIMUM_WIDTH) {
425                                 newBits &= ~OS.PBS_MARQUEE;
426                         } else {
427                                 newBits |= OS.PBS_MARQUEE;
428                         }
429                         if (newBits != oldBits) {
430                                 stopTimer ();
431                                 OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
432                                 startTimer ();
433                         }
434                 }
435         }
436         return result;
437 }
438
439 @Override
440 LRESULT WM_TIMER (long wParam, long lParam) {
441         LRESULT result = super.WM_TIMER (wParam, lParam);
442         if (result != null) return result;
443         if ((style & SWT.INDETERMINATE) != 0) {
444                 int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
445                 if ((bits & OS.PBS_MARQUEE) == 0) {
446                         if (wParam == TIMER_ID) {
447                                 OS.SendMessage (handle, OS.PBM_STEPIT, 0, 0);
448                         }
449                 }
450         }
451         return result;
452 }
453
454 }