source: trunk/src/gui/util/qsystemtrayicon_pm.cpp@ 385

Last change on this file since 385 was 286, checked in by Dmitry A. Kuminov, 16 years ago

gui: QSystemTrayIcon: Implemented showMessage() using the fallback code (Qt-drawn balloons).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 7.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** Copyright (C) 2009 netlabs.org. OS/2 parts.
7**
8** This file is part of the QtGui module of the Qt Toolkit.
9**
10** $QT_BEGIN_LICENSE:LGPL$
11** Commercial Usage
12** Licensees holding valid Qt Commercial licenses may use this file in
13** accordance with the Qt Commercial License Agreement provided with the
14** Software or, alternatively, in accordance with the terms contained in
15** a written agreement between you and Nokia.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Nokia gives you certain
26** additional rights. These rights are described in the Nokia Qt LGPL
27** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
28** 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 are unsure which license is appropriate for your use, please
39** contact the sales department 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
54QT_BEGIN_NAMESPACE
55
56#define WM_XST_MYNOTIFY (WM_USER + 1000)
57
58static ULONG WM_XST_CREATED = xstGetSysTrayCreatedMsgId();
59static ULONG MaxTextLen = xstGetSysTrayMaxTextLen();
60
61class QSystemTrayIconSys : QWidget
62{
63public:
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
77protected:
78 bool pmEvent(QMSG *msg, MRESULT *result);
79
80private:
81 HPOINTER hIcon;
82 QPoint globalPos;
83 QSystemTrayIcon *q;
84};
85
86QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object)
87 : hIcon(NULLHANDLE), q(object)
88{
89}
90
91QSystemTrayIconSys::~QSystemTrayIconSys()
92{
93 if (hIcon != NULLHANDLE)
94 WinDestroyPointer(hIcon);
95}
96
97void QSystemTrayIconSys::createIcon()
98{
99 HPOINTER hIconToDestroy = hIcon;
100
101 hIcon = QPixmap::toPmHPOINTER(q->icon());
102
103 if (hIconToDestroy != NULLHANDLE)
104 WinDestroyPointer(hIconToDestroy);
105}
106
107void QSystemTrayIconSys::addToTray()
108{
109 QByteArray toolTip = q->toolTip().toLocal8Bit();
110 xstAddSysTrayIcon(winId(), 0, hIcon, toolTip.constData(), WM_XST_MYNOTIFY, 0);
111}
112
113void QSystemTrayIconSys::removeFromTray()
114{
115 xstRemoveSysTrayIcon(winId(), 0);
116}
117
118QRect QSystemTrayIconSys::geometry()
119{
120 QRect rect;
121 RECTL rcl;
122 if (xstQuerySysTrayIconRect(winId(), 0, &rcl)) {
123 int sh = QApplication::desktop()->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
132void QSystemTrayIconSys::updateIcon()
133{
134 createIcon();
135 xstReplaceSysTrayIcon(winId(), 0, hIcon);
136}
137
138void QSystemTrayIconSys::setToolTip()
139{
140 QByteArray toolTip = q->toolTip().toLocal8Bit();
141 xstSetSysTrayIconToolTip(winId(), 0, toolTip.constData());
142}
143
144void 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
160bool QSystemTrayIconSys::pmEvent(QMSG *msg, MRESULT *result)
161{
162 switch(msg->msg) {
163
164 case WM_XST_MYNOTIFY: {
165 switch (SHORT2FROMMP(msg->mp1)) {
166 case XST_IN_MOUSE: {
167 PXSTMOUSEMSG pMsg = (PXSTMOUSEMSG)msg->mp2;
168 switch (pMsg->ulMouseMsg) {
169 case WM_BUTTON1CLICK:
170 emit q->activated(QSystemTrayIcon::Trigger);
171 break;
172 case WM_BUTTON1DBLCLK:
173 emit q->activated(QSystemTrayIcon::DoubleClick);
174 break;
175 case WM_BUTTON3CLICK:
176 emit q->activated(QSystemTrayIcon::MiddleClick);
177 break;
178 default:
179 break;
180 }
181 break;
182 }
183 case XST_IN_CONTEXT: {
184 PXSTCONTEXTMSG pMsg = (PXSTCONTEXTMSG)msg->mp2;
185 if (q->contextMenu()) {
186 QPoint gpos(pMsg->ptsPointerPos.x,
187 // flip y coordinate
188 QApplication::desktop()->height() -
189 (pMsg->ptsPointerPos.y + 1));
190 q->contextMenu()->popup(gpos);
191 q->contextMenu()->activateWindow();
192 // Must be activated for proper keyboardfocus and
193 // menu closing on OS/2
194 }
195 emit q->activated(QSystemTrayIcon::Context);
196 break;
197 }
198 default:
199 break;
200 }
201 break;
202 }
203
204 default: {
205 if (msg->msg == WM_XST_CREATED) {
206 addToTray();
207 return true;
208 }
209 break;
210 }
211 }
212
213 return QWidget::pmEvent(msg, result);
214}
215
216void QSystemTrayIconPrivate::install_sys()
217{
218 Q_Q(QSystemTrayIcon);
219 if (!sys) {
220 // @todo make QSystemTrayIconSys a singleton and use different icon IDs
221 // to differentiate between icons (this will save us from creating a new
222 // HWND per each icon the application wants to show)
223 sys = new QSystemTrayIconSys(q);
224 sys->createIcon();
225 sys->addToTray();
226 }
227}
228
229void QSystemTrayIconPrivate::
230showMessage_sys(const QString &title, const QString &message,
231 QSystemTrayIcon::MessageIcon type, int timeOut)
232{
233 if (!sys)
234 return;
235
236 sys->showMessage(title, message, type, timeOut);
237}
238
239QRect QSystemTrayIconPrivate::geometry_sys() const
240{
241 if (!sys)
242 return QRect();
243
244 return sys->geometry();
245}
246
247void QSystemTrayIconPrivate::remove_sys()
248{
249 if (!sys)
250 return;
251
252 sys->removeFromTray();
253 delete sys;
254 sys = 0;
255}
256
257void QSystemTrayIconPrivate::updateIcon_sys()
258{
259 if (!sys)
260 return;
261
262 sys->updateIcon();
263}
264
265void QSystemTrayIconPrivate::updateMenu_sys()
266{
267 // nothing to do
268}
269
270void QSystemTrayIconPrivate::updateToolTip_sys()
271{
272 if (!sys)
273 return;
274
275 sys->setToolTip();
276}
277
278bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
279{
280 return xstQuerySysTrayVersion(0, 0, 0);
281}
282
283QT_END_NAMESPACE
284
285#endif // ifndef QT_NO_SYSTEMTRAYICON
286
Note: See TracBrowser for help on using the repository browser.