Leaked source code of windows server 2003
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.

229 lines
7.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // TaskMan - NT TaskManager
  4. // Copyright (C) Microsoft
  5. //
  6. // File: trayicon.CPP
  7. //
  8. // History: Jan-27-96 DavePl Created
  9. //
  10. //--------------------------------------------------------------------------
  11. #include "precomp.h"
  12. /*++ TrayThreadMessageLoop (WORKER THREAD CODE)
  13. Routine Description:
  14. Waits for messages telling it a notification packet is ready
  15. in the queue, then dispatches it to the tray
  16. Mar-27-95 Davepl Created
  17. May-28-99 Jonburs Check for NIM_DELETE during PM_QUITTRAYTHREAD
  18. --*/
  19. DWORD TrayThreadMessageLoop(LPVOID)
  20. {
  21. MSG msg;
  22. //
  23. // Loop forever and process our messages
  24. //
  25. while( GetMessage( &msg, NULL, 0, 0 ) )
  26. {
  27. switch(msg.message)
  28. {
  29. case PM_INITIALIZEICONS:
  30. {
  31. //
  32. // Add the tray icons to the tray icon cache by adding them all hidden.
  33. //
  34. NOTIFYICONDATA NotifyIconData;
  35. ZeroMemory( &NotifyIconData, sizeof(NotifyIconData) );
  36. NotifyIconData.cbSize = sizeof(NotifyIconData);
  37. NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_STATE;
  38. NotifyIconData.dwState = NIS_HIDDEN;
  39. NotifyIconData.dwStateMask = NotifyIconData.dwState;
  40. NotifyIconData.hWnd = g_hMainWnd;
  41. NotifyIconData.uCallbackMessage = PWM_TRAYICON;
  42. for ( UINT idx = 0; idx < g_cTrayIcons; idx ++ )
  43. {
  44. NotifyIconData.uID = ~idx; // anything but zero
  45. NotifyIconData.hIcon = g_aTrayIcons[ idx ];
  46. Shell_NotifyIcon( NIM_ADD, &NotifyIconData );
  47. }
  48. //
  49. // We now add the zero-th icon which we will used to refer to the hidden
  50. // cached icons we added above. This is the visible icon that users see
  51. // in notification area.
  52. //
  53. NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  54. NotifyIconData.uID = 0;
  55. NotifyIconData.hIcon = g_aTrayIcons[ 0 ];
  56. //
  57. // Initialize with the app title, so that the tray knows that it is
  58. // task manager starting up, rather than "CPU Usage blah blah.."...
  59. //
  60. LoadString( g_hInstance, IDS_APPTITLE, NotifyIconData.szTip, ARRAYSIZE(NotifyIconData.szTip) );
  61. Shell_NotifyIcon( NIM_ADD, &NotifyIconData );
  62. }
  63. break;
  64. case PM_NOTIFYWAITING:
  65. {
  66. NOTIFYICONDATA NotifyIconData;
  67. UINT uIcon = (UINT) msg.wParam;
  68. LPCWSTR pszTipText = (LPCWSTR) msg.lParam;
  69. //
  70. // We need to update the icon. To do this, we tell the tray to
  71. // use one of the icons we cached using NIS_HIDDEN into the
  72. // zero-th position and make it visible. The hIcon indicates
  73. // which icon to retrieve and display.
  74. //
  75. ZeroMemory( &NotifyIconData, sizeof(NotifyIconData) );
  76. NotifyIconData.cbSize = sizeof(NotifyIconData);
  77. NotifyIconData.hWnd = g_hMainWnd;
  78. // NotifyIconData.uID = 0; - zero'ed above
  79. NotifyIconData.uFlags = NIF_STATE | NIF_ICON;
  80. NotifyIconData.dwStateMask = NIS_SHAREDICON;
  81. NotifyIconData.dwState = NotifyIconData.dwStateMask;
  82. NotifyIconData.hIcon = g_aTrayIcons[ uIcon ];
  83. //
  84. // If there is tool tip data to update, add it here and free
  85. // the buffer.
  86. //
  87. if ( NULL != pszTipText)
  88. {
  89. NotifyIconData.uFlags |= NIF_TIP;
  90. StringCchCopy( NotifyIconData.szTip, ARRAYSIZE(NotifyIconData.szTip), pszTipText );
  91. HeapFree( GetProcessHeap( ), 0, (LPVOID) pszTipText );
  92. }
  93. Shell_NotifyIcon( NIM_MODIFY, &NotifyIconData );
  94. }
  95. break;
  96. case PM_QUITTRAYTHREAD:
  97. {
  98. //
  99. // Remove the hidden tray icons.
  100. //
  101. NOTIFYICONDATA NotifyIconData;
  102. ZeroMemory( &NotifyIconData, sizeof(NotifyIconData) );
  103. NotifyIconData.cbSize = sizeof(NotifyIconData);
  104. NotifyIconData.hWnd = g_hMainWnd;
  105. for ( UINT idx = 0; idx < g_cTrayIcons; idx ++ )
  106. {
  107. NotifyIconData.uID = ~idx;
  108. Shell_NotifyIcon( NIM_DELETE, &NotifyIconData );
  109. }
  110. //
  111. // Before we leave, update the tool tip so the "notification
  112. // area manager" has something better than "CPU Usage: 49%"
  113. // to show.
  114. //
  115. LoadString( g_hInstance, IDS_APPTITLE, NotifyIconData.szTip, ARRAYSIZE(NotifyIconData.szTip) );
  116. NotifyIconData.uID = 0;
  117. NotifyIconData.uFlags = NIF_TIP;
  118. Shell_NotifyIcon( NIM_MODIFY, &NotifyIconData );
  119. //
  120. // And now delete the last icon (the one we actually show).
  121. //
  122. NotifyIconData.uFlags = 0;
  123. Shell_NotifyIcon( NIM_DELETE, &NotifyIconData );
  124. g_idTrayThread = 0;
  125. PostQuitMessage(0);
  126. }
  127. break;
  128. default:
  129. ASSERT(0 && "Taskman tray worker got unexpected message");
  130. break;
  131. }
  132. }
  133. return 0;
  134. }
  135. /*++ Tray_Notify (MAIN THREAD CODE)
  136. Routine Description:
  137. Handles notifications sent by the tray
  138. Revision History:
  139. Jan-27-95 Davepl Created
  140. --*/
  141. void Tray_Notify(HWND hWnd, LPARAM lParam)
  142. {
  143. switch (lParam)
  144. {
  145. case WM_LBUTTONDBLCLK:
  146. ShowRunningInstance();
  147. break;
  148. case WM_RBUTTONDOWN:
  149. {
  150. HMENU hPopup = LoadPopupMenu(g_hInstance, IDR_TRAYMENU);
  151. // Display the tray icons context menu at the current cursor location
  152. if (hPopup)
  153. {
  154. POINT pt;
  155. GetCursorPos(&pt);
  156. if (IsWindowVisible(g_hMainWnd))
  157. {
  158. DeleteMenu(hPopup, IDM_RESTORETASKMAN, MF_BYCOMMAND);
  159. }
  160. else
  161. {
  162. SetMenuDefaultItem(hPopup, IDM_RESTORETASKMAN, FALSE);
  163. }
  164. CheckMenuItem(hPopup, IDM_ALWAYSONTOP,
  165. MF_BYCOMMAND | (g_Options.m_fAlwaysOnTop ? MF_CHECKED : MF_UNCHECKED));
  166. SetForegroundWindow(hWnd);
  167. g_fInPopup = TRUE;
  168. TrackPopupMenuEx(hPopup, 0, pt.x, pt.y, hWnd, NULL);
  169. g_fInPopup = FALSE;
  170. DestroyMenu(hPopup);
  171. }
  172. break;
  173. }
  174. }
  175. }