comparison dwtx/draw2d/ToolTipHelper.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, 2005 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.ToolTipHelper;
14
15 import dwt.dwthelper.utils;
16 import dwt.dwthelper.Runnable;
17 import dwtx.dwtxhelper.Timer;
18 import dwtx.dwtxhelper.TimerTask;
19
20 import dwt.DWT;
21 import dwt.events.MouseTrackAdapter;
22 import dwt.graphics.Point;
23 import dwt.widgets.Display;
24 static import dwt.widgets.Control;
25 static import dwt.graphics.Rectangle;
26 static import dwt.events.MouseEvent;
27
28 import dwtx.draw2d.geometry.Dimension;
29 import dwtx.draw2d.IFigure;
30 import dwtx.draw2d.PopUpHelper;
31 import dwtx.draw2d.ColorConstants;
32
33 /**
34 * This class is used by SWTEventDispatcher as support to display Figure tooltips on a
35 * mouse hover event. Tooltips are drawn directly below the cursor unless the display
36 * does not allow, in which case the tooltip will be drawn directly above the cursor.
37 * Tooltips will be displayed with a LineBorder. The background of the tooltips will be
38 * the standard DWT tooltipBackground color unless the Figure's tooltip has set its own
39 * background.
40 */
41 public class ToolTipHelper
42 : PopUpHelper
43 {
44
45 private Timer timer;
46 private IFigure currentTipSource;
47
48 /**
49 * Constructs a ToolTipHelper to be associated with Control <i>c</i>.
50 *
51 * @param c the control
52 * @since 2.0
53 */
54 public this(dwt.widgets.Control.Control c) {
55 super(c, DWT.TOOL | DWT.ON_TOP);
56 getShell().setBackground(ColorConstants.tooltipBackground);
57 getShell().setForeground(ColorConstants.tooltipForeground);
58 }
59
60 /*
61 * Calculates the location where the tooltip will be painted. Returns this as a Point.
62 * Tooltip will be painted directly below the cursor if possible, otherwise it will be
63 * painted directly above cursor.
64 */
65 private Point computeWindowLocation(IFigure tip, int eventX, int eventY) {
66 dwt.graphics.Rectangle.Rectangle clientArea = control.getDisplay().getClientArea();
67 Point preferredLocation = new Point(eventX, eventY + 26);
68
69 Dimension tipSize = getLightweightSystem()
70 .getRootFigure()
71 .getPreferredSize()
72 .getExpanded(getShellTrimSize());
73
74 // Adjust location if tip is going to fall outside display
75 if (preferredLocation.y + tipSize.height > clientArea.height)
76 preferredLocation.y = eventY - tipSize.height;
77
78 if (preferredLocation.x + tipSize.width > clientArea.width)
79 preferredLocation.x -= (preferredLocation.x + tipSize.width) - clientArea.width;
80
81 return preferredLocation;
82 }
83
84 /**
85 * Sets the LightWeightSystem's contents to the passed tooltip, and displays the tip. The
86 * tip will be displayed only if the tip source is different than the previously viewed
87 * tip source. (i.e. The cursor has moved off of the previous tooltip source figure.)
88 * <p>
89 * The tooltip will be painted directly below the cursor if possible, otherwise it will be
90 * painted directly above cursor.
91 *
92 * @param hoverSource the figure over which the hover event was fired
93 * @param tip the tooltip to be displayed
94 * @param eventX the x coordinate of the hover event
95 * @param eventY the y coordinate of the hover event
96 * @since 2.0
97 */
98 public void displayToolTipNear(IFigure hoverSource, IFigure tip, int eventX, int eventY) {
99 if (tip !is null && hoverSource !is currentTipSource) {
100 getLightweightSystem().setContents(tip);
101 Point displayPoint = computeWindowLocation(tip, eventX, eventY);
102 Dimension shellSize = getLightweightSystem().getRootFigure()
103 .getPreferredSize().getExpanded(getShellTrimSize());
104 setShellBounds(displayPoint.x, displayPoint.y, shellSize.width, shellSize.height);
105 show();
106 currentTipSource = hoverSource;
107 timer = new Timer(true);
108 timer.schedule(new class() TimerTask {
109 public void run() {
110 Display.getDefault().syncExec(dgRunnable( {
111 hide();
112 timer.cancel();
113 }));
114 }
115 }, 5000);
116 }
117 }
118
119 /**
120 * Disposes of the tooltip's shell and kills the timer.
121 *
122 * @see PopUpHelper#dispose()
123 */
124 public void dispose() {
125 if (isShowing()) {
126 timer.cancel();
127 hide();
128 }
129 getShell().dispose();
130 }
131
132 /**
133 * @see PopUpHelper#hookShellListeners()
134 */
135 protected void hookShellListeners() {
136 // Close the tooltip window if the mouse enters the tooltip
137 getShell().addMouseTrackListener(new class() MouseTrackAdapter {
138 public void mouseEnter(dwt.events.MouseEvent.MouseEvent e) {
139 hide();
140 currentTipSource = null;
141 timer.cancel();
142 }
143 });
144 }
145
146 /**
147 * Displays the hover source's tooltip if a tooltip of another source is currently being
148 * displayed.
149 *
150 * @param figureUnderMouse the figure over which the cursor was when called
151 * @param tip the tooltip to be displayed
152 * @param eventX the x coordinate of the cursor
153 * @param eventY the y coordinate of the cursor
154 * @since 2.0
155 */
156 public void updateToolTip(IFigure figureUnderMouse, IFigure tip, int eventX, int eventY) {
157 /* If the cursor is not on any Figures, it has been moved
158 off of the control. Hide the tool tip. */
159 if (figureUnderMouse is null) {
160 if (isShowing()) {
161 hide();
162 timer.cancel();
163 }
164 }
165 // Makes tooltip appear without a hover event if a tip is currently being displayed
166 if (isShowing() && figureUnderMouse !is currentTipSource) {
167 hide();
168 timer.cancel();
169 displayToolTipNear(figureUnderMouse, tip, eventX, eventY);
170 } else if (!isShowing() && figureUnderMouse !is currentTipSource)
171 currentTipSource = null;
172 }
173
174 }