1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.internal.win32.*;
18 import org.eclipse.swt.*;
19 import org.eclipse.swt.graphics.*;
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.
26 * <dt><b>Styles:</b></dt>
27 * <dd>SMOOTH, HORIZONTAL, VERTICAL, INDETERMINATE</dd>
28 * <dt><b>Events:</b></dt>
32 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
34 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
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.
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);
49 WNDCLASS lpWndClass = new WNDCLASS ();
50 OS.GetClassInfo (0, ProgressBarClass, lpWndClass);
51 ProgressBarProc = lpWndClass.lpfnWndProc;
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.
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.
69 lpWndClass.hInstance = OS.GetModuleHandle (null);
70 lpWndClass.style &= ~OS.CS_GLOBALCLASS;
71 lpWndClass.style |= OS.CS_DBLCLKS;
72 OS.RegisterClass (ProgressBarClass, lpWndClass);
76 * Constructs a new instance of this class given its parent
77 * and a style value describing its behavior and appearance.
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.
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
91 * @exception IllegalArgumentException <ul>
92 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
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>
100 * @see SWT#HORIZONTAL
102 * @see SWT#INDETERMINATE
103 * @see Widget#checkSubclass
104 * @see Widget#getStyle
106 public ProgressBar (Composite parent, int style) {
107 super (parent, checkStyle (style));
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);
116 static int checkStyle (int style) {
117 style |= SWT.NO_FOCUS;
118 return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
121 @Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
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);
129 width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
130 height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10;
132 if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
133 if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
134 return new Point (width, height);
138 void createHandle () {
139 super.createHandle ();
144 int defaultForeground () {
145 return OS.GetSysColor (OS.COLOR_HIGHLIGHT);
149 * Returns the maximum value which the receiver will allow.
151 * @return the maximum
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>
158 public int getMaximum () {
160 return (int)OS.SendMessage (handle, OS.PBM_GETRANGE, 0, 0);
164 * Returns the minimum value which the receiver will allow.
166 * @return the minimum
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>
173 public int getMinimum () {
175 return (int)OS.SendMessage (handle, OS.PBM_GETRANGE, 1, 0);
179 * Returns the single 'selection' that is the receiver's position.
181 * @return the selection
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>
188 public int getSelection () {
190 return (int)OS.SendMessage (handle, OS.PBM_GETPOS, 0, 0);
194 * Returns the state of the receiver. The value will be one of:
196 * <li>{@link SWT#NORMAL}</li>
197 * <li>{@link SWT#ERROR}</li>
198 * <li>{@link SWT#PAUSED}</li>
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>
210 public int getState () {
212 int state = (int)OS.SendMessage (handle, OS.PBM_GETSTATE, 0, 0);
214 case OS.PBST_NORMAL: return SWT.NORMAL;
215 case OS.PBST_ERROR: return SWT.ERROR;
216 case OS.PBST_PAUSED: return SWT.PAUSED;
222 void releaseWidget () {
223 super.releaseWidget ();
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);
233 OS.SendMessage (handle, OS.PBM_SETMARQUEE, 1, DELAY);
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);
244 OS.SendMessage (handle, OS.PBM_SETMARQUEE, 0, 0);
250 void setBackgroundPixel (int pixel) {
251 if (pixel == -1) pixel = OS.CLR_DEFAULT;
252 OS.SendMessage (handle, OS.PBM_SETBKCOLOR, 0, pixel);
256 void setForegroundPixel (int pixel) {
257 if (pixel == -1) pixel = OS.CLR_DEFAULT;
258 OS.SendMessage (handle, OS.PBM_SETBARCOLOR, 0, pixel);
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.
267 * @param value the new maximum, which must be greater than the current minimum
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>
274 public void setMaximum (int value) {
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);
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.
288 * @param value the new minimum, which must be nonnegative and less than the current maximum
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>
295 public void setMinimum (int value) {
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);
304 * Sets the single 'selection' that is the receiver's
305 * position to the argument which must be greater than or equal
308 * @param value the new selection (must be zero or greater)
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>
315 public void setSelection (int value) {
317 OS.SendMessage (handle, OS.PBM_SETPOS, value, 0);
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
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);
332 * Sets the state of the receiver. The state must be one of these values:
334 * <li>{@link SWT#NORMAL}</li>
335 * <li>{@link SWT#ERROR}</li>
336 * <li>{@link SWT#PAUSED}</li>
339 * Note: This operation is a hint and is not supported on
340 * platforms that do not have this concept.
343 * @param state the new state
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>
352 public void setState (int state) {
356 OS.SendMessage (handle, OS.PBM_SETSTATE, OS.PBST_NORMAL, 0);
359 OS.SendMessage (handle, OS.PBM_SETSTATE, OS.PBST_ERROR, 0);
362 OS.SendMessage (handle, OS.PBM_SETSTATE, OS.PBST_PAUSED, 0);
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;
377 TCHAR windowClass () {
378 return ProgressBarClass;
383 return ProgressBarProc;
387 LRESULT WM_GETDLGCODE (long wParam, long lParam) {
388 LRESULT result = super.WM_GETDLGCODE (wParam, lParam);
389 if (result != null) return result;
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
399 return new LRESULT (OS.DLGC_STATIC);
403 LRESULT WM_SIZE (long wParam, long lParam) {
404 LRESULT result = super.WM_SIZE (wParam, lParam);
405 if (result != null) return result;
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.
415 * NOTE: This only happens on Window XP.
417 if ((style & SWT.INDETERMINATE) != 0) {
418 if (!OS.IsAppThemed()) {
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;
427 newBits |= OS.PBS_MARQUEE;
429 if (newBits != oldBits) {
431 OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
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);