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

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

progress, trackbar: finished version 5.00 (IE5)

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