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
37 #include "shell32_main.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
43 typedef struct SystrayItem {
46 NOTIFYICONDATAA notifyIcon;
47 struct SystrayItem *nextTrayItem;
50 static SystrayItem *systray=NULL;
51 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
53 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
56 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
57 /* space around icon (forces icon to center of KDE systray area) */
62 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
64 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
65 if (pnid1->uID != pnid2->uID) return FALSE;
69 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
78 SystrayItem *ptrayItem = systray;
81 if (ptrayItem->hWnd==hWnd) {
82 if (ptrayItem->notifyIcon.hIcon) {
83 hdc = BeginPaint(hWnd, &ps);
84 GetClientRect(hWnd, &rc);
85 if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
86 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
87 ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
88 SYSTRAY_Delete(&ptrayItem->notifyIcon);
93 ptrayItem = ptrayItem->nextTrayItem;
108 SystrayItem *ptrayItem = systray;
110 while ( ptrayItem ) {
111 if (ptrayItem->hWnd == hWnd) {
116 msg.time = GetMessageTime ();
117 msg.pt.x = LOWORD(GetMessagePos ());
118 msg.pt.y = HIWORD(GetMessagePos ());
120 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
122 ptrayItem = ptrayItem->nextTrayItem;
127 case WM_LBUTTONDBLCLK:
128 case WM_RBUTTONDBLCLK:
129 case WM_MBUTTONDBLCLK:
131 SystrayItem *ptrayItem = systray;
134 if (ptrayItem->hWnd == hWnd) {
135 if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
136 if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
137 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
138 ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
139 SYSTRAY_Delete(&ptrayItem->notifyIcon);
144 ptrayItem = ptrayItem->nextTrayItem;
150 return (DefWindowProcA(hWnd, message, wParam, lParam));
157 BOOL SYSTRAY_RegisterClass(void)
161 wc.style = CS_SAVEBITS|CS_DBLCLKS;
162 wc.lpfnWndProc = (WNDPROC)SYSTRAY_WndProc;
167 wc.hCursor = LoadCursorA(0, IDC_ARROWA);
168 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
169 wc.lpszMenuName = NULL;
170 wc.lpszClassName = "WineSystray";
172 if (!RegisterClassA(&wc)) {
173 ERR("RegisterClass(WineSystray) failed\n");
180 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
184 /* Register the class if this is our first tray item. */
185 if ( firstSystray ) {
186 firstSystray = FALSE;
187 if ( !SYSTRAY_RegisterClass() ) {
188 ERR( "RegisterClass(WineSystray) failed\n" );
193 /* Initialize the window size. */
196 rect.right = ICON_SIZE+2*ICON_BORDER;
197 rect.bottom = ICON_SIZE+2*ICON_BORDER;
199 ZeroMemory( ptrayItem, sizeof(SystrayItem) );
200 /* Create tray window for icon. */
201 ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
202 "WineSystray", "Wine-Systray",
204 CW_USEDEFAULT, CW_USEDEFAULT,
205 rect.right-rect.left, rect.bottom-rect.top,
207 if ( !ptrayItem->hWnd ) {
208 ERR( "CreateWindow(WineSystray) failed\n" );
212 /* Create tooltip for icon. */
213 ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
214 CW_USEDEFAULT, CW_USEDEFAULT,
215 CW_USEDEFAULT, CW_USEDEFAULT,
216 ptrayItem->hWnd, 0, 0, 0 );
217 if ( !ptrayItem->hWndToolTip ) {
218 ERR( "CreateWindow(TOOLTIP) failed\n" );
225 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
227 if(ptrayItem->notifyIcon.hIcon)
228 DestroyIcon(ptrayItem->notifyIcon.hIcon);
229 if(ptrayItem->hWndToolTip)
230 DestroyWindow(ptrayItem->hWndToolTip);
232 DestroyWindow(ptrayItem->hWnd);
237 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
239 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
243 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
245 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
246 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
250 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
254 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
255 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
257 ti.cbSize = sizeof(TTTOOLINFOA);
259 ti.hwnd = ptrayItem->hWnd;
262 ti.lpszText = ptrayItem->notifyIcon.szTip;
265 ti.rect.right = ICON_SIZE+2*ICON_BORDER;
266 ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
269 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
271 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
275 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
277 SystrayItem **ptrayItem = &systray;
279 /* Find last element. */
280 while( *ptrayItem ) {
281 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
283 ptrayItem = &((*ptrayItem)->nextTrayItem);
285 /* Allocate SystrayItem for element and add to end of list. */
286 (*ptrayItem) = ( SystrayItem *)malloc( sizeof(SystrayItem) );
288 /* Initialize and set data for the tray element. */
289 SYSTRAY_ItemInit( (*ptrayItem) );
290 (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
291 (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
292 SYSTRAY_ItemSetIcon (*ptrayItem, (pnid->uFlags&NIF_ICON) ?pnid->hIcon :0);
293 SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
294 SYSTRAY_ItemSetTip (*ptrayItem, (pnid->uFlags&NIF_TIP) ?pnid->szTip :"", FALSE);
296 TRACE("%p: %p %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
297 (*ptrayItem)->notifyIcon.szTip);
302 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
304 SystrayItem *ptrayItem = systray;
306 while ( ptrayItem ) {
307 if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
308 if (pnid->uFlags & NIF_ICON)
309 SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
310 if (pnid->uFlags & NIF_MESSAGE)
311 SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
312 if (pnid->uFlags & NIF_TIP)
313 SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
315 TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
318 ptrayItem = ptrayItem->nextTrayItem;
320 return FALSE; /* not found */
324 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
326 SystrayItem **ptrayItem = &systray;
329 if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
330 SystrayItem *next = (*ptrayItem)->nextTrayItem;
331 TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
332 SYSTRAY_ItemTerm(*ptrayItem);
339 ptrayItem = &((*ptrayItem)->nextTrayItem);
342 return FALSE; /* not found */
345 /*************************************************************************
348 BOOL SYSTRAY_Init(void)
353 /*************************************************************************
354 * Shell_NotifyIcon [SHELL32.296]
355 * Shell_NotifyIconA [SHELL32.297]
357 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
360 TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
363 flag = SYSTRAY_Add(pnid);
366 flag = SYSTRAY_Modify(pnid);
369 flag = SYSTRAY_Delete(pnid);
372 TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
376 /*************************************************************************
377 * Shell_NotifyIconW [SHELL32.298]
379 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
383 PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
384 memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
385 WideCharToMultiByte( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip), NULL, NULL );
386 p->szTip[sizeof(p->szTip)-1] = 0;
388 ret = Shell_NotifyIconA(dwMessage, p );
390 HeapFree(GetProcessHeap(),0,p);