KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > ProgressBar


1 /*******************************************************************************
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
10  *******************************************************************************/

11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Instances of the receiver represent an unselectable
20  * user interface object that is used to display progress,
21  * typically in the form of a bar.
22  * <dl>
23  * <dt><b>Styles:</b></dt>
24  * <dd>SMOOTH, HORIZONTAL, VERTICAL, INDETERMINATE</dd>
25  * <dt><b>Events:</b></dt>
26  * <dd>(none)</dd>
27  * </dl>
28  * <p>
29  * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
30  * </p><p>
31  * IMPORTANT: This class is intended to be subclassed <em>only</em>
32  * within the SWT implementation.
33  * </p>
34  */

35 public class ProgressBar extends Control {
36     static final int DELAY = 100;
37     static final int TIMER_ID = 100;
38     static final int MINIMUM_WIDTH = 100;
39     static final int ProgressBarProc;
40     static final TCHAR ProgressBarClass = new TCHAR (0, OS.PROGRESS_CLASS, true);
41     static {
42         WNDCLASS lpWndClass = new WNDCLASS ();
43         OS.GetClassInfo (0, ProgressBarClass, lpWndClass);
44         ProgressBarProc = lpWndClass.lpfnWndProc;
45         /*
46         * Feature in Windows. The progress bar window class
47         * does not include CS_DBLCLKS. This mean that these
48         * controls will not get double click messages such as
49         * WM_LBUTTONDBLCLK. The fix is to register a new
50         * window class with CS_DBLCLKS.
51         *
52         * NOTE: Screen readers look for the exact class name
53         * of the control in order to provide the correct kind
54         * of assistance. Therefore, it is critical that the
55         * new window class have the same name. It is possible
56         * to register a local window class with the same name
57         * as a global class. Since bits that affect the class
58         * are being changed, it is possible that other native
59         * code, other than SWT, could create a control with
60         * this class name, and fail unexpectedly.
61         */

62         int hInstance = OS.GetModuleHandle (null);
63         int hHeap = OS.GetProcessHeap ();
64         lpWndClass.hInstance = hInstance;
65         lpWndClass.style &= ~OS.CS_GLOBALCLASS;
66         lpWndClass.style |= OS.CS_DBLCLKS;
67         int byteCount = ProgressBarClass.length () * TCHAR.sizeof;
68         int lpszClassName = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
69         OS.MoveMemory (lpszClassName, ProgressBarClass, byteCount);
70         lpWndClass.lpszClassName = lpszClassName;
71         OS.RegisterClass (lpWndClass);
72         OS.HeapFree (hHeap, 0, lpszClassName);
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 Widget#checkSubclass
103  * @see Widget#getStyle
104  */

105 public ProgressBar (Composite parent, int style) {
106     super (parent, checkStyle (style));
107 }
108
109 int callWindowProc (int hwnd, int msg, int wParam, int lParam) {
110     if (handle == 0) return 0;
111     return OS.CallWindowProc (ProgressBarProc, hwnd, msg, wParam, lParam);
112 }
113
114 static int checkStyle (int style) {
115     style |= SWT.NO_FOCUS;
116     return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
117 }
118
119 public Point computeSize (int wHint, int hHint, boolean changed) {
120     checkWidget ();
121     int border = getBorderWidth ();
122     int width = border * 2, height = border * 2;
123     if ((style & SWT.HORIZONTAL) != 0) {
124         width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10;
125         height += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
126     } else {
127         width += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
128         height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10;
129     }
130     if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
131     if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
132     return new Point (width, height);
133 }
134
135 void createHandle () {
136     super.createHandle ();
137     startTimer ();
138 }
139
140 int defaultForeground () {
141     return OS.GetSysColor (OS.COLOR_HIGHLIGHT);
142 }
143
144 /**
145  * Returns the maximum value which the receiver will allow.
146  *
147  * @return the maximum
148  *
149  * @exception SWTException <ul>
150  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
151  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
152  * </ul>
153  */

154 public int getMaximum () {
155     checkWidget ();
156     return OS.SendMessage (handle, OS.PBM_GETRANGE, 0, 0);
157 }
158
159 /**
160  * Returns the minimum value which the receiver will allow.
161  *
162  * @return the minimum
163  *
164  * @exception SWTException <ul>
165  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
166  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
167  * </ul>
168  */

169 public int getMinimum () {
170     checkWidget ();
171     return OS.SendMessage (handle, OS.PBM_GETRANGE, 1, 0);
172 }
173
174 /**
175  * Returns the single 'selection' that is the receiver's position.
176  *
177  * @return the selection
178  *
179  * @exception SWTException <ul>
180  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
181  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
182  * </ul>
183  */

184 public int getSelection () {
185     checkWidget ();
186     return OS.SendMessage (handle, OS.PBM_GETPOS, 0, 0);
187 }
188
189 void releaseWidget () {
190     super.releaseWidget ();
191     stopTimer ();
192 }
193
194 void startTimer () {
195     if ((style & SWT.INDETERMINATE) != 0) {
196         int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
197         if (OS.COMCTL32_MAJOR < 6 || (bits & OS.PBS_MARQUEE) == 0) {
198             OS.SetTimer (handle, TIMER_ID, DELAY, 0);
199         } else {
200             OS.SendMessage (handle, OS.PBM_SETMARQUEE, 1, DELAY);
201         }
202     }
203 }
204
205 void stopTimer () {
206     if ((style & SWT.INDETERMINATE) != 0) {
207         int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
208         if (OS.COMCTL32_MAJOR < 6 || (bits & OS.PBS_MARQUEE) == 0) {
209             OS.KillTimer (handle, TIMER_ID);
210         } else {
211             OS.SendMessage (handle, OS.PBM_SETMARQUEE, 0, 0);
212         }
213     }
214 }
215
216 void setBackgroundPixel (int pixel) {
217     if (pixel == -1) pixel = OS.CLR_DEFAULT;
218     OS.SendMessage (handle, OS.PBM_SETBKCOLOR, 0, pixel);
219 }
220
221 void setForegroundPixel (int pixel) {
222     if (pixel == -1) pixel = OS.CLR_DEFAULT;
223     OS.SendMessage (handle, OS.PBM_SETBARCOLOR, 0, pixel);
224 }
225
226 /**
227  * Sets the maximum value that the receiver will allow. This new
228  * value will be ignored if it is not greater than the receiver's current
229  * minimum value. If the new maximum is applied then the receiver's
230  * selection value will be adjusted if necessary to fall within its new range.
231  *
232  * @param value the new maximum, which must be greater than the current minimum
233  *
234  * @exception SWTException <ul>
235  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
236  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
237  * </ul>
238  */

239 public void setMaximum (int value) {
240     checkWidget ();
241     int minimum = OS.SendMessage (handle, OS.PBM_GETRANGE, 1, 0);
242     if (0 <= minimum && minimum < value) {
243         OS.SendMessage (handle, OS.PBM_SETRANGE32, minimum, value);
244     }
245 }
246
247 /**
248  * Sets the minimum value that the receiver will allow. This new
249  * value will be ignored if it is negative or is not less than the receiver's
250  * current maximum value. If the new minimum is applied then the receiver's
251  * selection value will be adjusted if necessary to fall within its new range.
252  *
253  * @param value the new minimum, which must be nonnegative and less than the current maximum
254  *
255  * @exception SWTException <ul>
256  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
257  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
258  * </ul>
259  */

260 public void setMinimum (int value) {
261     checkWidget ();
262     int maximum = OS.SendMessage (handle, OS.PBM_GETRANGE, 0, 0);
263     if (0 <= value && value < maximum) {
264         OS.SendMessage (handle, OS.PBM_SETRANGE32, value, maximum);
265     }
266 }
267
268 /**
269  * Sets the single 'selection' that is the receiver's
270  * position to the argument which must be greater than or equal
271  * to zero.
272  *
273  * @param value the new selection (must be zero or greater)
274  *
275  * @exception SWTException <ul>
276  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
277  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
278  * </ul>
279  */

280 public void setSelection (int value) {
281     checkWidget ();
282     OS.SendMessage (handle, OS.PBM_SETPOS, value, 0);
283 }
284
285 int widgetStyle () {
286     int bits = super.widgetStyle ();
287     if ((style & SWT.SMOOTH) != 0) bits |= OS.PBS_SMOOTH;
288     if ((style & SWT.VERTICAL) != 0) bits |= OS.PBS_VERTICAL;
289     if ((style & SWT.INDETERMINATE) != 0) bits |= OS.PBS_MARQUEE;
290     return bits;
291 }
292
293 TCHAR windowClass () {
294     return ProgressBarClass;
295 }
296
297 int windowProc () {
298     return ProgressBarProc;
299 }
300
301 LRESULT WM_GETDLGCODE (int wParam, int lParam) {
302     LRESULT result = super.WM_GETDLGCODE (wParam, lParam);
303     if (result != null) return result;
304     /*
305     * Feature in Windows. The progress bar does
306     * not implement WM_GETDLGCODE. As a result,
307     * a progress bar takes focus and takes part
308     * in tab traversal. This behavior, while
309     * unspecified, is unwanted. The fix is to
310     * implement WM_GETDLGCODE to behave like a
311     * STATIC control.
312     */

313     return new LRESULT (OS.DLGC_STATIC);
314 }
315
316 LRESULT WM_SIZE (int wParam, int lParam) {
317     LRESULT result = super.WM_SIZE (wParam, lParam);
318     if (result != null) return result;
319     /*
320     * Feature in Windows. When a progress bar with the style
321     * PBS_MARQUEE becomes too small, the animation (currently
322     * a small bar moving from right to left) does not have
323     * enough space to draw. The result is that the progress
324     * bar does not appear to be moving. The fix is to detect
325     * this case, clear the PBS_MARQUEE style and emulate the
326     * animation using PBM_STEPIT.
327     *
328     * NOTE: This only happens on Window XP.
329     */

330     if ((style & SWT.INDETERMINATE) != 0) {
331         if (OS.COMCTL32_MAJOR >= 6) {
332             forceResize ();
333             RECT rect = new RECT ();
334             OS.GetClientRect (handle, rect);
335             int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE);
336             int newBits = oldBits;
337             if (rect.right - rect.left < MINIMUM_WIDTH) {
338                 newBits &= ~OS.PBS_MARQUEE;
339             } else {
340                 newBits |= OS.PBS_MARQUEE;
341             }
342             if (newBits != oldBits) {
343                 stopTimer ();
344                 OS.SetWindowLong (handle, OS.GWL_STYLE, newBits);
345                 startTimer ();
346             }
347         }
348     }
349     return result;
350 }
351
352 LRESULT WM_TIMER (int wParam, int lParam) {
353     LRESULT result = super.WM_TIMER (wParam, lParam);
354     if (result != null) return result;
355     if ((style & SWT.INDETERMINATE) != 0) {
356         int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
357         if (OS.COMCTL32_MAJOR < 6 || (bits & OS.PBS_MARQUEE) == 0) {
358             if (wParam == TIMER_ID) {
359                 OS.SendMessage (handle, OS.PBM_STEPIT, 0, 0);
360             }
361         }
362     }
363     return result;
364 }
365
366 }
367
Popular Tags