KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > ScrollBar


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
20  * Instances of this class are selectable user interface
21  * objects that represent a range of positive, numeric values.
22  * <p>
23  * At any given moment, a given scroll bar will have a
24  * single 'selection' that is considered to be its
25  * value, which is constrained to be within the range of
26  * values the scroll bar represents (that is, between its
27  * <em>minimum</em> and <em>maximum</em> values).
28  * </p><p>
29  * Typically, scroll bars will be made up of five areas:
30  * <ol>
31  * <li>an arrow button for decrementing the value</li>
32  * <li>a page decrement area for decrementing the value by a larger amount</li>
33  * <li>a <em>thumb</em> for modifying the value by mouse dragging</li>
34  * <li>a page increment area for incrementing the value by a larger amount</li>
35  * <li>an arrow button for incrementing the value</li>
36  * </ol>
37  * Based on their style, scroll bars are either <code>HORIZONTAL</code>
38  * (which have a left facing button for decrementing the value and a
39  * right facing button for incrementing it) or <code>VERTICAL</code>
40  * (which have an upward facing button for decrementing the value
41  * and a downward facing buttons for incrementing it).
42  * </p><p>
43  * On some platforms, the size of the scroll bar's thumb can be
44  * varied relative to the magnitude of the range of values it
45  * represents (that is, relative to the difference between its
46  * maximum and minimum values). Typically, this is used to
47  * indicate some proportional value such as the ratio of the
48  * visible area of a document to the total amount of space that
49  * it would take to display it. SWT supports setting the thumb
50  * size even if the underlying platform does not, but in this
51  * case the appearance of the scroll bar will not change.
52  * </p><p>
53  * Scroll bars are created by specifying either <code>H_SCROLL</code>,
54  * <code>V_SCROLL</code> or both when creating a <code>Scrollable</code>.
55  * They are accessed from the <code>Scrollable</code> using
56  * <code>getHorizontalBar</code> and <code>getVerticalBar</code>.
57  * </p><p>
58  * Note: Scroll bars are not Controls. On some platforms, scroll bars
59  * that appear as part of some standard controls such as a text or list
60  * have no operating system resources and are not children of the control.
61  * For this reason, scroll bars are treated specially. To create a control
62  * that looks like a scroll bar but has operating system resources, use
63  * <code>Slider</code>.
64  * </p>
65  * <dl>
66  * <dt><b>Styles:</b></dt>
67  * <dd>HORIZONTAL, VERTICAL</dd>
68  * <dt><b>Events:</b></dt>
69  * <dd>Selection</dd>
70  * </dl>
71  * <p>
72  * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
73  * </p><p>
74  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
75  * </p>
76  *
77  * @see Slider
78  * @see Scrollable
79  * @see Scrollable#getHorizontalBar
80  * @see Scrollable#getVerticalBar
81  */

