]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/ScrollBar.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / ScrollBar.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2013 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
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.*;
22
23 /**
24  * Instances of this class are selectable user interface
25  * objects that represent a range of positive, numeric values.
26  * <p>
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).
32  * </p><p>
33  * Typically, scroll bars will be made up of five areas:</p>
34  * <ol>
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>
40  * </ol>
41  * <p>
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).
47  * </p><p>
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.
57  * </p><p>
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>.
62  * </p><p>
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>.
69  * </p>
70  * <dl>
71  * <dt><b>Styles:</b></dt>
72  * <dd>HORIZONTAL, VERTICAL</dd>
73  * <dt><b>Events:</b></dt>
74  * <dd>Selection</dd>
75  * </dl>
76  * <p>
77  * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
78  * </p><p>
79  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
80  * </p>
81  *
82  * @see Slider
83  * @see Scrollable
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.
89  */
90 public class ScrollBar extends Widget {
91         Scrollable parent;
92         int increment, pageIncrement;
93
94 /**
95  * Constructs a new instance of this class given its parent
96  * and a style value describing its behavior and appearance.
97  * <p>
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.
105  * </p>
106  *
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
109  *
110  * @exception IllegalArgumentException <ul>
111  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
112  * </ul>
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>
116  * </ul>
117  *
118  * @see SWT#HORIZONTAL
119  * @see SWT#VERTICAL
120  * @see Widget#checkSubclass
121  * @see Widget#getStyle
122  */
123 ScrollBar (Scrollable parent, int style) {
124         super (parent, checkStyle (style));
125         this.parent = parent;
126         createWidget ();
127 }
128
129 /**
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>
133  * interface.
134  * <p>
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.
145  * </p>
146  *
147  * @param listener the listener which should be notified when the user changes the receiver's value
148  *
149  * @exception IllegalArgumentException <ul>
150  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
151  * </ul>
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>
155  * </ul>
156  *
157  * @see SelectionListener
158  * @see #removeSelectionListener
159  * @see SelectionEvent
160  */
161 public void addSelectionListener (SelectionListener listener) {
162         checkWidget();
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);
167 }
168
169 static int checkStyle (int style) {
170         return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
171 }
172
173 void createWidget () {
174         increment = 1;
175         pageIncrement = 10;
176         /*
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
184         * list widget.
185         */
186 }
187
188 @Override
189 void destroyWidget () {
190         long hwnd = hwndScrollBar ();
191         int type = scrollBarType ();
192         OS.ShowScrollBar (hwnd, type, false);
193         parent.destroyScrollBar (style);
194         releaseHandle ();
195         //This code is intentionally commented
196         //parent.sendEvent (SWT.Resize);
197 }
198
199 Rectangle getBounds () {
200 //      checkWidget ();
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);
209         } else {
210                 x = rect.right - rect.left;
211                 width = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
212                 height = rect.bottom - rect.top;
213         }
214         return new Rectangle (x, y, width, height);
215 }
216
217 /**
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.
222  *
223  * @return the receiver's enabled state
224  *
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>
228  * </ul>
229  *
230  * @see #isEnabled
231  */
232 public boolean getEnabled () {
233         checkWidget();
234         return (state & DISABLED) == 0;
235 }
236
237 /**
238  * Returns the amount that the receiver's value will be
239  * modified by when the up/down (or right/left) arrows
240  * are pressed.
241  *
242  * @return the increment
243  *
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>
247  * </ul>
248  */
249 public int getIncrement () {
250         checkWidget();
251         return increment;
252 }
253
254 /**
255  * Returns the maximum value which the receiver will allow.
256  *
257  * @return the maximum
258  *
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>
262  * </ul>
263  */
264 public int getMaximum () {
265         checkWidget();
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);
272         return info.nMax;
273 }
274
275 /**
276  * Returns the minimum value which the receiver will allow.
277  *
278  * @return the minimum
279  *
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>
283  * </ul>
284  */
285 public int getMinimum () {
286         checkWidget();
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);
293         return info.nMin;
294 }
295
296 /**
297  * Returns the amount that the receiver's value will be
298  * modified by when the page increment/decrement areas
299  * are selected.
300  *
301  * @return the page increment
302  *
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>
306  * </ul>
307  */
308 public int getPageIncrement () {
309         checkWidget();
310         return pageIncrement;
311 }
312
313 /**
314  * Returns the receiver's parent, which must be a Scrollable.
315  *
316  * @return the receiver's parent
317  *
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>
321  * </ul>
322  */
323 public Scrollable getParent () {
324         checkWidget();
325         return parent;
326 }
327
328 /**
329  * Returns the single 'selection' that is the receiver's value.
330  *
331  * @return the selection
332  *
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>
336  * </ul>
337  */
338 public int getSelection () {
339         checkWidget();
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);
346         return info.nPos;
347 }
348
349 /**
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
353  * receiver.
354  *
355  * @return the receiver's size
356  *
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>
360  * </ul>
361  */
362 public Point getSize () {
363         checkWidget();
364         return DPIUtil.autoScaleDown(getSizeInPixels());
365 }
366
367 Point getSizeInPixels () {
368         parent.forceResize ();
369         RECT rect = new RECT ();
370         OS.GetClientRect (parent.scrolledHandle (), rect);
371         int width, height;
372         if ((style & SWT.HORIZONTAL) != 0) {
373                 width = rect.right - rect.left;
374                 height = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
375         } else {
376                 width = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
377                 height = rect.bottom - rect.top;
378         }
379         return new Point (width, height);
380 }
381
382 /**
383  * Returns the receiver's thumb value.
384  *
385  * @return the thumb value
386  *
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>
390  * </ul>
391  *
392  * @see ScrollBar
393  */
394 public int getThumb () {
395         checkWidget();
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;
403         return info.nPage;
404 }
405
406 /**
407  * Returns a rectangle describing the size and location of the
408  * receiver's thumb relative to its parent.
409  *
410  * @return the thumb bounds, relative to the {@link #getParent() parent}
411  *
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>
415  * </ul>
416  *
417  * @since 3.6
418  */
419 public Rectangle getThumbBounds () {
420         checkWidget();
421         return DPIUtil.autoScaleDown(getThumbBoundsInPixels());
422 }
423
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;
435         } else {
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;
441         }
442         RECT rect = new RECT ();
443         rect.left = x;
444         rect.top  = y;
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);
449 }
450
451 /**
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}.
455  *
456  * @return the thumb track bounds, relative to the {@link #getParent() parent}
457  *
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>
461  * </ul>
462  *
463  * @since 3.6
464  */
465 public Rectangle getThumbTrackBounds () {
466         checkWidget();
467         return DPIUtil.autoScaleDown(getThumbTrackBoundsInPixels());
468 }
469
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;
480                 height = size;
481                 if (width <= 2 * size) {
482                         x = info.rcScrollBar.left + width / 2;
483                         width = 0;
484                 } else {
485                         x = info.rcScrollBar.left + size;
486                         width -= 2 * size;
487                 }
488         } else {
489                 OS.GetScrollBarInfo(parent.handle, OS.OBJID_VSCROLL, info);
490                 int size = OS.GetSystemMetrics (OS.SM_CYVSCROLL);
491                 x = info.rcScrollBar.left;
492                 width = size;
493                 height = info.rcScrollBar.bottom - info.rcScrollBar.top;
494                 if (height <= 2 * size) {
495                         y = info.rcScrollBar.top + height / 2;
496                         height = 0;
497                 } else {
498                         y = info.rcScrollBar.top + size;
499                         height -= 2 * size;
500                 }
501         }
502         RECT rect = new RECT ();
503         rect.left = x;
504         rect.top  = y;
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);
509 }
510
511 /**
512  * Returns <code>true</code> if the receiver is visible, and
513  * <code>false</code> otherwise.
514  * <p>
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.
519  * </p>
520  *
521  * @return the receiver's visibility state
522  *
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>
526  * </ul>
527  */
528 public boolean getVisible () {
529         checkWidget();
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;
535 }
536
537 long hwndScrollBar () {
538         return parent.scrolledHandle ();
539 }
540
541 /**
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.
546  *
547  * @return the receiver's enabled state
548  *
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>
552  * </ul>
553  *
554  * @see #getEnabled
555  */
556 public boolean isEnabled () {
557         checkWidget();
558         return getEnabled () && parent.isEnabled ();
559 }
560
561 /**
562  * Returns <code>true</code> if the receiver is visible and all
563  * of the receiver's ancestors are visible and <code>false</code>
564  * otherwise.
565  *
566  * @return the receiver's visibility state
567  *
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>
571  * </ul>
572  *
573  * @see #getVisible
574  */
575 public boolean isVisible () {
576         checkWidget();
577         return getVisible () && parent.isVisible ();
578 }
579
580 @Override
581 void releaseHandle () {
582         super.releaseHandle ();
583         parent = null;
584 }
585
586 @Override
587 void releaseParent () {
588         super.releaseParent ();
589         if (parent.horizontalBar == this) parent.horizontalBar = null;
590         if (parent.verticalBar == this) parent.verticalBar = null;
591 }
592
593 /**
594  * Removes the listener from the collection of listeners who will
595  * be notified when the user changes the receiver's value.
596  *
597  * @param listener the listener which should no longer be notified
598  *
599  * @exception IllegalArgumentException <ul>
600  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
601  * </ul>
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>
605  * </ul>
606  *
607  * @see SelectionListener
608  * @see #addSelectionListener
609  */
610 public void removeSelectionListener (SelectionListener listener) {
611         checkWidget();
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);
616 }
617
618 int scrollBarType () {
619         return (style & SWT.VERTICAL) != 0 ? OS.SB_VERT : OS.SB_HORZ;
620 }
621
622 /**
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.
627  *
628  * @param enabled the new enabled state
629  *
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>
633  * </ul>
634  */
635 public void setEnabled (boolean enabled) {
636         checkWidget();
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);
641         if (enabled) {
642                 state &= ~DISABLED;
643         } else {
644                 state |= DISABLED;
645         }
646 }
647
648 /**
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
652  * one.
653  *
654  * @param value the new increment (must be greater than zero)
655  *
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>
659  * </ul>
660  */
661 public void setIncrement (int value) {
662         checkWidget();
663         if (value < 1) return;
664         increment = value;
665 }
666
667 /**
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
671  * new range.
672  *
673  * @param value the new maximum
674  *
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>
678  * </ul>
679  */
680 public void setMaximum (int value) {
681         checkWidget();
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;
690         info.nMax = value;
691         SetScrollInfo (hwnd, type, info, true);
692 }
693
694 /**
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
698  * the new range.
699  *
700  * @param value the new minimum
701  *
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>
705  * </ul>
706  */
707 public void setMinimum (int value) {
708         checkWidget();
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;
717         info.nMin = value;
718         SetScrollInfo (hwnd, type, info, true);
719 }
720
721 /**
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
725  * one.
726  *
727  * @param value the page increment (must be greater than zero)
728  *
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>
732  * </ul>
733  */
734 public void setPageIncrement (int value) {
735         checkWidget();
736         if (value < 1) return;
737         pageIncrement = value;
738 }
739
740 boolean SetScrollInfo (long hwnd, int flags, SCROLLINFO info, boolean fRedraw) {
741         /*
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.
748         */
749         boolean barVisible = false;
750         boolean visible = getVisible ();
751
752         ScrollBar bar = null;
753         switch (flags) {
754                 case OS.SB_HORZ:
755                         bar = parent.getVerticalBar ();
756                         break;
757                 case OS.SB_VERT:
758                         bar = parent.getHorizontalBar ();
759                         break;
760         }
761         barVisible = bar != null && bar.getVisible ();
762         if (!visible || (state & DISABLED) != 0) fRedraw = false;
763         boolean result = OS.SetScrollInfo (hwnd, flags, info, fRedraw);
764
765         /*
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.
773         */
774         if (!visible) {
775                 OS.ShowScrollBar (hwnd, !barVisible ? OS.SB_BOTH : flags, false);
776         }
777
778         /*
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.
786         *
787         * NOTE: This problem only happens on Vista
788         */
789         if (visible && bar != null && !barVisible) {
790                 OS.ShowScrollBar (hwnd, flags == OS.SB_HORZ ? OS.SB_VERT : OS.SB_HORZ, false);
791         }
792
793         /*
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.
800         */
801         if ((state & DISABLED) != 0) {
802                 OS.EnableScrollBar (hwnd, flags, OS.ESB_DISABLE_BOTH);
803         }
804         return result;
805 }
806
807 /**
808  * Sets the single <em>selection</em> that is the receiver's
809  * value to the argument which must be greater than or equal
810  * to zero.
811  *
812  * @param selection the new selection (must be zero or greater)
813  *
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>
817  * </ul>
818  */
819 public void setSelection (int selection) {
820         checkWidget();
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);
828 }
829
830 /**
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.
834  * <p>
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.
837  * </p>
838  *
839  * @param value the new thumb value, which must be at least one and not
840  * larger than the size of the current range
841  *
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>
845  * </ul>
846  */
847 public void setThumb (int value) {
848         checkWidget();
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);
856         info.nPage = value;
857         if (info.nPage != 0) info.nPage++;
858         SetScrollInfo (hwnd, type, info, true);
859 }
860
861 /**
862  * Sets the receiver's selection, minimum value, maximum
863  * value, thumb, increment and page increment all at once.
864  * <p>
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.
868  * </p>
869  *
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
876  *
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>
880  * </ul>
881  */
882 public void setValues (int selection, int minimum, int maximum, int thumb, int increment, int pageIncrement) {
883         checkWidget();
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;
895         info.nMin = minimum;
896         info.nMax = maximum;
897         info.nPage = thumb;
898         if (info.nPage != 0) info.nPage++;
899         long hwnd = hwndScrollBar ();
900         int type = scrollBarType ();
901         SetScrollInfo (hwnd, type, info, true);
902 }
903
904 /**
905  * Marks the receiver as visible if the argument is <code>true</code>,
906  * and marks it invisible otherwise.
907  * <p>
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.
911  * </p>
912  *
913  * @param visible the new visibility state
914  *
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>
918  * </ul>
919  */
920 public void setVisible (boolean visible) {
921         checkWidget();
922         if (visible == getVisible ()) return;
923         /*
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.
930         */
931         state = visible ? state & ~HIDDEN : state | HIDDEN;
932         long hwnd = hwndScrollBar ();
933         int type = scrollBarType ();
934         /*
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
938         * bar and hide both.
939         */
940         if (!visible) {
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);
948                                 type = OS.SB_BOTH;
949                         }
950                 }
951         }
952         if (OS.ShowScrollBar (hwnd, type, visible)) {
953                 /*
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
964                 * visible.
965                 */
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);
973                         }
974                 }
975                 sendEvent (visible ? SWT.Show : SWT.Hide);
976                 // widget could be disposed at this point
977         }
978 }
979
980 LRESULT wmScrollChild (long wParam, long lParam) {
981
982         /* Do nothing when scrolling is ending */
983         int code = OS.LOWORD (wParam);
984         if (code == OS.SB_ENDSCROLL) return null;
985
986         /*
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
990         * user is scrolling.
991         */
992         Event event = new Event ();
993         switch (code) {
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;
1002         }
1003         sendSelectionEvent  (SWT.Selection, event, true);
1004         // the widget could be destroyed at this point
1005         return null;
1006 }
1007
1008 }