wer: Add new stubbed wer.dll.
[wine/hramrach.git] / programs / explorer / systray.c
blob615768f077fc537ce1a89c6ceef806d31f2a8840
1 /*
2 * Copyright (C) 2004 Mike Hearn, for CodeWeavers
3 * Copyright (C) 2005 Robert Shearman
4 * Copyright (C) 2008 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
23 #define NONAMELESSUNION
24 #define _WIN32_IE 0x500
25 #include <windows.h>
26 #include <commctrl.h>
28 #include <wine/debug.h>
29 #include <wine/list.h>
31 #include "explorer_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(systray);
35 #define IS_OPTION_FALSE(ch) \
36 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
38 struct notify_data /* platform-independent format for NOTIFYICONDATA */
40 LONG hWnd;
41 UINT uID;
42 UINT uFlags;
43 UINT uCallbackMessage;
44 WCHAR szTip[128];
45 DWORD dwState;
46 DWORD dwStateMask;
47 WCHAR szInfo[256];
48 union {
49 UINT uTimeout;
50 UINT uVersion;
51 } u;
52 WCHAR szInfoTitle[64];
53 DWORD dwInfoFlags;
54 GUID guidItem;
55 /* data for the icon bitmap */
56 UINT width;
57 UINT height;
58 UINT planes;
59 UINT bpp;
62 static int (CDECL *wine_notify_icon)(DWORD,NOTIFYICONDATAW *);
64 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
65 struct icon
67 struct list entry;
68 HICON image; /* the image to render */
69 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
70 HWND tooltip; /* Icon tooltip */
71 UINT id; /* the unique id given by the app */
72 UINT callback_message;
73 int display; /* index in display list, or -1 if hidden */
74 WCHAR tiptext[128]; /* Tooltip text. If empty => tooltip disabled */
77 static struct list icon_list = LIST_INIT( icon_list );
78 static HWND tray_window;
80 static unsigned int alloc_displayed;
81 static unsigned int nb_displayed;
82 static struct icon **displayed; /* array of currently displayed icons */
84 static BOOL hide_systray;
85 static int icon_cx, icon_cy;
87 #define MIN_DISPLAYED 8
88 #define ICON_BORDER 2
90 /* Retrieves icon record by owner window and ID */
91 static struct icon *get_icon(HWND owner, UINT id)
93 struct icon *this;
95 /* search for the icon */
96 LIST_FOR_EACH_ENTRY( this, &icon_list, struct icon, entry )
97 if ((this->id == id) && (this->owner == owner)) return this;
99 return NULL;
102 /* compute the size of the tray window */
103 static SIZE get_window_size(void)
105 SIZE size;
106 RECT rect;
108 rect.left = 0;
109 rect.top = 0;
110 rect.right = icon_cx * max( nb_displayed, MIN_DISPLAYED );
111 rect.bottom = icon_cy;
112 AdjustWindowRect( &rect, WS_CAPTION, FALSE );
113 size.cx = rect.right - rect.left;
114 size.cy = rect.bottom - rect.top;
115 return size;
118 /* Creates tooltip window for icon. */
119 static void create_tooltip(struct icon *icon)
121 TTTOOLINFOW ti;
122 static BOOL tooltips_initialized = FALSE;
124 /* Register tooltip classes if this is the first icon */
125 if (!tooltips_initialized)
127 INITCOMMONCONTROLSEX init_tooltip;
129 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
130 init_tooltip.dwICC = ICC_TAB_CLASSES;
132 InitCommonControlsEx(&init_tooltip);
133 tooltips_initialized = TRUE;
136 icon->tooltip = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
137 WS_POPUP | TTS_ALWAYSTIP,
138 CW_USEDEFAULT, CW_USEDEFAULT,
139 CW_USEDEFAULT, CW_USEDEFAULT,
140 tray_window, NULL, NULL, NULL);
142 ZeroMemory(&ti, sizeof(ti));
143 ti.cbSize = sizeof(TTTOOLINFOW);
144 ti.hwnd = tray_window;
145 ti.lpszText = icon->tiptext;
146 if (icon->display != -1)
148 ti.rect.left = icon_cx * icon->display;
149 ti.rect.right = icon_cx * (icon->display + 1);
150 ti.rect.top = 0;
151 ti.rect.bottom = icon_cy;
153 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
156 /* Synchronize tooltip text with tooltip window */
157 static void update_tooltip_text(struct icon *icon)
159 TTTOOLINFOW ti;
161 ZeroMemory(&ti, sizeof(ti));
162 ti.cbSize = sizeof(TTTOOLINFOW);
163 ti.hwnd = tray_window;
164 ti.lpszText = icon->tiptext;
166 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
169 /* synchronize tooltip position with tooltip window */
170 static void update_tooltip_position( struct icon *icon )
172 TTTOOLINFOW ti;
174 ZeroMemory(&ti, sizeof(ti));
175 ti.cbSize = sizeof(TTTOOLINFOW);
176 ti.hwnd = tray_window;
177 if (icon->display != -1)
179 ti.rect.left = icon_cx * icon->display;
180 ti.rect.right = icon_cx * (icon->display + 1);
181 ti.rect.top = 0;
182 ti.rect.bottom = icon_cy;
184 SendMessageW( icon->tooltip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti );
187 /* find the icon located at a certain point in the tray window */
188 static struct icon *icon_from_point( int x, int y )
190 if (y < 0 || y >= icon_cy) return NULL;
191 if (x < 0 || x >= icon_cx * nb_displayed) return NULL;
192 return displayed[x / icon_cx];
195 /* invalidate the portion of the tray window that contains the specified icons */
196 static void invalidate_icons( unsigned int start, unsigned int end )
198 RECT rect;
200 rect.left = start * icon_cx;
201 rect.top = 0;
202 rect.right = (end + 1) * icon_cx;
203 rect.bottom = icon_cy;
204 InvalidateRect( tray_window, &rect, TRUE );
207 /* make an icon visible */
208 static BOOL show_icon(struct icon *icon)
210 WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
212 if (icon->display != -1) return TRUE; /* already displayed */
214 if (nb_displayed >= alloc_displayed)
216 unsigned int new_count = max( alloc_displayed * 2, 32 );
217 struct icon **ptr;
218 if (displayed) ptr = HeapReAlloc( GetProcessHeap(), 0, displayed, new_count * sizeof(*ptr) );
219 else ptr = HeapAlloc( GetProcessHeap(), 0, new_count * sizeof(*ptr) );
220 if (!ptr) return FALSE;
221 displayed = ptr;
222 alloc_displayed = new_count;
225 icon->display = nb_displayed;
226 displayed[nb_displayed++] = icon;
227 update_tooltip_position( icon );
228 invalidate_icons( nb_displayed-1, nb_displayed-1 );
230 if (nb_displayed > MIN_DISPLAYED)
232 SIZE size = get_window_size();
233 SetWindowPos( tray_window, 0, 0, 0, size.cx, size.cy, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE );
235 else if (nb_displayed == 1)
237 if (!hide_systray) ShowWindow( tray_window, SW_SHOWNA );
240 create_tooltip(icon);
241 return TRUE;
244 /* make an icon invisible */
245 static BOOL hide_icon(struct icon *icon)
247 unsigned int i;
249 WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
251 if (icon->display == -1) return TRUE; /* already hidden */
253 assert( nb_displayed );
254 for (i = icon->display; i < nb_displayed - 1; i++)
256 displayed[i] = displayed[i + 1];
257 displayed[i]->display = i;
258 update_tooltip_position( displayed[i] );
260 nb_displayed--;
261 invalidate_icons( icon->display, nb_displayed );
262 icon->display = -1;
264 if (nb_displayed >= MIN_DISPLAYED)
266 SIZE size = get_window_size();
267 SetWindowPos( tray_window, 0, 0, 0, size.cx, size.cy, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE );
269 else if (!nb_displayed)
271 ShowWindow( tray_window, SW_HIDE );
274 update_tooltip_position( icon );
275 return TRUE;
278 /* Modifies an existing icon record */
279 static BOOL modify_icon( struct icon *icon, NOTIFYICONDATAW *nid )
281 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
283 /* demarshal the request from the NID */
284 if (!icon)
286 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
287 return FALSE;
290 if ((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))
292 if (nid->dwState & NIS_HIDDEN) hide_icon( icon );
293 else show_icon( icon );
296 if (nid->uFlags & NIF_ICON)
298 if (icon->image) DestroyIcon(icon->image);
299 icon->image = CopyIcon(nid->hIcon);
300 if (icon->display != -1) invalidate_icons( icon->display, icon->display );
303 if (nid->uFlags & NIF_MESSAGE)
305 icon->callback_message = nid->uCallbackMessage;
307 if (nid->uFlags & NIF_TIP)
309 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
310 if (icon->display != -1) update_tooltip_text(icon);
312 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
314 WINE_FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid->szInfoTitle), wine_dbgstr_w(nid->szInfo));
316 return TRUE;
319 /* Adds a new icon record to the list */
320 static BOOL add_icon(NOTIFYICONDATAW *nid)
322 struct icon *icon;
324 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
326 if ((icon = get_icon(nid->hWnd, nid->uID)))
328 WINE_WARN("duplicate tray icon add, buggy app?\n");
329 return FALSE;
332 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
334 WINE_ERR("out of memory\n");
335 return FALSE;
338 ZeroMemory(icon, sizeof(struct icon));
339 icon->id = nid->uID;
340 icon->owner = nid->hWnd;
341 icon->display = -1;
343 if (list_empty( &icon_list )) SetTimer( tray_window, 1, 2000, NULL );
344 list_add_tail(&icon_list, &icon->entry);
346 modify_icon( icon, nid );
347 /* show icon, unless hidden state was explicitly specified */
348 if (!((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))) show_icon( icon );
349 return TRUE;
352 /* Deletes tray icon window and icon record */
353 static BOOL delete_icon(struct icon *icon)
355 hide_icon(icon);
356 list_remove(&icon->entry);
357 DestroyIcon(icon->image);
358 HeapFree(GetProcessHeap(), 0, icon);
359 if (list_empty( &icon_list )) KillTimer( tray_window, 1 );
360 return TRUE;
363 /* cleanup icons belonging to windows that have been destroyed */
364 static void cleanup_destroyed_windows(void)
366 struct icon *icon, *next;
368 LIST_FOR_EACH_ENTRY_SAFE( icon, next, &icon_list, struct icon, entry )
369 if (!IsWindow( icon->owner )) delete_icon( icon );
372 static BOOL handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
374 struct icon *icon = NULL;
375 const struct notify_data *data;
376 NOTIFYICONDATAW nid;
377 int ret = FALSE;
379 if (cds->cbData < sizeof(*data)) return FALSE;
380 data = cds->lpData;
382 nid.cbSize = sizeof(nid);
383 nid.hWnd = LongToHandle( data->hWnd );
384 nid.uID = data->uID;
385 nid.uFlags = data->uFlags;
386 nid.uCallbackMessage = data->uCallbackMessage;
387 nid.hIcon = 0;
388 nid.dwState = data->dwState;
389 nid.dwStateMask = data->dwStateMask;
390 nid.u.uTimeout = data->u.uTimeout;
391 nid.dwInfoFlags = data->dwInfoFlags;
392 nid.guidItem = data->guidItem;
393 lstrcpyW( nid.szTip, data->szTip );
394 lstrcpyW( nid.szInfo, data->szInfo );
395 lstrcpyW( nid.szInfoTitle, data->szInfoTitle );
396 nid.hBalloonIcon = 0;
398 /* FIXME: if statement only needed because we don't support interprocess
399 * icon handles */
400 if ((nid.uFlags & NIF_ICON) && cds->cbData > sizeof(*data))
402 LONG cbMaskBits;
403 LONG cbColourBits;
404 const char *buffer = (const char *)(data + 1);
406 cbMaskBits = (data->width * data->height + 15) / 16 * 2;
407 cbColourBits = (data->planes * data->width * data->height * data->bpp + 15) / 16 * 2;
409 if (cds->cbData < sizeof(*data) + cbMaskBits + cbColourBits)
411 WINE_ERR("buffer underflow\n");
412 return FALSE;
414 nid.hIcon = CreateIcon(NULL, data->width, data->height, data->planes, data->bpp,
415 buffer, buffer + cbMaskBits);
418 /* try forward to x11drv first */
419 if (cds->dwData == NIM_ADD || !(icon = get_icon( nid.hWnd, nid.uID )))
421 if (wine_notify_icon && ((ret = wine_notify_icon( cds->dwData, &nid )) != -1))
423 if (nid.hIcon) DestroyIcon( nid.hIcon );
424 return ret;
426 ret = FALSE;
429 switch (cds->dwData)
431 case NIM_ADD:
432 ret = add_icon(&nid);
433 break;
434 case NIM_DELETE:
435 if (icon) ret = delete_icon( icon );
436 break;
437 case NIM_MODIFY:
438 if (icon) ret = modify_icon( icon, &nid );
439 break;
440 default:
441 WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
442 break;
445 if (nid.hIcon) DestroyIcon( nid.hIcon );
446 return ret;
449 static void do_hide_systray(void)
451 SetWindowPos( tray_window, 0,
452 GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN),
453 GetSystemMetrics(SM_YVIRTUALSCREEN) + GetSystemMetrics(SM_CYVIRTUALSCREEN),
454 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
457 static LRESULT WINAPI tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
459 switch (msg)
461 case WM_COPYDATA:
462 return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
464 case WM_DISPLAYCHANGE:
465 if (hide_systray) do_hide_systray();
466 break;
468 case WM_TIMER:
469 cleanup_destroyed_windows();
470 break;
472 case WM_PAINT:
474 unsigned int i;
475 PAINTSTRUCT ps;
476 HDC hdc;
478 hdc = BeginPaint( hwnd, &ps );
479 for (i = ps.rcPaint.left / icon_cx;
480 (i < (ps.rcPaint.right + icon_cx - 1) / icon_cx) && (i < nb_displayed);
481 i++)
483 DrawIconEx( hdc, i * icon_cx + ICON_BORDER, ICON_BORDER, displayed[i]->image,
484 icon_cx - 2*ICON_BORDER, icon_cy - 2*ICON_BORDER,
485 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
487 EndPaint( hwnd, &ps );
488 break;
491 case WM_MOUSEMOVE:
492 case WM_LBUTTONDOWN:
493 case WM_LBUTTONUP:
494 case WM_RBUTTONDOWN:
495 case WM_RBUTTONUP:
496 case WM_MBUTTONDOWN:
497 case WM_MBUTTONUP:
498 case WM_LBUTTONDBLCLK:
499 case WM_RBUTTONDBLCLK:
500 case WM_MBUTTONDBLCLK:
502 MSG message;
503 struct icon *icon = icon_from_point( (short)LOWORD(lparam), (short)HIWORD(lparam) );
504 if (!icon) break;
506 /* notify the owner hwnd of the message */
507 WINE_TRACE("relaying 0x%x\n", msg);
509 message.hwnd = hwnd;
510 message.message = msg;
511 message.wParam = wparam;
512 message.lParam = lparam;
513 SendMessageW( icon->tooltip, TTM_RELAYEVENT, 0, (LPARAM)&message );
515 if (!PostMessageW( icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg ) &&
516 GetLastError() == ERROR_INVALID_WINDOW_HANDLE)
518 WINE_WARN("application window was destroyed without removing "
519 "notification icon, removing automatically\n");
520 delete_icon( icon );
522 break;
525 case WM_CLOSE:
526 /* don't destroy the tray window, just hide it */
527 ShowWindow( hwnd, SW_HIDE );
528 return 0;
530 default:
531 return DefWindowProcW( hwnd, msg, wparam, lparam );
533 return 0;
536 static BOOL is_systray_hidden(void)
538 const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
539 'X','1','1',' ','D','r','i','v','e','r',0};
540 const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
541 HKEY hkey;
542 BOOL ret = FALSE;
544 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
545 if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
547 WCHAR value[10];
548 DWORD type, size = sizeof(value);
549 if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
551 ret = IS_OPTION_FALSE(value[0]);
553 RegCloseKey(hkey);
555 return ret;
558 /* this function creates the listener window */
559 void initialize_systray(void)
561 HMODULE x11drv;
562 SIZE size;
563 WNDCLASSEXW class;
564 static const WCHAR classname[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
565 static const WCHAR winname[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
567 if ((x11drv = GetModuleHandleA( "winex11.drv" )))
568 wine_notify_icon = (void *)GetProcAddress( x11drv, "wine_notify_icon" );
570 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
571 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
572 hide_systray = is_systray_hidden();
574 /* register the systray listener window class */
575 ZeroMemory(&class, sizeof(class));
576 class.cbSize = sizeof(class);
577 class.style = CS_DBLCLKS;
578 class.lpfnWndProc = tray_wndproc;
579 class.hInstance = NULL;
580 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
581 class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
582 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
583 class.lpszClassName = (WCHAR *) &classname;
585 if (!RegisterClassExW(&class))
587 WINE_ERR("Could not register SysTray window class\n");
588 return;
591 size = get_window_size();
592 tray_window = CreateWindowW( classname, winname, WS_OVERLAPPED | WS_CAPTION,
593 CW_USEDEFAULT, CW_USEDEFAULT, size.cx, size.cy, 0, 0, 0, 0 );
594 if (!tray_window)
596 WINE_ERR("Could not create tray window\n");
597 return;
600 if (hide_systray) do_hide_systray();