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
24 #define NONAMELESSUNION
25 #define _WIN32_IE 0x500
29 #include <wine/debug.h>
30 #include <wine/list.h>
32 #include "explorer_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(systray
);
36 #define IS_OPTION_FALSE(ch) \
37 ((ch) == 'n' || (ch) == 'N' || (ch) == 'f' || (ch) == 'F' || (ch) == '0')
39 struct notify_data
/* platform-independent format for NOTIFYICONDATA */
44 UINT uCallbackMessage
;
53 WCHAR szInfoTitle
[64];
56 /* data for the icon bitmap */
63 static int (CDECL
*wine_notify_icon
)(DWORD
,NOTIFYICONDATAW
*);
65 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
69 HICON image
; /* the image to render */
70 HWND owner
; /* the HWND passed in to the Shell_NotifyIcon call */
71 HWND tooltip
; /* Icon tooltip */
72 UINT id
; /* the unique id given by the app */
73 UINT callback_message
;
74 int display
; /* index in display list, or -1 if hidden */
75 WCHAR tiptext
[128]; /* Tooltip text. If empty => tooltip disabled */
78 static struct list icon_list
= LIST_INIT( icon_list
);
79 static HWND tray_window
;
81 static unsigned int alloc_displayed
;
82 static unsigned int nb_displayed
;
83 static struct icon
**displayed
; /* array of currently displayed icons */
85 static BOOL hide_systray
;
86 static int icon_cx
, icon_cy
;
88 #define MIN_DISPLAYED 8
91 /* Retrieves icon record by owner window and ID */
92 static struct icon
*get_icon(HWND owner
, UINT id
)
96 /* search for the icon */
97 LIST_FOR_EACH_ENTRY( this, &icon_list
, struct icon
, entry
)
98 if ((this->id
== id
) && (this->owner
== owner
)) return this;
103 /* compute the size of the tray window */
104 static SIZE
get_window_size(void)
111 rect
.right
= icon_cx
* max( nb_displayed
, MIN_DISPLAYED
);
112 rect
.bottom
= icon_cy
;
113 AdjustWindowRect( &rect
, WS_CAPTION
, FALSE
);
114 size
.cx
= rect
.right
- rect
.left
;
115 size
.cy
= rect
.bottom
- rect
.top
;
119 /* Creates tooltip window for icon. */
120 static void create_tooltip(struct icon
*icon
)
123 static BOOL tooltips_initialized
= FALSE
;
125 /* Register tooltip classes if this is the first icon */
126 if (!tooltips_initialized
)
128 INITCOMMONCONTROLSEX init_tooltip
;
130 init_tooltip
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
131 init_tooltip
.dwICC
= ICC_TAB_CLASSES
;
133 InitCommonControlsEx(&init_tooltip
);
134 tooltips_initialized
= TRUE
;
137 icon
->tooltip
= CreateWindowExW(WS_EX_TOPMOST
, TOOLTIPS_CLASSW
, NULL
,
138 WS_POPUP
| TTS_ALWAYSTIP
,
139 CW_USEDEFAULT
, CW_USEDEFAULT
,
140 CW_USEDEFAULT
, CW_USEDEFAULT
,
141 tray_window
, NULL
, NULL
, NULL
);
143 ZeroMemory(&ti
, sizeof(ti
));
144 ti
.cbSize
= sizeof(TTTOOLINFOW
);
145 ti
.hwnd
= tray_window
;
146 ti
.lpszText
= icon
->tiptext
;
147 if (icon
->display
!= -1)
149 ti
.rect
.left
= icon_cx
* icon
->display
;
150 ti
.rect
.right
= icon_cx
* (icon
->display
+ 1);
152 ti
.rect
.bottom
= icon_cy
;
154 SendMessageW(icon
->tooltip
, TTM_ADDTOOLW
, 0, (LPARAM
)&ti
);
157 /* Synchronize tooltip text with tooltip window */
158 static void update_tooltip_text(struct icon
*icon
)
162 ZeroMemory(&ti
, sizeof(ti
));
163 ti
.cbSize
= sizeof(TTTOOLINFOW
);
164 ti
.hwnd
= tray_window
;
165 ti
.lpszText
= icon
->tiptext
;
167 SendMessageW(icon
->tooltip
, TTM_UPDATETIPTEXTW
, 0, (LPARAM
)&ti
);
170 /* synchronize tooltip position with tooltip window */
171 static void update_tooltip_position( struct icon
*icon
)
175 ZeroMemory(&ti
, sizeof(ti
));
176 ti
.cbSize
= sizeof(TTTOOLINFOW
);
177 ti
.hwnd
= tray_window
;
178 if (icon
->display
!= -1)
180 ti
.rect
.left
= icon_cx
* icon
->display
;
181 ti
.rect
.right
= icon_cx
* (icon
->display
+ 1);
183 ti
.rect
.bottom
= icon_cy
;
185 SendMessageW( icon
->tooltip
, TTM_NEWTOOLRECTW
, 0, (LPARAM
)&ti
);
188 /* find the icon located at a certain point in the tray window */
189 static struct icon
*icon_from_point( int x
, int y
)
191 if (y
< 0 || y
>= icon_cy
) return NULL
;
192 if (x
< 0 || x
>= icon_cx
* nb_displayed
) return NULL
;
193 return displayed
[x
/ icon_cx
];
196 /* invalidate the portion of the tray window that contains the specified icons */
197 static void invalidate_icons( unsigned int start
, unsigned int end
)
201 rect
.left
= start
* icon_cx
;
203 rect
.right
= (end
+ 1) * icon_cx
;
204 rect
.bottom
= icon_cy
;
205 InvalidateRect( tray_window
, &rect
, TRUE
);
208 /* make an icon visible */
209 static BOOL
show_icon(struct icon
*icon
)
211 WINE_TRACE("id=0x%x, hwnd=%p\n", icon
->id
, icon
->owner
);
213 if (icon
->display
!= -1) return TRUE
; /* already displayed */
215 if (nb_displayed
>= alloc_displayed
)
217 unsigned int new_count
= max( alloc_displayed
* 2, 32 );
219 if (displayed
) ptr
= HeapReAlloc( GetProcessHeap(), 0, displayed
, new_count
* sizeof(*ptr
) );
220 else ptr
= HeapAlloc( GetProcessHeap(), 0, new_count
* sizeof(*ptr
) );
221 if (!ptr
) return FALSE
;
223 alloc_displayed
= new_count
;
226 icon
->display
= nb_displayed
;
227 displayed
[nb_displayed
++] = icon
;
228 update_tooltip_position( icon
);
229 invalidate_icons( nb_displayed
-1, nb_displayed
-1 );
231 if (nb_displayed
> MIN_DISPLAYED
)
233 SIZE size
= get_window_size();
234 SetWindowPos( tray_window
, 0, 0, 0, size
.cx
, size
.cy
, SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOMOVE
);
236 else if (nb_displayed
== 1)
238 if (!hide_systray
) ShowWindow( tray_window
, SW_SHOWNA
);
241 create_tooltip(icon
);
245 /* make an icon invisible */
246 static BOOL
hide_icon(struct icon
*icon
)
250 WINE_TRACE("id=0x%x, hwnd=%p\n", icon
->id
, icon
->owner
);
252 if (icon
->display
== -1) return TRUE
; /* already hidden */
254 assert( nb_displayed
);
255 for (i
= icon
->display
; i
< nb_displayed
- 1; i
++)
257 displayed
[i
] = displayed
[i
+ 1];
258 displayed
[i
]->display
= i
;
259 update_tooltip_position( displayed
[i
] );
262 invalidate_icons( icon
->display
, nb_displayed
);
265 if (nb_displayed
>= MIN_DISPLAYED
)
267 SIZE size
= get_window_size();
268 SetWindowPos( tray_window
, 0, 0, 0, size
.cx
, size
.cy
, SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOMOVE
);
270 else if (!nb_displayed
)
272 ShowWindow( tray_window
, SW_HIDE
);
275 update_tooltip_position( icon
);
279 /* Modifies an existing icon record */
280 static BOOL
modify_icon( struct icon
*icon
, NOTIFYICONDATAW
*nid
)
282 WINE_TRACE("id=0x%x, hwnd=%p\n", nid
->uID
, nid
->hWnd
);
284 /* demarshal the request from the NID */
287 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid
->uID
, nid
->hWnd
);
291 if ((nid
->uFlags
& NIF_STATE
) && (nid
->dwStateMask
& NIS_HIDDEN
))
293 if (nid
->dwState
& NIS_HIDDEN
) hide_icon( icon
);
294 else show_icon( icon
);
297 if (nid
->uFlags
& NIF_ICON
)
299 if (icon
->image
) DestroyIcon(icon
->image
);
300 icon
->image
= CopyIcon(nid
->hIcon
);
301 if (icon
->display
!= -1) invalidate_icons( icon
->display
, icon
->display
);
304 if (nid
->uFlags
& NIF_MESSAGE
)
306 icon
->callback_message
= nid
->uCallbackMessage
;
308 if (nid
->uFlags
& NIF_TIP
)
310 lstrcpynW(icon
->tiptext
, nid
->szTip
, sizeof(icon
->tiptext
)/sizeof(WCHAR
));
311 if (icon
->display
!= -1) update_tooltip_text(icon
);
313 if (nid
->uFlags
& NIF_INFO
&& nid
->cbSize
>= NOTIFYICONDATAA_V2_SIZE
)
315 WINE_FIXME("balloon tip title %s, message %s\n", wine_dbgstr_w(nid
->szInfoTitle
), wine_dbgstr_w(nid
->szInfo
));
320 /* Adds a new icon record to the list */
321 static BOOL
add_icon(NOTIFYICONDATAW
*nid
)
325 WINE_TRACE("id=0x%x, hwnd=%p\n", nid
->uID
, nid
->hWnd
);
327 if ((icon
= get_icon(nid
->hWnd
, nid
->uID
)))
329 WINE_WARN("duplicate tray icon add, buggy app?\n");
333 if (!(icon
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*icon
))))
335 WINE_ERR("out of memory\n");
339 ZeroMemory(icon
, sizeof(struct icon
));
341 icon
->owner
= nid
->hWnd
;
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
);
362 /* cleanup icons belonging to windows that have been destroyed */
363 static void cleanup_destroyed_windows(void)
365 struct icon
*icon
, *next
;
367 LIST_FOR_EACH_ENTRY_SAFE( icon
, next
, &icon_list
, struct icon
, entry
)
368 if (!IsWindow( icon
->owner
)) delete_icon( icon
);
371 static BOOL
handle_incoming(HWND hwndSource
, COPYDATASTRUCT
*cds
)
373 struct icon
*icon
= NULL
;
374 const struct notify_data
*data
;
378 if (cds
->cbData
< sizeof(*data
)) return FALSE
;
381 nid
.cbSize
= sizeof(nid
);
382 nid
.hWnd
= LongToHandle( data
->hWnd
);
384 nid
.uFlags
= data
->uFlags
;
385 nid
.uCallbackMessage
= data
->uCallbackMessage
;
387 nid
.dwState
= data
->dwState
;
388 nid
.dwStateMask
= data
->dwStateMask
;
389 nid
.u
.uTimeout
= data
->u
.uTimeout
;
390 nid
.dwInfoFlags
= data
->dwInfoFlags
;
391 nid
.guidItem
= data
->guidItem
;
392 lstrcpyW( nid
.szTip
, data
->szTip
);
393 lstrcpyW( nid
.szInfo
, data
->szInfo
);
394 lstrcpyW( nid
.szInfoTitle
, data
->szInfoTitle
);
395 nid
.hBalloonIcon
= 0;
397 /* FIXME: if statement only needed because we don't support interprocess
399 if ((nid
.uFlags
& NIF_ICON
) && cds
->cbData
> sizeof(*data
))
403 const char *buffer
= (const char *)(data
+ 1);
405 cbMaskBits
= (data
->width
* data
->height
+ 15) / 16 * 2;
406 cbColourBits
= (data
->planes
* data
->width
* data
->height
* data
->bpp
+ 15) / 16 * 2;
408 if (cds
->cbData
< sizeof(*data
) + cbMaskBits
+ cbColourBits
)
410 WINE_ERR("buffer underflow\n");
413 nid
.hIcon
= CreateIcon(NULL
, data
->width
, data
->height
, data
->planes
, data
->bpp
,
414 buffer
, buffer
+ cbMaskBits
);
417 /* try forward to x11drv first */
418 if (cds
->dwData
== NIM_ADD
|| !(icon
= get_icon( nid
.hWnd
, nid
.uID
)))
420 if (wine_notify_icon
&& ((ret
= wine_notify_icon( cds
->dwData
, &nid
)) != -1))
422 if (nid
.hIcon
) DestroyIcon( nid
.hIcon
);
431 ret
= add_icon(&nid
);
434 if (icon
) ret
= delete_icon( icon
);
437 if (icon
) ret
= modify_icon( icon
, &nid
);
440 WINE_FIXME("unhandled tray message: %ld\n", cds
->dwData
);
444 if (nid
.hIcon
) DestroyIcon( nid
.hIcon
);
448 static void do_hide_systray(void)
450 SetWindowPos( tray_window
, 0,
451 GetSystemMetrics(SM_XVIRTUALSCREEN
) + GetSystemMetrics(SM_CXVIRTUALSCREEN
),
452 GetSystemMetrics(SM_YVIRTUALSCREEN
) + GetSystemMetrics(SM_CYVIRTUALSCREEN
),
453 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
456 static LRESULT WINAPI
tray_wndproc( HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
461 return handle_incoming((HWND
)wparam
, (COPYDATASTRUCT
*)lparam
);
463 case WM_DISPLAYCHANGE
:
464 if (hide_systray
) do_hide_systray();
468 cleanup_destroyed_windows();
477 hdc
= BeginPaint( hwnd
, &ps
);
478 for (i
= ps
.rcPaint
.left
/ icon_cx
;
479 (i
< (ps
.rcPaint
.right
+ icon_cx
- 1) / icon_cx
) && (i
< nb_displayed
);
482 DrawIconEx( hdc
, i
* icon_cx
+ ICON_BORDER
, ICON_BORDER
, displayed
[i
]->image
,
483 icon_cx
- 2*ICON_BORDER
, icon_cy
- 2*ICON_BORDER
,
484 0, 0, DI_DEFAULTSIZE
|DI_NORMAL
);
486 EndPaint( hwnd
, &ps
);
497 case WM_LBUTTONDBLCLK
:
498 case WM_RBUTTONDBLCLK
:
499 case WM_MBUTTONDBLCLK
:
502 struct icon
*icon
= icon_from_point( (short)LOWORD(lparam
), (short)HIWORD(lparam
) );
505 /* notify the owner hwnd of the message */
506 WINE_TRACE("relaying 0x%x\n", msg
);
509 message
.message
= msg
;
510 message
.wParam
= wparam
;
511 message
.lParam
= lparam
;
512 SendMessageW( icon
->tooltip
, TTM_RELAYEVENT
, 0, (LPARAM
)&message
);
514 if (!PostMessageW( icon
->owner
, icon
->callback_message
, (WPARAM
) icon
->id
, (LPARAM
) msg
) &&
515 GetLastError() == ERROR_INVALID_WINDOW_HANDLE
)
517 WINE_WARN("application window was destroyed without removing "
518 "notification icon, removing automatically\n");
525 /* don't destroy the tray window, just hide it */
526 ShowWindow( hwnd
, SW_HIDE
);
530 return DefWindowProcW( hwnd
, msg
, wparam
, lparam
);
535 static BOOL
is_systray_hidden(void)
537 const WCHAR show_systray_keyname
[] = {'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
538 'X','1','1',' ','D','r','i','v','e','r',0};
539 const WCHAR show_systray_valuename
[] = {'S','h','o','w','S','y','s','t','r','a','y',0};
543 /* @@ Wine registry key: HKCU\Software\Wine\X11 Driver */
544 if (RegOpenKeyW(HKEY_CURRENT_USER
, show_systray_keyname
, &hkey
) == ERROR_SUCCESS
)
547 DWORD type
, size
= sizeof(value
);
548 if (RegQueryValueExW(hkey
, show_systray_valuename
, 0, &type
, (LPBYTE
)&value
, &size
) == ERROR_SUCCESS
)
550 ret
= IS_OPTION_FALSE(value
[0]);
557 /* this function creates the listener window */
558 void initialize_systray(void)
563 static const WCHAR classname
[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
564 static const WCHAR winname
[] = {'W','i','n','e',' ','S','y','s','t','e','m',' ','T','r','a','y',0};
566 if ((x11drv
= GetModuleHandleA( "winex11.drv" )))
567 wine_notify_icon
= (void *)GetProcAddress( x11drv
, "wine_notify_icon" );
569 icon_cx
= GetSystemMetrics( SM_CXSMICON
) + 2*ICON_BORDER
;
570 icon_cy
= GetSystemMetrics( SM_CYSMICON
) + 2*ICON_BORDER
;
571 hide_systray
= is_systray_hidden();
573 /* register the systray listener window class */
574 ZeroMemory(&class, sizeof(class));
575 class.cbSize
= sizeof(class);
576 class.style
= CS_DBLCLKS
;
577 class.lpfnWndProc
= tray_wndproc
;
578 class.hInstance
= NULL
;
579 class.hIcon
= LoadIconW(0, (LPCWSTR
)IDI_WINLOGO
);
580 class.hCursor
= LoadCursorW(0, (LPCWSTR
)IDC_ARROW
);
581 class.hbrBackground
= (HBRUSH
) COLOR_WINDOW
;
582 class.lpszClassName
= (WCHAR
*) &classname
;
584 if (!RegisterClassExW(&class))
586 WINE_ERR("Could not register SysTray window class\n");
590 size
= get_window_size();
591 tray_window
= CreateWindowW( classname
, winname
, WS_OVERLAPPED
| WS_CAPTION
,
592 CW_USEDEFAULT
, CW_USEDEFAULT
, size
.cx
, size
.cy
, 0, 0, 0, 0 );
595 WINE_ERR("Could not create tray window\n");
599 if (hide_systray
) do_hide_systray();
601 SetTimer( tray_window
, 1, 2000, NULL
);