1 /*******************************************************************************
2 * Copyright (c) 2000, 2013 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.internal.win32.*;
24 * Instances of this class are selectable user interface
25 * objects that represent a range of positive, numeric values.
27 * At any given moment, a given scroll bar will have a
28 * single 'selection' that is considered to be its
29 * value, which is constrained to be within the range of
30 * values the scroll bar represents (that is, between its
31 * <em>minimum</em> and <em>maximum</em> values).
33 * Typically, scroll bars will be made up of five areas:</p>
35 * <li>an arrow button for decrementing the value</li>
36 * <li>a page decrement area for decrementing the value by a larger amount</li>
37 * <li>a <em>thumb</em> for modifying the value by mouse dragging</li>
38 * <li>a page increment area for incrementing the value by a larger amount</li>
39 * <li>an arrow button for incrementing the value</li>
42 * Based on their style, scroll bars are either <code>HORIZONTAL</code>
43 * (which have a left facing button for decrementing the value and a
44 * right facing button for incrementing it) or <code>VERTICAL</code>
45 * (which have an upward facing button for decrementing the value
46 * and a downward facing buttons for incrementing it).
48 * On some platforms, the size of the scroll bar's thumb can be
49 * varied relative to the magnitude of the range of values it
50 * represents (that is, relative to the difference between its
51 * maximum and minimum values). Typically, this is used to
52 * indicate some proportional value such as the ratio of the
53 * visible area of a document to the total amount of space that
54 * it would take to display it. SWT supports setting the thumb
55 * size even if the underlying platform does not, but in this
56 * case the appearance of the scroll bar will not change.
58 * Scroll bars are created by specifying either <code>H_SCROLL</code>,
59 * <code>V_SCROLL</code> or both when creating a <code>Scrollable</code>.
60 * They are accessed from the <code>Scrollable</code> using
61 * <code>getHorizontalBar</code> and <code>getVerticalBar</code>.
63 * Note: Scroll bars are not Controls. On some platforms, scroll bars
64 * that appear as part of some standard controls such as a text or list
65 * have no operating system resources and are not children of the control.
66 * For this reason, scroll bars are treated specially. To create a control
67 * that looks like a scroll bar but has operating system resources, use
68 * <code>Slider</code>.
71 * <dt><b>Styles:</b></dt>
72 * <dd>HORIZONTAL, VERTICAL</dd>
73 * <dt><b>Events:</b></dt>
77 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
79 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
84 * @see Scrollable#getHorizontalBar
85 * @see Scrollable#getVerticalBar
86 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
87 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
88 * @noextend This class is not intended to be subclassed by clients.
90 public class ScrollBar extends Widget {
92 int increment, pageIncrement;
95 * Constructs a new instance of this class given its parent
96 * and a style value describing its behavior and appearance.
98 * The style value is either one of the style constants defined in
99 * class <code>SWT</code> which is applicable to instances of this
100 * class, or must be built by <em>bitwise OR</em>'ing together
101 * (that is, using the <code>int</code> "|" operator) two or more
102 * of those <code>SWT</code> style constants. The class description
103 * lists the style constants that are applicable to the class.
104 * Style bits are also inherited from superclasses.
107 * @param parent a composite control which will be the parent of the new instance (cannot be null)
108 * @param style the style of control to construct
110 * @exception IllegalArgumentException <ul>
111 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
113 * @exception SWTException <ul>
114 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
115 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
118 * @see SWT#HORIZONTAL
120 * @see Widget#checkSubclass
121 * @see Widget#getStyle
123 ScrollBar (Scrollable parent, int style) {
124 super (parent, checkStyle (style));
125 this.parent = parent;
130 * Adds the listener to the collection of listeners who will
131 * be notified when the user changes the receiver's value, by sending
132 * it one of the messages defined in the <code>SelectionListener</code>
135 * When <code>widgetSelected</code> is called, the event object detail field contains one of the following values:
136 * <code>SWT.NONE</code> - for the end of a drag.
137 * <code>SWT.DRAG</code>.
138 * <code>SWT.HOME</code>.
139 * <code>SWT.END</code>.
140 * <code>SWT.ARROW_DOWN</code>.
141 * <code>SWT.ARROW_UP</code>.
142 * <code>SWT.PAGE_DOWN</code>.
143 * <code>SWT.PAGE_UP</code>.
144 * <code>widgetDefaultSelected</code> is not called.
147 * @param listener the listener which should be notified when the user changes the receiver's value
149 * @exception IllegalArgumentException <ul>
150 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
152 * @exception SWTException <ul>
153 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
154 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
157 * @see SelectionListener
158 * @see #removeSelectionListener
159 * @see SelectionEvent
161 public void addSelectionListener (SelectionListener listener) {
163 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
164 TypedListener typedListener = new TypedListener(listener);
165 addListener (SWT.Selection,typedListener);
166 addListener (SWT.DefaultSelection,typedListener);
169 static int checkStyle (int style) {
170 return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
173 void createWidget () {
177 * Do not set the initial values of the maximum
178 * or the thumb. These values normally default
179 * to 100 and 10 but may have been set already
180 * by the widget that owns the scroll bar. For
181 * example, a scroll bar that is created for a
182 * list widget, setting these defaults would
183 * override the initial values provided by the
189 void destroyWidget () {
190 long hwnd = hwndScrollBar ();
191 int type = scrollBarType ();
192 OS.ShowScrollBar (hwnd, type, false);
193 parent.destroyScrollBar (style);
195 //This code is intentionally commented
196 //parent.sendEvent (SWT.Resize);
199 Rectangle getBounds () {
201 parent.forceResize ();
202 RECT rect = new RECT ();
203 OS.GetClientRect (parent.scrolledHandle (), rect);
204 int x = 0, y = 0, width, height;
205 if ((style & SWT.HORIZONTAL) != 0) {
206 y = rect.bottom - rect.top;
207 width = rect.right - rect.left;
208 height = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
210 x = rect.right - rect.left;
211 width = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
212 height = rect.bottom - rect.top;
214 return new Rectangle (x, y, width, height);
218 * Returns <code>true</code> if the receiver is enabled, and
219 * <code>false</code> otherwise. A disabled control is typically
220 * not selectable from the user interface and draws with an
221 * inactive or "grayed" look.
223 * @return the receiver's enabled state
225 * @exception SWTException <ul>
226 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
227 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
232 public boolean getEnabled () {
234 return (state & DISABLED) == 0;
238 * Returns the amount that the receiver's value will be
239 * modified by when the up/down (or right/left) arrows
242 * @return the increment
244 * @exception SWTException <ul>
245 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
246 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
249 public int getIncrement () {
255 * Returns the maximum value which the receiver will allow.
257 * @return the maximum
259 * @exception SWTException <ul>
260 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
261 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
264 public int getMaximum () {
266 SCROLLINFO info = new SCROLLINFO ();
267 info.cbSize = SCROLLINFO.sizeof;
268 info.fMask = OS.SIF_RANGE;
269 long hwnd = hwndScrollBar ();
270 int type = scrollBarType ();
271 OS.GetScrollInfo (hwnd, type, info);
276 * Returns the minimum value which the receiver will allow.
278 * @return the minimum
280 * @exception SWTException <ul>
281 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
282 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
285 public int getMinimum () {
287 SCROLLINFO info = new SCROLLINFO ();
288 info.cbSize = SCROLLINFO.sizeof;
289 info.fMask = OS.SIF_RANGE;
290 long hwnd = hwndScrollBar ();
291 int type = scrollBarType ();
292 OS.GetScrollInfo (hwnd, type, info);
297 * Returns the amount that the receiver's value will be
298 * modified by when the page increment/decrement areas
301 * @return the page increment
303 * @exception SWTException <ul>
304 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
305 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
308 public int getPageIncrement () {
310 return pageIncrement;
314 * Returns the receiver's parent, which must be a Scrollable.
316 * @return the receiver's parent
318 * @exception SWTException <ul>
319 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
320 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
323 public Scrollable getParent () {
329 * Returns the single 'selection' that is the receiver's value.
331 * @return the selection
333 * @exception SWTException <ul>
334 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
335 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
338 public int getSelection () {
340 SCROLLINFO info = new SCROLLINFO ();
341 info.cbSize = SCROLLINFO.sizeof;
342 info.fMask = OS.SIF_POS;
343 long hwnd = hwndScrollBar ();
344 int type = scrollBarType ();
345 OS.GetScrollInfo (hwnd, type, info);
350 * Returns a point describing the receiver's size. The
351 * x coordinate of the result is the width of the receiver.
352 * The y coordinate of the result is the height of the
355 * @return the receiver's size
357 * @exception SWTException <ul>
358 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
359 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
362 public Point getSize () {
364 return DPIUtil.autoScaleDown(getSizeInPixels());
367 Point getSizeInPixels () {
368 parent.forceResize ();
369 RECT rect = new RECT ();
370 OS.GetClientRect (parent.scrolledHandle (), rect);
372 if ((style & SWT.HORIZONTAL) != 0) {
373 width = rect.right - rect.left;
374 height = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
376 width = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
377 height = rect.bottom - rect.top;
379 return new Point (width, height);
383 * Returns the receiver's thumb value.
385 * @return the thumb value
387 * @exception SWTException <ul>
388 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
389 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
394 public int getThumb () {
396 SCROLLINFO info = new SCROLLINFO ();
397 info.cbSize = SCROLLINFO.sizeof;
398 info.fMask = OS.SIF_PAGE;
399 long hwnd = hwndScrollBar ();
400 int type = scrollBarType ();
401 OS.GetScrollInfo (hwnd, type, info);
402 if (info.nPage != 0) --info.nPage;
407 * Returns a rectangle describing the size and location of the
408 * receiver's thumb relative to its parent.
410 * @return the thumb bounds, relative to the {@link #getParent() parent}
412 * @exception SWTException <ul>
413 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
414 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
419 public Rectangle getThumbBounds () {
421 return DPIUtil.autoScaleDown(getThumbBoundsInPixels());
424 Rectangle getThumbBoundsInPixels () {
425 parent.forceResize ();
426 SCROLLBARINFO info = new SCROLLBARINFO();
427 info.cbSize = SCROLLBARINFO.sizeof;
428 int x, y, width, height;
429 if ((style & SWT.HORIZONTAL) != 0) {
430 OS.GetScrollBarInfo(parent.handle, OS.OBJID_HSCROLL, info);
431 x = info.rcScrollBar.left + info.xyThumbTop;
432 y = info.rcScrollBar.top;
433 width = info.xyThumbBottom - info.xyThumbTop;
434 height = info.rcScrollBar.bottom - info.rcScrollBar.top;
436 OS.GetScrollBarInfo(parent.handle, OS.OBJID_VSCROLL, info);
437 x = info.rcScrollBar.left;
438 y = info.rcScrollBar.top + info.xyThumbTop;
439 width = info.rcScrollBar.right - info.rcScrollBar.left;
440 height = info.xyThumbBottom - info.xyThumbTop;
442 RECT rect = new RECT ();
445 rect.right = x + width;
446 rect.bottom = y + height;
447 OS.MapWindowPoints (0, parent.handle, rect, 2);
448 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
452 * Returns a rectangle describing the size and location of the
453 * receiver's thumb track relative to its parent. This rectangle
454 * comprises the areas 2, 3, and 4 as described in {@link ScrollBar}.
456 * @return the thumb track bounds, relative to the {@link #getParent() parent}
458 * @exception SWTException <ul>
459 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
460 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
465 public Rectangle getThumbTrackBounds () {
467 return DPIUtil.autoScaleDown(getThumbTrackBoundsInPixels());
470 Rectangle getThumbTrackBoundsInPixels () {
471 parent.forceResize ();
472 SCROLLBARINFO info = new SCROLLBARINFO();
473 info.cbSize = SCROLLBARINFO.sizeof;
474 int x = 0, y = 0, width, height;
475 if ((style & SWT.HORIZONTAL) != 0) {
476 OS.GetScrollBarInfo(parent.handle, OS.OBJID_HSCROLL, info);
477 int size = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
478 y = info.rcScrollBar.top;
479 width = info.rcScrollBar.right - info.rcScrollBar.left;
481 if (width <= 2 * size) {
482 x = info.rcScrollBar.left + width / 2;
485 x = info.rcScrollBar.left + size;
489 OS.GetScrollBarInfo(parent.handle, OS.OBJID_VSCROLL, info);
490 int size = OS.GetSystemMetrics (OS.SM_CYVSCROLL);
491 x = info.rcScrollBar.left;
493 height = info.rcScrollBar.bottom - info.rcScrollBar.top;
494 if (height <= 2 * size) {
495 y = info.rcScrollBar.top + height / 2;
498 y = info.rcScrollBar.top + size;
502 RECT rect = new RECT ();
505 rect.right = x + width;
506 rect.bottom = y + height;
507 OS.MapWindowPoints (0, parent.handle, rect, 2);
508 return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
512 * Returns <code>true</code> if the receiver is visible, and
513 * <code>false</code> otherwise.
515 * If one of the receiver's ancestors is not visible or some
516 * other condition makes the receiver not visible, this method
517 * may still indicate that it is considered visible even though
518 * it may not actually be showing.
521 * @return the receiver's visibility state
523 * @exception SWTException <ul>
524 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
525 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
528 public boolean getVisible () {
530 SCROLLBARINFO psbi = new SCROLLBARINFO ();
531 psbi.cbSize = SCROLLBARINFO.sizeof;
532 int idObject = (style & SWT.VERTICAL) != 0 ? OS.OBJID_VSCROLL : OS.OBJID_HSCROLL;
533 OS.GetScrollBarInfo (hwndScrollBar (), idObject, psbi);
534 return (psbi.rgstate [0] & OS.STATE_SYSTEM_INVISIBLE) == 0;
537 long hwndScrollBar () {
538 return parent.scrolledHandle ();
542 * Returns <code>true</code> if the receiver is enabled and all
543 * of the receiver's ancestors are enabled, and <code>false</code>
544 * otherwise. A disabled control is typically not selectable from the
545 * user interface and draws with an inactive or "grayed" look.
547 * @return the receiver's enabled state
549 * @exception SWTException <ul>
550 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
551 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
556 public boolean isEnabled () {
558 return getEnabled () && parent.isEnabled ();
562 * Returns <code>true</code> if the receiver is visible and all
563 * of the receiver's ancestors are visible and <code>false</code>
566 * @return the receiver's visibility state
568 * @exception SWTException <ul>
569 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
570 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
575 public boolean isVisible () {
577 return getVisible () && parent.isVisible ();
581 void releaseHandle () {
582 super.releaseHandle ();
587 void releaseParent () {
588 super.releaseParent ();
589 if (parent.horizontalBar == this) parent.horizontalBar = null;
590 if (parent.verticalBar == this) parent.verticalBar = null;
594 * Removes the listener from the collection of listeners who will
595 * be notified when the user changes the receiver's value.
597 * @param listener the listener which should no longer be notified
599 * @exception IllegalArgumentException <ul>
600 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
602 * @exception SWTException <ul>
603 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
604 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
607 * @see SelectionListener
608 * @see #addSelectionListener
610 public void removeSelectionListener (SelectionListener listener) {
612 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
613 if (eventTable == null) return;
614 eventTable.unhook (SWT.Selection, listener);
615 eventTable.unhook (SWT.DefaultSelection,listener);
618 int scrollBarType () {
619 return (style & SWT.VERTICAL) != 0 ? OS.SB_VERT : OS.SB_HORZ;
623 * Enables the receiver if the argument is <code>true</code>,
624 * and disables it otherwise. A disabled control is typically
625 * not selectable from the user interface and draws with an
626 * inactive or "grayed" look.
628 * @param enabled the new enabled state
630 * @exception SWTException <ul>
631 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
632 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
635 public void setEnabled (boolean enabled) {
637 long hwnd = hwndScrollBar ();
638 int type = scrollBarType ();
639 int flags = enabled ? OS.ESB_ENABLE_BOTH : OS.ESB_DISABLE_BOTH;
640 OS.EnableScrollBar (hwnd, type, flags);
649 * Sets the amount that the receiver's value will be
650 * modified by when the up/down (or right/left) arrows
651 * are pressed to the argument, which must be at least
654 * @param value the new increment (must be greater than zero)
656 * @exception SWTException <ul>
657 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
658 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
661 public void setIncrement (int value) {
663 if (value < 1) return;
668 * Sets the maximum. If this value is negative or less than or
669 * equal to the minimum, the value is ignored. If necessary, first
670 * the thumb and then the selection are adjusted to fit within the
673 * @param value the new maximum
675 * @exception SWTException <ul>
676 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
677 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
680 public void setMaximum (int value) {
682 if (value < 0) return;
683 SCROLLINFO info = new SCROLLINFO ();
684 info.cbSize = SCROLLINFO.sizeof;
685 long hwnd = hwndScrollBar ();
686 int type = scrollBarType ();
687 info.fMask = OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL;
688 OS.GetScrollInfo (hwnd, type, info);
689 if (value - info.nMin - info.nPage < 1) return;
691 SetScrollInfo (hwnd, type, info, true);
695 * Sets the minimum value. If this value is negative or greater
696 * than or equal to the maximum, the value is ignored. If necessary,
697 * first the thumb and then the selection are adjusted to fit within
700 * @param value the new minimum
702 * @exception SWTException <ul>
703 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
704 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
707 public void setMinimum (int value) {
709 if (value < 0) return;
710 SCROLLINFO info = new SCROLLINFO ();
711 info.cbSize = SCROLLINFO.sizeof;
712 long hwnd = hwndScrollBar ();
713 int type = scrollBarType ();
714 info.fMask = OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL;
715 OS.GetScrollInfo (hwnd, type, info);
716 if (info.nMax - value - info.nPage < 1) return;
718 SetScrollInfo (hwnd, type, info, true);
722 * Sets the amount that the receiver's value will be
723 * modified by when the page increment/decrement areas
724 * are selected to the argument, which must be at least
727 * @param value the page increment (must be greater than zero)
729 * @exception SWTException <ul>
730 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
731 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
734 public void setPageIncrement (int value) {
736 if (value < 1) return;
737 pageIncrement = value;
740 boolean SetScrollInfo (long hwnd, int flags, SCROLLINFO info, boolean fRedraw) {
742 * Bug in Windows. For some reason, when SetScrollInfo()
743 * is used with SIF_POS and the scroll bar is hidden,
744 * the opposite scroll bar is incorrectly made visible
745 * so that the next time the parent is resized (or another
746 * scroll bar operation is performed), the opposite scroll
747 * bar draws. The fix is to hide both scroll bars.
749 boolean barVisible = false;
750 boolean visible = getVisible ();
752 ScrollBar bar = null;
755 bar = parent.getVerticalBar ();
758 bar = parent.getHorizontalBar ();
761 barVisible = bar != null && bar.getVisible ();
762 if (!visible || (state & DISABLED) != 0) fRedraw = false;
763 boolean result = OS.SetScrollInfo (hwnd, flags, info, fRedraw);
766 * Bug in Windows. For some reason, when the widget
767 * is a standard scroll bar, and SetScrollInfo() is
768 * called with SIF_RANGE or SIF_PAGE, the widget is
769 * incorrectly made visible so that the next time the
770 * parent is resized (or another scroll bar operation
771 * is performed), the scroll bar draws. The fix is
772 * to hide the scroll bar (again) when already hidden.
775 OS.ShowScrollBar (hwnd, !barVisible ? OS.SB_BOTH : flags, false);
779 * Bug in Windows. When only one scroll bar is visible,
780 * and the thumb changes using SIF_RANGE or SIF_PAGE
781 * from being visible to hidden, the opposite scroll
782 * bar is incorrectly made visible. The next time the
783 * parent is resized (or another scroll bar operation
784 * is performed), the opposite scroll bar draws. The
785 * fix is to hide the opposite scroll bar again.
787 * NOTE: This problem only happens on Vista
789 if (visible && bar != null && !barVisible) {
790 OS.ShowScrollBar (hwnd, flags == OS.SB_HORZ ? OS.SB_VERT : OS.SB_HORZ, false);
794 * Feature in Windows. Using SIF_DISABLENOSCROLL,
795 * SetScrollInfo () can change enabled and disabled
796 * state of the scroll bar causing a scroll bar that
797 * was disabled by the application to become enabled.
798 * The fix is to disable the scroll bar (again) when
799 * the application has disabled the scroll bar.
801 if ((state & DISABLED) != 0) {
802 OS.EnableScrollBar (hwnd, flags, OS.ESB_DISABLE_BOTH);
808 * Sets the single <em>selection</em> that is the receiver's
809 * value to the argument which must be greater than or equal
812 * @param selection the new selection (must be zero or greater)
814 * @exception SWTException <ul>
815 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
816 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
819 public void setSelection (int selection) {
821 SCROLLINFO info = new SCROLLINFO ();
822 info.cbSize = SCROLLINFO.sizeof;
823 long hwnd = hwndScrollBar ();
824 int type = scrollBarType ();
825 info.fMask = OS.SIF_POS;
826 info.nPos = selection;
827 SetScrollInfo (hwnd, type, info, true);
831 * Sets the thumb value. The thumb value should be used to represent
832 * the size of the visual portion of the current range. This value is
833 * usually the same as the page increment value.
835 * This new value will be ignored if it is less than one, and will be
836 * clamped if it exceeds the receiver's current range.
839 * @param value the new thumb value, which must be at least one and not
840 * larger than the size of the current range
842 * @exception SWTException <ul>
843 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
844 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
847 public void setThumb (int value) {
849 if (value < 1) return;
850 SCROLLINFO info = new SCROLLINFO ();
851 info.cbSize = SCROLLINFO.sizeof;
852 long hwnd = hwndScrollBar ();
853 int type = scrollBarType ();
854 info.fMask = OS.SIF_PAGE | OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL;
855 OS.GetScrollInfo (hwnd, type, info);
857 if (info.nPage != 0) info.nPage++;
858 SetScrollInfo (hwnd, type, info, true);
862 * Sets the receiver's selection, minimum value, maximum
863 * value, thumb, increment and page increment all at once.
865 * Note: This is similar to setting the values individually
866 * using the appropriate methods, but may be implemented in a
867 * more efficient fashion on some platforms.
870 * @param selection the new selection value
871 * @param minimum the new minimum value
872 * @param maximum the new maximum value
873 * @param thumb the new thumb value
874 * @param increment the new increment value
875 * @param pageIncrement the new pageIncrement value
877 * @exception SWTException <ul>
878 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
879 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
882 public void setValues (int selection, int minimum, int maximum, int thumb, int increment, int pageIncrement) {
884 if (minimum < 0) return;
885 if (maximum < 0) return;
886 if (thumb < 1) return;
887 if (increment < 1) return;
888 if (pageIncrement < 1) return;
889 this.increment = increment;
890 this.pageIncrement = pageIncrement;
891 SCROLLINFO info = new SCROLLINFO ();
892 info.cbSize = SCROLLINFO.sizeof;
893 info.fMask = OS.SIF_POS | OS.SIF_PAGE | OS.SIF_RANGE | OS.SIF_DISABLENOSCROLL;
894 info.nPos = selection;
898 if (info.nPage != 0) info.nPage++;
899 long hwnd = hwndScrollBar ();
900 int type = scrollBarType ();
901 SetScrollInfo (hwnd, type, info, true);
905 * Marks the receiver as visible if the argument is <code>true</code>,
906 * and marks it invisible otherwise.
908 * If one of the receiver's ancestors is not visible or some
909 * other condition makes the receiver not visible, marking
910 * it visible may not actually cause it to be displayed.
913 * @param visible the new visibility state
915 * @exception SWTException <ul>
916 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
917 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
920 public void setVisible (boolean visible) {
922 if (visible == getVisible ()) return;
924 * Set the state bits before calling ShowScrollBar ()
925 * because hiding and showing the scroll bar can cause
926 * WM_SIZE messages when the client area is resized.
927 * Setting the state before the call means that code
928 * that runs during WM_SIZE that queries the visibility
929 * of the scroll bar will get the correct value.
931 state = visible ? state & ~HIDDEN : state | HIDDEN;
932 long hwnd = hwndScrollBar ();
933 int type = scrollBarType ();
935 * Bug in Windows 7. Windows will cause pixel corruption
936 * when there is only one scroll bar visible and it is
937 * hidden. The fix is to temporarily show the other scroll
941 if (OS.IsAppThemed ()) {
942 SCROLLBARINFO psbi = new SCROLLBARINFO ();
943 psbi.cbSize = SCROLLBARINFO.sizeof;
944 int idObject = (style & SWT.VERTICAL) != 0 ? OS.OBJID_HSCROLL : OS.OBJID_VSCROLL;
945 OS.GetScrollBarInfo (hwnd, idObject, psbi);
946 if ((psbi.rgstate [0] & OS.STATE_SYSTEM_INVISIBLE) != 0) {
947 OS.ShowScrollBar (hwnd, type == OS.SB_VERT ? OS.SB_HORZ : OS.SB_VERT, true);
952 if (OS.ShowScrollBar (hwnd, type, visible)) {
954 * Bug in Windows. For some reason, when the widget
955 * is a standard scroll bar, and SetScrollInfo () is
956 * called with SIF_RANGE or SIF_PAGE while the widget
957 * is not visible, the widget is incorrectly disabled
958 * even though the values for SIF_RANGE and SIF_PAGE,
959 * when set for a visible scroll bar would not disable
960 * the scroll bar. The fix is to enable the scroll bar
961 * when not disabled by the application and the current
962 * scroll bar ranges would cause the scroll bar to be
963 * enabled had they been set when the scroll bar was
966 if ((state & DISABLED) == 0) {
967 SCROLLINFO info = new SCROLLINFO ();
968 info.cbSize = SCROLLINFO.sizeof;
969 info.fMask = OS.SIF_RANGE | OS.SIF_PAGE;
970 OS.GetScrollInfo (hwnd, type, info);
971 if (info.nMax - info.nMin - info.nPage >= 0) {
972 OS.EnableScrollBar (hwnd, type, OS.ESB_ENABLE_BOTH);
975 sendEvent (visible ? SWT.Show : SWT.Hide);
976 // widget could be disposed at this point
980 LRESULT wmScrollChild (long wParam, long lParam) {
982 /* Do nothing when scrolling is ending */
983 int code = OS.LOWORD (wParam);
984 if (code == OS.SB_ENDSCROLL) return null;
987 * Send the event because WM_HSCROLL and
988 * WM_VSCROLL are sent from a modal message
989 * loop in Windows that is active when the
992 Event event = new Event ();
994 case OS.SB_THUMBPOSITION: event.detail = SWT.NONE; break;
995 case OS.SB_THUMBTRACK: event.detail = SWT.DRAG; break;
996 case OS.SB_TOP: event.detail = SWT.HOME; break;
997 case OS.SB_BOTTOM: event.detail = SWT.END; break;
998 case OS.SB_LINEDOWN: event.detail = SWT.ARROW_DOWN; break;
999 case OS.SB_LINEUP: event.detail = SWT.ARROW_UP; break;
1000 case OS.SB_PAGEDOWN: event.detail = SWT.PAGE_DOWN; break;
1001 case OS.SB_PAGEUP: event.detail = SWT.PAGE_UP; break;
1003 sendSelectionEvent (SWT.Selection, event, true);
1004 // the widget could be destroyed at this point