KickJava   Java API By Example, From Geeks To Geeks.

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


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.graphics.*;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.events.*;
18 import org.eclipse.swt.accessibility.*;
19
20 /**
21  * Control is the abstract superclass of all windowed user interface classes.
22  * <p>
23  * <dl>
24  * <dt><b>Styles:</b>
25  * <dd>BORDER</dd>
26  * <dd>LEFT_TO_RIGHT, RIGHT_TO_LEFT</dd>
27  * <dt><b>Events:</b>
28  * <dd>DragDetect, FocusIn, FocusOut, Help, KeyDown, KeyUp, MenuDetect, MouseDoubleClick, MouseDown, MouseEnter,
29  * MouseExit, MouseHover, MouseUp, MouseMove, Move, Paint, Resize, Traverse</dd>
30  * </dl>
31  * </p><p>
32  * Only one of LEFT_TO_RIGHT or RIGHT_TO_LEFT may be specified.
33  * </p><p>
34  * IMPORTANT: This class is intended to be subclassed <em>only</em>
35  * within the SWT implementation.
36  * </p>
37  */

38
39 public abstract class Control extends Widget implements Drawable {
40     /**
41      * the handle to the OS resource
42      * (Warning: This field is platform dependent)
43      * <p>
44      * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
45      * public API. It is marked public only so that it can be shared
46      * within the packages provided by SWT. It is not available on all
47      * platforms and should never be accessed from application code.
48      * </p>
49      */

50     public int handle;
51     Composite parent;
52     Cursor cursor;
53     Menu menu;
54     String JavaDoc toolTipText;
55     Object JavaDoc layoutData;
56     Accessible accessible;
57     Image backgroundImage;
58     int drawCount, foreground, background;
59
60 /**
61  * Prevents uninitialized instances from being created outside the package.
62  */

63 Control () {
64 }
65
66 /**
67  * Constructs a new instance of this class given its parent
68  * and a style value describing its behavior and appearance.
69  * <p>
70  * The style value is either one of the style constants defined in
71  * class <code>SWT</code> which is applicable to instances of this
72  * class, or must be built by <em>bitwise OR</em>'ing together
73  * (that is, using the <code>int</code> "|" operator) two or more
74  * of those <code>SWT</code> style constants. The class description
75  * lists the style constants that are applicable to the class.
76  * Style bits are also inherited from superclasses.
77  * </p>
78  *
79  * @param parent a composite control which will be the parent of the new instance (cannot be null)
80  * @param style the style of control to construct
81  *
82  * @exception IllegalArgumentException <ul>
83  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
84  * </ul>
85  * @exception SWTException <ul>
86  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
87  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
88  * </ul>
89  *
90  * @see SWT#BORDER
91  * @see Widget#checkSubclass
92  * @see Widget#getStyle
93  */

94 public Control (Composite parent, int style) {
95     super (parent, style);
96     this.parent = parent;
97     createWidget ();
98 }
99
100 /**
101  * Adds the listener to the collection of listeners who will
102  * be notified when the control is moved or resized, by sending
103  * it one of the messages defined in the <code>ControlListener</code>
104  * interface.
105  *
106  * @param listener the listener which should be notified
107  *
108  * @exception IllegalArgumentException <ul>
109  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
110  * </ul>
111  * @exception SWTException <ul>
112  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
113  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
114  * </ul>
115  *
116  * @see ControlListener
117  * @see #removeControlListener
118  */

119 public void addControlListener(ControlListener listener) {
120     checkWidget ();
121     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
122     TypedListener typedListener = new TypedListener (listener);
123     addListener (SWT.Resize,typedListener);
124     addListener (SWT.Move,typedListener);
125 }
126
127 /**
128  * Adds the listener to the collection of listeners who will
129  * be notified when a drag gesture occurs, by sending it
130  * one of the messages defined in the <code>DragDetectListener</code>
131  * interface.
132  *
133  * @param listener the listener which should be notified
134  *
135  * @exception IllegalArgumentException <ul>
136  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
137  * </ul>
138  * @exception SWTException <ul>
139  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
140  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
141  * </ul>
142  *
143  * @see DragDetectListener
144  * @see #removeDragDetectListener
145  *
146  * @since 3.3
147  */

148 public void addDragDetectListener (DragDetectListener listener) {
149     checkWidget ();
150     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
151     TypedListener typedListener = new TypedListener (listener);
152     addListener (SWT.DragDetect,typedListener);
153 }
154
155 /**
156  * Adds the listener to the collection of listeners who will
157  * be notified when the control gains or loses focus, by sending
158  * it one of the messages defined in the <code>FocusListener</code>
159  * interface.
160  *
161  * @param listener the listener which should be notified
162  *
163  * @exception IllegalArgumentException <ul>
164  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
165  * </ul>
166  * @exception SWTException <ul>
167  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
168  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
169  * </ul>
170  *
171  * @see FocusListener
172  * @see #removeFocusListener
173  */

174 public void addFocusListener (FocusListener listener) {
175     checkWidget ();
176     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
177     TypedListener typedListener = new TypedListener (listener);
178     addListener (SWT.FocusIn,typedListener);
179     addListener (SWT.FocusOut,typedListener);
180 }
181
182 /**
183  * Adds the listener to the collection of listeners who will
184  * be notified when help events are generated for the control,
185  * by sending it one of the messages defined in the
186  * <code>HelpListener</code> interface.
187  *
188  * @param listener the listener which should be notified
189  *
190  * @exception IllegalArgumentException <ul>
191  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
192  * </ul>
193  * @exception SWTException <ul>
194  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
195  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
196  * </ul>
197  *
198  * @see HelpListener
199  * @see #removeHelpListener
200  */

201 public void addHelpListener (HelpListener listener) {
202     checkWidget ();
203     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
204     TypedListener typedListener = new TypedListener (listener);
205     addListener (SWT.Help, typedListener);
206 }
207
208 /**
209  * Adds the listener to the collection of listeners who will
210  * be notified when keys are pressed and released on the system keyboard, by sending
211  * it one of the messages defined in the <code>KeyListener</code>
212  * interface.
213  * <p>
214  * When a key listener is added to a control, the control
215  * will take part in widget traversal. By default, all
216  * traversal keys (such as the tab key and so on) are
217  * delivered to the control. In order for a control to take
218  * part in traversal, it should listen for traversal events.
219  * Otherwise, the user can traverse into a control but not
220  * out. Note that native controls such as table and tree
221  * implement key traversal in the operating system. It is
222  * not necessary to add traversal listeners for these controls,
223  * unless you want to override the default traversal.
224  * </p>
225  * @param listener the listener which should be notified
226  *
227  * @exception IllegalArgumentException <ul>
228  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
229  * </ul>
230  * @exception SWTException <ul>
231  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
232  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
233  * </ul>
234  *
235  * @see KeyListener
236  * @see #removeKeyListener
237  */

238 public void addKeyListener (KeyListener listener) {
239     checkWidget ();
240     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
241     TypedListener typedListener = new TypedListener (listener);
242     addListener (SWT.KeyUp,typedListener);
243     addListener (SWT.KeyDown,typedListener);
244 }
245
246 /**
247  * Adds the listener to the collection of listeners who will
248  * be notified when the platform-specific context menu trigger
249  * has occurred, by sending it one of the messages defined in
250  * the <code>MenuDetectListener</code> interface.
251  *
252  * @param listener the listener which should be notified
253  *
254  * @exception IllegalArgumentException <ul>
255  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
256  * </ul>
257  * @exception SWTException <ul>
258  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
259  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
260  * </ul>
261  *
262  * @see MenuDetectListener
263  * @see #removeMenuDetectListener
264  *
265  * @since 3.3
266  */

267 public void addMenuDetectListener (MenuDetectListener listener) {
268     checkWidget ();
269     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
270     TypedListener typedListener = new TypedListener (listener);
271     addListener (SWT.MenuDetect, typedListener);
272 }
273
274 /**
275  * Adds the listener to the collection of listeners who will
276  * be notified when mouse buttons are pressed and released, by sending
277  * it one of the messages defined in the <code>MouseListener</code>
278  * interface.
279  *
280  * @param listener the listener which should be notified
281  *
282  * @exception IllegalArgumentException <ul>
283  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
284  * </ul>
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  * @see MouseListener
291  * @see #removeMouseListener
292  */

293 public void addMouseListener (MouseListener listener) {
294     checkWidget ();
295     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
296     TypedListener typedListener = new TypedListener (listener);
297     addListener (SWT.MouseDown,typedListener);
298     addListener (SWT.MouseUp,typedListener);
299     addListener (SWT.MouseDoubleClick,typedListener);
300 }
301
302 /**
303  * Adds the listener to the collection of listeners who will
304  * be notified when the mouse passes or hovers over controls, by sending
305  * it one of the messages defined in the <code>MouseTrackListener</code>
306  * interface.
307  *
308  * @param listener the listener which should be notified
309  *
310  * @exception IllegalArgumentException <ul>
311  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
312  * </ul>
313  * @exception SWTException <ul>
314  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
315  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
316  * </ul>
317  *
318  * @see MouseTrackListener
319  * @see #removeMouseTrackListener
320  */

321 public void addMouseTrackListener (MouseTrackListener listener) {
322     checkWidget ();
323     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
324     TypedListener typedListener = new TypedListener (listener);
325     addListener (SWT.MouseEnter,typedListener);
326     addListener (SWT.MouseExit,typedListener);
327     addListener (SWT.MouseHover,typedListener);
328 }
329
330 /**
331  * Adds the listener to the collection of listeners who will
332  * be notified when the mouse moves, by sending it one of the
333  * messages defined in the <code>MouseMoveListener</code>
334  * interface.
335  *
336  * @param listener the listener which should be notified
337  *
338  * @exception IllegalArgumentException <ul>
339  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
340  * </ul>
341  * @exception SWTException <ul>
342  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
343  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
344  * </ul>
345  *
346  * @see MouseMoveListener
347  * @see #removeMouseMoveListener
348  */

349 public void addMouseMoveListener (MouseMoveListener listener) {
350     checkWidget ();
351     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
352     TypedListener typedListener = new TypedListener (listener);
353     addListener (SWT.MouseMove,typedListener);
354 }
355
356 /**
357  * Adds the listener to the collection of listeners who will
358  * be notified when the mouse wheel is scrolled, by sending
359  * it one of the messages defined in the
360  * <code>MouseWheelListener</code> interface.
361  *
362  * @param listener the listener which should be notified
363  *
364  * @exception IllegalArgumentException <ul>
365  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
366  * </ul>
367  * @exception SWTException <ul>
368  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
369  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
370  * </ul>
371  *
372  * @see MouseWheelListener
373  * @see #removeMouseWheelListener
374  *
375  * @since 3.3
376  */

377 public void addMouseWheelListener (MouseWheelListener listener) {
378     checkWidget ();
379     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
380     TypedListener typedListener = new TypedListener (listener);
381     addListener (SWT.MouseWheel, typedListener);
382 }
383
384 /**
385  * Adds the listener to the collection of listeners who will
386  * be notified when the receiver needs to be painted, by sending it
387  * one of the messages defined in the <code>PaintListener</code>
388  * interface.
389  *
390  * @param listener the listener which should be notified
391  *
392  * @exception IllegalArgumentException <ul>
393  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
394  * </ul>
395  * @exception SWTException <ul>
396  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
397  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
398  * </ul>
399  *
400  * @see PaintListener
401  * @see #removePaintListener
402  */

403 public void addPaintListener (PaintListener listener) {
404     checkWidget ();
405     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
406     TypedListener typedListener = new TypedListener (listener);
407     addListener (SWT.Paint,typedListener);
408 }
409
410 /**
411  * Adds the listener to the collection of listeners who will
412  * be notified when traversal events occur, by sending it
413  * one of the messages defined in the <code>TraverseListener</code>
414  * interface.
415  *
416  * @param listener the listener which should be notified
417  *
418  * @exception IllegalArgumentException <ul>
419  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
420  * </ul>
421  * @exception SWTException <ul>
422  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
423  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
424  * </ul>
425  *
426  * @see TraverseListener
427  * @see #removeTraverseListener
428  */

429 public void addTraverseListener (TraverseListener listener) {
430     checkWidget ();
431     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
432     TypedListener typedListener = new TypedListener (listener);
433     addListener (SWT.Traverse,typedListener);
434 }
435
436 int borderHandle () {
437     return handle;
438 }
439
440 void checkBackground () {
441     Shell shell = getShell ();
442     if (this == shell) return;
443     state &= ~PARENT_BACKGROUND;
444     Composite composite = parent;
445     do {
446         int mode = composite.backgroundMode;
447         if (mode != 0) {
448             if (mode == SWT.INHERIT_DEFAULT) {
449                 Control control = this;
450                 do {
451                     if ((control.state & THEME_BACKGROUND) == 0) {
452                         return;
453                     }
454                     control = control.parent;
455                 } while (control != composite);
456             }
457             state |= PARENT_BACKGROUND;
458             return;
459         }
460         if (composite == shell) break;
461         composite = composite.parent;
462     } while (true);
463 }
464
465 void checkBorder () {
466     if (getBorderWidth () == 0) style &= ~SWT.BORDER;
467 }
468
469 void checkBuffered () {
470     style &= ~SWT.DOUBLE_BUFFERED;
471 }
472
473 boolean checkHandle (int hwnd) {
474     return hwnd == handle;
475 }
476
477 void checkMirrored () {
478     if ((style & SWT.RIGHT_TO_LEFT) != 0) {
479         int bits = OS.GetWindowLong (handle, OS.GWL_EXSTYLE);
480         if ((bits & OS.WS_EX_LAYOUTRTL) != 0) style |= SWT.MIRRORED;
481     }
482 }
483
484 /**
485  * Returns the preferred size of the receiver.
486  * <p>
487  * The <em>preferred size</em> of a control is the size that it would
488  * best be displayed at. The width hint and height hint arguments
489  * allow the caller to ask a control questions such as "Given a particular
490  * width, how high does the control need to be to show all of the contents?"
491  * To indicate that the caller does not wish to constrain a particular
492  * dimension, the constant <code>SWT.DEFAULT</code> is passed for the hint.
493  * </p>
494  *
495  * @param wHint the width hint (can be <code>SWT.DEFAULT</code>)
496  * @param hHint the height hint (can be <code>SWT.DEFAULT</code>)
497  * @return the preferred size of the control
498  *
499  * @exception SWTException <ul>
500  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
501  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
502  * </ul>
503  *
504  * @see Layout
505  * @see #getBorderWidth
506  * @see #getBounds
507  * @see #getSize
508  * @see #pack(boolean)
509  * @see "computeTrim, getClientArea for controls that implement them"
510  */

511 public Point computeSize (int wHint, int hHint) {
512     return computeSize (wHint, hHint, true);
513 }
514
515 /**
516  * Returns the preferred size of the receiver.
517  * <p>
518  * The <em>preferred size</em> of a control is the size that it would
519  * best be displayed at. The width hint and height hint arguments
520  * allow the caller to ask a control questions such as "Given a particular
521  * width, how high does the control need to be to show all of the contents?"
522  * To indicate that the caller does not wish to constrain a particular
523  * dimension, the constant <code>SWT.DEFAULT</code> is passed for the hint.
524  * </p><p>
525  * If the changed flag is <code>true</code>, it indicates that the receiver's
526  * <em>contents</em> have changed, therefore any caches that a layout manager
527  * containing the control may have been keeping need to be flushed. When the
528  * control is resized, the changed flag will be <code>false</code>, so layout
529  * manager caches can be retained.
530  * </p>
531  *
532  * @param wHint the width hint (can be <code>SWT.DEFAULT</code>)
533  * @param hHint the height hint (can be <code>SWT.DEFAULT</code>)
534  * @param changed <code>true</code> if the control's contents have changed, and <code>false</code> otherwise
535  * @return the preferred size of the control.
536  *
537  * @exception SWTException <ul>
538  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
539  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
540  * </ul>
541  *
542  * @see Layout
543  * @see #getBorderWidth
544  * @see #getBounds
545  * @see #getSize
546  * @see #pack(boolean)
547  * @see "computeTrim, getClientArea for controls that implement them"
548  */

549 public Point computeSize (int wHint, int hHint, boolean changed) {
550     checkWidget ();
551     int width = DEFAULT_WIDTH;
552     int height = DEFAULT_HEIGHT;
553     if (wHint != SWT.DEFAULT) width = wHint;
554     if (hHint != SWT.DEFAULT) height = hHint;
555     int border = getBorderWidth ();
556     width += border * 2;
557     height += border * 2;
558     return new Point (width, height);
559 }
560
561 Control computeTabGroup () {
562     if (isTabGroup ()) return this;
563     return parent.computeTabGroup ();
564 }
565
566 Control computeTabRoot () {
567     Control [] tabList = parent._getTabList ();
568     if (tabList != null) {
569         int index = 0;
570         while (index < tabList.length) {
571             if (tabList [index] == this) break;
572             index++;
573         }
574         if (index == tabList.length) {
575             if (isTabGroup ()) return this;
576         }
577     }
578     return parent.computeTabRoot ();
579 }
580
581 Control [] computeTabList () {
582     if (isTabGroup ()) {
583         if (getVisible () && getEnabled ()) {
584             return new Control [] {this};
585         }
586     }
587     return new Control [0];
588 }
589
590 void createHandle () {
591     int hwndParent = widgetParent ();
592     handle = OS.CreateWindowEx (
593         widgetExtStyle (),
594         windowClass (),
595         null,
596         widgetStyle (),
597         OS.CW_USEDEFAULT, 0, OS.CW_USEDEFAULT, 0,
598         hwndParent,
599         0,
600         OS.GetModuleHandle (null),
601         widgetCreateStruct ());
602     if (handle == 0) error (SWT.ERROR_NO_HANDLES);
603     int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
604     if ((bits & OS.WS_CHILD) != 0) {
605         OS.SetWindowLong (handle, OS.GWL_ID, handle);
606     }
607     if (OS.IsDBLocale && hwndParent != 0) {
608         int hIMC = OS.ImmGetContext (hwndParent);
609         OS.ImmAssociateContext (handle, hIMC);
610         OS.ImmReleaseContext (hwndParent, hIMC);
611     }
612 }
613
614 void createWidget () {
615     state |= DRAG_DETECT;
616     foreground = background = -1;
617     checkOrientation (parent);
618     createHandle ();
619     checkBackground ();
620     checkBuffered ();
621     register ();
622     subclass ();
623     setDefaultFont ();
624     checkMirrored ();
625     checkBorder ();
626     if ((state & PARENT_BACKGROUND) != 0) {
627         setBackground ();
628     }
629 }
630
631 int defaultBackground () {
632     if (OS.IsWinCE) return OS.GetSysColor (OS.COLOR_WINDOW);
633     return OS.GetSysColor (OS.COLOR_BTNFACE);
634 }
635
636 int defaultFont () {
637     return display.getSystemFont ().handle;
638 }
639
640 int defaultForeground () {
641     return OS.GetSysColor (OS.COLOR_WINDOWTEXT);
642 }
643
644 void deregister () {
645     display.removeControl (handle);
646 }
647
648 void destroyWidget () {
649     int hwnd = topHandle ();
650     releaseHandle ();
651     if (hwnd != 0) {
652         OS.DestroyWindow (hwnd);
653     }
654 }
655
656 /**
657  * Detects a drag and drop gesture. This method is used
658  * to detect a drag gesture when called from within a mouse
659  * down listener.
660  *
661  * <p>By default, a drag is detected when the gesture
662  * occurs anywhere within the client area of a control.
663  * Some controls, such as tables and trees, override this
664  * behavior. In addition to the operating system specific
665  * drag gesture, they require the mouse to be inside an
666  * item. Custom widget writers can use <code>setDragDetect</code>
667  * to disable the default detection, listen for mouse down,
668  * and then call <code>dragDetect()</code> from within the
669  * listener to conditionally detect a drag.
670  * </p>
671  *
672  * @param event the mouse down event
673  *
674  * @return <code>true</code> if the gesture occurred, and <code>false</code> otherwise.
675  *
676  * @exception IllegalArgumentException <ul>
677  * <li>ERROR_NULL_ARGUMENT when the event is null</li>
678  * </ul>
679  * @exception SWTException <ul>
680  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
681  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
682  * </ul>
683  *
684  * @see DragDetectListener
685  * @see #addDragDetectListener
686  *
687  * @see #getDragDetect
688  * @see #setDragDetect
689  *
690  * @since 3.3
691  */

692 public boolean dragDetect (Event event) {
693     checkWidget ();
694     if (event == null) error (SWT.ERROR_NULL_ARGUMENT);
695     return dragDetect (event.button, event.count, event.stateMask, event.x, event.y);
696 }
697
698 /**
699  * Detects a drag and drop gesture. This method is used
700  * to detect a drag gesture when called from within a mouse
701  * down listener.
702  *
703  * <p>By default, a drag is detected when the gesture
704  * occurs anywhere within the client area of a control.
705  * Some controls, such as tables and trees, override this
706  * behavior. In addition to the operating system specific
707  * drag gesture, they require the mouse to be inside an
708  * item. Custom widget writers can use <code>setDragDetect</code>
709  * to disable the default detection, listen for mouse down,
710  * and then call <code>dragDetect()</code> from within the
711  * listener to conditionally detect a drag.
712  * </p>
713  *
714  * @param event the mouse down event
715  *
716  * @return <code>true</code> if the gesture occurred, and <code>false</code> otherwise.
717  *
718  * @exception IllegalArgumentException <ul>
719  * <li>ERROR_NULL_ARGUMENT when the event is null</li>
720  * </ul>
721  * @exception SWTException <ul>
722  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
723  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
724  * </ul>
725  *
726  * @see DragDetectListener
727  * @see #addDragDetectListener
728  *
729  * @see #getDragDetect
730  * @see #setDragDetect
731  *
732  * @since 3.3
733  */

734 public boolean dragDetect (MouseEvent event) {
735     checkWidget ();
736     if (event == null) error (SWT.ERROR_NULL_ARGUMENT);
737     return dragDetect (event.button, event.count, event.stateMask, event.x, event.y);
738 }
739
740 boolean dragDetect (int button, int count, int stateMask, int x, int y) {
741     if (button != 1 || count != 1) return false;
742     boolean dragging = dragDetect (handle, x, y, false, null, null);
743     if (OS.GetKeyState (OS.VK_LBUTTON) < 0) {
744         if (OS.GetCapture () != handle) OS.SetCapture (handle);
745     }
746     if (!dragging) {
747         /*
748         * Feature in Windows. DragDetect() captures the mouse
749         * and tracks its movement until the user releases the
750         * left mouse button, presses the ESC key, or moves the
751         * mouse outside the drag rectangle. If the user moves
752         * the mouse outside of the drag rectangle, DragDetect()
753         * returns true and a drag and drop operation can be
754         * started. When the left mouse button is released or
755         * the ESC key is pressed, these events are consumed by
756         * DragDetect() so that application code that matches
757         * mouse down/up pairs or looks for the ESC key will not
758         * function properly. The fix is to send the missing
759         * events when the drag has not started.
760         *
761         * NOTE: For now, don't send a fake WM_KEYDOWN/WM_KEYUP
762         * events for the ESC key. This would require computing
763         * wParam (the key) and lParam (the repeat count, scan code,
764         * extended-key flag, context code, previous key-state flag,
765         * and transition-state flag) which is non-trivial.
766         */

767         if (button == 1 && OS.GetKeyState (OS.VK_ESCAPE) >= 0) {
768             int wParam = 0;
769             if ((stateMask & SWT.CTRL) != 0) wParam |= OS.MK_CONTROL;
770             if ((stateMask & SWT.SHIFT) != 0) wParam |= OS.MK_SHIFT;
771             if ((stateMask & SWT.ALT) != 0) wParam |= OS.MK_ALT;
772             if ((stateMask & SWT.BUTTON1) != 0) wParam |= OS.MK_LBUTTON;
773             if ((stateMask & SWT.BUTTON2) != 0) wParam |= OS.MK_MBUTTON;
774             if ((stateMask & SWT.BUTTON3) != 0) wParam |= OS.MK_RBUTTON;
775             if ((stateMask & SWT.BUTTON4) != 0) wParam |= OS.MK_XBUTTON1;
776             if ((stateMask & SWT.BUTTON5) != 0) wParam |= OS.MK_XBUTTON2;
777             int lParam = (x & 0xFFFF) | ((y << 16) & 0xFFFF0000);
778             OS.SendMessage (handle, OS.WM_LBUTTONUP, wParam, lParam);
779         }
780         return false;
781     }
782     return sendDragEvent (button, stateMask, x, y);
783 }
784
785 void drawBackground (int hDC) {
786     RECT rect = new RECT ();
787     OS.GetClientRect (handle, rect);
788     drawBackground (hDC, rect);
789 }
790
791 void drawBackground (int hDC, RECT rect) {
792     drawBackground (hDC, rect, -1);
793 }
794
795 void drawBackground (int hDC, RECT rect, int pixel) {
796     Control control = findBackgroundControl ();
797     if (control != null) {
798         if (control.backgroundImage != null) {
799             fillImageBackground (hDC, control, rect);
800             return;
801         }
802         pixel = control.getBackgroundPixel ();
803     }
804     if (pixel == -1) {
805         if ((state & THEME_BACKGROUND) != 0) {
806             if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
807                 control = findThemeControl ();
808                 if (control != null) {
809                     fillThemeBackground (hDC, control, rect);
810                     return;
811                 }
812             }
813         }
814     }
815     if (pixel == -1) pixel = getBackgroundPixel ();
816     fillBackground (hDC, pixel, rect);
817 }
818
819 void drawImageBackground (int hDC, int hwnd, int hBitmap, RECT rect) {
820     RECT rect2 = new RECT ();
821     OS.GetClientRect (hwnd, rect2);
822     OS.MapWindowPoints (hwnd, handle, rect2, 2);
823     int hBrush = findBrush (hBitmap, OS.BS_PATTERN);
824     POINT lpPoint = new POINT ();
825     OS.GetWindowOrgEx (hDC, lpPoint);
826     OS.SetBrushOrgEx (hDC, -rect2.left - lpPoint.x, -rect2.top - lpPoint.y, lpPoint);
827     int hOldBrush = OS.SelectObject (hDC, hBrush);
828     OS.PatBlt (hDC, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, OS.PATCOPY);
829     OS.SetBrushOrgEx (hDC, lpPoint.x, lpPoint.y, null);
830     OS.SelectObject (hDC, hOldBrush);
831 }
832
833 void drawThemeBackground (int hDC, int hwnd, RECT rect) {
834     /* Do nothing */
835 }
836
837 void enableDrag (boolean enabled) {
838     /* Do nothing */
839 }
840
841 void enableWidget (boolean enabled) {
842     OS.EnableWindow (handle, enabled);
843 }
844
845 void fillBackground (int hDC, int pixel, RECT rect) {
846     if (rect.left > rect.right || rect.top > rect.bottom) return;
847     int hPalette = display.hPalette;
848     if (hPalette != 0) {
849         OS.SelectPalette (hDC, hPalette, false);
850         OS.RealizePalette (hDC);
851     }
852     OS.FillRect (hDC, rect, findBrush (pixel, OS.BS_SOLID));
853 }
854
855 void fillImageBackground (int hDC, Control control, RECT rect) {
856     if (rect.left > rect.right || rect.top > rect.bottom) return;
857     if (control != null) {
858         Image image = control.backgroundImage;
859         if (image != null) {
860             control.drawImageBackground (hDC, handle, image.handle, rect);
861         }
862     }
863 }
864
865 void fillThemeBackground (int hDC, Control control, RECT rect) {
866     if (rect.left > rect.right || rect.top > rect.bottom) return;
867     if (control != null) {
868         control.drawThemeBackground (hDC, handle, rect);
869     }
870 }
871
872 Control findBackgroundControl () {
873     if (background != -1 || backgroundImage != null) return this;
874     return (state & PARENT_BACKGROUND) != 0 ? parent.findBackgroundControl () : null;
875 }
876
877 int findBrush (int value, int lbStyle) {
878     return parent.findBrush (value, lbStyle);
879 }
880
881 Cursor findCursor () {
882     if (cursor != null) return cursor;
883     return parent.findCursor ();
884 }
885
886 Control findImageControl () {
887     Control control = findBackgroundControl ();
888     return control != null && control.backgroundImage != null ? control : null;
889 }
890
891 Control findThemeControl () {
892     return background == -1 && backgroundImage == null ? parent.findThemeControl () : null;
893 }
894
895 Menu [] findMenus (Control control) {
896     if (menu != null && this != control) return new Menu [] {menu};
897     return new Menu [0];
898 }
899
900 char findMnemonic (String JavaDoc string) {
901     int index = 0;
902     int length = string.length ();
903     do {
904         while (index < length && string.charAt (index) != '&') index++;
905         if (++index >= length) return '\0';
906         if (string.charAt (index) != '&') return string.charAt (index);
907         index++;
908     } while (index < length);
909     return '\0';
910 }
911
912 void fixChildren (Shell newShell, Shell oldShell, Decorations newDecorations, Decorations oldDecorations, Menu [] menus) {
913     oldShell.fixShell (newShell, this);
914     oldDecorations.fixDecorations (newDecorations, this, menus);
915 }
916
917 void fixFocus (Control focusControl) {
918     Shell shell = getShell ();
919     Control control = this;
920     while (control != shell && (control = control.parent) != null) {
921         if (control.setFixedFocus ()) return;
922     }
923     shell.setSavedFocus (focusControl);
924     OS.SetFocus (0);
925 }
926
927 /**
928  * Forces the receiver to have the <em>keyboard focus</em>, causing
929  * all keyboard events to be delivered to it.
930  *
931  * @return <code>true</code> if the control got focus, and <code>false</code> if it was unable to.
932  *
933  * @exception SWTException <ul>
934  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
935  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
936  * </ul>
937  *
938  * @see #setFocus
939  */

