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

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

bug fixes (unicode) and improvements

File size: 16.0 KB
Line 
1/* $Id: progress.c,v 1.7 1999-06-28 15:46:25 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
286static LRESULT PROGRESS_NCCreate(HWND hwnd,WPARAM wParam,LPARAM lParam)
287{
288 DWORD dwExStyle;
289
290 dwExStyle = GetWindowLongA(hwnd,GWL_EXSTYLE);
291 SetWindowLongA(hwnd,GWL_EXSTYLE,dwExStyle | WS_EX_STATICEDGE);
292
293 return TRUE;
294}
295
296static LRESULT PROGRESS_Create(HWND hwnd,WPARAM wParam,LPARAM lParam)
297{
298 PROGRESS_INFO *infoPtr;
299
300 /* allocate memory for info struct */
301 infoPtr = (PROGRESS_INFO *)COMCTL32_Alloc(sizeof(PROGRESS_INFO));
302 SetWindowLongA(hwnd,0,(DWORD)infoPtr);
303
304 /* initialize the info struct */
305 infoPtr->MinVal = 0;
306 infoPtr->MaxVal = 100;
307 infoPtr->CurVal = 0;
308 infoPtr->Step = 10;
309 infoPtr->ColorBar = CLR_DEFAULT;
310 infoPtr->ColorBk = CLR_DEFAULT;
311 infoPtr->hFont = (HANDLE)NULL;
312// TRACE(progress, "Progress Ctrl creation, hwnd=%04x\n", hwnd);
313
314 return 0;
315}
316
317static LRESULT PROGRESS_Destroy(HWND hwnd,WPARAM wParam,LPARAM lParam)
318{
319 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
320
321 // TRACE (progress, "Progress Ctrl destruction, hwnd=%04x\n", hwnd);
322 COMCTL32_Free (infoPtr);
323
324 return 0;
325}
326
327static LRESULT PROGRESS_GetFont(HWND hwnd,WPARAM wParam,LPARAM lParam)
328{
329 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
330
331 return (LRESULT)infoPtr->hFont;
332}
333
334/***********************************************************************
335 * PROGRESS_SetFont
336 * Set new Font for progress bar
337 */
338static HFONT PROGRESS_SetFont (HWND hwnd,WPARAM wParam,LPARAM lParam)
339{
340 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
341 HFONT hOldFont = infoPtr->hFont;
342
343 infoPtr->hFont = (HFONT)wParam;
344 if (LOWORD(lParam)) PROGRESS_Refresh (hwnd);
345
346 return hOldFont;
347}
348
349static LRESULT PROGRESS_DeltaPos(HWND hwnd,WPARAM wParam,LPARAM lParam)
350{
351 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
352 INT temp;
353
354 //if(lParam) UNKNOWN_PARAM(PBM_DELTAPOS, wParam, lParam);
355 temp = infoPtr->CurVal;
356 if (wParam != 0)
357 {
358 infoPtr->CurVal += (INT)wParam;
359 PROGRESS_CoercePos(hwnd);
360 PROGRESS_Update(hwnd,temp);
361 }
362
363 return temp;
364}
365
366static LRESULT PROGRESS_SetPos(HWND hwnd,WPARAM wParam,LPARAM lParam)
367{
368 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
369 INT temp;
370
371 //if (lParam) UNKNOWN_PARAM(PBM_SETPOS, wParam, lParam);
372 temp = infoPtr->CurVal;
373 if (temp != wParam)
374 {
375 infoPtr->CurVal = (UINT16)wParam; //CB: 0..65535 allowed
376 PROGRESS_CoercePos(hwnd);
377 PROGRESS_Update(hwnd,temp);
378 }
379
380 return temp;
381}
382
383static LRESULT PROGRESS_SetRange(HWND hwnd,WPARAM wParam,LPARAM lParam)
384{
385 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
386 INT temp;
387
388 //if (wParam) UNKNOWN_PARAM(PBM_SETRANGE, wParam, lParam);
389 temp = MAKELONG(infoPtr->MinVal,infoPtr->MaxVal);
390 if (temp != lParam)
391 {
392 infoPtr->MinVal = LOWORD(lParam);
393 infoPtr->MaxVal = HIWORD(lParam);
394 if (infoPtr->MaxVal <= infoPtr->MinVal) infoPtr->MaxVal = infoPtr->MinVal+1;
395 PROGRESS_CoercePos(hwnd);
396 PROGRESS_Refresh(hwnd);
397 }
398
399 return temp;
400}
401
402static LRESULT PROGRESS_SetStep(HWND hwnd,WPARAM wParam,LPARAM lParam)
403{
404 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
405 INT temp;
406
407 //if (lParam) UNKNOWN_PARAM(PBM_SETSTEP, wParam, lParam);
408 temp = infoPtr->Step;
409 infoPtr->Step = (INT)wParam; //CB: negative steps allowed
410
411 return temp;
412}
413
414static LRESULT PROGRESS_StepIt(HWND hwnd,WPARAM wParam,LPARAM lParam)
415{
416 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
417 INT temp;
418
419 //if (wParam || lParam) UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
420 temp = infoPtr->CurVal;
421 infoPtr->CurVal += infoPtr->Step;
422 if(infoPtr->CurVal > infoPtr->MaxVal) infoPtr->CurVal = infoPtr->MinVal;
423 if(temp != infoPtr->CurVal) PROGRESS_Update (hwnd,temp);
424
425 return temp;
426}
427
428static LRESULT PROGRESS_SetRange32(HWND hwnd,WPARAM wParam,LPARAM lParam)
429{
430 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
431 INT temp;
432
433 temp = MAKELONG(infoPtr->MinVal,infoPtr->MaxVal);
434 if((infoPtr->MinVal != (INT)wParam) || (infoPtr->MaxVal != (INT)lParam))
435 {
436 infoPtr->MinVal = (INT)wParam;
437 infoPtr->MaxVal = (INT)lParam;
438 if(infoPtr->MaxVal <= infoPtr->MinVal) infoPtr->MaxVal = infoPtr->MinVal+1;
439 PROGRESS_CoercePos(hwnd);
440 PROGRESS_Refresh(hwnd);
441 }
442
443 return temp;
444}
445
446static LRESULT PROGRESS_GetRange(HWND hwnd,WPARAM wParam,LPARAM lParam)
447{
448 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
449
450 if (lParam)
451 {
452 ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
453 ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
454 }
455
456 return (wParam) ? infoPtr->MinVal : infoPtr->MaxVal;
457}
458
459static LRESULT PROGRESS_GetPos(HWND hwnd,WPARAM wParam,LPARAM lParam)
460{
461 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
462
463 //if (wParam || lParam) UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
464
465 return (infoPtr->CurVal);
466}
467
468static LRESULT PROGRESS_SetBarColor(HWND hwnd,WPARAM wParam,LPARAM lParam)
469{
470 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
471 COLORREF oldColorBar = infoPtr->ColorBar;
472
473 //if (wParam) UNKNOWN_PARAM(PBM_SETBARCOLOR, wParam, lParam);
474 infoPtr->ColorBar = (COLORREF)lParam;
475 if (infoPtr->ColorBar != oldColorBar) PROGRESS_Refresh(hwnd);
476
477 return 0;
478}
479
480static LRESULT PROGRESS_SetBkColor(HWND hwnd,WPARAM wParam,LPARAM lParam)
481{
482 PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(hwnd);
483 COLORREF oldColorBk = infoPtr->ColorBk;
484
485 //if (wParam) UNKNOWN_PARAM(PBM_SETBKCOLOR, wParam, lParam);
486 infoPtr->ColorBk = (COLORREF)lParam;
487 if (infoPtr->ColorBk != oldColorBk) PROGRESS_Refresh (hwnd);
488
489 return 0;
490}
491
492/***********************************************************************
493 * ProgressWindowProc
494 */
495LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
496 WPARAM wParam, LPARAM lParam)
497{
498 switch(message)
499 {
500 case WM_NCCREATE:
501 return PROGRESS_NCCreate(hwnd,wParam,lParam);
502
503 case WM_CREATE:
504 return PROGRESS_Create(hwnd,wParam,lParam);
505
506 case WM_DESTROY:
507 return PROGRESS_Destroy(hwnd,wParam,lParam);
508
509 case WM_ERASEBKGND:
510 /* pretend to erase it here, but we will do it in the paint
511 function to avoid flicker */
512 return 1;
513
514 case WM_GETFONT:
515 return PROGRESS_GetFont(hwnd,wParam,lParam);
516
517 case WM_SETFONT:
518 return PROGRESS_SetFont (hwnd,wParam,lParam);
519
520 case WM_PAINT:
521 PROGRESS_Paint(hwnd);
522 break;
523
524 case PBM_DELTAPOS:
525 return PROGRESS_DeltaPos(hwnd,wParam,lParam);
526
527 case PBM_SETPOS:
528 return PROGRESS_SetPos(hwnd,wParam,lParam);
529
530 case PBM_SETRANGE:
531 return PROGRESS_SetRange(hwnd,wParam,lParam);
532
533 case PBM_SETSTEP:
534 return PROGRESS_SetStep(hwnd,wParam,lParam);
535
536 case PBM_STEPIT:
537 return PROGRESS_StepIt(hwnd,wParam,lParam);
538
539 case PBM_SETRANGE32:
540 return PROGRESS_SetRange32(hwnd,wParam,lParam);
541
542 case PBM_GETRANGE:
543 return PROGRESS_GetRange(hwnd,wParam,lParam);
544
545 case PBM_GETPOS:
546 return PROGRESS_GetPos(hwnd,wParam,lParam);
547
548 case PBM_SETBARCOLOR:
549 return PROGRESS_SetBarColor(hwnd,wParam,lParam);
550
551 case PBM_SETBKCOLOR:
552 return PROGRESS_SetBkColor(hwnd,wParam,lParam);
553
554 default:
555// if (message >= WM_USER)
556// ERR(progress, "unknown msg %04x wp=%04x lp=%08lx\n",
557// message, wParam, lParam );
558 return DefWindowProcA( hwnd, message, wParam, lParam );
559 }
560
561 return 0;
562}
563
564
565/***********************************************************************
566 * PROGRESS_Register [Internal]
567 *
568 * Registers the progress bar window class.
569 */
570
571VOID
572PROGRESS_Register (VOID)
573{
574 WNDCLASSA wndClass;
575
576 if (GlobalFindAtomA(PROGRESS_CLASSA)) return;
577
578 ZeroMemory (&wndClass, sizeof( WNDCLASSA));
579 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
580 wndClass.lpfnWndProc = (WNDPROC)ProgressWindowProc;
581 wndClass.cbClsExtra = 0;
582 wndClass.cbWndExtra = sizeof (PROGRESS_INFO *);
583 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
584 wndClass.lpszClassName = PROGRESS_CLASSA;
585
586 RegisterClassA(&wndClass);
587}
588
589
590/***********************************************************************
591 * PROGRESS_Unregister [Internal]
592 *
593 * Unregisters the progress bar window class.
594 */
595
596VOID
597PROGRESS_Unregister (VOID)
598{
599 if (GlobalFindAtomA(PROGRESS_CLASSA))
600 UnregisterClassA(PROGRESS_CLASSA, (HINSTANCE)NULL);
601}
602
Note: See TracBrowser for help on using the repository browser.