4 * Copyright 1997, 2002 Dimitrie O. Paun
5 * Copyright 1998, 1999 Eric Kohl
12 #include "debugtools.h"
14 DEFAULT_DEBUG_CHANNEL(progress);
18 HWND Self; /* The window handle for this control */
19 INT CurVal; /* Current progress value */
20 INT MinVal; /* Minimum progress value */
21 INT MaxVal; /* Maximum progress value */
22 INT Step; /* Step to use on PMB_STEPIT */
23 COLORREF ColorBar; /* Bar color */
24 COLORREF ColorBk; /* Background color */
25 HFONT Font; /* Handle to font (not unused) */
28 /* Control configuration constants */
32 #define UNKNOWN_PARAM(msg, wParam, lParam) WARN( \
33 "Unknown parameter(s) for message " #msg \
34 "(%04x): wp=%04x lp=%08lx\n", msg, wParam, lParam);
36 /***********************************************************************
38 * Draws the progress bar.
40 static LRESULT PROGRESS_Draw (PROGRESS_INFO *infoPtr, HDC hdc)
43 int rightBar, rightMost, ledWidth;
47 TRACE("(infoPtr=%p, hdc=%x)\n", infoPtr, hdc);
49 /* get the required bar brush */
50 if (infoPtr->ColorBar == CLR_DEFAULT)
51 hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
53 hbrBar = CreateSolidBrush (infoPtr->ColorBar);
55 /* get the required background brush */
56 if (infoPtr->ColorBk == CLR_DEFAULT)
57 hbrBk = GetSysColorBrush (COLOR_3DFACE);
59 hbrBk = CreateSolidBrush (infoPtr->ColorBk);
61 /* get client rectangle */
62 GetClientRect (infoPtr->Self, &rect);
64 /* draw the background */
65 FillRect(hdc, &rect, hbrBk);
67 rect.left++; rect.right--; rect.top++; rect.bottom--;
69 /* get the window style */
70 dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
72 /* compute extent of progress bar */
73 if (dwStyle & PBS_VERTICAL) {
74 rightBar = rect.bottom -
75 MulDiv (infoPtr->CurVal - infoPtr->MinVal,
76 rect.bottom - rect.top,
77 infoPtr->MaxVal - infoPtr->MinVal);
78 ledWidth = MulDiv (rect.right - rect.left, 2, 3);
81 rightBar = rect.left +
82 MulDiv (infoPtr->CurVal - infoPtr->MinVal,
83 rect.right - rect.left,
84 infoPtr->MaxVal - infoPtr->MinVal);
85 ledWidth = MulDiv (rect.bottom - rect.top, 2, 3);
86 rightMost = rect.right;
89 /* now draw the bar */
90 if (dwStyle & PBS_SMOOTH) {
91 if (dwStyle & PBS_VERTICAL)
94 rect.right = rightBar;
95 FillRect(hdc, &rect, hbrBar);
97 if (dwStyle & PBS_VERTICAL) {
98 while(rect.bottom > rightBar) {
99 rect.top = rect.bottom - ledWidth;
100 if (rect.top < rightMost)
101 rect.top = rightMost;
102 FillRect(hdc, &rect, hbrBar);
103 rect.bottom = rect.top - LED_GAP;
106 while(rect.left < rightBar) {
107 rect.right = rect.left + ledWidth;
108 if (rect.right > rightMost)
109 rect.right = rightMost;
110 FillRect(hdc, &rect, hbrBar);
111 rect.left = rect.right + LED_GAP;
116 /* delete bar brush */
117 if (infoPtr->ColorBar != CLR_DEFAULT)
118 DeleteObject (hbrBar);
120 /* delete background brush */
121 if (infoPtr->ColorBk != CLR_DEFAULT)
122 DeleteObject (hbrBk);
127 /***********************************************************************
129 * Draw the progress bar. The background need not be erased.
131 static LRESULT PROGRESS_Refresh (PROGRESS_INFO *infoPtr)
133 HDC hdc = GetDC (infoPtr->Self);
134 LRESULT res = PROGRESS_Draw (infoPtr, hdc);
135 ReleaseDC (infoPtr->Self, hdc);
139 /***********************************************************************
141 * Draw the progress bar. The background need not be erased.
142 * If dc!=0, it draws on it
144 static LRESULT PROGRESS_Paint (PROGRESS_INFO *infoPtr, HDC hdc)
147 if (hdc) return PROGRESS_Draw (infoPtr, hdc);
148 hdc = BeginPaint (infoPtr->Self, &ps);
149 PROGRESS_Draw (infoPtr, hdc);
150 EndPaint (infoPtr->Self, &ps);
155 /***********************************************************************
157 * Makes sure the current position (CurVal) is within bounds.
159 static void PROGRESS_CoercePos(PROGRESS_INFO *infoPtr)
161 if(infoPtr->CurVal < infoPtr->MinVal)
162 infoPtr->CurVal = infoPtr->MinVal;
163 if(infoPtr->CurVal > infoPtr->MaxVal)
164 infoPtr->CurVal = infoPtr->MaxVal;
168 /***********************************************************************
170 * Set new Font for progress bar
172 static HFONT PROGRESS_SetFont (PROGRESS_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
174 HFONT hOldFont = infoPtr->Font;
175 infoPtr->Font = hFont;
176 if (bRedraw) PROGRESS_Refresh (infoPtr);
180 static DWORD PROGRESS_SetRange (PROGRESS_INFO *infoPtr, int low, int high)
182 DWORD res = MAKELONG(LOWORD(infoPtr->MinVal), LOWORD(infoPtr->MaxVal));
184 /* if nothing changes, simply return */
185 if(infoPtr->MinVal == low && infoPtr->MaxVal == high) return res;
187 /* if things are different, adjust values and repaint the control */
188 if (high <= low) high = low + 1;
189 infoPtr->MinVal = low;
190 infoPtr->MaxVal = high;
191 PROGRESS_CoercePos(infoPtr);
192 PROGRESS_Refresh (infoPtr);
196 /***********************************************************************
199 static LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
200 WPARAM wParam, LPARAM lParam)
202 PROGRESS_INFO *infoPtr = (PROGRESS_INFO *)GetWindowLongW(hwnd, 0);
206 if (!infoPtr && message != WM_CREATE && message != WM_NCCREATE)
207 return DefWindowProcW( hwnd, message, wParam, lParam );
211 dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
212 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle | WS_EX_STATICEDGE);
216 /* allocate memory for info struct */
217 infoPtr = (PROGRESS_INFO *)COMCTL32_Alloc (sizeof(PROGRESS_INFO));
218 if (!infoPtr) return -1;
219 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
221 /* initialize the info struct */
222 infoPtr->Self = hwnd;
224 infoPtr->MaxVal = 100;
227 infoPtr->ColorBar = CLR_DEFAULT;
228 infoPtr->ColorBk = CLR_DEFAULT;
230 TRACE("Progress Ctrl creation, hwnd=%04x\n", hwnd);
234 TRACE("Progress Ctrl destruction, hwnd=%04x\n", hwnd);
235 COMCTL32_Free (infoPtr);
236 SetWindowLongW(hwnd, 0, 0);
240 /* pretend to erase it here, but we will do it in the paint
241 function to avoid flicker */
245 return (LRESULT)infoPtr->Font;
248 return PROGRESS_SetFont (infoPtr, (HFONT)wParam, (BOOL)lParam);
251 return PROGRESS_Paint (infoPtr, (HDC)wParam);
254 if(lParam) UNKNOWN_PARAM(PBM_DELTAPOS, wParam, lParam);
255 temp = infoPtr->CurVal;
257 infoPtr->CurVal += (WORD)wParam;
258 PROGRESS_CoercePos (infoPtr);
259 PROGRESS_Refresh (infoPtr);
264 if (lParam) UNKNOWN_PARAM(PBM_SETPOS, wParam, lParam);
265 temp = infoPtr->CurVal;
267 infoPtr->CurVal = (WORD)wParam;
268 PROGRESS_CoercePos(infoPtr);
269 PROGRESS_Refresh (infoPtr);
274 if (wParam) UNKNOWN_PARAM(PBM_SETRANGE, wParam, lParam);
275 return PROGRESS_SetRange (infoPtr, (int)LOWORD(lParam), (int)HIWORD(lParam));
278 if (lParam) UNKNOWN_PARAM(PBM_SETSTEP, wParam, lParam);
279 temp = infoPtr->Step;
280 infoPtr->Step = (WORD)wParam;
284 if (wParam || lParam) UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
285 temp = infoPtr->CurVal;
286 infoPtr->CurVal += infoPtr->Step;
287 if(infoPtr->CurVal > infoPtr->MaxVal)
288 infoPtr->CurVal = infoPtr->MinVal;
289 if(temp != infoPtr->CurVal)
290 PROGRESS_Refresh (infoPtr);
294 return PROGRESS_SetRange (infoPtr, (int)wParam, (int)lParam);
298 ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
299 ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
301 return wParam ? infoPtr->MinVal : infoPtr->MaxVal;
304 if (wParam || lParam) UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
305 return infoPtr->CurVal;
307 case PBM_SETBARCOLOR:
308 if (wParam) UNKNOWN_PARAM(PBM_SETBARCOLOR, wParam, lParam);
309 infoPtr->ColorBar = (COLORREF)lParam;
310 return PROGRESS_Refresh (infoPtr);
313 if (wParam) UNKNOWN_PARAM(PBM_SETBKCOLOR, wParam, lParam);
314 infoPtr->ColorBk = (COLORREF)lParam;
315 return PROGRESS_Refresh (infoPtr);
318 if (message >= WM_USER)
319 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
320 return DefWindowProcW( hwnd, message, wParam, lParam );
325 /***********************************************************************
326 * PROGRESS_Register [Internal]
328 * Registers the progress bar window class.
330 VOID PROGRESS_Register (void)
334 ZeroMemory (&wndClass, sizeof(wndClass));
335 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
336 wndClass.lpfnWndProc = (WNDPROC)ProgressWindowProc;
337 wndClass.cbClsExtra = 0;
338 wndClass.cbWndExtra = sizeof (PROGRESS_INFO *);
339 wndClass.hCursor = LoadCursorW (0, IDC_ARROWW);
340 wndClass.lpszClassName = PROGRESS_CLASSW;
342 RegisterClassW (&wndClass);
346 /***********************************************************************
347 * PROGRESS_Unregister [Internal]
349 * Unregisters the progress bar window class.
351 VOID PROGRESS_Unregister (void)
353 UnregisterClassW (PROGRESS_CLASSW, (HINSTANCE)NULL);