Add theming support for the tab control.
[wine] / dlls / comctl32 / progress.c
1 /*
2  * Progress control
3  *
4  * Copyright 1997, 2002 Dimitrie O. Paun
5  * Copyright 1998, 1999 Eric Kohl
6  *
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.
11  *
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.
16  *
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
20  *
21  * NOTE
22  * 
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.
25  * 
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.
29  *
30  */
31
32 #include <stdarg.h>
33 #include <string.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winnls.h"
39 #include "commctrl.h"
40 #include "comctl32.h"
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(progress);
44
45 typedef struct
46 {
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) */
57 } PROGRESS_INFO;
58
59 /* Control configuration constants */
60
61 #define LED_GAP           2
62 #define MARQUEE_LEDS      5
63 #define ID_MARQUEE_TIMER  1
64
65 /* Helper to compute size of a progress bar chunk ("led"). */
66 static inline int get_led_size ( PROGRESS_INFO *infoPtr, LONG style,
67                                  const RECT* rect)
68 {
69     if (style & PBS_VERTICAL)
70         return MulDiv (rect->right - rect->left, 2, 3);
71     else
72         return MulDiv (rect->bottom - rect->top, 2, 3);
73 }
74
75 /* Compute the extend of the bar */
76 static inline int get_bar_size( LONG style, const RECT* rect )
77 {
78     if (style & PBS_VERTICAL)
79         return rect->bottom - rect->top;
80     else
81         return rect->right - rect->left;
82 }
83
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 )
87 {
88     return MulDiv (value - infoPtr->MinVal, get_bar_size (style, rect),
89                       infoPtr->MaxVal - infoPtr->MinVal);
90 }
91
92 /***********************************************************************
93  * PROGRESS_Invalidate
94  *
95  * Invalide the range between old and new pos.
96  */
97 static void PROGRESS_Invalidate( PROGRESS_INFO *infoPtr, INT old, INT new )
98 {
99     LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
100     RECT rect;
101     int oldPos, newPos;
102
103     GetClientRect (infoPtr->Self, &rect);
104     InflateRect(&rect, -1, -1);
105
106     oldPos = get_bar_position( infoPtr, style, &rect, old );
107     newPos = get_bar_position( infoPtr, style, &rect, new );
108
109     if (style & PBS_VERTICAL)
110     {
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);
115     }
116     else
117     {
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);
122     }
123     InvalidateRect( infoPtr->Self, &rect, oldPos > newPos );
124 }
125
126 /* Information for a progress bar drawing helper */
127 typedef struct tagProgressDrawInfo
128 {
129     HDC hdc;
130     RECT rect;
131     HBRUSH hbrBar;
132     HBRUSH hbrBk;
133     int ledW;
134 } ProgressDrawInfo;
135
136 typedef void (*ProgressDrawProc)(const ProgressDrawInfo* di, int start, int end);
137
138 /* draw solid horizontal bar from 'start' to 'end' */
139 static void draw_solid_bar_H (const ProgressDrawInfo* di, int start, int end)
140 {
141     RECT r;
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);
147 }
148
149 /* draw solid horizontal background from 'start' to 'end' */
150 static void draw_solid_bkg_H (const ProgressDrawInfo* di, int start, int end)
151 {
152     RECT r;
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);
158 }
159
160 /* draw solid vertical bar from 'start' to 'end' */
161 static void draw_solid_bar_V (const ProgressDrawInfo* di, int start, int end)
162 {
163     RECT r;
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);
169 }
170
171 /* draw solid vertical background from 'start' to 'end' */
172 static void draw_solid_bkg_V (const ProgressDrawInfo* di, int start, int end)
173 {
174     RECT r;
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);
180 }
181
182 /* draw chunky horizontal bar from 'start' to 'end' */
183 static void draw_chunk_bar_H (const ProgressDrawInfo* di, int start, int end)
184 {
185     RECT r;
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)
191     {
192         r.right = min (r.left + di->ledW, right);
193         FillRect (di->hdc, &r, di->hbrBar);
194         r.left = r.right;
195         r.right = min (r.left + LED_GAP, right);
196         FillRect (di->hdc, &r, di->hbrBk);
197         r.left = r.right;
198     }
199 }
200
201 /* draw chunky vertical bar from 'start' to 'end' */
202 static void draw_chunk_bar_V (const ProgressDrawInfo* di, int start, int end)
203 {
204     RECT r;
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)
210     {
211         r.top = max (r.bottom - di->ledW, top);
212         FillRect (di->hdc, &r, di->hbrBar);
213         r.bottom = r.top;
214         r.top = max (r.bottom - LED_GAP, top);
215         FillRect (di->hdc, &r, di->hbrBk);
216         r.bottom = r.top;
217     }
218 }
219
220 /* drawing functions for "classic" style */
221 static const ProgressDrawProc drawProcClassic[8] = {
222   /* Smooth */
223     /* Horizontal */
224     draw_solid_bar_H, draw_solid_bkg_H,
225     /* Vertical */
226     draw_solid_bar_V, draw_solid_bkg_V,
227   /* Chunky */
228     /* Horizontal */
229     draw_chunk_bar_H, draw_solid_bkg_H,
230     /* Vertical */
231     draw_chunk_bar_V, draw_solid_bkg_V,
232 };
233
234 /***********************************************************************
235  * PROGRESS_Draw
236  * Draws the progress bar.
237  */
238 static LRESULT PROGRESS_Draw (PROGRESS_INFO *infoPtr, HDC hdc)
239 {
240     int barSize;
241     DWORD dwStyle;
242     BOOL barSmooth;
243     const ProgressDrawProc* drawProcs;
244     ProgressDrawInfo pdi;
245
246     TRACE("(infoPtr=%p, hdc=%p)\n", infoPtr, hdc);
247
248     pdi.hdc = hdc;
249
250     /* get the required bar brush */
251     if (infoPtr->ColorBar == CLR_DEFAULT)
252         pdi.hbrBar = GetSysColorBrush(COLOR_HIGHLIGHT);
253     else
254         pdi.hbrBar = CreateSolidBrush (infoPtr->ColorBar);
255
256     if (infoPtr->ColorBk == CLR_DEFAULT)
257         pdi.hbrBk = GetSysColorBrush(COLOR_3DFACE);
258     else
259         pdi.hbrBk = CreateSolidBrush(infoPtr->ColorBk);
260
261     /* get the window style */
262     dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
263
264     /* get client rectangle */
265     GetClientRect (infoPtr->Self, &pdi.rect);
266     FrameRect( hdc, &pdi.rect, pdi.hbrBk );
267     InflateRect(&pdi.rect, -1, -1);
268
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 );
274
275     if (!barSmooth)
276         pdi.ledW = get_led_size( infoPtr, dwStyle, &pdi.rect);
277
278     /* now draw the bar */
279     if (dwStyle & PBS_MARQUEE)
280     {
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;
284
285         if (ledMEnd > leds)
286         {
287             /* case 1: the marquee bar extends over the end and wraps around to 
288              * the start */
289             const int gapStart = max((ledMEnd - leds) * ledW, 0);
290             const int gapEnd = min(infoPtr->MarqueePos * ledW, barSize);
291
292             drawProcs[0]( &pdi, 0, gapStart);
293             drawProcs[1]( &pdi, gapStart, gapEnd);
294             drawProcs[0]( &pdi, gapEnd, barSize);
295         }
296         else
297         {
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);
301
302             drawProcs[1]( &pdi, 0, barStart);
303             drawProcs[0]( &pdi, barStart, barEnd);
304             drawProcs[1]( &pdi, barEnd, barSize);
305         }
306     }
307     else
308     {
309         int barEnd = get_bar_position( infoPtr, dwStyle, &pdi.rect,
310             infoPtr->CurVal);
311         if (!barSmooth)
312         {
313             const int ledW = pdi.ledW + LED_GAP;
314             barEnd = min (((barEnd + ledW - 1) / ledW) * ledW, barSize);
315         }
316         drawProcs[0]( &pdi, 0, barEnd);
317         drawProcs[1]( &pdi, barEnd, barSize);
318     }
319
320     /* delete bar brush */
321     if (infoPtr->ColorBar != CLR_DEFAULT) DeleteObject (pdi.hbrBar);
322     if (infoPtr->ColorBk != CLR_DEFAULT) DeleteObject (pdi.hbrBk);
323
324     return 0;
325 }
326
327
328 /***********************************************************************
329  * PROGRESS_Paint
330  * Draw the progress bar. The background need not be erased.
331  * If dc!=0, it draws on it
332  */
333 static LRESULT PROGRESS_Paint (PROGRESS_INFO *infoPtr, HDC hdc)
334 {
335     PAINTSTRUCT ps;
336     if (hdc) return PROGRESS_Draw (infoPtr, hdc);
337     hdc = BeginPaint (infoPtr->Self, &ps);
338     PROGRESS_Draw (infoPtr, hdc);
339     EndPaint (infoPtr->Self, &ps);
340     return 0;
341 }
342
343
344 /***********************************************************************
345  * PROGRESS_Timer
346  * Handle the marquee timer messages
347  */
348 static LRESULT PROGRESS_Timer (PROGRESS_INFO *infoPtr, INT idTimer)
349 {
350     if(idTimer == ID_MARQUEE_TIMER)
351     {
352         LONG style = GetWindowLongW (infoPtr->Self, GWL_STYLE);
353         RECT rect;
354         int ledWidth, leds;
355
356         GetClientRect (infoPtr->Self, &rect);
357
358         if(!(style & PBS_SMOOTH))
359             ledWidth = get_led_size( infoPtr, style, &rect ) + LED_GAP;
360         else
361             ledWidth = 1;
362
363         leds = (get_bar_size( style, &rect ) + ledWidth - 1) / 
364             ledWidth;
365
366         /* increment the marquee progress */
367         if(++infoPtr->MarqueePos > leds)
368         {
369             infoPtr->MarqueePos = 0;
370         }
371
372         InvalidateRect(infoPtr->Self, &rect, FALSE);
373     }
374     return 0;
375 }
376
377
378 /***********************************************************************
379  *           PROGRESS_CoercePos
380  * Makes sure the current position (CurVal) is within bounds.
381  */
382 static void PROGRESS_CoercePos(PROGRESS_INFO *infoPtr)
383 {
384     if(infoPtr->CurVal < infoPtr->MinVal)
385         infoPtr->CurVal = infoPtr->MinVal;
386     if(infoPtr->CurVal > infoPtr->MaxVal)
387         infoPtr->CurVal = infoPtr->MaxVal;
388 }
389
390
391 /***********************************************************************
392  *           PROGRESS_SetFont
393  * Set new Font for progress bar
394  */
395 static HFONT PROGRESS_SetFont (PROGRESS_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
396 {
397     HFONT hOldFont = infoPtr->Font;
398     infoPtr->Font = hFont;
399     /* Since infoPtr->Font is not used, there is no need for repaint */
400     return hOldFont;
401 }
402
403 static DWORD PROGRESS_SetRange (PROGRESS_INFO *infoPtr, int low, int high)
404 {
405     DWORD res = MAKELONG(LOWORD(infoPtr->MinVal), LOWORD(infoPtr->MaxVal));
406
407     /* if nothing changes, simply return */
408     if(infoPtr->MinVal == low && infoPtr->MaxVal == high) return res;
409
410     infoPtr->MinVal = low;
411     infoPtr->MaxVal = high;
412     PROGRESS_CoercePos(infoPtr);
413     InvalidateRect(infoPtr->Self, NULL, TRUE);
414     return res;
415 }
416
417 /***********************************************************************
418  *           ProgressWindowProc
419  */
420 static LRESULT WINAPI ProgressWindowProc(HWND hwnd, UINT message,
421                                          WPARAM wParam, LPARAM lParam)
422 {
423     PROGRESS_INFO *infoPtr;
424
425     TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
426
427     infoPtr = (PROGRESS_INFO *)GetWindowLongPtrW(hwnd, 0);
428
429     if (!infoPtr && message != WM_CREATE)
430         return DefWindowProcW( hwnd, message, wParam, lParam );
431
432     switch(message) {
433     case WM_CREATE:
434     {
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);
442
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);
447
448         /* initialize the info struct */
449         infoPtr->Self = hwnd;
450         infoPtr->MinVal = 0;
451         infoPtr->MaxVal = 100;
452         infoPtr->CurVal = 0;
453         infoPtr->Step = 10;
454         infoPtr->MarqueePos = 0;
455         infoPtr->Marquee = FALSE;
456         infoPtr->ColorBar = CLR_DEFAULT;
457         infoPtr->ColorBk = CLR_DEFAULT;
458         infoPtr->Font = 0;
459         TRACE("Progress Ctrl creation, hwnd=%p\n", hwnd);
460         return 0;
461     }
462
463     case WM_DESTROY:
464         TRACE("Progress Ctrl destruction, hwnd=%p\n", hwnd);
465         Free (infoPtr);
466         SetWindowLongPtrW(hwnd, 0, 0);
467         return 0;
468
469     case WM_GETFONT:
470         return (LRESULT)infoPtr->Font;
471
472     case WM_SETFONT:
473         return (LRESULT)PROGRESS_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
474
475     case WM_PAINT:
476         return PROGRESS_Paint (infoPtr, (HDC)wParam);
477
478     case WM_TIMER:
479         return PROGRESS_Timer (infoPtr, (INT)wParam);
480
481     case PBM_DELTAPOS:
482     {
483         INT oldVal;
484         oldVal = infoPtr->CurVal;
485         if(wParam != 0) {
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 );
490         }
491         return oldVal;
492     }
493
494     case PBM_SETPOS:
495     {
496         UINT oldVal;
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 );
503         }
504         return oldVal;
505     }
506
507     case PBM_SETRANGE:
508         return PROGRESS_SetRange (infoPtr, (int)LOWORD(lParam), (int)HIWORD(lParam));
509
510     case PBM_SETSTEP:
511     {
512         INT oldStep;
513         oldStep = infoPtr->Step;
514         infoPtr->Step = (INT)wParam;
515         return oldStep;
516     }
517
518     case PBM_STEPIT:
519     {
520         INT oldVal;
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)
526         {
527             TRACE("PBM_STEPIT: current pos changed from %d to %d\n", oldVal, infoPtr->CurVal);
528             PROGRESS_Invalidate( infoPtr, oldVal, infoPtr->CurVal );
529         }
530         return oldVal;
531     }
532
533     case PBM_SETRANGE32:
534         return PROGRESS_SetRange (infoPtr, (int)wParam, (int)lParam);
535
536     case PBM_GETRANGE:
537         if (lParam) {
538             ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
539             ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
540         }
541         return wParam ? infoPtr->MinVal : infoPtr->MaxVal;
542
543     case PBM_GETPOS:
544         return infoPtr->CurVal;
545
546     case PBM_SETBARCOLOR:
547         infoPtr->ColorBar = (COLORREF)lParam;
548         InvalidateRect(hwnd, NULL, TRUE);
549         return 0;
550
551     case PBM_SETBKCOLOR:
552         infoPtr->ColorBk = (COLORREF)lParam;
553         InvalidateRect(hwnd, NULL, TRUE);
554         return 0;
555
556     case PBM_SETMARQUEE:
557         if(wParam != 0)
558         {
559             infoPtr->Marquee = TRUE;
560             SetTimer(infoPtr->Self, ID_MARQUEE_TIMER, (UINT)lParam, NULL);
561         }
562         else
563         {
564             infoPtr->Marquee = FALSE;
565             KillTimer(infoPtr->Self, ID_MARQUEE_TIMER);
566         }
567         return infoPtr->Marquee;
568
569     default:
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 );
573     }
574 }
575
576
577 /***********************************************************************
578  * PROGRESS_Register [Internal]
579  *
580  * Registers the progress bar window class.
581  */
582 void PROGRESS_Register (void)
583 {
584     WNDCLASSW wndClass;
585
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;
593
594     RegisterClassW (&wndClass);
595 }
596
597
598 /***********************************************************************
599  * PROGRESS_Unregister [Internal]
600  *
601  * Unregisters the progress bar window class.
602  */
603 void PROGRESS_Unregister (void)
604 {
605     UnregisterClassW (PROGRESS_CLASSW, NULL);
606 }