explorer: Use a valid taskbar position when the taskbar is hidden.
[wine.git] / programs / explorer / systray.c
blob7790648d021edf9d3f2db103e8eb02401c250ce8
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 #include <windows.h>
25 #include <commctrl.h>
26 #include <shellapi.h>
28 #include <wine/debug.h>
29 #include <wine/list.h>
31 #include "explorer_private.h"
32 #include "resource.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(systray);
36 struct notify_data /* platform-independent format for NOTIFYICONDATA */
38 LONG hWnd;
39 UINT uID;
40 UINT uFlags;
41 UINT uCallbackMessage;
42 WCHAR szTip[128];
43 DWORD dwState;
44 DWORD dwStateMask;
45 WCHAR szInfo[256];
46 union {
47 UINT uTimeout;
48 UINT uVersion;
49 } u;
50 WCHAR szInfoTitle[64];
51 DWORD dwInfoFlags;
52 GUID guidItem;
53 /* data for the icon bitmap */
54 UINT width;
55 UINT height;
56 UINT planes;
57 UINT bpp;
60 static int (CDECL *wine_notify_icon)(DWORD,NOTIFYICONDATAW *);
62 /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
63 struct icon
65 struct list entry;
66 HICON image; /* the image to render */
67 HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
68 HWND tooltip; /* Icon tooltip */
69 UINT state; /* state flags */
70 UINT id; /* the unique id given by the app */
71 UINT callback_message;
72 int display; /* index in display list, or -1 if hidden */
73 WCHAR tiptext[128]; /* Tooltip text. If empty => tooltip disabled */
74 WCHAR info_text[256]; /* info balloon text */
75 WCHAR info_title[64]; /* info balloon title */
76 UINT info_flags; /* flags for info balloon */
77 UINT info_timeout; /* timeout for info balloon */
78 HICON info_icon; /* info balloon icon */
79 UINT version; /* notify icon api version */
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, ARRAY_SIZE( icon->tiptext ));
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, ARRAY_SIZE( icon->info_text ));
413 lstrcpynW( icon->info_title, nid->szInfoTitle, ARRAY_SIZE( icon->info_title ));
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 forwarding to the display driver 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 case NIM_SETVERSION:
590 if (icon)
592 icon->version = nid.u.uVersion;
593 ret = TRUE;
595 break;
596 default:
597 WINE_FIXME("unhandled tray message: %Id\n", cds->dwData);
598 break;
601 if (nid.hIcon) DestroyIcon( nid.hIcon );
602 sync_taskbar_buttons();
603 return ret;
606 static void add_taskbar_button( HWND hwnd )
608 struct taskbar_button *win;
610 if (hide_systray) return;
612 /* ignore our own windows */
613 if (hwnd)
615 DWORD process;
616 if (!GetWindowThreadProcessId( hwnd, &process ) || process == GetCurrentProcessId()) return;
619 if (!(win = HeapAlloc( GetProcessHeap(), 0, sizeof(*win) ))) return;
620 win->hwnd = hwnd;
621 win->button = CreateWindowW( WC_BUTTONW, NULL, WS_CHILD | BS_OWNERDRAW,
622 0, 0, 0, 0, tray_window, (HMENU)hwnd, 0, 0 );
623 list_add_tail( &taskbar_buttons, &win->entry );
626 static struct taskbar_button *find_taskbar_button( HWND hwnd )
628 struct taskbar_button *win;
630 LIST_FOR_EACH_ENTRY( win, &taskbar_buttons, struct taskbar_button, entry )
631 if (win->hwnd == hwnd) return win;
633 return NULL;
636 static void remove_taskbar_button( HWND hwnd )
638 struct taskbar_button *win = find_taskbar_button( hwnd );
640 if (!win) return;
641 list_remove( &win->entry );
642 DestroyWindow( win->button );
643 HeapFree( GetProcessHeap(), 0, win );
646 static void paint_taskbar_button( const DRAWITEMSTRUCT *dis )
648 RECT rect;
649 UINT flags = DC_TEXT;
650 struct taskbar_button *win = find_taskbar_button( LongToHandle( dis->CtlID ));
652 if (!win) return;
653 GetClientRect( dis->hwndItem, &rect );
654 DrawFrameControl( dis->hDC, &rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_ADJUSTRECT |
655 ((dis->itemState & ODS_SELECTED) ? DFCS_PUSHED : 0 ));
656 if (win->hwnd)
658 flags |= win->active ? DC_ACTIVE : DC_INBUTTON;
659 DrawCaptionTempW( win->hwnd, dis->hDC, &rect, 0, 0, NULL, flags );
661 else /* start button */
662 DrawCaptionTempW( 0, dis->hDC, &rect, 0, 0, start_label, flags | DC_INBUTTON | DC_ICON );
665 static void click_taskbar_button( HWND button )
667 LONG_PTR id = GetWindowLongPtrW( button, GWLP_ID );
668 HWND hwnd = (HWND)id;
670 if (!hwnd) /* start button */
672 do_startmenu( tray_window );
673 return;
676 if (IsIconic( hwnd ))
678 SendMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0 );
679 return;
682 if (IsWindowEnabled( hwnd ))
684 if (hwnd == GetForegroundWindow())
686 SendMessageW( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 );
687 return;
690 else /* look for an enabled window owned by this one */
692 HWND owned = GetWindow( GetDesktopWindow(), GW_CHILD );
693 while (owned && owned != hwnd)
695 if (IsWindowVisible( owned ) &&
696 IsWindowEnabled( owned ) &&
697 (GetWindow( owned, GW_OWNER ) == hwnd))
698 break;
699 owned = GetWindow( owned, GW_HWNDNEXT );
701 hwnd = owned;
703 SetForegroundWindow( hwnd );
706 static void show_taskbar_contextmenu( HWND button, LPARAM lparam )
708 ULONG_PTR id = GetWindowLongPtrW( button, GWLP_ID );
710 if (id) SendNotifyMessageW( (HWND)id, WM_POPUPSYSTEMMENU, 0, lparam );
713 static void do_hide_systray(void)
715 ShowWindow( tray_window, SW_HIDE );
718 static BOOL notify_owner( struct icon *icon, UINT msg, POINT pt )
720 WPARAM wp = icon->id;
721 LPARAM lp = msg;
723 if (icon->version >= NOTIFYICON_VERSION_4)
725 ClientToScreen( tray_window, &pt );
726 wp = MAKEWPARAM( pt.x, pt.y );
727 lp = MAKELPARAM( msg, icon->id );
730 TRACE( "relaying 0x%x\n", msg );
731 if (!SendNotifyMessageW( icon->owner, icon->callback_message, wp, lp ) &&
732 (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
734 WARN( "application window was destroyed, removing icon %u\n", icon->id );
735 delete_icon( icon );
736 return FALSE;
738 return TRUE;
741 static void do_show_systray(void)
743 SIZE size;
744 NONCLIENTMETRICSW ncm;
745 HFONT font;
746 HDC hdc = GetDC( 0 );
748 ncm.cbSize = sizeof(NONCLIENTMETRICSW);
749 SystemParametersInfoW( SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0 );
750 font = CreateFontIndirectW( &ncm.lfCaptionFont );
751 /* FIXME: Implement BCM_GETIDEALSIZE and use that instead. */
752 SelectObject( hdc, font );
753 GetTextExtentPointA( hdc, "abcdefghijklmnopqrstuvwxyz", 26, &size );
754 taskbar_button_width = size.cx;
755 GetTextExtentPointW( hdc, start_label, lstrlenW(start_label), &size );
756 /* add some margins (FIXME) */
757 size.cx += 12 + GetSystemMetrics( SM_CXSMICON );
758 size.cy += 4;
759 ReleaseDC( 0, hdc );
760 DeleteObject( font );
762 tray_width = GetSystemMetrics( SM_CXSCREEN );
763 tray_height = max( icon_cy, size.cy );
764 start_button_width = size.cx;
765 SetWindowPos( tray_window, 0, 0, GetSystemMetrics( SM_CYSCREEN ) - tray_height,
766 tray_width, tray_height, SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW );
767 sync_taskbar_buttons();
770 static LRESULT WINAPI tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
772 switch (msg)
774 case WM_COPYDATA:
775 return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
777 case WM_DISPLAYCHANGE:
778 if (hide_systray || (!nb_displayed && !enable_shell)) do_hide_systray();
779 else do_show_systray();
780 break;
782 case WM_TIMER:
783 switch (wparam)
785 case BALLOON_CREATE_TIMER: balloon_create_timer(); break;
786 case BALLOON_SHOW_TIMER: balloon_timer(); break;
788 break;
790 case WM_PAINT:
792 unsigned int i;
793 PAINTSTRUCT ps;
794 HDC hdc;
796 hdc = BeginPaint( hwnd, &ps );
797 for (i = 0; i < nb_displayed; i++)
799 RECT dummy, rect = get_icon_rect( displayed[i] );
800 if (IntersectRect( &dummy, &rect, &ps.rcPaint ))
801 DrawIconEx( hdc, rect.left + ICON_BORDER, rect.top + ICON_BORDER, displayed[i]->image,
802 icon_cx - 2*ICON_BORDER, icon_cy - 2*ICON_BORDER,
803 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
805 EndPaint( hwnd, &ps );
806 break;
809 case WM_MOUSEMOVE:
810 case WM_LBUTTONDOWN:
811 case WM_LBUTTONUP:
812 case WM_RBUTTONDOWN:
813 case WM_RBUTTONUP:
814 case WM_MBUTTONDOWN:
815 case WM_MBUTTONUP:
816 case WM_LBUTTONDBLCLK:
817 case WM_RBUTTONDBLCLK:
818 case WM_MBUTTONDBLCLK:
820 MSG message;
821 POINT pt = { (short)LOWORD(lparam), (short)HIWORD(lparam) };
822 struct icon *icon = icon_from_point( pt.x, pt.y );
823 if (!icon) break;
825 message.hwnd = hwnd;
826 message.message = msg;
827 message.wParam = wparam;
828 message.lParam = lparam;
829 SendMessageW( icon->tooltip, TTM_RELAYEVENT, 0, (LPARAM)&message );
831 if (!notify_owner( icon, msg, pt )) break;
833 if (icon->version > 0)
835 switch (msg)
837 case WM_RBUTTONUP:
838 notify_owner( icon, WM_CONTEXTMENU, pt );
839 break;
840 case WM_LBUTTONUP:
841 notify_owner( icon, NIN_SELECT, pt );
842 break;
845 break;
848 case WM_CLOSE:
849 /* don't destroy the tray window, just hide it */
850 ShowWindow( hwnd, SW_HIDE );
851 return 0;
853 case WM_DRAWITEM:
854 paint_taskbar_button( (const DRAWITEMSTRUCT *)lparam );
855 break;
857 case WM_COMMAND:
858 if (HIWORD(wparam) == BN_CLICKED) click_taskbar_button( (HWND)lparam );
859 break;
861 case WM_CONTEXTMENU:
862 show_taskbar_contextmenu( (HWND)wparam, lparam );
863 break;
865 case WM_MOUSEACTIVATE:
866 return MA_NOACTIVATE;
868 case WM_INITMENUPOPUP:
869 case WM_MENUCOMMAND:
870 return menu_wndproc(hwnd, msg, wparam, lparam);
872 default:
873 return DefWindowProcW( hwnd, msg, wparam, lparam );
875 return 0;
878 /* notification posted to the desktop window */
879 void handle_parent_notify( HWND hwnd, WPARAM wp )
881 switch (LOWORD(wp))
883 case WM_CREATE:
884 add_taskbar_button( hwnd );
885 break;
886 case WM_DESTROY:
887 remove_taskbar_button( hwnd );
888 cleanup_systray_window( hwnd );
889 break;
891 sync_taskbar_buttons();
894 /* this function creates the listener window */
895 void initialize_systray( HMODULE graphics_driver, BOOL using_root, BOOL arg_enable_shell )
897 WNDCLASSEXW class;
898 static const WCHAR classname[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
899 RECT work_rect, primary_rect, taskbar_rect;
901 if (using_root && graphics_driver) wine_notify_icon = (void *)GetProcAddress( graphics_driver, "wine_notify_icon" );
903 icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
904 icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
905 hide_systray = using_root;
906 enable_shell = arg_enable_shell;
908 /* register the systray listener window class */
909 ZeroMemory(&class, sizeof(class));
910 class.cbSize = sizeof(class);
911 class.style = CS_DBLCLKS | CS_HREDRAW;
912 class.lpfnWndProc = tray_wndproc;
913 class.hInstance = NULL;
914 class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
915 class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
916 class.hbrBackground = (HBRUSH) COLOR_WINDOW;
917 class.lpszClassName = classname;
919 if (!RegisterClassExW(&class))
921 WINE_ERR("Could not register SysTray window class\n");
922 return;
925 SystemParametersInfoW( SPI_GETWORKAREA, 0, &work_rect, 0 );
926 SetRect( &primary_rect, 0, 0, GetSystemMetrics( SM_CXSCREEN ), GetSystemMetrics( SM_CYSCREEN ) );
927 SubtractRect( &taskbar_rect, &primary_rect, &work_rect );
929 tray_window = CreateWindowExW( WS_EX_NOACTIVATE, classname, NULL, WS_POPUP, taskbar_rect.left,
930 taskbar_rect.top, taskbar_rect.right - taskbar_rect.left,
931 taskbar_rect.bottom - taskbar_rect.top, 0, 0, 0, 0 );
932 if (!tray_window)
934 WINE_ERR("Could not create tray window\n");
935 return;
938 LoadStringW( NULL, IDS_START_LABEL, start_label, ARRAY_SIZE( start_label ));
940 add_taskbar_button( 0 );
942 if (hide_systray) do_hide_systray();
943 else if (enable_shell) do_show_systray();