wordpad: Remove untranslated resources from the Turkish translation.
[wine/testsucceed.git] / programs / explorer / systray.c
blob9a4226adb3c5f01ec64ac4346d5394bb330095d3
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 list_add_tail(&icon_list, &icon->entry);
345 modify_icon( icon, nid );
346 /* show icon, unless hidden state was explicitly specified */
347 if (!((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))) show_icon( icon );
348 return TRUE;
351 /* Deletes tray icon window and icon record */
352 static BOOL delete_icon(struct icon *icon)
354 hide_icon(icon);
355 list_remove(&icon->entry);
356 DestroyIcon(icon->image);
357 HeapFree(GetProcessHeap(), 0, icon);
358 return TRUE;
361 /* cleanup icons belonging to windows that have been destroyed */
362 static void cleanup_destroyed_windows(void)
364 struct icon *icon, *next;
366 LIST_FOR_EACH_ENTRY_SAFE( icon, next, &icon_list, struct icon, entry )
367 if (!IsWindow( icon->owner )) delete_icon( icon );
370 static BOOL handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
372 struct icon *icon = NULL;
373 const struct notify_data *data;
374 NOTIFYICONDATAW nid;
375 int ret = FALSE;
377 if (cds->cbData < sizeof(*data)) return FALSE;
378 data = cds->lpData;
380 nid.cbSize = sizeof(nid);
381 nid.hWnd = LongToHandle( data->hWnd );
382 nid.uID = data->uID;
383 nid.uFlags = data->uFlags;
384 nid.uCallbackMessage = data->uCallbackMessage;
385 nid.hIcon = 0;
386 nid.dwState = data->dwState;
387 nid.dwStateMask = data->dwStateMask;
388 nid.u.uTimeout = data->u.uTimeout;
389 nid.dwInfoFlags = data->dwInfoFlags;
390 nid.guidItem = data->guidItem;
391 lstrcpyW( nid.szTip, data->szTip );
392 lstrcpyW( nid.szInfo, data->szInfo );
393 lstrcpyW( nid.szInfoTitle, data->szInfoTitle );
394 nid.hBalloonIcon = 0;
396 /* FIXME: if statement only needed because we don't support interprocess
397 * icon handles */
398 if ((nid.uFlags & NIF_ICON) && cds->cbData > sizeof(*data))
400 LONG cbMaskBits;
401 LONG cbColourBits;
402 const char *buffer = (const char *)(data + 1);
404 cbMaskBits = (data->width * data->height + 15) / 16 * 2;
405 cbColourBits = (data->planes * data->width * data->height * data->bpp + 15) / 16 * 2;
407 if (cds->cbData < sizeof(*data) + cbMaskBits + cbColourBits)
409 WINE_ERR("buffer underflow\n");
410 return FALSE;
412 nid.hIcon = CreateIcon(NULL, data->width, data->height, data->planes, data->bpp,
413 buffer, buffer + cbMaskBits);
416 /* try forward to x11drv first */
417 if (cds->dwData == NIM_ADD || !(icon = get_icon( nid.hWnd, nid.uID )))
419 if (wine_notify_icon && ((ret = wine_notify_icon( cds->dwData, &nid )) != -1))
421 if (nid.hIcon) DestroyIcon( nid.hIcon );
422 return ret;
424 ret = FALSE;
427 switch (cds->dwData)
429 case NIM_ADD:
430 ret = add_icon(&nid);
431 break;
432 case NIM_DELETE:
433 if (icon) ret = delete_icon( icon );
434 break;
435 case NIM_MODIFY:
436 if (icon) ret = modify_icon( icon, &nid );
437 break;
438 default:
439 WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
440 break;
443 if (nid.hIcon) DestroyIcon( nid.hIcon );
444 return ret;
447 static void do_hide_systray(void)
449 SetWindowPos( tray_window, 0,
450 GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN),
451 GetSystemMetrics(SM_YVIRTUALSCREEN) + GetSystemMetrics(SM_CYVIRTUALSCREEN),
452 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
455 static LRESULT WINAPI tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
457 switch (msg)
459 case WM_COPYDATA:
460 return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
462 case WM_DISPLAYCHANGE:
463 if (hide_systray) do_hide_systray();
464 break;
466 case WM_TIMER:
467 cleanup_destroyed_windows();
468 break;
470 case WM_PAINT:
472 unsigned int i;
473 PAINTSTRUCT ps;
474 HDC hdc;
476 hdc = BeginPaint( hwnd, &ps );
477 for (i = ps.rcPaint.left / icon_cx;
478 (i < (ps.rcPaint.right + icon_cx - 1) / icon_cx) && (i < nb_displayed);
479 i++)
481 DrawIconEx( hdc, i * icon_cx + ICON_BORDER, ICON_BORDER, displayed[i]->image,
482 icon_cx - 2*ICON_BORDER, icon_cy - 2*ICON_BORDER,
483 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
485 EndPaint( hwnd, &ps );
486 break;
489 case WM_MOUSEMOVE:
490 case WM_LBUTTONDOWN:
491 case WM_LBUTTONUP:
492 case WM_RBUTTONDOWN:
493 case WM_RBUTTONUP:
494 case WM_MBUTTONDOWN:
495 case WM_MBUTTONUP:
496 case WM_LBUTTONDBLCLK:
497 case WM_RBUTTONDBLCLK:
498 case WM_MBUTTONDBLCLK:
500 MSG message;
501 struct icon *icon = icon_from_point( (short)LOWORD(lparam), (short)HIWORD(lparam) );
502 if (!icon) break;
504 /* notify the owner hwnd of the message */
505 WINE_TRACE("relaying 0x%x\n", msg);
507 message.hwnd = hwnd;
508 message.message = msg;
509 message.wParam = wparam;
510 message.lParam = lparam;
511 SendMessageW( icon->tooltip, TTM_RELAYEVENT, 0, (LPARAM)&message );
513 if (!PostMessageW( icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg ) &&
514 GetLastError() == ERROR_INVALID_WINDOW_HANDLE)
516 WINE_WARN("application window was destroyed without removing "
517 "notification icon, removing automatically\n");
518 delete_icon( icon );
520 break;
523 case WM_CLOSE:
524 /* don't destroy the tray window, just hide it */
525 ShowWindow( hwnd, SW_HIDE );
526 return 0;
528 default:
529 return DefWindowProcW( hwnd, msg, wparam, lparam );
531 return 0;
534 static BOOL is_systray_hidden(void)
536 const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
537 'X','1','1',' ','D','r','i','v','e','r',0};
538 const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
539 HKEY hkey;
540 BOOL ret = FALSE;
542 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
543 if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
545 WCHAR value[10];
546 DWORD type, size = sizeof(value);
547 if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
549 ret = IS_OPTION_FALSE(value[0]);
551 RegCloseKey(hkey);
553 return ret;
556 /* this function creates the listener window */
557 void initialize_systray(void)
559 HMODULE x11drv;
560 SIZE size;
561 WNDCLASSEXW class;
562 static const WCHAR classname[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
563 static const WCHAR winname[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
565 if ((x11drv = GetModuleHandleA( "winex11.drv" )))
566 wine_notify_icon = (void *)GetProcAddress( x11drv, "wine_notify_icon" );
568 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
569 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
570 hide_systray = is_systray_hidden();
572 /* register the systray listener window class */
573 ZeroMemory(&class, sizeof(class));
574 class.cbSize = sizeof(class);
575 class.style = CS_DBLCLKS;
576 class.lpfnWndProc = tray_wndproc;
577 class.hInstance = NULL;
578 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
579 class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
580 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
581 class.lpszClassName = (WCHAR *) &classname;
583 if (!RegisterClassExW(&class))
585 WINE_ERR("Could not register SysTray window class\n");
586 return;
589 size = get_window_size();
590 tray_window = CreateWindowW( classname, winname, WS_OVERLAPPED | WS_CAPTION,
591 CW_USEDEFAULT, CW_USEDEFAULT, size.cx, size.cy, 0, 0, 0, 0 );
592 if (!tray_window)
594 WINE_ERR("Could not create tray window\n");
595 return;
598 if (hide_systray) do_hide_systray();
600 SetTimer( tray_window, 1, 2000, NULL );