1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** Copyright (C) 2010 netlabs.org. OS/2 parts.
|
---|
8 | **
|
---|
9 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
10 | **
|
---|
11 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
12 | ** Commercial Usage
|
---|
13 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
14 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
15 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
16 | ** a written agreement between you and Nokia.
|
---|
17 | **
|
---|
18 | ** GNU Lesser General Public License Usage
|
---|
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
20 | ** General Public License version 2.1 as published by the Free Software
|
---|
21 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
22 | ** packaging of this file. Please review the following information to
|
---|
23 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
24 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
25 | **
|
---|
26 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
27 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
29 | **
|
---|
30 | ** GNU General Public License Usage
|
---|
31 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
32 | ** General Public License version 3.0 as published by the Free Software
|
---|
33 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
34 | ** packaging of this file. Please review the following information to
|
---|
35 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
36 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
37 | **
|
---|
38 | ** If you have questions regarding the use of this file, please contact
|
---|
39 | ** Nokia at [email protected].
|
---|
40 | ** $QT_END_LICENSE$
|
---|
41 | **
|
---|
42 | ****************************************************************************/
|
---|
43 |
|
---|
44 | #include "qsystemtrayicon_p.h"
|
---|
45 |
|
---|
46 | #ifndef QT_NO_SYSTEMTRAYICON
|
---|
47 |
|
---|
48 | #include "qapplication.h"
|
---|
49 | #include "qdesktopwidget.h"
|
---|
50 | #include "qt_os2.h"
|
---|
51 |
|
---|
52 | #include "xsystray_api.h"
|
---|
53 |
|
---|
54 | QT_BEGIN_NAMESPACE
|
---|
55 |
|
---|
56 | #define WM_XST_MYNOTIFY (WM_USER + 1000)
|
---|
57 |
|
---|
58 | static ULONG WM_XST_CREATED = xstGetSysTrayCreatedMsgId();
|
---|
59 | static ULONG MaxTextLen = xstGetSysTrayMaxTextLen();
|
---|
60 |
|
---|
61 | class QSystemTrayIconSys : QWidget
|
---|
62 | {
|
---|
63 | public:
|
---|
64 | QSystemTrayIconSys(QSystemTrayIcon *object);
|
---|
65 | ~QSystemTrayIconSys();
|
---|
66 |
|
---|
67 | void createIcon();
|
---|
68 |
|
---|
69 | void addToTray();
|
---|
70 | void removeFromTray();
|
---|
71 | QRect geometry();
|
---|
72 | void updateIcon();
|
---|
73 | void setToolTip();
|
---|
74 | void showMessage(const QString &title, const QString &message,
|
---|
75 | QSystemTrayIcon::MessageIcon type, int timeOut);
|
---|
76 |
|
---|
77 | protected:
|
---|
78 | bool pmEvent(QMSG *msg, MRESULT *result);
|
---|
79 |
|
---|
80 | private:
|
---|
81 | HPOINTER hIcon;
|
---|
82 | QPoint globalPos;
|
---|
83 | QSystemTrayIcon *q;
|
---|
84 | };
|
---|
85 |
|
---|
86 | QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object)
|
---|
87 | : hIcon(NULLHANDLE), q(object)
|
---|
88 | {
|
---|
89 | }
|
---|
90 |
|
---|
91 | QSystemTrayIconSys::~QSystemTrayIconSys()
|
---|
92 | {
|
---|
93 | if (hIcon != NULLHANDLE)
|
---|
94 | WinDestroyPointer(hIcon);
|
---|
95 | }
|
---|
96 |
|
---|
97 | void QSystemTrayIconSys::createIcon()
|
---|
98 | {
|
---|
99 | HPOINTER hIconToDestroy = hIcon;
|
---|
100 |
|
---|
101 | hIcon = QPixmap::toPmHPOINTER(q->icon());
|
---|
102 |
|
---|
103 | if (hIconToDestroy != NULLHANDLE)
|
---|
104 | WinDestroyPointer(hIconToDestroy);
|
---|
105 | }
|
---|
106 |
|
---|
107 | void QSystemTrayIconSys::addToTray()
|
---|
108 | {
|
---|
109 | QByteArray toolTip = q->toolTip().toLocal8Bit();
|
---|
110 | xstAddSysTrayIcon(winId(), 0, hIcon, toolTip.constData(), WM_XST_MYNOTIFY, 0);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void QSystemTrayIconSys::removeFromTray()
|
---|
114 | {
|
---|
115 | xstRemoveSysTrayIcon(winId(), 0);
|
---|
116 | }
|
---|
117 |
|
---|
118 | QRect QSystemTrayIconSys::geometry()
|
---|
119 | {
|
---|
120 | QRect rect;
|
---|
121 | RECTL rcl;
|
---|
122 | if (xstQuerySysTrayIconRect(winId(), 0, &rcl)) {
|
---|
123 | int sh = qt_display_height();
|
---|
124 | // flip y coordinates
|
---|
125 | rcl.yTop = sh - rcl.yTop;
|
---|
126 | rcl.yBottom = sh - rcl.yBottom;
|
---|
127 | rect.setCoords(rcl.xLeft, rcl.yTop, rcl.xRight, rcl.yBottom);
|
---|
128 | }
|
---|
129 | return rect;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void QSystemTrayIconSys::updateIcon()
|
---|
133 | {
|
---|
134 | createIcon();
|
---|
135 | xstReplaceSysTrayIcon(winId(), 0, hIcon);
|
---|
136 | }
|
---|
137 |
|
---|
138 | void QSystemTrayIconSys::setToolTip()
|
---|
139 | {
|
---|
140 | QByteArray toolTip = q->toolTip().toLocal8Bit();
|
---|
141 | xstSetSysTrayIconToolTip(winId(), 0, toolTip.constData());
|
---|
142 | }
|
---|
143 |
|
---|
144 | void QSystemTrayIconSys::showMessage(const QString &title, const QString &message,
|
---|
145 | QSystemTrayIcon::MessageIcon type, int timeOut)
|
---|
146 | {
|
---|
147 | uint uSecs = 0;
|
---|
148 | if ( timeOut < 0)
|
---|
149 | uSecs = 10000; //10 sec default
|
---|
150 | else uSecs = (int)timeOut;
|
---|
151 |
|
---|
152 | // so far, we use fallbacks
|
---|
153 | // @todo use xstShowSysTrayIconBalloon() when it's implemented
|
---|
154 | QRect iconPos = geometry();
|
---|
155 | if (iconPos.isValid()) {
|
---|
156 | QBalloonTip::showBalloon(type, title, message, q, iconPos.center(), uSecs, true);
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | static void closeNormalPopups()
|
---|
161 | {
|
---|
162 | if (QApplication::activePopupWidget()) {
|
---|
163 | // The system tray area is actually another application so we close all
|
---|
164 | // normal popups for consistency (see qapplication_pm.cpp). In case some
|
---|
165 | // popup refuses to close, we give up after 1024 attempts (to avoid an
|
---|
166 | // infinite loop).
|
---|
167 | int maxiter = 1024;
|
---|
168 | QWidget *popup;
|
---|
169 | while ((popup = QApplication::activePopupWidget()) && maxiter--)
|
---|
170 | popup->close();
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | bool QSystemTrayIconSys::pmEvent(QMSG *msg, MRESULT *result)
|
---|
175 | {
|
---|
176 | switch(msg->msg) {
|
---|
177 |
|
---|
178 | case WM_XST_MYNOTIFY: {
|
---|
179 | switch (SHORT2FROMMP(msg->mp1)) {
|
---|
180 | case XST_IN_MOUSE: {
|
---|
181 | closeNormalPopups();
|
---|
182 | PXSTMOUSEMSG pMsg = (PXSTMOUSEMSG)msg->mp2;
|
---|
183 | switch (pMsg->ulMouseMsg) {
|
---|
184 | case WM_BUTTON1CLICK:
|
---|
185 | emit q->activated(QSystemTrayIcon::Trigger);
|
---|
186 | break;
|
---|
187 | case WM_BUTTON1DBLCLK:
|
---|
188 | emit q->activated(QSystemTrayIcon::DoubleClick);
|
---|
189 | break;
|
---|
190 | case WM_BUTTON3CLICK:
|
---|
191 | emit q->activated(QSystemTrayIcon::MiddleClick);
|
---|
192 | break;
|
---|
193 | default:
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | break;
|
---|
197 | }
|
---|
198 | case XST_IN_CONTEXT: {
|
---|
199 | closeNormalPopups();
|
---|
200 | if (QApplication::activePopupWidget()) {
|
---|
201 | // The system tray area is actually another application
|
---|
202 | // so we close all normal popups for consistency (see
|
---|
203 | // qapplication_pm.cpp). In case some popup refuses to
|
---|
204 | // close, we give up after 1024 attempts (to avoid an
|
---|
205 | // infinite loop).
|
---|
206 | int maxiter = 1024;
|
---|
207 | QWidget *popup;
|
---|
208 | while ((popup=QApplication::activePopupWidget()) && maxiter--)
|
---|
209 | popup->close();
|
---|
210 | }
|
---|
211 | PXSTCONTEXTMSG pMsg = (PXSTCONTEXTMSG)msg->mp2;
|
---|
212 | if (q->contextMenu()) {
|
---|
213 | QPoint gpos(pMsg->ptsPointerPos.x,
|
---|
214 | // flip y coordinate
|
---|
215 | qt_display_height() - (pMsg->ptsPointerPos.y + 1));
|
---|
216 | q->contextMenu()->popup(gpos);
|
---|
217 | q->contextMenu()->activateWindow();
|
---|
218 | // Must be activated for proper keyboardfocus and
|
---|
219 | // menu closing on OS/2
|
---|
220 | }
|
---|
221 | emit q->activated(QSystemTrayIcon::Context);
|
---|
222 | break;
|
---|
223 | }
|
---|
224 | case XST_IN_WHEEL: {
|
---|
225 | closeNormalPopups();
|
---|
226 | }
|
---|
227 | default:
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | break;
|
---|
231 | }
|
---|
232 |
|
---|
233 | default: {
|
---|
234 | if (msg->msg == WM_XST_CREATED) {
|
---|
235 | addToTray();
|
---|
236 | return true;
|
---|
237 | }
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | return QWidget::pmEvent(msg, result);
|
---|
243 | }
|
---|
244 |
|
---|
245 | void QSystemTrayIconPrivate::install_sys()
|
---|
246 | {
|
---|
247 | Q_Q(QSystemTrayIcon);
|
---|
248 | if (!sys) {
|
---|
249 | // @todo make QSystemTrayIconSys a singleton and use different icon IDs
|
---|
250 | // to differentiate between icons (this will save us from creating a new
|
---|
251 | // HWND per each icon the application wants to show)
|
---|
252 | sys = new QSystemTrayIconSys(q);
|
---|
253 | sys->createIcon();
|
---|
254 | sys->addToTray();
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | void QSystemTrayIconPrivate::
|
---|
259 | showMessage_sys(const QString &title, const QString &message,
|
---|
260 | QSystemTrayIcon::MessageIcon type, int timeOut)
|
---|
261 | {
|
---|
262 | if (!sys)
|
---|
263 | return;
|
---|
264 |
|
---|
265 | sys->showMessage(title, message, type, timeOut);
|
---|
266 | }
|
---|
267 |
|
---|
268 | QRect QSystemTrayIconPrivate::geometry_sys() const
|
---|
269 | {
|
---|
270 | if (!sys)
|
---|
271 | return QRect();
|
---|
272 |
|
---|
273 | return sys->geometry();
|
---|
274 | }
|
---|
275 |
|
---|
276 | void QSystemTrayIconPrivate::remove_sys()
|
---|
277 | {
|
---|
278 | if (!sys)
|
---|
279 | return;
|
---|
280 |
|
---|
281 | sys->removeFromTray();
|
---|
282 | delete sys;
|
---|
283 | sys = 0;
|
---|
284 | }
|
---|
285 |
|
---|
286 | void QSystemTrayIconPrivate::updateIcon_sys()
|
---|
287 | {
|
---|
288 | if (!sys)
|
---|
289 | return;
|
---|
290 |
|
---|
291 | sys->updateIcon();
|
---|
292 | }
|
---|
293 |
|
---|
294 | void QSystemTrayIconPrivate::updateMenu_sys()
|
---|
295 | {
|
---|
296 | // nothing to do
|
---|
297 | }
|
---|
298 |
|
---|
299 | void QSystemTrayIconPrivate::updateToolTip_sys()
|
---|
300 | {
|
---|
301 | if (!sys)
|
---|
302 | return;
|
---|
303 |
|
---|
304 | sys->setToolTip();
|
---|
305 | }
|
---|
306 |
|
---|
307 | bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
|
---|
308 | {
|
---|
309 | return xstQuerySysTrayVersion(0, 0, 0);
|
---|
310 | }
|
---|
311 |
|
---|
312 | QT_END_NAMESPACE
|
---|
313 |
|
---|
314 | #endif // ifndef QT_NO_SYSTEMTRAYICON
|
---|
315 |
|
---|