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
34 #include "shell32_main.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40 typedef struct SystrayItem {
43 NOTIFYICONDATAA notifyIcon;
44 struct SystrayItem *nextTrayItem;
47 static SystrayItem *systray=NULL;
48 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
50 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
53 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
54 /* space around icon (forces icon to center of KDE systray area) */
59 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
61 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
62 if (pnid1->uID != pnid2->uID) return FALSE;
66 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
75 SystrayItem *ptrayItem = systray;
78 if (ptrayItem->hWnd==hWnd) {
79 if (ptrayItem->notifyIcon.hIcon) {
80 hdc = BeginPaint(hWnd, &ps);
81 GetClientRect(hWnd, &rc);
82 if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
83 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
84 ERR("Paint(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
85 SYSTRAY_Delete(&ptrayItem->notifyIcon);
90 ptrayItem = ptrayItem->nextTrayItem;
105 SystrayItem *ptrayItem = systray;
107 while ( ptrayItem ) {
108 if (ptrayItem->hWnd == hWnd) {
113 msg.time = GetMessageTime ();
114 msg.pt.x = LOWORD(GetMessagePos ());
115 msg.pt.y = HIWORD(GetMessagePos ());
117 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
119 ptrayItem = ptrayItem->nextTrayItem;
124 case WM_LBUTTONDBLCLK:
125 case WM_RBUTTONDBLCLK:
126 case WM_MBUTTONDBLCLK:
128 SystrayItem *ptrayItem = systray;
131 if (ptrayItem->hWnd == hWnd) {
132 if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
133 if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
134 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
135 ERR("PostMessage(SystrayWindow 0x%08x) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
136 SYSTRAY_Delete(&ptrayItem->notifyIcon);
141 ptrayItem = ptrayItem->nextTrayItem;
147 return (DefWindowProcA(hWnd, message, wParam, lParam));
154 BOOL SYSTRAY_RegisterClass(void)
158 wc.style = CS_SAVEBITS;
159 wc.lpfnWndProc = (WNDPROC)SYSTRAY_WndProc;
164 wc.hCursor = LoadCursorA(0, IDC_ARROWA);
165 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
166 wc.lpszMenuName = NULL;
167 wc.lpszClassName = "WineSystray";
169 if (!RegisterClassA(&wc)) {
170 ERR("RegisterClass(WineSystray) failed\n");
177 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
181 /* Register the class if this is our first tray item. */
182 if ( firstSystray ) {
183 firstSystray = FALSE;
184 if ( !SYSTRAY_RegisterClass() ) {
185 ERR( "RegisterClass(WineSystray) failed\n" );
190 /* Initialize the window size. */
193 rect.right = ICON_SIZE+2*ICON_BORDER;
194 rect.bottom = ICON_SIZE+2*ICON_BORDER;
196 ZeroMemory( ptrayItem, sizeof(SystrayItem) );
197 /* Create tray window for icon. */
198 ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
199 "WineSystray", "Wine-Systray",
201 CW_USEDEFAULT, CW_USEDEFAULT,
202 rect.right-rect.left, rect.bottom-rect.top,
204 if ( !ptrayItem->hWnd ) {
205 ERR( "CreateWindow(WineSystray) failed\n" );
209 /* Create tooltip for icon. */
210 ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
211 CW_USEDEFAULT, CW_USEDEFAULT,
212 CW_USEDEFAULT, CW_USEDEFAULT,
213 ptrayItem->hWnd, 0, 0, 0 );
214 if ( !ptrayItem->hWndToolTip ) {
215 ERR( "CreateWindow(TOOLTIP) failed\n" );
222 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
224 if(ptrayItem->notifyIcon.hIcon)
225 DestroyIcon(ptrayItem->notifyIcon.hIcon);
226 if(ptrayItem->hWndToolTip)
227 DestroyWindow(ptrayItem->hWndToolTip);
229 DestroyWindow(ptrayItem->hWnd);
234 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
236 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
240 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
242 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
243 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
247 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
251 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
252 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
254 ti.cbSize = sizeof(TTTOOLINFOA);
256 ti.hwnd = ptrayItem->hWnd;
259 ti.lpszText = ptrayItem->notifyIcon.szTip;
262 ti.rect.right = ICON_SIZE+2*ICON_BORDER;
263 ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
266 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
268 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
272 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
274 SystrayItem **ptrayItem = &systray;
276 /* Find last element. */
277 while( *ptrayItem ) {
278 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
280 ptrayItem = &((*ptrayItem)->nextTrayItem);
282 /* Allocate SystrayItem for element and add to end of list. */
283 (*ptrayItem) = ( SystrayItem *)malloc( sizeof(SystrayItem) );
285 /* Initialize and set data for the tray element. */
286 SYSTRAY_ItemInit( (*ptrayItem) );
287 (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
288 (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
289 SYSTRAY_ItemSetIcon (*ptrayItem, (pnid->uFlags&NIF_ICON) ?pnid->hIcon :0);
290 SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
291 SYSTRAY_ItemSetTip (*ptrayItem, (pnid->uFlags&NIF_TIP) ?pnid->szTip :"", FALSE);
293 TRACE("%p: 0x%08x %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
294 (*ptrayItem)->notifyIcon.szTip);
299 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
301 SystrayItem *ptrayItem = systray;
303 while ( ptrayItem ) {
304 if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
305 if (pnid->uFlags & NIF_ICON)
306 SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
307 if (pnid->uFlags & NIF_MESSAGE)
308 SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
309 if (pnid->uFlags & NIF_TIP)
310 SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
312 TRACE("%p: 0x%08x %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
315 ptrayItem = ptrayItem->nextTrayItem;
317 return FALSE; /* not found */
321 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
323 SystrayItem **ptrayItem = &systray;
326 if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
327 SystrayItem *next = (*ptrayItem)->nextTrayItem;
328 TRACE("%p: 0x%08x %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
329 SYSTRAY_ItemTerm(*ptrayItem);
336 ptrayItem = &((*ptrayItem)->nextTrayItem);
339 return FALSE; /* not found */
342 /*************************************************************************
345 BOOL SYSTRAY_Init(void)
350 /*************************************************************************
351 * Shell_NotifyIcon [SHELL32.296]
352 * Shell_NotifyIconA [SHELL32.297]
354 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
357 TRACE("enter %d %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
360 flag = SYSTRAY_Add(pnid);
363 flag = SYSTRAY_Modify(pnid);
366 flag = SYSTRAY_Delete(pnid);
369 TRACE("leave %d %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
373 /*************************************************************************
374 * Shell_NotifyIconW [SHELL32.298]
376 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
380 PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
381 memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
382 WideCharToMultiByte( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip), NULL, NULL );
383 p->szTip[sizeof(p->szTip)-1] = 0;
385 ret = Shell_NotifyIconA(dwMessage, p );
387 HeapFree(GetProcessHeap(),0,p);