Team Fortress 2 Source Code as on 22/4/2020
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.

359 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // PROGRESS.CPP
  4. //
  5. // Progress Metering Utility.
  6. //=====================================================================================//
  7. #include "vxconsole.h"
  8. #define PROGRESS_WIDTH 425
  9. #define PROGRESS_HEIGHT 170
  10. #define ID_PROGRESS_STATUS1 100
  11. #define ID_PROGRESS_STATUS2 101
  12. #define ID_PROGRESS_STATUS3 102
  13. #define ID_PROGRESS_PERCENT 103
  14. #define ID_PROGRESS_METER 104
  15. #define ID_PROGRESS_CANCEL 105
  16. //-----------------------------------------------------------------------------
  17. // CProgress_WndProc
  18. //
  19. //-----------------------------------------------------------------------------
  20. LRESULT CALLBACK Progress_WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
  21. {
  22. CProgress *pProgress;
  23. CREATESTRUCT *createStructPtr;
  24. switch ( message )
  25. {
  26. case WM_CREATE:
  27. createStructPtr = ( CREATESTRUCT* )lParam;
  28. SetWindowLong( hwnd, GWL_USERDATA+0, ( LONG )createStructPtr->lpCreateParams );
  29. return 0L;
  30. case WM_DESTROY:
  31. pProgress = ( CProgress* )GetWindowLong( hwnd, GWL_USERDATA+0 );
  32. if ( pProgress )
  33. pProgress->m_hWnd = NULL;
  34. return 0L;
  35. case WM_CTLCOLORSTATIC:
  36. SetBkColor( ( HDC )wParam, g_backgroundColor );
  37. return ( BOOL )g_hBackgroundBrush;
  38. case WM_COMMAND:
  39. switch ( LOWORD( wParam ) )
  40. {
  41. case ID_PROGRESS_CANCEL:
  42. pProgress = ( CProgress* )GetWindowLong( hwnd, GWL_USERDATA+0 );
  43. if ( pProgress )
  44. pProgress->m_bCancelPressed = true;
  45. return ( TRUE );
  46. }
  47. break;
  48. }
  49. return ( DefWindowProc( hwnd, message, wParam, lParam ) );
  50. }
  51. //-----------------------------------------------------------------------------
  52. // CProgress::Update
  53. //
  54. // Pump the message loop
  55. //-----------------------------------------------------------------------------
  56. void CProgress::Update()
  57. {
  58. MSG msg;
  59. while ( PeekMessage( &msg, NULL, 0, 0, PM_NOYIELD|PM_REMOVE ) )
  60. {
  61. if ( !TranslateAccelerator( g_hDlgMain, g_hAccel, &msg ) )
  62. {
  63. TranslateMessage( &msg );
  64. DispatchMessage( &msg );
  65. }
  66. }
  67. }
  68. //-----------------------------------------------------------------------------
  69. // CProgress::IsCancel
  70. //
  71. //-----------------------------------------------------------------------------
  72. bool CProgress::IsCancel()
  73. {
  74. return m_bCancelPressed;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // CProgress::SetMeter
  78. //
  79. //-----------------------------------------------------------------------------
  80. void CProgress::SetMeter( int currentPos, int range )
  81. {
  82. char buff[16];
  83. int percent;
  84. if ( !m_hWnd || !m_hWndPercent || !m_hWndMeter )
  85. return;
  86. if ( range >= 0 )
  87. {
  88. SendMessage( m_hWndMeter, PBM_SETRANGE, 0, MAKELPARAM( 0, range ) );
  89. m_range = range;
  90. }
  91. SendMessage( m_hWndMeter, PBM_SETPOS, currentPos, 0 );
  92. if ( m_range > 0 )
  93. {
  94. percent = ( int )( 100.0f*currentPos/m_range );
  95. if ( percent > 100 )
  96. percent = 100;
  97. }
  98. else
  99. percent = 0;
  100. sprintf( buff, "%d%%", percent );
  101. SetWindowText( m_hWndPercent, buff );
  102. Update();
  103. }
  104. //-----------------------------------------------------------------------------
  105. // CProgress::SetStatus
  106. //
  107. //-----------------------------------------------------------------------------
  108. void CProgress::SetStatus( const char *line1, const char *line2, const char *line3 )
  109. {
  110. if ( !m_hWnd )
  111. return;
  112. if ( line1 )
  113. SetWindowText( m_hWndStatus1, line1 );
  114. if ( line2 )
  115. SetWindowText( m_hWndStatus2, line2 );
  116. if ( line3 )
  117. SetWindowText( m_hWndStatus3, line3 );
  118. Update();
  119. }
  120. //-----------------------------------------------------------------------------
  121. // CProgress::Open
  122. //
  123. //-----------------------------------------------------------------------------
  124. void CProgress::Open( const char* title, bool canCancel, bool bHasMeter )
  125. {
  126. HWND hWnd;
  127. RECT clientRect;
  128. RECT parentRect;
  129. int cx;
  130. int cy;
  131. int cw;
  132. int ch;
  133. int y;
  134. int dialogHeight;
  135. dialogHeight = PROGRESS_HEIGHT;
  136. if ( !canCancel )
  137. dialogHeight -= 25;
  138. if ( !bHasMeter )
  139. dialogHeight -= GetSystemMetrics( SM_CYVSCROLL );
  140. hWnd = CreateWindowEx(
  141. WS_EX_CLIENTEDGE,
  142. "PROGRESSCLASS",
  143. title,
  144. WS_POPUP|WS_CAPTION,
  145. 0,
  146. 0,
  147. PROGRESS_WIDTH,
  148. dialogHeight,
  149. g_hDlgMain,
  150. NULL,
  151. g_hInstance,
  152. ( void* )this );
  153. m_hWnd = hWnd;
  154. if ( !m_hWnd )
  155. return;
  156. // status text line #1
  157. GetClientRect( m_hWnd, &clientRect );
  158. y = 10;
  159. hWnd = CreateWindowEx(
  160. 0,
  161. WC_STATIC,
  162. "",
  163. WS_VISIBLE|WS_CHILD|SS_WORDELLIPSIS,
  164. 8,
  165. 10,
  166. clientRect.right-clientRect.left-2*8 - 50,
  167. 20,
  168. m_hWnd,
  169. ( HMENU )ID_PROGRESS_STATUS1,
  170. g_hInstance,
  171. NULL );
  172. m_hWndStatus1 = hWnd;
  173. y += 20;
  174. // status text line #2
  175. hWnd = CreateWindowEx(
  176. 0,
  177. WC_STATIC,
  178. "",
  179. WS_VISIBLE|WS_CHILD|SS_PATHELLIPSIS,
  180. 8,
  181. y,
  182. clientRect.right-clientRect.left-2*8 -50,
  183. 20,
  184. m_hWnd,
  185. ( HMENU )ID_PROGRESS_STATUS2,
  186. g_hInstance,
  187. NULL );
  188. m_hWndStatus2 = hWnd;
  189. y += 20;
  190. // status text line #3
  191. hWnd = CreateWindowEx(
  192. 0,
  193. WC_STATIC,
  194. "",
  195. WS_VISIBLE|WS_CHILD|SS_PATHELLIPSIS,
  196. 8,
  197. y,
  198. clientRect.right-clientRect.left-2*8 -50,
  199. 20,
  200. m_hWnd,
  201. ( HMENU )ID_PROGRESS_STATUS3,
  202. g_hInstance,
  203. NULL );
  204. m_hWndStatus3 = hWnd;
  205. y += 20;
  206. // set font
  207. SendMessage( m_hWndStatus1, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
  208. SendMessage( m_hWndStatus2, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
  209. SendMessage( m_hWndStatus3, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
  210. if ( bHasMeter )
  211. {
  212. // percent
  213. hWnd = CreateWindowEx(
  214. 0,
  215. WC_STATIC,
  216. "0%",
  217. WS_VISIBLE|WS_CHILD|SS_RIGHT,
  218. ( clientRect.right-clientRect.left ) - 2*8 - 50,
  219. y - 20,
  220. 50,
  221. 20,
  222. m_hWnd,
  223. ( HMENU )ID_PROGRESS_PERCENT,
  224. g_hInstance,
  225. NULL );
  226. m_hWndPercent = hWnd;
  227. SendMessage( m_hWndPercent, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
  228. // progress meter
  229. ch = GetSystemMetrics( SM_CYVSCROLL );
  230. cw = ( clientRect.right-clientRect.left ) - 2*8;
  231. cx = ( clientRect.left + clientRect.right )/2 - cw/2;
  232. cy = y;
  233. hWnd = CreateWindowEx(
  234. WS_EX_CLIENTEDGE,
  235. PROGRESS_CLASS,
  236. NULL,
  237. WS_VISIBLE|WS_CHILD,
  238. cx,
  239. cy,
  240. cw,
  241. ch,
  242. m_hWnd,
  243. ( HMENU )ID_PROGRESS_METER,
  244. g_hInstance,
  245. NULL );
  246. m_hWndMeter = hWnd;
  247. y = cy+ch;
  248. // ensure bar is reset
  249. SendMessage( m_hWndMeter, PBM_SETRANGE, 0, 0 );
  250. SendMessage( m_hWndMeter, PBM_SETPOS, 0, 0 );
  251. }
  252. else
  253. {
  254. m_hWndPercent = NULL;
  255. m_hWndMeter = NULL;
  256. }
  257. m_bCancelPressed = false;
  258. if ( canCancel )
  259. {
  260. ch = 25;
  261. cw = 80;
  262. cx = ( clientRect.left + clientRect.right )/2 - cw/2;
  263. cy = clientRect.bottom - 8 - ch;
  264. // cancel button
  265. hWnd = CreateWindowEx(
  266. 0,
  267. WC_BUTTON,
  268. "Cancel",
  269. WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
  270. cx,
  271. cy,
  272. cw,
  273. ch,
  274. m_hWnd,
  275. ( HMENU )ID_PROGRESS_CANCEL,
  276. g_hInstance,
  277. NULL );
  278. m_hWndCancel = hWnd;
  279. SendMessage( m_hWndCancel, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
  280. }
  281. // get parent rectangle
  282. GetWindowRect( g_hDlgMain, &parentRect );
  283. cx = ( parentRect.left + parentRect.right )/2 - PROGRESS_WIDTH/2;
  284. cy = ( parentRect.top + parentRect.bottom )/2 - dialogHeight/2;
  285. MoveWindow( m_hWnd, cx, cy, PROGRESS_WIDTH, dialogHeight, FALSE );
  286. ShowWindow( m_hWnd, SHOW_OPENWINDOW );
  287. }
  288. //-----------------------------------------------------------------------------
  289. // CProgress::~CProgress
  290. //
  291. //-----------------------------------------------------------------------------
  292. CProgress::~CProgress()
  293. {
  294. if ( !m_hWnd )
  295. return;
  296. DestroyWindow( m_hWnd );
  297. m_hWnd = NULL;
  298. }
  299. //-----------------------------------------------------------------------------
  300. // CProgress::CProgress
  301. //
  302. //-----------------------------------------------------------------------------
  303. CProgress::CProgress()
  304. {
  305. // set up our window class
  306. WNDCLASS wndclass;
  307. memset( &wndclass, 0, sizeof( wndclass ) );
  308. wndclass.style = 0;
  309. wndclass.lpfnWndProc = Progress_WndProc;
  310. wndclass.cbClsExtra = 0;
  311. wndclass.cbWndExtra = sizeof( CProgress* );
  312. wndclass.hInstance = g_hInstance;
  313. wndclass.hIcon = g_hIcons[ICON_APPLICATION];
  314. wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
  315. wndclass.hbrBackground = g_hBackgroundBrush;
  316. wndclass.lpszMenuName = NULL;
  317. wndclass.lpszClassName = "PROGRESSCLASS";
  318. RegisterClass( &wndclass );
  319. m_hWnd = 0;
  320. m_bCancelPressed = false;
  321. }