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
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).
34 #define _WIN32_IE 0x500
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};
57 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
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 */
70 static struct tray tray;
71 static BOOL hide_systray;
75 #define ICON_SIZE GetSystemMetrics(SM_CXSMICON)
76 /* space around icon (forces icon to center of KDE systray area) */
79 static LRESULT WINAPI adaptor_wndproc(HWND window, UINT msg,
80 WPARAM wparam, LPARAM lparam)
82 struct icon *icon = NULL;
85 WINE_TRACE("hwnd=%p, msg=0x%x\n", window, msg);
87 /* set the icon data for the window from the data passed into CreateWindow */
88 if (msg == WM_NCCREATE)
89 SetWindowLongPtrW(window, GWLP_USERDATA, (LPARAM)((const CREATESTRUCT *)lparam)->lpCreateParams);
91 icon = (struct icon *) GetWindowLongPtr(window, GWLP_USERDATA);
102 WINE_TRACE("painting\n");
104 hdc = BeginPaint(window, &ps);
105 GetClientRect(window, &rc);
107 /* calculate top so we can deal with arbitrary sized trays */
108 top = ((rc.bottom-rc.top)/2) - ((ICON_SIZE)/2);
110 DrawIconEx(hdc, (ICON_BORDER/2), top, icon->image,
111 ICON_SIZE, ICON_SIZE, 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
113 EndPaint(window, &ps);
124 case WM_LBUTTONDBLCLK:
125 case WM_RBUTTONDBLCLK:
126 case WM_MBUTTONDBLCLK:
127 /* notify the owner hwnd of the message */
128 WINE_TRACE("relaying 0x%x\n", msg);
129 ret = PostMessage(icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg);
130 if (!ret && (GetLastError() == ERROR_INVALID_HANDLE))
132 WINE_WARN("application window was destroyed without removing "
133 "notification icon, removing automatically\n");
134 DestroyWindow(window);
139 SetWindowLongPtr(window, GWLP_USERDATA, 0);
141 list_remove(&icon->entry);
142 DestroyIcon(icon->image);
143 HeapFree(GetProcessHeap(), 0, icon);
147 return DefWindowProc(window, msg, wparam, lparam);
156 static struct icon *get_icon(HWND owner, UINT id)
160 /* search for the icon */
161 LIST_FOR_EACH_ENTRY( this, &tray.icons, struct icon, entry )
162 if ((this->id == id) && (this->owner == owner)) return this;
167 static void set_tooltip(struct icon *icon, WCHAR *szTip, BOOL modify)
171 ti.cbSize = sizeof(TTTOOLINFOW);
172 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
173 ti.hwnd = icon->window;
175 ti.uId = (UINT_PTR)icon->window;
178 ti.lpReserved = NULL;
181 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
183 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
186 static BOOL display_icon(struct icon *icon, BOOL hide)
188 HMODULE x11drv = GetModuleHandleA("winex11.drv");
190 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};
191 static BOOL tooltps_initialized = FALSE;
193 WINE_TRACE("id=0x%x, hwnd=%p, hide=%d\n", icon->id, icon->owner, hide);
195 if (icon->hidden == hide || (!icon->hidden) == (!hide)) return TRUE;
199 DestroyWindow(icon->window);
200 DestroyWindow(icon->tooltip);
206 rect.right = GetSystemMetrics(SM_CXSMICON) + ICON_BORDER;
207 rect.bottom = GetSystemMetrics(SM_CYSMICON) + ICON_BORDER;
208 AdjustWindowRect(&rect, WS_CLIPSIBLINGS | WS_CAPTION, FALSE);
210 /* create the adaptor window */
211 icon->window = CreateWindowEx(0, adaptor_classname,
213 WS_CLIPSIBLINGS | WS_CAPTION,
214 CW_USEDEFAULT, CW_USEDEFAULT,
215 rect.right - rect.left,
216 rect.bottom - rect.top,
217 NULL, NULL, NULL, icon);
220 void (*make_systray_window)(HWND) = (void *)GetProcAddress(x11drv, "wine_make_systray_window");
221 if (make_systray_window) make_systray_window(icon->window);
225 ShowWindow(icon->window, SW_SHOWNA);
227 /* create icon tooltip */
229 /* Register tooltip classes if this is the first icon */
230 if (!tooltps_initialized)
232 INITCOMMONCONTROLSEX init_tooltip;
234 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
235 init_tooltip.dwICC = ICC_TAB_CLASSES;
237 InitCommonControlsEx(&init_tooltip);
238 tooltps_initialized = TRUE;
241 icon->tooltip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
242 WS_POPUP | TTS_ALWAYSTIP,
243 CW_USEDEFAULT, CW_USEDEFAULT,
244 CW_USEDEFAULT, CW_USEDEFAULT,
245 icon->window, NULL, NULL, NULL);
249 static BOOL modify_icon(NOTIFYICONDATAW *nid, BOOL modify_tooltip)
253 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
255 /* demarshal the request from the NID */
256 icon = get_icon(nid->hWnd, nid->uID);
259 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
263 if (nid->uFlags & NIF_STATE)
265 if (nid->dwStateMask & NIS_HIDDEN)
266 display_icon(icon, !!(nid->dwState & NIS_HIDDEN));
268 display_icon(icon, FALSE);
271 display_icon(icon, FALSE);
273 if (nid->uFlags & NIF_ICON)
275 if (icon->image) DestroyIcon(icon->image);
276 icon->image = CopyIcon(nid->hIcon);
279 RedrawWindow(icon->window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
282 if (nid->uFlags & NIF_MESSAGE)
284 icon->callback_message = nid->uCallbackMessage;
286 if (nid->uFlags & NIF_TIP)
288 set_tooltip(icon, nid->szTip, modify_tooltip);
290 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
292 WINE_FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid->szInfoTitle), wine_dbgstr_w(nid->szInfo));
297 static BOOL add_icon(NOTIFYICONDATAW *nid)
301 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
303 if ((icon = get_icon(nid->hWnd, nid->uID)))
305 WINE_WARN("duplicate tray icon add, buggy app?\n");
309 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
311 WINE_ERR("out of memory\n");
316 icon->owner = nid->hWnd;
321 list_add_tail(&tray.icons, &icon->entry);
323 return modify_icon(nid, FALSE);
326 static BOOL delete_icon(const NOTIFYICONDATAW *nid)
328 struct icon *icon = get_icon(nid->hWnd, nid->uID);
330 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
334 WINE_WARN("invalid tray icon ID specified: %u\n", nid->uID);
338 display_icon(icon, TRUE);
342 static BOOL handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
348 if (cds->cbData < NOTIFYICONDATAW_V1_SIZE) return FALSE;
349 cbSize = ((PNOTIFYICONDATA)cds->lpData)->cbSize;
350 if (cbSize < NOTIFYICONDATAW_V1_SIZE) return FALSE;
352 ZeroMemory(&nid, sizeof(nid));
353 memcpy(&nid, cds->lpData, min(sizeof(nid), cbSize));
355 /* FIXME: if statement only needed because we don't support interprocess
357 if ((nid.uFlags & NIF_ICON) && (cds->cbData >= nid.cbSize + 2 * sizeof(BITMAP)))
363 const char *buffer = cds->lpData;
365 buffer += nid.cbSize;
367 memcpy(&bmMask, buffer, sizeof(bmMask));
368 buffer += sizeof(bmMask);
369 memcpy(&bmColour, buffer, sizeof(bmColour));
370 buffer += sizeof(bmColour);
372 cbMaskBits = (bmMask.bmPlanes * bmMask.bmWidth * bmMask.bmHeight * bmMask.bmBitsPixel) / 8;
373 cbColourBits = (bmColour.bmPlanes * bmColour.bmWidth * bmColour.bmHeight * bmColour.bmBitsPixel) / 8;
375 if (cds->cbData < nid.cbSize + 2 * sizeof(BITMAP) + cbMaskBits + cbColourBits)
377 WINE_ERR("buffer underflow\n");
382 if ((bmColour.bmWidth != bmMask.bmWidth) || (bmColour.bmHeight != bmMask.bmHeight))
384 WINE_ERR("colour and mask bitmaps aren't consistent\n");
388 nid.hIcon = CreateIcon(NULL, bmColour.bmWidth, bmColour.bmHeight,
389 bmColour.bmPlanes, bmColour.bmBitsPixel,
390 buffer, buffer + cbMaskBits);
396 ret = add_icon(&nid);
399 ret = delete_icon(&nid);
402 ret = modify_icon(&nid, TRUE);
405 WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
409 /* FIXME: if statement only needed because we don't support interprocess
411 if (nid.uFlags & NIF_ICON)
412 DestroyIcon(nid.hIcon);
417 static LRESULT WINAPI listener_wndproc(HWND window, UINT msg,
418 WPARAM wparam, LPARAM lparam)
420 if (msg == WM_COPYDATA)
421 return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
423 return DefWindowProc(window, msg, wparam, lparam);
427 static BOOL is_systray_hidden(void)
429 const WCHAR show_systray_keyname[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
430 'X','1','1',' ','D','r','i','v','e','r',0};
431 const WCHAR show_systray_valuename[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
435 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
436 if (RegOpenKeyW(HKEY_CURRENT_USER, show_systray_keyname, &hkey) == ERROR_SUCCESS)
439 DWORD type, size = sizeof(value);
440 if (RegQueryValueExW(hkey, show_systray_valuename, 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS)
442 ret = IS_OPTION_FALSE(value[0]);
449 /* this function creates the listener window */
450 void initialize_systray(void)
453 static const WCHAR classname[] = /* Shell_TrayWnd */ {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
454 static const WCHAR winname[] = /* Wine Systray Listener */
455 {'W','i','n','e',' ','S','y','s','t','r','a','y',' ','L','i','s','t','e','n','e','r',0};
457 WINE_TRACE("initiaizing\n");
459 hide_systray = is_systray_hidden();
461 list_init(&tray.icons);
463 /* register the systray listener window class */
464 ZeroMemory(&class, sizeof(class));
465 class.cbSize = sizeof(class);
466 class.lpfnWndProc = &listener_wndproc;
467 class.hInstance = NULL;
468 class.hIcon = LoadIcon(0, IDI_WINLOGO);
469 class.hCursor = LoadCursor(0, IDC_ARROW);
470 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
471 class.lpszClassName = (WCHAR *) &classname;
473 if (!RegisterClassEx(&class))
475 WINE_ERR("Could not register SysTray window class\n");
479 /* now register the adaptor window class */
480 ZeroMemory(&class, sizeof(class));
481 class.cbSize = sizeof(class);
482 class.lpfnWndProc = adaptor_wndproc;
483 class.hInstance = NULL;
484 class.hIcon = LoadIcon(0, IDI_WINLOGO);
485 class.hCursor = LoadCursor(0, IDC_ARROW);
486 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
487 class.lpszClassName = adaptor_classname;
488 class.style = CS_SAVEBITS | CS_DBLCLKS;
490 if (!RegisterClassEx(&class))
492 WINE_ERR("Could not register adaptor class\n");
496 tray.window = CreateWindow(classname, winname, WS_OVERLAPPED,
497 CW_USEDEFAULT, CW_USEDEFAULT,
502 WINE_ERR("Could not create tray window\n");