0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtGui module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "private/qt_x11_p.h"
|
|
43 |
#include "qlabel.h"
|
|
44 |
#include "qx11info_x11.h"
|
|
45 |
#include "qpainter.h"
|
|
46 |
#include "qpixmap.h"
|
|
47 |
#include "qbitmap.h"
|
|
48 |
#include "qevent.h"
|
|
49 |
#include "qapplication.h"
|
|
50 |
#include "qlist.h"
|
|
51 |
#include "qmenu.h"
|
|
52 |
#include "qtimer.h"
|
|
53 |
#include "qsystemtrayicon_p.h"
|
|
54 |
#include "qpaintengine.h"
|
|
55 |
|
|
56 |
#ifndef QT_NO_SYSTEMTRAYICON
|
|
57 |
QT_BEGIN_NAMESPACE
|
|
58 |
|
|
59 |
Window QSystemTrayIconSys::sysTrayWindow = XNone;
|
|
60 |
QList<QSystemTrayIconSys *> QSystemTrayIconSys::trayIcons;
|
|
61 |
QCoreApplication::EventFilter QSystemTrayIconSys::oldEventFilter = 0;
|
|
62 |
Atom QSystemTrayIconSys::sysTraySelection = XNone;
|
|
63 |
XVisualInfo QSystemTrayIconSys::sysTrayVisual = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
|
64 |
|
|
65 |
// Locate the system tray
|
|
66 |
Window QSystemTrayIconSys::locateSystemTray()
|
|
67 |
{
|
|
68 |
Display *display = QX11Info::display();
|
|
69 |
if (sysTraySelection == XNone) {
|
|
70 |
int screen = QX11Info::appScreen();
|
|
71 |
QString net_sys_tray = QString::fromLatin1("_NET_SYSTEM_TRAY_S%1").arg(screen);
|
|
72 |
sysTraySelection = XInternAtom(display, net_sys_tray.toLatin1(), False);
|
|
73 |
}
|
|
74 |
|
|
75 |
return XGetSelectionOwner(QX11Info::display(), sysTraySelection);
|
|
76 |
}
|
|
77 |
|
|
78 |
XVisualInfo* QSystemTrayIconSys::getSysTrayVisualInfo()
|
|
79 |
{
|
|
80 |
Display *display = QX11Info::display();
|
|
81 |
|
|
82 |
if (!sysTrayVisual.visual) {
|
|
83 |
Window win = locateSystemTray();
|
|
84 |
if (win != XNone) {
|
|
85 |
Atom actual_type;
|
|
86 |
int actual_format;
|
|
87 |
ulong nitems, bytes_remaining;
|
|
88 |
uchar *data = 0;
|
|
89 |
int result = XGetWindowProperty(display, win, ATOM(_NET_SYSTEM_TRAY_VISUAL), 0, 1,
|
|
90 |
False, XA_VISUALID, &actual_type,
|
|
91 |
&actual_format, &nitems, &bytes_remaining, &data);
|
|
92 |
VisualID vid = 0;
|
|
93 |
if (result == Success && data && actual_type == XA_VISUALID && actual_format == 32 &&
|
|
94 |
nitems == 1 && bytes_remaining == 0)
|
|
95 |
vid = *(VisualID*)data;
|
|
96 |
if (data)
|
|
97 |
XFree(data);
|
|
98 |
if (vid == 0)
|
|
99 |
return 0;
|
|
100 |
|
|
101 |
uint mask = VisualIDMask;
|
|
102 |
XVisualInfo *vi, rvi;
|
|
103 |
int count;
|
|
104 |
rvi.visualid = vid;
|
|
105 |
vi = XGetVisualInfo(display, mask, &rvi, &count);
|
|
106 |
if (vi) {
|
|
107 |
sysTrayVisual = vi[0];
|
|
108 |
XFree((char*)vi);
|
|
109 |
}
|
|
110 |
if (sysTrayVisual.depth != 32)
|
|
111 |
memset(&sysTrayVisual, 0, sizeof(sysTrayVisual));
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
return sysTrayVisual.visual ? &sysTrayVisual : 0;
|
|
116 |
}
|
|
117 |
|
|
118 |
bool QSystemTrayIconSys::sysTrayTracker(void *message, long *result)
|
|
119 |
{
|
|
120 |
bool retval = false;
|
|
121 |
if (QSystemTrayIconSys::oldEventFilter)
|
|
122 |
retval = QSystemTrayIconSys::oldEventFilter(message, result);
|
|
123 |
|
|
124 |
if (trayIcons.isEmpty())
|
|
125 |
return retval;
|
|
126 |
|
|
127 |
Display *display = QX11Info::display();
|
|
128 |
XEvent *ev = (XEvent *)message;
|
|
129 |
if (ev->type == DestroyNotify && ev->xany.window == sysTrayWindow) {
|
|
130 |
sysTrayWindow = locateSystemTray();
|
|
131 |
memset(&sysTrayVisual, 0, sizeof(sysTrayVisual));
|
|
132 |
for (int i = 0; i < trayIcons.count(); i++) {
|
|
133 |
if (sysTrayWindow == XNone) {
|
|
134 |
QBalloonTip::hideBalloon();
|
|
135 |
trayIcons[i]->hide(); // still no luck
|
|
136 |
trayIcons[i]->destroy();
|
|
137 |
trayIcons[i]->create();
|
|
138 |
} else
|
|
139 |
trayIcons[i]->addToTray(); // add it to the new tray
|
|
140 |
}
|
|
141 |
retval = true;
|
|
142 |
} else if (ev->type == ClientMessage && sysTrayWindow == XNone) {
|
|
143 |
static Atom manager_atom = XInternAtom(display, "MANAGER", False);
|
|
144 |
XClientMessageEvent *cm = (XClientMessageEvent *)message;
|
|
145 |
if ((cm->message_type == manager_atom) && ((Atom)cm->data.l[1] == sysTraySelection)) {
|
|
146 |
sysTrayWindow = cm->data.l[2];
|
|
147 |
memset(&sysTrayVisual, 0, sizeof(sysTrayVisual));
|
|
148 |
XSelectInput(display, sysTrayWindow, StructureNotifyMask);
|
|
149 |
for (int i = 0; i < trayIcons.count(); i++) {
|
|
150 |
trayIcons[i]->addToTray();
|
|
151 |
}
|
|
152 |
retval = true;
|
|
153 |
}
|
|
154 |
} else if (ev->type == PropertyNotify && ev->xproperty.atom == ATOM(_NET_SYSTEM_TRAY_VISUAL) &&
|
|
155 |
ev->xproperty.window == sysTrayWindow) {
|
|
156 |
memset(&sysTrayVisual, 0, sizeof(sysTrayVisual));
|
|
157 |
for (int i = 0; i < trayIcons.count(); i++) {
|
|
158 |
trayIcons[i]->addToTray();
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
return retval;
|
|
163 |
}
|
|
164 |
|
|
165 |
QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *q)
|
|
166 |
: QWidget(0, Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint),
|
|
167 |
q(q), colormap(0)
|
|
168 |
{
|
|
169 |
setAttribute(Qt::WA_AlwaysShowToolTips);
|
|
170 |
setAttribute(Qt::WA_QuitOnClose, false);
|
|
171 |
setAttribute(Qt::WA_NoSystemBackground, true);
|
|
172 |
setAttribute(Qt::WA_PaintOnScreen);
|
|
173 |
|
|
174 |
static bool eventFilterAdded = false;
|
|
175 |
Display *display = QX11Info::display();
|
|
176 |
if (!eventFilterAdded) {
|
|
177 |
oldEventFilter = qApp->setEventFilter(sysTrayTracker);
|
|
178 |
eventFilterAdded = true;
|
|
179 |
Window root = QX11Info::appRootWindow();
|
|
180 |
XWindowAttributes attr;
|
|
181 |
XGetWindowAttributes(display, root, &attr);
|
|
182 |
if ((attr.your_event_mask & StructureNotifyMask) != StructureNotifyMask) {
|
|
183 |
(void) QApplication::desktop(); // lame trick to ensure our event mask is not overridden
|
|
184 |
XSelectInput(display, root, attr.your_event_mask | StructureNotifyMask); // for MANAGER selection
|
|
185 |
}
|
|
186 |
}
|
|
187 |
if (trayIcons.isEmpty()) {
|
|
188 |
sysTrayWindow = locateSystemTray();
|
|
189 |
if (sysTrayWindow != XNone)
|
|
190 |
XSelectInput(display, sysTrayWindow, StructureNotifyMask); // track tray events
|
|
191 |
}
|
|
192 |
trayIcons.append(this);
|
|
193 |
setMouseTracking(true);
|
|
194 |
#ifndef QT_NO_TOOLTIP
|
|
195 |
setToolTip(q->toolTip());
|
|
196 |
#endif
|
|
197 |
if (sysTrayWindow != XNone)
|
|
198 |
addToTray();
|
|
199 |
}
|
|
200 |
|
|
201 |
QSystemTrayIconSys::~QSystemTrayIconSys()
|
|
202 |
{
|
|
203 |
trayIcons.removeAt(trayIcons.indexOf(this));
|
|
204 |
Display *display = QX11Info::display();
|
|
205 |
if (trayIcons.isEmpty()) {
|
|
206 |
if (sysTrayWindow == XNone)
|
|
207 |
return;
|
|
208 |
if (display)
|
|
209 |
XSelectInput(display, sysTrayWindow, 0); // stop tracking the tray
|
|
210 |
sysTrayWindow = XNone;
|
|
211 |
}
|
|
212 |
if (colormap)
|
|
213 |
XFreeColormap(display, colormap);
|
|
214 |
}
|
|
215 |
|
|
216 |
void QSystemTrayIconSys::addToTray()
|
|
217 |
{
|
|
218 |
Q_ASSERT(sysTrayWindow != XNone);
|
|
219 |
Display *display = QX11Info::display();
|
|
220 |
|
|
221 |
XVisualInfo *vi = getSysTrayVisualInfo();
|
|
222 |
if (vi && vi->visual) {
|
|
223 |
Window root = RootWindow(display, vi->screen);
|
|
224 |
Window p = root;
|
|
225 |
if (QWidget *pw = parentWidget())
|
|
226 |
p = pw->effectiveWinId();
|
|
227 |
colormap = XCreateColormap(display, root, vi->visual, AllocNone);
|
|
228 |
XSetWindowAttributes wsa;
|
|
229 |
wsa.background_pixmap = 0;
|
|
230 |
wsa.colormap = colormap;
|
|
231 |
wsa.background_pixel = 0;
|
|
232 |
wsa.border_pixel = 0;
|
|
233 |
Window wid = XCreateWindow(display, p, -1, -1, 1, 1,
|
|
234 |
0, vi->depth, InputOutput, vi->visual,
|
|
235 |
CWBackPixmap|CWBackPixel|CWBorderPixel|CWColormap, &wsa);
|
|
236 |
create(wid);
|
|
237 |
} else {
|
|
238 |
XSetWindowBackgroundPixmap(display, winId(), ParentRelative);
|
|
239 |
}
|
|
240 |
|
|
241 |
// GNOME, NET WM Specification
|
|
242 |
static Atom netwm_tray_atom = XInternAtom(display, "_NET_SYSTEM_TRAY_OPCODE", False);
|
|
243 |
long l[5] = { CurrentTime, SYSTEM_TRAY_REQUEST_DOCK, winId(), 0, 0 };
|
|
244 |
XEvent ev;
|
|
245 |
memset(&ev, 0, sizeof(ev));
|
|
246 |
ev.xclient.type = ClientMessage;
|
|
247 |
ev.xclient.window = sysTrayWindow;
|
|
248 |
ev.xclient.message_type = netwm_tray_atom;
|
|
249 |
ev.xclient.format = 32;
|
|
250 |
memcpy((char *)&ev.xclient.data, (const char *) l, sizeof(l));
|
|
251 |
XSendEvent(display, sysTrayWindow, False, 0, &ev);
|
|
252 |
setMinimumSize(22, 22); // required at least on GNOME
|
|
253 |
}
|
|
254 |
|
|
255 |
void QSystemTrayIconSys::updateIcon()
|
|
256 |
{
|
|
257 |
update();
|
|
258 |
}
|
|
259 |
|
|
260 |
void QSystemTrayIconSys::resizeEvent(QResizeEvent *re)
|
|
261 |
{
|
|
262 |
QWidget::resizeEvent(re);
|
|
263 |
updateIcon();
|
|
264 |
}
|
|
265 |
|
|
266 |
void QSystemTrayIconSys::paintEvent(QPaintEvent*)
|
|
267 |
{
|
|
268 |
QPainter p(this);
|
|
269 |
if (!getSysTrayVisualInfo()) {
|
|
270 |
const QRegion oldSystemClip = p.paintEngine()->systemClip();
|
|
271 |
const QRect clearedRect = oldSystemClip.boundingRect();
|
|
272 |
XClearArea(QX11Info::display(), winId(), clearedRect.x(), clearedRect.y(),
|
|
273 |
clearedRect.width(), clearedRect.height(), False);
|
|
274 |
QPaintEngine *pe = p.paintEngine();
|
|
275 |
pe->setSystemClip(clearedRect);
|
|
276 |
q->icon().paint(&p, rect());
|
|
277 |
pe->setSystemClip(oldSystemClip);
|
|
278 |
} else {
|
|
279 |
p.setCompositionMode(QPainter::CompositionMode_Source);
|
|
280 |
p.fillRect(rect(), Qt::transparent);
|
|
281 |
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
|
|
282 |
q->icon().paint(&p, rect());
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
void QSystemTrayIconSys::mousePressEvent(QMouseEvent *ev)
|
|
287 |
{
|
|
288 |
QPoint globalPos = ev->globalPos();
|
|
289 |
if (ev->button() == Qt::RightButton && q->contextMenu())
|
|
290 |
q->contextMenu()->popup(globalPos);
|
|
291 |
|
|
292 |
if (QBalloonTip::isBalloonVisible()) {
|
|
293 |
emit q->messageClicked();
|
|
294 |
QBalloonTip::hideBalloon();
|
|
295 |
}
|
|
296 |
|
|
297 |
if (ev->button() == Qt::LeftButton)
|
|
298 |
emit q->activated(QSystemTrayIcon::Trigger);
|
|
299 |
else if (ev->button() == Qt::RightButton)
|
|
300 |
emit q->activated(QSystemTrayIcon::Context);
|
|
301 |
else if (ev->button() == Qt::MidButton)
|
|
302 |
emit q->activated(QSystemTrayIcon::MiddleClick);
|
|
303 |
}
|
|
304 |
|
|
305 |
void QSystemTrayIconSys::mouseDoubleClickEvent(QMouseEvent *ev)
|
|
306 |
{
|
|
307 |
if (ev->button() == Qt::LeftButton)
|
|
308 |
emit q->activated(QSystemTrayIcon::DoubleClick);
|
|
309 |
}
|
|
310 |
|
|
311 |
void QSystemTrayIconSys::wheelEvent(QWheelEvent *e)
|
|
312 |
{
|
|
313 |
QApplication::sendEvent(q, e);
|
|
314 |
}
|
|
315 |
|
|
316 |
bool QSystemTrayIconSys::event(QEvent *e)
|
|
317 |
{
|
|
318 |
if (e->type() == QEvent::ToolTip) {
|
|
319 |
return QApplication::sendEvent(q, e);
|
|
320 |
}
|
|
321 |
return QWidget::event(e);
|
|
322 |
}
|
|
323 |
|
|
324 |
bool QSystemTrayIconSys::x11Event(XEvent *event)
|
|
325 |
{
|
|
326 |
if (event->type == ReparentNotify)
|
|
327 |
show();
|
|
328 |
return QWidget::x11Event(event);
|
|
329 |
}
|
|
330 |
|
|
331 |
////////////////////////////////////////////////////////////////////////////
|
|
332 |
void QSystemTrayIconPrivate::install_sys()
|
|
333 |
{
|
|
334 |
Q_Q(QSystemTrayIcon);
|
|
335 |
if (!sys)
|
|
336 |
sys = new QSystemTrayIconSys(q);
|
|
337 |
}
|
|
338 |
|
|
339 |
QRect QSystemTrayIconPrivate::geometry_sys() const
|
|
340 |
{
|
|
341 |
if (!sys)
|
|
342 |
return QRect();
|
|
343 |
return QRect(sys->mapToGlobal(QPoint(0, 0)), sys->size());
|
|
344 |
}
|
|
345 |
|
|
346 |
void QSystemTrayIconPrivate::remove_sys()
|
|
347 |
{
|
|
348 |
if (!sys)
|
|
349 |
return;
|
|
350 |
QBalloonTip::hideBalloon();
|
|
351 |
sys->hide(); // this should do the trick, but...
|
|
352 |
delete sys; // wm may resize system tray only for DestroyEvents
|
|
353 |
sys = 0;
|
|
354 |
}
|
|
355 |
|
|
356 |
void QSystemTrayIconPrivate::updateIcon_sys()
|
|
357 |
{
|
|
358 |
if (!sys)
|
|
359 |
return;
|
|
360 |
sys->updateIcon();
|
|
361 |
}
|
|
362 |
|
|
363 |
void QSystemTrayIconPrivate::updateMenu_sys()
|
|
364 |
{
|
|
365 |
|
|
366 |
}
|
|
367 |
|
|
368 |
void QSystemTrayIconPrivate::updateToolTip_sys()
|
|
369 |
{
|
|
370 |
if (!sys)
|
|
371 |
return;
|
|
372 |
#ifndef QT_NO_TOOLTIP
|
|
373 |
sys->setToolTip(toolTip);
|
|
374 |
#endif
|
|
375 |
}
|
|
376 |
|
|
377 |
bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
|
|
378 |
{
|
|
379 |
return QSystemTrayIconSys::locateSystemTray() != XNone;
|
|
380 |
}
|
|
381 |
|
|
382 |
void QSystemTrayIconPrivate::showMessage_sys(const QString &message, const QString &title,
|
|
383 |
QSystemTrayIcon::MessageIcon icon, int msecs)
|
|
384 |
{
|
|
385 |
if (!sys)
|
|
386 |
return;
|
|
387 |
QPoint g = sys->mapToGlobal(QPoint(0, 0));
|
|
388 |
QBalloonTip::showBalloon(icon, message, title, sys->q,
|
|
389 |
QPoint(g.x() + sys->width()/2, g.y() + sys->height()/2),
|
|
390 |
msecs);
|
|
391 |
}
|
|
392 |
|
|
393 |
QT_END_NAMESPACE
|
|
394 |
#endif //QT_NO_SYSTEMTRAYICON
|