940 public boolean forceFocus () {
941     checkWidget ();
942     if (display.focusEvent == SWT.FocusOut) return false;
943     Decorations shell = menuShell ();
944     shell.setSavedFocus (this);
945     if (!isEnabled () || !isVisible () || !isActive ()) return false;
946     if (isFocusControl ()) return true;
947     shell.setSavedFocus (null);
948     /*
949     * This code is intentionally commented.
950     *
951     * When setting focus to a control, it is
952     * possible that application code can set
953     * the focus to another control inside of
954     * WM_SETFOCUS. In this case, the original
955     * control will no longer have the focus
956     * and the call to setFocus() will return
957     * false indicating failure.
958     *
959     * We are still working on a solution at
960     * this time.
961     */

962 // if (OS.GetFocus () != OS.SetFocus (handle)) return false;
963
OS.SetFocus (handle);
964     if (isDisposed ()) return false;
965     shell.setSavedFocus (this);
966     return isFocusControl ();
967 }
968
969 void forceResize () {
970     if (parent == null) return;
971     WINDOWPOS [] lpwp = parent.lpwp;
972     if (lpwp == null) return;
973     for (int i=0; i<lpwp.length; i++) {
974         WINDOWPOS wp = lpwp [i];
975         if (wp != null && wp.hwnd == handle) {
976             /*
977             * This code is intentionally commented. All widgets that
978             * are created by SWT have WS_CLIPSIBLINGS to ensure that
979             * application code does not draw outside of the control.
980             */

981 // int count = parent.getChildrenCount ();
982
// if (count > 1) {
983
// int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
984
// if ((bits & OS.WS_CLIPSIBLINGS) == 0) wp.flags |= OS.SWP_NOCOPYBITS;
985
// }
986
SetWindowPos (wp.hwnd, 0, wp.x, wp.y, wp.cx, wp.cy, wp.flags);
987             lpwp [i] = null;
988             return;
989         }
990     }
991 }
992
993 /**
994  * Returns the accessible object for the receiver.
995  * If this is the first time this object is requested,
996  * then the object is created and returned.
997  *
998  * @return the accessible object
999  *
1000 * @exception SWTException <ul>
1001 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1002 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1003 * </ul>
1004 *
1005 * @see Accessible#addAccessibleListener
1006 * @see Accessible#addAccessibleControlListener
1007 *
1008 * @since 2.0
1009 */

1010public Accessible getAccessible () {
1011    checkWidget ();
1012    if (accessible == null) accessible = new_Accessible (this);
1013    return accessible;
1014}
1015
1016/**
1017 * Returns the receiver's background color.
1018 *
1019 * @return the background color
1020 *
1021 * @exception SWTException <ul>
1022 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1023 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1024 * </ul>
1025 */

1026public Color getBackground () {
1027    checkWidget ();
1028    Control control = findBackgroundControl ();
1029    if (control == null) control = this;
1030    return Color.win32_new (display, control.getBackgroundPixel ());
1031}
1032
1033/**
1034 * Returns the receiver's background image.
1035 *
1036 * @return the background image
1037 *
1038 * @exception SWTException <ul>
1039 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1040 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1041 * </ul>
1042 *
1043 * @since 3.2
1044 */

1045public Image getBackgroundImage () {
1046    checkWidget ();
1047    Control control = findBackgroundControl ();
1048    if (control == null) control = this;
1049    return control.backgroundImage;
1050}
1051
1052int getBackgroundPixel () {
1053    return background != -1 ? background : defaultBackground ();
1054}
1055
1056/**
1057 * Returns the receiver's border width.
1058 *
1059 * @return the border width
1060 *
1061 * @exception SWTException <ul>
1062 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1063 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1064 * </ul>
1065 */

1066public int getBorderWidth () {
1067    checkWidget ();
1068    int borderHandle = borderHandle ();
1069    int bits1 = OS.GetWindowLong (borderHandle, OS.GWL_EXSTYLE);
1070    if ((bits1 & OS.WS_EX_CLIENTEDGE) != 0) return OS.GetSystemMetrics (OS.SM_CXEDGE);
1071    if ((bits1 & OS.WS_EX_STATICEDGE) != 0) return OS.GetSystemMetrics (OS.SM_CXBORDER);
1072    int bits2 = OS.GetWindowLong (borderHandle, OS.GWL_STYLE);
1073    if ((bits2 & OS.WS_BORDER) != 0) return OS.GetSystemMetrics (OS.SM_CXBORDER);
1074    return 0;
1075}
1076
1077/**
1078 * Returns a rectangle describing the receiver's size and location
1079 * relative to its parent (or its display if its parent is null),
1080 * unless the receiver is a shell. In this case, the location is
1081 * relative to the display.
1082 *
1083 * @return the receiver's bounding rectangle
1084 *
1085 * @exception SWTException <ul>
1086 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1087 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1088 * </ul>
1089 */

1090public Rectangle getBounds () {
1091    checkWidget ();
1092    forceResize ();
1093    RECT rect = new RECT ();
1094    OS.GetWindowRect (topHandle (), rect);
1095    int hwndParent = parent == null ? 0 : parent.handle;
1096    OS.MapWindowPoints (0, hwndParent, rect, 2);
1097    int width = rect.right - rect.left;
1098    int height = rect.bottom - rect.top;
1099    return new Rectangle (rect.left, rect.top, width, height);
1100}
1101
1102int getCodePage () {
1103    if (OS.IsUnicode) return OS.CP_ACP;
1104    int hFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
1105    LOGFONT logFont = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA ();
1106    OS.GetObject (hFont, LOGFONT.sizeof, logFont);
1107    int cs = logFont.lfCharSet & 0xFF;
1108    int [] lpCs = new int [8];
1109    if (OS.TranslateCharsetInfo (cs, lpCs, OS.TCI_SRCCHARSET)) {
1110        return lpCs [1];
1111    }
1112    return OS.GetACP ();
1113}
1114
1115String JavaDoc getClipboardText () {
1116    String JavaDoc string = "";
1117    if (OS.OpenClipboard (0)) {
1118        int hMem = OS.GetClipboardData (OS.IsUnicode ? OS.CF_UNICODETEXT : OS.CF_TEXT);
1119        if (hMem != 0) {
1120            /* Ensure byteCount is a multiple of 2 bytes on UNICODE platforms */
1121            int byteCount = OS.GlobalSize (hMem) / TCHAR.sizeof * TCHAR.sizeof;
1122            int ptr = OS.GlobalLock (hMem);
1123            if (ptr != 0) {
1124                /* Use the character encoding for the default locale */
1125                TCHAR buffer = new TCHAR (0, byteCount / TCHAR.sizeof);
1126                OS.MoveMemory (buffer, ptr, byteCount);
1127                string = buffer.toString (0, buffer.strlen ());
1128                OS.GlobalUnlock (hMem);
1129            }
1130        }
1131        OS.CloseClipboard ();
1132    }
1133    return string;
1134}
1135
1136/**
1137 * Returns the receiver's cursor, or null if it has not been set.
1138 * <p>
1139 * When the mouse pointer passes over a control its appearance
1140 * is changed to match the control's cursor.
1141 * </p>
1142 * </ul>
1143 * @exception SWTException <ul>
1144 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1145 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1146 * </ul>
1147 *
1148 * @since 3.3
1149 */

1150public Cursor getCursor () {
1151    checkWidget ();
1152    return cursor;
1153}
1154
1155/**
1156 * Returns <code>true</code> if the receiver is detecting
1157 * drag gestures, and <code>false</code> otherwise.
1158 *
1159 * @return the receiver's drag detect state
1160 *
1161 * @exception SWTException <ul>
1162 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1163 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1164 * </ul>
1165 *
1166 * @since 3.3
1167 */

1168public boolean getDragDetect () {
1169    checkWidget ();
1170    return (state & DRAG_DETECT) != 0;
1171}
1172
1173/**
1174 * Returns <code>true</code> if the receiver is enabled, and
1175 * <code>false</code> otherwise. A disabled control is typically
1176 * not selectable from the user interface and draws with an
1177 * inactive or "grayed" look.
1178 *
1179 * @return the receiver's enabled state
1180 *
1181 * @exception SWTException <ul>
1182 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1183 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1184 * </ul>
1185 *
1186 * @see #isEnabled
1187 */

1188public boolean getEnabled () {
1189    checkWidget ();
1190    return OS.IsWindowEnabled (handle);
1191}
1192
1193/**
1194 * Returns the font that the receiver will use to paint textual information.
1195 *
1196 * @return the receiver's font
1197 *
1198 * @exception SWTException <ul>
1199 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1200 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1201 * </ul>
1202 */

1203public Font getFont () {
1204    checkWidget ();
1205    int hFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
1206    if (hFont == 0) hFont = defaultFont ();
1207    return Font.win32_new (display, hFont);
1208}
1209
1210/**
1211 * Returns the foreground color that the receiver will use to draw.
1212 *
1213 * @return the receiver's foreground color
1214 *
1215 * @exception SWTException <ul>
1216 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1217 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1218 * </ul>
1219 */

1220public Color getForeground () {
1221    checkWidget ();
1222    return Color.win32_new (display, getForegroundPixel ());
1223}
1224
1225int getForegroundPixel () {
1226    return foreground != -1 ? foreground : defaultForeground ();
1227}
1228
1229/**
1230 * Returns layout data which is associated with the receiver.
1231 *
1232 * @return the receiver's layout data
1233 *
1234 * @exception SWTException <ul>
1235 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1236 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1237 * </ul>
1238 */

1239public Object JavaDoc getLayoutData () {
1240    checkWidget ();
1241    return layoutData;
1242}
1243
1244/**
1245 * Returns a point describing the receiver's location relative
1246 * to its parent (or its display if its parent is null), unless
1247 * the receiver is a shell. In this case, the point is
1248 * relative to the display.
1249 *
1250 * @return the receiver's location
1251 *
1252 * @exception SWTException <ul>
1253 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1254 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1255 * </ul>
1256 */

1257public Point getLocation () {
1258    checkWidget ();
1259    forceResize ();
1260    RECT rect = new RECT ();
1261    OS.GetWindowRect (topHandle (), rect);
1262    int hwndParent = parent == null ? 0 : parent.handle;
1263    OS.MapWindowPoints (0, hwndParent, rect, 2);
1264    return new Point (rect.left, rect.top);
1265}
1266
1267/**
1268 * Returns the receiver's pop up menu if it has one, or null
1269 * if it does not. All controls may optionally have a pop up
1270 * menu that is displayed when the user requests one for
1271 * the control. The sequence of key strokes, button presses
1272 * and/or button releases that are used to request a pop up
1273 * menu is platform specific.
1274 *
1275 * @return the receiver's menu
1276 *
1277 * @exception SWTException <ul>
1278 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1279 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1280 * </ul>
1281 */

