source: trunk/src/comctl32/progress.c@ 219

Last change on this file since 219 was 219, checked in by cbratschi, 26 years ago

Updates for new DrawEdge and colors

File size: 13.8 KB
Line 
1/* $Id: progress.c,v 1.6 1999-06-26 14:20:31 cbratschi Exp $ */
2/*
3 * Progress control
4 *
5 * Copyright 1997 Dimitrie O. Paun
6 * Copyright 1998, 1999 Eric Kohl
7 * Copyright 1999 Achim Hasenmueller
8 * Copyright 1999 Christoph Bratschi
9 *
10 * Status: complete
11 */
12
13#include "winbase.h"
14#include "commctrl.h"
15#include "progress.h"
16#include "comctl32.h"
17
18
19/* Control configuration constants */
20
21#define LED_GAP 2
22#define BORDER_WIDTH 3
23
24/* Work constants */
25
26//#define UNKNOWN_PARAM(msg, wParam, lParam) WARN(progress, \
27// "Unknown parameter(s) for message " #msg \
28// "(%04x): wp=%04x lp=%08lx\n", msg, wParam, lParam);
29#define UNKNOWN_PARAM(msg, wParam, lParam)
30
31#define PROGRESS_GetInfoPtr(hwnd) ((PROGRESS_INFO *)GetWindowLongA(hwnd, 0))
32
33
34/***********************************************************************
35 * PROGRESS_Draw
36 * Draws the progress bar.
37 */
38static void
39PROGRESS_Draw (HWND hwnd, HDC hdc, INT lastVal, BOOL inUpdate)
40{
41 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
42 HBRUSH hbrBar,hbrBk,hbrLight,hbrShadow,hbrOld;
43 int rightBar, rightMost, ledWidth;
44 int lastBar;
45 RECT rect;
46 DWORD dwStyle;
47
48// TRACE(progress, "refresh pos=%d min=%d, max=%d\n",
49// infoPtr->CurVal, infoPtr->MinVal, infoPtr->MaxVal);
50
51 if (infoPtr->MinVal == infoPtr->MaxVal) return; //Prevent division through 0
52
53 /* get the required bar brush */
54 if (infoPtr->ColorBar == CLR_DEFAULT) hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
55 else hbrBar = CreateSolidBrush (infoPtr->ColorBar);
56
57 /* get the required background brush */
58 if (infoPtr->ColorBk == CLR_DEFAULT) hbrBk = GetSysColorBrush(COLOR_3DFACE);
59 else hbrBk = CreateSolidBrush(infoPtr->ColorBk);
60
61 /* get client rectangle */
62 GetClientRect (hwnd, &rect);
63
64 /* draw the background */
65 if (!inUpdate)
66 {
67 FillRect(hdc, &rect, hbrBk);
68 //Border
69 hbrLight = GetSysColorPen(COLOR_BTNHILIGHT);
70 hbrShadow = GetSysColorPen(COLOR_BTNSHADOW);
71 MoveToEx(hdc,rect.left,rect.bottom-1,NULL);
72 hbrOld = SelectObject(hdc,hbrShadow);
73 LineTo(hdc,rect.left,rect.top);
74 LineTo(hdc,rect.right-1,rect.top);
75 SelectObject(hdc,hbrLight);
76 LineTo(hdc,rect.right-1,rect.bottom-1);
77 LineTo(hdc,rect.left,rect.bottom-1);
78 SelectObject(hdc,hbrOld);
79 }
80
81 rect.left += BORDER_WIDTH;
82 rect.right -= BORDER_WIDTH;
83 rect.top += BORDER_WIDTH;
84 rect.bottom -= BORDER_WIDTH;
85
86 /* get the window style */
87 dwStyle = GetWindowLongA(hwnd, GWL_STYLE);
88
89 /* compute extent of progress bar */
90 if (dwStyle & PBS_VERTICAL)
91 {
92 rightBar = rect.bottom-MulDiv(infoPtr->CurVal-infoPtr->MinVal,rect.bottom-rect.top,infoPtr->MaxVal-infoPtr->MinVal);
93 if (inUpdate) lastBar = rect.bottom-MulDiv(lastVal-infoPtr->MinVal,rect.bottom-rect.top,infoPtr->MaxVal-infoPtr->MinVal);
94 ledWidth = MulDiv ((rect.right - rect.left), 2, 3);
95 rightMost = rect.top;
96 }
97 else
98 {
99 rightBar = rect.left+MulDiv(infoPtr->CurVal-infoPtr->MinVal,rect.right-rect.left,infoPtr->MaxVal-infoPtr->MinVal);
100 if (inUpdate) lastBar = rect.left+MulDiv(lastVal-infoPtr->MinVal,rect.right-rect.left,infoPtr->MaxVal-infoPtr->MinVal);
101 ledWidth = MulDiv ((rect.bottom - rect.top), 2, 3);
102 rightMost = rect.right;
103 }
104
105 /* now draw the bar */
106 if (dwStyle & PBS_SMOOTH)
107 {
108 if (dwStyle & PBS_VERTICAL)
109 {
110 if (inUpdate)
111 {
112 if (infoPtr->CurVal > lastVal)
113 {
114 rect.top = rightBar;
115 rect.bottom = lastBar;
116 FillRect(hdc,&rect,hbrBar);
117 } else if (infoPtr->CurVal < lastVal)
118 {
119 rect.top = lastBar;
120 rect.bottom = rightBar;
121 FillRect(hdc,&rect,hbrBk);
122 }
123 } else
124 {
125 rect.top = rightBar;
126 FillRect(hdc,&rect,hbrBar);
127 }
128 } else //Horizontal
129 {
130 if (inUpdate)
131 {
132 if (infoPtr->CurVal > lastVal)
133 {
134 rect.left = lastBar;
135 rect.right = rightBar;
136 FillRect(hdc,&rect,hbrBar);
137 } else if (infoPtr->CurVal < lastVal)
138 {
139 rect.left = rightBar;
140 rect.right = lastBar;
141 FillRect(hdc,&rect,hbrBk);
142 }
143 } else
144 {
145 rect.right = rightBar;
146 FillRect(hdc,&rect,hbrBar);
147 }
148 }
149 } else
150 {
151 if (dwStyle & PBS_VERTICAL)
152 {
153 if (inUpdate)
154 {
155 if (infoPtr->CurVal > lastVal)
156 {
157 rect.bottom -= ((int)(rect.bottom-lastBar)/(ledWidth+LED_GAP))*(ledWidth+LED_GAP);
158 while(rect.bottom > rightBar)
159 {
160 rect.top = rect.bottom-ledWidth;
161 if (rect.top < rightMost) rect.top = rightMost;
162 FillRect(hdc,&rect,hbrBar);
163 rect.bottom = rect.top-LED_GAP;
164 }
165 } else if (infoPtr->CurVal < lastVal)
166 {
167 rect.top = rect.bottom-((int)(rect.bottom-lastBar)/(ledWidth+LED_GAP))*(ledWidth+LED_GAP)-ledWidth;
168 if (rect.top < rightMost) rect.top = rightMost;
169 rect.bottom -= ((int)(rect.bottom-rightBar)/(ledWidth+LED_GAP))*(ledWidth+LED_GAP);
170 FillRect(hdc,&rect,hbrBk);
171 }
172 } else
173 {
174 while(rect.bottom > rightBar)
175 {
176 rect.top = rect.bottom-ledWidth;
177 if (rect.top < rightMost) rect.top = rightMost;
178 FillRect(hdc,&rect,hbrBar);
179 rect.bottom = rect.top-LED_GAP;
180 }
181 }
182 } else //Horizontal
183 {
184 if (inUpdate)
185 {
186 if (infoPtr->CurVal > lastVal)
187 {
188 rect.left += ((int)(lastBar-rect.left)/(ledWidth+LED_GAP))*(ledWidth+LED_GAP);
189 while(rect.left < rightBar)
190 {
191 rect.right = rect.left+ledWidth;
192 if (rect.right > rightMost) rect.right = rightMost;
193 FillRect(hdc,&rect,hbrBar);
194 rect.left = rect.right+LED_GAP;
195 }
196 } else if (infoPtr->CurVal < lastVal)
197 {
198 rect.right = rect.left+((int)(lastBar-rect.left)/(ledWidth+LED_GAP))*(ledWidth+LED_GAP)+ledWidth;
199 if (rect.right > rightMost) rect.right = rightMost;
200 rect.left += ((int)(rightBar-rect.left)/(ledWidth+LED_GAP))*(ledWidth+LED_GAP);
201 FillRect(hdc,&rect,hbrBk);
202 }
203 } else
204 {
205 while(rect.left < rightBar)
206 {
207 rect.right = rect.left+ledWidth;
208 if (rect.right > rightMost) rect.right = rightMost;
209 FillRect(hdc,&rect,hbrBar);
210 rect.left = rect.right+LED_GAP;
211 }
212 }
213 }
214 }
215
216 /* delete bar brush */
217 if (infoPtr->ColorBar != CLR_DEFAULT) DeleteObject (hbrBar);
218
219 /* delete background brush */
220 if (infoPtr->ColorBk != CLR_DEFAULT) DeleteObject (hbrBk);
221}
222
223/***********************************************************************
224 * PROGRESS_Update (prototype, todo)
225 * Updates only the changed pixels -> faster, no flickering
226 */
227static void PROGRESS_Update(HWND hwnd,INT lastVal)
228{
229 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
230 HDC hdc;
231
232 if (lastVal != infoPtr->CurVal) //Only update if really necessary
233 {
234 hdc = GetDC(hwnd);
235 PROGRESS_Draw(hwnd,hdc,lastVal,TRUE);
236 ReleaseDC(hwnd,hdc);
237 }
238}
239
240/***********************************************************************
241 * PROGRESS_Refresh
242 * Draw the progress bar. The background need not be erased.
243 */
244static void
245PROGRESS_Refresh (HWND hwnd)
246{
247 HDC hdc;
248
249 hdc = GetDC (hwnd);
250 PROGRESS_Draw (hwnd, hdc, 0, FALSE);
251 ReleaseDC (hwnd, hdc);
252}
253
254/***********************************************************************
255 * PROGRESS_Paint
256 * Draw the progress bar. The background need not be erased.
257 * If dc!=0, it draws on it
258 */
259static void
260PROGRESS_Paint (HWND hwnd)
261{
262 PAINTSTRUCT ps;
263 HDC hdc;
264
265 hdc = BeginPaint (hwnd, &ps);
266 PROGRESS_Draw (hwnd, hdc, 0, FALSE);
267 EndPaint (hwnd, &ps);
268}
269
270
271/***********************************************************************
272 * PROGRESS_CoercePos
273 * Makes sure the current position (CUrVal) is within bounds.
274 */
275static void PROGRESS_CoercePos(HWND hwnd)
276{
277 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
278
279 if(infoPtr->CurVal < infoPtr->MinVal)
280 infoPtr->CurVal = infoPtr->MinVal;
281 if(infoPtr->CurVal > infoPtr->MaxVal)
282 infoPtr->CurVal = infoPtr->MaxVal;
283}
284
285
286/***********************************************************************
287 * PROGRESS_SetFont
288 * Set new Font for progress bar
289 */
290static HFONT
291PROGRESS_SetFont (HWND hwnd, WPARAM wParam, LPARAM lParam)
292{
293 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
294 HFONT hOldFont = infoPtr->hFont;
295
296 infoPtr->hFont = (HFONT)wParam;
297 if (LOWORD(lParam))
298 PROGRESS_Refresh (hwnd);
299 return hOldFont;
300}
301
302
303/***********************************************************************
304 * ProgressWindowProc
305 */
306LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
307 WPARAM wParam, LPARAM lParam)
308{
309 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
310 INT temp;
311
312 switch(message)
313 {
314 case WM_NCCREATE:
315 {
316 DWORD dwExStyle;
317 dwExStyle = GetWindowLongA(hwnd, GWL_EXSTYLE);
318 SetWindowLongA(hwnd, GWL_EXSTYLE, dwExStyle | WS_EX_STATICEDGE);
319 }
320 return TRUE;
321
322 case WM_CREATE:
323 /* allocate memory for info struct */
324 infoPtr =
325 (PROGRESS_INFO *)COMCTL32_Alloc (sizeof(PROGRESS_INFO));
326 SetWindowLongA(hwnd, 0, (DWORD)infoPtr);
327
328 /* initialize the info struct */
329 infoPtr->MinVal=0;
330 infoPtr->MaxVal=100;
331 infoPtr->CurVal=0;
332 infoPtr->Step=10;
333 infoPtr->ColorBar=CLR_DEFAULT;
334 infoPtr->ColorBk=CLR_DEFAULT;
335 infoPtr->hFont=(HANDLE)NULL;
336// TRACE(progress, "Progress Ctrl creation, hwnd=%04x\n", hwnd);
337 break;
338
339 case WM_DESTROY:
340// TRACE (progress, "Progress Ctrl destruction, hwnd=%04x\n", hwnd);
341 COMCTL32_Free (infoPtr);
342 break;
343
344 case WM_ERASEBKGND:
345 /* pretend to erase it here, but we will do it in the paint
346 function to avoid flicker */
347 return 1;
348
349 case WM_GETFONT:
350 return (LRESULT)infoPtr->hFont;
351
352 case WM_SETFONT:
353 return PROGRESS_SetFont (hwnd, wParam, lParam);
354
355 case WM_PAINT:
356 PROGRESS_Paint (hwnd);
357 break;
358
359 case PBM_DELTAPOS:
360 if(lParam)
361 UNKNOWN_PARAM(PBM_DELTAPOS, wParam, lParam);
362 temp = infoPtr->CurVal;
363 if(wParam != 0){
364 infoPtr->CurVal += (INT)wParam;
365 PROGRESS_CoercePos (hwnd);
366 PROGRESS_Update (hwnd,temp);
367 }
368 return temp;
369
370 case PBM_SETPOS:
371 if (lParam)
372 UNKNOWN_PARAM(PBM_SETPOS, wParam, lParam);
373 temp = infoPtr->CurVal;
374 if(temp != wParam){
375 infoPtr->CurVal = (UINT16)wParam; //CB: 0..65535 allowed
376 PROGRESS_CoercePos(hwnd);
377 PROGRESS_Update (hwnd,temp);
378 }
379 return temp;
380
381 case PBM_SETRANGE:
382 if (wParam)
383 UNKNOWN_PARAM(PBM_SETRANGE, wParam, lParam);
384 temp = MAKELONG(infoPtr->MinVal, infoPtr->MaxVal);
385 if(temp != lParam){
386 infoPtr->MinVal = LOWORD(lParam);
387 infoPtr->MaxVal = HIWORD(lParam);
388 if(infoPtr->MaxVal <= infoPtr->MinVal)
389 infoPtr->MaxVal = infoPtr->MinVal+1;
390 PROGRESS_CoercePos(hwnd);
391 PROGRESS_Refresh (hwnd);
392 }
393 return temp;
394
395 case PBM_SETSTEP:
396 if (lParam)
397 UNKNOWN_PARAM(PBM_SETSTEP, wParam, lParam);
398 temp = infoPtr->Step;
399 infoPtr->Step = (INT)wParam; //CB: negative steps allowed
400 return temp;
401
402 case PBM_STEPIT:
403 if (wParam || lParam)
404 UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
405 temp = infoPtr->CurVal;
406 infoPtr->CurVal += infoPtr->Step;
407 if(infoPtr->CurVal > infoPtr->MaxVal)
408 infoPtr->CurVal = infoPtr->MinVal;
409 if(temp != infoPtr->CurVal)
410 PROGRESS_Update (hwnd,temp);
411 return temp;
412
413 case PBM_SETRANGE32:
414 temp = MAKELONG(infoPtr->MinVal, infoPtr->MaxVal);
415 if((infoPtr->MinVal != (INT)wParam) ||
416 (infoPtr->MaxVal != (INT)lParam)) {
417 infoPtr->MinVal = (INT)wParam;
418 infoPtr->MaxVal = (INT)lParam;
419 if(infoPtr->MaxVal <= infoPtr->MinVal)
420 infoPtr->MaxVal = infoPtr->MinVal+1;
421 PROGRESS_CoercePos(hwnd);
422 PROGRESS_Refresh (hwnd);
423 }
424 return temp;
425
426 case PBM_GETRANGE:
427 if (lParam){
428 ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
429 ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
430 }
431 return (wParam) ? infoPtr->MinVal : infoPtr->MaxVal;
432
433 case PBM_GETPOS:
434 if (wParam || lParam)
435 UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
436 return (infoPtr->CurVal);
437
438 case PBM_SETBARCOLOR:
439 if (wParam)
440 UNKNOWN_PARAM(PBM_SETBARCOLOR, wParam, lParam);
441 infoPtr->ColorBar = (COLORREF)lParam;
442 PROGRESS_Refresh (hwnd);
443 break;
444
445 case PBM_SETBKCOLOR:
446 if (wParam)
447 UNKNOWN_PARAM(PBM_SETBKCOLOR, wParam, lParam);
448 infoPtr->ColorBk = (COLORREF)lParam;
449 PROGRESS_Refresh (hwnd);
450 break;
451
452 default:
453// if (message >= WM_USER)
454// ERR(progress, "unknown msg %04x wp=%04x lp=%08lx\n",
455// message, wParam, lParam );
456 return DefWindowProcA( hwnd, message, wParam, lParam );
457 }
458
459 return 0;
460}
461
462
463/***********************************************************************
464 * PROGRESS_Register [Internal]
465 *
466 * Registers the progress bar window class.
467 */
468
469VOID
470PROGRESS_Register (VOID)
471{
472 WNDCLASSA wndClass;
473
474 if (GlobalFindAtomA(PROGRESS_CLASSA)) return;
475
476 ZeroMemory (&wndClass, sizeof( WNDCLASSA));
477 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
478 wndClass.lpfnWndProc = (WNDPROC)ProgressWindowProc;
479 wndClass.cbClsExtra = 0;
480 wndClass.cbWndExtra = sizeof (PROGRESS_INFO *);
481 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
482 wndClass.lpszClassName = PROGRESS_CLASSA;
483
484 RegisterClassA(&wndClass);
485}
486
487
488/***********************************************************************
489 * PROGRESS_Unregister [Internal]
490 *
491 * Unregisters the progress bar window class.
492 */
493
494VOID
495PROGRESS_Unregister (VOID)
496{
497 if (GlobalFindAtomA(PROGRESS_CLASSA))
498 UnregisterClassA(PROGRESS_CLASSA, (HINSTANCE)NULL);
499}
500
Note: See TracBrowser for help on using the repository browser.