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.

298 lines
6.8 KiB

  1. // ProgressDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "msqscan.h"
  5. #include "ProgressDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. //
  12. // globals
  13. //
  14. extern IGlobalInterfaceTable * g_pGIT;
  15. HWND g_hWnd = NULL;
  16. BOOL g_bCancel = FALSE; // use global for now, It would be better to use an "event"
  17. BOOL g_bPaintPreview = TRUE;
  18. /////////////////////////////////////////////////////////////////////////////
  19. // Thread Information
  20. UINT WINAPIV DataAcquireThreadProc(LPVOID pParam)
  21. {
  22. HRESULT hr = S_OK;
  23. //
  24. // Initialize COM, for this thread
  25. //
  26. hr = CoInitialize(NULL);
  27. if(SUCCEEDED(hr)) {
  28. //
  29. // set global cancel flag
  30. //
  31. g_bCancel = FALSE;
  32. //
  33. // prepare, and use the DATA_ACQUIRE_INFO struct
  34. //
  35. DATA_ACQUIRE_INFO *pDataAcquireInfo = (DATA_ACQUIRE_INFO*)pParam;
  36. pDataAcquireInfo->pProgressFunc = &ProgressFunction;
  37. g_bPaintPreview = pDataAcquireInfo->bPreview;
  38. IWiaItem *pIWiaRootItem = NULL;
  39. hr = ReadInterfaceFromGlobalInterfaceTable(pDataAcquireInfo->dwCookie, &pIWiaRootItem);
  40. if(SUCCEEDED(hr)) {
  41. //
  42. // create a new WIA object for data transfer
  43. //
  44. CWIA MyWIA;
  45. //
  46. // set the Root Item, used for current settings
  47. //
  48. MyWIA.SetRootItem(pIWiaRootItem);
  49. //
  50. // Initiate WIA Transfer
  51. //
  52. if(pDataAcquireInfo->bTransferToFile) {
  53. hr = MyWIA.DoFileTransfer(pDataAcquireInfo);
  54. if(SUCCEEDED(hr)) {
  55. OutputDebugString(TEXT("WIA File Transfer is complete...\n"));
  56. } else if(hr == WIA_ERROR_PAPER_EMPTY){
  57. MessageBox(NULL,TEXT("Document Feeder is out of Paper"),TEXT("ADF Status Message"),MB_ICONERROR);
  58. }
  59. } else {
  60. hr = MyWIA.DoBandedTransfer(pDataAcquireInfo);
  61. if(SUCCEEDED(hr)) {
  62. OutputDebugString(TEXT("WIA Banded Transfer is complete...\n"));
  63. }
  64. }
  65. //
  66. // Do Window messaging, while thread processes
  67. //
  68. while (!MyWIA.IsAcquireComplete()) {
  69. MSG message;
  70. if(::PeekMessage(&message, NULL, 0, 0, PM_NOREMOVE)) {
  71. ::TranslateMessage(&message);
  72. ::DispatchMessage(&message);
  73. }
  74. }
  75. }
  76. //
  77. // Uninitialize COM, for this thread
  78. //
  79. CoUninitialize();
  80. }
  81. //
  82. // Post the quit message, to close the progress dialog
  83. //
  84. ::PostMessage(g_hWnd, WM_CANCEL_ACQUIRE, 0, 0);
  85. return 0;
  86. }
  87. /////////////////////////////////////////////////////////////////////////////
  88. // Progress Callback function
  89. BOOL ProgressFunction(LPTSTR lpszText, LONG lPercentComplete)
  90. {
  91. ::PostMessage(g_hWnd, WM_STEP_PROGRESS, 0, lPercentComplete);
  92. ::PostMessage(g_hWnd, WM_UPDATE_PROGRESS_TEXT, 0, (LPARAM)lpszText);
  93. if(g_bPaintPreview) {
  94. //
  95. // make parent update it's preview
  96. //
  97. HWND hParentWnd = NULL;
  98. hParentWnd = GetParent(g_hWnd);
  99. if(hParentWnd != NULL)
  100. ::PostMessage(hParentWnd,WM_UPDATE_PREVIEW,0,0);
  101. }
  102. return g_bCancel;
  103. }
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CProgressDlg dialog
  106. CProgressDlg::CProgressDlg(CWnd* pParent /*=NULL*/)
  107. : CDialog(CProgressDlg::IDD, pParent)
  108. {
  109. //{{AFX_DATA_INIT(CProgressDlg)
  110. m_ProgressText = _T("");
  111. m_pDataAcquireThread = NULL;
  112. //}}AFX_DATA_INIT
  113. }
  114. void CProgressDlg::SetAcquireData(DATA_ACQUIRE_INFO* pDataAcquireInfo)
  115. {
  116. m_pDataAcquireInfo = pDataAcquireInfo;
  117. }
  118. void CProgressDlg::DoDataExchange(CDataExchange* pDX)
  119. {
  120. CDialog::DoDataExchange(pDX);
  121. //{{AFX_DATA_MAP(CProgressDlg)
  122. DDX_Control(pDX, IDC_CANCEL, m_CancelButton);
  123. DDX_Control(pDX, IDC_PROGRESS_CONTROL, m_ProgressCtrl);
  124. DDX_Text(pDX, IDC_PROGRESS_TEXT, m_ProgressText);
  125. //}}AFX_DATA_MAP
  126. }
  127. BEGIN_MESSAGE_MAP(CProgressDlg, CDialog)
  128. //{{AFX_MSG_MAP(CProgressDlg)
  129. ON_BN_CLICKED(IDC_CANCEL, OnCancel)
  130. //}}AFX_MSG_MAP
  131. END_MESSAGE_MAP()
  132. /////////////////////////////////////////////////////////////////////////////
  133. // CProgressDlg message handlers
  134. void CProgressDlg::OnCancel()
  135. {
  136. //
  137. // suspend the data acquire thread
  138. //
  139. m_pDataAcquireThread->SuspendThread();
  140. //
  141. // set the global cancel flag
  142. //
  143. g_bCancel = TRUE;
  144. //
  145. // resume the data acquire thread
  146. //
  147. m_pDataAcquireThread->ResumeThread();
  148. //
  149. // post a nice, wait message while WIA catches up with the
  150. // cancel..ie. S_FALSE sent through the callback Interface
  151. //
  152. m_ProgressText = TEXT("Please Wait... Your Acquire is being canceled.");
  153. UpdateData(FALSE);
  154. //
  155. // disable the 'cancel' button, to show the user that somthing did happen,
  156. // when they pushed 'cancel'.
  157. //
  158. m_CancelButton.EnableWindow(FALSE);
  159. }
  160. BOOL CProgressDlg::OnInitDialog()
  161. {
  162. CDialog::OnInitDialog();
  163. //
  164. // set the progress range, 0-100% complete
  165. //
  166. m_ProgressCtrl.SetRange(0,100);
  167. m_ProgressCtrl.SetPos(0);
  168. //
  169. // save window handle, for messages
  170. //
  171. g_hWnd = m_hWnd;
  172. //
  173. // start data acquire thread
  174. //
  175. m_pDataAcquireThread = AfxBeginThread(DataAcquireThreadProc, m_pDataAcquireInfo, THREAD_PRIORITY_NORMAL);
  176. //
  177. // suspend thread until dialog is ready to acquire
  178. //
  179. m_pDataAcquireThread->SuspendThread();
  180. return TRUE; // return TRUE unless you set the focus to a control
  181. // EXCEPTION: OCX Property Pages should return FALSE
  182. }
  183. LRESULT CProgressDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  184. {
  185. //
  186. // Trap progress control message, sent from data acquire thread
  187. //
  188. switch(message) {
  189. case WM_STEP_PROGRESS:
  190. //
  191. // step progress control
  192. //
  193. m_ProgressCtrl.SetPos((int)lParam);
  194. break;
  195. case WM_ACTIVATE:
  196. //
  197. // dialog is ready, so resume thread for acquiring data
  198. //
  199. m_pDataAcquireThread->ResumeThread();
  200. break;
  201. case WM_CANCEL_ACQUIRE:
  202. //
  203. // cancel/close dialog
  204. //
  205. CDialog::OnOK();
  206. break;
  207. default:
  208. break;
  209. }
  210. //
  211. // handle any special cases
  212. //
  213. //
  214. // if the user has canceled the acquire, do not process another
  215. // progress text message, because we already have updated them
  216. // with a 'friendly' wait message.
  217. //
  218. if(!g_bCancel) {
  219. if(message == WM_UPDATE_PROGRESS_TEXT) {
  220. m_ProgressText = (LPTSTR)lParam;
  221. UpdateData(FALSE);
  222. }
  223. }
  224. return CDialog::WindowProc(message, wParam, lParam);
  225. }