1 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 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 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 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 154 public int getMaximum () { 155 checkWidget (); 156 return OS.SendMessage (handle, OS.PBM_GETRANGE, 0, 0); 157 } 158 159 169 public int getMinimum () { 170 checkWidget (); 171 return OS.SendMessage (handle, OS.PBM_GETRANGE, 1, 0); 172 } 173 174 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 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 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 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 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 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 |