Update Russian translations.
[wine/wine-kai.git] / programs / explorer / systray.c
blobb01203fa76e139b3ecbf5c2eb2037de207d4cb9b
1 /*
2 * Copyright (C) 2004 Mike Hearn, for CodeWeavers
3 * Copyright (C) 2005 Robert Shearman
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* There are two types of window involved here. The first is the
21 * listener window. This is like the taskbar in Windows. It doesn't
22 * ever appear on-screen in our implementation, instead we create
23 * individual mini "adaptor" windows which are docked by the native
24 * systray host.
26 * In future for those who don't have a systray we could make the
27 * listener window more clever so it can draw itself like the Windows
28 * tray area does (with a clock and stuff).
31 #include <assert.h>
33 #define UNICODE
34 #define _WIN32_IE 0x500
35 #include <windows.h>
36 #include <commctrl.h>
38 #include <wine/debug.h>
39 #include <wine/list.h>
41 #include "explorer_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(systray);
45 #define IS_OPTION_FALSE(ch) \
46 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
48 static const WCHAR adaptor_classname[] = /* Adaptor */ {'A','d','a','p','t','o','r',0};
50 /* tray state */
51 struct tray
53 HWND window;
54 struct list icons;
57 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
58 struct icon
60 struct list entry;
61 HICON image; /* the image to render */
62 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
63 HWND window; /* the adaptor window */
64 HWND tooltip; /* Icon tooltip */
65 UINT id; /* the unique id given by the app */
66 UINT callback_message;
67 BOOL hidden; /* icon display state */
68 WCHAR tiptext[128]; /* Tooltip text. If empty => tooltip disabled */
71 static struct tray tray;
72 static BOOL hide_systray;
74 /* adaptor code */
76 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
77 /* space around icon (forces icon to center of KDE systray area) */
78 #define ICON_BORDER 4
81 /* Retrieves icon record by owner window and ID */
82 static struct icon *get_icon(HWND owner, UINT id)
84 struct icon *this;
86 /* search for the icon */
87 LIST_FOR_EACH_ENTRY( this, &tray.icons, struct icon, entry )
88 if ((this->id == id) && (this->owner == owner)) return this;
90 return NULL;
93 /* Creates tooltip window for icon. */
94 static void create_tooltip(struct icon *icon)
96 TTTOOLINFOW ti;
97 static BOOL tooltips_initialized = FALSE;
99 /* Register tooltip classes if this is the first icon */
100 if (!tooltips_initialized)
102 INITCOMMONCONTROLSEX init_tooltip;
104 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
105 init_tooltip.dwICC = ICC_TAB_CLASSES;
107 InitCommonControlsEx(&init_tooltip);
108 tooltips_initialized = TRUE;
111 icon->tooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
112 WS_POPUP | TTS_ALWAYSTIP,
113 CW_USEDEFAULT, CW_USEDEFAULT,
114 CW_USEDEFAULT, CW_USEDEFAULT,
115 icon->window, NULL, NULL, NULL);
117 ZeroMemory(&ti, sizeof(ti));
118 ti.cbSize = sizeof(TTTOOLINFOW);
119 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
120 ti.hwnd = icon->window;
121 ti.uId = (UINT_PTR)icon->window;
122 ti.lpszText = icon->tiptext;
123 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
126 /* Synchronize tooltip text with tooltip window */
127 static void update_tooltip_text(struct icon *icon)
129 TTTOOLINFOW ti;
131 ZeroMemory(&ti, sizeof(ti));
132 ti.cbSize = sizeof(TTTOOLINFOW);
133 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
134 ti.hwnd = icon->window;
135 ti.uId = (UINT_PTR)icon->window;
136 ti.lpszText = icon->tiptext;
138 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
142 * Sets visibility status of tray icon. Creates/deletes icon window.
143 * Does not create/delete icon record.
145 * The purpose is similar to ShowWindow function.
147 static BOOL display_icon(struct icon *icon, BOOL hide)
149 HMODULE x11drv = GetModuleHandleA("winex11.drv");
150 RECT rect;
151 static const WCHAR adaptor_windowname[] = /* Wine System Tray Adaptor */ {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',' ','A','d','a','p','t','o','r',0};
153 WINE_TRACE("id=0x%x, hwnd=%p, hide=%d\n", icon->id, icon->owner, hide);
155 /* not a startup case and nothing to do */
156 if (icon->window && !icon->hidden == !hide)
157 return TRUE;
159 icon->hidden = hide;
160 if (hide)
162 /* At startup icon->hidden == FALSE and icon->window == NULL */
163 if (icon->window)
165 DestroyWindow(icon->window);
166 DestroyWindow(icon->tooltip);
168 return TRUE;
171 rect.left = 0;
172 rect.top = 0;
173 rect.right = GetSystemMetrics(SM_CXSMICON) + ICON_BORDER;
174 rect.bottom = GetSystemMetrics(SM_CYSMICON) + ICON_BORDER;
175 AdjustWindowRect(&rect, WS_CLIPSIBLINGS | WS_CAPTION, FALSE);
177 /* create the adaptor window */
178 icon->window = CreateWindowEx(0, adaptor_classname,
179 adaptor_windowname,
180 WS_CLIPSIBLINGS | WS_CAPTION,
181 CW_USEDEFAULT, CW_USEDEFAULT,
182 rect.right - rect.left,
183 rect.bottom - rect.top,
184 NULL, NULL, NULL, icon);
185 if (x11drv)
187 void (*make_systray_window)(HWND) = (void *)GetProcAddress(x11drv, "wine_make_systray_window");
188 if (make_systray_window) make_systray_window(icon->window);
191 if (!hide_systray)
192 ShowWindow(icon->window, SW_SHOWNA);
194 create_tooltip(icon);
196 return TRUE;
199 /* Modifies an existing icon record */
200 static BOOL modify_icon(NOTIFYICONDATAW *nid)
202 struct icon *icon;
204 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
206 /* demarshal the request from the NID */
207 icon = get_icon(nid->hWnd, nid->uID);
208 if (!icon)
210 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
211 return FALSE;
214 if ((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))
215 display_icon(icon, !!(nid->dwState & NIS_HIDDEN));
217 /* startup case*/
218 if ((!icon->window) && (!icon->hidden))
219 display_icon(icon, FALSE);
221 if (nid->uFlags & NIF_ICON)
223 if (icon->image) DestroyIcon(icon->image);
224 icon->image = CopyIcon(nid->hIcon);
226 if (!icon->hidden)
227 RedrawWindow(icon->window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
230 if (nid->uFlags & NIF_MESSAGE)
232 icon->callback_message = nid->uCallbackMessage;
234 if (nid->uFlags & NIF_TIP)
236 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
237 if (!icon->hidden)
238 update_tooltip_text(icon);
240 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
242 WINE_FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid->szInfoTitle), wine_dbgstr_w(nid->szInfo));
244 return TRUE;
247 /* Adds a new icon record to the list */
248 static BOOL add_icon(NOTIFYICONDATAW *nid)
250 struct icon *icon;
252 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
254 if ((icon = get_icon(nid->hWnd, nid->uID)))
256 WINE_WARN("duplicate tray icon add, buggy app?\n");
257 return FALSE;
260 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
262 WINE_ERR("out of memory\n");
263 return FALSE;
266 ZeroMemory(icon, sizeof(struct icon));
267 icon->id = nid->uID;
268 icon->owner = nid->hWnd;
270 list_add_tail(&tray.icons, &icon->entry);
273 * Both icon->window and icon->hidden are zero. modify_icon function
274 * will treat this case as a startup, i.e. icon window will be created if
275 * NIS_HIDDEN flag is not set.
278 return modify_icon(nid);
281 /* Deletes tray icon window and icon record */
282 static BOOL delete_icon_directly(struct icon *icon)
284 display_icon(icon, TRUE);
285 list_remove(&icon->entry);
286 DestroyIcon(icon->image);
287 HeapFree(GetProcessHeap(), 0, icon);
288 return TRUE;
291 /* Deletes tray icon window and icon structure */
292 static BOOL delete_icon(const NOTIFYICONDATAW *nid)
294 struct icon *icon = get_icon(nid->hWnd, nid->uID);
296 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
298 if (!icon)
300 WINE_WARN("invalid tray icon ID specified: %u\n", nid->uID);
301 return FALSE;
304 return delete_icon_directly(icon);
307 static BOOL handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
309 NOTIFYICONDATAW nid;
310 DWORD cbSize;
311 BOOL ret = FALSE;
313 if (cds->cbData < NOTIFYICONDATAW_V1_SIZE) return FALSE;
314 cbSize = ((PNOTIFYICONDATA)cds->lpData)->cbSize;
315 if (cbSize < NOTIFYICONDATAW_V1_SIZE) return FALSE;
317 ZeroMemory(&nid, sizeof(nid));
318 memcpy(&nid, cds->lpData, min(sizeof(nid), cbSize));
320 /* FIXME: if statement only needed because we don't support interprocess
321 * icon handles */
322 if ((nid.uFlags & NIF_ICON) && (cds->cbData >= nid.cbSize + 2 * sizeof(BITMAP)))
324 LONG cbMaskBits;
325 LONG cbColourBits;
326 BITMAP bmMask;
327 BITMAP bmColour;
328 const char *buffer = cds->lpData;
330 buffer += nid.cbSize;
332 memcpy(&bmMask, buffer, sizeof(bmMask));
333 buffer += sizeof(bmMask);
334 memcpy(&bmColour, buffer, sizeof(bmColour));
335 buffer += sizeof(bmColour);
337 cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel) / 8;
338 cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel) / 8;
340 if (cds->cbData < nid.cbSize + 2 * sizeof(BITMAP) + cbMaskBits + cbColourBits)
342 WINE_ERR("buffer underflow\n");
343 return FALSE;
346 /* sanity check */
347 if ((bmColour.bmWidth != bmMask.bmWidth) || (bmColour.bmHeight != bmMask.bmHeight))
349 WINE_ERR("colour and mask bitmaps aren't consistent\n");
350 return FALSE;
353 nid.hIcon = CreateIcon(NULL, bmColour.bmWidth, bmColour.bmHeight,
354 bmColour.bmPlanes, bmColour.bmBitsPixel,
355 buffer, buffer + cbMaskBits);
358 switch (cds->dwData)
360 case NIM_ADD:
361 ret = add_icon(&nid);
362 break;
363 case NIM_DELETE:
364 ret = delete_icon(&nid);
365 break;
366 case NIM_MODIFY:
367 ret = modify_icon(&nid);
368 break;
369 default:
370 WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
371 break;
374 /* FIXME: if statement only needed because we don't support interprocess
375 * icon handles */
376 if (nid.uFlags & NIF_ICON)
377 DestroyIcon(nid.hIcon);
379 return ret;
382 static LRESULT WINAPI listener_wndproc(HWND window, UINT msg,
383 WPARAM wparam, LPARAM lparam)
385 if (msg == WM_COPYDATA)
386 return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
388 return DefWindowProc(window, msg, wparam, lparam);
391 static LRESULT WINAPI adaptor_wndproc(HWND window, UINT msg,
392 WPARAM wparam, LPARAM lparam)
394 struct icon *icon = NULL;
395 BOOL ret;
397 WINE_TRACE("hwnd=%p, msg=0x%x\n", window, msg);
399 /* set the icon data for the window from the data passed into CreateWindow */
400 if (msg == WM_NCCREATE)
401 SetWindowLongPtrW(window, GWLP_USERDATA, (LPARAM)((const CREATESTRUCT *)lparam)->lpCreateParams);
403 icon = (struct icon *) GetWindowLongPtr(window, GWLP_USERDATA);
405 switch (msg)
407 case WM_PAINT:
409 RECT rc;
410 int top;
411 PAINTSTRUCT ps;
412 HDC hdc;
414 WINE_TRACE("painting\n");
416 hdc = BeginPaint(window, &ps);
417 GetClientRect(window, &rc);
419 /* calculate top so we can deal with arbitrary sized trays */
420 top = ((rc.bottom-rc.top)/2) - ((ICON_SIZE)/2);
422 DrawIconEx(hdc, (ICON_BORDER/2), top, icon->image,
423 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
425 EndPaint(window, &ps);
426 break;
429 case WM_MOUSEMOVE:
430 case WM_LBUTTONDOWN:
431 case WM_LBUTTONUP:
432 case WM_RBUTTONDOWN:
433 case WM_RBUTTONUP:
434 case WM_MBUTTONDOWN:
435 case WM_MBUTTONUP:
436 case WM_LBUTTONDBLCLK:
437 case WM_RBUTTONDBLCLK:
438 case WM_MBUTTONDBLCLK:
439 /* notify the owner hwnd of the message */
440 WINE_TRACE("relaying 0x%x\n", msg);
441 ret = PostMessage(icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg);
442 if (!ret && (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
444 WINE_WARN("application window was destroyed without removing "
445 "notification icon, removing automatically\n");
446 delete_icon_directly(icon);
448 break;
450 case WM_NCDESTROY:
451 SetWindowLongPtr(window, GWLP_USERDATA, 0);
452 break;
454 default:
455 return DefWindowProc(window, msg, wparam, lparam);
458 return 0;
462 static BOOL is_systray_hidden(void)
464 const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
465 'X','1','1',' ','D','r','i','v','e','r',0};
466 const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
467 HKEY hkey;
468 BOOL ret = FALSE;
470 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
471 if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
473 WCHAR value[10];
474 DWORD type, size = sizeof(value);
475 if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
477 ret = IS_OPTION_FALSE(value[0]);
479 RegCloseKey(hkey);
481 return ret;
484 /* this function creates the listener window */
485 void initialize_systray(void)
487 WNDCLASSEX class;
488 static const WCHAR classname[] = /* Shell_TrayWnd */ {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
489 static const WCHAR winname[] = /* Wine Systray Listener */
490 {'W','i','n','e',' ','S','y','s','t','r','a','y',' ','L','i','s','t','e','n','e','r',0};
492 WINE_TRACE("initiaizing\n");
494 hide_systray = is_systray_hidden();
496 list_init(&tray.icons);
498 /* register the systray listener window class */
499 ZeroMemory(&class, sizeof(class));
500 class.cbSize = sizeof(class);
501 class.lpfnWndProc = &listener_wndproc;
502 class.hInstance = NULL;
503 class.hIcon = LoadIcon(0, IDI_WINLOGO);
504 class.hCursor = LoadCursor(0, IDC_ARROW);
505 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
506 class.lpszClassName = (WCHAR *) &classname;
508 if (!RegisterClassEx(&class))
510 WINE_ERR("Could not register SysTray window class\n");
511 return;
514 /* now register the adaptor window class */
515 ZeroMemory(&class, sizeof(class));
516 class.cbSize = sizeof(class);
517 class.lpfnWndProc = adaptor_wndproc;
518 class.hInstance = NULL;
519 class.hIcon = LoadIcon(0, IDI_WINLOGO);
520 class.hCursor = LoadCursor(0, IDC_ARROW);
521 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
522 class.lpszClassName = adaptor_classname;
523 class.style = CS_SAVEBITS | CS_DBLCLKS;
525 if (!RegisterClassEx(&class))
527 WINE_ERR("Could not register adaptor class\n");
528 return;
531 tray.window = CreateWindow(classname, winname, WS_OVERLAPPED,
532 CW_USEDEFAULT, CW_USEDEFAULT,
533 0, 0, 0, 0, 0, 0);
535 if (!tray.window)
537 WINE_ERR("Could not create tray window\n");
538 return;