Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/widgets/Tracker.d @ 47:65761bc28ab2
swt linux again compilable for d1.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 27 Mar 2009 11:43:53 +0100 |
parents | f713da8bc051 |
children | ddbfe84d86df |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.widgets.Tracker; | |
14 | |
15 import java.lang.all; | |
16 | |
17 | |
18 | |
19 import org.eclipse.swt.SWT; | |
20 import org.eclipse.swt.SWTException; | |
21 import org.eclipse.swt.events.ControlListener; | |
22 import org.eclipse.swt.events.KeyListener; | |
23 import org.eclipse.swt.graphics.Cursor; | |
24 import org.eclipse.swt.graphics.Point; | |
25 import org.eclipse.swt.graphics.Rectangle; | |
26 import org.eclipse.swt.internal.gtk.OS; | |
27 import org.eclipse.swt.widgets.Widget; | |
28 import org.eclipse.swt.widgets.Composite; | |
29 import org.eclipse.swt.widgets.Display; | |
30 import org.eclipse.swt.widgets.TypedListener; | |
31 import org.eclipse.swt.widgets.Event; | |
32 | |
33 import tango.core.Exception; | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
25
diff
changeset
|
34 import java.lang.Thread; |
25 | 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 * @see <a href="http://www.eclipse.org/swt/snippets/#tracker">Tracker snippets</a> | |
56 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
57 */ | |
58 public class Tracker : Widget { | |
59 Composite parent; | |
60 Cursor cursor; | |
61 GdkCursor* lastCursor; | |
62 GdkDrawable* window; | |
63 bool tracking, cancelled, grabbed, stippled; | |
64 Rectangle [] rectangles, proportions; | |
65 Rectangle bounds; | |
66 int cursorOrientation = SWT.NONE; | |
67 int oldX, oldY; | |
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>SWT</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>SWT</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 SWTException <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 SWT#LEFT | |
97 * @see SWT#RIGHT | |
98 * @see SWT#UP | |
99 * @see SWT#DOWN | |
100 * @see SWT#RESIZE | |
101 * @see Widget#checkSubclass | |
102 * @see Widget#getStyle | |
103 */ | |
104 public this (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>SWT</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>SWT</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 SWT.</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 SWTException <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 SWT#LEFT | |
139 * @see SWT#RIGHT | |
140 * @see SWT#UP | |
141 * @see SWT#DOWN | |
142 */ | |
143 public this (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 (SWT.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 SWTException <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 (SWT.ERROR_NULL_ARGUMENT); | |
175 TypedListener typedListener = new TypedListener (listener); | |
176 addListener (SWT.Resize, typedListener); | |
177 addListener (SWT.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 SWTException <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 (SWT.ERROR_NULL_ARGUMENT); | |
202 TypedListener typedListener = new TypedListener (listener); | |
203 addListener(SWT.KeyUp,typedListener); | |
204 addListener(SWT.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 Point point = display.map (parent, null, newX, newY); | |
213 display.setCursorLocation (point); | |
214 | |
215 /* | |
216 * The call to XWarpPointer does not always place the pointer on the | |
217 * exact location that is specified, so do a query (below) to get the | |
218 * actual location of the pointer after it has been moved. | |
219 */ | |
220 int actualX, actualY, state; | |
221 OS.gdk_window_get_pointer (cast(GdkWindow*)window, &actualX, &actualY, &state); | |
222 return new Point (actualX, actualY); | |
223 } | |
224 | |
225 Point adjustResizeCursor () { | |
226 if (bounds is null) return null; | |
227 int newX, newY; | |
228 | |
229 if ((cursorOrientation & SWT.LEFT) !is 0) { | |
230 newX = bounds.x; | |
231 } else if ((cursorOrientation & SWT.RIGHT) !is 0) { | |
232 newX = bounds.x + bounds.width; | |
233 } else { | |
234 newX = bounds.x + bounds.width / 2; | |
235 } | |
236 | |
237 if ((cursorOrientation & SWT.UP) !is 0) { | |
238 newY = bounds.y; | |
239 } else if ((cursorOrientation & SWT.DOWN) !is 0) { | |
240 newY = bounds.y + bounds.height; | |
241 } else { | |
242 newY = bounds.y + bounds.height / 2; | |
243 } | |
244 | |
245 Point point = display.map (parent, null, newX, newY); | |
246 display.setCursorLocation (point); | |
247 | |
248 /* | |
249 * The call to XWarpPointer does not always place the pointer on the | |
250 * exact location that is specified, so do a query (below) to get the | |
251 * actual location of the pointer after it has been moved. | |
252 */ | |
253 int actualX, actualY, state; | |
254 OS.gdk_window_get_pointer (window, &actualX, &actualY, &state); | |
255 return new Point (actualX, actualY ); | |
256 } | |
257 | |
258 | |
259 /** | |
260 * Stops displaying the tracker rectangles. Note that this is not considered | |
261 * to be a cancelation by the user. | |
262 * | |
263 * @exception SWTException <ul> | |
264 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
265 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
266 * </ul> | |
267 */ | |
268 public void close () { | |
269 checkWidget(); | |
270 tracking = false; | |
271 } | |
272 | |
273 static int checkStyle (int style) { | |
274 if ((style & (SWT.LEFT | SWT.RIGHT | SWT.UP | SWT.DOWN)) is 0) { | |
275 style |= SWT.LEFT | SWT.RIGHT | SWT.UP | SWT.DOWN; | |
276 } | |
277 return style; | |
278 } | |
279 | |
280 Rectangle computeBounds () { | |
281 if (rectangles.length is 0) return null; | |
282 int xMin = rectangles [0].x; | |
283 int yMin = rectangles [0].y; | |
284 int xMax = rectangles [0].x + rectangles [0].width; | |
285 int yMax = rectangles [0].y + rectangles [0].height; | |
286 | |
287 for (int i = 1; i < rectangles.length; i++) { | |
288 if (rectangles [i].x < xMin) xMin = rectangles [i].x; | |
289 if (rectangles [i].y < yMin) yMin = rectangles [i].y; | |
290 int rectRight = rectangles [i].x + rectangles [i].width; | |
291 if (rectRight > xMax) xMax = rectRight; | |
292 int rectBottom = rectangles [i].y + rectangles [i].height; | |
293 if (rectBottom > yMax) yMax = rectBottom; | |
294 } | |
295 | |
296 return new Rectangle (xMin, yMin, xMax - xMin, yMax - yMin); | |
297 } | |
298 | |
299 Rectangle [] computeProportions (Rectangle [] rects) { | |
300 Rectangle [] result = new Rectangle [rects.length]; | |
301 bounds = computeBounds (); | |
302 if (bounds !is null) { | |
303 for (int i = 0; i < rects.length; i++) { | |
304 int x = 0, y = 0, width = 0, height = 0; | |
305 if (bounds.width !is 0) { | |
306 x = (rects [i].x - bounds.x) * 100 / bounds.width; | |
307 width = rects [i].width * 100 / bounds.width; | |
308 } else { | |
309 width = 100; | |
310 } | |
311 if (bounds.height !is 0) { | |
312 y = (rects [i].y - bounds.y) * 100 / bounds.height; | |
313 height = rects [i].height * 100 / bounds.height; | |
314 } else { | |
315 height = 100; | |
316 } | |
317 result [i] = new Rectangle (x, y, width, height); | |
318 } | |
319 } | |
320 return result; | |
321 } | |
322 | |
323 void drawRectangles (Rectangle [] rects) { | |
324 auto window = OS.GDK_ROOT_PARENT (); | |
325 if (parent !is null) { | |
326 window = OS.GTK_WIDGET_WINDOW (parent.paintHandle()); | |
327 } | |
328 if (window is null) return; | |
329 auto gc = OS.gdk_gc_new (window); | |
330 if (gc is null) return; | |
331 auto colormap = OS.gdk_colormap_get_system (); | |
332 GdkColor color; | |
333 OS.gdk_color_white (colormap, &color); | |
334 OS.gdk_gc_set_foreground (gc, &color); | |
335 OS.gdk_gc_set_subwindow (gc, OS.GDK_INCLUDE_INFERIORS); | |
336 OS.gdk_gc_set_function (gc, OS.GDK_XOR); | |
337 for (int i=0; i<rects.length; i++) { | |
338 Rectangle rect = rects [i]; | |
339 int x = rect.x; | |
340 if (parent !is null && (parent.style & SWT.MIRRORED) !is 0) x = parent.getClientWidth () - rect.width - x; | |
341 OS.gdk_draw_rectangle (window, gc, 0, x, rect.y, rect.width, rect.height); | |
342 } | |
343 OS.g_object_unref (gc); | |
344 } | |
345 | |
346 /** | |
347 * Returns the bounds that are being drawn, expressed relative to the parent | |
348 * widget. If the parent is a <code>Display</code> then these are screen | |
349 * coordinates. | |
350 * | |
351 * @return the bounds of the Rectangles being drawn | |
352 * | |
353 * @exception SWTException <ul> | |
354 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
355 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
356 * </ul> | |
357 */ | |
358 public Rectangle [] getRectangles () { | |
359 checkWidget(); | |
360 Rectangle [] result = new Rectangle [rectangles.length]; | |
361 for (int i = 0; i < rectangles.length; i++) { | |
362 Rectangle current = rectangles [i]; | |
363 result [i] = new Rectangle (current.x, current.y, current.width, current.height); | |
364 } | |
365 return result; | |
366 } | |
367 | |
368 /** | |
369 * Returns <code>true</code> if the rectangles are drawn with a stippled line, <code>false</code> otherwise. | |
370 * | |
371 * @return the stippled effect of the rectangles | |
372 * | |
373 * @exception SWTException <ul> | |
374 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
375 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
376 * </ul> | |
377 */ | |
378 public bool getStippled () { | |
379 checkWidget(); | |
380 return stippled; | |
381 } | |
382 | |
383 bool grab () { | |
384 auto cursor = this.cursor !is null ? this.cursor.handle : null; | |
385 int result = OS.gdk_pointer_grab (window, false, OS.GDK_POINTER_MOTION_MASK | OS.GDK_BUTTON_RELEASE_MASK, window, cursor, OS.GDK_CURRENT_TIME); | |
386 return result is OS.GDK_GRAB_SUCCESS; | |
387 } | |
388 | |
389 override int gtk_button_release_event (GtkWidget* widget, GdkEventButton* event) { | |
390 return gtk_mouse (OS.GDK_BUTTON_RELEASE, widget, event); | |
391 } | |
392 | |
393 override int gtk_key_press_event (GtkWidget* widget, GdkEventKey* keyEvent) { | |
394 auto result = super.gtk_key_press_event (widget, keyEvent); | |
395 if (result !is 0) return result; | |
396 int stepSize = ((keyEvent.state & OS.GDK_CONTROL_MASK) !is 0) ? STEPSIZE_SMALL : STEPSIZE_LARGE; | |
397 int xChange = 0, yChange = 0; | |
398 switch (keyEvent.keyval) { | |
399 case OS.GDK_Escape: | |
400 cancelled = true; | |
401 // fallthrough | |
402 case OS.GDK_Return: | |
403 tracking = false; | |
404 break; | |
405 case OS.GDK_Left: | |
406 xChange = -stepSize; | |
407 break; | |
408 case OS.GDK_Right: | |
409 xChange = stepSize; | |
410 break; | |
411 case OS.GDK_Up: | |
412 yChange = -stepSize; | |
413 break; | |
414 case OS.GDK_Down: | |
415 yChange = stepSize; | |
416 break; | |
417 default: | |
418 } | |
419 if (xChange !is 0 || yChange !is 0) { | |
420 Rectangle [] oldRectangles = rectangles; | |
421 Rectangle [] rectsToErase = new Rectangle [rectangles.length]; | |
422 for (int i = 0; i < rectangles.length; i++) { | |
423 Rectangle current = rectangles [i]; | |
424 rectsToErase [i] = new Rectangle (current.x, current.y, current.width, current.height); | |
425 } | |
426 Event event = new Event (); | |
427 event.x = oldX + xChange; | |
428 event.y = oldY + yChange; | |
429 if (parent !is null && (parent.style & SWT.MIRRORED) !is 0) { | |
430 event.x = parent.getClientWidth () - event.width - event.x; | |
431 } | |
432 if ((style & SWT.RESIZE) !is 0) { | |
433 resizeRectangles (xChange, yChange); | |
434 sendEvent (SWT.Resize, event); | |
435 /* | |
436 * It is possible (but unlikely) that application | |
437 * code could have disposed the widget in the resize | |
438 * event. If this happens return false to indicate | |
439 * that the tracking has failed. | |
440 */ | |
441 if (isDisposed ()) { | |
442 cancelled = true; | |
443 return 1; | |
444 } | |
445 bool draw = false; | |
446 /* | |
447 * It is possible that application code could have | |
448 * changed the rectangles in the resize event. If this | |
449 * happens then only redraw the tracker if the rectangle | |
450 * values have changed. | |
451 */ | |
452 if (rectangles !is oldRectangles) { | |
453 int length = rectangles.length; | |
454 if (length !is rectsToErase.length) { | |
455 draw = true; | |
456 } else { | |
457 for (int i = 0; i < length; i++) { | |
458 if (rectangles [i] !=/*eq*/ rectsToErase [i]) { | |
459 draw = true; | |
460 break; | |
461 } | |
462 } | |
463 } | |
464 } else { | |
465 draw = true; | |
466 } | |
467 if (draw) { | |
468 drawRectangles (rectsToErase); | |
469 update (); | |
470 drawRectangles (rectangles); | |
471 } | |
472 Point cursorPos = adjustResizeCursor (); | |
473 if (cursorPos !is null) { | |
474 oldX = cursorPos.x; | |
475 oldY = cursorPos.y; | |
476 } | |
477 } else { | |
478 moveRectangles (xChange, yChange); | |
479 sendEvent (SWT.Move, event); | |
480 /* | |
481 * It is possible (but unlikely) that application | |
482 * code could have disposed the widget in the move | |
483 * event. If this happens return false to indicate | |
484 * that the tracking has failed. | |
485 */ | |
486 if (isDisposed ()) { | |
487 cancelled = true; | |
488 return 1; | |
489 } | |
490 bool draw = false; | |
491 /* | |
492 * It is possible that application code could have | |
493 * changed the rectangles in the move event. If this | |
494 * happens then only redraw the tracker if the rectangle | |
495 * values have changed. | |
496 */ | |
497 if (rectangles !is oldRectangles) { | |
498 int length = rectangles.length; | |
499 if (length !is rectsToErase.length) { | |
500 draw = true; | |
501 } else { | |
502 for (int i = 0; i < length; i++) { | |
503 if (rectangles [i] !=/*eq*/ rectsToErase [i]) { | |
504 draw = true; | |
505 break; | |
506 } | |
507 } | |
508 } | |
509 } else { | |
510 draw = true; | |
511 } | |
512 if (draw) { | |
513 drawRectangles (rectsToErase); | |
514 update (); | |
515 drawRectangles (rectangles); | |
516 } | |
517 Point cursorPos = adjustMoveCursor (); | |
518 if (cursorPos !is null) { | |
519 oldX = cursorPos.x; | |
520 oldY = cursorPos.y; | |
521 } | |
522 } | |
523 } | |
524 return result; | |
525 } | |
526 | |
527 override int gtk_motion_notify_event (GtkWidget* widget, GdkEventMotion* eventPtr) { | |
528 auto cursor = this.cursor !is null ? this.cursor.handle : null; | |
529 if (cursor !is lastCursor) { | |
530 ungrab (); | |
531 grabbed = grab (); | |
532 lastCursor = cursor; | |
533 } | |
534 return gtk_mouse (OS.GDK_MOTION_NOTIFY, widget, eventPtr); | |
535 } | |
536 | |
537 private int gtk_mouse (int eventType, GtkWidget* widget, void* eventPtr) { | |
538 int newX, newY; | |
539 OS.gdk_window_get_pointer (window, &newX, &newY, null); | |
540 if (oldX !is newX || oldY !is newY ) { | |
541 Rectangle [] oldRectangles = rectangles; | |
542 Rectangle [] rectsToErase = new Rectangle [rectangles.length]; | |
543 for (int i = 0; i < rectangles.length; i++) { | |
544 Rectangle current = rectangles [i]; | |
545 rectsToErase [i] = new Rectangle (current.x, current.y, current.width, current.height); | |
546 } | |
547 Event event = new Event (); | |
548 if (parent is null) { | |
549 event.x = newX ; | |
550 event.y = newY ; | |
551 } else { | |
552 Point screenCoord = display.map (parent, null, newX , newY ); | |
553 event.x = screenCoord.x; | |
554 event.y = screenCoord.y; | |
555 } | |
556 if ((style & SWT.RESIZE) !is 0) { | |
557 resizeRectangles (newX - oldX, newY - oldY); | |
558 sendEvent (SWT.Resize, event); | |
559 /* | |
560 * It is possible (but unlikely), that application | |
561 * code could have disposed the widget in the resize | |
562 * event. If this happens, return false to indicate | |
563 * that the tracking has failed. | |
564 */ | |
565 if (isDisposed ()) { | |
566 cancelled = true; | |
567 return 1; | |
568 } | |
569 bool draw = false; | |
570 /* | |
571 * It is possible that application code could have | |
572 * changed the rectangles in the resize event. If this | |
573 * happens then only redraw the tracker if the rectangle | |
574 * values have changed. | |
575 */ | |
576 if (rectangles !is oldRectangles) { | |
577 int length = rectangles.length; | |
578 if (length !is rectsToErase.length) { | |
579 draw = true; | |
580 } else { | |
581 for (int i = 0; i < length; i++) { | |
582 if (rectangles [i] !=/*eq*/ rectsToErase [i]) { | |
583 draw = true; | |
584 break; | |
585 } | |
586 } | |
587 } | |
588 } else { | |
589 draw = true; | |
590 } | |
591 if (draw) { | |
592 drawRectangles (rectsToErase); | |
593 update (); | |
594 drawRectangles (rectangles); | |
595 } | |
596 Point cursorPos = adjustResizeCursor (); | |
597 if (cursorPos !is null) { | |
598 newX = cursorPos.x; | |
599 newY = cursorPos.y; | |
600 } | |
601 } else { | |
602 moveRectangles (newX - oldX, newY - oldY); | |
603 sendEvent (SWT.Move, event); | |
604 /* | |
605 * It is possible (but unlikely), that application | |
606 * code could have disposed the widget in the move | |
607 * event. If this happens, return false to indicate | |
608 * that the tracking has failed. | |
609 */ | |
610 if (isDisposed ()) { | |
611 cancelled = true; | |
612 return 1; | |
613 } | |
614 bool draw = false; | |
615 /* | |
616 * It is possible that application code could have | |
617 * changed the rectangles in the move event. If this | |
618 * happens then only redraw the tracker if the rectangle | |
619 * values have changed. | |
620 */ | |
621 if (rectangles !is oldRectangles) { | |
622 int length = rectangles.length; | |
623 if (length !is rectsToErase.length) { | |
624 draw = true; | |
625 } else { | |
626 for (int i = 0; i < length; i++) { | |
627 if (rectangles [i] !=/*eq*/ rectsToErase [i]) { | |
628 draw = true; | |
629 break; | |
630 } | |
631 } | |
632 } | |
633 } else { | |
634 draw = true; | |
635 } | |
636 if (draw) { | |
637 drawRectangles (rectsToErase); | |
638 update (); | |
639 drawRectangles (rectangles); | |
640 } | |
641 } | |
642 oldX = newX ; | |
643 oldY = newY ; | |
644 } | |
645 tracking = eventType !is OS.GDK_BUTTON_RELEASE; | |
646 return 0; | |
647 } | |
648 | |
649 void moveRectangles (int xChange, int yChange) { | |
650 if (bounds is null) return; | |
651 if (xChange < 0 && ((style & SWT.LEFT) is 0)) xChange = 0; | |
652 if (xChange > 0 && ((style & SWT.RIGHT) is 0)) xChange = 0; | |
653 if (yChange < 0 && ((style & SWT.UP) is 0)) yChange = 0; | |
654 if (yChange > 0 && ((style & SWT.DOWN) is 0)) yChange = 0; | |
655 if (xChange is 0 && yChange is 0) return; | |
656 if (parent !is null && (parent.style & SWT.MIRRORED) !is 0) xChange *= -1; | |
657 bounds.x += xChange; bounds.y += yChange; | |
658 for (int i = 0; i < rectangles.length; i++) { | |
659 rectangles [i].x += xChange; | |
660 rectangles [i].y += yChange; | |
661 } | |
662 } | |
663 | |
664 /** | |
665 * Displays the Tracker rectangles for manipulation by the user. Returns when | |
666 * the user has either finished manipulating the rectangles or has cancelled the | |
667 * Tracker. | |
668 * | |
669 * @return <code>true</code> if the user did not cancel the Tracker, <code>false</code> otherwise | |
670 * | |
671 * @exception SWTException <ul> | |
672 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
673 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
674 * </ul> | |
675 */ | |
676 public bool open () { | |
677 checkWidget(); | |
678 window = OS.GDK_ROOT_PARENT (); | |
679 if (parent !is null) { | |
680 window = OS.GTK_WIDGET_WINDOW (parent.paintHandle()); | |
681 } | |
682 if (window is null) return false; | |
683 cancelled = false; | |
684 tracking = true; | |
685 update (); | |
686 drawRectangles (rectangles); | |
687 int oldX, oldY, state; | |
688 OS.gdk_window_get_pointer (window, &oldX, &oldY, &state); | |
689 | |
690 /* | |
691 * if exactly one of UP/DOWN is specified as a style then set the cursor | |
692 * orientation accordingly (the same is done for LEFT/RIGHT styles below) | |
693 */ | |
694 int vStyle = style & (SWT.UP | SWT.DOWN); | |
695 if (vStyle is SWT.UP || vStyle is SWT.DOWN) { | |
696 cursorOrientation |= vStyle; | |
697 } | |
698 int hStyle = style & (SWT.LEFT | SWT.RIGHT); | |
699 if (hStyle is SWT.LEFT || hStyle is SWT.RIGHT) { | |
700 cursorOrientation |= hStyle; | |
701 } | |
702 | |
703 int mask = OS.GDK_BUTTON1_MASK | OS.GDK_BUTTON2_MASK | OS.GDK_BUTTON3_MASK; | |
704 bool mouseDown = (state & mask) !is 0; | |
705 if (!mouseDown) { | |
706 Point cursorPos = null; | |
707 if ((style & SWT.RESIZE) !is 0) { | |
708 cursorPos = adjustResizeCursor (); | |
709 } else { | |
710 cursorPos = adjustMoveCursor (); | |
711 } | |
712 if (cursorPos !is null) { | |
713 oldX = cursorPos.x; | |
714 oldY = cursorPos.y; | |
715 } | |
716 } | |
717 this.oldX = oldX ; | |
718 this.oldY = oldY ; | |
719 | |
720 grabbed = grab (); | |
721 lastCursor = this.cursor !is null ? this.cursor.handle : null; | |
722 | |
723 /* Tracker behaves like a Dialog with its own OS event loop. */ | |
724 GdkEvent* gdkEvent; | |
725 while (tracking) { | |
726 if (parent !is null && parent.isDisposed ()) break; | |
727 GdkEvent* eventPtr; | |
728 while (true) { | |
729 eventPtr = OS.gdk_event_get (); | |
730 if (eventPtr !is null) { | |
731 break; | |
732 } else { | |
47
65761bc28ab2
swt linux again compilable for d1.
Frank Benoit <benoit@tionex.de>
parents:
25
diff
changeset
|
733 try { Thread.sleep(50); } catch (Exception ex) {} |
25 | 734 } |
735 } | |
736 gdkEvent = eventPtr; | |
737 auto widget = OS.gtk_get_event_widget (eventPtr); | |
738 switch (gdkEvent.type) { | |
739 case OS.GDK_MOTION_NOTIFY: gtk_motion_notify_event (widget, cast(GdkEventMotion*)eventPtr); break; | |
740 case OS.GDK_BUTTON_RELEASE: gtk_button_release_event (widget, cast(GdkEventButton*)eventPtr); break; | |
741 case OS.GDK_KEY_PRESS: gtk_key_press_event (widget, cast(GdkEventKey*)eventPtr); break; | |
742 case OS.GDK_KEY_RELEASE: gtk_key_release_event (widget, cast(GdkEventKey*)eventPtr); break; | |
743 case OS.GDK_BUTTON_PRESS: | |
744 case OS.GDK_2BUTTON_PRESS: | |
745 case OS.GDK_3BUTTON_PRESS: | |
746 case OS.GDK_ENTER_NOTIFY: | |
747 case OS.GDK_LEAVE_NOTIFY: | |
748 /* Do not dispatch these */ | |
749 break; | |
750 case OS.GDK_EXPOSE: | |
751 update (); | |
752 drawRectangles (rectangles); | |
753 OS.gtk_main_do_event (eventPtr); | |
754 drawRectangles (rectangles); | |
755 break; | |
756 default: | |
757 OS.gtk_main_do_event (eventPtr); | |
758 } | |
759 OS.gdk_event_free (eventPtr); | |
760 } | |
761 if (!isDisposed ()) { | |
762 update (); | |
763 drawRectangles (rectangles); | |
764 } | |
765 ungrab (); | |
766 window = null; | |
767 return !cancelled; | |
768 } | |
769 | |
770 override void releaseWidget () { | |
771 super.releaseWidget (); | |
772 parent = null; | |
773 rectangles = proportions = null; | |
774 bounds = null; | |
775 } | |
776 | |
777 /** | |
778 * Removes the listener from the collection of listeners who will | |
779 * be notified when the control is moved or resized. | |
780 * | |
781 * @param listener the listener which should no longer be notified | |
782 * | |
783 * @exception IllegalArgumentException <ul> | |
784 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
785 * </ul> | |
786 * @exception SWTException <ul> | |
787 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
788 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
789 * </ul> | |
790 * | |
791 * @see ControlListener | |
792 * @see #addControlListener | |
793 */ | |
794 public void removeControlListener (ControlListener listener) { | |
795 checkWidget(); | |
796 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); | |
797 if (eventTable is null) return; | |
798 eventTable.unhook (SWT.Resize, listener); | |
799 eventTable.unhook (SWT.Move, listener); | |
800 } | |
801 | |
802 /** | |
803 * Removes the listener from the collection of listeners who will | |
804 * be notified when keys are pressed and released on the system keyboard. | |
805 * | |
806 * @param listener the listener which should no longer be notified | |
807 * | |
808 * @exception IllegalArgumentException <ul> | |
809 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
810 * </ul> | |
811 * @exception SWTException <ul> | |
812 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
813 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
814 * </ul> | |
815 * | |
816 * @see KeyListener | |
817 * @see #addKeyListener | |
818 */ | |
819 public void removeKeyListener(KeyListener listener) { | |
820 checkWidget(); | |
821 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); | |
822 if (eventTable is null) return; | |
823 eventTable.unhook (SWT.KeyUp, listener); | |
824 eventTable.unhook (SWT.KeyDown, listener); | |
825 } | |
826 | |
827 void resizeRectangles (int xChange, int yChange) { | |
828 if (bounds is null) return; | |
829 if (parent !is null && (parent.style & SWT.MIRRORED) !is 0) xChange *= -1; | |
830 /* | |
831 * If the cursor orientation has not been set in the orientation of | |
832 * this change then try to set it here. | |
833 */ | |
834 if (xChange < 0 && ((style & SWT.LEFT) !is 0) && ((cursorOrientation & SWT.RIGHT) is 0)) { | |
835 cursorOrientation |= SWT.LEFT; | |
836 } | |
837 if (xChange > 0 && ((style & SWT.RIGHT) !is 0) && ((cursorOrientation & SWT.LEFT) is 0)) { | |
838 cursorOrientation |= SWT.RIGHT; | |
839 } | |
840 if (yChange < 0 && ((style & SWT.UP) !is 0) && ((cursorOrientation & SWT.DOWN) is 0)) { | |
841 cursorOrientation |= SWT.UP; | |
842 } | |
843 if (yChange > 0 && ((style & SWT.DOWN) !is 0) && ((cursorOrientation & SWT.UP) is 0)) { | |
844 cursorOrientation |= SWT.DOWN; | |
845 } | |
846 | |
847 /* | |
848 * If the bounds will flip about the x or y axis then apply the adjustment | |
849 * up to the axis (ie.- where bounds width/height becomes 0), change the | |
850 * cursor's orientation accordingly, and flip each Rectangle's origin (only | |
851 * necessary for > 1 Rectangles) | |
852 */ | |
853 if ((cursorOrientation & SWT.LEFT) !is 0) { | |
854 if (xChange > bounds.width) { | |
855 if ((style & SWT.RIGHT) is 0) return; | |
856 cursorOrientation |= SWT.RIGHT; | |
857 cursorOrientation &= ~SWT.LEFT; | |
858 bounds.x += bounds.width; | |
859 xChange -= bounds.width; | |
860 bounds.width = 0; | |
861 if (proportions.length > 1) { | |
862 for (int i = 0; i < proportions.length; i++) { | |
863 Rectangle proportion = proportions [i]; | |
864 proportion.x = 100 - proportion.x - proportion.width; | |
865 } | |
866 } | |
867 } | |
868 } else if ((cursorOrientation & SWT.RIGHT) !is 0) { | |
869 if (bounds.width < -xChange) { | |
870 if ((style & SWT.LEFT) is 0) return; | |
871 cursorOrientation |= SWT.LEFT; | |
872 cursorOrientation &= ~SWT.RIGHT; | |
873 xChange += bounds.width; | |
874 bounds.width = 0; | |
875 if (proportions.length > 1) { | |
876 for (int i = 0; i < proportions.length; i++) { | |
877 Rectangle proportion = proportions [i]; | |
878 proportion.x = 100 - proportion.x - proportion.width; | |
879 } | |
880 } | |
881 } | |
882 } | |
883 if ((cursorOrientation & SWT.UP) !is 0) { | |
884 if (yChange > bounds.height) { | |
885 if ((style & SWT.DOWN) is 0) return; | |
886 cursorOrientation |= SWT.DOWN; | |
887 cursorOrientation &= ~SWT.UP; | |
888 bounds.y += bounds.height; | |
889 yChange -= bounds.height; | |
890 bounds.height = 0; | |
891 if (proportions.length > 1) { | |
892 for (int i = 0; i < proportions.length; i++) { | |
893 Rectangle proportion = proportions [i]; | |
894 proportion.y = 100 - proportion.y - proportion.height; | |
895 } | |
896 } | |
897 } | |
898 } else if ((cursorOrientation & SWT.DOWN) !is 0) { | |
899 if (bounds.height < -yChange) { | |
900 if ((style & SWT.UP) is 0) return; | |
901 cursorOrientation |= SWT.UP; | |
902 cursorOrientation &= ~SWT.DOWN; | |
903 yChange += bounds.height; | |
904 bounds.height = 0; | |
905 if (proportions.length > 1) { | |
906 for (int i = 0; i < proportions.length; i++) { | |
907 Rectangle proportion = proportions [i]; | |
908 proportion.y = 100 - proportion.y - proportion.height; | |
909 } | |
910 } | |
911 } | |
912 } | |
913 | |
914 // apply the bounds adjustment | |
915 if ((cursorOrientation & SWT.LEFT) !is 0) { | |
916 bounds.x += xChange; | |
917 bounds.width -= xChange; | |
918 } else if ((cursorOrientation & SWT.RIGHT) !is 0) { | |
919 bounds.width += xChange; | |
920 } | |
921 if ((cursorOrientation & SWT.UP) !is 0) { | |
922 bounds.y += yChange; | |
923 bounds.height -= yChange; | |
924 } else if ((cursorOrientation & SWT.DOWN) !is 0) { | |
925 bounds.height += yChange; | |
926 } | |
927 | |
928 Rectangle [] newRects = new Rectangle [rectangles.length]; | |
929 for (int i = 0; i < rectangles.length; i++) { | |
930 Rectangle proportion = proportions[i]; | |
931 newRects[i] = new Rectangle ( | |
932 proportion.x * bounds.width / 100 + bounds.x, | |
933 proportion.y * bounds.height / 100 + bounds.y, | |
934 proportion.width * bounds.width / 100, | |
935 proportion.height * bounds.height / 100); | |
936 } | |
937 rectangles = newRects; | |
938 } | |
939 | |
940 /** | |
941 * Sets the <code>Cursor</code> of the Tracker. If this cursor is <code>null</code> | |
942 * then the cursor reverts to the default. | |
943 * | |
944 * @param newCursor the new <code>Cursor</code> to display | |
945 * | |
946 * @exception SWTException <ul> | |
947 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
948 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
949 * </ul> | |
950 */ | |
951 public void setCursor (Cursor value) { | |
952 checkWidget (); | |
953 cursor = value; | |
954 } | |
955 | |
956 /** | |
957 * Specifies the rectangles that should be drawn, expressed relative to the parent | |
958 * widget. If the parent is a Display then these are screen coordinates. | |
959 * | |
960 * @param rectangles the bounds of the rectangles to be drawn | |
961 * | |
962 * @exception IllegalArgumentException <ul> | |
963 * <li>ERROR_NULL_ARGUMENT - if the set of rectangles contains a null rectangle</li> | |
964 * </ul> | |
965 * @exception SWTException <ul> | |
966 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
967 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
968 * </ul> | |
969 */ | |
970 public void setRectangles (Rectangle [] rectangles) { | |
971 checkWidget(); | |
972 // SWT extension: allow null for zero length string | |
973 //if (rectangles is null) error (SWT.ERROR_NULL_ARGUMENT); | |
974 int length = rectangles.length; | |
975 this.rectangles = new Rectangle [length]; | |
976 for (int i = 0; i < length; i++) { | |
977 Rectangle current = rectangles [i]; | |
978 if (current is null) error (SWT.ERROR_NULL_ARGUMENT); | |
979 this.rectangles [i] = new Rectangle (current.x, current.y, current.width, current.height); | |
980 } | |
981 proportions = computeProportions (rectangles); | |
982 } | |
983 | |
984 /** | |
985 * Changes the appearance of the line used to draw the rectangles. | |
986 * | |
987 * @param stippled <code>true</code> if rectangle should appear stippled | |
988 * | |
989 * @exception SWTException <ul> | |
990 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
991 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
992 * </ul> | |
993 */ | |
994 public void setStippled (bool stippled) { | |
995 checkWidget(); | |
996 this.stippled = stippled; | |
997 } | |
998 | |
999 void ungrab () { | |
1000 if (grabbed) OS.gdk_pointer_ungrab (OS.GDK_CURRENT_TIME); | |
1001 } | |
1002 | |
1003 void update () { | |
1004 if (parent !is null) { | |
1005 if (parent.isDisposed ()) return; | |
1006 parent.getShell ().update (); | |
1007 } else { | |
1008 display.update (); | |
1009 } | |
1010 } | |
1011 | |
1012 } |