Release 980927
[wine] / dlls / comctl32 / progress.c
1 /*              
2  * Progress control
3  *
4  * Copyright 1997 Dimitrie O. Paun
5  *
6  * TODO:
7  *   - I do not know what to to on WM_[SG]ET_FONT
8  */
9
10 #include "windows.h"
11 #include "commctrl.h"
12 #include "progress.h"
13 #include "win.h"
14 #include "debug.h"
15
16
17 /* Control configuration constants */
18
19 #define LED_GAP    2
20
21 /* Work constants */
22
23 #define UNKNOWN_PARAM(msg, wParam, lParam) WARN(progress, \
24         "Unknown parameter(s) for message " #msg     \
25         "(%04x): wp=%04x lp=%08lx\n", msg, wParam, lParam); 
26
27 #define PROGRESS_GetInfoPtr(wndPtr) ((PROGRESS_INFO *)wndPtr->wExtra[0])
28
29
30 /***********************************************************************
31  * PROGRESS_Draw
32  * Draws the progress bar.
33  */
34 static void
35 PROGRESS_Draw (WND *wndPtr, HDC32 hdc)
36 {
37   PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(wndPtr);
38   HBRUSH32 hbrBar, hbrBk;
39   int rightBar, rightMost, ledWidth;
40   RECT32 rect;
41
42   TRACE(progress, "refresh pos=%d min=%d, max=%d\n",
43                infoPtr->CurVal, infoPtr->MinVal, infoPtr->MaxVal);
44
45   /* get the required bar brush */
46   if (infoPtr->ColorBar == CLR_DEFAULT)
47     hbrBar = GetSysColorBrush32(COLOR_HIGHLIGHT);
48   else
49     hbrBar = CreateSolidBrush32 (infoPtr->ColorBar);
50
51   /* get the required background brush */
52   if (infoPtr->ColorBk == CLR_DEFAULT)
53     hbrBk = GetSysColorBrush32 (COLOR_3DFACE);
54   else
55     hbrBk = CreateSolidBrush32 (infoPtr->ColorBk);
56
57   /* get client rectangle */
58   GetClientRect32 (wndPtr->hwndSelf, &rect);
59
60   /* draw the background */
61   FillRect32(hdc, &rect, hbrBk);
62
63   rect.left++; rect.right--; rect.top++; rect.bottom--;
64
65   /* compute extent of progress bar */
66   if (wndPtr->dwStyle & PBS_VERTICAL)
67   {
68     rightBar = rect.bottom - 
69       MulDiv32(infoPtr->CurVal-infoPtr->MinVal,
70                rect.bottom - rect.top,
71                infoPtr->MaxVal-infoPtr->MinVal);
72     ledWidth = MulDiv32 ((rect.right - rect.left), 2, 3);
73     rightMost = rect.top;
74   }
75   else
76   {
77     rightBar = rect.left + 
78       MulDiv32(infoPtr->CurVal-infoPtr->MinVal,
79                rect.right - rect.left,
80                infoPtr->MaxVal-infoPtr->MinVal);
81     ledWidth = MulDiv32 ((rect.bottom - rect.top), 2, 3);
82     rightMost = rect.right;
83   }
84
85   /* now draw the bar */
86   if (wndPtr->dwStyle & PBS_SMOOTH)
87   {
88     if (wndPtr->dwStyle & PBS_VERTICAL)
89       rect.top = rightBar;
90     else
91       rect.right = rightBar;
92     FillRect32(hdc, &rect, hbrBar);
93   }
94   else
95   {
96     if (wndPtr->dwStyle & PBS_VERTICAL)
97       while(rect.bottom > rightBar) { 
98         rect.top = rect.bottom-ledWidth;
99         if (rect.top < rightMost)
100           rect.top = rightMost;
101         FillRect32(hdc, &rect, hbrBar);
102         rect.bottom = rect.top-LED_GAP;
103       }
104     else
105       while(rect.left < rightBar) { 
106         rect.right = rect.left+ledWidth;
107         if (rect.right > rightMost)
108           rect.right = rightMost;
109         FillRect32(hdc, &rect, hbrBar);
110         rect.left  = rect.right+LED_GAP;
111       }
112   }
113
114   /* delete bar brush */
115   if (infoPtr->ColorBar != CLR_DEFAULT)
116       DeleteObject32 (hbrBar);
117
118   /* delete background brush */
119   if (infoPtr->ColorBk != CLR_DEFAULT)
120       DeleteObject32 (hbrBk);
121 }
122
123 /***********************************************************************
124  * PROGRESS_Refresh
125  * Draw the progress bar. The background need not be erased.
126  */
127 static void
128 PROGRESS_Refresh (WND *wndPtr)
129 {
130     HDC32 hdc;
131
132     hdc = GetDC32 (wndPtr->hwndSelf);
133     PROGRESS_Draw (wndPtr, hdc);
134     ReleaseDC32 (wndPtr->hwndSelf, hdc);
135 }
136
137 /***********************************************************************
138  * PROGRESS_Paint
139  * Draw the progress bar. The background need not be erased.
140  * If dc!=0, it draws on it
141  */
142 static void
143 PROGRESS_Paint (WND *wndPtr)
144 {
145     PAINTSTRUCT32 ps;
146     HDC32 hdc;
147
148     hdc = BeginPaint32 (wndPtr->hwndSelf, &ps);
149     PROGRESS_Draw (wndPtr, hdc);
150     EndPaint32 (wndPtr->hwndSelf, &ps);
151 }
152
153
154 /***********************************************************************
155  *           PROGRESS_CoercePos
156  * Makes sure the current position (CUrVal) is within bounds.
157  */
158 static void PROGRESS_CoercePos(WND *wndPtr)
159 {
160   PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(wndPtr); 
161
162   if(infoPtr->CurVal < infoPtr->MinVal)
163     infoPtr->CurVal = infoPtr->MinVal;
164   if(infoPtr->CurVal > infoPtr->MaxVal)
165     infoPtr->CurVal = infoPtr->MaxVal;
166 }
167
168 /***********************************************************************
169  *           ProgressWindowProc
170  */
171 LRESULT WINAPI ProgressWindowProc(HWND32 hwnd, UINT32 message, 
172                                   WPARAM32 wParam, LPARAM lParam)
173 {
174   WND *wndPtr = WIN_FindWndPtr(hwnd);
175   PROGRESS_INFO *infoPtr = PROGRESS_GetInfoPtr(wndPtr); 
176   UINT32 temp;
177
178   switch(message)
179     {
180     case WM_NCCREATE:
181       wndPtr->dwExStyle |= WS_EX_STATICEDGE;
182       return TRUE;
183
184     case WM_CREATE:
185       /* allocate memory for info struct */
186       infoPtr = 
187         (PROGRESS_INFO *)COMCTL32_Alloc (sizeof(PROGRESS_INFO));
188       wndPtr->wExtra[0] = (DWORD)infoPtr;
189
190       /* initialize the info struct */
191       infoPtr->MinVal=0; 
192       infoPtr->MaxVal=100;
193       infoPtr->CurVal=0; 
194       infoPtr->Step=10;
195       infoPtr->ColorBar=CLR_DEFAULT;
196       infoPtr->ColorBk=CLR_DEFAULT;
197       TRACE(progress, "Progress Ctrl creation, hwnd=%04x\n", hwnd);
198       break;
199     
200     case WM_DESTROY:
201       TRACE (progress, "Progress Ctrl destruction, hwnd=%04x\n", hwnd);
202       COMCTL32_Free (infoPtr);
203       break;
204
205     case WM_ERASEBKGND:
206       /* pretend to erase it here, but we will do it in the paint
207          function to avoid flicker */
208       return 1;
209         
210     case WM_GETFONT:
211       FIXME (progress, "WM_GETFONT - empty message!\n");
212       /* FIXME: What do we need to do? */
213       break;
214
215     case WM_SETFONT:
216       FIXME (progress, "WM_SETFONT - empty message!\n");
217       /* FIXME: What do we need to do? */
218       break;
219
220     case WM_PAINT:
221       PROGRESS_Paint (wndPtr);
222       break;
223     
224     case PBM_DELTAPOS:
225       if(lParam)
226         UNKNOWN_PARAM(PBM_DELTAPOS, wParam, lParam);
227       temp = infoPtr->CurVal;
228       if(wParam != 0){
229         infoPtr->CurVal += (UINT16)wParam;
230         PROGRESS_CoercePos(wndPtr);
231         PROGRESS_Refresh (wndPtr);
232       }
233       return temp;
234
235     case PBM_SETPOS:
236       if (lParam)
237         UNKNOWN_PARAM(PBM_SETPOS, wParam, lParam);
238       temp = infoPtr->CurVal;
239       if(temp != wParam){
240         infoPtr->CurVal = (UINT16)wParam;
241         PROGRESS_CoercePos(wndPtr);
242         PROGRESS_Refresh (wndPtr);
243       }
244       return temp;          
245       
246     case PBM_SETRANGE:
247       if (wParam)
248         UNKNOWN_PARAM(PBM_SETRANGE, wParam, lParam);
249       temp = MAKELONG(infoPtr->MinVal, infoPtr->MaxVal);
250       if(temp != lParam){
251         infoPtr->MinVal = LOWORD(lParam); 
252         infoPtr->MaxVal = HIWORD(lParam);
253         if(infoPtr->MaxVal <= infoPtr->MinVal)
254           infoPtr->MaxVal = infoPtr->MinVal+1;
255         PROGRESS_CoercePos(wndPtr);
256         PROGRESS_Refresh (wndPtr);
257       }
258       return temp;
259
260     case PBM_SETSTEP:
261       if (lParam)
262         UNKNOWN_PARAM(PBM_SETSTEP, wParam, lParam);
263       temp = infoPtr->Step;   
264       infoPtr->Step = (UINT16)wParam;   
265       return temp;
266
267     case PBM_STEPIT:
268       if (wParam || lParam)
269         UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
270       temp = infoPtr->CurVal;   
271       infoPtr->CurVal += infoPtr->Step;
272       if(infoPtr->CurVal > infoPtr->MaxVal)
273         infoPtr->CurVal = infoPtr->MinVal;
274       if(temp != infoPtr->CurVal)
275         PROGRESS_Refresh (wndPtr);
276       return temp;
277
278     case PBM_SETRANGE32:
279       temp = MAKELONG(infoPtr->MinVal, infoPtr->MaxVal);
280       if((infoPtr->MinVal != (INT32)wParam) ||
281          (infoPtr->MaxVal != (INT32)lParam)) {
282         infoPtr->MinVal = (INT32)wParam;
283         infoPtr->MaxVal = (INT32)lParam;
284         if(infoPtr->MaxVal <= infoPtr->MinVal)
285           infoPtr->MaxVal = infoPtr->MinVal+1;
286         PROGRESS_CoercePos(wndPtr);
287         PROGRESS_Refresh (wndPtr);
288       }
289       return temp;
290     
291     case PBM_GETRANGE:
292       if (lParam){
293         ((PPBRANGE)lParam)->iLow = infoPtr->MinVal;
294         ((PPBRANGE)lParam)->iHigh = infoPtr->MaxVal;
295       }
296       return (wParam) ? infoPtr->MinVal : infoPtr->MaxVal;
297
298     case PBM_GETPOS:
299       if (wParam || lParam)
300         UNKNOWN_PARAM(PBM_STEPIT, wParam, lParam);
301       return (infoPtr->CurVal);
302
303     case PBM_SETBARCOLOR:
304       if (wParam)
305         UNKNOWN_PARAM(PBM_SETBARCOLOR, wParam, lParam);
306       infoPtr->ColorBar = (COLORREF)lParam;     
307       PROGRESS_Refresh (wndPtr);
308       break;
309
310     case PBM_SETBKCOLOR:
311       if (wParam)
312         UNKNOWN_PARAM(PBM_SETBKCOLOR, wParam, lParam);
313       infoPtr->ColorBk = (COLORREF)lParam;
314       PROGRESS_Refresh (wndPtr);
315       break;
316
317     default: 
318       if (message >= WM_USER) 
319         ERR(progress, "unknown msg %04x wp=%04x lp=%08lx\n", 
320                     message, wParam, lParam );
321       return DefWindowProc32A( hwnd, message, wParam, lParam ); 
322     } 
323
324     return 0;
325 }
326
327
328 /***********************************************************************
329  * PROGRESS_Register [Internal]
330  *
331  * Registers the progress bar window class.
332  * 
333  */
334
335 void 
336 PROGRESS_Register(void)
337 {
338     WNDCLASS32A wndClass;
339
340     if( GlobalFindAtom32A( PROGRESS_CLASS32A ) ) return;
341
342     ZeroMemory( &wndClass, sizeof( WNDCLASS32A ) );
343     wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
344     wndClass.lpfnWndProc   = (WNDPROC32)ProgressWindowProc;
345     wndClass.cbClsExtra    = 0;
346     wndClass.cbWndExtra    = sizeof(PROGRESS_INFO *);
347     wndClass.hCursor       = LoadCursor32A( 0, IDC_ARROW32A );
348     wndClass.lpszClassName = PROGRESS_CLASS32A;
349
350     RegisterClass32A( &wndClass );
351 }
352