summaryrefslogtreecommitdiff
path: root/utils/xbox/vxconsole/progress.cpp
blob: 50d90f46405b494e1d547052bec455c8e19c8d5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//========= Copyright Valve Corporation, All rights reserved. ============//
//
//	PROGRESS.CPP
//
//	Progress Metering Utility.
//=====================================================================================//
#include "vxconsole.h"

#define PROGRESS_WIDTH		425
#define PROGRESS_HEIGHT		170

#define ID_PROGRESS_STATUS1	100
#define ID_PROGRESS_STATUS2	101
#define ID_PROGRESS_STATUS3	102
#define ID_PROGRESS_PERCENT	103
#define ID_PROGRESS_METER	104
#define ID_PROGRESS_CANCEL	105

//-----------------------------------------------------------------------------
//	CProgress_WndProc
// 
//-----------------------------------------------------------------------------
LRESULT CALLBACK Progress_WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	CProgress		*pProgress;
	CREATESTRUCT	*createStructPtr;

	switch ( message )
	{
		case WM_CREATE:
			createStructPtr = ( CREATESTRUCT* )lParam;
			SetWindowLong( hwnd, GWL_USERDATA+0, ( LONG )createStructPtr->lpCreateParams );
			return 0L;

		case WM_DESTROY:
			pProgress = ( CProgress* )GetWindowLong( hwnd, GWL_USERDATA+0 );
			if ( pProgress )
				pProgress->m_hWnd = NULL;
			return 0L;
	
		case WM_CTLCOLORSTATIC:
			SetBkColor( ( HDC )wParam, g_backgroundColor );
			return ( BOOL )g_hBackgroundBrush;

		case WM_COMMAND: 
            switch ( LOWORD( wParam ) ) 
            {
				case ID_PROGRESS_CANCEL: 
					pProgress = ( CProgress* )GetWindowLong( hwnd, GWL_USERDATA+0 );
					if ( pProgress )
						pProgress->m_bCancelPressed = true;
					return ( TRUE ); 
			}
			break;
	}	

	return ( DefWindowProc( hwnd, message, wParam, lParam ) );
}

