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.

508 lines
12 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2000
  4. *
  5. * TITLE: ImageLst.cpp
  6. *
  7. * VERSION: 1.0
  8. *
  9. * DATE: 2000/11/14
  10. *
  11. * DESCRIPTION: Manages Images Item List
  12. *
  13. *****************************************************************************/
  14. #include <stdafx.h>
  15. #include <mmsystem.h>
  16. #include "wiavideotest.h"
  17. static struct
  18. {
  19. UINT uiNumPicturesInList;
  20. BOOL bExitThread;
  21. HANDLE hItemListThread;
  22. } LOCAL_GVAR =
  23. {
  24. 0,
  25. FALSE,
  26. NULL
  27. };
  28. typedef struct tagThreadArgs_t
  29. {
  30. BOOL bWiaDeviceListMode;
  31. union
  32. {
  33. struct
  34. {
  35. DWORD dwWiaCookie;
  36. IGlobalInterfaceTable *pGIT;
  37. } WiaItemList;
  38. struct
  39. {
  40. TCHAR szImagesDirectory[255 + 1];
  41. } DShowItemList;
  42. };
  43. } ThreadArgs_t;
  44. /****************************Local Function Prototypes********************/
  45. void IncNumPicsInList();
  46. DWORD WINAPI LoadListWithWiaItems(IGlobalInterfaceTable *pGIT,
  47. DWORD dwCookie);
  48. DWORD WINAPI LoadListWithFileItems(const TCHAR *pszImagesDirectory);
  49. DWORD WINAPI ItemListThreadProc(void *pArgs);
  50. ///////////////////////////////
  51. // IncNumPicsInList
  52. //
  53. void IncNumPicsInList()
  54. {
  55. InterlockedIncrement((LONG*) &LOCAL_GVAR.uiNumPicturesInList);
  56. SetDlgItemInt(APP_GVAR.hwndMainDlg, IDC_EDIT_NUM_PICTURES_TAKEN,
  57. LOCAL_GVAR.uiNumPicturesInList, FALSE);
  58. return;
  59. }
  60. ///////////////////////////////
  61. // ImageLst_PostAddImageRequest
  62. //
  63. HRESULT ImageLst_PostAddImageRequest(BSTR bstrNewImage)
  64. {
  65. HRESULT hr = S_OK;
  66. if (bstrNewImage == NULL)
  67. {
  68. return E_POINTER;
  69. }
  70. //
  71. // This will be freed by the AddImageToList function below.
  72. //
  73. BSTR bstrPosted = ::SysAllocString(bstrNewImage);
  74. PostMessage(APP_GVAR.hwndMainDlg, WM_CUSTOM_ADD_IMAGE, 0,
  75. (LPARAM)bstrPosted);
  76. return hr;
  77. }
  78. ///////////////////////////////
  79. // ImageLst_AddImageToList
  80. //
  81. HRESULT ImageLst_AddImageToList(BSTR bstrNewImage)
  82. {
  83. HRESULT hr = S_OK;
  84. TCHAR szNewImage[MAX_PATH] = {0};
  85. if (bstrNewImage == NULL)
  86. {
  87. return E_POINTER;
  88. }
  89. if (hr == S_OK)
  90. {
  91. hr = AppUtil_ConvertToTCHAR((WCHAR*) bstrNewImage,
  92. szNewImage,
  93. sizeof(szNewImage) / sizeof(TCHAR));
  94. }
  95. if (hr == S_OK)
  96. {
  97. //
  98. // Insert at the top of the list.
  99. //
  100. LRESULT lResult = 0;
  101. lResult = SendDlgItemMessage(APP_GVAR.hwndMainDlg,
  102. IDC_LIST_NEW_IMAGES,
  103. LB_ADDSTRING,
  104. 0,
  105. (LPARAM) szNewImage);
  106. WPARAM Index = (WPARAM) lResult;
  107. IncNumPicsInList();
  108. SendDlgItemMessage(APP_GVAR.hwndMainDlg,
  109. IDC_LIST_NEW_IMAGES,
  110. LB_SETCURSEL,
  111. Index,
  112. 0);
  113. }
  114. if (bstrNewImage)
  115. {
  116. ::SysFreeString(bstrNewImage);
  117. }
  118. return hr;
  119. }
  120. ///////////////////////////////
  121. // LoadListWithWiaItems
  122. //
  123. DWORD WINAPI LoadListWithWiaItems(IGlobalInterfaceTable *pGIT,
  124. DWORD dwCookie)
  125. {
  126. HRESULT hr = S_OK;
  127. IWiaItem *pSelectedDevice = NULL;
  128. IEnumWiaItem *pIEnumItem = NULL;
  129. if ((pGIT == NULL) || (dwCookie == 0))
  130. {
  131. return -1;
  132. }
  133. hr = pGIT->GetInterfaceFromGlobal(dwCookie,
  134. IID_IWiaItem,
  135. (void**)&pSelectedDevice);
  136. if (pSelectedDevice == NULL)
  137. {
  138. return -1;
  139. }
  140. hr = pSelectedDevice->EnumChildItems(&pIEnumItem);
  141. if (hr == S_OK)
  142. {
  143. hr = pIEnumItem->Reset();
  144. }
  145. DWORD dwStartTime = 0;
  146. DWORD dwEndTime = 0;
  147. dwStartTime = timeGetTime();
  148. if (hr == S_OK)
  149. {
  150. BOOL bDone = LOCAL_GVAR.bExitThread;
  151. while (!bDone)
  152. {
  153. IWiaItem *pIWiaItem = NULL;
  154. IWiaPropertyStorage *pStorage = NULL;
  155. TCHAR szItemName[MAX_PATH] = {0};
  156. hr = pIEnumItem->Next(1, &pIWiaItem, NULL);
  157. if (LOCAL_GVAR.bExitThread)
  158. {
  159. //
  160. // Exit the thread
  161. //
  162. hr = E_FAIL;
  163. }
  164. if (hr == S_OK)
  165. {
  166. hr = pIWiaItem->QueryInterface(IID_IWiaPropertyStorage, (void**) &pStorage);
  167. }
  168. if (hr == S_OK)
  169. {
  170. PROPVARIANT pv;
  171. PropVariantInit(&pv);
  172. hr = WiaProc_GetProperty(pStorage, WIA_IPA_FULL_ITEM_NAME, &pv);
  173. if (pv.vt == VT_BSTR)
  174. {
  175. ImageLst_PostAddImageRequest(pv.bstrVal);
  176. }
  177. PropVariantClear(&pv);
  178. }
  179. if (pIWiaItem)
  180. {
  181. pIWiaItem->Release();
  182. pIWiaItem = NULL;
  183. }
  184. if (pStorage)
  185. {
  186. pStorage->Release();
  187. pStorage = NULL;
  188. }
  189. if (hr != S_OK)
  190. {
  191. bDone = TRUE;
  192. }
  193. else if (LOCAL_GVAR.bExitThread)
  194. {
  195. bDone = TRUE;
  196. }
  197. }
  198. }
  199. dwEndTime = timeGetTime();
  200. SetDlgItemInt(APP_GVAR.hwndMainDlg,
  201. IDC_EDIT_LIST_LOAD_TIME,
  202. (UINT) abs(dwEndTime - dwStartTime),
  203. FALSE);
  204. if (pIEnumItem)
  205. {
  206. pIEnumItem->Release();
  207. pIEnumItem = NULL;
  208. }
  209. return 0;
  210. }
  211. ///////////////////////////////
  212. // LoadListWithFileItems
  213. //
  214. DWORD WINAPI LoadListWithFileItems(const TCHAR *pszImagesDirectory)
  215. {
  216. HRESULT hr = S_OK;
  217. TCHAR szSearchPath[MAX_PATH + 1] = {0};
  218. HANDLE hFindHandle = NULL;
  219. BOOL bDone = FALSE;
  220. WIN32_FIND_DATA FindData;
  221. if (pszImagesDirectory == NULL)
  222. {
  223. return -1;
  224. }
  225. _sntprintf(szSearchPath, sizeof(szSearchPath) / sizeof(TCHAR),
  226. TEXT("%s\\*.jpg"), pszImagesDirectory);
  227. hFindHandle = FindFirstFile(szSearchPath, &FindData);
  228. if (hFindHandle == INVALID_HANDLE_VALUE)
  229. {
  230. bDone = TRUE;
  231. }
  232. DWORD dwStartTime = 0;
  233. DWORD dwEndTime = 0;
  234. dwStartTime = timeGetTime();
  235. while ((!bDone) && (!LOCAL_GVAR.bExitThread))
  236. {
  237. BOOL bSuccess = FALSE;
  238. BSTR bstrFileName = NULL;
  239. TCHAR szFileName[_MAX_FNAME + MAX_PATH + 1] = {0};
  240. WCHAR wszFileName[_MAX_FNAME + MAX_PATH + 1] = {0};
  241. _sntprintf(szFileName, sizeof(szFileName) / sizeof(TCHAR),
  242. TEXT("%s\\%s"), pszImagesDirectory, FindData.cFileName);
  243. AppUtil_ConvertToWideString(szFileName,
  244. wszFileName,
  245. sizeof(wszFileName) / sizeof(WCHAR));
  246. //
  247. // This is relased by the post processor function
  248. //
  249. bstrFileName = ::SysAllocString(wszFileName);
  250. ImageLst_PostAddImageRequest(bstrFileName);
  251. bSuccess = FindNextFile(hFindHandle, &FindData);
  252. if (!bSuccess)
  253. {
  254. bDone = TRUE;
  255. }
  256. }
  257. dwEndTime = timeGetTime();
  258. SetDlgItemInt(APP_GVAR.hwndMainDlg,
  259. IDC_EDIT_LIST_LOAD_TIME,
  260. (UINT) abs(dwEndTime - dwStartTime),
  261. FALSE);
  262. if (hFindHandle)
  263. {
  264. FindClose(hFindHandle);
  265. hFindHandle = NULL;
  266. }
  267. return 0;
  268. }
  269. ///////////////////////////////
  270. // ItemListThreadProc
  271. //
  272. DWORD WINAPI ItemListThreadProc(void *pArgs)
  273. {
  274. HRESULT hr = S_OK;
  275. ThreadArgs_t *pThreadArgs = (ThreadArgs_t*) pArgs;
  276. if (pThreadArgs == NULL)
  277. {
  278. return -1;
  279. }
  280. if (pThreadArgs->bWiaDeviceListMode == TRUE)
  281. {
  282. LoadListWithWiaItems(pThreadArgs->WiaItemList.pGIT,
  283. pThreadArgs->WiaItemList.dwWiaCookie);
  284. if (pThreadArgs->WiaItemList.pGIT)
  285. {
  286. pThreadArgs->WiaItemList.pGIT->Release();
  287. pThreadArgs->WiaItemList.pGIT = NULL;
  288. }
  289. }
  290. else
  291. {
  292. LoadListWithFileItems(pThreadArgs->DShowItemList.szImagesDirectory);
  293. }
  294. delete pThreadArgs;
  295. pThreadArgs = NULL;
  296. return 0;
  297. }
  298. ///////////////////////////////
  299. // ImageLst_PopulateWiaItemList
  300. //
  301. HRESULT ImageLst_PopulateWiaItemList(IGlobalInterfaceTable *pGIT,
  302. DWORD dwCookie)
  303. {
  304. HRESULT hr = S_OK;
  305. DWORD dwThreadID = 0;
  306. ThreadArgs_t *pArgs = NULL;
  307. if ((pGIT == NULL) ||
  308. (dwCookie == 0))
  309. {
  310. return E_POINTER;
  311. }
  312. LOCAL_GVAR.uiNumPicturesInList = 0;
  313. SetDlgItemInt(APP_GVAR.hwndMainDlg, IDC_EDIT_NUM_PICTURES_TAKEN,
  314. LOCAL_GVAR.uiNumPicturesInList, FALSE);
  315. SetDlgItemInt(APP_GVAR.hwndMainDlg,
  316. IDC_EDIT_LIST_LOAD_TIME,
  317. 0,
  318. FALSE);
  319. pArgs = new ThreadArgs_t;
  320. if (pArgs == NULL)
  321. {
  322. return E_OUTOFMEMORY;
  323. }
  324. ZeroMemory(pArgs, sizeof(*pArgs));
  325. pGIT->AddRef();
  326. pArgs->bWiaDeviceListMode = TRUE;
  327. pArgs->WiaItemList.pGIT = pGIT;
  328. pArgs->WiaItemList.dwWiaCookie = dwCookie;
  329. LOCAL_GVAR.bExitThread = FALSE;
  330. LOCAL_GVAR.hItemListThread = CreateThread(NULL, 0, ItemListThreadProc,
  331. (void*) pArgs,
  332. 0, &dwThreadID);
  333. return hr;
  334. }
  335. ///////////////////////////////
  336. // ImageLst_PopulateDShowItemList
  337. //
  338. HRESULT ImageLst_PopulateDShowItemList(const TCHAR *pszImagesDirectory)
  339. {
  340. HRESULT hr = S_OK;
  341. DWORD dwThreadID = 0;
  342. ThreadArgs_t *pArgs = NULL;
  343. if (pszImagesDirectory == NULL)
  344. {
  345. return E_POINTER;
  346. }
  347. LOCAL_GVAR.uiNumPicturesInList = 0;
  348. SetDlgItemInt(APP_GVAR.hwndMainDlg, IDC_EDIT_NUM_PICTURES_TAKEN,
  349. LOCAL_GVAR.uiNumPicturesInList, FALSE);
  350. SetDlgItemInt(APP_GVAR.hwndMainDlg,
  351. IDC_EDIT_LIST_LOAD_TIME,
  352. 0,
  353. FALSE);
  354. pArgs = new ThreadArgs_t;
  355. if (pArgs == NULL)
  356. {
  357. return E_OUTOFMEMORY;
  358. }
  359. ZeroMemory(pArgs, sizeof(*pArgs));
  360. pArgs->bWiaDeviceListMode = FALSE;
  361. _tcsncpy(pArgs->DShowItemList.szImagesDirectory,
  362. pszImagesDirectory,
  363. sizeof(pArgs->DShowItemList.szImagesDirectory) / sizeof(TCHAR));
  364. LOCAL_GVAR.bExitThread = FALSE;
  365. LOCAL_GVAR.hItemListThread = CreateThread(NULL, 0, ItemListThreadProc,
  366. (void*) pArgs,
  367. 0, &dwThreadID);
  368. return hr;
  369. }
  370. ///////////////////////////////
  371. // ImageLst_Clear
  372. //
  373. HRESULT ImageLst_Clear()
  374. {
  375. HRESULT hr = S_OK;
  376. //
  377. // Clear the New Image List
  378. //
  379. SendDlgItemMessage(APP_GVAR.hwndMainDlg,
  380. IDC_LIST_NEW_IMAGES,
  381. LB_RESETCONTENT,
  382. 0,
  383. 0);
  384. LOCAL_GVAR.uiNumPicturesInList = 0;
  385. SetDlgItemInt(APP_GVAR.hwndMainDlg, IDC_EDIT_NUM_PICTURES_TAKEN,
  386. LOCAL_GVAR.uiNumPicturesInList, FALSE);
  387. return hr;
  388. }
  389. ///////////////////////////////
  390. // ImageLst_CancelLoadAndWait
  391. //
  392. HRESULT ImageLst_CancelLoadAndWait(DWORD dwTimeout)
  393. {
  394. HRESULT hr = S_OK;
  395. if (LOCAL_GVAR.hItemListThread)
  396. {
  397. LOCAL_GVAR.bExitThread = TRUE;
  398. WaitForSingleObject(LOCAL_GVAR.hItemListThread, dwTimeout);
  399. }
  400. return hr;
  401. }