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 list_add_tail(&icon_list
, &icon
->entry
);
345 modify_icon( icon
, nid
);
346 /* show icon, unless hidden state was explicitly specified */
347 if (!((nid
->uFlags
& NIF_STATE
) && (nid
->dwStateMask
& NIS_HIDDEN
))) show_icon( icon
);
351 /* Deletes tray icon window and icon record */
352 static BOOL
delete_icon(struct icon
*icon
)
355 list_remove(&icon
->entry
);
356 DestroyIcon(icon
->image
);
357 HeapFree(GetProcessHeap(), 0, icon
);
361 /* cleanup icons belonging to windows that have been destroyed */
362 static void cleanup_destroyed_windows(void)
364 struct icon
*icon
, *next
;
366 LIST_FOR_EACH_ENTRY_SAFE( icon
, next
, &icon_list
, struct icon
, entry
)
367 if (!IsWindow( icon
->owner
)) delete_icon( icon
);
370 static BOOL
handle_incoming(HWND hwndSource
, COPYDATASTRUCT
*cds
)
372 struct icon
*icon
= NULL
;
373 const struct notify_data
*data
;
377 if (cds
->cbData
< sizeof(*data
)) return FALSE
;
380 nid
.cbSize
= sizeof(nid
);
381 nid
.hWnd
= LongToHandle( data
->hWnd
);
383 nid
.uFlags
= data
->uFlags
;
384 nid
.uCallbackMessage
= data
->uCallbackMessage
;
386 nid
.dwState
= data
->dwState
;
387 nid
.dwStateMask
= data
->dwStateMask
;
388 nid
.u
.uTimeout
= data
->u
.uTimeout
;
389 nid
.dwInfoFlags
= data
->dwInfoFlags
;
390 nid
.guidItem
= data
->guidItem
;
391 lstrcpyW( nid
.szTip
, data
->szTip
);
392 lstrcpyW( nid
.szInfo
, data
->szInfo
);
393 lstrcpyW( nid
.szInfoTitle
, data
->szInfoTitle
);
394 nid
.hBalloonIcon
= 0;
396 /* FIXME: if statement only needed because we don't support interprocess
398 if ((nid
.uFlags
& NIF_ICON
) && cds
->cbData
> sizeof(*data
))
402 const char *buffer
= (const char *)(data
+ 1);
404 cbMaskBits
= (data
->width
* data
->height
+ 15) / 16 * 2;
405 cbColourBits
= (data
->planes
* data
->width
* data
->height
* data
->bpp
+ 15) / 16 * 2;
407 if (cds
->cbData
< sizeof(*data
) + cbMaskBits
+ cbColourBits
)
409 WINE_ERR("buffer underflow\n");
412 nid
.hIcon
= CreateIcon(NULL
, data
->width
, data
->height
, data
->planes
, data
->bpp
,
413 buffer
, buffer
+ cbMaskBits
);
416 /* try forward to x11drv first */
417 if (cds
->dwData
== NIM_ADD
|| !(icon
= get_icon( nid
.hWnd
, nid
.uID
)))
419 if (wine_notify_icon
&& ((ret
= wine_notify_icon( cds
->dwData
, &nid
)) != -1))
421 if (nid
.hIcon
) DestroyIcon( nid
.hIcon
);
430 ret
= add_icon(&nid
);
433 if (icon
) ret
= delete_icon( icon
);
436 if (icon
) ret
= modify_icon( icon
, &nid
);
439 WINE_FIXME("unhandled tray message: %ld\n", cds
->dwData
);
443 if (nid
.hIcon
) DestroyIcon( nid
.hIcon
);
447 static void do_hide_systray(void)
449 SetWindowPos( tray_window
, 0,
450 GetSystemMetrics(SM_XVIRTUALSCREEN
) + GetSystemMetrics(SM_CXVIRTUALSCREEN
),
451 GetSystemMetrics(SM_YVIRTUALSCREEN
) + GetSystemMetrics(SM_CYVIRTUALSCREEN
),
452 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
455 static LRESULT WINAPI
tray_wndproc( HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
460 return handle_incoming((HWND
)wparam
, (COPYDATASTRUCT
*)lparam
);
462 case WM_DISPLAYCHANGE
:
463 if (hide_systray
) do_hide_systray();
467 cleanup_destroyed_windows();
476 hdc
= BeginPaint( hwnd
, &ps
);
477 for (i
= ps
.rcPaint
.left
/ icon_cx
;
478 (i
< (ps
.rcPaint
.right
+ icon_cx
- 1) / icon_cx
) && (i
< nb_displayed
);
481 DrawIconEx( hdc
, i
* icon_cx
+ ICON_BORDER
, ICON_BORDER
, displayed
[i
]->image
,
482 icon_cx
- 2*ICON_BORDER
, icon_cy
- 2*ICON_BORDER
,
483 0, 0, DI_DEFAULTSIZE
|DI_NORMAL
);
485 EndPaint( hwnd
, &ps
);
496 case WM_LBUTTONDBLCLK
:
497 case WM_RBUTTONDBLCLK
:
498 case WM_MBUTTONDBLCLK
:
501 struct icon
*icon
= icon_from_point( (short)LOWORD(lparam
), (short)HIWORD(lparam
) );
504 /* notify the owner hwnd of the message */
505 WINE_TRACE("relaying 0x%x\n", msg
);
508 message
.message
= msg
;
509 message
.wParam
= wparam
;
510 message
.lParam
= lparam
;
511 SendMessageW( icon
->tooltip
, TTM_RELAYEVENT
, 0, (LPARAM
)&message
);
513 if (!PostMessageW( icon
->owner
, icon
->callback_message
, (WPARAM
) icon
->id
, (LPARAM
) msg
) &&
514 GetLastError() == ERROR_INVALID_WINDOW_HANDLE
)
516 WINE_WARN("application window was destroyed without removing "
517 "notification icon, removing automatically\n");
524 /* don't destroy the tray window, just hide it */
525 ShowWindow( hwnd
, SW_HIDE
);
529 return DefWindowProcW( hwnd
, msg
, wparam
, lparam
);
534 static BOOL
is_systray_hidden(void)
536 const WCHAR show_systray_keyname
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
537 'X','1','1',' ','D','r','i','v','e','r',0};
538 const WCHAR show_systray_valuename
[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
542 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
543 if (RegOpenKeyW(HKEY_CURRENT_USER
, show_systray_keyname
, &hkey
) == ERROR_SUCCESS
)
546 DWORD type
, size
= sizeof(value
);
547 if (RegQueryValueExW(hkey
, show_systray_valuename
, 0, &type
, (LPBYTE
)&value
, &size
) == ERROR_SUCCESS
)
549 ret
= IS_OPTION_FALSE(value
[0]);
556 /* this function creates the listener window */
557 void initialize_systray(void)
562 static const WCHAR classname
[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
563 static const WCHAR winname
[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
565 if ((x11drv
= GetModuleHandleA( "winex11.drv" )))
566 wine_notify_icon
= (void *)GetProcAddress( x11drv
, "wine_notify_icon" );
568 icon_cx
= GetSystemMetrics( SM_CXSMICON
) + 2*ICON_BORDER
;
569 icon_cy
= GetSystemMetrics( SM_CYSMICON
) + 2*ICON_BORDER
;
570 hide_systray
= is_systray_hidden();
572 /* register the systray listener window class */
573 ZeroMemory(&class, sizeof(class));
574 class.cbSize
= sizeof(class);
575 class.style
= CS_DBLCLKS
;
576 class.lpfnWndProc
= tray_wndproc
;
577 class.hInstance
= NULL
;
578 class.hIcon
= LoadIconW(0, (LPCWSTR
)IDI_WINLOGO
);
579 class.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
580 class.hbrBackground
= (HBRUSH
) COLOR_WINDOW
;
581 class.lpszClassName
= (WCHAR
*) &classname
;
583 if (!RegisterClassExW(&class))
585 WINE_ERR("Could not register SysTray window class\n");
589 size
= get_window_size();
590 tray_window
= CreateWindowW( classname
, winname
, WS_OVERLAPPED
| WS_CAPTION
,
591 CW_USEDEFAULT
, CW_USEDEFAULT
, size
.cx
, size
.cy
, 0, 0, 0, 0 );
594 WINE_ERR("Could not create tray window\n");
598 if (hide_systray
) do_hide_systray();
600 SetTimer( tray_window
, 1, 2000, NULL
);