comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/widgets/ToolTip.d @ 0:6dd524f61e62

add dwt win and basic java stuff
author Frank Benoit <benoit@tionex.de>
date Mon, 02 Mar 2009 14:44:16 +0100
parents
children 2e09b0e6857a
comparison
equal deleted inserted replaced
-1:000000000000 0:6dd524f61e62
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.ToolTip;
14
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.SWTException;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.internal.win32.OS;
22
23 import org.eclipse.swt.widgets.Widget;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.TrayItem;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.TypedListener;
28
29 import java.lang.all;
30
31 /**
32 * Instances of this class represent popup windows that are used
33 * to inform or warn the user.
34 * <p>
35 * <dl>
36 * <dt><b>Styles:</b></dt>
37 * <dd>BALLOON, ICON_ERROR, ICON_INFORMATION, ICON_WARNING</dd>
38 * <dt><b>Events:</b></dt>
39 * <dd>Selection</dd>
40 * </dl>
41 * </p><p>
42 * Note: Only one of the styles ICON_ERROR, ICON_INFORMATION,
43 * and ICON_WARNING may be specified.
44 * </p><p>
45 * IMPORTANT: This class is intended to be subclassed <em>only</em>
46 * within the SWT implementation.
47 * </p>
48 *
49 * @see <a href="http://www.eclipse.org/swt/snippets/#tooltips">Tool Tips snippets</a>
50 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
51 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
52 *
53 * @since 3.2
54 */
55
56 public class ToolTip : Widget {
57 Shell parent;
58 TrayItem item;
59 String text = "", message = "";
60 int id, x, y;
61 bool autoHide = true, hasLocation, visible;
62 static const int TIMER_ID = 100;
63
64 /**
65 * Constructs a new instance of this class given its parent
66 * and a style value describing its behavior and appearance.
67 * <p>
68 * The style value is either one of the style constants defined in
69 * class <code>SWT</code> which is applicable to instances of this
70 * class, or must be built by <em>bitwise OR</em>'ing together
71 * (that is, using the <code>int</code> "|" operator) two or more
72 * of those <code>SWT</code> style constants. The class description
73 * lists the style constants that are applicable to the class.
74 * Style bits are also inherited from superclasses.
75 * </p>
76 *
77 * @param parent a composite control which will be the parent of the new instance (cannot be null)
78 * @param style the style of control to construct
79 *
80 * @exception IllegalArgumentException <ul>
81 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
82 * </ul>
83 * @exception SWTException <ul>
84 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
85 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
86 * </ul>
87 *
88 * @see SWT#ICON_ERROR
89 * @see SWT#ICON_INFORMATION
90 * @see SWT#ICON_WARNING
91 * @see Widget#checkSubclass
92 * @see Widget#getStyle
93 */
94 public this (Shell parent, int style) {
95 super (parent, checkStyle (style));
96 this.parent = parent;
97 checkOrientation (parent);
98 parent.createToolTip (this);
99 }
100
101 static int checkStyle (int style) {
102 int mask = SWT.ICON_ERROR | SWT.ICON_INFORMATION | SWT.ICON_WARNING;
103 if ((style & mask) is 0) return style;
104 return checkBits (style, SWT.ICON_INFORMATION, SWT.ICON_WARNING, SWT.ICON_ERROR, 0, 0, 0);
105 }
106
107 /**
108 * Adds the listener to the collection of listeners who will
109 * be notified when the receiver is selected by the user, by sending
110 * it one of the messages defined in the <code>SelectionListener</code>
111 * interface.
112 * <p>
113 * <code>widgetSelected</code> is called when the receiver is selected.
114 * <code>widgetDefaultSelected</code> is not called.
115 * </p>
116 *
117 * @param listener the listener which should be notified when the receiver is selected by the user
118 *
119 * @exception IllegalArgumentException <ul>
120 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
121 * </ul>
122 * @exception SWTException <ul>
123 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
124 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
125 * </ul>
126 *
127 * @see SelectionListener
128 * @see #removeSelectionListener
129 * @see SelectionEvent
130 */
131 public void addSelectionListener (SelectionListener listener) {
132 checkWidget ();
133 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
134 TypedListener typedListener = new TypedListener(listener);
135 addListener (SWT.Selection,typedListener);
136 addListener (SWT.DefaultSelection,typedListener);
137 }
138
139 override void destroyWidget () {
140 if (parent !is null) parent.destroyToolTip (this);
141 releaseHandle ();
142 }
143
144 /**
145 * Returns <code>true</code> if the receiver is automatically
146 * hidden by the platform, and <code>false</code> otherwise.
147 *
148 * @return the receiver's auto hide state
149 *
150 * @exception SWTException <ul>
151 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
152 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
153 * </ul>
154 *
155 */
156 public bool getAutoHide () {
157 checkWidget();
158 return autoHide;
159 }
160
161 /**
162 * Returns the receiver's message, which will be an empty
163 * string if it has never been set.
164 *
165 * @return the receiver's message
166 *
167 * @exception SWTException <ul>
168 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
169 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
170 * </ul>
171 */
172 public String getMessage () {
173 checkWidget();
174 return message;
175 }
176
177 /**
178 * Returns the receiver's parent, which must be a <code>Shell</code>.
179 *
180 * @return the receiver's parent
181 *
182 * @exception SWTException <ul>
183 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
184 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
185 * </ul>
186 */
187 public Shell getParent () {
188 checkWidget ();
189 return parent;
190 }
191
192 /**
193 * Returns the receiver's text, which will be an empty
194 * string if it has never been set.
195 *
196 * @return the receiver's text
197 *
198 * @exception SWTException <ul>
199 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
200 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
201 * </ul>
202 */
203 public String getText () {
204 checkWidget();
205 return text;
206 }
207
208 /**
209 * Returns <code>true</code> if the receiver is visible, and
210 * <code>false</code> otherwise.
211 * <p>
212 * If one of the receiver's ancestors is not visible or some
213 * other condition makes the receiver not visible, this method
214 * may still indicate that it is considered visible even though
215 * it may not actually be showing.
216 * </p>
217 *
218 * @return the receiver's visibility state
219 *
220 * @exception SWTException <ul>
221 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
222 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
223 * </ul>
224 */
225 public bool getVisible () {
226 checkWidget();
227 static if (OS.IsWinCE) return false;
228 if (item !is null) return visible;
229 auto hwndToolTip_ = hwndToolTip ();
230 if (OS.SendMessage (hwndToolTip_, OS.TTM_GETCURRENTTOOL, 0, 0) !is 0) {
231 TOOLINFO lpti;
232 lpti.cbSize = OS.TOOLINFO_sizeof;
233 if (OS.SendMessage (hwndToolTip_, OS.TTM_GETCURRENTTOOL, 0, &lpti) !is 0) {
234 return (lpti.uFlags & OS.TTF_IDISHWND) is 0 && lpti.uId is id;
235 }
236 }
237 return false;
238 }
239
240 HWND hwndToolTip () {
241 return (style & SWT.BALLOON) !is 0 ? parent.balloonTipHandle () : parent.toolTipHandle ();
242 }
243
244 /**
245 * Returns <code>true</code> if the receiver is visible and all
246 * of the receiver's ancestors are visible and <code>false</code>
247 * otherwise.
248 *
249 * @return the receiver's visibility state
250 *
251 * @exception SWTException <ul>
252 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
253 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
254 * </ul>
255 *
256 * @see #getVisible
257 */
258 public bool isVisible () {
259 checkWidget ();
260 if (item !is null) return getVisible () && item.getVisible ();
261 return getVisible ();
262 }
263
264 override void releaseHandle () {
265 super.releaseHandle ();
266 parent = null;
267 item = null;
268 id = -1;
269 }
270
271 override void releaseWidget () {
272 super.releaseWidget ();
273 if (item is null) {
274 if (autoHide) {
275 auto hwndToolTip_ = hwndToolTip ();
276 if (OS.SendMessage (hwndToolTip_, OS.TTM_GETCURRENTTOOL, 0, 0) !is 0) {
277 TOOLINFO lpti;
278 lpti.cbSize = OS.TOOLINFO_sizeof;
279 if (OS.SendMessage (hwndToolTip_, OS.TTM_GETCURRENTTOOL, 0, &lpti) !is 0) {
280 if ((lpti.uFlags & OS.TTF_IDISHWND) is 0) {
281 if (lpti.uId is id) {
282 OS.SendMessage (hwndToolTip_, OS.TTM_TRACKACTIVATE, 0, &lpti);
283 OS.SendMessage (hwndToolTip_, OS.TTM_POP, 0, 0);
284 OS.KillTimer (hwndToolTip_, TIMER_ID);
285 }
286 }
287 }
288 }
289 }
290 }
291 if (item !is null && item.toolTip is this) {
292 item.toolTip = null;
293 }
294 item = null;
295 text = message = null;
296 }
297
298 /**
299 * Removes the listener from the collection of listeners who will
300 * be notified when the receiver is selected by the user.
301 *
302 * @param listener the listener which should no longer be notified
303 *
304 * @exception IllegalArgumentException <ul>
305 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
306 * </ul>
307 * @exception SWTException <ul>
308 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
309 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
310 * </ul>
311 *
312 * @see SelectionListener
313 * @see #addSelectionListener
314 */
315 public void removeSelectionListener (SelectionListener listener) {
316 checkWidget ();
317 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
318 if (eventTable is null) return;
319 eventTable.unhook (SWT.Selection, listener);
320 eventTable.unhook (SWT.DefaultSelection,listener);
321 }
322
323 /**
324 * Makes the receiver hide automatically when <code>true</code>,
325 * and remain visible when <code>false</code>.
326 *
327 * @param autoHide the auto hide state
328 *
329 * @exception SWTException <ul>
330 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
331 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
332 * </ul>
333 *
334 * @see #getVisible
335 * @see #setVisible
336 */
337 public void setAutoHide (bool autoHide) {
338 checkWidget ();
339 this.autoHide = autoHide;
340 //TODO - update when visible
341 }
342
343 /**
344 * Sets the location of the receiver, which must be a tooltip,
345 * to the point specified by the arguments which are relative
346 * to the display.
347 * <p>
348 * Note that this is different from most widgets where the
349 * location of the widget is relative to the parent.
350 * </p>
351 *
352 * @param x the new x coordinate for the receiver
353 * @param y the new y coordinate for the receiver
354 *
355 * @exception SWTException <ul>
356 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
357 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
358 * </ul>
359 */
360 public void setLocation (int x, int y) {
361 checkWidget ();
362 this.x = x;
363 this.y = y;
364 hasLocation = true;
365 //TODO - update when visible
366 }
367
368 /**
369 * Sets the location of the receiver, which must be a tooltip,
370 * to the point specified by the argument which is relative
371 * to the display.
372 * <p>
373 * Note that this is different from most widgets where the
374 * location of the widget is relative to the parent.
375 * </p><p>
376 * Note that the platform window manager ultimately has control
377 * over the location of tooltips.
378 * </p>
379 *
380 * @param location the new location for the receiver
381 *
382 * @exception IllegalArgumentException <ul>
383 * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
384 * </ul>
385 * @exception SWTException <ul>
386 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
387 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
388 * </ul>
389 */
390 public void setLocation (Point location) {
391 checkWidget ();
392 if (location is null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
393 setLocation (location.x, location.y);
394 }
395
396 /**
397 * Sets the receiver's message.
398 *
399 * @param string the new message
400 *
401 * @exception SWTException <ul>
402 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
403 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
404 * </ul>
405 */
406 public void setMessage (String string) {
407 checkWidget ();
408 // SWT extension: allow null string
409 //if (string is null) error (SWT.ERROR_NULL_ARGUMENT);
410 message = string;
411 //TODO - update when visible
412 }
413
414 /**
415 * Sets the receiver's text.
416 *
417 * @param string the new text
418 *
419 * @exception SWTException <ul>
420 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
421 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
422 * </ul>
423 */
424 public void setText (String string) {
425 checkWidget ();
426 // SWT extension: allow null string
427 //if (string is null) error (SWT.ERROR_NULL_ARGUMENT);
428 text = string;
429 //TODO - update when visible
430 }
431
432 /**
433 * Marks the receiver as visible if the argument is <code>true</code>,
434 * and marks it invisible otherwise.
435 * <p>
436 * If one of the receiver's ancestors is not visible or some
437 * other condition makes the receiver not visible, marking
438 * it visible may not actually cause it to be displayed.
439 * </p>
440 *
441 * @param visible the new visibility state
442 *
443 * @exception SWTException <ul>
444 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
445 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
446 * </ul>
447 */
448 public void setVisible (bool visible) {
449 checkWidget ();
450 static if (OS.IsWinCE) return;
451 if (visible is getVisible ()) return;
452 if (item is null) {
453 auto hwnd = parent.handle;
454 TOOLINFO lpti;
455 lpti.cbSize = OS.TOOLINFO_sizeof;
456 lpti.uId = id;
457 lpti.hwnd = hwnd;
458 auto hwndToolTip_ = hwndToolTip ();
459 Shell shell = parent.getShell ();
460 if (text.length !is 0) {
461 int icon = OS.TTI_NONE;
462 if ((style & SWT.ICON_INFORMATION) !is 0) icon = OS.TTI_INFO;
463 if ((style & SWT.ICON_WARNING) !is 0) icon = OS.TTI_WARNING;
464 if ((style & SWT.ICON_ERROR) !is 0) icon = OS.TTI_ERROR;
465 shell.setToolTipTitle (hwndToolTip_, text, cast(HICON) icon);
466 } else {
467 shell.setToolTipTitle (hwndToolTip_, null, null);
468 }
469 int maxWidth = 0;
470 if (OS.IsWinCE || OS.WIN32_VERSION < OS.VERSION (4, 10)) {
471 RECT rect;
472 OS.SystemParametersInfo (OS.SPI_GETWORKAREA, 0, &rect, 0);
473 maxWidth = (rect.right - rect.left) / 4;
474 } else {
475 auto hmonitor = OS.MonitorFromWindow (hwnd, OS.MONITOR_DEFAULTTONEAREST);
476 MONITORINFO lpmi;
477 lpmi.cbSize = MONITORINFO.sizeof;
478 OS.GetMonitorInfo (hmonitor, &lpmi);
479 maxWidth = (lpmi.rcWork.right - lpmi.rcWork.left) / 4;
480 }
481 OS.SendMessage (hwndToolTip_, OS.TTM_SETMAXTIPWIDTH, 0, maxWidth);
482 if (visible) {
483 int nX = x, nY = y;
484 if (!hasLocation) {
485 POINT pt;
486 if (OS.GetCursorPos (&pt)) {
487 nX = pt.x;
488 nY = pt.y;
489 }
490 }
491 int /*long*/ lParam = OS.MAKELPARAM (nX, nY);
492 OS.SendMessage (hwndToolTip_, OS.TTM_TRACKPOSITION, 0, lParam);
493
494 /*
495 * Feature in Windows. Windows will not show a tool tip
496 * if the cursor is outside the parent window (even on XP,
497 * TTM_POPUP will not do this). The fix is to temporarily
498 * move the cursor into the tool window, show the tool tip,
499 * and then restore the cursor.
500 */
501 POINT pt;
502 OS.GetCursorPos (&pt);
503 RECT rect;
504 OS.GetClientRect (hwnd, &rect);
505 OS.MapWindowPoints (hwnd, null, cast(POINT*) &rect, 2);
506 if (!OS.PtInRect (&rect, pt)) {
507 HCURSOR hCursor = OS.GetCursor ();
508 OS.SetCursor (null);
509 OS.SetCursorPos (rect.left, rect.top);
510 OS.SendMessage (hwndToolTip_, OS.TTM_TRACKACTIVATE, 1, &lpti);
511 OS.SetCursorPos (pt.x, pt.y);
512 OS.SetCursor (hCursor);
513 } else {
514 OS.SendMessage (hwndToolTip_, OS.TTM_TRACKACTIVATE, 1, &lpti);
515 }
516
517 int time = OS.SendMessage (hwndToolTip_, OS.TTM_GETDELAYTIME, OS.TTDT_AUTOPOP, 0);
518 OS.SetTimer (hwndToolTip_, TIMER_ID, time, null);
519 } else {
520 OS.SendMessage (hwndToolTip_, OS.TTM_TRACKACTIVATE, 0, &lpti);
521 OS.SendMessage (hwndToolTip_, OS.TTM_POP, 0, 0);
522 OS.KillTimer (hwndToolTip_, TIMER_ID);
523 }
524 return;
525 }
526 if (item !is null && OS.SHELL32_MAJOR >= 5) {
527 if (visible) {
528 NOTIFYICONDATA iconData;
529 TCHAR[] buffer1 = StrToTCHARs (0, text, true);
530 TCHAR[] buffer2 = StrToTCHARs (0, message, true);
531 static if (OS.IsUnicode) {
532 TCHAR [] szInfoTitle = iconData.szInfoTitle;
533 int length1 = Math.min (szInfoTitle.length - 1, buffer1.length);
534 System.arraycopy (buffer1, 0, szInfoTitle, 0, length1);
535 TCHAR [] szInfo = iconData.szInfo;
536 int length2 = Math.min (szInfo.length - 1, buffer2.length );
537 System.arraycopy (buffer2, 0, szInfo, 0, length2);
538 } else {
539 byte [] szInfoTitle = iconData.szInfoTitle;
540 int length = Math.min (szInfoTitle.length - 1, buffer1.length );
541 System.arraycopy (buffer1, 0, szInfoTitle, 0, length);
542 byte [] szInfo = iconData.szInfo;
543 int length2 = Math.min (szInfo.length - 1, buffer2.length );
544 System.arraycopy (buffer2, 0, szInfo, 0, length2);
545 }
546 Display display = item.getDisplay ();
547 iconData.cbSize = NOTIFYICONDATA.sizeof;
548 iconData.uID = item.id;
549 iconData.hWnd = display.hwndMessage;
550 iconData.uFlags = OS.NIF_INFO;
551 if ((style & SWT.ICON_INFORMATION) !is 0) iconData.dwInfoFlags = OS.NIIF_INFO;
552 if ((style & SWT.ICON_WARNING) !is 0) iconData.dwInfoFlags = OS.NIIF_WARNING;
553 if ((style & SWT.ICON_ERROR) !is 0) iconData.dwInfoFlags = OS.NIIF_ERROR;
554 sendEvent (SWT.Show);
555 this.visible = cast(bool) OS.Shell_NotifyIcon (OS.NIM_MODIFY, &iconData);
556 } else {
557 //TODO - hide the tray item
558 }
559 }
560 }
561 }
562