1282public Menu getMenu () {
1283    checkWidget ();
1284    return menu;
1285}
1286
1287/**
1288 * Returns the receiver's monitor.
1289 *
1290 * @return the receiver's monitor
1291 *
1292 * @since 3.0
1293 */

1294public Monitor getMonitor () {
1295    checkWidget ();
1296    if (OS.IsWinCE || OS.WIN32_VERSION < OS.VERSION (4, 10)) {
1297        return display.getPrimaryMonitor ();
1298    }
1299    int hmonitor = OS.MonitorFromWindow (handle, OS.MONITOR_DEFAULTTONEAREST);
1300    MONITORINFO lpmi = new MONITORINFO ();
1301    lpmi.cbSize = MONITORINFO.sizeof;
1302    OS.GetMonitorInfo (hmonitor, lpmi);
1303    Monitor monitor = new Monitor ();
1304    monitor.handle = hmonitor;
1305    monitor.x = lpmi.rcMonitor_left;
1306    monitor.y = lpmi.rcMonitor_top;
1307    monitor.width = lpmi.rcMonitor_right - lpmi.rcMonitor_left;
1308    monitor.height = lpmi.rcMonitor_bottom - lpmi.rcMonitor_top;
1309    monitor.clientX = lpmi.rcWork_left;
1310    monitor.clientY = lpmi.rcWork_top;
1311    monitor.clientWidth = lpmi.rcWork_right - lpmi.rcWork_left;
1312    monitor.clientHeight = lpmi.rcWork_bottom - lpmi.rcWork_top;
1313    return monitor;
1314}
1315
1316/**
1317 * Returns the receiver's parent, which must be a <code>Composite</code>
1318 * or null when the receiver is a shell that was created with null or
1319 * a display for a parent.
1320 *
1321 * @return the receiver's parent
1322 *
1323 * @exception SWTException <ul>
1324 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1325 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1326 * </ul>
1327 */

1328public Composite getParent () {
1329    checkWidget ();
1330    return parent;
1331}
1332
1333Control [] getPath () {
1334    int count = 0;
1335    Shell shell = getShell ();
1336    Control control = this;
1337    while (control != shell) {
1338        count++;
1339        control = control.parent;
1340    }
1341    control = this;
1342    Control [] result = new Control [count];
1343    while (control != shell) {
1344        result [--count] = control;
1345        control = control.parent;
1346    }
1347    return result;
1348}
1349
1350/**
1351 * Returns the receiver's shell. For all controls other than
1352 * shells, this simply returns the control's nearest ancestor
1353 * shell. Shells return themselves, even if they are children
1354 * of other shells.
1355 *
1356 * @return the receiver's shell
1357 *
1358 * @exception SWTException <ul>
1359 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1360 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1361 * </ul>
1362 *
1363 * @see #getParent
1364 */

1365public Shell getShell () {
1366    checkWidget ();
1367    return parent.getShell ();
1368}
1369
1370/**
1371 * Returns a point describing the receiver's size. The
1372 * x coordinate of the result is the width of the receiver.
1373 * The y coordinate of the result is the height of the
1374 * receiver.
1375 *
1376 * @return the receiver's size
1377 *
1378 * @exception SWTException <ul>
1379 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1380 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1381 * </ul>
1382 */

1383public Point getSize () {
1384    checkWidget ();
1385    forceResize ();
1386    RECT rect = new RECT ();
1387    OS.GetWindowRect (topHandle (), rect);
1388    int width = rect.right - rect.left;
1389    int height = rect.bottom - rect.top;
1390    return new Point (width, height);
1391}
1392
1393/**
1394 * Returns the receiver's tool tip text, or null if it has
1395 * not been set.
1396 *
1397 * @return the receiver's tool tip text
1398 *
1399 * @exception SWTException <ul>
1400 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1401 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1402 * </ul>
1403 */

1404public String JavaDoc getToolTipText () {
1405    checkWidget ();
1406    return toolTipText;
1407}
1408
1409/**
1410 * Returns <code>true</code> if the receiver is visible, and
1411 * <code>false</code> otherwise.
1412 * <p>
1413 * If one of the receiver's ancestors is not visible or some
1414 * other condition makes the receiver not visible, this method
1415 * may still indicate that it is considered visible even though
1416 * it may not actually be showing.
1417 * </p>
1418 *
1419 * @return the receiver's visibility state
1420 *
1421 * @exception SWTException <ul>
1422 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1423 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1424 * </ul>
1425 */

1426public boolean getVisible () {
1427    checkWidget ();
1428    if (drawCount != 0) return (state & HIDDEN) == 0;
1429    int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
1430    return (bits & OS.WS_VISIBLE) != 0;
1431}
1432
1433boolean hasCursor () {
1434    RECT rect = new RECT ();
1435    if (!OS.GetClientRect (handle, rect)) return false;
1436    OS.MapWindowPoints (handle, 0, rect, 2);
1437    POINT pt = new POINT ();
1438    return OS.GetCursorPos (pt) && OS.PtInRect (rect, pt);
1439}
1440
1441boolean hasFocus () {
1442    /*
1443    * If a non-SWT child of the control has focus,
1444    * then this control is considered to have focus
1445    * even though it does not have focus in Windows.
1446    */

1447    int hwndFocus = OS.GetFocus ();
1448    while (hwndFocus != 0) {
1449        if (hwndFocus == handle) return true;
1450        if (display.getControl (hwndFocus) != null) {
1451            return false;
1452        }
1453        hwndFocus = OS.GetParent (hwndFocus);
1454    }
1455    return false;
1456}
1457
1458/**
1459 * Invokes platform specific functionality to allocate a new GC handle.
1460 * <p>
1461 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
1462 * API for <code>Control</code>. It is marked public only so that it
1463 * can be shared within the packages provided by SWT. It is not
1464 * available on all platforms, and should never be called from
1465 * application code.
1466 * </p>
1467 *
1468 * @param data the platform specific GC data
1469 * @return the platform specific GC handle
1470 */

1471public int internal_new_GC (GCData data) {
1472    checkWidget();
1473    int hwnd = handle;
1474    if (data != null && data.hwnd != 0) hwnd = data.hwnd;
1475    if (data != null) data.hwnd = hwnd;
1476    int hDC = 0;
1477    if (data == null || data.ps == null) {
1478        hDC = OS.GetDC (hwnd);
1479    } else {
1480        hDC = OS.BeginPaint (hwnd, data.ps);
1481    }
1482    if (hDC == 0) SWT.error(SWT.ERROR_NO_HANDLES);
1483    if (data != null) {
1484        if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (4, 10)) {
1485            int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
1486            if ((data.style & mask) != 0) {
1487                data.layout = (data.style & SWT.RIGHT_TO_LEFT) != 0 ? OS.LAYOUT_RTL : 0;
1488            } else {
1489                int flags = OS.GetLayout (hDC);
1490                if ((flags & OS.LAYOUT_RTL) != 0) {
1491                    data.style |= SWT.RIGHT_TO_LEFT | SWT.MIRRORED;
1492                } else {
1493                    data.style |= SWT.LEFT_TO_RIGHT;
1494                }
1495            }
1496        } else {
1497            data.style |= SWT.LEFT_TO_RIGHT;
1498        }
1499        data.device = display;
1500        int foreground = getForegroundPixel ();
1501        if (foreground != OS.GetTextColor (hDC)) data.foreground = foreground;
1502        Control control = findBackgroundControl ();
1503        if (control == null) control = this;
1504        int background = control.getBackgroundPixel ();
1505        if (background != OS.GetBkColor (hDC)) data.background = background;
1506        data.hFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0);
1507        data.uiState = OS.SendMessage (hwnd, OS.WM_QUERYUISTATE, 0, 0);
1508    }
1509    return hDC;
1510}
1511
1512/**
1513 * Invokes platform specific functionality to dispose a GC handle.
1514 * <p>
1515 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
1516 * API for <code>Control</code>. It is marked public only so that it
1517 * can be shared within the packages provided by SWT. It is not
1518 * available on all platforms, and should never be called from
1519 * application code.
1520 * </p>
1521 *
1522 * @param hDC the platform specific GC handle
1523 * @param data the platform specific GC data
1524 */

1525public void internal_dispose_GC (int hDC, GCData data) {
1526    checkWidget ();
1527    int hwnd = handle;
1528    if (data != null && data.hwnd != 0) {
1529        hwnd = data.hwnd;
1530    }
1531    if (data == null || data.ps == null) {
1532        OS.ReleaseDC (hwnd, hDC);
1533    } else {
1534        OS.EndPaint (hwnd, data.ps);
1535    }
1536}
1537
1538boolean isActive () {
1539    Shell dialogShell = display.getModalDialogShell ();
1540    if (dialogShell != null && dialogShell != getShell ()) {
1541        return false;
1542    }
1543    Shell shell = null;
1544    Shell [] modalShells = display.modalShells;
1545    if (modalShells != null) {
1546        int bits = SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL;
1547        int index = modalShells.length;
1548        while (--index >= 0) {
1549            Shell modal = modalShells [index];
1550            if (modal != null) {
1551                if ((modal.style & bits) != 0) {
1552                    Control control = this;
1553                    while (control != null) {
1554                        if (control == modal) break;
1555                        control = control.parent;
1556                    }
1557                    if (control != modal) return false;
1558                    break;
1559                }
1560                if ((modal.style & SWT.PRIMARY_MODAL) != 0) {
1561                    if (shell == null) shell = getShell ();
1562                    if (modal.parent == shell) return false;
1563                }
1564            }
1565        }
1566    }
1567    if (shell == null) shell = getShell ();
1568    return shell.getEnabled ();
1569}
1570
1571/**
1572 * Returns <code>true</code> if the receiver is enabled and all
1573 * ancestors up to and including the receiver's nearest ancestor
1574 * shell are enabled. Otherwise, <code>false</code> is returned.
1575 * A disabled control is typically not selectable from the user
1576 * interface and draws with an inactive or "grayed" look.
1577 *
1578 * @return the receiver's enabled state
1579 *
1580 * @exception SWTException <ul>
1581 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1582 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1583 * </ul>
1584 *
1585 * @see #getEnabled
1586 */

1587public boolean isEnabled () {
1588    checkWidget ();
1589    return getEnabled () && parent.isEnabled ();
1590}
1591
1592/**
1593 * Returns <code>true</code> if the receiver has the user-interface
1594 * focus, and <code>false</code> otherwise.
1595 *
1596 * @return the receiver's focus state
1597 *
1598 * @exception SWTException <ul>
1599 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1600 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1601 * </ul>
1602 */

1603public boolean isFocusControl () {
1604    checkWidget ();
1605    Control focusControl = display.focusControl;
1606    if (focusControl != null && !focusControl.isDisposed ()) {
1607        return this == focusControl;
1608    }
1609    return hasFocus ();
1610}
1611
1612boolean isFocusAncestor (Control control) {
1613    while (control != null && control != this && !(control instanceof Shell)) {
1614        control = control.parent;
1615    }
1616    return control == this;
1617}
1618
1619/**
1620 * Returns <code>true</code> if the underlying operating
1621 * system supports this reparenting, otherwise <code>false</code>
1622 *
1623 * @return <code>true</code> if the widget can be reparented, otherwise <code>false</code>
1624 *
1625 * @exception SWTException <ul>
1626 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1627 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1628 * </ul>
1629 */

1630public boolean isReparentable () {
1631    checkWidget ();
1632    return true;
1633}
1634
1635boolean isShowing () {
1636    /*
1637    * This is not complete. Need to check if the
1638    * widget is obscured by a parent or sibling.
1639    */

1640    if (!isVisible ()) return false;
1641    Control control = this;
1642    while (control != null) {
1643        Point size = control.getSize ();
1644        if (size.x == 0 || size.y == 0) {
1645            return false;
1646        }
1647        control = control.parent;
1648    }
1649    return true;
1650    /*
1651    * Check to see if current damage is included.
1652    */

1653// if (!OS.IsWindowVisible (handle)) return false;
1654
// int flags = OS.DCX_CACHE | OS.DCX_CLIPCHILDREN | OS.DCX_CLIPSIBLINGS;
1655
// int hDC = OS.GetDCEx (handle, 0, flags);
1656
// int result = OS.GetClipBox (hDC, new RECT ());
1657
// OS.ReleaseDC (handle, hDC);
1658
// return result != OS.NULLREGION;
1659
}
1660
1661boolean isTabGroup () {
1662    Control [] tabList = parent._getTabList ();
1663    if (tabList != null) {
1664        for (int i=0; i<tabList.length; i++) {
1665            if (tabList [i] == this) return true;
1666        }
1667    }
1668    int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
1669    return (bits & OS.WS_TABSTOP) != 0;
1670}
1671
1672boolean isTabItem () {
1673    Control [] tabList = parent._getTabList ();
1674    if (tabList != null) {
1675        for (int i=0; i<tabList.length; i++) {
1676            if (tabList [i] == this) return false;
1677        }
1678    }
1679    int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
1680    if ((bits & OS.WS_TABSTOP) != 0) return false;
1681    int code = OS.SendMessage (handle, OS.WM_GETDLGCODE, 0, 0);
1682    if ((code & OS.DLGC_STATIC) != 0) return false;
1683    if ((code & OS.DLGC_WANTALLKEYS) != 0) return false;
1684    if ((code & OS.DLGC_WANTARROWS) != 0) return false;
1685    if ((code & OS.DLGC_WANTTAB) != 0) return false;
1686    return true;
1687}
1688
1689/**
1690 * Returns <code>true</code> if the receiver is visible and all
1691 * ancestors up to and including the receiver's nearest ancestor
1692 * shell are visible. Otherwise, <code>false</code> is returned.
1693 *
1694 * @return the receiver's visibility state
1695 *
1696 * @exception SWTException <ul>
1697 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1698 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1699 * </ul>
1700 *
1701 * @see #getVisible
1702 */

1703public boolean isVisible () {
1704    checkWidget ();
1705    if (OS.IsWindowVisible (handle)) return true;
1706    return getVisible () && parent.isVisible ();
1707}
1708
1709void mapEvent (int hwnd, Event event) {
1710    if (hwnd != handle) {
1711        POINT point = new POINT ();
1712        point.x = event.x;
1713        point.y = event.y;
1714        OS.MapWindowPoints (hwnd, handle, point, 1);
1715        event.x = point.x;
1716        event.y = point.y;
1717    }
1718}
1719
1720void markLayout (boolean changed, boolean all) {
1721    /* Do nothing */
1722}
1723
1724Decorations menuShell () {
1725    return parent.menuShell ();
1726}
1727
1728boolean mnemonicHit (char key) {
1729    return false;
1730}
1731
1732boolean mnemonicMatch (char key) {
1733    return false;
1734}
1735
1736/**
1737 * Moves the receiver above the specified control in the
1738 * drawing order. If the argument is null, then the receiver
1739 * is moved to the top of the drawing order. The control at
1740 * the top of the drawing order will not be covered by other
1741 * controls even if they occupy intersecting areas.
1742 *
1743 * @param control the sibling control (or null)
1744 *
1745 * @exception IllegalArgumentException <ul>
1746 * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
1747 * </ul>
1748 * @exception SWTException <ul>
1749 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1750 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1751 * </ul>
1752 *
1753 * @see Control#moveBelow
1754 * @see Composite#getChildren
1755 */

1756public void moveAbove (Control control) {
1757    checkWidget ();
1758    int topHandle = topHandle (), hwndAbove = OS.HWND_TOP;
1759    if (control != null) {
1760        if (control.isDisposed ()) error(SWT.ERROR_INVALID_ARGUMENT);
1761        if (parent != control.parent) return;
1762        int hwnd = control.topHandle ();
1763        if (hwnd == 0 || hwnd == topHandle) return;
1764        hwndAbove = OS.GetWindow (hwnd, OS.GW_HWNDPREV);
1765        /*
1766        * Bug in Windows. For some reason, when GetWindow ()
1767        * with GW_HWNDPREV is used to query the previous window
1768        * in the z-order with the first child, Windows returns
1769        * the first child instead of NULL. The fix is to detect
1770        * this case and move the control to the top.
1771        */

1772        if (hwndAbove == 0 || hwndAbove == hwnd) {
1773            hwndAbove = OS.HWND_TOP;
1774        }
1775    }
1776    int flags = OS.SWP_NOSIZE | OS.SWP_NOMOVE | OS.SWP_NOACTIVATE;
1777    SetWindowPos (topHandle, hwndAbove, 0, 0, 0, 0, flags);
1778}
1779
1780/**
1781 * Moves the receiver below the specified control in the
1782 * drawing order. If the argument is null, then the receiver
1783 * is moved to the bottom of the drawing order. The control at
1784 * the bottom of the drawing order will be covered by all other
1785 * controls which occupy intersecting areas.
1786 *
1787 * @param control the sibling control (or null)
1788 *
1789 * @exception IllegalArgumentException <ul>
1790 * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
1791 * </ul>
1792 * @exception SWTException <ul>
1793 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1794 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1795 * </ul>
1796 *
1797 * @see Control#moveAbove
1798 * @see Composite#getChildren
1799 */

1800public void moveBelow (Control control) {
1801    checkWidget ();
1802    int topHandle = topHandle (), hwndAbove = OS.HWND_BOTTOM;
1803    if (control != null) {
1804        if (control.isDisposed ()) error(SWT.ERROR_INVALID_ARGUMENT);
1805        if (parent != control.parent) return;
1806        hwndAbove = control.topHandle ();
1807    } else {
1808        /*
1809        * Bug in Windows. When SetWindowPos() is called
1810        * with HWND_BOTTOM on a dialog shell, the dialog
1811        * and the parent are moved to the bottom of the
1812        * desktop stack. The fix is to move the dialog
1813        * to the bottom of the dialog window stack by
1814        * moving behind the first dialog child.
1815        */

1816        Shell shell = getShell ();
1817        if (this == shell && parent != null) {
1818            /*
1819            * Bug in Windows. For some reason, when GetWindow ()
1820            * with GW_HWNDPREV is used to query the previous window
1821            * in the z-order with the first child, Windows returns
1822            * the first child instead of NULL. The fix is to detect
1823            * this case and do nothing because the control is already
1824            * at the bottom.
1825            */

1826            int hwndParent = parent.handle, hwnd = hwndParent;
1827            hwndAbove = OS.GetWindow (hwnd, OS.GW_HWNDPREV);
1828            while (hwndAbove != 0 && hwndAbove != hwnd) {
1829                if (OS.GetWindow (hwndAbove, OS.GW_OWNER) == hwndParent) break;
1830                hwndAbove = OS.GetWindow (hwnd = hwndAbove, OS.GW_HWNDPREV);
1831            }
1832            if (hwndAbove == hwnd) return;
1833        }
1834    }
1835    if (hwndAbove == 0 || hwndAbove == topHandle) return;
1836    int flags = OS.SWP_NOSIZE | OS.SWP_NOMOVE | OS.SWP_NOACTIVATE;
1837    SetWindowPos (topHandle, hwndAbove, 0, 0, 0, 0, flags);
1838}
1839
1840Accessible new_Accessible (Control control) {
1841    return Accessible.internal_new_Accessible (this);
1842}
1843
1844GC new_GC (GCData data) {
1845    return GC.win32_new (this, data);
1846}
1847
1848/**
1849 * Causes the receiver to be resized to its preferred size.
1850 * For a composite, this involves computing the preferred size
1851 * from its layout, if there is one.
1852 *
1853 * @exception SWTException <ul>
1854 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1855 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1856 * </ul>
1857 *
1858 * @see #computeSize(int, int, boolean)
1859 */

