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.

346 lines
9.6 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: icon.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 4/5/1996 RaviR Created
  15. //
  16. //____________________________________________________________________________
  17. #include "..\pch\headers.hxx"
  18. #pragma hdrstop
  19. #include "dbg.h"
  20. #include "macros.h"
  21. #include "..\inc\resource.h"
  22. #include "resource.h"
  23. #include "jobicons.hxx"
  24. extern HINSTANCE g_hInstance;
  25. CJobIcon::CJobIcon(void)
  26. {
  27. m_himlSmall = NULL; // init so that if anything fails before any list
  28. m_himlLarge = NULL; // gets created, the destructor will not fault.
  29. m_himlXLarge = NULL;
  30. //
  31. // Load and setup the small overlay imagelist
  32. //
  33. // if (!LoadImageList(&m_himlSmall, BMP_JOBSTATES, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)))
  34. if (!LoadImageList(&m_himlSmall, BMP_JOBSTATES, 16, 16))
  35. {
  36. DEBUG_OUT((DEB_ERROR, "Small image list failed to load.\n"));
  37. return;
  38. }
  39. //
  40. // Load and setup the large overlay imagelist
  41. //
  42. // if (!LoadImageList(&m_himlLarge, BMP_JOBSTATEL, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)))
  43. if (!LoadImageList(&m_himlLarge, BMP_JOBSTATEL, 32, 32))
  44. {
  45. DEBUG_OUT((DEB_ERROR, "Large image list failed to load.\n"));
  46. return;
  47. }
  48. //
  49. // Load and setup the Xtralarge overlay imagelist
  50. // extra large icons are 48x48
  51. //
  52. if (!LoadImageList(&m_himlXLarge, BMP_JOBSTATEXL, 48, 48))
  53. {
  54. DEBUG_OUT((DEB_ERROR, "XLarge image list failed to load.\n"));
  55. return;
  56. }
  57. }
  58. bool CJobIcon::LoadImageList(HIMAGELIST* phiml, UINT nBmpToLoad, int cx, int cy)
  59. {
  60. //
  61. // Load and setup the overlay imagelist
  62. //
  63. *phiml = ImageList_Create(cx, cy, ILC_COLOR32 | ILC_MASK, 1, 1);
  64. if (!phiml)
  65. {
  66. DEBUG_OUT((DEB_ERROR, "ImageList_Create returned NULL.\n"));
  67. return false;
  68. }
  69. HBITMAP hBmp = (HBITMAP)LoadImage(g_hInstance, MAKEINTRESOURCE(nBmpToLoad), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
  70. if (!hBmp)
  71. {
  72. DEBUG_OUT((DEB_ERROR, "LoadImage returned NULL.\n"));
  73. return false;
  74. }
  75. int i = ImageList_AddMasked(*phiml, hBmp, RGB(0, 255, 0));
  76. // delete this here regardless of whether above call failed so that it won't get leaked by an early return
  77. DeleteObject(hBmp);
  78. if (i == -1)
  79. {
  80. DEBUG_OUT((DEB_ERROR, "ImageList_AddMasked returned -1.\n"));
  81. return false;
  82. }
  83. if ( !ImageList_SetOverlayImage(*phiml, 0, 1) ||
  84. !ImageList_SetOverlayImage(*phiml, 1, 2))
  85. {
  86. DEBUG_OUT((DEB_ERROR, "ImageList_SetOverlayImage returned 0.\n"));
  87. return false;
  88. }
  89. return true;
  90. }
  91. HICON GetDefaultAppIcon(UINT nIconSize)
  92. {
  93. TRACE_FUNCTION(GetDefaultAppIcon);
  94. return (HICON)LoadImage(g_hInstance, (LPCTSTR)IDI_GENERIC, IMAGE_ICON, nIconSize, nIconSize, LR_CREATEDIBSECTION);
  95. }
  96. void
  97. CJobIcon::GetIcons(
  98. LPCTSTR pszApp,
  99. BOOL fEnabled,
  100. HICON * phiconLarge,
  101. HICON * phiconSmall,
  102. UINT nIconSize)
  103. {
  104. TRACE(CJobIcon, GetIcons);
  105. UINT nLargeSize = nIconSize & 0x0000ffff;
  106. UINT nSmallSize = (nIconSize & 0xffff0000) >> 16;
  107. if (nLargeSize && phiconLarge)
  108. {
  109. TS_ExtractIconEx(pszApp, 0, phiconLarge, 1, nLargeSize);
  110. _OverlayIcons(phiconLarge, fEnabled, nLargeSize);
  111. }
  112. if (nSmallSize && phiconSmall)
  113. {
  114. TS_ExtractIconEx(pszApp, 0, phiconSmall, 1, nSmallSize);
  115. _OverlayIcons(phiconSmall, fEnabled, nSmallSize);
  116. }
  117. }
  118. //+--------------------------------------------------------------------------
  119. //
  120. // Member: CJobIcon::GetTemplateIcons
  121. //
  122. // Synopsis: Fill out pointers with large and small template icons
  123. //
  124. // Arguments: [phiconLarge] - NULL or ptr to icon handle to fill
  125. // [phiconSmall] - ditto
  126. //
  127. // History: 5-15-1997 DavidMun Created
  128. //
  129. //---------------------------------------------------------------------------
  130. void
  131. CJobIcon::GetTemplateIcons(
  132. HICON * phiconLarge,
  133. HICON * phiconSmall,
  134. UINT nIconSize)
  135. {
  136. TRACE(CJobIcon, GetTemplateIcons);
  137. UINT nLargeSize = nIconSize & 0x0000ffff; // nIconSize contains the large size in the low word
  138. UINT nSmallSize = (nIconSize & 0xffff0000) >> 16; // nIconSize contains the small size in the high word
  139. if (nLargeSize && phiconLarge)
  140. {
  141. *phiconLarge = (HICON) LoadImage(g_hInstance,
  142. MAKEINTRESOURCE(IDI_TEMPLATE),
  143. IMAGE_ICON,
  144. nLargeSize,
  145. nLargeSize,
  146. LR_CREATEDIBSECTION);
  147. if (!*phiconLarge)
  148. {
  149. DEBUG_OUT_LASTERROR;
  150. }
  151. }
  152. if (nSmallSize && phiconSmall)
  153. {
  154. *phiconSmall = (HICON) LoadImage(g_hInstance,
  155. MAKEINTRESOURCE(IDI_TEMPLATE),
  156. IMAGE_ICON,
  157. nSmallSize,
  158. nSmallSize,
  159. LR_CREATEDIBSECTION);
  160. if (!*phiconSmall)
  161. {
  162. DEBUG_OUT_LASTERROR;
  163. }
  164. }
  165. }
  166. void
  167. CJobIcon::_OverlayIcons(
  168. HICON * phicon,
  169. BOOL fEnabled,
  170. UINT nIconSize)
  171. {
  172. HICON hiconTemp;
  173. if (phicon != NULL)
  174. {
  175. if (*phicon == NULL)
  176. {
  177. *phicon = GetDefaultAppIcon(nIconSize);
  178. }
  179. hiconTemp = OverlayStateIcon(*phicon, fEnabled, nIconSize);
  180. DestroyIcon(*phicon);
  181. *phicon = hiconTemp;
  182. }
  183. }
  184. HICON
  185. CJobIcon::OverlayStateIcon(
  186. HICON hicon,
  187. BOOL fEnabled,
  188. UINT nIconSize)
  189. {
  190. TRACE(CJobIcon, OverlayStateIcon);
  191. HICON hiconOut;
  192. UINT nLargeSize = GetSystemMetrics(SM_CXICON);
  193. UINT nSmallSize = GetSystemMetrics(SM_CXSMICON);
  194. // dont destroy rhiml !!
  195. HIMAGELIST &rhiml = (nIconSize > nLargeSize) ? m_himlXLarge : ((nIconSize > nSmallSize) ? m_himlLarge : m_himlSmall);
  196. int i = ImageList_AddIcon(rhiml, hicon);
  197. hiconOut = ImageList_GetIcon(rhiml, i, INDEXTOOVERLAYMASK((fEnabled ? 1 : 2)));
  198. ImageList_Remove(rhiml, i);
  199. return hiconOut;
  200. }
  201. //+--------------------------------------------------------------------------
  202. //
  203. // Function: TS_ExtractIconEx
  204. //
  205. // Synopsis: Extract the desired icons from an executable. The file is pre-
  206. // processed to avoid NULL and empty files, and files which are
  207. // actually links (they weren't resolved to their exes because they
  208. // are MSI apps -- e.g. Office apps) get resolved to the actual
  209. // executable so that an icon can be obtained.
  210. //
  211. // Arguments: Same as for ExtractIconEx:
  212. //
  213. // LPCTSTR lpszFile, // file name
  214. // int nIconIndex, // icon index
  215. // HICON* phiconLarge, // large icon array
  216. // HICON* phiconSmall, // small icon array
  217. // UINT nIcons, // number of icons to extract
  218. // UINT nIconSize // large (low word) and small (high word) sizes
  219. //
  220. // History: 08-28-2001 ShBrown Created in response to bug #446344
  221. //
  222. //---------------------------------------------------------------------------
  223. UINT TS_ExtractIconEx(
  224. LPCTSTR lpszFile, // file name
  225. int nIconIndex, // icon index
  226. HICON* phicon, // icon array
  227. UINT nIcons, // number of icons to extract
  228. UINT nIconSize // width or height (they're the same)
  229. )
  230. {
  231. if (lpszFile != NULL && *lpszFile != TEXT('\0'))
  232. {
  233. LPTSTR ptszExt = PathFindExtension(lpszFile);
  234. if (ptszExt && !_tcsicmp(ptszExt, TEXT(".LNK")))
  235. {
  236. TCHAR szLnkPath[MAX_PATH];
  237. ResolveLnk(lpszFile, szLnkPath);
  238. return SHExtractIconsW(szLnkPath, nIconIndex, nIconSize, nIconSize, phicon, NULL, nIcons, LR_CREATEDIBSECTION);
  239. }
  240. else
  241. return SHExtractIconsW(lpszFile, nIconIndex, nIconSize, nIconSize, phicon, NULL, nIcons, LR_CREATEDIBSECTION);
  242. }
  243. return 0;
  244. }
  245. //+--------------------------------------------------------------------------
  246. //
  247. // Function: ResolveLnk
  248. //
  249. // Synopsis: Helper for TS_ExtractIconEx. Take a link and find its exe.
  250. //
  251. // Arguments: LPCTSTR lpszLnkPath, // path to link
  252. // LPTSTR lpszExePath // path to exe
  253. //
  254. // History: 08-28-2001 ShBrown Created in response to bug #446344
  255. //
  256. // Mainly stolen from wizard\walklib.cxx and modified
  257. //
  258. //---------------------------------------------------------------------------
  259. void ResolveLnk(
  260. LPCTSTR lpszLnkPath, // path to link
  261. LPTSTR lpszExePath // path to exe
  262. )
  263. {
  264. // Get a pointer to the IShellLink interface.
  265. IShellLink* psl;
  266. HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
  267. if (SUCCEEDED(hres))
  268. {
  269. // Get a pointer to the IPersistFile interface.
  270. IPersistFile* ppf;
  271. hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
  272. if (SUCCEEDED(hres))
  273. {
  274. WCHAR wsz[MAX_PATH];
  275. lstrcpyn(wsz, lpszLnkPath, ARRAYLEN(wsz));
  276. // Load the shell link.
  277. hres = ppf->Load(wsz, STGM_READ);
  278. if (SUCCEEDED(hres))
  279. {
  280. TCHAR szGotPath[MAX_PATH];
  281. lstrcpyn(szGotPath, lpszLnkPath, MAX_PATH);
  282. // Get the path to the link target.
  283. WIN32_FIND_DATA wfdExeData;
  284. hres = psl->GetPath(szGotPath, MAX_PATH, &wfdExeData, SLGP_SHORTPATH );
  285. if (SUCCEEDED(hres))
  286. {
  287. if (lstrlen(szGotPath) > 0)
  288. {
  289. lstrcpyn(lpszExePath, szGotPath, MAX_PATH);
  290. }
  291. }
  292. }
  293. // Release pointer to IPersistFile interface.
  294. ppf->Release();
  295. }
  296. // Release pointer to IShellLink interface.
  297. psl->Release();
  298. }
  299. }