1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qsystemtrayicon_p.h"
|
---|
43 | #ifndef QT_NO_SYSTEMTRAYICON
|
---|
44 | #define _WIN32_IE 0x0600 //required for NOTIFYICONDATA_V2_SIZE
|
---|
45 |
|
---|
46 | #include <qt_windows.h>
|
---|
47 | #include <shlwapi.h>
|
---|
48 | #include <QApplication>
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | static const UINT q_uNOTIFYICONID = 13; // IDs from 0 to 12 are reserved on WinCE.
|
---|
53 | #define MYWM_NOTIFYICON (WM_APP+101)
|
---|
54 |
|
---|
55 | struct Q_NOTIFYICONIDENTIFIER {
|
---|
56 | DWORD cbSize;
|
---|
57 | HWND hWnd;
|
---|
58 | UINT uID;
|
---|
59 | GUID guidItem;
|
---|
60 | };
|
---|
61 |
|
---|
62 | class QSystemTrayIconSys : QWidget
|
---|
63 | {
|
---|
64 | public:
|
---|
65 | QSystemTrayIconSys(QSystemTrayIcon *object);
|
---|
66 | ~QSystemTrayIconSys();
|
---|
67 | bool winEvent( MSG *m, long *result );
|
---|
68 | bool trayMessage(DWORD msg);
|
---|
69 | void setIconContents(NOTIFYICONDATA &data);
|
---|
70 | void createIcon();
|
---|
71 | QRect findTrayGeometry();
|
---|
72 | HICON hIcon;
|
---|
73 | QPoint globalPos;
|
---|
74 | QSystemTrayIcon *q;
|
---|
75 | private:
|
---|
76 | uint notifyIconSize;
|
---|
77 | int maxTipLength;
|
---|
78 | bool ignoreNextMouseRelease;
|
---|
79 | };
|
---|
80 |
|
---|
81 | QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object)
|
---|
82 | : hIcon(0), q(object), ignoreNextMouseRelease(false)
|
---|
83 |
|
---|
84 | {
|
---|
85 | notifyIconSize = FIELD_OFFSET(NOTIFYICONDATA, szTip[64]); // NOTIFYICONDATAW_V1_SIZE;
|
---|
86 | maxTipLength = 64;
|
---|
87 | }
|
---|
88 |
|
---|
89 | QSystemTrayIconSys::~QSystemTrayIconSys()
|
---|
90 | {
|
---|
91 | if (hIcon)
|
---|
92 | DestroyIcon(hIcon);
|
---|
93 | }
|
---|
94 |
|
---|
95 | QRect QSystemTrayIconSys::findTrayGeometry()
|
---|
96 | {
|
---|
97 | // Use lower right corner as fallback
|
---|
98 | QPoint brCorner = qApp->desktop()->screenGeometry().bottomRight();
|
---|
99 | QRect ret(brCorner.x() - 10, brCorner.y() - 10, 10, 10);
|
---|
100 | return ret;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void QSystemTrayIconSys::setIconContents(NOTIFYICONDATA &tnd)
|
---|
104 | {
|
---|
105 | tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
|
---|
106 | tnd.uCallbackMessage = MYWM_NOTIFYICON;
|
---|
107 | tnd.hIcon = hIcon;
|
---|
108 | QString tip = q->toolTip();
|
---|
109 |
|
---|
110 | if (!tip.isNull()) {
|
---|
111 | tip = tip.left(maxTipLength - 1) + QChar();
|
---|
112 | memcpy(tnd.szTip, tip.utf16(), qMin(tip.length() + 1, maxTipLength) * sizeof(wchar_t));
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool QSystemTrayIconSys::trayMessage(DWORD msg)
|
---|
117 | {
|
---|
118 | NOTIFYICONDATA tnd;
|
---|
119 | memset(&tnd, 0, notifyIconSize);
|
---|
120 | tnd.uID = q_uNOTIFYICONID;
|
---|
121 | tnd.cbSize = notifyIconSize;
|
---|
122 | tnd.hWnd = winId();
|
---|
123 |
|
---|
124 | Q_ASSERT(testAttribute(Qt::WA_WState_Created));
|
---|
125 |
|
---|
126 | if (msg != NIM_DELETE) {
|
---|
127 | setIconContents(tnd);
|
---|
128 | }
|
---|
129 |
|
---|
130 | return Shell_NotifyIcon(msg, &tnd);
|
---|
131 | }
|
---|
132 |
|
---|
133 | void QSystemTrayIconSys::createIcon()
|
---|
134 | {
|
---|
135 | hIcon = 0;
|
---|
136 | QIcon icon = q->icon();
|
---|
137 | if (icon.isNull())
|
---|
138 | return;
|
---|
139 |
|
---|
140 | //const QSize preferredSize(GetSystemMetrics(SM_CXSMICON) * 2, GetSystemMetrics(SM_CYSMICON) * 2);
|
---|
141 | const QSize preferredSize(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
|
---|
142 | QPixmap pm = icon.pixmap(preferredSize);
|
---|
143 | if (pm.isNull())
|
---|
144 | return;
|
---|
145 |
|
---|
146 | hIcon = pm.toWinHICON();
|
---|
147 | }
|
---|
148 |
|
---|
149 | bool QSystemTrayIconSys::winEvent( MSG *m, long *result )
|
---|
150 | {
|
---|
151 | switch(m->message) {
|
---|
152 | case WM_CREATE:
|
---|
153 | SetWindowLong(winId(), GWL_USERDATA, (LONG)((CREATESTRUCTW*)m->lParam)->lpCreateParams);
|
---|
154 | break;
|
---|
155 |
|
---|
156 | case MYWM_NOTIFYICON:
|
---|
157 | {
|
---|
158 | QPoint gpos = QCursor::pos();
|
---|
159 |
|
---|
160 | switch (m->lParam) {
|
---|
161 | case WM_LBUTTONUP:
|
---|
162 | if (ignoreNextMouseRelease)
|
---|
163 | ignoreNextMouseRelease = false;
|
---|
164 | else
|
---|
165 | emit q->activated(QSystemTrayIcon::Trigger);
|
---|
166 | break;
|
---|
167 |
|
---|
168 | case WM_LBUTTONDBLCLK:
|
---|
169 | ignoreNextMouseRelease = true; // Since DBLCLICK Generates a second mouse
|
---|
170 | // release we must ignore it
|
---|
171 | emit q->activated(QSystemTrayIcon::DoubleClick);
|
---|
172 | break;
|
---|
173 |
|
---|
174 | case WM_RBUTTONUP:
|
---|
175 | if (q->contextMenu()) {
|
---|
176 | q->contextMenu()->popup(gpos);
|
---|
177 |
|
---|
178 | // We must ensure that the popup menu doesn't show up behind the task bar.
|
---|
179 | QRect desktopRect = qApp->desktop()->availableGeometry();
|
---|
180 | int maxY = desktopRect.y() + desktopRect.height() - q->contextMenu()->height();
|
---|
181 | if (gpos.y() > maxY) {
|
---|
182 | gpos.ry() = maxY;
|
---|
183 | q->contextMenu()->move(gpos);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | emit q->activated(QSystemTrayIcon::Context);
|
---|
187 | break;
|
---|
188 |
|
---|
189 | case WM_MBUTTONUP:
|
---|
190 | emit q->activated(QSystemTrayIcon::MiddleClick);
|
---|
191 | break;
|
---|
192 |
|
---|
193 | default:
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | break;
|
---|
197 | }
|
---|
198 | default:
|
---|
199 | return QWidget::winEvent(m, result);
|
---|
200 | }
|
---|
201 | return 0;
|
---|
202 | }
|
---|
203 |
|
---|
204 | void QSystemTrayIconPrivate::install_sys()
|
---|
205 | {
|
---|
206 | Q_Q(QSystemTrayIcon);
|
---|
207 | if (!sys) {
|
---|
208 | sys = new QSystemTrayIconSys(q);
|
---|
209 | sys->createIcon();
|
---|
210 | sys->trayMessage(NIM_ADD);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, int timeOut)
|
---|
215 | {
|
---|
216 | if (!sys)
|
---|
217 | return;
|
---|
218 |
|
---|
219 | uint uSecs = 0;
|
---|
220 | if ( timeOut < 0)
|
---|
221 | uSecs = 10000; //10 sec default
|
---|
222 | else uSecs = (int)timeOut;
|
---|
223 |
|
---|
224 | //message is limited to 255 chars + NULL
|
---|
225 | QString messageString;
|
---|
226 | if (message.isEmpty() && !title.isEmpty())
|
---|
227 | messageString = QLatin1Char(' '); //ensures that the message shows when only title is set
|
---|
228 | else
|
---|
229 | messageString = message.left(255) + QChar();
|
---|
230 |
|
---|
231 | //title is limited to 63 chars + NULL
|
---|
232 | QString titleString = title.left(63) + QChar();
|
---|
233 |
|
---|
234 | //show QBalloonTip
|
---|
235 | QRect trayRect = sys->findTrayGeometry();
|
---|
236 | QBalloonTip::showBalloon(type, title, message, sys->q, QPoint(trayRect.left(),
|
---|
237 | trayRect.center().y()), uSecs, false);
|
---|
238 | }
|
---|
239 |
|
---|
240 | QRect QSystemTrayIconPrivate::geometry_sys() const
|
---|
241 | {
|
---|
242 | return QRect();
|
---|
243 | }
|
---|
244 |
|
---|
245 | void QSystemTrayIconPrivate::remove_sys()
|
---|
246 | {
|
---|
247 | if (!sys)
|
---|
248 | return;
|
---|
249 |
|
---|
250 | sys->trayMessage(NIM_DELETE);
|
---|
251 | delete sys;
|
---|
252 | sys = 0;
|
---|
253 | }
|
---|
254 |
|
---|
255 | void QSystemTrayIconPrivate::updateIcon_sys()
|
---|
256 | {
|
---|
257 | if (!sys)
|
---|
258 | return;
|
---|
259 |
|
---|
260 | HICON hIconToDestroy = sys->hIcon;
|
---|
261 |
|
---|
262 | sys->createIcon();
|
---|
263 | sys->trayMessage(NIM_MODIFY);
|
---|
264 |
|
---|
265 | if (hIconToDestroy)
|
---|
266 | DestroyIcon(hIconToDestroy);
|
---|
267 | }
|
---|
268 |
|
---|
269 | void QSystemTrayIconPrivate::updateMenu_sys()
|
---|
270 | {
|
---|
271 |
|
---|
272 | }
|
---|
273 |
|
---|
274 | void QSystemTrayIconPrivate::updateToolTip_sys()
|
---|
275 | {
|
---|
276 | // Calling sys->trayMessage(NIM_MODIFY) on an existing icon is broken on Windows CE.
|
---|
277 | // So we need to call updateIcon_sys() which creates a new icon handle.
|
---|
278 | updateIcon_sys();
|
---|
279 | }
|
---|
280 |
|
---|
281 | bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
|
---|
282 | {
|
---|
283 | return true;
|
---|
284 | }
|
---|
285 |
|
---|
286 | bool QSystemTrayIconPrivate::supportsMessages_sys()
|
---|
287 | {
|
---|
288 | return true;
|
---|
289 | }
|
---|
290 |
|
---|
291 | QT_END_NAMESPACE
|
---|
292 |
|
---|
293 | #endif
|
---|