4 * Copyright 1999 Kai Morich <kai.morich@bigfoot.de>
6 * Manage the systray window. That it actually appears in the docking
7 * area of KDE or GNOME is delegated to windows/x11drv/wnd.c,
8 * X11DRV_WND_DockWindow.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include "shell32_main.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
42 typedef struct SystrayItem
{
45 NOTIFYICONDATAA notifyIcon
;
46 struct SystrayItem
*nextTrayItem
;
49 static SystrayItem
*systray
=NULL
;
50 static int firstSystray
=TRUE
; /* defer creation of window class until first systray item is created */
52 static BOOL
SYSTRAY_Delete(PNOTIFYICONDATAA pnid
);
55 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
56 /* space around icon (forces icon to center of KDE systray area) */
61 static BOOL
SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1
, PNOTIFYICONDATAA pnid2
)
63 if (pnid1
->hWnd
!= pnid2
->hWnd
) return FALSE
;
64 if (pnid1
->uID
!= pnid2
->uID
) return FALSE
;
68 static LRESULT CALLBACK
SYSTRAY_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
77 SystrayItem
*ptrayItem
= systray
;
80 if (ptrayItem
->hWnd
==hWnd
) {
81 if (ptrayItem
->notifyIcon
.hIcon
) {
82 hdc
= BeginPaint(hWnd
, &ps
);
83 GetClientRect(hWnd
, &rc
);
84 if (!DrawIconEx(hdc
, rc
.left
+ICON_BORDER
, rc
.top
+ICON_BORDER
, ptrayItem
->notifyIcon
.hIcon
,
85 ICON_SIZE
, ICON_SIZE
, 0, 0, DI_DEFAULTSIZE
|DI_NORMAL
)) {
86 ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd
, ptrayItem
);
87 SYSTRAY_Delete(&ptrayItem
->notifyIcon
);
92 ptrayItem
= ptrayItem
->nextTrayItem
;
107 SystrayItem
*ptrayItem
= systray
;
109 while ( ptrayItem
) {
110 if (ptrayItem
->hWnd
== hWnd
) {
115 msg
.time
= GetMessageTime ();
116 msg
.pt
.x
= LOWORD(GetMessagePos ());
117 msg
.pt
.y
= HIWORD(GetMessagePos ());
119 SendMessageA(ptrayItem
->hWndToolTip
, TTM_RELAYEVENT
, 0, (LPARAM
)&msg
);
121 ptrayItem
= ptrayItem
->nextTrayItem
;
126 case WM_LBUTTONDBLCLK
:
127 case WM_RBUTTONDBLCLK
:
128 case WM_MBUTTONDBLCLK
:
130 SystrayItem
*ptrayItem
= systray
;
133 if (ptrayItem
->hWnd
== hWnd
) {
134 if (ptrayItem
->notifyIcon
.hWnd
&& ptrayItem
->notifyIcon
.uCallbackMessage
) {
135 if (!PostMessageA(ptrayItem
->notifyIcon
.hWnd
, ptrayItem
->notifyIcon
.uCallbackMessage
,
136 (WPARAM
)ptrayItem
->notifyIcon
.uID
, (LPARAM
)message
)) {
137 ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd
, ptrayItem
);
138 SYSTRAY_Delete(&ptrayItem
->notifyIcon
);
143 ptrayItem
= ptrayItem
->nextTrayItem
;
149 return (DefWindowProcA(hWnd
, message
, wParam
, lParam
));
156 BOOL
SYSTRAY_RegisterClass(void)
160 wc
.style
= CS_SAVEBITS
|CS_DBLCLKS
;
161 wc
.lpfnWndProc
= (WNDPROC
)SYSTRAY_WndProc
;
166 wc
.hCursor
= LoadCursorA(0, IDC_ARROWA
);
167 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
);
168 wc
.lpszMenuName
= NULL
;
169 wc
.lpszClassName
= "WineSystray";
171 if (!RegisterClassA(&wc
)) {
172 ERR("RegisterClass(WineSystray) failed\n");
179 BOOL
SYSTRAY_ItemInit(SystrayItem
*ptrayItem
)
183 /* Register the class if this is our first tray item. */
184 if ( firstSystray
) {
185 firstSystray
= FALSE
;
186 if ( !SYSTRAY_RegisterClass() ) {
187 ERR( "RegisterClass(WineSystray) failed\n" );
192 /* Initialize the window size. */
195 rect
.right
= ICON_SIZE
+2*ICON_BORDER
;
196 rect
.bottom
= ICON_SIZE
+2*ICON_BORDER
;
198 ZeroMemory( ptrayItem
, sizeof(SystrayItem
) );
199 /* Create tray window for icon. */
200 ptrayItem
->hWnd
= CreateWindowExA( WS_EX_TRAYWINDOW
,
201 "WineSystray", "Wine-Systray",
203 CW_USEDEFAULT
, CW_USEDEFAULT
,
204 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
,
206 if ( !ptrayItem
->hWnd
) {
207 ERR( "CreateWindow(WineSystray) failed\n" );
211 /* Create tooltip for icon. */
212 ptrayItem
->hWndToolTip
= CreateWindowA( TOOLTIPS_CLASSA
,NULL
,TTS_ALWAYSTIP
,
213 CW_USEDEFAULT
, CW_USEDEFAULT
,
214 CW_USEDEFAULT
, CW_USEDEFAULT
,
215 ptrayItem
->hWnd
, 0, 0, 0 );
216 if ( !ptrayItem
->hWndToolTip
) {
217 ERR( "CreateWindow(TOOLTIP) failed\n" );
224 static void SYSTRAY_ItemTerm(SystrayItem
*ptrayItem
)
226 if(ptrayItem
->notifyIcon
.hIcon
)
227 DestroyIcon(ptrayItem
->notifyIcon
.hIcon
);
228 if(ptrayItem
->hWndToolTip
)
229 DestroyWindow(ptrayItem
->hWndToolTip
);
231 DestroyWindow(ptrayItem
->hWnd
);
236 void SYSTRAY_ItemSetMessage(SystrayItem
*ptrayItem
, UINT uCallbackMessage
)
238 ptrayItem
->notifyIcon
.uCallbackMessage
= uCallbackMessage
;
242 void SYSTRAY_ItemSetIcon(SystrayItem
*ptrayItem
, HICON hIcon
)
244 ptrayItem
->notifyIcon
.hIcon
= CopyIcon(hIcon
);
245 InvalidateRect(ptrayItem
->hWnd
, NULL
, TRUE
);
249 void SYSTRAY_ItemSetTip(SystrayItem
*ptrayItem
, CHAR
* szTip
, int modify
)
253 strncpy(ptrayItem
->notifyIcon
.szTip
, szTip
, sizeof(ptrayItem
->notifyIcon
.szTip
));
254 ptrayItem
->notifyIcon
.szTip
[sizeof(ptrayItem
->notifyIcon
.szTip
)-1]=0;
256 ti
.cbSize
= sizeof(TTTOOLINFOA
);
258 ti
.hwnd
= ptrayItem
->hWnd
;
261 ti
.lpszText
= ptrayItem
->notifyIcon
.szTip
;
264 ti
.rect
.right
= ICON_SIZE
+2*ICON_BORDER
;
265 ti
.rect
.bottom
= ICON_SIZE
+2*ICON_BORDER
;
268 SendMessageA(ptrayItem
->hWndToolTip
, TTM_UPDATETIPTEXTA
, 0, (LPARAM
)&ti
);
270 SendMessageA(ptrayItem
->hWndToolTip
, TTM_ADDTOOLA
, 0, (LPARAM
)&ti
);
274 static BOOL
SYSTRAY_Add(PNOTIFYICONDATAA pnid
)
276 SystrayItem
**ptrayItem
= &systray
;
278 /* Find last element. */
279 while( *ptrayItem
) {
280 if ( SYSTRAY_ItemIsEqual(pnid
, &(*ptrayItem
)->notifyIcon
) )
282 ptrayItem
= &((*ptrayItem
)->nextTrayItem
);
284 /* Allocate SystrayItem for element and add to end of list. */
285 (*ptrayItem
) = ( SystrayItem
*)malloc( sizeof(SystrayItem
) );
287 /* Initialize and set data for the tray element. */
288 SYSTRAY_ItemInit( (*ptrayItem
) );
289 (*ptrayItem
)->notifyIcon
.uID
= pnid
->uID
; /* only needed for callback message */
290 (*ptrayItem
)->notifyIcon
.hWnd
= pnid
->hWnd
; /* only needed for callback message */
291 SYSTRAY_ItemSetIcon (*ptrayItem
, (pnid
->uFlags
&NIF_ICON
) ?pnid
->hIcon
:0);
292 SYSTRAY_ItemSetMessage(*ptrayItem
, (pnid
->uFlags
&NIF_MESSAGE
)?pnid
->uCallbackMessage
:0);
293 SYSTRAY_ItemSetTip (*ptrayItem
, (pnid
->uFlags
&NIF_TIP
) ?pnid
->szTip
:"", FALSE
);
295 TRACE("%p: %p %s\n", (*ptrayItem
), (*ptrayItem
)->notifyIcon
.hWnd
,
296 (*ptrayItem
)->notifyIcon
.szTip
);
301 static BOOL
SYSTRAY_Modify(PNOTIFYICONDATAA pnid
)
303 SystrayItem
*ptrayItem
= systray
;
305 while ( ptrayItem
) {
306 if ( SYSTRAY_ItemIsEqual(pnid
, &ptrayItem
->notifyIcon
) ) {
307 if (pnid
->uFlags
& NIF_ICON
)
308 SYSTRAY_ItemSetIcon(ptrayItem
, pnid
->hIcon
);
309 if (pnid
->uFlags
& NIF_MESSAGE
)
310 SYSTRAY_ItemSetMessage(ptrayItem
, pnid
->uCallbackMessage
);
311 if (pnid
->uFlags
& NIF_TIP
)
312 SYSTRAY_ItemSetTip(ptrayItem
, pnid
->szTip
, TRUE
);
314 TRACE("%p: %p %s\n", ptrayItem
, ptrayItem
->notifyIcon
.hWnd
, ptrayItem
->notifyIcon
.szTip
);
317 ptrayItem
= ptrayItem
->nextTrayItem
;
319 return FALSE
; /* not found */
323 static BOOL
SYSTRAY_Delete(PNOTIFYICONDATAA pnid
)
325 SystrayItem
**ptrayItem
= &systray
;
328 if (SYSTRAY_ItemIsEqual(pnid
, &(*ptrayItem
)->notifyIcon
)) {
329 SystrayItem
*next
= (*ptrayItem
)->nextTrayItem
;
330 TRACE("%p: %p %s\n", *ptrayItem
, (*ptrayItem
)->notifyIcon
.hWnd
, (*ptrayItem
)->notifyIcon
.szTip
);
331 SYSTRAY_ItemTerm(*ptrayItem
);
338 ptrayItem
= &((*ptrayItem
)->nextTrayItem
);
341 return FALSE
; /* not found */
344 /*************************************************************************
347 BOOL
SYSTRAY_Init(void)
352 /*************************************************************************
353 * Shell_NotifyIcon [SHELL32.296]
354 * Shell_NotifyIconA [SHELL32.297]
356 BOOL WINAPI
Shell_NotifyIconA(DWORD dwMessage
, PNOTIFYICONDATAA pnid
)
359 TRACE("enter %p %d %ld\n", pnid
->hWnd
, pnid
->uID
, dwMessage
);
362 flag
= SYSTRAY_Add(pnid
);
365 flag
= SYSTRAY_Modify(pnid
);
368 flag
= SYSTRAY_Delete(pnid
);
371 TRACE("leave %p %d %ld=%d\n", pnid
->hWnd
, pnid
->uID
, dwMessage
, flag
);
375 /*************************************************************************
376 * Shell_NotifyIconW [SHELL32.298]
378 BOOL WINAPI
Shell_NotifyIconW (DWORD dwMessage
, PNOTIFYICONDATAW pnid
)
382 PNOTIFYICONDATAA p
= HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA
));
383 memcpy(p
, pnid
, sizeof(NOTIFYICONDATAA
));
384 WideCharToMultiByte( CP_ACP
, 0, pnid
->szTip
, -1, p
->szTip
, sizeof(p
->szTip
), NULL
, NULL
);
385 p
->szTip
[sizeof(p
->szTip
)-1] = 0;
387 ret
= Shell_NotifyIconA(dwMessage
, p
);
389 HeapFree(GetProcessHeap(),0,p
);