1860public void pack () {
1861    checkWidget ();
1862    pack (true);
1863}
1864
1865/**
1866 * Causes the receiver to be resized to its preferred size.
1867 * For a composite, this involves computing the preferred size
1868 * from its layout, if there is one.
1869 * <p>
1870 * If the changed flag is <code>true</code>, it indicates that the receiver's
1871 * <em>contents</em> have changed, therefore any caches that a layout manager
1872 * containing the control may have been keeping need to be flushed. When the
1873 * control is resized, the changed flag will be <code>false</code>, so layout
1874 * manager caches can be retained.
1875 * </p>
1876 *
1877 * @param changed whether or not the receiver's contents have changed
1878 *
1879 * @exception SWTException <ul>
1880 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1881 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1882 * </ul>
1883 *
1884 * @see #computeSize(int, int, boolean)
1885 */

1886public void pack (boolean changed) {
1887    checkWidget ();
1888    setSize (computeSize (SWT.DEFAULT, SWT.DEFAULT, changed));
1889}
1890
1891/**
1892 * Causes the entire bounds of the receiver to be marked
1893 * as needing to be redrawn. The next time a paint request
1894 * is processed, the control will be completely painted,
1895 * including the background.
1896 *
1897 * @exception SWTException <ul>
1898 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1899 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1900 * </ul>
1901 *
1902 * @see #update()
1903 * @see PaintListener
1904 * @see SWT#Paint
1905 * @see SWT#NO_BACKGROUND
1906 * @see SWT#NO_REDRAW_RESIZE
1907 * @see SWT#NO_MERGE_PAINTS
1908 * @see SWT#DOUBLE_BUFFERED
1909 */

1910public void redraw () {
1911    checkWidget ();
1912    redraw (false);
1913}
1914
1915void redraw (boolean all) {
1916// checkWidget ();
1917
if (!OS.IsWindowVisible (handle)) return;
1918    if (OS.IsWinCE) {
1919        OS.InvalidateRect (handle, null, true);
1920    } else {
1921        int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
1922        if (all) flags |= OS.RDW_ALLCHILDREN;
1923        OS.RedrawWindow (handle, null, 0, flags);
1924    }
1925}
1926/**
1927 * Causes the rectangular area of the receiver specified by
1928 * the arguments to be marked as needing to be redrawn.
1929 * The next time a paint request is processed, that area of
1930 * the receiver will be painted, including the background.
1931 * If the <code>all</code> flag is <code>true</code>, any
1932 * children of the receiver which intersect with the specified
1933 * area will also paint their intersecting areas. If the
1934 * <code>all</code> flag is <code>false</code>, the children
1935 * will not be painted.
1936 *
1937 * @param x the x coordinate of the area to draw
1938 * @param y the y coordinate of the area to draw
1939 * @param width the width of the area to draw
1940 * @param height the height of the area to draw
1941 * @param all <code>true</code> if children should redraw, and <code>false</code> otherwise
1942 *
1943 * @exception SWTException <ul>
1944 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1945 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1946 * </ul>
1947 *
1948 * @see #update()
1949 * @see PaintListener
1950 * @see SWT#Paint
1951 * @see SWT#NO_BACKGROUND
1952 * @see SWT#NO_REDRAW_RESIZE
1953 * @see SWT#NO_MERGE_PAINTS
1954 * @see SWT#DOUBLE_BUFFERED
1955 */

1956public void redraw (int x, int y, int width, int height, boolean all) {
1957    checkWidget ();
1958    if (width <= 0 || height <= 0) return;
1959    if (!OS.IsWindowVisible (handle)) return;
1960    RECT rect = new RECT ();
1961    OS.SetRect (rect, x, y, x + width, y + height);
1962    if (OS.IsWinCE) {
1963        OS.InvalidateRect (handle, rect, true);
1964    } else {
1965        int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
1966        if (all) flags |= OS.RDW_ALLCHILDREN;
1967        OS.RedrawWindow (handle, rect, 0, flags);
1968    }
1969}
1970
1971boolean redrawChildren () {
1972    if (!OS.IsWindowVisible (handle)) return false;
1973    Control control = findBackgroundControl ();
1974    if (control == null) {
1975        if ((state & THEME_BACKGROUND) != 0) {
1976            if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
1977                OS.InvalidateRect (handle, null, true);
1978                return true;
1979            }
1980        }
1981    } else {
1982        if (control.backgroundImage != null) {
1983            OS.InvalidateRect (handle, null, true);
1984            return true;
1985        }
1986    }
1987    return false;
1988}
1989
1990void register () {
1991    display.addControl (handle, this);
1992}
1993
1994void releaseHandle () {
1995    super.releaseHandle ();
1996    handle = 0;
1997    parent = null;
1998}
1999
2000void releaseParent () {
2001    parent.removeControl (this);
2002}
2003
2004void releaseWidget () {
2005    super.releaseWidget ();
2006    if (OS.IsDBLocale) {
2007        OS.ImmAssociateContext (handle, 0);
2008    }
2009    if (toolTipText != null) {
2010        setToolTipText (getShell (), null);
2011    }
2012    toolTipText = null;
2013    if (menu != null && !menu.isDisposed ()) {
2014        menu.dispose ();
2015    }
2016    backgroundImage = null;
2017    menu = null;
2018    cursor = null;
2019    unsubclass ();
2020    deregister ();
2021    layoutData = null;
2022    if (accessible != null) {
2023        accessible.internal_dispose_Accessible ();
2024    }
2025    accessible = null;
2026}
2027
2028/**
2029 * Removes the listener from the collection of listeners who will
2030 * be notified when the control is moved or resized.
2031 *
2032 * @param listener the listener which should no longer be notified
2033 *
2034 * @exception IllegalArgumentException <ul>
2035 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2036 * </ul>
2037 * @exception SWTException <ul>
2038 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2039 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2040 * </ul>
2041 *
2042 * @see ControlListener
2043 * @see #addControlListener
2044 */

2045public void removeControlListener (ControlListener listener) {
2046    checkWidget ();
2047    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2048    if (eventTable == null) return;
2049    eventTable.unhook (SWT.Move, listener);
2050    eventTable.unhook (SWT.Resize, listener);
2051}
2052
2053/**
2054 * Removes the listener from the collection of listeners who will
2055 * be notified when a drag gesture occurs.
2056 *
2057 * @param listener the listener which should no longer be notified
2058 *
2059 * @exception IllegalArgumentException <ul>
2060 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2061 * </ul>
2062 * @exception SWTException <ul>
2063 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2064 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2065 * </ul>
2066 *
2067 * @see DragDetectListener
2068 * @see #addDragDetectListener
2069 *
2070 * @since 3.3
2071 */

2072public void removeDragDetectListener(DragDetectListener listener) {
2073    checkWidget ();
2074    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2075    if (eventTable == null) return;
2076    eventTable.unhook (SWT.DragDetect, listener);
2077}
2078
2079/**
2080 * Removes the listener from the collection of listeners who will
2081 * be notified when the control gains or loses focus.
2082 *
2083 * @param listener the listener which should no longer be notified
2084 *
2085 * @exception IllegalArgumentException <ul>
2086 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2087 * </ul>
2088 * @exception SWTException <ul>
2089 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2090 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2091 * </ul>
2092 *
2093 * @see FocusListener
2094 * @see #addFocusListener
2095 */

2096public void removeFocusListener(FocusListener listener) {
2097    checkWidget ();
2098    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2099    if (eventTable == null) return;
2100    eventTable.unhook (SWT.FocusIn, listener);
2101    eventTable.unhook (SWT.FocusOut, listener);
2102}
2103
2104/**
2105 * Removes the listener from the collection of listeners who will
2106 * be notified when the help events are generated for the control.
2107 *
2108 * @param listener the listener which should no longer be notified
2109 *
2110 * @exception IllegalArgumentException <ul>
2111 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2112 * </ul>
2113 * @exception SWTException <ul>
2114 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2115 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2116 * </ul>
2117 *
2118 * @see HelpListener
2119 * @see #addHelpListener
2120 */

2121public void removeHelpListener (HelpListener listener) {
2122    checkWidget ();
2123    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2124    if (eventTable == null) return;
2125    eventTable.unhook (SWT.Help, listener);
2126}
2127
2128/**
2129 * Removes the listener from the collection of listeners who will
2130 * be notified when keys are pressed and released on the system keyboard.
2131 *
2132 * @param listener the listener which should no longer be notified
2133 *
2134 * @exception IllegalArgumentException <ul>
2135 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2136 * </ul>
2137 * @exception SWTException <ul>
2138 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2139 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2140 * </ul>
2141 *
2142 * @see KeyListener
2143 * @see #addKeyListener
2144 */

2145public void removeKeyListener(KeyListener listener) {
2146    checkWidget ();
2147    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2148    if (eventTable == null) return;
2149    eventTable.unhook (SWT.KeyUp, listener);
2150    eventTable.unhook (SWT.KeyDown, listener);
2151}
2152
2153/**
2154 * Removes the listener from the collection of listeners who will
2155 * be notified when the platform-specific context menu trigger has
2156 * occurred.
2157 *
2158 * @param listener the listener which should no longer be notified
2159 *
2160 * @exception IllegalArgumentException <ul>
2161 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2162 * </ul>
2163 * @exception SWTException <ul>
2164 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2165 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2166 * </ul>
2167 *
2168 * @see MenuDetectListener
2169 * @see #addMenuDetectListener
2170 *
2171 * @since 3.3
2172 */

2173public void removeMenuDetectListener (MenuDetectListener listener) {
2174    checkWidget ();
2175    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2176    if (eventTable == null) return;
2177    eventTable.unhook (SWT.MenuDetect, listener);
2178}
2179
2180/**
2181 * Removes the listener from the collection of listeners who will
2182 * be notified when the mouse passes or hovers over controls.
2183 *
2184 * @param listener the listener which should no longer be notified
2185 *
2186 * @exception IllegalArgumentException <ul>
2187 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2188 * </ul>
2189 * @exception SWTException <ul>
2190 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2191 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2192 * </ul>
2193 *
2194 * @see MouseTrackListener
2195 * @see #addMouseTrackListener
2196 */

2197public void removeMouseTrackListener(MouseTrackListener listener) {
2198    checkWidget ();
2199    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2200    if (eventTable == null) return;
2201    eventTable.unhook (SWT.MouseEnter, listener);
2202    eventTable.unhook (SWT.MouseExit, listener);
2203    eventTable.unhook (SWT.MouseHover, listener);
2204}
2205
2206/**
2207 * Removes the listener from the collection of listeners who will
2208 * be notified when mouse buttons are pressed and released.
2209 *
2210 * @param listener the listener which should no longer be notified
2211 *
2212 * @exception IllegalArgumentException <ul>
2213 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2214 * </ul>
2215 * @exception SWTException <ul>
2216 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2217 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2218 * </ul>
2219 *
2220 * @see MouseListener
2221 * @see #addMouseListener
2222 */

2223public void removeMouseListener (MouseListener listener) {
2224    checkWidget ();
2225    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2226    if (eventTable == null) return;
2227    eventTable.unhook (SWT.MouseDown, listener);
2228    eventTable.unhook (SWT.MouseUp, listener);
2229    eventTable.unhook (SWT.MouseDoubleClick, listener);
2230}
2231
2232/**
2233 * Removes the listener from the collection of listeners who will
2234 * be notified when the mouse moves.
2235 *
2236 * @param listener the listener which should no longer be notified
2237 *
2238 * @exception IllegalArgumentException <ul>
2239 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2240 * </ul>
2241 * @exception SWTException <ul>
2242 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2243 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2244 * </ul>
2245 *
2246 * @see MouseMoveListener
2247 * @see #addMouseMoveListener
2248 */

2249public void removeMouseMoveListener(MouseMoveListener listener) {
2250    checkWidget ();
2251    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2252    if (eventTable == null) return;
2253    eventTable.unhook (SWT.MouseMove, listener);
2254}
2255
2256/**
2257 * Removes the listener from the collection of listeners who will
2258 * be notified when the mouse wheel is scrolled.
2259 *
2260 * @param listener the listener which should no longer be notified
2261 *
2262 * @exception IllegalArgumentException <ul>
2263 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2264 * </ul>
2265 * @exception SWTException <ul>
2266 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2267 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2268 * </ul>
2269 *
2270 * @see MouseWheelListener
2271 * @see #addMouseWheelListener
2272 *
2273 * @since 3.3
2274 */

2275public void removeMouseWheelListener (MouseWheelListener listener) {
2276    checkWidget ();
2277    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2278    if (eventTable == null) return;
2279    eventTable.unhook (SWT.MouseWheel, listener);
2280}
2281
2282/**
2283 * Removes the listener from the collection of listeners who will
2284 * be notified when the receiver needs to be painted.
2285 *
2286 * @param listener the listener which should no longer be notified
2287 *
2288 * @exception IllegalArgumentException <ul>
2289 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2290 * </ul>
2291 * @exception SWTException <ul>
2292 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2293 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2294 * </ul>
2295 *
2296 * @see PaintListener
2297 * @see #addPaintListener
2298 */

2299public void removePaintListener(PaintListener listener) {
2300    checkWidget ();
2301    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2302    if (eventTable == null) return;
2303    eventTable.unhook(SWT.Paint, listener);
2304}
2305
2306/**
2307 * Removes the listener from the collection of listeners who will
2308 * be notified when traversal events occur.
2309 *
2310 * @param listener the listener which should no longer be notified
2311 *
2312 * @exception IllegalArgumentException <ul>
2313 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
2314 * </ul>
2315 * @exception SWTException <ul>
2316 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2317 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2318 * </ul>
2319 *
2320 * @see TraverseListener
2321 * @see #addTraverseListener
2322 */

2323public void removeTraverseListener(TraverseListener listener) {
2324    checkWidget ();
2325    if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
2326    if (eventTable == null) return;
2327    eventTable.unhook (SWT.Traverse, listener);
2328}
2329
2330void showWidget (boolean visible) {
2331    int topHandle = topHandle ();
2332    OS.ShowWindow (topHandle, visible ? OS.SW_SHOW : OS.SW_HIDE);
2333    if (handle != topHandle) OS.ShowWindow (handle, visible ? OS.SW_SHOW : OS.SW_HIDE);
2334}
2335
2336boolean sendFocusEvent (int type) {
2337    Shell shell = getShell ();
2338    
2339    /*
2340    * Feature in Windows. During the processing of WM_KILLFOCUS,
2341    * when the focus window is queried using GetFocus(), it has
2342    * already been assigned to the new window. The fix is to
2343    * remember the control that is losing or gaining focus and
2344    * answer it during WM_KILLFOCUS. If a WM_SETFOCUS occurs
2345    * during WM_KILLFOCUS, the focus control needs to be updated
2346    * to the current control. At any other time, the focus
2347    * control matches Windows.
2348    */

2349    Display display = this.display;
2350    display.focusEvent = type;
2351    display.focusControl = this;
2352    sendEvent (type);
2353    // widget could be disposed at this point
2354
display.focusEvent = SWT.None;
2355    display.focusControl = null;
2356
2357    /*
2358    * It is possible that the shell may be
2359    * disposed at this point. If this happens
2360    * don't send the activate and deactivate
2361    * events.
2362    */

2363    if (!shell.isDisposed ()) {
2364        switch (type) {
2365            case SWT.FocusIn:
2366                shell.setActiveControl (this);
2367                break;
2368            case SWT.FocusOut:
2369                if (shell != display.getActiveShell ()) {
2370                    shell.setActiveControl (null);
2371                }
2372                break;
2373        }
2374    }
2375    return true;
2376}
2377
2378void setBackground () {
2379    Control control = findBackgroundControl ();
2380    if (control == null) control = this;
2381    if (control.backgroundImage != null) {
2382        Shell shell = getShell ();
2383        shell.releaseBrushes ();
2384        setBackgroundImage (control.backgroundImage.handle);
2385    } else {
2386        setBackgroundPixel (control.background == -1 ? control.defaultBackground() : control.background);
2387    }
2388}
2389
2390/**
2391 * Sets the receiver's background color to the color specified
2392 * by the argument, or to the default system color for the control
2393 * if the argument is null.
2394 * <p>
2395 * Note: This operation is a hint and may be overridden by the platform.
2396 * For example, on Windows the background of a Button cannot be changed.
2397 * </p>
2398 * @param color the new color (or null)
2399 *
2400 * @exception IllegalArgumentException <ul>
2401 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
2402 * </ul>
2403 * @exception SWTException <ul>
2404 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2405 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2406 * </ul>
2407 */

2408public void setBackground (Color color) {
2409    checkWidget ();
2410    int pixel = -1;
2411    if (color != null) {
2412        if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2413        pixel = color.handle;
2414    }
2415    if (pixel == background) return;
2416    background = pixel;
2417    updateBackgroundColor ();
2418}
2419
2420/**
2421 * Sets the receiver's background image to the image specified
2422 * by the argument, or to the default system color for the control
2423 * if the argument is null. The background image is tiled to fill
2424 * the available space.
2425 * <p>
2426 * Note: This operation is a hint and may be overridden by the platform.
2427 * For example, on Windows the background of a Button cannot be changed.
2428 * </p>
2429 * @param image the new image (or null)
2430 *
2431 * @exception IllegalArgumentException <ul>
2432 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
2433 * <li>ERROR_INVALID_ARGUMENT - if the argument is not a bitmap</li>
2434 * </ul>
2435 * @exception SWTException <ul>
2436 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2437 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2438 * </ul>
2439 *
2440 * @since 3.2
2441 */

2442public void setBackgroundImage (Image image) {
2443    checkWidget ();
2444    if (image != null) {
2445        if (image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
2446        if (image.type != SWT.BITMAP) error (SWT.ERROR_INVALID_ARGUMENT);
2447    }
2448    if (backgroundImage == image) return;
2449    backgroundImage = image;
2450    Shell shell = getShell ();
2451    shell.releaseBrushes ();
2452    updateBackgroundImage ();
2453}
2454
2455void setBackgroundImage (int hBitmap) {
2456    if (OS.IsWinCE) {
2457        OS.InvalidateRect (handle, null, true);
2458    } else {
2459        int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
2460        OS.RedrawWindow (handle, null, 0, flags);
2461    }
2462}
2463
2464void setBackgroundPixel (int pixel) {
2465    if (OS.IsWinCE) {
2466        OS.InvalidateRect (handle, null, true);
2467    } else {
2468        int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
2469        OS.RedrawWindow (handle, null, 0, flags);
2470    }
2471}
2472
2473/**
2474 * Sets the receiver's size and location to the rectangular
2475 * area specified by the arguments. The <code>x</code> and
2476 * <code>y</code> arguments are relative to the receiver's
2477 * parent (or its display if its parent is null), unless
2478 * the receiver is a shell. In this case, the <code>x</code>
2479 * and <code>y</code> arguments are relative to the display.
2480 * <p>
2481 * Note: Attempting to set the width or height of the
2482 * receiver to a negative number will cause that
2483 * value to be set to zero instead.
2484 * </p>
2485 *
2486 * @param x the new x coordinate for the receiver
2487 * @param y the new y coordinate for the receiver
2488 * @param width the new width for the receiver
2489 * @param height the new height for the receiver
2490 *
2491 * @exception SWTException <ul>
2492 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2493 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2494 * </ul>
2495 */

2496public void setBounds (int x, int y, int width, int height) {
2497    checkWidget ();
2498    int flags = OS.SWP_NOZORDER | OS.SWP_DRAWFRAME | OS.SWP_NOACTIVATE;
2499    setBounds (x, y, Math.max (0, width), Math.max (0, height), flags);
2500}
2501
2502void setBounds (int x, int y, int width, int height, int flags) {
2503    setBounds (x, y, width, height, flags, true);
2504}
2505
2506void setBounds (int x, int y, int width, int height, int flags, boolean defer) {
2507    int topHandle = topHandle ();
2508    if (defer && parent != null) {
2509        forceResize ();
2510        if (OS.GetWindow (handle, OS.GW_CHILD) == 0) {
2511            if (findImageControl () != null) {
2512                flags |= OS.SWP_NOCOPYBITS;
2513            } else {
2514                if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
2515                    if (findThemeControl () != null) flags |= OS.SWP_NOCOPYBITS;
2516                }
2517            }
2518        }
2519        WINDOWPOS [] lpwp = parent.lpwp;
2520        if (lpwp == null) {
2521            SetWindowPos (topHandle, 0, x, y, width, height, flags);
2522        } else {
2523            int index = 0;
2524            while (index < lpwp.length) {
2525                if (lpwp [index] == null) break;
2526                index ++;
2527            }
2528            if (index == lpwp.length) {
2529                WINDOWPOS [] newLpwp = new WINDOWPOS [lpwp.length + 4];
2530                System.arraycopy (lpwp, 0, newLpwp, 0, lpwp.length);
2531                parent.lpwp = lpwp = newLpwp;
2532            }
2533            WINDOWPOS wp = new WINDOWPOS ();
2534            wp.hwnd = topHandle;
2535            wp.x = x;
2536            wp.y = y;
2537            wp.cx = width;
2538            wp.cy = height;
2539            wp.flags = flags;
2540            lpwp [index] = wp;
2541        }
2542    } else {
2543        SetWindowPos (topHandle, 0, x, y, width, height, flags);
2544    }
2545}
2546
2547/**
2548 * Sets the receiver's size and location to the rectangular
2549 * area specified by the argument. The <code>x</code> and
2550 * <code>y</code> fields of the rectangle are relative to
2551 * the receiver's parent (or its display if its parent is null).
2552 * <p>
2553 * Note: Attempting to set the width or height of the
2554 * receiver to a negative number will cause that
2555 * value to be set to zero instead.
2556 * </p>
2557 *
2558 * @param rect the new bounds for the receiver
2559 *
2560 * @exception SWTException <ul>
2561 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2562 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2563 * </ul>
2564 */

