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 82 83 public class ScrollBar extends Widget { 84 Scrollable parent; 85 int increment, pageIncrement; 86 87 116 ScrollBar (Scrollable parent, int style) { 117 super (parent, checkStyle (style)); 118 this.parent = parent; 119 createWidget (); 120 } 121 122 154 public void addSelectionListener (SelectionListener listener) { 155 checkWidget(); 156 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 157 TypedListener typedListener = new TypedListener(listener); 158 addListener (SWT.Selection,typedListener); 159 addListener (SWT.DefaultSelection,typedListener); 160 } 161 162 static int checkStyle (int style) { 163 return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0); 164 } 165 166 void createWidget () { 167 increment = 1; 168 pageIncrement = 10; 169 179 } 180 181 void destroyWidget () { 182 int hwnd = hwndScrollBar (), type = scrollBarType (); 183 if (OS.IsWinCE) { 184 SCROLLINFO info = new SCROLLINFO (); 185 info.cbSize = SCROLLINFO.sizeof; 186 info.fMask = OS.SIF_RANGE | OS.SIF_PAGE; 187 info.nPage = 101; 188 info.nMax = 100; 189 info.nMin = 0; 190 OS.SetScrollInfo (hwnd, type, info, true); 191 } else { 192 OS.ShowScrollBar (hwnd, type, false); 193 } 194 releaseHandle (); 195 } 196 197 Rectangle getBounds () { 198 parent.forceResize (); 200 RECT rect = new RECT (); 201 OS.GetClientRect (parent.scrolledHandle (), rect); 202 int x = 0, y = 0, width, height; 203 if ((style & SWT.HORIZONTAL) != 0) { 204 y = rect.bottom - rect.top; 205 width = rect.right - rect.left; 206 height = OS.GetSystemMetrics (OS.SM_CYHSCROLL); 207 } else { 208 x = rect.right - rect.left; 209 width = OS.GetSystemMetrics (OS.SM_CXVSCROLL); 210 height = rect.bottom - rect.top; 211 } 212 return new Rectangle (x, y, width, height); 213 } 214 215 230 public boolean getEnabled () { 231 checkWidget(); 232 return (state & DISABLED) == 0; 233 } 234 235 247 public int getIncrement () { 248 checkWidget(); 249 return increment; 250 } 251 252 262 public int getMaximum () { 263 checkWidget(); 264 SCROLLINFO info = new SCROLLINFO (); 265 info.cbSize = SCROLLINFO.sizeof; 266 info.fMask = OS.SIF_RANGE; 267 int hwnd = hwndScrollBar (); 268 int type = scrollBarType (); 269 OS.GetScrollInfo (hwnd, type, info); 270 return info.nMax; 271 } 272 273 283 public int getMinimum () { 284 checkWidget(); 285 SCROLLINFO info = new SCROLLINFO (); 286 info.cbSize = SCROLLINFO.sizeof; 287 info.fMask = OS.SIF_RANGE; 288 int hwnd = hwndScrollBar (); 289 int type = scrollBarType (); 290 OS.GetScrollInfo (hwnd, type, info); 291 return info.nMin; 292 } 293 294 306 public int getPageIncrement () { 307 checkWidget(); 308 return pageIncrement; 309 } 310 311 321 public Scrollable getParent () { 322 checkWidget(); 323 return parent; 324 } 325 326 336 public int getSelection () { 337 checkWidget(); 338 SCROLLINFO info = new SCROLLINFO (); 339 info.cbSize = SCROLLINFO.sizeof; 340 info.fMask = OS.SIF_POS; 341 int hwnd = hwndScrollBar (); 342 int type = scrollBarType (); 343 OS.GetScrollInfo (hwnd, type, info); 344 return info.nPos; 345 } 346 347 360 public Point getSize () { 361 checkWidget(); 362 parent.forceResize (); 363 RECT rect = new RECT (); 364 OS.GetClientRect (parent.scrolledHandle (), rect); 365 int width, height; 366 if ((style & SWT.HORIZONTAL) != 0) { 367 width = rect.right - rect.left; 368 height = OS.GetSystemMetrics (OS.SM_CYHSCROLL); 369 } else { 370 width = OS.GetSystemMetrics (OS.SM_CXVSCROLL); 371 height = rect.bottom - rect.top; 372 } 373 return new Point (width, height); 374 } 375 376 389 public int getThumb () { 390 checkWidget(); 391 SCROLLINFO info = new SCROLLINFO (); 392 info.cbSize = SCROLLINFO.sizeof; 393 info.fMask = OS.SIF_PAGE; 394 int hwnd = hwndScrollBar (); 395 int type = scrollBarType (); 396 OS.GetScrollInfo (hwnd, type, info); 397 if (info.nPage != 0) --info.nPage; 398 return info.nPage; 399 } 400 401 418 public boolean getVisible () { 419 checkWidget(); 420 return (state & HIDDEN) == 0; 421 } 422 423 int hwndScrollBar () { 424 return parent.scrolledHandle (); 425 } 426 427 442 public boolean isEnabled () { 443 checkWidget(); 444 return getEnabled () && parent.isEnabled (); 445 } 446 447 461 public boolean isVisible () { 462 checkWidget(); 463 return getVisible () && parent.isVisible (); 464 } 465 466 void releaseHandle () { 467 super.releaseHandle (); 468 parent = null; 469 } 470 471 void releaseParent () { 472 super.releaseParent (); 473 if (parent.horizontalBar == this) parent.horizontalBar = null; 474 if (parent.verticalBar == this) parent.verticalBar = null; 475 } 476 477 494 public void removeSelectionListener (SelectionListener listener) { 495 checkWidget(); 496 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 497 if (eventTable == null) return; 498 eventTable.unhook (SWT.Selection, listener); 499 eventTable.unhook (SWT.DefaultSelection,listener); 500 } 501 502 int scrollBarType () { 503 return (style & SWT.VERTICAL) != 0 ? OS.SB_VERT : OS.SB_HORZ; 504 } 505 506 519 public void setEnabled (boolean enabled) { 520 checkWidget(); 521 525 if (!OS.IsWinCE) { 527 int hwnd = hwndScrollBar (), type = scrollBarType (); 528 int flags = enabled ? OS.ESB_ENABLE_BOTH : OS.ESB_DISABLE_BOTH; 529 OS.EnableScrollBar (hwnd, type, flags); 530 if (enabled) { 531 state &= ~DISABLED; 532 } else { 533 state |= DISABLED; 534 } 535 } 536 } 537 538 551 public void setIncrement (int value) { 552 checkWidget(); 553 if (value < 1) return; 554 increment = value; 555 } 556 557 570 public void setMaximum (int value) { 571 checkWidget(); 572 if (value < 0) return; 573 SCROLLINFO info = new SCROLLINFO (); 574 info.cbSize = SCROLLINFO.sizeof; 575 int hwnd = hwndScrollBar (), type = scrollBarType (); 576 info.fMask = OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 577 OS.GetScrollInfo (hwnd, type, info); 578 if (value - info.nMin - info.nPage < 1) return; 579 info.nMax = value; 580 SetScrollInfo (hwnd, type, info, true); 581 } 582 583 596 public void setMinimum (int value) { 597 checkWidget(); 598 if (value < 0) return; 599 SCROLLINFO info = new SCROLLINFO (); 600 info.cbSize = SCROLLINFO.sizeof; 601 int hwnd = hwndScrollBar (), type = scrollBarType (); 602 info.fMask = OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 603 OS.GetScrollInfo (hwnd, type, info); 604 if (info.nMax - value - info.nPage < 1) return; 605 info.nMin = value; 606 SetScrollInfo (hwnd, type, info, true); 607 } 608 609 622 public void setPageIncrement (int value) { 623 checkWidget(); 624 if (value < 1) return; 625 pageIncrement = value; 626 } 627 628 boolean SetScrollInfo (int hwnd, int flags, SCROLLINFO info, boolean fRedraw) { 629 637 if ((state & (DISABLED | HIDDEN)) != 0) fRedraw = false; 638 boolean result = OS.SetScrollInfo (hwnd, flags, info, fRedraw); 639 640 649 if ((state & HIDDEN) != 0) { 650 654 if (!OS.IsWinCE) { 656 ScrollBar bar = null; 657 switch (flags) { 658 case OS.SB_HORZ: 659 bar = parent.getVerticalBar (); 660 break; 661 case OS.SB_VERT: 662 bar = parent.getHorizontalBar (); 663 break; 664 } 665 boolean both = bar != null && !bar.getVisible (); 666 OS.ShowScrollBar (hwnd, both ? OS.SB_BOTH : flags, false); 667 } 668 } 669 670 678 if ((state & DISABLED) != 0) { 679 683 if (!OS.IsWinCE) { 685 OS.EnableScrollBar (hwnd, flags, OS.ESB_DISABLE_BOTH); 686 } 687 } 688 return result; 689 } 690 691 703 public void setSelection (int selection) { 704 checkWidget(); 705 SCROLLINFO info = new SCROLLINFO (); 706 info.cbSize = SCROLLINFO.sizeof; 707 int hwnd = hwndScrollBar (), type = scrollBarType (); 708 info.fMask = OS.SIF_POS; 709 info.nPos = selection; 710 SetScrollInfo (hwnd, type, info, true); 711 } 712 713 727 public void setThumb (int value) { 728 checkWidget(); 729 if (value < 1) return; 730 SCROLLINFO info = new SCROLLINFO (); 731 info.cbSize = SCROLLINFO.sizeof; 732 int hwnd = hwndScrollBar (), type = scrollBarType (); 733 info.fMask = OS.SIF_PAGE | OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 734 OS.GetScrollInfo (hwnd, type, info); 735 info.nPage = value; 736 if (info.nPage != 0) info.nPage++; 737 SetScrollInfo (hwnd, type, info, true); 738 } 739 740 761 public void setValues (int selection, int minimum, int maximum, int thumb, int increment, int pageIncrement) { 762 checkWidget(); 763 if (minimum < 0) return; 764 if (maximum < 0) return; 765 if (thumb < 1) return; 766 if (increment < 1) return; 767 if (pageIncrement < 1) return; 768 this.increment = increment; 769 this.pageIncrement = pageIncrement; 770 SCROLLINFO info = new SCROLLINFO (); 771 info.cbSize = SCROLLINFO.sizeof; 772 info.fMask = OS.SIF_POS | OS.SIF_PAGE | OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL; 773 info.nPos = selection; 774 info.nMin = minimum; 775 info.nMax = maximum; 776 info.nPage = thumb; 777 if (info.nPage != 0) info.nPage++; 778 int hwnd = hwndScrollBar (), type = scrollBarType (); 779 SetScrollInfo (hwnd, type, info, true); 780 } 781 782 798 public void setVisible (boolean visible) { 799 checkWidget(); 800 boolean isVisible = (state & HIDDEN) == 0; 801 if (isVisible == visible) return; 802 803 808 if (OS.IsWinCE) { 809 SCROLLINFO info = new SCROLLINFO (); 810 info.cbSize = SCROLLINFO.sizeof; 811 int hwnd = hwndScrollBar (), type = scrollBarType (); 812 info.fMask = OS.SIF_RANGE | OS.SIF_PAGE; 813 if (visible) info.fMask |= OS.SIF_DISABLENOSCROLL; 814 OS.GetScrollInfo (hwnd, type, info); 815 if (info.nPage == info.nMax - info.nMin + 1) { 816 823 int max = info.nMax; 824 info.nMax++; 825 OS.SetScrollInfo (hwnd, type, info, false); 826 info.nMax = max; 827 OS.SetScrollInfo (hwnd, type, info, true); 828 } else { 829 833 } 835 return; 836 } 837 838 846 state = visible ? state & ~HIDDEN : state | HIDDEN; 847 int hwnd = hwndScrollBar (), type = scrollBarType (); 848 if (OS.ShowScrollBar (hwnd, type, visible)) { 849 862 if ((state & DISABLED) == 0) { 863 SCROLLINFO info = new SCROLLINFO (); 864 info.cbSize = SCROLLINFO.sizeof; 865 info.fMask = OS.SIF_RANGE | OS.SIF_PAGE; 866 OS.GetScrollInfo (hwnd, type, info); 867 if (info.nMax - info.nMin - info.nPage >= 0) { 868 OS.EnableScrollBar (hwnd, type, OS.ESB_ENABLE_BOTH); 869 } 870 } 871 sendEvent (visible ? SWT.Show : SWT.Hide); 872 } 874 } 875 876 LRESULT wmScrollChild (int wParam, int lParam) { 877 878 879 int code = wParam & 0xFFFF; 880 if (code == OS.SB_ENDSCROLL) return null; 881 882 888 Event event = new Event (); 889 switch (code) { 890 case OS.SB_THUMBPOSITION: event.detail = SWT.NONE; break; 891 case OS.SB_THUMBTRACK: event.detail = SWT.DRAG; break; 892 case OS.SB_TOP: event.detail = SWT.HOME; break; 893 case OS.SB_BOTTOM: event.detail = SWT.END; break; 894 case OS.SB_LINEDOWN: event.detail = SWT.ARROW_DOWN; break; 895 case OS.SB_LINEUP: event.detail = SWT.ARROW_UP; break; 896 case OS.SB_PAGEDOWN: event.detail = SWT.PAGE_DOWN; break; 897 case OS.SB_PAGEUP: event.detail = SWT.PAGE_UP; break; 898 } 899 sendEvent (SWT.Selection, event); 900 return null; 902 } 903 904 } 905 | Popular Tags |