2
* trayicon_x11.cpp - X11 trayicon (for use with KDE and GNOME)
3
* Copyright (C) 2003 Justin Karneges
4
* GNOME2 Notification Area support: Tomasz Sterna
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
#include<qapplication.h>
34
//#if QT_VERSION < 0x030200
35
extern Time qt_x_time;
38
//----------------------------------------------------------------------------
40
//----------------------------------------------------------------------------
42
// for Gnome2 Notification Area
43
static XErrorHandler old_handler = 0;
44
static int dock_xerror = 0;
45
extern "C" int dock_xerrhandler(Display* dpy, XErrorEvent* err)
47
dock_xerror = err->error_code;
48
return old_handler(dpy, err);
51
static void trap_errors()
54
old_handler = XSetErrorHandler(dock_xerrhandler);
57
static bool untrap_errors()
59
XSetErrorHandler(old_handler);
60
return (dock_xerror == 0);
63
static bool send_message(
64
Display* dpy, /* display */
65
Window w, /* sender (tray icon window) */
66
long message, /* message opcode */
67
long data1, /* message data 1 */
68
long data2, /* message data 2 */
69
long data3 /* message data 3 */
73
memset(&ev, 0, sizeof(ev));
74
ev.xclient.type = ClientMessage;
75
ev.xclient.window = w;
76
ev.xclient.message_type = XInternAtom (dpy, "_NET_SYSTEM_TRAY_OPCODE", False );
77
ev.xclient.format = 32;
78
ev.xclient.data.l[0] = CurrentTime;
79
ev.xclient.data.l[1] = message;
80
ev.xclient.data.l[2] = data1;
81
ev.xclient.data.l[3] = data2;
82
ev.xclient.data.l[4] = data3;
85
XSendEvent(dpy, w, False, NoEventMask, &ev);
87
return untrap_errors();
90
#define SYSTEM_TRAY_REQUEST_DOCK 0
91
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
92
#define SYSTEM_TRAY_CANCEL_MESSAGE 2
94
//----------------------------------------------------------------------------
95
// TrayIcon::TrayIconPrivate
96
//----------------------------------------------------------------------------
98
class TrayIcon::TrayIconPrivate : public QWidget
101
TrayIconPrivate(TrayIcon *object, int size);
102
~TrayIconPrivate() { }
104
virtual void initWM(WId icon);
106
virtual void setPixmap(const QPixmap &pm);
108
virtual void paintEvent(QPaintEvent *);
109
virtual void enterEvent(QEvent *);
110
virtual void mouseMoveEvent(QMouseEvent *e);
111
virtual void mousePressEvent(QMouseEvent *e);
112
virtual void mouseReleaseEvent(QMouseEvent *e);
113
virtual void mouseDoubleClickEvent(QMouseEvent *e);
114
virtual void closeEvent(QCloseEvent *e);
117
TrayIcon *iconObject;
122
TrayIcon::TrayIconPrivate::TrayIconPrivate(TrayIcon *object, int _size)
123
: QWidget(0, "psidock", WRepaintNoErase)
128
setFocusPolicy(NoFocus);
129
setBackgroundMode(X11ParentRelative);
131
setMinimumSize(size, size);
132
setMaximumSize(size, size);
135
// This base stuff is required by both FreeDesktop specification and WindowMaker
136
void TrayIcon::TrayIconPrivate::initWM(WId icon)
138
Display *dsp = x11Display();
139
WId leader = winId();
141
// set the class hint
142
XClassHint classhint;
143
classhint.res_name = (char*)"psidock";
144
classhint.res_class = (char*)"Psi";
145
XSetClassHint(dsp, leader, &classhint);
147
// set the Window Manager hints
149
hints = XGetWMHints(dsp, leader); // init hints
150
hints->flags = WindowGroupHint | IconWindowHint | StateHint; // set the window group hint
151
hints->window_group = leader; // set the window hint
152
hints->initial_state = WithdrawnState; // initial state
153
hints->icon_window = icon; // in WM, this should be winId() of separate widget
156
XSetWMHints(dsp, leader, hints); // set the window hints for WM to use.
160
void TrayIcon::TrayIconPrivate::setPixmap(const QPixmap &pm)
167
void TrayIcon::TrayIconPrivate::paintEvent(QPaintEvent *)
170
p.drawPixmap((width() - pix.width())/2, (height() - pix.height())/2, pix);
173
void TrayIcon::TrayIconPrivate::enterEvent(QEvent *e)
175
// Taken from KSystemTray..
176
//#if QT_VERSION < 0x030200
177
//if ( !qApp->focusWidget() ) {
179
memset(&ev, 0, sizeof(ev));
180
ev.xfocus.display = qt_xdisplay();
181
ev.xfocus.type = FocusIn;
182
ev.xfocus.window = winId();
183
ev.xfocus.mode = NotifyNormal;
184
ev.xfocus.detail = NotifyAncestor;
185
Time time = qt_x_time;
187
qApp->x11ProcessEvent( &ev );
191
QWidget::enterEvent(e);
194
void TrayIcon::TrayIconPrivate::mouseMoveEvent(QMouseEvent *e)
196
QApplication::sendEvent(iconObject, e);
199
void TrayIcon::TrayIconPrivate::mousePressEvent(QMouseEvent *e)
201
QApplication::sendEvent(iconObject, e);
204
void TrayIcon::TrayIconPrivate::mouseReleaseEvent(QMouseEvent *e)
206
QApplication::sendEvent(iconObject, e);
209
void TrayIcon::TrayIconPrivate::mouseDoubleClickEvent(QMouseEvent *e)
211
QApplication::sendEvent(iconObject, e);
214
void TrayIcon::TrayIconPrivate::closeEvent(QCloseEvent *e)
216
iconObject->gotCloseEvent();
220
//----------------------------------------------------------------------------
221
// TrayIconFreeDesktop
222
//----------------------------------------------------------------------------
224
class TrayIconFreeDesktop : public TrayIcon::TrayIconPrivate
227
TrayIconFreeDesktop(TrayIcon *object, const QPixmap &pm);
229
virtual bool x11Event(XEvent*);
232
TrayIconFreeDesktop::TrayIconFreeDesktop(TrayIcon *object, const QPixmap &pm)
233
: TrayIconPrivate(object, 22)
238
Display *dsp = x11Display();
240
// dock the widget (adapted from SIM-ICQ)
241
Screen *screen = XDefaultScreenOfDisplay(dsp); // get the screen
242
int screen_id = XScreenNumberOfScreen(screen); // and it's number
244
// tell X that we want to see ClientMessage and Deleted events, which
245
// are picked up by QApplication::x11EventFilter
246
Window root_window = QApplication::desktop()->winId();
247
XWindowAttributes attr;
249
XGetWindowAttributes(dsp, root_window, &attr);
250
XSelectInput(dsp, root_window, attr.your_event_mask | StructureNotifyMask);
253
snprintf(buf, sizeof(buf), "_NET_SYSTEM_TRAY_S%d", screen_id);
254
Atom selection_atom = XInternAtom(dsp, buf, false);
256
Window manager_window = XGetSelectionOwner(dsp, selection_atom);
257
if ( manager_window != None )
258
XSelectInput(dsp, manager_window, StructureNotifyMask);
262
if ( manager_window != None )
263
send_message(dsp, manager_window, SYSTEM_TRAY_REQUEST_DOCK, winId(), 0, 0);
270
// some KDE mumbo-jumbo... why is it there? anybody?
271
Atom kwm_dockwindow_atom = XInternAtom(dsp, "KWM_DOCKWINDOW", false);
272
Atom kde_net_system_tray_window_for_atom = XInternAtom(dsp, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", false);
275
XChangeProperty(dsp, winId(), kwm_dockwindow_atom, kwm_dockwindow_atom, 32, PropModeReplace, (uchar*)&data, 1);
276
XChangeProperty(dsp, winId(), kde_net_system_tray_window_for_atom, XA_WINDOW, 32, PropModeReplace, (uchar*)&data, 1);
281
bool TrayIconFreeDesktop::x11Event(XEvent *ev)
292
//----------------------------------------------------------------------------
293
// TrayIconWindowMaker
294
//----------------------------------------------------------------------------
296
class TrayIconWharf : public TrayIcon::TrayIconPrivate
299
TrayIconWharf(TrayIcon *object, const QPixmap &pm)
300
: TrayIconPrivate(object, 44)
302
// set the class hint
303
XClassHint classhint;
304
classhint.res_name = (char*)"psidock-wharf";
305
classhint.res_class = (char*)"Psi";
306
XSetClassHint(x11Display(), winId(), &classhint);
311
void setPixmap(const QPixmap &_pm)
314
QImage i = _pm.convertToImage();
315
i = i.scale(i.width() * 2, i.height() * 2);
316
pm.convertFromImage(i);
318
TrayIconPrivate::setPixmap(pm);
320
// thanks to Robert Spier for this:
321
// for some reason the repaint() isn't being honored, or isn't for
322
// the icon. So force one on the widget behind the icon
324
QPaintEvent pe( rect() );
329
class TrayIconWindowMaker : public TrayIcon::TrayIconPrivate
332
TrayIconWindowMaker(TrayIcon *object, const QPixmap &pm);
333
~TrayIconWindowMaker();
335
void setPixmap(const QPixmap &pm);
338
TrayIconWharf *wharf;
341
TrayIconWindowMaker::TrayIconWindowMaker(TrayIcon *object, const QPixmap &pm)
342
: TrayIconPrivate(object, 32)
344
wharf = new TrayIconWharf(object, pm);
346
initWM( wharf->winId() );
349
TrayIconWindowMaker::~TrayIconWindowMaker()
354
void TrayIconWindowMaker::setPixmap(const QPixmap &pm)
356
wharf->setPixmap(pm);
359
//----------------------------------------------------------------------------
361
//----------------------------------------------------------------------------
363
void TrayIcon::sysInstall()
369
d = (TrayIconPrivate *)(new TrayIconWindowMaker(this, pm));
371
d = (TrayIconPrivate *)(new TrayIconFreeDesktop(this, pm));
379
void TrayIcon::sysRemove()
388
void TrayIcon::sysUpdateIcon()
397
void TrayIcon::sysUpdateToolTip()
405
QToolTip::add(d, tip);