winex11: Add a timer to detect when a systray owner has been destroyed.
[wine/hacks.git] / dlls / winex11.drv / systray.c
blob347e5a763f7d25b60eaf1d0a0de6460f0c22ba05
1 /*
2 * X11 system tray management
4 * Copyright (C) 2004 Mike Hearn, for CodeWeavers
5 * Copyright (C) 2005 Robert Shearman
6 * Copyright (C) 2008 Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
33 #include <X11/Xlib.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "commctrl.h"
40 #include "shellapi.h"
42 #include "x11drv.h"
43 #include "wine/list.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(systray);
48 /* an individual systray icon */
49 struct tray_icon
51 struct list entry;
52 HICON image; /* the image to render */
53 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
54 HWND window; /* the adaptor window */
55 HWND tooltip; /* Icon tooltip */
56 UINT id; /* the unique id given by the app */
57 UINT callback_message;
58 BOOL hidden; /* icon display state */
59 WCHAR tiptext[128]; /* Tooltip text. If empty => tooltip disabled */
62 static struct list icon_list = LIST_INIT( icon_list );
64 static const WCHAR tray_classname[] = {'_','_','w','i','n','e','x','1','1','_','t','r','a','y','_','w','i','n','d','o','w',0};
66 static BOOL delete_icon( struct tray_icon *icon );
68 #define SYSTEM_TRAY_REQUEST_DOCK 0
69 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
70 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
72 #define XEMBED_MAPPED (1 << 0)
74 #define ICON_BORDER 2
76 /* retrieves icon record by owner window and ID */
77 static struct tray_icon *get_icon(HWND owner, UINT id)
79 struct tray_icon *this;
81 LIST_FOR_EACH_ENTRY( this, &icon_list, struct tray_icon, entry )
82 if ((this->id == id) && (this->owner == owner)) return this;
83 return NULL;
86 /* create tooltip window for icon */
87 static void create_tooltip(struct tray_icon *icon)
89 static BOOL tooltips_initialized = FALSE;
91 if (!tooltips_initialized)
93 INITCOMMONCONTROLSEX init_tooltip;
95 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
96 init_tooltip.dwICC = ICC_TAB_CLASSES;
98 InitCommonControlsEx(&init_tooltip);
99 tooltips_initialized = TRUE;
102 icon->tooltip = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
103 WS_POPUP | TTS_ALWAYSTIP,
104 CW_USEDEFAULT, CW_USEDEFAULT,
105 CW_USEDEFAULT, CW_USEDEFAULT,
106 icon->window, NULL, NULL, NULL);
107 if (icon->tooltip)
109 TTTOOLINFOW ti;
110 ZeroMemory(&ti, sizeof(ti));
111 ti.cbSize = sizeof(TTTOOLINFOW);
112 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
113 ti.hwnd = icon->window;
114 ti.uId = (UINT_PTR)icon->window;
115 ti.lpszText = icon->tiptext;
116 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
120 /* synchronize tooltip text with tooltip window */
121 static void update_tooltip_text(struct tray_icon *icon)
123 TTTOOLINFOW ti;
125 ZeroMemory(&ti, sizeof(ti));
126 ti.cbSize = sizeof(TTTOOLINFOW);
127 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
128 ti.hwnd = icon->window;
129 ti.uId = (UINT_PTR)icon->window;
130 ti.lpszText = icon->tiptext;
132 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
135 /* window procedure for the tray window */
136 static LRESULT WINAPI tray_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
138 struct tray_icon *icon = NULL;
139 BOOL ret;
141 WINE_TRACE("hwnd=%p, msg=0x%x\n", hwnd, msg);
143 /* set the icon data for the window from the data passed into CreateWindow */
144 if (msg == WM_NCCREATE)
145 SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LPARAM)((const CREATESTRUCTW *)lparam)->lpCreateParams);
147 icon = (struct tray_icon *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
149 switch (msg)
151 case WM_PAINT:
153 PAINTSTRUCT ps;
154 RECT rc;
155 HDC hdc;
156 int cx = GetSystemMetrics( SM_CXSMICON );
157 int cy = GetSystemMetrics( SM_CYSMICON );
159 hdc = BeginPaint(hwnd, &ps);
160 GetClientRect(hwnd, &rc);
161 TRACE("painting rect %s\n", wine_dbgstr_rect(&rc));
162 DrawIconEx( hdc, (rc.left + rc.right - cx) / 2, (rc.top + rc.bottom - cy) / 2,
163 icon->image, cx, cy, 0, 0, DI_DEFAULTSIZE|DI_NORMAL );
164 EndPaint(hwnd, &ps);
165 break;
168 case WM_MOUSEMOVE:
169 case WM_LBUTTONDOWN:
170 case WM_LBUTTONUP:
171 case WM_RBUTTONDOWN:
172 case WM_RBUTTONUP:
173 case WM_MBUTTONDOWN:
174 case WM_MBUTTONUP:
175 case WM_LBUTTONDBLCLK:
176 case WM_RBUTTONDBLCLK:
177 case WM_MBUTTONDBLCLK:
178 /* notify the owner hwnd of the message */
179 TRACE("relaying 0x%x\n", msg);
180 ret = PostMessageW(icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg);
181 if (!ret && (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
183 WARN( "application window was destroyed, removing icon %u\n", icon->id );
184 delete_icon( icon );
186 break;
188 case WM_TIMER:
189 if (!IsWindow( icon->owner )) delete_icon( icon );
190 return 0;
192 default:
193 return DefWindowProcW(hwnd, msg, wparam, lparam);
195 return 0;
198 /* find the X11 window owner the system tray selection */
199 static Window get_systray_selection_owner( Display *display )
201 static Atom systray_atom;
202 Window ret;
204 if (root_window != DefaultRootWindow( display )) return 0;
206 wine_tsx11_lock();
207 if (!systray_atom)
209 if (DefaultScreen( display ) == 0)
210 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
211 else
213 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
214 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
215 systray_atom = XInternAtom( display, systray_buffer, False );
218 ret = XGetSelectionOwner( display, systray_atom );
219 wine_tsx11_unlock();
220 return ret;
224 /* dock the given X window with the NETWM system tray */
225 static void dock_systray_window( HWND hwnd, Window systray_window )
227 Display *display = thread_display();
228 struct x11drv_win_data *data;
229 XEvent ev;
230 XSetWindowAttributes attr;
231 unsigned long info[2];
233 if (!(data = X11DRV_get_win_data( hwnd )) &&
234 !(data = X11DRV_create_win_data( hwnd ))) return;
236 TRACE( "icon window %p/%lx managed %u\n", data->hwnd, data->whole_window, data->managed );
238 /* the window _cannot_ be mapped if we intend to dock with an XEMBED tray */
239 assert( !data->mapped );
241 /* set XEMBED protocol data on the window */
242 info[0] = 0; /* protocol version */
243 info[1] = XEMBED_MAPPED; /* flags */
245 wine_tsx11_lock();
246 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
247 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
249 /* send the docking request message */
250 ev.xclient.type = ClientMessage;
251 ev.xclient.window = systray_window;
252 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
253 ev.xclient.format = 32;
254 ev.xclient.data.l[0] = CurrentTime;
255 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
256 ev.xclient.data.l[2] = data->whole_window;
257 ev.xclient.data.l[3] = 0;
258 ev.xclient.data.l[4] = 0;
259 XSendEvent( display, systray_window, False, NoEventMask, &ev );
260 attr.background_pixmap = ParentRelative;
261 attr.bit_gravity = ForgetGravity;
262 XChangeWindowAttributes( display, data->whole_window, CWBackPixmap | CWBitGravity, &attr );
263 XChangeWindowAttributes( display, data->client_window, CWBackPixmap | CWBitGravity, &attr );
264 wine_tsx11_unlock();
266 data->mapped = TRUE;
267 data->wm_state = NormalState;
271 /* hide a tray icon */
272 static BOOL hide_icon( struct tray_icon *icon )
274 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
276 if (!icon->window || icon->hidden) return TRUE; /* already hidden */
278 if (icon->window)
280 DestroyWindow(icon->window);
281 DestroyWindow(icon->tooltip);
283 icon->hidden = TRUE;
284 return TRUE;
287 /* make the icon visible */
288 static BOOL show_icon( struct tray_icon *icon )
290 RECT rect;
291 static BOOL class_registered;
292 Window systray_window;
294 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
296 if (icon->window && !icon->hidden) return TRUE; /* already shown */
298 if (!class_registered)
300 WNDCLASSEXW class;
302 ZeroMemory( &class, sizeof(class) );
303 class.cbSize = sizeof(class);
304 class.lpfnWndProc = tray_wndproc;
305 class.hCursor = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
306 class.lpszClassName = tray_classname;
307 class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
309 if (!RegisterClassExW(&class) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
311 WINE_ERR( "Could not register tray window class\n" );
312 return FALSE;
314 class_registered = TRUE;
317 if (!(systray_window = get_systray_selection_owner( thread_display() ))) return FALSE;
319 rect.left = 0;
320 rect.top = 0;
321 rect.right = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
322 rect.bottom = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
324 icon->window = CreateWindowExW( WS_EX_APPWINDOW, tray_classname, NULL, WS_CLIPSIBLINGS | WS_POPUP,
325 CW_USEDEFAULT, CW_USEDEFAULT,
326 rect.right - rect.left, rect.bottom - rect.top,
327 NULL, NULL, NULL, icon );
328 create_tooltip( icon );
329 dock_systray_window( icon->window, systray_window );
330 SetTimer( icon->window, 1, 1000, NULL );
331 ShowWindow( icon->window, SW_SHOWNA );
332 icon->hidden = FALSE;
333 return TRUE;
336 /* Modifies an existing icon record */
337 static BOOL modify_icon( struct tray_icon *icon, NOTIFYICONDATAW *nid )
339 TRACE( "id=0x%x hwnd=%p flags=%x\n", nid->uID, nid->hWnd, nid->uFlags );
341 if ((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))
343 if (nid->dwState & NIS_HIDDEN) hide_icon( icon );
344 else show_icon( icon );
347 /* startup case*/
348 if (!icon->window && !icon->hidden) show_icon( icon );
350 if (nid->uFlags & NIF_ICON)
352 if (icon->image) DestroyIcon(icon->image);
353 icon->image = CopyIcon(nid->hIcon);
355 if (!icon->hidden)
357 struct x11drv_win_data *data = X11DRV_get_win_data( icon->window );
358 if (data) XClearArea( gdi_display, data->client_window, 0, 0, 0, 0, True );
362 if (nid->uFlags & NIF_MESSAGE)
364 icon->callback_message = nid->uCallbackMessage;
366 if (nid->uFlags & NIF_TIP)
368 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
369 if (!icon->hidden)
370 update_tooltip_text(icon);
372 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
374 FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid->szInfoTitle), wine_dbgstr_w(nid->szInfo));
376 return TRUE;
379 /* Adds a new icon record to the list */
380 static BOOL add_icon(NOTIFYICONDATAW *nid)
382 struct tray_icon *icon;
384 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
386 if ((icon = get_icon(nid->hWnd, nid->uID)))
388 WINE_WARN("duplicate tray icon add, buggy app?\n");
389 return FALSE;
392 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
394 WINE_ERR("out of memory\n");
395 return FALSE;
398 ZeroMemory(icon, sizeof(struct tray_icon));
399 icon->id = nid->uID;
400 icon->owner = nid->hWnd;
402 list_add_tail(&icon_list, &icon->entry);
405 * Both icon->window and icon->hidden are zero. modify_icon function
406 * will treat this case as a startup, i.e. icon window will be created if
407 * NIS_HIDDEN flag is not set.
410 return modify_icon( icon, nid );
413 /* delete tray icon window and icon structure */
414 static BOOL delete_icon( struct tray_icon *icon )
416 hide_icon( icon );
417 list_remove( &icon->entry );
418 DestroyIcon( icon->image );
419 HeapFree( GetProcessHeap(), 0, icon );
420 return TRUE;
424 /***********************************************************************
425 * wine_notify_icon (X11DRV.@)
427 * Driver-side implementation of Shell_NotifyIcon.
429 BOOL wine_notify_icon( DWORD msg, NOTIFYICONDATAW *data )
431 BOOL ret = FALSE;
432 struct tray_icon *icon;
433 Window owner;
435 switch (msg)
437 case NIM_ADD:
438 if ((owner = get_systray_selection_owner( thread_display() ))) ret = add_icon( data );
439 break;
440 case NIM_DELETE:
441 if ((icon = get_icon( data->hWnd, data->uID ))) ret = delete_icon( icon );
442 break;
443 case NIM_MODIFY:
444 if ((icon = get_icon( data->hWnd, data->uID ))) ret = modify_icon( icon, data );
445 break;
446 default:
447 FIXME( "unhandled tray message: %u\n", msg );
448 break;
450 return ret;