2565public void setBounds (Rectangle rect) {
2566    checkWidget ();
2567    if (rect == null) error (SWT.ERROR_NULL_ARGUMENT);
2568    setBounds (rect.x, rect.y, rect.width, rect.height);
2569}
2570
2571/**
2572 * If the argument is <code>true</code>, causes the receiver to have
2573 * all mouse events delivered to it until the method is called with
2574 * <code>false</code> as the argument.
2575 *
2576 * @param capture <code>true</code> to capture the mouse, and <code>false</code> to release it
2577 *
2578 * @exception SWTException <ul>
2579 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2580 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2581 * </ul>
2582 */

2583public void setCapture (boolean capture) {
2584    checkWidget ();
2585    if (capture) {
2586        OS.SetCapture (handle);
2587    } else {
2588        if (OS.GetCapture () == handle) {
2589            OS.ReleaseCapture ();
2590        }
2591    }
2592}
2593
2594void setCursor () {
2595    int lParam = OS.HTCLIENT | (OS.WM_MOUSEMOVE << 16);
2596    OS.SendMessage (handle, OS.WM_SETCURSOR, handle, lParam);
2597}
2598
2599/**
2600 * Sets the receiver's cursor to the cursor specified by the
2601 * argument, or to the default cursor for that kind of control
2602 * if the argument is null.
2603 * <p>
2604 * When the mouse pointer passes over a control its appearance
2605 * is changed to match the control's cursor.
2606 * </p>
2607 *
2608 * @param cursor the new cursor (or null)
2609 *
2610 * @exception IllegalArgumentException <ul>
2611 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
2612 * </ul>
2613 * @exception SWTException <ul>
2614 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2615 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2616 * </ul>
2617 */

