2
* trayicon_win.cpp - Windows trayicon, adapted from Qt example
3
* Copyright (C) 2003 Justin Karneges
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
#include <qapplication.h>
31
#include <qt_windows.h>
33
static uint WM_TASKBARCREATED = 0;
34
#define WM_NOTIFYICON (WM_APP+101)
36
typedef BOOL (WINAPI *PtrShell_NotifyIcon)(DWORD,PNOTIFYICONDATA);
37
static PtrShell_NotifyIcon ptrShell_NotifyIcon = 0;
39
static void resolveLibs()
41
QLibrary lib("shell32");
42
lib.setAutoUnload( FALSE );
43
static bool triedResolve = FALSE;
44
if ( !ptrShell_NotifyIcon && !triedResolve ) {
46
ptrShell_NotifyIcon = (PtrShell_NotifyIcon) lib.resolve( "Shell_NotifyIconW" );
50
class TrayIcon::TrayIconPrivate : public QWidget
57
TrayIconPrivate( TrayIcon *object )
58
: QWidget( 0 ), hIcon( 0 ), hMask( 0 ), iconObject( object )
60
if ( !WM_TASKBARCREATED )
61
WM_TASKBARCREATED = RegisterWindowMessage( TEXT("TaskbarCreated") );
67
DeleteObject( hMask );
76
// the unavoidable A/W versions. Don't forget to keep them in sync!
77
bool trayMessageA( DWORD msg )
80
ZeroMemory( &tnd, sizeof(NOTIFYICONDATAA) );
81
tnd.cbSize = sizeof(NOTIFYICONDATAA);
83
tnd.uID = 1; // michalj
85
if ( msg != NIM_DELETE ) {
86
tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
87
tnd.uCallbackMessage= WM_NOTIFYICON;
89
if ( !iconObject->toolTip().isNull() ) {
90
// Tip is limited to 63 + NULL; lstrcpyn appends a NULL terminator.
91
QString tip = iconObject->toolTip().left( 63 ) + QChar();
92
lstrcpynA(tnd.szTip, (const char*)tip.local8Bit(), QMIN( tip.length()+1, 64 ) );
96
return Shell_NotifyIconA(msg, &tnd);
100
bool trayMessageW( DWORD msg )
103
if ( ! (ptrShell_NotifyIcon && qWinVersion() & Qt::WV_NT_based) )
104
return trayMessageA( msg );
107
ZeroMemory( &tnd, sizeof(NOTIFYICONDATAW) );
108
tnd.cbSize = sizeof(NOTIFYICONDATAW);
110
tnd.uID = 1; // michalj
112
if ( msg != NIM_DELETE ) {
113
tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
114
tnd.uCallbackMessage= WM_NOTIFYICON;
116
if ( !iconObject->toolTip().isNull() ) {
117
// Tip is limited to 63 + NULL; lstrcpyn appends a NULL terminator.
118
QString tip = iconObject->toolTip().left( 63 ) + QChar();
119
lstrcpynW(tnd.szTip, (TCHAR*)tip.unicode(), QMIN( tip.length()+1, 64 ) );
120
// lstrcpynW(tnd.szTip, (TCHAR*)qt_winTchar( tip, FALSE ), QMIN( tip.length()+1, 64 ) );
123
return ptrShell_NotifyIcon(msg, &tnd);
127
bool trayMessage( DWORD msg )
130
return trayMessageW(msg);
132
return trayMessageA(msg);
136
bool iconDrawItem(LPDRAWITEMSTRUCT lpdi)
141
DrawIconEx(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top, hIcon, 0, 0, 0, NULL, DI_NORMAL );
145
bool winEvent( MSG *m )
149
return iconDrawItem( (LPDRAWITEMSTRUCT)m->lParam );
153
QPoint gpos = QCursor::pos();
155
case WM_MOUSEMOVE: e = new QMouseEvent( QEvent::MouseMove, mapFromGlobal( gpos ), gpos, 0, 0 ); break;
156
case WM_LBUTTONDOWN: e = new QMouseEvent( QEvent::MouseButtonPress, mapFromGlobal( gpos ), gpos, LeftButton, LeftButton ); break;
157
case WM_LBUTTONUP: e = new QMouseEvent( QEvent::MouseButtonRelease, mapFromGlobal( gpos ), gpos, LeftButton, LeftButton ); break;
158
case WM_LBUTTONDBLCLK: e = new QMouseEvent( QEvent::MouseButtonDblClick, mapFromGlobal( gpos ), gpos, LeftButton, LeftButton ); break;
159
case WM_RBUTTONDOWN: e = new QMouseEvent( QEvent::MouseButtonPress, mapFromGlobal( gpos ), gpos, RightButton, RightButton ); break;
160
case WM_RBUTTONUP: e = new QMouseEvent( QEvent::MouseButtonRelease, mapFromGlobal( gpos ), gpos, RightButton, RightButton ); break;
161
case WM_RBUTTONDBLCLK: e = new QMouseEvent( QEvent::MouseButtonDblClick, mapFromGlobal( gpos ), gpos, RightButton, RightButton ); break;
162
case WM_MBUTTONDOWN: e = new QMouseEvent( QEvent::MouseButtonPress, mapFromGlobal( gpos ), gpos, MidButton, MidButton ); break;
163
case WM_MBUTTONUP: e = new QMouseEvent( QEvent::MouseButtonRelease, mapFromGlobal( gpos ), gpos, MidButton, MidButton ); break;
164
case WM_MBUTTONDBLCLK: e = new QMouseEvent( QEvent::MouseButtonDblClick, mapFromGlobal( gpos ), gpos, MidButton, MidButton ); break;
165
case WM_CONTEXTMENU: e = new QMouseEvent( QEvent::MouseButtonRelease, mapFromGlobal( gpos ), gpos, RightButton, RightButton ); break;
168
bool res = QApplication::sendEvent( iconObject, e );
175
if ( m->message == WM_TASKBARCREATED ) trayMessage( NIM_ADD );
177
return QWidget::winEvent( m );
181
static HBITMAP createIconMask( const QPixmap &qp )
183
QImage bm = qp.convertToImage();
186
int bpl = ((w+15)/16)*2; // bpl, 16 bit alignment
187
uchar *bits = new uchar[bpl*h];
189
for ( int y=0; y<h; y++ )
190
memcpy( bits+y*bpl, bm.scanLine(y), bpl );
191
HBITMAP hbm = CreateBitmap( w, h, 1, 1, bits );
196
static HICON createIcon( const QPixmap &pm, HBITMAP &hbm )
198
QPixmap maskpm( pm.size(), pm.depth(), QPixmap::NormalOptim );
199
QBitmap mask( pm.size(), FALSE, QPixmap::NormalOptim );
201
maskpm.fill( Qt::black ); // make masked area black
202
bitBlt( &mask, 0, 0, pm.mask() );
204
maskpm.fill( Qt::color1 );
206
bitBlt( &maskpm, 0, 0, &pm);
208
iconInfo.fIcon = TRUE;
209
iconInfo.hbmMask = hbm = createIconMask(mask);
210
iconInfo.hbmColor = maskpm.hbm();
212
HICON icon = CreateIconIndirect( &iconInfo );
213
DeleteObject(iconInfo.hbmMask);
214
iconInfo.hbmMask = hbm = 0; // michalj
218
void TrayIcon::sysInstall()
221
d = new TrayIconPrivate( this );
222
d->hIcon = createIcon( pm, d->hMask );
224
d->trayMessage( NIM_ADD );
228
void TrayIcon::sysRemove()
231
d->trayMessage( NIM_DELETE );
238
void TrayIcon::sysUpdateIcon()
242
DeleteObject( d->hMask );
243
d->hMask = 0; // michalj
246
DestroyIcon( d->hIcon );
247
d->hIcon = 0; // michalj
250
d->hIcon = createIcon( pm, d->hMask );
251
d->trayMessage( NIM_MODIFY );
255
void TrayIcon::sysUpdateToolTip()
258
d->trayMessage( NIM_MODIFY );