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 is handled in dlls/x11drv/window.c,
8 * X11DRV_set_wm_hints using KWM_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
40 #include "shell32_main.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46 typedef struct SystrayItem {
49 NOTIFYICONDATAW notifyIcon;
50 struct SystrayItem *nextTrayItem;
53 static SystrayItem *systray=NULL;
54 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
57 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
58 /* space around icon (forces icon to center of KDE systray area) */
63 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAW pnid1, PNOTIFYICONDATAW pnid2)
65 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
66 if (pnid1->uID != pnid2->uID) return FALSE;
71 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
73 if(ptrayItem->notifyIcon.hIcon)
74 DestroyIcon(ptrayItem->notifyIcon.hIcon);
75 if(ptrayItem->hWndToolTip)
76 DestroyWindow(ptrayItem->hWndToolTip);
78 DestroyWindow(ptrayItem->hWnd);
83 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAW pnid)
85 SystrayItem **ptrayItem = &systray;
88 if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
89 SystrayItem *next = (*ptrayItem)->nextTrayItem;
90 TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, debugstr_w((*ptrayItem)->notifyIcon.szTip));
91 SYSTRAY_ItemTerm(*ptrayItem);
93 HeapFree(GetProcessHeap(),0,*ptrayItem);
98 ptrayItem = &((*ptrayItem)->nextTrayItem);
101 return FALSE; /* not found */
104 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
113 SystrayItem *ptrayItem = systray;
116 if (ptrayItem->hWnd==hWnd) {
117 if (ptrayItem->notifyIcon.hIcon) {
118 hdc = BeginPaint(hWnd, &ps);
119 GetClientRect(hWnd, &rc);
120 if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
121 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
122 ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
123 SYSTRAY_Delete(&ptrayItem->notifyIcon);
128 ptrayItem = ptrayItem->nextTrayItem;
143 SystrayItem *ptrayItem = systray;
145 while ( ptrayItem ) {
146 if (ptrayItem->hWnd == hWnd) {
151 msg.time = GetMessageTime ();
152 msg.pt.x = LOWORD(GetMessagePos ());
153 msg.pt.y = HIWORD(GetMessagePos ());
155 SendMessageW(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
157 ptrayItem = ptrayItem->nextTrayItem;
162 case WM_LBUTTONDBLCLK:
163 case WM_RBUTTONDBLCLK:
164 case WM_MBUTTONDBLCLK:
166 SystrayItem *ptrayItem = systray;
169 if (ptrayItem->hWnd == hWnd) {
170 if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
171 if (!PostMessageW(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
172 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
173 ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
174 SYSTRAY_Delete(&ptrayItem->notifyIcon);
179 ptrayItem = ptrayItem->nextTrayItem;
185 return (DefWindowProcW(hWnd, message, wParam, lParam));
192 static BOOL SYSTRAY_RegisterClass(void)
195 static const WCHAR WineSystrayW[] = { 'W','i','n','e','S','y','s','t','r','a','y',0 };
197 wc.style = CS_SAVEBITS|CS_DBLCLKS;
198 wc.lpfnWndProc = SYSTRAY_WndProc;
203 wc.hCursor = LoadCursorW(0, (LPWSTR)IDC_ARROW);
204 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
205 wc.lpszMenuName = NULL;
206 wc.lpszClassName = WineSystrayW;
208 if (!RegisterClassW(&wc)) {
209 ERR("RegisterClass(WineSystray) failed\n");
216 static BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
219 static const WCHAR WineSystrayW[] = { 'W','i','n','e','S','y','s','t','r','a','y',0 };
220 static const WCHAR Wine_SystrayW[] = { 'W','i','n','e','-','S','y','s','t','r','a','y',0 };
222 /* Register the class if this is our first tray item. */
223 if ( firstSystray ) {
224 firstSystray = FALSE;
225 if ( !SYSTRAY_RegisterClass() ) {
226 ERR( "RegisterClass(WineSystray) failed\n" );
231 /* Initialize the window size. */
234 rect.right = ICON_SIZE+2*ICON_BORDER;
235 rect.bottom = ICON_SIZE+2*ICON_BORDER;
237 ZeroMemory( ptrayItem, sizeof(SystrayItem) );
238 /* Create tray window for icon. */
239 ptrayItem->hWnd = CreateWindowExW( WS_EX_TRAYWINDOW,
240 WineSystrayW, Wine_SystrayW,
242 CW_USEDEFAULT, CW_USEDEFAULT,
243 rect.right-rect.left, rect.bottom-rect.top,
245 if ( !ptrayItem->hWnd ) {
246 ERR( "CreateWindow(WineSystray) failed\n" );
250 /* Create tooltip for icon. */
251 ptrayItem->hWndToolTip = CreateWindowW( TOOLTIPS_CLASSW,NULL,TTS_ALWAYSTIP,
252 CW_USEDEFAULT, CW_USEDEFAULT,
253 CW_USEDEFAULT, CW_USEDEFAULT,
254 ptrayItem->hWnd, 0, 0, 0 );
255 if ( !ptrayItem->hWndToolTip ) {
256 ERR( "CreateWindow(TOOLTIP) failed\n" );
263 static void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
265 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
269 static void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
271 if(ptrayItem->notifyIcon.hIcon)
272 DestroyIcon(ptrayItem->notifyIcon.hIcon);
273 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
274 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
278 static void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, const WCHAR* szTip, int modify)
282 lstrcpynW(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip)/sizeof(WCHAR));
284 ti.cbSize = sizeof(TTTOOLINFOW);
286 ti.hwnd = ptrayItem->hWnd;
289 ti.lpszText = ptrayItem->notifyIcon.szTip;
292 ti.rect.right = ICON_SIZE+2*ICON_BORDER;
293 ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
296 SendMessageW(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
298 SendMessageW(ptrayItem->hWndToolTip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
302 static BOOL SYSTRAY_Add(PNOTIFYICONDATAW pnid)
304 SystrayItem **ptrayItem = &systray;
305 static const WCHAR emptyW[] = { 0 };
307 /* Find last element. */
308 while( *ptrayItem ) {
309 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
311 ptrayItem = &((*ptrayItem)->nextTrayItem);
313 /* Allocate SystrayItem for element and add to end of list. */
314 (*ptrayItem) = HeapAlloc(GetProcessHeap(),0,sizeof(SystrayItem));
316 /* Initialize and set data for the tray element. */
317 SYSTRAY_ItemInit( (*ptrayItem) );
318 (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
319 (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
320 SYSTRAY_ItemSetIcon (*ptrayItem, (pnid->uFlags&NIF_ICON) ?pnid->hIcon :0);
321 SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
322 SYSTRAY_ItemSetTip (*ptrayItem, (pnid->uFlags&NIF_TIP) ?pnid->szTip :emptyW, FALSE);
324 TRACE("%p: %p %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
325 debugstr_w((*ptrayItem)->notifyIcon.szTip));
330 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAW pnid)
332 SystrayItem *ptrayItem = systray;
334 while ( ptrayItem ) {
335 if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
336 if (pnid->uFlags & NIF_ICON)
337 SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
338 if (pnid->uFlags & NIF_MESSAGE)
339 SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
340 if (pnid->uFlags & NIF_TIP)
341 SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
343 TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, debugstr_w(ptrayItem->notifyIcon.szTip));
346 ptrayItem = ptrayItem->nextTrayItem;
348 return FALSE; /* not found */
352 /*************************************************************************
355 BOOL SYSTRAY_Init(void)
360 /*************************************************************************
361 * Shell_NotifyIconW [SHELL32.298]
363 BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW pnid )
366 TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
369 flag = SYSTRAY_Add(pnid);
372 flag = SYSTRAY_Modify(pnid);
375 flag = SYSTRAY_Delete(pnid);
378 TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
382 /*************************************************************************
383 * Shell_NotifyIconA [SHELL32.297]
384 * Shell_NotifyIcon [SHELL32.296]
386 BOOL WINAPI Shell_NotifyIconA (DWORD dwMessage, PNOTIFYICONDATAA pnid )
390 PNOTIFYICONDATAW p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAW));
391 memcpy(p, pnid, sizeof(NOTIFYICONDATAW));
392 MultiByteToWideChar( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip)/sizeof(WCHAR) );
393 p->szTip[sizeof(p->szTip)/sizeof(WCHAR)-1] = 0;
395 ret = Shell_NotifyIconW(dwMessage, p );
397 HeapFree(GetProcessHeap(),0,p);