2618public void setCursor (Cursor cursor) {
2619    checkWidget ();
2620    if (cursor != null && cursor.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2621    this.cursor = cursor;
2622    if (OS.IsWinCE) {
2623        int hCursor = cursor != null ? cursor.handle : 0;
2624        OS.SetCursor (hCursor);
2625        return;
2626    }
2627    int hwndCursor = OS.GetCapture ();
2628    if (hwndCursor == 0) {
2629        POINT pt = new POINT ();
2630        if (!OS.GetCursorPos (pt)) return;
2631        int hwnd = hwndCursor = OS.WindowFromPoint (pt);
2632        while (hwnd != 0 && hwnd != handle) {
2633            hwnd = OS.GetParent (hwnd);
2634        }
2635        if (hwnd == 0) return;
2636    }
2637    Control control = display.getControl (hwndCursor);
2638    if (control == null) control = this;
2639    control.setCursor ();
2640}
2641
2642void setDefaultFont () {
2643    int hFont = display.getSystemFont ().handle;
2644    OS.SendMessage (handle, OS.WM_SETFONT, hFont, 0);
2645}
2646
2647/**
2648 * Sets the receiver's drag detect state. If the argument is
2649 * <code>true</code>, the receiver will detect drag gestures,
2650 * otherwise these gestures will be ignored.
2651 *
2652 * @param dragDetect the new drag detect state
2653 *
2654 * @exception SWTException <ul>
2655 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2656 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2657 * </ul>
2658 *
2659 * @since 3.3
2660 */

2661public void setDragDetect (boolean dragDetect) {
2662    checkWidget ();
2663    if (dragDetect) {
2664        state |= DRAG_DETECT;
2665    } else {
2666        state &= ~DRAG_DETECT;
2667    }
2668    enableDrag (dragDetect);
2669}
2670
2671/**
2672 * Enables the receiver if the argument is <code>true</code>,
2673 * and disables it otherwise. A disabled control is typically
2674 * not selectable from the user interface and draws with an
2675 * inactive or "grayed" look.
2676 *
2677 * @param enabled the new enabled state
2678 *
2679 * @exception SWTException <ul>
2680 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2681 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2682 * </ul>
2683 */

2684public void setEnabled (boolean enabled) {
2685    checkWidget ();
2686    /*
2687    * Feature in Windows. If the receiver has focus, disabling
2688    * the receiver causes no window to have focus. The fix is
2689    * to assign focus to the first ancestor window that takes
2690    * focus. If no window will take focus, set focus to the
2691    * desktop.
2692    */

2693    Control control = null;
2694    boolean fixFocus = false;
2695    if (!enabled) {
2696        if (display.focusEvent != SWT.FocusOut) {
2697            control = display.getFocusControl ();
2698            fixFocus = isFocusAncestor (control);
2699        }
2700    }
2701    enableWidget (enabled);
2702    if (fixFocus) fixFocus (control);
2703}
2704
2705boolean setFixedFocus () {
2706    if ((style & SWT.NO_FOCUS) != 0) return false;
2707    return forceFocus ();
2708}
2709
2710/**
2711 * Causes the receiver to have the <em>keyboard focus</em>,
2712 * such that all keyboard events will be delivered to it. Focus
2713 * reassignment will respect applicable platform constraints.
2714 *
2715 * @return <code>true</code> if the control got focus, and <code>false</code> if it was unable to.
2716 *
2717 * @exception SWTException <ul>
2718 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2719 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2720 * </ul>
2721 *
2722 * @see #forceFocus
2723 */

2724public boolean setFocus () {
2725    checkWidget ();
2726    if ((style & SWT.NO_FOCUS) != 0) return false;
2727    return forceFocus ();
2728}
2729
2730/**
2731 * Sets the font that the receiver will use to paint textual information
2732 * to the font specified by the argument, or to the default font for that
2733 * kind of control if the argument is null.
2734 *
2735 * @param font the new font (or null)
2736 *
2737 * @exception IllegalArgumentException <ul>
2738 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
2739 * </ul>
2740 * @exception SWTException <ul>
2741 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2742 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2743 * </ul>
2744 */

2745public void setFont (Font font) {
2746    checkWidget ();
2747    int hFont = 0;
2748    if (font != null) {
2749        if (font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2750        hFont = font.handle;
2751    }
2752    if (hFont == 0) hFont = defaultFont ();
2753    OS.SendMessage (handle, OS.WM_SETFONT, hFont, 1);
2754}
2755
2756/**
2757 * Sets the receiver's foreground color to the color specified
2758 * by the argument, or to the default system color for the control
2759 * if the argument is null.
2760 * <p>
2761 * Note: This operation is a hint and may be overridden by the platform.
2762 * </p>
2763 * @param color the new color (or null)
2764 *
2765 * @exception IllegalArgumentException <ul>
2766 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
2767 * </ul>
2768 * @exception SWTException <ul>
2769 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2770 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2771 * </ul>
2772 */

2773public void setForeground (Color color) {
2774    checkWidget ();
2775    int pixel = -1;
2776    if (color != null) {
2777        if (color.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2778        pixel = color.handle;
2779    }
2780    if (pixel == foreground) return;
2781    foreground = pixel;
2782    setForegroundPixel (pixel);
2783}
2784
2785void setForegroundPixel (int pixel) {
2786    OS.InvalidateRect (handle, null, true);
2787}
2788
2789/**
2790 * Sets the layout data associated with the receiver to the argument.
2791 *
2792 * @param layoutData the new layout data for the receiver.
2793 *
2794 * @exception SWTException <ul>
2795 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2796 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2797 * </ul>
2798 */

2799public void setLayoutData (Object JavaDoc layoutData) {
2800    checkWidget ();
2801    this.layoutData = layoutData;
2802}
2803
2804/**
2805 * Sets the receiver's location to the point specified by
2806 * the arguments which are relative to the receiver's
2807 * parent (or its display if its parent is null), unless
2808 * the receiver is a shell. In this case, the point is
2809 * relative to the display.
2810 *
2811 * @param x the new x coordinate for the receiver
2812 * @param y the new y coordinate for the receiver
2813 *
2814 * @exception SWTException <ul>
2815 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2816 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2817 * </ul>
2818 */

2819public void setLocation (int x, int y) {
2820    checkWidget ();
2821    int flags = OS.SWP_NOSIZE | OS.SWP_NOZORDER | OS.SWP_NOACTIVATE;
2822    /*
2823    * Feature in WinCE. The SWP_DRAWFRAME flag for SetWindowPos()
2824    * causes a WM_SIZE message to be sent even when the SWP_NOSIZE
2825    * flag is specified. The fix is to set SWP_DRAWFRAME only when
2826    * not running on WinCE.
2827    */

2828    if (!OS.IsWinCE) flags |= OS.SWP_DRAWFRAME;
2829    setBounds (x, y, 0, 0, flags);
2830}
2831
2832/**
2833 * Sets the receiver's location to the point specified by
2834 * the arguments which are relative to the receiver's
2835 * parent (or its display if its parent is null), unless
2836 * the receiver is a shell. In this case, the point is
2837 * relative to the display.
2838 *
2839 * @param location the new location for the receiver
2840 *
2841 * @exception SWTException <ul>
2842 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2843 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2844 * </ul>
2845 */

2846public void setLocation (Point location) {
2847    checkWidget ();
2848    if (location == null) error (SWT.ERROR_NULL_ARGUMENT);
2849    setLocation (location.x, location.y);
2850}
2851
2852/**
2853 * Sets the receiver's pop up menu to the argument.
2854 * All controls may optionally have a pop up
2855 * menu that is displayed when the user requests one for
2856 * the control. The sequence of key strokes, button presses
2857 * and/or button releases that are used to request a pop up
2858 * menu is platform specific.
2859 * <p>
2860 * Note: Disposing of a control that has a pop up menu will
2861 * dispose of the menu. To avoid this behavior, set the
2862 * menu to null before the control is disposed.
2863 * </p>
2864 *
2865 * @param menu the new pop up menu
2866 *
2867 * @exception IllegalArgumentException <ul>
2868 * <li>ERROR_MENU_NOT_POP_UP - the menu is not a pop up menu</li>
2869 * <li>ERROR_INVALID_PARENT - if the menu is not in the same widget tree</li>
2870 * <li>ERROR_INVALID_ARGUMENT - if the menu has been disposed</li>
2871 * </ul>
2872 * @exception SWTException <ul>
2873 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2874 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2875 * </ul>
2876 */

2877public void setMenu (Menu menu) {
2878    checkWidget ();
2879    if (menu != null) {
2880        if (menu.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
2881        if ((menu.style & SWT.POP_UP) == 0) {
2882            error (SWT.ERROR_MENU_NOT_POP_UP);
2883        }
2884        if (menu.parent != menuShell ()) {
2885            error (SWT.ERROR_INVALID_PARENT);
2886        }
2887    }
2888    this.menu = menu;
2889}
2890
2891boolean setRadioFocus () {
2892    return false;
2893}
2894
2895boolean setRadioSelection (boolean value) {
2896    return false;
2897}
2898
2899/**
2900 * If the argument is <code>false</code>, causes subsequent drawing
2901 * operations in the receiver to be ignored. No drawing of any kind
2902 * can occur in the receiver until the flag is set to true.
2903 * Graphics operations that occurred while the flag was
2904 * <code>false</code> are lost. When the flag is set to <code>true</code>,
2905 * the entire widget is marked as needing to be redrawn. Nested calls
2906 * to this method are stacked.
2907 * <p>
2908 * Note: This operation is a hint and may not be supported on some
2909 * platforms or for some widgets.
2910 * </p>
2911 *
2912 * @param redraw the new redraw state
2913 *
2914 * @exception SWTException <ul>
2915 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2916 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2917 * </ul>
2918 *
2919 * @see #redraw(int, int, int, int, boolean)
2920 * @see #update()
2921 */

2922public void setRedraw (boolean redraw) {
2923    checkWidget ();
2924    /*
2925     * Feature in Windows. When WM_SETREDRAW is used to turn
2926     * off drawing in a widget, it clears the WS_VISIBLE bits
2927     * and then sets them when redraw is turned back on. This
2928     * means that WM_SETREDRAW will make a widget unexpectedly
2929     * visible. The fix is to track the visibility state while
2930     * drawing is turned off and restore it when drawing is
2931     * turned back on.
2932     */

2933    if (drawCount == 0) {
2934        int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
2935        if ((bits & OS.WS_VISIBLE) == 0) state |= HIDDEN;
2936    }
2937    if (redraw) {
2938        if (--drawCount == 0) {
2939            int topHandle = topHandle ();
2940            OS.SendMessage (topHandle, OS.WM_SETREDRAW, 1, 0);
2941            if (handle != topHandle) OS.SendMessage (handle, OS.WM_SETREDRAW, 1, 0);
2942            if ((state & HIDDEN) != 0) {
2943                state &= ~HIDDEN;
2944                OS.ShowWindow (topHandle, OS.SW_HIDE);
2945                if (handle != topHandle) OS.ShowWindow (handle, OS.SW_HIDE);
2946            } else {
2947                if (OS.IsWinCE) {
2948                    OS.InvalidateRect (topHandle, null, true);
2949                    if (handle != topHandle) OS.InvalidateRect (handle, null, true);
2950                } else {
2951                    int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE | OS.RDW_ALLCHILDREN;
2952                    OS.RedrawWindow (topHandle, null, 0, flags);
2953                }
2954            }
2955        }
2956    } else {
2957        if (drawCount++ == 0) {
2958            int topHandle = topHandle ();
2959            OS.SendMessage (topHandle, OS.WM_SETREDRAW, 0, 0);
2960            if (handle != topHandle) OS.SendMessage (handle, OS.WM_SETREDRAW, 0, 0);
2961        }
2962    }
2963}
2964
2965boolean setSavedFocus () {
2966    return forceFocus ();
2967}
2968
2969/**
2970 * Sets the receiver's size to the point specified by the arguments.
2971 * <p>
2972 * Note: Attempting to set the width or height of the
2973 * receiver to a negative number will cause that
2974 * value to be set to zero instead.
2975 * </p>
2976 *
2977 * @param width the new width for the receiver
2978 * @param height the new height for the receiver
2979 *
2980 * @exception SWTException <ul>
2981 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
2982 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
2983 * </ul>
2984 */

2985public void setSize (int width, int height) {
2986    checkWidget ();
2987    int flags = OS.SWP_NOMOVE | OS.SWP_NOZORDER | OS.SWP_DRAWFRAME | OS.SWP_NOACTIVATE;
2988    setBounds (0, 0, Math.max (0, width), Math.max (0, height), flags);
2989}
2990
2991/**
2992 * Sets the receiver's size to the point specified by the argument.
2993 * <p>
2994 * Note: Attempting to set the width or height of the
2995 * receiver to a negative number will cause them to be
2996 * set to zero instead.
2997 * </p>
2998 *
2999 * @param size the new size for the receiver
3000 *
3001 * @exception IllegalArgumentException <ul>
3002 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
3003 * </ul>
3004 * @exception SWTException <ul>
3005 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3006 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3007 * </ul>
3008 */

3009public void setSize (Point size) {
3010    checkWidget ();
3011    if (size == null) error (SWT.ERROR_NULL_ARGUMENT);
3012    setSize (size.x, size.y);
3013}
3014
3015boolean setTabGroupFocus () {
3016    return setTabItemFocus ();
3017}
3018
3019boolean setTabItemFocus () {
3020    if (!isShowing ()) return false;
3021    return forceFocus ();
3022}
3023
3024/**
3025 * Sets the receiver's tool tip text to the argument, which
3026 * may be null indicating that no tool tip text should be shown.
3027 *
3028 * @param string the new tool tip text (or null)
3029 *
3030 * @exception SWTException <ul>
3031 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3032 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3033 * </ul>
3034 */

3035public void setToolTipText (String JavaDoc string) {
3036    checkWidget ();
3037    toolTipText = string;
3038    setToolTipText (getShell (), string);
3039}
3040
3041void setToolTipText (Shell shell, String JavaDoc string) {
3042    shell.setToolTipText (handle, string);
3043}
3044
3045/**
3046 * Marks the receiver as visible if the argument is <code>true</code>,
3047 * and marks it invisible otherwise.
3048 * <p>
3049 * If one of the receiver's ancestors is not visible or some
3050 * other condition makes the receiver not visible, marking
3051 * it visible may not actually cause it to be displayed.
3052 * </p>
3053 *
3054 * @param visible the new visibility state
3055 *
3056 * @exception SWTException <ul>
3057 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3058 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3059 * </ul>
3060 */

3061public void setVisible (boolean visible) {
3062    checkWidget ();
3063    if (drawCount != 0) {
3064        if (((state & HIDDEN) == 0) == visible) return;
3065    } else {
3066        int bits = OS.GetWindowLong (handle, OS.GWL_STYLE);
3067        if (((bits & OS.WS_VISIBLE) != 0) == visible) return;
3068    }
3069    if (visible) {
3070        sendEvent (SWT.Show);
3071        if (isDisposed ()) return;
3072    }
3073    
3074    /*
3075    * Feature in Windows. If the receiver has focus, hiding
3076    * the receiver causes no window to have focus. The fix is
3077    * to assign focus to the first ancestor window that takes
3078    * focus. If no window will take focus, set focus to the
3079    * desktop.
3080    */

3081    Control control = null;
3082    boolean fixFocus = false;
3083    if (!visible) {
3084        if (display.focusEvent != SWT.FocusOut) {
3085            control = display.getFocusControl ();
3086            fixFocus = isFocusAncestor (control);
3087        }
3088    }
3089    if (drawCount != 0) {
3090        state = visible ? state & ~HIDDEN : state | HIDDEN;
3091    } else {
3092        showWidget (visible);
3093        if (isDisposed ()) return;
3094    }
3095    if (!visible) {
3096        sendEvent (SWT.Hide);
3097        if (isDisposed ()) return;
3098    }
3099    if (fixFocus) fixFocus (control);
3100}
3101
3102void sort (int [] items) {
3103    /* Shell Sort from K&R, pg 108 */
3104    int length = items.length;
3105    for (int gap=length/2; gap>0; gap/=2) {
3106        for (int i=gap; i<length; i++) {
3107            for (int j=i-gap; j>=0; j-=gap) {
3108                if (items [j] <= items [j + gap]) {
3109                    int swap = items [j];
3110                    items [j] = items [j + gap];
3111                    items [j + gap] = swap;
3112                }
3113            }
3114        }
3115    }
3116}
3117
3118void subclass () {
3119    int oldProc = windowProc ();
3120    int newProc = display.windowProc;
3121    if (oldProc == newProc) return;
3122    OS.SetWindowLong (handle, OS.GWL_WNDPROC, newProc);
3123}
3124
3125/**
3126 * Returns a point which is the result of converting the
3127 * argument, which is specified in display relative coordinates,
3128 * to coordinates relative to the receiver.
3129 * <p>
3130 * @param x the x coordinate to be translated
3131 * @param y the y coordinate to be translated
3132 * @return the translated coordinates
3133 *
3134 * @exception SWTException <ul>
3135 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3136 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3137 * </ul>
3138 *
3139 * @since 2.1
3140 */

3141public Point toControl (int x, int y) {
3142    checkWidget ();
3143    POINT pt = new POINT ();
3144    pt.x = x; pt.y = y;
3145    OS.ScreenToClient (handle, pt);
3146    return new Point (pt.x, pt.y);
3147}
3148
3149/**
3150 * Returns a point which is the result of converting the
3151 * argument, which is specified in display relative coordinates,
3152 * to coordinates relative to the receiver.
3153 * <p>
3154 * @param point the point to be translated (must not be null)
3155 * @return the translated coordinates
3156 *
3157 * @exception IllegalArgumentException <ul>
3158 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
3159 * </ul>
3160 * @exception SWTException <ul>
3161 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3162 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3163 * </ul>
3164 */

3165public Point toControl (Point point) {
3166    checkWidget ();
3167    if (point == null) error (SWT.ERROR_NULL_ARGUMENT);
3168    return toControl (point.x, point.y);
3169}
3170
3171/**
3172 * Returns a point which is the result of converting the
3173 * argument, which is specified in coordinates relative to
3174 * the receiver, to display relative coordinates.
3175 * <p>
3176 * @param x the x coordinate to be translated
3177 * @param y the y coordinate to be translated
3178 * @return the translated coordinates
3179 *
3180 * @exception SWTException <ul>
3181 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3182 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3183 * </ul>
3184 *
3185 * @since 2.1
3186 */

3187public Point toDisplay (int x, int y) {
3188    checkWidget ();
3189    POINT pt = new POINT ();
3190    pt.x = x; pt.y = y;
3191    OS.ClientToScreen (handle, pt);
3192    return new Point (pt.x, pt.y);
3193}
3194
3195/**
3196 * Returns a point which is the result of converting the
3197 * argument, which is specified in coordinates relative to
3198 * the receiver, to display relative coordinates.
3199 * <p>
3200 * @param point the point to be translated (must not be null)
3201 * @return the translated coordinates
3202 *
3203 * @exception IllegalArgumentException <ul>
3204 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
3205 * </ul>
3206 * @exception SWTException <ul>
3207 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3208 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3209 * </ul>
3210 */

3211public Point toDisplay (Point point) {
3212    checkWidget ();
3213    if (point == null) error (SWT.ERROR_NULL_ARGUMENT);
3214    return toDisplay (point.x, point.y);
3215}
3216
3217int topHandle () {
3218    return handle;
3219}
3220
3221boolean translateAccelerator (MSG msg) {
3222    return menuShell ().translateAccelerator (msg);
3223}
3224
3225boolean translateMnemonic (Event event, Control control) {
3226    if (control == this) return false;
3227    if (!isVisible () || !isEnabled ()) return false;
3228    event.doit = mnemonicMatch (event.character);
3229    return traverse (event);
3230}
3231
3232boolean translateMnemonic (MSG msg) {
3233    if (msg.wParam < 0x20) return false;
3234    int hwnd = msg.hwnd;
3235    if (OS.GetKeyState (OS.VK_MENU) >= 0) {
3236        int code = OS.SendMessage (hwnd, OS.WM_GETDLGCODE, 0, 0);
3237        if ((code & OS.DLGC_WANTALLKEYS) != 0) return false;
3238        if ((code & OS.DLGC_BUTTON) == 0) return false;
3239    }
3240    Decorations shell = menuShell ();
3241    if (shell.isVisible () && shell.isEnabled ()) {
3242        display.lastAscii = msg.wParam;
3243        display.lastNull = display.lastDead = false;
3244        Event event = new Event ();
3245        event.detail = SWT.TRAVERSE_MNEMONIC;
3246        if (setKeyState (event, SWT.Traverse, msg.wParam, msg.lParam)) {
3247            return translateMnemonic (event, null) || shell.translateMnemonic (event, this);
3248        }
3249    }
3250    return false;
3251}
3252
3253boolean translateTraversal (MSG msg) {
3254    int hwnd = msg.hwnd;
3255    int key = msg.wParam;
3256    if (key == OS.VK_MENU) {
3257        OS.SendMessage (hwnd, OS.WM_CHANGEUISTATE, OS.UIS_INITIALIZE, 0);
3258        return false;
3259    }
3260    int detail = SWT.TRAVERSE_NONE;
3261    boolean doit = true, all = false;
3262    boolean lastVirtual = false;
3263    int lastKey = key, lastAscii = 0;
3264    switch (key) {
3265        case OS.VK_ESCAPE: {
3266            all = true;
3267            lastAscii = 27;
3268            int code = OS.SendMessage (hwnd, OS.WM_GETDLGCODE, 0, 0);
3269            if ((code & OS.DLGC_WANTALLKEYS) != 0) {
3270                /*
3271                * Use DLGC_HASSETSEL to determine that the control
3272                * is a text widget. A text widget normally wants
3273                * all keys except VK_ESCAPE. If this bit is not
3274                * set, then assume the control wants all keys,
3275                * including VK_ESCAPE.
3276                */

3277                if ((code & OS.DLGC_HASSETSEL) == 0) doit = false;
3278            }
3279            detail = SWT.TRAVERSE_ESCAPE;
3280            break;
3281        }
3282        case OS.VK_RETURN: {
3283            all = true;
3284            lastAscii = '\r';
3285            int code = OS.SendMessage (hwnd, OS.WM_GETDLGCODE, 0, 0);
3286            if ((code & OS.DLGC_WANTALLKEYS) != 0) doit = false;
3287            detail = SWT.TRAVERSE_RETURN;
3288            break;
3289        }
3290        case OS.VK_TAB: {
3291            lastAscii = '\t';
3292            boolean next = OS.GetKeyState (OS.VK_SHIFT) >= 0;
3293            int code = OS.SendMessage (hwnd, OS.WM_GETDLGCODE, 0, 0);
3294            if ((code & (OS.DLGC_WANTTAB | OS.DLGC_WANTALLKEYS)) != 0) {
3295                /*
3296                * Use DLGC_HASSETSEL to determine that the control is a
3297                * text widget. If the control is a text widget, then
3298                * Ctrl+Tab and Shift+Tab should traverse out of the widget.
3299                * If the control is not a text widget, the correct behavior
3300                * is to give every character, including Tab, Ctrl+Tab and
3301                * Shift+Tab to the control.
3302                */

3303                if ((code & OS.DLGC_HASSETSEL) != 0) {
3304                    if (next && OS.GetKeyState (OS.VK_CONTROL) >= 0) {
3305                        doit = false;
3306                    }
3307                } else {
3308                    doit = false;
3309                }
3310            }
3311            if (parent != null && (parent.style & SWT.MIRRORED) != 0) {
3312                if (key == OS.VK_LEFT || key == OS.VK_RIGHT) next = !next;
3313            }
3314            detail = next ? SWT.TRAVERSE_TAB_NEXT : SWT.TRAVERSE_TAB_PREVIOUS;
3315            break;
3316        }
3317        case OS.VK_UP:
3318        case OS.VK_LEFT:
3319        case OS.VK_DOWN:
3320        case OS.VK_RIGHT: {
3321            /*
3322            * On WinCE SP there is no tab key. Focus is assigned
3323            * using the VK_UP and VK_DOWN keys, not with VK_LEFT
3324            * or VK_RIGHT.
3325            */

3326            if (OS.IsSP) {
3327                if (key == OS.VK_LEFT || key == OS.VK_RIGHT) return false;
3328            }
3329            lastVirtual = true;
3330            int code = OS.SendMessage (hwnd, OS.WM_GETDLGCODE, 0, 0);
3331            if ((code & (OS.DLGC_WANTARROWS /*| OS.DLGC_WANTALLKEYS*/)) != 0) doit = false;
3332            boolean next = key == OS.VK_DOWN || key == OS.VK_RIGHT;
3333            detail = next ? SWT.TRAVERSE_ARROW_NEXT : SWT.TRAVERSE_ARROW_PREVIOUS;
3334            break;
3335        }
3336        case OS.VK_PRIOR:
3337        case OS.VK_NEXT: {
3338            all = true;
3339            lastVirtual = true;
3340            if (OS.GetKeyState (OS.VK_CONTROL) >= 0) return false;
3341            int code = OS.SendMessage (hwnd, OS.WM_GETDLGCODE, 0, 0);
3342            if ((code & OS.DLGC_WANTALLKEYS) != 0) {
3343                /*
3344                * Use DLGC_HASSETSEL to determine that the control is a
3345                * text widget. If the control is a text widget, then
3346                * Ctrl+PgUp and Ctrl+PgDn should traverse out of the widget.
3347                */

3348                if ((code & OS.DLGC_HASSETSEL) == 0) doit = false;
3349            }
3350            detail = key == OS.VK_PRIOR ? SWT.TRAVERSE_PAGE_PREVIOUS : SWT.TRAVERSE_PAGE_NEXT;
3351            break;
3352        }
3353        default:
3354            return false;
3355    }
3356    Event event = new Event ();
3357    event.doit = doit;
3358    event.detail = detail;
3359    display.lastKey = lastKey;
3360    display.lastAscii = lastAscii;
3361    display.lastVirtual = lastVirtual;
3362    display.lastNull = display.lastDead = false;
3363    if (!setKeyState (event, SWT.Traverse, msg.wParam, msg.lParam)) return false;
3364    Shell shell = getShell ();
3365    Control control = this;
3366    do {
3367        if (control.traverse (event)) {
3368            OS.SendMessage (hwnd, OS.WM_CHANGEUISTATE, OS.UIS_INITIALIZE, 0);
3369            return true;
3370        }
3371        if (!event.doit && control.hooks (SWT.Traverse)) return false;
3372        if (control == shell) return false;
3373        control = control.parent;
3374    } while (all && control != null);
3375    return false;
3376}
3377
3378boolean traverse (Event event) {
3379    /*
3380    * It is possible (but unlikely), that application
3381    * code could have disposed the widget in the traverse
3382    * event. If this happens, return true to stop further
3383    * event processing.
3384    */

3385    sendEvent (SWT.Traverse, event);
3386    if (isDisposed ()) return true;
3387    if (!event.doit) return false;
3388    switch (event.detail) {
3389        case SWT.TRAVERSE_NONE: return true;
3390        case SWT.TRAVERSE_ESCAPE: return traverseEscape ();
3391        case SWT.TRAVERSE_RETURN: return traverseReturn ();
3392        case SWT.TRAVERSE_TAB_NEXT: return traverseGroup (true);
3393        case SWT.TRAVERSE_TAB_PREVIOUS: return traverseGroup (false);
3394        case SWT.TRAVERSE_ARROW_NEXT: return traverseItem (true);
3395        case SWT.TRAVERSE_ARROW_PREVIOUS: return traverseItem (false);
3396        case SWT.TRAVERSE_MNEMONIC: return traverseMnemonic (event.character);
3397        case SWT.TRAVERSE_PAGE_NEXT: return traversePage (true);
3398        case SWT.TRAVERSE_PAGE_PREVIOUS: return traversePage (false);
3399    }
3400    return false;
3401}
3402
3403/**
3404 * Based on the argument, perform one of the expected platform
3405 * traversal action. The argument should be one of the constants:
3406 * <code>SWT.TRAVERSE_ESCAPE</code>, <code>SWT.TRAVERSE_RETURN</code>,
3407 * <code>SWT.TRAVERSE_TAB_NEXT</code>, <code>SWT.TRAVERSE_TAB_PREVIOUS</code>,
3408 * <code>SWT.TRAVERSE_ARROW_NEXT</code> and <code>SWT.TRAVERSE_ARROW_PREVIOUS</code>.
3409 *
3410 * @param traversal the type of traversal
3411 * @return true if the traversal succeeded
3412 *
3413 * @exception SWTException <ul>
3414 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3415 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3416 * </ul>
3417 */

3418public boolean traverse (int traversal) {
3419    checkWidget ();
3420    Event event = new Event ();
3421    event.doit = true;
3422    event.detail = traversal;
3423    return traverse (event);
3424}
3425
3426boolean traverseEscape () {
3427    return false;
3428}
3429
3430boolean traverseGroup (boolean next) {
3431    Control root = computeTabRoot ();
3432    Control group = computeTabGroup ();
3433    Control [] list = root.computeTabList ();
3434    int length = list.length;
3435    int index = 0;
3436    while (index < length) {
3437        if (list [index] == group) break;
3438        index++;
3439    }
3440    /*
3441    * It is possible (but unlikely), that application
3442    * code could have disposed the widget in focus in
3443    * or out events. Ensure that a disposed widget is
3444    * not accessed.
3445    */

3446    if (index == length) return false;
3447    int start = index, offset = (next) ? 1 : -1;
3448    while ((index = ((index + offset + length) % length)) != start) {
3449        Control control = list [index];
3450        if (!control.isDisposed () && control.setTabGroupFocus ()) {
3451            return true;
3452        }
3453    }
3454    if (group.isDisposed ()) return false;
3455    return group.setTabGroupFocus ();
3456}
3457
3458boolean traverseItem (boolean next) {
3459    Control [] children = parent._getChildren ();
3460    int length = children.length;
3461    int index = 0;
3462    while (index < length) {
3463        if (children [index] == this) break;
3464        index++;
3465    }
3466    /*
3467    * It is possible (but unlikely), that application
3468    * code could have disposed the widget in focus in
3469    * or out events. Ensure that a disposed widget is
3470    * not accessed.
3471    */

3472    if (index == length) return false;
3473    int start = index, offset = (next) ? 1 : -1;
3474    while ((index = (index + offset + length) % length) != start) {
3475        Control child = children [index];
3476        if (!child.isDisposed () && child.isTabItem ()) {
3477            if (child.setTabItemFocus ()) return true;
3478        }
3479    }
3480    return false;
3481}
3482
3483boolean traverseMnemonic (char key) {
3484    if (mnemonicHit (key)) {
3485        OS.SendMessage (handle, OS.WM_CHANGEUISTATE, OS.UIS_INITIALIZE, 0);
3486        return true;
3487    }
3488    return false;
3489}
3490
3491boolean traversePage (boolean next) {
3492    return false;
3493}
3494
3495boolean traverseReturn () {
3496    return false;
3497}
3498
3499void unsubclass () {
3500    int newProc = windowProc ();
3501    int oldProc = display.windowProc;
3502    if (oldProc == newProc) return;
3503    OS.SetWindowLong (handle, OS.GWL_WNDPROC, newProc);
3504}
3505
3506/**
3507 * Forces all outstanding paint requests for the widget
3508 * to be processed before this method returns. If there
3509 * are no outstanding paint request, this method does
3510 * nothing.
3511 * <p>
3512 * Note: This method does not cause a redraw.
3513 * </p>
3514 *
3515 * @exception SWTException <ul>
3516 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3517 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3518 * </ul>
3519 *
3520 * @see #redraw()
3521 * @see #redraw(int, int, int, int, boolean)
3522 * @see PaintListener
3523 * @see SWT#Paint
3524 */

3525public void update () {
3526    checkWidget ();
3527    update (false);
3528}
3529
3530void update (boolean all) {
3531// checkWidget ();
3532
if (OS.IsWinCE) {
3533        OS.UpdateWindow (handle);
3534    } else {
3535        int flags = OS.RDW_UPDATENOW;
3536        if (all) flags |= OS.RDW_ALLCHILDREN;
3537        OS.RedrawWindow (handle, null, 0, flags);
3538    }
3539}
3540
3541void updateBackgroundColor () {
3542    Control control = findBackgroundControl ();
3543    if (control == null) control = this;
3544    setBackgroundPixel (control.background);
3545}
3546
3547void updateBackgroundImage () {
3548    Control control = findBackgroundControl ();
3549    Image image = control != null ? control.backgroundImage : backgroundImage;
3550    setBackgroundImage (image != null ? image.handle : 0);
3551}
3552
3553void updateBackgroundMode () {
3554    int oldState = state & PARENT_BACKGROUND;
3555    checkBackground ();
3556    if (oldState != (state & PARENT_BACKGROUND)) {
3557        setBackground ();
3558    }
3559}
3560
3561void updateFont (Font oldFont, Font newFont) {
3562    if (getFont ().equals (oldFont)) setFont (newFont);
3563}
3564
3565void updateImages () {
3566    /* Do nothing */
3567}
3568
3569void updateLayout (boolean resize, boolean all) {
3570    /* Do nothing */
3571}
3572
3573CREATESTRUCT widgetCreateStruct () {
3574    return null;
3575}
3576
3577int widgetExtStyle () {
3578    int bits = 0;
3579    if (!OS.IsPPC) {
3580        if ((style & SWT.BORDER) != 0) bits |= OS.WS_EX_CLIENTEDGE;
3581    }
3582// if ((style & SWT.BORDER) != 0) {
3583
// if ((style & SWT.FLAT) == 0) bits |= OS.WS_EX_CLIENTEDGE;
3584
// }
3585
/*
3586    * Feature in Windows NT. When CreateWindowEx() is called with
3587    * WS_EX_LAYOUTRTL or WS_EX_NOINHERITLAYOUT, CreateWindowEx()
3588    * fails to create the HWND. The fix is to not use these bits.
3589    */

3590    if (OS.WIN32_VERSION < OS.VERSION (4, 10)) {
3591        return bits;
3592    }
3593    bits |= OS.WS_EX_NOINHERITLAYOUT;
3594    if ((style & SWT.RIGHT_TO_LEFT) != 0) bits |= OS.WS_EX_LAYOUTRTL;
3595    return bits;
3596}
3597
3598int widgetParent () {
3599    return parent.handle;
3600}
3601
3602int widgetStyle () {
3603    /* Force clipping of siblings by setting WS_CLIPSIBLINGS */
3604    int bits = OS.WS_CHILD | OS.WS_VISIBLE | OS.WS_CLIPSIBLINGS;
3605// if ((style & SWT.BORDER) != 0) {
3606
// if ((style & SWT.FLAT) != 0) bits |= OS.WS_BORDER;
3607
// }
3608
if (OS.IsPPC) {
3609        if ((style & SWT.BORDER) != 0) bits |= OS.WS_BORDER;
3610    }
3611    return bits;
3612    
3613    /*
3614    * This code is intentionally commented. When clipping
3615    * of both siblings and children is not enforced, it is
3616    * possible for application code to draw outside of the
3617    * control.
3618    */

3619// int bits = OS.WS_CHILD | OS.WS_VISIBLE;
3620
// if ((style & SWT.CLIP_SIBLINGS) != 0) bits |= OS.WS_CLIPSIBLINGS;
3621
// if ((style & SWT.CLIP_CHILDREN) != 0) bits |= OS.WS_CLIPCHILDREN;
3622
// return bits;
3623
}
3624
3625/**
3626 * Changes the parent of the widget to be the one provided if
3627 * the underlying operating system supports this feature.
3628 * Returns <code>true</code> if the parent is successfully changed.
3629 *
3630 * @param parent the new parent for the control.
3631 * @return <code>true</code> if the parent is changed and <code>false</code> otherwise.
3632 *
3633 * @exception IllegalArgumentException <ul>
3634 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
3635 * <li>ERROR_NULL_ARGUMENT - if the parent is <code>null</code></li>
3636 * </ul>
3637 * @exception SWTException <ul>
3638 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
3639 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
3640 * </ul>
3641 */

3642public boolean setParent (Composite parent) {
3643    checkWidget ();
3644    if (parent == null) error (SWT.ERROR_NULL_ARGUMENT);
3645    if (parent.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
3646    if (this.parent == parent) return true;
3647    if (!isReparentable ()) return false;
3648    releaseParent ();
3649    Shell newShell = parent.getShell (), oldShell = getShell ();
3650    Decorations newDecorations = parent.menuShell (), oldDecorations = menuShell ();
3651    if (oldShell != newShell || oldDecorations != newDecorations) {
3652        Menu [] menus = oldShell.findMenus (this);
3653        fixChildren (newShell, oldShell, newDecorations, oldDecorations, menus);
3654    }
3655    int topHandle = topHandle ();
3656    if (OS.SetParent (topHandle, parent.handle) == 0) return false;
3657    this.parent = parent;
3658    int flags = OS.SWP_NOSIZE | OS.SWP_NOMOVE | OS.SWP_NOACTIVATE;
3659    SetWindowPos (topHandle, OS.HWND_BOTTOM, 0, 0, 0, 0, flags);
3660    return true;
3661}
3662
3663abstract TCHAR windowClass ();
3664
3665abstract int windowProc ();
3666
3667int windowProc (int hwnd, int msg, int wParam, int lParam) {
3668    LRESULT result = null;
3669    switch (msg) {
3670        case OS.WM_ACTIVATE: result = WM_ACTIVATE (wParam, lParam); break;
3671        case OS.WM_CAPTURECHANGED: result = WM_CAPTURECHANGED (wParam, lParam); break;
3672        case OS.WM_CHAR: result = WM_CHAR (wParam, lParam); break;
3673        case OS.WM_CLEAR: result = WM_CLEAR (wParam, lParam); break;
3674        case OS.WM_CLOSE: result = WM_CLOSE (wParam, lParam); break;
3675        case OS.WM_COMMAND: result = WM_COMMAND (wParam, lParam); break;
3676        case OS.WM_CONTEXTMENU: result = WM_CONTEXTMENU (wParam, lParam); break;
3677        case OS.WM_CTLCOLORBTN:
3678        case OS.WM_CTLCOLORDLG:
3679        case OS.WM_CTLCOLOREDIT:
3680        case OS.WM_CTLCOLORLISTBOX:
3681        case OS.WM_CTLCOLORMSGBOX:
3682        case OS.WM_CTLCOLORSCROLLBAR:
3683        case OS.WM_CTLCOLORSTATIC: result = WM_CTLCOLOR (wParam, lParam); break;
3684        case OS.WM_CUT: result = WM_CUT (wParam, lParam); break;
3685        case OS.WM_DESTROY: result = WM_DESTROY (wParam, lParam); break;
3686        case OS.WM_DRAWITEM: result = WM_DRAWITEM (wParam, lParam); break;
3687        case OS.WM_ENDSESSION: result = WM_ENDSESSION (wParam, lParam); break;
3688        case OS.WM_ENTERIDLE: result = WM_ENTERIDLE (wParam, lParam); break;
3689        case OS.WM_ERASEBKGND: result = WM_ERASEBKGND (wParam, lParam); break;
3690        case OS.WM_GETDLGCODE: result = WM_GETDLGCODE (wParam, lParam); break;
3691        case OS.WM_GETFONT: result = WM_GETFONT (wParam, lParam); break;
3692        case OS.WM_GETOBJECT: result = WM_GETOBJECT (wParam, lParam); break;
3693        case OS.WM_GETMINMAXINFO: result = WM_GETMINMAXINFO (wParam, lParam); break;
3694        case OS.WM_HELP: result = WM_HELP (wParam, lParam); break;
3695        case OS.WM_HSCROLL: result = WM_HSCROLL (wParam, lParam); break;
3696        case OS.WM_IME_CHAR: result = WM_IME_CHAR (wParam, lParam); break;
3697        case OS.WM_IME_COMPOSITION: result = WM_IME_COMPOSITION (wParam, lParam); break;
3698        case OS.WM_INITMENUPOPUP: result = WM_INITMENUPOPUP (wParam, lParam); break;
3699        case OS.WM_INPUTLANGCHANGE: result = WM_INPUTLANGCHANGE (wParam, lParam); break;
3700        case OS.WM_HOTKEY: result = WM_HOTKEY (wParam, lParam); break;
3701        case OS.WM_KEYDOWN: result = WM_KEYDOWN (wParam, lParam); break;
3702        case OS.WM_KEYUP: result = WM_KEYUP (wParam, lParam); break;
3703        case OS.WM_KILLFOCUS: result = WM_KILLFOCUS (wParam, lParam); break;
3704        case OS.WM_LBUTTONDBLCLK: result = WM_LBUTTONDBLCLK (wParam, lParam); break;
3705        case OS.WM_LBUTTONDOWN: result = WM_LBUTTONDOWN (wParam, lParam); break;
3706        case OS.WM_LBUTTONUP: result = WM_LBUTTONUP (wParam, lParam); break;
3707        case OS.WM_MBUTTONDBLCLK: result = WM_MBUTTONDBLCLK (wParam, lParam); break;
3708        case OS.WM_MBUTTONDOWN: result = WM_MBUTTONDOWN (wParam, lParam); break;
3709        case OS.WM_MBUTTONUP: result = WM_MBUTTONUP (wParam, lParam); break;
3710        case OS.WM_MEASUREITEM: result = WM_MEASUREITEM (wParam, lParam); break;
3711        case OS.WM_MENUCHAR: result = WM_MENUCHAR (wParam, lParam); break;
3712        case OS.WM_MENUSELECT: result = WM_MENUSELECT (wParam, lParam); break;
3713        case OS.WM_MOUSEACTIVATE: result = WM_MOUSEACTIVATE (wParam, lParam); break;
3714        case OS.WM_MOUSEHOVER: result = WM_MOUSEHOVER (wParam, lParam); break;
3715        case OS.WM_MOUSELEAVE: result = WM_MOUSELEAVE (wParam, lParam); break;
3716        case OS.WM_MOUSEMOVE: result = WM_MOUSEMOVE (wParam, lParam); break;
3717        case OS.WM_MOUSEWHEEL: result = WM_MOUSEWHEEL (wParam, lParam); break;
3718        case OS.WM_MOVE: result = WM_MOVE (wParam, lParam); break;
3719        case OS.WM_NCACTIVATE: result = WM_NCACTIVATE (wParam, lParam); break;
3720        case OS.WM_NCCALCSIZE: result = WM_NCCALCSIZE (wParam, lParam); break;
3721        case OS.WM_NCHITTEST: result = WM_NCHITTEST (wParam, lParam); break;
3722        case OS.WM_NCLBUTTONDOWN: result = WM_NCLBUTTONDOWN (wParam, lParam); break;
3723        case OS.WM_NCPAINT: result = WM_NCPAINT (wParam, lParam); break;
3724        case OS.WM_NOTIFY: result = WM_NOTIFY (wParam, lParam); break;
3725        case OS.WM_PAINT: result = WM_PAINT (wParam, lParam); break;
3726        case OS.WM_PALETTECHANGED: result = WM_PALETTECHANGED (wParam, lParam); break;
3727        case OS.WM_PARENTNOTIFY: result = WM_PARENTNOTIFY (wParam, lParam); break;
3728        case OS.WM_PASTE: result = WM_PASTE (wParam, lParam); break;
3729        case OS.WM_PRINT: result = WM_PRINT (wParam, lParam); break;
3730        case OS.WM_PRINTCLIENT: result = WM_PRINTCLIENT (wParam, lParam); break;
3731        case OS.WM_QUERYENDSESSION: result = WM_QUERYENDSESSION (wParam, lParam); break;
3732        case OS.WM_QUERYNEWPALETTE: result = WM_QUERYNEWPALETTE (wParam, lParam); break;
3733        case OS.WM_QUERYOPEN: result = WM_QUERYOPEN (wParam, lParam); break;
3734        case OS.WM_RBUTTONDBLCLK: result = WM_RBUTTONDBLCLK (wParam, lParam); break;
3735        case OS.WM_RBUTTONDOWN: result = WM_RBUTTONDOWN (wParam, lParam); break;
3736        case OS.WM_RBUTTONUP: result = WM_RBUTTONUP (wParam, lParam); break;
3737        case OS.WM_SETCURSOR: result = WM_SETCURSOR (wParam, lParam); break;
3738        case OS.WM_SETFOCUS: result = WM_SETFOCUS (wParam, lParam); break;
3739        case OS.WM_SETFONT: result = WM_SETFONT (wParam, lParam); break;
3740        case OS.WM_SETTINGCHANGE: result = WM_SETTINGCHANGE (wParam, lParam); break;
3741        case OS.WM_SETREDRAW: result = WM_SETREDRAW (wParam, lParam); break;
3742        case OS.WM_SHOWWINDOW: result = WM_SHOWWINDOW (wParam, lParam); break;
3743        case OS.WM_SIZE: result = WM_SIZE (wParam, lParam); break;
3744        case OS.WM_SYSCHAR: result = WM_SYSCHAR (wParam, lParam); break;
3745        case OS.WM_SYSCOLORCHANGE: result = WM_SYSCOLORCHANGE (wParam, lParam); break;
3746        case OS.WM_SYSCOMMAND: result = WM_SYSCOMMAND (wParam, lParam); break;
3747        case OS.WM_SYSKEYDOWN: result = WM_SYSKEYDOWN (wParam, lParam); break;
3748        case OS.WM_SYSKEYUP: result = WM_SYSKEYUP (wParam, lParam); break;
3749        case OS.WM_TIMER: result = WM_TIMER (wParam, lParam); break;
3750        case OS.WM_UNDO: result = WM_UNDO (wParam, lParam); break;
3751        case OS.WM_UPDATEUISTATE: result = WM_UPDATEUISTATE (wParam, lParam); break;
3752        case OS.WM_VSCROLL: result = WM_VSCROLL (wParam, lParam); break;
3753        case OS.WM_WINDOWPOSCHANGED: result = WM_WINDOWPOSCHANGED (wParam, lParam); break;
3754        case OS.WM_WINDOWPOSCHANGING: result = WM_WINDOWPOSCHANGING (wParam, lParam); break;
3755        case OS.WM_XBUTTONDBLCLK: result = WM_XBUTTONDBLCLK (wParam, lParam); break;
3756        case OS.WM_XBUTTONDOWN: result = WM_XBUTTONDOWN (wParam, lParam); break;
3757        case OS.WM_XBUTTONUP: result = WM_XBUTTONUP (wParam, lParam); break;
3758    }
3759    if (result != null) return result.value;
3760    return callWindowProc (hwnd, msg, wParam, lParam);
3761}
3762
3763LRESULT WM_ACTIVATE (int wParam, int lParam) {
3764    return null;
3765}
3766
3767LRESULT WM_CAPTURECHANGED (int wParam, int lParam) {
3768    return wmCaptureChanged (handle, wParam, lParam);
3769}
3770
3771LRESULT WM_CHAR (int wParam, int lParam) {
3772    return wmChar (handle, wParam, lParam);
3773}
3774
3775LRESULT WM_CLEAR (int wParam, int lParam) {
3776    return null;
3777}
3778
3779LRESULT WM_CLOSE (int wParam, int lParam) {
3780    return null;
3781}
3782
3783LRESULT WM_COMMAND (int wParam, int lParam) {
3784    /*
3785    * When the WM_COMMAND message is sent from a
3786    * menu, the HWND parameter in LPARAM is zero.
3787    */

3788    if (lParam == 0) {
3789        Decorations shell = menuShell ();
3790        if (shell.isEnabled ()) {
3791            int id = wParam & 0xFFFF;
3792            MenuItem item = display.getMenuItem (id);
3793            if (item != null && item.isEnabled ()) {
3794                return item.wmCommandChild (wParam, lParam);
3795            }
3796        }
3797        return null;
3798    }
3799    Control control = display.getControl (lParam);
3800    if (control == null) return null;
3801    return control.wmCommandChild (wParam, lParam);
3802}
3803
3804LRESULT WM_CONTEXTMENU (int wParam, int lParam) {
3805    return wmContextMenu (handle, wParam, lParam);
3806}
3807
3808LRESULT WM_CTLCOLOR (int wParam, int lParam) {
3809    int hPalette = display.hPalette;
3810    if (hPalette != 0) {
3811        OS.SelectPalette (wParam, hPalette, false);
3812        OS.RealizePalette (wParam);
3813    }
3814    Control control = display.getControl (lParam);
3815    if (control == null) return null;
3816    return control.wmColorChild (wParam, lParam);
3817}
3818
3819LRESULT WM_CUT (int wParam, int lParam) {
3820    return null;
3821}
3822
3823LRESULT WM_DESTROY (int wParam, int lParam) {
3824    return null;
3825}
3826
3827LRESULT WM_DRAWITEM (int wParam, int lParam) {
3828    DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT ();
3829    OS.MoveMemory (struct, lParam, DRAWITEMSTRUCT.sizeof);
3830    if (struct.CtlType == OS.ODT_MENU) {
3831        MenuItem item = display.getMenuItem (struct.itemID);
3832        if (item == null) return null;
3833        return item.wmDrawChild (wParam, lParam);
3834    }
3835    Control control = display.getControl (struct.hwndItem);
3836    if (control == null) return null;
3837    return control.wmDrawChild (wParam, lParam);
3838}
3839
3840LRESULT WM_ENDSESSION (int wParam, int lParam) {
3841    return null;
3842}
3843
3844LRESULT WM_ENTERIDLE (int wParam, int lParam) {
3845    return null;
3846}
3847
3848LRESULT WM_ERASEBKGND (int wParam, int lParam) {
3849    if ((state & DRAW_BACKGROUND) != 0) {
3850        if (findImageControl () != null) return LRESULT.ONE;
3851    }
3852    if ((state & THEME_BACKGROUND) != 0) {
3853        if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
3854            if (findThemeControl () != null) return LRESULT.ONE;
3855        }
3856    }
3857    return null;
3858}
3859
3860LRESULT WM_GETDLGCODE (int wParam, int lParam) {
3861    return null;
3862}
3863
3864LRESULT WM_GETFONT (int wParam, int lParam) {
3865    return null;
3866}
3867
3868LRESULT WM_GETOBJECT (int wParam, int lParam) {
3869    if (accessible != null) {
3870        int result = accessible.internal_WM_GETOBJECT (wParam, lParam);
3871        if (result != 0) return new LRESULT (result);
3872    }
3873    return null;
3874}
3875
3876LRESULT WM_GETMINMAXINFO (int wParam, int lParam) {
3877    return null;
3878}
3879
3880LRESULT WM_HOTKEY (int wParam, int lParam) {
3881    return null;
3882}
3883
3884LRESULT WM_HELP (int wParam, int lParam) {
3885    if (OS.IsWinCE) return null;
3886    HELPINFO lphi = new HELPINFO ();
3887    OS.MoveMemory (lphi, lParam, HELPINFO.sizeof);
3888    Decorations shell = menuShell ();
3889    if (!shell.isEnabled ()) return null;
3890    if (lphi.iContextType == OS.HELPINFO_MENUITEM) {
3891        MenuItem item = display.getMenuItem (lphi.iCtrlId);
3892        if (item != null && item.isEnabled ()) {
3893            Widget widget = null;
3894            if (item.hooks (SWT.Help)) {
3895                widget = item;
3896            } else {
3897                Menu menu = item.parent;
3898                if (menu.hooks (SWT.Help)) widget = menu;
3899            }
3900            if (widget != null) {
3901                int hwndShell = shell.handle;
3902                OS.SendMessage (hwndShell, OS.WM_CANCELMODE, 0, 0);
3903                widget.postEvent (SWT.Help);
3904                return LRESULT.ONE;
3905            }
3906        }
3907        return null;
3908    }
3909    if (hooks (SWT.Help)) {
3910        postEvent (SWT.Help);
3911        return LRESULT.ONE;
3912    }
3913    return null;
3914}
3915
3916LRESULT WM_HSCROLL (int wParam, int lParam) {
3917    Control control = display.getControl (lParam);
3918    if (control == null) return null;
3919    return control.wmScrollChild (wParam, lParam);
3920}
3921
3922LRESULT WM_IME_CHAR (int wParam, int lParam) {
3923    return wmIMEChar (handle, wParam, lParam);
3924}
3925
3926LRESULT WM_IME_COMPOSITION (int wParam, int lParam) {
3927    return null;
3928}
3929
3930LRESULT WM_INITMENUPOPUP (int wParam, int lParam) {
3931    
3932    /* Ignore WM_INITMENUPOPUP for an accelerator */
3933    if (display.accelKeyHit) return null;
3934
3935    /*
3936    * If the high order word of LPARAM is non-zero,
3937    * the menu is the system menu and we can ignore
3938    * WPARAM. Otherwise, use WPARAM to find the menu.
3939    */

3940    Shell shell = getShell ();
3941    Menu oldMenu = shell.activeMenu, newMenu = null;
3942    if ((lParam >> 16) == 0) {
3943        newMenu = menuShell ().findMenu (wParam);
3944        if (newMenu != null) newMenu.update ();
3945    }
3946    Menu menu = newMenu;
3947    while (menu != null && menu != oldMenu) {
3948        menu = menu.getParentMenu ();
3949    }
3950    if (menu == null) {
3951        menu = shell.activeMenu;
3952        while (menu != null) {
3953            /*
3954            * It is possible (but unlikely), that application
3955            * code could have disposed the widget in the hide
3956            * event. If this happens, stop searching up the
3957            * ancestor list because there is no longer a link
3958            * to follow.
3959            */

3960            menu.sendEvent (SWT.Hide);
3961            if (menu.isDisposed ()) break;
3962            menu = menu.getParentMenu ();
3963            Menu ancestor = newMenu;
3964            while (ancestor != null && ancestor != menu) {
3965                ancestor = ancestor.getParentMenu ();
3966            }
3967            if (ancestor != null) break;
3968        }
3969    }
3970    
3971    /*
3972    * The shell and the new menu may be disposed because of
3973    * sending the hide event to the ancestor menus but setting
3974    * a field to null in a disposed shell is not harmful.
3975    */

3976    if (newMenu != null && newMenu.isDisposed ()) newMenu = null;
3977    shell.activeMenu = newMenu;
3978    
3979    /* Send the show event */
3980    if (newMenu != null && newMenu != oldMenu) {
3981        newMenu.sendEvent (SWT.Show);
3982        // widget could be disposed at this point
3983
}
3984    return null;
3985}
3986
3987LRESULT WM_INPUTLANGCHANGE (int wParam, int lParam) {
3988    return null;
3989}
3990
3991LRESULT WM_KEYDOWN (int wParam, int lParam) {
3992    return wmKeyDown (handle, wParam, lParam);
3993}
3994
3995LRESULT WM_KEYUP (int wParam, int lParam) {
3996    return wmKeyUp (handle, wParam, lParam);
3997}
3998
3999LRESULT WM_KILLFOCUS (int wParam, int lParam) {
4000    return wmKillFocus (handle, wParam, lParam);
4001}
4002
4003LRESULT WM_LBUTTONDBLCLK (int wParam, int lParam) {
4004    return wmLButtonDblClk (handle, wParam, lParam);
4005}
4006
4007LRESULT WM_LBUTTONDOWN (int wParam, int lParam) {
4008    return wmLButtonDown (handle, wParam, lParam);
4009}
4010
4011LRESULT WM_LBUTTONUP (int wParam, int lParam) {
4012    return wmLButtonUp (handle, wParam, lParam);
4013}
4014
4015LRESULT WM_MBUTTONDBLCLK (int wParam, int lParam) {
4016    return wmMButtonDblClk (handle, wParam, lParam);
4017}
4018
4019LRESULT WM_MBUTTONDOWN (int wParam, int lParam) {
4020    return wmMButtonDown (handle, wParam, lParam);
4021}
4022
4023LRESULT WM_MBUTTONUP (int wParam, int lParam) {
4024    return wmMButtonUp (handle, wParam, lParam);
4025}
4026
4027LRESULT WM_MEASUREITEM (int wParam, int lParam) {
4028    MEASUREITEMSTRUCT struct = new MEASUREITEMSTRUCT ();
4029    OS.MoveMemory (struct, lParam, MEASUREITEMSTRUCT.sizeof);
4030    if (struct.CtlType == OS.ODT_MENU) {
4031        MenuItem item = display.getMenuItem (struct.itemID);
4032        if (item == null) return null;
4033        return item.wmMeasureChild (wParam, lParam);
4034    }
4035    int hwnd = OS.GetDlgItem (handle, struct.CtlID);
4036    Control control = display.getControl (hwnd);
4037    if (control == null) return null;
4038    return control.wmMeasureChild (wParam, lParam);
4039}
4040
4041LRESULT WM_MENUCHAR (int wParam, int lParam) {
4042    /*
4043    * Feature in Windows. When the user types Alt+<key>
4044    * and <key> does not match a mnemonic in the System
4045    * menu or the menu bar, Windows beeps. This beep is
4046    * unexpected and unwanted by applications that look
4047    * for Alt+<key>. The fix is to detect the case and
4048    * stop Windows from beeping by closing the menu.
4049    */

4050    int type = wParam >> 16;
4051    if (type == 0 || type == OS.MF_SYSMENU) {
4052        display.mnemonicKeyHit = false;
4053        return new LRESULT (OS.MNC_CLOSE << 16);
4054    }
4055    return null;
4056}
4057
4058LRESULT WM_MENUSELECT (int wParam, int lParam) {
4059    int code = wParam >> 16;
4060    Shell shell = getShell ();
4061    if (code == -1 && lParam == 0) {
4062        Menu menu = shell.activeMenu;
4063        while (menu != null) {
4064            /*
4065            * When the user cancels any menu that is not the
4066            * menu bar, assume a mnemonic key was pressed to open
4067            * the menu from WM_SYSCHAR. When the menu was invoked
4068            * using the mouse, this assumption is wrong but not
4069            * harmful. This variable is only used in WM_SYSCHAR
4070            * and WM_SYSCHAR is only sent after the user has pressed
4071            * a mnemonic.
4072            */

4073            display.mnemonicKeyHit = true;
4074            /*
4075            * It is possible (but unlikely), that application
4076            * code could have disposed the widget in the hide
4077            * event. If this happens, stop searching up the
4078            * parent list because there is no longer a link
4079            * to follow.
4080            */

4081            menu.sendEvent (SWT.Hide);
4082            if (menu.isDisposed ()) break;
4083            menu = menu.getParentMenu ();
4084        }
4085        /*
4086        * The shell may be disposed because of sending the hide
4087        * event to the last active menu menu but setting a field
4088        * to null in a destroyed widget is not harmful.
4089        */

4090        shell.activeMenu = null;
4091        return null;
4092    }
4093    if ((code & OS.MF_SYSMENU) != 0) return null;
4094    if ((code & OS.MF_HILITE) != 0) {
4095        MenuItem item = null;
4096        Decorations menuShell = menuShell ();
4097        if ((code & OS.MF_POPUP) != 0) {
4098            int index = wParam & 0xFFFF;
4099            MENUITEMINFO info = new MENUITEMINFO ();
4100            info.cbSize = MENUITEMINFO.sizeof;
4101            info.fMask = OS.MIIM_SUBMENU;
4102            if (OS.GetMenuItemInfo (lParam, index, true, info)) {
4103                Menu newMenu = menuShell.findMenu (info.hSubMenu);
4104                if (newMenu != null) item = newMenu.cascade;
4105            }
4106        } else {
4107            Menu newMenu = menuShell.findMenu (lParam);
4108            if (newMenu != null) {
4109                int id = wParam & 0xFFFF;
4110                item = display.getMenuItem (id);
4111            }
4112            Menu oldMenu = shell.activeMenu;
4113            if (oldMenu != null) {
4114                Menu ancestor = oldMenu;
4115                while (ancestor != null && ancestor != newMenu) {
4116                    ancestor = ancestor.getParentMenu ();
4117                }
4118                if (ancestor == newMenu) {
4119                    ancestor = oldMenu;
4120                    while (ancestor != newMenu) {
4121                        /*
4122                        * It is possible (but unlikely), that application
4123                        * code could have disposed the widget in the hide
4124                        * event or the item about to be armed. If this
4125                        * happens, stop searching up the ancestor list
4126                        * because there is no longer a link to follow.
4127                        */

4128                        ancestor.sendEvent (SWT.Hide);
4129                        if (ancestor.isDisposed ()) break;
4130                        ancestor = ancestor.getParentMenu ();
4131                    }
4132                    /*
4133                    * The shell and/or the item could be disposed when
4134                    * processing hide events from above. If this happens,
4135                    * ensure that the shell is not accessed and that no
4136                    * arm event is sent to the item.
4137                    */

4138                    if (!shell.isDisposed ()) {
4139                        if (newMenu != null && newMenu.isDisposed ()) {
4140                            newMenu = null;
4141                        }
4142                        shell.activeMenu = newMenu;
4143                    }
4144                    if (item != null && item.isDisposed ()) item = null;
4145                }
4146            }
4147        }
4148        if (item != null) item.sendEvent (SWT.Arm);
4149    }
4150    return null;
4151}
4152
4153LRESULT WM_MOUSEACTIVATE (int wParam, int lParam) {
4154    return null;
4155}
4156
4157LRESULT WM_MOUSEHOVER (int wParam, int lParam) {
4158    return wmMouseHover (handle, wParam, lParam);
4159}
4160
4161LRESULT WM_MOUSELEAVE (int wParam, int lParam) {
4162    if (OS.COMCTL32_MAJOR >= 6) getShell ().fixToolTip ();
4163    return wmMouseLeave (handle, wParam, lParam);
4164}
4165
4166LRESULT WM_MOUSEMOVE (int wParam, int lParam) {
4167    return wmMouseMove (handle, wParam, lParam);
4168}
4169
4170LRESULT WM_MOUSEWHEEL (int wParam, int lParam) {
4171    return wmMouseWheel (handle, wParam, lParam);
4172}
4173
4174LRESULT WM_MOVE (int wParam, int lParam) {
4175    if (findImageControl () != null) {
4176        if (this != getShell ()) redrawChildren ();
4177    } else {
4178        if ((state & THEME_BACKGROUND) != 0) {
4179            if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
4180                if (OS.IsWindowVisible (handle)) {
4181                    if (findThemeControl () != null) redrawChildren ();
4182                }
4183            }
4184        }
4185    }
4186    sendEvent (SWT.Move);
4187    // widget could be disposed at this point
4188
return null;
4189}
4190
4191LRESULT WM_NCACTIVATE (int wParam, int lParam) {
4192    return null;
4193}
4194
4195LRESULT WM_NCCALCSIZE (int wParam, int lParam) {
4196    return null;
4197}
4198
4199LRESULT WM_NCHITTEST (int wParam, int lParam) {
4200    if (!OS.IsWindowEnabled (handle)) return null;
4201    if (!isActive ()) return new LRESULT (OS.HTTRANSPARENT);
4202    return null;
4203}
4204
4205LRESULT WM_NCLBUTTONDOWN (int wParam, int lParam) {
4206    return null;
4207}
4208
4209LRESULT WM_NCPAINT (int wParam, int lParam) {
4210    return null;
4211}
4212
4213LRESULT WM_NOTIFY (int wParam, int lParam) {
4214    NMHDR hdr = new NMHDR ();
4215    OS.MoveMemory (hdr, lParam, NMHDR.sizeof);
4216    return wmNotify (hdr, wParam, lParam);
4217}
4218
4219LRESULT WM_PAINT (int wParam, int lParam) {
4220    return super.wmPaint (handle, wParam, lParam);
4221}
4222
4223LRESULT WM_PALETTECHANGED (int wParam, int lParam) {
4224    return null;
4225}
4226
4227LRESULT WM_PARENTNOTIFY (int wParam, int lParam) {
4228    return null;
4229}
4230
4231LRESULT WM_PASTE (int wParam, int lParam) {
4232    return null;
4233}
4234
4235LRESULT WM_PRINT (int wParam, int lParam) {
4236    return wmPrint (handle, wParam, lParam);
4237}
4238
4239LRESULT WM_PRINTCLIENT (int wParam, int lParam) {
4240    return null;
4241}
4242
4243LRESULT WM_QUERYENDSESSION (int wParam, int lParam) {
4244    return null;
4245}
4246
4247LRESULT WM_QUERYNEWPALETTE (int wParam, int lParam) {
4248    return null;
4249}
4250
4251LRESULT WM_QUERYOPEN (int wParam, int lParam) {
4252    return null;
4253}
4254
4255LRESULT WM_RBUTTONDBLCLK (int wParam, int lParam) {
4256    return wmRButtonDblClk (handle, wParam, lParam);
4257}
4258
4259LRESULT WM_RBUTTONDOWN (int wParam, int lParam) {
4260    return wmRButtonDown (handle, wParam, lParam);
4261}
4262
4263LRESULT WM_RBUTTONUP (int wParam, int lParam) {
4264    return wmRButtonUp (handle, wParam, lParam);
4265}
4266
4267LRESULT WM_SETCURSOR (int wParam, int lParam) {
4268    int hitTest = (short) (lParam & 0xFFFF);
4269    if (hitTest == OS.HTCLIENT) {
4270        Control control = display.getControl (wParam);
4271        if (control == null) return null;
4272        Cursor cursor = control.findCursor ();
4273        if (cursor != null) {
4274            OS.SetCursor (cursor.handle);
4275            return LRESULT.ONE;
4276        }
4277    }
4278    return null;
4279}
4280
4281LRESULT WM_SETFOCUS (int wParam, int lParam) {
4282    return wmSetFocus (handle, wParam, lParam);
4283}
4284
4285LRESULT WM_SETTINGCHANGE (int wParam, int lParam) {
4286    return null;
4287}
4288
4289LRESULT WM_SETFONT (int wParam, int lParam) {
4290    return null;
4291}
4292
4293LRESULT WM_SETREDRAW (int wParam, int lParam) {
4294    return null;
4295}
4296
4297LRESULT WM_SHOWWINDOW (int wParam, int lParam) {
4298    return null;
4299}
4300
4301LRESULT WM_SIZE (int wParam, int lParam) {
4302    sendEvent (SWT.Resize);
4303    // widget could be disposed at this point
4304
return null;
4305}
4306
4307LRESULT WM_SYSCHAR (int wParam, int lParam) {
4308    return wmSysChar (handle, wParam, lParam);
4309}
4310
4311LRESULT WM_SYSCOLORCHANGE (int wParam, int lParam) {
4312    return null;
4313}
4314
4315LRESULT WM_SYSCOMMAND (int wParam, int lParam) {
4316    /*
4317    * Check to see if the command is a system command or
4318    * a user menu item that was added to the System menu.
4319    * When a user item is added to the System menu,
4320    * WM_SYSCOMMAND must always return zero.
4321    */

4322    if ((wParam & 0xF000) == 0) {
4323        Decorations shell = menuShell ();
4324        if (shell.isEnabled ()) {
4325            MenuItem item = display.getMenuItem (wParam & 0xFFFF);
4326            if (item != null) item.wmCommandChild (wParam, lParam);
4327        }
4328        return LRESULT.ZERO;
4329    }
4330
4331    /* Process the System Command */
4332    int cmd = wParam & 0xFFF0;
4333    switch (cmd) {
4334        case OS.SC_CLOSE:
4335            int hwndShell = menuShell ().handle;
4336            int bits = OS.GetWindowLong (hwndShell, OS.GWL_STYLE);
4337            if ((bits & OS.WS_SYSMENU) == 0) return LRESULT.ZERO;
4338            break;
4339        case OS.SC_KEYMENU:
4340            /*
4341            * When lParam is zero, one of F10, Shift+F10, Ctrl+F10 or
4342            * Ctrl+Shift+F10 was pressed. If there is no menu bar and
4343            * the focus control is interested in keystrokes, give the
4344            * key to the focus control. Normally, F10 with no menu bar
4345            * moves focus to the System menu but this can be achieved
4346            * using Alt+Space. To allow the application to see F10,
4347            * avoid running the default window proc.
4348            *
4349            * NOTE: When F10 is pressed, WM_SYSCOMMAND is sent to the
4350            * shell, not the focus control. This is undocumented Windows
4351            * behavior.
4352            */

4353            if (lParam == 0) {
4354                Decorations shell = menuShell ();
4355                Menu menu = shell.getMenuBar ();
4356                if (menu == null) {
4357                    Control control = display._getFocusControl ();
4358                    if (control != null) {
4359                        if (control.hooks (SWT.KeyDown) || control.hooks (SWT.KeyUp)) {
4360                            display.mnemonicKeyHit = false;
4361                            return LRESULT.ZERO;
4362                        }
4363                    }
4364                }
4365            } else {
4366                /*
4367                * When lParam is not zero, Alt+<key> was pressed. If the
4368                * application is interested in keystrokes and there is a
4369                * menu bar, check to see whether the key that was pressed
4370                * matches a mnemonic on the menu bar. Normally, Windows
4371                * matches the first character of a menu item as well as
4372                * matching the mnemonic character. To allow the application
4373                * to see the keystrokes in this case, avoid running the default
4374                * window proc.
4375                *
4376                * NOTE: When the user types Alt+Space, the System menu is
4377                * activated. In this case the application should not see
4378                * the keystroke.
4379                */

4380                if (hooks (SWT.KeyDown) || hooks (SWT.KeyUp)) {
4381                    if (lParam != ' ') {
4382                        Decorations shell = menuShell ();
4383                        Menu menu = shell.getMenuBar ();
4384                        if (menu != null) {
4385                            char key = Display.mbcsToWcs (lParam);
4386                            if (key != 0) {
4387                                key = Character.toUpperCase (key);
4388                                MenuItem [] items = menu.getItems ();
4389                                for (int i=0; i<items.length; i++) {
4390                                    MenuItem item = items [i];
4391                                    String JavaDoc text = item.getText ();
4392                                    char mnemonic = findMnemonic (text);
4393                                    if (text.length () > 0 && mnemonic == 0) {
4394                                        char ch = text.charAt (0);
4395                                        if (Character.toUpperCase (ch) == key) {
4396                                            display.mnemonicKeyHit = false;
4397                                            return LRESULT.ZERO;
4398                                        }
4399                                    }
4400                                }
4401                            }
4402                        } else {
4403                            display.mnemonicKeyHit = false;
4404                        }
4405                    }
4406                }
4407            }
4408            // FALL THROUGH
4409
case OS.SC_HSCROLL:
4410        case OS.SC_VSCROLL:
4411            /*
4412            * Do not allow keyboard traversal of the menu bar
4413            * or scrolling when the shell is not enabled.
4414            */

4415            Decorations shell = menuShell ();
4416            if (!shell.isEnabled () || !shell.isActive ()) {
4417                return LRESULT.ZERO;
4418            }
4419            break;
4420        case OS.SC_MINIMIZE:
4421            /* Save the focus widget when the shell is minimized */
4422            menuShell ().saveFocus ();
4423            break;
4424    }
4425    return null;
4426}
4427
4428LRESULT WM_SYSKEYDOWN (int wParam, int lParam) {
4429    return wmSysKeyDown (handle, wParam, lParam);
4430}
4431
4432LRESULT WM_SYSKEYUP (int wParam, int lParam) {
4433    return wmSysKeyUp (handle, wParam, lParam);
4434}
4435
4436LRESULT WM_TIMER (int wParam, int lParam) {
4437    return null;
4438}
4439
4440LRESULT WM_UNDO (int wParam, int lParam) {
4441    return null;
4442}
4443
4444LRESULT WM_UPDATEUISTATE (int wParam, int lParam) {
4445    return null;
4446}
4447
4448LRESULT WM_VSCROLL (int wParam, int lParam) {
4449    Control control = display.getControl (lParam);
4450    if (control == null) return null;
4451    return control.wmScrollChild (wParam, lParam);
4452}
4453
4454LRESULT WM_WINDOWPOSCHANGED (int wParam, int lParam) {
4455    return null;
4456}
4457
4458LRESULT WM_WINDOWPOSCHANGING (int wParam, int lParam) {
4459    /*
4460    * Bug in Windows. When WM_SETREDRAW is used to turn off drawing
4461    * for a control and the control is moved or resized, Windows does
4462    * not redraw the area where the control once was in the parent.
4463    * The fix is to detect this case and redraw the area.
4464    */

4465    if (drawCount != 0) {
4466        Shell shell = getShell ();
4467        if (shell != this) {
4468            WINDOWPOS lpwp = new WINDOWPOS ();
4469            OS.MoveMemory (lpwp, lParam, WINDOWPOS.sizeof);
4470            if ((lpwp.flags & OS.SWP_NOMOVE) == 0 || (lpwp.flags & OS.SWP_NOSIZE) == 0) {
4471                RECT rect = new RECT ();
4472                OS.GetWindowRect (topHandle (), rect);
4473                int hwndParent = parent == null ? 0 : parent.handle;
4474                OS.MapWindowPoints (0, hwndParent, rect, 2);
4475                OS.InvalidateRect (hwndParent, rect, true);
4476            }
4477        }
4478    }
4479    return null;
4480}
4481
4482LRESULT WM_XBUTTONDBLCLK (int wParam, int lParam) {
4483    return wmXButtonDblClk (handle, wParam, lParam);
4484}
4485
4486LRESULT WM_XBUTTONDOWN (int wParam, int lParam) {
4487    return wmXButtonDown (handle, wParam, lParam);
4488}
4489
4490LRESULT WM_XBUTTONUP (int wParam, int lParam) {
4491    return wmXButtonUp (handle, wParam, lParam);
4492}
4493
4494LRESULT wmColorChild (int wParam, int lParam) {
4495    Control control = findBackgroundControl ();
4496    if (control == null) {
4497        if ((state & THEME_BACKGROUND) != 0) {
4498            if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
4499                control = findThemeControl ();
4500                if (control != null) {
4501                    RECT rect = new RECT ();
4502                    OS.GetClientRect (handle, rect);
4503                    OS.SetTextColor (wParam, getForegroundPixel ());
4504                    OS.SetBkColor (wParam, getBackgroundPixel ());
4505                    fillThemeBackground (wParam, control, rect);
4506                    OS.SetBkMode (wParam, OS.TRANSPARENT);
4507                    return new LRESULT (OS.GetStockObject (OS.NULL_BRUSH));
4508                }
4509            }
4510        }
4511        if (foreground == -1) return null;
4512    }
4513    if (control == null) control = this;
4514    int forePixel = getForegroundPixel ();
4515    int backPixel = control.getBackgroundPixel ();
4516    OS.SetTextColor (wParam, forePixel);
4517    OS.SetBkColor (wParam, backPixel);
4518    if (control.backgroundImage != null) {
4519        RECT rect = new RECT ();
4520        OS.GetClientRect (handle, rect);
4521        int hwnd = control.handle;
4522        int hBitmap = control.backgroundImage.handle;
4523        OS.MapWindowPoints (handle, hwnd, rect, 2);
4524        POINT lpPoint = new POINT ();
4525        OS.GetWindowOrgEx (wParam, lpPoint);
4526        OS.SetBrushOrgEx (wParam, -rect.left - lpPoint.x, -rect.top - lpPoint.y, lpPoint);
4527        int hBrush = findBrush (hBitmap, OS.BS_PATTERN);
4528        if ((state & DRAW_BACKGROUND) != 0) {
4529            int hOldBrush = OS.SelectObject (wParam, hBrush);
4530            OS.MapWindowPoints (hwnd, handle, rect, 2);
4531            OS.PatBlt (wParam, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, OS.PATCOPY);
4532            OS.SelectObject (wParam, hOldBrush);
4533        }
4534        OS.SetBkMode (wParam, OS.TRANSPARENT);
4535        return new LRESULT (hBrush);
4536    }
4537    int hBrush = findBrush (backPixel, OS.BS_SOLID);
4538    if ((state & DRAW_BACKGROUND) != 0) {
4539        RECT rect = new RECT ();
4540        OS.GetClientRect (handle, rect);
4541        int hOldBrush = OS.SelectObject (wParam, hBrush);
4542        OS.PatBlt (wParam, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, OS.PATCOPY);
4543        OS.SelectObject (wParam, hOldBrush);
4544    }
4545    return new LRESULT (hBrush);
4546}
4547
4548LRESULT wmCommandChild (int wParam, int lParam) {
4549    return null;
4550}
4551
4552LRESULT wmDrawChild (int wParam, int lParam) {
4553    return null;
4554}
4555
4556LRESULT wmMeasureChild (int wParam, int lParam) {
4557    return null;
4558}
4559
4560LRESULT wmNotify (NMHDR hdr, int wParam, int lParam) {
4561    Control control = display.getControl (hdr.hwndFrom);
4562    if (control == null) return null;
4563    return control.wmNotifyChild (hdr, wParam, lParam);
4564}
4565
4566LRESULT wmNotifyChild (NMHDR hdr, int wParam, int lParam) {
4567    return null;
4568}
4569
4570LRESULT wmScrollChild (int wParam, int lParam) {
4571    return null;
4572}
4573
4574}
4575
4576
Popular Tags