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.

323 lines
8.5 KiB

  1. #include "cabinet.h"
  2. #include "trayitem.h"
  3. #include "shellapi.h"
  4. #include "strsafe.h"
  5. //
  6. // CTrayItem members...
  7. //
  8. DWORD CTrayItem::_GetStateFlag(ICONSTATEFLAG sf)
  9. {
  10. DWORD dwFlag = 0;
  11. switch (sf)
  12. {
  13. case TIF_HIDDEN:
  14. dwFlag = NIS_HIDDEN;
  15. break;
  16. case TIF_DEMOTED:
  17. dwFlag = NISP_DEMOTED;
  18. break;
  19. case TIF_STARTUPICON:
  20. dwFlag = NISP_STARTUPICON;
  21. break;
  22. case TIF_SHARED:
  23. dwFlag = NIS_SHAREDICON;
  24. break;
  25. case TIF_SHAREDICONSOURCE:
  26. dwFlag = NISP_SHAREDICONSOURCE;
  27. break;
  28. case TIF_ONCEVISIBLE:
  29. dwFlag = NISP_ONCEVISIBLE;
  30. break;
  31. case TIF_ITEMCLICKED:
  32. dwFlag = NISP_ITEMCLICKED;
  33. break;
  34. case TIF_ITEMSAMEICONMODIFY:
  35. dwFlag = NISP_ITEMSAMEICONMODIFY;
  36. break;
  37. }
  38. ASSERT(dwFlag);
  39. return dwFlag;
  40. }
  41. void CTrayItem::_SetIconState(ICONSTATEFLAG sf, BOOL bSet)
  42. {
  43. DWORD dwFlag = _GetStateFlag(sf);
  44. ASSERT(dwFlag);
  45. if (bSet)
  46. dwState |= (dwFlag & 0xFFFFFFFF);
  47. else
  48. dwState &= ~dwFlag;
  49. }
  50. BOOL CTrayItem::_CheckIconState(ICONSTATEFLAG sf)
  51. {
  52. DWORD dwFlag = _GetStateFlag(sf);
  53. ASSERT(dwFlag);
  54. return ((dwState & dwFlag) != 0);
  55. }
  56. //
  57. // CTrayItemManager members
  58. //
  59. CTrayItem * CTrayItemManager::GetItemData(INT_PTR i, BOOL byIndex, HWND hwndToolbar)
  60. {
  61. TBBUTTONINFO tbbi;
  62. tbbi.cbSize = sizeof(tbbi);
  63. tbbi.lParam = 0;
  64. tbbi.dwMask = TBIF_LPARAM;
  65. if (byIndex)
  66. tbbi.dwMask |= TBIF_BYINDEX;
  67. SendMessage(hwndToolbar, TB_GETBUTTONINFO, i, (LPARAM)&tbbi);
  68. return (CTrayItem *)(void *)tbbi.lParam;
  69. }
  70. INT_PTR CTrayItemManager::FindItemAssociatedWithGuid(GUID guidItemToCheck)
  71. {
  72. if (guidItemToCheck == GUID_NULL)
  73. return -1;
  74. for (INT_PTR i = GetItemCount()-1; i >= 0; i--)
  75. {
  76. CTrayItem * pti = GetItemDataByIndex(i);
  77. if (pti && pti->IsGuidItemValid() && IsEqualGUID(pti->guidItem, guidItemToCheck))
  78. return i;
  79. }
  80. return -1;
  81. }
  82. INT_PTR CTrayItemManager::FindItemAssociatedWithTimer(UINT_PTR uIconDemoteTimerID)
  83. {
  84. for (INT_PTR i = GetItemCount()-1; i >= 0; i--)
  85. {
  86. CTrayItem * pti = GetItemDataByIndex(i);
  87. if (pti && pti->uIconDemoteTimerID == uIconDemoteTimerID)
  88. return i;
  89. }
  90. return -1;
  91. }
  92. INT_PTR CTrayItemManager::FindItemAssociatedWithHwndUid(HWND hwnd, UINT uID)
  93. {
  94. for (INT_PTR i = GetItemCount() - 1; i >= 0; --i)
  95. {
  96. CTrayItem * pti = GetItemDataByIndex(i);
  97. if (pti && (pti->hWnd == hwnd) && (pti->uID == uID))
  98. {
  99. return i;
  100. }
  101. }
  102. return -1;
  103. }
  104. // Decides if there are as many "TNUP_AUTOMATIC" demoted items in the tray, above the
  105. // threshold that the user has specified...
  106. // Returns TRUE if there is any TNUP_DEMOTED item in the list...
  107. BOOL CTrayItemManager::DemotedItemsPresent(int nMinDemotedItemsThreshold)
  108. {
  109. ASSERT(nMinDemotedItemsThreshold >= 0);
  110. INT_PTR cIcons = 0;
  111. INT_PTR nItems = SendMessage(m_hwndToolbar, TB_BUTTONCOUNT, 0, 0L);
  112. for (INT_PTR i = 0; i < nItems; i++)
  113. {
  114. CTrayItem * pti = GetItemDataByIndex(i);
  115. ASSERT(pti);
  116. // If the item is set to ALWAYS HIDE, then it must be shown in demoted state...
  117. if (pti->dwUserPref == TNUP_DEMOTED)
  118. {
  119. return TRUE;
  120. }
  121. // If the item is demoted, then only if there are enough demoted items must
  122. // they all be shown in demoted state...
  123. else if (pti->IsDemoted())
  124. {
  125. cIcons++;
  126. if (cIcons >= nMinDemotedItemsThreshold)
  127. return TRUE;
  128. }
  129. }
  130. return FALSE;
  131. }
  132. // Works irrespective of whether AutoTray is enabled or not...
  133. INT_PTR CTrayItemManager::_GetItemCountHelper(int nItemFlag, int nItemCountThreshold)
  134. {
  135. INT_PTR cIcons = 0;
  136. INT_PTR nItems = SendMessage(m_hwndToolbar, TB_BUTTONCOUNT, 0, 0L);
  137. switch(nItemFlag)
  138. {
  139. case GIC_ALL:
  140. cIcons = nItems;
  141. break;
  142. case GIC_PROMOTED:
  143. case GIC_DEMOTED:
  144. for (INT_PTR i = nItems-1; i>= 0; i--)
  145. {
  146. CTrayItem * pti = GetItemDataByIndex(i);
  147. if (nItemFlag == GIC_PROMOTED)
  148. {
  149. if (pti && !pti->IsDemoted() && !pti->IsHidden())
  150. cIcons ++;
  151. }
  152. else
  153. {
  154. if (pti && pti->IsDemoted())
  155. cIcons++;
  156. }
  157. if (nItemCountThreshold != -1 && cIcons >= nItemCountThreshold)
  158. break;
  159. }
  160. break;
  161. }
  162. return cIcons;
  163. }
  164. void CTrayItemManager::SetTBBtnImage(INT_PTR iIndex, int iImage)
  165. {
  166. TBBUTTONINFO tbbi;
  167. tbbi.cbSize = sizeof(tbbi);
  168. tbbi.dwMask = TBIF_IMAGE | TBIF_BYINDEX;
  169. tbbi.iImage = iImage;
  170. SendMessage(m_hwndToolbar, TB_SETBUTTONINFO, iIndex, (LPARAM)&tbbi);
  171. }
  172. int CTrayItemManager::GetTBBtnImage(INT_PTR iIndex, BOOL fByIndex /* = TRUE */)
  173. {
  174. TBBUTTONINFO tbbi;
  175. tbbi.cbSize = sizeof(tbbi);
  176. tbbi.dwMask = TBIF_IMAGE;
  177. if (fByIndex)
  178. tbbi.dwMask |= TBIF_BYINDEX;
  179. SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, iIndex, (LPARAM)&tbbi);
  180. return tbbi.iImage;
  181. }
  182. BOOL CTrayItemManager::SetTBBtnStateHelper(INT_PTR iIndex, BYTE fsState, BOOL_PTR bSet)
  183. {
  184. TBBUTTONINFO tbbi;
  185. tbbi.cbSize = sizeof(tbbi);
  186. tbbi.dwMask = TBIF_STATE | TBIF_BYINDEX;
  187. // Get the original state of the button
  188. SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, iIndex, (LPARAM)&tbbi);
  189. // Or the new state to the original state
  190. BYTE fsStateOld = tbbi.fsState;
  191. if (bSet)
  192. tbbi.fsState |= fsState;
  193. else
  194. tbbi.fsState &= ~fsState;
  195. if (tbbi.fsState ^ fsStateOld)
  196. {
  197. SendMessage(m_hwndToolbar, TB_SETBUTTONINFO, iIndex, (LPARAM)&tbbi);
  198. return TRUE;
  199. }
  200. return FALSE;
  201. }
  202. void CTrayItemManager::SetTBBtnText(INT_PTR iIndex, LPTSTR pszText)
  203. {
  204. TBBUTTONINFO tbbi;
  205. tbbi.cbSize = sizeof(tbbi);
  206. tbbi.dwMask = TBIF_TEXT | TBIF_BYINDEX;
  207. tbbi.pszText = pszText;
  208. tbbi.cchText = -1;
  209. SendMessage(m_hwndToolbar, TB_SETBUTTONINFO, iIndex, (LPARAM)&tbbi);
  210. }
  211. int CTrayItemManager::FindImageIndex(HICON hIcon, BOOL fSetAsSharedSource)
  212. {
  213. INT_PTR i;
  214. INT_PTR iCount = GetItemCount();
  215. for (i = 0; i < iCount; i++)
  216. {
  217. CTrayItem * pti = GetItemDataByIndex(i);
  218. if (pti && pti->hIcon == hIcon)
  219. {
  220. // if we're supposed to mark this as a shared icon source and its not itself a shared icon
  221. // target, mark it now. this is to allow us to recognize when the source icon changes and
  222. // that we can know that we need to find other indicies and update them too.
  223. if (fSetAsSharedSource && !pti->IsIconShared())
  224. pti->SetSharedIconSource(TRUE);
  225. return GetTBBtnImage(i);
  226. }
  227. }
  228. return -1;
  229. }
  230. // TO DO szText can be replaced by pti->szIconText
  231. BOOL CTrayItemManager::GetTrayItem(INT_PTR nIndex, CNotificationItem * pni, BOOL * pbStat)
  232. {
  233. if (nIndex < 0 || nIndex >= GetItemCount())
  234. {
  235. *pbStat = FALSE;
  236. return FALSE;
  237. }
  238. ASSERT(pni->hIcon == NULL); // else we're going to leak it
  239. TBBUTTONINFO tbbi;
  240. tbbi.cbSize = sizeof(tbbi);
  241. tbbi.dwMask = TBIF_BYINDEX | TBIF_IMAGE | TBIF_LPARAM | TBIF_TEXT;
  242. TCHAR szText[80] = {0};
  243. tbbi.pszText = szText;
  244. tbbi.cchText = ARRAYSIZE(szText);
  245. if (SendMessage(m_hwndToolbar, TB_GETBUTTONINFO, nIndex, (LPARAM)&tbbi) != -1)
  246. {
  247. CTrayItem * pti = (CTrayItem *)tbbi.lParam;
  248. // don't expose the NIS_HIDDEN icons
  249. if (pti && !pti->IsHidden())
  250. {
  251. pni->hWnd = pti->hWnd;
  252. pni->uID = pti->uID;
  253. pni->hIcon = ImageList_GetIcon(m_himlIcons, tbbi.iImage, ILD_NORMAL);
  254. pni->dwUserPref = pti->dwUserPref;
  255. pni->SetExeName(pti->szExeName);
  256. pni->SetIconText(szText);
  257. memcpy(&(pni->guidItem), &(pti->guidItem), sizeof(pti->guidItem));
  258. *pbStat = TRUE;
  259. return TRUE;
  260. }
  261. }
  262. *pbStat = FALSE;
  263. return TRUE;
  264. }