1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.graphics.*; 15 import org.eclipse.swt.internal.win32.*; 16 import org.eclipse.swt.*; 17 import org.eclipse.swt.events.*; 18 19 39 40 public class ToolTip extends Widget { 41 Shell parent; 42 TrayItem item; 43 String text = "", message = ""; 44 int id, x, y; 45 boolean autoHide = true, hasLocation, visible; 46 static final int TIMER_ID = 100; 47 48 78 public ToolTip (Shell parent, int style) { 79 super (parent, checkStyle (style)); 80 this.parent = parent; 81 checkOrientation (parent); 82 parent.createToolTip (this); 83 } 84 85 static int checkStyle (int style) { 86 int mask = SWT.ICON_ERROR | SWT.ICON_INFORMATION | SWT.ICON_WARNING; 87 if ((style & mask) == 0) return style; 88 return checkBits (style, SWT.ICON_INFORMATION, SWT.ICON_WARNING, SWT.ICON_ERROR, 0, 0, 0); 89 } 90 91 115 public void addSelectionListener (SelectionListener listener) { 116 checkWidget (); 117 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 118 TypedListener typedListener = new TypedListener(listener); 119 addListener (SWT.Selection,typedListener); 120 addListener (SWT.DefaultSelection,typedListener); 121 } 122 123 void destroyWidget () { 124 if (parent != null) parent.destroyToolTip (this); 125 releaseHandle (); 126 } 127 128 140 public boolean getAutoHide () { 141 checkWidget(); 142 return autoHide; 143 } 144 145 156 public String getMessage () { 157 checkWidget(); 158 return message; 159 } 160 161 171 public Shell getParent () { 172 checkWidget (); 173 return parent; 174 } 175 176 187 public String getText () { 188 checkWidget(); 189 return text; 190 } 191 192 209 public boolean getVisible () { 210 checkWidget(); 211 if (OS.IsWinCE) return false; 212 if (item != null) return visible; 213 int hwndToolTip = hwndToolTip (); 214 if (OS.SendMessage (hwndToolTip, OS.TTM_GETCURRENTTOOL, 0, 0) != 0) { 215 TOOLINFO lpti = new TOOLINFO (); 216 lpti.cbSize = TOOLINFO.sizeof; 217 if (OS.SendMessage (hwndToolTip, OS.TTM_GETCURRENTTOOL, 0, lpti) != 0) { 218 return (lpti.uFlags & OS.TTF_IDISHWND) == 0 && lpti.uId == id; 219 } 220 } 221 return false; 222 } 223 224 int hwndToolTip () { 225 return (style & SWT.BALLOON) != 0 ? parent.balloonTipHandle () : parent.toolTipHandle (); 226 } 227 228 242 public boolean isVisible () { 243 checkWidget (); 244 if (item != null) return getVisible () && item.getVisible (); 245 return getVisible (); 246 } 247 248 void releaseHandle () { 249 super.releaseHandle (); 250 parent = null; 251 item = null; 252 id = -1; 253 } 254 255 void releaseWidget () { 256 super.releaseWidget (); 257 if (item == null) { 258 if (autoHide) { 259 int hwndToolTip = hwndToolTip (); 260 if (OS.SendMessage (hwndToolTip, OS.TTM_GETCURRENTTOOL, 0, 0) != 0) { 261 TOOLINFO lpti = new TOOLINFO (); 262 lpti.cbSize = TOOLINFO.sizeof; 263 if (OS.SendMessage (hwndToolTip, OS.TTM_GETCURRENTTOOL, 0, lpti) != 0) { 264 if ((lpti.uFlags & OS.TTF_IDISHWND) == 0) { 265 if (lpti.uId == id) { 266 OS.SendMessage (hwndToolTip, OS.TTM_TRACKACTIVATE, 0, lpti); 267 OS.SendMessage (hwndToolTip, OS.TTM_POP, 0, 0); 268 OS.KillTimer (hwndToolTip, TIMER_ID); 269 } 270 } 271 } 272 } 273 } 274 } 275 if (item != null && item.toolTip == this) { 276 item.toolTip = null; 277 } 278 item = null; 279 text = message = null; 280 } 281 282 299 public void removeSelectionListener (SelectionListener listener) { 300 checkWidget (); 301 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 302 if (eventTable == null) return; 303 eventTable.unhook (SWT.Selection, listener); 304 eventTable.unhook (SWT.DefaultSelection,listener); 305 } 306 307 321 public void setAutoHide (boolean autoHide) { 322 checkWidget (); 323 this.autoHide = autoHide; 324 } 326 327 344 public void setLocation (int x, int y) { 345 checkWidget (); 346 this.x = x; 347 this.y = y; 348 hasLocation = true; 349 } 351 352 374 public void setLocation (Point location) { 375 checkWidget (); 376 if (location == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 377 setLocation (location.x, location.y); 378 } 379 380 393 public void setMessage (String string) { 394 checkWidget (); 395 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 396 message = string; 397 } 399 400 413 public void setText (String string) { 414 checkWidget (); 415 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 416 text = string; 417 } 419 420 436 public void setVisible (boolean visible) { 437 checkWidget (); 438 if (OS.IsWinCE) return; 439 if (visible == getVisible ()) return; 440 if (item == null) { 441 int hwnd = parent.handle; 442 TOOLINFO lpti = new TOOLINFO (); 443 lpti.cbSize = TOOLINFO.sizeof; 444 lpti.uId = id; 445 lpti.hwnd = hwnd; 446 int hwndToolTip = hwndToolTip (); 447 Shell shell = parent.getShell (); 448 if (text.length () != 0) { 449 int icon = OS.TTI_NONE; 450 if ((style & SWT.ICON_INFORMATION) != 0) icon = OS.TTI_INFO; 451 if ((style & SWT.ICON_WARNING) != 0) icon = OS.TTI_WARNING; 452 if ((style & SWT.ICON_ERROR) != 0) icon = OS.TTI_ERROR; 453 shell.setToolTipTitle (hwndToolTip, text, icon); 454 } else { 455 shell.setToolTipTitle (hwndToolTip, null, 0); 456 } 457 int maxWidth = 0; 458 if (OS.IsWinCE || OS.WIN32_VERSION < OS.VERSION (4, 10)) { 459 RECT rect = new RECT (); 460 OS.SystemParametersInfo (OS.SPI_GETWORKAREA, 0, rect, 0); 461 maxWidth = (rect.right - rect.left) / 4; 462 } else { 463 int hmonitor = OS.MonitorFromWindow (hwnd, OS.MONITOR_DEFAULTTONEAREST); 464 MONITORINFO lpmi = new MONITORINFO (); 465 lpmi.cbSize = MONITORINFO.sizeof; 466 OS.GetMonitorInfo (hmonitor, lpmi); 467 maxWidth = (lpmi.rcWork_right - lpmi.rcWork_left) / 4; 468 } 469 OS.SendMessage (hwndToolTip, OS.TTM_SETMAXTIPWIDTH, 0, maxWidth); 470 if (visible) { 471 int nX = x, nY = y; 472 if (!hasLocation) { 473 POINT pt = new POINT (); 474 if (OS.GetCursorPos (pt)) { 475 nX = pt.x; 476 nY = pt.y; 477 } 478 } 479 int lParam = (nX & 0xFFFF) | ((nY << 16) & 0xFFFF0000); 480 OS.SendMessage (hwndToolTip, OS.TTM_TRACKPOSITION, 0, lParam); 481 482 489 POINT pt = new POINT (); 490 OS.GetCursorPos (pt); 491 RECT rect = new RECT (); 492 OS.GetClientRect (hwnd, rect); 493 OS.MapWindowPoints (hwnd, 0, rect, 2); 494 if (!OS.PtInRect (rect, pt)) { 495 int hCursor = OS.GetCursor (); 496 OS.SetCursor (0); 497 OS.SetCursorPos (rect.left, rect.top); 498 OS.SendMessage (hwndToolTip, OS.TTM_TRACKACTIVATE, 1, lpti); 499 OS.SetCursorPos (pt.x, pt.y); 500 OS.SetCursor (hCursor); 501 } else { 502 OS.SendMessage (hwndToolTip, OS.TTM_TRACKACTIVATE, 1, lpti); 503 } 504 505 int time = OS.SendMessage (hwndToolTip, OS.TTM_GETDELAYTIME, OS.TTDT_AUTOPOP, 0); 506 OS.SetTimer (hwndToolTip, TIMER_ID, time, 0); 507 } else { 508 OS.SendMessage (hwndToolTip, OS.TTM_TRACKACTIVATE, 0, lpti); 509 OS.SendMessage (hwndToolTip, OS.TTM_POP, 0, 0); 510 OS.KillTimer (hwndToolTip, TIMER_ID); 511 } 512 return; 513 } 514 if (item != null && OS.SHELL32_MAJOR >= 5) { 515 if (visible) { 516 NOTIFYICONDATA iconData = OS.IsUnicode ? (NOTIFYICONDATA) new NOTIFYICONDATAW () : new NOTIFYICONDATAA (); 517 TCHAR buffer1 = new TCHAR (0, text, true); 518 TCHAR buffer2 = new TCHAR (0, message, true); 519 if (OS.IsUnicode) { 520 char [] szInfoTitle = ((NOTIFYICONDATAW) iconData).szInfoTitle; 521 int length1 = Math.min (szInfoTitle.length - 1, buffer1.length ()); 522 System.arraycopy (buffer1.chars, 0, szInfoTitle, 0, length1); 523 char [] szInfo = ((NOTIFYICONDATAW) iconData).szInfo; 524 int length2 = Math.min (szInfo.length - 1, buffer2.length ()); 525 System.arraycopy (buffer2.chars, 0, szInfo, 0, length2); 526 } else { 527 byte [] szInfoTitle = ((NOTIFYICONDATAA) iconData).szInfoTitle; 528 int length = Math.min (szInfoTitle.length - 1, buffer1.length ()); 529 System.arraycopy (buffer1.bytes, 0, szInfoTitle, 0, length); 530 byte [] szInfo = ((NOTIFYICONDATAA) iconData).szInfo; 531 int length2 = Math.min (szInfo.length - 1, buffer2.length ()); 532 System.arraycopy (buffer2.bytes, 0, szInfo, 0, length2); 533 } 534 Display display = item.getDisplay (); 535 iconData.cbSize = NOTIFYICONDATA.sizeof; 536 iconData.uID = item.id; 537 iconData.hWnd = display.hwndMessage; 538 iconData.uFlags = OS.NIF_INFO; 539 if ((style & SWT.ICON_INFORMATION) != 0) iconData.dwInfoFlags = OS.NIIF_INFO; 540 if ((style & SWT.ICON_WARNING) != 0) iconData.dwInfoFlags = OS.NIIF_WARNING; 541 if ((style & SWT.ICON_ERROR) != 0) iconData.dwInfoFlags = OS.NIIF_ERROR; 542 sendEvent (SWT.Show); 543 this.visible = OS.Shell_NotifyIcon (OS.NIM_MODIFY, iconData); 544 } else { 545 } 547 } 548 } 549 } 550 | Popular Tags |