1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 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.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.win32.*;
23 * Instances of the receiver represent a selectable user
24 * interface object that present a range of continuous
27 * <dt><b>Styles:</b></dt>
28 * <dd>HORIZONTAL, VERTICAL</dd>
29 * <dt><b>Events:</b></dt>
33 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
36 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
39 * @see <a href="http://www.eclipse.org/swt/snippets/#scale">Scale snippets</a>
40 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
41 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
42 * @noextend This class is not intended to be subclassed by clients.
44 public class Scale extends Control {
45 boolean ignoreResize, ignoreSelection;
46 static final long TrackBarProc;
47 static final TCHAR TrackBarClass = new TCHAR (0, OS.TRACKBAR_CLASS, true);
50 WNDCLASS lpWndClass = new WNDCLASS ();
51 OS.GetClassInfo (0, TrackBarClass, lpWndClass);
52 TrackBarProc = lpWndClass.lpfnWndProc;
54 * Feature in Windows. The track bar window class
55 * does not include CS_DBLCLKS. This means that these
56 * controls will not get double click messages such as
57 * WM_LBUTTONDBLCLK. The fix is to register a new
58 * window class with CS_DBLCLKS.
60 * NOTE: Screen readers look for the exact class name
61 * of the control in order to provide the correct kind
62 * of assistance. Therefore, it is critical that the
63 * new window class have the same name. It is possible
64 * to register a local window class with the same name
65 * as a global class. Since bits that affect the class
66 * are being changed, it is possible that other native
67 * code, other than SWT, could create a control with
68 * this class name, and fail unexpectedly.
70 lpWndClass.hInstance = OS.GetModuleHandle (null);
71 lpWndClass.style &= ~OS.CS_GLOBALCLASS;
72 lpWndClass.style |= OS.CS_DBLCLKS;
73 OS.RegisterClass (TrackBarClass, lpWndClass);
77 * Constructs a new instance of this class given its parent
78 * and a style value describing its behavior and appearance.
80 * The style value is either one of the style constants defined in
81 * class <code>SWT</code> which is applicable to instances of this
82 * class, or must be built by <em>bitwise OR</em>'ing together
83 * (that is, using the <code>int</code> "|" operator) two or more
84 * of those <code>SWT</code> style constants. The class description
85 * lists the style constants that are applicable to the class.
86 * Style bits are also inherited from superclasses.
89 * @param parent a composite control which will be the parent of the new instance (cannot be null)
90 * @param style the style of control to construct
92 * @exception IllegalArgumentException <ul>
93 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
95 * @exception SWTException <ul>
96 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
97 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
100 * @see SWT#HORIZONTAL
102 * @see Widget#checkSubclass
103 * @see Widget#getStyle
105 public Scale (Composite parent, int style) {
106 super (parent, checkStyle (style));
110 * Adds the listener to the collection of listeners who will
111 * be notified when the user changes the receiver's value, by sending
112 * it one of the messages defined in the <code>SelectionListener</code>
115 * <code>widgetSelected</code> is called when the user changes the receiver's value.
116 * <code>widgetDefaultSelected</code> is not called.
119 * @param listener the listener which should be notified
121 * @exception IllegalArgumentException <ul>
122 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
124 * @exception SWTException <ul>
125 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
126 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
129 * @see SelectionListener
130 * @see #removeSelectionListener
132 public void addSelectionListener(SelectionListener listener) {
134 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
135 TypedListener typedListener = new TypedListener (listener);
136 addListener (SWT.Selection,typedListener);
137 addListener (SWT.DefaultSelection,typedListener);
141 long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
142 if (handle == 0) return 0;
143 return OS.CallWindowProc (TrackBarProc, hwnd, msg, wParam, lParam);
146 static int checkStyle (int style) {
147 return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
150 @Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
152 int border = getBorderWidthInPixels ();
153 int width = border * 2, height = border * 2;
154 RECT rect = new RECT ();
155 OS.SendMessage (handle, OS.TBM_GETTHUMBRECT, 0, rect);
156 if ((style & SWT.HORIZONTAL) != 0) {
157 width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10;
158 int scrollY = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
159 height += (rect.top * 2) + scrollY + (scrollY / 3);
161 int scrollX = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
162 width += (rect.left * 2) + scrollX + (scrollX / 3);
163 height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10;
165 if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
166 if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
167 return new Point (width, height);
171 void createHandle () {
172 super.createHandle ();
173 state |= THEME_BACKGROUND | DRAW_BACKGROUND;
174 OS.SendMessage (handle, OS.TBM_SETRANGEMAX, 0, 100);
175 OS.SendMessage (handle, OS.TBM_SETPAGESIZE, 0, 10);
176 OS.SendMessage (handle, OS.TBM_SETTICFREQ, 10, 0);
177 createdAsRTL = (style & SWT.RIGHT_TO_LEFT) != 0;
181 int defaultForeground () {
182 return OS.GetSysColor (OS.COLOR_BTNFACE);
186 * Returns the amount that the receiver's value will be
187 * modified by when the up/down (or right/left) arrows
190 * @return the increment
192 * @exception SWTException <ul>
193 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
194 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
197 public int getIncrement () {
199 return (int)OS.SendMessage (handle, OS.TBM_GETLINESIZE, 0, 0);
203 * Returns the maximum value which the receiver will allow.
205 * @return the maximum
207 * @exception SWTException <ul>
208 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
209 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
212 public int getMaximum () {
214 return (int)OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
218 * Returns the minimum value which the receiver will allow.
220 * @return the minimum
222 * @exception SWTException <ul>
223 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
224 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
227 public int getMinimum () {
229 return (int)OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
233 * Returns the amount that the receiver's value will be
234 * modified by when the page increment/decrement areas
237 * @return the page increment
239 * @exception SWTException <ul>
240 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
241 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
244 public int getPageIncrement () {
246 return (int)OS.SendMessage (handle, OS.TBM_GETPAGESIZE, 0, 0);
250 * Returns the 'selection', which is the receiver's position.
252 * @return the selection
254 * @exception SWTException <ul>
255 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
256 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
259 public int getSelection () {
261 return (int)OS.SendMessage (handle, OS.TBM_GETPOS, 0, 0);
265 * Removes the listener from the collection of listeners who will
266 * be notified when the user changes the receiver's value.
268 * @param listener the listener which should no longer be notified
270 * @exception IllegalArgumentException <ul>
271 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
273 * @exception SWTException <ul>
274 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
275 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
278 * @see SelectionListener
279 * @see #addSelectionListener
281 public void removeSelectionListener(SelectionListener listener) {
283 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
284 if (eventTable == null) return;
285 eventTable.unhook (SWT.Selection, listener);
286 eventTable.unhook (SWT.DefaultSelection,listener);
290 void setBackgroundImage (long hImage) {
291 super.setBackgroundImage (hImage);
293 * Bug in Windows. Changing the background color of the Scale
294 * widget and calling InvalidateRect() still draws with the old
295 * color. The fix is to send a fake WM_SIZE event to cause
296 * it to redraw with the new background color.
299 OS.SendMessage (handle, OS.WM_SIZE, 0, 0);
300 ignoreResize = false;
304 void setBackgroundPixel (int pixel) {
305 super.setBackgroundPixel (pixel);
307 * Bug in Windows. Changing the background color of the Scale
308 * widget and calling InvalidateRect() still draws with the old
309 * color. The fix is to send a fake WM_SIZE event to cause
310 * it to redraw with the new background color.
313 OS.SendMessage (handle, OS.WM_SIZE, 0, 0);
314 ignoreResize = false;
318 void setBoundsInPixels (int x, int y, int width, int height, int flags, boolean defer) {
320 * Bug in Windows. If SetWindowPos() is called on a
321 * track bar with either SWP_DRAWFRAME, a new size,
322 * or both during mouse down, the track bar posts a
323 * WM_MOUSEMOVE message when the mouse has not moved.
324 * The window proc for the track bar uses WM_MOUSEMOVE
325 * to issue WM_HSCROLL or WM_SCROLL events to notify
326 * the application that the slider has changed. The
327 * end result is that when the user requests a page
328 * scroll and the application resizes the track bar
329 * during the change notification, continuous stream
330 * of WM_MOUSEMOVE messages are generated and the
331 * thumb moves to the mouse position rather than
332 * scrolling by a page. The fix is to clear the
333 * SWP_DRAWFRAME flag.
335 * NOTE: There is no fix for the WM_MOUSEMOVE that
336 * is generated by a new size. Clearing SWP_DRAWFRAME
337 * does not fix the problem. However, it is unlikely
338 * that the programmer will resize the control during
341 flags &= ~OS.SWP_DRAWFRAME;
342 super.setBoundsInPixels (x, y, width, height, flags, true);
346 * Sets the amount that the receiver's value will be
347 * modified by when the up/down (or right/left) arrows
348 * are pressed to the argument, which must be at least
351 * @param increment the new increment (must be greater than zero)
353 * @exception SWTException <ul>
354 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
355 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
358 public void setIncrement (int increment) {
360 if (increment < 1) return;
361 int minimum = (int)OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
362 int maximum = (int)OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
363 if (increment > maximum - minimum) return;
364 OS.SendMessage (handle, OS.TBM_SETLINESIZE, 0, increment);
368 * Sets the maximum value that the receiver will allow. This new
369 * value will be ignored if it is not greater than the receiver's current
370 * minimum value. If the new maximum is applied then the receiver's
371 * selection value will be adjusted if necessary to fall within its new range.
373 * @param value the new maximum, which must be greater than the current minimum
375 * @exception SWTException <ul>
376 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
377 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
380 public void setMaximum (int value) {
382 int minimum = (int)OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
383 if (0 <= minimum && minimum < value) {
384 OS.SendMessage (handle, OS.TBM_SETRANGEMAX, 1, value);
389 * Sets the minimum value that the receiver will allow. This new
390 * value will be ignored if it is negative or is not less than the receiver's
391 * current maximum value. If the new minimum is applied then the receiver's
392 * selection value will be adjusted if necessary to fall within its new range.
394 * @param value the new minimum, which must be nonnegative and less than the current maximum
396 * @exception SWTException <ul>
397 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
398 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
401 public void setMinimum (int value) {
403 int maximum = (int)OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
404 if (0 <= value && value < maximum) {
405 OS.SendMessage (handle, OS.TBM_SETRANGEMIN, 1, value);
410 * Sets the amount that the receiver's value will be
411 * modified by when the page increment/decrement areas
412 * are selected to the argument, which must be at least
415 * @param pageIncrement the page increment (must be greater than zero)
417 * @exception SWTException <ul>
418 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
419 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
422 public void setPageIncrement (int pageIncrement) {
424 if (pageIncrement < 1) return;
425 int minimum = (int)OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
426 int maximum = (int)OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
427 if (pageIncrement > maximum - minimum) return;
428 OS.SendMessage (handle, OS.TBM_SETPAGESIZE, 0, pageIncrement);
429 OS.SendMessage (handle, OS.TBM_SETTICFREQ, pageIncrement, 0);
433 * Sets the 'selection', which is the receiver's value,
434 * to the argument which must be greater than or equal to zero.
436 * @param value the new selection (must be zero or greater)
438 * @exception SWTException <ul>
439 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
440 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
443 public void setSelection (int value) {
445 OS.SendMessage (handle, OS.TBM_SETPOS, 1, value);
450 int bits = super.widgetStyle () | OS.WS_TABSTOP | OS.TBS_BOTH | OS.TBS_AUTOTICKS;
451 if ((style & SWT.HORIZONTAL) != 0) return bits | OS.TBS_HORZ | OS.TBS_DOWNISLEFT;
452 return bits | OS.TBS_VERT;
456 TCHAR windowClass () {
457 return TrackBarClass;
466 LRESULT WM_KEYDOWN (long wParam, long lParam) {
467 LRESULT result = super.WM_KEYDOWN (wParam, lParam);
468 if (result != null) return result;
469 switch ((int)wParam) {
473 * Bug in Windows. The behavior for the left and right keys is not
474 * changed if the orientation changes after the control was created.
475 * The fix is to replace VK_LEFT by VK_RIGHT and VK_RIGHT by VK_LEFT
476 * when the current orientation differs from the orientation used to
477 * create the control.
479 boolean isRTL = (style & SWT.RIGHT_TO_LEFT) != 0;
480 if (isRTL != createdAsRTL) {
481 long code = callWindowProc (handle, OS.WM_KEYDOWN, wParam == OS.VK_RIGHT ? OS.VK_LEFT : OS.VK_RIGHT, lParam);
482 return new LRESULT (code);
490 LRESULT WM_MOUSEWHEEL (long wParam, long lParam) {
491 LRESULT result = super.WM_MOUSEWHEEL (wParam, lParam);
492 if (result != null) return result;
494 * Bug in Windows. When a track bar slider is changed
495 * from WM_MOUSEWHEEL, it does not always send either
496 * a WM_VSCROLL or M_HSCROLL to notify the application
497 * of the change. The fix is to detect that the selection
498 * has changed and that notification has not been issued
499 * and send the selection event.
501 int oldPosition = (int)OS.SendMessage (handle, OS.TBM_GETPOS, 0, 0);
502 ignoreSelection = true;
503 long code = callWindowProc (handle, OS.WM_MOUSEWHEEL, wParam, lParam);
504 ignoreSelection = false;
505 int newPosition = (int)OS.SendMessage (handle, OS.TBM_GETPOS, 0, 0);
506 if (oldPosition != newPosition) {
508 * Send the event because WM_HSCROLL and WM_VSCROLL
509 * are sent from a modal message loop in windows that
510 * is active when the user is scrolling.
512 sendSelectionEvent (SWT.Selection, null, true);
513 // widget could be disposed at this point
515 return new LRESULT (code);
519 LRESULT WM_PAINT (long wParam, long lParam) {
520 if ((state & DISPOSE_SENT) != 0) return LRESULT.ZERO;
523 * Bug in Windows. For some reason, when WM_CTLCOLORSTATIC
524 * is used to implement transparency and returns a NULL brush,
525 * Windows doesn't always draw the track bar. It seems that
526 * it is drawn correctly the first time. It is possible that
527 * Windows double buffers the control and the double buffer
528 * strategy fails when WM_CTLCOLORSTATIC returns unexpected
529 * results. The fix is to send a fake WM_SIZE to force it
530 * to redraw every time there is a WM_PAINT.
532 boolean fixPaint = findBackgroundControl () != null;
534 if (OS.IsAppThemed ()) {
535 Control control = findThemeControl ();
536 fixPaint = control != null;
540 boolean redraw = getDrawing () && OS.IsWindowVisible (handle);
541 if (redraw) OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0);
543 OS.SendMessage (handle, OS.WM_SIZE, 0, 0);
544 ignoreResize = false;
546 OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0);
547 OS.InvalidateRect (handle, null, false);
550 return super.WM_PAINT (wParam, lParam);
554 LRESULT WM_SIZE (long wParam, long lParam) {
555 if (ignoreResize) return null;
556 return super.WM_SIZE (wParam, lParam);
560 LRESULT wmScrollChild (long wParam, long lParam) {
562 /* Do nothing when scrolling is ending */
563 int code = OS.LOWORD (wParam);
566 case OS.TB_THUMBPOSITION:
570 if (!ignoreSelection) {
571 Event event = new Event ();
573 * This code is intentionally commented. The event
574 * detail field is not currently supported on all
578 // case OS.TB_TOP: event.detail = SWT.HOME; break;
579 // case OS.TB_BOTTOM: event.detail = SWT.END; break;
580 // case OS.TB_LINEDOWN: event.detail = SWT.ARROW_DOWN; break;
581 // case OS.TB_LINEUP: event.detail = SWT.ARROW_UP; break;
582 // case OS.TB_PAGEDOWN: event.detail = SWT.PAGE_DOWN; break;
583 // case OS.TB_PAGEUP: event.detail = SWT.PAGE_UP; break;
586 * Send the event because WM_HSCROLL and WM_VSCROLL
587 * are sent from a modal message loop in windows that
588 * is active when the user is scrolling.
590 sendSelectionEvent (SWT.Selection, event, true);
591 // widget could be disposed at this point