winex11: Move all the embedded systray window support to winex11.drv.
[wine/gsoc_dplay.git] / dlls / winex11.drv / systray.c
blob9deb003e17c0523684e4b639e21f411ac91feb23
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 default:
189 return DefWindowProcW(hwnd, msg, wparam, lparam);
191 return 0;
194 /* find the X11 window owner the system tray selection */
195 static Window get_systray_selection_owner( Display *display )
197 static Atom systray_atom;
198 Window ret;
200 if (root_window != DefaultRootWindow( display )) return 0;
202 wine_tsx11_lock();
203 if (!systray_atom)
205 if (DefaultScreen( display ) == 0)
206 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
207 else
209 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
210 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
211 systray_atom = XInternAtom( display, systray_buffer, False );
214 ret = XGetSelectionOwner( display, systray_atom );
215 wine_tsx11_unlock();
216 return ret;
220 /* dock the given X window with the NETWM system tray */
221 static void dock_systray_window( HWND hwnd, Window systray_window )
223 Display *display = thread_display();
224 struct x11drv_win_data *data;
225 XEvent ev;
226 unsigned long info[2];
228 if (!(data = X11DRV_get_win_data( hwnd )) &&
229 !(data = X11DRV_create_win_data( hwnd ))) return;
231 TRACE( "icon window %p/%lx managed %u\n", data->hwnd, data->whole_window, data->managed );
233 /* the window _cannot_ be mapped if we intend to dock with an XEMBED tray */
234 assert( !data->mapped );
236 /* set XEMBED protocol data on the window */
237 info[0] = 0; /* protocol version */
238 info[1] = XEMBED_MAPPED; /* flags */
240 wine_tsx11_lock();
241 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
242 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
244 /* send the docking request message */
245 ev.xclient.type = ClientMessage;
246 ev.xclient.window = systray_window;
247 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
248 ev.xclient.format = 32;
249 ev.xclient.data.l[0] = CurrentTime;
250 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
251 ev.xclient.data.l[2] = data->whole_window;
252 ev.xclient.data.l[3] = 0;
253 ev.xclient.data.l[4] = 0;
254 XSendEvent( display, systray_window, False, NoEventMask, &ev );
255 wine_tsx11_unlock();
257 data->mapped = TRUE;
258 data->wm_state = NormalState;
262 /* hide a tray icon */
263 static BOOL hide_icon( struct tray_icon *icon )
265 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
267 if (!icon->window || icon->hidden) return TRUE; /* already hidden */
269 if (icon->window)
271 DestroyWindow(icon->window);
272 DestroyWindow(icon->tooltip);
274 icon->hidden = TRUE;
275 return TRUE;
278 /* make the icon visible */
279 static BOOL show_icon( struct tray_icon *icon )
281 RECT rect;
282 static BOOL class_registered;
283 Window systray_window;
285 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
287 if (icon->window && !icon->hidden) return TRUE; /* already shown */
289 if (!class_registered)
291 WNDCLASSEXW class;
293 ZeroMemory( &class, sizeof(class) );
294 class.cbSize = sizeof(class);
295 class.lpfnWndProc = tray_wndproc;
296 class.hCursor = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
297 class.lpszClassName = tray_classname;
298 class.hbrBackground = (HBRUSH)COLOR_WINDOW;
299 class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
301 if (!RegisterClassExW(&class) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
303 WINE_ERR( "Could not register tray window class\n" );
304 return FALSE;
306 class_registered = TRUE;
309 if (!(systray_window = get_systray_selection_owner( thread_display() ))) return FALSE;
311 rect.left = 0;
312 rect.top = 0;
313 rect.right = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
314 rect.bottom = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
316 icon->window = CreateWindowExW( WS_EX_APPWINDOW, tray_classname, NULL, WS_CLIPSIBLINGS | WS_POPUP,
317 CW_USEDEFAULT, CW_USEDEFAULT,
318 rect.right - rect.left, rect.bottom - rect.top,
319 NULL, NULL, NULL, icon );
320 create_tooltip( icon );
321 dock_systray_window( icon->window, systray_window );
322 ShowWindow( icon->window, SW_SHOWNA );
323 icon->hidden = FALSE;
324 return TRUE;
327 /* Modifies an existing icon record */
328 static BOOL modify_icon( struct tray_icon *icon, NOTIFYICONDATAW *nid )
330 TRACE( "id=0x%x hwnd=%p flags=%x\n", nid->uID, nid->hWnd, nid->uFlags );
332 if ((nid->uFlags & NIF_STATE) && (nid->dwStateMask & NIS_HIDDEN))
334 if (nid->dwState & NIS_HIDDEN) hide_icon( icon );
335 else show_icon( icon );
338 /* startup case*/
339 if (!icon->window && !icon->hidden) show_icon( icon );
341 if (nid->uFlags & NIF_ICON)
343 if (icon->image) DestroyIcon(icon->image);
344 icon->image = CopyIcon(nid->hIcon);
346 if (!icon->hidden)
347 RedrawWindow(icon->window, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW);
350 if (nid->uFlags & NIF_MESSAGE)
352 icon->callback_message = nid->uCallbackMessage;
354 if (nid->uFlags & NIF_TIP)
356 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
357 if (!icon->hidden)
358 update_tooltip_text(icon);
360 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
362 FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid->szInfoTitle), wine_dbgstr_w(nid->szInfo));
364 return TRUE;
367 /* Adds a new icon record to the list */
368 static BOOL add_icon(NOTIFYICONDATAW *nid)
370 struct tray_icon *icon;
372 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
374 if ((icon = get_icon(nid->hWnd, nid->uID)))
376 WINE_WARN("duplicate tray icon add, buggy app?\n");
377 return FALSE;
380 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
382 WINE_ERR("out of memory\n");
383 return FALSE;
386 ZeroMemory(icon, sizeof(struct tray_icon));
387 icon->id = nid->uID;
388 icon->owner = nid->hWnd;
390 list_add_tail(&icon_list, &icon->entry);
393 * Both icon->window and icon->hidden are zero. modify_icon function
394 * will treat this case as a startup, i.e. icon window will be created if
395 * NIS_HIDDEN flag is not set.
398 return modify_icon( icon, nid );
401 /* delete tray icon window and icon structure */
402 static BOOL delete_icon( struct tray_icon *icon )
404 hide_icon( icon );
405 list_remove( &icon->entry );
406 DestroyIcon( icon->image );
407 HeapFree( GetProcessHeap(), 0, icon );
408 return TRUE;
412 /***********************************************************************
413 * wine_notify_icon (X11DRV.@)
415 * Driver-side implementation of Shell_NotifyIcon.
417 BOOL wine_notify_icon( DWORD msg, NOTIFYICONDATAW *data )
419 BOOL ret = FALSE;
420 struct tray_icon *icon;
421 Window owner;
423 switch (msg)
425 case NIM_ADD:
426 if ((owner = get_systray_selection_owner( thread_display() ))) ret = add_icon( data );
427 break;
428 case NIM_DELETE:
429 if ((icon = get_icon( data->hWnd, data->uID ))) ret = delete_icon( icon );
430 break;
431 case NIM_MODIFY:
432 if ((icon = get_icon( data->hWnd, data->uID ))) ret = modify_icon( icon, data );
433 break;
434 default:
435 FIXME( "unhandled tray message: %u\n", msg );
436 break;
438 return ret;