mshtml: Fixed leaks in IHTMLStyleSheet::Release and IHTMLStyleSheetsCollection::Relea...
[wine/multimedia.git] / dlls / winex11.drv / systray.c
bloba694610a86af28161ab4ae3cbb2de69a6e66ebba
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 VALID_WIN_TIMER 1
89 #define BALLOON_CREATE_TIMER 2
90 #define BALLOON_SHOW_TIMER 3
92 #define VALID_WIN_TIMEOUT 2000
93 #define BALLOON_CREATE_TIMEOUT 2000
94 #define BALLOON_SHOW_MIN_TIMEOUT 10000
95 #define BALLOON_SHOW_MAX_TIMEOUT 30000
97 static struct tray_icon *balloon_icon;
98 static HWND balloon_window;
99 static POINT balloon_pos;
101 /* stand-alone tray window */
102 static HWND standalone_tray;
103 static int icon_cx, icon_cy;
104 static unsigned int nb_displayed;
106 /* retrieves icon record by owner window and ID */
107 static struct tray_icon *get_icon(HWND owner, UINT id)
109 struct tray_icon *this;
111 LIST_FOR_EACH_ENTRY( this, &icon_list, struct tray_icon, entry )
112 if ((this->id == id) && (this->owner == owner)) return this;
113 return NULL;
116 static void init_common_controls(void)
118 static BOOL initialized = FALSE;
120 if (!initialized)
122 INITCOMMONCONTROLSEX init_tooltip;
124 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
125 init_tooltip.dwICC = ICC_TAB_CLASSES;
127 InitCommonControlsEx(&init_tooltip);
128 initialized = TRUE;
132 /* create tooltip window for icon */
133 static void create_tooltip(struct tray_icon *icon)
135 init_common_controls();
136 icon->tooltip = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
137 WS_POPUP | TTS_ALWAYSTIP,
138 CW_USEDEFAULT, CW_USEDEFAULT,
139 CW_USEDEFAULT, CW_USEDEFAULT,
140 icon->window, NULL, NULL, NULL);
141 if (icon->tooltip)
143 TTTOOLINFOW ti;
144 ZeroMemory(&ti, sizeof(ti));
145 ti.cbSize = sizeof(TTTOOLINFOW);
146 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
147 ti.hwnd = icon->window;
148 ti.uId = (UINT_PTR)icon->window;
149 ti.lpszText = icon->tiptext;
150 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
154 void update_systray_balloon_position(void)
156 RECT rect;
157 POINT pos;
159 if (!balloon_icon) return;
160 GetWindowRect( balloon_icon->window, &rect );
161 pos.x = (rect.left + rect.right) / 2;
162 pos.y = (rect.top + rect.bottom) / 2;
163 if (pos.x == balloon_pos.x && pos.y == balloon_pos.y) return; /* nothing changed */
164 balloon_pos = pos;
165 SendMessageW( balloon_window, TTM_TRACKPOSITION, 0, MAKELONG( pos.x, pos.y ));
168 static void balloon_create_timer( struct tray_icon *icon )
170 TTTOOLINFOW ti;
172 init_common_controls();
173 balloon_window = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
174 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE,
175 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
176 icon->window, NULL, NULL, NULL);
178 memset( &ti, 0, sizeof(ti) );
179 ti.cbSize = sizeof(TTTOOLINFOW);
180 ti.hwnd = icon->window;
181 ti.uId = (UINT_PTR)icon->window;
182 ti.uFlags = TTF_TRACK | TTF_IDISHWND;
183 ti.lpszText = icon->info_text;
184 SendMessageW( balloon_window, TTM_ADDTOOLW, 0, (LPARAM)&ti );
185 if ((icon->info_flags & NIIF_ICONMASK) == NIIF_USER)
186 SendMessageW( balloon_window, TTM_SETTITLEW, (WPARAM)icon->info_icon, (LPARAM)icon->info_title );
187 else
188 SendMessageW( balloon_window, TTM_SETTITLEW, icon->info_flags, (LPARAM)icon->info_title );
189 balloon_icon = icon;
190 balloon_pos.x = balloon_pos.y = MAXLONG;
191 update_systray_balloon_position();
192 SendMessageW( balloon_window, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti );
193 KillTimer( icon->window, BALLOON_CREATE_TIMER );
194 SetTimer( icon->window, BALLOON_SHOW_TIMER, icon->info_timeout, NULL );
197 static BOOL show_balloon( struct tray_icon *icon )
199 if (standalone_tray && !show_systray) return FALSE; /* no systray window */
200 if (!icon->window) return FALSE; /* not displayed */
201 if (!icon->info_text[0]) return FALSE; /* no balloon */
202 balloon_icon = icon;
203 SetTimer( icon->window, BALLOON_CREATE_TIMER, BALLOON_CREATE_TIMEOUT, NULL );
204 return TRUE;
207 static void hide_balloon(void)
209 if (!balloon_icon) return;
210 if (balloon_window)
212 KillTimer( balloon_icon->window, BALLOON_SHOW_TIMER );
213 DestroyWindow( balloon_window );
214 balloon_window = 0;
216 else KillTimer( balloon_icon->window, BALLOON_CREATE_TIMER );
217 balloon_icon = NULL;
220 static void show_next_balloon(void)
222 struct tray_icon *icon;
224 LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
225 if (show_balloon( icon )) break;
228 static void update_balloon( struct tray_icon *icon )
230 if (balloon_icon == icon)
232 hide_balloon();
233 show_balloon( icon );
235 else if (!balloon_icon)
237 if (!show_balloon( icon )) return;
239 if (!balloon_icon) show_next_balloon();
242 static void balloon_timer(void)
244 if (balloon_icon) balloon_icon->info_text[0] = 0; /* clear text now that balloon has been shown */
245 hide_balloon();
246 show_next_balloon();
249 /* synchronize tooltip text with tooltip window */
250 static void update_tooltip_text(struct tray_icon *icon)
252 TTTOOLINFOW ti;
254 ZeroMemory(&ti, sizeof(ti));
255 ti.cbSize = sizeof(TTTOOLINFOW);
256 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
257 ti.hwnd = icon->window;
258 ti.uId = (UINT_PTR)icon->window;
259 ti.lpszText = icon->tiptext;
261 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
264 /* get the size of the stand-alone tray window */
265 static SIZE get_window_size(void)
267 SIZE size;
268 RECT rect;
270 rect.left = 0;
271 rect.top = 0;
272 rect.right = icon_cx * max( nb_displayed, MIN_DISPLAYED );
273 rect.bottom = icon_cy;
274 AdjustWindowRect( &rect, WS_CAPTION, FALSE );
275 size.cx = rect.right - rect.left;
276 size.cy = rect.bottom - rect.top;
277 return size;
280 /* get the position of an icon in the stand-alone tray */
281 static POINT get_icon_pos( struct tray_icon *icon )
283 POINT pos;
285 pos.x = icon_cx * icon->display;
286 pos.y = 0;
287 return pos;
290 /* window procedure for the standalone tray window */
291 static LRESULT WINAPI standalone_tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
293 switch (msg)
295 case WM_MOVE:
296 update_systray_balloon_position();
297 break;
298 case WM_CLOSE:
299 ShowWindow( hwnd, SW_HIDE );
300 hide_balloon();
301 show_systray = FALSE;
302 return 0;
303 case WM_DESTROY:
304 standalone_tray = 0;
305 break;
307 return DefWindowProcW( hwnd, msg, wparam, lparam );
310 /* add an icon to the standalone tray window */
311 static void add_to_standalone_tray( struct tray_icon *icon )
313 SIZE size;
314 POINT pos;
316 if (!standalone_tray)
318 static const WCHAR winname[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
320 size = get_window_size();
321 standalone_tray = CreateWindowExW( 0, tray_classname, winname, WS_CAPTION | WS_SYSMENU,
322 CW_USEDEFAULT, CW_USEDEFAULT, size.cx, size.cy, 0, 0, 0, 0 );
323 if (!standalone_tray) return;
326 icon->display = nb_displayed;
327 pos = get_icon_pos( icon );
328 icon->window = CreateWindowW( icon_classname, NULL, WS_CHILD | WS_VISIBLE,
329 pos.x, pos.y, icon_cx, icon_cy, standalone_tray, NULL, NULL, icon );
330 if (!icon->window)
332 icon->display = -1;
333 return;
335 create_tooltip( icon );
337 nb_displayed++;
338 size = get_window_size();
339 SetWindowPos( standalone_tray, 0, 0, 0, size.cx, size.cy, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
340 if (nb_displayed == 1 && show_systray) ShowWindow( standalone_tray, SW_SHOWNA );
341 TRACE( "added %u now %d icons\n", icon->id, nb_displayed );
344 /* remove an icon from the stand-alone tray */
345 static void remove_from_standalone_tray( struct tray_icon *icon )
347 struct tray_icon *ptr;
348 POINT pos;
350 if (icon->display == -1) return;
352 LIST_FOR_EACH_ENTRY( ptr, &icon_list, struct tray_icon, entry )
354 if (ptr == icon) continue;
355 if (ptr->display < icon->display) continue;
356 ptr->display--;
357 pos = get_icon_pos( ptr );
358 SetWindowPos( ptr->window, 0, pos.x, pos.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
360 icon->display = -1;
361 if (!--nb_displayed) ShowWindow( standalone_tray, SW_HIDE );
362 TRACE( "removed %u now %d icons\n", icon->id, nb_displayed );
365 static void repaint_tray_icon( struct tray_icon *icon )
367 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
368 int width = GetSystemMetrics( SM_CXSMICON );
369 int height = GetSystemMetrics( SM_CYSMICON );
370 BITMAPINFO *info;
371 HBITMAP dib, mask;
372 HDC hdc;
373 RECT rc;
374 SIZE size;
375 POINT pos;
376 int i, x, y;
377 void *color_bits, *mask_bits;
378 DWORD *ptr;
379 BOOL has_alpha = FALSE;
381 GetWindowRect( icon->window, &rc );
382 size.cx = rc.right - rc.left;
383 size.cy = rc.bottom - rc.top;
384 pos.x = (size.cx - width) / 2;
385 pos.y = (size.cy - height) / 2;
387 info = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ));
388 if (!info) return;
389 info->bmiHeader.biSize = sizeof(info->bmiHeader);
390 info->bmiHeader.biWidth = size.cx;
391 info->bmiHeader.biHeight = size.cy;
392 info->bmiHeader.biBitCount = 32;
393 info->bmiHeader.biPlanes = 1;
394 info->bmiHeader.biCompression = BI_RGB;
396 hdc = CreateCompatibleDC( 0 );
397 if (!(dib = CreateDIBSection( 0, info, DIB_RGB_COLORS, &color_bits, NULL, 0 ))) goto done;
398 SelectObject( hdc, dib );
399 DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_NORMAL );
401 /* check if the icon was drawn with an alpha channel */
402 for (i = 0, ptr = color_bits; i < size.cx * size.cy; i++)
403 if ((has_alpha = (ptr[i] & 0xff000000) != 0)) break;
405 if (!has_alpha)
407 unsigned int width_bytes = (size.cx + 31) / 32 * 4;
409 info->bmiHeader.biBitCount = 1;
410 info->bmiColors[0].rgbRed = 0;
411 info->bmiColors[0].rgbGreen = 0;
412 info->bmiColors[0].rgbBlue = 0;
413 info->bmiColors[0].rgbReserved = 0;
414 info->bmiColors[1].rgbRed = 0xff;
415 info->bmiColors[1].rgbGreen = 0xff;
416 info->bmiColors[1].rgbBlue = 0xff;
417 info->bmiColors[1].rgbReserved = 0;
419 if (!(mask = CreateDIBSection( 0, info, DIB_RGB_COLORS, &mask_bits, NULL, 0 ))) goto done;
420 memset( mask_bits, 0xff, width_bytes * size.cy );
421 SelectObject( hdc, mask );
422 DrawIconEx( hdc, pos.x, pos.y, icon->image, width, height, 0, 0, DI_DEFAULTSIZE | DI_MASK );
424 for (y = 0, ptr = color_bits; y < size.cy; y++)
425 for (x = 0; x < size.cx; x++, ptr++)
426 if (!((((BYTE *)mask_bits)[y * width_bytes + x / 8] << (x % 8)) & 0x80))
427 *ptr |= 0xff000000;
429 SelectObject( hdc, dib );
430 DeleteObject( mask );
433 UpdateLayeredWindow( icon->window, 0, NULL, NULL, hdc, NULL, 0, &blend, ULW_ALPHA );
434 done:
435 HeapFree (GetProcessHeap(), 0, info);
436 if (hdc) DeleteDC( hdc );
437 if (dib) DeleteObject( dib );
440 /* window procedure for the individual tray icon window */
441 static LRESULT WINAPI tray_icon_wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
443 struct tray_icon *icon = NULL;
444 BOOL ret;
446 TRACE("hwnd=%p, msg=0x%x\n", hwnd, msg);
448 /* set the icon data for the window from the data passed into CreateWindow */
449 if (msg == WM_NCCREATE)
450 SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LPARAM)((const CREATESTRUCTW *)lparam)->lpCreateParams);
452 icon = (struct tray_icon *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
454 switch (msg)
456 case WM_CREATE:
457 SetTimer( hwnd, VALID_WIN_TIMER, VALID_WIN_TIMEOUT, NULL );
458 break;
460 case WM_SIZE:
461 if (icon->window && icon->layered) repaint_tray_icon( icon );
462 break;
464 case WM_PAINT:
465 if (!icon->layered)
467 PAINTSTRUCT ps;
468 RECT rc;
469 HDC hdc;
470 int cx = GetSystemMetrics( SM_CXSMICON );
471 int cy = GetSystemMetrics( SM_CYSMICON );
473 hdc = BeginPaint(hwnd, &ps);
474 GetClientRect(hwnd, &rc);
475 TRACE("painting rect %s\n", wine_dbgstr_rect(&rc));
476 DrawIconEx( hdc, (rc.left + rc.right - cx) / 2, (rc.top + rc.bottom - cy) / 2,
477 icon->image, cx, cy, 0, 0, DI_DEFAULTSIZE|DI_NORMAL );
478 EndPaint(hwnd, &ps);
479 return 0;
481 break;
483 case WM_MOUSEMOVE:
484 case WM_LBUTTONDOWN:
485 case WM_LBUTTONUP:
486 case WM_RBUTTONDOWN:
487 case WM_RBUTTONUP:
488 case WM_MBUTTONDOWN:
489 case WM_MBUTTONUP:
490 case WM_LBUTTONDBLCLK:
491 case WM_RBUTTONDBLCLK:
492 case WM_MBUTTONDBLCLK:
493 /* notify the owner hwnd of the message */
494 TRACE("relaying 0x%x\n", msg);
495 ret = PostMessageW(icon->owner, icon->callback_message, icon->id, msg);
496 if (!ret && (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
498 WARN( "application window was destroyed, removing icon %u\n", icon->id );
499 delete_icon( icon );
501 return 0;
503 case WM_WINDOWPOSCHANGED:
504 update_systray_balloon_position();
505 break;
507 case WM_TIMER:
508 switch (wparam)
510 case VALID_WIN_TIMER:
511 if (!IsWindow( icon->owner )) delete_icon( icon );
512 break;
513 case BALLOON_CREATE_TIMER:
514 balloon_create_timer( icon );
515 break;
516 case BALLOON_SHOW_TIMER:
517 balloon_timer();
518 break;
520 return 0;
522 case WM_CLOSE:
523 if (icon->display == -1)
525 TRACE( "icon %u no longer embedded\n", icon->id );
526 hide_icon( icon );
527 add_to_standalone_tray( icon );
529 return 0;
531 return DefWindowProcW( hwnd, msg, wparam, lparam );
534 /* find the X11 window owner the system tray selection */
535 static Window get_systray_selection_owner( Display *display )
537 return XGetSelectionOwner( display, systray_atom );
540 static void get_systray_visual_info( Display *display, Window systray_window, XVisualInfo *info )
542 XVisualInfo *list, template;
543 VisualID *visual_id;
544 Atom type;
545 int format, num;
546 unsigned long count, remaining;
548 *info = default_visual;
549 if (XGetWindowProperty( display, systray_window, x11drv_atom(_NET_SYSTEM_TRAY_VISUAL), 0,
550 65536/sizeof(CARD32), False, XA_VISUALID, &type, &format, &count,
551 &remaining, (unsigned char **)&visual_id ))
552 return;
554 if (type == XA_VISUALID && format == 32)
556 template.visualid = visual_id[0];
557 if ((list = XGetVisualInfo( display, VisualIDMask, &template, &num )))
559 *info = list[0];
560 TRACE( "systray window %lx got visual %lx\n", systray_window, info->visualid );
561 XFree( list );
564 XFree( visual_id );
567 static BOOL init_systray(void)
569 static BOOL init_done;
570 WNDCLASSEXW class;
571 Display *display;
573 if (root_window != DefaultRootWindow( gdi_display )) return FALSE;
574 if (init_done) return TRUE;
576 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2 * ICON_BORDER;
577 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2 * ICON_BORDER;
579 memset( &class, 0, sizeof(class) );
580 class.cbSize = sizeof(class);
581 class.lpfnWndProc = tray_icon_wndproc;
582 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
583 class.hCursor = LoadCursorW( 0, (LPCWSTR)IDC_ARROW );
584 class.lpszClassName = icon_classname;
585 class.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
587 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
589 ERR( "Could not register icon tray window class\n" );
590 return FALSE;
593 class.lpfnWndProc = standalone_tray_wndproc;
594 class.hbrBackground = (HBRUSH)COLOR_WINDOW;
595 class.lpszClassName = tray_classname;
596 class.style = CS_DBLCLKS;
598 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
600 ERR( "Could not register standalone tray window class\n" );
601 return FALSE;
604 display = thread_init_display();
605 if (DefaultScreen( display ) == 0)
606 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
607 else
609 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
610 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
611 systray_atom = XInternAtom( display, systray_buffer, False );
613 XSelectInput( display, root_window, StructureNotifyMask );
615 init_done = TRUE;
616 return TRUE;
619 /* dock the given icon with the NETWM system tray */
620 static void dock_systray_icon( Display *display, struct tray_icon *icon, Window systray_window )
622 Window window;
623 XEvent ev;
624 XSetWindowAttributes attr;
625 XVisualInfo visual;
626 struct x11drv_win_data *data;
628 get_systray_visual_info( display, systray_window, &visual );
630 icon->layered = (visual.visualid != default_visual.visualid);
631 icon->window = CreateWindowExW( icon->layered ? WS_EX_LAYERED : 0,
632 icon_classname, NULL, WS_CLIPSIBLINGS | WS_POPUP,
633 CW_USEDEFAULT, CW_USEDEFAULT, icon_cx, icon_cy,
634 NULL, NULL, NULL, icon );
636 if (!(data = get_win_data( icon->window ))) return;
637 if (icon->layered) set_window_visual( data, &visual );
638 make_window_embedded( data );
639 window = data->whole_window;
640 release_win_data( data );
642 create_tooltip( icon );
643 ShowWindow( icon->window, SW_SHOWNA );
645 TRACE( "icon window %p/%lx\n", icon->window, window );
647 /* send the docking request message */
648 ev.xclient.type = ClientMessage;
649 ev.xclient.window = systray_window;
650 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
651 ev.xclient.format = 32;
652 ev.xclient.data.l[0] = CurrentTime;
653 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
654 ev.xclient.data.l[2] = window;
655 ev.xclient.data.l[3] = 0;
656 ev.xclient.data.l[4] = 0;
657 XSendEvent( display, systray_window, False, NoEventMask, &ev );
659 if (!icon->layered)
661 attr.background_pixmap = ParentRelative;
662 attr.bit_gravity = ForgetGravity;
663 XChangeWindowAttributes( display, window, CWBackPixmap | CWBitGravity, &attr );
665 else repaint_tray_icon( icon );
668 /* dock systray windows again with the new owner */
669 void change_systray_owner( Display *display, Window systray_window )
671 struct tray_icon *icon;
673 TRACE( "new owner %lx\n", systray_window );
674 LIST_FOR_EACH_ENTRY( icon, &icon_list, struct tray_icon, entry )
676 if (icon->display == -1) continue;
677 hide_icon( icon );
678 dock_systray_icon( display, icon, systray_window );
682 /* hide a tray icon */
683 static BOOL hide_icon( struct tray_icon *icon )
685 struct x11drv_win_data *data;
687 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
689 if (!icon->window) return TRUE; /* already hidden */
691 /* make sure we don't try to unmap it, it confuses some systray docks */
692 if ((data = get_win_data( icon->window )))
694 if (data->embedded) data->mapped = FALSE;
695 release_win_data( data );
697 DestroyWindow(icon->window);
698 DestroyWindow(icon->tooltip);
699 icon->window = 0;
700 icon->layered = FALSE;
701 icon->tooltip = 0;
702 remove_from_standalone_tray( icon );
703 update_balloon( icon );
704 return TRUE;
707 /* make the icon visible */
708 static BOOL show_icon( struct tray_icon *icon )
710 Window systray_window;
711 Display *display = thread_init_display();
713 TRACE( "id=0x%x, hwnd=%p\n", icon->id, icon->owner );
715 if (icon->window) return TRUE; /* already shown */
717 if ((systray_window = get_systray_selection_owner( display )))
718 dock_systray_icon( display, icon, systray_window );
719 else
720 add_to_standalone_tray( icon );
722 update_balloon( icon );
723 return TRUE;
726 /* Modifies an existing icon record */
727 static BOOL modify_icon( struct tray_icon *icon, NOTIFYICONDATAW *nid )
729 TRACE( "id=0x%x hwnd=%p flags=%x\n", nid->uID, nid->hWnd, nid->uFlags );
731 if (nid->uFlags & NIF_STATE)
733 icon->state = (icon->state & ~nid->dwStateMask) | (nid->dwState & nid->dwStateMask);
736 if (nid->uFlags & NIF_ICON)
738 if (icon->image) DestroyIcon(icon->image);
739 icon->image = CopyIcon(nid->hIcon);
740 if (icon->window)
742 if (icon->display != -1) InvalidateRect( icon->window, NULL, TRUE );
743 else if (icon->layered) repaint_tray_icon( icon );
744 else
746 Window win = X11DRV_get_whole_window( icon->window );
747 if (win) XClearArea( gdi_display, win, 0, 0, 0, 0, True );
752 if (nid->uFlags & NIF_MESSAGE)
754 icon->callback_message = nid->uCallbackMessage;
756 if (nid->uFlags & NIF_TIP)
758 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
759 if (icon->tooltip) update_tooltip_text(icon);
761 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
763 lstrcpynW( icon->info_text, nid->szInfo, sizeof(icon->info_text)/sizeof(WCHAR) );
764 lstrcpynW( icon->info_title, nid->szInfoTitle, sizeof(icon->info_title)/sizeof(WCHAR) );
765 icon->info_flags = nid->dwInfoFlags;
766 icon->info_timeout = max(min(nid->u.uTimeout, BALLOON_SHOW_MAX_TIMEOUT), BALLOON_SHOW_MIN_TIMEOUT);
767 icon->info_icon = nid->hBalloonIcon;
768 update_balloon( icon );
770 if (icon->state & NIS_HIDDEN) hide_icon( icon );
771 else show_icon( icon );
772 return TRUE;
775 /* Adds a new icon record to the list */
776 static BOOL add_icon(NOTIFYICONDATAW *nid)
778 struct tray_icon *icon;
780 TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
782 if ((icon = get_icon(nid->hWnd, nid->uID)))
784 WARN("duplicate tray icon add, buggy app?\n");
785 return FALSE;
788 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
790 ERR("out of memory\n");
791 return FALSE;
794 ZeroMemory(icon, sizeof(struct tray_icon));
795 icon->id = nid->uID;
796 icon->owner = nid->hWnd;
797 icon->display = -1;
799 list_add_tail(&icon_list, &icon->entry);
801 return modify_icon( icon, nid );
804 /* delete tray icon window and icon structure */
805 static BOOL delete_icon( struct tray_icon *icon )
807 hide_icon( icon );
808 list_remove( &icon->entry );
809 DestroyIcon( icon->image );
810 HeapFree( GetProcessHeap(), 0, icon );
811 return TRUE;
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 default:
838 FIXME( "unhandled tray message: %u\n", msg );
839 break;
841 return ret;