Changed some treeview related definitions.
[wine] / dlls / comctl32 / updown.c
1 /*              
2  * Updown control
3  *
4  * Copyright 1997 Dimitrie O. Paun
5  *
6  * TODO:
7  *   - subclass the buddy window (in UPDOWN_SetBuddy) to process the
8  *     arrow keys
9  *   - I am not sure about the default values for the Min, Max, Pos
10  *     (in the UPDOWN_INFO the fields: MinVal, MaxVal, CurVal)
11  *   - I think I do not handle correctly the WS_BORDER style.
12  *     (Should be fixed. <ekohl@abo.rhein-zeitung.de>)
13  *
14  * Testing:
15  *   Not much. The following  have not been tested at all:
16  *     - horizontal arrows
17  *     - listbox as buddy window
18  *     - acceleration
19  *     - base 16
20  *     - UDS_ALIGNLEFT, ~UDS_WRAP
21  *       (tested - they work)
22  *     - integers with thousand separators.
23  *       (fixed bugs. <noel@macadamian.com>)
24  *
25  *   Even though the above list seems rather large, the control seems to
26  *   behave very well so I am confident it does work in most (all) of the
27  *   untested cases.
28  * Problems:
29  *   I do not like the arrows yet, I'll work more on them later on.
30  */
31
32 #include <stdlib.h>
33 #include "windows.h"
34 #include "commctrl.h"
35 #include "winnls.h"
36 #include "sysmetrics.h"
37 #include "updown.h"
38 #include "win.h"
39 #include "debug.h"
40
41 /* Control configuration constants */
42
43 #define INITIAL_DELAY    500 /* initial timer until auto-increment kicks in */
44 #define REPEAT_DELAY     50  /* delay between auto-increments */
45
46 #define DEFAULT_WIDTH    14  /* default width of the ctrl */
47 #define DEFAULT_XSEP      0  /* default separation between buddy and crtl */
48 #define DEFAULT_ADDTOP    0  /* amount to extend above the buddy window */
49 #define DEFAULT_ADDBOT    0  /* amount to extend below the buddy window */
50
51
52 /* Work constants */
53
54 #define FLAG_INCR        0x01
55 #define FLAG_DECR        0x02
56 #define FLAG_MOUSEIN     0x04
57 #define FLAG_CLICKED     (FLAG_INCR | FLAG_DECR)
58
59 #define TIMERID1         1
60 #define TIMERID2         2
61
62 static int accelIndex = -1;
63
64 #define UNKNOWN_PARAM(msg, wParam, lParam) WARN(updown, \
65         "UpDown Ctrl: Unknown parameter(s) for message " #msg     \
66         "(%04x): wp=%04x lp=%08lx\n", msg, wParam, lParam);
67
68 #define UPDOWN_GetInfoPtr(wndPtr) ((UPDOWN_INFO *)wndPtr->wExtra[0])
69
70
71 /***********************************************************************
72  *           UPDOWN_InBounds
73  * Tests if a given value 'val' is between the Min&Max limits
74  */
75 static BOOL32 UPDOWN_InBounds(WND *wndPtr, int val)
76 {
77   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
78
79   if(infoPtr->MaxVal > infoPtr->MinVal)
80     return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
81   else
82     return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
83 }
84
85 /***********************************************************************
86  *           UPDOWN_OffsetVal
87  * Tests if we can change the current value by delta. If so, it changes
88  * it and returns TRUE. Else, it leaves it unchanged and returns FALSE.
89  */
90 static BOOL32 UPDOWN_OffsetVal(WND *wndPtr, int delta)
91 {
92   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
93
94   /* check if we can do the modification first */
95   if(!UPDOWN_InBounds(wndPtr, infoPtr->CurVal+delta)){
96     if(wndPtr->dwStyle & UDS_WRAP)
97       delta += (delta < 0 ? -1 : 1) *
98         (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
99         (infoPtr->MinVal - infoPtr->MaxVal) +
100         (delta < 0 ? 1 : -1);
101     else
102       return FALSE;
103   }
104
105   infoPtr->CurVal += delta;
106   return TRUE;
107 }
108
109 /***********************************************************************
110  *           UPDOWN_GetArrowRect
111  * wndPtr   - pointer to the up-down wnd
112  * rect     - will hold the rectangle
113  * incr     - TRUE  get the "increment" rect (up or right)
114  *            FALSE get the "decrement" rect (down or left)
115  *          
116  */
117 static void UPDOWN_GetArrowRect(WND *wndPtr, RECT32 *rect, BOOL32 incr)
118 {
119   int len; /* will hold the width or height */
120
121   GetClientRect32(wndPtr->hwndSelf, rect);
122
123   if (wndPtr->dwStyle & UDS_HORZ) {
124     len = rect->right - rect->left; /* compute the width */
125     if (incr)
126       rect->left = len/2+1; 
127     else
128       rect->right = len/2;
129   }
130   else {
131     len = rect->bottom - rect->top; /* compute the height */
132     if (incr)
133       rect->bottom = len/2;
134     else
135       rect->top = len/2+1;
136   }
137 }
138
139 /***********************************************************************
140  *           UPDOWN_GetArrowFromPoint
141  * Returns the rectagle (for the up or down arrow) that contains pt.
142  * If it returns the up rect, it returns TRUE.
143  * If it returns the down rect, it returns FALSE.
144  */
145 static int UPDOWN_GetArrowFromPoint(WND *wndPtr, RECT32 *rect, POINT32 pt)
146 {
147   UPDOWN_GetArrowRect(wndPtr, rect, TRUE);
148   if(PtInRect32(rect, pt))
149     return TRUE;
150
151   UPDOWN_GetArrowRect(wndPtr, rect, FALSE);
152   return FALSE;
153 }
154
155
156 /***********************************************************************
157  *           UPDOWN_GetThousandSep
158  * Returns the thousand sep. If an error occurs, it returns ','.
159  */
160 static char UPDOWN_GetThousandSep()
161 {
162   char sep[2];
163
164   if(GetLocaleInfo32A(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, 
165                       sep, sizeof(sep)) != 1)
166     return ',';
167
168   return sep[0];
169 }
170
171 /***********************************************************************
172  *           UPDOWN_GetBuddyInt
173  * Tries to read the pos from the buddy window and if it succeeds,
174  * it stores it in the control's CurVal
175  * returns:
176  *   TRUE  - if it read the integer from the buddy successfully
177  *   FALSE - if an error occured
178  */
179 static BOOL32 UPDOWN_GetBuddyInt(WND *wndPtr)
180 {
181   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
182   char txt[20], sep, *src, *dst;
183   int newVal;
184
185   if (!IsWindow32(infoPtr->Buddy))
186     return FALSE;
187
188   /*if the buddy is a list window, we must set curr index */
189   if(WIDGETS_IsControl32(WIN_FindWndPtr(infoPtr->Buddy), BIC32_LISTBOX)){
190     newVal = SendMessage32A(infoPtr->Buddy, LB_GETCARETINDEX32, 0, 0);
191     if(newVal < 0)
192       return FALSE;
193   }
194   else{
195     /* we have a regular window, so will get the text */
196     if (!GetWindowText32A(infoPtr->Buddy, txt, sizeof(txt)))
197       return FALSE;
198
199     sep = UPDOWN_GetThousandSep(); 
200
201     /* now get rid of the separators */
202     for(src = dst = txt; *src; src++)
203       if(*src != sep)
204         *dst++ = *src;
205     *dst = 0;
206
207     /* try to convert the number and validate it */
208     newVal = strtol(txt, &src, infoPtr->Base);
209     if(*src || !UPDOWN_InBounds(wndPtr, newVal)) 
210       return FALSE;
211
212     TRACE(updown, "new value(%d) read from buddy (old=%d)\n", 
213                  newVal, infoPtr->CurVal);
214   }
215   
216   infoPtr->CurVal = newVal;
217   return TRUE;
218 }
219
220
221 /***********************************************************************
222  *           UPDOWN_SetBuddyInt
223  * Tries to set the pos to the buddy window based on current pos
224  * returns:
225  *   TRUE  - if it set the caption of the  buddy successfully
226  *   FALSE - if an error occured
227  */
228 static BOOL32 UPDOWN_SetBuddyInt(WND *wndPtr)
229 {
230   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
231   char txt1[20], sep;
232   int len;
233
234   if (!IsWindow32(infoPtr->Buddy)) 
235     return FALSE;
236
237   TRACE(updown, "set new value(%d) to buddy.\n",
238                infoPtr->CurVal);
239
240   /*if the buddy is a list window, we must set curr index */
241   if(WIDGETS_IsControl32(WIN_FindWndPtr(infoPtr->Buddy), BIC32_LISTBOX)){
242     SendMessage32A(infoPtr->Buddy, LB_SETCURSEL32, infoPtr->CurVal, 0);
243   }
244   else{ /* Regular window, so set caption to the number */
245     len = sprintf(txt1, (infoPtr->Base==16) ? "%X" : "%d", infoPtr->CurVal);
246
247     sep = UPDOWN_GetThousandSep(); 
248
249     /* Do thousands seperation if necessary */
250     if (!(wndPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
251       char txt2[20], *src = txt1, *dst = txt2;
252       if(len%3 > 0){
253         lstrcpyn32A (dst, src, len%3 + 1);      /* need to include the null */ 
254         dst += len%3;
255         src += len%3;
256       }
257       for(len=0; *src; len++){
258         if(len%3==0)
259           *dst++ = sep;
260         *dst++ = *src++;
261       }
262       *dst = 0;           /* null terminate it */
263       strcpy(txt1, txt2); /* move it to the proper place */
264     }
265     SetWindowText32A(infoPtr->Buddy, txt1);
266   }
267
268   return TRUE;
269
270
271 /***********************************************************************
272  * UPDOWN_Draw [Internal]
273  *
274  * Draw the arrows. The background need not be erased.
275  */
276 static void UPDOWN_Draw (WND *wndPtr, HDC32 hdc)
277 {
278   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
279   BOOL32 prssed;
280   RECT32 rect;
281   
282   /* Draw the incr button */
283   UPDOWN_GetArrowRect(wndPtr, &rect, TRUE);
284   prssed = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
285   DrawFrameControl32(hdc, &rect, DFC_SCROLL, 
286         (wndPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLUP) |
287         (prssed ? DFCS_PUSHED : 0) |
288         (wndPtr->dwStyle&WS_DISABLED ? DFCS_INACTIVE : 0) );
289
290   /* Draw the space between the buttons */
291   rect.top = rect.bottom; rect.bottom++;
292   DrawEdge32(hdc, &rect, 0, BF_MIDDLE);
293                     
294   /* Draw the decr button */
295   UPDOWN_GetArrowRect(wndPtr, &rect, FALSE);
296   prssed = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
297   DrawFrameControl32(hdc, &rect, DFC_SCROLL, 
298         (wndPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLDOWN) |
299         (prssed ? DFCS_PUSHED : 0) |
300         (wndPtr->dwStyle&WS_DISABLED ? DFCS_INACTIVE : 0) );
301 }
302
303 /***********************************************************************
304  * UPDOWN_Refresh [Internal]
305  *
306  * Synchronous drawing (must NOT be used in WM_PAINT).
307  * Calls UPDOWN_Draw.
308  */
309 static void UPDOWN_Refresh (WND *wndPtr)
310 {
311     HDC32 hdc;
312   
313     hdc = GetDC32 (wndPtr->hwndSelf);
314     UPDOWN_Draw (wndPtr, hdc);
315     ReleaseDC32 (wndPtr->hwndSelf, hdc);
316 }
317
318
319 /***********************************************************************
320  * UPDOWN_Paint [Internal]
321  *
322  * Asynchronous drawing (must ONLY be used in WM_PAINT).
323  * Calls UPDOWN_Draw.
324  */
325 static void UPDOWN_Paint (WND *wndPtr)
326 {
327     PAINTSTRUCT32 ps;
328     HDC32 hdc;
329   
330     hdc = BeginPaint32 (wndPtr->hwndSelf, &ps);
331     UPDOWN_Draw (wndPtr, hdc);
332     EndPaint32 (wndPtr->hwndSelf, &ps);
333 }
334
335 /***********************************************************************
336  *           UPDOWN_SetBuddy
337  * Tests if 'hwndBud' is a valid window handle. If not, returns FALSE.
338  * Else, sets it as a new Buddy.
339  * Then, it should subclass the buddy 
340  * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
341  * process the UP/DOWN arrow keys.
342  * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
343  * the size/pos of the buddy and the control are adjusted accordingly.
344  */
345 static BOOL32 UPDOWN_SetBuddy(WND *wndPtr, HWND32 hwndBud)
346 {
347   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr); 
348   RECT32 budRect; /* new coord for the buddy */
349   int x;          /* new x position and width for the up-down */
350           
351   /* Is is a valid bud? */
352   if(!IsWindow32(hwndBud))
353     return FALSE;
354
355   if(wndPtr->dwStyle & UDS_ARROWKEYS){
356     FIXME(updown, "we need to subclass the buddy to process the arrow keys.\n");
357   }
358
359   /* do we need to do any adjustments? */
360   if(!(wndPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)))
361     return TRUE;
362
363   /* Get the rect of the buddy relative to its parent */
364   GetWindowRect32(infoPtr->Buddy, &budRect);
365   MapWindowPoints32(HWND_DESKTOP, GetParent32(infoPtr->Buddy),
366                   (POINT32 *)(&budRect.left), 2);
367
368   /* now do the positioning */
369   if(wndPtr->dwStyle & UDS_ALIGNRIGHT){
370     budRect.right -= DEFAULT_WIDTH+DEFAULT_XSEP;
371     x  = budRect.right+DEFAULT_XSEP;
372   }
373   else{ /* UDS_ALIGNLEFT */
374     x  = budRect.left;
375     budRect.left += DEFAULT_WIDTH+DEFAULT_XSEP;
376   }
377
378   /* first adjust the buddy to accomodate the up/down */
379   SetWindowPos32(infoPtr->Buddy, 0, budRect.left, budRect.top,
380                budRect.right  - budRect.left, budRect.bottom - budRect.top, 
381                SWP_NOACTIVATE|SWP_NOZORDER);
382
383   /* now position the up/down */
384   /* Since the UDS_ALIGN* flags were used, */
385   /* we will pick the position and size of the window. */
386
387   SetWindowPos32(wndPtr->hwndSelf,0,x,budRect.top-DEFAULT_ADDTOP,DEFAULT_WIDTH,
388                  (budRect.bottom-budRect.top)+DEFAULT_ADDTOP+DEFAULT_ADDBOT,
389                  SWP_NOACTIVATE|SWP_NOZORDER);
390
391   return TRUE;
392 }         
393
394 /***********************************************************************
395  *           UPDOWN_DoAction
396  *
397  * This function increments/decrements the CurVal by the 
398  * 'delta' amount according to the 'incr' flag
399  * It notifies the parent as required.
400  * It handles wraping and non-wraping correctly.
401  * It is assumed that delta>0
402  */
403 static void UPDOWN_DoAction(WND *wndPtr, int delta, BOOL32 incr)
404 {
405   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr); 
406   int old_val = infoPtr->CurVal;
407   NM_UPDOWN ni;
408
409   TRACE(updown, "%s by %d\n", incr ? "inc" : "dec", delta);
410
411   /* check if we can do the modification first */
412   delta *= (incr ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
413   if(!UPDOWN_OffsetVal(wndPtr, delta))
414     return;
415
416   /* so, if we can do the change, recompute delta and restore old value */
417   delta = infoPtr->CurVal - old_val;
418   infoPtr->CurVal = old_val;
419
420   /* We must notify parent now to obtain permission */
421   ni.iPos = infoPtr->CurVal;
422   ni.iDelta = delta;
423   ni.hdr.hwndFrom = wndPtr->hwndSelf;
424   ni.hdr.idFrom = wndPtr->wIDmenu;
425   ni.hdr.code = UDN_DELTAPOS; 
426   if(SendMessage32A(wndPtr->parent->hwndSelf, 
427                     WM_NOTIFY, wndPtr->wIDmenu, (LPARAM)&ni))
428     return; /* we are not allowed to change */
429   
430   /* Now adjust value with (maybe new) delta */
431   if(!UPDOWN_OffsetVal(wndPtr, ni.iDelta))
432     return;
433
434   /* Now take care about our buddy */
435   if(!IsWindow32(infoPtr->Buddy)) 
436     return; /* Nothing else to do */
437
438
439   if(wndPtr->dwStyle & UDS_SETBUDDYINT)
440     UPDOWN_SetBuddyInt(wndPtr);
441
442   /* Also, notify it */
443   /* FIXME: do we need to send the notification only if
444             we do not have the UDS_SETBUDDYINT style set? */
445
446   SendMessage32A(GetParent32 (wndPtr->hwndSelf), 
447                  wndPtr->dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL, 
448                  MAKELONG(incr ? SB_LINEUP : SB_LINEDOWN, infoPtr->CurVal),
449                  wndPtr->hwndSelf);
450 }
451
452 /***********************************************************************
453  *           UPDOWN_IsEnabled
454  *
455  * Returns TRUE if it is enabled as well as its buddy (if any)
456  *         FALSE otherwise
457  */
458 static BOOL32 UPDOWN_IsEnabled(WND *wndPtr)
459 {
460   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
461
462   if(wndPtr->dwStyle & WS_DISABLED)
463     return FALSE;
464   return IsWindowEnabled32(infoPtr->Buddy);
465 }
466
467 /***********************************************************************
468  *           UPDOWN_CancelMode
469  *
470  * Deletes any timers, releases the mouse and does  redraw if necessary.
471  * If the control is not in "capture" mode, it does nothing.
472  * If the control was not in cancel mode, it returns FALSE. 
473  * If the control was in cancel mode, it returns TRUE.
474  */
475 static BOOL32 UPDOWN_CancelMode(WND *wndPtr)
476 {
477   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
478  
479   /* if not in 'capture' mode, do nothing */
480   if(!(infoPtr->Flags & FLAG_CLICKED))
481     return FALSE;
482
483   KillTimer32(wndPtr->hwndSelf, TIMERID1); /* kill all possible timers */
484   KillTimer32(wndPtr->hwndSelf, TIMERID2);
485   
486   if(GetCapture32() == wndPtr->hwndSelf)   /* let the mouse go         */
487     ReleaseCapture();          /* if we still have it      */  
488   
489   infoPtr->Flags = 0;          /* get rid of any flags     */
490   UPDOWN_Refresh (wndPtr);     /* redraw the control just in case */
491   
492   return TRUE;
493 }
494
495 /***********************************************************************
496  *           UPDOWN_HandleMouseEvent
497  *
498  * Handle a mouse event for the updown.
499  * 'pt' is the location of the mouse event in client or
500  * windows coordinates. 
501  */
502 static void UPDOWN_HandleMouseEvent(WND *wndPtr, UINT32 msg, POINT32 pt)
503 {
504   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr);
505   RECT32 rect;
506   int temp;
507
508   switch(msg)
509     {
510     case WM_LBUTTONDOWN:  /* Initialise mouse tracking */
511       /* If we are already in the 'clicked' mode, then nothing to do */
512       if(infoPtr->Flags & FLAG_CLICKED)
513         return;
514
515       /* If the buddy is an edit, will set focus to it */
516       if(WIDGETS_IsControl32(WIN_FindWndPtr(infoPtr->Buddy), BIC32_EDIT))
517         SetFocus32(infoPtr->Buddy);
518
519       /* Now see which one is the 'active' arrow */
520       temp = UPDOWN_GetArrowFromPoint(wndPtr, &rect, pt);
521
522       /* Update the CurVal if necessary */
523       if(wndPtr->dwStyle & UDS_SETBUDDYINT)
524         UPDOWN_GetBuddyInt(wndPtr);
525         
526       /* Before we proceed, see if we can spin... */
527       if(!(wndPtr->dwStyle & UDS_WRAP))
528         if(( temp && infoPtr->CurVal==infoPtr->MaxVal) ||
529            (!temp && infoPtr->CurVal==infoPtr->MinVal))
530           return;
531
532       /* Set up the correct flags */
533       infoPtr->Flags  = 0; 
534       infoPtr->Flags |= temp ? FLAG_INCR : FLAG_DECR;
535       infoPtr->Flags |= FLAG_MOUSEIN;
536       
537       /* repaint the control */
538       UPDOWN_Refresh (wndPtr);
539
540       /* process the click */
541       UPDOWN_DoAction(wndPtr, 1, infoPtr->Flags & FLAG_INCR);
542
543       /* now capture all mouse messages */
544       SetCapture32(wndPtr->hwndSelf);
545
546       /* and startup the first timer */
547       SetTimer32(wndPtr->hwndSelf, TIMERID1, INITIAL_DELAY, 0); 
548       break;
549
550     case WM_MOUSEMOVE:
551       /* If we are not in the 'clicked' mode, then nothing to do */
552       if(!(infoPtr->Flags & FLAG_CLICKED))
553         return;
554
555       /* save the flags to see if any got modified */
556       temp = infoPtr->Flags;
557
558       /* Now get the 'active' arrow rectangle */
559       if (infoPtr->Flags & FLAG_INCR)
560         UPDOWN_GetArrowRect(wndPtr, &rect, TRUE);
561       else
562         UPDOWN_GetArrowRect(wndPtr, &rect, FALSE);
563
564       /* Update the flags if we are in/out */
565       if(PtInRect32(&rect, pt))
566         infoPtr->Flags |=  FLAG_MOUSEIN;
567       else{
568         infoPtr->Flags &= ~FLAG_MOUSEIN;
569         if(accelIndex != -1) /* if we have accel info */
570           accelIndex = 0;    /* reset it              */
571       }
572       /* If state changed, redraw the control */
573       if(temp != infoPtr->Flags)
574         UPDOWN_Refresh (wndPtr);
575       break;
576
577       default:
578         ERR(updown, "Impossible case!\n");
579     }
580
581 }
582
583 /***********************************************************************
584  *           UpDownWndProc
585  */
586 LRESULT WINAPI UpDownWindowProc(HWND32 hwnd, UINT32 message, WPARAM32 wParam,
587                                 LPARAM lParam)
588 {
589   WND *wndPtr = WIN_FindWndPtr(hwnd);
590   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(wndPtr); 
591   int temp;
592
593   switch(message)
594     {
595     case WM_NCCREATE:
596       /* get rid of border, if any */
597       wndPtr->dwStyle &= ~WS_BORDER;
598       return TRUE;
599
600     case WM_CREATE:
601       infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
602       wndPtr->wExtra[0] = (DWORD)infoPtr;
603
604       /* initialize the info struct */
605       infoPtr->AccelCount=0; infoPtr->AccelVect=0; 
606       infoPtr->CurVal=0; infoPtr->MinVal=0; infoPtr->MaxVal=100; /*FIXME*/
607       infoPtr->Base  = 10; /* Default to base 10  */
608       infoPtr->Buddy = 0;  /* No buddy window yet */
609       infoPtr->Flags = 0;  /* And no flags        */
610
611       /* Do we pick the buddy win ourselves? */
612       if(wndPtr->dwStyle & UDS_AUTOBUDDY)
613         UPDOWN_SetBuddy(wndPtr, GetWindow32(wndPtr->hwndSelf, GW_HWNDPREV));
614         
615       TRACE(updown, "UpDown Ctrl creation, hwnd=%04x\n", hwnd);
616       break;
617     
618     case WM_DESTROY:
619       if(infoPtr->AccelVect)
620         COMCTL32_Free (infoPtr->AccelVect);
621
622       COMCTL32_Free (infoPtr);
623       wndPtr->wExtra[0] = 0;
624
625       TRACE(updown, "UpDown Ctrl destruction, hwnd=%04x\n", hwnd);
626       break;
627         
628     case WM_ENABLE:
629       if(wndPtr->dwStyle & WS_DISABLED)
630         UPDOWN_CancelMode(wndPtr);
631       UPDOWN_Paint(wndPtr);
632       break;
633
634     case WM_TIMER:
635       /* if initial timer, kill it and start the repeat timer */
636       if(wParam == TIMERID1){
637         KillTimer32(hwnd, TIMERID1);
638         /* if no accel info given, used default timer */
639         if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0){
640           accelIndex = -1;
641           temp = REPEAT_DELAY;
642         }
643         else{
644           accelIndex = 0; /* otherwise, use it */
645           temp = infoPtr->AccelVect[accelIndex].nSec * 1000 + 1;
646         }
647         SetTimer32(hwnd, TIMERID2, temp, 0);
648       }
649
650       /* now, if the mouse is above us, do the thing...*/
651       if(infoPtr->Flags & FLAG_MOUSEIN){
652         temp = accelIndex==-1 ? 1 : infoPtr->AccelVect[accelIndex].nInc;
653         UPDOWN_DoAction(wndPtr, temp, infoPtr->Flags & FLAG_INCR);
654         
655         if(accelIndex!=-1 && accelIndex < infoPtr->AccelCount-1){
656           KillTimer32(hwnd, TIMERID2);
657           accelIndex++; /* move to the next accel info */
658           temp = infoPtr->AccelVect[accelIndex].nSec * 1000 + 1;
659           /* make sure we have at least 1ms intervals */
660           SetTimer32(hwnd, TIMERID2, temp, 0);      
661         }
662       }
663       break;
664
665     case WM_CANCELMODE:
666       UPDOWN_CancelMode(wndPtr);
667       break;
668
669     case WM_LBUTTONUP:
670       if(!UPDOWN_CancelMode(wndPtr))
671         break;
672       /*If we released the mouse and our buddy is an edit */
673       /* we must select all text in it.                   */
674       if(WIDGETS_IsControl32(WIN_FindWndPtr(infoPtr->Buddy), BIC32_EDIT))
675         SendMessage32A(infoPtr->Buddy, EM_SETSEL32, 0, MAKELONG(0, -1));
676       break;
677       
678     case WM_LBUTTONDOWN:
679     case WM_MOUSEMOVE:
680       if(UPDOWN_IsEnabled(wndPtr)){
681         POINT32 pt;
682         CONV_POINT16TO32( (POINT16 *)&lParam, &pt );
683         UPDOWN_HandleMouseEvent( wndPtr, message, pt );
684       }
685     break;
686
687     case WM_KEYDOWN:
688       if((wndPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(wndPtr)){
689         switch(wParam){
690         case VK_UP:  
691         case VK_DOWN:
692           UPDOWN_GetBuddyInt(wndPtr);
693           UPDOWN_DoAction(wndPtr, 1, wParam==VK_UP);
694           break;
695         }
696       }
697       break;
698       
699     case WM_PAINT:
700       UPDOWN_Paint(wndPtr);
701       break;
702     
703     case UDM_GETACCEL:
704       if (wParam==0 && lParam==0)    /*if both zero, */
705         return infoPtr->AccelCount;  /*just return the accel count*/
706       if (wParam || lParam){
707         UNKNOWN_PARAM(UDM_GETACCEL, wParam, lParam);
708         return 0;
709       }
710       temp = MIN(infoPtr->AccelCount, wParam);
711       memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
712       return temp;
713
714     case UDM_SETACCEL:
715       TRACE(updown, "UpDown Ctrl new accel info, hwnd=%04x\n", hwnd);
716       if(infoPtr->AccelVect){
717         COMCTL32_Free (infoPtr->AccelVect);
718         infoPtr->AccelCount = 0;
719         infoPtr->AccelVect  = 0;
720       }
721       if(wParam==0)
722         return TRUE;
723       infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
724       if(infoPtr->AccelVect==0)
725         return FALSE;
726       memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
727       return TRUE;
728
729     case UDM_GETBASE:
730       if (wParam || lParam)
731         UNKNOWN_PARAM(UDM_GETBASE, wParam, lParam);
732       return infoPtr->Base;
733
734     case UDM_SETBASE:
735       TRACE(updown, "UpDown Ctrl new base(%d), hwnd=%04x\n", 
736                      wParam, hwnd);
737       if ( !(wParam==10 || wParam==16) || lParam)
738         UNKNOWN_PARAM(UDM_SETBASE, wParam, lParam);
739       if (wParam==10 || wParam==16){
740         temp = infoPtr->Base;
741         infoPtr->Base = wParam;
742         return temp;       /* return the prev base */
743       }
744       break;
745
746     case UDM_GETBUDDY:
747       if (wParam || lParam)
748         UNKNOWN_PARAM(UDM_GETBUDDY, wParam, lParam);
749       return infoPtr->Buddy;
750
751     case UDM_SETBUDDY:
752       if (lParam)
753         UNKNOWN_PARAM(UDM_SETBUDDY, wParam, lParam);
754       temp = infoPtr->Buddy;
755       infoPtr->Buddy = wParam;
756       UPDOWN_SetBuddy(wndPtr, wParam);
757       TRACE(updown, "UpDown Ctrl new buddy(%04x), hwnd=%04x\n", 
758                      infoPtr->Buddy, hwnd);
759       return temp;
760
761     case UDM_GETPOS:
762       if (wParam || lParam)
763         UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
764       temp = UPDOWN_GetBuddyInt(wndPtr);
765       return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
766
767     case UDM_SETPOS:
768       if (wParam || HIWORD(lParam))
769         UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
770       temp = SLOWORD(lParam);
771       TRACE(updown, "UpDown Ctrl new value(%d), hwnd=%04x\n",
772                      temp, hwnd);
773       if(!UPDOWN_InBounds(wndPtr, temp)){
774         if(temp < infoPtr->MinVal)  
775           temp = infoPtr->MinVal;
776         if(temp > infoPtr->MaxVal)
777           temp = infoPtr->MaxVal;
778       }
779       wParam = infoPtr->CurVal; /* save prev value   */
780       infoPtr->CurVal = temp;   /* set the new value */
781       if(wndPtr->dwStyle & UDS_SETBUDDYINT)
782         UPDOWN_SetBuddyInt(wndPtr);
783       return wParam;            /* return prev value */
784       
785     case UDM_GETRANGE:
786       if (wParam || lParam)
787         UNKNOWN_PARAM(UDM_GETRANGE, wParam, lParam);
788       return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
789
790     case UDM_SETRANGE:
791       if (wParam)
792         UNKNOWN_PARAM(UDM_SETRANGE, wParam, lParam); /* we must have:     */
793       infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
794       infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
795                                          /* |Max-Min| <= UD_MAXVAL        */
796       TRACE(updown, "UpDown Ctrl new range(%d to %d), hwnd=%04x\n", 
797                      infoPtr->MinVal, infoPtr->MaxVal, hwnd);
798       break;                             
799
800     case UDM_GETRANGE32:
801       if (wParam)
802         *(LPINT32)wParam = infoPtr->MinVal;
803       if (lParam)
804         *(LPINT32)lParam = infoPtr->MaxVal;
805       break;
806
807     case UDM_SETRANGE32:
808       infoPtr->MinVal = (INT32)wParam;
809       infoPtr->MaxVal = (INT32)lParam;
810       if (infoPtr->MaxVal <= infoPtr->MinVal)
811         infoPtr->MaxVal = infoPtr->MinVal + 1;
812       TRACE(updown, "UpDown Ctrl new range(%d to %d), hwnd=%04x\n", 
813                      infoPtr->MinVal, infoPtr->MaxVal, hwnd);
814       break;
815
816     default: 
817       if (message >= WM_USER) 
818         ERR (updown, "unknown msg %04x wp=%04x lp=%08lx\n", 
819              message, wParam, lParam);
820       return DefWindowProc32A (hwnd, message, wParam, lParam); 
821     } 
822
823     return 0;
824 }
825
826
827 /***********************************************************************
828  *              UPDOWN_Register [Internal]
829  *
830  * Registers the updown window class.
831  */
832
833 VOID
834 UPDOWN_Register(void)
835 {
836     WNDCLASS32A wndClass;
837
838     if( GlobalFindAtom32A( UPDOWN_CLASS32A ) ) return;
839
840     ZeroMemory( &wndClass, sizeof( WNDCLASS32A ) );
841     wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW;
842     wndClass.lpfnWndProc   = (WNDPROC32)UpDownWindowProc;
843     wndClass.cbClsExtra    = 0;
844     wndClass.cbWndExtra    = sizeof(UPDOWN_INFO*);
845     wndClass.hCursor       = LoadCursor32A( 0, IDC_ARROW32A );
846     wndClass.hbrBackground = (HBRUSH32)(COLOR_3DFACE + 1);
847     wndClass.lpszClassName = UPDOWN_CLASS32A;
848  
849     RegisterClass32A( &wndClass );
850 }
851
852
853 /***********************************************************************
854  *              UPDOWN_Unregister       [Internal]
855  *
856  * Unregisters the updown window class.
857  */
858
859 VOID
860 UPDOWN_Unregister (VOID)
861 {
862     if (GlobalFindAtom32A (UPDOWN_CLASS32A))
863         UnregisterClass32A (UPDOWN_CLASS32A, (HINSTANCE32)NULL);
864 }
865