root/dwt/widgets/Scale.d

Revision 246:fd9c62a2998e, 20.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 5 months ago)

Updater SWT 3.4M7 to 3.4

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module dwt.widgets.Scale;
14
15 import dwt.DWT;
16 import dwt.DWTException;
17 import dwt.events.SelectionListener;
18 import dwt.graphics.Point;
19 import dwt.internal.win32.OS;
20
21 import dwt.widgets.Control;
22 import dwt.widgets.Composite;
23 import dwt.widgets.TypedListener;
24 import dwt.widgets.Event;
25
26 import dwt.dwthelper.utils;
27
28 /**
29  * Instances of the receiver represent a selectable user
30  * interface object that present a range of continuous
31  * numeric values.
32  * <dl>
33  * <dt><b>Styles:</b></dt>
34  * <dd>HORIZONTAL, VERTICAL</dd>
35  * <dt><b>Events:</b></dt>
36  * <dd>Selection</dd>
37  * </dl>
38  * <p>
39  * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
40  * </p><p>
41  * <p>
42  * IMPORTANT: This class is intended to be subclassed <em>only</em>
43  * within the DWT implementation.
44  * </p>
45  *
46  * @see <a href="http://www.eclipse.org/swt/snippets/#scale">Scale snippets</a>
47  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
48  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
49  */
50
51 public class Scale : Control {
52
53     alias Control.computeSize computeSize;
54     alias Control.setBackgroundImage setBackgroundImage;
55     alias Control.windowProc windowProc;
56
57     bool ignoreResize, ignoreSelection;
58     private static /+const+/ WNDPROC TrackBarProc;
59     static const TCHAR[] TrackBarClass = OS.TRACKBAR_CLASS;
60
61     private static bool static_this_completed = false;
62     private static void static_this() {
63         if( static_this_completed ){
64             return;
65         }
66         synchronized {
67             if( static_this_completed ){
68                 return;
69             }
70             WNDCLASS lpWndClass;
71             OS.GetClassInfo (null, TrackBarClass.ptr, &lpWndClass);
72             TrackBarProc = lpWndClass.lpfnWndProc;
73             /*
74             * Feature in Windows.  The track bar window class
75             * does not include CS_DBLCLKS.  This means that these
76             * controls will not get double click messages such as
77             * WM_LBUTTONDBLCLK.  The fix is to register a new
78             * window class with CS_DBLCLKS.
79             *
80             * NOTE:  Screen readers look for the exact class name
81             * of the control in order to provide the correct kind
82             * of assistance.  Therefore, it is critical that the
83             * new window class have the same name.  It is possible
84             * to register a local window class with the same name
85             * as a global class.  Since bits that affect the class
86             * are being changed, it is possible that other native
87             * code, other than DWT, could create a control with
88             * this class name, and fail unexpectedly.
89             */
90             auto hInstance = OS.GetModuleHandle (null);
91             auto hHeap = OS.GetProcessHeap ();
92             lpWndClass.hInstance = hInstance;
93             lpWndClass.style &= ~OS.CS_GLOBALCLASS;
94             lpWndClass.style |= OS.CS_DBLCLKS;
95             int byteCount = (TrackBarClass.length+1) * TCHAR.sizeof;
96             auto lpszClassName = cast(TCHAR*) OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
97             OS.MoveMemory (lpszClassName, TrackBarClass.ptr, byteCount);
98             lpWndClass.lpszClassName = lpszClassName;
99             OS.RegisterClass (&lpWndClass);
100             OS.HeapFree (hHeap, 0, lpszClassName);
101             static_this_completed = true;
102         }
103     }
104
105 /**
106  * Constructs a new instance of this class given its parent
107  * and a style value describing its behavior and appearance.
108  * <p>
109  * The style value is either one of the style constants defined in
110  * class <code>DWT</code> which is applicable to instances of this
111  * class, or must be built by <em>bitwise OR</em>'ing together
112  * (that is, using the <code>int</code> "|" operator) two or more
113  * of those <code>DWT</code> style constants. The class description
114  * lists the style constants that are applicable to the class.
115  * Style bits are also inherited from superclasses.
116  * </p>
117  *
118  * @param parent a composite control which will be the parent of the new instance (cannot be null)
119  * @param style the style of control to construct
120  *
121  * @exception IllegalArgumentException <ul>
122  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
123  * </ul>
124  * @exception DWTException <ul>
125  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
126  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
127  * </ul>
128  *
129  * @see DWT#HORIZONTAL
130  * @see DWT#VERTICAL
131  * @see Widget#checkSubclass
132  * @see Widget#getStyle
133  */
134 public this (Composite parent, int style) {
135     static_this();
136     super (parent, checkStyle (style));
137 }
138
139 /**
140  * Adds the listener to the collection of listeners who will
141  * be notified when the user changes the receiver's value, by sending
142  * it one of the messages defined in the <code>SelectionListener</code>
143  * interface.
144  * <p>
145  * <code>widgetSelected</code> is called when the user changes the receiver's value.
146  * <code>widgetDefaultSelected</code> is not called.
147  * </p>
148  *
149  * @param listener the listener which should be notified
150  *
151  * @exception IllegalArgumentException <ul>
152  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
153  * </ul>
154  * @exception DWTException <ul>
155  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
156  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
157  * </ul>
158  *
159  * @see SelectionListener
160  * @see #removeSelectionListener
161  */
162 public void addSelectionListener(SelectionListener listener) {
163     checkWidget ();
164     if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
165     TypedListener typedListener = new TypedListener (listener);
166     addListener (DWT.Selection,typedListener);
167     addListener (DWT.DefaultSelection,typedListener);
168 }
169
170 override int callWindowProc (HWND hwnd, int msg, int wParam, int lParam) {
171     if (handle is null) return 0;
172     return OS.CallWindowProc (TrackBarProc, hwnd, msg, wParam, lParam);
173 }
174
175 static int checkStyle (int style) {
176     return checkBits (style, DWT.HORIZONTAL, DWT.VERTICAL, 0, 0, 0, 0);
177 }
178
179 override public Point computeSize (int wHint, int hHint, bool changed) {
180     checkWidget ();
181     int border = getBorderWidth ();
182     int width = border * 2, height = border * 2;
183     RECT rect;
184     OS.SendMessage (handle, OS.TBM_GETTHUMBRECT, 0, &rect);
185     if ((style & DWT.HORIZONTAL) !is 0) {
186         width += OS.GetSystemMetrics (OS.SM_CXHSCROLL) * 10;
187         int scrollY = OS.GetSystemMetrics (OS.SM_CYHSCROLL);
188         height += (rect.top * 2) + scrollY + (scrollY / 3);
189     } else {
190         int scrollX = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
191         width += (rect.left * 2) + scrollX + (scrollX / 3);
192         height += OS.GetSystemMetrics (OS.SM_CYVSCROLL) * 10;
193     }
194     if (wHint !is DWT.DEFAULT) width = wHint + (border * 2);
195     if (hHint !is DWT.DEFAULT) height = hHint + (border * 2);
196     return new Point (width, height);
197 }
198
199 override void createHandle () {
200     super.createHandle ();
201     state |= THEME_BACKGROUND | DRAW_BACKGROUND;
202     OS.SendMessage (handle, OS.TBM_SETRANGEMAX, 0, 100);
203     OS.SendMessage (handle, OS.TBM_SETPAGESIZE, 0, 10);
204     OS.SendMessage (handle, OS.TBM_SETTICFREQ, 10, 0);
205 }
206
207 override int defaultForeground () {
208     return OS.GetSysColor (OS.COLOR_BTNFACE);
209 }
210
211 /**
212  * Returns the amount that the receiver's value will be
213  * modified by when the up/down (or right/left) arrows
214  * are pressed.
215  *
216  * @return the increment
217  *
218  * @exception DWTException <ul>
219  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
220  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
221  * </ul>
222  */
223 public int getIncrement () {
224     checkWidget ();
225     return OS.SendMessage (handle, OS.TBM_GETLINESIZE, 0, 0);
226 }
227
228 /**
229  * Returns the maximum value which the receiver will allow.
230  *
231  * @return the maximum
232  *
233  * @exception DWTException <ul>
234  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
235  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
236  * </ul>
237  */
238 public int getMaximum () {
239     checkWidget ();
240     return OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
241 }
242
243 /**
244  * Returns the minimum value which the receiver will allow.
245  *
246  * @return the minimum
247  *
248  * @exception DWTException <ul>
249  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
250  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
251  * </ul>
252  */
253 public int getMinimum () {
254     checkWidget ();
255     return cast(int)OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
256 }
257
258 /**
259  * Returns the amount that the receiver's value will be
260  * modified by when the page increment/decrement areas
261  * are selected.
262  *
263  * @return the page increment
264  *
265  * @exception DWTException <ul>
266  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
267  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
268  * </ul>
269  */
270 public int getPageIncrement () {
271     checkWidget ();
272     return OS.SendMessage (handle, OS.TBM_GETPAGESIZE, 0, 0);
273 }
274
275 /**
276  * Returns the 'selection', which is the receiver's position.
277  *
278  * @return the selection
279  *
280  * @exception DWTException <ul>
281  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
282  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
283  * </ul>
284  */
285 public int getSelection () {
286     checkWidget ();
287     return OS.SendMessage (handle, OS.TBM_GETPOS, 0, 0);
288 }
289
290 /**
291  * Removes the listener from the collection of listeners who will
292  * be notified when the user changes the receiver's value.
293  *
294  * @param listener the listener which should no longer be notified
295  *
296  * @exception IllegalArgumentException <ul>
297  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
298  * </ul>
299  * @exception DWTException <ul>
300  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
301  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
302  * </ul>
303  *
304  * @see SelectionListener
305  * @see #addSelectionListener
306  */
307 public void removeSelectionListener(SelectionListener listener) {
308     checkWidget ();
309     if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
310     if (eventTable is null) return;
311     eventTable.unhook (DWT.Selection, listener);
312     eventTable.unhook (DWT.DefaultSelection,listener);
313 }
314
315 override void setBackgroundImage (HBITMAP hImage) {
316     super.setBackgroundImage (hImage);
317     /*
318     * Bug in Windows.  Changing the background color of the Scale
319     * widget and calling InvalidateRect() still draws with the old
320     * color.  The fix is to send a fake WM_SIZE event to cause
321     * it to redraw with the new background color.
322     */
323     ignoreResize = true;
324     OS.SendMessage (handle, OS.WM_SIZE, 0, 0);
325     ignoreResize = false;
326 }
327
328 override void setBackgroundPixel (int pixel) {
329     super.setBackgroundPixel (pixel);
330     /*
331     * Bug in Windows.  Changing the background color of the Scale
332     * widget and calling InvalidateRect() still draws with the old
333     * color.  The fix is to send a fake WM_SIZE event to cause
334     * it to redraw with the new background color.
335     */
336     ignoreResize = true;
337     OS.SendMessage (handle, OS.WM_SIZE, 0, 0);
338     ignoreResize = false;
339 }
340
341 void setBounds (int x, int y, int width, int height, int flags, bool defer) {
342     /*
343     * Bug in Windows.  If SetWindowPos() is called on a
344     * track bar with either SWP_DRAWFRAME, a new size,
345     * or both during mouse down, the track bar posts a
346     * WM_MOUSEMOVE message when the mouse has not moved.
347     * The window proc for the track bar uses WM_MOUSEMOVE
348     * to issue WM_HSCROLL or WM_SCROLL events to notify
349     * the application that the slider has changed.  The
350     * end result is that when the user requests a page
351     * scroll and the application resizes the track bar
352     * during the change notification, continuous stream
353     * of WM_MOUSEMOVE messages are generated and the
354     * thumb moves to the mouse position rather than
355     * scrolling by a page.  The fix is to clear the
356     * SWP_DRAWFRAME flag.
357     *
358     * NOTE:  There is no fix for the WM_MOUSEMOVE that
359     * is generated by a new size.  Clearing SWP_DRAWFRAME
360     * does not fix the problem.  However, it is unlikely
361     * that the programmer will resize the control during
362     * mouse down.
363     */
364     flags &= ~OS.SWP_DRAWFRAME;
365     super.setBounds (x, y, width, height, flags, true);
366 }
367
368 /**
369  * Sets the amount that the receiver's value will be
370  * modified by when the up/down (or right/left) arrows
371  * are pressed to the argument, which must be at least
372  * one.
373  *
374  * @param increment the new increment (must be greater than zero)
375  *
376  * @exception DWTException <ul>
377  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
378  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
379  * </ul>
380  */
381 public void setIncrement (int increment) {
382     checkWidget ();
383     if (increment < 1) return;
384     int minimum = OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
385     int maximum = OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
386     if (increment > maximum - minimum) return;
387     OS.SendMessage (handle, OS.TBM_SETLINESIZE, 0, increment);
388 }
389
390 /**
391  * Sets the maximum value that the receiver will allow.  This new
392  * value will be ignored if it is not greater than the receiver's current
393  * minimum value.  If the new maximum is applied then the receiver's
394  * selection value will be adjusted if necessary to fall within its new range.
395  *
396  * @param value the new maximum, which must be greater than the current minimum
397  *
398  * @exception DWTException <ul>
399  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
400  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
401  * </ul>
402  */
403 public void setMaximum (int value) {
404     checkWidget ();
405     int minimum = OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
406     if (0 <= minimum && minimum < value) {
407         OS.SendMessage (handle, OS.TBM_SETRANGEMAX, 1, value);
408     }
409 }
410
411 /**
412  * Sets the minimum value that the receiver will allow.  This new
413  * value will be ignored if it is negative or is not less than the receiver's
414  * current maximum value.  If the new minimum is applied then the receiver's
415  * selection value will be adjusted if necessary to fall within its new range.
416  *
417  * @param value the new minimum, which must be nonnegative and less than the current maximum
418  *
419  * @exception DWTException <ul>
420  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
421  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
422  * </ul>
423  */
424 public void setMinimum (int value) {
425     checkWidget ();
426     int maximum = OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
427     if (0 <= value && value < maximum) {
428         OS.SendMessage (handle, OS.TBM_SETRANGEMIN, 1, value);
429     }
430 }
431
432 /**
433  * Sets the amount that the receiver's value will be
434  * modified by when the page increment/decrement areas
435  * are selected to the argument, which must be at least
436  * one.
437  *
438  * @param pageIncrement the page increment (must be greater than zero)
439  *
440  * @exception DWTException <ul>
441  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
442  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
443  * </ul>
444  */
445 public void setPageIncrement (int pageIncrement) {
446     checkWidget ();
447     if (pageIncrement < 1) return;
448     int minimum = OS.SendMessage (handle, OS.TBM_GETRANGEMIN, 0, 0);
449     int maximum = OS.SendMessage (handle, OS.TBM_GETRANGEMAX, 0, 0);
450     if (pageIncrement > maximum - minimum) return;
451     OS.SendMessage (handle, OS.TBM_SETPAGESIZE, 0, pageIncrement);
452     OS.SendMessage (handle, OS.TBM_SETTICFREQ, pageIncrement, 0);
453 }
454
455 /**
456  * Sets the 'selection', which is the receiver's value,
457  * to the argument which must be greater than or equal to zero.
458  *
459  * @param value the new selection (must be zero or greater)
460  *
461  * @exception DWTException <ul>
462  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
463  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
464  * </ul>
465  */
466 public void setSelection (int value) {
467     checkWidget ();
468     OS.SendMessage (handle, OS.TBM_SETPOS, 1, value);
469 }
470
471 override int widgetStyle () {
472     int bits = super.widgetStyle () | OS.WS_TABSTOP | OS.TBS_BOTH | OS.TBS_AUTOTICKS;
473     if ((style & DWT.HORIZONTAL) !is 0) return bits | OS.TBS_HORZ | OS.TBS_DOWNISLEFT;
474     return bits | OS.TBS_VERT;
475 }
476
477 override String windowClass () {
478     return TCHARsToStr(TrackBarClass);
479 }
480
481 override int windowProc () {
482     return cast(int) TrackBarProc;
483 }
484
485 override LRESULT WM_MOUSEWHEEL (int wParam, int lParam) {
486     LRESULT result = super.WM_MOUSEWHEEL (wParam, lParam);
487     if (result !is null) return result;
488     /*
489     * Bug in Windows.  When a track bar slider is changed
490     * from WM_MOUSEWHEEL, it does not always send either
491     * a WM_VSCROLL or M_HSCROLL to notify the application
492     * of the change.  The fix is to detect that the selection
493     * has changed and that notification has not been issued
494     * and send the selection event.
495     */
496     int oldPosition = OS.SendMessage (handle, OS.TBM_GETPOS, 0, 0);
497     ignoreSelection = true;
498     int /*long*/ code = callWindowProc (handle, OS.WM_MOUSEWHEEL, wParam, lParam);
499     ignoreSelection = false;
500     int newPosition = OS.SendMessage (handle, OS.TBM_GETPOS, 0, 0);
501     if (oldPosition !is newPosition) {
502         /*
503         * Send the event because WM_HSCROLL and WM_VSCROLL
504         * are sent from a modal message loop in windows that
505         * is active when the user is scrolling.
506         */
507         sendEvent (DWT.Selection);
508         // widget could be disposed at this point
509     }
510     return new LRESULT (code);
511 }
512
513 override LRESULT WM_PAINT (int wParam, int lParam) {
514     /*
515     * Bug in Windows.  For some reason, when WM_CTLCOLORSTATIC
516     * is used to implement transparency and returns a NULL brush,
517     * Windows doesn't always draw the track bar.  It seems that
518     * it is drawn correctly the first time.  It is possible that
519     * Windows double buffers the control and the double buffer
520     * strategy fails when WM_CTLCOLORSTATIC returns unexpected
521     * results.  The fix is to send a fake WM_SIZE to force it
522     * to redraw every time there is a WM_PAINT.
523     */
524     bool fixPaint = findBackgroundControl () !is null;
525     if (!fixPaint) {
526         if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
527             Control control = findThemeControl ();
528             fixPaint = control !is null;
529         }
530     }
531     if (fixPaint) {
532         bool redraw = drawCount is 0 && OS.IsWindowVisible (handle);
533         if (redraw) OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0);
534         ignoreResize = true;
535         OS.SendMessage (handle, OS.WM_SIZE, 0, 0);
536         ignoreResize = false;
537         if (redraw) {
538             OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0);
539             OS.InvalidateRect (handle, null, false);
540         }
541     }
542     return super.WM_PAINT (wParam, lParam);
543 }
544
545 override LRESULT WM_SIZE (int wParam, int lParam) {
546     if (ignoreResize) return null;
547     return super.WM_SIZE (wParam, lParam);
548 }
549
550 override LRESULT wmScrollChild (int wParam, int lParam) {
551
552     /* Do nothing when scrolling is ending */
553     int code = OS.LOWORD (wParam);
554     switch (code) {
555         case OS.TB_ENDTRACK:
556         case OS.TB_THUMBPOSITION:
557             return null;
558         default:
559     }
560
561     if (!ignoreSelection) {
562         Event event = new Event ();
563         /*
564         * This code is intentionally commented.  The event
565         * detail field is not currently supported on all
566         * platforms.
567         */
568 //      switch (code) {
569 //          case OS.TB_TOP:         event.detail = DWT.HOME;  break;
570 //          case OS.TB_BOTTOM:      event.detail = DWT.END;  break;
571 //          case OS.TB_LINEDOWN:    event.detail = DWT.ARROW_DOWN;  break;
572 //          case OS.TB_LINEUP:      event.detail = DWT.ARROW_UP;  break;
573 //          case OS.TB_PAGEDOWN:    event.detail = DWT.PAGE_DOWN;  break;
574 //          case OS.TB_PAGEUP:      event.detail = DWT.PAGE_UP;  break;
575 //      default:
576 //      }
577
578         /*
579         * Send the event because WM_HSCROLL and WM_VSCROLL
580         * are sent from a modal message loop in windows that
581         * is active when the user is scrolling.
582         */
583         sendEvent (DWT.Selection, event);
584         // widget could be disposed at this point
585     }
586     return null;
587 }
588
589 }
Note: See TracBrowser for help on using the browser.