1 /*******************************************************************************
2 * Copyright (c) 2000, 2015 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.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
23 * This class is the abstract superclass of all classes which
24 * represent controls that have standard scroll bars.
26 * <dt><b>Styles:</b></dt>
27 * <dd>H_SCROLL, V_SCROLL</dd>
32 * IMPORTANT: This class is intended to be subclassed <em>only</em>
33 * within the SWT implementation.
36 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
37 * @noextend This class is not intended to be subclassed by clients.
39 public abstract class Scrollable extends Control {
40 ScrollBar horizontalBar, verticalBar;
43 * The regular expression used to determine the string which should be deleted
44 * when Ctrl+Bs is hit.
46 static final java.util.regex.Pattern CTRL_BS_PATTERN =
47 java.util.regex.Pattern.compile ("\\r?\\n\\z|[\\p{Punct}]+[\\t ]*\\z|[^\\p{Punct}\\s\\n\\r]*[\\t ]*\\z");
50 * Prevents uninitialized instances from being created outside the package.
56 * Constructs a new instance of this class given its parent
57 * and a style value describing its behavior and appearance.
59 * The style value is either one of the style constants defined in
60 * class <code>SWT</code> which is applicable to instances of this
61 * class, or must be built by <em>bitwise OR</em>'ing together
62 * (that is, using the <code>int</code> "|" operator) two or more
63 * of those <code>SWT</code> style constants. The class description
64 * lists the style constants that are applicable to the class.
65 * Style bits are also inherited from superclasses.
68 * @param parent a composite control which will be the parent of the new instance (cannot be null)
69 * @param style the style of control to construct
71 * @exception IllegalArgumentException <ul>
72 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
74 * @exception SWTException <ul>
75 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
76 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
81 * @see Widget#checkSubclass
82 * @see Widget#getStyle
84 public Scrollable (Composite parent, int style) {
85 super (parent, style);
89 long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
90 if (handle == 0) return 0;
91 return OS.DefWindowProc (hwnd, msg, wParam, lParam);
95 * Given a desired <em>client area</em> for the receiver
96 * (as described by the arguments), returns the bounding
97 * rectangle which would be required to produce that client
100 * In other words, it returns a rectangle such that, if the
101 * receiver's bounds were set to that rectangle, the area
102 * of the receiver which is capable of displaying data
103 * (that is, not covered by the "trimmings") would be the
104 * rectangle described by the arguments (relative to the
105 * receiver's parent).
108 * @param x the desired x coordinate of the client area
109 * @param y the desired y coordinate of the client area
110 * @param width the desired width of the client area
111 * @param height the desired height of the client area
112 * @return the required bounds to produce the given client area
114 * @exception SWTException <ul>
115 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
116 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
119 * @see #getClientArea
121 public Rectangle computeTrim (int x, int y, int width, int height) {
123 x = DPIUtil.autoScaleUp(x);
124 y = DPIUtil.autoScaleUp(y);
125 width = DPIUtil.autoScaleUp(width);
126 height = DPIUtil.autoScaleUp(height);
127 return DPIUtil.autoScaleDown(computeTrimInPixels(x, y, width, height));
130 Rectangle computeTrimInPixels (int x, int y, int width, int height) {
131 long scrolledHandle = scrolledHandle ();
132 RECT rect = new RECT ();
133 OS.SetRect (rect, x, y, x + width, y + height);
134 int bits1 = OS.GetWindowLong (scrolledHandle, OS.GWL_STYLE);
135 int bits2 = OS.GetWindowLong (scrolledHandle, OS.GWL_EXSTYLE);
136 OS.AdjustWindowRectEx (rect, bits1, false, bits2);
137 if (horizontalBar != null) rect.bottom += OS.GetSystemMetrics (OS.SM_CYHSCROLL);
138 if (verticalBar != null) rect.right += OS.GetSystemMetrics (OS.SM_CXVSCROLL);
139 int nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top;
140 return new Rectangle (rect.left, rect.top, nWidth, nHeight);
143 ScrollBar createScrollBar (int type) {
144 ScrollBar bar = new ScrollBar (this, type);
145 if ((state & CANVAS) != 0) {
146 bar.setMaximum (100);
153 void createWidget () {
154 super.createWidget ();
156 * NOTE: ICON_CANCEL and ICON_SEARCH have the same value as H_SCROLL and
157 * V_SCROLL. The meaning is determined by whether SWT.SEARCH is set.
159 if ((style & SWT.SEARCH) == 0) {
160 if ((style & SWT.H_SCROLL) != 0) horizontalBar = createScrollBar (SWT.H_SCROLL);
161 if ((style & SWT.V_SCROLL) != 0) verticalBar = createScrollBar (SWT.V_SCROLL);
166 void updateBackgroundColor () {
167 switch (applyThemeBackground ()) {
168 case 0: state &= ~THEME_BACKGROUND; break;
169 case 1: state |= THEME_BACKGROUND; break;
170 default: /* No change */
172 super.updateBackgroundColor ();
177 * <li>0 to remove THEME_BACKGROUND</li>
178 * <li>1 to apply THEME_BACKGROUND</li>
179 * <li>otherwise don't change THEME_BACKGROUND state</li>
181 int applyThemeBackground () {
182 return (backgroundAlpha == 0) ? 1 : 0;
185 void destroyScrollBar (int type) {
186 long hwnd = scrolledHandle ();
187 int bits = OS.GetWindowLong (hwnd, OS.GWL_STYLE);
188 if ((type & SWT.HORIZONTAL) != 0) {
189 style &= ~SWT.H_SCROLL;
190 bits &= ~OS.WS_HSCROLL;
192 if ((type & SWT.VERTICAL) != 0) {
193 style &= ~SWT.V_SCROLL;
194 bits &= ~OS.WS_VSCROLL;
196 OS.SetWindowLong (hwnd, OS.GWL_STYLE, bits);
200 * Returns a rectangle which describes the area of the
201 * receiver which is capable of displaying data (that is,
202 * not covered by the "trimmings").
204 * @return the client area
206 * @exception SWTException <ul>
207 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
208 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
213 public Rectangle getClientArea () {
215 return DPIUtil.autoScaleDown(getClientAreaInPixels());
218 Rectangle getClientAreaInPixels () {
220 RECT rect = new RECT ();
221 long scrolledHandle = scrolledHandle ();
222 OS.GetClientRect (scrolledHandle, rect);
223 int x = rect.left, y = rect.top;
224 int width = rect.right - rect.left;
225 int height = rect.bottom - rect.top;
226 if (scrolledHandle != handle) {
227 OS.GetClientRect (handle, rect);
228 OS.MapWindowPoints(handle, scrolledHandle, rect, 2);
232 return new Rectangle (x, y, width, height);
236 * Returns the receiver's horizontal scroll bar if it has
237 * one, and null if it does not.
239 * @return the horizontal scroll bar (or null)
241 * @exception SWTException <ul>
242 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
243 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
246 public ScrollBar getHorizontalBar () {
248 return horizontalBar;
252 * Returns the mode of the receiver's scrollbars. This will be
253 * <em>bitwise</em> OR of one or more of the constants defined in class
254 * <code>SWT</code>.<br>
256 * <li><code>SWT.SCROLLBAR_OVERLAY</code> - if receiver
257 * uses overlay scrollbars</li>
258 * <li><code>SWT.NONE</code> - otherwise</li>
261 * @return the mode of scrollbars
263 * @exception SWTException <ul>
264 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
266 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
267 * thread that created the receiver</li>
270 * @see SWT#SCROLLBAR_OVERLAY
274 public int getScrollbarsMode () {
280 * Returns the receiver's vertical scroll bar if it has
281 * one, and null if it does not.
283 * @return the vertical scroll bar (or null)
285 * @exception SWTException <ul>
286 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
287 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
290 public ScrollBar getVerticalBar () {
296 void releaseChildren (boolean destroy) {
297 if (horizontalBar != null) {
298 horizontalBar.release (false);
299 horizontalBar = null;
301 if (verticalBar != null) {
302 verticalBar.release (false);
305 super.releaseChildren (destroy);
309 void reskinChildren (int flags) {
310 if (horizontalBar != null) horizontalBar.reskin (flags);
311 if (verticalBar != null) verticalBar.reskin (flags);
312 super.reskinChildren (flags);
315 long scrolledHandle () {
320 int widgetExtStyle () {
321 return super.widgetExtStyle ();
323 * This code is intentionally commented. In future,
324 * we may wish to support different standard Windows
325 * edge styles. The issue here is that not all of
326 * these styles are available on the other platforms
327 * this would need to be a hint.
329 // if ((style & SWT.BORDER) != 0) return OS.WS_EX_CLIENTEDGE;
330 // if ((style & SWT.SHADOW_IN) != 0) return OS.WS_EX_STATICEDGE;
331 // return super.widgetExtStyle ();
336 int bits = super.widgetStyle () | OS.WS_TABSTOP;
338 * NOTE: ICON_CANCEL and ICON_SEARCH have the same value as H_SCROLL and
339 * V_SCROLL. The meaning is determined by whether SWT.SEARCH is set.
341 if ((style & SWT.SEARCH) == 0) {
342 if ((style & SWT.H_SCROLL) != 0) bits |= OS.WS_HSCROLL;
343 if ((style & SWT.V_SCROLL) != 0) bits |= OS.WS_VSCROLL;
349 TCHAR windowClass () {
350 return display.windowClass;
355 return display.windowProc;
359 LRESULT WM_HSCROLL (long wParam, long lParam) {
360 LRESULT result = super.WM_HSCROLL (wParam, lParam);
361 if (result != null) return result;
362 if (horizontalBar != null && lParam == 0) {
363 return wmScroll (horizontalBar, (state & CANVAS) != 0, handle, OS.WM_HSCROLL, wParam, lParam);
369 LRESULT WM_MOUSEWHEEL (long wParam, long lParam) {
370 return wmScrollWheel ((state & CANVAS) != 0, wParam, lParam);
374 LRESULT WM_SIZE (long wParam, long lParam) {
375 long code = callWindowProc (handle, OS.WM_SIZE, wParam, lParam);
376 super.WM_SIZE (wParam, lParam);
377 // widget may be disposed at this point
378 if (code == 0) return LRESULT.ZERO;
379 return new LRESULT (code);
383 LRESULT WM_VSCROLL (long wParam, long lParam) {
384 LRESULT result = super.WM_VSCROLL (wParam, lParam);
385 if (result != null) return result;
386 if (verticalBar != null && lParam == 0) {
387 return wmScroll (verticalBar, (state & CANVAS) != 0, handle, OS.WM_VSCROLL, wParam, lParam);
392 LRESULT wmScrollWheel (boolean update, long wParam, long lParam) {
393 LRESULT result = super.WM_MOUSEWHEEL (wParam, lParam);
394 if (result != null) return result;
396 * Translate WM_MOUSEWHEEL to WM_VSCROLL or WM_HSCROLL.
399 if ((wParam & (OS.MK_SHIFT | OS.MK_CONTROL)) != 0) return result;
400 boolean vertical = verticalBar != null && verticalBar.getEnabled ();
401 boolean horizontal = horizontalBar != null && horizontalBar.getEnabled ();
402 int msg = vertical ? OS.WM_VSCROLL : horizontal ? OS.WM_HSCROLL : 0;
403 if (msg == 0) return result;
404 int [] linesToScroll = new int [1];
405 OS.SystemParametersInfo (OS.SPI_GETWHEELSCROLLLINES, 0, linesToScroll, 0);
406 int delta = OS.GET_WHEEL_DELTA_WPARAM (wParam);
407 boolean pageScroll = linesToScroll [0] == OS.WHEEL_PAGESCROLL;
408 ScrollBar bar = vertical ? verticalBar : horizontalBar;
409 SCROLLINFO info = new SCROLLINFO ();
410 info.cbSize = SCROLLINFO.sizeof;
411 info.fMask = OS.SIF_POS;
412 OS.GetScrollInfo (handle, bar.scrollBarType (), info);
413 if (vertical && !pageScroll) delta *= linesToScroll [0];
414 int increment = pageScroll ? bar.getPageIncrement () : bar.getIncrement ();
415 info.nPos -= increment * delta / OS.WHEEL_DELTA;
416 OS.SetScrollInfo (handle, bar.scrollBarType (), info, true);
417 OS.SendMessage (handle, msg, OS.SB_THUMBPOSITION, 0);
422 * When the native widget scrolls inside WM_MOUSEWHEEL, it
423 * may or may not send a WM_VSCROLL or WM_HSCROLL to do the
424 * actual scrolling. This depends on the implementation of
425 * each native widget. In order to ensure that application
426 * code is notified when the scroll bar moves, compare the
427 * scroll bar position before and after the WM_MOUSEWHEEL.
428 * If the native control sends a WM_VSCROLL or WM_HSCROLL,
429 * then the application has already been notified. If not
430 * explicitly send the event.
432 int vPosition = verticalBar == null ? 0 : verticalBar.getSelection ();
433 int hPosition = horizontalBar == null ? 0 : horizontalBar.getSelection ();
434 long code = callWindowProc (handle, OS.WM_MOUSEWHEEL, wParam, lParam);
435 if (verticalBar != null) {
436 int position = verticalBar.getSelection ();
437 if (position != vPosition) {
438 Event event = new Event ();
439 event.detail = position < vPosition ? SWT.PAGE_UP : SWT.PAGE_DOWN;
440 verticalBar.sendSelectionEvent (SWT.Selection, event, true);
443 if (horizontalBar != null) {
444 int position = horizontalBar.getSelection ();
445 if (position != hPosition) {
446 Event event = new Event ();
447 event.detail = position < hPosition ? SWT.PAGE_UP : SWT.PAGE_DOWN;
448 horizontalBar.sendSelectionEvent (SWT.Selection, event, true);
451 return new LRESULT (code);
454 LRESULT wmScroll (ScrollBar bar, boolean update, long hwnd, int msg, long wParam, long lParam) {
455 LRESULT result = null;
457 int type = msg == OS.WM_HSCROLL ? OS.SB_HORZ : OS.SB_VERT;
458 SCROLLINFO info = new SCROLLINFO ();
459 info.cbSize = SCROLLINFO.sizeof;
460 info.fMask = OS.SIF_TRACKPOS | OS.SIF_POS | OS.SIF_RANGE;
461 OS.GetScrollInfo (hwnd, type, info);
462 info.fMask = OS.SIF_POS;
463 int code = OS.LOWORD (wParam);
465 case OS.SB_ENDSCROLL: return null;
466 case OS.SB_THUMBPOSITION:
467 case OS.SB_THUMBTRACK:
468 info.nPos = info.nTrackPos;
471 info.nPos = info.nMin;
474 info.nPos = info.nMax;
477 info.nPos += bar.getIncrement ();
480 int increment = bar.getIncrement ();
481 info.nPos = Math.max (info.nMin, info.nPos - increment);
484 info.nPos += bar.getPageIncrement ();
487 int pageIncrement = bar.getPageIncrement ();
488 info.nPos = Math.max (info.nMin, info.nPos - pageIncrement);
491 OS.SetScrollInfo (hwnd, type, info, true);
493 long code = callWindowProc (hwnd, msg, wParam, lParam);
494 result = code == 0 ? LRESULT.ZERO : new LRESULT (code);
496 bar.wmScrollChild (wParam, lParam);