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.

431 lines
13 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2000
  4. *
  5. * TITLE: WIAFFMT.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 8/24/2000
  12. *
  13. * DESCRIPTION: Helper class which encapsulates WIA image file formats
  14. *
  15. *******************************************************************************/
  16. #ifndef __WIAFFMT_H_INCLUDED
  17. #define __WIAFFMT_H_INCLUDED
  18. #include <windows.h>
  19. #include <simstr.h>
  20. #include <wia.h>
  21. #include <pshelper.h>
  22. class CWiaFileFormat
  23. {
  24. private:
  25. GUID m_guidFormat;
  26. CSimpleString m_strExtension;
  27. CSimpleString m_strDescription;
  28. HICON m_hIcon;
  29. public:
  30. CWiaFileFormat( const GUID &guidFormat=IID_NULL, const CSimpleString &strExtension=TEXT(""), const CSimpleString &strDescription=TEXT(""), const HICON hIcon=NULL )
  31. : m_guidFormat(guidFormat),
  32. m_strExtension(strExtension),
  33. m_strDescription(strDescription),
  34. m_hIcon(NULL)
  35. {
  36. if (hIcon)
  37. {
  38. m_hIcon = CopyIcon(hIcon);
  39. }
  40. }
  41. CWiaFileFormat( const CWiaFileFormat &other )
  42. : m_guidFormat(other.Format()),
  43. m_strExtension(other.Extension()),
  44. m_strDescription(other.Description()),
  45. m_hIcon(NULL)
  46. {
  47. if (other.Icon())
  48. {
  49. m_hIcon = CopyIcon(other.Icon());
  50. }
  51. }
  52. CWiaFileFormat( const GUID &guidFormat, const HICON hDefaultIcon )
  53. : m_guidFormat(guidFormat),
  54. m_strExtension(GetExtension(guidFormat)),
  55. m_strDescription(TEXT("")),
  56. m_hIcon(NULL)
  57. {
  58. if (m_strExtension.Length())
  59. {
  60. AcquireDescription();
  61. AcquireIcon(hDefaultIcon);
  62. }
  63. }
  64. ~CWiaFileFormat(void)
  65. {
  66. Destroy();
  67. }
  68. CWiaFileFormat &operator=( const CWiaFileFormat &other )
  69. {
  70. if (this != &other)
  71. {
  72. m_guidFormat = other.Format();
  73. m_strExtension = other.Extension();
  74. m_strDescription = other.Description();
  75. if (other.Icon())
  76. {
  77. m_hIcon = CopyIcon(other.Icon());
  78. }
  79. }
  80. return *this;
  81. }
  82. void Destroy(void)
  83. {
  84. m_guidFormat = IID_NULL;
  85. m_strExtension = TEXT("");
  86. m_strDescription = TEXT("");
  87. if (m_hIcon)
  88. {
  89. DestroyIcon(m_hIcon);
  90. m_hIcon = NULL;
  91. }
  92. }
  93. bool IsValid(void) const
  94. {
  95. return (m_guidFormat != IID_NULL && m_strExtension.Length());
  96. }
  97. HICON AcquireIcon( const HICON hDefaultIcon, bool bSmall=true )
  98. {
  99. if (!IsValid())
  100. {
  101. return false;
  102. }
  103. //
  104. // If we've already gotten the icon, return it
  105. //
  106. if (m_hIcon)
  107. {
  108. return m_hIcon;
  109. }
  110. //
  111. // Use the extension to get the icon
  112. //
  113. CSimpleString strExtension(m_strExtension.ToUpper());
  114. SHFILEINFO SHFileInfo = {0};
  115. if (SHGetFileInfo( CSimpleString(TEXT(".")) + strExtension, 0, &SHFileInfo, sizeof(SHFileInfo), bSmall ? SHGFI_SMALLICON|SHGFI_ICON|SHGFI_USEFILEATTRIBUTES : SHGFI_ICON|SHGFI_USEFILEATTRIBUTES ))
  116. {
  117. //
  118. // We will take ownership of this icon
  119. //
  120. if (SHFileInfo.hIcon)
  121. {
  122. m_hIcon = SHFileInfo.hIcon;
  123. }
  124. }
  125. //
  126. // If we haven't gotten the icon, use the default icon
  127. //
  128. if (!m_hIcon && hDefaultIcon)
  129. {
  130. m_hIcon = CopyIcon(hDefaultIcon);
  131. }
  132. return m_hIcon;
  133. }
  134. CSimpleString AcquireDescription(void)
  135. {
  136. if (!IsValid())
  137. {
  138. return TEXT("");
  139. }
  140. //
  141. // If we've already gotten the description, return it
  142. //
  143. if (m_strDescription.Length())
  144. {
  145. return m_strDescription;
  146. }
  147. //
  148. // Use the extension to get the description
  149. //
  150. CSimpleString strExtension(m_strExtension.ToUpper());
  151. SHFILEINFO SHFileInfo = {0};
  152. if (SHGetFileInfo( CSimpleString(TEXT(".")) + strExtension, 0, &SHFileInfo, sizeof(SHFileInfo), SHGFI_USEFILEATTRIBUTES|SHGFI_TYPENAME ))
  153. {
  154. //
  155. // We will take ownership of this icon
  156. //
  157. if (lstrlen(SHFileInfo.szTypeName))
  158. {
  159. m_strDescription = SHFileInfo.szTypeName;
  160. }
  161. }
  162. return m_strDescription;
  163. }
  164. static CSimpleString GetExtension( const GUID &guidFormat )
  165. {
  166. static const struct
  167. {
  168. const GUID *guidFormat;
  169. LPCTSTR pszExtension;
  170. }
  171. cs_WiaFormatExtensions[] =
  172. {
  173. { &WiaImgFmt_BMP, TEXT("bmp")},
  174. { &WiaImgFmt_MEMORYBMP, TEXT("bmp")},
  175. { &WiaImgFmt_JPEG, TEXT("jpg")},
  176. { &WiaImgFmt_TIFF, TEXT("tif")},
  177. { &WiaImgFmt_FLASHPIX, TEXT("fpx")},
  178. { &WiaImgFmt_GIF, TEXT("gif")},
  179. { &WiaImgFmt_EXIF, TEXT("jpg")},
  180. { &WiaImgFmt_WMF, TEXT("wmf")},
  181. { &WiaImgFmt_PNG, TEXT("png")},
  182. { &WiaImgFmt_PHOTOCD, TEXT("pcd")},
  183. { &WiaImgFmt_EMF, TEXT("emf")},
  184. { &WiaImgFmt_ICO, TEXT("ico")},
  185. { &WiaAudFmt_WAV, TEXT("wav")},
  186. { &WiaAudFmt_MP3, TEXT("mp3")},
  187. { &WiaAudFmt_AIFF, TEXT("aif")},
  188. { &WiaAudFmt_WMA, TEXT("wma")},
  189. { &WiaImgFmt_RTF, TEXT("rtf")},
  190. { &WiaImgFmt_XML, TEXT("xml")},
  191. { &WiaImgFmt_HTML, TEXT("htm")},
  192. { &WiaImgFmt_TXT, TEXT("txt")},
  193. { &WiaImgFmt_MPG, TEXT("mpg")},
  194. { &WiaImgFmt_AVI, TEXT("avi")}
  195. };
  196. for (int i=0;i<ARRAYSIZE(cs_WiaFormatExtensions);i++)
  197. {
  198. if (*cs_WiaFormatExtensions[i].guidFormat == guidFormat)
  199. {
  200. return cs_WiaFormatExtensions[i].pszExtension;
  201. }
  202. }
  203. return TEXT("");
  204. }
  205. static CSimpleString GetExtension( const GUID &guidFormat, LONG nMediaType, IUnknown *pUnknown )
  206. {
  207. //
  208. // First, try to get the extension from the static table above
  209. //
  210. CSimpleString strResult = GetExtension( guidFormat );
  211. if (!strResult.Length())
  212. {
  213. //
  214. // Save the current media type and format, because (unfortunately), there is no way to
  215. // get the preferred extension for a given format without setting it.
  216. //
  217. LONG nOldMediaType = 0;
  218. if (PropStorageHelpers::GetProperty( pUnknown, WIA_IPA_TYMED, nOldMediaType ))
  219. {
  220. //
  221. // Save the current format
  222. //
  223. GUID guidOldFormat = IID_NULL;
  224. if (PropStorageHelpers::GetProperty( pUnknown, WIA_IPA_FORMAT, guidOldFormat ))
  225. {
  226. //
  227. // Set the format and media type to the ones chosen
  228. //
  229. if (PropStorageHelpers::SetProperty( pUnknown, WIA_IPA_FORMAT, guidFormat ) &&
  230. PropStorageHelpers::SetProperty( pUnknown, WIA_IPA_TYMED, nMediaType ))
  231. {
  232. //
  233. // Try to read the extension property
  234. //
  235. CSimpleStringWide strwExtension;
  236. if (PropStorageHelpers::GetProperty( pUnknown, WIA_IPA_FILENAME_EXTENSION, strwExtension ))
  237. {
  238. //
  239. // If we got an extension, save it as the result
  240. //
  241. strResult = CSimpleStringConvert::NaturalString(strwExtension);
  242. }
  243. }
  244. //
  245. // Restore the original format property
  246. //
  247. PropStorageHelpers::SetProperty( pUnknown, WIA_IPA_FORMAT, guidOldFormat );
  248. }
  249. //
  250. // Restore the original media type property
  251. //
  252. PropStorageHelpers::SetProperty( pUnknown, WIA_IPA_TYMED, nOldMediaType );
  253. }
  254. }
  255. return strResult;
  256. }
  257. static bool IsKnownAudioFormat( const GUID &guidFormat )
  258. {
  259. static const GUID *cs_WiaAudioFormats[] =
  260. {
  261. &WiaAudFmt_WAV,
  262. &WiaAudFmt_MP3,
  263. &WiaAudFmt_AIFF,
  264. &WiaAudFmt_WMA
  265. };
  266. for (int i=0;i<ARRAYSIZE(cs_WiaAudioFormats);i++)
  267. {
  268. if (*cs_WiaAudioFormats[i] == guidFormat)
  269. {
  270. return true;
  271. }
  272. }
  273. return false;
  274. }
  275. GUID Format(void) const
  276. {
  277. return m_guidFormat;
  278. }
  279. CSimpleString Extension(void) const
  280. {
  281. return m_strExtension;
  282. }
  283. CSimpleString Description(void) const
  284. {
  285. return m_strDescription;
  286. }
  287. HICON Icon(void) const
  288. {
  289. return m_hIcon;
  290. }
  291. void Format( const GUID &guidFormat )
  292. {
  293. m_guidFormat = guidFormat;
  294. }
  295. void Extension( const CSimpleString &strExtension )
  296. {
  297. m_strExtension = strExtension;
  298. }
  299. void Description( const CSimpleString &strDescription )
  300. {
  301. m_strDescription = strDescription;
  302. }
  303. void Icon( const HICON hIcon )
  304. {
  305. if (m_hIcon)
  306. {
  307. DestroyIcon(m_hIcon);
  308. m_hIcon = NULL;
  309. }
  310. if (hIcon)
  311. {
  312. m_hIcon = CopyIcon(m_hIcon);
  313. }
  314. }
  315. bool operator==( const CWiaFileFormat &other )
  316. {
  317. return ((other.Format() == Format()) != 0);
  318. }
  319. bool operator==( const GUID &guidFormat )
  320. {
  321. return ((guidFormat == Format()) != 0);
  322. }
  323. void Dump(void)
  324. {
  325. WIA_PUSHFUNCTION(TEXT("CWiaFileFormat::Dump()"));
  326. WIA_PRINTGUID((m_guidFormat,TEXT(" m_guidFormat")));
  327. WIA_TRACE((TEXT(" m_strExtension: %s"), m_strExtension.String()));
  328. WIA_TRACE((TEXT(" m_strDescription: %s"), m_strDescription.String()));
  329. WIA_TRACE((TEXT(" m_strDescription: %p"), m_hIcon));
  330. }
  331. };
  332. class CWiaFileFormatList
  333. {
  334. private:
  335. CSimpleDynamicArray<CWiaFileFormat> m_FormatList;
  336. private:
  337. CWiaFileFormatList( const CWiaFileFormatList & );
  338. CWiaFileFormatList &operator=( const CWiaFileFormatList & );
  339. public:
  340. CWiaFileFormatList(void);
  341. CWiaFileFormatList( const GUID **pguidFormats, UINT nFormatCount, HICON hDefaultIcon )
  342. {
  343. if (pguidFormats)
  344. {
  345. for (UINT i=0;i<nFormatCount;i++)
  346. {
  347. m_FormatList.Append( CWiaFileFormat( *pguidFormats[i], hDefaultIcon ) );
  348. }
  349. }
  350. }
  351. CWiaFileFormatList( IWiaItem *pWiaItem, HICON hDefaultIcon )
  352. {
  353. //
  354. // Get the data transfer interface
  355. //
  356. CComPtr<IWiaDataTransfer> pWiaDataTransfer;
  357. HRESULT hr = pWiaItem->QueryInterface( IID_IWiaDataTransfer, (void**)&pWiaDataTransfer );
  358. if (SUCCEEDED(hr))
  359. {
  360. //
  361. // Get the format info enumerator
  362. //
  363. CComPtr<IEnumWIA_FORMAT_INFO> pEnumWIA_FORMAT_INFO;
  364. hr = pWiaDataTransfer->idtEnumWIA_FORMAT_INFO(&pEnumWIA_FORMAT_INFO);
  365. if (SUCCEEDED(hr))
  366. {
  367. //
  368. // Enumerate the formats
  369. //
  370. ULONG ulFetched = 0;
  371. WIA_FORMAT_INFO WiaFormatInfo;
  372. while (pEnumWIA_FORMAT_INFO->Next(1,&WiaFormatInfo,&ulFetched) == S_OK)
  373. {
  374. if (WiaFormatInfo.lTymed == TYMED_FILE)
  375. {
  376. CWiaFileFormat newFormat(WiaFormatInfo.guidFormatID,hDefaultIcon);
  377. if (newFormat.IsValid())
  378. {
  379. m_FormatList.Append(newFormat);
  380. }
  381. }
  382. }
  383. }
  384. }
  385. }
  386. const CSimpleDynamicArray<CWiaFileFormat> &FormatList(void) const
  387. {
  388. return m_FormatList;
  389. }
  390. CWiaFileFormatList &Union( const CWiaFileFormatList &other )
  391. {
  392. for (int nSource=0;nSource<other.FormatList().Size();nSource++)
  393. {
  394. int nFindResult = m_FormatList.Find(other.FormatList()[nSource]);
  395. if (nFindResult < 0)
  396. {
  397. m_FormatList.Append(other.FormatList()[nSource]);
  398. }
  399. }
  400. return *this;
  401. }
  402. void Dump(void)
  403. {
  404. for (int i=0;i<m_FormatList.Size();i++)
  405. {
  406. m_FormatList[i].Dump();
  407. }
  408. }
  409. };
  410. #endif // __WIAFFMT_H_INCLUDED