author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qsystemtrayicon_p.h" |
|
43 |
#ifndef QT_NO_SYSTEMTRAYICON |
|
44 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
45 |
#ifndef _WIN32_WINNT |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
46 |
#define _WIN32_WINNT 0x0600 |
0 | 47 |
#endif |
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
48 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
49 |
#ifndef _WIN32_IE |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
50 |
#define _WIN32_IE 0x600 |
0 | 51 |
#endif |
52 |
||
53 |
#include <qt_windows.h> |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
54 |
#include <windowsx.h> |
0 | 55 |
#include <commctrl.h> |
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
56 |
|
0 | 57 |
#include <QLibrary> |
58 |
#include <QApplication> |
|
59 |
#include <QSettings> |
|
60 |
||
61 |
QT_BEGIN_NAMESPACE |
|
62 |
||
63 |
static const UINT q_uNOTIFYICONID = 0; |
|
64 |
||
65 |
static uint MYWM_TASKBARCREATED = 0; |
|
66 |
#define MYWM_NOTIFYICON (WM_APP+101) |
|
67 |
||
68 |
struct Q_NOTIFYICONIDENTIFIER { |
|
69 |
DWORD cbSize; |
|
70 |
HWND hWnd; |
|
71 |
UINT uID; |
|
72 |
GUID guidItem; |
|
73 |
}; |
|
74 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
75 |
#ifndef NOTIFYICON_VERSION_4 |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
76 |
#define NOTIFYICON_VERSION_4 4 |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
77 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
78 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
79 |
#ifndef NIN_SELECT |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
80 |
#define NIN_SELECT (WM_USER + 0) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
81 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
82 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
83 |
#ifndef NIN_KEYSELECT |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
84 |
#define NIN_KEYSELECT (WM_USER + 1) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
85 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
86 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
87 |
#ifndef NIN_BALLOONTIMEOUT |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
88 |
#define NIN_BALLOONTIMEOUT (WM_USER + 4) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
89 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
90 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
91 |
#ifndef NIN_BALLOONUSERCLICK |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
92 |
#define NIN_BALLOONUSERCLICK (WM_USER + 5) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
93 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
94 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
95 |
#ifndef NIF_SHOWTIP |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
96 |
#define NIF_SHOWTIP 0x00000080 |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
97 |
#endif |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
98 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
#define Q_MSGFLT_ALLOW 1 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
|
0 | 101 |
typedef HRESULT (WINAPI *PtrShell_NotifyIconGetRect)(const Q_NOTIFYICONIDENTIFIER* identifier, RECT* iconLocation); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
102 |
typedef BOOL (WINAPI *PtrChangeWindowMessageFilter)(UINT message, DWORD dwFlag); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
103 |
typedef BOOL (WINAPI *PtrChangeWindowMessageFilterEx)(HWND hWnd, UINT message, DWORD action, void* pChangeFilterStruct); |
0 | 104 |
|
105 |
class QSystemTrayIconSys : QWidget |
|
106 |
{ |
|
107 |
public: |
|
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; |
|
119 |
private: |
|
120 |
uint notifyIconSize; |
|
121 |
int maxTipLength; |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
122 |
int version; |
0 | 123 |
bool ignoreNextMouseRelease; |
124 |
}; |
|
125 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
126 |
static bool allowsMessages() |
0 | 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 |
||
137 |
QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object) |
|
138 |
: hIcon(0), q(object), ignoreNextMouseRelease(false) |
|
139 |
||
140 |
{ |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
141 |
if (QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA) { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
142 |
notifyIconSize = sizeof(NOTIFYICONDATA); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
143 |
version = NOTIFYICON_VERSION_4; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
144 |
} else { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
145 |
notifyIconSize = NOTIFYICONDATA_V2_SIZE; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
146 |
version = NOTIFYICON_VERSION; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
147 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
148 |
|
0 | 149 |
maxTipLength = 128; |
150 |
||
151 |
// For restoring the tray icon after explorer crashes |
|
152 |
if (!MYWM_TASKBARCREATED) { |
|
153 |
MYWM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated"); |
|
154 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
// Allow the WM_TASKBARCREATED message through the UIPI filter on Windows Vista and higher |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
static PtrChangeWindowMessageFilterEx pChangeWindowMessageFilterEx = |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
(PtrChangeWindowMessageFilterEx)QLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilterEx"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
if (pChangeWindowMessageFilterEx) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
// Call the safer ChangeWindowMessageFilterEx API if available |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
pChangeWindowMessageFilterEx(winId(), MYWM_TASKBARCREATED, Q_MSGFLT_ALLOW, 0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
static PtrChangeWindowMessageFilter pChangeWindowMessageFilter = |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
(PtrChangeWindowMessageFilter)QLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilter"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
if (pChangeWindowMessageFilter) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
// Call the deprecated ChangeWindowMessageFilter API otherwise |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
pChangeWindowMessageFilter(MYWM_TASKBARCREATED, Q_MSGFLT_ALLOW); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
} |
0 | 172 |
} |
173 |
||
174 |
QSystemTrayIconSys::~QSystemTrayIconSys() |
|
175 |
{ |
|
176 |
if (hIcon) |
|
177 |
DestroyIcon(hIcon); |
|
178 |
} |
|
179 |
||
180 |
void QSystemTrayIconSys::setIconContents(NOTIFYICONDATA &tnd) |
|
181 |
{ |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
182 |
tnd.uFlags |= NIF_MESSAGE | NIF_ICON | NIF_TIP; |
0 | 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 |
||
193 |
static 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 |
||
210 |
bool 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; |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
223 |
tnd.uFlags = NIF_INFO | NIF_SHOWTIP; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
224 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
225 |
Q_ASSERT(testAttribute(Qt::WA_WState_Created)); |
0 | 226 |
|
227 |
return Shell_NotifyIcon(NIM_MODIFY, &tnd); |
|
228 |
} |
|
229 |
||
230 |
bool QSystemTrayIconSys::trayMessage(DWORD msg) |
|
231 |
{ |
|
232 |
NOTIFYICONDATA tnd; |
|
233 |
memset(&tnd, 0, notifyIconSize); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
234 |
|
0 | 235 |
tnd.uID = q_uNOTIFYICONID; |
236 |
tnd.cbSize = notifyIconSize; |
|
237 |
tnd.hWnd = winId(); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
238 |
tnd.uFlags = NIF_SHOWTIP; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
239 |
tnd.uVersion = version; |
0 | 240 |
|
241 |
Q_ASSERT(testAttribute(Qt::WA_WState_Created)); |
|
242 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
243 |
if (msg == NIM_ADD || msg == NIM_MODIFY) { |
0 | 244 |
setIconContents(tnd); |
245 |
} |
|
246 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
247 |
bool success = Shell_NotifyIcon(msg, &tnd); |
0 | 248 |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
249 |
if (msg == NIM_ADD) |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
250 |
return success && Shell_NotifyIcon(NIM_SETVERSION, &tnd); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
251 |
else |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
252 |
return success; |
0 | 253 |
} |
254 |
||
255 |
void 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 |
||
272 |
bool QSystemTrayIconSys::winEvent( MSG *m, long *result ) |
|
273 |
{ |
|
274 |
switch(m->message) { |
|
275 |
case MYWM_NOTIFYICON: |
|
276 |
{ |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
277 |
int message = 0; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
278 |
QPoint gpos; |
0 | 279 |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
280 |
if (version == NOTIFYICON_VERSION_4) { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
281 |
Q_ASSERT(q_uNOTIFYICONID == HIWORD(m->lParam)); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
282 |
message = LOWORD(m->lParam); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
283 |
gpos = QPoint(GET_X_LPARAM(m->wParam), GET_Y_LPARAM(m->wParam)); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
284 |
} else { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
285 |
Q_ASSERT(q_uNOTIFYICONID == m->wParam); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
286 |
message = m->lParam; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
287 |
gpos = QCursor::pos(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
288 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
289 |
|
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
290 |
switch (message) { |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
291 |
case NIN_SELECT: |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
292 |
case NIN_KEYSELECT: |
0 | 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 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
305 |
case WM_CONTEXTMENU: |
0 | 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; |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
319 |
|
0 | 320 |
default: |
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
321 |
break; |
0 | 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 |
||
335 |
void 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 |
*/ |
|
350 |
QRect QSystemTrayIconSys::findIconGeometry(const int iconId) |
|
351 |
{ |
|
352 |
static PtrShell_NotifyIconGetRect Shell_NotifyIconGetRect = |
|
353 |
(PtrShell_NotifyIconGetRect)QLibrary::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 |
||
443 |
void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, int timeOut) |
|
444 |
{ |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
445 |
if (!sys || !allowsMessages()) |
0 | 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 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
463 |
sys->showMessage(titleString, messageString, type, uSecs); |
0 | 464 |
} |
465 |
||
466 |
QRect QSystemTrayIconPrivate::geometry_sys() const |
|
467 |
{ |
|
468 |
if (!sys) |
|
469 |
return QRect(); |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
470 |
|
0 | 471 |
return sys->findIconGeometry(q_uNOTIFYICONID); |
472 |
} |
|
473 |
||
474 |
void QSystemTrayIconPrivate::remove_sys() |
|
475 |
{ |
|
476 |
if (!sys) |
|
477 |
return; |
|
478 |
||
479 |
sys->trayMessage(NIM_DELETE); |
|
480 |
delete sys; |
|
481 |
sys = 0; |
|
482 |
} |
|
483 |
||
484 |
void 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 |
||
498 |
void QSystemTrayIconPrivate::updateMenu_sys() |
|
499 |
{ |
|
500 |
||
501 |
} |
|
502 |
||
503 |
void QSystemTrayIconPrivate::updateToolTip_sys() |
|
504 |
{ |
|
505 |
if (!sys) |
|
506 |
return; |
|
507 |
||
508 |
sys->trayMessage(NIM_MODIFY); |
|
509 |
} |
|
510 |
||
511 |
bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys() |
|
512 |
{ |
|
513 |
return true; |
|
514 |
} |
|
515 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
516 |
bool QSystemTrayIconPrivate::supportsMessages_sys() |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
517 |
{ |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
518 |
return allowsMessages(); |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
519 |
} |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
520 |
|
0 | 521 |
QT_END_NAMESPACE |
522 |
||
523 |
#endif |