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 SystrayItem {
29 NOTIFYICONDATAA notifyIcon;
30 struct SystrayItem *nextTrayItem;
33 static SystrayItem *systray=NULL;
34 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
36 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
39 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
40 /* space around icon (forces icon to center of KDE systray area) */
45 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
47 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
48 if (pnid1->uID != pnid2->uID) return FALSE;
52 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
61 SystrayItem *ptrayItem = systray;
64 if (ptrayItem->hWnd==hWnd) {
65 if (ptrayItem->notifyIcon.hIcon) {
66 hdc = BeginPaint(hWnd, &ps);
67 GetClientRect(hWnd, &rc);
68 if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
69 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
70 ERR("Paint(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
71 SYSTRAY_Delete(&ptrayItem->notifyIcon);
76 ptrayItem = ptrayItem->nextTrayItem;
91 SystrayItem *ptrayItem = systray;
94 if (ptrayItem->hWnd == hWnd) {
99 msg.time = GetMessageTime ();
100 msg.pt.x = LOWORD(GetMessagePos ());
101 msg.pt.y = HIWORD(GetMessagePos ());
103 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
105 ptrayItem = ptrayItem->nextTrayItem;
110 case WM_LBUTTONDBLCLK:
111 case WM_RBUTTONDBLCLK:
112 case WM_MBUTTONDBLCLK:
114 SystrayItem *ptrayItem = systray;
117 if (ptrayItem->hWnd == hWnd) {
118 if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
119 if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
120 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
121 ERR("PostMessage(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
122 SYSTRAY_Delete(&ptrayItem->notifyIcon);
127 ptrayItem = ptrayItem->nextTrayItem;
133 return (DefWindowProcA(hWnd, message, wParam, lParam));
140 BOOL SYSTRAY_RegisterClass(void)
144 wc.style = CS_SAVEBITS;
145 wc.lpfnWndProc = (WNDPROC)SYSTRAY_WndProc;
150 wc.hCursor = LoadCursorA(0, IDC_ARROWA);
151 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
152 wc.lpszMenuName = NULL;
153 wc.lpszClassName = "WineSystray";
155 if (!RegisterClassA(&wc)) {
156 ERR("RegisterClass(WineSystray) failed\n");
163 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
167 /* Register the class if this is our first tray item. */
168 if ( firstSystray ) {
169 firstSystray = FALSE;
170 if ( !SYSTRAY_RegisterClass() ) {
171 ERR( "RegisterClass(WineSystray) failed\n" );
176 /* Initialize the window size. */
179 rect.right = ICON_SIZE+2*ICON_BORDER;
180 rect.bottom = ICON_SIZE+2*ICON_BORDER;
182 ZeroMemory( ptrayItem, sizeof(SystrayItem) );
183 /* Create tray window for icon. */
184 ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
185 "WineSystray", "Wine-Systray",
187 CW_USEDEFAULT, CW_USEDEFAULT,
188 rect.right-rect.left, rect.bottom-rect.top,
190 if ( !ptrayItem->hWnd ) {
191 ERR( "CreateWindow(WineSystray) failed\n" );
195 /* Create tooltip for icon. */
196 ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
197 CW_USEDEFAULT, CW_USEDEFAULT,
198 CW_USEDEFAULT, CW_USEDEFAULT,
199 ptrayItem->hWnd, 0, 0, 0 );
200 if ( !ptrayItem->hWndToolTip ) {
201 ERR( "CreateWindow(TOOLTIP) failed\n" );
208 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
210 if(ptrayItem->notifyIcon.hIcon)
211 DestroyIcon(ptrayItem->notifyIcon.hIcon);
212 if(ptrayItem->hWndToolTip)
213 DestroyWindow(ptrayItem->hWndToolTip);
215 DestroyWindow(ptrayItem->hWnd);
220 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
222 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
226 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
228 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
229 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
233 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
237 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
238 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
240 ti.cbSize = sizeof(TTTOOLINFOA);
242 ti.hwnd = ptrayItem->hWnd;
245 ti.lpszText = ptrayItem->notifyIcon.szTip;
248 ti.rect.right = ICON_SIZE+2*ICON_BORDER;
249 ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
252 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
254 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
258 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
260 SystrayItem **ptrayItem = &systray;
262 /* Find last element. */
263 while( *ptrayItem ) {
264 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
266 ptrayItem = &((*ptrayItem)->nextTrayItem);
268 /* Allocate SystrayItem for element and add to end of list. */
269 (*ptrayItem) = ( SystrayItem *)malloc( sizeof(SystrayItem) );
271 /* Initialize and set data for the tray element. */
272 SYSTRAY_ItemInit( (*ptrayItem) );
273 (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
274 (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
275 SYSTRAY_ItemSetIcon (*ptrayItem, (pnid->uFlags&NIF_ICON) ?pnid->hIcon :0);
276 SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
277 SYSTRAY_ItemSetTip (*ptrayItem, (pnid->uFlags&NIF_TIP) ?pnid->szTip :"", FALSE);
279 TRACE("%p: 0x%08x %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
280 (*ptrayItem)->notifyIcon.szTip);
285 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
287 SystrayItem *ptrayItem = systray;
289 while ( ptrayItem ) {
290 if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
291 if (pnid->uFlags & NIF_ICON)
292 SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
293 if (pnid->uFlags & NIF_MESSAGE)
294 SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
295 if (pnid->uFlags & NIF_TIP)
296 SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
298 TRACE("%p: 0x%08x %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
301 ptrayItem = ptrayItem->nextTrayItem;
303 return FALSE; /* not found */
307 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
309 SystrayItem **ptrayItem = &systray;
312 if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
313 SystrayItem *next = (*ptrayItem)->nextTrayItem;
314 TRACE("%p: 0x%08x %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
315 SYSTRAY_ItemTerm(*ptrayItem);
322 ptrayItem = &((*ptrayItem)->nextTrayItem);
325 return FALSE; /* not found */
328 /*************************************************************************
331 BOOL SYSTRAY_Init(void)
336 /*************************************************************************
337 * Shell_NotifyIconA [SHELL32.297]
339 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
342 TRACE("enter %d %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
345 flag = SYSTRAY_Add(pnid);
348 flag = SYSTRAY_Modify(pnid);
351 flag = SYSTRAY_Delete(pnid);
354 TRACE("leave %d %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
358 /*************************************************************************
359 * Shell_NotifyIconW [SHELL32.297]
361 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
365 PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
366 memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
368 lstrcpynWtoA (p->szTip, pnid->szTip, 64 );
370 ret = Shell_NotifyIconA(dwMessage, p );
372 HeapFree(GetProcessHeap(),0,p);
376 /*************************************************************************
377 * Shell_NotifyIcon [SHELL32.296]
379 BOOL WINAPI Shell_NotifyIcon(DWORD dwMessage, PNOTIFYICONDATAA pnid)
381 return Shell_NotifyIconA(dwMessage, pnid);