source: trunk/src/gui/util/qsystemtrayicon_win.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 15.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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** 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 qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qsystemtrayicon_p.h"
43#ifndef QT_NO_SYSTEMTRAYICON
44
45#ifndef _WIN32_WINNT
46#define _WIN32_WINNT 0x0600
47#endif
48
49#ifndef _WIN32_IE
50#define _WIN32_IE 0x600
51#endif
52
53#include <qt_windows.h>
54#include <windowsx.h>
55#include <commctrl.h>
56
57#include <private/qsystemlibrary_p.h>
58#include <QApplication>
59#include <QSettings>
60
61QT_BEGIN_NAMESPACE
62
63static const UINT q_uNOTIFYICONID = 0;
64
65static uint MYWM_TASKBARCREATED = 0;
66#define MYWM_NOTIFYICON (WM_APP+101)
67
68struct Q_NOTIFYICONIDENTIFIER {
69 DWORD cbSize;
70 HWND hWnd;
71 UINT uID;
72 GUID guidItem;
73};
74
75#ifndef NOTIFYICON_VERSION_4
76#define NOTIFYICON_VERSION_4 4
77#endif
78
79#ifndef NIN_SELECT
80#define NIN_SELECT (WM_USER + 0)
81#endif
82
83#ifndef NIN_KEYSELECT
84#define NIN_KEYSELECT (WM_USER + 1)
85#endif
86
87#ifndef NIN_BALLOONTIMEOUT
88#define NIN_BALLOONTIMEOUT (WM_USER + 4)
89#endif
90
91#ifndef NIN_BALLOONUSERCLICK
92#define NIN_BALLOONUSERCLICK (WM_USER + 5)
93#endif
94
95#ifndef NIF_SHOWTIP
96#define NIF_SHOWTIP 0x00000080
97#endif
98
99#define Q_MSGFLT_ALLOW 1
100
101typedef HRESULT (WINAPI *PtrShell_NotifyIconGetRect)(const Q_NOTIFYICONIDENTIFIER* identifier, RECT* iconLocation);
102typedef BOOL (WINAPI *PtrChangeWindowMessageFilter)(UINT message, DWORD dwFlag);
103typedef BOOL (WINAPI *PtrChangeWindowMessageFilterEx)(HWND hWnd, UINT message, DWORD action, void* pChangeFilterStruct);
104
105class QSystemTrayIconSys : QWidget
106{
107public:
108 QSystemTrayIconSys(QSystemTrayIcon *object);
109 ~QSystemTrayIconSys();
110 bool winEvent( MSG *m, long *result );
111 bool trayMessage(DWORD msg);
112 void setIconContents(NOTIFYICONDATA &data);
113 bool showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, uint uSecs);
114 QRect findIconGeometry(const int a_iButtonID);
115 void createIcon();
116 HICON hIcon;
117 QPoint globalPos;
118 QSystemTrayIcon *q;
119private:
120 uint notifyIconSize;
121 int maxTipLength;
122 int version;
123 bool ignoreNextMouseRelease;
124};
125
126static bool allowsMessages()
127{
128#ifndef QT_NO_SETTINGS
129 QSettings settings(QLatin1String("HKEY_CURRENT_USER\\Software\\Microsoft"
130 "\\Windows\\CurrentVersion\\Explorer\\Advanced"), QSettings::NativeFormat);
131 return settings.value(QLatin1String("EnableBalloonTips"), true).toBool();
132#else
133 return false;
134#endif
135}
136
137QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object)
138 : hIcon(0), q(object), ignoreNextMouseRelease(false)
139
140{
141 if (QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA) {
142 notifyIconSize = sizeof(NOTIFYICONDATA);
143 version = NOTIFYICON_VERSION_4;
144 } else {
145 notifyIconSize = NOTIFYICONDATA_V2_SIZE;
146 version = NOTIFYICON_VERSION;
147 }
148
149 maxTipLength = 128;
150
151 // For restoring the tray icon after explorer crashes
152 if (!MYWM_TASKBARCREATED) {
153 MYWM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated");
154 }
155
156 // Allow the WM_TASKBARCREATED message through the UIPI filter on Windows Vista and higher
157 static PtrChangeWindowMessageFilterEx pChangeWindowMessageFilterEx =
158 (PtrChangeWindowMessageFilterEx)QSystemLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilterEx");
159
160 if (pChangeWindowMessageFilterEx) {
161 // Call the safer ChangeWindowMessageFilterEx API if available
162 pChangeWindowMessageFilterEx(winId(), MYWM_TASKBARCREATED, Q_MSGFLT_ALLOW, 0);
163 } else {
164 static PtrChangeWindowMessageFilter pChangeWindowMessageFilter =
165 (PtrChangeWindowMessageFilter)QSystemLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilter");
166
167 if (pChangeWindowMessageFilter) {
168 // Call the deprecated ChangeWindowMessageFilter API otherwise
169 pChangeWindowMessageFilter(MYWM_TASKBARCREATED, Q_MSGFLT_ALLOW);
170 }
171 }
172}
173
174QSystemTrayIconSys::~QSystemTrayIconSys()
175{
176 if (hIcon)
177 DestroyIcon(hIcon);
178}
179
180void QSystemTrayIconSys::setIconContents(NOTIFYICONDATA &tnd)
181{
182 tnd.uFlags |= NIF_MESSAGE | NIF_ICON | NIF_TIP;
183 tnd.uCallbackMessage = MYWM_NOTIFYICON;
184 tnd.hIcon = hIcon;
185 QString tip = q->toolTip();
186
187 if (!tip.isNull()) {
188 tip = tip.left(maxTipLength - 1) + QChar();
189 memcpy(tnd.szTip, tip.utf16(), qMin(tip.length() + 1, maxTipLength) * sizeof(wchar_t));
190 }
191}
192
193static int iconFlag( QSystemTrayIcon::MessageIcon icon )
194{
195 switch (icon) {
196 case QSystemTrayIcon::Information:
197 return NIIF_INFO;
198 case QSystemTrayIcon::Warning:
199 return NIIF_WARNING;
200 case QSystemTrayIcon::Critical:
201 return NIIF_ERROR;
202 case QSystemTrayIcon::NoIcon:
203 return NIIF_NONE;
204 default:
205 Q_ASSERT_X(false, "QSystemTrayIconSys::showMessage", "Invalid QSystemTrayIcon::MessageIcon value");
206 return NIIF_NONE;
207 }
208}
209
210bool QSystemTrayIconSys::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, uint uSecs)
211{
212 NOTIFYICONDATA tnd;
213 memset(&tnd, 0, notifyIconSize);
214
215 memcpy(tnd.szInfo, message.utf16(), qMin(message.length() + 1, 256) * sizeof(wchar_t));
216 memcpy(tnd.szInfoTitle, title.utf16(), qMin(title.length() + 1, 64) * sizeof(wchar_t));
217
218 tnd.uID = q_uNOTIFYICONID;
219 tnd.dwInfoFlags = iconFlag(type);
220 tnd.cbSize = notifyIconSize;
221 tnd.hWnd = winId();
222 tnd.uTimeout = uSecs;
223 tnd.uFlags = NIF_INFO | NIF_SHOWTIP;
224
225 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
226
227 return Shell_NotifyIcon(NIM_MODIFY, &tnd);
228}
229
230bool QSystemTrayIconSys::trayMessage(DWORD msg)
231{
232 NOTIFYICONDATA tnd;
233 memset(&tnd, 0, notifyIconSize);
234
235 tnd.uID = q_uNOTIFYICONID;
236 tnd.cbSize = notifyIconSize;
237 tnd.hWnd = winId();
238 tnd.uFlags = NIF_SHOWTIP;
239 tnd.uVersion = version;
240
241 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
242
243 if (msg == NIM_ADD || msg == NIM_MODIFY) {
244 setIconContents(tnd);
245 }
246
247 bool success = Shell_NotifyIcon(msg, &tnd);
248
249 if (msg == NIM_ADD)
250 return success && Shell_NotifyIcon(NIM_SETVERSION, &tnd);
251 else
252 return success;
253}
254
255void QSystemTrayIconSys::createIcon()
256{
257 hIcon = 0;
258 QIcon icon = q->icon();
259 if (icon.isNull())
260 return;
261
262 const int iconSizeX = GetSystemMetrics(SM_CXSMICON);
263 const int iconSizeY = GetSystemMetrics(SM_CYSMICON);
264 QSize size = icon.actualSize(QSize(iconSizeX, iconSizeY));
265 QPixmap pm = icon.pixmap(size);
266 if (pm.isNull())
267 return;
268
269 hIcon = pm.toWinHICON();
270}
271
272bool QSystemTrayIconSys::winEvent( MSG *m, long *result )
273{
274 switch(m->message) {
275 case MYWM_NOTIFYICON:
276 {
277 int message = 0;
278 QPoint gpos;
279
280 if (version == NOTIFYICON_VERSION_4) {
281 Q_ASSERT(q_uNOTIFYICONID == HIWORD(m->lParam));
282 message = LOWORD(m->lParam);
283 gpos = QPoint(GET_X_LPARAM(m->wParam), GET_Y_LPARAM(m->wParam));
284 } else {
285 Q_ASSERT(q_uNOTIFYICONID == m->wParam);
286 message = m->lParam;
287 gpos = QCursor::pos();
288 }
289
290 switch (message) {
291 case NIN_SELECT:
292 case NIN_KEYSELECT:
293 if (ignoreNextMouseRelease)
294 ignoreNextMouseRelease = false;
295 else
296 emit q->activated(QSystemTrayIcon::Trigger);
297 break;
298
299 case WM_LBUTTONDBLCLK:
300 ignoreNextMouseRelease = true; // Since DBLCLICK Generates a second mouse
301 // release we must ignore it
302 emit q->activated(QSystemTrayIcon::DoubleClick);
303 break;
304
305 case WM_CONTEXTMENU:
306 if (q->contextMenu()) {
307 q->contextMenu()->popup(gpos);
308 }
309 emit q->activated(QSystemTrayIcon::Context);
310 break;
311
312 case NIN_BALLOONUSERCLICK:
313 emit q->messageClicked();
314 break;
315
316 case WM_MBUTTONUP:
317 emit q->activated(QSystemTrayIcon::MiddleClick);
318 break;
319
320 default:
321 break;
322 }
323 break;
324 }
325 default:
326 if (m->message == MYWM_TASKBARCREATED)
327 trayMessage(NIM_ADD);
328 else
329 return QWidget::winEvent(m, result);
330 break;
331 }
332 return 0;
333}
334
335void QSystemTrayIconPrivate::install_sys()
336{
337 Q_Q(QSystemTrayIcon);
338 if (!sys) {
339 sys = new QSystemTrayIconSys(q);
340 sys->createIcon();
341 sys->trayMessage(NIM_ADD);
342 }
343}
344
345/*
346* This function tries to determine the icon geometry from the tray
347*
348* If it fails an invalid rect is returned.
349*/
350QRect QSystemTrayIconSys::findIconGeometry(const int iconId)
351{
352 static PtrShell_NotifyIconGetRect Shell_NotifyIconGetRect =
353 (PtrShell_NotifyIconGetRect)QSystemLibrary::resolve(QLatin1String("shell32"), "Shell_NotifyIconGetRect");
354
355 if (Shell_NotifyIconGetRect) {
356 Q_NOTIFYICONIDENTIFIER nid;
357 memset(&nid, 0, sizeof(nid));
358 nid.cbSize = sizeof(nid);
359 nid.hWnd = winId();
360 nid.uID = iconId;
361
362 RECT rect;
363 HRESULT hr = Shell_NotifyIconGetRect(&nid, &rect);
364 if (SUCCEEDED(hr)) {
365 return QRect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
366 }
367 }
368
369 QRect ret;
370
371 TBBUTTON buttonData;
372 DWORD processID = 0;
373 HWND trayHandle = FindWindow(L"Shell_TrayWnd", NULL);
374
375 //find the toolbar used in the notification area
376 if (trayHandle) {
377 trayHandle = FindWindowEx(trayHandle, NULL, L"TrayNotifyWnd", NULL);
378 if (trayHandle) {
379 HWND hwnd = FindWindowEx(trayHandle, NULL, L"SysPager", NULL);
380 if (hwnd) {
381 hwnd = FindWindowEx(hwnd, NULL, L"ToolbarWindow32", NULL);
382 if (hwnd)
383 trayHandle = hwnd;
384 }
385 }
386 }
387
388 if (!trayHandle)
389 return ret;
390
391 GetWindowThreadProcessId(trayHandle, &processID);
392 if (processID <= 0)
393 return ret;
394
395 HANDLE trayProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, 0, processID);
396 if (!trayProcess)
397 return ret;
398
399 int buttonCount = SendMessage(trayHandle, TB_BUTTONCOUNT, 0, 0);
400 LPVOID data = VirtualAllocEx(trayProcess, NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE);
401
402 if ( buttonCount < 1 || !data ) {
403 CloseHandle(trayProcess);
404 return ret;
405 }
406
407 //search for our icon among all toolbar buttons
408 for (int toolbarButton = 0; toolbarButton < buttonCount; ++toolbarButton ) {
409 SIZE_T numBytes = 0;
410 DWORD appData[2] = { 0, 0 };
411 SendMessage(trayHandle, TB_GETBUTTON, toolbarButton , (LPARAM)data);
412
413 if (!ReadProcessMemory(trayProcess, data, &buttonData, sizeof(TBBUTTON), &numBytes))
414 continue;
415
416 if (!ReadProcessMemory(trayProcess, (LPVOID) buttonData.dwData, appData, sizeof(appData), &numBytes))
417 continue;
418
419 int currentIconId = appData[1];
420 HWND currentIconHandle = (HWND) appData[0];
421 bool isHidden = buttonData.fsState & TBSTATE_HIDDEN;
422
423 if (currentIconHandle == winId() &&
424 currentIconId == iconId && !isHidden) {
425 SendMessage(trayHandle, TB_GETITEMRECT, toolbarButton , (LPARAM)data);
426 RECT iconRect = {0, 0};
427 if(ReadProcessMemory(trayProcess, data, &iconRect, sizeof(RECT), &numBytes)) {
428 MapWindowPoints(trayHandle, NULL, (LPPOINT)&iconRect, 2);
429 QRect geometry(iconRect.left + 1, iconRect.top + 1,
430 iconRect.right - iconRect.left - 2,
431 iconRect.bottom - iconRect.top - 2);
432 if (geometry.isValid())
433 ret = geometry;
434 break;
435 }
436 }
437 }
438 VirtualFreeEx(trayProcess, data, 0, MEM_RELEASE);
439 CloseHandle(trayProcess);
440 return ret;
441}
442
443void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, int timeOut)
444{
445 if (!sys || !allowsMessages())
446 return;
447
448 uint uSecs = 0;
449 if ( timeOut < 0)
450 uSecs = 10000; //10 sec default
451 else uSecs = (int)timeOut;
452
453 //message is limited to 255 chars + NULL
454 QString messageString;
455 if (message.isEmpty() && !title.isEmpty())
456 messageString = QLatin1Char(' '); //ensures that the message shows when only title is set
457 else
458 messageString = message.left(255) + QChar();
459
460 //title is limited to 63 chars + NULL
461 QString titleString = title.left(63) + QChar();
462
463 sys->showMessage(titleString, messageString, type, uSecs);
464}
465
466QRect QSystemTrayIconPrivate::geometry_sys() const
467{
468 if (!sys)
469 return QRect();
470
471 return sys->findIconGeometry(q_uNOTIFYICONID);
472}
473
474void QSystemTrayIconPrivate::remove_sys()
475{
476 if (!sys)
477 return;
478
479 sys->trayMessage(NIM_DELETE);
480 delete sys;
481 sys = 0;
482}
483
484void QSystemTrayIconPrivate::updateIcon_sys()
485{
486 if (!sys)
487 return;
488
489 HICON hIconToDestroy = sys->hIcon;
490
491 sys->createIcon();
492 sys->trayMessage(NIM_MODIFY);
493
494 if (hIconToDestroy)
495 DestroyIcon(hIconToDestroy);
496}
497
498void QSystemTrayIconPrivate::updateMenu_sys()
499{
500
501}
502
503void QSystemTrayIconPrivate::updateToolTip_sys()
504{
505 if (!sys)
506 return;
507
508 sys->trayMessage(NIM_MODIFY);
509}
510
511bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys()
512{
513 return true;
514}
515
516bool QSystemTrayIconPrivate::supportsMessages_sys()
517{
518 return allowsMessages();
519}
520
521QT_END_NAMESPACE
522
523#endif
Note: See TracBrowser for help on using the repository browser.