d3d11/tests: Add test for writing floating-point specials to render target.
[wine.git] / programs / explorer / systray.c
blob97cffbcbf914a1f2a32bfe258ac61070bda97852
1 /*
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
21 #include <assert.h>
23 #define NONAMELESSUNION
24 #define _WIN32_IE 0x500
25 #include <windows.h>
26 #include <commctrl.h>
27 #include <shellapi.h>
29 #include <wine/debug.h>
30 #include <wine/list.h>
32 #include "explorer_private.h"
33 #include "resource.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(systray);
37 struct notify_data /* platform-independent format for NOTIFYICONDATA */
39 LONG hWnd;
40 UINT uID;
41 UINT uFlags;
42 UINT uCallbackMessage;
43 WCHAR szTip[128];
44 DWORD dwState;
45 DWORD dwStateMask;
46 WCHAR szInfo[256];
47 union {
48 UINT uTimeout;
49 UINT uVersion;
50 } u;
51 WCHAR szInfoTitle[64];
52 DWORD dwInfoFlags;
53 GUID guidItem;
54 /* data for the icon bitmap */
55 UINT width;
56 UINT height;
57 UINT planes;
58 UINT bpp;
61 static int (CDECL *wine_notify_icon)(DWORD,NOTIFYICONDATAW *);
63 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
64 struct icon
66 struct list entry;
67 HICON image; /* the image to render */
68 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
69 HWND tooltip; /* Icon tooltip */
70 UINT state; /* state flags */
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 */
75 WCHAR info_text[256]; /* info balloon text */
76 WCHAR info_title[64]; /* info balloon title */
77 UINT info_flags; /* flags for info balloon */
78 UINT info_timeout; /* timeout for info balloon */
79 HICON info_icon; /* info balloon icon */
82 static struct list icon_list = LIST_INIT( icon_list );
84 struct taskbar_button
86 struct list entry;
87 HWND hwnd;
88 HWND button;
89 BOOL active;
90 BOOL visible;
93 static struct list taskbar_buttons = LIST_INIT( taskbar_buttons );
95 static HWND tray_window;
97 static unsigned int alloc_displayed;
98 static unsigned int nb_displayed;
99 static struct icon **displayed; /* array of currently displayed icons */
101 static BOOL hide_systray, enable_shell;
102 static int icon_cx, icon_cy, tray_width, tray_height;
103 static int start_button_width, taskbar_button_width;
104 static WCHAR start_label[50];
106 static struct icon *balloon_icon;
107 static HWND balloon_window;
109 #define MIN_DISPLAYED 8
110 #define ICON_BORDER 2
112 #define BALLOON_CREATE_TIMER 1
113 #define BALLOON_SHOW_TIMER 2
115 #define BALLOON_CREATE_TIMEOUT 2000
116 #define BALLOON_SHOW_MIN_TIMEOUT 10000
117 #define BALLOON_SHOW_MAX_TIMEOUT 30000
119 #define WM_POPUPSYSTEMMENU 0x0313
121 static void do_hide_systray(void);
122 static void do_show_systray(void);
124 /* Retrieves icon record by owner window and ID */
125 static struct icon *get_icon(HWND owner, UINT id)
127 struct icon *this;
129 /* search for the icon */
130 LIST_FOR_EACH_ENTRY( this, &icon_list, struct icon, entry )
131 if ((this->id == id) && (this->owner == owner)) return this;
133 return NULL;
136 static RECT get_icon_rect( struct icon *icon )
138 RECT rect;
140 rect.right = tray_width - icon_cx * icon->display;
141 rect.left = rect.right - icon_cx;
142 rect.top = (tray_height - icon_cy) / 2;
143 rect.bottom = rect.top + icon_cy;
144 return rect;
147 static void init_common_controls(void)
149 static BOOL initialized = FALSE;
151 if (!initialized)
153 INITCOMMONCONTROLSEX init_tooltip;
155 init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
156 init_tooltip.dwICC = ICC_TAB_CLASSES|ICC_STANDARD_CLASSES;
158 InitCommonControlsEx(&init_tooltip);
159 initialized = TRUE;
163 /* Creates tooltip window for icon. */
164 static void create_tooltip(struct icon *icon)
166 TTTOOLINFOW ti;
168 init_common_controls();
169 icon->tooltip = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
170 WS_POPUP | TTS_ALWAYSTIP,
171 CW_USEDEFAULT, CW_USEDEFAULT,
172 CW_USEDEFAULT, CW_USEDEFAULT,
173 tray_window, NULL, NULL, NULL);
175 ZeroMemory(&ti, sizeof(ti));
176 ti.cbSize = sizeof(TTTOOLINFOW);
177 ti.hwnd = tray_window;
178 ti.lpszText = icon->tiptext;
179 if (icon->display != -1) ti.rect = get_icon_rect( icon );
180 SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
183 static void set_balloon_position( struct icon *icon )
185 RECT rect = get_icon_rect( icon );
186 POINT pos;
188 MapWindowPoints( tray_window, 0, (POINT *)&rect, 2 );
189 pos.x = (rect.left + rect.right) / 2;
190 pos.y = (rect.top + rect.bottom) / 2;
191 SendMessageW( balloon_window, TTM_TRACKPOSITION, 0, MAKELONG( pos.x, pos.y ));
194 static void balloon_create_timer(void)
196 TTTOOLINFOW ti;
198 init_common_controls();
199 balloon_window = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
200 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE,
201 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
202 tray_window, NULL, NULL, NULL);
204 memset( &ti, 0, sizeof(ti) );
205 ti.cbSize = sizeof(TTTOOLINFOW);
206 ti.hwnd = tray_window;
207 ti.uFlags = TTF_TRACK;
208 ti.lpszText = balloon_icon->info_text;
209 SendMessageW( balloon_window, TTM_ADDTOOLW, 0, (LPARAM)&ti );
210 if ((balloon_icon->info_flags & NIIF_ICONMASK) == NIIF_USER)
211 SendMessageW( balloon_window, TTM_SETTITLEW, (WPARAM)balloon_icon->info_icon,
212 (LPARAM)balloon_icon->info_title );
213 else
214 SendMessageW( balloon_window, TTM_SETTITLEW, balloon_icon->info_flags,
215 (LPARAM)balloon_icon->info_title );
216 set_balloon_position( balloon_icon );
217 SendMessageW( balloon_window, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti );
218 KillTimer( tray_window, BALLOON_CREATE_TIMER );
219 SetTimer( tray_window, BALLOON_SHOW_TIMER, balloon_icon->info_timeout, NULL );
222 static BOOL show_balloon( struct icon *icon )
224 if (icon->display == -1) return FALSE; /* not displayed */
225 if (!icon->info_text[0]) return FALSE; /* no balloon */
226 balloon_icon = icon;
227 SetTimer( tray_window, BALLOON_CREATE_TIMER, BALLOON_CREATE_TIMEOUT, NULL );
228 return TRUE;
231 static void hide_balloon(void)
233 if (!balloon_icon) return;
234 if (balloon_window)
236 KillTimer( tray_window, BALLOON_SHOW_TIMER );
237 DestroyWindow( balloon_window );
238 balloon_window = 0;
240 else KillTimer( tray_window, BALLOON_CREATE_TIMER );
241 balloon_icon = NULL;
244 static void show_next_balloon(void)
246 struct icon *icon;
248 LIST_FOR_EACH_ENTRY( icon, &icon_list, struct icon, entry )
249 if (show_balloon( icon )) break;
252 static void update_balloon( struct icon *icon )
254 if (balloon_icon == icon)
256 hide_balloon();
257 show_balloon( icon );
259 else if (!balloon_icon)
261 if (!show_balloon( icon )) return;
263 if (!balloon_icon) show_next_balloon();
266 static void balloon_timer(void)
268 if (balloon_icon) balloon_icon->info_text[0] = 0; /* clear text now that balloon has been shown */
269 hide_balloon();
270 show_next_balloon();
273 /* Synchronize tooltip text with tooltip window */
274 static void update_tooltip_text(struct icon *icon)
276 TTTOOLINFOW ti;
278 ZeroMemory(&ti, sizeof(ti));
279 ti.cbSize = sizeof(TTTOOLINFOW);
280 ti.hwnd = tray_window;
281 ti.lpszText = icon->tiptext;
283 SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
286 /* synchronize tooltip position with tooltip window */
287 static void update_tooltip_position( struct icon *icon )
289 TTTOOLINFOW ti;
291 ZeroMemory(&ti, sizeof(ti));
292 ti.cbSize = sizeof(TTTOOLINFOW);
293 ti.hwnd = tray_window;
294 if (icon->display != -1) ti.rect = get_icon_rect( icon );
295 SendMessageW( icon->tooltip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti );
296 if (balloon_icon == icon) set_balloon_position( icon );
299 /* find the icon located at a certain point in the tray window */
300 static struct icon *icon_from_point( int x, int y )
302 if (y < 0 || y >= icon_cy) return NULL;
303 x = tray_width - x;
304 if (x < 0 || x >= icon_cx * nb_displayed) return NULL;
305 return displayed[x / icon_cx];
308 /* invalidate the portion of the tray window that contains the specified icons */
309 static void invalidate_icons( unsigned int start, unsigned int end )
311 RECT rect;
313 rect.left = tray_width - (end + 1) * icon_cx;
314 rect.top = (tray_height - icon_cy) / 2;
315 rect.right = tray_width - start * icon_cx;
316 rect.bottom = rect.top + icon_cy;
317 InvalidateRect( tray_window, &rect, TRUE );
320 /* make an icon visible */
321 static BOOL show_icon(struct icon *icon)
323 WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
325 if (icon->display != -1) return TRUE; /* already displayed */
327 if (nb_displayed >= alloc_displayed)
329 unsigned int new_count = max( alloc_displayed * 2, 32 );
330 struct icon **ptr;
331 if (displayed) ptr = HeapReAlloc( GetProcessHeap(), 0, displayed, new_count * sizeof(*ptr) );
332 else ptr = HeapAlloc( GetProcessHeap(), 0, new_count * sizeof(*ptr) );
333 if (!ptr) return FALSE;
334 displayed = ptr;
335 alloc_displayed = new_count;
338 icon->display = nb_displayed;
339 displayed[nb_displayed++] = icon;
340 update_tooltip_position( icon );
341 invalidate_icons( nb_displayed-1, nb_displayed-1 );
343 if (nb_displayed == 1 && !hide_systray) do_show_systray();
345 create_tooltip(icon);
346 update_balloon( icon );
347 return TRUE;
350 /* make an icon invisible */
351 static BOOL hide_icon(struct icon *icon)
353 unsigned int i;
355 WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
357 if (icon->display == -1) return TRUE; /* already hidden */
359 assert( nb_displayed );
360 for (i = icon->display; i < nb_displayed - 1; i++)
362 displayed[i] = displayed[i + 1];
363 displayed[i]->display = i;
364 update_tooltip_position( displayed[i] );
366 nb_displayed--;
367 invalidate_icons( icon->display, nb_displayed );
368 icon->display = -1;
370 if (!nb_displayed && !enable_shell) do_hide_systray();
372 update_balloon( icon );
373 update_tooltip_position( icon );
374 return TRUE;
377 /* Modifies an existing icon record */
378 static BOOL modify_icon( struct icon *icon, NOTIFYICONDATAW *nid )
380 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
382 /* demarshal the request from the NID */
383 if (!icon)
385 WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
386 return FALSE;
389 if (nid->uFlags & NIF_STATE)
391 icon->state = (icon->state & ~nid->dwStateMask) | (nid->dwState & nid->dwStateMask);
394 if (nid->uFlags & NIF_ICON)
396 if (icon->image) DestroyIcon(icon->image);
397 icon->image = CopyIcon(nid->hIcon);
398 if (icon->display != -1) invalidate_icons( icon->display, icon->display );
401 if (nid->uFlags & NIF_MESSAGE)
403 icon->callback_message = nid->uCallbackMessage;
405 if (nid->uFlags & NIF_TIP)
407 lstrcpynW(icon->tiptext, nid->szTip, sizeof(icon->tiptext)/sizeof(WCHAR));
408 if (icon->display != -1) update_tooltip_text(icon);
410 if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
412 lstrcpynW( icon->info_text, nid->szInfo, sizeof(icon->info_text)/sizeof(WCHAR) );
413 lstrcpynW( icon->info_title, nid->szInfoTitle, sizeof(icon->info_title)/sizeof(WCHAR) );
414 icon->info_flags = nid->dwInfoFlags;
415 icon->info_timeout = max(min(nid->u.uTimeout, BALLOON_SHOW_MAX_TIMEOUT), BALLOON_SHOW_MIN_TIMEOUT);
416 icon->info_icon = nid->hBalloonIcon;
417 update_balloon( icon );
419 if (icon->state & NIS_HIDDEN) hide_icon( icon );
420 else show_icon( icon );
421 return TRUE;
424 /* Adds a new icon record to the list */
425 static BOOL add_icon(NOTIFYICONDATAW *nid)
427 struct icon *icon;
429 WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
431 if ((icon = get_icon(nid->hWnd, nid->uID)))
433 WINE_WARN("duplicate tray icon add, buggy app?\n");
434 return FALSE;
437 if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
439 WINE_ERR("out of memory\n");
440 return FALSE;
443 ZeroMemory(icon, sizeof(struct icon));
444 icon->id = nid->uID;
445 icon->owner = nid->hWnd;
446 icon->display = -1;
448 list_add_tail(&icon_list, &icon->entry);
450 return modify_icon( icon, nid );
453 /* Deletes tray icon window and icon record */
454 static BOOL delete_icon(struct icon *icon)
456 hide_icon(icon);
457 list_remove(&icon->entry);
458 DestroyIcon(icon->image);
459 HeapFree(GetProcessHeap(), 0, icon);
460 return TRUE;
463 /* cleanup icons belonging to a window that has been destroyed */
464 static void cleanup_systray_window( HWND hwnd )
466 struct icon *icon, *next;
468 LIST_FOR_EACH_ENTRY_SAFE( icon, next, &icon_list, struct icon, entry )
469 if (icon->owner == hwnd) delete_icon( icon );
471 if (wine_notify_icon)
473 NOTIFYICONDATAW nid = { sizeof(nid), hwnd };
474 wine_notify_icon( 0xdead, &nid );
478 /* update the taskbar buttons when something changed */
479 static void sync_taskbar_buttons(void)
481 struct taskbar_button *win;
482 int pos = 0, count = 0;
483 int width = taskbar_button_width;
484 int right = tray_width - nb_displayed * icon_cx;
485 HWND foreground = GetAncestor( GetForegroundWindow(), GA_ROOTOWNER );
487 if (!IsWindowVisible( tray_window )) return;
489 LIST_FOR_EACH_ENTRY( win, &taskbar_buttons, struct taskbar_button, entry )
491 if (!win->hwnd) /* start button */
493 SetWindowPos( win->button, 0, pos, 0, start_button_width, tray_height,
494 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW );
495 pos += start_button_width;
496 continue;
498 win->active = (win->hwnd == foreground);
499 win->visible = IsWindowVisible( win->hwnd ) && !GetWindow( win->hwnd, GW_OWNER );
500 if (win->visible) count++;
503 /* shrink buttons if space is tight */
504 if (count && (count * width > right - pos))
505 width = max( taskbar_button_width / 4, (right - pos) / count );
507 LIST_FOR_EACH_ENTRY( win, &taskbar_buttons, struct taskbar_button, entry )
509 if (!win->hwnd) continue; /* start button */
510 if (win->visible && right - pos >= width)
512 SetWindowPos( win->button, 0, pos, 0, width, tray_height,
513 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW );
514 InvalidateRect( win->button, NULL, TRUE );
515 pos += width;
517 else SetWindowPos( win->button, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
521 static BOOL handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
523 struct icon *icon = NULL;
524 const struct notify_data *data;
525 NOTIFYICONDATAW nid;
526 int ret = FALSE;
528 if (cds->cbData < sizeof(*data)) return FALSE;
529 data = cds->lpData;
531 nid.cbSize = sizeof(nid);
532 nid.hWnd = LongToHandle( data->hWnd );
533 nid.uID = data->uID;
534 nid.uFlags = data->uFlags;
535 nid.uCallbackMessage = data->uCallbackMessage;
536 nid.hIcon = 0;
537 nid.dwState = data->dwState;
538 nid.dwStateMask = data->dwStateMask;
539 nid.u.uTimeout = data->u.uTimeout;
540 nid.dwInfoFlags = data->dwInfoFlags;
541 nid.guidItem = data->guidItem;
542 lstrcpyW( nid.szTip, data->szTip );
543 lstrcpyW( nid.szInfo, data->szInfo );
544 lstrcpyW( nid.szInfoTitle, data->szInfoTitle );
545 nid.hBalloonIcon = 0;
547 /* FIXME: if statement only needed because we don't support interprocess
548 * icon handles */
549 if ((nid.uFlags & NIF_ICON) && cds->cbData > sizeof(*data))
551 LONG cbMaskBits;
552 LONG cbColourBits;
553 const char *buffer = (const char *)(data + 1);
555 cbMaskBits = (data->width * data->height + 15) / 16 * 2;
556 cbColourBits = (data->planes * data->width * data->height * data->bpp + 15) / 16 * 2;
558 if (cds->cbData < sizeof(*data) + cbMaskBits + cbColourBits)
560 WINE_ERR("buffer underflow\n");
561 return FALSE;
563 nid.hIcon = CreateIcon(NULL, data->width, data->height, data->planes, data->bpp,
564 buffer, buffer + cbMaskBits);
567 /* try forward to x11drv first */
568 if (cds->dwData == NIM_ADD || !(icon = get_icon( nid.hWnd, nid.uID )))
570 if (wine_notify_icon && ((ret = wine_notify_icon( cds->dwData, &nid )) != -1))
572 if (nid.hIcon) DestroyIcon( nid.hIcon );
573 return ret;
575 ret = FALSE;
578 switch (cds->dwData)
580 case NIM_ADD:
581 ret = add_icon(&nid);
582 break;
583 case NIM_DELETE:
584 if (icon) ret = delete_icon( icon );
585 break;
586 case NIM_MODIFY:
587 if (icon) ret = modify_icon( icon, &nid );
588 break;
589 default:
590 WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
591 break;
594 if (nid.hIcon) DestroyIcon( nid.hIcon );
595 sync_taskbar_buttons();
596 return ret;
599 static void add_taskbar_button( HWND hwnd )
601 struct taskbar_button *win;
603 if (hide_systray) return;
605 /* ignore our own windows */
606 if (hwnd)
608 DWORD process;
609 if (!GetWindowThreadProcessId( hwnd, &process ) || process == GetCurrentProcessId()) return;
612 if (!(win = HeapAlloc( GetProcessHeap(), 0, sizeof(*win) ))) return;
613 win->hwnd = hwnd;
614 win->button = CreateWindowW( WC_BUTTONW, NULL, WS_CHILD | BS_OWNERDRAW,
615 0, 0, 0, 0, tray_window, (HMENU)hwnd, 0, 0 );
616 list_add_tail( &taskbar_buttons, &win->entry );
619 static struct taskbar_button *find_taskbar_button( HWND hwnd )
621 struct taskbar_button *win;
623 LIST_FOR_EACH_ENTRY( win, &taskbar_buttons, struct taskbar_button, entry )
624 if (win->hwnd == hwnd) return win;
626 return NULL;
629 static void remove_taskbar_button( HWND hwnd )
631 struct taskbar_button *win = find_taskbar_button( hwnd );
633 if (!win) return;
634 list_remove( &win->entry );
635 DestroyWindow( win->button );
636 HeapFree( GetProcessHeap(), 0, win );
639 static void paint_taskbar_button( const DRAWITEMSTRUCT *dis )
641 RECT rect;
642 UINT flags = DC_TEXT;
643 struct taskbar_button *win = find_taskbar_button( LongToHandle( dis->CtlID ));
645 if (!win) return;
646 GetClientRect( dis->hwndItem, &rect );
647 DrawFrameControl( dis->hDC, &rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_ADJUSTRECT |
648 ((dis->itemState & ODS_SELECTED) ? DFCS_PUSHED : 0 ));
649 if (win->hwnd)
651 flags |= win->active ? DC_ACTIVE : DC_INBUTTON;
652 DrawCaptionTempW( win->hwnd, dis->hDC, &rect, 0, 0, NULL, flags );
654 else /* start button */
655 DrawCaptionTempW( 0, dis->hDC, &rect, 0, 0, start_label, flags | DC_INBUTTON | DC_ICON );
658 static void click_taskbar_button( HWND button )
660 LONG_PTR id = GetWindowLongPtrW( button, GWLP_ID );
661 HWND hwnd = (HWND)id;
663 if (!hwnd) /* start button */
665 do_startmenu( tray_window );
666 return;
669 if (IsIconic( hwnd ))
671 SendMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0 );
672 return;
675 if (IsWindowEnabled( hwnd ))
677 if (hwnd == GetForegroundWindow())
679 SendMessageW( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 );
680 return;
683 else /* look for an enabled window owned by this one */
685 HWND owned = GetWindow( GetDesktopWindow(), GW_CHILD );
686 while (owned && owned != hwnd)
688 if (IsWindowVisible( owned ) &&
689 IsWindowEnabled( owned ) &&
690 (GetWindow( owned, GW_OWNER ) == hwnd))
691 break;
692 owned = GetWindow( owned, GW_HWNDNEXT );
694 hwnd = owned;
696 SetForegroundWindow( hwnd );
699 static void show_taskbar_contextmenu( HWND button, LPARAM lparam )
701 ULONG_PTR id = GetWindowLongPtrW( button, GWLP_ID );
703 if (id) SendNotifyMessageW( (HWND)id, WM_POPUPSYSTEMMENU, 0, lparam );
706 static void do_hide_systray(void)
708 SetWindowPos( tray_window, 0,
709 GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN),
710 GetSystemMetrics(SM_YVIRTUALSCREEN) + GetSystemMetrics(SM_CYVIRTUALSCREEN),
711 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
714 static void do_show_systray(void)
716 SIZE size;
717 NONCLIENTMETRICSW ncm;
718 HFONT font;
719 HDC hdc = GetDC( 0 );
721 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
722 SystemParametersInfoW( SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0 );
723 font = CreateFontIndirectW( &ncm.lfCaptionFont );
724 /* FIXME: Implement BCM_GETIDEALSIZE and use that instead. */
725 SelectObject( hdc, font );
726 GetTextExtentPointA( hdc, "abcdefghijklmnopqrstuvwxyz", 26, &size );
727 taskbar_button_width = size.cx;
728 GetTextExtentPointW( hdc, start_label, lstrlenW(start_label), &size );
729 /* add some margins (FIXME) */
730 size.cx += 12 + GetSystemMetrics( SM_CXSMICON );
731 size.cy += 4;
732 ReleaseDC( 0, hdc );
733 DeleteObject( font );
735 tray_width = GetSystemMetrics( SM_CXSCREEN );
736 tray_height = max( icon_cy, size.cy );
737 start_button_width = size.cx;
738 SetWindowPos( tray_window, HWND_TOPMOST, 0, GetSystemMetrics( SM_CYSCREEN ) - tray_height,
739 tray_width, tray_height, SWP_NOACTIVATE | SWP_SHOWWINDOW );
740 sync_taskbar_buttons();
743 static LRESULT WINAPI tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
745 switch (msg)
747 case WM_COPYDATA:
748 return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
750 case WM_DISPLAYCHANGE:
751 if (hide_systray || (!nb_displayed && !enable_shell)) do_hide_systray();
752 else do_show_systray();
753 break;
755 case WM_TIMER:
756 switch (wparam)
758 case BALLOON_CREATE_TIMER: balloon_create_timer(); break;
759 case BALLOON_SHOW_TIMER: balloon_timer(); break;
761 break;
763 case WM_PAINT:
765 unsigned int i;
766 PAINTSTRUCT ps;
767 HDC hdc;
769 hdc = BeginPaint( hwnd, &ps );
770 for (i = 0; i < nb_displayed; i++)
772 RECT dummy, rect = get_icon_rect( displayed[i] );
773 if (IntersectRect( &dummy, &rect, &ps.rcPaint ))
774 DrawIconEx( hdc, rect.left + ICON_BORDER, rect.top + ICON_BORDER, displayed[i]->image,
775 icon_cx - 2*ICON_BORDER, icon_cy - 2*ICON_BORDER,
776 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
778 EndPaint( hwnd, &ps );
779 break;
782 case WM_MOUSEMOVE:
783 case WM_LBUTTONDOWN:
784 case WM_LBUTTONUP:
785 case WM_RBUTTONDOWN:
786 case WM_RBUTTONUP:
787 case WM_MBUTTONDOWN:
788 case WM_MBUTTONUP:
789 case WM_LBUTTONDBLCLK:
790 case WM_RBUTTONDBLCLK:
791 case WM_MBUTTONDBLCLK:
793 MSG message;
794 struct icon *icon = icon_from_point( (short)LOWORD(lparam), (short)HIWORD(lparam) );
795 if (!icon) break;
797 /* notify the owner hwnd of the message */
798 WINE_TRACE("relaying 0x%x\n", msg);
800 message.hwnd = hwnd;
801 message.message = msg;
802 message.wParam = wparam;
803 message.lParam = lparam;
804 SendMessageW( icon->tooltip, TTM_RELAYEVENT, 0, (LPARAM)&message );
806 if (!PostMessageW( icon->owner, icon->callback_message, (WPARAM) icon->id, (LPARAM) msg ) &&
807 GetLastError() == ERROR_INVALID_WINDOW_HANDLE)
809 WINE_WARN("application window was destroyed without removing "
810 "notification icon, removing automatically\n");
811 delete_icon( icon );
813 break;
816 case WM_CLOSE:
817 /* don't destroy the tray window, just hide it */
818 ShowWindow( hwnd, SW_HIDE );
819 return 0;
821 case WM_DRAWITEM:
822 paint_taskbar_button( (const DRAWITEMSTRUCT *)lparam );
823 break;
825 case WM_COMMAND:
826 if (HIWORD(wparam) == BN_CLICKED) click_taskbar_button( (HWND)lparam );
827 break;
829 case WM_CONTEXTMENU:
830 show_taskbar_contextmenu( (HWND)wparam, lparam );
831 break;
833 case WM_MOUSEACTIVATE:
834 return MA_NOACTIVATE;
836 case WM_INITMENUPOPUP:
837 case WM_MENUCOMMAND:
838 return menu_wndproc(hwnd, msg, wparam, lparam);
840 default:
841 return DefWindowProcW( hwnd, msg, wparam, lparam );
843 return 0;
846 /* notification posted to the desktop window */
847 void handle_parent_notify( HWND hwnd, WPARAM wp )
849 switch (LOWORD(wp))
851 case WM_CREATE:
852 add_taskbar_button( hwnd );
853 break;
854 case WM_DESTROY:
855 remove_taskbar_button( hwnd );
856 cleanup_systray_window( hwnd );
857 break;
859 sync_taskbar_buttons();
862 /* this function creates the listener window */
863 void initialize_systray( HMODULE graphics_driver, BOOL using_root, BOOL arg_enable_shell )
865 WNDCLASSEXW class;
866 static const WCHAR classname[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
868 wine_notify_icon = (void *)GetProcAddress( graphics_driver, "wine_notify_icon" );
870 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
871 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
872 hide_systray = using_root;
873 enable_shell = arg_enable_shell;
875 /* register the systray listener window class */
876 ZeroMemory(&class, sizeof(class));
877 class.cbSize = sizeof(class);
878 class.style = CS_DBLCLKS | CS_HREDRAW;
879 class.lpfnWndProc = tray_wndproc;
880 class.hInstance = NULL;
881 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
882 class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
883 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
884 class.lpszClassName = classname;
886 if (!RegisterClassExW(&class))
888 WINE_ERR("Could not register SysTray window class\n");
889 return;
892 tray_window = CreateWindowExW( WS_EX_NOACTIVATE, classname, NULL, WS_POPUP,
893 0, GetSystemMetrics( SM_CYSCREEN ), 0, 0, 0, 0, 0, 0 );
894 if (!tray_window)
896 WINE_ERR("Could not create tray window\n");
897 return;
900 LoadStringW( NULL, IDS_START_LABEL, start_label, sizeof(start_label)/sizeof(WCHAR) );
902 add_taskbar_button( 0 );
904 if (hide_systray) do_hide_systray();
905 else if (enable_shell) do_show_systray();