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

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

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

File size: 15.1 KB
RevLine 
[2]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]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**
[561]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.
[2]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**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qsystemtrayicon_p.h"
43#ifndef QT_NO_SYSTEMTRAYICON
44
[846]45#ifndef _WIN32_WINNT
46#define _WIN32_WINNT 0x0600
[2]47#endif
[846]48
49#ifndef _WIN32_IE
50#define _WIN32_IE 0x600
[2]51#endif
52
53#include <qt_windows.h>
[846]54#include <windowsx.h>
[2]55#include <commctrl.h>
[846]56
57#include <private/qsystemlibrary_p.h>
[2]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
[561]68struct Q_NOTIFYICONIDENTIFIER {
69 DWORD cbSize;
70 HWND hWnd;
71 UINT uID;
72 GUID guidItem;
73};
[2]74
[846]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
[561]99#define Q_MSGFLT_ALLOW 1
[2]100
[561]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
[2]105class QSystemTrayIconSys : QWidget
106{
107public:
108 QSystemTrayIconSys(QSystemTrayIcon *object);
109 ~QSystemTrayIconSys();
110 bool winEvent( MSG *m, long *result );
111 bool trayMessage(DWORD msg);
[561]112 void setIconContents(NOTIFYICONDATA &data);
113 bool showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, uint uSecs);
[2]114 QRect findIconGeometry(const int a_iButtonID);
115 void createIcon();
116 HICON hIcon;
117 QPoint globalPos;
118 QSystemTrayIcon *q;
119private:
[561]120 uint notifyIconSize;
[2]121 int maxTipLength;
[846]122 int version;
[561]123 bool ignoreNextMouseRelease;
[2]124};
125
[846]126static bool allowsMessages()
[2]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)
[561]138 : hIcon(0), q(object), ignoreNextMouseRelease(false)
139
[2]140{
[846]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
[561]149 maxTipLength = 128;
[2]150
151 // For restoring the tray icon after explorer crashes
152 if (!MYWM_TASKBARCREATED) {
[561]153 MYWM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated");
[2]154 }
[561]155
156 // Allow the WM_TASKBARCREATED message through the UIPI filter on Windows Vista and higher
157 static PtrChangeWindowMessageFilterEx pChangeWindowMessageFilterEx =
[846]158 (PtrChangeWindowMessageFilterEx)QSystemLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilterEx");
[561]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 =
[846]165 (PtrChangeWindowMessageFilter)QSystemLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilter");
[561]166
167 if (pChangeWindowMessageFilter) {
168 // Call the deprecated ChangeWindowMessageFilter API otherwise
169 pChangeWindowMessageFilter(MYWM_TASKBARCREATED, Q_MSGFLT_ALLOW);
170 }
171 }
[2]172}
173
174QSystemTrayIconSys::~QSystemTrayIconSys()
175{
176 if (hIcon)
177 DestroyIcon(hIcon);
178}
179
[561]180void QSystemTrayIconSys::setIconContents(NOTIFYICONDATA &tnd)
[2]181{
[846]182 tnd.uFlags |= NIF_MESSAGE | NIF_ICON | NIF_TIP;
[2]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();
[561]189 memcpy(tnd.szTip, tip.utf16(), qMin(tip.length() + 1, maxTipLength) * sizeof(wchar_t));
[2]190 }
191}
192
[561]193static int iconFlag( QSystemTrayIcon::MessageIcon icon )
[2]194{
195 switch (icon) {
[561]196 case QSystemTrayIcon::Information:
197 return NIIF_INFO;
198 case QSystemTrayIcon::Warning:
199 return NIIF_WARNING;
200 case QSystemTrayIcon::Critical:
201 return NIIF_ERROR;
[2]202 case QSystemTrayIcon::NoIcon:
[561]203 return NIIF_NONE;
204 default:
205 Q_ASSERT_X(false, "QSystemTrayIconSys::showMessage", "Invalid QSystemTrayIcon::MessageIcon value");
206 return NIIF_NONE;
[2]207 }
208}
209
[561]210bool QSystemTrayIconSys::showMessage(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, uint uSecs)
[2]211{
212 NOTIFYICONDATA tnd;
[561]213 memset(&tnd, 0, notifyIconSize);
[2]214
[561]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
[2]218 tnd.uID = q_uNOTIFYICONID;
219 tnd.dwInfoFlags = iconFlag(type);
[561]220 tnd.cbSize = notifyIconSize;
[2]221 tnd.hWnd = winId();
222 tnd.uTimeout = uSecs;
[846]223 tnd.uFlags = NIF_INFO | NIF_SHOWTIP;
[2]224
[846]225 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
226
[561]227 return Shell_NotifyIcon(NIM_MODIFY, &tnd);
[2]228}
229
[561]230bool QSystemTrayIconSys::trayMessage(DWORD msg)
[2]231{
[561]232 NOTIFYICONDATA tnd;
233 memset(&tnd, 0, notifyIconSize);
[846]234
[2]235 tnd.uID = q_uNOTIFYICONID;
[561]236 tnd.cbSize = notifyIconSize;
[2]237 tnd.hWnd = winId();
[846]238 tnd.uFlags = NIF_SHOWTIP;
239 tnd.uVersion = version;
[2]240
241 Q_ASSERT(testAttribute(Qt::WA_WState_Created));
242
[846]243 if (msg == NIM_ADD || msg == NIM_MODIFY) {
[561]244 setIconContents(tnd);
[2]245 }
246
[846]247 bool success = Shell_NotifyIcon(msg, &tnd);
[2]248
[846]249 if (msg == NIM_ADD)
250 return success && Shell_NotifyIcon(NIM_SETVERSION, &tnd);
251 else
252 return success;
[2]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
[561]269 hIcon = pm.toWinHICON();
[2]270}
271
272bool QSystemTrayIconSys::winEvent( MSG *m, long *result )
273{
274 switch(m->message) {
275 case MYWM_NOTIFYICON:
276 {
[846]277 int message = 0;
278 QPoint gpos;
[2]279
[846]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:
[561]293 if (ignoreNextMouseRelease)
294 ignoreNextMouseRelease = false;
295 else
296 emit q->activated(QSystemTrayIcon::Trigger);
[2]297 break;
298
299 case WM_LBUTTONDBLCLK:
[561]300 ignoreNextMouseRelease = true; // Since DBLCLICK Generates a second mouse
301 // release we must ignore it
[2]302 emit q->activated(QSystemTrayIcon::DoubleClick);
303 break;
304
[846]305 case WM_CONTEXTMENU:
[2]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;
[846]319
[2]320 default:
[846]321 break;
[2]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{
[561]352 static PtrShell_NotifyIconGetRect Shell_NotifyIconGetRect =
[846]353 (PtrShell_NotifyIconGetRect)QSystemLibrary::resolve(QLatin1String("shell32"), "Shell_NotifyIconGetRect");
[561]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
[2]369 QRect ret;
370
371 TBBUTTON buttonData;
372 DWORD processID = 0;
[561]373 HWND trayHandle = FindWindow(L"Shell_TrayWnd", NULL);
[2]374
375 //find the toolbar used in the notification area
376 if (trayHandle) {
[561]377 trayHandle = FindWindowEx(trayHandle, NULL, L"TrayNotifyWnd", NULL);
[2]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
[561]413 if (!ReadProcessMemory(trayProcess, data, &buttonData, sizeof(TBBUTTON), &numBytes))
[2]414 continue;
415
[561]416 if (!ReadProcessMemory(trayProcess, (LPVOID) buttonData.dwData, appData, sizeof(appData), &numBytes))
[2]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{
[846]445 if (!sys || !allowsMessages())
[2]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())
[561]456 messageString = QLatin1Char(' '); //ensures that the message shows when only title is set
[2]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
[846]463 sys->showMessage(titleString, messageString, type, uSecs);
[2]464}
465
466QRect QSystemTrayIconPrivate::geometry_sys() const
467{
468 if (!sys)
469 return QRect();
[846]470
[561]