Do not create symlinks for the PE build since they can conflict with
[wine.git] / dlls / shell32 / systray.c
blob55d0403883e435e4115babee2d0ab3800b0468c9
1 /*
2 * Systray
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
25 #include "config.h"
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "shlobj.h"
40 #include "shellapi.h"
41 #include "shell32_main.h"
42 #include "commctrl.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(shell);
47 typedef struct SystrayItem {
48 HWND hWnd;
49 HWND hWndToolTip;
50 NOTIFYICONDATAA notifyIcon;
51 struct SystrayItem *nextTrayItem;
52 } SystrayItem;
54 static SystrayItem *systray=NULL;
55 static int firstSystray=TRUE; /* defer creation of window class until first systray item is created */
57 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid);
60 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
61 /* space around icon (forces icon to center of KDE systray area) */
62 #define ICON_BORDER 4
66 static BOOL SYSTRAY_ItemIsEqual(PNOTIFYICONDATAA pnid1, PNOTIFYICONDATAA pnid2)
68 if (pnid1->hWnd != pnid2->hWnd) return FALSE;
69 if (pnid1->uID != pnid2->uID) return FALSE;
70 return TRUE;
73 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
75 HDC hdc;
76 PAINTSTRUCT ps;
78 switch (message) {
79 case WM_PAINT:
81 RECT rc;
82 SystrayItem *ptrayItem = systray;
84 while (ptrayItem) {
85 if (ptrayItem->hWnd==hWnd) {
86 if (ptrayItem->notifyIcon.hIcon) {
87 hdc = BeginPaint(hWnd, &ps);
88 GetClientRect(hWnd, &rc);
89 if (!DrawIconEx(hdc, rc.left+ICON_BORDER, rc.top+ICON_BORDER, ptrayItem->notifyIcon.hIcon,
90 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL)) {
91 ERR("Paint(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
92 SYSTRAY_Delete(&ptrayItem->notifyIcon);
95 break;
97 ptrayItem = ptrayItem->nextTrayItem;
99 EndPaint(hWnd, &ps);
101 break;
103 case WM_MOUSEMOVE:
104 case WM_LBUTTONDOWN:
105 case WM_LBUTTONUP:
106 case WM_RBUTTONDOWN:
107 case WM_RBUTTONUP:
108 case WM_MBUTTONDOWN:
109 case WM_MBUTTONUP:
111 MSG msg;
112 SystrayItem *ptrayItem = systray;
114 while ( ptrayItem ) {
115 if (ptrayItem->hWnd == hWnd) {
116 msg.hwnd=hWnd;
117 msg.message=message;
118 msg.wParam=wParam;
119 msg.lParam=lParam;
120 msg.time = GetMessageTime ();
121 msg.pt.x = LOWORD(GetMessagePos ());
122 msg.pt.y = HIWORD(GetMessagePos ());
124 SendMessageA(ptrayItem->hWndToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg);
126 ptrayItem = ptrayItem->nextTrayItem;
129 /* fall through */
131 case WM_LBUTTONDBLCLK:
132 case WM_RBUTTONDBLCLK:
133 case WM_MBUTTONDBLCLK:
135 SystrayItem *ptrayItem = systray;
137 while (ptrayItem) {
138 if (ptrayItem->hWnd == hWnd) {
139 if (ptrayItem->notifyIcon.hWnd && ptrayItem->notifyIcon.uCallbackMessage) {
140 if (!PostMessageA(ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.uCallbackMessage,
141 (WPARAM)ptrayItem->notifyIcon.uID, (LPARAM)message)) {
142 ERR("PostMessage(SystrayWindow %p) failed -> removing SystrayItem %p\n", hWnd, ptrayItem);
143 SYSTRAY_Delete(&ptrayItem->notifyIcon);
146 break;
148 ptrayItem = ptrayItem->nextTrayItem;
151 break;
153 default:
154 return (DefWindowProcA(hWnd, message, wParam, lParam));
156 return (0);
161 BOOL SYSTRAY_RegisterClass(void)
163 WNDCLASSA wc;
165 wc.style = CS_SAVEBITS|CS_DBLCLKS;
166 wc.lpfnWndProc = (WNDPROC)SYSTRAY_WndProc;
167 wc.cbClsExtra = 0;
168 wc.cbWndExtra = 0;
169 wc.hInstance = 0;
170 wc.hIcon = 0;
171 wc.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
172 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
173 wc.lpszMenuName = NULL;
174 wc.lpszClassName = "WineSystray";
176 if (!RegisterClassA(&wc)) {
177 ERR("RegisterClass(WineSystray) failed\n");
178 return FALSE;
180 return TRUE;
184 BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
186 RECT rect;
188 /* Register the class if this is our first tray item. */
189 if ( firstSystray ) {
190 firstSystray = FALSE;
191 if ( !SYSTRAY_RegisterClass() ) {
192 ERR( "RegisterClass(WineSystray) failed\n" );
193 return FALSE;
197 /* Initialize the window size. */
198 rect.left = 0;
199 rect.top = 0;
200 rect.right = ICON_SIZE+2*ICON_BORDER;
201 rect.bottom = ICON_SIZE+2*ICON_BORDER;
203 ZeroMemory( ptrayItem, sizeof(SystrayItem) );
204 /* Create tray window for icon. */
205 ptrayItem->hWnd = CreateWindowExA( WS_EX_TRAYWINDOW,
206 "WineSystray", "Wine-Systray",
207 WS_VISIBLE,
208 CW_USEDEFAULT, CW_USEDEFAULT,
209 rect.right-rect.left, rect.bottom-rect.top,
210 0, 0, 0, 0 );
211 if ( !ptrayItem->hWnd ) {
212 ERR( "CreateWindow(WineSystray) failed\n" );
213 return FALSE;
216 /* Create tooltip for icon. */
217 ptrayItem->hWndToolTip = CreateWindowA( TOOLTIPS_CLASSA,NULL,TTS_ALWAYSTIP,
218 CW_USEDEFAULT, CW_USEDEFAULT,
219 CW_USEDEFAULT, CW_USEDEFAULT,
220 ptrayItem->hWnd, 0, 0, 0 );
221 if ( !ptrayItem->hWndToolTip ) {
222 ERR( "CreateWindow(TOOLTIP) failed\n" );
223 return FALSE;
225 return TRUE;
229 static void SYSTRAY_ItemTerm(SystrayItem *ptrayItem)
231 if(ptrayItem->notifyIcon.hIcon)
232 DestroyIcon(ptrayItem->notifyIcon.hIcon);
233 if(ptrayItem->hWndToolTip)
234 DestroyWindow(ptrayItem->hWndToolTip);
235 if(ptrayItem->hWnd)
236 DestroyWindow(ptrayItem->hWnd);
237 return;
241 void SYSTRAY_ItemSetMessage(SystrayItem *ptrayItem, UINT uCallbackMessage)
243 ptrayItem->notifyIcon.uCallbackMessage = uCallbackMessage;
247 void SYSTRAY_ItemSetIcon(SystrayItem *ptrayItem, HICON hIcon)
249 if(ptrayItem->notifyIcon.hIcon)
250 DestroyIcon(ptrayItem->notifyIcon.hIcon);
251 ptrayItem->notifyIcon.hIcon = CopyIcon(hIcon);
252 InvalidateRect(ptrayItem->hWnd, NULL, TRUE);
256 void SYSTRAY_ItemSetTip(SystrayItem *ptrayItem, CHAR* szTip, int modify)
258 TTTOOLINFOA ti;
260 strncpy(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip));
261 ptrayItem->notifyIcon.szTip[sizeof(ptrayItem->notifyIcon.szTip)-1]=0;
263 ti.cbSize = sizeof(TTTOOLINFOA);
264 ti.uFlags = 0;
265 ti.hwnd = ptrayItem->hWnd;
266 ti.hinst = 0;
267 ti.uId = 0;
268 ti.lpszText = ptrayItem->notifyIcon.szTip;
269 ti.rect.left = 0;
270 ti.rect.top = 0;
271 ti.rect.right = ICON_SIZE+2*ICON_BORDER;
272 ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
274 if(modify)
275 SendMessageA(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTA, 0, (LPARAM)&ti);
276 else
277 SendMessageA(ptrayItem->hWndToolTip, TTM_ADDTOOLA, 0, (LPARAM)&ti);
281 static BOOL SYSTRAY_Add(PNOTIFYICONDATAA pnid)
283 SystrayItem **ptrayItem = &systray;
285 /* Find last element. */
286 while( *ptrayItem ) {
287 if ( SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon) )
288 return FALSE;
289 ptrayItem = &((*ptrayItem)->nextTrayItem);
291 /* Allocate SystrayItem for element and add to end of list. */
292 (*ptrayItem) = ( SystrayItem *)malloc( sizeof(SystrayItem) );
294 /* Initialize and set data for the tray element. */
295 SYSTRAY_ItemInit( (*ptrayItem) );
296 (*ptrayItem)->notifyIcon.uID = pnid->uID; /* only needed for callback message */
297 (*ptrayItem)->notifyIcon.hWnd = pnid->hWnd; /* only needed for callback message */
298 SYSTRAY_ItemSetIcon (*ptrayItem, (pnid->uFlags&NIF_ICON) ?pnid->hIcon :0);
299 SYSTRAY_ItemSetMessage(*ptrayItem, (pnid->uFlags&NIF_MESSAGE)?pnid->uCallbackMessage:0);
300 SYSTRAY_ItemSetTip (*ptrayItem, (pnid->uFlags&NIF_TIP) ?pnid->szTip :"", FALSE);
302 TRACE("%p: %p %s\n", (*ptrayItem), (*ptrayItem)->notifyIcon.hWnd,
303 (*ptrayItem)->notifyIcon.szTip);
304 return TRUE;
308 static BOOL SYSTRAY_Modify(PNOTIFYICONDATAA pnid)
310 SystrayItem *ptrayItem = systray;
312 while ( ptrayItem ) {
313 if ( SYSTRAY_ItemIsEqual(pnid, &ptrayItem->notifyIcon) ) {
314 if (pnid->uFlags & NIF_ICON)
315 SYSTRAY_ItemSetIcon(ptrayItem, pnid->hIcon);
316 if (pnid->uFlags & NIF_MESSAGE)
317 SYSTRAY_ItemSetMessage(ptrayItem, pnid->uCallbackMessage);
318 if (pnid->uFlags & NIF_TIP)
319 SYSTRAY_ItemSetTip(ptrayItem, pnid->szTip, TRUE);
321 TRACE("%p: %p %s\n", ptrayItem, ptrayItem->notifyIcon.hWnd, ptrayItem->notifyIcon.szTip);
322 return TRUE;
324 ptrayItem = ptrayItem->nextTrayItem;
326 return FALSE; /* not found */
330 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAA pnid)
332 SystrayItem **ptrayItem = &systray;
334 while (*ptrayItem) {
335 if (SYSTRAY_ItemIsEqual(pnid, &(*ptrayItem)->notifyIcon)) {
336 SystrayItem *next = (*ptrayItem)->nextTrayItem;
337 TRACE("%p: %p %s\n", *ptrayItem, (*ptrayItem)->notifyIcon.hWnd, (*ptrayItem)->notifyIcon.szTip);
338 SYSTRAY_ItemTerm(*ptrayItem);
340 free(*ptrayItem);
341 *ptrayItem = next;
343 return TRUE;
345 ptrayItem = &((*ptrayItem)->nextTrayItem);
348 return FALSE; /* not found */
351 /*************************************************************************
354 BOOL SYSTRAY_Init(void)
356 return TRUE;
359 /*************************************************************************
360 * Shell_NotifyIcon [SHELL32.296]
361 * Shell_NotifyIconA [SHELL32.297]
363 BOOL WINAPI Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA pnid )
365 BOOL flag=FALSE;
366 TRACE("enter %p %d %ld\n", pnid->hWnd, pnid->uID, dwMessage);
367 switch(dwMessage) {
368 case NIM_ADD:
369 flag = SYSTRAY_Add(pnid);
370 break;
371 case NIM_MODIFY:
372 flag = SYSTRAY_Modify(pnid);
373 break;
374 case NIM_DELETE:
375 flag = SYSTRAY_Delete(pnid);
376 break;
378 TRACE("leave %p %d %ld=%d\n", pnid->hWnd, pnid->uID, dwMessage, flag);
379 return flag;
382 /*************************************************************************
383 * Shell_NotifyIconW [SHELL32.298]
385 BOOL WINAPI Shell_NotifyIconW (DWORD dwMessage, PNOTIFYICONDATAW pnid )
387 BOOL ret;
389 PNOTIFYICONDATAA p = HeapAlloc(GetProcessHeap(),0,sizeof(NOTIFYICONDATAA));
390 memcpy(p, pnid, sizeof(NOTIFYICONDATAA));
391 WideCharToMultiByte( CP_ACP, 0, pnid->szTip, -1, p->szTip, sizeof(p->szTip), NULL, NULL );
392 p->szTip[sizeof(p->szTip)-1] = 0;
394 ret = Shell_NotifyIconA(dwMessage, p );
396 HeapFree(GetProcessHeap(),0,p);
397 return ret;