shell: Convert the filesystem shell folder to Unicode.
[wine/multimedia.git] / dlls / shell32 / systray.c
blob8dd241f8190908d19360f9cc76fd0aa609eaacb1
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 <string.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winnls.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "shlobj.h"
39 #include "shellapi.h"
40 #include "shell32_main.h"
41 #include "commctrl.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
46 typedef struct SystrayItem {
47 HWND hWnd;
48 HWND hWndToolTip;
49 NOTIFYICONDATAW notifyIcon;
50 struct SystrayItem *nextTrayItem;
51 } SystrayItem;
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) */
59 #define ICON_BORDER 4
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;
67 return TRUE;
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);
77 if(ptrayItem->hWnd)
78 DestroyWindow(ptrayItem->hWnd);
79 return;
83 static BOOL SYSTRAY_Delete(PNOTIFYICONDATAW pnid)
85 SystrayItem **ptrayItem = &systray;
87 while (*ptrayItem) {
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);
94 *ptrayItem = next;
96 return TRUE;
98 ptrayItem = &((*ptrayItem)->nextTrayItem);
101 return FALSE; /* not found */
104 static LRESULT CALLBACK SYSTRAY_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
106 HDC hdc;
107 PAINTSTRUCT ps;
109 switch (message) {
110 case WM_PAINT:
112 RECT rc;
113 SystrayItem *ptrayItem = systray;
115 while (ptrayItem) {
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);
126 break;
128 ptrayItem = ptrayItem->nextTrayItem;
130 EndPaint(hWnd, &ps);
132 break;
134 case WM_MOUSEMOVE:
135 case WM_LBUTTONDOWN:
136 case WM_LBUTTONUP:
137 case WM_RBUTTONDOWN:
138 case WM_RBUTTONUP:
139 case WM_MBUTTONDOWN:
140 case WM_MBUTTONUP:
142 MSG msg;
143 SystrayItem *ptrayItem = systray;
145 while ( ptrayItem ) {
146 if (ptrayItem->hWnd == hWnd) {
147 msg.hwnd=hWnd;
148 msg.message=message;
149 msg.wParam=wParam;
150 msg.lParam=lParam;
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;
160 /* fall through */
162 case WM_LBUTTONDBLCLK:
163 case WM_RBUTTONDBLCLK:
164 case WM_MBUTTONDBLCLK:
166 SystrayItem *ptrayItem = systray;
168 while (ptrayItem) {
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);
177 break;
179 ptrayItem = ptrayItem->nextTrayItem;
182 break;
184 default:
185 return (DefWindowProcW(hWnd, message, wParam, lParam));
187 return (0);
192 static BOOL SYSTRAY_RegisterClass(void)
194 WNDCLASSW wc;
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;
199 wc.cbClsExtra = 0;
200 wc.cbWndExtra = 0;
201 wc.hInstance = 0;
202 wc.hIcon = 0;
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");
210 return FALSE;
212 return TRUE;
216 static BOOL SYSTRAY_ItemInit(SystrayItem *ptrayItem)
218 RECT rect;
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" );
227 return FALSE;
231 /* Initialize the window size. */
232 rect.left = 0;
233 rect.top = 0;
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,
241 WS_VISIBLE,
242 CW_USEDEFAULT, CW_USEDEFAULT,
243 rect.right-rect.left, rect.bottom-rect.top,
244 0, 0, 0, 0 );
245 if ( !ptrayItem->hWnd ) {
246 ERR( "CreateWindow(WineSystray) failed\n" );
247 return FALSE;
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" );
257 return FALSE;
259 return TRUE;
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)
280 TTTOOLINFOW ti;
282 lstrcpynW(ptrayItem->notifyIcon.szTip, szTip, sizeof(ptrayItem->notifyIcon.szTip)/sizeof(WCHAR));
284 ti.cbSize = sizeof(TTTOOLINFOW);
285 ti.uFlags = 0;
286 ti.hwnd = ptrayItem->hWnd;
287 ti.hinst = 0;
288 ti.uId = 0;
289 ti.lpszText = ptrayItem->notifyIcon.szTip;
290 ti.rect.left = 0;
291 ti.rect.top = 0;
292 ti.rect.right = ICON_SIZE+2*ICON_BORDER;
293 ti.rect.bottom = ICON_SIZE+2*ICON_BORDER;
295 if(modify)
296 SendMessageW(ptrayItem->hWndToolTip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
297 else
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) )
310 return FALSE;
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));
326 return TRUE;
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));
344 return TRUE;
346 ptrayItem = ptrayItem->nextTrayItem;
348 return FALSE; /* not found */
352 /*************************************************************************
355 BOOL SYSTRAY_Init(void)
357 return TRUE;
360 /*************************************************************************
361 * Shell_NotifyIconW [SHELL32.298]
363 BOOL WINAPI Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW 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_NotifyIconA [SHELL32.297]
384 * Shell_NotifyIcon [SHELL32.296]
386 BOOL WINAPI Shell_NotifyIconA (DWORD dwMessage, PNOTIFYICONDATAA pnid )
388 BOOL ret;
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);
398 return ret;