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.

403 lines
6.2 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1997 - 1998
  3. All rights reserved.
  4. Module Name:
  5. asyncdlg.cxx
  6. Abstract:
  7. Asynchronous Dialog.
  8. Author:
  9. Steve Kiraly (SteveKi) 10-Feb-1997
  10. Revision History:
  11. --*/
  12. #include "precomp.hxx"
  13. #pragma hdrstop
  14. #include "asyncdlg.hxx"
  15. /*++
  16. Routine Name:
  17. TAsyncDlg
  18. Routine Description:
  19. Asychronous dialog. This is a dialog which starts a
  20. worker thread to do its work. Note because a thread
  21. is started and this class is reference counted it must
  22. be instantiated from the heap.
  23. Arguments:
  24. Nothing.
  25. Return Value:
  26. Nothing.
  27. --*/
  28. TAsyncDlg::
  29. TAsyncDlg(
  30. IN HWND hwnd,
  31. IN TAsyncData *pData,
  32. IN UINT uResourceId
  33. ) : _pData( pData ),
  34. _hWnd( hwnd ),
  35. _uResourceId( uResourceId ),
  36. _bActive( FALSE )
  37. {
  38. DBGMSG( DBG_TRACE, ( "TAsyncDlg::ctor.\n" ) );
  39. }
  40. TAsyncDlg::
  41. ~TAsyncDlg(
  42. VOID
  43. )
  44. {
  45. DBGMSG( DBG_TRACE, ( "TAsyncDlg::dtor.\n" ) );
  46. }
  47. BOOL
  48. TAsyncDlg::
  49. bDoModal(
  50. VOID
  51. )
  52. {
  53. //
  54. // Create a modal dialog.
  55. //
  56. return (BOOL)DialogBoxParam( ghInst,
  57. MAKEINTRESOURCE( _uResourceId ),
  58. _hWnd,
  59. MGenericDialog::SetupDlgProc,
  60. (LPARAM)this );
  61. }
  62. VOID
  63. TAsyncDlg::
  64. vSetTitle(
  65. IN LPCTSTR pszTitle
  66. )
  67. {
  68. if( pszTitle && *pszTitle )
  69. {
  70. //
  71. // Update the dialog title.
  72. //
  73. TStatusB bStatus;
  74. bStatus DBGCHK = _strTitle.bUpdate( pszTitle );
  75. //
  76. // If the window is currently active then post
  77. // our special set title message.
  78. //
  79. if( _bActive )
  80. {
  81. PostMessage( _hDlg, WM_COMMAND, WM_APP, 0 );
  82. }
  83. }
  84. }
  85. BOOL
  86. TAsyncDlg::
  87. bIsActive(
  88. VOID
  89. ) const
  90. {
  91. return _bActive;
  92. }
  93. BOOL
  94. TAsyncDlg::
  95. bHandle_InitDialog(
  96. VOID
  97. )
  98. {
  99. BOOL bRetval = FALSE;
  100. //
  101. // Indicate the dialog is active.
  102. //
  103. _bActive = TRUE;
  104. //
  105. // Start the animation.
  106. //
  107. HWND hwndAni = GetDlgItem( _hDlg, IDD_STATUS );
  108. Animate_Open(hwndAni, MAKEINTRESOURCE( IDA_SEARCH ) );
  109. Animate_Play(hwndAni, 0, -1, -1);
  110. //
  111. // Set the window title.
  112. //
  113. SetWindowText( _hDlg, _strTitle );
  114. //
  115. //
  116. // Start the asynchronous thread.
  117. //
  118. bRetval = bStartAsyncThread();
  119. if( !bRetval )
  120. {
  121. vTerminate( IDCANCEL );
  122. }
  123. return bRetval;
  124. }
  125. BOOL
  126. TAsyncDlg::
  127. bHandle_Destroy(
  128. VOID
  129. )
  130. {
  131. DBGMSG( DBG_TRACE, ( "TAsyncDlg::bHandle_Destroy.\n" ) );
  132. Animate_Stop( GetDlgItem( _hDlg, IDD_STATUS ) );
  133. _bActive = FALSE;
  134. return TRUE;
  135. }
  136. BOOL
  137. TAsyncDlg::
  138. bHandleMessage(
  139. IN UINT uMsg,
  140. IN WPARAM wParam,
  141. IN LPARAM lParam
  142. )
  143. {
  144. BOOL bStatus = TRUE;
  145. switch( uMsg )
  146. {
  147. case WM_INITDIALOG:
  148. bStatus = bHandle_InitDialog();
  149. break;
  150. case WM_COMMAND:
  151. bStatus = bHandle_Command( GET_WM_COMMAND_ID( wParam, lParam ), wParam, lParam );
  152. break;
  153. case WM_DESTROY:
  154. bStatus = bHandle_Destroy();
  155. break;
  156. default:
  157. bStatus = FALSE;
  158. break;
  159. }
  160. bStatus &= _pData->bHandleMessage( this, uMsg, wParam, lParam );
  161. return bStatus;
  162. }
  163. /*++
  164. Routine Name:
  165. bHandle_Command
  166. Routine Description:
  167. Handles WM_COMMAND messages from the dialog
  168. box procedure.
  169. Arguments:
  170. uMsg - Command message( control ID )
  171. wParam - dialog procedure passed wParam
  172. lParam - dialog procedure passed lParam
  173. Return Value:
  174. TRUE message handled, otherwise false.
  175. --*/
  176. BOOL
  177. TAsyncDlg::
  178. bHandle_Command(
  179. IN UINT uMsg,
  180. IN WPARAM wParam,
  181. IN LPARAM lParam
  182. )
  183. {
  184. BOOL bStatus = TRUE;
  185. switch( uMsg )
  186. {
  187. case IDOK:
  188. case IDCANCEL:
  189. EndDialog( _hDlg, uMsg );
  190. break;
  191. case IDC_CANCEL:
  192. vTerminate( IDCANCEL );
  193. break;
  194. case WM_APP:
  195. SetWindowText( _hDlg, _strTitle );
  196. break;
  197. default:
  198. bStatus = FALSE;
  199. break;
  200. }
  201. return bStatus;
  202. }
  203. /*++
  204. Routine Name:
  205. bRefZeroed
  206. Routine Description:
  207. Called when the reference count drops to zero.
  208. Arguments:
  209. Nothing.
  210. Return Value:
  211. Nothing.
  212. --*/
  213. VOID
  214. TAsyncDlg::
  215. vRefZeroed(
  216. VOID
  217. )
  218. {
  219. DBGMSG( DBG_TRACE, ( "TAsyncDlg::vRefZeroed.\n" ) );
  220. //
  221. // Since we are reference counting the printer data object
  222. // we know that it is safe to delete ourselves now.
  223. //
  224. delete this;
  225. }
  226. VOID
  227. TAsyncDlg::
  228. vTerminate(
  229. IN WPARAM wParam
  230. )
  231. {
  232. PostMessage( _hDlg, WM_COMMAND, wParam, 0 );
  233. }
  234. BOOL
  235. TAsyncDlg::
  236. bStartAsyncThread(
  237. VOID
  238. )
  239. {
  240. BOOL bStatus = FALSE;
  241. HANDLE hThread;
  242. DWORD dwIgnore;
  243. //
  244. // Ensure we bump the reference count.
  245. //
  246. vIncRef();
  247. //
  248. // Create a save printui thread.
  249. //
  250. hThread = TSafeThread::Create( NULL,
  251. 0,
  252. reinterpret_cast<LPTHREAD_START_ROUTINE>( iProc ),
  253. this,
  254. 0,
  255. &dwIgnore );
  256. //
  257. // Close the handle to the new thread if it was created.
  258. //
  259. if( hThread )
  260. {
  261. CloseHandle( hThread );
  262. bStatus = TRUE;
  263. }
  264. return bStatus;
  265. }
  266. INT
  267. TAsyncDlg::
  268. iProc(
  269. IN TAsyncDlg *pDlg
  270. )
  271. {
  272. //
  273. // Execute the callback.
  274. //
  275. pDlg->_pData->bAsyncWork( pDlg );
  276. //
  277. // Terminate the async dialog.
  278. //
  279. pDlg->vTerminate( IDOK );
  280. //
  281. // Release our reference to the async dialog.
  282. //
  283. pDlg->cDecRef();
  284. return ERROR_SUCCESS;
  285. }
  286. /********************************************************************
  287. TAsynchronous data class.
  288. ********************************************************************/
  289. TAsyncData::
  290. TAsyncData(
  291. VOID
  292. )
  293. {
  294. DBGMSG( DBG_TRACE, ( "TAsyncData::ctor.\n" ) );
  295. }
  296. TAsyncData::
  297. ~TAsyncData(
  298. VOID
  299. )
  300. {
  301. DBGMSG( DBG_TRACE, ( "TAsyncData::dtor.\n" ) );
  302. }
  303. BOOL
  304. TAsyncData::
  305. bHandleMessage(
  306. IN TAsyncDlg *pDlg,
  307. IN UINT uMsg,
  308. IN WPARAM wParam,
  309. IN LPARAM lParam
  310. )
  311. {
  312. return FALSE;
  313. }