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 import org.eclipse.swt.events.*; 18 19 37 38 public class Scale extends Control { 39 boolean ignoreResize; 40 static final int TrackBarProc; 41 static final TCHAR TrackBarClass = new TCHAR (0, OS.TRACKBAR_CLASS, true); 42 static { 43 WNDCLASS lpWndClass = new WNDCLASS (); 44 OS.GetClassInfo (0, TrackBarClass, lpWndClass); 45 TrackBarProc = lpWndClass.lpfnWndProc; 46 63 int hInstance = OS.GetModuleHandle (null); 64 int hHeap = OS.GetProcessHeap (); 65 lpWndClass.hInstance = hInstance; 66 lpWndClass.style &= ~OS.CS_GLOBALCLASS; 67 lpWndClass.style |= OS.CS_DBLCLKS; 68 int byteCount = TrackBarClass.length () * TCHAR.sizeof; 69 int lpszClassName = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 70 OS.MoveMemory (lpszClassName, TrackBarClass, byteCount); 71 lpWndClass.lpszClassName = lpszClassName; 72 OS.RegisterClass (lpWndClass); 73 OS.HeapFree (hHeap, 0, lpszClassName); 74 } 75 76 105 public Scale (Composite parent, int style) { 106 super (parent, checkStyle (style)); 107 } 108 109 132 public void addSelectionListener(SelectionListener listener) { 133 checkWidget (); 134 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 135 TypedListener typedListener = new TypedListener (listener); 136 addListener (SWT.Selection,typedListener); 137 addListener (SWT.DefaultSelection,typedListener); 138 } 139 140 int callWindowProc (int hwnd, int msg, int wParam, int lParam) { 141 if (handle == 0) return 0; 142 return OS.CallWindowProc (TrackBarProc, hwnd, msg, wParam, lParam); 143 } 144 145 static int checkStyle (int style) { 146 return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0); 147 } 148 149 public Point computeSize (int wHint, int hHint, boolean changed) { 150 checkWidget (); 151 int border = getBorderWidth (); 152 int width = border * 2, height = border * 2; 153 RECT rect = new RECT (); 154 OS.SendMessage (handle, OS.TBM_GETTHUMBRECT, 0, rect); 155 if ((style & SWT.HORIZONTAL) != 0) { 156 width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10; 157 int scrollY = OS.GetSystemMetrics (OS.SM_CYHSCROLL); 158 height += (rect.top * 2) + scrollY + (scrollY / 3); 159 } else { 160 int scrollX = OS.GetSystemMetrics (OS.SM_CXVSCROLL); 161 width += (rect.left * 2) + scrollX + (scrollX / 3); 162 height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10; 163 } 164 if (wHint != SWT.DEFAULT) width = wHint + (border * 2); 165 if (hHint != SWT.DEFAULT) height = hHint + (border * 2); 166 return new Point (width, height); 167 } 168 169 void createHandle () { 170 super.createHandle (); 171 state |= THEME_BACKGROUND | DRAW_BACKGROUND; 172 OS.SendMessage (handle, OS.TBM_SETRANGEMAX, 0, 100); 173 OS.SendMessage (handle, OS.TBM_SETPAGESIZE, 0, 10); 174 OS.SendMessage (handle, OS.TBM_SETTICFREQ, 10, 0); 175 } 176 177 int defaultForeground () { 178 return OS.GetSysColor (OS.COLOR_BTNFACE); 179 } 180 181 193 public int getIncrement () { 194 checkWidget (); 195 return OS.SendMessage (handle, OS.TBM_GETLINESIZE, 0, 0); 196 } 197 198 208 public int getMaximum () { 209 checkWidget (); 210 return OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0); 211 } 212 213 223 public int getMinimum () { 224 checkWidget (); 225 return OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0); 226 } 227 228 240 public int getPageIncrement () { 241 checkWidget (); 242 return OS.SendMessage (handle, OS.TBM_GETPAGESIZE, 0, 0); 243 } 244 245 255 public int getSelection () { 256 checkWidget (); 257 return OS.SendMessage (handle, OS.TBM_GETPOS, 0, 0); 258 } 259 260 277 public void removeSelectionListener(SelectionListener listener) { 278 checkWidget (); 279 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 280 if (eventTable == null) return; 281 eventTable.unhook (SWT.Selection, listener); 282 eventTable.unhook (SWT.DefaultSelection,listener); 283 } 284 285 void setBackgroundImage (int hImage) { 286 super.setBackgroundImage (hImage); 287 293 ignoreResize = true; 294 OS.SendMessage (handle, OS.WM_SIZE, 0, 0); 295 ignoreResize = false; 296 } 297 298 void setBackgroundPixel (int pixel) { 299 super.setBackgroundPixel (pixel); 300 306 ignoreResize = true; 307 OS.SendMessage (handle, OS.WM_SIZE, 0, 0); 308 ignoreResize = false; 309 } 310 311 324 public void setIncrement (int increment) { 325 checkWidget (); 326 if (increment < 1) return; 327 int minimum = OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0); 328 int maximum = OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0); 329 if (increment > maximum - minimum) return; 330 OS.SendMessage (handle, OS.TBM_SETLINESIZE, 0, increment); 331 } 332 333 346 public void setMaximum (int value) { 347 checkWidget (); 348 int minimum = OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0); 349 if (0 <= minimum && minimum < value) { 350 OS.SendMessage (handle, OS.TBM_SETRANGEMAX, 1, value); 351 } 352 } 353 354 367 public void setMinimum (int value) { 368 checkWidget (); 369 int maximum = OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0); 370 if (0 <= value && value < maximum) { 371 OS.SendMessage (handle, OS.TBM_SETRANGEMIN, 1, value); 372 } 373 } 374 375 388 public void setPageIncrement (int pageIncrement) { 389 checkWidget (); 390 if (pageIncrement < 1) return; 391 int minimum = OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0); 392 int maximum = OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0); 393 if (pageIncrement > maximum - minimum) return; 394 OS.SendMessage (handle, OS.TBM_SETPAGESIZE, 0, pageIncrement); 395 OS.SendMessage (handle, OS.TBM_SETTICFREQ, pageIncrement, 0); 396 } 397 398 409 public void setSelection (int value) { 410 checkWidget (); 411 OS.SendMessage (handle, OS.TBM_SETPOS, 1, value); 412 } 413 414 int widgetStyle () { 415 int bits = super.widgetStyle () | OS.WS_TABSTOP | OS.TBS_BOTH | OS.TBS_AUTOTICKS; 416 if ((style & SWT.HORIZONTAL) != 0) return bits | OS.TBS_HORZ | OS.TBS_DOWNISLEFT; 417 return bits | OS.TBS_VERT; 418 } 419 420 TCHAR windowClass () { 421 return TrackBarClass; 422 } 423 424 int windowProc () { 425 return TrackBarProc; 426 } 427 428 LRESULT WM_PAINT (int wParam, int lParam) { 429 439 boolean fixPaint = findBackgroundControl () != null; 440 if (!fixPaint) { 441 if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) { 442 Control control = findThemeControl (); 443 fixPaint = control != null; 444 } 445 } 446 if (fixPaint) { 447 boolean redraw = drawCount == 0 && OS.IsWindowVisible (handle); 448 if (redraw) OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0); 449 ignoreResize = true; 450 OS.SendMessage (handle, OS.WM_SIZE, 0, 0); 451 ignoreResize = false; 452 if (redraw) { 453 OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0); 454 OS.InvalidateRect (handle, null, false); 455 } 456 } 457 return super.WM_PAINT (wParam, lParam); 458 } 459 460 LRESULT WM_SIZE (int wParam, int lParam) { 461 if (ignoreResize) return null; 462 return super.WM_SIZE (wParam, lParam); 463 } 464 465 LRESULT wmScrollChild (int wParam, int lParam) { 466 467 468 int code = wParam & 0xFFFF; 469 switch (code) { 470 case OS.TB_ENDTRACK: 471 case OS.TB_THUMBPOSITION: 472 return null; 473 } 474 475 Event event = new Event (); 476 481 490 495 sendEvent (SWT.Selection, event); 496 return null; 498 } 499 500 } 501 | Popular Tags |