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.

298 lines
9.7 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. #include <wiadebug.h>
  4. #include <wiaffmt.h>
  5. #include "wiadefui.h"
  6. #include "wiauiext.h"
  7. extern HINSTANCE g_hInstance;
  8. STDMETHODIMP CWiaDefaultUI::Initialize( IWiaItem *pWiaItem, LONG nMediaType )
  9. {
  10. WIA_PUSH_FUNCTION((TEXT("CWiaDefaultUI::Initialize")));
  11. m_WiaFormatPairs.Destroy();
  12. m_nDefaultFormat = 0;
  13. HRESULT hr = S_OK;
  14. if (!pWiaItem)
  15. return(E_POINTER);
  16. GUID guidDefaultClipFormat = GUID_NULL;
  17. //
  18. // Don't worry about failure, we will come up with our own default if the driver doesn't report on
  19. //
  20. PropStorageHelpers::GetProperty( pWiaItem, WIA_IPA_PREFERRED_FORMAT, guidDefaultClipFormat );
  21. WIA_PRINTGUID((guidDefaultClipFormat,TEXT("guidDefaultClipFormat")));
  22. if (guidDefaultClipFormat == GUID_NULL)
  23. {
  24. if (TYMED_FILE == nMediaType || TYMED_MULTIPAGE_FILE == nMediaType)
  25. {
  26. guidDefaultClipFormat = WiaImgFmt_BMP;
  27. }
  28. else
  29. {
  30. guidDefaultClipFormat = WiaImgFmt_MEMORYBMP;
  31. }
  32. }
  33. //
  34. // if the transfer mechanism is known to be incompatible
  35. // with the current tymed, change it
  36. //
  37. if (guidDefaultClipFormat == WiaImgFmt_BMP && nMediaType == TYMED_CALLBACK)
  38. {
  39. guidDefaultClipFormat = WiaImgFmt_MEMORYBMP;
  40. }
  41. else if ((guidDefaultClipFormat == WiaImgFmt_MEMORYBMP) && ((nMediaType == TYMED_FILE) || (TYMED_MULTIPAGE_FILE == nMediaType)))
  42. {
  43. guidDefaultClipFormat = WiaImgFmt_BMP;
  44. }
  45. //
  46. // Get the data transfer interface
  47. //
  48. CComPtr<IWiaDataTransfer> pWiaDataTransfer;
  49. hr = pWiaItem->QueryInterface( IID_IWiaDataTransfer, (void**)&pWiaDataTransfer );
  50. if (SUCCEEDED(hr))
  51. {
  52. //
  53. // Get the format info enumerator
  54. //
  55. CComPtr<IEnumWIA_FORMAT_INFO> pEnumWIA_FORMAT_INFO;
  56. hr = pWiaDataTransfer->idtEnumWIA_FORMAT_INFO(&pEnumWIA_FORMAT_INFO);
  57. if (SUCCEEDED(hr))
  58. {
  59. //
  60. // Enumerate the formats
  61. //
  62. ULONG ulFetched = 0;
  63. WIA_FORMAT_INFO WiaFormatInfo;
  64. while (pEnumWIA_FORMAT_INFO->Next(1,&WiaFormatInfo,&ulFetched) == S_OK)
  65. {
  66. //
  67. // If this is the media type we are interested in...
  68. //
  69. if (static_cast<LONG>(WiaFormatInfo.lTymed) == nMediaType)
  70. {
  71. //
  72. // The friendly description for this file type
  73. //
  74. CSimpleString strDescription;
  75. //
  76. // Get the file extension for this type
  77. //
  78. CSimpleString strExtension = CWiaFileFormat::GetExtension( WiaFormatInfo.guidFormatID, nMediaType, pWiaItem );
  79. if (strExtension.Length())
  80. {
  81. //
  82. // Save the extension
  83. //
  84. CSimpleString strExtensionPlusDot = TEXT(".");
  85. strExtensionPlusDot += strExtension;
  86. if (strExtensionPlusDot.Length())
  87. {
  88. //
  89. // Get the description
  90. //
  91. SHFILEINFO SHFileInfo;
  92. if (SHGetFileInfo(strExtensionPlusDot.String(), FILE_ATTRIBUTE_NORMAL, &SHFileInfo, sizeof(SHFILEINFO), SHGFI_USEFILEATTRIBUTES|SHGFI_TYPENAME ))
  93. {
  94. strDescription = SHFileInfo.szTypeName;
  95. }
  96. }
  97. }
  98. //
  99. // SUCCESS! Save the extension and description
  100. //
  101. int nIndex = m_WiaFormatPairs.Append( CWiaFormatPair( static_cast<GUID>(WiaFormatInfo.guidFormatID), strExtension, CSimpleStringConvert::WideString(strDescription) ) );
  102. //
  103. // Save the default format index, if this is the default format
  104. //
  105. if (guidDefaultClipFormat == WiaFormatInfo.guidFormatID)
  106. {
  107. m_nDefaultFormat = nIndex;
  108. }
  109. }
  110. }
  111. }
  112. }
  113. return(hr);
  114. }
  115. STDMETHODIMP CWiaDefaultUI::GetFormatCount( LONG *pnCount )
  116. {
  117. HRESULT hr = S_OK;
  118. if (!pnCount)
  119. return(E_POINTER);
  120. *pnCount = m_WiaFormatPairs.Size();
  121. return(hr);
  122. }
  123. STDMETHODIMP CWiaDefaultUI::GetFormatType( LONG nFormat, GUID *pcfClipFormat )
  124. {
  125. WIA_PUSH_FUNCTION((TEXT("CWiaDefaultUI::GetFormatType")));
  126. WIA_PRINTGUID((*pcfClipFormat,TEXT("nFormat: %d, pcfClipFormat:"),nFormat));
  127. WIA_TRACE((TEXT("m_WiaFormatPairs.Size(): %d"),m_WiaFormatPairs.Size()));
  128. HRESULT hr = S_OK;
  129. // Out of range
  130. if (nFormat >= m_WiaFormatPairs.Size() || nFormat < 0)
  131. return(E_FAIL);
  132. *pcfClipFormat = m_WiaFormatPairs[nFormat].Type();
  133. return(hr);
  134. }
  135. STDMETHODIMP CWiaDefaultUI::GetFormatExtension( LONG nFormat, LPWSTR pszExtension, int nMaxLen )
  136. {
  137. HRESULT hr = S_OK;
  138. // Out of range
  139. if (nFormat >= m_WiaFormatPairs.Size() || nFormat < 0)
  140. return(E_FAIL);
  141. CSimpleStringWide str = m_WiaFormatPairs[nFormat].Extension();
  142. if (static_cast<int>(str.Length()) >= nMaxLen)
  143. return(E_FAIL);
  144. lstrcpyW( pszExtension, str.String() );
  145. return(hr);
  146. }
  147. STDMETHODIMP CWiaDefaultUI::GetFormatDescription( LONG nFormat, LPWSTR pszDescription, int nMaxLen )
  148. {
  149. HRESULT hr = S_OK;
  150. // Out of range
  151. if (nFormat >= m_WiaFormatPairs.Size() || nFormat < 0)
  152. return(E_FAIL);
  153. CSimpleStringWide str = m_WiaFormatPairs[nFormat].Description();
  154. if (static_cast<int>(str.Length()) >= nMaxLen)
  155. return(E_FAIL);
  156. lstrcpyW( pszDescription, str.String() );
  157. return(hr);
  158. }
  159. STDMETHODIMP CWiaDefaultUI::GetDefaultClipboardFileFormat( GUID *pguidFormat )
  160. {
  161. if (!pguidFormat)
  162. return E_POINTER;
  163. return GetFormatType(m_nDefaultFormat,pguidFormat);
  164. }
  165. STDMETHODIMP CWiaDefaultUI::GetDefaultClipboardFileFormatIndex( LONG *pnIndex )
  166. {
  167. if (!pnIndex)
  168. return E_POINTER;
  169. if (m_nDefaultFormat >= m_WiaFormatPairs.Size() || m_nDefaultFormat < 0)
  170. return(E_FAIL);
  171. *pnIndex = m_nDefaultFormat;
  172. return S_OK;
  173. }
  174. STDMETHODIMP CWiaDefaultUI::GetClipboardFileExtension( GUID guidFormat, LPWSTR pszExt, DWORD nMaxLen )
  175. {
  176. if (!pszExt)
  177. {
  178. return E_POINTER;
  179. }
  180. CSimpleString strExtension = CWiaFileFormat::GetExtension( guidFormat );
  181. if (strExtension.Length())
  182. {
  183. CSimpleStringWide strwExtension = CSimpleStringConvert::WideString(strExtension);
  184. if (strwExtension.Length() < nMaxLen)
  185. {
  186. lstrcpyW( pszExt, strwExtension.String() );
  187. return S_OK;
  188. }
  189. }
  190. return S_FALSE;
  191. }
  192. STDMETHODIMP CWiaDefaultUI::ChangeClipboardFileExtension( GUID guidFormat, LPWSTR pszFilename, DWORD nMaxLen )
  193. {
  194. HRESULT hr = S_OK;
  195. if (!pszFilename || guidFormat==GUID_NULL) // We don't accept a default type here
  196. {
  197. return E_INVALIDARG;
  198. }
  199. WCHAR szExtension[MAX_PATH]=L"";
  200. GetClipboardFileExtension( guidFormat, szExtension, ARRAYSIZE(szExtension) );
  201. if (!lstrlenW(szExtension))
  202. {
  203. return S_FALSE; // Not really an error, just an unknown file type.
  204. }
  205. CSimpleStringWide strName(pszFilename);
  206. // Make sure the string is valid
  207. if (strName.Length())
  208. {
  209. int nPeriodFind = strName.ReverseFind( L'.' );
  210. int nBSlashFind = strName.ReverseFind( L'\\' );
  211. if (nPeriodFind < 0) // No extension found
  212. {
  213. strName += L'.';
  214. strName += szExtension;
  215. }
  216. else if (nPeriodFind > nBSlashFind) // Assume this is an extension, because it is following a back slash
  217. {
  218. strName = strName.Left(nPeriodFind);
  219. strName += L'.';
  220. strName += szExtension;
  221. }
  222. else // It must not be an extension
  223. {
  224. strName += L'.';
  225. strName += szExtension;
  226. }
  227. // Make sure this string can handle the addition of the extension
  228. if ((strName.Length()+1) <= nMaxLen)
  229. {
  230. lstrcpyW( pszFilename, strName.String() );
  231. }
  232. else
  233. {
  234. hr = E_INVALIDARG;
  235. }
  236. }
  237. return hr;
  238. }
  239. STDMETHODIMP CWiaDefaultUI::ConstructFileOpenDialogStyleString( BSTR *pbstrString )
  240. {
  241. HRESULT hr = S_OK;
  242. int nLength = 0;
  243. // For each ext: "Foo Files (*.foo)|*.foo|"
  244. for (int i=0;i<m_WiaFormatPairs.Size();i++)
  245. {
  246. nLength += m_WiaFormatPairs[i].Description().Length() +
  247. lstrlenW(L" (*.") +
  248. m_WiaFormatPairs[i].Extension().Length() +
  249. lstrlenW(L")|*.") +
  250. m_WiaFormatPairs[i].Extension().Length() +
  251. lstrlenW(L"|");
  252. }
  253. *pbstrString = SysAllocStringLen( NULL, nLength );
  254. if (*pbstrString)
  255. {
  256. CSimpleStringWide strTmp;
  257. for (i=0;i<m_WiaFormatPairs.Size();i++)
  258. {
  259. strTmp += m_WiaFormatPairs[i].Description() +
  260. L" (*." +
  261. m_WiaFormatPairs[i].Extension() +
  262. L")|*." +
  263. m_WiaFormatPairs[i].Extension() +
  264. L"|";
  265. CopyMemory( *pbstrString, strTmp.String(), nLength * sizeof(WCHAR) );
  266. }
  267. }
  268. else hr = E_OUTOFMEMORY;
  269. return(hr);
  270. }