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 67 public class Slider extends Control { 68 int increment, pageIncrement; 69 boolean ignoreFocus; 70 static final int ScrollBarProc; 71 static final TCHAR ScrollBarClass = new TCHAR (0, "SCROLLBAR", true); 72 static { 73 WNDCLASS lpWndClass = new WNDCLASS (); 74 OS.GetClassInfo (0, ScrollBarClass, lpWndClass); 75 ScrollBarProc = lpWndClass.lpfnWndProc; 76 } 77 78 107 public Slider (Composite parent, int style) { 108 super (parent, checkStyle (style)); 109 } 110 111 143 public void addSelectionListener (SelectionListener listener) { 144 checkWidget (); 145 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 146 TypedListener typedListener = new TypedListener(listener); 147 addListener (SWT.Selection,typedListener); 148 addListener (SWT.DefaultSelection,typedListener); 149 } 150 151 int callWindowProc (int hwnd, int msg, int wParam, int lParam) { 152 if (handle == 0) return 0; 153 161 switch (msg) { 162 case OS.WM_LBUTTONDOWN: 163 case OS.WM_LBUTTONDBLCLK: 164 display.runDeferredEvents (); 165 } 166 return OS.CallWindowProc (ScrollBarProc, hwnd, msg, wParam, lParam); 167 } 168 169 static int checkStyle (int style) { 170 return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0); 171 } 172 173 public Point computeSize (int wHint, int hHint, boolean changed) { 174 checkWidget (); 175 int border = getBorderWidth (); 176 int width = border * 2, height = border * 2; 177 if ((style & SWT.HORIZONTAL) != 0) { 178 width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10; 179 height += OS.GetSystemMetrics (OS.SM_CYHSCROLL); 180 } else { 181 width += OS.GetSystemMetrics (OS.SM_CXVSCROLL); 182 height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10; 183 } 184 if (wHint != SWT.DEFAULT) width = wHint + (border * 2); 185 if (hHint != SWT.DEFAULT) height = hHint + (border * 2); 186 return new Point (width, height); 187 } 188 189 void createWidget () { 190 super.createWidget (); 191 increment = 1; 192 pageIncrement = 10; 193 199 SCROLLINFO info = new SCROLLINFO (); 200 info.cbSize = SCROLLINFO.sizeof; 201 info.fMask = OS.SIF_ALL; 202 info.nMax = 100; 203 info.nPage = 11; 204 OS.SetScrollInfo (handle, OS.SB_CTL, info, true); 205 } 206 207 int defaultBackground () { 208 return OS.GetSysColor (OS.COLOR_SCROLLBAR); 209 } 210 211 int defaultForeground () { 212 return OS.GetSysColor (OS.COLOR_BTNFACE); 213 } 214 215 void enableWidget (boolean enabled) { 216 super.enableWidget (enabled); 217 if (!OS.IsWinCE) { 218 int flags = enabled ? OS.ESB_ENABLE_BOTH : OS.ESB_DISABLE_BOTH; 219 OS.EnableScrollBar (handle, OS.SB_CTL, flags); 220 } 221 if (enabled) { 222 state &= ~DISABLED; 223 } else { 224 state |= DISABLED; 225 } 226 } 227 228 public boolean getEnabled () { 229 checkWidget (); 230 return (state & DISABLED) == 0; 231 } 232 233 245 public int getIncrement () { 246 checkWidget (); 247 return increment; 248 } 249 250 260 public int getMaximum () { 261 checkWidget (); 262 SCROLLINFO info = new SCROLLINFO (); 263 info.cbSize = SCROLLINFO.sizeof; 264 info.fMask = OS.SIF_RANGE; 265 OS.GetScrollInfo (handle, OS.SB_CTL, info); 266 return info.nMax; 267 } 268 269 279 public int getMinimum () { 280 checkWidget (); 281 SCROLLINFO info = new SCROLLINFO (); 282 info.cbSize = SCROLLINFO.sizeof; 283 info.fMask = OS.SIF_RANGE; 284 OS.GetScrollInfo (handle, OS.SB_CTL, info); 285 return info.nMin; 286 } 287 288 300 public int getPageIncrement () { 301 checkWidget (); 302 return pageIncrement; 303 } 304 305 315 public int getSelection () { 316 checkWidget (); 317 SCROLLINFO info = new SCROLLINFO (); 318 info.cbSize = SCROLLINFO.sizeof; 319 info.fMask = OS.SIF_POS; 320 OS.GetScrollInfo (handle, OS.SB_CTL, info); 321 return info.nPos; 322 } 323 324 335 public int getThumb () { 336 checkWidget (); 337 SCROLLINFO info = new SCROLLINFO (); 338 info.cbSize = SCROLLINFO.sizeof; 339 info.fMask = OS.SIF_PAGE; 340 OS.GetScrollInfo (handle, OS.SB_CTL, info); 341 if (info.nPage != 0) --info.nPage; 342 return info.nPage; 343 } 344 345 362 public void removeSelectionListener (SelectionListener listener) { 363 checkWidget (); 364 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 365 if (eventTable == null) return; 366 eventTable.unhook (SWT.Selection, listener); 367 eventTable.unhook (SWT.DefaultSelection,listener); 368 } 369 370 void setBounds (int x, int y, int width, int height, int flags) { 371 super.setBounds (x, y, width, height, flags); 372 378 if (OS.GetFocus () == handle) { 379 ignoreFocus = true; 380 OS.SendMessage (handle, OS.WM_SETFOCUS, 0, 0); 381 ignoreFocus = false; 382 } 383 } 384 385 398 public void setIncrement (int value) { 399 checkWidget (); 400 if (value < 1) return; 401 increment = value; 402 } 403 404 417 public void setMaximum (int value) { 418 checkWidget (); 419 if (value < 0) return; 420 SCROLLINFO info = new SCROLLINFO (); 421 info.cbSize = SCROLLINFO.sizeof; 422 info.fMask = OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 423 OS.GetScrollInfo (handle, OS.SB_CTL, info); 424 if (value - info.nMin - info.nPage < 1) return; 425 info.nMax = value; 426 SetScrollInfo (handle, OS.SB_CTL, info, true); 427 } 428 429 442 public void setMinimum (int value) { 443 checkWidget (); 444 if (value < 0) return; 445 SCROLLINFO info = new SCROLLINFO (); 446 info.cbSize = SCROLLINFO.sizeof; 447 info.fMask = OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 448 OS.GetScrollInfo (handle, OS.SB_CTL, info); 449 if (info.nMax - value - info.nPage < 1) return; 450 info.nMin = value; 451 SetScrollInfo (handle, OS.SB_CTL, info, true); 452 } 453 454 467 public void setPageIncrement (int value) { 468 checkWidget (); 469 if (value < 1) return; 470 pageIncrement = value; 471 } 472 473 boolean SetScrollInfo (int hwnd, int flags, SCROLLINFO info, boolean fRedraw) { 474 482 if ((state & DISABLED) != 0) fRedraw = false; 483 boolean result = OS.SetScrollInfo (hwnd, flags, info, fRedraw); 484 if ((state & DISABLED) != 0) { 485 OS.EnableWindow (handle, false); 486 if (!OS.IsWinCE) { 487 OS.EnableScrollBar (handle, OS.SB_CTL, OS.ESB_DISABLE_BOTH); 488 } 489 } 490 491 498 if (OS.GetFocus () == handle) { 499 ignoreFocus = true; 500 OS.SendMessage (handle, OS.WM_SETFOCUS, 0, 0); 501 ignoreFocus = false; 502 } 503 return result; 504 } 505 506 518 public void setSelection (int value) { 519 checkWidget (); 520 SCROLLINFO info = new SCROLLINFO (); 521 info.cbSize = SCROLLINFO.sizeof; 522 info.fMask = OS.SIF_POS; 523 info.nPos = value; 524 SetScrollInfo (handle, OS.SB_CTL, info, true); 525 } 526 527 541 public void setThumb (int value) { 542 checkWidget (); 543 if (value < 1) return; 544 SCROLLINFO info = new SCROLLINFO (); 545 info.cbSize = SCROLLINFO.sizeof; 546 info.fMask = OS.SIF_PAGE | OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 547 OS.GetScrollInfo (handle, OS.SB_CTL, info); 548 info.nPage = value; 549 if (info.nPage != 0) info.nPage++; 550 SetScrollInfo (handle, OS.SB_CTL, info, true); 551 } 552 553 574 public void setValues (int selection, int minimum, int maximum, int thumb, int increment, int pageIncrement) { 575 checkWidget (); 576 if (minimum < 0) return; 577 if (maximum < 0) return; 578 if (thumb < 1) return; 579 if (increment < 1) return; 580 if (pageIncrement < 1) return; 581 this.increment = increment; 582 this.pageIncrement = pageIncrement; 583 SCROLLINFO info = new SCROLLINFO (); 584 info.cbSize = SCROLLINFO.sizeof; 585 info.fMask = OS.SIF_POS | OS.SIF_PAGE | OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 586 info.nPos = selection; 587 info.nMin = minimum; 588 info.nMax = maximum; 589 info.nPage = thumb; 590 if (info.nPage != 0) info.nPage++; 591 SetScrollInfo (handle, OS.SB_CTL, info, true); 592 } 593 594 int widgetExtStyle () { 595 601 int bits = super.widgetExtStyle (); 602 if ((style & SWT.BORDER) != 0) bits &= ~OS.WS_EX_CLIENTEDGE; 603 return bits; 604 } 605 606 int widgetStyle () { 607 int bits = super.widgetStyle () | OS.WS_TABSTOP; 608 613 if ((style & SWT.BORDER) != 0) bits &= ~OS.WS_BORDER; 614 if ((style & SWT.HORIZONTAL) != 0) return bits | OS.SBS_HORZ; 615 return bits | OS.SBS_VERT; 616 } 617 618 TCHAR windowClass () { 619 return ScrollBarClass; 620 } 621 622 int windowProc () { 623 return ScrollBarProc; 624 } 625 626 LRESULT WM_KEYDOWN (int wParam, int lParam) { 627 LRESULT result = super.WM_KEYDOWN (wParam, lParam); 628 if (result != null) return result; 629 if ((style & SWT.VERTICAL) != 0) return result; 630 639 if ((style & SWT.MIRRORED) != 0) { 640 switch (wParam) { 641 case OS.VK_LEFT: 642 case OS.VK_RIGHT: { 643 int key = wParam == OS.VK_LEFT ? OS.VK_RIGHT : OS.VK_LEFT; 644 int code = callWindowProc (handle, OS.WM_KEYDOWN, key, lParam); 645 return new LRESULT (code); 646 } 647 } 648 } 649 return result; 650 } 651 652 LRESULT WM_LBUTTONDBLCLK (int wParam, int lParam) { 653 661 int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE); 662 int newBits = oldBits & ~OS.WS_TABSTOP; 663 OS.SetWindowLong (handle, OS.GWL_STYLE, newBits); 664 LRESULT result = super.WM_LBUTTONDBLCLK (wParam, lParam); 665 if (isDisposed ()) return LRESULT.ZERO; 666 OS.SetWindowLong (handle, OS.GWL_STYLE, oldBits); 667 if (result == LRESULT.ZERO) return result; 668 669 676 if (!OS.IsWinCE) { 677 if (OS.GetCapture () == handle) OS.ReleaseCapture (); 678 if (!sendMouseEvent (SWT.MouseUp, 1, handle, OS.WM_LBUTTONUP, wParam, lParam)) { 679 return LRESULT.ZERO; 680 } 681 } 682 return result; 683 } 684 685 LRESULT WM_LBUTTONDOWN (int wParam, int lParam) { 686 694 int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE); 695 int newBits = oldBits & ~OS.WS_TABSTOP; 696 OS.SetWindowLong (handle, OS.GWL_STYLE, newBits); 697 LRESULT result = super.WM_LBUTTONDOWN (wParam, lParam); 698 if (isDisposed ()) return LRESULT.ZERO; 699 OS.SetWindowLong (handle, OS.GWL_STYLE, oldBits); 700 if (result == LRESULT.ZERO) return result; 701 702 709 if (!OS.IsWinCE) { 710 if (OS.GetCapture () == handle) OS.ReleaseCapture (); 711 if (!sendMouseEvent (SWT.MouseUp, 1, handle, OS.WM_LBUTTONUP, wParam, lParam)) { 712 return LRESULT.ONE; 713 } 714 } 715 return result; 716 } 717 718 LRESULT WM_SETFOCUS (int wParam, int lParam) { 719 if (ignoreFocus) return null; 720 return super.WM_SETFOCUS (wParam, lParam); 721 } 722 723 LRESULT wmScrollChild (int wParam, int lParam) { 724 725 726 int code = wParam & 0xFFFF; 727 if (code == OS.SB_ENDSCROLL) return null; 728 729 730 Event event = new Event (); 731 SCROLLINFO info = new SCROLLINFO (); 732 info.cbSize = SCROLLINFO.sizeof; 733 info.fMask = OS.SIF_TRACKPOS | OS.SIF_POS | OS.SIF_RANGE; 734 OS.GetScrollInfo (handle, OS.SB_CTL, info); 735 info.fMask = OS.SIF_POS; 736 switch (code) { 737 case OS.SB_THUMBPOSITION: 738 event.detail = SWT.NONE; 739 info.nPos = info.nTrackPos; 740 break; 741 case OS.SB_THUMBTRACK: 742 event.detail = SWT.DRAG; 743 info.nPos = info.nTrackPos; 744 break; 745 case OS.SB_TOP: 746 event.detail = SWT.HOME; 747 info.nPos = info.nMin; 748 break; 749 case OS.SB_BOTTOM: 750 event.detail = SWT.END; 751 info.nPos = info.nMax; 752 break; 753 case OS.SB_LINEDOWN: 754 event.detail = SWT.ARROW_DOWN; 755 info.nPos += increment; 756 break; 757 case OS.SB_LINEUP: 758 event.detail = SWT.ARROW_UP; 759 info.nPos = Math.max (info.nMin, info.nPos - increment); 760 break; 761 case OS.SB_PAGEDOWN: 762 event.detail = SWT.PAGE_DOWN; 763 info.nPos += pageIncrement; 764 break; 765 case OS.SB_PAGEUP: 766 event.detail = SWT.PAGE_UP; 767 info.nPos = Math.max (info.nMin, info.nPos - pageIncrement); 768 break; 769 } 770 OS.SetScrollInfo (handle, OS.SB_CTL, info, true); 771 772 779 sendEvent (SWT.Selection, event); 780 return null; 782 } 783 784 } 785 | Popular Tags |