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.

783 lines
23 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. /* File: progress.cpp
  3. Description: Implements the various flavors of progress dialogs used
  4. in the quota UI.
  5. Revision History:
  6. Date Description Programmer
  7. -------- --------------------------------------------------- ----------
  8. 05/28/97 Initial creation. BrianAu
  9. */
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "pch.h"
  12. #pragma hdrstop
  13. #include "progress.h"
  14. #include "resource.h"
  15. ///////////////////////////////////////////////////////////////////////////////
  16. /* Function: ProgressDialog::SendToProgressBar [inline]
  17. Description: Inline function that sends a message to the dialog's
  18. progress bar control. If there is no progress bar control, the
  19. function returns FALSE.
  20. Arguments: Standard Win32 message arguments.
  21. Returns:
  22. If progress bar window exists, returns the result of SendMessage.
  23. Otherwise, returns FALSE.
  24. Revision History:
  25. Date Description Programmer
  26. -------- --------------------------------------------------- ----------
  27. 05/28/97 Initial creation. BrianAu
  28. */
  29. ///////////////////////////////////////////////////////////////////////////////
  30. inline INT_PTR
  31. ProgressDialog::SendToProgressBar(
  32. UINT uMsg,
  33. WPARAM wParam,
  34. LPARAM lParam
  35. )
  36. {
  37. if (NULL != m_hwndProgressBar)
  38. return SendMessage(m_hwndProgressBar, uMsg, wParam, lParam);
  39. else
  40. return FALSE;
  41. }
  42. ///////////////////////////////////////////////////////////////////////////////
  43. /* Function: ProgressDialog::ProgressDialog
  44. Description: Class constructor for progress dialog base class.
  45. Arguments:
  46. idDialogTemplate - ID number for the dialog's resource template.
  47. idProgressBar - ID number for the progress bar control.
  48. idTxtDescription - ID number for text description in dialog.
  49. idTxtFileName - ID number for file name field in dialog.
  50. Returns: Nothing.
  51. Revision History:
  52. Date Description Programmer
  53. -------- --------------------------------------------------- ----------
  54. 05/28/97 Initial creation. BrianAu
  55. */
  56. ///////////////////////////////////////////////////////////////////////////////
  57. ProgressDialog::ProgressDialog(
  58. UINT idDialogTemplate,
  59. UINT idProgressBar,
  60. UINT idTxtDescription,
  61. UINT idTxtFileName
  62. ) : m_idDialogTemplate(idDialogTemplate),
  63. m_idProgressBar(idProgressBar),
  64. m_idTxtDescription(idTxtDescription),
  65. m_idTxtFileName(idTxtFileName),
  66. m_hWnd(NULL),
  67. m_bUserCancelled(FALSE)
  68. {
  69. DBGTRACE((DM_VIEW, DL_HIGH, TEXT("ProgressDialog::ProgressDialog")));
  70. DBGPRINT((DM_VIEW, DL_HIGH, TEXT("\tthis = 0x%08X"), this));
  71. //
  72. // Do nothing.
  73. //
  74. }
  75. ///////////////////////////////////////////////////////////////////////////////
  76. /* Function: ProgressDialog::~ProgressDialog
  77. Description: Class destructor for progress dialog base class.
  78. Arguments: None.
  79. Returns: Nothing.
  80. Revision History:
  81. Date Description Programmer
  82. -------- --------------------------------------------------- ----------
  83. 05/28/97 Initial creation. BrianAu
  84. */
  85. ///////////////////////////////////////////////////////////////////////////////
  86. ProgressDialog::~ProgressDialog(
  87. VOID
  88. )
  89. {
  90. DBGTRACE((DM_VIEW, DL_HIGH, TEXT("ProgressDialog::~ProgressDialog")));
  91. DBGPRINT((DM_VIEW, DL_HIGH, TEXT("\tthis = 0x%08X"), this));
  92. //
  93. // Call the Destroy() function to destroy the progress dialog window.
  94. //
  95. Destroy();
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////
  98. /* Function: ProgressDialog::Create
  99. Description: Creates the dialog.
  100. Arguments:
  101. hInstance - Instance handle for the DLL containing the dialog
  102. resource template.
  103. hwndParent - Parent window for dialog.
  104. Returns:
  105. TRUE = Dialog was created.
  106. FALSE = Dialog was not created.
  107. Revision History:
  108. Date Description Programmer
  109. -------- --------------------------------------------------- ----------
  110. 05/28/97 Initial creation. BrianAu
  111. */
  112. ///////////////////////////////////////////////////////////////////////////////
  113. BOOL
  114. ProgressDialog::Create(
  115. HINSTANCE hInstance,
  116. HWND hwndParent
  117. )
  118. {
  119. m_hWnd = CreateDialogParam(hInstance,
  120. MAKEINTRESOURCE(m_idDialogTemplate),
  121. hwndParent,
  122. DlgProc,
  123. (LPARAM)this);
  124. if (NULL != m_hWnd)
  125. {
  126. m_hwndProgressBar = GetDlgItem(m_hWnd, m_idProgressBar);
  127. DBGASSERT((NULL != m_hwndProgressBar));
  128. }
  129. return (NULL != m_hWnd);
  130. }
  131. ///////////////////////////////////////////////////////////////////////////////
  132. /* Function: ProgressDialog::Destroy
  133. Description: Destroys the dialog window.
  134. Arguments: None.
  135. Returns: Nothing.
  136. Revision History:
  137. Date Description Programmer
  138. -------- --------------------------------------------------- ----------
  139. 05/28/97 Initial creation. BrianAu
  140. */
  141. ///////////////////////////////////////////////////////////////////////////////
  142. VOID
  143. ProgressDialog::Destroy(
  144. VOID
  145. )
  146. {
  147. //
  148. // Note that m_hWnd is set to NULL in OnDestroy().
  149. //
  150. if (NULL != m_hWnd)
  151. DestroyWindow(m_hWnd);
  152. }
  153. ///////////////////////////////////////////////////////////////////////////////
  154. /* Function: ProgressDialog::DlgProc [static]
  155. Description: Message procedure for the dialog.
  156. Arguments: Standard Win32 message proc arguments.
  157. Returns: Standard Win32 message proc return values.
  158. Revision History:
  159. Date Description Programmer
  160. -------- --------------------------------------------------- ----------
  161. 05/28/97 Initial creation. BrianAu
  162. */
  163. ///////////////////////////////////////////////////////////////////////////////
  164. INT_PTR CALLBACK
  165. ProgressDialog::DlgProc(
  166. HWND hwnd,
  167. UINT uMsg,
  168. WPARAM wParam,
  169. LPARAM lParam
  170. )
  171. {
  172. //
  173. // Retrieve the dialog object's ptr from the window's userdata.
  174. // Place there in response to WM_INITDIALOG.
  175. //
  176. ProgressDialog *pThis = (ProgressDialog *)GetWindowLongPtr(hwnd, DWLP_USER);
  177. switch(uMsg)
  178. {
  179. case WM_INITDIALOG:
  180. //
  181. // Store "this" ptr in window's userdata.
  182. //
  183. SetWindowLongPtr(hwnd, DWLP_USER, (INT_PTR)lParam);
  184. pThis = (ProgressDialog *)lParam;
  185. //
  186. // Description text control is hidden by default.
  187. // Calling SetDescription() will show it.
  188. //
  189. ShowWindow(GetDlgItem(hwnd, pThis->m_idTxtDescription), SW_HIDE);
  190. ShowWindow(GetDlgItem(hwnd, pThis->m_idTxtFileName), SW_HIDE);
  191. //
  192. // Center dialog in it's parent.
  193. //
  194. ::CenterPopupWindow(hwnd);
  195. //
  196. // Let derived classes respond to WM_INITDIALOG.
  197. //
  198. return pThis->OnInitDialog(hwnd, wParam, lParam);
  199. case WM_DESTROY:
  200. //
  201. // Let derived classes respond to WM_DESTROY.
  202. //
  203. return pThis->OnDestroy(hwnd);
  204. case WM_COMMAND:
  205. if (IDCANCEL == LOWORD(wParam))
  206. {
  207. //
  208. // User pressed "Cancel" button.
  209. // Set the "User cancelled" flag and let derived
  210. // classes respond to the cancellation.
  211. //
  212. pThis->m_bUserCancelled = TRUE;
  213. return pThis->OnCancel(hwnd, wParam, lParam);
  214. }
  215. break;
  216. }
  217. if (NULL != pThis)
  218. {
  219. //
  220. // Let derived classes respond to any message if they wish.
  221. // Note that only WM_INITDIALOG, WM_DESTROY and the "user cancelled"
  222. // events are the only special cases.
  223. //
  224. return pThis->HandleMessage(hwnd, uMsg, wParam, lParam);
  225. }
  226. return FALSE;
  227. }
  228. ///////////////////////////////////////////////////////////////////////////////
  229. /* Function: ProgressDialog::HandleMessage
  230. Description: Base class implementation of virtual function. Derived
  231. classes can provide an implementation to handle any message other than
  232. WM_INITDIALOG or WM_DESTROY. These two messages have their own
  233. virtual message handlers.
  234. Arguments: Standard Win32 message proc arguments.
  235. Returns: Always returns FALSE.
  236. Revision History:
  237. Date Description Programmer
  238. -------- --------------------------------------------------- ----------
  239. 05/28/97 Initial creation. BrianAu
  240. */
  241. ///////////////////////////////////////////////////////////////////////////////
  242. INT_PTR
  243. ProgressDialog::HandleMessage(
  244. HWND hwnd,
  245. UINT uMsg,
  246. WPARAM wParam,
  247. LPARAM lParam
  248. )
  249. {
  250. return FALSE;
  251. }
  252. ///////////////////////////////////////////////////////////////////////////////
  253. /* Function: ProgressDialog::OnInitDialog
  254. Description: Base class implementation of virtual function. Called
  255. when the base class receives WM_INITDIALOG. Derived classes can
  256. provide an implementation if they wish to perform some operation
  257. in response to WM_INITDIALOG.
  258. Arguments:
  259. hwnd - Dialog window handle.
  260. wParam, lParam - Standard Win32 message proc arguments.
  261. Returns: Always returns TRUE so that USER will set the control focus.
  262. Revision History:
  263. Date Description Programmer
  264. -------- --------------------------------------------------- ----------
  265. 05/28/97 Initial creation. BrianAu
  266. */
  267. ///////////////////////////////////////////////////////////////////////////////
  268. INT_PTR
  269. ProgressDialog::OnInitDialog(
  270. HWND hwnd,
  271. WPARAM wParam,
  272. LPARAM lParam
  273. )
  274. {
  275. return TRUE;
  276. }
  277. ///////////////////////////////////////////////////////////////////////////////
  278. /* Function: ProgressDialog::OnDestroy
  279. Description: Base class implementation of virtual function. Called
  280. when the base class receives WM_DESTROY. Derived classes can
  281. provide an implementation if they wish to perform some operation
  282. in response to WM_DESTROY. Before returning, any derived implementation
  283. must call the base class implementation so that m_hWnd is properly
  284. set to NULL.
  285. Arguments:
  286. hwnd - Dialog window handle.
  287. Returns: Always returns FALSE.
  288. Revision History:
  289. Date Description Programmer
  290. -------- --------------------------------------------------- ----------
  291. 05/28/97 Initial creation. BrianAu
  292. */
  293. ///////////////////////////////////////////////////////////////////////////////
  294. INT_PTR
  295. ProgressDialog::OnDestroy(
  296. HWND hwnd
  297. )
  298. {
  299. m_hWnd = NULL;
  300. return FALSE;
  301. }
  302. ///////////////////////////////////////////////////////////////////////////////
  303. /* Function: ProgressDialog::OnCancel
  304. Description: Base class implementation of virtual function. Called
  305. when the user presses the "Cancel" button in the dialog. This
  306. implementation assumes that the Cancel button is assigned the ID
  307. of IDCANCEL (standard).
  308. Arguments:
  309. hwnd - Dialog window handle.
  310. wParam, lParam - Standard Win32 message proc arguments.
  311. Returns: Always returns FALSE.
  312. Revision History:
  313. Date Description Programmer
  314. -------- --------------------------------------------------- ----------
  315. 05/28/97 Initial creation. BrianAu
  316. */
  317. ///////////////////////////////////////////////////////////////////////////////
  318. INT_PTR
  319. ProgressDialog::OnCancel(
  320. HWND hwnd,
  321. WPARAM wParam,
  322. LPARAM lParam
  323. )
  324. {
  325. return FALSE;
  326. }
  327. ///////////////////////////////////////////////////////////////////////////////
  328. /* Function: ProgressDialog::FlushMessages
  329. Description: While the dialog is active, call this periodically so that
  330. the thread is able to properly update the dialog and respond to the
  331. user pressing "Cancel".
  332. Arguments: None.
  333. Returns: Nothing.
  334. Revision History:
  335. Date Description Programmer
  336. -------- --------------------------------------------------- ----------
  337. 05/28/97 Initial creation. BrianAu
  338. */
  339. ///////////////////////////////////////////////////////////////////////////////
  340. VOID
  341. ProgressDialog::FlushMessages(
  342. VOID
  343. )
  344. {
  345. if (NULL != m_hWnd)
  346. {
  347. //
  348. // Process messages for the dialog's parent and all of it's children.
  349. //
  350. MSG msg;
  351. while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) &&
  352. WM_QUIT != msg.message)
  353. {
  354. GetMessage(&msg, NULL, 0, 0);
  355. if (!IsDialogMessage(m_hWnd, &msg))
  356. {
  357. TranslateMessage(&msg);
  358. DispatchMessage(&msg);
  359. }
  360. }
  361. }
  362. }
  363. ///////////////////////////////////////////////////////////////////////////////
  364. /* Function: ProgressDialog::SetTitle
  365. Description: Sets the title string in the dialog.
  366. Arguments:
  367. pszTitle - Address of title string.
  368. Returns: Nothing.
  369. Revision History:
  370. Date Description Programmer
  371. -------- --------------------------------------------------- ----------
  372. 05/28/97 Initial creation. BrianAu
  373. */
  374. ///////////////////////////////////////////////////////////////////////////////
  375. VOID
  376. ProgressDialog::SetTitle(
  377. LPCTSTR pszTitle
  378. )
  379. {
  380. DBGASSERT((NULL != pszTitle));
  381. if (NULL != m_hWnd)
  382. {
  383. if (0 == ((DWORD_PTR)pszTitle & ~0xffff))
  384. {
  385. TCHAR szText[MAX_PATH] = { TEXT('\0') };
  386. LoadString(g_hInstDll, (DWORD)((DWORD_PTR)pszTitle), szText, ARRAYSIZE(szText));
  387. pszTitle = szText;
  388. }
  389. SetWindowText(m_hWnd, pszTitle);
  390. FlushMessages();
  391. }
  392. }
  393. ///////////////////////////////////////////////////////////////////////////////
  394. /* Function: ProgressDialog::SetDescription
  395. Description: Sets the progress description string in the dialog.
  396. Arguments:
  397. pszDescription - Address of description string.
  398. Returns: Nothing.
  399. Revision History:
  400. Date Description Programmer
  401. -------- --------------------------------------------------- ----------
  402. 05/28/97 Initial creation. BrianAu
  403. */
  404. ///////////////////////////////////////////////////////////////////////////////
  405. VOID
  406. ProgressDialog::SetDescription(
  407. LPCTSTR pszDescription
  408. )
  409. {
  410. DBGASSERT((NULL != pszDescription));
  411. if (NULL != m_hWnd)
  412. {
  413. if (0 == ((DWORD_PTR)pszDescription & ~0xffff))
  414. {
  415. TCHAR szText[MAX_PATH] = { TEXT('\0') };
  416. LoadString(g_hInstDll, (DWORD)((DWORD_PTR)pszDescription), szText, ARRAYSIZE(szText));
  417. pszDescription = szText;
  418. }
  419. SetWindowText(GetDlgItem(m_hWnd, m_idTxtDescription), pszDescription);
  420. //
  421. // Description control is by default hidden.
  422. //
  423. ShowWindow(GetDlgItem(m_hWnd, m_idTxtDescription), SW_NORMAL);
  424. FlushMessages();
  425. }
  426. }
  427. ///////////////////////////////////////////////////////////////////////////////
  428. /* Function: ProgressDialog::SetFileName
  429. Description: Sets the file name description string in the dialog.
  430. If the file name is too long, it is formatted with ellipses to
  431. fit in the space provided.
  432. Arguments:
  433. pszFileName - Address of file name string.
  434. Returns: Nothing.
  435. Revision History:
  436. Date Description Programmer
  437. -------- --------------------------------------------------- ----------
  438. 05/28/97 Initial creation. BrianAu
  439. */
  440. ///////////////////////////////////////////////////////////////////////////////
  441. VOID
  442. ProgressDialog::SetFileName(
  443. LPCTSTR pszFileName
  444. )
  445. {
  446. DBGASSERT((NULL != pszFileName));
  447. if (NULL != m_hWnd)
  448. {
  449. HWND hwndCtl = GetDlgItem(m_hWnd, m_idTxtFileName);
  450. HDC hdc = GetDC(hwndCtl);
  451. RECT rc;
  452. LPTSTR pszText = StringDup(pszFileName);
  453. if (NULL != pszText)
  454. {
  455. GetClientRect(hwndCtl, &rc);
  456. DrawText(hdc,
  457. pszText,
  458. -1,
  459. &rc,
  460. DT_CENTER |
  461. DT_PATH_ELLIPSIS |
  462. DT_MODIFYSTRING);
  463. SetWindowText(hwndCtl, pszText);
  464. delete[] pszText;
  465. //
  466. // FileName control is by default hidden.
  467. //
  468. ShowWindow(hwndCtl, SW_NORMAL);
  469. }
  470. FlushMessages();
  471. ReleaseDC(hwndCtl, hdc);
  472. }
  473. }
  474. ///////////////////////////////////////////////////////////////////////////////
  475. /* Function: ProgressDialog::ProgressBarInit
  476. Description: Initializes the progress bar control with range and step
  477. values. If this function is not called, the progress bar defaults
  478. to:
  479. iMin = 0
  480. iMax = 100
  481. iStep = 10
  482. Arguments:
  483. iMin - Minimum range value.
  484. iMax - Maximum range value.
  485. iStep - Amount bar advances each time PBM_STEPIT is received.
  486. Returns:
  487. TRUE = Progress bar control accepted settings.
  488. FALSE = Progress bar rejected settings.
  489. Revision History:
  490. Date Description Programmer
  491. -------- --------------------------------------------------- ----------
  492. 05/28/97 Initial creation. BrianAu
  493. */
  494. ///////////////////////////////////////////////////////////////////////////////
  495. BOOL
  496. ProgressDialog::ProgressBarInit(
  497. UINT iMin,
  498. UINT iMax,
  499. UINT iStep
  500. )
  501. {
  502. BOOL bResult = FALSE;
  503. if (0 != SendToProgressBar(PBM_SETSTEP, iStep, 0))
  504. bResult = (0 != SendToProgressBar(PBM_SETRANGE, 0, MAKELPARAM(iMin, iMax)));
  505. FlushMessages();
  506. return bResult;
  507. }
  508. ///////////////////////////////////////////////////////////////////////////////
  509. /* Function: ProgressDialog::ProgressBarReset
  510. Description: Resets the progres bar position at 0.
  511. Arguments: None.
  512. Returns: Previous "position" of progress bar.
  513. Revision History:
  514. Date Description Programmer
  515. -------- --------------------------------------------------- ----------
  516. 05/28/97 Initial creation. BrianAu
  517. */
  518. ///////////////////////////////////////////////////////////////////////////////
  519. UINT
  520. ProgressDialog::ProgressBarReset(
  521. VOID
  522. )
  523. {
  524. UINT iReturn = (UINT)ProgressBarSetPosition(0);
  525. FlushMessages();
  526. return iReturn;
  527. }
  528. ///////////////////////////////////////////////////////////////////////////////
  529. /* Function: ProgressDialog::ProgressBarAdvance
  530. Description: Advances the progress bar a given number of counts.
  531. Arguments:
  532. iDelta - Number of counts to advance. If -1, the bar is advanced
  533. by the step value supplied in ProgressBarInit.
  534. Returns: Previous "position" of progress bar.
  535. Revision History:
  536. Date Description Programmer
  537. -------- --------------------------------------------------- ----------
  538. 05/28/97 Initial creation. BrianAu
  539. */
  540. ///////////////////////////////////////////////////////////////////////////////
  541. UINT
  542. ProgressDialog::ProgressBarAdvance(
  543. UINT iDelta
  544. )
  545. {
  546. UINT iReturn;
  547. if ((UINT)-1 == iDelta)
  548. iReturn = (UINT)SendToProgressBar(PBM_STEPIT, 0, 0);
  549. else
  550. iReturn = (UINT)SendToProgressBar(PBM_DELTAPOS, (WPARAM)iDelta, 0);
  551. FlushMessages();
  552. return iReturn;
  553. }
  554. ///////////////////////////////////////////////////////////////////////////////
  555. /* Function: ProgressDialog::ProgressBarSetPosition
  556. Description: Advances the progress bar to a specific position.
  557. Arguments:
  558. iPosition - Specific position count.
  559. Returns: Previous "position" of progress bar.
  560. Revision History:
  561. Date Description Programmer
  562. -------- --------------------------------------------------- ----------
  563. 05/28/97 Initial creation. BrianAu
  564. */
  565. ///////////////////////////////////////////////////////////////////////////////
  566. UINT
  567. ProgressDialog::ProgressBarSetPosition(
  568. UINT iPosition
  569. )
  570. {
  571. UINT iReturn = (UINT)SendToProgressBar(PBM_SETPOS, (WPARAM)iPosition, 0);
  572. FlushMessages();
  573. return iReturn;
  574. }
  575. ///////////////////////////////////////////////////////////////////////////////
  576. /* Function: ProgressDialog::Show
  577. Description: Makes the progress dialog visible.
  578. Arguments: None.
  579. Returns: Nothing.
  580. Revision History:
  581. Date Description Programmer
  582. -------- --------------------------------------------------- ----------
  583. 05/28/97 Initial creation. BrianAu
  584. */
  585. ///////////////////////////////////////////////////////////////////////////////
  586. VOID
  587. ProgressDialog::Show(
  588. VOID
  589. )
  590. {
  591. if (NULL != m_hWnd)
  592. {
  593. ShowWindow(m_hWnd, SW_SHOWNORMAL);
  594. FlushMessages();
  595. }
  596. }
  597. ///////////////////////////////////////////////////////////////////////////////
  598. /* Function: ProgressDialog::Hide
  599. Description: Hides the progress dialog.
  600. Arguments: None.
  601. Returns: Nothing.
  602. Revision History:
  603. Date Description Programmer
  604. -------- --------------------------------------------------- ----------
  605. 05/28/97 Initial creation. BrianAu
  606. */
  607. ///////////////////////////////////////////////////////////////////////////////
  608. VOID
  609. ProgressDialog::Hide(
  610. VOID
  611. )
  612. {
  613. if (NULL != m_hWnd)
  614. {
  615. ShowWindow(m_hWnd, SW_HIDE);
  616. FlushMessages();
  617. }
  618. }