comparison dwt/widgets/Tracker.d @ 123:63a09873578e

Fixed compile errors
author Jacob Carlborg <doob@me.com>
date Thu, 15 Jan 2009 23:08:54 +0100
parents c7f7f4d7091a
children
comparison
equal deleted inserted replaced
122:2e671fa40eec 123:63a09873578e
13 *******************************************************************************/ 13 *******************************************************************************/
14 module dwt.widgets.Tracker; 14 module dwt.widgets.Tracker;
15 15
16 import dwt.dwthelper.utils; 16 import dwt.dwthelper.utils;
17 17
18 18
19 import dwt.DWT; 19 import dwt.DWT;
20 import dwt.DWTException; 20 import dwt.DWTException;
21 import dwt.events.ControlListener; 21 import dwt.events.ControlListener;
22 import dwt.events.KeyListener; 22 import dwt.events.KeyListener;
23 import dwt.graphics.Cursor; 23 import dwt.graphics.Cursor;
77 Rectangle bounds; 77 Rectangle bounds;
78 int cursorOrientation = DWT.NONE; 78 int cursorOrientation = DWT.NONE;
79 bool inEvent = false; 79 bool inEvent = false;
80 NSWindow window; 80 NSWindow window;
81 int oldX, oldY; 81 int oldX, oldY;
82 82
83 /* 83 /*
84 * The following values mirror step sizes on Windows 84 * The following values mirror step sizes on Windows
85 */ 85 */
86 const static int STEPSIZE_SMALL = 1; 86 const static int STEPSIZE_SMALL = 1;
87 const static int STEPSIZE_LARGE = 9; 87 const static int STEPSIZE_LARGE = 9;
88 88
89 /** 89 /**
90 * Constructs a new instance of this class given its parent 90 * Constructs a new instance of this class given its parent
244 } 244 }
245 245
246 Point adjustResizeCursor (bool movePointer) { 246 Point adjustResizeCursor (bool movePointer) {
247 if (bounds is null) return null; 247 if (bounds is null) return null;
248 int newX, newY; 248 int newX, newY;
249 249
250 if ((cursorOrientation & DWT.LEFT) !is 0) { 250 if ((cursorOrientation & DWT.LEFT) !is 0) {
251 newX = bounds.x; 251 newX = bounds.x;
252 } else if ((cursorOrientation & DWT.RIGHT) !is 0) { 252 } else if ((cursorOrientation & DWT.RIGHT) !is 0) {
253 newX = bounds.x + bounds.width; 253 newX = bounds.x + bounds.width;
254 } else { 254 } else {
255 newX = bounds.x + bounds.width / 2; 255 newX = bounds.x + bounds.width / 2;
256 } 256 }
257 257
258 if ((cursorOrientation & DWT.UP) !is 0) { 258 if ((cursorOrientation & DWT.UP) !is 0) {
259 newY = bounds.y; 259 newY = bounds.y;
260 } else if ((cursorOrientation & DWT.DOWN) !is 0) { 260 } else if ((cursorOrientation & DWT.DOWN) !is 0) {
261 newY = bounds.y + bounds.height; 261 newY = bounds.y + bounds.height;
262 } else { 262 } else {
263 newY = bounds.y + bounds.height / 2; 263 newY = bounds.y + bounds.height / 2;
264 } 264 }
265 265
266 /* 266 /*
267 * Convert to screen coordinates if needed 267 * Convert to screen coordinates if needed
268 */ 268 */
269 if (parent !is null) { 269 if (parent !is null) {
270 Point pt = parent.toDisplay (newX, newY); 270 Point pt = parent.toDisplay (newX, newY);
272 newY = pt.y; 272 newY = pt.y;
273 } 273 }
274 if (movePointer) { 274 if (movePointer) {
275 display.setCursorLocation(newX, newY); 275 display.setCursorLocation(newX, newY);
276 } 276 }
277 277
278 /* 278 /*
279 * If the client has not provided a custom cursor then determine 279 * If the client has not provided a custom cursor then determine
280 * the appropriate resize cursor. 280 * the appropriate resize cursor.
281 */ 281 */
282 if (clientCursor is null) { 282 if (clientCursor is null) {
283 Cursor newCursor = null; 283 Cursor newCursor = null;
284 switch (cursorOrientation) { 284 switch (cursorOrientation) {
285 case DWT.UP: 285 case DWT.UP:
286 newCursor = new Cursor(display, DWT.CURSOR_SIZENS); 286 newCursor = new Cursor(display, DWT.CURSOR_SIZENS);
314 if (resizeCursor !is null) { 314 if (resizeCursor !is null) {
315 resizeCursor.dispose (); 315 resizeCursor.dispose ();
316 } 316 }
317 resizeCursor = newCursor; 317 resizeCursor = newCursor;
318 } 318 }
319 319
320 return new Point (newX, newY); 320 return new Point (newX, newY);
321 } 321 }
322 322
323 static int checkStyle (int style) { 323 static int checkStyle (int style) {
324 if ((style & (DWT.LEFT | DWT.RIGHT | DWT.UP | DWT.DOWN)) is 0) { 324 if ((style & (DWT.LEFT | DWT.RIGHT | DWT.UP | DWT.DOWN)) is 0) {
480 bool orientationInit = resizeRectangles (newX - oldX, newY - oldY); 480 bool orientationInit = resizeRectangles (newX - oldX, newY - oldY);
481 inEvent = true; 481 inEvent = true;
482 sendEvent (DWT.Resize, event); 482 sendEvent (DWT.Resize, event);
483 inEvent = false; 483 inEvent = false;
484 /* 484 /*
485 * It is possible (but unlikely), that application 485 * It is possible (but unlikely), that application
486 * code could have disposed the widget in the move 486 * code could have disposed the widget in the move
487 * event. If this happens, return false to indicate 487 * event. If this happens, return false to indicate
488 * that the tracking has failed. 488 * that the tracking has failed.
489 */ 489 */
490 if (isDisposed ()) { 490 if (isDisposed ()) {
491 cancelled = true; 491 cancelled = true;
492 return; 492 return;
493 } 493 }
494 bool draw = false; 494 bool draw = false;
527 moveRectangles (newX - oldX, newY - oldY); 527 moveRectangles (newX - oldX, newY - oldY);
528 inEvent = true; 528 inEvent = true;
529 sendEvent (DWT.Move, event); 529 sendEvent (DWT.Move, event);
530 inEvent = false; 530 inEvent = false;
531 /* 531 /*
532 * It is possible (but unlikely), that application 532 * It is possible (but unlikely), that application
533 * code could have disposed the widget in the move 533 * code could have disposed the widget in the move
534 * event. If this happens, return false to indicate 534 * event. If this happens, return false to indicate
535 * that the tracking has failed. 535 * that the tracking has failed.
536 */ 536 */
537 if (isDisposed ()) { 537 if (isDisposed ()) {
538 cancelled = true; 538 cancelled = true;
539 return; 539 return;
540 } 540 }
541 bool draw = false; 541 bool draw = false;
576 } 576 }
577 } 577 }
578 578
579 void key (NSEvent nsEvent) { 579 void key (NSEvent nsEvent) {
580 //TODO send event 580 //TODO send event
581 // if (!sendKeyEvent (DWT.KeyDown, theEvent)) return OS.noErr; 581 // if (!sendKeyEvent (DWT.KeyDown, theEvent)) return OS.noErr;
582 NSUInteger modifierFlags = nsEvent.modifierFlags(); 582 NSUInteger modifierFlags = nsEvent.modifierFlags();
583 int stepSize = (modifierFlags & OS.NSControlKeyMask) !is 0 ? STEPSIZE_SMALL : STEPSIZE_LARGE; 583 int stepSize = (modifierFlags & OS.NSControlKeyMask) !is 0 ? STEPSIZE_SMALL : STEPSIZE_LARGE;
584 int xChange = 0, yChange = 0; 584 int xChange = 0, yChange = 0;
585 switch (nsEvent.keyCode()) { 585 switch (nsEvent.keyCode()) {
586 case 53: /* Esc */ 586 case 53: /* Esc */
622 resizeRectangles (xChange, yChange); 622 resizeRectangles (xChange, yChange);
623 inEvent = true; 623 inEvent = true;
624 sendEvent (DWT.Resize, event); 624 sendEvent (DWT.Resize, event);
625 inEvent = false; 625 inEvent = false;
626 /* 626 /*
627 * It is possible (but unlikely) that application 627 * It is possible (but unlikely) that application
628 * code could have disposed the widget in the move 628 * code could have disposed the widget in the move
629 * event. If this happens return false to indicate 629 * event. If this happens return false to indicate
630 * that the tracking has failed. 630 * that the tracking has failed.
631 */ 631 */
632 if (isDisposed ()) { 632 if (isDisposed ()) {
633 cancelled = true; 633 cancelled = true;
634 return; 634 return;
635 } 635 }
636 bool draw = false; 636 bool draw = false;
664 moveRectangles (xChange, yChange); 664 moveRectangles (xChange, yChange);
665 inEvent = true; 665 inEvent = true;
666 sendEvent (DWT.Move, event); 666 sendEvent (DWT.Move, event);
667 inEvent = false; 667 inEvent = false;
668 /* 668 /*
669 * It is possible (but unlikely) that application 669 * It is possible (but unlikely) that application
670 * code could have disposed the widget in the move 670 * code could have disposed the widget in the move
671 * event. If this happens return false to indicate 671 * event. If this happens return false to indicate
672 * that the tracking has failed. 672 * that the tracking has failed.
673 */ 673 */
674 if (isDisposed ()) { 674 if (isDisposed ()) {
675 cancelled = true; 675 cancelled = true;
676 return; 676 return;
677 } 677 }
678 bool draw = false; 678 bool draw = false;
771 NSGraphicsContext.setCurrentContext(context); 771 NSGraphicsContext.setCurrentContext(context);
772 context.setCompositingOperation(OS.NSCompositeClear); 772 context.setCompositingOperation(OS.NSCompositeClear);
773 frame.x = frame.y = 0; 773 frame.x = frame.y = 0;
774 NSBezierPath.fillRect(frame); 774 NSBezierPath.fillRect(frame);
775 window.orderFrontRegardless(); 775 window.orderFrontRegardless();
776 776
777 drawRectangles (window, rectangles, false); 777 drawRectangles (window, rectangles, false);
778 778
779 /* 779 /*
780 * If exactly one of UP/DOWN is specified as a style then set the cursor 780 * If exactly one of UP/DOWN is specified as a style then set the cursor
781 * orientation accordingly (the same is done for LEFT/RIGHT styles below). 781 * orientation accordingly (the same is done for LEFT/RIGHT styles below).
782 */ 782 */
783 int vStyle = style & (DWT.UP | DWT.DOWN); 783 int vStyle = style & (DWT.UP | DWT.DOWN);
784 if (vStyle is DWT.UP || vStyle is DWT.DOWN) { 784 if (vStyle is DWT.UP || vStyle is DWT.DOWN) {
785 cursorOrientation |= vStyle; 785 cursorOrientation |= vStyle;
786 } 786 }
787 int hStyle = style & (DWT.LEFT | DWT.RIGHT); 787 int hStyle = style & (DWT.LEFT | DWT.RIGHT);
811 } 811 }
812 if (cursorPos !is null) { 812 if (cursorPos !is null) {
813 oldX = cursorPos.x; 813 oldX = cursorPos.x;
814 oldY = cursorPos.y; 814 oldY = cursorPos.y;
815 } 815 }
816 816
817 /* Tracker behaves like a Dialog with its own OS event loop. */ 817 /* Tracker behaves like a Dialog with its own OS event loop. */
818 while (tracking && !cancelled) { 818 while (tracking && !cancelled) {
819 NSAutoreleasePool pool = cast(NSAutoreleasePool)(new NSAutoreleasePool()).alloc().init(); 819 NSAutoreleasePool pool = cast(NSAutoreleasePool)(new NSAutoreleasePool()).alloc().init();
820 NSEvent event = application.nextEventMatchingMask(0, NSDate.distantFuture(), OS.NSDefaultRunLoopMode, true); 820 NSEvent event = application.nextEventMatchingMask(0, NSDate.distantFuture(), OS.NSDefaultRunLoopMode, true);
821 if (event is null) continue; 821 if (event is null) continue;
829 case OS.NSRightMouseDragged: 829 case OS.NSRightMouseDragged:
830 case OS.NSOtherMouseDragged: 830 case OS.NSOtherMouseDragged:
831 mouse(event); 831 mouse(event);
832 break; 832 break;
833 case OS.NSKeyDown: 833 case OS.NSKeyDown:
834 // case OS.NSKeyUp: 834 // case OS.NSKeyUp:
835 case OS.NSFlagsChanged: 835 case OS.NSFlagsChanged:
836 key(event); 836 key(event);
837 break; 837 break;
838 default: 838 default:
839 } 839 }
840 /* 840 /*
841 * Don't dispatch mouse and key events in general, EXCEPT once this 841 * Don't dispatch mouse and key events in general, EXCEPT once this
842 * tracker has finished its work. 842 * tracker has finished its work.
843 */ 843 */
844 bool dispatch = true; 844 bool dispatch = true;
845 if (!(tracking && !cancelled)) { 845 if (!(tracking && !cancelled)) {
846 switch (type) { 846 switch (type) {
847 case OS.NSLeftMouseDown: 847 case OS.NSLeftMouseDown:
848 case OS.NSLeftMouseUp: 848 case OS.NSLeftMouseUp:
941 */ 941 */
942 bool resizeRectangles (int xChange, int yChange) { 942 bool resizeRectangles (int xChange, int yChange) {
943 if (bounds is null) return false; 943 if (bounds is null) return false;
944 bool orientationInit = false; 944 bool orientationInit = false;
945 /* 945 /*
946 * If the cursor orientation has not been set in the orientation of 946 * If the cursor orientation has not been set in the orientation of
947 * this change then try to set it here. 947 * this change then try to set it here.
948 */ 948 */
949 if (xChange < 0 && ((style & DWT.LEFT) !is 0) && ((cursorOrientation & DWT.RIGHT) is 0)) { 949 if (xChange < 0 && ((style & DWT.LEFT) !is 0) && ((cursorOrientation & DWT.RIGHT) is 0)) {
950 if ((cursorOrientation & DWT.LEFT) is 0) { 950 if ((cursorOrientation & DWT.LEFT) is 0) {
951 cursorOrientation |= DWT.LEFT; 951 cursorOrientation |= DWT.LEFT;
952 orientationInit = true; 952 orientationInit = true;
953 } 953 }
1054 1054
1055 Rectangle [] newRects = new Rectangle [rectangles.length]; 1055 Rectangle [] newRects = new Rectangle [rectangles.length];
1056 for (int i = 0; i < rectangles.length; i++) { 1056 for (int i = 0; i < rectangles.length; i++) {
1057 Rectangle proportion = proportions[i]; 1057 Rectangle proportion = proportions[i];
1058 newRects[i] = new Rectangle ( 1058 newRects[i] = new Rectangle (
1059 proportion.x * bounds.width / 100 + bounds.x, 1059 proportion.x * bounds.width / 100 + bounds.x,
1060 proportion.y * bounds.height / 100 + bounds.y, 1060 proportion.y * bounds.height / 100 + bounds.y,
1061 proportion.width * bounds.width / 100, 1061 proportion.width * bounds.width / 100,
1062 proportion.height * bounds.height / 100); 1062 proportion.height * bounds.height / 100);
1063 } 1063 }
1064 rectangles = newRects; 1064 rectangles = newRects;
1065 return orientationInit; 1065 return orientationInit;
1066 } 1066 }
1067 1067