/******************************************************************************* * Copyright (c) 2000, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.eclipse.swt.widgets; import org.eclipse.swt.*; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.internal.cocoa.*; /** * Instances of this class represent icons that can be placed on the * system tray or task bar status area. *

*

*
Styles:
*
(none)
*
Events:
*
DefaultSelection, MenuDetect, Selection
*
*

* IMPORTANT: This class is not intended to be subclassed. *

* * @see Tray, TrayItem snippets * @see Sample code and further information * * @since 3.0 * @noextend This class is not intended to be subclassed by clients. */ public class TrayItem extends Item { Tray parent; ToolTip toolTip; String toolTipText; boolean visible = true, highlight; NSStatusItem item; NSImageView view; Image highlightImage; /** * Constructs a new instance of this class given its parent * (which must be a Tray) and a style value * describing its behavior and appearance. The item is added * to the end of the items maintained by its parent. *

* The style value is either one of the style constants defined in * class SWT which is applicable to instances of this * class, or must be built by bitwise OR'ing together * (that is, using the int "|" operator) two or more * of those SWT style constants. The class description * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. *

* * @param parent a composite control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * * @exception IllegalArgumentException * @exception SWTException * * @see SWT * @see Widget#checkSubclass * @see Widget#getStyle */ public TrayItem (Tray parent, int style) { super (parent, style); this.parent = parent; parent.createItem (this, parent.getItemCount ()); createWidget (); } /** * Adds the listener to the collection of listeners who will * be notified when the platform-specific context menu trigger * has occurred, by sending it one of the messages defined in * the MenuDetectListener interface. * * @param listener the listener which should be notified * * @exception IllegalArgumentException * @exception SWTException * * @see MenuDetectListener * @see #removeMenuDetectListener * * @since 3.3 */ public void addMenuDetectListener (MenuDetectListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.MenuDetect, typedListener); } /** * Adds the listener to the collection of listeners who will * be notified when the receiver is selected by the user, by sending * it one of the messages defined in the SelectionListener * interface. *

* widgetSelected is called when the receiver is selected * widgetDefaultSelected is called when the receiver is double-clicked *

* * @param listener the listener which should be notified when the receiver is selected by the user * * @exception IllegalArgumentException * @exception SWTException * * @see SelectionListener * @see #removeSelectionListener * @see SelectionEvent */ public void addSelectionListener(SelectionListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.Selection,typedListener); addListener (SWT.DefaultSelection,typedListener); } protected void checkSubclass () { if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); } void createHandle () { NSStatusBar statusBar = NSStatusBar.systemStatusBar(); item = statusBar.statusItemWithLength(0); if (item == null) error (SWT.ERROR_NO_HANDLES); item.retain(); item.setHighlightMode(true); view = (NSImageView)new SWTImageView().alloc(); if (view == null) error (SWT.ERROR_NO_HANDLES); view.init (); item.setView(view); } void deregister () { super.deregister (); display.removeWidget (view); display.removeWidget(view.cell()); } void destroyWidget () { parent.destroyItem (this); releaseHandle (); } /** * Returns the receiver's highlight image if it has one, or null * if it does not. * * @return the receiver's highlight image * * @exception SWTException * * @since 3.8 */ public Image getHighlightImage () { checkWidget(); return highlightImage; } Point getLocation () { NSRect rect = view.frame(); NSRect windowRect = view.window().frame(); NSPoint pt = new NSPoint(); pt.x = rect.width / 2; pt = view.convertPoint_fromView_(pt, null); pt.x += windowRect.x; return new Point ((int)pt.x, (int)pt.y); } /** * Returns the receiver's parent, which must be a Tray. * * @return the receiver's parent * * @exception SWTException * * @since 3.2 */ public Tray getParent () { checkWidget (); return parent; } /** * Returns the receiver's tool tip, or null if it has * not been set. * * @return the receiver's tool tip text * * @exception SWTException * * @since 3.2 */ public ToolTip getToolTip () { checkWidget (); return toolTip; } /** * Returns the receiver's tool tip text, or null if it has * not been set. * * @return the receiver's tool tip text * * @exception SWTException */ public String getToolTipText () { checkWidget (); return toolTipText; } /** * Returns true if the receiver is visible and * false otherwise. * * @return the receiver's visibility * * @exception SWTException */ public boolean getVisible () { checkWidget (); return visible; } void register () { super.register (); display.addWidget (view, this); display.addWidget (((NSControl)view).cell(), this); } void releaseHandle () { super.releaseHandle (); parent = null; if (item != null) item.release(); if (view != null) view.release(); item = null; view = null; } void releaseWidget () { super.releaseWidget (); NSStatusBar statusBar = NSStatusBar.systemStatusBar(); statusBar.removeStatusItem(item); if (toolTip != null) toolTip.item = null; toolTip = null; toolTipText = null; highlightImage = null; } /** * Removes the listener from the collection of listeners who will * be notified when the platform-specific context menu trigger has * occurred. * * @param listener the listener which should no longer be notified * * @exception IllegalArgumentException * @exception SWTException * * @see MenuDetectListener * @see #addMenuDetectListener * * @since 3.3 */ public void removeMenuDetectListener (MenuDetectListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.MenuDetect, listener); } /** * Removes the listener from the collection of listeners who will * be notified when the receiver is selected by the user. * * @param listener the listener which should no longer be notified * * @exception IllegalArgumentException * @exception SWTException * * @see SelectionListener * @see #addSelectionListener */ public void removeSelectionListener (SelectionListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Selection, listener); eventTable.unhook (SWT.DefaultSelection,listener); } /** * Sets the receiver's image. * * @param image the new image * * @exception IllegalArgumentException * @exception SWTException */ public void setImage (Image image) { checkWidget (); if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); super.setImage (image); updateImage (); } /** * Sets the receiver's highlight image. * * @param image the new highlight image * * @exception IllegalArgumentException * @exception SWTException * * @since 3.8 */ public void setHighlightImage (Image image) { checkWidget (); if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT); highlightImage = image; updateImage (); } /** * Sets the receiver's tool tip to the argument, which * may be null indicating that no tool tip should be shown. * * @param toolTip the new tool tip (or null) * * @exception SWTException * * @since 3.2 */ public void setToolTip (ToolTip toolTip) { checkWidget (); ToolTip oldTip = this.toolTip, newTip = toolTip; if (oldTip != null) oldTip.item = null; this.toolTip = newTip; if (newTip != null) newTip.item = this; } /** * Sets the receiver's tool tip text to the argument, which * may be null indicating that the default tool tip for the * control will be shown. For a control that has a default * tool tip, such as the Tree control on Windows, setting * the tool tip text to an empty string replaces the default, * causing no tool tip text to be shown. *

* The mnemonic indicator (character '&') is not displayed in a tool tip. * To display a single '&' in the tool tip, the character '&' can be * escaped by doubling it in the string. *

* * @param string the new tool tip text (or null) * * @exception SWTException */ public void setToolTipText (String string) { checkWidget (); toolTipText = string; _setToolTipText (string); } void _setToolTipText (String string) { if (string != null) { char[] chars = new char [string.length ()]; string.getChars (0, chars.length, chars, 0); int length = fixMnemonic (chars); NSString str = NSString.stringWithCharacters (chars, length); view.setToolTip (str); } else { view.setToolTip (null); } } /** * Makes the receiver visible if the argument is true, * and makes it invisible otherwise. * * @param visible the new visibility state * * @exception SWTException */ public void setVisible (boolean visible) { checkWidget (); if (this.visible == visible) return; if (visible) { sendEvent (SWT.Show); if (isDisposed ()) return; } this.visible = visible; updateImage (); if (!visible) sendEvent (SWT.Hide); } void showMenu (Menu menu) { display.trayItemMenu = menu; item.popUpStatusItemMenu(menu.nsMenu); } void showMenu () { _setToolTipText (null); Display display = this.display; display.currentTrayItem = this; sendEvent (SWT.MenuDetect); if (!isDisposed ()) display.runPopups(); display.currentTrayItem = null; if (isDisposed ()) return; _setToolTipText (toolTipText); } void displayMenu () { if (highlight) { view.display(); display.trayItemMenu = null; showMenu(); if (display.trayItemMenu != null) { display.trayItemMenu = null; highlight = false; view.setNeedsDisplay(true); } } } boolean shouldShowMenu (NSEvent event) { if (!hooks(SWT.MenuDetect)) return false; switch ((int)/*64*/event.type()) { case OS.NSRightMouseDown: return true; case OS.NSLeftMouseDown: if (!(hooks(SWT.Selection) || hooks(SWT.DefaultSelection))) { return true; } if ((event.modifierFlags() & OS.NSDeviceIndependentModifierFlagsMask) == OS.NSControlKeyMask) { return true; } return false; case OS.NSLeftMouseDragged: case OS.NSRightMouseDragged: return true; } return false; } void mouseDown(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { NSEvent nsEvent = new NSEvent(theEvent); highlight = true; updateImage(); view.setNeedsDisplay(true); if (shouldShowMenu(nsEvent)) { displayMenu(); updateImage (); } } void mouseDragged(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { NSEvent nsEvent = new NSEvent(theEvent); NSRect frame = view.frame(); boolean oldHighlight = highlight; highlight = OS.NSPointInRect(nsEvent.locationInWindow(), frame); if (oldHighlight != highlight) { updateImage (); } view.setNeedsDisplay(true); if (shouldShowMenu(nsEvent)) { displayMenu(); updateImage (); } } void mouseUp(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { if (highlight) { NSEvent nsEvent = new NSEvent(theEvent); if (nsEvent.type() == OS.NSLeftMouseUp) { sendSelectionEvent(nsEvent.clickCount() == 2 ? SWT.DefaultSelection : SWT.Selection); } highlight = false; updateImage (); } view.setNeedsDisplay(true); } void rightMouseDown(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { mouseDown(id, sel, theEvent); } void rightMouseUp(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { mouseUp(id, sel, theEvent); } void rightMouseDragged(int /*long*/ id, int /*long*/ sel, int /*long*/ theEvent) { mouseDragged(id, sel, theEvent); } void drawRect(int /*long*/ id, int /*long*/ sel, NSRect rect) { item.drawStatusBarBackgroundInRect(rect, highlight); super.drawRect(id, sel, rect); } void updateImage () { float /*double*/ width = 0; Image image = this.image; if (highlight && highlightImage != null) image = highlightImage; if (image == null) { view.setImage (null); } else { /* * Feature in Cocoa. If the NSImage object being set into the view is * the same NSImage object that is already there then the new image is * not taken. This results in the view's image not changing even if the * NSImage object's content has changed since it was last set into the * view. The workaround is to temporarily set the view's image to null * so that the new image will then be taken. */ NSImage current = view.image (); if (current != null && current.id == image.handle.id) { view.setImage (null); item.setLength (0); } view.setImage (image.handle); if (visible) { width = OS.NSSquareStatusItemLength; } } item.setLength (width); } }