4 * Copyright 1997, 2002 Dimitrie O. Paun
5 * Copyright 1998, 1999 Eric Kohl
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * This code was audited for completeness against the documented features
24 * of Comctl32.dll version 6.0 on Sep. 9, 2002, by Dimitrie O. Paun.
26 * Unless otherwise noted, we believe this code to be complete, as per
27 * the specification mentioned above.
28 * If you discover missing features, or bugs, please note them below.
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(progress);
47 HWND Self; /* The window handle for this control */
48 INT CurVal; /* Current progress value */
49 INT MinVal; /* Minimum progress value */
50 INT MaxVal; /* Maximum progress value */
51 INT Step; /* Step to use on PMB_STEPIT */
52 INT MarqueePos; /* Marquee animation position */
53 BOOL Marquee; /* Whether the marquee animation is enabled */
54 COLORREF ColorBar; /* Bar color */
55 COLORREF ColorBk; /* Background color */
56 HFONT Font; /* Handle to font (not unused) */
59 /* Control configuration constants */
62 #define MARQUEE_LEDS 5
63 #define ID_MARQUEE_TIMER 1
65 /* Helper to compute size of a progress bar chunk ("led"). */
66 static inline int get_led_size ( PROGRESS_INFO *infoPtr, LONG style,
69 if (style & PBS_VERTICAL)
70 return MulDiv (rect->right - rect->left, 2, 3);
72 return MulDiv (rect->bottom - rect->top, 2, 3);
75 /* Compute the extend of the bar */
76 static inline int get_bar_size( LONG style, const RECT* rect )
78 if (style & PBS_VERTICAL)
79 return rect->bottom - rect->top;
81 return rect->right - rect->left;
84 /* Compute the pixel position of a progress value */
85 static inline int get_bar_position( PROGRESS_INFO *infoPtr, LONG style,
86 const RECT* rect, INT value )
88 return MulDiv (value - infoPtr->MinVal, get_bar_size (style, rect),
89 infoPtr->MaxVal - infoPtr->MinVal);
92 /***********************************************************************
95 * Invalide the range between old and new pos.
97 static void PROGRESS_Invalidate( PROGRESS_INFO *infoPtr, INT old, INT new )
99 LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
103 GetClientRect (infoPtr->Self, &rect);
104 InflateRect(&rect, -1, -1);
106 oldPos = get_bar_position( infoPtr, style, &rect, old );
107 newPos = get_bar_position( infoPtr, style, &rect, new );
109 if (style & PBS_VERTICAL)
111 rect.top = rect.bottom - max( oldPos, newPos );
112 rect.bottom = rect.bottom - min( oldPos, newPos );
113 if (!(style & PBS_SMOOTH)) rect.top -=
114 get_led_size (infoPtr, style, &rect);
118 rect.left = min( oldPos, newPos );
119 rect.right = max( oldPos, newPos );
120 if (!(style & PBS_SMOOTH)) rect.right +=
121 get_led_size (infoPtr, style, &rect);
123 InvalidateRect( infoPtr->Self, &rect, oldPos > newPos );
126 /* Information for a progress bar drawing helper */
127 typedef struct tagProgressDrawInfo
136 typedef void (*ProgressDrawProc)(const ProgressDrawInfo* di, int start, int end);
138 /* draw solid horizontal bar from 'start' to 'end' */
139 static void draw_solid_bar_H (const ProgressDrawInfo* di, int start, int end)
142 r.left = di->rect.left + start;
143 r.top = di->rect.top;
144 r.right = di->rect.left + end;
145 r.bottom = di->rect.bottom;
146 FillRect (di->hdc, &r, di->hbrBar);
149 /* draw solid horizontal background from 'start' to 'end' */
150 static void draw_solid_bkg_H (const ProgressDrawInfo* di, int start, int end)
153 r.left = di->rect.left + start;
154 r.top = di->rect.top;
155 r.right = di->rect.left + end;
156 r.bottom = di->rect.bottom;
157 FillRect (di->hdc, &r, di->hbrBk);
160 /* draw solid vertical bar from 'start' to 'end' */
161 static void draw_solid_bar_V (const ProgressDrawInfo* di, int start, int end)
164 r.left = di->rect.left;
165 r.top = di->rect.bottom - end;
166 r.right = di->rect.right;
167 r.bottom = di->rect.bottom - start;
168 FillRect (di->hdc, &r, di->hbrBar);
171 /* draw solid vertical background from 'start' to 'end' */
172 static void draw_solid_bkg_V (const ProgressDrawInfo* di, int start, int end)
175 r.left = di->rect.left;
176 r.top = di->rect.bottom - end;
177 r.right = di->rect.right;
178 r.bottom = di->rect.bottom - start;
179 FillRect (di->hdc, &r, di->hbrBk);
182 /* draw chunky horizontal bar from 'start' to 'end' */
183 static void draw_chunk_bar_H (const ProgressDrawInfo* di, int start, int end)
186 int right = di->rect.left + end;
187 r.left = di->rect.left + start;
188 r.top = di->rect.top;
189 r.bottom = di->rect.bottom;
190 while (r.left < right)
192 r.right = min (r.left + di->ledW, right);
193 FillRect (di->hdc, &r, di->hbrBar);
195 r.right = min (r.left + LED_GAP, right);
196 FillRect (di->hdc, &r, di->hbrBk);
201 /* draw chunky vertical bar from 'start' to 'end' */
202 static void draw_chunk_bar_V (const ProgressDrawInfo* di, int start, int end)
205 int top = di->rect.bottom - end;
206 r.left = di->rect.left;
207 r.right = di->rect.right;
208 r.bottom = di->rect.bottom - start;
209 while (r.bottom > top)
211 r.top = max (r.bottom - di->ledW, top);
212 FillRect (di->hdc, &r, di->hbrBar);
214 r.top = max (r.bottom - LED_GAP, top);
215 FillRect (di->hdc, &r, di->hbrBk);
220 /* drawing functions for "classic" style */
221 static const ProgressDrawProc drawProcClassic[8] = {
224 draw_solid_bar_H, draw_solid_bkg_H,
226 draw_solid_bar_V, draw_solid_bkg_V,
229 draw_chunk_bar_H, draw_solid_bkg_H,
231 draw_chunk_bar_V, draw_solid_bkg_V,
234 /***********************************************************************
236 * Draws the progress bar.
238 static LRESULT PROGRESS_Draw (PROGRESS_INFO *infoPtr, HDC hdc)
243 const ProgressDrawProc* drawProcs;
244 ProgressDrawInfo pdi;
246 TRACE("(infoPtr=%p, hdc=%p)\n", infoPtr, hdc);
250 /* get the required bar brush */
251 if (infoPtr->ColorBar == CLR_DEFAULT)
252 pdi.hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
254 pdi.hbrBar = CreateSolidBrush (infoPtr->ColorBar);
256 if (infoPtr->ColorBk == CLR_DEFAULT)
257 pdi.hbrBk = GetSysColorBrush(COLOR_3DFACE);
259 pdi.hbrBk = CreateSolidBrush(infoPtr->ColorBk);
261 /* get the window style */
262 dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
264 /* get client rectangle */
265 GetClientRect (infoPtr->Self, &pdi.rect);
266 FrameRect( hdc, &pdi.rect, pdi.hbrBk );
267 InflateRect(&pdi.rect, -1, -1);
269 /* compute some drawing parameters */
270 barSmooth = (dwStyle & PBS_SMOOTH);
271 drawProcs = &(drawProcClassic[(barSmooth ? 0 : 4)
272 + ((dwStyle & PBS_VERTICAL) ? 2 : 0)]);
273 barSize = get_bar_size( dwStyle, &pdi.rect );
276 pdi.ledW = get_led_size( infoPtr, dwStyle, &pdi.rect);
278 /* now draw the bar */
279 if (dwStyle & PBS_MARQUEE)
281 const int ledW = !barSmooth ? (pdi.ledW + LED_GAP) : 1;
282 const int leds = (barSize + ledW - 1) / ledW;
283 const int ledMEnd = infoPtr->MarqueePos + MARQUEE_LEDS;
287 /* case 1: the marquee bar extends over the end and wraps around to
289 const int gapStart = max((ledMEnd - leds) * ledW, 0);
290 const int gapEnd = min(infoPtr->MarqueePos * ledW, barSize);
292 drawProcs[0]( &pdi, 0, gapStart);
293 drawProcs[1]( &pdi, gapStart, gapEnd);
294 drawProcs[0]( &pdi, gapEnd, barSize);
298 /* case 2: the marquee bar is between start and end */
299 const int barStart = infoPtr->MarqueePos * ledW;
300 const int barEnd = min (ledMEnd * ledW, barSize);
302 drawProcs[1]( &pdi, 0, barStart);
303 drawProcs[0]( &pdi, barStart, barEnd);
304 drawProcs[1]( &pdi, barEnd, barSize);
309 int barEnd = get_bar_position( infoPtr, dwStyle, &pdi.rect,
313 const int ledW = pdi.ledW + LED_GAP;
314 barEnd = min (((barEnd + ledW - 1) / ledW) * ledW, barSize);
316 drawProcs[0]( &pdi, 0, barEnd);
317 drawProcs[1]( &pdi, barEnd, barSize);
320 /* delete bar brush */
321 if (infoPtr->ColorBar != CLR_DEFAULT) DeleteObject (pdi.hbrBar);
322 if (infoPtr->ColorBk != CLR_DEFAULT) DeleteObject (pdi.hbrBk);
328 /***********************************************************************
330 * Draw the progress bar. The background need not be erased.
331 * If dc!=0, it draws on it
333 static LRESULT PROGRESS_Paint (PROGRESS_INFO *infoPtr, HDC hdc)
336 if (hdc) return PROGRESS_Draw (infoPtr, hdc);
337 hdc = BeginPaint (infoPtr->Self, &ps);
338 PROGRESS_Draw (infoPtr, hdc);
339 EndPaint (infoPtr->Self, &ps);
344 /***********************************************************************
346 * Handle the marquee timer messages
348 static LRESULT PROGRESS_Timer (PROGRESS_INFO *infoPtr, INT idTimer)
350 if(idTimer == ID_MARQUEE_TIMER)
352 LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
356 GetClientRect (infoPtr->Self, &rect);
358 if(!(style & PBS_SMOOTH))
359 ledWidth = get_led_size( infoPtr, style, &rect ) + LED_GAP;
363 leds = (get_bar_size( style, &rect ) + ledWidth - 1) /
366 /* increment the marquee progress */
367 if(++infoPtr->MarqueePos > leds)
369 infoPtr->MarqueePos = 0;
372 InvalidateRect(infoPtr->Self, &rect, FALSE);
378 /***********************************************************************
380 * Makes sure the current position (CurVal) is within bounds.
382 static void PROGRESS_CoercePos(PROGRESS_INFO *infoPtr)
384 if(infoPtr->CurVal < infoPtr->MinVal)
385 infoPtr->CurVal = infoPtr->MinVal;
386 if(infoPtr->CurVal > infoPtr->MaxVal)
387 infoPtr->CurVal = infoPtr->MaxVal;
391 /***********************************************************************
393 * Set new Font for progress bar
395 static HFONT PROGRESS_SetFont (PROGRESS_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
397 HFONT hOldFont = infoPtr->Font;
398 infoPtr->Font = hFont;
399 /* Since infoPtr->Font is not used, there is no need for repaint */
403 static DWORD PROGRESS_SetRange (PROGRESS_INFO *infoPtr, int low, int high)
405 DWORD res = MAKELONG(LOWORD(infoPtr->MinVal), LOWORD(infoPtr->MaxVal));
407 /* if nothing changes, simply return */
408 if(infoPtr->MinVal == low && infoPtr->MaxVal == high) return res;
410 infoPtr->MinVal = low;
411 infoPtr->MaxVal = high;
412 PROGRESS_CoercePos(infoPtr);
413 InvalidateRect(infoPtr->Self, NULL, TRUE);
417 /***********************************************************************
420 static LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
421 WPARAM wParam, LPARAM lParam)
423 PROGRESS_INFO *infoPtr;
425 TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
427 infoPtr = (PROGRESS_INFO *)GetWindowLongPtrW(hwnd, 0);
429 if (!infoPtr && message != WM_CREATE)
430 return DefWindowProcW( hwnd, message, wParam, lParam );
435 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
436 dwExStyle &= ~(WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE);
437 dwExStyle |= WS_EX_STATICEDGE;
438 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
439 /* Force recalculation of a non-client area */
440 SetWindowPos(hwnd, 0, 0, 0, 0, 0,
441 SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
443 /* allocate memory for info struct */
444 infoPtr = (PROGRESS_INFO *)Alloc (sizeof(PROGRESS_INFO));
445 if (!infoPtr) return -1;
446 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
448 /* initialize the info struct */
449 infoPtr->Self = hwnd;
451 infoPtr->MaxVal = 100;
454 infoPtr->MarqueePos = 0;
455 infoPtr->Marquee = FALSE;
456 infoPtr->ColorBar = CLR_DEFAULT;
457 infoPtr->ColorBk = CLR_DEFAULT;
459 TRACE("Progress Ctrl creation, hwnd=%p\n", hwnd);
464 TRACE("Progress Ctrl destruction, hwnd=%p\n", hwnd);
466 SetWindowLongPtrW(hwnd, 0, 0);
470 return (LRESULT)infoPtr->Font;
473 return (LRESULT)PROGRESS_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
476 return PROGRESS_Paint (infoPtr, (HDC)wParam);
479 return PROGRESS_Timer (infoPtr, (INT)wParam);
484 oldVal = infoPtr->CurVal;
486 infoPtr->CurVal += (INT)wParam;
487 PROGRESS_CoercePos (infoPtr);
488 TRACE("PBM_DELTAPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
489 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
497 oldVal = infoPtr->CurVal;
498 if(oldVal != wParam) {
499 infoPtr->CurVal = (INT)wParam;
500 PROGRESS_CoercePos(infoPtr);
501 TRACE("PBM_SETPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
502 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
508 return PROGRESS_SetRange (infoPtr, (int)LOWORD(lParam), (int)HIWORD(lParam));
513 oldStep = infoPtr->Step;
514 infoPtr->Step = (INT)wParam;
521 oldVal = infoPtr->CurVal;
522 infoPtr->CurVal += infoPtr->Step;
523 if(infoPtr->CurVal > infoPtr->MaxVal)
524 infoPtr->CurVal = infoPtr->MinVal;
525 if(oldVal != infoPtr->CurVal)
527 TRACE("PBM_STEPIT: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
528 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
534 return PROGRESS_SetRange (infoPtr, (int)wParam, (int)lParam);
538 ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
539 ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
541 return wParam ? infoPtr->MinVal : infoPtr->MaxVal;
544 return infoPtr->CurVal;
546 case PBM_SETBARCOLOR:
547 infoPtr->ColorBar = (COLORREF)lParam;
548 InvalidateRect(hwnd, NULL, TRUE);
552 infoPtr->ColorBk = (COLORREF)lParam;
553 InvalidateRect(hwnd, NULL, TRUE);
559 infoPtr->Marquee = TRUE;
560 SetTimer(infoPtr->Self, ID_MARQUEE_TIMER, (UINT)lParam, NULL);
564 infoPtr->Marquee = FALSE;
565 KillTimer(infoPtr->Self, ID_MARQUEE_TIMER);
567 return infoPtr->Marquee;
570 if ((message >= WM_USER) && (message < WM_APP))
571 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
572 return DefWindowProcW( hwnd, message, wParam, lParam );
577 /***********************************************************************
578 * PROGRESS_Register [Internal]
580 * Registers the progress bar window class.
582 void PROGRESS_Register (void)
586 ZeroMemory (&wndClass, sizeof(wndClass));
587 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
588 wndClass.lpfnWndProc = (WNDPROC)ProgressWindowProc;
589 wndClass.cbClsExtra = 0;
590 wndClass.cbWndExtra = sizeof (PROGRESS_INFO *);
591 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
592 wndClass.lpszClassName = PROGRESS_CLASSW;
594 RegisterClassW (&wndClass);
598 /***********************************************************************
599 * PROGRESS_Unregister [Internal]
601 * Unregisters the progress bar window class.
603 void PROGRESS_Unregister (void)
605 UnregisterClassW (PROGRESS_CLASSW, NULL);