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.

332 lines
6.1 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1999 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: progress.cpp
  6. //
  7. // Purpose: Progress dialog and connection speed
  8. //
  9. // History: 12-dec-98 YAsmi Created
  10. //
  11. //=======================================================================
  12. #include "stdafx.h"
  13. #include "progress.h"
  14. #include "locstr.h"
  15. //
  16. // CWUProgress class
  17. //
  18. CWUProgress::CWUProgress(HINSTANCE hInst)
  19. :m_hInst(hInst),
  20. m_hDlg(NULL),
  21. m_dwDownloadTotal(0),
  22. m_dwDownloadVal(0),
  23. m_dwInstallVal(0),
  24. m_dwInstallTotal(0),
  25. m_style(ProgStyle::NORMAL)
  26. {
  27. m_hCancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  28. }
  29. CWUProgress::~CWUProgress()
  30. {
  31. Destroy();
  32. if (m_hCancelEvent != INVALID_HANDLE_VALUE)
  33. {
  34. CloseHandle(m_hCancelEvent);
  35. m_hCancelEvent = INVALID_HANDLE_VALUE;
  36. }
  37. }
  38. void CWUProgress::SetStyle(ProgStyle style)
  39. {
  40. m_style = style;
  41. if (m_style == ProgStyle::OFF)
  42. {
  43. Destroy();
  44. }
  45. }
  46. HANDLE CWUProgress::GetCancelEvent()
  47. {
  48. return m_hCancelEvent;
  49. }
  50. void CWUProgress::Destroy()
  51. {
  52. if (m_hDlg != NULL)
  53. {
  54. ::DestroyWindow(m_hDlg);
  55. m_hDlg = NULL;
  56. }
  57. m_style = ProgStyle::OFF;
  58. }
  59. void CWUProgress::StartDisplay()
  60. {
  61. if (m_style == ProgStyle::OFF)
  62. return;
  63. if (m_hDlg == NULL)
  64. {
  65. // create the dialog window
  66. m_hDlg = CreateDialogParam(m_hInst, MAKEINTRESOURCE(IDD_PROGRESS), 0,
  67. CWUProgress::DlgProc, (LPARAM)this);
  68. }
  69. ShowWindow(m_hDlg, SW_SHOWNORMAL);
  70. if (m_style == ProgStyle::DOWNLOADONLY)
  71. {
  72. ShowWindow(GetDlgItem(m_hDlg, IDC_PROG_INSTALL), SW_HIDE);
  73. ShowWindow(GetDlgItem(m_hDlg, IDC_INSTALLCAP), SW_HIDE);
  74. }
  75. //
  76. // update localized strings
  77. //
  78. UpdateLocStr(IDC_DOWNLOADCAP, IDS_PROG_DOWNLOADCAP);
  79. UpdateLocStr(IDC_TIMELEFTCAP, IDS_PROG_TIMELEFTCAP);
  80. UpdateLocStr(IDC_INSTALLCAP, IDS_PROG_INSTALLCAP);
  81. UpdateLocStr(IDCANCEL, IDS_PROG_CANCEL);
  82. SetWindowText(m_hDlg, GetLocStr(IDS_APP_TITLE));
  83. ResetAll();
  84. }
  85. void CWUProgress::ResetAll()
  86. {
  87. SetInstall(0);
  88. SetDownload(0);
  89. ResetEvent(m_hCancelEvent);
  90. }
  91. void CWUProgress::UpdateLocStr(int iDlg, int iStr)
  92. {
  93. LPCTSTR pszStr = GetLocStr(iStr);
  94. if (NULL != pszStr && pszStr[0] != _T('\0'))
  95. {
  96. //update the control with loc string only if its not an empty string
  97. if (m_style != ProgStyle::OFF)
  98. {
  99. SetDlgItemText(m_hDlg, iDlg, pszStr);
  100. }
  101. }
  102. }
  103. void CWUProgress::SetDownloadTotal(DWORD dwTotal)
  104. {
  105. m_dwDownloadTotal = dwTotal;
  106. m_dwDownloadLast = 0;
  107. m_dwDownloadVal = 0;
  108. if (m_style != ProgStyle::OFF)
  109. {
  110. UpdateBytes(0);
  111. UpdateTime(m_dwDownloadTotal);
  112. }
  113. }
  114. void CWUProgress::SetInstallTotal(DWORD dwTotal)
  115. {
  116. m_dwInstallTotal = dwTotal;
  117. m_dwInstallLast = 0;
  118. m_dwInstallVal = 0;
  119. }
  120. void CWUProgress::SetDownload(DWORD dwDone)
  121. {
  122. if (m_dwDownloadTotal == 0 || m_style == ProgStyle::OFF)
  123. return;
  124. if (dwDone > m_dwDownloadTotal)
  125. dwDone = m_dwDownloadTotal;
  126. m_dwDownloadVal = dwDone;
  127. // update bytes display
  128. UpdateBytes(dwDone);
  129. // update progress bar and time
  130. DWORD dwProgress = (int)((double)dwDone / m_dwDownloadTotal * 100);
  131. if (dwProgress != m_dwDownloadLast || dwDone == m_dwDownloadTotal)
  132. {
  133. m_dwDownloadLast = dwProgress;
  134. SendMessage(GetDlgItem(m_hDlg, IDC_PROG_DOWNLOAD), PBM_SETPOS, dwProgress, 0);
  135. if (dwDone == m_dwDownloadTotal)
  136. {
  137. ShowWindow(GetDlgItem(m_hDlg, IDC_TIMELEFTCAP), SW_HIDE);
  138. ShowWindow(GetDlgItem(m_hDlg, IDC_TIMELEFT), SW_HIDE);
  139. }
  140. else
  141. {
  142. UpdateTime(m_dwDownloadTotal - dwDone);
  143. }
  144. }
  145. }
  146. void CWUProgress::SetDownloadAdd(DWORD dwAddSize, DWORD dwTime)
  147. {
  148. //
  149. // add size and time to the connection speed tracker
  150. //
  151. CConnSpeed::Learn(dwAddSize, dwTime);
  152. SetDownload(m_dwDownloadVal + dwAddSize);
  153. }
  154. void CWUProgress::SetInstallAdd(DWORD dwAdd)
  155. {
  156. SetInstall(m_dwInstallVal + dwAdd);
  157. }
  158. void CWUProgress::SetInstall(DWORD dwDone)
  159. {
  160. if (m_hDlg == NULL || m_dwInstallTotal == 0 || m_style == ProgStyle::OFF)
  161. return;
  162. if (dwDone > m_dwInstallTotal)
  163. dwDone = m_dwInstallTotal;
  164. m_dwInstallVal = dwDone;
  165. DWORD dwProgress = (int)((double)dwDone / m_dwInstallTotal * 100);
  166. if (dwProgress != m_dwInstallLast || dwDone == m_dwInstallTotal)
  167. {
  168. m_dwInstallLast = dwProgress;
  169. SendMessage(GetDlgItem(m_hDlg, IDC_PROG_INSTALL), PBM_SETPOS, dwProgress, 0);
  170. }
  171. }
  172. void CWUProgress::SetStatusText(LPCTSTR pszStatus)
  173. {
  174. if (m_hDlg == NULL || m_style == ProgStyle::OFF)
  175. return;
  176. SetDlgItemText(m_hDlg, IDC_STATUS, pszStatus);
  177. }
  178. void CWUProgress::EndDisplay()
  179. {
  180. if (m_hDlg == NULL || m_style == ProgStyle::OFF)
  181. return;
  182. ShowWindow(m_hDlg, SW_HIDE);
  183. }
  184. void CWUProgress::UpdateBytes(DWORD dwDone)
  185. {
  186. TCHAR szBuf[128];
  187. wsprintf(szBuf, _T("%d KB/%d KB"), dwDone / 1024, m_dwDownloadTotal / 1024);
  188. SetDlgItemText(m_hDlg, IDC_BYTES, szBuf);
  189. }
  190. void CWUProgress::UpdateTime(DWORD dwBytesLeft)
  191. {
  192. DWORD dwBPS = CConnSpeed::BytesPerSecond();
  193. TCHAR szBuf[128];
  194. if (dwBPS != 0)
  195. {
  196. DWORD dwSecs;
  197. DWORD dwMinutes;
  198. DWORD dwHours;
  199. DWORD dwSecsLeft = 0;
  200. dwSecsLeft = (dwBytesLeft / dwBPS) + 1;
  201. // convert secs to hours, minutes, and secs
  202. dwSecs = dwSecsLeft % 60;
  203. dwMinutes = (dwSecsLeft % 3600) / 60;
  204. dwHours = dwSecsLeft / 3600;
  205. if (dwHours == 0)
  206. {
  207. if (dwMinutes == 0)
  208. wsprintf(szBuf, GetLocStr(IDS_PROG_TIME_SEC), dwSecs);
  209. else
  210. wsprintf(szBuf, GetLocStr(IDS_PROG_TIME_MIN), dwMinutes);
  211. }
  212. else
  213. {
  214. wsprintf(szBuf, GetLocStr(IDS_PROG_TIME_HRMIN), dwHours, dwMinutes);
  215. }
  216. }
  217. else
  218. {
  219. szBuf[0] = _T('\0');
  220. } //bps is zero
  221. SetDlgItemText(m_hDlg, IDC_TIMELEFT, szBuf);
  222. }
  223. INT_PTR CALLBACK CWUProgress::DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  224. {
  225. switch (uMsg)
  226. {
  227. case WM_INITDIALOG:
  228. SetWindowLongPtr(hwnd, DWLP_USER, lParam);
  229. Animate_Open(GetDlgItem(hwnd, IDC_ANIM), MAKEINTRESOURCE(IDA_FILECOPY));
  230. Animate_Play(GetDlgItem(hwnd, IDC_ANIM), 0, -1, -1);
  231. return FALSE;
  232. case WM_COMMAND:
  233. {
  234. switch (wParam)
  235. {
  236. case IDCANCEL:
  237. {
  238. CWUProgress* pProgress = (CWUProgress*)GetWindowLongPtr(hwnd, DWLP_USER);
  239. pProgress->Destroy();
  240. SetEvent(pProgress->GetCancelEvent());
  241. }
  242. break;
  243. default:
  244. return FALSE;
  245. }
  246. }
  247. break;
  248. default:
  249. return(FALSE);
  250. }
  251. return TRUE;
  252. }