/*******************************************************************************
* 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.wpf.*;
import org.eclipse.swt.internal.win32.*;
/**
* Instances of this class represent icons that can be placed on the
* system tray or task bar status area.
* <p>
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>(none)</dd>
* <dt><b>Events:</b></dt>
* <dd>DefaultSelection, MenuDetect, Selection</dd>
* </dl>
* </p><p>
* IMPORTANT: This class is <em>not</em> intended to be subclassed.
* </p>
*
* @see <a href="http://www.eclipse.org/swt/snippets/#tray">Tray, TrayItem snippets</a>
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
*
* @since 3.0
* @noextend This class is not intended to be subclassed by clients.
*/
public class TrayItem extends Item {
Tray parent;
int id;
ToolTip toolTip;
String toolTipText;
boolean visible = true;
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Tray</code>) and a style value
* describing its behavior and appearance. The item is added
* to the end of the items maintained by its parent.
* <p>
* The style value is either one of the style constants defined in
* class <code>SWT</code> which is applicable to instances of this
* class, or must be built by <em>bitwise OR</em>'ing together
* (that is, using the <code>int</code> "|" operator) two or more
* of those <code>SWT</code> style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
* </p>
*
* @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 <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @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 receiver is selected by the user, by sending
* it one of the messages defined in the <code>SelectionListener</code>
* interface.
* <p>
* <code>widgetSelected</code> is called when the receiver is selected
* <code>widgetDefaultSelected</code> is called when the receiver is double-clicked
* </p>
*
* @param listener the listener which should be notified when the receiver is selected by the user
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @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);
}
/**
* 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 <code>MenuDetectListener</code> interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @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);
}
protected void checkSubclass () {
if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
}
void createHandle () {
handle = OS.gcnew_NotifyIcon ();
if (handle == 0) error (SWT.ERROR_NO_HANDLES);
OS.NotifyIcon_Visible (handle, true);
}
void destroyWidget () {
parent.destroyItem (this);
releaseHandle ();
}
Point getLocation () {
return display.getCursorLocation ();
}
/**
* Returns the receiver's parent, which must be a <code>Tray</code>.
*
* @return the receiver's parent
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @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 <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @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 <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public String getToolTipText () {
checkWidget ();
return toolTipText;
}
/**
* Returns <code>true</code> if the receiver is visible and
* <code>false</code> otherwise.
*
* @return the receiver's visibility
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public boolean getVisible () {
checkWidget ();
return visible;
}
void HandleDoubleClick (int sender, int e) {
if (!checkEvent (e)) return;
if (hooks (SWT.DefaultSelection)) {
postEvent (SWT.DefaultSelection);
}
display.wakeThread ();
}
void HandleMouseDown (int sender, int e) {
if (!checkEvent (e)) return;
if (hooks (SWT.Selection)) {
postEvent (SWT.Selection);
}
display.wakeThread ();
}
void HandleMouseUp (int sender, int e) {
if (!checkEvent (e)) return;
if (hooks (SWT.MenuDetect)) {
int button = OS.FormsMouseEventArgs_Button (e);
if (button == OS.MouseButtons_Right) sendEvent (SWT.MenuDetect);
}
display.wakeThread ();
}
void hookEvents() {
super.hookEvents ();
int handler = OS.gcnew_FormsMouseEventHandler (jniRef, "HandleMouseDown");
if (handler == 0) error (SWT.ERROR_NO_HANDLES);
OS.NotifyIcon_MouseDown (handle, handler);
OS.GCHandle_Free (handler);
handler = OS.gcnew_FormsMouseEventHandler (jniRef, "HandleMouseUp");
if (handler == 0) error (SWT.ERROR_NO_HANDLES);
OS.NotifyIcon_MouseUp (handle, handler);
OS.GCHandle_Free (handler);
handler = OS.gcnew_EventHandler (jniRef, "HandleDoubleClick");
if (handler == 0) error (SWT.ERROR_NO_HANDLES);
OS.NotifyIcon_DoubleClick (handle, handler);
OS.GCHandle_Free (handler);
}
void releaseHandle () {
setImage (null);
super.releaseHandle ();
if (handle != 0) OS.GCHandle_Free (handle);
handle = 0;
parent = null;
}
/**
* 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 <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @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);
}
/**
* 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 <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @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);
}
/**
* Sets the receiver's image.
*
* @param image the new image
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public void setImage (Image image) {
checkWidget ();
if (image != null && image.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
super.setImage (image);
if (image != null) {
int format = OS.PixelFormats_Bgra32 ();
int newImage = OS.gcnew_FormatConvertedBitmap (image.handle, format, OS.BitmapSource_Palette(image.handle), 0);
int depth = OS.PixelFormat_BitsPerPixel (format);
OS.GCHandle_Free (format);
int width = OS.BitmapSource_PixelWidth (newImage);
int height = OS.BitmapSource_PixelHeight (newImage);
int scanlinePad = 4;
int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1)) / scanlinePad * scanlinePad;
byte[] buffer = new byte [bytesPerLine * height];
int rect = OS.Int32Rect_Empty ();
OS.BitmapSource_CopyPixels (newImage, rect, buffer, buffer.length, bytesPerLine);
OS.GCHandle_Free (rect);
OS.GCHandle_Free (newImage);
int bitmap = OS.gcnew_Bitmap (width, height, bytesPerLine, Win32.PixelFormat_Format32bppArgb, buffer);//TODO
int hIcon = OS.Bitmap_GetHicon (bitmap);
OS.GCHandle_Free (bitmap);
int icon = OS.Icon_FromHandle (hIcon);
OS.NotifyIcon_Icon (handle, icon);
OS.GCHandle_Free (icon);
} else {
OS.NotifyIcon_Icon (handle, 0);
}
}
/**
* 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 <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @since 3.2
*/
public void setToolTip (ToolTip toolTip) {
checkWidget ();
this.toolTip = toolTip;
}
/**
* 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.
* <p>
* 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.
* </p>
*
* @param string the new tool tip text (or null)
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public void setToolTipText (String value) {
checkWidget ();
toolTipText = value;
int strPtr = createDotNetString (value, false);
OS.NotifyIcon_Text (handle, strPtr);
OS.GCHandle_Free (strPtr);
}
/**
* Makes the receiver visible if the argument is <code>true</code>,
* and makes it invisible otherwise.
*
* @param visible the new visibility state
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public void setVisible (boolean visible) {
checkWidget ();
if (this.visible == visible) return;
if (visible) {
/*
* It is possible (but unlikely), that application
* code could have disposed the widget in the show
* event. If this happens, just return.
*/
sendEvent (SWT.Show);
if (isDisposed ()) return;
}
this.visible = visible;
OS.NotifyIcon_Visible (handle, visible);
if (!visible) sendEvent (SWT.Hide);
}
}