systray.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * Copyright (C) 2004 Mike Hearn, for CodeWeavers
  3. * Copyright (C) 2005 Robert Shearman
  4. * Copyright (C) 2008 Alexandre Julliard
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include <assert.h>
  21. #define NONAMELESSUNION
  22. #include <windows.h>
  23. #include <commctrl.h>
  24. #include <shellapi.h>
  25. #include <wine/debug.h>
  26. #include <wine/list.h>
  27. #include "explorer_private.h"
  28. #include "resource.h"
  29. WINE_DEFAULT_DEBUG_CHANNEL(systray);
  30. struct notify_data /* platform-independent format for NOTIFYICONDATA */
  31. {
  32. LONG hWnd;
  33. UINT uID;
  34. UINT uFlags;
  35. UINT uCallbackMessage;
  36. WCHAR szTip[128];
  37. DWORD dwState;
  38. DWORD dwStateMask;
  39. WCHAR szInfo[256];
  40. union {
  41. UINT uTimeout;
  42. UINT uVersion;
  43. } u;
  44. WCHAR szInfoTitle[64];
  45. DWORD dwInfoFlags;
  46. GUID guidItem;
  47. /* data for the icon bitmap */
  48. UINT width;
  49. UINT height;
  50. UINT planes;
  51. UINT bpp;
  52. };
  53. static int (CDECL *wine_notify_icon)(DWORD,NOTIFYICONDATAW *);
  54. /* an individual systray icon, unpacked from the NOTIFYICONDATA and always in unicode */
  55. struct icon
  56. {
  57. struct list entry;
  58. HICON image; /* the image to render */
  59. HWND owner; /* the HWND passed in to the Shell_NotifyIcon call */
  60. HWND tooltip; /* Icon tooltip */
  61. UINT state; /* state flags */
  62. UINT id; /* the unique id given by the app */
  63. UINT callback_message;
  64. int display; /* index in display list, or -1 if hidden */
  65. WCHAR tiptext[128]; /* Tooltip text. If empty => tooltip disabled */
  66. WCHAR info_text[256]; /* info balloon text */
  67. WCHAR info_title[64]; /* info balloon title */
  68. UINT info_flags; /* flags for info balloon */
  69. UINT info_timeout; /* timeout for info balloon */
  70. HICON info_icon; /* info balloon icon */
  71. UINT version; /* notify icon api version */
  72. };
  73. static struct list icon_list = LIST_INIT( icon_list );
  74. struct taskbar_button
  75. {
  76. struct list entry;
  77. HWND hwnd;
  78. HWND button;
  79. BOOL active;
  80. BOOL visible;
  81. };
  82. static struct list taskbar_buttons = LIST_INIT( taskbar_buttons );
  83. static HWND tray_window;
  84. static unsigned int alloc_displayed;
  85. static unsigned int nb_displayed;
  86. static struct icon **displayed; /* array of currently displayed icons */
  87. static BOOL hide_systray, enable_shell;
  88. static int icon_cx, icon_cy, tray_width, tray_height;
  89. static int start_button_width, taskbar_button_width;
  90. static WCHAR start_label[50];
  91. static struct icon *balloon_icon;
  92. static HWND balloon_window;
  93. #define MIN_DISPLAYED 8
  94. #define ICON_BORDER 2
  95. #define BALLOON_CREATE_TIMER 1
  96. #define BALLOON_SHOW_TIMER 2
  97. #define BALLOON_CREATE_TIMEOUT 2000
  98. #define BALLOON_SHOW_MIN_TIMEOUT 10000
  99. #define BALLOON_SHOW_MAX_TIMEOUT 30000
  100. #define WM_POPUPSYSTEMMENU 0x0313
  101. static void do_hide_systray(void);
  102. static void do_show_systray(void);
  103. /* Retrieves icon record by owner window and ID */
  104. static struct icon *get_icon(HWND owner, UINT id)
  105. {
  106. struct icon *this;
  107. /* search for the icon */
  108. LIST_FOR_EACH_ENTRY( this, &icon_list, struct icon, entry )
  109. if ((this->id == id) && (this->owner == owner)) return this;
  110. return NULL;
  111. }
  112. static RECT get_icon_rect( struct icon *icon )
  113. {
  114. RECT rect;
  115. rect.right = tray_width - icon_cx * icon->display;
  116. rect.left = rect.right - icon_cx;
  117. rect.top = (tray_height - icon_cy) / 2;
  118. rect.bottom = rect.top + icon_cy;
  119. return rect;
  120. }
  121. static void init_common_controls(void)
  122. {
  123. static BOOL initialized = FALSE;
  124. if (!initialized)
  125. {
  126. INITCOMMONCONTROLSEX init_tooltip;
  127. init_tooltip.dwSize = sizeof(INITCOMMONCONTROLSEX);
  128. init_tooltip.dwICC = ICC_TAB_CLASSES|ICC_STANDARD_CLASSES;
  129. InitCommonControlsEx(&init_tooltip);
  130. initialized = TRUE;
  131. }
  132. }
  133. /* Creates tooltip window for icon. */
  134. static void create_tooltip(struct icon *icon)
  135. {
  136. TTTOOLINFOW ti;
  137. init_common_controls();
  138. icon->tooltip = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
  139. WS_POPUP | TTS_ALWAYSTIP,
  140. CW_USEDEFAULT, CW_USEDEFAULT,
  141. CW_USEDEFAULT, CW_USEDEFAULT,
  142. 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) ti.rect = get_icon_rect( icon );
  148. SendMessageW(icon->tooltip, TTM_ADDTOOLW, 0, (LPARAM)&ti);
  149. }
  150. static void set_balloon_position( struct icon *icon )
  151. {
  152. RECT rect = get_icon_rect( icon );
  153. POINT pos;
  154. MapWindowPoints( tray_window, 0, (POINT *)&rect, 2 );
  155. pos.x = (rect.left + rect.right) / 2;
  156. pos.y = (rect.top + rect.bottom) / 2;
  157. SendMessageW( balloon_window, TTM_TRACKPOSITION, 0, MAKELONG( pos.x, pos.y ));
  158. }
  159. static void balloon_create_timer(void)
  160. {
  161. TTTOOLINFOW ti;
  162. init_common_controls();
  163. balloon_window = CreateWindowExW( WS_EX_TOPMOST, TOOLTIPS_CLASSW, NULL,
  164. WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX | TTS_BALLOON | TTS_CLOSE,
  165. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  166. tray_window, NULL, NULL, NULL);
  167. memset( &ti, 0, sizeof(ti) );
  168. ti.cbSize = sizeof(TTTOOLINFOW);
  169. ti.hwnd = tray_window;
  170. ti.uFlags = TTF_TRACK;
  171. ti.lpszText = balloon_icon->info_text;
  172. SendMessageW( balloon_window, TTM_ADDTOOLW, 0, (LPARAM)&ti );
  173. if ((balloon_icon->info_flags & NIIF_ICONMASK) == NIIF_USER)
  174. SendMessageW( balloon_window, TTM_SETTITLEW, (WPARAM)balloon_icon->info_icon,
  175. (LPARAM)balloon_icon->info_title );
  176. else
  177. SendMessageW( balloon_window, TTM_SETTITLEW, balloon_icon->info_flags,
  178. (LPARAM)balloon_icon->info_title );
  179. set_balloon_position( balloon_icon );
  180. SendMessageW( balloon_window, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti );
  181. KillTimer( tray_window, BALLOON_CREATE_TIMER );
  182. SetTimer( tray_window, BALLOON_SHOW_TIMER, balloon_icon->info_timeout, NULL );
  183. }
  184. static BOOL show_balloon( struct icon *icon )
  185. {
  186. if (icon->display == -1) return FALSE; /* not displayed */
  187. if (!icon->info_text[0]) return FALSE; /* no balloon */
  188. balloon_icon = icon;
  189. SetTimer( tray_window, BALLOON_CREATE_TIMER, BALLOON_CREATE_TIMEOUT, NULL );
  190. return TRUE;
  191. }
  192. static void hide_balloon(void)
  193. {
  194. if (!balloon_icon) return;
  195. if (balloon_window)
  196. {
  197. KillTimer( tray_window, BALLOON_SHOW_TIMER );
  198. DestroyWindow( balloon_window );
  199. balloon_window = 0;
  200. }
  201. else KillTimer( tray_window, BALLOON_CREATE_TIMER );
  202. balloon_icon = NULL;
  203. }
  204. static void show_next_balloon(void)
  205. {
  206. struct icon *icon;
  207. LIST_FOR_EACH_ENTRY( icon, &icon_list, struct icon, entry )
  208. if (show_balloon( icon )) break;
  209. }
  210. static void update_balloon( struct icon *icon )
  211. {
  212. if (balloon_icon == icon)
  213. {
  214. hide_balloon();
  215. show_balloon( icon );
  216. }
  217. else if (!balloon_icon)
  218. {
  219. if (!show_balloon( icon )) return;
  220. }
  221. if (!balloon_icon) show_next_balloon();
  222. }
  223. static void balloon_timer(void)
  224. {
  225. if (balloon_icon) balloon_icon->info_text[0] = 0; /* clear text now that balloon has been shown */
  226. hide_balloon();
  227. show_next_balloon();
  228. }
  229. /* Synchronize tooltip text with tooltip window */
  230. static void update_tooltip_text(struct icon *icon)
  231. {
  232. TTTOOLINFOW ti;
  233. ZeroMemory(&ti, sizeof(ti));
  234. ti.cbSize = sizeof(TTTOOLINFOW);
  235. ti.hwnd = tray_window;
  236. ti.lpszText = icon->tiptext;
  237. SendMessageW(icon->tooltip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&ti);
  238. }
  239. /* synchronize tooltip position with tooltip window */
  240. static void update_tooltip_position( struct icon *icon )
  241. {
  242. TTTOOLINFOW ti;
  243. ZeroMemory(&ti, sizeof(ti));
  244. ti.cbSize = sizeof(TTTOOLINFOW);
  245. ti.hwnd = tray_window;
  246. if (icon->display != -1) ti.rect = get_icon_rect( icon );
  247. SendMessageW( icon->tooltip, TTM_NEWTOOLRECTW, 0, (LPARAM)&ti );
  248. if (balloon_icon == icon) set_balloon_position( icon );
  249. }
  250. /* find the icon located at a certain point in the tray window */
  251. static struct icon *icon_from_point( int x, int y )
  252. {
  253. if (y < 0 || y >= icon_cy) return NULL;
  254. x = tray_width - x;
  255. if (x < 0 || x >= icon_cx * nb_displayed) return NULL;
  256. return displayed[x / icon_cx];
  257. }
  258. /* invalidate the portion of the tray window that contains the specified icons */
  259. static void invalidate_icons( unsigned int start, unsigned int end )
  260. {
  261. RECT rect;
  262. rect.left = tray_width - (end + 1) * icon_cx;
  263. rect.top = (tray_height - icon_cy) / 2;
  264. rect.right = tray_width - start * icon_cx;
  265. rect.bottom = rect.top + icon_cy;
  266. InvalidateRect( tray_window, &rect, TRUE );
  267. }
  268. /* make an icon visible */
  269. static BOOL show_icon(struct icon *icon)
  270. {
  271. WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
  272. if (icon->display != -1) return TRUE; /* already displayed */
  273. if (nb_displayed >= alloc_displayed)
  274. {
  275. unsigned int new_count = max( alloc_displayed * 2, 32 );
  276. struct icon **ptr;
  277. if (displayed) ptr = HeapReAlloc( GetProcessHeap(), 0, displayed, new_count * sizeof(*ptr) );
  278. else ptr = HeapAlloc( GetProcessHeap(), 0, new_count * sizeof(*ptr) );
  279. if (!ptr) return FALSE;
  280. displayed = ptr;
  281. alloc_displayed = new_count;
  282. }
  283. icon->display = nb_displayed;
  284. displayed[nb_displayed++] = icon;
  285. update_tooltip_position( icon );
  286. invalidate_icons( nb_displayed-1, nb_displayed-1 );
  287. if (nb_displayed == 1 && !hide_systray) do_show_systray();
  288. create_tooltip(icon);
  289. update_balloon( icon );
  290. return TRUE;
  291. }
  292. /* make an icon invisible */
  293. static BOOL hide_icon(struct icon *icon)
  294. {
  295. unsigned int i;
  296. WINE_TRACE("id=0x%x, hwnd=%p\n", icon->id, icon->owner);
  297. if (icon->display == -1) return TRUE; /* already hidden */
  298. assert( nb_displayed );
  299. for (i = icon->display; i < nb_displayed - 1; i++)
  300. {
  301. displayed[i] = displayed[i + 1];
  302. displayed[i]->display = i;
  303. update_tooltip_position( displayed[i] );
  304. }
  305. nb_displayed--;
  306. invalidate_icons( icon->display, nb_displayed );
  307. icon->display = -1;
  308. if (!nb_displayed && !enable_shell) do_hide_systray();
  309. update_balloon( icon );
  310. update_tooltip_position( icon );
  311. return TRUE;
  312. }
  313. /* Modifies an existing icon record */
  314. static BOOL modify_icon( struct icon *icon, NOTIFYICONDATAW *nid )
  315. {
  316. WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
  317. /* demarshal the request from the NID */
  318. if (!icon)
  319. {
  320. WINE_WARN("Invalid icon ID (0x%x) for HWND %p\n", nid->uID, nid->hWnd);
  321. return FALSE;
  322. }
  323. if (nid->uFlags & NIF_STATE)
  324. {
  325. icon->state = (icon->state & ~nid->dwStateMask) | (nid->dwState & nid->dwStateMask);
  326. }
  327. if (nid->uFlags & NIF_ICON)
  328. {
  329. if (icon->image) DestroyIcon(icon->image);
  330. icon->image = CopyIcon(nid->hIcon);
  331. if (icon->display != -1) invalidate_icons( icon->display, icon->display );
  332. }
  333. if (nid->uFlags & NIF_MESSAGE)
  334. {
  335. icon->callback_message = nid->uCallbackMessage;
  336. }
  337. if (nid->uFlags & NIF_TIP)
  338. {
  339. lstrcpynW( icon->tiptext, nid->szTip, ARRAY_SIZE( icon->tiptext ));
  340. if (icon->display != -1) update_tooltip_text(icon);
  341. }
  342. if (nid->uFlags & NIF_INFO && nid->cbSize >= NOTIFYICONDATAA_V2_SIZE)
  343. {
  344. lstrcpynW( icon->info_text, nid->szInfo, ARRAY_SIZE( icon->info_text ));
  345. lstrcpynW( icon->info_title, nid->szInfoTitle, ARRAY_SIZE( icon->info_title ));
  346. icon->info_flags = nid->dwInfoFlags;
  347. icon->info_timeout = max(min(nid->u.uTimeout, BALLOON_SHOW_MAX_TIMEOUT), BALLOON_SHOW_MIN_TIMEOUT);
  348. icon->info_icon = nid->hBalloonIcon;
  349. update_balloon( icon );
  350. }
  351. if (icon->state & NIS_HIDDEN) hide_icon( icon );
  352. else show_icon( icon );
  353. return TRUE;
  354. }
  355. /* Adds a new icon record to the list */
  356. static BOOL add_icon(NOTIFYICONDATAW *nid)
  357. {
  358. struct icon *icon;
  359. WINE_TRACE("id=0x%x, hwnd=%p\n", nid->uID, nid->hWnd);
  360. if ((icon = get_icon(nid->hWnd, nid->uID)))
  361. {
  362. WINE_WARN("duplicate tray icon add, buggy app?\n");
  363. return FALSE;
  364. }
  365. if (!(icon = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*icon))))
  366. {
  367. WINE_ERR("out of memory\n");
  368. return FALSE;
  369. }
  370. ZeroMemory(icon, sizeof(struct icon));
  371. icon->id = nid->uID;
  372. icon->owner = nid->hWnd;
  373. icon->display = -1;
  374. list_add_tail(&icon_list, &icon->entry);
  375. return modify_icon( icon, nid );
  376. }
  377. /* Deletes tray icon window and icon record */
  378. static BOOL delete_icon(struct icon *icon)
  379. {
  380. hide_icon(icon);
  381. list_remove(&icon->entry);
  382. DestroyIcon(icon->image);
  383. HeapFree(GetProcessHeap(), 0, icon);
  384. return TRUE;
  385. }
  386. /* cleanup icons belonging to a window that has been destroyed */
  387. static void cleanup_systray_window( HWND hwnd )
  388. {
  389. struct icon *icon, *next;
  390. LIST_FOR_EACH_ENTRY_SAFE( icon, next, &icon_list, struct icon, entry )
  391. if (icon->owner == hwnd) delete_icon( icon );
  392. if (wine_notify_icon)
  393. {
  394. NOTIFYICONDATAW nid = { sizeof(nid), hwnd };
  395. wine_notify_icon( 0xdead, &nid );
  396. }
  397. }
  398. /* update the taskbar buttons when something changed */
  399. static void sync_taskbar_buttons(void)
  400. {
  401. struct taskbar_button *win;
  402. int pos = 0, count = 0;
  403. int width = taskbar_button_width;
  404. int right = tray_width - nb_displayed * icon_cx;
  405. HWND foreground = GetAncestor( GetForegroundWindow(), GA_ROOTOWNER );
  406. if (!IsWindowVisible( tray_window )) return;
  407. LIST_FOR_EACH_ENTRY( win, &taskbar_buttons, struct taskbar_button, entry )
  408. {
  409. if (!win->hwnd) /* start button */
  410. {
  411. SetWindowPos( win->button, 0, pos, 0, start_button_width, tray_height,
  412. SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW );
  413. pos += start_button_width;
  414. continue;
  415. }
  416. win->active = (win->hwnd == foreground);
  417. win->visible = IsWindowVisible( win->hwnd ) && !GetWindow( win->hwnd, GW_OWNER );
  418. if (win->visible) count++;
  419. }
  420. /* shrink buttons if space is tight */
  421. if (count && (count * width > right - pos))
  422. width = max( taskbar_button_width / 4, (right - pos) / count );
  423. LIST_FOR_EACH_ENTRY( win, &taskbar_buttons, struct taskbar_button, entry )
  424. {
  425. if (!win->hwnd) continue; /* start button */
  426. if (win->visible && right - pos >= width)
  427. {
  428. SetWindowPos( win->button, 0, pos, 0, width, tray_height,
  429. SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW );
  430. InvalidateRect( win->button, NULL, TRUE );
  431. pos += width;
  432. }
  433. else SetWindowPos( win->button, 0, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW );
  434. }
  435. }
  436. static BOOL handle_incoming(HWND hwndSource, COPYDATASTRUCT *cds)
  437. {
  438. struct icon *icon = NULL;
  439. const struct notify_data *data;
  440. NOTIFYICONDATAW nid;
  441. int ret = FALSE;
  442. if (cds->cbData < sizeof(*data)) return FALSE;
  443. data = cds->lpData;
  444. nid.cbSize = sizeof(nid);
  445. nid.hWnd = LongToHandle( data->hWnd );
  446. nid.uID = data->uID;
  447. nid.uFlags = data->uFlags;
  448. nid.uCallbackMessage = data->uCallbackMessage;
  449. nid.hIcon = 0;
  450. nid.dwState = data->dwState;
  451. nid.dwStateMask = data->dwStateMask;
  452. nid.u.uTimeout = data->u.uTimeout;
  453. nid.dwInfoFlags = data->dwInfoFlags;
  454. nid.guidItem = data->guidItem;
  455. lstrcpyW( nid.szTip, data->szTip );
  456. lstrcpyW( nid.szInfo, data->szInfo );
  457. lstrcpyW( nid.szInfoTitle, data->szInfoTitle );
  458. nid.hBalloonIcon = 0;
  459. /* FIXME: if statement only needed because we don't support interprocess
  460. * icon handles */
  461. if ((nid.uFlags & NIF_ICON) && cds->cbData > sizeof(*data))
  462. {
  463. LONG cbMaskBits;
  464. LONG cbColourBits;
  465. const char *buffer = (const char *)(data + 1);
  466. cbMaskBits = (data->width * data->height + 15) / 16 * 2;
  467. cbColourBits = (data->planes * data->width * data->height * data->bpp + 15) / 16 * 2;
  468. if (cds->cbData < sizeof(*data) + cbMaskBits + cbColourBits)
  469. {
  470. WINE_ERR("buffer underflow\n");
  471. return FALSE;
  472. }
  473. nid.hIcon = CreateIcon(NULL, data->width, data->height, data->planes, data->bpp,
  474. buffer, buffer + cbMaskBits);
  475. }
  476. /* try forwarding to the display driver first */
  477. if (cds->dwData == NIM_ADD || !(icon = get_icon( nid.hWnd, nid.uID )))
  478. {
  479. if (wine_notify_icon && ((ret = wine_notify_icon( cds->dwData, &nid )) != -1))
  480. {
  481. if (nid.hIcon) DestroyIcon( nid.hIcon );
  482. return ret;
  483. }
  484. ret = FALSE;
  485. }
  486. switch (cds->dwData)
  487. {
  488. case NIM_ADD:
  489. ret = add_icon(&nid);
  490. break;
  491. case NIM_DELETE:
  492. if (icon) ret = delete_icon( icon );
  493. break;
  494. case NIM_MODIFY:
  495. if (icon) ret = modify_icon( icon, &nid );
  496. break;
  497. case NIM_SETVERSION:
  498. if (icon)
  499. {
  500. icon->version = nid.u.uVersion;
  501. ret = TRUE;
  502. }
  503. break;
  504. default:
  505. WINE_FIXME("unhandled tray message: %ld\n", cds->dwData);
  506. break;
  507. }
  508. if (nid.hIcon) DestroyIcon( nid.hIcon );
  509. sync_taskbar_buttons();
  510. return ret;
  511. }
  512. static void add_taskbar_button( HWND hwnd )
  513. {
  514. struct taskbar_button *win;
  515. if (hide_systray) return;
  516. /* ignore our own windows */
  517. if (hwnd)
  518. {
  519. DWORD process;
  520. if (!GetWindowThreadProcessId( hwnd, &process ) || process == GetCurrentProcessId()) return;
  521. }
  522. if (!(win = HeapAlloc( GetProcessHeap(), 0, sizeof(*win) ))) return;
  523. win->hwnd = hwnd;
  524. win->button = CreateWindowW( WC_BUTTONW, NULL, WS_CHILD | BS_OWNERDRAW,
  525. 0, 0, 0, 0, tray_window, (HMENU)hwnd, 0, 0 );
  526. list_add_tail( &taskbar_buttons, &win->entry );
  527. }
  528. static struct taskbar_button *find_taskbar_button( HWND hwnd )
  529. {
  530. struct taskbar_button *win;
  531. LIST_FOR_EACH_ENTRY( win, &taskbar_buttons, struct taskbar_button, entry )
  532. if (win->hwnd == hwnd) return win;
  533. return NULL;
  534. }
  535. static void remove_taskbar_button( HWND hwnd )
  536. {
  537. struct taskbar_button *win = find_taskbar_button( hwnd );
  538. if (!win) return;
  539. list_remove( &win->entry );
  540. DestroyWindow( win->button );
  541. HeapFree( GetProcessHeap(), 0, win );
  542. }
  543. static void paint_taskbar_button( const DRAWITEMSTRUCT *dis )
  544. {
  545. RECT rect;
  546. UINT flags = DC_TEXT;
  547. struct taskbar_button *win = find_taskbar_button( LongToHandle( dis->CtlID ));
  548. if (!win) return;
  549. GetClientRect( dis->hwndItem, &rect );
  550. DrawFrameControl( dis->hDC, &rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_ADJUSTRECT |
  551. ((dis->itemState & ODS_SELECTED) ? DFCS_PUSHED : 0 ));
  552. if (win->hwnd)
  553. {
  554. flags |= win->active ? DC_ACTIVE : DC_INBUTTON;
  555. DrawCaptionTempW( win->hwnd, dis->hDC, &rect, 0, 0, NULL, flags );
  556. }
  557. else /* start button */
  558. DrawCaptionTempW( 0, dis->hDC, &rect, 0, 0, start_label, flags | DC_INBUTTON | DC_ICON );
  559. }
  560. static void click_taskbar_button( HWND button )
  561. {
  562. LONG_PTR id = GetWindowLongPtrW( button, GWLP_ID );
  563. HWND hwnd = (HWND)id;
  564. if (!hwnd) /* start button */
  565. {
  566. do_startmenu( tray_window );
  567. return;
  568. }
  569. if (IsIconic( hwnd ))
  570. {
  571. SendMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, 0 );
  572. return;
  573. }
  574. if (IsWindowEnabled( hwnd ))
  575. {
  576. if (hwnd == GetForegroundWindow())
  577. {
  578. SendMessageW( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 );
  579. return;
  580. }
  581. }
  582. else /* look for an enabled window owned by this one */
  583. {
  584. HWND owned = GetWindow( GetDesktopWindow(), GW_CHILD );
  585. while (owned && owned != hwnd)
  586. {
  587. if (IsWindowVisible( owned ) &&
  588. IsWindowEnabled( owned ) &&
  589. (GetWindow( owned, GW_OWNER ) == hwnd))
  590. break;
  591. owned = GetWindow( owned, GW_HWNDNEXT );
  592. }
  593. hwnd = owned;
  594. }
  595. SetForegroundWindow( hwnd );
  596. }
  597. static void show_taskbar_contextmenu( HWND button, LPARAM lparam )
  598. {
  599. ULONG_PTR id = GetWindowLongPtrW( button, GWLP_ID );
  600. if (id) SendNotifyMessageW( (HWND)id, WM_POPUPSYSTEMMENU, 0, lparam );
  601. }
  602. static void do_hide_systray(void)
  603. {
  604. SetWindowPos( tray_window, 0,
  605. GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN),
  606. GetSystemMetrics(SM_YVIRTUALSCREEN) + GetSystemMetrics(SM_CYVIRTUALSCREEN),
  607. 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  608. }
  609. static BOOL notify_owner( struct icon *icon, UINT msg, POINT pt )
  610. {
  611. WPARAM wp = icon->id;
  612. LPARAM lp = msg;
  613. if (icon->version >= NOTIFYICON_VERSION_4)
  614. {
  615. ClientToScreen( tray_window, &pt );
  616. wp = MAKEWPARAM( pt.x, pt.y );
  617. lp = MAKELPARAM( msg, icon->id );
  618. }
  619. TRACE( "relaying 0x%x\n", msg );
  620. if (!SendNotifyMessageW( icon->owner, icon->callback_message, wp, lp ) &&
  621. (GetLastError() == ERROR_INVALID_WINDOW_HANDLE))
  622. {
  623. WARN( "application window was destroyed, removing icon %u\n", icon->id );
  624. delete_icon( icon );
  625. return FALSE;
  626. }
  627. return TRUE;
  628. }
  629. static void do_show_systray(void)
  630. {
  631. SIZE size;
  632. NONCLIENTMETRICSW ncm;
  633. HFONT font;
  634. HDC hdc = GetDC( 0 );
  635. ncm.cbSize = sizeof(NONCLIENTMETRICSW);
  636. SystemParametersInfoW( SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &ncm, 0 );
  637. font = CreateFontIndirectW( &ncm.lfCaptionFont );
  638. /* FIXME: Implement BCM_GETIDEALSIZE and use that instead. */
  639. SelectObject( hdc, font );
  640. GetTextExtentPointA( hdc, "abcdefghijklmnopqrstuvwxyz", 26, &size );
  641. taskbar_button_width = size.cx;
  642. GetTextExtentPointW( hdc, start_label, lstrlenW(start_label), &size );
  643. /* add some margins (FIXME) */
  644. size.cx += 12 + GetSystemMetrics( SM_CXSMICON );
  645. size.cy += 4;
  646. ReleaseDC( 0, hdc );
  647. DeleteObject( font );
  648. tray_width = GetSystemMetrics( SM_CXSCREEN );
  649. tray_height = max( icon_cy, size.cy );
  650. start_button_width = size.cx;
  651. SetWindowPos( tray_window, 0, 0, GetSystemMetrics( SM_CYSCREEN ) - tray_height,
  652. tray_width, tray_height, SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW );
  653. sync_taskbar_buttons();
  654. }
  655. static LRESULT WINAPI tray_wndproc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  656. {
  657. switch (msg)
  658. {
  659. case WM_COPYDATA:
  660. return handle_incoming((HWND)wparam, (COPYDATASTRUCT *)lparam);
  661. case WM_DISPLAYCHANGE:
  662. if (hide_systray || (!nb_displayed && !enable_shell)) do_hide_systray();
  663. else do_show_systray();
  664. break;
  665. case WM_TIMER:
  666. switch (wparam)
  667. {
  668. case BALLOON_CREATE_TIMER: balloon_create_timer(); break;
  669. case BALLOON_SHOW_TIMER: balloon_timer(); break;
  670. }
  671. break;
  672. case WM_PAINT:
  673. {
  674. unsigned int i;
  675. PAINTSTRUCT ps;
  676. HDC hdc;
  677. hdc = BeginPaint( hwnd, &ps );
  678. for (i = 0; i < nb_displayed; i++)
  679. {
  680. RECT dummy, rect = get_icon_rect( displayed[i] );
  681. if (IntersectRect( &dummy, &rect, &ps.rcPaint ))
  682. DrawIconEx( hdc, rect.left + ICON_BORDER, rect.top + ICON_BORDER, displayed[i]->image,
  683. icon_cx - 2*ICON_BORDER, icon_cy - 2*ICON_BORDER,
  684. 0, 0, DI_DEFAULTSIZE|DI_NORMAL);
  685. }
  686. EndPaint( hwnd, &ps );
  687. break;
  688. }
  689. case WM_MOUSEMOVE:
  690. case WM_LBUTTONDOWN:
  691. case WM_LBUTTONUP:
  692. case WM_RBUTTONDOWN:
  693. case WM_RBUTTONUP:
  694. case WM_MBUTTONDOWN:
  695. case WM_MBUTTONUP:
  696. case WM_LBUTTONDBLCLK:
  697. case WM_RBUTTONDBLCLK:
  698. case WM_MBUTTONDBLCLK:
  699. {
  700. MSG message;
  701. POINT pt = { (short)LOWORD(lparam), (short)HIWORD(lparam) };
  702. struct icon *icon = icon_from_point( pt.x, pt.y );
  703. if (!icon) break;
  704. message.hwnd = hwnd;
  705. message.message = msg;
  706. message.wParam = wparam;
  707. message.lParam = lparam;
  708. SendMessageW( icon->tooltip, TTM_RELAYEVENT, 0, (LPARAM)&message );
  709. if (!notify_owner( icon, msg, pt )) break;
  710. if (icon->version > 0)
  711. {
  712. switch (msg)
  713. {
  714. case WM_RBUTTONUP:
  715. notify_owner( icon, WM_CONTEXTMENU, pt );
  716. break;
  717. case WM_LBUTTONUP:
  718. notify_owner( icon, NIN_SELECT, pt );
  719. break;
  720. }
  721. }
  722. break;
  723. }
  724. case WM_CLOSE:
  725. /* don't destroy the tray window, just hide it */
  726. ShowWindow( hwnd, SW_HIDE );
  727. return 0;
  728. case WM_DRAWITEM:
  729. paint_taskbar_button( (const DRAWITEMSTRUCT *)lparam );
  730. break;
  731. case WM_COMMAND:
  732. if (HIWORD(wparam) == BN_CLICKED) click_taskbar_button( (HWND)lparam );
  733. break;
  734. case WM_CONTEXTMENU:
  735. show_taskbar_contextmenu( (HWND)wparam, lparam );
  736. break;
  737. case WM_MOUSEACTIVATE:
  738. return MA_NOACTIVATE;
  739. case WM_INITMENUPOPUP:
  740. case WM_MENUCOMMAND:
  741. return menu_wndproc(hwnd, msg, wparam, lparam);
  742. default:
  743. return DefWindowProcW( hwnd, msg, wparam, lparam );
  744. }
  745. return 0;
  746. }
  747. /* notification posted to the desktop window */
  748. void handle_parent_notify( HWND hwnd, WPARAM wp )
  749. {
  750. switch (LOWORD(wp))
  751. {
  752. case WM_CREATE:
  753. add_taskbar_button( hwnd );
  754. break;
  755. case WM_DESTROY:
  756. remove_taskbar_button( hwnd );
  757. cleanup_systray_window( hwnd );
  758. break;
  759. }
  760. sync_taskbar_buttons();
  761. }
  762. /* this function creates the listener window */
  763. void initialize_systray( HMODULE graphics_driver, BOOL using_root, BOOL arg_enable_shell )
  764. {
  765. WNDCLASSEXW class;
  766. static const WCHAR classname[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
  767. if (using_root && graphics_driver) wine_notify_icon = (void *)GetProcAddress( graphics_driver, "wine_notify_icon" );
  768. icon_cx = GetSystemMetrics( SM_CXSMICON ) + 2*ICON_BORDER;
  769. icon_cy = GetSystemMetrics( SM_CYSMICON ) + 2*ICON_BORDER;
  770. hide_systray = using_root;
  771. enable_shell = arg_enable_shell;
  772. /* register the systray listener window class */
  773. ZeroMemory(&class, sizeof(class));
  774. class.cbSize = sizeof(class);
  775. class.style = CS_DBLCLKS | CS_HREDRAW;
  776. class.lpfnWndProc = tray_wndproc;
  777. class.hInstance = NULL;
  778. class.hIcon = LoadIconW(0, (LPCWSTR)IDI_WINLOGO);
  779. class.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
  780. class.hbrBackground = (HBRUSH) COLOR_WINDOW;
  781. class.lpszClassName = classname;
  782. if (!RegisterClassExW(&class))
  783. {
  784. WINE_ERR("Could not register SysTray window class\n");
  785. return;
  786. }
  787. tray_window = CreateWindowExW( WS_EX_NOACTIVATE, classname, NULL, WS_POPUP,
  788. 0, GetSystemMetrics( SM_CYSCREEN ), 0, 0, 0, 0, 0, 0 );
  789. if (!tray_window)
  790. {
  791. WINE_ERR("Could not create tray window\n");
  792. return;
  793. }
  794. LoadStringW( NULL, IDS_START_LABEL, start_label, ARRAY_SIZE( start_label ));
  795. add_taskbar_button( 0 );
  796. if (hide_systray) do_hide_systray();
  797. else if (enable_shell) do_show_systray();
  798. }