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