//-----------------------------------------------------------------------------
//	CProgress::Update
//
//	Pump the message loop
//-----------------------------------------------------------------------------
void CProgress::Update()
{
	MSG	msg;

	while ( PeekMessage( &msg, NULL, 0, 0, PM_NOYIELD|PM_REMOVE ) )
    {
        if ( !TranslateAccelerator( g_hDlgMain, g_hAccel, &msg ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }
}

//-----------------------------------------------------------------------------
//	CProgress::IsCancel
//
//-----------------------------------------------------------------------------
bool CProgress::IsCancel()
{
	return m_bCancelPressed;	
}

//-----------------------------------------------------------------------------
//	CProgress::SetMeter
// 
//-----------------------------------------------------------------------------
void CProgress::SetMeter( int currentPos, int range )
{
	char	buff[16];
	int		percent;

	if ( !m_hWnd || !m_hWndPercent || !m_hWndMeter )
		return;

	if ( range >= 0 )
	{
		SendMessage( m_hWndMeter, PBM_SETRANGE, 0, MAKELPARAM( 0, range ) ); 
		m_range = range;
	}
	SendMessage( m_hWndMeter, PBM_SETPOS, currentPos, 0 ); 

	if ( m_range > 0 )
	{
		percent = ( int )( 100.0f*currentPos/m_range );
		if ( percent > 100 )
			percent = 100;
	}
	else
		percent = 0;
	sprintf( buff, "%d%%", percent );
	SetWindowText( m_hWndPercent, buff );

	Update();
}

//-----------------------------------------------------------------------------
//	CProgress::SetStatus
// 
//-----------------------------------------------------------------------------
void CProgress::SetStatus( const char *line1, const char *line2, const char *line3 )
{
	if ( !m_hWnd )
		return;

	if ( line1 )
		SetWindowText( m_hWndStatus1, line1 );
	if ( line2 )
		SetWindowText( m_hWndStatus2, line2 );
	if ( line3 )
		SetWindowText( m_hWndStatus3, line3 );

	Update();
}

//-----------------------------------------------------------------------------
//	CProgress::Open
// 
//-----------------------------------------------------------------------------
void CProgress::Open( const char* title, bool canCancel, bool bHasMeter )
{
	HWND	hWnd;
	RECT	clientRect;
	RECT	parentRect;
	int		cx;
	int		cy;
	int		cw;
	int		ch;
	int		y;
	int		dialogHeight;

	dialogHeight = PROGRESS_HEIGHT;
	if ( !canCancel )
		dialogHeight -= 25;
	if ( !bHasMeter )
		dialogHeight -= GetSystemMetrics( SM_CYVSCROLL );

	hWnd = CreateWindowEx( 
				WS_EX_CLIENTEDGE,
				"PROGRESSCLASS",
				title,
				WS_POPUP|WS_CAPTION,
				0,
				0,
				PROGRESS_WIDTH,
				dialogHeight,
				g_hDlgMain,
				NULL,
				g_hInstance,
				( void* )this );
	m_hWnd = hWnd;
	if ( !m_hWnd )
		return;

	// status text line #1
	GetClientRect( m_hWnd, &clientRect ); 
	y = 10;
	hWnd = CreateWindowEx( 
				0,
				WC_STATIC, 
				"", 
				WS_VISIBLE|WS_CHILD|SS_WORDELLIPSIS, 
				8, 
				10, 
				clientRect.right-clientRect.left-2*8 - 50,
				20, 
				m_hWnd,
				( HMENU )ID_PROGRESS_STATUS1,
				g_hInstance,
				NULL ); 
	m_hWndStatus1 = hWnd;
	y += 20;

	// status text line #2
	hWnd = CreateWindowEx( 
				0,
				WC_STATIC, 
				"", 
				WS_VISIBLE|WS_CHILD|SS_PATHELLIPSIS, 
				8, 
				y, 
				clientRect.right-clientRect.left-2*8 -50,
				20, 
				m_hWnd,
				( HMENU )ID_PROGRESS_STATUS2,
				g_hInstance,
				NULL ); 
	m_hWndStatus2 = hWnd;
	y += 20;

	// status text line #3
	hWnd = CreateWindowEx( 
				0,
				WC_STATIC, 
				"", 
				WS_VISIBLE|WS_CHILD|SS_PATHELLIPSIS, 
				8, 
				y, 
				clientRect.right-clientRect.left-2*8 -50,
				20, 
				m_hWnd,
				( HMENU )ID_PROGRESS_STATUS3,
				g_hInstance,
				NULL ); 
	m_hWndStatus3 = hWnd;
	y += 20;

	// set font
	SendMessage( m_hWndStatus1, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
	SendMessage( m_hWndStatus2, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
	SendMessage( m_hWndStatus3, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );

	if ( bHasMeter )
	{
		// percent
		hWnd = CreateWindowEx( 
					0,
					WC_STATIC, 
					"0%", 
					WS_VISIBLE|WS_CHILD|SS_RIGHT, 
					( clientRect.right-clientRect.left ) - 2*8 - 50, 
					y - 20, 
					50,
					20, 
					m_hWnd,
					( HMENU )ID_PROGRESS_PERCENT,
					g_hInstance,
					NULL ); 
		m_hWndPercent = hWnd;
		SendMessage( m_hWndPercent, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );

		// progress meter
		ch = GetSystemMetrics( SM_CYVSCROLL ); 
		cw = ( clientRect.right-clientRect.left ) - 2*8;
		cx = ( clientRect.left + clientRect.right )/2 - cw/2;
		cy = y;
		hWnd = CreateWindowEx( 
			WS_EX_CLIENTEDGE,
			PROGRESS_CLASS,
			NULL,
			WS_VISIBLE|WS_CHILD, 
			cx,
			cy,
			cw,
			ch, 
			m_hWnd,
			( HMENU )ID_PROGRESS_METER,
			g_hInstance,
			NULL );
		m_hWndMeter = hWnd;
		y = cy+ch;

		// ensure bar is reset
		SendMessage( m_hWndMeter, PBM_SETRANGE, 0, 0 ); 
		SendMessage( m_hWndMeter, PBM_SETPOS, 0, 0 ); 
	}
	else
	{
		m_hWndPercent = NULL;
		m_hWndMeter = NULL;
	}

	m_bCancelPressed = false;
	if ( canCancel )
	{
	    ch = 25; 
		cw = 80;
		cx = ( clientRect.left + clientRect.right )/2 - cw/2;
		cy = clientRect.bottom - 8 - ch;

		// cancel button
		hWnd = CreateWindowEx( 
					0,
					WC_BUTTON, 
					"Cancel", 
					WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 
					cx, 
					cy, 
					cw,
					ch, 
					m_hWnd,
					( HMENU )ID_PROGRESS_CANCEL,
					g_hInstance,
					NULL ); 
		m_hWndCancel = hWnd;

		SendMessage( m_hWndCancel, WM_SETFONT, ( WPARAM )g_hProportionalFont, TRUE );
	}

	// get parent rectangle
	GetWindowRect( g_hDlgMain, &parentRect );
	cx = ( parentRect.left + parentRect.right )/2 - PROGRESS_WIDTH/2;
	cy = ( parentRect.top + parentRect.bottom )/2 - dialogHeight/2;

	MoveWindow( m_hWnd, cx, cy, PROGRESS_WIDTH, dialogHeight, FALSE );
	ShowWindow( m_hWnd, SHOW_OPENWINDOW );
}

//-----------------------------------------------------------------------------
//	CProgress::~CProgress
// 
//-----------------------------------------------------------------------------
CProgress::~CProgress()
{
	if ( !m_hWnd )
		return;

	DestroyWindow( m_hWnd );
	m_hWnd = NULL;
}

//-----------------------------------------------------------------------------
//	CProgress::CProgress
// 
//-----------------------------------------------------------------------------
CProgress::CProgress()
{
	// set up our window class
	WNDCLASS wndclass;
	memset( &wndclass, 0, sizeof( wndclass ) );
	wndclass.style         = 0;
	wndclass.lpfnWndProc   = Progress_WndProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = sizeof( CProgress* );
	wndclass.hInstance     = g_hInstance;
	wndclass.hIcon         = g_hIcons[ICON_APPLICATION];
	wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
	wndclass.hbrBackground = g_hBackgroundBrush;
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = "PROGRESSCLASS";
	RegisterClass( &wndclass );

	m_hWnd = 0;
	m_bCancelPressed = false;
}