]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/Tracker.java
ba256fea18b1b86a685c9620305bb7e717191d4e
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / Tracker.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2013 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.*;
21 import org.eclipse.swt.internal.win32.*;
22
23 /**
24  *  Instances of this class implement rubber banding rectangles that are
25  *  drawn onto a parent <code>Composite</code> or <code>Display</code>.
26  *  These rectangles can be specified to respond to mouse and key events
27  *  by either moving or resizing themselves accordingly.  Trackers are
28  *  typically used to represent window geometries in a lightweight manner.
29  *
30  * <dl>
31  * <dt><b>Styles:</b></dt>
32  * <dd>LEFT, RIGHT, UP, DOWN, RESIZE</dd>
33  * <dt><b>Events:</b></dt>
34  * <dd>Move, Resize</dd>
35  * </dl>
36  * <p>
37  * Note: Rectangle move behavior is assumed unless RESIZE is specified.
38  * </p><p>
39  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
40  * </p>
41  *
42  * @see <a href="http://www.eclipse.org/swt/snippets/#tracker">Tracker snippets</a>
43  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
44  * @noextend This class is not intended to be subclassed by clients.
45  */
46 public class Tracker extends Widget {
47         Control parent;
48         boolean tracking, cancelled, stippled;
49         Rectangle [] rectangles = new Rectangle [0], proportions = rectangles;
50         Rectangle bounds;
51         long resizeCursor;
52         Cursor clientCursor;
53         int cursorOrientation = SWT.NONE;
54         boolean inEvent = false;
55         boolean drawn;
56         long hwndTransparent, hwndOpaque, oldTransparentProc, oldOpaqueProc;
57         int oldX, oldY;
58
59         /*
60         * The following values mirror step sizes on Windows
61         */
62         final static int STEPSIZE_SMALL = 1;
63         final static int STEPSIZE_LARGE = 9;
64
65 /**
66  * Constructs a new instance of this class given its parent
67  * and a style value describing its behavior and appearance.
68  * <p>
69  * The style value is either one of the style constants defined in
70  * class <code>SWT</code> which is applicable to instances of this
71  * class, or must be built by <em>bitwise OR</em>'ing together
72  * (that is, using the <code>int</code> "|" operator) two or more
73  * of those <code>SWT</code> style constants. The class description
74  * lists the style constants that are applicable to the class.
75  * Style bits are also inherited from superclasses.
76  * </p>
77  *
78  * @param parent a widget which will be the parent of the new instance (cannot be null)
79  * @param style the style of widget to construct
80  *
81  * @exception IllegalArgumentException <ul>
82  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
83  * </ul>
84  * @exception SWTException <ul>
85  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
86  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
87  * </ul>
88  *
89  * @see SWT#LEFT
90  * @see SWT#RIGHT
91  * @see SWT#UP
92  * @see SWT#DOWN
93  * @see SWT#RESIZE
94  * @see Widget#checkSubclass
95  * @see Widget#getStyle
96  */
97 public Tracker (Composite parent, int style) {
98         super (parent, checkStyle (style));
99         this.parent = parent;
100 }
101
102 /**
103  * Constructs a new instance of this class given the display
104  * to create it on and a style value describing its behavior
105  * and appearance.
106  * <p>
107  * The style value is either one of the style constants defined in
108  * class <code>SWT</code> which is applicable to instances of this
109  * class, or must be built by <em>bitwise OR</em>'ing together
110  * (that is, using the <code>int</code> "|" operator) two or more
111  * of those <code>SWT</code> style constants. The class description
112  * lists the style constants that are applicable to the class.
113  * Style bits are also inherited from superclasses.
114  * </p><p>
115  * Note: Currently, null can be passed in for the display argument.
116  * This has the effect of creating the tracker on the currently active
117  * display if there is one. If there is no current display, the
118  * tracker is created on a "default" display. <b>Passing in null as
119  * the display argument is not considered to be good coding style,
120  * and may not be supported in a future release of SWT.</b>
121  * </p>
122  *
123  * @param display the display to create the tracker on
124  * @param style the style of control to construct
125  *
126  * @exception SWTException <ul>
127  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
128  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
129  * </ul>
130  *
131  * @see SWT#LEFT
132  * @see SWT#RIGHT
133  * @see SWT#UP
134  * @see SWT#DOWN
135  * @see SWT#RESIZE
136  */
137 public Tracker (Display display, int style) {
138         if (display == null) display = Display.getCurrent ();
139         if (display == null) display = Display.getDefault ();
140         if (!display.isValidThread ()) {
141                 error (SWT.ERROR_THREAD_INVALID_ACCESS);
142         }
143         this.style = checkStyle (style);
144         this.display = display;
145         reskinWidget ();
146 }
147
148 /**
149  * Adds the listener to the collection of listeners who will
150  * be notified when the control is moved or resized, by sending
151  * it one of the messages defined in the <code>ControlListener</code>
152  * interface.
153  *
154  * @param listener the listener which should be notified
155  *
156  * @exception IllegalArgumentException <ul>
157  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
158  * </ul>
159  * @exception SWTException <ul>
160  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
161  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
162  * </ul>
163  *
164  * @see ControlListener
165  * @see #removeControlListener
166  */
167 public void addControlListener (ControlListener listener) {
168         checkWidget ();
169         if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
170         TypedListener typedListener = new TypedListener (listener);
171         addListener (SWT.Resize, typedListener);
172         addListener (SWT.Move, typedListener);
173 }
174
175 /**
176  * Adds the listener to the collection of listeners who will
177  * be notified when keys are pressed and released on the system keyboard, by sending
178  * it one of the messages defined in the <code>KeyListener</code>
179  * interface.
180  *
181  * @param listener the listener which should be notified
182  *
183  * @exception IllegalArgumentException <ul>
184  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
185  * </ul>
186  * @exception SWTException <ul>
187  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
188  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
189  * </ul>
190  *
191  * @see KeyListener
192  * @see #removeKeyListener
193  */
194 public void addKeyListener (KeyListener listener) {
195         checkWidget ();
196         if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
197         TypedListener typedListener = new TypedListener (listener);
198         addListener (SWT.KeyUp,typedListener);
199         addListener (SWT.KeyDown,typedListener);
200 }
201
202 Point adjustMoveCursor () {
203         if (bounds == null) return null;
204         int newX = bounds.x + bounds.width / 2;
205         int newY = bounds.y;
206         POINT pt = new POINT ();
207         pt.x = newX;  pt.y = newY;
208         /*
209          * Convert to screen coordinates iff needed
210          */
211         if (parent != null) {
212                 OS.ClientToScreen (parent.handle, pt);
213         }
214         OS.SetCursorPos (pt.x, pt.y);
215         return new Point (pt.x, pt.y);
216 }
217
218 Point adjustResizeCursor () {
219         if (bounds == null) return null;
220         int newX, newY;
221
222         if ((cursorOrientation & SWT.LEFT) != 0) {
223                 newX = bounds.x;
224         } else if ((cursorOrientation & SWT.RIGHT) != 0) {
225                 newX = bounds.x + bounds.width;
226         } else {
227                 newX = bounds.x + bounds.width / 2;
228         }
229
230         if ((cursorOrientation & SWT.UP) != 0) {
231                 newY = bounds.y;
232         } else if ((cursorOrientation & SWT.DOWN) != 0) {
233                 newY = bounds.y + bounds.height;
234         } else {
235                 newY = bounds.y + bounds.height / 2;
236         }
237
238         POINT pt = new POINT ();
239         pt.x = newX;  pt.y = newY;
240         /*
241          * Convert to screen coordinates iff needed
242          */
243         if (parent != null) {
244                 OS.ClientToScreen (parent.handle, pt);
245         }
246         OS.SetCursorPos (pt.x, pt.y);
247
248         /*
249         * If the client has not provided a custom cursor then determine
250         * the appropriate resize cursor.
251         */
252         if (clientCursor == null) {
253                 long newCursor = 0;
254                 switch (cursorOrientation) {
255                         case SWT.UP:
256                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZENS);
257                                 break;
258                         case SWT.DOWN:
259                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZENS);
260                                 break;
261                         case SWT.LEFT:
262                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZEWE);
263                                 break;
264                         case SWT.RIGHT:
265                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZEWE);
266                                 break;
267                         case SWT.LEFT | SWT.UP:
268                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZENWSE);
269                                 break;
270                         case SWT.RIGHT | SWT.DOWN:
271                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZENWSE);
272                                 break;
273                         case SWT.LEFT | SWT.DOWN:
274                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZENESW);
275                                 break;
276                         case SWT.RIGHT | SWT.UP:
277                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZENESW);
278                                 break;
279                         default:
280                                 newCursor = OS.LoadCursor (0, OS.IDC_SIZEALL);
281                                 break;
282                 }
283                 OS.SetCursor (newCursor);
284                 if (resizeCursor != 0) {
285                         OS.DestroyCursor (resizeCursor);
286                 }
287                 resizeCursor = newCursor;
288         }
289
290         return new Point (pt.x, pt.y);
291 }
292
293 static int checkStyle (int style) {
294         if ((style & (SWT.LEFT | SWT.RIGHT | SWT.UP | SWT.DOWN)) == 0) {
295                 style |= SWT.LEFT | SWT.RIGHT | SWT.UP | SWT.DOWN;
296         }
297         return style;
298 }
299
300 /**
301  * Stops displaying the tracker rectangles.  Note that this is not considered
302  * to be a cancelation by the user.
303  *
304  * @exception SWTException <ul>
305  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
306  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
307  * </ul>
308  */
309 public void close () {
310         checkWidget ();
311         tracking = false;
312 }
313
314 Rectangle computeBounds () {
315         if (rectangles.length == 0) return null;
316         int xMin = rectangles [0].x;
317         int yMin = rectangles [0].y;
318         int xMax = rectangles [0].x + rectangles [0].width;
319         int yMax = rectangles [0].y + rectangles [0].height;
320
321         for (int i = 1; i < rectangles.length; i++) {
322                 if (rectangles [i].x < xMin) xMin = rectangles [i].x;
323                 if (rectangles [i].y < yMin) yMin = rectangles [i].y;
324                 int rectRight = rectangles [i].x + rectangles [i].width;
325                 if (rectRight > xMax) xMax = rectRight;
326                 int rectBottom = rectangles [i].y + rectangles [i].height;
327                 if (rectBottom > yMax) yMax = rectBottom;
328         }
329
330         return new Rectangle (xMin, yMin, xMax - xMin, yMax - yMin);
331 }
332
333 Rectangle [] computeProportions (Rectangle [] rects) {
334         Rectangle [] result = new Rectangle [rects.length];
335         bounds = computeBounds ();
336         if (bounds != null) {
337                 for (int i = 0; i < rects.length; i++) {
338                         int x = 0, y = 0, width = 0, height = 0;
339                         if (bounds.width != 0) {
340                                 x = (rects [i].x - bounds.x) * 100 / bounds.width;
341                                 width = rects [i].width * 100 / bounds.width;
342                         } else {
343                                 width = 100;
344                         }
345                         if (bounds.height != 0) {
346                                 y = (rects [i].y - bounds.y) * 100 / bounds.height;
347                                 height = rects [i].height * 100 / bounds.height;
348                         } else {
349                                 height = 100;
350                         }
351                         result [i] = new Rectangle (x, y, width, height);
352                 }
353         }
354         return result;
355 }
356
357 /**
358  * Draw the rectangles displayed by the tracker.
359  */
360 void drawRectangles (Rectangle [] rects, boolean stippled) {
361         if (hwndOpaque != 0) {
362                 RECT rect1 = new RECT();
363                 int bandWidth = stippled ? 3 : 1;
364                 for (int i = 0; i < rects.length; i++) {
365                         Rectangle rect = rects[i];
366                         rect1.left = rect.x - bandWidth;
367                         rect1.top = rect.y - bandWidth;
368                         rect1.right = rect.x + rect.width + bandWidth * 2;
369                         rect1.bottom = rect.y + rect.height + bandWidth * 2;
370                         OS.MapWindowPoints (0, hwndOpaque, rect1, 2);
371                         OS.RedrawWindow (hwndOpaque, rect1, 0, OS.RDW_INVALIDATE);
372                 }
373                 return;
374         }
375         int bandWidth = 1;
376         long hwndTrack = parent == null ? OS.GetDesktopWindow () : parent.handle;
377         long hDC = OS.GetDCEx (hwndTrack, 0, OS.DCX_CACHE);
378         long hBitmap = 0, hBrush = 0, oldBrush = 0;
379         if (stippled) {
380                 bandWidth = 3;
381                 byte [] bits = {-86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0};
382                 hBitmap = OS.CreateBitmap (8, 8, 1, 1, bits);
383                 hBrush = OS.CreatePatternBrush (hBitmap);
384                 oldBrush = OS.SelectObject (hDC, hBrush);
385         }
386         for (int i=0; i<rects.length; i++) {
387                 Rectangle rect = rects [i];
388                 OS.PatBlt (hDC, rect.x, rect.y, rect.width, bandWidth, OS.PATINVERT);
389                 OS.PatBlt (hDC, rect.x, rect.y + bandWidth, bandWidth, rect.height - (bandWidth * 2), OS.PATINVERT);
390                 OS.PatBlt (hDC, rect.x + rect.width - bandWidth, rect.y + bandWidth, bandWidth, rect.height - (bandWidth * 2), OS.PATINVERT);
391                 OS.PatBlt (hDC, rect.x, rect.y + rect.height - bandWidth, rect.width, bandWidth, OS.PATINVERT);
392         }
393         if (stippled) {
394                 OS.SelectObject (hDC, oldBrush);
395                 OS.DeleteObject (hBrush);
396                 OS.DeleteObject (hBitmap);
397         }
398         OS.ReleaseDC (hwndTrack, hDC);
399 }
400
401 /**
402  * Returns the bounds that are being drawn, expressed relative to the parent
403  * widget.  If the parent is a <code>Display</code> then these are screen
404  * coordinates.
405  *
406  * @return the bounds of the Rectangles being drawn
407  *
408  * @exception SWTException <ul>
409  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
410  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
411  * </ul>
412  */
413 public Rectangle [] getRectangles () {
414         checkWidget();
415         Rectangle [] result = getRectanglesInPixels();
416         for (int i = 0; i < result.length; i++) {
417                 result[i] = DPIUtil.autoScaleDown(result[i]);
418         }
419         return result;
420 }
421
422 Rectangle [] getRectanglesInPixels () {
423         Rectangle [] result = new Rectangle [rectangles.length];
424         for (int i = 0; i < rectangles.length; i++) {
425                 Rectangle current = rectangles [i];
426                 result [i] = new Rectangle (current.x, current.y, current.width, current.height);
427         }
428         return result;
429 }
430
431 /**
432  * Returns <code>true</code> if the rectangles are drawn with a stippled line, <code>false</code> otherwise.
433  *
434  * @return the stippled effect of the rectangles
435  *
436  * @exception SWTException <ul>
437  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
438  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
439  * </ul>
440  */
441 public boolean getStippled () {
442         checkWidget ();
443         return stippled;
444 }
445
446 void moveRectangles (int xChange, int yChange) {
447         if (bounds == null) return;
448         if (xChange < 0 && ((style & SWT.LEFT) == 0)) xChange = 0;
449         if (xChange > 0 && ((style & SWT.RIGHT) == 0)) xChange = 0;
450         if (yChange < 0 && ((style & SWT.UP) == 0)) yChange = 0;
451         if (yChange > 0 && ((style & SWT.DOWN) == 0)) yChange = 0;
452         if (xChange == 0 && yChange == 0) return;
453         bounds.x += xChange; bounds.y += yChange;
454         for (int i = 0; i < rectangles.length; i++) {
455                 rectangles [i].x += xChange;
456                 rectangles [i].y += yChange;
457         }
458 }
459
460 /**
461  * Displays the Tracker rectangles for manipulation by the user.  Returns when
462  * the user has either finished manipulating the rectangles or has cancelled the
463  * Tracker.
464  *
465  * @return <code>true</code> if the user did not cancel the Tracker, <code>false</code> otherwise
466  *
467  * @exception SWTException <ul>
468  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
469  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
470  * </ul>
471  */
472 public boolean open () {
473         checkWidget ();
474         cancelled = false;
475         tracking = true;
476
477         /*
478         * If exactly one of UP/DOWN is specified as a style then set the cursor
479         * orientation accordingly (the same is done for LEFT/RIGHT styles below).
480         */
481         int vStyle = style & (SWT.UP | SWT.DOWN);
482         if (vStyle == SWT.UP || vStyle == SWT.DOWN) {
483                 cursorOrientation |= vStyle;
484         }
485         int hStyle = style & (SWT.LEFT | SWT.RIGHT);
486         if (hStyle == SWT.LEFT || hStyle == SWT.RIGHT) {
487                 cursorOrientation |= hStyle;
488         }
489
490         Callback newProc = null;
491         boolean mouseDown = OS.GetKeyState(OS.VK_LBUTTON) < 0;
492         /*
493         * Bug in Vista. Drawing directly to the screen with XOR does not
494         * perform well. The fix is to draw on layered window instead.
495         *
496         * Note that one window (almost opaque) is used for catching all events and a
497         * second window is used for drawing the rectangles.
498         */
499         if (parent == null) {
500                 Rectangle bounds = display.getBoundsInPixels();
501                 hwndTransparent = OS.CreateWindowEx (
502                         OS.WS_EX_LAYERED | OS.WS_EX_NOACTIVATE | OS.WS_EX_TOOLWINDOW,
503                         display.windowClass,
504                         null,
505                         OS.WS_POPUP,
506                         bounds.x, bounds.y,
507                         bounds.width, bounds.height,
508                         0,
509                         0,
510                         OS.GetModuleHandle (null),
511                         null);
512                 OS.SetLayeredWindowAttributes (hwndTransparent, 0, (byte)0x01, OS.LWA_ALPHA);
513                 hwndOpaque = OS.CreateWindowEx (
514                         OS.WS_EX_LAYERED | OS.WS_EX_NOACTIVATE | OS.WS_EX_TOOLWINDOW,
515                         display.windowClass,
516                         null,
517                         OS.WS_POPUP,
518                         bounds.x, bounds.y,
519                         bounds.width, bounds.height,
520                         hwndTransparent,
521                         0,
522                         OS.GetModuleHandle (null),
523                         null);
524                 OS.SetLayeredWindowAttributes (hwndOpaque, 0xFFFFFF, (byte)0, OS.LWA_COLORKEY | OS.LWA_ALPHA);
525                 drawn = false;
526                 newProc = new Callback (this, "transparentProc", 4); //$NON-NLS-1$
527                 long newProcAddress = newProc.getAddress ();
528                 if (newProcAddress == 0) error (SWT.ERROR_NO_MORE_CALLBACKS);
529                 oldTransparentProc = OS.GetWindowLongPtr (hwndTransparent, OS.GWLP_WNDPROC);
530                 OS.SetWindowLongPtr (hwndTransparent, OS.GWLP_WNDPROC, newProcAddress);
531                 oldOpaqueProc = OS.GetWindowLongPtr (hwndOpaque, OS.GWLP_WNDPROC);
532                 OS.SetWindowLongPtr (hwndOpaque, OS.GWLP_WNDPROC, newProcAddress);
533                 OS.ShowWindow (hwndTransparent, OS.SW_SHOWNOACTIVATE);
534                 OS.ShowWindow (hwndOpaque, OS.SW_SHOWNOACTIVATE);
535         } else {
536                 /*
537                 * If this tracker is being created without a mouse drag then
538                 * we need to create a transparent window that fills the screen
539                 * in order to get all mouse/keyboard events that occur
540                 * outside of our visible windows (ie.- over the desktop).
541                 */
542                 if (!mouseDown) {
543                         Rectangle bounds = display.getBoundsInPixels();
544                         hwndTransparent = OS.CreateWindowEx (
545                                 OS.WS_EX_TRANSPARENT,
546                                 display.windowClass,
547                                 null,
548                                 OS.WS_POPUP,
549                                 bounds.x, bounds.y,
550                                 bounds.width, bounds.height,
551                                 0,
552                                 0,
553                                 OS.GetModuleHandle (null),
554                                 null);
555                         newProc = new Callback (this, "transparentProc", 4); //$NON-NLS-1$
556                         long newProcAddress = newProc.getAddress ();
557                         if (newProcAddress == 0) error (SWT.ERROR_NO_MORE_CALLBACKS);
558                         oldTransparentProc = OS.GetWindowLongPtr (hwndTransparent, OS.GWLP_WNDPROC);
559                         OS.SetWindowLongPtr (hwndTransparent, OS.GWLP_WNDPROC, newProcAddress);
560                         OS.ShowWindow (hwndTransparent, OS.SW_SHOWNOACTIVATE);
561                 }
562         }
563
564         update ();
565         drawRectangles (rectangles, stippled);
566         Point cursorPos = null;
567         if (mouseDown) {
568                 POINT pt = new POINT ();
569                 OS.GetCursorPos (pt);
570                 cursorPos = new Point (pt.x, pt.y);
571         } else {
572                 if ((style & SWT.RESIZE) != 0) {
573                         cursorPos = adjustResizeCursor ();
574                 } else {
575                         cursorPos = adjustMoveCursor ();
576                 }
577         }
578         if (cursorPos != null) {
579                 oldX = cursorPos.x;
580                 oldY = cursorPos.y;
581         }
582
583         Display display = this.display;
584         try {
585                 /* Tracker behaves like a Dialog with its own OS event loop. */
586                 MSG msg = new MSG ();
587                 while (tracking && !cancelled) {
588                         if (parent != null && parent.isDisposed ()) break;
589                         display.runSkin ();
590                         display.runDeferredLayouts ();
591                         display.sendPreExternalEventDispatchEvent ();
592                         OS.GetMessage (msg, 0, 0, 0);
593                         display.sendPostExternalEventDispatchEvent ();
594                         OS.TranslateMessage (msg);
595                         switch (msg.message) {
596                                 case OS.WM_LBUTTONUP:
597                                 case OS.WM_MOUSEMOVE:
598                                         wmMouse (msg.message, msg.wParam, msg.lParam);
599                                         break;
600                                 case OS.WM_IME_CHAR: wmIMEChar (msg.hwnd, msg.wParam, msg.lParam); break;
601                                 case OS.WM_CHAR: wmChar (msg.hwnd, msg.wParam, msg.lParam); break;
602                                 case OS.WM_KEYDOWN: wmKeyDown (msg.hwnd, msg.wParam, msg.lParam); break;
603                                 case OS.WM_KEYUP: wmKeyUp (msg.hwnd, msg.wParam, msg.lParam); break;
604                                 case OS.WM_SYSCHAR: wmSysChar (msg.hwnd, msg.wParam, msg.lParam); break;
605                                 case OS.WM_SYSKEYDOWN: wmSysKeyDown (msg.hwnd, msg.wParam, msg.lParam); break;
606                                 case OS.WM_SYSKEYUP: wmSysKeyUp (msg.hwnd, msg.wParam, msg.lParam); break;
607                         }
608                         if (OS.WM_KEYFIRST <= msg.message && msg.message <= OS.WM_KEYLAST) continue;
609                         if (OS.WM_MOUSEFIRST <= msg.message && msg.message <= OS.WM_MOUSELAST) continue;
610                         if (hwndOpaque == 0) {
611                                 if (msg.message == OS.WM_PAINT) {
612                                         update ();
613                                         drawRectangles (rectangles, stippled);
614                                 }
615                         }
616                         OS.DispatchMessage (msg);
617                         if (hwndOpaque == 0) {
618                                 if (msg.message == OS.WM_PAINT) {
619                                         drawRectangles (rectangles, stippled);
620                                 }
621                         }
622                         display.runAsyncMessages (false);
623                 }
624                 if (mouseDown) OS.ReleaseCapture ();
625                 if (!isDisposed()) {
626                         update ();
627                         drawRectangles (rectangles, stippled);
628                 }
629         } finally {
630                 /*
631                 * Cleanup: If a transparent window was created in order to capture events then
632                 * destroy it and its callback object now.
633                 */
634                 if (hwndTransparent != 0) {
635                         OS.DestroyWindow (hwndTransparent);
636                         hwndTransparent = 0;
637                 }
638                 hwndOpaque = 0;
639                 if (newProc != null) {
640                         newProc.dispose ();
641                         oldTransparentProc = oldOpaqueProc = 0;
642                 }
643                 /*
644                 * Cleanup: If this tracker was resizing then the last cursor that it created
645                 * needs to be destroyed.
646                 */
647                 if (resizeCursor != 0) {
648                         OS.DestroyCursor (resizeCursor);
649                         resizeCursor = 0;
650                 }
651         }
652         tracking = false;
653         return !cancelled;
654 }
655
656 @Override
657 void releaseWidget () {
658         super.releaseWidget ();
659         parent = null;
660         rectangles = proportions = null;
661         bounds = null;
662 }
663
664 /**
665  * Removes the listener from the collection of listeners who will
666  * be notified when the control is moved or resized.
667  *
668  * @param listener the listener which should no longer be notified
669  *
670  * @exception IllegalArgumentException <ul>
671  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
672  * </ul>
673  * @exception SWTException <ul>
674  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
675  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
676  * </ul>
677  *
678  * @see ControlListener
679  * @see #addControlListener
680  */
681 public void removeControlListener (ControlListener listener) {
682         checkWidget ();
683         if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
684         if (eventTable == null) return;
685         eventTable.unhook (SWT.Resize, listener);
686         eventTable.unhook (SWT.Move, listener);
687 }
688
689 /**
690  * Removes the listener from the collection of listeners who will
691  * be notified when keys are pressed and released on the system keyboard.
692  *
693  * @param listener the listener which should no longer be notified
694  *
695  * @exception IllegalArgumentException <ul>
696  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
697  * </ul>
698  * @exception SWTException <ul>
699  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
700  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
701  * </ul>
702  *
703  * @see KeyListener
704  * @see #addKeyListener
705  */
706 public void removeKeyListener(KeyListener listener) {
707         checkWidget ();
708         if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
709         if (eventTable == null) return;
710         eventTable.unhook (SWT.KeyUp, listener);
711         eventTable.unhook (SWT.KeyDown, listener);
712 }
713
714 void resizeRectangles (int xChange, int yChange) {
715         if (bounds == null) return;
716         /*
717         * If the cursor orientation has not been set in the orientation of
718         * this change then try to set it here.
719         */
720         if (xChange < 0 && ((style & SWT.LEFT) != 0) && ((cursorOrientation & SWT.RIGHT) == 0)) {
721                 cursorOrientation |= SWT.LEFT;
722         }
723         if (xChange > 0 && ((style & SWT.RIGHT) != 0) && ((cursorOrientation & SWT.LEFT) == 0)) {
724                 cursorOrientation |= SWT.RIGHT;
725         }
726         if (yChange < 0 && ((style & SWT.UP) != 0) && ((cursorOrientation & SWT.DOWN) == 0)) {
727                 cursorOrientation |= SWT.UP;
728         }
729         if (yChange > 0 && ((style & SWT.DOWN) != 0) && ((cursorOrientation & SWT.UP) == 0)) {
730                 cursorOrientation |= SWT.DOWN;
731         }
732
733         /*
734          * If the bounds will flip about the x or y axis then apply the adjustment
735          * up to the axis (ie.- where bounds width/height becomes 0), change the
736          * cursor's orientation accordingly, and flip each Rectangle's origin (only
737          * necessary for > 1 Rectangles)
738          */
739         if ((cursorOrientation & SWT.LEFT) != 0) {
740                 if (xChange > bounds.width) {
741                         if ((style & SWT.RIGHT) == 0) return;
742                         cursorOrientation |= SWT.RIGHT;
743                         cursorOrientation &= ~SWT.LEFT;
744                         bounds.x += bounds.width;
745                         xChange -= bounds.width;
746                         bounds.width = 0;
747                         if (proportions.length > 1) {
748                                 for (int i = 0; i < proportions.length; i++) {
749                                         Rectangle proportion = proportions [i];
750                                         proportion.x = 100 - proportion.x - proportion.width;
751                                 }
752                         }
753                 }
754         } else if ((cursorOrientation & SWT.RIGHT) != 0) {
755                 if (bounds.width < -xChange) {
756                         if ((style & SWT.LEFT) == 0) return;
757                         cursorOrientation |= SWT.LEFT;
758                         cursorOrientation &= ~SWT.RIGHT;
759                         xChange += bounds.width;
760                         bounds.width = 0;
761                         if (proportions.length > 1) {
762                                 for (int i = 0; i < proportions.length; i++) {
763                                         Rectangle proportion = proportions [i];
764                                         proportion.x = 100 - proportion.x - proportion.width;
765                                 }
766                         }
767                 }
768         }
769         if ((cursorOrientation & SWT.UP) != 0) {
770                 if (yChange > bounds.height) {
771                         if ((style & SWT.DOWN) == 0) return;
772                         cursorOrientation |= SWT.DOWN;
773                         cursorOrientation &= ~SWT.UP;
774                         bounds.y += bounds.height;
775                         yChange -= bounds.height;
776                         bounds.height = 0;
777                         if (proportions.length > 1) {
778                                 for (int i = 0; i < proportions.length; i++) {
779                                         Rectangle proportion = proportions [i];
780                                         proportion.y = 100 - proportion.y - proportion.height;
781                                 }
782                         }
783                 }
784         } else if ((cursorOrientation & SWT.DOWN) != 0) {
785                 if (bounds.height < -yChange) {
786                         if ((style & SWT.UP) == 0) return;
787                         cursorOrientation |= SWT.UP;
788                         cursorOrientation &= ~SWT.DOWN;
789                         yChange += bounds.height;
790                         bounds.height = 0;
791                         if (proportions.length > 1) {
792                                 for (int i = 0; i < proportions.length; i++) {
793                                         Rectangle proportion = proportions [i];
794                                         proportion.y = 100 - proportion.y - proportion.height;
795                                 }
796                         }
797                 }
798         }
799
800         // apply the bounds adjustment
801         if ((cursorOrientation & SWT.LEFT) != 0) {
802                 bounds.x += xChange;
803                 bounds.width -= xChange;
804         } else if ((cursorOrientation & SWT.RIGHT) != 0) {
805                 bounds.width += xChange;
806         }
807         if ((cursorOrientation & SWT.UP) != 0) {
808                 bounds.y += yChange;
809                 bounds.height -= yChange;
810         } else if ((cursorOrientation & SWT.DOWN) != 0) {
811                 bounds.height += yChange;
812         }
813
814         Rectangle [] newRects = new Rectangle [rectangles.length];
815         for (int i = 0; i < rectangles.length; i++) {
816                 Rectangle proportion = proportions[i];
817                 newRects[i] = new Rectangle (
818                         proportion.x * bounds.width / 100 + bounds.x,
819                         proportion.y * bounds.height / 100 + bounds.y,
820                         proportion.width * bounds.width / 100,
821                         proportion.height * bounds.height / 100);
822         }
823         rectangles = newRects;
824 }
825
826 /**
827  * Sets the <code>Cursor</code> of the Tracker.  If this cursor is <code>null</code>
828  * then the cursor reverts to the default.
829  *
830  * @param newCursor the new <code>Cursor</code> to display
831  *
832  * @exception SWTException <ul>
833  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
834  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
835  * </ul>
836  */
837 public void setCursor(Cursor newCursor) {
838         checkWidget();
839         clientCursor = newCursor;
840         if (newCursor != null) {
841                 if (inEvent) OS.SetCursor (clientCursor.handle);
842         }
843 }
844
845 /**
846  * Specifies the rectangles that should be drawn, expressed relative to the parent
847  * widget.  If the parent is a Display then these are screen coordinates.
848  *
849  * @param rectangles the bounds of the rectangles to be drawn
850  *
851  * @exception IllegalArgumentException <ul>
852  *    <li>ERROR_NULL_ARGUMENT - if the set of rectangles is null or contains a null rectangle</li>
853  * </ul>
854  * @exception SWTException <ul>
855  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
856  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
857  * </ul>
858  */
859 public void setRectangles (Rectangle [] rectangles) {
860         checkWidget ();
861         if (rectangles == null) error (SWT.ERROR_NULL_ARGUMENT);
862         Rectangle [] rectanglesInPixels = new Rectangle [rectangles.length];
863         for (int i = 0; i < rectangles.length; i++) {
864                 rectanglesInPixels [i] = DPIUtil.autoScaleUp (rectangles [i]);
865         }
866         setRectanglesInPixels (rectanglesInPixels);
867 }
868
869 void setRectanglesInPixels (Rectangle [] rectangles) {
870         this.rectangles = new Rectangle [rectangles.length];
871         for (int i = 0; i < rectangles.length; i++) {
872                 Rectangle current = rectangles [i];
873                 if (current == null) error (SWT.ERROR_NULL_ARGUMENT);
874                 this.rectangles [i] = new Rectangle (current.x, current.y, current.width, current.height);
875         }
876         proportions = computeProportions (rectangles);
877 }
878
879 /**
880  * Changes the appearance of the line used to draw the rectangles.
881  *
882  * @param stippled <code>true</code> if rectangle should appear stippled
883  *
884  * @exception SWTException <ul>
885  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
886  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
887  * </ul>
888  */
889 public void setStippled (boolean stippled) {
890         checkWidget ();
891         this.stippled = stippled;
892 }
893
894 long transparentProc (long hwnd, long msg, long wParam, long lParam) {
895         switch ((int)msg) {
896                 /*
897                 * We typically do not want to answer that the transparent window is
898                 * transparent to hits since doing so negates the effect of having it
899                 * to grab events.  However, clients of the tracker should not be aware
900                 * of this transparent window.  Therefore if there is a hit query
901                 * performed as a result of client code then answer that the transparent
902                 * window is transparent to hits so that its existence will not impact
903                 * the client.
904                 */
905                 case OS.WM_NCHITTEST:
906                         if (inEvent) return OS.HTTRANSPARENT;
907                         break;
908                 case OS.WM_SETCURSOR:
909                         if (clientCursor != null) {
910                                 OS.SetCursor (clientCursor.handle);
911                                 return 1;
912                         }
913                         if (resizeCursor != 0) {
914                                 OS.SetCursor (resizeCursor);
915                                 return 1;
916                         }
917                         break;
918                 case OS.WM_PAINT:
919                         if (hwndOpaque == hwnd) {
920                                 PAINTSTRUCT ps = new PAINTSTRUCT();
921                                 long hDC = OS.BeginPaint (hwnd, ps);
922                                 long hBitmap = 0, hBrush = 0, oldBrush = 0;
923                                 long transparentBrush = OS.CreateSolidBrush(0xFFFFFF);
924                                 oldBrush = OS.SelectObject (hDC, transparentBrush);
925                                 OS.PatBlt (hDC, ps.left, ps.top, ps.right - ps.left, ps.bottom - ps.top, OS.PATCOPY);
926                                 OS.SelectObject (hDC, oldBrush);
927                                 OS.DeleteObject (transparentBrush);
928                                 int bandWidth = 1;
929                                 if (stippled) {
930                                         bandWidth = 3;
931                                         byte [] bits = {-86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0};
932                                         hBitmap = OS.CreateBitmap (8, 8, 1, 1, bits);
933                                         hBrush = OS.CreatePatternBrush (hBitmap);
934                                         oldBrush = OS.SelectObject (hDC, hBrush);
935                                         OS.SetBkColor (hDC, 0xF0F0F0);
936                                 } else {
937                                         oldBrush = OS.SelectObject (hDC, OS.GetStockObject(OS.BLACK_BRUSH));
938                                 }
939                                 Rectangle[] rects = this.rectangles;
940                                 RECT rect1 = new RECT ();
941                                 for (int i=0; i<rects.length; i++) {
942                                         Rectangle rect = rects [i];
943                                         rect1.left = rect.x;
944                                         rect1.top  = rect.y;
945                                         rect1.right = rect.x + rect.width;
946                                         rect1.bottom = rect.y + rect.height;
947                                         OS.MapWindowPoints (0, hwndOpaque, rect1, 2);
948                                         int width = rect1.right - rect1.left;
949                                         int height = rect1.bottom - rect1.top;
950                                         OS.PatBlt (hDC, rect1.left, rect1.top, width, bandWidth, OS.PATCOPY);
951                                         OS.PatBlt (hDC, rect1.left, rect1.top + bandWidth, bandWidth, height - (bandWidth * 2), OS.PATCOPY);
952                                         OS.PatBlt (hDC, rect1.right - bandWidth, rect1.top + bandWidth, bandWidth, height - (bandWidth * 2), OS.PATCOPY);
953                                         OS.PatBlt (hDC, rect1.left, rect1.bottom - bandWidth, width, bandWidth, OS.PATCOPY);
954                                 }
955                                 OS.SelectObject (hDC, oldBrush);
956                                 if (stippled) {
957                                         OS.DeleteObject (hBrush);
958                                         OS.DeleteObject (hBitmap);
959                                 }
960                                 OS.EndPaint (hwnd, ps);
961                                 if (!drawn) {
962                                         OS.SetLayeredWindowAttributes (hwndOpaque, 0xFFFFFF, (byte)0xFF, OS.LWA_COLORKEY | OS.LWA_ALPHA);
963                                         drawn = true;
964                                 }
965                                 return 0;
966                         }
967         }
968         return OS.CallWindowProc (hwnd == hwndTransparent ? oldTransparentProc : oldOpaqueProc, hwnd, (int)msg, wParam, lParam);
969 }
970
971 void update () {
972         if (hwndOpaque != 0) return;
973         if (parent != null) {
974                 if (parent.isDisposed ()) return;
975                 Shell shell = parent.getShell ();
976                 shell.update (true);
977         } else {
978                 display.update ();
979         }
980 }
981
982 @Override
983 LRESULT wmKeyDown (long hwnd, long wParam, long lParam) {
984         LRESULT result = super.wmKeyDown (hwnd, wParam, lParam);
985         if (result != null) return result;
986         boolean isMirrored = parent != null && (parent.style & SWT.MIRRORED) != 0;
987         int stepSize = OS.GetKeyState (OS.VK_CONTROL) < 0 ? STEPSIZE_SMALL : STEPSIZE_LARGE;
988         int xChange = 0, yChange = 0;
989         switch ((int)wParam) {
990                 case OS.VK_ESCAPE:
991                         cancelled = true;
992                         tracking = false;
993                         break;
994                 case OS.VK_RETURN:
995                         tracking = false;
996                         break;
997                 case OS.VK_LEFT:
998                         xChange = isMirrored ? stepSize : -stepSize;
999                         break;
1000                 case OS.VK_RIGHT:
1001                         xChange = isMirrored ? -stepSize : stepSize;
1002                         break;
1003                 case OS.VK_UP:
1004                         yChange = -stepSize;
1005                         break;
1006                 case OS.VK_DOWN:
1007                         yChange = stepSize;
1008                         break;
1009         }
1010         if (xChange != 0 || yChange != 0) {
1011                 Rectangle [] oldRectangles = rectangles;
1012                 boolean oldStippled = stippled;
1013                 Rectangle [] rectsToErase = new Rectangle [rectangles.length];
1014                 for (int i = 0; i < rectangles.length; i++) {
1015                         Rectangle current = rectangles [i];
1016                         rectsToErase [i] = new Rectangle (current.x, current.y, current.width, current.height);
1017                 }
1018                 Event event = new Event ();
1019                 event.setLocationInPixels(oldX + xChange, oldY + yChange);
1020                 Point cursorPos;
1021                 if ((style & SWT.RESIZE) != 0) {
1022                         resizeRectangles (xChange, yChange);
1023                         inEvent = true;
1024                         sendEvent (SWT.Resize, event);
1025                         inEvent = false;
1026                         /*
1027                         * It is possible (but unlikely) that application
1028                         * code could have disposed the widget in the resize
1029                         * event.  If this happens return false to indicate
1030                         * that the tracking has failed.
1031                         */
1032                         if (isDisposed ()) {
1033                                 cancelled = true;
1034                                 return LRESULT.ONE;
1035                         }
1036                         boolean draw = false;
1037                         /*
1038                          * It is possible that application code could have
1039                          * changed the rectangles in the resize event.  If this
1040                          * happens then only redraw the tracker if the rectangle
1041                          * values have changed.
1042                          */
1043                         if (rectangles != oldRectangles) {
1044                                 int length = rectangles.length;
1045                                 if (length != rectsToErase.length) {
1046                                         draw = true;
1047                                 } else {
1048                                         for (int i = 0; i < length; i++) {
1049                                                 if (!rectangles [i].equals (rectsToErase [i])) {
1050                                                         draw = true;
1051                                                         break;
1052                                                 }
1053                                         }
1054                                 }
1055                         } else {
1056                                 draw = true;
1057                         }
1058                         if (draw) {
1059                                 drawRectangles (rectsToErase, oldStippled);
1060                                 update ();
1061                                 drawRectangles (rectangles, stippled);
1062                         }
1063                         cursorPos = adjustResizeCursor ();
1064                 } else {
1065                         moveRectangles (xChange, yChange);
1066                         inEvent = true;
1067                         sendEvent (SWT.Move, event);
1068                         inEvent = false;
1069                         /*
1070                         * It is possible (but unlikely) that application
1071                         * code could have disposed the widget in the move
1072                         * event.  If this happens return false to indicate
1073                         * that the tracking has failed.
1074                         */
1075                         if (isDisposed ()) {
1076                                 cancelled = true;
1077                                 return LRESULT.ONE;
1078                         }
1079                         boolean draw = false;
1080                         /*
1081                          * It is possible that application code could have
1082                          * changed the rectangles in the move event.  If this
1083                          * happens then only redraw the tracker if the rectangle
1084                          * values have changed.
1085                          */
1086                         if (rectangles != oldRectangles) {
1087                                 int length = rectangles.length;
1088                                 if (length != rectsToErase.length) {
1089                                         draw = true;
1090                                 } else {
1091                                         for (int i = 0; i < length; i++) {
1092                                                 if (!rectangles [i].equals (rectsToErase [i])) {
1093                                                         draw = true;
1094                                                         break;
1095                                                 }
1096                                         }
1097                                 }
1098                         } else {
1099                                 draw = true;
1100                         }
1101                         if (draw) {
1102                                 drawRectangles (rectsToErase, oldStippled);
1103                                 update ();
1104                                 drawRectangles (rectangles, stippled);
1105                         }
1106                         cursorPos = adjustMoveCursor ();
1107                 }
1108                 if (cursorPos != null) {
1109                         oldX = cursorPos.x;
1110                         oldY = cursorPos.y;
1111                 }
1112         }
1113         return result;
1114 }
1115
1116 @Override
1117 LRESULT wmSysKeyDown (long hwnd, long wParam, long lParam) {
1118         LRESULT result = super.wmSysKeyDown (hwnd, wParam, lParam);
1119         if (result != null) return result;
1120         cancelled = true;
1121         tracking = false;
1122         return result;
1123 }
1124
1125 LRESULT wmMouse (int message, long wParam, long lParam) {
1126         boolean isMirrored = parent != null && (parent.style & SWT.MIRRORED) != 0;
1127         int newPos = OS.GetMessagePos ();
1128         int newX = OS.GET_X_LPARAM (newPos);
1129         int newY = OS.GET_Y_LPARAM (newPos);
1130         if (newX != oldX || newY != oldY) {
1131                 Rectangle [] oldRectangles = rectangles;
1132                 boolean oldStippled = stippled;
1133                 Rectangle [] rectsToErase = new Rectangle [rectangles.length];
1134                 for (int i = 0; i < rectangles.length; i++) {
1135                         Rectangle current = rectangles [i];
1136                         rectsToErase [i] = new Rectangle (current.x, current.y, current.width, current.height);
1137                 }
1138                 Event event = new Event ();
1139                 event.setLocationInPixels(newX, newY);
1140                 if ((style & SWT.RESIZE) != 0) {
1141                         if (isMirrored) {
1142                                 resizeRectangles (oldX - newX, newY - oldY);
1143                         } else {
1144                                 resizeRectangles (newX - oldX, newY - oldY);
1145                         }
1146                         inEvent = true;
1147                         sendEvent (SWT.Resize, event);
1148                         inEvent = false;
1149                         /*
1150                         * It is possible (but unlikely), that application
1151                         * code could have disposed the widget in the resize
1152                         * event.  If this happens, return false to indicate
1153                         * that the tracking has failed.
1154                         */
1155                         if (isDisposed ()) {
1156                                 cancelled = true;
1157                                 return LRESULT.ONE;
1158                         }
1159                         boolean draw = false;
1160                         /*
1161                          * It is possible that application code could have
1162                          * changed the rectangles in the resize event.  If this
1163                          * happens then only redraw the tracker if the rectangle
1164                          * values have changed.
1165                          */
1166                         if (rectangles != oldRectangles) {
1167                                 int length = rectangles.length;
1168                                 if (length != rectsToErase.length) {
1169                                         draw = true;
1170                                 } else {
1171                                         for (int i = 0; i < length; i++) {
1172                                                 if (!rectangles [i].equals (rectsToErase [i])) {
1173                                                         draw = true;
1174                                                         break;
1175                                                 }
1176                                         }
1177                                 }
1178                         }
1179                         else {
1180                                 draw = true;
1181                         }
1182                         if (draw) {
1183                                 drawRectangles (rectsToErase, oldStippled);
1184                                 update ();
1185                                 drawRectangles (rectangles, stippled);
1186                         }
1187                         Point cursorPos = adjustResizeCursor ();
1188                         if (cursorPos != null) {
1189                                 newX = cursorPos.x;
1190                                 newY = cursorPos.y;
1191                         }
1192                 } else {
1193                         if (isMirrored) {
1194                                 moveRectangles (oldX - newX, newY - oldY);
1195                         } else {
1196                                 moveRectangles (newX - oldX, newY - oldY);
1197                         }
1198                         inEvent = true;
1199                         sendEvent (SWT.Move, event);
1200                         inEvent = false;
1201                         /*
1202                         * It is possible (but unlikely), that application
1203                         * code could have disposed the widget in the move
1204                         * event.  If this happens, return false to indicate
1205                         * that the tracking has failed.
1206                         */
1207                         if (isDisposed ()) {
1208                                 cancelled = true;
1209                                 return LRESULT.ONE;
1210                         }
1211                         boolean draw = false;
1212                         /*
1213                          * It is possible that application code could have
1214                          * changed the rectangles in the move event.  If this
1215                          * happens then only redraw the tracker if the rectangle
1216                          * values have changed.
1217                          */
1218                         if (rectangles != oldRectangles) {
1219                                 int length = rectangles.length;
1220                                 if (length != rectsToErase.length) {
1221                                         draw = true;
1222                                 } else {
1223                                         for (int i = 0; i < length; i++) {
1224                                                 if (!rectangles [i].equals (rectsToErase [i])) {
1225                                                         draw = true;
1226                                                         break;
1227                                                 }
1228                                         }
1229                                 }
1230                         } else {
1231                                 draw = true;
1232                         }
1233                         if (draw) {
1234                                 drawRectangles (rectsToErase, oldStippled);
1235                                 update ();
1236                                 drawRectangles (rectangles, stippled);
1237                         }
1238                 }
1239                 oldX = newX;
1240                 oldY = newY;
1241         }
1242         tracking = message != OS.WM_LBUTTONUP;
1243         return null;
1244 }
1245
1246 }