qsystemtrayicon_win.cpp: Clean obsolete code
No longer dynamically load API that is present on Windows 7. Increase the WINVER and related defines locally to keep MinGW 5.3 building. Remove all if clauses checking fore Windows Vista and below. Task-number: QTBUG-51673 Change-Id: I0feedb95537aea2453d48ef784f7852c4e33347f Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
This commit is contained in:
parent
9dff809b45
commit
11361f903a
@ -40,11 +40,18 @@
|
|||||||
#include "qsystemtrayicon_p.h"
|
#include "qsystemtrayicon_p.h"
|
||||||
#ifndef QT_NO_SYSTEMTRAYICON
|
#ifndef QT_NO_SYSTEMTRAYICON
|
||||||
|
|
||||||
#if defined(_WIN32_IE) && _WIN32_IE < 0x0600
|
#if defined(WINVER) && WINVER < 0x0601
|
||||||
# undef _WIN32_IE
|
# undef WINVER
|
||||||
#endif
|
#endif
|
||||||
#if !defined(_WIN32_IE)
|
#if !defined(WINVER)
|
||||||
# define _WIN32_IE 0x0600 //required for NOTIFYICONDATA_V2_SIZE
|
# define WINVER 0x0601 // required for NOTIFYICONDATA_V2_SIZE, ChangeWindowMessageFilterEx() (MinGW 5.3)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(NTDDI_VERSION) && NTDDI_VERSION < 0x06010000
|
||||||
|
# undef NTDDI_VERSION
|
||||||
|
#endif
|
||||||
|
#if !defined(NTDDI_VERSION)
|
||||||
|
# define NTDDI_VERSION 0x06010000 // required for Shell_NotifyIconGetRect (MinGW 5.3)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <private/qsystemlibrary_p.h>
|
#include <private/qsystemlibrary_p.h>
|
||||||
@ -56,6 +63,7 @@
|
|||||||
|
|
||||||
#include <qt_windows.h>
|
#include <qt_windows.h>
|
||||||
#include <commctrl.h>
|
#include <commctrl.h>
|
||||||
|
#include <shellapi.h>
|
||||||
#include <windowsx.h>
|
#include <windowsx.h>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
@ -65,34 +73,8 @@ static const UINT q_uNOTIFYICONID = 0;
|
|||||||
static uint MYWM_TASKBARCREATED = 0;
|
static uint MYWM_TASKBARCREATED = 0;
|
||||||
#define MYWM_NOTIFYICON (WM_APP+101)
|
#define MYWM_NOTIFYICON (WM_APP+101)
|
||||||
|
|
||||||
struct Q_NOTIFYICONIDENTIFIER {
|
|
||||||
DWORD cbSize;
|
|
||||||
HWND hWnd;
|
|
||||||
UINT uID;
|
|
||||||
GUID guidItem;
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifndef NIN_KEYSELECT
|
|
||||||
# define NIN_KEYSELECT (WM_USER + 1)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef Q_CC_MINGW
|
|
||||||
# define NIN_SELECT (WM_USER + 0)
|
|
||||||
# define NIN_BALLOONTIMEOUT (WM_USER + 4)
|
|
||||||
# define NIN_BALLOONUSERCLICK (WM_USER + 5)
|
|
||||||
# define NIF_SHOWTIP 0x00000080
|
|
||||||
# define NIIF_LARGE_ICON 0x00000020
|
|
||||||
# define NOTIFYICON_VERSION_4 4
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define Q_MSGFLT_ALLOW 1
|
|
||||||
|
|
||||||
Q_GUI_EXPORT HICON qt_pixmapToWinHICON(const QPixmap &);
|
Q_GUI_EXPORT HICON qt_pixmapToWinHICON(const QPixmap &);
|
||||||
|
|
||||||
typedef HRESULT (WINAPI *PtrShell_NotifyIconGetRect)(const Q_NOTIFYICONIDENTIFIER* identifier, RECT* iconLocation);
|
|
||||||
typedef BOOL (WINAPI *PtrChangeWindowMessageFilter)(UINT message, DWORD dwFlag);
|
|
||||||
typedef BOOL (WINAPI *PtrChangeWindowMessageFilterEx)(HWND hWnd, UINT message, DWORD action, void* pChangeFilterStruct);
|
|
||||||
|
|
||||||
// Copy QString data to a limited wchar_t array including \0.
|
// Copy QString data to a limited wchar_t array including \0.
|
||||||
static inline void qStringToLimitedWCharArray(QString in, wchar_t *target, int maxLength)
|
static inline void qStringToLimitedWCharArray(QString in, wchar_t *target, int maxLength)
|
||||||
{
|
{
|
||||||
@ -120,8 +102,6 @@ private:
|
|||||||
HICON hIcon;
|
HICON hIcon;
|
||||||
QPoint globalPos;
|
QPoint globalPos;
|
||||||
QSystemTrayIcon *q;
|
QSystemTrayIcon *q;
|
||||||
uint notifyIconSize;
|
|
||||||
int version;
|
|
||||||
bool ignoreNextMouseRelease;
|
bool ignoreNextMouseRelease;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -185,41 +165,27 @@ static inline HWND createTrayIconMessageWindow()
|
|||||||
NULL, NULL, (HINSTANCE)GetModuleHandle(0), NULL);
|
NULL, NULL, (HINSTANCE)GetModuleHandle(0), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void initNotifyIconData(NOTIFYICONDATA &tnd)
|
||||||
|
{
|
||||||
|
memset(&tnd, 0, sizeof(NOTIFYICONDATA));
|
||||||
|
tnd.cbSize = sizeof(NOTIFYICONDATA);
|
||||||
|
tnd.uVersion = NOTIFYICON_VERSION_4;
|
||||||
|
}
|
||||||
|
|
||||||
QSystemTrayIconSys::QSystemTrayIconSys(HWND hwnd, QSystemTrayIcon *object)
|
QSystemTrayIconSys::QSystemTrayIconSys(HWND hwnd, QSystemTrayIcon *object)
|
||||||
: m_hwnd(hwnd), hIcon(0), q(object)
|
: m_hwnd(hwnd), hIcon(0), q(object)
|
||||||
, notifyIconSize(sizeof(NOTIFYICONDATA)), version(NOTIFYICON_VERSION_4)
|
|
||||||
, ignoreNextMouseRelease(false)
|
, ignoreNextMouseRelease(false)
|
||||||
|
|
||||||
{
|
{
|
||||||
handleTrayIconHash()->insert(m_hwnd, this);
|
handleTrayIconHash()->insert(m_hwnd, this);
|
||||||
|
|
||||||
if (QSysInfo::windowsVersion() < QSysInfo::WV_VISTA) {
|
|
||||||
notifyIconSize = NOTIFYICONDATA_V2_SIZE;
|
|
||||||
version = NOTIFYICON_VERSION;
|
|
||||||
}
|
|
||||||
|
|
||||||
// For restoring the tray icon after explorer crashes
|
// For restoring the tray icon after explorer crashes
|
||||||
if (!MYWM_TASKBARCREATED) {
|
if (!MYWM_TASKBARCREATED) {
|
||||||
MYWM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated");
|
MYWM_TASKBARCREATED = RegisterWindowMessage(L"TaskbarCreated");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow the WM_TASKBARCREATED message through the UIPI filter on Windows 7 and higher
|
// Allow the WM_TASKBARCREATED message through the UIPI filter
|
||||||
static PtrChangeWindowMessageFilterEx pChangeWindowMessageFilterEx =
|
ChangeWindowMessageFilterEx(m_hwnd, MYWM_TASKBARCREATED, MSGFLT_ALLOW, 0);
|
||||||
(PtrChangeWindowMessageFilterEx)QSystemLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilterEx");
|
|
||||||
|
|
||||||
if (pChangeWindowMessageFilterEx) {
|
|
||||||
// Call the safer ChangeWindowMessageFilterEx API if available (Windows 7 onwards)
|
|
||||||
pChangeWindowMessageFilterEx(m_hwnd, MYWM_TASKBARCREATED, Q_MSGFLT_ALLOW, 0);
|
|
||||||
} else {
|
|
||||||
// Call the deprecated ChangeWindowMessageFilter API otherwise (Vista onwards)
|
|
||||||
// May 2016: Still resolved at runtime since the definition is not present in MinGW 4.9.
|
|
||||||
// TODO: Replace by direct invocation when upgrading MinGW.
|
|
||||||
static PtrChangeWindowMessageFilter pChangeWindowMessageFilter =
|
|
||||||
(PtrChangeWindowMessageFilter)QSystemLibrary::resolve(QLatin1String("user32"), "ChangeWindowMessageFilter");
|
|
||||||
|
|
||||||
if (pChangeWindowMessageFilter)
|
|
||||||
pChangeWindowMessageFilter(MYWM_TASKBARCREATED, Q_MSGFLT_ALLOW);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QSystemTrayIconSys::~QSystemTrayIconSys()
|
QSystemTrayIconSys::~QSystemTrayIconSys()
|
||||||
@ -243,23 +209,19 @@ void QSystemTrayIconSys::setIconContents(NOTIFYICONDATA &tnd)
|
|||||||
bool QSystemTrayIconSys::showMessage(const QString &title, const QString &message, const QIcon &icon, uint uSecs)
|
bool QSystemTrayIconSys::showMessage(const QString &title, const QString &message, const QIcon &icon, uint uSecs)
|
||||||
{
|
{
|
||||||
NOTIFYICONDATA tnd;
|
NOTIFYICONDATA tnd;
|
||||||
memset(&tnd, 0, notifyIconSize);
|
initNotifyIconData(tnd);
|
||||||
qStringToLimitedWCharArray(message, tnd.szInfo, 256);
|
qStringToLimitedWCharArray(message, tnd.szInfo, 256);
|
||||||
qStringToLimitedWCharArray(title, tnd.szInfoTitle, 64);
|
qStringToLimitedWCharArray(title, tnd.szInfoTitle, 64);
|
||||||
|
|
||||||
tnd.uID = q_uNOTIFYICONID;
|
tnd.uID = q_uNOTIFYICONID;
|
||||||
tnd.dwInfoFlags = NIIF_USER;
|
tnd.dwInfoFlags = NIIF_USER;
|
||||||
|
|
||||||
HICON *phIcon = &tnd.hIcon;
|
|
||||||
QSize size(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
|
QSize size(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
|
||||||
if (version == NOTIFYICON_VERSION_4) {
|
const QSize largeIcon(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
|
||||||
const QSize largeIcon(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
|
const QSize more = icon.actualSize(largeIcon);
|
||||||
QSize more = icon.actualSize(largeIcon);
|
if (more.height() > (largeIcon.height() * 3/4) || more.width() > (largeIcon.width() * 3/4)) {
|
||||||
if (more.height() > (largeIcon.height() * 3/4) || more.width() > (largeIcon.width() * 3/4)) {
|
tnd.dwInfoFlags |= NIIF_LARGE_ICON;
|
||||||
tnd.dwInfoFlags |= NIIF_LARGE_ICON;
|
size = largeIcon;
|
||||||
size = largeIcon;
|
|
||||||
}
|
|
||||||
phIcon = &tnd.hBalloonIcon;
|
|
||||||
}
|
}
|
||||||
QPixmap pm = icon.pixmap(size);
|
QPixmap pm = icon.pixmap(size);
|
||||||
if (pm.isNull()) {
|
if (pm.isNull()) {
|
||||||
@ -270,10 +232,8 @@ bool QSystemTrayIconSys::showMessage(const QString &title, const QString &messag
|
|||||||
pm.size().width(), pm.size().height(), size.width(), size.height());
|
pm.size().width(), pm.size().height(), size.width(), size.height());
|
||||||
pm = pm.scaled(size, Qt::IgnoreAspectRatio);
|
pm = pm.scaled(size, Qt::IgnoreAspectRatio);
|
||||||
}
|
}
|
||||||
*phIcon = qt_pixmapToWinHICON(pm);
|
tnd.hBalloonIcon = qt_pixmapToWinHICON(pm);
|
||||||
}
|
}
|
||||||
tnd.cbSize = notifyIconSize;
|
|
||||||
tnd.uVersion = version;
|
|
||||||
tnd.hWnd = m_hwnd;
|
tnd.hWnd = m_hwnd;
|
||||||
tnd.uTimeout = uSecs;
|
tnd.uTimeout = uSecs;
|
||||||
tnd.uFlags = NIF_INFO | NIF_SHOWTIP;
|
tnd.uFlags = NIF_INFO | NIF_SHOWTIP;
|
||||||
@ -284,13 +244,11 @@ bool QSystemTrayIconSys::showMessage(const QString &title, const QString &messag
|
|||||||
bool QSystemTrayIconSys::trayMessage(DWORD msg)
|
bool QSystemTrayIconSys::trayMessage(DWORD msg)
|
||||||
{
|
{
|
||||||
NOTIFYICONDATA tnd;
|
NOTIFYICONDATA tnd;
|
||||||
memset(&tnd, 0, notifyIconSize);
|
initNotifyIconData(tnd);
|
||||||
|
|
||||||
tnd.uID = q_uNOTIFYICONID;
|
tnd.uID = q_uNOTIFYICONID;
|
||||||
tnd.cbSize = notifyIconSize;
|
|
||||||
tnd.hWnd = m_hwnd;
|
tnd.hWnd = m_hwnd;
|
||||||
tnd.uFlags = NIF_SHOWTIP;
|
tnd.uFlags = NIF_SHOWTIP;
|
||||||
tnd.uVersion = version;
|
|
||||||
|
|
||||||
if (msg == NIM_ADD || msg == NIM_MODIFY) {
|
if (msg == NIM_ADD || msg == NIM_MODIFY) {
|
||||||
setIconContents(tnd);
|
setIconContents(tnd);
|
||||||
@ -311,9 +269,7 @@ HICON QSystemTrayIconSys::createIcon()
|
|||||||
const QIcon icon = q->icon();
|
const QIcon icon = q->icon();
|
||||||
if (icon.isNull())
|
if (icon.isNull())
|
||||||
return oldIcon;
|
return oldIcon;
|
||||||
const QSize requestedSize = QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA
|
const QSize requestedSize(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
|
||||||
? QSize(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON))
|
|
||||||
: QSize(GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
|
|
||||||
const QSize size = icon.actualSize(requestedSize);
|
const QSize size = icon.actualSize(requestedSize);
|
||||||
const QPixmap pm = icon.pixmap(size);
|
const QPixmap pm = icon.pixmap(size);
|
||||||
if (pm.isNull())
|
if (pm.isNull())
|
||||||
@ -328,18 +284,9 @@ bool QSystemTrayIconSys::winEvent( MSG *m, long *result )
|
|||||||
switch(m->message) {
|
switch(m->message) {
|
||||||
case MYWM_NOTIFYICON:
|
case MYWM_NOTIFYICON:
|
||||||
{
|
{
|
||||||
int message = 0;
|
Q_ASSERT(q_uNOTIFYICONID == HIWORD(m->lParam));
|
||||||
QPoint gpos;
|
const int message = LOWORD(m->lParam);
|
||||||
|
const QPoint gpos = QPoint(GET_X_LPARAM(m->wParam), GET_Y_LPARAM(m->wParam));
|
||||||
if (version == NOTIFYICON_VERSION_4) {
|
|
||||||
Q_ASSERT(q_uNOTIFYICONID == HIWORD(m->lParam));
|
|
||||||
message = LOWORD(m->lParam);
|
|
||||||
gpos = QPoint(GET_X_LPARAM(m->wParam), GET_Y_LPARAM(m->wParam));
|
|
||||||
} else {
|
|
||||||
Q_ASSERT(q_uNOTIFYICONID == m->wParam);
|
|
||||||
message = m->lParam;
|
|
||||||
gpos = QCursor::pos();
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (message) {
|
switch (message) {
|
||||||
case NIN_SELECT:
|
case NIN_SELECT:
|
||||||
@ -423,24 +370,15 @@ QRect QSystemTrayIconSys::findIconGeometry(UINT iconId)
|
|||||||
UINT uID;
|
UINT uID;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Windows 7 onwards.
|
NOTIFYICONIDENTIFIER nid;
|
||||||
static PtrShell_NotifyIconGetRect Shell_NotifyIconGetRect =
|
memset(&nid, 0, sizeof(nid));
|
||||||
(PtrShell_NotifyIconGetRect)QSystemLibrary::resolve(QLatin1String("shell32"),
|
nid.cbSize = sizeof(nid);
|
||||||
"Shell_NotifyIconGetRect");
|
nid.hWnd = m_hwnd;
|
||||||
|
nid.uID = iconId;
|
||||||
|
|
||||||
if (Shell_NotifyIconGetRect) {
|
RECT rect;
|
||||||
Q_NOTIFYICONIDENTIFIER nid;
|
if (SUCCEEDED(Shell_NotifyIconGetRect(&nid, &rect)))
|
||||||
memset(&nid, 0, sizeof(nid));
|
return QRect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
||||||
nid.cbSize = sizeof(nid);
|
|
||||||
nid.hWnd = m_hwnd;
|
|
||||||
nid.uID = iconId;
|
|
||||||
|
|
||||||
RECT rect;
|
|
||||||
HRESULT hr = Shell_NotifyIconGetRect(&nid, &rect);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
return QRect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QRect ret;
|
QRect ret;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user