KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 /**
19  * This class is the abstract superclass of all classes which
20  * represent controls that have standard scroll bars.
21  * <dl>
22  * <dt><b>Styles:</b></dt>
23  * <dd>H_SCROLL, V_SCROLL</dd>
24  * <dt><b>Events:</b>
25  * <dd>(none)</dd>
26  * </dl>
27  * <p>
28  * IMPORTANT: This class is intended to be subclassed <em>only</em>
29  * within the SWT implementation.
30  * </p>
31  */

32
33 public abstract class Scrollable extends Control {
34     ScrollBar horizontalBar, verticalBar;
35
36 /**
37  * Prevents uninitialized instances from being created outside the package.
38  */

39 Scrollable () {
40 }
41
42 /**
43  * Constructs a new instance of this class given its parent
44  * and a style value describing its behavior and appearance.
45  * <p>
46  * The style value is either one of the style constants defined in
47  * class <code>SWT</code> which is applicable to instances of this
48  * class, or must be built by <em>bitwise OR</em>'ing together
49  * (that is, using the <code>int</code> "|" operator) two or more
50  * of those <code>SWT</code> style constants. The class description
51  * lists the style constants that are applicable to the class.
52  * Style bits are also inherited from superclasses.
53  * </p>
54  *
55  * @param parent a composite control which will be the parent of the new instance (cannot be null)
56  * @param style the style of control to construct
57  *
58  * @exception IllegalArgumentException <ul>
59  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
60  * </ul>
61  * @exception SWTException <ul>
62  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
63  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
64  * </ul>
65  *
66  * @see SWT#H_SCROLL
67  * @see SWT#V_SCROLL
68  * @see Widget#checkSubclass
69  * @see Widget#getStyle
70  */

71 public Scrollable (Composite parent, int style) {
72     super (parent, style);
73 }
74
75 int callWindowProc (int hwnd, int msg, int wParam, int lParam) {
76     if (handle == 0) return 0;
77     return OS.DefWindowProc (hwnd, msg, wParam, lParam);
78 }
79
80 /**
81  * Given a desired <em>client area</em> for the receiver
82  * (as described by the arguments), returns the bounding
83  * rectangle which would be required to produce that client
84  * area.
85  * <p>
86  * In other words, it returns a rectangle such that, if the
87  * receiver's bounds were set to that rectangle, the area
88  * of the receiver which is capable of displaying data
89  * (that is, not covered by the "trimmings") would be the
90  * rectangle described by the arguments (relative to the
91  * receiver's parent).
92  * </p>
93  *
94  * @param x the desired x coordinate of the client area
95  * @param y the desired y coordinate of the client area
96  * @param width the desired width of the client area
97  * @param height the desired height of the client area
98  * @return the required bounds to produce the given client area
99  *
100  * @exception SWTException <ul>
101  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
102  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
103  * </ul>
104  *
105  * @see #getClientArea
106  */

107 public Rectangle computeTrim (int x, int y, int width, int height) {
108     checkWidget ();
109     int scrolledHandle = scrolledHandle ();
110     RECT rect = new RECT ();
111     OS.SetRect (rect, x, y, x + width, y + height);
112     int bits1 = OS.GetWindowLong (scrolledHandle, OS.GWL_STYLE);
113     int bits2 = OS.GetWindowLong (scrolledHandle, OS.GWL_EXSTYLE);
114     OS.AdjustWindowRectEx (rect, bits1, false, bits2);
115     if (horizontalBar != null) rect.bottom += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
116     if (verticalBar != null) rect.right += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
117     int nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top;
118     return new Rectangle (rect.left, rect.top, nWidth, nHeight);
119 }
120
121 ScrollBar createScrollBar (int type) {
122     ScrollBar bar = new ScrollBar (this, type);
123     if ((state & CANVAS) != 0) {
124         bar.setMaximum (100);
125         bar.setThumb (10);
126     }
127     return bar;
128 }
129
130 void createWidget () {
131     super.createWidget ();
132     if ((style & SWT.H_SCROLL) != 0) horizontalBar = createScrollBar (SWT.H_SCROLL);
133     if ((style & SWT.V_SCROLL) != 0) verticalBar = createScrollBar (SWT.V_SCROLL);
134 }
135
136 /**
137  * Returns a rectangle which describes the area of the
138  * receiver which is capable of displaying data (that is,
139  * not covered by the "trimmings").
140  *
141  * @return the client area
142  *
143  * @exception SWTException <ul>
144  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
145  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
146  * </ul>
147  *
148  * @see #computeTrim
149  */

150 public Rectangle getClientArea () {
151     checkWidget ();
152     forceResize ();
153     RECT rect = new RECT ();
154     int scrolledHandle = scrolledHandle ();
155     OS.GetClientRect (scrolledHandle, rect);
156     int x = rect.left, y = rect.top;
157     int width = rect.right - rect.left;
158     int height = rect.bottom - rect.top;
159     if (scrolledHandle != handle) {
160         OS.GetClientRect (handle, rect);
161         OS.MapWindowPoints(handle, scrolledHandle, rect, 2);
162         x = -rect.left;
163         y = -rect.top;
164     }
165     return new Rectangle (x, y, width, height);
166 }
167
168 /**
169  * Returns the receiver's horizontal scroll bar if it has
170  * one, and null if it does not.
171  *
172  * @return the horizontal scroll bar (or null)
173  *
174  * @exception SWTException <ul>
175  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
176  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
177  * </ul>
178  */

179 public ScrollBar getHorizontalBar () {
180     checkWidget ();
181     return horizontalBar;
182 }
183
184 /**
185  * Returns the receiver's vertical scroll bar if it has
186  * one, and null if it does not.
187  *
188  * @return the vertical scroll bar (or null)
189  *
190  * @exception SWTException <ul>
191  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
192  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
193  * </ul>
194  */

195 public ScrollBar getVerticalBar () {
196     checkWidget ();
197     return verticalBar;
198 }
199
200 void releaseChildren (boolean destroy) {
201     if (horizontalBar != null) {
202         horizontalBar.release (false);
203         horizontalBar = null;
204     }
205     if (verticalBar != null) {
206         verticalBar.release (false);
207         verticalBar = null;
208     }
209     super.releaseChildren (destroy);
210 }
211
212 int scrolledHandle () {
213     return handle;
214 }
215
216 int widgetExtStyle () {
217     return super.widgetExtStyle ();
218     /*
219     * This code is intentionally commented. In future,
220     * we may wish to support different standard Windows
221     * edge styles. The issue here is that not all of
222     * these styles are available on the other platforms
223     * this would need to be a hint.
224     */

225 // if ((style & SWT.BORDER) != 0) return OS.WS_EX_CLIENTEDGE;
226
// if ((style & SWT.SHADOW_IN) != 0) return OS.WS_EX_STATICEDGE;
227
// return super.widgetExtStyle ();
228
}
229
230 int widgetStyle () {
231     int bits = super.widgetStyle () | OS.WS_TABSTOP;
232     if ((style & SWT.H_SCROLL) != 0) bits |= OS.WS_HSCROLL;
233     if ((style & SWT.V_SCROLL) != 0) bits |= OS.WS_VSCROLL;
234     return bits;
235 }
236
237 TCHAR windowClass () {
238     return display.windowClass;
239 }
240
241 int windowProc () {
242     return display.windowProc;
243 }
244
245 LRESULT WM_HSCROLL (int wParam, int lParam) {
246     LRESULT result = super.WM_HSCROLL (wParam, lParam);
247     if (result != null) return result;
248     
249     /*
250     * Bug on WinCE. lParam should be NULL when the message is not sent
251     * by a scroll bar control, but it contains the handle to the window.
252     * When the message is sent by a scroll bar control, it correctly
253     * contains the handle to the scroll bar. The fix is to check for
254     * both.
255     */

256     if (horizontalBar != null && (lParam == 0 || lParam == handle)) {
257         return wmScroll (horizontalBar, (state & CANVAS) != 0, handle, OS.WM_HSCROLL, wParam, lParam);
258     }
259     return result;
260 }
261
262 LRESULT WM_MOUSEWHEEL (int wParam, int lParam) {
263     LRESULT result = super.WM_MOUSEWHEEL (wParam, lParam);
264     if (result != null) return result;
265     
266     /*
267     * Translate WM_MOUSEWHEEL to WM_VSCROLL or WM_HSCROLL.
268     */

269     if ((state & CANVAS) != 0) {
270         if ((wParam & (OS.MK_SHIFT | OS.MK_CONTROL)) != 0) return result;
271         boolean vertical = verticalBar != null && verticalBar.getEnabled ();
272         boolean horizontal = horizontalBar != null && horizontalBar.getEnabled ();
273         int msg = (vertical) ? OS.WM_VSCROLL : (horizontal) ? OS.WM_HSCROLL : 0;
274         if (msg == 0) return result;
275         int [] value = new int [1];
276         OS.SystemParametersInfo (OS.SPI_GETWHEELSCROLLLINES, 0, value, 0);
277         int delta = (short) (wParam >> 16);
278         int code = 0, count = 0;
279         if (value [0] == OS.WHEEL_PAGESCROLL) {
280             code = delta < 0 ? OS.SB_PAGEDOWN : OS.SB_PAGEUP;
281             count = Math.abs (delta / OS.WHEEL_DELTA);
282         } else {
283             code = delta < 0 ? OS.SB_LINEDOWN : OS.SB_LINEUP;
284             delta = Math.abs (delta);
285             if (delta < OS.WHEEL_DELTA) return result;
286             if (msg == OS.WM_VSCROLL) {
287                 count = value [0] * delta / OS.WHEEL_DELTA;
288             } else {
289                 count = delta / OS.WHEEL_DELTA;
290             }
291         }
292         for (int i=0; i<count; i++) {
293             OS.SendMessage (handle, msg, code, 0);
294         }
295         return LRESULT.ZERO;
296     }
297         
298     /*
299     * When the native widget scrolls inside WM_MOUSEWHEEL, it
300     * may or may not send a WM_VSCROLL or WM_HSCROLL to do the
301     * actual scrolling. This depends on the implementation of
302     * each native widget. In order to ensure that application
303     * code is notified when the scroll bar moves, compare the
304     * scroll bar position before and after the WM_MOUSEWHEEL.
305     * If the native control sends a WM_VSCROLL or WM_HSCROLL,
306     * then the application has already been notified. If not
307     * explicitly send the event.
308     */

309     int vPosition = verticalBar == null ? 0 : verticalBar.getSelection ();
310     int hPosition = horizontalBar == null ? 0 : horizontalBar.getSelection ();
311     int code = callWindowProc (handle, OS.WM_MOUSEWHEEL, wParam, lParam);
312     if (verticalBar != null) {
313         int position = verticalBar.getSelection ();
314         if (position != vPosition) {
315             Event event = new Event ();
316             event.detail = position < vPosition ? SWT.PAGE_UP : SWT.PAGE_DOWN;
317             verticalBar.sendEvent (SWT.Selection, event);
318         }
319     }
320     if (horizontalBar != null) {
321         int position = horizontalBar.getSelection ();
322         if (position != hPosition) {
323             Event event = new Event ();
324             event.detail = position < hPosition ? SWT.PAGE_UP : SWT.PAGE_DOWN;
325             horizontalBar.sendEvent (SWT.Selection, event);
326         }
327     }
328     return new LRESULT (code);
329 }
330
331 LRESULT WM_SIZE (int wParam, int lParam) {
332     int code = callWindowProc (handle, OS.WM_SIZE, wParam, lParam);
333     super.WM_SIZE (wParam, lParam);
334     // widget may be disposed at this point
335
if (code == 0) return LRESULT.ZERO;
336     return new LRESULT (code);
337 }
338
339 LRESULT WM_VSCROLL (int wParam, int lParam) {
340     LRESULT result = super.WM_VSCROLL (wParam, lParam);
341     if (result != null) return result;
342     /*
343     * Bug on WinCE. lParam should be NULL when the message is not sent
344     * by a scroll bar control, but it contains the handle to the window.
345     * When the message is sent by a scroll bar control, it correctly
346     * contains the handle to the scroll bar. The fix is to check for
347     * both.
348     */

349     if (verticalBar != null && (lParam == 0 || lParam == handle)) {
350         return wmScroll (verticalBar, (state & CANVAS) != 0, handle, OS.WM_VSCROLL, wParam, lParam);
351     }
352     return result;
353 }
354
355 LRESULT wmScroll (ScrollBar bar, boolean update, int hwnd, int msg, int wParam, int lParam) {
356     LRESULT result = null;
357     if (update) {
358         int type = msg == OS.WM_HSCROLL ? OS.SB_HORZ : OS.SB_VERT;
359         SCROLLINFO info = new SCROLLINFO ();
360         info.cbSize = SCROLLINFO.sizeof;
361         info.fMask = OS.SIF_TRACKPOS | OS.SIF_POS | OS.SIF_RANGE;
362         OS.GetScrollInfo (hwnd, type, info);
363         info.fMask = OS.SIF_POS;
364         int code = wParam & 0xFFFF;
365         switch (code) {
366             case OS.SB_ENDSCROLL: return null;
367             case OS.SB_THUMBPOSITION:
368             case OS.SB_THUMBTRACK:
369                 /*
370                 * Note: On WinCE, the value in SB_THUMBPOSITION is relative to nMin.
371                 * Same for SB_THUMBPOSITION 'except' for the very first thumb track
372                 * message which has the actual value of nMin. This is a problem when
373                 * nMin is not zero.
374                 */

375                 info.nPos = info.nTrackPos;
376                 break;
377             case OS.SB_TOP:
378                 info.nPos = info.nMin;
379                 break;
380             case OS.SB_BOTTOM:
381                 info.nPos = info.nMax;
382                 break;
383             case OS.SB_LINEDOWN:
384                 info.nPos += bar.getIncrement ();
385                 break;
386             case OS.SB_LINEUP:
387                 int increment = bar.getIncrement ();
388                 info.nPos = Math.max (info.nMin, info.nPos - increment);
389                 break;
390             case OS.SB_PAGEDOWN:
391                 info.nPos += bar.getPageIncrement ();
392                 break;
393             case OS.SB_PAGEUP:
394                 int pageIncrement = bar.getPageIncrement ();
395                 info.nPos = Math.max (info.nMin, info.nPos - pageIncrement);
396                 break;
397         }
398         OS.SetScrollInfo (hwnd, type, info, true);
399     } else {
400         int code = callWindowProc (hwnd, msg, wParam, lParam);
401         result = code == 0 ? LRESULT.ZERO : new LRESULT (code);
402     }
403     bar.wmScrollChild (wParam, lParam);
404     return result;
405 }
406
407 }
408
Popular Tags