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.
16 #include "wine/winestring.h"
19 #include "shell32_main.h"
21 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(shell)
27 typedef struct SystrayItem {
30 NOTIFYICONDATAA notifyIcon;
31 struct SystrayItem *nextTrayItem;
34 static SystrayItem *systray=NULL;
35 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
37 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
40 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
41 /* space around icon (forces icon to center of KDE systray area) */
46 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
48 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
49 if (pnid1->uID != pnid2->uID) return FALSE;
53 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
62 SystrayItem *ptrayItem = systray;
65 if (ptrayItem->hWnd==hWnd) {
66 if (ptrayItem->notifyIcon.hIcon) {
67 hdc = BeginPaint(hWnd, &ps);
68 GetClientRect(hWnd, &rc);
69 if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
70 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
71 ERR("Paint(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
72 SYSTRAY_Delete(&ptrayItem->notifyIcon);
77 ptrayItem = ptrayItem->nextTrayItem;
92 SystrayItem *ptrayItem = systray;
95 if (ptrayItem->hWnd == hWnd) {
100 msg.time = GetMessageTime ();
101 msg.pt.x = LOWORD(GetMessagePos ());
102 msg.pt.y = HIWORD(GetMessagePos ());
104 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
106 ptrayItem = ptrayItem->nextTrayItem;
111 case WM_LBUTTONDBLCLK:
112 case WM_RBUTTONDBLCLK:
113 case WM_MBUTTONDBLCLK:
115 SystrayItem *ptrayItem = systray;
118 if (ptrayItem->hWnd == hWnd) {
119 if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
120 if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
121 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
122 ERR("PostMessage(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
123 SYSTRAY_Delete(&ptrayItem->notifyIcon);
128 ptrayItem = ptrayItem->nextTrayItem;
134 return (DefWindowProcA(hWnd, message, wParam, lParam));
141 BOOL SYSTRAY_RegisterClass(void)
145 wc.style = CS_SAVEBITS;
146 wc.lpfnWndProc = (WNDPROC)SYSTRAY_WndProc;
151 wc.hCursor = LoadCursorA(0, IDC_ARROWA);
152 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
153 wc.lpszMenuName = NULL;
154 wc.lpszClassName = "WineSystray";
156 if (!RegisterClassA(&wc)) {
157 ERR("RegisterClass(WineSystray) failed\n");
164 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
168 /* Register the class if this is our first tray item. */
169 if ( firstSystray ) {
170 firstSystray = FALSE;
171 if ( !SYSTRAY_RegisterClass() ) {
172 ERR( "RegisterClass(WineSystray) failed\n" );
177 /* Initialize the window size. */
180 rect.right = ICON_SIZE+2*ICON_BORDER;
181 rect.bottom = ICON_SIZE+2*ICON_BORDER;
183 ZeroMemory( ptrayItem, sizeof(SystrayItem) );
184 /* Create tray window for icon. */
185 ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
186 "WineSystray", "Wine-Systray",
188 CW_USEDEFAULT, CW_USEDEFAULT,
189 rect.right-rect.left, rect.bottom-rect.top,
191 if ( !ptrayItem->hWnd ) {
192 ERR( "CreateWindow(WineSystray) failed\n" );
196 /* Create tooltip for icon. */
197 ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
198 CW_USEDEFAULT, CW_USEDEFAULT,
199 CW_USEDEFAULT, CW_USEDEFAULT,
200 ptrayItem->hWnd, 0, 0, 0 );
201 if ( !ptrayItem->hWndToolTip ) {
202 ERR( "CreateWindow(TOOLTIP) failed\n" );
209 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
211 if(ptrayItem->notifyIcon.hIcon)
212 DestroyIcon(ptrayItem->notifyIcon.hIcon);
213 if(ptrayItem->hWndToolTip)
214 DestroyWindow(ptrayItem->hWndToolTip);
216 DestroyWindow(ptrayItem->hWnd);
221 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
223 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
227 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
229 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
230 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
234 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
238 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
239 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
241 ti.cbSize = sizeof(TTTOOLINFOA);
243 ti.hwnd = ptrayItem->hWnd;
246 ti.lpszText = ptrayItem->notifyIcon.szTip;
249 ti.rect.right = ICON_SIZE+2*ICON_BORDER;
250 ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
253 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
255 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
259 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
261 SystrayItem **ptrayItem = &systray;
263 /* Find last element. */
264 while( *ptrayItem ) {
265 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
267 ptrayItem = &((*ptrayItem)->nextTrayItem);
269 /* Allocate SystrayItem for element and add to end of list. */
270 (*ptrayItem) = ( SystrayItem *)malloc( sizeof(SystrayItem) );
272 /* Initialize and set data for the tray element. */
273 SYSTRAY_ItemInit( (*ptrayItem) );
274 (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
275 (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
276 SYSTRAY_ItemSetIcon (*ptrayItem, (pnid->uFlags&NIF_ICON) ?pnid->hIcon :0);
277 SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
278 SYSTRAY_ItemSetTip (*ptrayItem, (pnid->uFlags&NIF_TIP) ?pnid->szTip :"", FALSE);
280 TRACE("%p: 0x%08x %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
281 (*ptrayItem)->notifyIcon.szTip);
286 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
288 SystrayItem *ptrayItem = systray;
290 while ( ptrayItem ) {
291 if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
292 if (pnid->uFlags & NIF_ICON)
293 SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
294 if (pnid->uFlags & NIF_MESSAGE)
295 SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
296 if (pnid->uFlags & NIF_TIP)
297 SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
299 TRACE("%p: 0x%08x %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
302 ptrayItem = ptrayItem->nextTrayItem;
304 return FALSE; /* not found */
308 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
310 SystrayItem **ptrayItem = &systray;
313 if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
314 SystrayItem *next = (*ptrayItem)->nextTrayItem;
315 TRACE("%p: 0x%08x %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
316 SYSTRAY_ItemTerm(*ptrayItem);
323 ptrayItem = &((*ptrayItem)->nextTrayItem);
326 return FALSE; /* not found */
329 /*************************************************************************
332 BOOL SYSTRAY_Init(void)
337 /*************************************************************************
338 * Shell_NotifyIconA [SHELL32.297][SHELL32.296]
340 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
343 TRACE("enter %d %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
346 flag = SYSTRAY_Add(pnid);
349 flag = SYSTRAY_Modify(pnid);
352 flag = SYSTRAY_Delete(pnid);
355 TRACE("leave %d %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
359 /*************************************************************************
360 * Shell_NotifyIconW [SHELL32.298]
362 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
366 PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
367 memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
369 lstrcpynWtoA (p->szTip, pnid->szTip, 64 );
371 ret = Shell_NotifyIconA(dwMessage, p );
373 HeapFree(GetProcessHeap(),0,p);