Mercurial > projects > dwt-addons
comparison dwtx/draw2d/SWTEventDispatcher.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.draw2d.SWTEventDispatcher; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwt.DWT; | |
18 import dwt.accessibility.AccessibleControlEvent; | |
19 import dwt.accessibility.AccessibleControlListener; | |
20 import dwt.accessibility.AccessibleEvent; | |
21 import dwt.accessibility.AccessibleListener; | |
22 import dwt.events.DisposeEvent; | |
23 import dwt.events.TraverseEvent; | |
24 import dwt.graphics.Cursor; | |
25 import dwt.widgets.Control; | |
26 import dwtx.draw2d.EventDispatcher; | |
27 import dwtx.draw2d.IFigure; | |
28 import dwtx.draw2d.Figure; | |
29 import dwtx.draw2d.MouseEvent; | |
30 import dwtx.draw2d.FocusEvent; | |
31 import dwtx.draw2d.KeyEvent; | |
32 import dwtx.draw2d.ToolTipHelper; | |
33 static import dwt.widgets.Control; | |
34 import dwtx.draw2d.FocusTraverseManager; | |
35 static import dwt.events.FocusEvent; | |
36 static import dwt.events.KeyEvent; | |
37 static import dwt.events.MouseEvent; | |
38 | |
39 /** | |
40 * The SWTEventDispatcher provides draw2d with the ability to dispatch DWT Events. The | |
41 * {@link dwtx.draw2d.LightweightSystem} adds DWT event listeners on its Canvas. | |
42 * When the Canvas receives an DWT event, it calls the appropriate dispatcher method in | |
43 * SWTEventDispatcher. | |
44 */ | |
45 public class SWTEventDispatcher | |
46 : EventDispatcher | |
47 { | |
48 | |
49 /** | |
50 * Used to tell if any button is pressed without regard to the specific button. | |
51 * @deprecated Use {@link DWT#BUTTON_MASK} instead. | |
52 */ | |
53 protected static const int ANY_BUTTON = DWT.BUTTON_MASK; | |
54 | |
55 private bool figureTraverse = true; | |
56 | |
57 private bool captured; | |
58 private IFigure root; | |
59 private IFigure mouseTarget; | |
60 private IFigure cursorTarget; | |
61 private IFigure focusOwner; | |
62 private IFigure hoverSource; | |
63 | |
64 private MouseEvent currentEvent; | |
65 private Cursor cursor; | |
66 /** The control this dispatcher is listening to. */ | |
67 protected dwt.widgets.Control.Control control; | |
68 | |
69 private ToolTipHelper toolTipHelper; | |
70 private FocusTraverseManager focusManager; | |
71 | |
72 /** | |
73 * Implements {@link EventDispatcher.AccessibilityDispatcher} but | |
74 * does nothing in the implementation. | |
75 */ | |
76 protected class FigureAccessibilityDispatcher | |
77 : AccessibilityDispatcher | |
78 { | |
79 /** @see AccessibleControlListener#getChildAtPoint(AccessibleControlEvent) */ | |
80 public void getChildAtPoint(AccessibleControlEvent e) { } | |
81 /** @see AccessibleControlListener#getChildCount(AccessibleControlEvent) */ | |
82 public void getChildCount(AccessibleControlEvent e) { } | |
83 /** @see AccessibleControlListener#getChildren(AccessibleControlEvent) */ | |
84 public void getChildren(AccessibleControlEvent e) { } | |
85 /** @see AccessibleControlListener#getDefaultAction(AccessibleControlEvent) */ | |
86 public void getDefaultAction(AccessibleControlEvent e) { } | |
87 /** @see AccessibleListener#getDescription(AccessibleEvent) */ | |
88 public void getDescription(AccessibleEvent e) { } | |
89 /** @see AccessibleControlListener#getFocus(AccessibleControlEvent) */ | |
90 public void getFocus(AccessibleControlEvent e) { } | |
91 /** @see AccessibleListener#getHelp(AccessibleEvent) */ | |
92 public void getHelp(AccessibleEvent e) { } | |
93 /** @see AccessibleListener#getKeyboardShortcut(AccessibleEvent) */ | |
94 public void getKeyboardShortcut(AccessibleEvent e) { } | |
95 /** @see AccessibleControlListener#getLocation(AccessibleControlEvent) */ | |
96 public void getLocation(AccessibleControlEvent e) { } | |
97 /** @see AccessibleListener#getName(AccessibleEvent) */ | |
98 public void getName(AccessibleEvent e) { } | |
99 /** @see AccessibleControlListener#getRole(AccessibleControlEvent) */ | |
100 public void getRole(AccessibleControlEvent e) { } | |
101 /** @see AccessibleControlListener#getSelection(AccessibleControlEvent) */ | |
102 public void getSelection(AccessibleControlEvent e) { } | |
103 /** @see AccessibleControlListener#getState(AccessibleControlEvent) */ | |
104 public void getState(AccessibleControlEvent e) { } | |
105 /** @see AccessibleControlListener#getValue(AccessibleControlEvent) */ | |
106 public void getValue(AccessibleControlEvent e) { } | |
107 } | |
108 | |
109 public this(){ | |
110 focusManager = new FocusTraverseManager(); | |
111 } | |
112 | |
113 /** | |
114 * @see EventDispatcher#dispatchFocusGained(dwt.events.FocusEvent) | |
115 */ | |
116 public void dispatchFocusGained(dwt.events.FocusEvent.FocusEvent e) { | |
117 IFigure currentFocusOwner = getFocusTraverseManager().getCurrentFocusOwner(); | |
118 | |
119 /* | |
120 * Upon focus gained, if there is no current focus owner, | |
121 * set focus on first focusable child. | |
122 */ | |
123 if (currentFocusOwner is null) | |
124 currentFocusOwner = | |
125 getFocusTraverseManager().getNextFocusableFigure(root, focusOwner); | |
126 setFocus(currentFocusOwner); | |
127 } | |
128 | |
129 /** | |
130 * @see EventDispatcher#dispatchFocusLost(dwt.events.FocusEvent) | |
131 */ | |
132 public void dispatchFocusLost(dwt.events.FocusEvent.FocusEvent e) { | |
133 setFocus(null); | |
134 } | |
135 | |
136 /** | |
137 * @see EventDispatcher#dispatchKeyPressed(dwt.events.KeyEvent) | |
138 */ | |
139 public void dispatchKeyPressed(dwt.events.KeyEvent.KeyEvent e) { | |
140 if (focusOwner !is null) { | |
141 KeyEvent event = new KeyEvent(this, focusOwner, e); | |
142 focusOwner.handleKeyPressed(event); | |
143 } | |
144 } | |
145 | |
146 /** | |
147 * @see EventDispatcher#dispatchKeyReleased(dwt.events.KeyEvent) | |
148 */ | |
149 public void dispatchKeyReleased(dwt.events.KeyEvent.KeyEvent e) { | |
150 if (focusOwner !is null) { | |
151 KeyEvent event = new KeyEvent(this, focusOwner, e); | |
152 focusOwner.handleKeyReleased(event); | |
153 } | |
154 } | |
155 | |
156 /** | |
157 * @see EventDispatcher#dispatchKeyTraversed(TraverseEvent) | |
158 */ | |
159 public void dispatchKeyTraversed(TraverseEvent e) { | |
160 if (!figureTraverse) | |
161 return; | |
162 IFigure nextFigure = null; | |
163 | |
164 if (e.detail is DWT.TRAVERSE_TAB_NEXT) | |
165 nextFigure = getFocusTraverseManager().getNextFocusableFigure(root, focusOwner); | |
166 else if (e.detail is DWT.TRAVERSE_TAB_PREVIOUS) | |
167 nextFigure = | |
168 getFocusTraverseManager().getPreviousFocusableFigure(root, focusOwner); | |
169 | |
170 if (nextFigure !is null) { | |
171 e.doit = false; | |
172 setFocus(nextFigure); | |
173 } | |
174 } | |
175 | |
176 /** | |
177 * @see EventDispatcher#dispatchMouseHover(dwt.events.MouseEvent) | |
178 */ | |
179 public void dispatchMouseHover(dwt.events.MouseEvent.MouseEvent me) { | |
180 receive(me); | |
181 if (mouseTarget !is null) | |
182 mouseTarget.handleMouseHover(currentEvent); | |
183 /* | |
184 * Check Tooltip source. | |
185 * Get Tooltip source's Figure. | |
186 * Set that tooltip as the lws contents on the helper. | |
187 */ | |
188 if (hoverSource !is null) { | |
189 toolTipHelper = getToolTipHelper(); | |
190 IFigure tip = hoverSource.getToolTip(); | |
191 Control control = cast(Control)me.getSource(); | |
192 dwt.graphics.Point.Point absolute; | |
193 absolute = control.toDisplay(new dwt.graphics.Point.Point(me.x, me.y)); | |
194 toolTipHelper.displayToolTipNear(hoverSource, tip, absolute.x, absolute.y); | |
195 } | |
196 } | |
197 | |
198 /** | |
199 * @see EventDispatcher#dispatchMouseDoubleClicked(dwt.events.MouseEvent) | |
200 */ | |
201 public void dispatchMouseDoubleClicked(dwt.events.MouseEvent.MouseEvent me) { | |
202 receive(me); | |
203 if (mouseTarget !is null) | |
204 mouseTarget.handleMouseDoubleClicked(currentEvent); | |
205 } | |
206 | |
207 /** | |
208 * @see EventDispatcher#dispatchMouseEntered(dwt.events.MouseEvent) | |
209 */ | |
210 public void dispatchMouseEntered(dwt.events.MouseEvent.MouseEvent me) { | |
211 receive(me); | |
212 } | |
213 | |
214 /** | |
215 * @see EventDispatcher#dispatchMouseExited(dwt.events.MouseEvent) | |
216 */ | |
217 public void dispatchMouseExited(dwt.events.MouseEvent.MouseEvent me) { | |
218 setHoverSource(null, me); | |
219 if (mouseTarget !is null) { | |
220 currentEvent = | |
221 new MouseEvent(me.x, me.y, this, mouseTarget, me.button, me.stateMask); | |
222 mouseTarget.handleMouseExited(currentEvent); | |
223 releaseCapture(); | |
224 mouseTarget = null; | |
225 } | |
226 } | |
227 | |
228 /** | |
229 * @see EventDispatcher#dispatchMousePressed(dwt.events.MouseEvent) | |
230 */ | |
231 public void dispatchMousePressed(dwt.events.MouseEvent.MouseEvent me) { | |
232 receive(me); | |
233 if (mouseTarget !is null) { | |
234 mouseTarget.handleMousePressed(currentEvent); | |
235 if (currentEvent.isConsumed()) | |
236 setCapture(mouseTarget); | |
237 } | |
238 } | |
239 | |
240 /** | |
241 * @see EventDispatcher#dispatchMouseMoved(dwt.events.MouseEvent) | |
242 */ | |
243 public void dispatchMouseMoved(dwt.events.MouseEvent.MouseEvent me) { | |
244 receive(me); | |
245 if (mouseTarget !is null) { | |
246 if ((me.stateMask & DWT.BUTTON_MASK) !is 0) | |
247 mouseTarget.handleMouseDragged(currentEvent); | |
248 else | |
249 mouseTarget.handleMouseMoved(currentEvent); | |
250 } | |
251 } | |
252 | |
253 /** | |
254 * @see EventDispatcher#dispatchMouseReleased(dwt.events.MouseEvent) | |
255 */ | |
256 public void dispatchMouseReleased(dwt.events.MouseEvent.MouseEvent me) { | |
257 receive(me); | |
258 if (mouseTarget !is null) { | |
259 mouseTarget.handleMouseReleased(currentEvent); | |
260 } | |
261 releaseCapture(); | |
262 receive(me); | |
263 } | |
264 | |
265 /** | |
266 * @see EventDispatcher#getAccessibilityDispatcher() | |
267 */ | |
268 protected AccessibilityDispatcher getAccessibilityDispatcher() { | |
269 return null; | |
270 } | |
271 | |
272 /** | |
273 * Returns the current mouse event. | |
274 * @return the current mouse event; can be <code>null</code> | |
275 */ | |
276 protected MouseEvent getCurrentEvent() { | |
277 return currentEvent; | |
278 } | |
279 | |
280 private IFigure getCurrentToolTip() { | |
281 if (hoverSource !is null) | |
282 return hoverSource.getToolTip(); | |
283 else | |
284 return null; | |
285 } | |
286 | |
287 /** | |
288 * Returns the figure that the cursor is over. | |
289 * @return the cursor target | |
290 */ | |
291 protected IFigure getCursorTarget() { | |
292 return cursorTarget; | |
293 } | |
294 | |
295 /** | |
296 * Returns the ToolTipHelper used to display tooltips on hover events. | |
297 * @return the ToolTipHelper | |
298 */ | |
299 protected ToolTipHelper getToolTipHelper() { | |
300 if (toolTipHelper is null) | |
301 toolTipHelper = new ToolTipHelper(control); | |
302 return toolTipHelper; | |
303 } | |
304 | |
305 /** | |
306 * Returns the FocusTraverseManager which is used to determine which figure will get focus | |
307 * when a TAB or ALT+TAB key sequence occurs. | |
308 * @return the FocusTraverseManager | |
309 */ | |
310 protected final FocusTraverseManager getFocusTraverseManager() { | |
311 if (focusManager is null) { | |
312 focusManager = new FocusTraverseManager(); | |
313 } | |
314 return focusManager; | |
315 } | |
316 | |
317 /** | |
318 * @see EventDispatcher#getFocusOwner() | |
319 */ | |
320 /*package*/ IFigure getFocusOwner() { | |
321 return focusOwner; | |
322 } | |
323 | |
324 /** | |
325 * Returns the figure that is the target of mouse events. This may not be the figure | |
326 * beneath the cursor because another figure may have captured the mouse and will continue | |
327 * to get mouse events until capture is released. | |
328 * @return the mouse target | |
329 */ | |
330 protected IFigure getMouseTarget() { | |
331 return mouseTarget; | |
332 } | |
333 | |
334 /** | |
335 * Returns the root figure for this dispatcher. | |
336 * @return the root figure | |
337 */ | |
338 protected IFigure getRoot() { | |
339 return root; | |
340 } | |
341 | |
342 /** | |
343 * @see EventDispatcher#isCaptured() | |
344 */ | |
345 public bool isCaptured() { | |
346 return captured; | |
347 } | |
348 | |
349 private void receive(dwt.events.MouseEvent.MouseEvent me) { | |
350 currentEvent = null; | |
351 updateFigureUnderCursor(me); | |
352 int state = me.stateMask; | |
353 if (captured) { | |
354 if (mouseTarget !is null) | |
355 currentEvent = new MouseEvent(me.x, me.y, this, mouseTarget, me.button, state); | |
356 } else { | |
357 IFigure f = root.findMouseEventTargetAt(me.x, me.y); | |
358 if (f is mouseTarget) { | |
359 if (mouseTarget !is null) | |
360 currentEvent = | |
361 new MouseEvent(me.x, me.y, this, mouseTarget, me.button, state); | |
362 return; | |
363 } | |
364 if (mouseTarget !is null) { | |
365 currentEvent = new MouseEvent(me.x, me.y, this, mouseTarget, me.button, state); | |
366 mouseTarget.handleMouseExited(currentEvent); | |
367 } | |
368 setMouseTarget(f); | |
369 if (mouseTarget !is null) { | |
370 currentEvent = new MouseEvent(me.x, me.y, this, mouseTarget, me.button, state); | |
371 mouseTarget.handleMouseEntered(currentEvent); | |
372 } | |
373 } | |
374 } | |
375 | |
376 /** | |
377 * @see EventDispatcher#releaseCapture() | |
378 */ | |
379 protected void releaseCapture() { | |
380 captured = false; | |
381 } | |
382 | |
383 /** | |
384 * @see EventDispatcher#requestFocus(IFigure) | |
385 */ | |
386 public void requestFocus(IFigure fig) { | |
387 setFocus(fig); | |
388 } | |
389 | |
390 /** | |
391 * @see EventDispatcher#requestRemoveFocus(IFigure) | |
392 */ | |
393 public void requestRemoveFocus(IFigure fig) { | |
394 if (getFocusOwner() is fig) | |
395 setFocus(null); | |
396 if (mouseTarget is fig) | |
397 mouseTarget = null; | |
398 if (cursorTarget is fig) | |
399 cursorTarget = null; | |
400 if (hoverSource is fig) | |
401 hoverSource = null; | |
402 getFocusTraverseManager().setCurrentFocusOwner(null); | |
403 } | |
404 | |
405 /** | |
406 * @see EventDispatcher#setCapture(IFigure) | |
407 */ | |
408 protected void setCapture(IFigure figure) { | |
409 captured = true; | |
410 mouseTarget = figure; | |
411 } | |
412 | |
413 /** | |
414 * @see EventDispatcher#setControl(Control) | |
415 */ | |
416 public void setControl(Control c) { | |
417 if (c is control) | |
418 return; | |
419 if (control !is null && !control.isDisposed()) | |
420 throw new RuntimeException( | |
421 "Can not set control again once it has been set"); //$NON-NLS-1$ | |
422 if (c !is null) | |
423 c.addDisposeListener(new class() dwt.events.DisposeListener.DisposeListener { | |
424 public void widgetDisposed(DisposeEvent e) { | |
425 if (toolTipHelper !is null) | |
426 toolTipHelper.dispose(); | |
427 } | |
428 }); | |
429 control = c; | |
430 } | |
431 | |
432 /** | |
433 * Sets the mouse cursor. | |
434 * @param c the new cursor | |
435 */ | |
436 protected void setCursor(Cursor c) { | |
437 if (c is null && cursor is null) { | |
438 return; | |
439 } else if ((c !is cursor) || (!c.opEquals(cursor))) { | |
440 cursor = c; | |
441 if (control !is null && !control.isDisposed()) | |
442 control.setCursor(c); | |
443 } | |
444 } | |
445 | |
446 /** | |
447 * Enables key traversal via TAB and ALT+TAB if <i>traverse</i> is <code>true</code>. | |
448 * Disables it otherwise. | |
449 * @param traverse whether key traversal should be enabled | |
450 */ | |
451 public void setEnableKeyTraversal(bool traverse) { | |
452 figureTraverse = traverse; | |
453 } | |
454 | |
455 /** | |
456 * Sets the figure under the mouse cursor. | |
457 * @param f the new figure under the cursor | |
458 */ | |
459 protected void setFigureUnderCursor(IFigure f) { | |
460 if (cursorTarget is f) | |
461 return; | |
462 cursorTarget = f; | |
463 updateCursor(); | |
464 } | |
465 | |
466 /** | |
467 * Sets the focus figure. If the figure currently with focus is not <code>null</code>, | |
468 * {@link IFigure#handleFocusLost(FocusEvent)} is called on the current focused figure. If | |
469 * the new focus figure is not <code>null</code>, this will call | |
470 * {@link IFigure#handleFocusGained(FocusEvent)} on the new focused figure. | |
471 * @param fig the new focus figure | |
472 */ | |
473 protected void setFocus(IFigure fig) { | |
474 if (fig is focusOwner) | |
475 return; | |
476 FocusEvent fe = new FocusEvent(focusOwner, fig); | |
477 IFigure oldOwner = focusOwner; | |
478 focusOwner = fig; | |
479 if (oldOwner !is null) | |
480 oldOwner.handleFocusLost(fe); | |
481 if (fig !is null) | |
482 getFocusTraverseManager().setCurrentFocusOwner(fig); | |
483 if (focusOwner !is null) | |
484 focusOwner.handleFocusGained(fe); | |
485 } | |
486 | |
487 /** | |
488 * Sets the figure that the mouse cursor is hovering over. | |
489 * @param figure the new hover source | |
490 * @param me the mouse event | |
491 */ | |
492 protected void setHoverSource(Figure figure, dwt.events.MouseEvent.MouseEvent me) { | |
493 hoverSource = figure; | |
494 if (figure !is null) { | |
495 Control control = cast(Control)me.getSource(); | |
496 dwt.graphics.Point.Point absolute; | |
497 absolute = control.toDisplay(new dwt.graphics.Point.Point(me.x, me.y)); | |
498 toolTipHelper = getToolTipHelper(); | |
499 toolTipHelper.updateToolTip( | |
500 hoverSource, getCurrentToolTip(), absolute.x, absolute.y); | |
501 } else if (toolTipHelper !is null) { | |
502 // Update with null to clear hoverSource in ToolTipHelper | |
503 toolTipHelper.updateToolTip(hoverSource, getCurrentToolTip(), me.x, me.y); | |
504 } | |
505 } | |
506 | |
507 /** | |
508 * Sets the given figure to be the target of future mouse events. | |
509 * @param figure the new mouse target | |
510 */ | |
511 protected void setMouseTarget(IFigure figure) { | |
512 mouseTarget = figure; | |
513 } | |
514 | |
515 /** | |
516 * @see EventDispatcher#setRoot(IFigure) | |
517 */ | |
518 public void setRoot(IFigure figure) { | |
519 root = figure; | |
520 } | |
521 | |
522 /** | |
523 * @see EventDispatcher#updateCursor() | |
524 */ | |
525 protected void updateCursor() { | |
526 Cursor newCursor = null; | |
527 if (cursorTarget !is null) | |
528 newCursor = cursorTarget.getCursor(); | |
529 setCursor(newCursor); | |
530 } | |
531 | |
532 /** | |
533 * Updates the figure under the cursor, unless the mouse is captured, in which case all | |
534 * mouse events will be routed to the figure that captured the mouse. | |
535 * @param me the mouse event | |
536 */ | |
537 protected void updateFigureUnderCursor(dwt.events.MouseEvent.MouseEvent me) { | |
538 if (!captured) { | |
539 IFigure f = root.findFigureAt(me.x, me.y); | |
540 setFigureUnderCursor(f); | |
541 if (cast(Figure)cursorTarget !is hoverSource) | |
542 updateHoverSource(me); | |
543 } | |
544 } | |
545 | |
546 /** | |
547 * Updates the figure that will receive hover events. The hover source must have a | |
548 * tooltip. If the figure under the mouse doesn't have a tooltip set, this method will | |
549 * walk up the ancestor hierarchy until either a figure with a tooltip is found or it | |
550 * gets to the root figure. | |
551 * @param me the mouse event | |
552 */ | |
553 protected void updateHoverSource(dwt.events.MouseEvent.MouseEvent me) { | |
554 /* | |
555 * Derive source from figure under cursor. | |
556 * Set the source in setHoverSource(); | |
557 * If figure.getToolTip() is null, get parent's toolTip | |
558 * Continue parent traversal until a toolTip is found or root is reached. | |
559 */ | |
560 if (cursorTarget !is null) { | |
561 bool sourceFound = false; | |
562 Figure source = cast(Figure)cursorTarget; | |
563 while (!sourceFound && source.getParent() !is null) { | |
564 if (source.getToolTip() !is null) | |
565 sourceFound = true; | |
566 else | |
567 source = cast(Figure)source.getParent(); | |
568 } | |
569 setHoverSource(source, me); | |
570 } else { | |
571 setHoverSource(null, me); | |
572 } | |
573 } | |
574 | |
575 } |