ntdll: Use an __ms_va_list in sscanf (Clang).
[wine/multimedia.git] / dlls / winex11.drv / systray.c
blobc7e7013b1dd35584a590a014d2fa83eccdc7ea1c
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 #define NONAMELESSUNION
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "commctrl.h"
41 #include "shellapi.h"
43 #include "x11drv.h"
44 #include "wine/list.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(systray);
49 /* an individual systray icon */
50 struct tray_icon
52 struct list entry;
53 HICON image; /* the image to render */
54 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
55 HWND window; /* the adaptor window */
56 BOOL layered; /* whether we are using a layered window */
57 HWND tooltip; /* Icon tooltip */
58 UINT state; /* state flags */
59 UINT id; /* the unique id given by the app */
60 UINT callback_message;
61 int display; /* display index, or -1 if hidden */
62 WCHAR tiptext[128]; /* tooltip text */
63 WCHAR info_text[256]; /* info balloon text */
64 WCHAR info_title[64]; /* info balloon title */
65 UINT info_flags; /* flags for info balloon */
66 UINT info_timeout; /* timeout for info balloon */
67 HICON info_icon; /* info balloon icon */
70 static struct list icon_list = LIST_INIT( icon_list );
72 static const WCHAR icon_classname[] = {'_','_','w','i','n','e','x','1','1','_','t','r','a','y','_','i','c','o','n',0};
73 static const WCHAR tray_classname[] = {'_','_','w','i','n','e','x','1','1','_','s','t','a','n','d','a','l','o','n','e','_','t','r','a','y',0};
75 static BOOL show_icon( struct tray_icon *icon );
76 static BOOL hide_icon( struct tray_icon *icon );
77 static BOOL delete_icon( struct tray_icon *icon );
79 #define SYSTEM_TRAY_REQUEST_DOCK 0
80 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
81 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
83 Atom systray_atom = 0;
85 #define MIN_DISPLAYED 8
86 #define ICON_BORDER 2
88 #define BALLOON_CREATE_TIMER 1
89 #define BALLOON_SHOW_TIMER 2
91 #define BALLOON_CREATE_TIMEOUT 2000
92 #define BALLOON_SHOW_MIN_TIMEOUT 10000
93 #define BALLOON_SHOW_MAX_TIMEOUT 30000
95 static struct tray_icon *balloon_icon;
96 static HWND balloon_window;
97 static POINT balloon_pos;
99 /* stand-alone tray window */
100 static HWND standalone_tray;
101 static int icon_cx, icon_cy;
102 static unsigned int nb_displayed;
104 /* retrieves icon record by owner window and ID */
105 static struct tray_icon *get_icon(HWND owner, UINT id)
107 struct tray_icon *this;
109 LIST_FOR_EACH_ENTRY( this, &icon_list, struct tray_icon, entry )
110 if ((this->id == id) && (this->owner == owner)) return this;
111 return NULL;
114 static void init_common_controls(void)
116 static BOOL initialized = FALSE;
118 if (!initialized)
120 INITCOMMONCONTROLSEX init_tooltip;
122 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
123 init_tooltip.dwICC = ICC_TAB_CLASSES;
125 InitCommonControlsEx(&init_tooltip);
126 initialized = TRUE;
130 /* create tooltip window for icon */
131 static void create_tooltip(struct tray_icon *icon)
133 init_common_controls();
134 icon->tooltip = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
135 WS_POPUP | TTS_ALWAYSTIP,
136 CW_USEDEFAULT, CW_USEDEFAULT,
137 CW_USEDEFAULT, CW_USEDEFAULT,
138 icon->window, NULL, NULL, NULL);
139 if (icon->tooltip)
141 TTTOOLINFOW ti;
142 ZeroMemory(&ti, sizeof(ti));
143 ti.cbSize = sizeof(TTTOOLINFOW);
144 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
145 ti.hwnd = icon->window;
146 ti.uId = (UINT_PTR)icon->window;
147 ti.lpszText = icon->tiptext;
148 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
152 void update_systray_balloon_position(void)
154 RECT rect;
155 POINT pos;
157 if (!balloon_icon) return;
158 GetWindowRect( balloon_icon->window, &rect );
159 pos.x = (rect.left + rect.right) / 2;
160 pos.y = (rect.top + rect.bottom) / 2;
161 if (pos.x == balloon_pos.x && pos.y == balloon_pos.y) return; /* nothing changed */
162 balloon_pos = pos;
163 SendMessageW( balloon_window, TTM_TRACKPOSITION, 0, MAKELONG( pos.x, pos.y ));
166 static void balloon_create_timer( struct tray_icon *icon )
168 TTTOOLINFOW ti;
170 init_common_controls();
171 balloon_window = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
172 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE,
173 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
174 icon->window, NULL, NULL, NULL);
176 memset( &ti, 0, sizeof(ti) );
177 ti.cbSize = sizeof(TTTOOLINFOW);
178 ti.hwnd = icon->window;
179 ti.uId = (UINT_PTR)icon->window;
180 ti.uFlags = TTF_TRACK | TTF_IDISHWND;
181 ti.lpszText = icon->info_text;
182 SendMessageW( balloon_window, TTM_ADDTOOLW, 0, (LPARAM)&ti );
183 if ((icon->info_flags & NIIF_ICONMASK) == NIIF_USER)
184 SendMessageW( balloon_window, TTM_SETTITLEW, (WPARAM)icon->info_icon, (LPARAM)icon->info_title );
185 else
186 SendMessageW( balloon_window, TTM_SETTITLEW, icon->info_flags, (LPARAM)icon->info_title );
187 balloon_icon = icon;
188 balloon_pos.x = balloon_pos.y = MAXLONG;
189 update_systray_balloon_position();
190 SendMessageW( balloon_window, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti );
191 KillTimer( icon->window, BALLOON_CREATE_TIMER );
192 SetTimer( icon->window, BALLOON_SHOW_TIMER, icon->info_timeout, NULL );
195 static BOOL show_balloon( struct tray_icon *icon )
197 if (standalone_tray && !show_systray) return FALSE; /* no systray window */
198 if (!icon->window) return FALSE; /* not displayed */
199 if (!icon->info_text[0]) return FALSE; /* no balloon */
200 balloon_icon = icon;
201 SetTimer( icon->window, BALLOON_CREATE_TIMER, BALLOON_CREATE_TIMEOUT, NULL );
202 return TRUE;
205 static void hide_balloon(void)
207 if (!balloon_icon) return;
208 if (balloon_window)
210 KillTimer( balloon_icon->window, BALLOON_SHOW_TIMER );
211 DestroyWindow( balloon_window );
212 balloon_window = 0;
214 else KillTimer( balloon_icon->window, BALLOON_CREATE_TIMER );
215 balloon_icon = NULL;
218 static void show_next_balloon(void)
220 struct tray_icon *icon;
222 LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
223 if (show_balloon( icon )) break;
226 static void update_balloon( struct tray_icon *icon )
228 if (balloon_icon == icon)
230 hide_balloon();
231 show_balloon( icon );
233 else if (!balloon_icon)
235 if (!show_balloon( icon )) return;
237 if (!balloon_icon) show_next_balloon();
240 static void balloon_timer(void)
242 if (balloon_icon) balloon_icon->info_text[0] = 0; /* clear text now that balloon has been shown */
243 hide_balloon();
244 show_next_balloon();
247 /* synchronize tooltip text with tooltip window */
248 static void update_tooltip_text(struct tray_icon *icon)
250 TTTOOLINFOW ti;
252 ZeroMemory(&ti, sizeof(ti));
253 ti.cbSize = sizeof(TTTOOLINFOW);
254 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
255 ti.hwnd = icon->window;
256 ti.uId = (UINT_PTR)icon->window;
257 ti.lpszText = icon->tiptext;
259 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
262 /* get the size of the stand-alone tray window */
263 static SIZE get_window_size(void)
265 SIZE size;
266 RECT rect;
268 rect.left = 0;
269 rect.top = 0;
270 rect.right = icon_cx * max( nb_displayed, MIN_DISPLAYED );
271 rect.bottom = icon_cy;
272 AdjustWindowRect( &rect, WS_CAPTION, FALSE );
273 size.cx = rect.right - rect.left;
274 size.cy = rect.bottom - rect.top;
275 return size;
278 /* get the position of an icon in the stand-alone tray */
279 static POINT get_icon_pos( struct tray_icon *icon )
281 POINT pos;
283 pos.x = icon_cx * icon->display;
284 pos.y = 0;
285 return pos;
288 /* window procedure for the standalone tray window */
289 static LRESULT WINAPI standalone_tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
291 switch (msg)
293 case WM_MOVE:
294 update_systray_balloon_position();
295 break;
296 case WM_CLOSE:
297 ShowWindow( hwnd, SW_HIDE );
298 hide_balloon();
299 show_systray = FALSE;
300 return 0;
301 case WM_DESTROY:
302 standalone_tray = 0;
303 break;
305 return DefWindowProcW( hwnd, msg, wparam, lparam );
308 /* add an icon to the standalone tray window */
309 static void add_to_standalone_tray( struct tray_icon *icon )
311 SIZE size;
312 POINT pos;
314 if (!standalone_tray)
316 static const WCHAR winname[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
318 size = get_window_size();
319 standalone_tray = CreateWindowExW( 0, tray_classname, winname, WS_CAPTION | WS_SYSMENU,
320 CW_USEDEFAULT, CW_USEDEFAULT, size.cx, size.cy, 0, 0, 0, 0 );
321 if (!standalone_tray) return;
324 icon->display = nb_displayed;
325 pos = get_icon_pos( icon );
326 icon->window = CreateWindowW( icon_classname, NULL, WS_CHILD | WS_VISIBLE,
327 pos.x, pos.y, icon_cx, icon_cy, standalone_tray, NULL, NULL, icon );
328 if (!icon->window)
330 icon->display = -1;
331 return;
333 create_tooltip( icon );
335 nb_displayed++;
336 size = get_window_size();
337 SetWindowPos( standalone_tray, 0, 0, 0, size.cx, size.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
338 if (nb_displayed == 1 && show_systray) ShowWindow( standalone_tray, SW_SHOWNA );
339 TRACE( "added %u now %d icons\n", icon->id, nb_displayed );
342 /* remove an icon from the stand-alone tray */
343 static void remove_from_standalone_tray( struct tray_icon *icon )
345 struct tray_icon *ptr;
346 POINT pos;
348 if (icon->display == -1) return;
350 LIST_FOR_EACH_ENTRY( ptr, &icon_list, struct tray_icon, entry )
352 if (ptr == icon) continue;
353 if (ptr->display < icon->display) continue;
354 ptr->display--;
355 pos = get_icon_pos( ptr );
356 SetWindowPos( ptr->window, 0, pos.x, pos.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
358 icon->display = -1;
359 if (!--nb_displayed) ShowWindow( standalone_tray, SW_HIDE );
360 TRACE( "removed %u now %d icons\n", icon->id, nb_displayed );
363 static void repaint_tray_icon( struct tray_icon *icon )
365 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
366 int width = GetSystemMetrics( SM_CXSMICON );
367 int height = GetSystemMetrics( SM_CYSMICON );
368 BITMAPINFO *info;
369 HBITMAP dib, mask;
370 HDC hdc;
371 RECT rc;
372 SIZE size;
373 POINT pos;
374 int i, x, y;
375 void *color_bits, *mask_bits;
376 DWORD *ptr;
377 BOOL has_alpha = FALSE;
379 GetWindowRect( icon->window, &rc );
380 size.cx = rc.right - rc.left;
381 size.cy = rc.bottom - rc.top;
382 pos.x = (size.cx - width) / 2;
383 pos.y = (size.cy - height) / 2;
385 info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ));
386 if (!info) return;
387 info->bmiHeader.biSize = sizeof(info->bmiHeader);
388 info->bmiHeader.biWidth = size.cx;
389 info->bmiHeader.biHeight = size.cy;
390 info->bmiHeader.biBitCount = 32;
391 info->bmiHeader.biPlanes = 1;
392 info->bmiHeader.biCompression = BI_RGB;
394 hdc = CreateCompatibleDC( 0 );
395 if (!(dib = CreateDIBSection( 0, info, DIB_RGB_COLORS, &color_bits, NULL, 0 ))) goto done;
396 SelectObject( hdc, dib );
397 DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_NORMAL );
399 /* check if the icon was drawn with an alpha channel */
400 for (i = 0, ptr = color_bits; i < size.cx * size.cy; i++)
401 if ((has_alpha = (ptr[i] & 0xff000000) != 0)) break;
403 if (!has_alpha)
405 unsigned int width_bytes = (size.cx + 31) / 32 * 4;
407 info->bmiHeader.biBitCount = 1;
408 info->bmiColors[0].rgbRed = 0;
409 info->bmiColors[0].rgbGreen = 0;
410 info->bmiColors[0].rgbBlue = 0;
411 info->bmiColors[0].rgbReserved = 0;
412 info->bmiColors[1].rgbRed = 0xff;
413 info->bmiColors[1].rgbGreen = 0xff;
414 info->bmiColors[1].rgbBlue = 0xff;
415 info->bmiColors[1].rgbReserved = 0;
417 if (!(mask = CreateDIBSection( 0, info, DIB_RGB_COLORS, &mask_bits, NULL, 0 ))) goto done;
418 memset( mask_bits, 0xff, width_bytes * size.cy );
419 SelectObject( hdc, mask );
420 DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_MASK );
422 for (y = 0, ptr = color_bits; y < size.cy; y++)
423 for (x = 0; x < size.cx; x++, ptr++)
424 if (!((((BYTE *)mask_bits)[y * width_bytes + x / 8] << (x % 8)) & 0x80))
425 *ptr |= 0xff000000;
427 SelectObject( hdc, dib );
428 DeleteObject( mask );
431 UpdateLayeredWindow( icon->window, 0, NULL, NULL, hdc, NULL, 0, &blend, ULW_ALPHA );
432 done:
433 HeapFree (GetProcessHeap(), 0, info);
434 if (hdc) DeleteDC( hdc );
435 if (dib) DeleteObject( dib );
438 /* window procedure for the individual tray icon window */
439 static LRESULT WINAPI tray_icon_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
441 struct tray_icon *icon = NULL;
442 BOOL ret;
444 TRACE("hwnd=%p, msg=0x%x\n", hwnd, msg);
446 /* set the icon data for the window from the data passed into CreateWindow */
447 if (msg == WM_NCCREATE)
448 SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LPARAM)((const CREATESTRUCTW *)lparam)->lpCreateParams);
450 icon = (struct tray_icon *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
452 switch (msg)
454 case WM_SIZE:
455 if (icon->window && icon->layered) repaint_tray_icon( icon );
456 break;
458 case WM_PAINT:
459 if (!icon->layered)
461 PAINTSTRUCT ps;
462 RECT rc;
463 HDC hdc;
464 int cx = GetSystemMetrics( SM_CXSMICON );
465 int cy = GetSystemMetrics( SM_CYSMICON );
467 hdc = BeginPaint(hwnd, &ps);
468 GetClientRect(hwnd, &rc);
469 TRACE("painting rect %s\n", wine_dbgstr_rect(&rc));
470 DrawIconEx( hdc, (rc.left + rc.right - cx) / 2, (rc.top + rc.bottom - cy) / 2,
471 icon->image, cx, cy, 0, 0, DI_DEFAULTSIZE|DI_NORMAL );
472 EndPaint(hwnd, &ps);
473 return 0;
475 break;
477 case WM_MOUSEMOVE:
478 case WM_LBUTTONDOWN:
479 case WM_LBUTTONUP:
480 case WM_RBUTTONDOWN:
481 case WM_RBUTTONUP:
482 case WM_MBUTTONDOWN:
483 case WM_MBUTTONUP:
484 case WM_LBUTTONDBLCLK:
485 case WM_RBUTTONDBLCLK:
486 case WM_MBUTTONDBLCLK:
487 /* notify the owner hwnd of the message */
488 TRACE("relaying 0x%x\n", msg);
489 ret = PostMessageW(icon->owner, icon->callback_message, icon->id, msg);
490 if (!ret && (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
492 WARN( "application window was destroyed, removing icon %u\n", icon->id );
493 delete_icon( icon );
495 return 0;
497 case WM_WINDOWPOSCHANGED:
498 update_systray_balloon_position();
499 break;
501 case WM_TIMER:
502 switch (wparam)
504 case BALLOON_CREATE_TIMER:
505 balloon_create_timer( icon );
506 break;
507 case BALLOON_SHOW_TIMER:
508 balloon_timer();
509 break;
511 return 0;
513 case WM_CLOSE:
514 if (icon->display == -1)
516 TRACE( "icon %u no longer embedded\n", icon->id );
517 hide_icon( icon );
518 add_to_standalone_tray( icon );
520 return 0;
522 return DefWindowProcW( hwnd, msg, wparam, lparam );
525 /* find the X11 window owner the system tray selection */
526 static Window get_systray_selection_owner( Display *display )
528 return XGetSelectionOwner( display, systray_atom );
531 static void get_systray_visual_info( Display *display, Window systray_window, XVisualInfo *info )
533 XVisualInfo *list, template;
534 VisualID *visual_id;
535 Atom type;
536 int format, num;
537 unsigned long count, remaining;
539 *info = default_visual;
540 if (XGetWindowProperty( display, systray_window, x11drv_atom(_NET_SYSTEM_TRAY_VISUAL), 0,
541 65536/sizeof(CARD32), False, XA_VISUALID, &type, &format, &count,
542 &remaining, (unsigned char **)&visual_id ))
543 return;
545 if (type == XA_VISUALID && format == 32)
547 template.visualid = visual_id[0];
548 if ((list = XGetVisualInfo( display, VisualIDMask, &template, &num )))
550 *info = list[0];
551 TRACE( "systray window %lx got visual %lx\n", systray_window, info->visualid );
552 XFree( list );
555 XFree( visual_id );
558 static BOOL init_systray(void)
560 static BOOL init_done;
561 WNDCLASSEXW class;
562 Display *display;
564 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
565 if (init_done) return TRUE;
567 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2 * ICON_BORDER;
568 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2 * ICON_BORDER;
570 memset( &class, 0, sizeof(class) );
571 class.cbSize = sizeof(class);
572 class.lpfnWndProc = tray_icon_wndproc;
573 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
574 class.hCursor = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
575 class.lpszClassName = icon_classname;
576 class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
578 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
580 ERR( "Could not register icon tray window class\n" );
581 return FALSE;
584 class.lpfnWndProc = standalone_tray_wndproc;
585 class.hbrBackground = (HBRUSH)COLOR_WINDOW;
586 class.lpszClassName = tray_classname;
587 class.style = CS_DBLCLKS;
589 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
591 ERR( "Could not register standalone tray window class\n" );
592 return FALSE;
595 display = thread_init_display();
596 if (DefaultScreen( display ) == 0)
597 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
598 else
600 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
601 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
602 systray_atom = XInternAtom( display, systray_buffer, False );
604 XSelectInput( display, root_window, StructureNotifyMask );
606 init_done = TRUE;
607 return TRUE;
610 /* dock the given icon with the NETWM system tray */
611 static void dock_systray_icon( Display *display, struct tray_icon *icon, Window systray_window )
613 Window window;
614 XEvent ev;
615 XSetWindowAttributes attr;
616 XVisualInfo visual;
617 struct x11drv_win_data *data;
619 get_systray_visual_info( display, systray_window, &visual );
621 icon->layered = (visual.visualid != default_visual.visualid);
622 icon->window = CreateWindowExW( icon->layered ? WS_EX_LAYERED : 0,
623 icon_classname, NULL, WS_CLIPSIBLINGS | WS_POPUP,
624 CW_USEDEFAULT, CW_USEDEFAULT, icon_cx, icon_cy,
625 NULL, NULL, NULL, icon );
627 if (!(data = get_win_data( icon->window ))) return;
628 if (icon->layered) set_window_visual( data, &visual );
629 make_window_embedded( data );
630 window = data->whole_window;
631 release_win_data( data );
633 create_tooltip( icon );
634 ShowWindow( icon->window, SW_SHOWNA );
636 TRACE( "icon window %p/%lx\n", icon->window, window );
638 /* send the docking request message */
639 ev.xclient.type = ClientMessage;
640 ev.xclient.window = systray_window;
641 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
642 ev.xclient.format = 32;
643 ev.xclient.data.l[0] = CurrentTime;
644 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
645 ev.xclient.data.l[2] = window;
646 ev.xclient.data.l[3] = 0;
647 ev.xclient.data.l[4] = 0;
648 XSendEvent( display, systray_window, False, NoEventMask, &ev );
650 if (!icon->layered)
652 attr.background_pixmap = ParentRelative;
653 attr.bit_gravity = ForgetGravity;
654 XChangeWindowAttributes( display, window, CWBackPixmap | CWBitGravity, &attr );
656 else repaint_tray_icon( icon );
659 /* dock systray windows again with the new owner */
660 void change_systray_owner( Display *display, Window systray_window )
662 struct tray_icon *icon;
664 TRACE( "new owner %lx\n", systray_window );
665 LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
667 if (icon->display == -1) continue;
668 hide_icon( icon );
669 dock_systray_icon( display, icon, systray_window );
673 /* hide a tray icon */
674 static BOOL hide_icon( struct tray_icon *icon )
676 struct x11drv_win_data *data;
678 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
680 if (!icon->window) return TRUE; /* already hidden */
682 /* make sure we don't try to unmap it, it confuses some systray docks */
683 if ((data = get_win_data( icon->window )))
685 if (data->embedded) data->mapped = FALSE;
686 release_win_data( data );
688 DestroyWindow(icon->window);
689 DestroyWindow(icon->tooltip);
690 icon->window = 0;
691 icon->layered = FALSE;
692 icon->tooltip = 0;
693 remove_from_standalone_tray( icon );
694 update_balloon( icon );
695 return TRUE;
698 /* make the icon visible */
699 static BOOL show_icon( struct tray_icon *icon )
701 Window systray_window;
702 Display *display = thread_init_display();
704 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
706 if (icon->window) return TRUE; /* already shown */
708 if ((systray_window = get_systray_selection_owner( display )))
709 dock_systray_icon( display, icon, systray_window );
710 else
711 add_to_standalone_tray( icon );
713 update_balloon( icon );
714 return TRUE;
717 /* Modifies an existing icon record */
718 static BOOL modify_icon( struct tray_icon *icon, NOTIFYICONDATAW *nid )
720 TRACE( "id=0x%x hwnd=%p flags=%x\n", nid->uID, nid->hWnd, nid->uFlags );
722 if (nid->uFlags & NIF_STATE)
724 icon->state = (icon->state & ~nid->dwStateMask) | (nid->dwState & nid->dwStateMask);
727 if (nid->uFlags & NIF_ICON)
729 if (icon->image) DestroyIcon(icon->image);
730 icon->image = CopyIcon(nid->hIcon);
731 if (icon->window)
733 if (icon->display != -1) InvalidateRect( icon->window, NULL, TRUE );
734 else if (icon->layered) repaint_tray_icon( icon );
735 else
737 Window win = X11DRV_get_whole_window( icon->window );
738 if (win) XClearArea( gdi_display, win, 0, 0, 0, 0, True );
743 if (nid->uFlags & NIF_MESSAGE)
745 icon->callback_message = nid->uCallbackMessage;
747 if (nid->uFlags & NIF_TIP)
749 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
750 if (icon->tooltip) update_tooltip_text(icon);
752 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
754 lstrcpynW( icon->info_text, nid->szInfo, sizeof(icon->info_text)/sizeof(WCHAR) );
755 lstrcpynW( icon->info_title, nid->szInfoTitle, sizeof(icon->info_title)/sizeof(WCHAR) );
756 icon->info_flags = nid->dwInfoFlags;
757 icon->info_timeout = max(min(nid->u.uTimeout, BALLOON_SHOW_MAX_TIMEOUT), BALLOON_SHOW_MIN_TIMEOUT);
758 icon->info_icon = nid->hBalloonIcon;
759 update_balloon( icon );
761 if (icon->state & NIS_HIDDEN) hide_icon( icon );
762 else show_icon( icon );
763 return TRUE;
766 /* Adds a new icon record to the list */
767 static BOOL add_icon(NOTIFYICONDATAW *nid)
769 struct tray_icon *icon;
771 TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
773 if ((icon = get_icon(nid->hWnd, nid->uID)))
775 WARN("duplicate tray icon add, buggy app?\n");
776 return FALSE;
779 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
781 ERR("out of memory\n");
782 return FALSE;
785 ZeroMemory(icon, sizeof(struct tray_icon));
786 icon->id = nid->uID;
787 icon->owner = nid->hWnd;
788 icon->display = -1;
790 list_add_tail(&icon_list, &icon->entry);
792 return modify_icon( icon, nid );
795 /* delete tray icon window and icon structure */
796 static BOOL delete_icon( struct tray_icon *icon )
798 hide_icon( icon );
799 list_remove( &icon->entry );
800 DestroyIcon( icon->image );
801 HeapFree( GetProcessHeap(), 0, icon );
802 return TRUE;
805 /* cleanup all icons for a given window */
806 static void cleanup_icons( HWND owner )
808 struct tray_icon *this, *next;
810 LIST_FOR_EACH_ENTRY_SAFE( this, next, &icon_list, struct tray_icon, entry )
811 if (this->owner == owner) delete_icon( this );
815 /***********************************************************************
816 * wine_notify_icon (X11DRV.@)
818 * Driver-side implementation of Shell_NotifyIcon.
820 int CDECL wine_notify_icon( DWORD msg, NOTIFYICONDATAW *data )
822 BOOL ret = FALSE;
823 struct tray_icon *icon;
825 switch (msg)
827 case NIM_ADD:
828 if (!init_systray()) return -1; /* fall back to default handling */
829 ret = add_icon( data );
830 break;
831 case NIM_DELETE:
832 if ((icon = get_icon( data->hWnd, data->uID ))) ret = delete_icon( icon );
833 break;
834 case NIM_MODIFY:
835 if ((icon = get_icon( data->hWnd, data->uID ))) ret = modify_icon( icon, data );
836 break;
837 case 0xdead: /* Wine extension: owner window has died */
838 cleanup_icons( data->hWnd );
839 break;
840 default:
841 FIXME( "unhandled tray message: %u\n", msg );
842 break;
844 return ret;