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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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.
33 * -- PBS_SMOOTHREVERSE
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(progress);
54 HWND Self; /* The window handle for this control */
55 INT CurVal; /* Current progress value */
56 INT MinVal; /* Minimum progress value */
57 INT MaxVal; /* Maximum progress value */
58 INT Step; /* Step to use on PMB_STEPIT */
59 INT MarqueePos; /* Marquee animation position */
60 BOOL Marquee; /* Whether the marquee animation is enabled */
61 COLORREF ColorBar; /* Bar color */
62 COLORREF ColorBk; /* Background color */
63 HFONT Font; /* Handle to font (not unused) */
66 /* Control configuration constants */
69 #define MARQUEE_LEDS 5
70 #define ID_MARQUEE_TIMER 1
72 /* Helper to obtain size of a progress bar chunk ("led"). */
73 static inline int get_led_size ( const PROGRESS_INFO *infoPtr, LONG style,
76 HTHEME theme = GetWindowTheme (infoPtr->Self);
80 if (SUCCEEDED( GetThemeInt( theme, 0, 0, TMT_PROGRESSCHUNKSIZE, &chunkSize )))
84 if (style & PBS_VERTICAL)
85 return MulDiv (rect->right - rect->left, 2, 3);
87 return MulDiv (rect->bottom - rect->top, 2, 3);
90 /* Helper to obtain gap between progress bar chunks */
91 static inline int get_led_gap ( const PROGRESS_INFO *infoPtr )
93 HTHEME theme = GetWindowTheme (infoPtr->Self);
97 if (SUCCEEDED( GetThemeInt( theme, 0, 0, TMT_PROGRESSSPACESIZE, &spaceSize )))
104 /* Get client rect. Takes into account that theming needs no adjustment. */
105 static inline void get_client_rect (HWND hwnd, RECT* rect)
107 HTHEME theme = GetWindowTheme (hwnd);
108 GetClientRect (hwnd, rect);
110 InflateRect(rect, -1, -1);
113 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
114 int part = (dwStyle & PBS_VERTICAL) ? PP_BARVERT : PP_BAR;
115 GetThemeBackgroundContentRect (theme, 0, part, 0, rect, rect);
119 /* Compute the extend of the bar */
120 static inline int get_bar_size( LONG style, const RECT* rect )
122 if (style & PBS_VERTICAL)
123 return rect->bottom - rect->top;
125 return rect->right - rect->left;
128 /* Compute the pixel position of a progress value */
129 static inline int get_bar_position( const PROGRESS_INFO *infoPtr, LONG style,
130 const RECT* rect, INT value )
132 return MulDiv (value - infoPtr->MinVal, get_bar_size (style, rect),
133 infoPtr->MaxVal - infoPtr->MinVal);
136 /***********************************************************************
137 * PROGRESS_Invalidate
139 * Don't be too clever about invalidating the progress bar.
140 * InstallShield depends on this simple behaviour.
142 static void PROGRESS_Invalidate( const PROGRESS_INFO *infoPtr, INT old, INT new )
144 InvalidateRect( infoPtr->Self, NULL, old > new );
147 /* Information for a progress bar drawing helper */
148 typedef struct tagProgressDrawInfo
159 typedef void (*ProgressDrawProc)(const ProgressDrawInfo* di, int start, int end);
161 /* draw solid horizontal bar from 'start' to 'end' */
162 static void draw_solid_bar_H (const ProgressDrawInfo* di, int start, int end)
165 r.left = di->rect.left + start;
166 r.top = di->rect.top;
167 r.right = di->rect.left + end;
168 r.bottom = di->rect.bottom;
169 FillRect (di->hdc, &r, di->hbrBar);
172 /* draw solid horizontal background from 'start' to 'end' */
173 static void draw_solid_bkg_H (const ProgressDrawInfo* di, int start, int end)
176 r.left = di->rect.left + start;
177 r.top = di->rect.top;
178 r.right = di->rect.left + end;
179 r.bottom = di->rect.bottom;
180 FillRect (di->hdc, &r, di->hbrBk);
183 /* draw solid vertical bar from 'start' to 'end' */
184 static void draw_solid_bar_V (const ProgressDrawInfo* di, int start, int end)
187 r.left = di->rect.left;
188 r.top = di->rect.bottom - end;
189 r.right = di->rect.right;
190 r.bottom = di->rect.bottom - start;
191 FillRect (di->hdc, &r, di->hbrBar);
194 /* draw solid vertical background from 'start' to 'end' */
195 static void draw_solid_bkg_V (const ProgressDrawInfo* di, int start, int end)
198 r.left = di->rect.left;
199 r.top = di->rect.bottom - end;
200 r.right = di->rect.right;
201 r.bottom = di->rect.bottom - start;
202 FillRect (di->hdc, &r, di->hbrBk);
205 /* draw chunky horizontal bar from 'start' to 'end' */
206 static void draw_chunk_bar_H (const ProgressDrawInfo* di, int start, int end)
209 int right = di->rect.left + end;
210 r.left = di->rect.left + start;
211 r.top = di->rect.top;
212 r.bottom = di->rect.bottom;
213 while (r.left < right)
215 r.right = min (r.left + di->ledW, right);
216 FillRect (di->hdc, &r, di->hbrBar);
218 r.right = min (r.left + di->ledGap, right);
219 FillRect (di->hdc, &r, di->hbrBk);
224 /* draw chunky vertical bar from 'start' to 'end' */
225 static void draw_chunk_bar_V (const ProgressDrawInfo* di, int start, int end)
228 int top = di->rect.bottom - end;
229 r.left = di->rect.left;
230 r.right = di->rect.right;
231 r.bottom = di->rect.bottom - start;
232 while (r.bottom > top)
234 r.top = max (r.bottom - di->ledW, top);
235 FillRect (di->hdc, &r, di->hbrBar);
237 r.top = max (r.bottom - di->ledGap, top);
238 FillRect (di->hdc, &r, di->hbrBk);
243 /* drawing functions for "classic" style */
244 static const ProgressDrawProc drawProcClassic[8] = {
247 draw_solid_bar_H, draw_solid_bkg_H,
249 draw_solid_bar_V, draw_solid_bkg_V,
252 draw_chunk_bar_H, draw_solid_bkg_H,
254 draw_chunk_bar_V, draw_solid_bkg_V,
257 /* draw themed horizontal bar from 'start' to 'end' */
258 static void draw_theme_bar_H (const ProgressDrawInfo* di, int start, int end)
261 r.left = di->rect.left + start;
262 r.top = di->rect.top;
263 r.bottom = di->rect.bottom;
264 r.right = di->rect.left + end;
265 DrawThemeBackground (di->theme, di->hdc, PP_CHUNK, 0, &r, NULL);
268 /* draw themed vertical bar from 'start' to 'end' */
269 static void draw_theme_bar_V (const ProgressDrawInfo* di, int start, int end)
272 r.left = di->rect.left;
273 r.right = di->rect.right;
274 r.bottom = di->rect.bottom - start;
275 r.top = di->rect.bottom - end;
276 DrawThemeBackground (di->theme, di->hdc, PP_CHUNKVERT, 0, &r, NULL);
279 /* draw themed horizontal background from 'start' to 'end' */
280 static void draw_theme_bkg_H (const ProgressDrawInfo* di, int start, int end)
283 r.left = di->rect.left + start;
284 r.top = di->rect.top;
285 r.right = di->rect.left + end;
286 r.bottom = di->rect.bottom;
287 DrawThemeBackground (di->theme, di->hdc, PP_BAR, 0, &di->bgRect, &r);
290 /* draw themed vertical background from 'start' to 'end' */
291 static void draw_theme_bkg_V (const ProgressDrawInfo* di, int start, int end)
294 r.left = di->rect.left;
295 r.top = di->rect.bottom - end;
296 r.right = di->rect.right;
297 r.bottom = di->rect.bottom - start;
298 DrawThemeBackground (di->theme, di->hdc, PP_BARVERT, 0, &di->bgRect, &r);
301 /* drawing functions for themed style */
302 static const ProgressDrawProc drawProcThemed[8] = {
305 draw_theme_bar_H, draw_theme_bkg_H,
307 draw_theme_bar_V, draw_theme_bkg_V,
310 draw_theme_bar_H, draw_theme_bkg_H,
312 draw_theme_bar_V, draw_theme_bkg_V,
315 /***********************************************************************
317 * Draws the progress bar.
319 static LRESULT PROGRESS_Draw (PROGRESS_INFO *infoPtr, HDC hdc)
324 const ProgressDrawProc* drawProcs;
325 ProgressDrawInfo pdi;
327 TRACE("(infoPtr=%p, hdc=%p)\n", infoPtr, hdc);
330 pdi.theme = GetWindowTheme (infoPtr->Self);
332 /* get the required bar brush */
333 if (infoPtr->ColorBar == CLR_DEFAULT)
334 pdi.hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
336 pdi.hbrBar = CreateSolidBrush (infoPtr->ColorBar);
338 if (infoPtr->ColorBk == CLR_DEFAULT)
339 pdi.hbrBk = GetSysColorBrush(COLOR_3DFACE);
341 pdi.hbrBk = CreateSolidBrush(infoPtr->ColorBk);
343 /* get the window style */
344 dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
346 /* get client rectangle */
347 GetClientRect (infoPtr->Self, &pdi.rect);
349 FrameRect( hdc, &pdi.rect, pdi.hbrBk );
350 InflateRect(&pdi.rect, -1, -1);
355 int part = (dwStyle & PBS_VERTICAL) ? PP_BARVERT : PP_BAR;
357 GetThemeBackgroundContentRect (pdi.theme, hdc, part, 0, &pdi.rect,
360 /* Exclude content rect - content background will be drawn later */
361 ExcludeClipRect (hdc, cntRect.left, cntRect.top,
362 cntRect.right, cntRect.bottom);
363 if (IsThemeBackgroundPartiallyTransparent (pdi.theme, part, 0))
364 DrawThemeParentBackground (infoPtr->Self, hdc, NULL);
365 DrawThemeBackground (pdi.theme, hdc, part, 0, &pdi.rect, NULL);
366 SelectClipRgn (hdc, NULL);
367 CopyRect (&pdi.rect, &cntRect);
370 /* compute some drawing parameters */
371 barSmooth = (dwStyle & PBS_SMOOTH) && !pdi.theme;
372 drawProcs = &((pdi.theme ? drawProcThemed : drawProcClassic)[(barSmooth ? 0 : 4)
373 + ((dwStyle & PBS_VERTICAL) ? 2 : 0)]);
374 barSize = get_bar_size( dwStyle, &pdi.rect );
377 GetWindowRect( infoPtr->Self, &pdi.bgRect );
378 MapWindowPoints( infoPtr->Self, 0, (POINT*)&pdi.bgRect, 2 );
382 pdi.ledW = get_led_size( infoPtr, dwStyle, &pdi.rect);
383 pdi.ledGap = get_led_gap( infoPtr );
385 if (dwStyle & PBS_MARQUEE)
387 const int ledW = !barSmooth ? (pdi.ledW + pdi.ledGap) : 1;
388 const int leds = (barSize + ledW - 1) / ledW;
389 const int ledMEnd = infoPtr->MarqueePos + MARQUEE_LEDS;
393 /* case 1: the marquee bar extends over the end and wraps around to
395 const int gapStart = max((ledMEnd - leds) * ledW, 0);
396 const int gapEnd = min(infoPtr->MarqueePos * ledW, barSize);
398 drawProcs[0]( &pdi, 0, gapStart);
399 drawProcs[1]( &pdi, gapStart, gapEnd);
400 drawProcs[0]( &pdi, gapEnd, barSize);
404 /* case 2: the marquee bar is between start and end */
405 const int barStart = infoPtr->MarqueePos * ledW;
406 const int barEnd = min (ledMEnd * ledW, barSize);
408 drawProcs[1]( &pdi, 0, barStart);
409 drawProcs[0]( &pdi, barStart, barEnd);
410 drawProcs[1]( &pdi, barEnd, barSize);
415 int barEnd = get_bar_position( infoPtr, dwStyle, &pdi.rect,
419 const int ledW = pdi.ledW + pdi.ledGap;
420 barEnd = min (((barEnd + ledW - 1) / ledW) * ledW, barSize);
422 drawProcs[0]( &pdi, 0, barEnd);
423 drawProcs[1]( &pdi, barEnd, barSize);
426 /* delete bar brush */
427 if (infoPtr->ColorBar != CLR_DEFAULT) DeleteObject (pdi.hbrBar);
428 if (infoPtr->ColorBk != CLR_DEFAULT) DeleteObject (pdi.hbrBk);
433 /***********************************************************************
435 * Draw the progress bar. The background need not be erased.
436 * If dc!=0, it draws on it
438 static LRESULT PROGRESS_Paint (PROGRESS_INFO *infoPtr, HDC hdc)
441 if (hdc) return PROGRESS_Draw (infoPtr, hdc);
442 hdc = BeginPaint (infoPtr->Self, &ps);
443 PROGRESS_Draw (infoPtr, hdc);
444 EndPaint (infoPtr->Self, &ps);
449 /***********************************************************************
451 * Handle the marquee timer messages
453 static LRESULT PROGRESS_Timer (PROGRESS_INFO *infoPtr, INT idTimer)
455 if(idTimer == ID_MARQUEE_TIMER)
457 LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
460 HTHEME theme = GetWindowTheme (infoPtr->Self);
461 BOOL barSmooth = (style & PBS_SMOOTH) && !theme;
463 get_client_rect (infoPtr->Self, &rect);
466 ledWidth = get_led_size( infoPtr, style, &rect ) +
467 get_led_gap( infoPtr );
471 leds = (get_bar_size( style, &rect ) + ledWidth - 1) /
474 /* increment the marquee progress */
475 if(++infoPtr->MarqueePos >= leds)
477 infoPtr->MarqueePos = 0;
480 InvalidateRect(infoPtr->Self, &rect, FALSE);
481 UpdateWindow(infoPtr->Self);
487 /***********************************************************************
489 * Makes sure the current position (CurVal) is within bounds.
491 static void PROGRESS_CoercePos(PROGRESS_INFO *infoPtr)
493 if(infoPtr->CurVal < infoPtr->MinVal)
494 infoPtr->CurVal = infoPtr->MinVal;
495 if(infoPtr->CurVal > infoPtr->MaxVal)
496 infoPtr->CurVal = infoPtr->MaxVal;
500 /***********************************************************************
502 * Set new Font for progress bar
504 static HFONT PROGRESS_SetFont (PROGRESS_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
506 HFONT hOldFont = infoPtr->Font;
507 infoPtr->Font = hFont;
508 /* Since infoPtr->Font is not used, there is no need for repaint */
512 static DWORD PROGRESS_SetRange (PROGRESS_INFO *infoPtr, int low, int high)
514 DWORD res = MAKELONG(LOWORD(infoPtr->MinVal), LOWORD(infoPtr->MaxVal));
516 /* if nothing changes, simply return */
517 if(infoPtr->MinVal == low && infoPtr->MaxVal == high) return res;
519 infoPtr->MinVal = low;
520 infoPtr->MaxVal = high;
521 PROGRESS_CoercePos(infoPtr);
522 InvalidateRect(infoPtr->Self, NULL, TRUE);
526 /***********************************************************************
529 static LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
530 WPARAM wParam, LPARAM lParam)
532 PROGRESS_INFO *infoPtr;
533 static const WCHAR themeClass[] = {'P','r','o','g','r','e','s','s',0};
536 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd, message, wParam, lParam);
538 infoPtr = (PROGRESS_INFO *)GetWindowLongPtrW(hwnd, 0);
540 if (!infoPtr && message != WM_CREATE)
541 return DefWindowProcW( hwnd, message, wParam, lParam );
546 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
548 theme = OpenThemeData (hwnd, themeClass);
550 dwExStyle &= ~(WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE);
551 if (!theme) dwExStyle |= WS_EX_STATICEDGE;
552 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
553 /* Force recalculation of a non-client area */
554 SetWindowPos(hwnd, 0, 0, 0, 0, 0,
555 SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
557 /* allocate memory for info struct */
558 infoPtr = Alloc (sizeof(PROGRESS_INFO));
559 if (!infoPtr) return -1;
560 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
562 /* initialize the info struct */
563 infoPtr->Self = hwnd;
565 infoPtr->MaxVal = 100;
568 infoPtr->MarqueePos = 0;
569 infoPtr->Marquee = FALSE;
570 infoPtr->ColorBar = CLR_DEFAULT;
571 infoPtr->ColorBk = CLR_DEFAULT;
574 TRACE("Progress Ctrl creation, hwnd=%p\n", hwnd);
579 TRACE("Progress Ctrl destruction, hwnd=%p\n", hwnd);
581 SetWindowLongPtrW(hwnd, 0, 0);
582 theme = GetWindowTheme (hwnd);
583 CloseThemeData (theme);
590 return (LRESULT)infoPtr->Font;
593 return (LRESULT)PROGRESS_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
597 return PROGRESS_Paint (infoPtr, (HDC)wParam);
600 return PROGRESS_Timer (infoPtr, (INT)wParam);
602 case WM_THEMECHANGED:
604 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
606 theme = GetWindowTheme (hwnd);
607 CloseThemeData (theme);
608 theme = OpenThemeData (hwnd, themeClass);
610 /* WS_EX_STATICEDGE disappears when the control is themed */
612 dwExStyle &= ~WS_EX_STATICEDGE;
614 dwExStyle |= WS_EX_STATICEDGE;
615 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
617 InvalidateRect (hwnd, NULL, FALSE);
624 oldVal = infoPtr->CurVal;
626 infoPtr->CurVal += (INT)wParam;
627 PROGRESS_CoercePos (infoPtr);
628 TRACE("PBM_DELTAPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
629 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
630 UpdateWindow( infoPtr->Self );
638 oldVal = infoPtr->CurVal;
639 if(oldVal != wParam) {
640 infoPtr->CurVal = (INT)wParam;
641 PROGRESS_CoercePos(infoPtr);
642 TRACE("PBM_SETPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
643 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
644 UpdateWindow( infoPtr->Self );
650 return PROGRESS_SetRange (infoPtr, (int)LOWORD(lParam), (int)HIWORD(lParam));
655 oldStep = infoPtr->Step;
656 infoPtr->Step = (INT)wParam;
661 return infoPtr->Step;
666 oldVal = infoPtr->CurVal;
667 infoPtr->CurVal += infoPtr->Step;
668 if(infoPtr->CurVal > infoPtr->MaxVal)
669 infoPtr->CurVal = infoPtr->MinVal;
670 if(oldVal != infoPtr->CurVal)
672 TRACE("PBM_STEPIT: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
673 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
674 UpdateWindow( infoPtr->Self );
680 return PROGRESS_SetRange (infoPtr, (int)wParam, (int)lParam);
684 ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
685 ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
687 return wParam ? infoPtr->MinVal : infoPtr->MaxVal;
690 return infoPtr->CurVal;
692 case PBM_SETBARCOLOR:
693 infoPtr->ColorBar = (COLORREF)lParam;
694 InvalidateRect(hwnd, NULL, TRUE);
697 case PBM_GETBARCOLOR:
698 return infoPtr->ColorBar;
701 infoPtr->ColorBk = (COLORREF)lParam;
702 InvalidateRect(hwnd, NULL, TRUE);
706 return infoPtr->ColorBk;
709 if(wParam != PBST_NORMAL)
710 FIXME("state %04lx not yet handled\n", wParam);
719 infoPtr->Marquee = TRUE;
720 SetTimer(infoPtr->Self, ID_MARQUEE_TIMER, (UINT)lParam, NULL);
724 infoPtr->Marquee = FALSE;
725 KillTimer(infoPtr->Self, ID_MARQUEE_TIMER);
727 return infoPtr->Marquee;
730 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
731 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam );
732 return DefWindowProcW( hwnd, message, wParam, lParam );
737 /***********************************************************************
738 * PROGRESS_Register [Internal]
740 * Registers the progress bar window class.
742 void PROGRESS_Register (void)
746 ZeroMemory (&wndClass, sizeof(wndClass));
747 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
748 wndClass.lpfnWndProc = (WNDPROC)ProgressWindowProc;
749 wndClass.cbClsExtra = 0;
750 wndClass.cbWndExtra = sizeof (PROGRESS_INFO *);
751 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
752 wndClass.lpszClassName = PROGRESS_CLASSW;
754 RegisterClassW (&wndClass);
758 /***********************************************************************
759 * PROGRESS_Unregister [Internal]
761 * Unregisters the progress bar window class.
763 void PROGRESS_Unregister (void)
765 UnregisterClassW (PROGRESS_CLASSW, NULL);