1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.events.*; 16 import org.eclipse.swt.graphics.*; 17 import org.eclipse.swt.internal.win32.*; 18 19 35 public class TrayItem extends Item { 36 Tray parent; 37 int id; 38 Image image2; 39 ToolTip toolTip; 40 String toolTipText; 41 boolean visible = true; 42 43 73 public TrayItem (Tray parent, int style) { 74 super (parent, style); 75 this.parent = parent; 76 parent.createItem (this, parent.getItemCount ()); 77 createWidget (); 78 } 79 80 104 public void addSelectionListener(SelectionListener listener) { 105 checkWidget (); 106 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 107 TypedListener typedListener = new TypedListener (listener); 108 addListener (SWT.Selection,typedListener); 109 addListener (SWT.DefaultSelection,typedListener); 110 } 111 112 133 public void addMenuDetectListener (MenuDetectListener listener) { 134 checkWidget (); 135 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 136 TypedListener typedListener = new TypedListener (listener); 137 addListener (SWT.MenuDetect, typedListener); 138 } 139 140 protected void checkSubclass () { 141 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); 142 } 143 144 void createWidget () { 145 NOTIFYICONDATA iconData = OS.IsUnicode ? (NOTIFYICONDATA) new NOTIFYICONDATAW () : new NOTIFYICONDATAA (); 146 iconData.cbSize = NOTIFYICONDATA.sizeof; 147 iconData.uID = id = display.nextTrayId++; 148 iconData.hWnd = display.hwndMessage; 149 iconData.uFlags = OS.NIF_MESSAGE; 150 iconData.uCallbackMessage = Display.SWT_TRAYICONMSG; 151 OS.Shell_NotifyIcon (OS.NIM_ADD, iconData); 152 } 153 154 void destroyWidget () { 155 parent.destroyItem (this); 156 releaseHandle (); 157 } 158 159 171 public Tray getParent () { 172 checkWidget (); 173 return parent; 174 } 175 176 189 public ToolTip getToolTip () { 190 checkWidget (); 191 return toolTip; 192 } 193 194 205 public String getToolTipText () { 206 checkWidget (); 207 return toolTipText; 208 } 209 210 221 public boolean getVisible () { 222 checkWidget (); 223 return visible; 224 } 225 226 int messageProc (int hwnd, int msg, int wParam, int lParam) { 227 238 switch (lParam) { 239 case OS.WM_LBUTTONDOWN: 240 if (hooks (SWT.Selection)) { 241 OS.SetForegroundWindow (hwnd); 242 postEvent (SWT.Selection); 243 } 244 break; 245 case OS.WM_LBUTTONDBLCLK: 246 case OS.WM_RBUTTONDBLCLK: 247 if (hooks (SWT.DefaultSelection)) { 248 OS.SetForegroundWindow (hwnd); 249 postEvent (SWT.DefaultSelection); 250 } 251 break; 252 case OS.WM_RBUTTONUP: { 253 if (hooks (SWT.MenuDetect)) { 254 OS.SetForegroundWindow (hwnd); 255 sendEvent (SWT.MenuDetect); 256 if (isDisposed()) return 0; 258 } 259 break; 260 } 261 case OS.NIN_BALLOONSHOW: 262 if (toolTip != null && !toolTip.visible) { 263 toolTip.visible = true; 264 if (toolTip.hooks (SWT.Show)) { 265 OS.SetForegroundWindow (hwnd); 266 toolTip.sendEvent (SWT.Show); 267 if (isDisposed()) return 0; 269 } 270 } 271 break; 272 case OS.NIN_BALLOONHIDE: 273 case OS.NIN_BALLOONTIMEOUT: 274 case OS.NIN_BALLOONUSERCLICK: 275 if (toolTip != null) { 276 if (toolTip.visible) { 277 toolTip.visible = false; 278 if (toolTip.hooks (SWT.Hide)) { 279 OS.SetForegroundWindow (hwnd); 280 toolTip.sendEvent (SWT.Hide); 281 if (isDisposed()) return 0; 283 } 284 } 285 if (lParam == OS.NIN_BALLOONUSERCLICK) { 286 if (toolTip.hooks (SWT.Selection)) { 287 OS.SetForegroundWindow (hwnd); 288 toolTip.postEvent (SWT.Selection); 289 if (isDisposed()) return 0; 291 } 292 } 293 } 294 break; 295 } 296 display.wakeThread (); 297 return 0; 298 } 299 300 void recreate () { 301 createWidget (); 302 if (!visible) setVisible (false); 303 if (text.length () != 0) setText (text); 304 if (image != null) setImage (image); 305 if (toolTipText != null) setToolTipText (toolTipText); 306 } 307 308 void releaseHandle () { 309 super.releaseHandle (); 310 parent = null; 311 } 312 313 void releaseWidget () { 314 super.releaseWidget (); 315 if (toolTip != null) toolTip.item = null; 316 toolTip = null; 317 if (image2 != null) image2.dispose (); 318 image2 = null; 319 toolTipText = null; 320 NOTIFYICONDATA iconData = OS.IsUnicode ? (NOTIFYICONDATA) new NOTIFYICONDATAW () : new NOTIFYICONDATAA (); 321 iconData.cbSize = NOTIFYICONDATA.sizeof; 322 iconData.uID = id; 323 iconData.hWnd = display.hwndMessage; 324 OS.Shell_NotifyIcon (OS.NIM_DELETE, iconData); 325 } 326 327 344 public void removeSelectionListener(SelectionListener listener) { 345 checkWidget (); 346 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 347 if (eventTable == null) return; 348 eventTable.unhook (SWT.Selection, listener); 349 eventTable.unhook (SWT.DefaultSelection,listener); 350 } 351 352 372 public void removeMenuDetectListener (MenuDetectListener listener) { 373 checkWidget (); 374 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 375 if (eventTable == null) return; 376 eventTable.unhook (SWT.MenuDetect, listener); 377 } 378 379 392 public void setImage (Image image) { 393 checkWidget (); 394 if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); 395 super.setImage (image); 396 if (image2 != null) image2.dispose (); 397 image2 = null; 398 int hIcon = 0; 399 Image icon = image; 400 if (icon != null) { 401 switch (icon.type) { 402 case SWT.BITMAP: 403 image2 = Display.createIcon (image); 404 hIcon = image2.handle; 405 break; 406 case SWT.ICON: 407 hIcon = icon.handle; 408 break; 409 } 410 } 411 NOTIFYICONDATA iconData = OS.IsUnicode ? (NOTIFYICONDATA) new NOTIFYICONDATAW () : new NOTIFYICONDATAA (); 412 iconData.cbSize = NOTIFYICONDATA.sizeof; 413 iconData.uID = id; 414 iconData.hWnd = display.hwndMessage; 415 iconData.hIcon = hIcon; 416 iconData.uFlags = OS.NIF_ICON; 417 OS.Shell_NotifyIcon (OS.NIM_MODIFY, iconData); 418 } 419 420 433 public void setToolTip (ToolTip toolTip) { 434 checkWidget (); 435 ToolTip oldTip = this.toolTip, newTip = toolTip; 436 if (oldTip != null) oldTip.item = null; 437 this.toolTip = newTip; 438 if (newTip != null) newTip.item = this; 439 } 440 441 452 public void setToolTipText (String value) { 453 checkWidget (); 454 toolTipText = value; 455 NOTIFYICONDATA iconData = OS.IsUnicode ? (NOTIFYICONDATA) new NOTIFYICONDATAW () : new NOTIFYICONDATAA (); 456 TCHAR buffer = new TCHAR (0, toolTipText == null ? "" : toolTipText, true); 457 460 int length = OS.SHELL32_MAJOR < 5 ? 64 : 128; 461 if (OS.IsUnicode) { 462 char [] szTip = ((NOTIFYICONDATAW) iconData).szTip; 463 length = Math.min (length - 1, buffer.length ()); 464 System.arraycopy (buffer.chars, 0, szTip, 0, length); 465 } else { 466 byte [] szTip = ((NOTIFYICONDATAA) iconData).szTip; 467 length = Math.min (length - 1, buffer.length ()); 468 System.arraycopy (buffer.bytes, 0, szTip, 0, length); 469 } 470 iconData.cbSize = NOTIFYICONDATA.sizeof; 471 iconData.uID = id; 472 iconData.hWnd = display.hwndMessage; 473 iconData.uFlags = OS.NIF_TIP; 474 OS.Shell_NotifyIcon (OS.NIM_MODIFY, iconData); 475 } 476 477 488 public void setVisible (boolean visible) { 489 checkWidget (); 490 if (this.visible == visible) return; 491 if (visible) { 492 497 sendEvent (SWT.Show); 498 if (isDisposed ()) return; 499 } 500 this.visible = visible; 501 NOTIFYICONDATA iconData = OS.IsUnicode ? (NOTIFYICONDATA) new NOTIFYICONDATAW () : new NOTIFYICONDATAA (); 502 iconData.cbSize = NOTIFYICONDATA.sizeof; 503 iconData.uID = id; 504 iconData.hWnd = display.hwndMessage; 505 if (OS.SHELL32_MAJOR < 5) { 506 if (visible) { 507 iconData.uFlags = OS.NIF_MESSAGE; 508 iconData.uCallbackMessage = Display.SWT_TRAYICONMSG; 509 OS.Shell_NotifyIcon (OS.NIM_ADD, iconData); 510 setImage (image); 511 setToolTipText (toolTipText); 512 } else { 513 OS.Shell_NotifyIcon (OS.NIM_DELETE, iconData); 514 } 515 } else { 516 iconData.uFlags = OS.NIF_STATE; 517 iconData.dwState = visible ? 0 : OS.NIS_HIDDEN; 518 iconData.dwStateMask = OS.NIS_HIDDEN; 519 OS.Shell_NotifyIcon (OS.NIM_MODIFY, iconData); 520 } 521 if (!visible) sendEvent (SWT.Hide); 522 } 523 524 } 525 | Popular Tags |