]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/Scrollable.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / Scrollable.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 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.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
21
22 /**
23  * This class is the abstract superclass of all classes which
24  * represent controls that have standard scroll bars.
25  * <dl>
26  * <dt><b>Styles:</b></dt>
27  * <dd>H_SCROLL, V_SCROLL</dd>
28  * <dt><b>Events:</b>
29  * <dd>(none)</dd>
30  * </dl>
31  * <p>
32  * IMPORTANT: This class is intended to be subclassed <em>only</em>
33  * within the SWT implementation.
34  * </p>
35  *
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.
38  */
39 public abstract class Scrollable extends Control {
40         ScrollBar horizontalBar, verticalBar;
41
42         /**
43          * The regular expression used to determine the string which should be deleted
44          * when Ctrl+Bs is hit.
45          */
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");
48
49 /**
50  * Prevents uninitialized instances from being created outside the package.
51  */
52 Scrollable () {
53 }
54
55 /**
56  * Constructs a new instance of this class given its parent
57  * and a style value describing its behavior and appearance.
58  * <p>
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.
66  * </p>
67  *
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
70  *
71  * @exception IllegalArgumentException <ul>
72  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
73  * </ul>
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>
77  * </ul>
78  *
79  * @see SWT#H_SCROLL
80  * @see SWT#V_SCROLL
81  * @see Widget#checkSubclass
82  * @see Widget#getStyle
83  */
84 public Scrollable (Composite parent, int style) {
85         super (parent, style);
86 }
87
88 @Override
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);
92 }
93
94 /**
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
98  * area.
99  * <p>
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).
106  * </p>
107  *
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
113  *
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>
117  * </ul>
118  *
119  * @see #getClientArea
120  */
121 public Rectangle computeTrim (int x, int y, int width, int height) {
122         checkWidget ();
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));
128 }
129
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);
141 }
142
143 ScrollBar createScrollBar (int type) {
144         ScrollBar bar = new ScrollBar (this, type);
145         if ((state & CANVAS) != 0) {
146                 bar.setMaximum (100);
147                 bar.setThumb (10);
148         }
149         return bar;
150 }
151
152 @Override
153 void createWidget () {
154         super.createWidget ();
155         /*
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.
158          */
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);
162         }
163 }
164
165 @Override
166 void updateBackgroundColor () {
167         switch (applyThemeBackground ()) {
168                 case 0: state &= ~THEME_BACKGROUND; break;
169                 case 1: state |= THEME_BACKGROUND; break;
170                 default: /* No change */
171         }
172         super.updateBackgroundColor ();
173 }
174
175 /**
176  * @return
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>
180  */
181 int applyThemeBackground () {
182         return (backgroundAlpha == 0) ? 1 : 0;
183 }
184
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;
191         }
192         if ((type & SWT.VERTICAL) != 0) {
193                 style &= ~SWT.V_SCROLL;
194                 bits &= ~OS.WS_VSCROLL;
195         }
196         OS.SetWindowLong (hwnd, OS.GWL_STYLE, bits);
197 }
198
199 /**
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").
203  *
204  * @return the client area
205  *
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>
209  * </ul>
210  *
211  * @see #computeTrim
212  */
213 public Rectangle getClientArea () {
214         checkWidget ();
215         return DPIUtil.autoScaleDown(getClientAreaInPixels());
216 }
217
218 Rectangle getClientAreaInPixels () {
219         forceResize ();
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);
229                 x = -rect.left;
230                 y = -rect.top;
231         }
232         return new Rectangle (x, y, width, height);
233 }
234
235 /**
236  * Returns the receiver's horizontal scroll bar if it has
237  * one, and null if it does not.
238  *
239  * @return the horizontal scroll bar (or null)
240  *
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>
244  * </ul>
245  */
246 public ScrollBar getHorizontalBar () {
247         checkWidget ();
248         return horizontalBar;
249 }
250
251 /**
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>
255  * <ul>
256  * <li><code>SWT.SCROLLBAR_OVERLAY</code> - if receiver
257  * uses overlay scrollbars</li>
258  * <li><code>SWT.NONE</code> - otherwise</li>
259  * </ul>
260  *
261  * @return the mode of scrollbars
262  *
263  * @exception SWTException <ul>
264  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been
265  * disposed</li>
266  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
267  * thread that created the receiver</li>
268  * </ul>
269  *
270  * @see SWT#SCROLLBAR_OVERLAY
271  *
272  * @since 3.8
273  */
274 public int getScrollbarsMode () {
275         checkWidget();
276         return SWT.NONE;
277 }
278
279 /**
280  * Returns the receiver's vertical scroll bar if it has
281  * one, and null if it does not.
282  *
283  * @return the vertical scroll bar (or null)
284  *
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>
288  * </ul>
289  */
290 public ScrollBar getVerticalBar () {
291         checkWidget ();
292         return verticalBar;
293 }
294
295 @Override
296 void releaseChildren (boolean destroy) {
297         if (horizontalBar != null) {
298                 horizontalBar.release (false);
299                 horizontalBar = null;
300         }
301         if (verticalBar != null) {
302                 verticalBar.release (false);
303                 verticalBar = null;
304         }
305         super.releaseChildren (destroy);
306 }
307
308 @Override
309 void reskinChildren (int flags) {
310         if (horizontalBar != null) horizontalBar.reskin (flags);
311         if (verticalBar != null) verticalBar.reskin (flags);
312         super.reskinChildren (flags);
313 }
314
315 long scrolledHandle () {
316         return handle;
317 }
318
319 @Override
320 int widgetExtStyle () {
321         return super.widgetExtStyle ();
322         /*
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.
328         */
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 ();
332 }
333
334 @Override
335 int widgetStyle () {
336         int bits = super.widgetStyle () | OS.WS_TABSTOP;
337         /*
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.
340          */
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;
344         }
345         return bits;
346 }
347
348 @Override
349 TCHAR windowClass () {
350         return display.windowClass;
351 }
352
353 @Override
354 long windowProc () {
355         return display.windowProc;
356 }
357
358 @Override
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);
364         }
365         return result;
366 }
367
368 @Override
369 LRESULT WM_MOUSEWHEEL (long wParam, long lParam) {
370         return wmScrollWheel ((state & CANVAS) != 0, wParam, lParam);
371 }
372
373 @Override
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);
380 }
381
382 @Override
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);
388         }
389         return result;
390 }
391
392 LRESULT wmScrollWheel (boolean update, long wParam, long lParam) {
393         LRESULT result = super.WM_MOUSEWHEEL (wParam, lParam);
394         if (result != null) return result;
395         /*
396         * Translate WM_MOUSEWHEEL to WM_VSCROLL or WM_HSCROLL.
397         */
398         if (update) {
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);
418                 return LRESULT.ZERO;
419         }
420
421         /*
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.
431         */
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);
441                 }
442         }
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);
449                 }
450         }
451         return new LRESULT (code);
452 }
453
454 LRESULT wmScroll (ScrollBar bar, boolean update, long hwnd, int msg, long wParam, long lParam) {
455         LRESULT result = null;
456         if (update) {
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);
464                 switch (code) {
465                         case OS.SB_ENDSCROLL:  return null;
466                         case OS.SB_THUMBPOSITION:
467                         case OS.SB_THUMBTRACK:
468                                 info.nPos = info.nTrackPos;
469                                 break;
470                         case OS.SB_TOP:
471                                 info.nPos = info.nMin;
472                                 break;
473                         case OS.SB_BOTTOM:
474                                 info.nPos = info.nMax;
475                                 break;
476                         case OS.SB_LINEDOWN:
477                                 info.nPos += bar.getIncrement ();
478                                 break;
479                         case OS.SB_LINEUP:
480                                 int increment = bar.getIncrement ();
481                                 info.nPos = Math.max (info.nMin, info.nPos - increment);
482                                 break;
483                         case OS.SB_PAGEDOWN:
484                                 info.nPos += bar.getPageIncrement ();
485                                 break;
486                         case OS.SB_PAGEUP:
487                                 int pageIncrement = bar.getPageIncrement ();
488                                 info.nPos = Math.max (info.nMin, info.nPos - pageIncrement);
489                                 break;
490                 }
491                 OS.SetScrollInfo (hwnd, type, info, true);
492         } else {
493                 long code = callWindowProc (hwnd, msg, wParam, lParam);
494                 result = code == 0 ? LRESULT.ZERO : new LRESULT (code);
495         }
496         bar.wmScrollChild (wParam, lParam);
497         return result;
498 }
499
500 }