diff options
-rw-r--r-- | src/gui/util/qsystemtrayicon_win.cpp | 53 | ||||
-rw-r--r-- | src/gui/util/qsystemtrayicon_wince.cpp | 299 | ||||
-rw-r--r-- | src/gui/util/util.pri | 5 |
3 files changed, 303 insertions, 54 deletions
diff --git a/src/gui/util/qsystemtrayicon_win.cpp b/src/gui/util/qsystemtrayicon_win.cpp index 6db158e..1571b94 100644 --- a/src/gui/util/qsystemtrayicon_win.cpp +++ b/src/gui/util/qsystemtrayicon_win.cpp @@ -61,17 +61,9 @@ #include <QDesktopWidget> #include <QSettings> -#if defined(Q_WS_WINCE) && !defined(STANDARDSHELL_UI_MODEL) -# include <streams.h> -#endif - QT_BEGIN_NAMESPACE -#if defined(Q_WS_WINCE) -static const UINT q_uNOTIFYICONID = 13; // IDs from 0 to 12 are reserved on WinCE. -#else static const UINT q_uNOTIFYICONID = 0; -#endif static uint MYWM_TASKBARCREATED = 0; #define MYWM_NOTIFYICON (WM_APP+101) @@ -125,23 +117,15 @@ bool QSystemTrayIconSys::allowsMessages() bool QSystemTrayIconSys::supportsMessages() { -#ifndef Q_OS_WINCE return allowsMessages(); -#endif - return false; } QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object) : hIcon(0), q(object), ignoreNextMouseRelease(false) { -#ifndef Q_OS_WINCE notifyIconSize = FIELD_OFFSET(NOTIFYICONDATA, guidItem); // NOTIFYICONDATAW_V2_SIZE; maxTipLength = 128; -#else - notifyIconSize = FIELD_OFFSET(NOTIFYICONDATA, szTip[64]); // NOTIFYICONDATAW_V1_SIZE; - maxTipLength = 64; -#endif // For restoring the tray icon after explorer crashes if (!MYWM_TASKBARCREATED) { @@ -317,26 +301,15 @@ bool QSystemTrayIconSys::winEvent( MSG *m, long *result ) case WM_RBUTTONUP: if (q->contextMenu()) { q->contextMenu()->popup(gpos); -#if defined(Q_WS_WINCE) - // We must ensure that the popup menu doesn't show up behind the task bar. - QRect desktopRect = qApp->desktop()->availableGeometry(); - int maxY = desktopRect.y() + desktopRect.height() - q->contextMenu()->height(); - if (gpos.y() > maxY) { - gpos.ry() = maxY; - q->contextMenu()->move(gpos); - } -#endif q->contextMenu()->activateWindow(); //Must be activated for proper keyboardfocus and menu closing on windows: } emit q->activated(QSystemTrayIcon::Context); break; -#if !defined(Q_WS_WINCE) case NIN_BALLOONUSERCLICK: emit q->messageClicked(); break; -#endif case WM_MBUTTONUP: emit q->activated(QSystemTrayIcon::MiddleClick); @@ -403,23 +376,11 @@ QRect QSystemTrayIconSys::findIconGeometry(const int iconId) //find the toolbar used in the notification area if (trayHandle) { -#if defined(Q_OS_WINCE) - trayHandle = FindWindow(L"TrayNotifyWnd", NULL); -#else trayHandle = FindWindowEx(trayHandle, NULL, L"TrayNotifyWnd", NULL); -#endif if (trayHandle) { -#if defined(Q_OS_WINCE) - HWND hwnd = FindWindow(L"SysPager", NULL); -#else HWND hwnd = FindWindowEx(trayHandle, NULL, L"SysPager", NULL); -#endif if (hwnd) { -#if defined(Q_OS_WINCE) - hwnd = FindWindow(L"ToolbarWindow32", NULL); -#else hwnd = FindWindowEx(hwnd, NULL, L"ToolbarWindow32", NULL); -#endif if (hwnd) trayHandle = hwnd; } @@ -438,11 +399,7 @@ QRect QSystemTrayIconSys::findIconGeometry(const int iconId) return ret; int buttonCount = SendMessage(trayHandle, TB_BUTTONCOUNT, 0, 0); -#if defined(Q_OS_WINCE) - LPVOID data = VirtualAlloc(NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE); -#else LPVOID data = VirtualAllocEx(trayProcess, NULL, sizeof(TBBUTTON), MEM_COMMIT, PAGE_READWRITE); -#endif if ( buttonCount < 1 || !data ) { CloseHandle(trayProcess); @@ -480,11 +437,7 @@ QRect QSystemTrayIconSys::findIconGeometry(const int iconId) } } } -#if defined(Q_OS_WINCE) - VirtualFree(data, 0, MEM_RELEASE); -#else VirtualFreeEx(trayProcess, data, 0, MEM_RELEASE); -#endif CloseHandle(trayProcess); return ret; } @@ -558,16 +511,10 @@ void QSystemTrayIconPrivate::updateMenu_sys() void QSystemTrayIconPrivate::updateToolTip_sys() { -#ifdef Q_WS_WINCE - // Calling sys->trayMessage(NIM_MODIFY) on an existing icon is broken on Windows CE. - // So we need to call updateIcon_sys() which creates a new icon handle. - updateIcon_sys(); -#else if (!sys) return; sys->trayMessage(NIM_MODIFY); -#endif } bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys() diff --git a/src/gui/util/qsystemtrayicon_wince.cpp b/src/gui/util/qsystemtrayicon_wince.cpp new file mode 100644 index 0000000..b74420d --- /dev/null +++ b/src/gui/util/qsystemtrayicon_wince.cpp @@ -0,0 +1,299 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtGui module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsystemtrayicon_p.h" +#ifndef QT_NO_SYSTEMTRAYICON +#define _WIN32_IE 0x0600 //required for NOTIFYICONDATA_V2_SIZE + +#include <qt_windows.h> +#include <shlwapi.h> +#include <QApplication> + +QT_BEGIN_NAMESPACE + +static const UINT q_uNOTIFYICONID = 13; // IDs from 0 to 12 are reserved on WinCE. +#define MYWM_NOTIFYICON (WM_APP+101) + +struct Q_NOTIFYICONIDENTIFIER { + DWORD cbSize; + HWND hWnd; + UINT uID; + GUID guidItem; +}; + +class QSystemTrayIconSys : QWidget +{ +public: + QSystemTrayIconSys(QSystemTrayIcon *object); + ~QSystemTrayIconSys(); + bool winEvent( MSG *m, long *result ); + bool trayMessage(DWORD msg); + void setIconContents(NOTIFYICONDATA &data); + void createIcon(); + QRect findTrayGeometry(); + HICON hIcon; + QPoint globalPos; + QSystemTrayIcon *q; +private: + uint notifyIconSize; + int maxTipLength; + bool ignoreNextMouseRelease; +}; + +QSystemTrayIconSys::QSystemTrayIconSys(QSystemTrayIcon *object) + : hIcon(0), q(object), ignoreNextMouseRelease(false) + +{ + notifyIconSize = FIELD_OFFSET(NOTIFYICONDATA, szTip[64]); // NOTIFYICONDATAW_V1_SIZE; + maxTipLength = 64; +} + +QSystemTrayIconSys::~QSystemTrayIconSys() +{ + if (hIcon) + DestroyIcon(hIcon); +} + +QRect QSystemTrayIconSys::findTrayGeometry() +{ + // Use lower right corner as fallback + QPoint brCorner = qApp->desktop()->screenGeometry().bottomRight(); + QRect ret(brCorner.x() - 10, brCorner.y() - 10, 10, 10); + return ret; +} + +void QSystemTrayIconSys::setIconContents(NOTIFYICONDATA &tnd) +{ + tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; + tnd.uCallbackMessage = MYWM_NOTIFYICON; + tnd.hIcon = hIcon; + QString tip = q->toolTip(); + + if (!tip.isNull()) { + tip = tip.left(maxTipLength - 1) + QChar(); + memcpy(tnd.szTip, tip.utf16(), qMin(tip.length() + 1, maxTipLength) * sizeof(wchar_t)); + } +} + +bool QSystemTrayIconSys::trayMessage(DWORD msg) +{ + NOTIFYICONDATA tnd; + memset(&tnd, 0, notifyIconSize); + tnd.uID = q_uNOTIFYICONID; + tnd.cbSize = notifyIconSize; + tnd.hWnd = winId(); + + Q_ASSERT(testAttribute(Qt::WA_WState_Created)); + + if (msg != NIM_DELETE) { + setIconContents(tnd); + } + + return Shell_NotifyIcon(msg, &tnd); +} + +void QSystemTrayIconSys::createIcon() +{ + hIcon = 0; + QIcon icon = q->icon(); + if (icon.isNull()) + return; + + //const QSize preferredSize(GetSystemMetrics(SM_CXSMICON) * 2, GetSystemMetrics(SM_CYSMICON) * 2); + const QSize preferredSize(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)); + QPixmap pm = icon.pixmap(preferredSize); + if (pm.isNull()) + return; + + hIcon = pm.toWinHICON(); +} + +bool QSystemTrayIconSys::winEvent( MSG *m, long *result ) +{ + switch(m->message) { + case WM_CREATE: + SetWindowLong(winId(), GWL_USERDATA, (LONG)((CREATESTRUCTW*)m->lParam)->lpCreateParams); + break; + + case MYWM_NOTIFYICON: + { + RECT r; + GetWindowRect(winId(), &r); + QEvent *e = 0; + Qt::KeyboardModifiers keys = QApplication::keyboardModifiers(); + QPoint gpos = QCursor::pos(); + + switch (m->lParam) { + case WM_LBUTTONUP: + if (ignoreNextMouseRelease) + ignoreNextMouseRelease = false; + else + emit q->activated(QSystemTrayIcon::Trigger); + break; + + case WM_LBUTTONDBLCLK: + ignoreNextMouseRelease = true; // Since DBLCLICK Generates a second mouse + // release we must ignore it + emit q->activated(QSystemTrayIcon::DoubleClick); + break; + + case WM_RBUTTONUP: + if (q->contextMenu()) { + q->contextMenu()->popup(gpos); + + // We must ensure that the popup menu doesn't show up behind the task bar. + QRect desktopRect = qApp->desktop()->availableGeometry(); + int maxY = desktopRect.y() + desktopRect.height() - q->contextMenu()->height(); + if (gpos.y() > maxY) { + gpos.ry() = maxY; + q->contextMenu()->move(gpos); + } + + q->contextMenu()->activateWindow(); + //Must be activated for proper keyboardfocus and menu closing on windows: + } + emit q->activated(QSystemTrayIcon::Context); + break; + + case WM_MBUTTONUP: + emit q->activated(QSystemTrayIcon::MiddleClick); + break; + default: + break; + } + if (e) { + bool res = QApplication::sendEvent(q, e); + delete e; + return res; + } + break; + } + default: + return QWidget::winEvent(m, result); + } + return 0; +} + +void QSystemTrayIconPrivate::install_sys() +{ + Q_Q(QSystemTrayIcon); + if (!sys) { + sys = new QSystemTrayIconSys(q); + sys->createIcon(); + sys->trayMessage(NIM_ADD); + } +} + +void QSystemTrayIconPrivate::showMessage_sys(const QString &title, const QString &message, QSystemTrayIcon::MessageIcon type, int timeOut) +{ + if (!sys) + return; + + uint uSecs = 0; + if ( timeOut < 0) + uSecs = 10000; //10 sec default + else uSecs = (int)timeOut; + + //message is limited to 255 chars + NULL + QString messageString; + if (message.isEmpty() && !title.isEmpty()) + messageString = QLatin1Char(' '); //ensures that the message shows when only title is set + else + messageString = message.left(255) + QChar(); + + //title is limited to 63 chars + NULL + QString titleString = title.left(63) + QChar(); + + //show QBalloonTip + QRect trayRect = sys->findTrayGeometry(); + QBalloonTip::showBalloon(type, title, message, sys->q, QPoint(trayRect.left(), + trayRect.center().y()), uSecs, false); +} + +QRect QSystemTrayIconPrivate::geometry_sys() const +{ + return QRect(); +} + +void QSystemTrayIconPrivate::remove_sys() +{ + if (!sys) + return; + + sys->trayMessage(NIM_DELETE); + delete sys; + sys = 0; +} + +void QSystemTrayIconPrivate::updateIcon_sys() +{ + if (!sys) + return; + + HICON hIconToDestroy = sys->hIcon; + + sys->createIcon(); + sys->trayMessage(NIM_MODIFY); + + if (hIconToDestroy) + DestroyIcon(hIconToDestroy); +} + +void QSystemTrayIconPrivate::updateMenu_sys() +{ + +} + +void QSystemTrayIconPrivate::updateToolTip_sys() +{ + // Calling sys->trayMessage(NIM_MODIFY) on an existing icon is broken on Windows CE. + // So we need to call updateIcon_sys() which creates a new icon handle. + updateIcon_sys(); +} + +bool QSystemTrayIconPrivate::isSystemTrayAvailable_sys() +{ + return true; +} + +QT_END_NAMESPACE + +#endif diff --git a/src/gui/util/util.pri b/src/gui/util/util.pri index 3074367..7344fbd 100644 --- a/src/gui/util/util.pri +++ b/src/gui/util/util.pri @@ -20,7 +20,10 @@ SOURCES += \ util/qundoview.cpp -win32 { +wince* { + SOURCES += \ + util/qsystemtrayicon_wince.cpp +} else:win32 { SOURCES += \ util/qsystemtrayicon_win.cpp } |