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.
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(progress);
49 HWND Self; /* The window handle for this control */
50 INT CurVal; /* Current progress value */
51 INT MinVal; /* Minimum progress value */
52 INT MaxVal; /* Maximum progress value */
53 INT Step; /* Step to use on PMB_STEPIT */
54 INT MarqueePos; /* Marquee animation position */
55 BOOL Marquee; /* Whether the marquee animation is enabled */
56 COLORREF ColorBar; /* Bar color */
57 COLORREF ColorBk; /* Background color */
58 HFONT Font; /* Handle to font (not unused) */
61 /* Control configuration constants */
64 #define MARQUEE_LEDS 5
65 #define ID_MARQUEE_TIMER 1
67 /* Helper to obtain size of a progress bar chunk ("led"). */
68 static inline int get_led_size ( PROGRESS_INFO *infoPtr, LONG style,
71 HTHEME theme = GetWindowTheme (infoPtr->Self);
75 if (SUCCEEDED( GetThemeInt( theme, 0, 0, TMT_PROGRESSCHUNKSIZE, &chunkSize )))
79 if (style & PBS_VERTICAL)
80 return MulDiv (rect->right - rect->left, 2, 3);
82 return MulDiv (rect->bottom - rect->top, 2, 3);
85 /* Helper to obtain gap between progress bar chunks */
86 static inline int get_led_gap ( PROGRESS_INFO *infoPtr )
88 HTHEME theme = GetWindowTheme (infoPtr->Self);
92 if (SUCCEEDED( GetThemeInt( theme, 0, 0, TMT_PROGRESSSPACESIZE, &spaceSize )))
99 /* Get client rect. Takes into account that theming needs no adjustment. */
100 static inline void get_client_rect (HWND hwnd, RECT* rect)
102 HTHEME theme = GetWindowTheme (hwnd);
103 GetClientRect (hwnd, rect);
105 InflateRect(rect, -1, -1);
108 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
109 int part = (dwStyle & PBS_VERTICAL) ? PP_BARVERT : PP_BAR;
110 GetThemeBackgroundContentRect (theme, 0, part, 0, rect, rect);
114 /* Compute the extend of the bar */
115 static inline int get_bar_size( LONG style, const RECT* rect )
117 if (style & PBS_VERTICAL)
118 return rect->bottom - rect->top;
120 return rect->right - rect->left;
123 /* Compute the pixel position of a progress value */
124 static inline int get_bar_position( PROGRESS_INFO *infoPtr, LONG style,
125 const RECT* rect, INT value )
127 return MulDiv (value - infoPtr->MinVal, get_bar_size (style, rect),
128 infoPtr->MaxVal - infoPtr->MinVal);
131 /***********************************************************************
132 * PROGRESS_Invalidate
134 * Invalide the range between old and new pos.
136 static void PROGRESS_Invalidate( PROGRESS_INFO *infoPtr, INT old, INT new )
138 LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
141 BOOL barSmooth = (style & PBS_SMOOTH) && !GetWindowTheme (infoPtr->Self);
143 get_client_rect( infoPtr->Self, &rect );
145 oldPos = get_bar_position( infoPtr, style, &rect, old );
146 newPos = get_bar_position( infoPtr, style, &rect, new );
148 if (style & PBS_VERTICAL)
150 rect.top = rect.bottom - max( oldPos, newPos );
151 rect.bottom = rect.bottom - min( oldPos, newPos );
152 if (!barSmooth) rect.top -=
153 get_led_size (infoPtr, style, &rect) + get_led_gap (infoPtr);
157 rect.left = min( oldPos, newPos );
158 rect.right = max( oldPos, newPos );
159 if (!barSmooth) rect.right +=
160 get_led_size (infoPtr, style, &rect) + get_led_gap (infoPtr);
162 InvalidateRect( infoPtr->Self, &rect, oldPos > newPos );
165 /* Information for a progress bar drawing helper */
166 typedef struct tagProgressDrawInfo
177 typedef void (*ProgressDrawProc)(const ProgressDrawInfo* di, int start, int end);
179 /* draw solid horizontal bar from 'start' to 'end' */
180 static void draw_solid_bar_H (const ProgressDrawInfo* di, int start, int end)
183 r.left = di->rect.left + start;
184 r.top = di->rect.top;
185 r.right = di->rect.left + end;
186 r.bottom = di->rect.bottom;
187 FillRect (di->hdc, &r, di->hbrBar);
190 /* draw solid horizontal background from 'start' to 'end' */
191 static void draw_solid_bkg_H (const ProgressDrawInfo* di, int start, int end)
194 r.left = di->rect.left + start;
195 r.top = di->rect.top;
196 r.right = di->rect.left + end;
197 r.bottom = di->rect.bottom;
198 FillRect (di->hdc, &r, di->hbrBk);
201 /* draw solid vertical bar from 'start' to 'end' */
202 static void draw_solid_bar_V (const ProgressDrawInfo* di, int start, int end)
205 r.left = di->rect.left;
206 r.top = di->rect.bottom - end;
207 r.right = di->rect.right;
208 r.bottom = di->rect.bottom - start;
209 FillRect (di->hdc, &r, di->hbrBar);
212 /* draw solid vertical background from 'start' to 'end' */
213 static void draw_solid_bkg_V (const ProgressDrawInfo* di, int start, int end)
216 r.left = di->rect.left;
217 r.top = di->rect.bottom - end;
218 r.right = di->rect.right;
219 r.bottom = di->rect.bottom - start;
220 FillRect (di->hdc, &r, di->hbrBk);
223 /* draw chunky horizontal bar from 'start' to 'end' */
224 static void draw_chunk_bar_H (const ProgressDrawInfo* di, int start, int end)
227 int right = di->rect.left + end;
228 r.left = di->rect.left + start;
229 r.top = di->rect.top;
230 r.bottom = di->rect.bottom;
231 while (r.left < right)
233 r.right = min (r.left + di->ledW, right);
234 FillRect (di->hdc, &r, di->hbrBar);
236 r.right = min (r.left + di->ledGap, right);
237 FillRect (di->hdc, &r, di->hbrBk);
242 /* draw chunky vertical bar from 'start' to 'end' */
243 static void draw_chunk_bar_V (const ProgressDrawInfo* di, int start, int end)
246 int top = di->rect.bottom - end;
247 r.left = di->rect.left;
248 r.right = di->rect.right;
249 r.bottom = di->rect.bottom - start;
250 while (r.bottom > top)
252 r.top = max (r.bottom - di->ledW, top);
253 FillRect (di->hdc, &r, di->hbrBar);
255 r.top = max (r.bottom - di->ledGap, top);
256 FillRect (di->hdc, &r, di->hbrBk);
261 /* drawing functions for "classic" style */
262 static const ProgressDrawProc drawProcClassic[8] = {
265 draw_solid_bar_H, draw_solid_bkg_H,
267 draw_solid_bar_V, draw_solid_bkg_V,
270 draw_chunk_bar_H, draw_solid_bkg_H,
272 draw_chunk_bar_V, draw_solid_bkg_V,
275 /* draw themed horizontal bar from 'start' to 'end' */
276 static void draw_theme_bar_H (const ProgressDrawInfo* di, int start, int end)
279 int right = di->rect.left + end;
280 r.left = di->rect.left + start;
281 r.top = di->rect.top;
282 r.bottom = di->rect.bottom;
283 while (r.left < right)
285 r.right = min (r.left + di->ledW, right);
286 DrawThemeBackground (di->theme, di->hdc, PP_CHUNK, 0, &r, NULL);
288 r.right = min (r.left + di->ledGap, right);
289 DrawThemeBackground (di->theme, di->hdc, PP_BAR, 0, &di->bgRect, &r);
294 /* draw themed horizontal bar from 'start' to 'end' */
295 static void draw_theme_bar_V (const ProgressDrawInfo* di, int start, int end)
298 int top = di->rect.bottom - end;
299 r.left = di->rect.left;
300 r.right = di->rect.right;
301 r.bottom = di->rect.bottom - start;
302 while (r.bottom > top)
304 r.top = max (r.bottom - di->ledW, top);
305 DrawThemeBackground (di->theme, di->hdc, PP_CHUNKVERT, 0, &r, NULL);
307 r.top = max (r.bottom - di->ledGap, top);
308 DrawThemeBackground (di->theme, di->hdc, PP_BARVERT, 0, &di->bgRect, &r);
313 /* draw themed horizontal background from 'start' to 'end' */
314 static void draw_theme_bkg_H (const ProgressDrawInfo* di, int start, int end)
317 r.left = di->rect.left + start;
318 r.top = di->rect.top;
319 r.right = di->rect.left + end;
320 r.bottom = di->rect.bottom;
321 DrawThemeBackground (di->theme, di->hdc, PP_BAR, 0, &di->bgRect, &r);
324 /* draw themed vertical background from 'start' to 'end' */
325 static void draw_theme_bkg_V (const ProgressDrawInfo* di, int start, int end)
328 r.left = di->rect.left;
329 r.top = di->rect.bottom - end;
330 r.right = di->rect.right;
331 r.bottom = di->rect.bottom - start;
332 DrawThemeBackground (di->theme, di->hdc, PP_BARVERT, 0, &di->bgRect, &r);
335 /* drawing functions for themed style */
336 static const ProgressDrawProc drawProcThemed[8] = {
339 draw_theme_bar_H, draw_theme_bkg_H,
341 draw_theme_bar_V, draw_theme_bkg_V,
344 draw_theme_bar_H, draw_theme_bkg_H,
346 draw_theme_bar_V, draw_theme_bkg_V,
349 /***********************************************************************
351 * Draws the progress bar.
353 static LRESULT PROGRESS_Draw (PROGRESS_INFO *infoPtr, HDC hdc)
358 const ProgressDrawProc* drawProcs;
359 ProgressDrawInfo pdi;
361 TRACE("(infoPtr=%p, hdc=%p)\n", infoPtr, hdc);
364 pdi.theme = GetWindowTheme (infoPtr->Self);
366 /* get the required bar brush */
367 if (infoPtr->ColorBar == CLR_DEFAULT)
368 pdi.hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
370 pdi.hbrBar = CreateSolidBrush (infoPtr->ColorBar);
372 if (infoPtr->ColorBk == CLR_DEFAULT)
373 pdi.hbrBk = GetSysColorBrush(COLOR_3DFACE);
375 pdi.hbrBk = CreateSolidBrush(infoPtr->ColorBk);
377 /* get the window style */
378 dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
380 /* get client rectangle */
381 GetClientRect (infoPtr->Self, &pdi.rect);
383 FrameRect( hdc, &pdi.rect, pdi.hbrBk );
384 InflateRect(&pdi.rect, -1, -1);
389 int part = (dwStyle & PBS_VERTICAL) ? PP_BARVERT : PP_BAR;
391 GetThemeBackgroundContentRect (pdi.theme, hdc, part, 0, &pdi.rect,
394 /* Exclude content rect - content background will be drawn later */
395 ExcludeClipRect (hdc, cntRect.left, cntRect.top,
396 cntRect.right, cntRect.bottom);
397 if (IsThemeBackgroundPartiallyTransparent (pdi.theme, part, 0))
398 DrawThemeParentBackground (infoPtr->Self, hdc, NULL);
399 DrawThemeBackground (pdi.theme, hdc, part, 0, &pdi.rect, NULL);
400 SelectClipRgn (hdc, NULL);
401 CopyRect (&pdi.rect, &cntRect);
404 /* compute some drawing parameters */
405 barSmooth = (dwStyle & PBS_SMOOTH) && !pdi.theme;
406 drawProcs = &((pdi.theme ? drawProcThemed : drawProcClassic)[(barSmooth ? 0 : 4)
407 + ((dwStyle & PBS_VERTICAL) ? 2 : 0)]);
408 barSize = get_bar_size( dwStyle, &pdi.rect );
411 GetWindowRect( infoPtr->Self, &pdi.bgRect );
412 ScreenToClient( infoPtr->Self, (POINT*)&pdi.bgRect );
413 ScreenToClient( infoPtr->Self, (POINT*)&pdi.bgRect.right );
417 pdi.ledW = get_led_size( infoPtr, dwStyle, &pdi.rect);
418 pdi.ledGap = get_led_gap( infoPtr );
420 if (dwStyle & PBS_MARQUEE)
422 const int ledW = !barSmooth ? (pdi.ledW + pdi.ledGap) : 1;
423 const int leds = (barSize + ledW - 1) / ledW;
424 const int ledMEnd = infoPtr->MarqueePos + MARQUEE_LEDS;
428 /* case 1: the marquee bar extends over the end and wraps around to
430 const int gapStart = max((ledMEnd - leds) * ledW, 0);
431 const int gapEnd = min(infoPtr->MarqueePos * ledW, barSize);
433 drawProcs[0]( &pdi, 0, gapStart);
434 drawProcs[1]( &pdi, gapStart, gapEnd);
435 drawProcs[0]( &pdi, gapEnd, barSize);
439 /* case 2: the marquee bar is between start and end */
440 const int barStart = infoPtr->MarqueePos * ledW;
441 const int barEnd = min (ledMEnd * ledW, barSize);
443 drawProcs[1]( &pdi, 0, barStart);
444 drawProcs[0]( &pdi, barStart, barEnd);
445 drawProcs[1]( &pdi, barEnd, barSize);
450 int barEnd = get_bar_position( infoPtr, dwStyle, &pdi.rect,
454 const int ledW = pdi.ledW + pdi.ledGap;
455 barEnd = min (((barEnd + ledW - 1) / ledW) * ledW, barSize);
457 drawProcs[0]( &pdi, 0, barEnd);
458 drawProcs[1]( &pdi, barEnd, barSize);
461 /* delete bar brush */
462 if (infoPtr->ColorBar != CLR_DEFAULT) DeleteObject (pdi.hbrBar);
463 if (infoPtr->ColorBk != CLR_DEFAULT) DeleteObject (pdi.hbrBk);
468 /***********************************************************************
470 * Draw the progress bar. The background need not be erased.
471 * If dc!=0, it draws on it
473 static LRESULT PROGRESS_Paint (PROGRESS_INFO *infoPtr, HDC hdc)
476 if (hdc) return PROGRESS_Draw (infoPtr, hdc);
477 hdc = BeginPaint (infoPtr->Self, &ps);
478 PROGRESS_Draw (infoPtr, hdc);
479 EndPaint (infoPtr->Self, &ps);
484 /***********************************************************************
486 * Handle the marquee timer messages
488 static LRESULT PROGRESS_Timer (PROGRESS_INFO *infoPtr, INT idTimer)
490 if(idTimer == ID_MARQUEE_TIMER)
492 LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
495 HTHEME theme = GetWindowTheme (infoPtr->Self);
496 BOOL barSmooth = (style & PBS_SMOOTH) && !theme;
498 get_client_rect (infoPtr->Self, &rect);
501 ledWidth = get_led_size( infoPtr, style, &rect ) +
502 get_led_gap( infoPtr );
506 leds = (get_bar_size( style, &rect ) + ledWidth - 1) /
509 /* increment the marquee progress */
510 if(++infoPtr->MarqueePos >= leds)
512 infoPtr->MarqueePos = 0;
515 InvalidateRect(infoPtr->Self, &rect, FALSE);
521 /***********************************************************************
523 * Makes sure the current position (CurVal) is within bounds.
525 static void PROGRESS_CoercePos(PROGRESS_INFO *infoPtr)
527 if(infoPtr->CurVal < infoPtr->MinVal)
528 infoPtr->CurVal = infoPtr->MinVal;
529 if(infoPtr->CurVal > infoPtr->MaxVal)
530 infoPtr->CurVal = infoPtr->MaxVal;
534 /***********************************************************************
536 * Set new Font for progress bar
538 static HFONT PROGRESS_SetFont (PROGRESS_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
540 HFONT hOldFont = infoPtr->Font;
541 infoPtr->Font = hFont;
542 /* Since infoPtr->Font is not used, there is no need for repaint */
546 static DWORD PROGRESS_SetRange (PROGRESS_INFO *infoPtr, int low, int high)
548 DWORD res = MAKELONG(LOWORD(infoPtr->MinVal), LOWORD(infoPtr->MaxVal));
550 /* if nothing changes, simply return */
551 if(infoPtr->MinVal == low && infoPtr->MaxVal == high) return res;
553 infoPtr->MinVal = low;
554 infoPtr->MaxVal = high;
555 PROGRESS_CoercePos(infoPtr);
556 InvalidateRect(infoPtr->Self, NULL, TRUE);
560 /***********************************************************************
563 static LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
564 WPARAM wParam, LPARAM lParam)
566 PROGRESS_INFO *infoPtr;
567 static const WCHAR themeClass[] = {'P','r','o','g','r','e','s','s',0};
570 TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
572 infoPtr = (PROGRESS_INFO *)GetWindowLongPtrW(hwnd, 0);
574 if (!infoPtr && message != WM_CREATE)
575 return DefWindowProcW( hwnd, message, wParam, lParam );
580 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
582 theme = OpenThemeData (hwnd, themeClass);
584 dwExStyle &= ~(WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE);
585 if (!theme) dwExStyle |= WS_EX_STATICEDGE;
586 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
587 /* Force recalculation of a non-client area */
588 SetWindowPos(hwnd, 0, 0, 0, 0, 0,
589 SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
591 /* allocate memory for info struct */
592 infoPtr = (PROGRESS_INFO *)Alloc (sizeof(PROGRESS_INFO));
593 if (!infoPtr) return -1;
594 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
596 /* initialize the info struct */
597 infoPtr->Self = hwnd;
599 infoPtr->MaxVal = 100;
602 infoPtr->MarqueePos = 0;
603 infoPtr->Marquee = FALSE;
604 infoPtr->ColorBar = CLR_DEFAULT;
605 infoPtr->ColorBk = CLR_DEFAULT;
608 TRACE("Progress Ctrl creation, hwnd=%p\n", hwnd);
613 TRACE("Progress Ctrl destruction, hwnd=%p\n", hwnd);
615 SetWindowLongPtrW(hwnd, 0, 0);
616 theme = GetWindowTheme (hwnd);
617 CloseThemeData (theme);
624 return (LRESULT)infoPtr->Font;
627 return (LRESULT)PROGRESS_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
630 return PROGRESS_Paint (infoPtr, (HDC)wParam);
633 return PROGRESS_Timer (infoPtr, (INT)wParam);
635 case WM_THEMECHANGED:
637 DWORD dwExStyle = GetWindowLongW (hwnd, GWL_EXSTYLE);
639 theme = GetWindowTheme (hwnd);
640 CloseThemeData (theme);
641 theme = OpenThemeData (hwnd, themeClass);
643 /* WS_EX_STATICEDGE disappears when the control is themed */
645 dwExStyle &= ~WS_EX_STATICEDGE;
647 dwExStyle |= WS_EX_STATICEDGE;
648 SetWindowLongW (hwnd, GWL_EXSTYLE, dwExStyle);
650 InvalidateRect (hwnd, NULL, FALSE);
657 oldVal = infoPtr->CurVal;
659 infoPtr->CurVal += (INT)wParam;
660 PROGRESS_CoercePos (infoPtr);
661 TRACE("PBM_DELTAPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
662 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
670 oldVal = infoPtr->CurVal;
671 if(oldVal != wParam) {
672 infoPtr->CurVal = (INT)wParam;
673 PROGRESS_CoercePos(infoPtr);
674 TRACE("PBM_SETPOS: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
675 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
681 return PROGRESS_SetRange (infoPtr, (int)LOWORD(lParam), (int)HIWORD(lParam));
686 oldStep = infoPtr->Step;
687 infoPtr->Step = (INT)wParam;
694 oldVal = infoPtr->CurVal;
695 infoPtr->CurVal += infoPtr->Step;
696 if(infoPtr->CurVal > infoPtr->MaxVal)
697 infoPtr->CurVal = infoPtr->MinVal;
698 if(oldVal != infoPtr->CurVal)
700 TRACE("PBM_STEPIT: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
701 PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
707 return PROGRESS_SetRange (infoPtr, (int)wParam, (int)lParam);
711 ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
712 ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
714 return wParam ? infoPtr->MinVal : infoPtr->MaxVal;
717 return infoPtr->CurVal;
719 case PBM_SETBARCOLOR:
720 infoPtr->ColorBar = (COLORREF)lParam;
721 InvalidateRect(hwnd, NULL, TRUE);
725 infoPtr->ColorBk = (COLORREF)lParam;
726 InvalidateRect(hwnd, NULL, TRUE);
732 infoPtr->Marquee = TRUE;
733 SetTimer(infoPtr->Self, ID_MARQUEE_TIMER, (UINT)lParam, NULL);
737 infoPtr->Marquee = FALSE;
738 KillTimer(infoPtr->Self, ID_MARQUEE_TIMER);
740 return infoPtr->Marquee;
743 if ((message >= WM_USER) && (message < WM_APP))
744 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
745 return DefWindowProcW( hwnd, message, wParam, lParam );
750 /***********************************************************************
751 * PROGRESS_Register [Internal]
753 * Registers the progress bar window class.
755 void PROGRESS_Register (void)
759 ZeroMemory (&wndClass, sizeof(wndClass));
760 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
761 wndClass.lpfnWndProc = (WNDPROC)ProgressWindowProc;
762 wndClass.cbClsExtra = 0;
763 wndClass.cbWndExtra = sizeof (PROGRESS_INFO *);
764 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
765 wndClass.lpszClassName = PROGRESS_CLASSW;
767 RegisterClassW (&wndClass);
771 /***********************************************************************
772 * PROGRESS_Unregister [Internal]
774 * Unregisters the progress bar window class.
776 void PROGRESS_Unregister (void)
778 UnregisterClassW (PROGRESS_CLASSW, NULL);