Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
5.3 KiB

  1. // shelltray.cpp
  2. //
  3. // Copyright 2000 Microsoft Corporation, all rights reserved
  4. //
  5. // Created 2-00 - anbrad
  6. //
  7. #include "main.h"
  8. #include "shelltray.h"
  9. #include "resource.h"
  10. #include "netwatch.h"
  11. #include "dsubmit.h"
  12. void OpenContextMenu(HWND hwnd, POINT * pPoint);
  13. void OnTaskBarIconRButtonUp(HWND hwnd);
  14. //----------------------------------------------------------------------------
  15. // AddTrayIcon
  16. //
  17. void
  18. AddTrayIcon(
  19. HWND hwnd)
  20. {
  21. NOTIFYICONDATA nid;
  22. HICON hiconTray = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_FACE));
  23. if (hiconTray)
  24. {
  25. nid.uID = 0;
  26. nid.cbSize = sizeof(NOTIFYICONDATA);
  27. nid.hWnd = hwnd;
  28. nid.uCallbackMessage = WM_USER_TRAYCALLBACK;
  29. nid.hIcon = hiconTray;
  30. nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  31. lstrcpy(nid.szTip, TEXT("Double click to save your network trace."));
  32. }
  33. g_fTrayPresent = Shell_NotifyIcon(NIM_ADD, &nid);
  34. }
  35. //----------------------------------------------------------------------------
  36. // RemoveTrayIcon
  37. //
  38. void
  39. RemoveTrayIcon(
  40. HWND hwnd)
  41. {
  42. NOTIFYICONDATA nid;
  43. if (g_fTrayPresent)
  44. {
  45. nid.uID = 0;
  46. nid.cbSize = sizeof(NOTIFYICONDATA);
  47. nid.hWnd = hwnd;
  48. nid.uCallbackMessage = WM_USER_TRAYCALLBACK;
  49. nid.uFlags = 0;
  50. }
  51. g_fTrayPresent = !(Shell_NotifyIcon(NIM_DELETE, &nid));
  52. }
  53. //----------------------------------------------------------------------------
  54. // UpdateTrayIcon
  55. //
  56. void
  57. UpdateTrayIcon(
  58. HWND hwnd)
  59. {
  60. NOTIFYICONDATA nid;
  61. if (g_fTrayPresent)
  62. {
  63. nid.uID = 0;
  64. nid.cbSize = sizeof(NOTIFYICONDATA);
  65. nid.hWnd = hwnd;
  66. nid.uCallbackMessage = WM_USER_TRAYCALLBACK;
  67. nid.uFlags = NIF_TIP;
  68. _tcscpy(nid.szTip, TEXT("Double click to save your network trace."));
  69. }
  70. Shell_NotifyIcon(NIM_MODIFY, &nid);
  71. }
  72. //----------------------------------------------------------------------------
  73. // ProcessTrayCallback
  74. //
  75. void
  76. ProcessTrayCallback(
  77. HWND hwnd,
  78. WPARAM wParam,
  79. LPARAM lParam)
  80. {
  81. UINT uID = (UINT) wParam;
  82. UINT uMouseMsg = (UINT) lParam;
  83. static bInDialog;
  84. switch(uMouseMsg)
  85. {
  86. case WM_LBUTTONDBLCLK:
  87. if (!bInDialog)
  88. {
  89. bInDialog = TRUE;
  90. StopCapture();
  91. if (IDOK == DialogBox(
  92. g_hInst,
  93. MAKEINTRESOURCE(IDD_SUBMIT),
  94. NULL,
  95. DlgProcSubmit))
  96. {
  97. SaveCapture();
  98. }
  99. else
  100. {
  101. RestartCapture();
  102. }
  103. bInDialog = FALSE;
  104. }
  105. break;
  106. case WM_RBUTTONUP:
  107. OnTaskBarIconRButtonUp(hwnd);
  108. break;
  109. }
  110. }
  111. void OnTaskBarIconRButtonUp(HWND hwnd)
  112. {
  113. POINT pt;
  114. GetCursorPos(&pt);
  115. OpenContextMenu(hwnd, &pt);
  116. }
  117. #if (WINVER > 0x0400)
  118. VOID SetIconFocus(HWND hwnd)
  119. {
  120. NOTIFYICONDATA nid;
  121. ZeroMemory (&nid, sizeof(nid));
  122. nid.cbSize = sizeof(NOTIFYICONDATA);
  123. nid.hWnd = hwnd;
  124. nid.uID = 0;
  125. // Shell_NotifyIcon(NIM_SETFOCUS, &nid);
  126. }
  127. #endif
  128. void OpenContextMenu(HWND hwnd, POINT * pPoint)
  129. {
  130. HRESULT hr = S_OK;
  131. INT iCmd = 0;
  132. INT iMenu = 0;
  133. HMENU hmenu = 0;
  134. BOOL fDisconnected = FALSE;
  135. INT iIdCustomMin = -1;
  136. INT iIdCustomMax = -1;
  137. BOOL fBranded = FALSE;
  138. // Find the connection info based on the tray icon id.
  139. //
  140. hmenu = LoadMenu(g_hInst, MAKEINTRESOURCE(POPUP_TRAY));
  141. if (hmenu)
  142. {
  143. // Get the first menu from the popup. For some reason, this hack is
  144. // required instead of tracking on the outside menu
  145. //
  146. HMENU hmenuTrack = GetSubMenu(hmenu, 0);
  147. // Set the default menu item
  148. //
  149. SetMenuDefaultItem(hmenuTrack, CMIDM_TRAY_CLOSE, FALSE);
  150. // Set the owner window to be foreground as a hack so the
  151. // popup menu disappears when the user clicks elsewhere.
  152. //
  153. SetForegroundWindow(hwnd);
  154. // Part of the above hack. Bring up the menu and figure out the result
  155. iCmd = TrackPopupMenu(hmenuTrack, TPM_RETURNCMD | TPM_NONOTIFY | TPM_RIGHTBUTTON,
  156. pPoint->x, pPoint->y, 0, hwnd, NULL);
  157. DestroyMenu(hmenu);
  158. MSG msgTmp;
  159. while (PeekMessage(&msgTmp, hwnd, WM_LBUTTONDOWN, WM_LBUTTONUP, PM_REMOVE))
  160. {
  161. DispatchMessage(&msgTmp);
  162. }
  163. // Process the command
  164. //
  165. switch (iCmd)
  166. {
  167. case CMIDM_TRAY_CLOSE:
  168. SendMessage(hwnd, WM_DESTROY, 0, 0);
  169. break;
  170. // Tray menu cancelled without selection
  171. //
  172. case 0:
  173. break;
  174. // Unknown command
  175. //
  176. default:
  177. break;
  178. }
  179. // Shift the focus back to the shell
  180. //
  181. #if (WINVER > 0x0400)
  182. SetIconFocus(hwnd);
  183. #endif
  184. }
  185. }