2 * Copyright (C) 2004 Mike Hearn, for CodeWeavers
3 * Copyright (C) 2005 Robert Shearman
4 * Copyright (C) 2008 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
24 #define _WIN32_IE 0x500
28 #include <wine/debug.h>
29 #include <wine/list.h>
31 #include "explorer_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(systray
);
35 #define IS_OPTION_FALSE(ch) \
36 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
38 struct notify_data
/* platform-independent format for NOTIFYICONDATA */
43 UINT uCallbackMessage
;
52 WCHAR szInfoTitle
[64];
55 /* data for the icon bitmap */
62 static int (CDECL
*wine_notify_icon
)(DWORD
,NOTIFYICONDATAW
*);
64 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
68 HICON image
; /* the image to render */
69 HWND owner
; /* the HWND passed in to the Shell_NotifyIcon call */
70 HWND tooltip
; /* Icon tooltip */
71 UINT id
; /* the unique id given by the app */
72 UINT callback_message
;
73 int display
; /* index in display list, or -1 if hidden */
74 WCHAR tiptext
[128]; /* Tooltip text. If empty => tooltip disabled */
77 static struct list icon_list
= LIST_INIT( icon_list
);
78 static HWND tray_window
;
80 static unsigned int alloc_displayed
;
81 static unsigned int nb_displayed
;
82 static struct icon
**displayed
; /* array of currently displayed icons */
84 static BOOL hide_systray
;
85 static int icon_cx
, icon_cy
;
87 #define MIN_DISPLAYED 8
90 /* Retrieves icon record by owner window and ID */
91 static struct icon
*get_icon(HWND owner
, UINT id
)
95 /* search for the icon */
96 LIST_FOR_EACH_ENTRY( this, &icon_list
, struct icon
, entry
)
97 if ((this->id
== id
) && (this->owner
== owner
)) return this;
102 /* compute the size of the tray window */
103 static SIZE
get_window_size(void)
110 rect
.right
= icon_cx
* max( nb_displayed
, MIN_DISPLAYED
);
111 rect
.bottom
= icon_cy
;
112 AdjustWindowRect( &rect
, WS_CAPTION
, FALSE
);
113 size
.cx
= rect
.right
- rect
.left
;
114 size
.cy
= rect
.bottom
- rect
.top
;
118 /* Creates tooltip window for icon. */
119 static void create_tooltip(struct icon
*icon
)
122 static BOOL tooltips_initialized
= FALSE
;
124 /* Register tooltip classes if this is the first icon */
125 if (!tooltips_initialized
)
127 INITCOMMONCONTROLSEX init_tooltip
;
129 init_tooltip
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
130 init_tooltip
.dwICC
= ICC_TAB_CLASSES
;
132 InitCommonControlsEx(&init_tooltip
);
133 tooltips_initialized
= TRUE
;
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 tray_window
, NULL
, NULL
, NULL
);
142 ZeroMemory(&ti
, sizeof(ti
));
143 ti
.cbSize
= sizeof(TTTOOLINFOW
);
144 ti
.hwnd
= tray_window
;
145 ti
.lpszText
= icon
->tiptext
;
146 if (icon
->display
!= -1)
148 ti
.rect
.left
= icon_cx
* icon
->display
;
149 ti
.rect
.right
= icon_cx
* (icon
->display
+ 1);
151 ti
.rect
.bottom
= icon_cy
;
153 SendMessageW(icon
->tooltip
, TTM_ADDTOOLW
, 0, (LPARAM
)&ti
);
156 /* Synchronize tooltip text with tooltip window */
157 static void update_tooltip_text(struct icon
*icon
)
161 ZeroMemory(&ti
, sizeof(ti
));
162 ti
.cbSize
= sizeof(TTTOOLINFOW
);
163 ti
.hwnd
= tray_window
;
164 ti
.lpszText
= icon
->tiptext
;
166 SendMessageW(icon
->tooltip
, TTM_UPDATETIPTEXTW
, 0, (LPARAM
)&ti
);
169 /* synchronize tooltip position with tooltip window */
170 static void update_tooltip_position( struct icon
*icon
)
174 ZeroMemory(&ti
, sizeof(ti
));
175 ti
.cbSize
= sizeof(TTTOOLINFOW
);
176 ti
.hwnd
= tray_window
;
177 if (icon
->display
!= -1)
179 ti
.rect
.left
= icon_cx
* icon
->display
;
180 ti
.rect
.right
= icon_cx
* (icon
->display
+ 1);
182 ti
.rect
.bottom
= icon_cy
;
184 SendMessageW( icon
->tooltip
, TTM_NEWTOOLRECTW
, 0, (LPARAM
)&ti
);
187 /* find the icon located at a certain point in the tray window */
188 static struct icon
*icon_from_point( int x
, int y
)
190 if (y
< 0 || y
>= icon_cy
) return NULL
;
191 if (x
< 0 || x
>= icon_cx
* nb_displayed
) return NULL
;
192 return displayed
[x
/ icon_cx
];
195 /* invalidate the portion of the tray window that contains the specified icons */
196 static void invalidate_icons( unsigned int start
, unsigned int end
)
200 rect
.left
= start
* icon_cx
;
202 rect
.right
= (end
+ 1) * icon_cx
;
203 rect
.bottom
= icon_cy
;
204 InvalidateRect( tray_window
, &rect
, TRUE
);
207 /* make an icon visible */
208 static BOOL
show_icon(struct icon
*icon
)
210 WINE_TRACE("id=0x%x, hwnd=%p\n", icon
->id
, icon
->owner
);
212 if (icon
->display
!= -1) return TRUE
; /* already displayed */
214 if (nb_displayed
>= alloc_displayed
)
216 unsigned int new_count
= max( alloc_displayed
* 2, 32 );
218 if (displayed
) ptr
= HeapReAlloc( GetProcessHeap(), 0, displayed
, new_count
* sizeof(*ptr
) );
219 else ptr
= HeapAlloc( GetProcessHeap(), 0, new_count
* sizeof(*ptr
) );
220 if (!ptr
) return FALSE
;
222 alloc_displayed
= new_count
;
225 icon
->display
= nb_displayed
;
226 displayed
[nb_displayed
++] = icon
;
227 update_tooltip_position( icon
);
228 invalidate_icons( nb_displayed
-1, nb_displayed
-1 );
230 if (nb_displayed
> MIN_DISPLAYED
)
232 SIZE size
= get_window_size();
233 SetWindowPos( tray_window
, 0, 0, 0, size
.cx
, size
.cy
, SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOMOVE
);
235 else if (nb_displayed
== 1)
237 if (!hide_systray
) ShowWindow( tray_window
, SW_SHOWNA
);
240 create_tooltip(icon
);
244 /* make an icon invisible */
245 static BOOL
hide_icon(struct icon
*icon
)
249 WINE_TRACE("id=0x%x, hwnd=%p\n", icon
->id
, icon
->owner
);
251 if (icon
->display
== -1) return TRUE
; /* already hidden */
253 assert( nb_displayed
);
254 for (i
= icon
->display
; i
< nb_displayed
- 1; i
++)
256 displayed
[i
] = displayed
[i
+ 1];
257 displayed
[i
]->display
= i
;
258 update_tooltip_position( displayed
[i
] );
261 invalidate_icons( icon
->display
, nb_displayed
);
264 if (nb_displayed
>= MIN_DISPLAYED
)
266 SIZE size
= get_window_size();
267 SetWindowPos( tray_window
, 0, 0, 0, size
.cx
, size
.cy
, SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOMOVE
);
269 else if (!nb_displayed
)
271 ShowWindow( tray_window
, SW_HIDE
);
274 update_tooltip_position( icon
);
278 /* Modifies an existing icon record */
279 static BOOL
modify_icon( struct icon
*icon
, NOTIFYICONDATAW
*nid
)
281 WINE_TRACE("id=0x%x, hwnd=%p\n", nid
->uID
, nid
->hWnd
);
283 /* demarshal the request from the NID */
286 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid
->uID
, nid
->hWnd
);
290 if ((nid
->uFlags
& NIF_STATE
) && (nid
->dwStateMask
& NIS_HIDDEN
))
292 if (nid
->dwState
& NIS_HIDDEN
) hide_icon( icon
);
293 else show_icon( icon
);
296 if (nid
->uFlags
& NIF_ICON
)
298 if (icon
->image
) DestroyIcon(icon
->image
);
299 icon
->image
= CopyIcon(nid
->hIcon
);
300 if (icon
->display
!= -1) invalidate_icons( icon
->display
, icon
->display
);
303 if (nid
->uFlags
& NIF_MESSAGE
)
305 icon
->callback_message
= nid
->uCallbackMessage
;
307 if (nid
->uFlags
& NIF_TIP
)
309 lstrcpynW(icon
->tiptext
, nid
->szTip
, sizeof(icon
->tiptext
)/sizeof(WCHAR
));
310 if (icon
->display
!= -1) update_tooltip_text(icon
);
312 if (nid
->uFlags
& NIF_INFO
&& nid
->cbSize
>= NOTIFYICONDATAA_V2_SIZE
)
314 WINE_FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid
->szInfoTitle
), wine_dbgstr_w(nid
->szInfo
));
319 /* Adds a new icon record to the list */
320 static BOOL
add_icon(NOTIFYICONDATAW
*nid
)
324 WINE_TRACE("id=0x%x, hwnd=%p\n", nid
->uID
, nid
->hWnd
);
326 if ((icon
= get_icon(nid
->hWnd
, nid
->uID
)))
328 WINE_WARN("duplicate tray icon add, buggy app?\n");
332 if (!(icon
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*icon
))))
334 WINE_ERR("out of memory\n");
338 ZeroMemory(icon
, sizeof(struct icon
));
340 icon
->owner
= nid
->hWnd
;
343 if (list_empty( &icon_list
)) SetTimer( tray_window
, 1, 2000, NULL
);
344 list_add_tail(&icon_list
, &icon
->entry
);
346 modify_icon( icon
, nid
);
347 /* show icon, unless hidden state was explicitly specified */
348 if (!((nid
->uFlags
& NIF_STATE
) && (nid
->dwStateMask
& NIS_HIDDEN
))) show_icon( icon
);
352 /* Deletes tray icon window and icon record */
353 static BOOL
delete_icon(struct icon
*icon
)
356 list_remove(&icon
->entry
);
357 DestroyIcon(icon
->image
);
358 HeapFree(GetProcessHeap(), 0, icon
);
359 if (list_empty( &icon_list
)) KillTimer( tray_window
, 1 );
363 /* cleanup icons belonging to windows that have been destroyed */
364 static void cleanup_destroyed_windows(void)
366 struct icon
*icon
, *next
;
368 LIST_FOR_EACH_ENTRY_SAFE( icon
, next
, &icon_list
, struct icon
, entry
)
369 if (!IsWindow( icon
->owner
)) delete_icon( icon
);
372 static BOOL
handle_incoming(HWND hwndSource
, COPYDATASTRUCT
*cds
)
374 struct icon
*icon
= NULL
;
375 const struct notify_data
*data
;
379 if (cds
->cbData
< sizeof(*data
)) return FALSE
;
382 nid
.cbSize
= sizeof(nid
);
383 nid
.hWnd
= LongToHandle( data
->hWnd
);
385 nid
.uFlags
= data
->uFlags
;
386 nid
.uCallbackMessage
= data
->uCallbackMessage
;
388 nid
.dwState
= data
->dwState
;
389 nid
.dwStateMask
= data
->dwStateMask
;
390 nid
.u
.uTimeout
= data
->u
.uTimeout
;
391 nid
.dwInfoFlags
= data
->dwInfoFlags
;
392 nid
.guidItem
= data
->guidItem
;
393 lstrcpyW( nid
.szTip
, data
->szTip
);
394 lstrcpyW( nid
.szInfo
, data
->szInfo
);
395 lstrcpyW( nid
.szInfoTitle
, data
->szInfoTitle
);
396 nid
.hBalloonIcon
= 0;
398 /* FIXME: if statement only needed because we don't support interprocess
400 if ((nid
.uFlags
& NIF_ICON
) && cds
->cbData
> sizeof(*data
))
404 const char *buffer
= (const char *)(data
+ 1);
406 cbMaskBits
= (data
->width
* data
->height
+ 15) / 16 * 2;
407 cbColourBits
= (data
->planes
* data
->width
* data
->height
* data
->bpp
+ 15) / 16 * 2;
409 if (cds
->cbData
< sizeof(*data
) + cbMaskBits
+ cbColourBits
)
411 WINE_ERR("buffer underflow\n");
414 nid
.hIcon
= CreateIcon(NULL
, data
->width
, data
->height
, data
->planes
, data
->bpp
,
415 buffer
, buffer
+ cbMaskBits
);
418 /* try forward to x11drv first */
419 if (cds
->dwData
== NIM_ADD
|| !(icon
= get_icon( nid
.hWnd
, nid
.uID
)))
421 if (wine_notify_icon
&& ((ret
= wine_notify_icon( cds
->dwData
, &nid
)) != -1))
423 if (nid
.hIcon
) DestroyIcon( nid
.hIcon
);
432 ret
= add_icon(&nid
);
435 if (icon
) ret
= delete_icon( icon
);
438 if (icon
) ret
= modify_icon( icon
, &nid
);
441 WINE_FIXME("unhandled tray message: %ld\n", cds
->dwData
);
445 if (nid
.hIcon
) DestroyIcon( nid
.hIcon
);
449 static void do_hide_systray(void)
451 SetWindowPos( tray_window
, 0,
452 GetSystemMetrics(SM_XVIRTUALSCREEN
) + GetSystemMetrics(SM_CXVIRTUALSCREEN
),
453 GetSystemMetrics(SM_YVIRTUALSCREEN
) + GetSystemMetrics(SM_CYVIRTUALSCREEN
),
454 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
457 static LRESULT WINAPI
tray_wndproc( HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
462 return handle_incoming((HWND
)wparam
, (COPYDATASTRUCT
*)lparam
);
464 case WM_DISPLAYCHANGE
:
465 if (hide_systray
) do_hide_systray();
469 cleanup_destroyed_windows();
478 hdc
= BeginPaint( hwnd
, &ps
);
479 for (i
= ps
.rcPaint
.left
/ icon_cx
;
480 (i
< (ps
.rcPaint
.right
+ icon_cx
- 1) / icon_cx
) && (i
< nb_displayed
);
483 DrawIconEx( hdc
, i
* icon_cx
+ ICON_BORDER
, ICON_BORDER
, displayed
[i
]->image
,
484 icon_cx
- 2*ICON_BORDER
, icon_cy
- 2*ICON_BORDER
,
485 0, 0, DI_DEFAULTSIZE
|DI_NORMAL
);
487 EndPaint( hwnd
, &ps
);
498 case WM_LBUTTONDBLCLK
:
499 case WM_RBUTTONDBLCLK
:
500 case WM_MBUTTONDBLCLK
:
503 struct icon
*icon
= icon_from_point( (short)LOWORD(lparam
), (short)HIWORD(lparam
) );
506 /* notify the owner hwnd of the message */
507 WINE_TRACE("relaying 0x%x\n", msg
);
510 message
.message
= msg
;
511 message
.wParam
= wparam
;
512 message
.lParam
= lparam
;
513 SendMessageW( icon
->tooltip
, TTM_RELAYEVENT
, 0, (LPARAM
)&message
);
515 if (!PostMessageW( icon
->owner
, icon
->callback_message
, (WPARAM
) icon
->id
, (LPARAM
) msg
) &&
516 GetLastError() == ERROR_INVALID_WINDOW_HANDLE
)
518 WINE_WARN("application window was destroyed without removing "
519 "notification icon, removing automatically\n");
526 /* don't destroy the tray window, just hide it */
527 ShowWindow( hwnd
, SW_HIDE
);
531 return DefWindowProcW( hwnd
, msg
, wparam
, lparam
);
536 static BOOL
is_systray_hidden(void)
538 const WCHAR show_systray_keyname
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
539 'X','1','1',' ','D','r','i','v','e','r',0};
540 const WCHAR show_systray_valuename
[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
544 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
545 if (RegOpenKeyW(HKEY_CURRENT_USER
, show_systray_keyname
, &hkey
) == ERROR_SUCCESS
)
548 DWORD type
, size
= sizeof(value
);
549 if (RegQueryValueExW(hkey
, show_systray_valuename
, 0, &type
, (LPBYTE
)&value
, &size
) == ERROR_SUCCESS
)
551 ret
= IS_OPTION_FALSE(value
[0]);
558 /* this function creates the listener window */
559 void initialize_systray(void)
564 static const WCHAR classname
[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
565 static const WCHAR winname
[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
567 if ((x11drv
= GetModuleHandleA( "winex11.drv" )))
568 wine_notify_icon
= (void *)GetProcAddress( x11drv
, "wine_notify_icon" );
570 icon_cx
= GetSystemMetrics( SM_CXSMICON
) + 2*ICON_BORDER
;
571 icon_cy
= GetSystemMetrics( SM_CYSMICON
) + 2*ICON_BORDER
;
572 hide_systray
= is_systray_hidden();
574 /* register the systray listener window class */
575 ZeroMemory(&class, sizeof(class));
576 class.cbSize
= sizeof(class);
577 class.style
= CS_DBLCLKS
;
578 class.lpfnWndProc
= tray_wndproc
;
579 class.hInstance
= NULL
;
580 class.hIcon
= LoadIconW(0, (LPCWSTR
)IDI_WINLOGO
);
581 class.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
582 class.hbrBackground
= (HBRUSH
) COLOR_WINDOW
;
583 class.lpszClassName
= (WCHAR
*) &classname
;
585 if (!RegisterClassExW(&class))
587 WINE_ERR("Could not register SysTray window class\n");
591 size
= get_window_size();
592 tray_window
= CreateWindowW( classname
, winname
, WS_OVERLAPPED
| WS_CAPTION
,
593 CW_USEDEFAULT
, CW_USEDEFAULT
, size
.cx
, size
.cy
, 0, 0, 0, 0 );
596 WINE_ERR("Could not create tray window\n");
600 if (hide_systray
) do_hide_systray();