82
83 public class ScrollBar extends Widget {
84     Scrollable parent;
85     int increment, pageIncrement;
86
87 /**
88  * Constructs a new instance of this class given its parent
89  * and a style value describing its behavior and appearance.
90  * <p>
91  * The style value is either one of the style constants defined in
92  * class <code>SWT</code> which is applicable to instances of this
93  * class, or must be built by <em>bitwise OR</em>'ing together
94  * (that is, using the <code>int</code> "|" operator) two or more
95  * of those <code>SWT</code> style constants. The class description
96  * lists the style constants that are applicable to the class.
97  * Style bits are also inherited from superclasses.
98  * </p>
99  *
100  * @param parent a composite control which will be the parent of the new instance (cannot be null)
101  * @param style the style of control to construct
102  *
103  * @exception IllegalArgumentException <ul>
104  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
105  * </ul>
106  * @exception SWTException <ul>
107  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
108  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
109  * </ul>
110  *
111  * @see SWT#HORIZONTAL
112  * @see SWT#VERTICAL
113  * @see Widget#checkSubclass
114  * @see Widget#getStyle
115  */

116 ScrollBar (Scrollable parent, int style) {
117     super (parent, checkStyle (style));
118     this.parent = parent;
119     createWidget ();
120 }
121
122 /**
123  * Adds the listener to the collection of listeners who will
124  * be notified when the user changes the receiver's value, by sending
125  * it one of the messages defined in the <code>SelectionListener</code>
126  * interface.
127  * <p>
128  * When <code>widgetSelected</code> is called, the event object detail field contains one of the following values:
129  * <code>SWT.NONE</code> - for the end of a drag.
130  * <code>SWT.DRAG</code>.
131  * <code>SWT.HOME</code>.
132  * <code>SWT.END</code>.
133  * <code>SWT.ARROW_DOWN</code>.
134  * <code>SWT.ARROW_UP</code>.
135  * <code>SWT.PAGE_DOWN</code>.
136  * <code>SWT.PAGE_UP</code>.
137  * <code>widgetDefaultSelected</code> is not called.
138  * </p>
139  *
140  * @param listener the listener which should be notified when the user changes the receiver's value
141  *
142  * @exception IllegalArgumentException <ul>
143  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
144  * </ul>
145  * @exception SWTException <ul>
146  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
147  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
148  * </ul>
149  *
150  * @see SelectionListener
151  * @see #removeSelectionListener
152  * @see SelectionEvent
153  */

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     /*
170     * Do not set the initial values of the maximum
171     * or the thumb. These values normally default
172     * to 100 and 10 but may have been set already
173     * by the widget that owns the scroll bar. For
174     * example, a scroll bar that is created for a
175     * list widget, setting these defaults would
176     * override the initial values provided by the
177     * list widget.
178     */

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 // checkWidget ();
199
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 /**
216  * Returns <code>true</code> if the receiver is enabled, and
217  * <code>false</code> otherwise. A disabled control is typically
218  * not selectable from the user interface and draws with an
219  * inactive or "grayed" look.
220  *
221  * @return the receiver's enabled state
222  *
223  * @exception SWTException <ul>
224  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
225  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
226  * </ul>
227  *
228  * @see #isEnabled
229  */

230 public boolean getEnabled () {
231     checkWidget();
232     return (state & DISABLED) == 0;
233 }
234
235 /**
236  * Returns the amount that the receiver's value will be
237  * modified by when the up/down (or right/left) arrows
238  * are pressed.
239  *
240  * @return the increment
241  *
242  * @exception SWTException <ul>
243  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
244  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
245  * </ul>
246  */

247 public int getIncrement () {
248     checkWidget();
249     return increment;
250 }
251
252 /**
253  * Returns the maximum value which the receiver will allow.
254  *
255  * @return the maximum
256  *
257  * @exception SWTException <ul>
258  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
259  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
260  * </ul>
261  */

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 /**
274  * Returns the minimum value which the receiver will allow.
275  *
276  * @return the minimum
277  *
278  * @exception SWTException <ul>
279  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
280  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
281  * </ul>
282  */

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 /**
295  * Returns the amount that the receiver's value will be
296  * modified by when the page increment/decrement areas
297  * are selected.
298  *
299  * @return the page increment
300  *
301  * @exception SWTException <ul>
302  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
303  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
304  * </ul>
305  */

306 public int getPageIncrement () {
307     checkWidget();
308     return pageIncrement;
309 }
310
311 /**
312  * Returns the receiver's parent, which must be a Scrollable.
313  *
314  * @return the receiver's parent
315  *
316  * @exception SWTException <ul>
317  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
318  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
319  * </ul>
320  */

321 public Scrollable getParent () {
322     checkWidget();
323     return parent;
324 }
325
326 /**
327  * Returns the single 'selection' that is the receiver's value.
328  *
329  * @return the selection
330  *
331  * @exception SWTException <ul>
332  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
333  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
334  * </ul>
335  */

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 /**
348  * Returns a point describing the receiver's size. The
349  * x coordinate of the result is the width of the receiver.
350  * The y coordinate of the result is the height of the
351  * receiver.
352  *
353  * @return the receiver's size
354  *
355  * @exception SWTException <ul>
356  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
357  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
358  * </ul>
359  */

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 /**
377  * Returns the size of the receiver's thumb relative to the
378  * difference between its maximum and minimum values.
379  *
380  * @return the thumb value
381  *
382  * @exception SWTException <ul>
383  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
384  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
385  * </ul>
386  *
387  * @see ScrollBar
388  */

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 /**
402  * Returns <code>true</code> if the receiver is visible, and
403  * <code>false</code> otherwise.
404  * <p>
405  * If one of the receiver's ancestors is not visible or some
406  * other condition makes the receiver not visible, this method
407  * may still indicate that it is considered visible even though
408  * it may not actually be showing.
409  * </p>
410  *
411  * @return the receiver's visibility state
412  *
413  * @exception SWTException <ul>
414  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
415  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
416  * </ul>
417  */

418 public boolean getVisible () {
419     checkWidget();
420     return (state & HIDDEN) == 0;
421 }
422
423 int hwndScrollBar () {
424     return parent.scrolledHandle ();
425 }
426
427 /**
428  * Returns <code>true</code> if the receiver is enabled and all
429  * of the receiver's ancestors are enabled, and <code>false</code>
430  * otherwise. A disabled control is typically not selectable from the
431  * user interface and draws with an inactive or "grayed" look.
432  *
433  * @return the receiver's enabled state
434  *
435  * @exception SWTException <ul>
436  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
437  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
438  * </ul>
439  *
440  * @see #getEnabled
441  */

442 public boolean isEnabled () {
443     checkWidget();
444     return getEnabled () && parent.isEnabled ();
445 }
446
447 /**
448  * Returns <code>true</code> if the receiver is visible and all
449  * of the receiver's ancestors are visible and <code>false</code>
450  * otherwise.
451  *
452  * @return the receiver's visibility state
453  *
454  * @exception SWTException <ul>
455  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
456  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
457  * </ul>
458  *
459  * @see #getVisible
460  */

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 /**
478  * Removes the listener from the collection of listeners who will
479  * be notified when the user changes the receiver's value.
480  *
481  * @param listener the listener which should no longer be notified
482  *
483  * @exception IllegalArgumentException <ul>
484  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
485  * </ul>
486  * @exception SWTException <ul>
487  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
488  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
489  * </ul>
490  *
491  * @see SelectionListener
492  * @see #addSelectionListener
493  */

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 /**
507  * Enables the receiver if the argument is <code>true</code>,
508  * and disables it otherwise. A disabled control is typically
509  * not selectable from the user interface and draws with an
510  * inactive or "grayed" look.
511  *
512  * @param enabled the new enabled state
513  *
514  * @exception SWTException <ul>
515  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
516  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
517  * </ul>
518  */

519 public void setEnabled (boolean enabled) {
520     checkWidget();
521     /*
522     * This line is intentionally commented. Currently
523     * always show scrollbar as being enabled and visible.
524     */

525 // if (OS.IsWinCE) error (SWT.ERROR_NOT_IMPLEMENTED);
526
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 /**
539  * Sets the amount that the receiver's value will be
540  * modified by when the up/down (or right/left) arrows
541  * are pressed to the argument, which must be at least
542  * one.
543  *
544  * @param value the new increment (must be greater than zero)
545  *
546  * @exception SWTException <ul>
547  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
548  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
549  * </ul>
550  */

551 public void setIncrement (int value) {
552     checkWidget();
553     if (value < 1) return;
554     increment = value;
555 }
556
557 /**
558  * Sets the maximum. If this value is negative or less than or
559  * equal to the minimum, the value is ignored. If necessary, first
560  * the thumb and then the selection are adjusted to fit within the
561  * new range.
562  *
563  * @param value the new maximum
564  *
565  * @exception SWTException <ul>
566  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
567  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
568  * </ul>
569  */

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 /**
584  * Sets the minimum value. If this value is negative or greater
585  * than or equal to the maximum, the value is ignored. If necessary,
586  * first the thumb and then the selection are adjusted to fit within
587  * the new range.
588  *
589  * @param value the new minimum
590  *
591  * @exception SWTException <ul>
592  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
593  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
594  * </ul>
595  */

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 /**
610  * Sets the amount that the receiver's value will be
611  * modified by when the page increment/decrement areas
612  * are selected to the argument, which must be at least
613  * one.
614  *
615  * @param value the page increment (must be greater than zero)
616  *
617  * @exception SWTException <ul>
618  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
619  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
620  * </ul>
621  */

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     /*
630     * Bug in Windows. For some reason, when SetScrollInfo()
631     * is used with SIF_POS and the scroll bar is hidden,
632     * the opposite scroll bar is incorrectly made visible
633     * so that the next time the parent is resized (or another
634     * scroll bar operation is performed), the opposite scroll
635     * bar draws. The fix is to hide both scroll bars.
636     */

637     if ((state & (DISABLED | HIDDEN)) != 0) fRedraw = false;
638     boolean result = OS.SetScrollInfo (hwnd, flags, info, fRedraw);
639     
640     /*
641     * Bug in Windows. For some reason, when the widget
642     * is a standard scroll bar, and SetScrollInfo() is
643     * called with SIF_RANGE or SIF_PAGE, the widget is
644     * incorrectly made visible so that the next time the
645     * parent is resized (or another scroll bar operation
646     * is performed), the scroll bar draws. The fix is
647     * to hide the scroll bar (again) when already hidden.
648     */

649     if ((state & HIDDEN) != 0) {
650         /*
651         * This line is intentionally commented. Currently
652         * always show scrollbar as being enabled and visible.
653         */

654 // if (OS.IsWinCE) error (SWT.ERROR_NOT_IMPLEMENTED);
655
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     /*
671     * Feature in Windows. Using SIF_DISABLENOSCROLL,
672     * SetScrollInfo () can change enabled and disabled
673     * state of the scroll bar causing a scroll bar that
674     * was disabled by the application to become enabled.
675     * The fix is to disable the scroll bar (again) when
676     * the application has disabled the scroll bar.
677     */

678     if ((state & DISABLED) != 0) {
679         /*
680         * This line is intentionally commented. Currently
681         * always show scrollbar as being enabled and visible.
682         */

683 // if (OS.IsWinCE) error (SWT.ERROR_NOT_IMPLEMENTED);
684
if (!OS.IsWinCE) {
685             OS.EnableScrollBar (hwnd, flags, OS.ESB_DISABLE_BOTH);
686         }
687     }
688     return result;
689 }
690
691 /**
692  * Sets the single <em>selection</em> that is the receiver's
693  * value to the argument which must be greater than or equal
694  * to zero.
695  *
696  * @param selection the new selection (must be zero or greater)
697  *
698  * @exception SWTException <ul>
699  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
700  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
701  * </ul>
702  */

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 /**
714  * Sets the size of the receiver's thumb relative to the
715  * difference between its maximum and minimum values. This new
716  * value will be ignored if it is less than one, and will be
717  * clamped if it exceeds the receiver's current range.
718  *
719  * @param value the new thumb value, which must be at least one and not
720  * larger than the size of the current range
721  *
722  * @exception SWTException <ul>
723  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
724  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
725  * </ul>
726  */

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 /**
741  * Sets the receiver's selection, minimum value, maximum
742  * value, thumb, increment and page increment all at once.
743  * <p>
744  * Note: This is similar to setting the values individually
745  * using the appropriate methods, but may be implemented in a
746  * more efficient fashion on some platforms.
747  * </p>
748  *
749  * @param selection the new selection value
750  * @param minimum the new minimum value
751  * @param maximum the new maximum value
752  * @param thumb the new thumb value
753  * @param increment the new increment value
754  * @param pageIncrement the new pageIncrement value
755  *
756  * @exception SWTException <ul>
757  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
758  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
759  * </ul>
760  */

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 /**
783  * Marks the receiver as visible if the argument is <code>true</code>,
784  * and marks it invisible otherwise.
785  * <p>
786  * If one of the receiver's ancestors is not visible or some
787  * other condition makes the receiver not visible, marking
788  * it visible may not actually cause it to be displayed.
789  * </p>
790  *
791  * @param visible the new visibility state
792  *
793  * @exception SWTException <ul>
794  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
795  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
796  * </ul>
797  */

798 public void setVisible (boolean visible) {
799     checkWidget();
800     boolean isVisible = (state & HIDDEN) == 0;
801     if (isVisible == visible) return;
802     
803     /*
804     * On Windows CE, use SIF_DISABLENOSCROLL to show and
805     * hide the scroll bar when the page size is equal to
806     * the range.
807     */

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             /*
817             * Bug in Windows. When the only changed flag to
818             * SetScrollInfo () is OS.SIF_DISABLENOSCROLL,
819             * Windows does not update the scroll bar state.
820             * The fix is to increase and then decrease the
821             * maximum, causing Windows to honour the flag.
822             */

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             /*
830             * This line is intentionally commented. Currently
831             * always show scrollbar as being enabled and visible.
832             */

833 // if (OS.IsWinCE) error (SWT.ERROR_NOT_IMPLEMENTED);
834
}
835         return;
836     }
837     
838     /*
839     * Set the state bits before calling ShowScrollBar ()
840     * because hiding and showing the scroll bar can cause
841     * WM_SIZE messages when the client area is resized.
842     * Setting the state before the call means that code
843     * that runs during WM_SIZE that queries the visibility
844     * of the scroll bar will get the correct value.
845     */

846     state = visible ? state & ~HIDDEN : state | HIDDEN;
847     int hwnd = hwndScrollBar (), type = scrollBarType ();
848     if (OS.ShowScrollBar (hwnd, type, visible)) {
849         /*
850         * Bug in Windows. For some reason, when the widget
851         * is a standard scroll bar, and SetScrollInfo () is
852         * called with SIF_RANGE or SIF_PAGE while the widget
853         * is not visible, the widget is incorrectly disabled
854         * even though the values for SIF_RANGE and SIF_PAGE,
855         * when set for a visible scroll bar would not disable
856         * the scroll bar. The fix is to enable the scroll bar
857         * when not disabled by the application and the current
858         * scroll bar ranges would cause the scroll bar to be
859         * enabled had they been set when the scroll bar was
860         * visible.
861         */

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         // widget could be disposed at this point
873
}
874 }
875
876 LRESULT wmScrollChild (int wParam, int lParam) {
877
878     /* Do nothing when scrolling is ending */
879     int code = wParam & 0xFFFF;
880     if (code == OS.SB_ENDSCROLL) return null;
881
882     /*
883     * Send the event because WM_HSCROLL and
884     * WM_VSCROLL are sent from a modal message
885     * loop in Windows that is active when the
886     * user is scrolling.
887     */

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     // the widget could be destroyed at this point
901
return null;
902 }
903
904 }
905
Popular Tags