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.
18 #include "shell32_main.h"
21 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(shell)
26 typedef struct SystrayData {
29 NOTIFYICONDATAA notifyIcon;
30 int nitem; /* number of current element = tooltip id */
31 struct SystrayData *nextTrayItem;
34 typedef struct Systray {
36 CRITICAL_SECTION critSection;
37 SystrayData *systrayItemList;
40 static Systray systray;
41 static int nNumberTrayElements;
44 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
47 /**************************************************************************
52 #define SMALL_ICON_SIZE GetSystemMetrics(SM_CXSMICON)
54 /* space between icons and frame */
57 #define TBORDER (OBORDER+1+IBORDER)
59 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
68 SystrayData *ptrayItem = systray.systrayItemList;
72 if (ptrayItem->hWnd == hWnd)
74 hdc = BeginPaint(hWnd, &ps);
75 GetClientRect(hWnd, &rc);
77 if (!DrawIconEx(hdc, rc.left, rc.top, ptrayItem->notifyIcon.hIcon,
78 SMALL_ICON_SIZE, SMALL_ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL))
79 SYSTRAY_Delete(&ptrayItem->notifyIcon);
81 ptrayItem = ptrayItem->nextTrayItem;
96 SystrayData *ptrayItem = systray.systrayItemList;
100 if (ptrayItem->hWnd == hWnd)
106 msg.time = GetMessageTime ();
107 msg.pt.x = LOWORD(GetMessagePos ());
108 msg.pt.y = HIWORD(GetMessagePos ());
110 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
112 ptrayItem = ptrayItem->nextTrayItem;
116 case WM_LBUTTONDBLCLK:
117 case WM_RBUTTONDBLCLK:
118 case WM_MBUTTONDBLCLK:
121 SystrayData *ptrayItem = systray.systrayItemList;
125 if (ptrayItem->hWnd == hWnd)
127 xPos = LOWORD(lParam);
128 if( (xPos >= TBORDER) &&
129 (xPos < (TBORDER+SMALL_ICON_SIZE)) )
131 if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
132 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message))
133 SYSTRAY_Delete(&ptrayItem->notifyIcon);
137 ptrayItem = ptrayItem->nextTrayItem;
143 return (DefWindowProcA(hWnd, message, wParam, lParam));
149 BOOL SYSTRAY_RegisterClass(void)
153 wc.style = CS_SAVEBITS;
154 wc.lpfnWndProc = (WNDPROC)SYSTRAY_WndProc;
158 wc.hIcon = 0; /* LoadIcon (NULL, IDI_EXCLAMATION); */
159 wc.hCursor = LoadCursorA(0, IDC_ARROWA);
160 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
161 wc.lpszMenuName = NULL;
162 wc.lpszClassName = "WineSystray";
164 if (!RegisterClassA(&wc)) {
165 ERR("RegisterClass(WineSystray) failed\n");
172 BOOL SYSTRAY_Create(SystrayData *ptrayItem)
176 /* Register the class if this is our first tray item. */
177 if ( nNumberTrayElements == 1 )
179 if ( !SYSTRAY_RegisterClass() )
181 ERR( "RegisterClass(WineSystray) failed\n" );
186 /* Initialize the window size. */
189 rect.right = SMALL_ICON_SIZE+2*TBORDER;
190 rect.bottom = SMALL_ICON_SIZE+2*TBORDER;
192 /* Create tray window for icon. */
193 ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
194 "WineSystray", "Wine-Systray",
196 CW_USEDEFAULT, CW_USEDEFAULT,
197 rect.right-rect.left, rect.bottom-rect.top,
199 if ( !ptrayItem->hWnd )
201 ERR( "CreateWindow(WineSystray) failed\n" );
205 /* Create tooltip for icon. */
206 ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
207 CW_USEDEFAULT, CW_USEDEFAULT,
208 CW_USEDEFAULT, CW_USEDEFAULT,
209 ptrayItem->hWnd, 0, 0, 0 );
210 if ( ptrayItem->hWndToolTip==0 )
212 ERR( "CreateWindow(TOOLTIP) failed\n" );
218 static void SYSTRAY_RepaintAll(void)
220 SystrayData *ptrayItem = systray.systrayItemList;
224 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
225 ptrayItem = ptrayItem->nextTrayItem;
230 static void SYSTRAY_RepaintItem(int nitem)
233 SystrayData *ptrayItem = systray.systrayItemList;
237 if (ptrayItem->nitem == nitem)
238 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
240 ptrayItem = ptrayItem->nextTrayItem;
244 void SYSTRAY_InitItem(SystrayData *ptrayItem)
246 ptrayItem->nitem = nNumberTrayElements++;
247 SYSTRAY_Create(ptrayItem);
250 void SYSTRAY_SetIcon(SystrayData *ptrayItem, HICON hIcon)
252 ptrayItem->notifyIcon.hIcon = hIcon;
255 void SYSTRAY_SetTip(SystrayData *ptrayItem, CHAR* szTip)
259 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
260 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
262 ti.cbSize = sizeof(TTTOOLINFOA);
264 ti.hwnd = ptrayItem->hWnd;
266 ti.uId = ptrayItem->nitem;
267 ti.lpszText = ptrayItem->notifyIcon.szTip;
270 ti.rect.right = SMALL_ICON_SIZE+2*TBORDER;
271 ti.rect.bottom = SMALL_ICON_SIZE+2*TBORDER;
273 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
276 static void SYSTRAY_ModifyTip(SystrayData *ptrayItem, CHAR* szTip)
280 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
281 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
283 ti.cbSize = sizeof(TTTOOLINFOA);
285 ti.hwnd = ptrayItem->hWnd;
287 ti.uId = ptrayItem->nitem;
288 ti.lpszText = ptrayItem->notifyIcon.szTip;
291 ti.rect.right = SMALL_ICON_SIZE+2*TBORDER;
292 ti.rect.bottom = SMALL_ICON_SIZE+2*TBORDER;
294 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
297 static void SYSTRAY_TermItem(SystrayData *removeItem)
300 SystrayData **trayItem;
302 ti.cbSize = sizeof(TTTOOLINFOA);
306 /* delete all tooltips ...*/
307 trayItem = &systray.systrayItemList;
309 ti.uId = (*trayItem)->nitem;
310 ti.hwnd = (*trayItem)->hWnd;
311 SendMessageA((*trayItem)->hWndToolTip, TTM_DELTOOLA, 0, (LPARAM)&ti);
312 trayItem = &((*trayItem)->nextTrayItem);
314 /* ... and add them again, because uID may shift */
316 trayItem = &systray.systrayItemList;
319 if (*trayItem != removeItem)
321 (*trayItem)->nitem = nitem;
323 ti.hwnd = (*trayItem)->hWnd;
324 ti.lpszText = (*trayItem)->notifyIcon.szTip;
327 ti.rect.right = SMALL_ICON_SIZE+2*TBORDER;
328 ti.rect.bottom = SMALL_ICON_SIZE+2*TBORDER;
329 SendMessageA((*trayItem)->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
332 trayItem = &((*trayItem)->nextTrayItem);
334 nNumberTrayElements--;
338 /**************************************************************************
342 void SYSTRAY_SetMessage(SystrayData *ptrayItem, UINT uCallbackMessage)
344 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
347 static BOOL SYSTRAY_IsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
349 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
350 if (pnid1->uID != pnid2->uID) return FALSE;
354 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
356 SystrayData **ptrayItem = &systray.systrayItemList;
358 /* Find empty space for new element. */
361 if ( SYSTRAY_IsEqual(pnid, &(*ptrayItem)->notifyIcon) )
363 ptrayItem = &((*ptrayItem)->nextTrayItem);
366 /* Allocate SystrayData for element and zero memory. */
367 (*ptrayItem) = ( SystrayData *)malloc( sizeof(SystrayData) );
368 ZeroMemory( (*ptrayItem), sizeof(SystrayData) );
370 /* Copy notification data */
371 memcpy( &(*ptrayItem)->notifyIcon, pnid, sizeof(NOTIFYICONDATAA) );
373 /* Initialize and set data for the tray element. */
374 SYSTRAY_InitItem( (*ptrayItem) );
376 SYSTRAY_SetIcon (*ptrayItem, (pnid->uFlags&NIF_ICON) ?pnid->hIcon :0);
377 SYSTRAY_SetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
378 SYSTRAY_SetTip (*ptrayItem, (pnid->uFlags&NIF_TIP) ?pnid->szTip :"");
380 (*ptrayItem)->nextTrayItem = NULL; /* may be overkill after the ZeroMemory call. */
382 /* Repaint all system tray icons as we have added one. */
383 SYSTRAY_RepaintAll();
385 TRACE("%p: 0x%08x %d %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
386 (*ptrayItem)->notifyIcon.uID,
387 (*ptrayItem)->notifyIcon.szTip);
391 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
393 SystrayData *ptrayItem = systray.systrayItemList;
397 if ( SYSTRAY_IsEqual(pnid, &ptrayItem->notifyIcon) )
399 if (pnid->uFlags & NIF_ICON)
401 SYSTRAY_SetIcon(ptrayItem, pnid->hIcon);
402 SYSTRAY_RepaintItem(ptrayItem->nitem);
405 if (pnid->uFlags & NIF_MESSAGE)
406 SYSTRAY_SetMessage(ptrayItem, pnid->uCallbackMessage);
408 if (pnid->uFlags & NIF_TIP)
409 SYSTRAY_ModifyTip(ptrayItem, pnid->szTip);
411 TRACE("%p: 0x%08x %d %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd,
412 ptrayItem->notifyIcon.uID, ptrayItem->notifyIcon.szTip);
415 ptrayItem = ptrayItem->nextTrayItem;
417 return FALSE; /* not found */
420 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
422 SystrayData **ptrayItem = &systray.systrayItemList;
426 if (SYSTRAY_IsEqual(pnid, &(*ptrayItem)->notifyIcon))
428 SystrayData *next = (*ptrayItem)->nextTrayItem;
429 TRACE("%p: 0x%08x %d %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd,
430 (*ptrayItem)->notifyIcon.uID, (*ptrayItem)->notifyIcon.szTip);
431 SYSTRAY_TermItem(*ptrayItem);
433 DestroyWindow((*ptrayItem)->hWndToolTip);
434 DestroyWindow((*ptrayItem)->hWnd);
439 SYSTRAY_RepaintAll();
443 ptrayItem = &((*ptrayItem)->nextTrayItem);
446 return FALSE; /* not found */
449 /*************************************************************************
452 BOOL SYSTRAY_Init(void)
454 if (!systray.hasCritSection)
456 systray.hasCritSection=1;
457 InitializeCriticalSection(&systray.critSection);
458 MakeCriticalSectionGlobal(&systray.critSection);
459 TRACE(" =%p\n", &systray.critSection);
465 /*************************************************************************
466 * Shell_NotifyIconA [SHELL32.297]
468 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
471 TRACE("wait %d %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
472 /* must be serialized because all apps access same systray */
473 EnterCriticalSection(&systray.critSection);
474 TRACE("enter %d %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
479 TRACE("Calling systray add\n");
480 flag = SYSTRAY_Add(pnid);
483 flag = SYSTRAY_Modify(pnid);
486 flag = SYSTRAY_Delete(pnid);
490 LeaveCriticalSection(&systray.critSection);
491 TRACE("leave %d %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
495 /*************************************************************************
496 * Shell_NotifyIconW [SHELL32.297]
498 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
502 PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
503 memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
505 lstrcpynWtoA (p->szTip, pnid->szTip, 64 );
507 ret = Shell_NotifyIconA(dwMessage, p );
509 HeapFree(GetProcessHeap(),0,p);
512 /*************************************************************************
513 * Shell_NotifyIcon [SHELL32.296]
515 BOOL WINAPI Shell_NotifyIcon(DWORD dwMessage, PNOTIFYICONDATAA pnid)
517 return Shell_NotifyIconA(dwMessage, pnid);