Counter Strike : Global Offensive Source Code
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.

215 lines
4.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // ProgDlg.cpp : implementation file
  9. // CG: This file was added by the Progress Dialog component
  10. #include "stdafx.h"
  11. #include "resource.h"
  12. #include "ProgDlg.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include <tier0/memdbgon.h>
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CProgressDlg dialog
  17. CProgressDlg::CProgressDlg(UINT nCaptionID)
  18. {
  19. m_nCaptionID = CG_IDS_PROGRESS_CAPTION;
  20. if (nCaptionID != 0)
  21. m_nCaptionID = nCaptionID;
  22. m_bCancel=FALSE;
  23. m_nLower=0;
  24. m_nUpper=100;
  25. m_nStep=10;
  26. //{{AFX_DATA_INIT(CProgressDlg)
  27. // NOTE: the ClassWizard will add member initialization here
  28. //}}AFX_DATA_INIT
  29. m_bParentDisabled = FALSE;
  30. }
  31. CProgressDlg::~CProgressDlg()
  32. {
  33. if(m_hWnd!=NULL)
  34. DestroyWindow();
  35. }
  36. BOOL CProgressDlg::DestroyWindow()
  37. {
  38. ReEnableParent();
  39. return CDialog::DestroyWindow();
  40. }
  41. void CProgressDlg::ReEnableParent()
  42. {
  43. if(m_bParentDisabled && (m_pParentWnd!=NULL))
  44. m_pParentWnd->EnableWindow(TRUE);
  45. m_bParentDisabled=FALSE;
  46. }
  47. BOOL CProgressDlg::Create(CWnd *pParent)
  48. {
  49. // Get the true parent of the dialog
  50. m_pParentWnd = CWnd::GetSafeOwner(pParent);
  51. // m_bParentDisabled is used to re-enable the parent window
  52. // when the dialog is destroyed. So we don't want to set
  53. // it to TRUE unless the parent was already enabled.
  54. if((m_pParentWnd!=NULL) && m_pParentWnd->IsWindowEnabled())
  55. {
  56. m_pParentWnd->EnableWindow(FALSE);
  57. m_bParentDisabled = TRUE;
  58. }
  59. if(!CDialog::Create(CProgressDlg::IDD,pParent))
  60. {
  61. ReEnableParent();
  62. return FALSE;
  63. }
  64. return TRUE;
  65. }
  66. void CProgressDlg::DoDataExchange(CDataExchange* pDX)
  67. {
  68. CDialog::DoDataExchange(pDX);
  69. //{{AFX_DATA_MAP(CProgressDlg)
  70. DDX_Control(pDX, CG_IDC_PROGDLG_PROGRESS, m_Progress);
  71. //}}AFX_DATA_MAP
  72. }
  73. BEGIN_MESSAGE_MAP(CProgressDlg, CDialog)
  74. //{{AFX_MSG_MAP(CProgressDlg)
  75. //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77. void CProgressDlg::OnCancel()
  78. {
  79. m_bCancel=TRUE;
  80. }
  81. void CProgressDlg::SetRange(int nLower,int nUpper)
  82. {
  83. m_nLower = nLower;
  84. m_nUpper = nUpper;
  85. m_Progress.SetRange(nLower,nUpper);
  86. }
  87. int CProgressDlg::SetPos(int nPos)
  88. {
  89. PumpMessages();
  90. int iResult = m_Progress.SetPos(nPos);
  91. UpdatePercent(nPos);
  92. return iResult;
  93. }
  94. int CProgressDlg::SetStep(int nStep)
  95. {
  96. m_nStep = nStep; // Store for later use in calculating percentage
  97. return m_Progress.SetStep(nStep);
  98. }
  99. int CProgressDlg::OffsetPos(int nPos)
  100. {
  101. PumpMessages();
  102. int iResult = m_Progress.OffsetPos(nPos);
  103. UpdatePercent(iResult+nPos);
  104. return iResult;
  105. }
  106. int CProgressDlg::StepIt()
  107. {
  108. PumpMessages();
  109. int iResult = m_Progress.StepIt();
  110. UpdatePercent(iResult+m_nStep);
  111. return iResult;
  112. }
  113. void CProgressDlg::PumpMessages()
  114. {
  115. // Must call Create() before using the dialog
  116. Assert(m_hWnd!=NULL);
  117. MSG msg;
  118. // Handle dialog messages
  119. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  120. {
  121. if(!IsDialogMessage(&msg))
  122. {
  123. TranslateMessage(&msg);
  124. DispatchMessage(&msg);
  125. }
  126. }
  127. }
  128. BOOL CProgressDlg::CheckCancelButton()
  129. {
  130. // Process all pending messages
  131. PumpMessages();
  132. // Reset m_bCancel to FALSE so that
  133. // CheckCancelButton returns FALSE until the user
  134. // clicks Cancel again. This will allow you to call
  135. // CheckCancelButton and still continue the operation.
  136. // If m_bCancel stayed TRUE, then the next call to
  137. // CheckCancelButton would always return TRUE
  138. BOOL bResult = m_bCancel;
  139. m_bCancel = FALSE;
  140. return bResult;
  141. }
  142. void CProgressDlg::UpdatePercent(int nNewPos)
  143. {
  144. CWnd *pWndPercent = GetDlgItem(CG_IDC_PROGDLG_PERCENT);
  145. int nPercent;
  146. int nDivisor = m_nUpper - m_nLower;
  147. Assert(nDivisor>0); // m_nLower should be smaller than m_nUpper
  148. int nDividend = (nNewPos - m_nLower);
  149. Assert(nDividend>=0); // Current position should be greater than m_nLower
  150. nPercent = nDividend * 100 / nDivisor;
  151. // Since the Progress Control wraps, we will wrap the percentage
  152. // along with it. However, don't reset 100% back to 0%
  153. if(nPercent!=100)
  154. nPercent %= 100;
  155. // Display the percentage
  156. CString strBuf;
  157. strBuf.Format(_T("%d%c"),nPercent,_T('%'));
  158. CString strCur; // get current percentage
  159. pWndPercent->GetWindowText(strCur);
  160. if (strCur != strBuf)
  161. pWndPercent->SetWindowText(strBuf);
  162. }
  163. /////////////////////////////////////////////////////////////////////////////
  164. // CProgressDlg message handlers
  165. BOOL CProgressDlg::OnInitDialog()
  166. {
  167. CDialog::OnInitDialog();
  168. m_Progress.SetRange(m_nLower,m_nUpper);
  169. m_Progress.SetStep(m_nStep);
  170. m_Progress.SetPos(m_nLower);
  171. CString strCaption;
  172. VERIFY(strCaption.LoadString(m_nCaptionID));
  173. SetWindowText(strCaption);
  174. return TRUE;
  175. }