shell32: Improve some FIXMEs.
[wine/multimedia.git] / programs / explorer / systray.c
blob9be95bb8d2c277e7c88c7853392a8ced766c80d6
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 UNICODE
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 static int (CDECL *wine_notify_icon)(DWORD,NOTIFYICONDATAW *);
40 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
41 struct icon
43 struct list entry;
44 HICON image; /* the image to render */
45 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
46 HWND tooltip; /* Icon tooltip */
47 UINT id; /* the unique id given by the app */
48 UINT callback_message;
49 int display; /* index in display list, or -1 if hidden */
50 WCHAR tiptext[128]; /* Tooltip text. If empty => tooltip disabled */
53 static struct list icon_list = LIST_INIT( icon_list );
54 static HWND tray_window;
56 static unsigned int alloc_displayed;
57 static unsigned int nb_displayed;
58 static struct icon **displayed; /* array of currently displayed icons */
60 static BOOL hide_systray;
61 static int icon_cx, icon_cy;
63 #define MIN_DISPLAYED 8
64 #define ICON_BORDER 2
66 /* Retrieves icon record by owner window and ID */
67 static struct icon *get_icon(HWND owner, UINT id)
69 struct icon *this;
71 /* search for the icon */
72 LIST_FOR_EACH_ENTRY( this, &icon_list, struct icon, entry )
73 if ((this->id == id) && (this->owner == owner)) return this;
75 return NULL;
78 /* compute the size of the tray window */
79 static SIZE get_window_size(void)
81 SIZE size;
82 RECT rect;
84 rect.left = 0;
85 rect.top = 0;
86 rect.right = icon_cx * max( nb_displayed, MIN_DISPLAYED );
87 rect.bottom = icon_cy;
88 AdjustWindowRect( &rect, WS_CAPTION, FALSE );
89 size.cx = rect.right - rect.left;
90 size.cy = rect.bottom - rect.top;
91 return size;
94 /* Creates tooltip window for icon. */
95 static void create_tooltip(struct icon *icon)
97 TTTOOLINFOW ti;
98 static BOOL tooltips_initialized = FALSE;
100 /* Register tooltip classes if this is the first icon */
101 if (!tooltips_initialized)
103 INITCOMMONCONTROLSEX init_tooltip;
105 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
106 init_tooltip.dwICC = ICC_TAB_CLASSES;
108 InitCommonControlsEx(&init_tooltip);
109 tooltips_initialized = TRUE;
112 icon->tooltip = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
113 WS_POPUP | TTS_ALWAYSTIP,
114 CW_USEDEFAULT, CW_USEDEFAULT,
115 CW_USEDEFAULT, CW_USEDEFAULT,
116 tray_window, NULL, NULL, NULL);
118 ZeroMemory(&ti, sizeof(ti));
119 ti.cbSize = sizeof(TTTOOLINFOW);
120 ti.hwnd = tray_window;
121 ti.lpszText = icon->tiptext;
122 if (icon->display != -1)
124 ti.rect.left = icon_cx * icon->display;
125 ti.rect.right = icon_cx * (icon->display + 1);
126 ti.rect.top = 0;
127 ti.rect.bottom = icon_cy;
129 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
132 /* Synchronize tooltip text with tooltip window */
133 static void update_tooltip_text(struct icon *icon)
135 TTTOOLINFOW ti;
137 ZeroMemory(&ti, sizeof(ti));
138 ti.cbSize = sizeof(TTTOOLINFOW);
139 ti.hwnd = tray_window;
140 ti.lpszText = icon->tiptext;
142 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
145 /* synchronize tooltip position with tooltip window */
146 static void update_tooltip_position( struct icon *icon )
148 TTTOOLINFOW ti;
150 ZeroMemory(&ti, sizeof(ti));
151 ti.cbSize = sizeof(TTTOOLINFOW);
152 ti.hwnd = tray_window;
153 if (icon->display != -1)
155 ti.rect.left = icon_cx * icon->display;
156 ti.rect.right = icon_cx * (icon->display + 1);
157 ti.rect.top = 0;
158 ti.rect.bottom = icon_cy;
160 SendMessageW( icon->tooltip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti );
163 /* find the icon located at a certain point in the tray window */
164 static struct icon *icon_from_point( int x, int y )
166 if (y < 0 || y >= icon_cy) return NULL;
167 if (x < 0 || x >= icon_cx * nb_displayed) return NULL;
168 return displayed[x / icon_cx];
171 /* invalidate the portion of the tray window that contains the specified icons */
172 static void invalidate_icons( unsigned int start, unsigned int end )
174 RECT rect;
176 rect.left = start * icon_cx;
177 rect.top = 0;
178 rect.right = (end + 1) * icon_cx;
179 rect.bottom = icon_cy;
180 InvalidateRect( tray_window, &rect, TRUE );
183 /* make an icon visible */
184 static BOOL show_icon(struct icon *icon)
186 WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
188 if (icon->display != -1) return TRUE; /* already displayed */
190 if (nb_displayed >= alloc_displayed)
192 unsigned int new_count = max( alloc_displayed * 2, 32 );
193 struct icon **ptr;
194 if (displayed) ptr = HeapReAlloc( GetProcessHeap(), 0, displayed, new_count * sizeof(*ptr) );
195 else ptr = HeapAlloc( GetProcessHeap(), 0, new_count * sizeof(*ptr) );
196 if (!ptr) return FALSE;
197 displayed = ptr;
198 alloc_displayed = new_count;
201 icon->display = nb_displayed;
202 displayed[nb_displayed++] = icon;
203 update_tooltip_position( icon );
204 invalidate_icons( nb_displayed-1, nb_displayed-1 );
206 if (nb_displayed > MIN_DISPLAYED)
208 SIZE size = get_window_size();
209 SetWindowPos( tray_window, 0, 0, 0, size.cx, size.cy, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE );
211 else if (nb_displayed == 1)
213 if (!hide_systray) ShowWindow( tray_window, SW_SHOWNA );
216 create_tooltip(icon);
217 return TRUE;
220 /* make an icon invisible */
221 static BOOL hide_icon(struct icon *icon)
223 unsigned int i;
225 WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
227 if (icon->display == -1) return TRUE; /* already hidden */
229 assert( nb_displayed );
230 for (i = icon->display; i < nb_displayed - 1; i++)
232 displayed[i] = displayed[i + 1];
233 displayed[i]->display = i;
234 update_tooltip_position( displayed[i] );
236 nb_displayed--;
237 invalidate_icons( icon->display, nb_displayed );
238 icon->display = -1;
240 if (nb_displayed >= MIN_DISPLAYED)
242 SIZE size = get_window_size();
243 SetWindowPos( tray_window, 0, 0, 0, size.cx, size.cy, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE );
245 else if (!nb_displayed)
247 ShowWindow( tray_window, SW_HIDE );
250 update_tooltip_position( icon );
251 return TRUE;
254 /* Modifies an existing icon record */
255 static BOOL modify_icon( struct icon *icon, NOTIFYICONDATAW *nid )
257 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
259 /* demarshal the request from the NID */
260 if (!icon)
262 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
263 return FALSE;
266 if ((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))
268 if (nid->dwState & NIS_HIDDEN) hide_icon( icon );
269 else show_icon( icon );
272 if (nid->uFlags & NIF_ICON)
274 if (icon->image) DestroyIcon(icon->image);
275 icon->image = CopyIcon(nid->hIcon);
276 if (icon->display != -1) invalidate_icons( icon->display, icon->display );
279 if (nid->uFlags & NIF_MESSAGE)
281 icon->callback_message = nid->uCallbackMessage;
283 if (nid->uFlags & NIF_TIP)
285 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
286 if (icon->display != -1) update_tooltip_text(icon);
288 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
290 WINE_FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid->szInfoTitle), wine_dbgstr_w(nid->szInfo));
292 return TRUE;
295 /* Adds a new icon record to the list */
296 static BOOL add_icon(NOTIFYICONDATAW *nid)
298 struct icon *icon;
300 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
302 if ((icon = get_icon(nid->hWnd, nid->uID)))
304 WINE_WARN("duplicate tray icon add, buggy app?\n");
305 return FALSE;
308 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
310 WINE_ERR("out of memory\n");
311 return FALSE;
314 ZeroMemory(icon, sizeof(struct icon));
315 icon->id = nid->uID;
316 icon->owner = nid->hWnd;
317 icon->display = -1;
319 list_add_tail(&icon_list, &icon->entry);
321 modify_icon( icon, nid );
322 /* show icon, unless hidden state was explicitly specified */
323 if (!((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))) show_icon( icon );
324 return TRUE;
327 /* Deletes tray icon window and icon record */
328 static BOOL delete_icon(struct icon *icon)
330 hide_icon(icon);
331 list_remove(&icon->entry);
332 DestroyIcon(icon->image);
333 HeapFree(GetProcessHeap(), 0, icon);
334 return TRUE;
337 /* cleanup icons belonging to windows that have been destroyed */
338 static void cleanup_destroyed_windows(void)
340 struct icon *icon, *next;
342 LIST_FOR_EACH_ENTRY_SAFE( icon, next, &icon_list, struct icon, entry )
343 if (!IsWindow( icon->owner )) delete_icon( icon );
346 static BOOL handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
348 struct icon *icon = NULL;
349 NOTIFYICONDATAW nid;
350 DWORD cbSize;
351 int ret = FALSE;
353 if (cds->cbData < NOTIFYICONDATAW_V1_SIZE) return FALSE;
354 cbSize = ((PNOTIFYICONDATAW)cds->lpData)->cbSize;
355 if (cbSize < NOTIFYICONDATAW_V1_SIZE) return FALSE;
357 ZeroMemory(&nid, sizeof(nid));
358 memcpy(&nid, cds->lpData, min(sizeof(nid), cbSize));
360 /* FIXME: if statement only needed because we don't support interprocess
361 * icon handles */
362 if ((nid.uFlags & NIF_ICON) && (cds->cbData >= nid.cbSize + 2 * sizeof(BITMAP)))
364 LONG cbMaskBits;
365 LONG cbColourBits;
366 BITMAP bmMask;
367 BITMAP bmColour;
368 const char *buffer = cds->lpData;
370 buffer += nid.cbSize;
372 memcpy(&bmMask, buffer, sizeof(bmMask));
373 buffer += sizeof(bmMask);
374 memcpy(&bmColour, buffer, sizeof(bmColour));
375 buffer += sizeof(bmColour);
377 cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel) / 8;
378 cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel) / 8;
380 if (cds->cbData < nid.cbSize + 2 * sizeof(BITMAP) + cbMaskBits + cbColourBits)
382 WINE_ERR("buffer underflow\n");
383 return FALSE;
386 /* sanity check */
387 if ((bmColour.bmWidth != bmMask.bmWidth) || (bmColour.bmHeight != bmMask.bmHeight))
389 WINE_ERR("colour and mask bitmaps aren't consistent\n");
390 return FALSE;
393 nid.hIcon = CreateIcon(NULL, bmColour.bmWidth, bmColour.bmHeight,
394 bmColour.bmPlanes, bmColour.bmBitsPixel,
395 buffer, buffer + cbMaskBits);
398 /* try forward to x11drv first */
399 if (cds->dwData == NIM_ADD || !(icon = get_icon( nid.hWnd, nid.uID )))
401 if (wine_notify_icon && ((ret = wine_notify_icon( cds->dwData, &nid )) != -1))
403 if (nid.uFlags & NIF_ICON) DestroyIcon( nid.hIcon );
404 return ret;
406 ret = FALSE;
409 switch (cds->dwData)
411 case NIM_ADD:
412 ret = add_icon(&nid);
413 break;
414 case NIM_DELETE:
415 if (icon) ret = delete_icon( icon );
416 break;
417 case NIM_MODIFY:
418 if (icon) ret = modify_icon( icon, &nid );
419 break;
420 default:
421 WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
422 break;
425 /* FIXME: if statement only needed because we don't support interprocess
426 * icon handles */
427 if (nid.uFlags & NIF_ICON)
428 DestroyIcon(nid.hIcon);
430 return ret;
433 static void do_hide_systray(void)
435 SetWindowPos( tray_window, 0,
436 GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN),
437 GetSystemMetrics(SM_YVIRTUALSCREEN) + GetSystemMetrics(SM_CYVIRTUALSCREEN),
438 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
441 static LRESULT WINAPI tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
443 switch (msg)
445 case WM_COPYDATA:
446 return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
448 case WM_DISPLAYCHANGE:
449 if (hide_systray) do_hide_systray();
450 break;
452 case WM_TIMER:
453 cleanup_destroyed_windows();
454 break;
456 case WM_PAINT:
458 unsigned int i;
459 PAINTSTRUCT ps;
460 HDC hdc;
462 hdc = BeginPaint( hwnd, &ps );
463 for (i = ps.rcPaint.left / icon_cx;
464 (i < (ps.rcPaint.right + icon_cx - 1) / icon_cx) && (i < nb_displayed);
465 i++)
467 DrawIconEx( hdc, i * icon_cx + ICON_BORDER, ICON_BORDER, displayed[i]->image,
468 icon_cx - 2*ICON_BORDER, icon_cy - 2*ICON_BORDER,
469 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
471 EndPaint( hwnd, &ps );
472 break;
475 case WM_MOUSEMOVE:
476 case WM_LBUTTONDOWN:
477 case WM_LBUTTONUP:
478 case WM_RBUTTONDOWN:
479 case WM_RBUTTONUP:
480 case WM_MBUTTONDOWN:
481 case WM_MBUTTONUP:
482 case WM_LBUTTONDBLCLK:
483 case WM_RBUTTONDBLCLK:
484 case WM_MBUTTONDBLCLK:
486 MSG message;
487 struct icon *icon = icon_from_point( (short)LOWORD(lparam), (short)HIWORD(lparam) );
488 if (!icon) break;
490 /* notify the owner hwnd of the message */
491 WINE_TRACE("relaying 0x%x\n", msg);
493 message.hwnd = hwnd;
494 message.message = msg;
495 message.wParam = wparam;
496 message.lParam = lparam;
497 SendMessageW( icon->tooltip, TTM_RELAYEVENT, 0, (LPARAM)&message );
499 if (!PostMessageW( icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg ) &&
500 GetLastError() == ERROR_INVALID_WINDOW_HANDLE)
502 WINE_WARN("application window was destroyed without removing "
503 "notification icon, removing automatically\n");
504 delete_icon( icon );
506 break;
509 case WM_CLOSE:
510 /* don't destroy the tray window, just hide it */
511 ShowWindow( hwnd, SW_HIDE );
512 return 0;
514 default:
515 return DefWindowProcW( hwnd, msg, wparam, lparam );
517 return 0;
520 static BOOL is_systray_hidden(void)
522 const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
523 'X','1','1',' ','D','r','i','v','e','r',0};
524 const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
525 HKEY hkey;
526 BOOL ret = FALSE;
528 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
529 if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
531 WCHAR value[10];
532 DWORD type, size = sizeof(value);
533 if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
535 ret = IS_OPTION_FALSE(value[0]);
537 RegCloseKey(hkey);
539 return ret;
542 /* this function creates the listener window */
543 void initialize_systray(void)
545 HMODULE x11drv;
546 SIZE size;
547 WNDCLASSEXW class;
548 static const WCHAR classname[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
549 static const WCHAR winname[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
551 if ((x11drv = GetModuleHandleA( "winex11.drv" )))
552 wine_notify_icon = (void *)GetProcAddress( x11drv, "wine_notify_icon" );
554 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
555 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
556 hide_systray = is_systray_hidden();
558 /* register the systray listener window class */
559 ZeroMemory(&class, sizeof(class));
560 class.cbSize = sizeof(class);
561 class.style = CS_DBLCLKS;
562 class.lpfnWndProc = tray_wndproc;
563 class.hInstance = NULL;
564 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
565 class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
566 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
567 class.lpszClassName = (WCHAR *) &classname;
569 if (!RegisterClassExW(&class))
571 WINE_ERR("Could not register SysTray window class\n");
572 return;
575 size = get_window_size();
576 tray_window = CreateWindowW( classname, winname, WS_OVERLAPPED | WS_CAPTION,
577 CW_USEDEFAULT, CW_USEDEFAULT, size.cx, size.cy, 0, 0, 0, 0 );
578 if (!tray_window)
580 WINE_ERR("Could not create tray window\n");
581 return;
584 if (hide_systray) do_hide_systray();
586 SetTimer( tray_window, 1, 2000, NULL );