home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-24 | 11.0 KB | 363 lines |
-
- /* Copyright (C) Microsoft Corporation, 1996-1998. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Visual J++ 6.0. See this product's on-line documentation for detailed
- information regarding Microsoft code samples.
-
- */
-
- import wfc.app.*;
- import wfc.ui.*;
- import wfc.core.*;
- import wfc.win32.*;
- import wfc.util.*;
-
- public class TrayIcon extends Component {
- public static final int MB_LEFT = Control.MB_LEFT;
- public static final int MB_RIGHT = Control.MB_RIGHT;
- public static final int MB_MIDDLE = Control.MB_MIDDLE;
-
- private static final Object EVENT_MOUSEDOWN = new Object();
- private static final Object EVENT_MOUSEENTER = new Object();
- private static final Object EVENT_MOUSELEAVE = new Object();
- private static final Object EVENT_MOUSEMOVE = new Object();
- private static final Object EVENT_MOUSEUP = new Object();
- private static final Object EVENT_CLICK = new Object();
- private static final Object EVENT_DOUBLECLICK = new Object();
-
-
- // This API creates a unique message ID, and associates it with a string
- // that you specify. It allows multiple applications to get the same
- // ID for the same message, and to ensure that the ID doesn't collide with
- // other Win32 message IDs already defined. That's what we use it for here.
- private static final int WM_TRAYMOUSEMESSAGE = RegisterWindowMessage( "TrayIconNotification" ); // Windows.WM_USER + 1024;
- /** @dll.import( "user32", auto ) */
- private native static int RegisterWindowMessage( String messageName );
-
- private Icon icon = null;
- private String text = null;
- private int id = 0;
- private boolean visible = false;
- private boolean added = false;
- private boolean enabled = true;
- private TrayIconWindow window = null;
- private Menu contextMenu = null;
-
- private static int nextId = 0;
- private static NOTIFYICONDATA data = new NOTIFYICONDATA();
- static {
- data.uCallbackMessage = WM_TRAYMOUSEMESSAGE;
- }
-
- public TrayIcon() {
- id = ++nextId;
- window = new TrayIconWindow(this);
- }
-
- public void addOnMouseDown(MouseEventHandler value) {
- addEventHandler(EVENT_MOUSEDOWN, value);
- }
-
- public void addOnMouseMove(MouseEventHandler value) {
- addEventHandler(EVENT_MOUSEMOVE, value);
- }
-
- public void addOnMouseUp(MouseEventHandler value) {
- addEventHandler(EVENT_MOUSEUP, value);
- }
-
- protected void fireMouseEvent(Object key, MouseEvent e) {
- com.ms.lang.Delegate handler = getEventHandler(key);
- if (handler != null) ((MouseEventHandler)handler).invoke(this, e);
- }
-
- public void finalize() {
- updateIcon(false);
- window.destroyHandle();
- }
-
- public boolean getEnabled() {
- return enabled;
- }
-
- public String getText() {
- return text;
- }
-
- public Menu getContextMenu() {
- return contextMenu;
- }
-
- public Icon getIcon() {
- return icon;
- }
-
- public boolean getVisible() {
- return visible;
- }
-
- protected void onMouseDown(MouseEvent e) {
- if (enabled)
- fireMouseEvent(EVENT_MOUSEDOWN, e);
- }
-
- protected void onMouseMove(MouseEvent e) {
- if (enabled)
- fireMouseEvent(EVENT_MOUSEMOVE, e);
- }
-
- protected void onMouseUp(MouseEvent e) {
- if (enabled)
- fireMouseEvent(EVENT_MOUSEUP, e);
- }
-
- public void removeOnMouseDown(MouseEventHandler value) {
- removeEventHandler(EVENT_MOUSEDOWN, value);
- }
-
- public void removeOnMouseMove(MouseEventHandler value) {
- removeEventHandler(EVENT_MOUSEMOVE, value);
- }
-
- public void removeOnMouseUp(MouseEventHandler value) {
- removeEventHandler(EVENT_MOUSEUP, value);
- }
-
- public void setText(String text) {
- if (!Utils.equals(this.text, text)) {
- this.text = text;
- if (added) {
- updateIcon(true);
- }
- }
- }
-
- public void setContextMenu(Menu contextMenu) {
- this.contextMenu = contextMenu;
- }
-
- public void setIcon(Icon icon) {
- this.icon = icon;
- if (added) {
- updateIcon(true);
- }
- }
-
- public void setVisible(boolean value) {
- if (visible != value) {
- updateIcon(value);
- visible = value;
- }
- }
-
- public void setEnabled( boolean value ) {
- enabled = value;
- }
-
- private void showContextMenu() {
- if (contextMenu != null && enabled) {
- POINT pt = new POINT();
- Windows.GetCursorPos(pt);
- Windows.TrackPopupMenuEx(contextMenu.getHandle(),
- Windows.TPM_VERTICAL,
- pt.x,
- pt.y,
- window.getHandle(),
- null);
- }
- }
-
- private synchronized void updateIcon(boolean visible) {
- // Bail if in design mode...
- //
- ISite site = getSite();
- if (site != null) {
- if (site.getDesignMode()) {
- return;
- }
- }
-
- data.uFlags = Windows.NIF_MESSAGE;
- if (visible) {
- if (window.getHandle() == 0) {
- window.createHandle(new CreateParams());
- }
-
- data.hWnd = window.getHandle();
- }
- data.uID = id;
- data.hIcon = 0;
- data.szTip = null;
- if (icon != null) {
- data.uFlags |= Windows.NIF_ICON;
- data.hIcon = icon.getHandle();
- }
- if (text != null) {
- data.uFlags |= Windows.NIF_TIP;
- data.szTip = text;
- }
-
- if (visible) {
- if (!added) {
- Windows.Shell_NotifyIcon(Windows.NIM_ADD, data);
- added = true;
- }
- else {
- Windows.Shell_NotifyIcon(Windows.NIM_MODIFY, data);
- }
- }
- else if (added) {
- Windows.Shell_NotifyIcon(Windows.NIM_DELETE, data);
- added = false;
- }
- }
-
- private void wmMouseDown(Message m, int button, int clicks) {
- onMouseDown(new MouseEvent(button, clicks, 0, 0, 0));
- }
-
- private void wmMouseMove(Message m) {
- onMouseMove(new MouseEvent(Control.getMouseButtons(), 0, 0, 0, 0));
- }
-
- private void wmMouseUp(Message m, int button) {
- onMouseUp(new MouseEvent(button, 0, 0, 0, 0));
- }
-
- protected void wndProc(Message msg) {
- if (msg.msg == WM_TRAYMOUSEMESSAGE) {
- switch (msg.lParam) {
- case Windows.WM_LBUTTONDBLCLK:
- wmMouseDown(msg, MB_LEFT, 2);
- break;
- case Windows.WM_LBUTTONDOWN:
- wmMouseDown(msg, MB_LEFT, 1);
- break;
- case Windows.WM_LBUTTONUP:
- wmMouseUp(msg, MB_LEFT);
- break;
- case Windows.WM_MBUTTONDBLCLK:
- wmMouseDown(msg, MB_MIDDLE, 2);
- break;
- case Windows.WM_MBUTTONDOWN:
- wmMouseDown(msg, MB_MIDDLE, 1);
- break;
- case Windows.WM_MBUTTONUP:
- wmMouseUp(msg, MB_MIDDLE);
- break;
- case Windows.WM_MOUSEMOVE:
- wmMouseMove(msg);
- break;
- case Windows.WM_RBUTTONDBLCLK:
- wmMouseDown(msg, MB_RIGHT, 2);
- break;
- case Windows.WM_RBUTTONDOWN:
- if (contextMenu != null) {
- showContextMenu();
- }
- wmMouseDown(msg, MB_RIGHT, 1);
- break;
- case Windows.WM_RBUTTONUP:
- wmMouseUp(msg, MB_RIGHT);
- break;
- }
- }
- else if (msg.msg == Windows.WM_COMMAND) {
- if (0 == msg.lParam)
- if (Command.dispatchID(msg.wParam & 0xFFFF)) return;
- }
- }
-
- private static class TrayIconWindow extends Window {
- com.ms.vm.WeakReference ref;
- TrayIcon rootRef;
-
- TrayIconWindow(TrayIcon control) {
- ref = new com.ms.vm.WeakReference(control);
- }
-
- protected void wndProc(Message m) {
- TrayIcon control = (TrayIcon)ref.getReferent();
- control.wndProc(m);
- super.wndProc(m);
- }
- }
-
- public static class ClassInfo extends Component.ClassInfo {
-
- public static final PropertyInfo ContextMenu =
- new PropertyInfo(TrayIcon.class, "contextMenu", Menu.class,
- new DefaultValueAttribute(null),
- new DescriptionAttribute("The pop-up menu to show when the user right-clicks the icon"));
-
- public static final PropertyInfo Text =
- new PropertyInfo(TrayIcon.class, "text", String.class,
- CategoryAttribute.Appearance,
- new LocalizableAttribute(),
- new DescriptionAttribute("The text that will be displayed when the mouse hovers over the icon."));
-
- public static final PropertyInfo Icon =
- new PropertyInfo(TrayIcon.class, "icon", Icon.class,
- CategoryAttribute.Appearance,
- new LocalizableAttribute(),
- new DescriptionAttribute("The icon to display in the system tray."));
-
- public static final PropertyInfo Visible =
- new PropertyInfo(TrayIcon.class, "visible", Boolean.TYPE,
- CategoryAttribute.Behavior,
- new DefaultValueAttribute(Boolean.FALSE),
- new DescriptionAttribute("Determines whether the control is visible or hidden"));
-
- public static final PropertyInfo Enabled =
- new PropertyInfo( TrayIcon.class, "enabled", Boolean.TYPE,
- CategoryAttribute.Behavior,
- new DefaultValueAttribute( null ),
- new DescriptionAttribute( "Determines whether the control is enabled or disabled" )
- );
-
- public static final EventInfo OnMouseDown =
- new EventInfo(TrayIcon.class, "mouseDown", MouseEventHandler.class,
- CategoryAttribute.Mouse,
- new DescriptionAttribute("Occurs when a mouse button is pressed"));
-
- public static final EventInfo OnMouseMove =
- new EventInfo(TrayIcon.class, "mouseMove", MouseEventHandler.class,
- CategoryAttribute.Mouse,
- new DescriptionAttribute("Occurs when the mouse is moved"));
-
- public static final EventInfo OnMouseUp =
- new EventInfo(TrayIcon.class, "mouseUp", MouseEventHandler.class,
- CategoryAttribute.Mouse,
- new DescriptionAttribute("Occurs when a mouse button is released"));
-
- public void getAttributes( IAttributes attr ) {
- attr.add( new ShowInToolboxAttribute() );
- }
-
- public String getDefaultPropertyName() {
- return "text";
- }
-
- public String getDefaultEventName() {
- return "mouseDown";
- }
-
- public void getEvents(IEvents events) {
- super.getEvents(events);
- events.add(OnMouseDown);
- events.add(OnMouseMove);
- events.add(OnMouseUp);
- }
-
- public void getProperties(IProperties props) {
- super.getProperties(props);
- props.add(ContextMenu);
- props.add(Enabled);
- props.add(Icon);
- props.add(Text);
- props.add(Visible);
- }
- }
- }
-