comparison dwt/widgets/Tracker.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 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 module dwt.widgets.Tracker;
12
13 import dwt.dwthelper.utils;
14
15
16 import dwt.DWT;
17 import dwt.DWTException;
18 import dwt.events.ControlListener;
19 import dwt.events.KeyListener;
20 import dwt.graphics.Cursor;
21 import dwt.graphics.Point;
22 import dwt.graphics.Rectangle;
23 import dwt.internal.cocoa.NSAffineTransform;
24 import dwt.internal.cocoa.NSApplication;
25 import dwt.internal.cocoa.NSAutoreleasePool;
26 import dwt.internal.cocoa.NSBezierPath;
27 import dwt.internal.cocoa.NSDate;
28 import dwt.internal.cocoa.NSEvent;
29 import dwt.internal.cocoa.NSGraphicsContext;
30 import dwt.internal.cocoa.NSPoint;
31 import dwt.internal.cocoa.NSRect;
32 import dwt.internal.cocoa.NSScreen;
33 import dwt.internal.cocoa.NSWindow;
34 import dwt.internal.cocoa.OS;
35
36 /**
37 * Instances of this class implement rubber banding rectangles that are
38 * drawn onto a parent <code>Composite</code> or <code>Display</code>.
39 * These rectangles can be specified to respond to mouse and key events
40 * by either moving or resizing themselves accordingly. Trackers are
41 * typically used to represent window geometries in a lightweight manner.
42 *
43 * <dl>
44 * <dt><b>Styles:</b></dt>
45 * <dd>LEFT, RIGHT, UP, DOWN, RESIZE</dd>
46 * <dt><b>Events:</b></dt>
47 * <dd>Move, Resize</dd>
48 * </dl>
49 * <p>
50 * Note: Rectangle move behavior is assumed unless RESIZE is specified.
51 * </p><p>
52 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
53 * </p>
54 */
55 public class Tracker extends Widget {
56 Control parent;
57 bool tracking, cancelled, stippled;
58 Cursor clientCursor, resizeCursor;
59 Rectangle [] rectangles = new Rectangle [0], proportions = rectangles;
60 Rectangle bounds;
61 int cursorOrientation = DWT.NONE;
62 bool inEvent = false;
63 NSWindow window;
64 int oldX, oldY;
65
66 /*
67 * The following values mirror step sizes on Windows
68 */
69 final static int STEPSIZE_SMALL = 1;
70 final static int STEPSIZE_LARGE = 9;
71
72 /**
73 * Constructs a new instance of this class given its parent
74 * and a style value describing its behavior and appearance.
75 * <p>
76 * The style value is either one of the style constants defined in
77 * class <code>DWT</code> which is applicable to instances of this
78 * class, or must be built by <em>bitwise OR</em>'ing together
79 * (that is, using the <code>int</code> "|" operator) two or more
80 * of those <code>DWT</code> style constants. The class description
81 * lists the style constants that are applicable to the class.
82 * Style bits are also inherited from superclasses.
83 * </p>
84 *
85 * @param parent a widget which will be the parent of the new instance (cannot be null)
86 * @param style the style of widget to construct
87 *
88 * @exception IllegalArgumentException <ul>
89 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
90 * </ul>
91 * @exception DWTException <ul>
92 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
93 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
94 * </ul>
95 *
96 * @see DWT#LEFT
97 * @see DWT#RIGHT
98 * @see DWT#UP
99 * @see DWT#DOWN
100 * @see DWT#RESIZE
101 * @see Widget#checkSubclass
102 * @see Widget#getStyle
103 */
104 public Tracker (Composite parent, int style) {
105 super (parent, checkStyle (style));
106 this.parent = parent;
107 }
108
109 /**
110 * Constructs a new instance of this class given the display
111 * to create it on and a style value describing its behavior
112 * and appearance.
113 * <p>
114 * The style value is either one of the style constants defined in
115 * class <code>DWT</code> which is applicable to instances of this
116 * class, or must be built by <em>bitwise OR</em>'ing together
117 * (that is, using the <code>int</code> "|" operator) two or more
118 * of those <code>DWT</code> style constants. The class description
119 * lists the style constants that are applicable to the class.
120 * Style bits are also inherited from superclasses.
121 * </p><p>
122 * Note: Currently, null can be passed in for the display argument.
123 * This has the effect of creating the tracker on the currently active
124 * display if there is one. If there is no current display, the
125 * tracker is created on a "default" display. <b>Passing in null as
126 * the display argument is not considered to be good coding style,
127 * and may not be supported in a future release of DWT.</b>
128 * </p>
129 *
130 * @param display the display to create the tracker on
131 * @param style the style of control to construct
132 *
133 * @exception DWTException <ul>
134 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
135 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
136 * </ul>
137 *
138 * @see DWT#LEFT
139 * @see DWT#RIGHT
140 * @see DWT#UP
141 * @see DWT#DOWN
142 */
143 public Tracker (Display display, int style) {
144 if (display is null) display = Display.getCurrent ();
145 if (display is null) display = Display.getDefault ();
146 if (!display.isValidThread ()) {
147 error (DWT.ERROR_THREAD_INVALID_ACCESS);
148 }
149 this.style = checkStyle (style);
150 this.display = display;
151 }
152
153 /**
154 * Adds the listener to the collection of listeners who will
155 * be notified when the control is moved or resized, by sending
156 * it one of the messages defined in the <code>ControlListener</code>
157 * interface.
158 *
159 * @param listener the listener which should be notified
160 *
161 * @exception IllegalArgumentException <ul>
162 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
163 * </ul>
164 * @exception DWTException <ul>
165 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
166 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
167 * </ul>
168 *
169 * @see ControlListener
170 * @see #removeControlListener
171 */
172 public void addControlListener (ControlListener listener) {
173 checkWidget ();
174 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
175 TypedListener typedListener = new TypedListener (listener);
176 addListener (DWT.Resize, typedListener);
177 addListener (DWT.Move, typedListener);
178 }
179
180 /**
181 * Adds the listener to the collection of listeners who will
182 * be notified when keys are pressed and released on the system keyboard, by sending
183 * it one of the messages defined in the <code>KeyListener</code>
184 * interface.
185 *
186 * @param listener the listener which should be notified
187 *
188 * @exception IllegalArgumentException <ul>
189 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
190 * </ul>
191 * @exception DWTException <ul>
192 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
193 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
194 * </ul>
195 *
196 * @see KeyListener
197 * @see #removeKeyListener
198 */
199 public void addKeyListener(KeyListener listener) {
200 checkWidget();
201 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
202 TypedListener typedListener = new TypedListener (listener);
203 addListener(DWT.KeyUp,typedListener);
204 addListener(DWT.KeyDown,typedListener);
205 }
206
207 Point adjustMoveCursor () {
208 if (bounds is null) return null;
209 int newX = bounds.x + bounds.width / 2;
210 int newY = bounds.y;
211 /*
212 * Convert to screen coordinates if needed
213 */
214 if (parent !is null) {
215 Point pt = parent.toDisplay (newX, newY);
216 newX = pt.x;
217 newY = pt.y;
218 }
219 display.setCursorLocation(newX, newY);
220 return new Point (newX, newY);
221 }
222
223 Point adjustResizeCursor (bool movePointer) {
224 if (bounds is null) return null;
225 int newX, newY;
226
227 if ((cursorOrientation & DWT.LEFT) !is 0) {
228 newX = bounds.x;
229 } else if ((cursorOrientation & DWT.RIGHT) !is 0) {
230 newX = bounds.x + bounds.width;
231 } else {
232 newX = bounds.x + bounds.width / 2;
233 }
234
235 if ((cursorOrientation & DWT.UP) !is 0) {
236 newY = bounds.y;
237 } else if ((cursorOrientation & DWT.DOWN) !is 0) {
238 newY = bounds.y + bounds.height;
239 } else {
240 newY = bounds.y + bounds.height / 2;
241 }
242
243 /*
244 * Convert to screen coordinates if needed
245 */
246 if (parent !is null) {
247 Point pt = parent.toDisplay (newX, newY);
248 newX = pt.x;
249 newY = pt.y;
250 }
251 if (movePointer) {
252 display.setCursorLocation(newX, newY);
253 }
254
255 /*
256 * If the client has not provided a custom cursor then determine
257 * the appropriate resize cursor.
258 */
259 if (clientCursor is null) {
260 Cursor newCursor = null;
261 switch (cursorOrientation) {
262 case DWT.UP:
263 newCursor = new Cursor(display, DWT.CURSOR_SIZENS);
264 break;
265 case DWT.DOWN:
266 newCursor = new Cursor(display, DWT.CURSOR_SIZENS);
267 break;
268 case DWT.LEFT:
269 newCursor = new Cursor(display, DWT.CURSOR_SIZEWE);
270 break;
271 case DWT.RIGHT:
272 newCursor = new Cursor(display, DWT.CURSOR_SIZEWE);
273 break;
274 case DWT.LEFT | DWT.UP:
275 newCursor = new Cursor(display, DWT.CURSOR_SIZENWSE);
276 break;
277 case DWT.RIGHT | DWT.DOWN:
278 newCursor = new Cursor(display, DWT.CURSOR_SIZENWSE);
279 break;
280 case DWT.LEFT | DWT.DOWN:
281 newCursor = new Cursor(display, DWT.CURSOR_SIZENESW);
282 break;
283 case DWT.RIGHT | DWT.UP:
284 newCursor = new Cursor(display, DWT.CURSOR_SIZENESW);
285 break;
286 default:
287 newCursor = new Cursor(display, DWT.CURSOR_SIZEALL);
288 break;
289 }
290 newCursor.handle.set();
291 if (resizeCursor !is null) {
292 resizeCursor.dispose ();
293 }
294 resizeCursor = newCursor;
295 }
296
297 return new Point (newX, newY);
298 }
299
300 static int checkStyle (int style) {
301 if ((style & (DWT.LEFT | DWT.RIGHT | DWT.UP | DWT.DOWN)) is 0) {
302 style |= DWT.LEFT | DWT.RIGHT | DWT.UP | DWT.DOWN;
303 }
304 return style;
305 }
306
307 /**
308 * Stops displaying the tracker rectangles. Note that this is not considered
309 * to be a cancelation by the user.
310 *
311 * @exception DWTException <ul>
312 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
313 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
314 * </ul>
315 */
316 public void close () {
317 checkWidget ();
318 tracking = false;
319 }
320 Rectangle computeBounds () {
321 if (rectangles.length is 0) return null;
322 int xMin = rectangles [0].x;
323 int yMin = rectangles [0].y;
324 int xMax = rectangles [0].x + rectangles [0].width;
325 int yMax = rectangles [0].y + rectangles [0].height;
326
327 for (int i = 1; i < rectangles.length; i++) {
328 if (rectangles [i].x < xMin) xMin = rectangles [i].x;
329 if (rectangles [i].y < yMin) yMin = rectangles [i].y;
330 int rectRight = rectangles [i].x + rectangles [i].width;
331 if (rectRight > xMax) xMax = rectRight;
332 int rectBottom = rectangles [i].y + rectangles [i].height;
333 if (rectBottom > yMax) yMax = rectBottom;
334 }
335
336 return new Rectangle (xMin, yMin, xMax - xMin, yMax - yMin);
337 }
338
339 Rectangle [] computeProportions (Rectangle [] rects) {
340 Rectangle [] result = new Rectangle [rects.length];
341 bounds = computeBounds ();
342 if (bounds !is null) {
343 for (int i = 0; i < rects.length; i++) {
344 int x = 0, y = 0, width = 0, height = 0;
345 if (bounds.width !is 0) {
346 x = (rects [i].x - bounds.x) * 100 / bounds.width;
347 width = rects [i].width * 100 / bounds.width;
348 } else {
349 width = 100;
350 }
351 if (bounds.height !is 0) {
352 y = (rects [i].y - bounds.y) * 100 / bounds.height;
353 height = rects [i].height * 100 / bounds.height;
354 } else {
355 height = 100;
356 }
357 result [i] = new Rectangle (x, y, width, height);
358 }
359 }
360 return result;
361 }
362
363 void drawRectangles (NSWindow window, Rectangle [] rects, bool erase) {
364 NSRect frame = window.frame();
365 NSGraphicsContext context = window.graphicsContext();
366 NSGraphicsContext.setCurrentContext(context);
367 NSAffineTransform transform = NSAffineTransform.transform();
368 context.saveGraphicsState();
369 transform.scaleXBy(1, -1);
370 transform.translateXBy(0, -frame.height);
371 transform.concat();
372 Point parentOrigin;
373 if (parent !is null) {
374 parentOrigin = display.map (parent, null, 0, 0);
375 } else {
376 parentOrigin = new Point (0, 0);
377 }
378 context.setCompositingOperation(erase ? OS.NSCompositeClear : OS.NSCompositeSourceOver);
379 for (int i=0; i<rects.length; i++) {
380 Rectangle rect = rects [i];
381 frame.x = rect.x + parentOrigin.x;
382 frame.y = rect.y + parentOrigin.y;
383 frame.width = rect.width;
384 frame.height = rect.height;
385 if (erase) {
386 frame.width++;
387 frame.height++;
388 NSBezierPath.fillRect(frame);
389 } else {
390 frame.x += 0.5f;
391 frame.y += 0.5f;
392 NSBezierPath.strokeRect(frame);
393 }
394 }
395 context.flushGraphics();
396 context.restoreGraphicsState();
397 }
398
399 /**
400 * Returns the bounds that are being drawn, expressed relative to the parent
401 * widget. If the parent is a <code>Display</code> then these are screen
402 * coordinates.
403 *
404 * @return the bounds of the Rectangles being drawn
405 *
406 * @exception DWTException <ul>
407 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
408 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
409 * </ul>
410 */
411 public Rectangle [] getRectangles () {
412 checkWidget();
413 Rectangle [] result = new Rectangle [rectangles.length];
414 for (int i = 0; i < rectangles.length; i++) {
415 Rectangle current = rectangles [i];
416 result [i] = new Rectangle (current.x, current.y, current.width, current.height);
417 }
418 return result;
419 }
420
421 /**
422 * Returns <code>true</code> if the rectangles are drawn with a stippled line, <code>false</code> otherwise.
423 *
424 * @return the stippled effect of the rectangles
425 *
426 * @exception DWTException <ul>
427 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
428 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
429 * </ul>
430 */
431 public bool getStippled () {
432 checkWidget ();
433 return stippled;
434 }
435
436 void mouse (NSEvent nsEvent) {
437 NSPoint location = nsEvent.locationInWindow();
438 if (parent !is null) {
439 location = parent.view.convertPoint_toView_(location, parent.view);
440 } else {
441 NSWindow eventWindow = nsEvent.window();
442 location = eventWindow.convertBaseToScreen(location);
443 location.y = eventWindow.screen().frame().height - location.y;
444 }
445 int newX = (int)location.x, newY = (int)location.y;
446 if (newX !is oldX || newY !is oldY) {
447 Rectangle [] oldRectangles = rectangles;
448 Rectangle [] rectsToErase = new Rectangle [rectangles.length];
449 for (int i = 0; i < rectangles.length; i++) {
450 Rectangle current = rectangles [i];
451 rectsToErase [i] = new Rectangle (current.x, current.y, current.width, current.height);
452 }
453 Event event = new Event ();
454 event.x = newX;
455 event.y = newY;
456 if ((style & DWT.RESIZE) !is 0) {
457 bool orientationInit = resizeRectangles (newX - oldX, newY - oldY);
458 inEvent = true;
459 sendEvent (DWT.Resize, event);
460 inEvent = false;
461 /*
462 * It is possible (but unlikely), that application
463 * code could have disposed the widget in the move
464 * event. If this happens, return false to indicate
465 * that the tracking has failed.
466 */
467 if (isDisposed ()) {
468 cancelled = true;
469 return;
470 }
471 bool draw = false;
472 /*
473 * It is possible that application code could have
474 * changed the rectangles in the resize event. If this
475 * happens then only redraw the tracker if the rectangle
476 * values have changed.
477 */
478 if (rectangles !is oldRectangles) {
479 int length = rectangles.length;
480 if (length !is rectsToErase.length) {
481 draw = true;
482 } else {
483 for (int i = 0; i < length; i++) {
484 if (!rectangles [i].equals (rectsToErase [i])) {
485 draw = true;
486 break;
487 }
488 }
489 }
490 }
491 else {
492 draw = true;
493 }
494 if (draw) {
495 drawRectangles (window, rectsToErase, true);
496 drawRectangles (window, rectangles, false);
497 }
498 Point cursorPos = adjustResizeCursor (orientationInit);
499 if (cursorPos !is null) {
500 newX = cursorPos.x;
501 newY = cursorPos.y;
502 }
503 } else {
504 moveRectangles (newX - oldX, newY - oldY);
505 inEvent = true;
506 sendEvent (DWT.Move, event);
507 inEvent = false;
508 /*
509 * It is possible (but unlikely), that application
510 * code could have disposed the widget in the move
511 * event. If this happens, return false to indicate
512 * that the tracking has failed.
513 */
514 if (isDisposed ()) {
515 cancelled = true;
516 return;
517 }
518 bool draw = false;
519 /*
520 * It is possible that application code could have
521 * changed the rectangles in the move event. If this
522 * happens then only redraw the tracker if the rectangle
523 * values have changed.
524 */
525 if (rectangles !is oldRectangles) {
526 int length = rectangles.length;
527 if (length !is rectsToErase.length) {
528 draw = true;
529 } else {
530 for (int i = 0; i < length; i++) {
531 if (!rectangles [i].equals (rectsToErase [i])) {
532 draw = true;
533 break;
534 }
535 }
536 }
537 } else {
538 draw = true;
539 }
540 if (draw) {
541 drawRectangles (window, rectsToErase, true);
542 drawRectangles (window, rectangles, false);
543 }
544 }
545 oldX = newX; oldY = newY;
546 }
547 switch (nsEvent.type()) {
548 case OS.NSLeftMouseUp:
549 case OS.NSRightMouseUp:
550 case OS.NSOtherMouseUp:
551 tracking = false;
552 }
553 }
554
555 void key (NSEvent nsEvent) {
556 //TODO send event
557 // if (!sendKeyEvent (DWT.KeyDown, theEvent)) return OS.noErr;
558 int modifierFlags = nsEvent.modifierFlags();
559 int stepSize = (modifierFlags & OS.NSControlKeyMask) !is 0 ? STEPSIZE_SMALL : STEPSIZE_LARGE;
560 int xChange = 0, yChange = 0;
561 switch (nsEvent.keyCode()) {
562 case 53: /* Esc */
563 cancelled = true;
564 tracking = false;
565 break;
566 case 76: /* KP Enter */
567 case 36: /* Return */
568 tracking = false;
569 break;
570 case 123: /* Left arrow */
571 xChange = -stepSize;
572 break;
573 case 124: /* Right arrow */
574 xChange = stepSize;
575 break;
576 case 126: /* Up arrow */
577 yChange = -stepSize;
578 break;
579 case 125: /* Down arrow */
580 yChange = stepSize;
581 break;
582 }
583 if (xChange !is 0 || yChange !is 0) {
584 Rectangle [] oldRectangles = rectangles;
585 Rectangle [] rectsToErase = new Rectangle [rectangles.length];
586 for (int i = 0; i < rectangles.length; i++) {
587 Rectangle current = rectangles [i];
588 rectsToErase [i] = new Rectangle (current.x, current.y, current.width, current.height);
589 }
590 Event event = new Event ();
591 int newX = oldX + xChange;
592 int newY = oldY + yChange;
593 event.x = newX;
594 event.y = newY;
595 Point cursorPos;
596 if ((style & DWT.RESIZE) !is 0) {
597 resizeRectangles (xChange, yChange);
598 inEvent = true;
599 sendEvent (DWT.Resize, event);
600 inEvent = false;
601 /*
602 * It is possible (but unlikely) that application
603 * code could have disposed the widget in the move
604 * event. If this happens return false to indicate
605 * that the tracking has failed.
606 */
607 if (isDisposed ()) {
608 cancelled = true;
609 return;
610 }
611 bool draw = false;
612 /*
613 * It is possible that application code could have
614 * changed the rectangles in the resize event. If this
615 * happens then only redraw the tracker if the rectangle
616 * values have changed.
617 */
618 if (rectangles !is oldRectangles) {
619 int length = rectangles.length;
620 if (length !is rectsToErase.length) {
621 draw = true;
622 } else {
623 for (int i = 0; i < length; i++) {
624 if (!rectangles [i].equals (rectsToErase [i])) {
625 draw = true;
626 break;
627 }
628 }
629 }
630 } else {
631 draw = true;
632 }
633 if (draw) {
634 drawRectangles (window, rectsToErase, true);
635 drawRectangles (window, rectangles, false);
636 }
637 cursorPos = adjustResizeCursor (true);
638 } else {
639 moveRectangles (xChange, yChange);
640 inEvent = true;
641 sendEvent (DWT.Move, event);
642 inEvent = false;
643 /*
644 * It is possible (but unlikely) that application
645 * code could have disposed the widget in the move
646 * event. If this happens return false to indicate
647 * that the tracking has failed.
648 */
649 if (isDisposed ()) {
650 cancelled = true;
651 return;
652 }
653 bool draw = false;
654 /*
655 * It is possible that application code could have
656 * changed the rectangles in the move event. If this
657 * happens then only redraw the tracker if the rectangle
658 * values have changed.
659 */
660 if (rectangles !is oldRectangles) {
661 int length = rectangles.length;
662 if (length !is rectsToErase.length) {
663 draw = true;
664 } else {
665 for (int i = 0; i < length; i++) {
666 if (!rectangles [i].equals (rectsToErase [i])) {
667 draw = true;
668 break;
669 }
670 }
671 }
672 } else {
673 draw = true;
674 }
675 if (draw) {
676 drawRectangles (window, rectsToErase, true);
677 drawRectangles (window, rectangles, false);
678 }
679 cursorPos = adjustMoveCursor ();
680 }
681 if (cursorPos !is null) {
682 oldX = cursorPos.x;
683 oldY = cursorPos.y;
684 }
685 }
686 }
687
688 void moveRectangles (int xChange, int yChange) {
689 if (bounds is null) return;
690 if (xChange < 0 && ((style & DWT.LEFT) is 0)) xChange = 0;
691 if (xChange > 0 && ((style & DWT.RIGHT) is 0)) xChange = 0;
692 if (yChange < 0 && ((style & DWT.UP) is 0)) yChange = 0;
693 if (yChange > 0 && ((style & DWT.DOWN) is 0)) yChange = 0;
694 if (xChange is 0 && yChange is 0) return;
695 bounds.x += xChange; bounds.y += yChange;
696 for (int i = 0; i < rectangles.length; i++) {
697 rectangles [i].x += xChange;
698 rectangles [i].y += yChange;
699 }
700 }
701
702 /**
703 * Displays the Tracker rectangles for manipulation by the user. Returns when
704 * the user has either finished manipulating the rectangles or has cancelled the
705 * Tracker.
706 *
707 * @return <code>true</code> if the user did not cancel the Tracker, <code>false</code> otherwise
708 *
709 * @exception DWTException <ul>
710 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
711 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
712 * </ul>
713 */
714 public bool open () {
715 checkWidget ();
716 cancelled = false;
717 tracking = true;
718 window = (NSWindow)new NSWindow().alloc();
719 NSRect frame = NSScreen.mainScreen().frame();
720 window = window.initWithContentRect_styleMask_backing_defer_(frame, OS.NSBorderlessWindowMask, OS.NSBackingStoreBuffered, false);
721 window.setOpaque(false);
722 window.setContentView(null);
723 NSGraphicsContext context = window.graphicsContext();
724 NSGraphicsContext.setCurrentContext(context);
725 context.setCompositingOperation(OS.NSCompositeClear);
726 NSBezierPath.fillRect(frame);
727 window.orderFrontRegardless();
728
729 drawRectangles (window, rectangles, false);
730
731 /*
732 * If exactly one of UP/DOWN is specified as a style then set the cursor
733 * orientation accordingly (the same is done for LEFT/RIGHT styles below).
734 */
735 int vStyle = style & (DWT.UP | DWT.DOWN);
736 if (vStyle is DWT.UP || vStyle is DWT.DOWN) {
737 cursorOrientation |= vStyle;
738 }
739 int hStyle = style & (DWT.LEFT | DWT.RIGHT);
740 if (hStyle is DWT.LEFT || hStyle is DWT.RIGHT) {
741 cursorOrientation |= hStyle;
742 }
743
744 Point cursorPos;
745 bool down = false;
746 NSApplication application = NSApplication.sharedApplication();
747 NSEvent currentEvent = application.currentEvent();
748 switch (currentEvent.type()) {
749 case OS.NSLeftMouseDown:
750 case OS.NSRightMouseDown:
751 case OS.NSOtherMouseDown:
752 down = true;
753 }
754 if (down) {
755 cursorPos = display.getCursorLocation();
756 } else {
757 if ((style & DWT.RESIZE) !is 0) {
758 cursorPos = adjustResizeCursor (true);
759 } else {
760 cursorPos = adjustMoveCursor ();
761 }
762 }
763 if (cursorPos !is null) {
764 oldX = cursorPos.x;
765 oldY = cursorPos.y;
766 }
767
768 /* Tracker behaves like a Dialog with its own OS event loop. */
769 while (tracking && !cancelled) {
770 NSAutoreleasePool pool = (NSAutoreleasePool)new NSAutoreleasePool().alloc().init();
771 NSEvent event = application.nextEventMatchingMask(0, NSDate.distantFuture(), OS.NSDefaultRunLoopMode, true);
772 if (event is null) continue;
773 int type = event.type();
774 switch (type) {
775 case OS.NSLeftMouseUp:
776 case OS.NSRightMouseUp:
777 case OS.NSOtherMouseUp:
778 case OS.NSMouseMoved:
779 case OS.NSLeftMouseDragged:
780 case OS.NSRightMouseDragged:
781 case OS.NSOtherMouseDragged:
782 mouse(event);
783 break;
784 case OS.NSKeyDown:
785 // case OS.NSKeyUp:
786 case OS.NSFlagsChanged:
787 key(event);
788 break;
789 }
790 /*
791 * Don't dispatch mouse and key events in general, EXCEPT once this
792 * tracker has finished its work.
793 */
794 bool dispatch = true;
795 if (!(tracking && !cancelled)) {
796 switch (type) {
797 case OS.NSLeftMouseDown:
798 case OS.NSLeftMouseUp:
799 case OS.NSRightMouseDown:
800 case OS.NSRightMouseUp:
801 case OS.NSOtherMouseDown:
802 case OS.NSOtherMouseUp:
803 case OS.NSMouseMoved:
804 case OS.NSLeftMouseDragged:
805 case OS.NSRightMouseDragged:
806 case OS.NSOtherMouseDragged:
807 case OS.NSMouseEntered:
808 case OS.NSMouseExited:
809 case OS.NSKeyDown:
810 case OS.NSKeyUp:
811 case OS.NSFlagsChanged:
812 dispatch = false;
813 }
814 }
815 if (dispatch) application.sendEvent(event);
816 if (clientCursor !is null && resizeCursor is null) {
817 clientCursor.handle.set();
818 }
819 pool.release();
820 }
821 if (!isDisposed()) {
822 drawRectangles (window, rectangles, true);
823 }
824 if (window !is null) window.close();
825 tracking = false;
826 window = null;
827 return !cancelled;
828 }
829
830 void releaseWidget () {
831 super.releaseWidget ();
832 parent = null;
833 rectangles = proportions = null;
834 bounds = null;
835 }
836
837 /**
838 * Removes the listener from the collection of listeners who will
839 * be notified when the control is moved or resized.
840 *
841 * @param listener the listener which should no longer be notified
842 *
843 * @exception IllegalArgumentException <ul>
844 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
845 * </ul>
846 * @exception DWTException <ul>
847 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
848 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
849 * </ul>
850 *
851 * @see ControlListener
852 * @see #addControlListener
853 */
854 public void removeControlListener (ControlListener listener) {
855 checkWidget ();
856 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
857 if (eventTable is null) return;
858 eventTable.unhook (DWT.Resize, listener);
859 eventTable.unhook (DWT.Move, listener);
860 }
861
862 /**
863 * Removes the listener from the collection of listeners who will
864 * be notified when keys are pressed and released on the system keyboard.
865 *
866 * @param listener the listener which should no longer be notified
867 *
868 * @exception IllegalArgumentException <ul>
869 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
870 * </ul>
871 * @exception DWTException <ul>
872 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
873 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
874 * </ul>
875 *
876 * @see KeyListener
877 * @see #addKeyListener
878 */
879 public void removeKeyListener(KeyListener listener) {
880 checkWidget();
881 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
882 if (eventTable is null) return;
883 eventTable.unhook(DWT.KeyUp, listener);
884 eventTable.unhook(DWT.KeyDown, listener);
885 }
886
887 /*
888 * Returns true if the pointer's orientation was initialized in some dimension,
889 * and false otherwise.
890 */
891 bool resizeRectangles (int xChange, int yChange) {
892 if (bounds is null) return false;
893 bool orientationInit = false;
894 /*
895 * If the cursor orientation has not been set in the orientation of
896 * this change then try to set it here.
897 */
898 if (xChange < 0 && ((style & DWT.LEFT) !is 0) && ((cursorOrientation & DWT.RIGHT) is 0)) {
899 if ((cursorOrientation & DWT.LEFT) is 0) {
900 cursorOrientation |= DWT.LEFT;
901 orientationInit = true;
902 }
903 }
904 if (xChange > 0 && ((style & DWT.RIGHT) !is 0) && ((cursorOrientation & DWT.LEFT) is 0)) {
905 if ((cursorOrientation & DWT.RIGHT) is 0) {
906 cursorOrientation |= DWT.RIGHT;
907 orientationInit = true;
908 }
909 }
910 if (yChange < 0 && ((style & DWT.UP) !is 0) && ((cursorOrientation & DWT.DOWN) is 0)) {
911 if ((cursorOrientation & DWT.UP) is 0) {
912 cursorOrientation |= DWT.UP;
913 orientationInit = true;
914 }
915 }
916 if (yChange > 0 && ((style & DWT.DOWN) !is 0) && ((cursorOrientation & DWT.UP) is 0)) {
917 if ((cursorOrientation & DWT.DOWN) is 0) {
918 cursorOrientation |= DWT.DOWN;
919 orientationInit = true;
920 }
921 }
922
923 /*
924 * If the bounds will flip about the x or y axis then apply the adjustment
925 * up to the axis (ie.- where bounds width/height becomes 0), change the
926 * cursor's orientation accordingly, and flip each Rectangle's origin (only
927 * necessary for > 1 Rectangles)
928 */
929 if ((cursorOrientation & DWT.LEFT) !is 0) {
930 if (xChange > bounds.width) {
931 if ((style & DWT.RIGHT) is 0) return orientationInit;
932 cursorOrientation |= DWT.RIGHT;
933 cursorOrientation &= ~DWT.LEFT;
934 bounds.x += bounds.width;
935 xChange -= bounds.width;
936 bounds.width = 0;
937 if (proportions.length > 1) {
938 for (int i = 0; i < proportions.length; i++) {
939 Rectangle proportion = proportions [i];
940 proportion.x = 100 - proportion.x - proportion.width;
941 }
942 }
943 }
944 } else if ((cursorOrientation & DWT.RIGHT) !is 0) {
945 if (bounds.width < -xChange) {
946 if ((style & DWT.LEFT) is 0) return orientationInit;
947 cursorOrientation |= DWT.LEFT;
948 cursorOrientation &= ~DWT.RIGHT;
949 xChange += bounds.width;
950 bounds.width = 0;
951 if (proportions.length > 1) {
952 for (int i = 0; i < proportions.length; i++) {
953 Rectangle proportion = proportions [i];
954 proportion.x = 100 - proportion.x - proportion.width;
955 }
956 }
957 }
958 }
959 if ((cursorOrientation & DWT.UP) !is 0) {
960 if (yChange > bounds.height) {
961 if ((style & DWT.DOWN) is 0) return orientationInit;
962 cursorOrientation |= DWT.DOWN;
963 cursorOrientation &= ~DWT.UP;
964 bounds.y += bounds.height;
965 yChange -= bounds.height;
966 bounds.height = 0;
967 if (proportions.length > 1) {
968 for (int i = 0; i < proportions.length; i++) {
969 Rectangle proportion = proportions [i];
970 proportion.y = 100 - proportion.y - proportion.height;
971 }
972 }
973 }
974 } else if ((cursorOrientation & DWT.DOWN) !is 0) {
975 if (bounds.height < -yChange) {
976 if ((style & DWT.UP) is 0) return orientationInit;
977 cursorOrientation |= DWT.UP;
978 cursorOrientation &= ~DWT.DOWN;
979 yChange += bounds.height;
980 bounds.height = 0;
981 if (proportions.length > 1) {
982 for (int i = 0; i < proportions.length; i++) {
983 Rectangle proportion = proportions [i];
984 proportion.y = 100 - proportion.y - proportion.height;
985 }
986 }
987 }
988 }
989
990 // apply the bounds adjustment
991 if ((cursorOrientation & DWT.LEFT) !is 0) {
992 bounds.x += xChange;
993 bounds.width -= xChange;
994 } else if ((cursorOrientation & DWT.RIGHT) !is 0) {
995 bounds.width += xChange;
996 }
997 if ((cursorOrientation & DWT.UP) !is 0) {
998 bounds.y += yChange;
999 bounds.height -= yChange;
1000 } else if ((cursorOrientation & DWT.DOWN) !is 0) {
1001 bounds.height += yChange;
1002 }
1003
1004 Rectangle [] newRects = new Rectangle [rectangles.length];
1005 for (int i = 0; i < rectangles.length; i++) {
1006 Rectangle proportion = proportions[i];
1007 newRects[i] = new Rectangle (
1008 proportion.x * bounds.width / 100 + bounds.x,
1009 proportion.y * bounds.height / 100 + bounds.y,
1010 proportion.width * bounds.width / 100,
1011 proportion.height * bounds.height / 100);
1012 }
1013 rectangles = newRects;
1014 return orientationInit;
1015 }
1016
1017 /**
1018 * Sets the <code>Cursor</code> of the Tracker. If this cursor is <code>null</code>
1019 * then the cursor reverts to the default.
1020 *
1021 * @param newCursor the new <code>Cursor</code> to display
1022 *
1023 * @exception DWTException <ul>
1024 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1025 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1026 * </ul>
1027 */
1028 public void setCursor (Cursor newCursor) {
1029 checkWidget ();
1030 clientCursor = newCursor;
1031 if (newCursor !is null) {
1032 if (inEvent) newCursor.handle.set();
1033 }
1034 }
1035
1036 /**
1037 * Specifies the rectangles that should be drawn, expressed relative to the parent
1038 * widget. If the parent is a Display then these are screen coordinates.
1039 *
1040 * @param rectangles the bounds of the rectangles to be drawn
1041 *
1042 * @exception IllegalArgumentException <ul>
1043 * <li>ERROR_NULL_ARGUMENT - if the set of rectangles is null or contains a null rectangle</li>
1044 * </ul>
1045 * @exception DWTException <ul>
1046 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1047 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1048 * </ul>
1049 */
1050 public void setRectangles (Rectangle [] rectangles) {
1051 checkWidget ();
1052 if (rectangles is null) error (DWT.ERROR_NULL_ARGUMENT);
1053 int length = rectangles.length;
1054 this.rectangles = new Rectangle [length];
1055 for (int i = 0; i < length; i++) {
1056 Rectangle current = rectangles [i];
1057 if (current is null) error (DWT.ERROR_NULL_ARGUMENT);
1058 this.rectangles [i] = new Rectangle (current.x, current.y, current.width, current.height);
1059 }
1060 proportions = computeProportions (rectangles);
1061 }
1062
1063 /**
1064 * Changes the appearance of the line used to draw the rectangles.
1065 *
1066 * @param stippled <code>true</code> if rectangle should appear stippled
1067 *
1068 * @exception DWTException <ul>
1069 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
1070 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
1071 * </ul>
1072 */
1073 public void setStippled (bool stippled) {
1074 checkWidget ();
1075 this.stippled = stippled;
1076 }
1077
1078 }