mfplay: Add support for same-thread event callback.
[wine.git] / dlls / winex11.drv / systray.c
blob5522b229836dcd18d13724a31e97178edd34b27e
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 */
68 UINT version; /* notify icon api version */
71 static struct list icon_list = LIST_INIT( icon_list );
73 static const WCHAR icon_classname[] = {'_','_','w','i','n','e','x','1','1','_','t','r','a','y','_','i','c','o','n',0};
74 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};
76 static BOOL show_icon( struct tray_icon *icon );
77 static BOOL hide_icon( struct tray_icon *icon );
78 static BOOL delete_icon( struct tray_icon *icon );
80 #define SYSTEM_TRAY_REQUEST_DOCK 0
81 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
82 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
84 Atom systray_atom = 0;
86 #define MIN_DISPLAYED 8
87 #define ICON_BORDER 2
89 #define BALLOON_CREATE_TIMER 1
90 #define BALLOON_SHOW_TIMER 2
92 #define BALLOON_CREATE_TIMEOUT 2000
93 #define BALLOON_SHOW_MIN_TIMEOUT 10000
94 #define BALLOON_SHOW_MAX_TIMEOUT 30000
96 static struct tray_icon *balloon_icon;
97 static HWND balloon_window;
98 static POINT balloon_pos;
100 /* stand-alone tray window */
101 static HWND standalone_tray;
102 static int icon_cx, icon_cy;
103 static unsigned int nb_displayed;
105 /* retrieves icon record by owner window and ID */
106 static struct tray_icon *get_icon(HWND owner, UINT id)
108 struct tray_icon *this;
110 LIST_FOR_EACH_ENTRY( this, &icon_list, struct tray_icon, entry )
111 if ((this->id == id) && (this->owner == owner)) return this;
112 return NULL;
115 static void init_common_controls(void)
117 static BOOL initialized = FALSE;
119 if (!initialized)
121 INITCOMMONCONTROLSEX init_tooltip;
123 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
124 init_tooltip.dwICC = ICC_TAB_CLASSES;
126 InitCommonControlsEx(&init_tooltip);
127 initialized = TRUE;
131 /* create tooltip window for icon */
132 static void create_tooltip(struct tray_icon *icon)
134 init_common_controls();
135 icon->tooltip = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
136 WS_POPUP | TTS_ALWAYSTIP,
137 CW_USEDEFAULT, CW_USEDEFAULT,
138 CW_USEDEFAULT, CW_USEDEFAULT,
139 icon->window, NULL, NULL, NULL);
140 if (icon->tooltip)
142 TTTOOLINFOW ti;
143 ZeroMemory(&ti, sizeof(ti));
144 ti.cbSize = sizeof(TTTOOLINFOW);
145 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
146 ti.hwnd = icon->window;
147 ti.uId = (UINT_PTR)icon->window;
148 ti.lpszText = icon->tiptext;
149 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
153 void update_systray_balloon_position(void)
155 RECT rect;
156 POINT pos;
158 if (!balloon_icon) return;
159 GetWindowRect( balloon_icon->window, &rect );
160 pos.x = (rect.left + rect.right) / 2;
161 pos.y = (rect.top + rect.bottom) / 2;
162 if (pos.x == balloon_pos.x && pos.y == balloon_pos.y) return; /* nothing changed */
163 balloon_pos = pos;
164 SendMessageW( balloon_window, TTM_TRACKPOSITION, 0, MAKELONG( pos.x, pos.y ));
167 static void balloon_create_timer( struct tray_icon *icon )
169 TTTOOLINFOW ti;
171 init_common_controls();
172 balloon_window = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
173 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE,
174 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
175 icon->window, NULL, NULL, NULL);
177 memset( &ti, 0, sizeof(ti) );
178 ti.cbSize = sizeof(TTTOOLINFOW);
179 ti.hwnd = icon->window;
180 ti.uId = (UINT_PTR)icon->window;
181 ti.uFlags = TTF_TRACK | TTF_IDISHWND;
182 ti.lpszText = icon->info_text;
183 SendMessageW( balloon_window, TTM_ADDTOOLW, 0, (LPARAM)&ti );
184 if ((icon->info_flags & NIIF_ICONMASK) == NIIF_USER)
185 SendMessageW( balloon_window, TTM_SETTITLEW, (WPARAM)icon->info_icon, (LPARAM)icon->info_title );
186 else
187 SendMessageW( balloon_window, TTM_SETTITLEW, icon->info_flags, (LPARAM)icon->info_title );
188 balloon_icon = icon;
189 balloon_pos.x = balloon_pos.y = MAXLONG;
190 update_systray_balloon_position();
191 SendMessageW( balloon_window, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti );
192 KillTimer( icon->window, BALLOON_CREATE_TIMER );
193 SetTimer( icon->window, BALLOON_SHOW_TIMER, icon->info_timeout, NULL );
196 static BOOL show_balloon( struct tray_icon *icon )
198 if (standalone_tray && !show_systray) return FALSE; /* no systray window */
199 if (!icon->window) return FALSE; /* not displayed */
200 if (!icon->info_text[0]) return FALSE; /* no balloon */
201 balloon_icon = icon;
202 SetTimer( icon->window, BALLOON_CREATE_TIMER, BALLOON_CREATE_TIMEOUT, NULL );
203 return TRUE;
206 static void hide_balloon(void)
208 if (!balloon_icon) return;
209 if (balloon_window)
211 KillTimer( balloon_icon->window, BALLOON_SHOW_TIMER );
212 DestroyWindow( balloon_window );
213 balloon_window = 0;
215 else KillTimer( balloon_icon->window, BALLOON_CREATE_TIMER );
216 balloon_icon = NULL;
219 static void show_next_balloon(void)
221 struct tray_icon *icon;
223 LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
224 if (show_balloon( icon )) break;
227 static void update_balloon( struct tray_icon *icon )
229 if (balloon_icon == icon)
231 hide_balloon();
232 show_balloon( icon );
234 else if (!balloon_icon)
236 if (!show_balloon( icon )) return;
238 if (!balloon_icon) show_next_balloon();
241 static void balloon_timer(void)
243 if (balloon_icon) balloon_icon->info_text[0] = 0; /* clear text now that balloon has been shown */
244 hide_balloon();
245 show_next_balloon();
248 /* synchronize tooltip text with tooltip window */
249 static void update_tooltip_text(struct tray_icon *icon)
251 TTTOOLINFOW ti;
253 ZeroMemory(&ti, sizeof(ti));
254 ti.cbSize = sizeof(TTTOOLINFOW);
255 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
256 ti.hwnd = icon->window;
257 ti.uId = (UINT_PTR)icon->window;
258 ti.lpszText = icon->tiptext;
260 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
263 /* get the size of the stand-alone tray window */
264 static SIZE get_window_size(void)
266 SIZE size;
267 RECT rect;
269 rect.left = 0;
270 rect.top = 0;
271 rect.right = icon_cx * max( nb_displayed, MIN_DISPLAYED );
272 rect.bottom = icon_cy;
273 AdjustWindowRect( &rect, WS_CAPTION, FALSE );
274 size.cx = rect.right - rect.left;
275 size.cy = rect.bottom - rect.top;
276 return size;
279 /* get the position of an icon in the stand-alone tray */
280 static POINT get_icon_pos( struct tray_icon *icon )
282 POINT pos;
284 pos.x = icon_cx * icon->display;
285 pos.y = 0;
286 return pos;
289 /* window procedure for the standalone tray window */
290 static LRESULT WINAPI standalone_tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
292 switch (msg)
294 case WM_MOVE:
295 update_systray_balloon_position();
296 break;
297 case WM_CLOSE:
298 ShowWindow( hwnd, SW_HIDE );
299 hide_balloon();
300 show_systray = FALSE;
301 return 0;
302 case WM_DESTROY:
303 standalone_tray = 0;
304 break;
306 return DefWindowProcW( hwnd, msg, wparam, lparam );
309 /* add an icon to the standalone tray window */
310 static void add_to_standalone_tray( struct tray_icon *icon )
312 SIZE size;
313 POINT pos;
315 if (!standalone_tray)
317 static const WCHAR winname[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
319 size = get_window_size();
320 standalone_tray = CreateWindowExW( 0, tray_classname, winname, WS_CAPTION | WS_SYSMENU,
321 CW_USEDEFAULT, CW_USEDEFAULT, size.cx, size.cy, 0, 0, 0, 0 );
322 if (!standalone_tray) return;
325 icon->display = nb_displayed;
326 pos = get_icon_pos( icon );
327 icon->window = CreateWindowW( icon_classname, NULL, WS_CHILD | WS_VISIBLE,
328 pos.x, pos.y, icon_cx, icon_cy, standalone_tray, NULL, NULL, icon );
329 if (!icon->window)
331 icon->display = -1;
332 return;
334 create_tooltip( icon );
336 nb_displayed++;
337 size = get_window_size();
338 SetWindowPos( standalone_tray, 0, 0, 0, size.cx, size.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
339 if (nb_displayed == 1 && show_systray) ShowWindow( standalone_tray, SW_SHOWNA );
340 TRACE( "added %u now %d icons\n", icon->id, nb_displayed );
343 /* remove an icon from the stand-alone tray */
344 static void remove_from_standalone_tray( struct tray_icon *icon )
346 struct tray_icon *ptr;
347 POINT pos;
349 if (icon->display == -1) return;
351 LIST_FOR_EACH_ENTRY( ptr, &icon_list, struct tray_icon, entry )
353 if (ptr == icon) continue;
354 if (ptr->display < icon->display) continue;
355 ptr->display--;
356 pos = get_icon_pos( ptr );
357 SetWindowPos( ptr->window, 0, pos.x, pos.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
359 icon->display = -1;
360 if (!--nb_displayed) ShowWindow( standalone_tray, SW_HIDE );
361 TRACE( "removed %u now %d icons\n", icon->id, nb_displayed );
364 static void repaint_tray_icon( struct tray_icon *icon )
366 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
367 int width = GetSystemMetrics( SM_CXSMICON );
368 int height = GetSystemMetrics( SM_CYSMICON );
369 BITMAPINFO *info;
370 HBITMAP dib, mask;
371 HDC hdc;
372 RECT rc;
373 SIZE size;
374 POINT pos;
375 int i, x, y;
376 void *color_bits, *mask_bits;
377 DWORD *ptr;
378 BOOL has_alpha = FALSE;
380 GetWindowRect( icon->window, &rc );
381 size.cx = rc.right - rc.left;
382 size.cy = rc.bottom - rc.top;
383 pos.x = (size.cx - width) / 2;
384 pos.y = (size.cy - height) / 2;
386 info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ));
387 if (!info) return;
388 info->bmiHeader.biSize = sizeof(info->bmiHeader);
389 info->bmiHeader.biWidth = size.cx;
390 info->bmiHeader.biHeight = size.cy;
391 info->bmiHeader.biBitCount = 32;
392 info->bmiHeader.biPlanes = 1;
393 info->bmiHeader.biCompression = BI_RGB;
395 hdc = CreateCompatibleDC( 0 );
396 if (!(dib = CreateDIBSection( 0, info, DIB_RGB_COLORS, &color_bits, NULL, 0 ))) goto done;
397 SelectObject( hdc, dib );
398 DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_NORMAL );
400 /* check if the icon was drawn with an alpha channel */
401 for (i = 0, ptr = color_bits; i < size.cx * size.cy; i++)
402 if ((has_alpha = (ptr[i] & 0xff000000) != 0)) break;
404 if (!has_alpha)
406 unsigned int width_bytes = (size.cx + 31) / 32 * 4;
408 info->bmiHeader.biBitCount = 1;
409 info->bmiColors[0].rgbRed = 0;
410 info->bmiColors[0].rgbGreen = 0;
411 info->bmiColors[0].rgbBlue = 0;
412 info->bmiColors[0].rgbReserved = 0;
413 info->bmiColors[1].rgbRed = 0xff;
414 info->bmiColors[1].rgbGreen = 0xff;
415 info->bmiColors[1].rgbBlue = 0xff;
416 info->bmiColors[1].rgbReserved = 0;
418 if (!(mask = CreateDIBSection( 0, info, DIB_RGB_COLORS, &mask_bits, NULL, 0 ))) goto done;
419 memset( mask_bits, 0xff, width_bytes * size.cy );
420 SelectObject( hdc, mask );
421 DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_MASK );
423 for (y = 0, ptr = color_bits; y < size.cy; y++)
424 for (x = 0; x < size.cx; x++, ptr++)
425 if (!((((BYTE *)mask_bits)[y * width_bytes + x / 8] << (x % 8)) & 0x80))
426 *ptr |= 0xff000000;
428 SelectObject( hdc, dib );
429 DeleteObject( mask );
432 UpdateLayeredWindow( icon->window, 0, NULL, NULL, hdc, NULL, 0, &blend, ULW_ALPHA );
433 done:
434 HeapFree (GetProcessHeap(), 0, info);
435 if (hdc) DeleteDC( hdc );
436 if (dib) DeleteObject( dib );
439 static BOOL notify_owner( struct tray_icon *icon, UINT msg, LPARAM lparam )
441 WPARAM wp = icon->id;
442 LPARAM lp = msg;
444 if (icon->version >= NOTIFYICON_VERSION_4)
446 POINT pt = { (short)LOWORD(lparam), (short)HIWORD(lparam) };
448 ClientToScreen( icon->window, &pt );
449 wp = MAKEWPARAM( pt.x, pt.y );
450 lp = MAKELPARAM( msg, icon->id );
453 TRACE( "relaying 0x%x\n", msg );
454 if (!SendNotifyMessageW( icon->owner, icon->callback_message, wp, lp ) &&
455 (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
457 WARN( "application window was destroyed, removing icon %u\n", icon->id );
458 delete_icon( icon );
459 return FALSE;
461 return TRUE;
464 /* window procedure for the individual tray icon window */
465 static LRESULT WINAPI tray_icon_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
467 struct tray_icon *icon = NULL;
469 TRACE("hwnd=%p, msg=0x%x\n", hwnd, msg);
471 /* set the icon data for the window from the data passed into CreateWindow */
472 if (msg == WM_NCCREATE)
473 SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LPARAM)((const CREATESTRUCTW *)lparam)->lpCreateParams);
475 icon = (struct tray_icon *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
477 switch (msg)
479 case WM_SIZE:
480 if (icon->window && icon->layered) repaint_tray_icon( icon );
481 break;
483 case WM_PAINT:
484 if (!icon->layered)
486 PAINTSTRUCT ps;
487 RECT rc;
488 HDC hdc;
489 int cx = GetSystemMetrics( SM_CXSMICON );
490 int cy = GetSystemMetrics( SM_CYSMICON );
492 hdc = BeginPaint(hwnd, &ps);
493 GetClientRect(hwnd, &rc);
494 TRACE("painting rect %s\n", wine_dbgstr_rect(&rc));
495 DrawIconEx( hdc, (rc.left + rc.right - cx) / 2, (rc.top + rc.bottom - cy) / 2,
496 icon->image, cx, cy, 0, 0, DI_DEFAULTSIZE|DI_NORMAL );
497 EndPaint(hwnd, &ps);
498 return 0;
500 break;
502 case WM_MOUSEMOVE:
503 case WM_LBUTTONDOWN:
504 case WM_RBUTTONDOWN:
505 case WM_MBUTTONDOWN:
506 case WM_MBUTTONUP:
507 case WM_LBUTTONDBLCLK:
508 case WM_RBUTTONDBLCLK:
509 case WM_MBUTTONDBLCLK:
510 notify_owner( icon, msg, lparam );
511 break;
513 case WM_LBUTTONUP:
514 if (!notify_owner( icon, msg, lparam )) break;
515 if (icon->version > 0) notify_owner( icon, NIN_SELECT, lparam );
516 break;
518 case WM_RBUTTONUP:
519 if (!notify_owner( icon, msg, lparam )) break;
520 if (icon->version > 0) notify_owner( icon, WM_CONTEXTMENU, lparam );
521 break;
523 case WM_WINDOWPOSCHANGED:
524 update_systray_balloon_position();
525 break;
527 case WM_TIMER:
528 switch (wparam)
530 case BALLOON_CREATE_TIMER:
531 balloon_create_timer( icon );
532 break;
533 case BALLOON_SHOW_TIMER:
534 balloon_timer();
535 break;
537 return 0;
539 case WM_CLOSE:
540 if (icon->display == -1)
542 TRACE( "icon %u no longer embedded\n", icon->id );
543 hide_icon( icon );
544 add_to_standalone_tray( icon );
546 return 0;
548 return DefWindowProcW( hwnd, msg, wparam, lparam );
551 /* find the X11 window owner the system tray selection */
552 static Window get_systray_selection_owner( Display *display )
554 return XGetSelectionOwner( display, systray_atom );
557 static void get_systray_visual_info( Display *display, Window systray_window, XVisualInfo *info )
559 XVisualInfo *list, template;
560 VisualID *visual_id;
561 Atom type;
562 int format, num;
563 unsigned long count, remaining;
565 *info = default_visual;
566 if (XGetWindowProperty( display, systray_window, x11drv_atom(_NET_SYSTEM_TRAY_VISUAL), 0,
567 65536/sizeof(CARD32), False, XA_VISUALID, &type, &format, &count,
568 &remaining, (unsigned char **)&visual_id ))
569 return;
571 if (type == XA_VISUALID && format == 32)
573 template.visualid = visual_id[0];
574 if ((list = XGetVisualInfo( display, VisualIDMask, &template, &num )))
576 *info = list[0];
577 TRACE( "systray window %lx got visual %lx\n", systray_window, info->visualid );
578 XFree( list );
581 XFree( visual_id );
584 static BOOL init_systray(void)
586 static BOOL init_done;
587 WNDCLASSEXW class;
588 Display *display;
590 if (is_virtual_desktop()) return FALSE;
591 if (init_done) return TRUE;
593 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2 * ICON_BORDER;
594 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2 * ICON_BORDER;
596 memset( &class, 0, sizeof(class) );
597 class.cbSize = sizeof(class);
598 class.lpfnWndProc = tray_icon_wndproc;
599 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
600 class.hCursor = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
601 class.lpszClassName = icon_classname;
602 class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
604 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
606 ERR( "Could not register icon tray window class\n" );
607 return FALSE;
610 class.lpfnWndProc = standalone_tray_wndproc;
611 class.hbrBackground = (HBRUSH)COLOR_WINDOW;
612 class.lpszClassName = tray_classname;
613 class.style = CS_DBLCLKS;
615 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
617 ERR( "Could not register standalone tray window class\n" );
618 return FALSE;
621 display = thread_init_display();
622 if (DefaultScreen( display ) == 0)
623 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
624 else
626 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
627 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
628 systray_atom = XInternAtom( display, systray_buffer, False );
630 XSelectInput( display, root_window, StructureNotifyMask );
632 init_done = TRUE;
633 return TRUE;
636 /* dock the given icon with the NETWM system tray */
637 static void dock_systray_icon( Display *display, struct tray_icon *icon, Window systray_window )
639 Window window;
640 XEvent ev;
641 XSetWindowAttributes attr;
642 XVisualInfo visual;
643 struct x11drv_win_data *data;
645 get_systray_visual_info( display, systray_window, &visual );
647 icon->layered = (visual.depth == 32);
648 icon->window = CreateWindowExW( icon->layered ? WS_EX_LAYERED : 0,
649 icon_classname, NULL, WS_CLIPSIBLINGS | WS_POPUP,
650 CW_USEDEFAULT, CW_USEDEFAULT, icon_cx, icon_cy,
651 NULL, NULL, NULL, icon );
653 if (!(data = get_win_data( icon->window ))) return;
654 if (icon->layered) set_window_visual( data, &visual, TRUE );
655 make_window_embedded( data );
656 window = data->whole_window;
657 release_win_data( data );
659 create_tooltip( icon );
660 ShowWindow( icon->window, SW_SHOWNA );
662 TRACE( "icon window %p/%lx\n", icon->window, window );
664 /* send the docking request message */
665 ev.xclient.type = ClientMessage;
666 ev.xclient.window = systray_window;
667 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
668 ev.xclient.format = 32;
669 ev.xclient.data.l[0] = CurrentTime;
670 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
671 ev.xclient.data.l[2] = window;
672 ev.xclient.data.l[3] = 0;
673 ev.xclient.data.l[4] = 0;
674 XSendEvent( display, systray_window, False, NoEventMask, &ev );
676 if (!icon->layered)
678 attr.background_pixmap = ParentRelative;
679 attr.bit_gravity = ForgetGravity;
680 XChangeWindowAttributes( display, window, CWBackPixmap | CWBitGravity, &attr );
682 else repaint_tray_icon( icon );
685 /* dock systray windows again with the new owner */
686 void change_systray_owner( Display *display, Window systray_window )
688 struct tray_icon *icon;
690 TRACE( "new owner %lx\n", systray_window );
691 LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
693 if (icon->display == -1) continue;
694 hide_icon( icon );
695 dock_systray_icon( display, icon, systray_window );
699 /* hide a tray icon */
700 static BOOL hide_icon( struct tray_icon *icon )
702 struct x11drv_win_data *data;
704 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
706 if (!icon->window) return TRUE; /* already hidden */
708 /* make sure we don't try to unmap it, it confuses some systray docks */
709 if ((data = get_win_data( icon->window )))
711 if (data->embedded) data->mapped = FALSE;
712 release_win_data( data );
714 DestroyWindow(icon->window);
715 DestroyWindow(icon->tooltip);
716 icon->window = 0;
717 icon->layered = FALSE;
718 icon->tooltip = 0;
719 remove_from_standalone_tray( icon );
720 update_balloon( icon );
721 return TRUE;
724 /* make the icon visible */
725 static BOOL show_icon( struct tray_icon *icon )
727 Window systray_window;
728 Display *display = thread_init_display();
730 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
732 if (icon->window) return TRUE; /* already shown */
734 if ((systray_window = get_systray_selection_owner( display )))
735 dock_systray_icon( display, icon, systray_window );
736 else
737 add_to_standalone_tray( icon );
739 update_balloon( icon );
740 return TRUE;
743 /* Modifies an existing icon record */
744 static BOOL modify_icon( struct tray_icon *icon, NOTIFYICONDATAW *nid )
746 TRACE( "id=0x%x hwnd=%p flags=%x\n", nid->uID, nid->hWnd, nid->uFlags );
748 if (nid->uFlags & NIF_STATE)
750 icon->state = (icon->state & ~nid->dwStateMask) | (nid->dwState & nid->dwStateMask);
753 if (nid->uFlags & NIF_ICON)
755 if (icon->image) DestroyIcon(icon->image);
756 icon->image = CopyIcon(nid->hIcon);
757 if (icon->window)
759 if (icon->display != -1) InvalidateRect( icon->window, NULL, TRUE );
760 else if (icon->layered) repaint_tray_icon( icon );
761 else
763 Window win = X11DRV_get_whole_window( icon->window );
764 if (win) XClearArea( gdi_display, win, 0, 0, 0, 0, True );
769 if (nid->uFlags & NIF_MESSAGE)
771 icon->callback_message = nid->uCallbackMessage;
773 if (nid->uFlags & NIF_TIP)
775 lstrcpynW(icon->tiptext, nid->szTip, ARRAY_SIZE( icon->tiptext ));
776 if (icon->tooltip) update_tooltip_text(icon);
778 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
780 lstrcpynW( icon->info_text, nid->szInfo, ARRAY_SIZE( icon->info_text ));
781 lstrcpynW( icon->info_title, nid->szInfoTitle, ARRAY_SIZE( icon->info_title ));
782 icon->info_flags = nid->dwInfoFlags;
783 icon->info_timeout = max(min(nid->u.uTimeout, BALLOON_SHOW_MAX_TIMEOUT), BALLOON_SHOW_MIN_TIMEOUT);
784 icon->info_icon = nid->hBalloonIcon;
785 update_balloon( icon );
787 if (icon->state & NIS_HIDDEN) hide_icon( icon );
788 else show_icon( icon );
789 return TRUE;
792 /* Adds a new icon record to the list */
793 static BOOL add_icon(NOTIFYICONDATAW *nid)
795 struct tray_icon *icon;
797 TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
799 if ((icon = get_icon(nid->hWnd, nid->uID)))
801 WARN("duplicate tray icon add, buggy app?\n");
802 return FALSE;
805 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
807 ERR("out of memory\n");
808 return FALSE;
811 ZeroMemory(icon, sizeof(struct tray_icon));
812 icon->id = nid->uID;
813 icon->owner = nid->hWnd;
814 icon->display = -1;
816 list_add_tail(&icon_list, &icon->entry);
818 return modify_icon( icon, nid );
821 /* delete tray icon window and icon structure */
822 static BOOL delete_icon( struct tray_icon *icon )
824 hide_icon( icon );
825 list_remove( &icon->entry );
826 DestroyIcon( icon->image );
827 HeapFree( GetProcessHeap(), 0, icon );
828 return TRUE;
831 /* cleanup all icons for a given window */
832 static void cleanup_icons( HWND owner )
834 struct tray_icon *this, *next;
836 LIST_FOR_EACH_ENTRY_SAFE( this, next, &icon_list, struct tray_icon, entry )
837 if (this->owner == owner) delete_icon( this );
841 /***********************************************************************
842 * wine_notify_icon (X11DRV.@)
844 * Driver-side implementation of Shell_NotifyIcon.
846 int CDECL wine_notify_icon( DWORD msg, NOTIFYICONDATAW *data )
848 BOOL ret = FALSE;
849 struct tray_icon *icon;
851 switch (msg)
853 case NIM_ADD:
854 if (!init_systray()) return -1; /* fall back to default handling */
855 ret = add_icon( data );
856 break;
857 case NIM_DELETE:
858 if ((icon = get_icon( data->hWnd, data->uID ))) ret = delete_icon( icon );
859 break;
860 case NIM_MODIFY:
861 if ((icon = get_icon( data->hWnd, data->uID ))) ret = modify_icon( icon, data );
862 break;
863 case NIM_SETVERSION:
864 if ((icon = get_icon( data->hWnd, data->uID )))
866 icon->version = data->u.uVersion;
867 ret = TRUE;
869 break;
870 case 0xdead: /* Wine extension: owner window has died */
871 cleanup_icons( data->hWnd );
872 break;
873 default:
874 FIXME( "unhandled tray message: %u\n", msg );
875 break;
877 return ret;