001: /*
002: * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.awt.*;
029: import java.awt.peer.SystemTrayPeer;
030:
031: public class XSystemTrayPeer implements SystemTrayPeer {
032: SystemTray target;
033: long tray_owner;
034: static XSystemTrayPeer peerInstance; // there is only one SystemTray peer per application
035:
036: final static XAtom _NET_SYSTEM_TRAY = XAtom
037: .get("_NET_SYSTEM_TRAY_S0");
038: final static XAtom _XEMBED_INFO = XAtom.get("_XEMBED_INFO");
039: final static XAtom _NET_SYSTEM_TRAY_OPCODE = XAtom
040: .get("_NET_SYSTEM_TRAY_OPCODE");
041: final static XAtom _NET_WM_ICON = XAtom.get("_NET_WM_ICON");
042: final static long SYSTEM_TRAY_REQUEST_DOCK = 0;
043:
044: XSystemTrayPeer(SystemTray target) {
045: this .target = target;
046: peerInstance = this ;
047:
048: XToolkit.awtLock();
049: try {
050: tray_owner = XlibWrapper.XGetSelectionOwner(XToolkit
051: .getDisplay(), _NET_SYSTEM_TRAY.getAtom());
052: } finally {
053: XToolkit.awtUnlock();
054: }
055: }
056:
057: public Dimension getTrayIconSize() {
058: return new Dimension(XTrayIconPeer.TRAY_ICON_HEIGHT,
059: XTrayIconPeer.TRAY_ICON_WIDTH);
060: }
061:
062: // ***********************************************************************
063: // ***********************************************************************
064:
065: void addTrayIcon(XTrayIconPeer tiPeer) throws AWTException {
066: tray_owner = 0;
067: XToolkit.awtLock();
068: try {
069: tray_owner = XlibWrapper.XGetSelectionOwner(XToolkit
070: .getDisplay(), _NET_SYSTEM_TRAY.getAtom());
071: } finally {
072: XToolkit.awtUnlock();
073: }
074:
075: if (tray_owner == 0) {
076: throw new AWTException("TrayIcon couldn't be displayed.");
077: }
078:
079: long tray_window = tiPeer.getWindow();
080: long data[] = new long[] { XEmbedHelper.XEMBED_VERSION,
081: XEmbedHelper.XEMBED_MAPPED };
082: long data_ptr = Native.card32ToData(data);
083:
084: _XEMBED_INFO.setAtomData(tray_window, data_ptr, data.length);
085:
086: sendMessage(tray_owner, SYSTEM_TRAY_REQUEST_DOCK, tray_window,
087: 0, 0);
088: }
089:
090: void sendMessage(long win, long msg, long data1, long data2,
091: long data3) {
092: XClientMessageEvent xev = new XClientMessageEvent();
093:
094: try {
095: xev.set_type(XlibWrapper.ClientMessage);
096: xev.set_window(win);
097: xev.set_format(32);
098: xev.set_message_type(_NET_SYSTEM_TRAY_OPCODE.getAtom());
099: xev.set_data(0, 0);
100: xev.set_data(1, msg);
101: xev.set_data(2, data1);
102: xev.set_data(3, data2);
103: xev.set_data(4, data3);
104:
105: XToolkit.awtLock();
106: try {
107: XlibWrapper.XSendEvent(XToolkit.getDisplay(), win,
108: false, XlibWrapper.NoEventMask, xev.pData);
109: } finally {
110: XToolkit.awtUnlock();
111: }
112: } finally {
113: xev.dispose();
114: }
115: }
116:
117: static XSystemTrayPeer getPeerInstance() {
118: return peerInstance;
119: }
120: }
|