Added LGPL standard comment, and copyright notices where necessary.
[wine] / dlls / comctl32 / updown.c
1 /*              
2  * Updown control
3  *
4  * Copyright 1997 Dimitrie O. Paun
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * TODO:
21  *   - I think I do not handle correctly the WS_BORDER style.
22  *     (Should be fixed. <ekohl@abo.rhein-zeitung.de>)
23  *
24  * Testing:
25  *   Not much. The following  have not been tested at all:
26  *     - horizontal arrows
27  *     - listbox as buddy window
28  *     - acceleration
29  *     - base 16
30  *     - integers with thousand separators.
31  *       (fixed bugs. <noel@macadamian.com>)
32  *
33  *   Even though the above list seems rather large, the control seems to
34  *   behave very well so I am confident it does work in most (all) of the
35  *   untested cases.
36  */
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41
42 #include "windef.h"
43 #include "winbase.h"
44 #include "wingdi.h"
45 #include "winuser.h"
46 #include "commctrl.h"
47 #include "winnls.h"
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(updown);
51
52 #define UPDOWN_BUDDYCLASSNAMELEN 40
53
54 typedef struct
55 {
56   HWND      Self;         /* Handle to this up-down control */
57   UINT      AccelCount;   /* Number of elements in AccelVect */
58   UDACCEL*  AccelVect;    /* Vector containing AccelCount elements */
59   INT       AccelIndex;   /* Current accel index, -1 if not accelerating */
60   INT       Base;         /* Base to display nr in the buddy window */
61   INT       CurVal;       /* Current up-down value */
62   INT       MinVal;       /* Minimum up-down value */
63   INT       MaxVal;       /* Maximum up-down value */
64   HWND      Buddy;        /* Handle to the buddy window */
65   CHAR      szBuddyClass[UPDOWN_BUDDYCLASSNAMELEN]; /* Buddy window class name */
66   INT       Flags;        /* Internal Flags FLAG_* */
67 } UPDOWN_INFO;
68
69 /* Control configuration constants */
70
71 #define INITIAL_DELAY    500 /* initial timer until auto-increment kicks in */
72 #define REPEAT_DELAY     50  /* delay between auto-increments */
73
74 #define DEFAULT_WIDTH       14  /* default width of the ctrl */
75 #define DEFAULT_XSEP         0  /* default separation between buddy and crtl */
76 #define DEFAULT_ADDTOP       0  /* amount to extend above the buddy window */
77 #define DEFAULT_ADDBOT       0  /* amount to extend below the buddy window */
78 #define DEFAULT_BUDDYBORDER  2  /* Width/height of the buddy border */
79
80
81 /* Work constants */
82
83 #define FLAG_INCR        0x01
84 #define FLAG_DECR        0x02
85 #define FLAG_MOUSEIN     0x04
86 #define FLAG_CLICKED     (FLAG_INCR | FLAG_DECR)
87
88 #define TIMERID1         1
89 #define TIMERID2         2
90 #define BUDDY_UPDOWN_HWND        "buddyUpDownHWND"
91 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
92
93 #define UNKNOWN_PARAM(msg, wParam, lParam) WARN(\
94         "Unknown parameter(s) for message " #msg \
95         "(%04x): wp=%04x lp=%08lx\n", msg, wParam, lParam);
96
97 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
98
99 static LRESULT CALLBACK
100 UPDOWN_Buddy_SubclassProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
101
102 /***********************************************************************
103  *           UPDOWN_InBounds
104  * Tests if a given value 'val' is between the Min&Max limits
105  */
106 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
107 {
108   if(infoPtr->MaxVal > infoPtr->MinVal)
109     return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
110   else
111     return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
112 }
113
114 /***********************************************************************
115  *           UPDOWN_OffsetVal
116  * Tests if we can change the current value by delta. If so, it changes
117  * it and returns TRUE. Else, it leaves it unchanged and returns FALSE.
118  */
119 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
120 {
121   /* check if we can do the modification first */
122   if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)){
123     if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP)
124     {
125       delta += (delta < 0 ? -1 : 1) *
126         (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
127         (infoPtr->MinVal - infoPtr->MaxVal) +
128         (delta < 0 ? 1 : -1);
129     }
130     else
131       return FALSE;
132   }
133
134   infoPtr->CurVal += delta;
135   return TRUE;
136 }
137
138 /***********************************************************************
139  * UPDOWN_HasBuddyBorder [Internal]
140  *
141  * When we have a buddy set and that we are aligned on our buddy, we
142  * want to draw a sunken edge to make like we are part of that control.
143  */
144 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
145 {  
146   DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
147
148   return  ( ((dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
149             (SendMessageW(infoPtr->Self, UDM_GETBUDDY, 0, 0) != 0) &&
150             (lstrcmpiA(infoPtr->szBuddyClass, "EDIT") == 0 ) );
151 }
152
153 /***********************************************************************
154  *           UPDOWN_GetArrowRect
155  * wndPtr   - pointer to the up-down wnd
156  * rect     - will hold the rectangle
157  * incr     - TRUE  get the "increment" rect (up or right)
158  *            FALSE get the "decrement" rect (down or left)
159  */
160 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, BOOL incr)
161 {
162   DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
163   int   len; /* will hold the width or height */
164
165   GetClientRect (infoPtr->Self, rect);
166
167   /*
168    * Make sure we calculate the rectangle to fit even if we draw the
169    * border.
170    */
171   if (UPDOWN_HasBuddyBorder(infoPtr)) {
172     if (dwStyle & UDS_ALIGNLEFT)
173       rect->left += DEFAULT_BUDDYBORDER;
174     else
175       rect->right -= DEFAULT_BUDDYBORDER;
176     
177     InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
178   }
179
180   /*
181    * We're calculating the midpoint to figure-out where the
182    * separation between the buttons will lay. We make sure that we
183    * round the uneven numbers by adding 1.
184    */
185   if (dwStyle & UDS_HORZ) {
186     len = rect->right - rect->left + 1; /* compute the width */
187     if (incr)
188       rect->left = rect->left + len/2; 
189     else
190       rect->right =  rect->left + len/2;
191   } else {
192     len = rect->bottom - rect->top + 1; /* compute the height */
193     if (incr)
194       rect->bottom =  rect->top + len/2;
195     else
196       rect->top =  rect->top + len/2;
197   }
198 }
199
200 /***********************************************************************
201  *           UPDOWN_GetArrowFromPoint
202  * Returns the rectagle (for the up or down arrow) that contains pt.
203  * If it returns the up rect, it returns TRUE.
204  * If it returns the down rect, it returns FALSE.
205  */
206 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
207 {
208   UPDOWN_GetArrowRect (infoPtr, rect, TRUE);
209   if(PtInRect(rect, pt)) return TRUE;
210
211   UPDOWN_GetArrowRect (infoPtr, rect, FALSE);
212   return FALSE;
213 }
214
215
216 /***********************************************************************
217  *           UPDOWN_GetThousandSep
218  * Returns the thousand sep. If an error occurs, it returns ','.
219  */
220 static CHAR UPDOWN_GetThousandSep()
221 {
222   CHAR sep[2];
223
224   if(GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
225     return ',';
226
227   return sep[0];
228 }
229
230 /***********************************************************************
231  *           UPDOWN_GetBuddyInt
232  * Tries to read the pos from the buddy window and if it succeeds,
233  * it stores it in the control's CurVal
234  * returns:
235  *   TRUE  - if it read the integer from the buddy successfully
236  *   FALSE - if an error occurred
237  */
238 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
239 {
240   char txt[20], sep, *src, *dst;
241   int newVal;
242
243   if (!IsWindow(infoPtr->Buddy))
244     return FALSE;
245
246   /*if the buddy is a list window, we must set curr index */
247   if (!lstrcmpA (infoPtr->szBuddyClass, "ListBox")){
248     newVal = SendMessageA(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
249     if(newVal < 0)
250       return FALSE;
251   }
252   else{
253     /* we have a regular window, so will get the text */
254     if (!GetWindowTextA(infoPtr->Buddy, txt, sizeof(txt)))
255       return FALSE;
256
257     sep = UPDOWN_GetThousandSep(); 
258
259     /* now get rid of the separators */
260     for(src = dst = txt; *src; src++)
261       if(*src != sep) *dst++ = *src;
262     *dst = 0;
263
264     /* try to convert the number and validate it */
265     newVal = strtol(txt, &src, infoPtr->Base);
266     if(*src || !UPDOWN_InBounds (infoPtr, newVal)) 
267       return FALSE;
268
269     TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
270   }
271   
272   infoPtr->CurVal = newVal;
273   return TRUE;
274 }
275
276
277 /***********************************************************************
278  *           UPDOWN_SetBuddyInt
279  * Tries to set the pos to the buddy window based on current pos
280  * returns:
281  *   TRUE  - if it set the caption of the  buddy successfully
282  *   FALSE - if an error occurred
283  */
284 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
285 {
286   char txt1[20], sep;
287   int len;
288
289   if (!IsWindow(infoPtr->Buddy)) 
290     return FALSE;
291
292   TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
293
294   /*if the buddy is a list window, we must set curr index */
295   if(!lstrcmpA (infoPtr->szBuddyClass, "ListBox")){
296     SendMessageA(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0);
297   }
298   else{ /* Regular window, so set caption to the number */
299     len = sprintf(txt1, (infoPtr->Base==16) ? "%X" : "%d", infoPtr->CurVal);
300
301     sep = UPDOWN_GetThousandSep(); 
302
303     /* Do thousands seperation if necessary */
304     if (!(GetWindowLongA (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
305       char txt2[20], *src = txt1, *dst = txt2;
306       if(len % 3 > 0) {
307         lstrcpynA (dst, src, len%3 + 1);      /* need to include the null */ 
308         dst += len%3;
309         src += len%3;
310       }
311       for(len=0; *src; len++) {
312         if(len%3==0) *dst++ = sep;
313         *dst++ = *src++;
314       }
315       *dst = 0;           /* null terminate it */
316       strcpy(txt1, txt2); /* move it to the proper place */
317     }
318     SetWindowTextA(infoPtr->Buddy, txt1);
319   }
320
321   return TRUE;
322
323
324 /***********************************************************************
325  * UPDOWN_DrawBuddyBorder [Internal]
326  *
327  * When we have a buddy set and that we are aligned on our buddy, we
328  * want to draw a sunken edge to make like we are part of that control.
329  */
330 static void UPDOWN_DrawBuddyBorder (UPDOWN_INFO *infoPtr, HDC hdc)
331 {
332   DWORD dwStyle = GetWindowLongA (infoPtr->Self, GWL_STYLE);
333   RECT  clientRect;
334
335   GetClientRect(infoPtr->Self, &clientRect);
336
337   if (dwStyle & UDS_ALIGNLEFT)
338     DrawEdge(hdc, &clientRect, EDGE_SUNKEN, BF_BOTTOM | BF_LEFT | BF_TOP);
339   else
340     DrawEdge(hdc, &clientRect, EDGE_SUNKEN, BF_BOTTOM | BF_RIGHT | BF_TOP);
341 }
342
343 /***********************************************************************
344  * UPDOWN_Draw [Internal]
345  *
346  * Draw the arrows. The background need not be erased.
347  */
348 static void UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
349 {
350   DWORD dwStyle = GetWindowLongA (infoPtr->Self, GWL_STYLE);
351   BOOL prssed;
352   RECT rect;
353
354   /*
355    * Draw the common border between ourselves and our buddy.
356    */
357   if (UPDOWN_HasBuddyBorder(infoPtr))
358     UPDOWN_DrawBuddyBorder(infoPtr, hdc);
359   
360   /* Draw the incr button */
361   UPDOWN_GetArrowRect (infoPtr, &rect, TRUE);
362   prssed = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
363   DrawFrameControl(hdc, &rect, DFC_SCROLL, 
364         (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
365         (prssed ? DFCS_PUSHED : 0) |
366         (dwStyle&WS_DISABLED ? DFCS_INACTIVE : 0) );
367
368   /* Draw the space between the buttons */
369   rect.top = rect.bottom; rect.bottom++;
370   DrawEdge(hdc, &rect, 0, BF_MIDDLE);
371                     
372   /* Draw the decr button */
373   UPDOWN_GetArrowRect(infoPtr, &rect, FALSE);
374   prssed = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
375   DrawFrameControl(hdc, &rect, DFC_SCROLL, 
376         (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
377         (prssed ? DFCS_PUSHED : 0) |
378         (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
379 }
380
381 /***********************************************************************
382  * UPDOWN_Refresh [Internal]
383  *
384  * Synchronous drawing (must NOT be used in WM_PAINT).
385  * Calls UPDOWN_Draw.
386  */
387 static void UPDOWN_Refresh (UPDOWN_INFO *infoPtr)
388 {
389     HDC hdc = GetDC (infoPtr->Self);
390     UPDOWN_Draw (infoPtr, hdc);
391     ReleaseDC (infoPtr->Self, hdc);
392 }
393
394
395 /***********************************************************************
396  * UPDOWN_Paint [Internal]
397  *
398  * Asynchronous drawing (must ONLY be used in WM_PAINT).
399  * Calls UPDOWN_Draw.
400  */
401 static void UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
402 {
403     if (hdc) {
404       UPDOWN_Draw (infoPtr, hdc);
405     } else {
406       PAINTSTRUCT ps;
407
408       hdc = BeginPaint (infoPtr->Self, &ps);
409       UPDOWN_Draw (infoPtr, hdc);
410       EndPaint (infoPtr->Self, &ps);
411     }
412 }
413
414 /***********************************************************************
415  *           UPDOWN_SetBuddy
416  * Tests if 'bud' is a valid window handle. If not, returns FALSE.
417  * Else, sets it as a new Buddy.
418  * Then, it should subclass the buddy 
419  * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
420  * process the UP/DOWN arrow keys.
421  * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
422  * the size/pos of the buddy and the control are adjusted accordingly.
423  */
424 static BOOL UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
425 {
426   DWORD        dwStyle = GetWindowLongA (infoPtr->Self, GWL_STYLE);
427   RECT         budRect;  /* new coord for the buddy */
428   int          x,width;  /* new x position and width for the up-down */
429   WNDPROC baseWndProc, currWndProc;
430           
431   /* Is it a valid bud? */
432   if(!IsWindow(bud)) return FALSE;
433
434   /* there is already a body assigned */
435   if ( infoPtr->Buddy )  
436     RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
437
438   /* Store buddy window handle */
439   infoPtr->Buddy = bud;   
440
441   /* keep upDown ctrl hwnd in a buddy property */            
442   SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self); 
443
444   /* Store buddy window clas name */
445   memset(infoPtr->szBuddyClass, 0, UPDOWN_BUDDYCLASSNAMELEN);
446   GetClassNameA (bud, infoPtr->szBuddyClass, UPDOWN_BUDDYCLASSNAMELEN-1);
447
448   if(dwStyle & UDS_ARROWKEYS){
449     /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property 
450        when we reset the upDown ctrl buddy to another buddy because it is not 
451        good to break the window proc chain. */
452
453         currWndProc = (WNDPROC) GetWindowLongA(bud, GWL_WNDPROC);
454         if (currWndProc != UPDOWN_Buddy_SubclassProc) 
455         {
456                 // replace the buddy's WndProc with ours
457                 baseWndProc = (WNDPROC)SetWindowLongA(bud, GWL_WNDPROC,
458                               (LPARAM)UPDOWN_Buddy_SubclassProc); 
459                 // and save the base class' WndProc 
460                 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
461         }
462         // else
463         // its already been subclassed, don't overwrite BUDDY_SUPERCLASS_WNDPROC
464   }
465
466   /* Get the rect of the buddy relative to its parent */
467   GetWindowRect(infoPtr->Buddy, &budRect);
468   MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy),
469                   (POINT *)(&budRect.left), 2);
470
471   /* now do the positioning */
472   if  (dwStyle & UDS_ALIGNLEFT) {
473     x  = budRect.left;
474     budRect.left += DEFAULT_WIDTH+DEFAULT_XSEP;
475   }
476   else if (dwStyle & UDS_ALIGNRIGHT){
477     budRect.right -= DEFAULT_WIDTH+DEFAULT_XSEP;
478     x  = budRect.right+DEFAULT_XSEP;
479   }
480   else {
481     x  = budRect.right+DEFAULT_XSEP;
482   }
483
484   /* first adjust the buddy to accomodate the up/down */
485   SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
486                budRect.right  - budRect.left, budRect.bottom - budRect.top, 
487                SWP_NOACTIVATE|SWP_NOZORDER);
488
489   /* now position the up/down */
490   /* Since the UDS_ALIGN* flags were used, */
491   /* we will pick the position and size of the window. */
492   width = DEFAULT_WIDTH;
493
494   /*
495    * If the updown has a buddy border, it has to overlap with the buddy
496    * to look as if it is integrated with the buddy control. 
497    * We nudge the control or change it size to overlap.
498    */
499   if (UPDOWN_HasBuddyBorder(infoPtr))
500   {
501     if(dwStyle & UDS_ALIGNLEFT)
502       width+=DEFAULT_BUDDYBORDER;
503     else
504       x-=DEFAULT_BUDDYBORDER;
505   }
506
507   SetWindowPos (infoPtr->Self, infoPtr->Buddy, 
508                 x, budRect.top-DEFAULT_ADDTOP,
509                 width, (budRect.bottom-budRect.top)+DEFAULT_ADDTOP+DEFAULT_ADDBOT,
510                 SWP_NOACTIVATE);
511
512   return TRUE;
513 }         
514
515 /***********************************************************************
516  *           UPDOWN_DoAction
517  *
518  * This function increments/decrements the CurVal by the 
519  * 'delta' amount according to the 'incr' flag
520  * It notifies the parent as required.
521  * It handles wraping and non-wraping correctly.
522  * It is assumed that delta>0
523  */
524 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, BOOL incr)
525 {
526   DWORD dwStyle = GetWindowLongA (infoPtr->Self, GWL_STYLE);
527   NM_UPDOWN ni;
528
529   TRACE("%s by %d\n", incr ? "inc" : "dec", delta);
530
531   /* check if we can do the modification first */
532   delta *= (incr ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
533
534   /* We must notify parent now to obtain permission */
535   ni.iPos = infoPtr->CurVal;
536   ni.iDelta = delta;
537   ni.hdr.hwndFrom = infoPtr->Self;
538   ni.hdr.idFrom   = GetWindowLongA (infoPtr->Self, GWL_ID);
539   ni.hdr.code = UDN_DELTAPOS; 
540   if (!SendMessageA(GetParent (infoPtr->Self), WM_NOTIFY,
541                    (WPARAM)ni.hdr.idFrom, (LPARAM)&ni))
542   {
543      /* Parent said: OK to adjust */
544
545      /* Now adjust value with (maybe new) delta */
546      if (UPDOWN_OffsetVal (infoPtr, ni.iDelta))
547      {
548         /* Now take care about our buddy */
549         if(infoPtr->Buddy && IsWindow(infoPtr->Buddy)
550            && (dwStyle & UDS_SETBUDDYINT) )
551            UPDOWN_SetBuddyInt (infoPtr);
552      }
553   }
554   
555   /* Also, notify it. This message is sent in any case. */
556   SendMessageA (GetParent (infoPtr->Self), 
557                 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL, 
558                  MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), infoPtr->Self);
559 }
560
561 /***********************************************************************
562  *           UPDOWN_IsEnabled
563  *
564  * Returns TRUE if it is enabled as well as its buddy (if any)
565  *         FALSE otherwise
566  */
567 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
568 {
569   if(GetWindowLongA (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
570     return FALSE;
571   if(infoPtr->Buddy)
572     return IsWindowEnabled(infoPtr->Buddy);
573   return TRUE;
574 }
575
576 /***********************************************************************
577  *           UPDOWN_CancelMode
578  *
579  * Deletes any timers, releases the mouse and does  redraw if necessary.
580  * If the control is not in "capture" mode, it does nothing.
581  * If the control was not in cancel mode, it returns FALSE. 
582  * If the control was in cancel mode, it returns TRUE.
583  */
584 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
585 {
586   /* if not in 'capture' mode, do nothing */
587   if(!(infoPtr->Flags & FLAG_CLICKED))
588     return FALSE;
589
590   KillTimer (infoPtr->Self, TIMERID1); /* kill all possible timers */
591   KillTimer (infoPtr->Self, TIMERID2);
592   
593   if (GetCapture() == infoPtr->Self)   /* let the mouse go         */
594     ReleaseCapture();                  /* if we still have it      */  
595   
596   infoPtr->Flags = 0;                  /* get rid of any flags     */
597   UPDOWN_Refresh (infoPtr);            /* redraw the control just in case */
598   
599   return TRUE;
600 }
601
602 /***********************************************************************
603  *           UPDOWN_HandleMouseEvent
604  *
605  * Handle a mouse event for the updown.
606  * 'pt' is the location of the mouse event in client or
607  * windows coordinates. 
608  */
609 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINT pt)
610 {
611   DWORD dwStyle = GetWindowLongA (infoPtr->Self, GWL_STYLE);
612   RECT rect;
613   int temp;
614
615   switch(msg)
616     {
617     case WM_LBUTTONDOWN:  /* Initialise mouse tracking */
618       /* If we are already in the 'clicked' mode, then nothing to do */
619       if(infoPtr->Flags & FLAG_CLICKED)
620         return;
621
622       /* If the buddy is an edit, will set focus to it */
623       if (!lstrcmpA (infoPtr->szBuddyClass, "Edit"))
624         SetFocus(infoPtr->Buddy);
625
626       /* Now see which one is the 'active' arrow */
627       temp = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
628
629       /* Update the CurVal if necessary */
630       if (dwStyle & UDS_SETBUDDYINT)
631         UPDOWN_GetBuddyInt (infoPtr);
632         
633       /* Set up the correct flags */
634       infoPtr->Flags  = 0; 
635       infoPtr->Flags |= temp ? FLAG_INCR : FLAG_DECR;
636       infoPtr->Flags |= FLAG_MOUSEIN;
637       
638       /* repaint the control */
639       UPDOWN_Refresh (infoPtr);
640
641       /* process the click */
642       UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_INCR);
643
644       /* now capture all mouse messages */
645       SetCapture (infoPtr->Self);
646
647       /* and startup the first timer */
648       SetTimer(infoPtr->Self, TIMERID1, INITIAL_DELAY, 0); 
649       break;
650
651     case WM_MOUSEMOVE:
652       /* If we are not in the 'clicked' mode, then nothing to do */
653       if(!(infoPtr->Flags & FLAG_CLICKED))
654         return;
655
656       /* save the flags to see if any got modified */
657       temp = infoPtr->Flags;
658
659       /* Now get the 'active' arrow rectangle */
660       if (infoPtr->Flags & FLAG_INCR)
661         UPDOWN_GetArrowRect (infoPtr, &rect, TRUE);
662       else
663         UPDOWN_GetArrowRect (infoPtr, &rect, FALSE);
664
665       /* Update the flags if we are in/out */
666       if(PtInRect(&rect, pt))
667         infoPtr->Flags |=  FLAG_MOUSEIN;
668       else{
669         infoPtr->Flags &= ~FLAG_MOUSEIN;
670         if(infoPtr->AccelIndex != -1) /* if we have accel info */
671           infoPtr->AccelIndex = 0;    /* reset it              */
672       }
673       /* If state changed, redraw the control */
674       if(temp != infoPtr->Flags)
675         UPDOWN_Refresh (infoPtr);
676       break;
677
678       default:
679         ERR("Impossible case!\n");
680     }
681
682 }
683
684 /***********************************************************************
685  *           UpDownWndProc
686  */
687 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
688                                 LPARAM lParam)
689 {
690   UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
691   DWORD dwStyle = GetWindowLongA (hwnd, GWL_STYLE);
692   int temp;
693   if (!infoPtr && (message != WM_CREATE) && (message != WM_NCCREATE))
694       return DefWindowProcA (hwnd, message, wParam, lParam); 
695   switch(message)
696     {
697     case WM_NCCREATE:
698       /* get rid of border, if any */
699       SetWindowLongA (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
700       return TRUE;
701
702     case WM_CREATE:
703       infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
704       SetWindowLongA (hwnd, 0, (DWORD)infoPtr);
705
706       /* initialize the info struct */
707       infoPtr->Self = hwnd;
708       infoPtr->AccelCount = 0;
709       infoPtr->AccelVect = 0;
710       infoPtr->AccelIndex = -1;
711       infoPtr->CurVal = 0; 
712       infoPtr->MinVal = 0; 
713       infoPtr->MaxVal = 9999;
714       infoPtr->Base  = 10; /* Default to base 10  */
715       infoPtr->Buddy = 0;  /* No buddy window yet */
716       infoPtr->Flags = 0;  /* And no flags        */
717
718       /* Do we pick the buddy win ourselves? */
719       if (dwStyle & UDS_AUTOBUDDY)
720         UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
721         
722       TRACE("UpDown Ctrl creation, hwnd=%04x\n", hwnd);
723       break;
724     
725     case WM_DESTROY:
726       if(infoPtr->AccelVect)
727         COMCTL32_Free (infoPtr->AccelVect);
728
729       if ( IsWindow(infoPtr->Buddy) ) /* Cleanup */
730         RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
731
732       COMCTL32_Free (infoPtr);
733       SetWindowLongA (hwnd, 0, 0);
734       TRACE("UpDown Ctrl destruction, hwnd=%04x\n", hwnd);
735       break;
736         
737     case WM_ENABLE:
738       if (dwStyle & WS_DISABLED)
739         UPDOWN_CancelMode (infoPtr);
740
741       UPDOWN_Refresh (infoPtr);
742       break;
743
744     case WM_TIMER:
745       /* if initial timer, kill it and start the repeat timer */
746       if(wParam == TIMERID1){
747         KillTimer(hwnd, TIMERID1);
748         /* if no accel info given, used default timer */
749         if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0){
750           infoPtr->AccelIndex = -1;
751           temp = REPEAT_DELAY;
752         }
753         else{
754           infoPtr->AccelIndex = 0; /* otherwise, use it */
755           temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
756         }
757         SetTimer(hwnd, TIMERID2, temp, 0);
758       }
759
760       /* now, if the mouse is above us, do the thing...*/
761       if(infoPtr->Flags & FLAG_MOUSEIN){
762         temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
763         UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_INCR);
764         
765         if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1){
766           KillTimer(hwnd, TIMERID2);
767           infoPtr->AccelIndex++; /* move to the next accel info */
768           temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
769           /* make sure we have at least 1ms intervals */
770           SetTimer(hwnd, TIMERID2, temp, 0);        
771         }
772       }
773       break;
774
775     case WM_CANCELMODE:
776       UPDOWN_CancelMode (infoPtr);
777       break;
778
779     case WM_LBUTTONUP:
780       if(!UPDOWN_CancelMode(infoPtr))
781         break;
782
783       SendMessageA(GetParent(hwnd), dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
784                   MAKELONG(SB_ENDSCROLL, infoPtr->CurVal), hwnd);
785
786       /*If we released the mouse and our buddy is an edit */
787       /* we must select all text in it.                   */
788       if (!lstrcmpA (infoPtr->szBuddyClass, "Edit"))
789           SendMessageA(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
790       break;
791       
792     case WM_LBUTTONDOWN:
793     case WM_MOUSEMOVE:
794       if(UPDOWN_IsEnabled(infoPtr)){
795         POINT pt;
796         pt.x = SLOWORD(lParam);
797         pt.y = SHIWORD(lParam);
798         UPDOWN_HandleMouseEvent (infoPtr, message, pt );
799       }
800     break;
801
802     case WM_KEYDOWN:
803       if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr)){
804         switch(wParam){
805         case VK_UP:  
806         case VK_DOWN:
807           UPDOWN_GetBuddyInt (infoPtr);
808           /* FIXME: Paint the according button pressed for some time, like win95 does*/
809           UPDOWN_DoAction (infoPtr, 1, wParam==VK_UP);
810           break;
811         }
812       }
813       break;
814       
815     case WM_PAINT:
816       UPDOWN_Paint (infoPtr, (HDC)wParam);
817       break;
818     
819     case UDM_GETACCEL:
820       if (wParam==0 && lParam==0)    /*if both zero, */
821         return infoPtr->AccelCount;  /*just return the accel count*/
822       if (wParam || lParam){
823         UNKNOWN_PARAM(UDM_GETACCEL, wParam, lParam);
824         return 0;
825       }
826       temp = min(infoPtr->AccelCount, wParam);
827       memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
828       return temp;
829
830     case UDM_SETACCEL:
831       TRACE("UpDown Ctrl new accel info, hwnd=%04x\n", hwnd);
832       if(infoPtr->AccelVect){
833         COMCTL32_Free (infoPtr->AccelVect);
834         infoPtr->AccelCount = 0;
835         infoPtr->AccelVect  = 0;
836       }
837       if(wParam==0)
838         return TRUE;
839       infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
840       if(infoPtr->AccelVect==0)
841         return FALSE;
842       memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
843       return TRUE;
844
845     case UDM_GETBASE:
846       if (wParam || lParam)
847         UNKNOWN_PARAM(UDM_GETBASE, wParam, lParam);
848       return infoPtr->Base;
849
850     case UDM_SETBASE:
851       TRACE("UpDown Ctrl new base(%d), hwnd=%04x\n", wParam, hwnd);
852       if ( !(wParam==10 || wParam==16) || lParam)
853         UNKNOWN_PARAM(UDM_SETBASE, wParam, lParam);
854       if (wParam==10 || wParam==16){
855         temp = infoPtr->Base;
856         infoPtr->Base = wParam;
857         return temp;       /* return the prev base */
858       }
859       break;
860
861     case UDM_GETBUDDY:
862       if (wParam || lParam)
863         UNKNOWN_PARAM(UDM_GETBUDDY, wParam, lParam);
864       return infoPtr->Buddy;
865
866     case UDM_SETBUDDY:
867       if (lParam)
868         UNKNOWN_PARAM(UDM_SETBUDDY, wParam, lParam);
869       temp = infoPtr->Buddy;
870       UPDOWN_SetBuddy (infoPtr, wParam);
871       TRACE("UpDown Ctrl new buddy(%04x), hwnd=%04x\n", infoPtr->Buddy, hwnd);
872       return temp;
873
874     case UDM_GETPOS:
875       if (wParam || lParam)
876         UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
877       temp = UPDOWN_GetBuddyInt (infoPtr);
878       return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
879
880     case UDM_SETPOS:
881       if (wParam || HIWORD(lParam))
882         UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
883       temp = SLOWORD(lParam);
884       TRACE("UpDown Ctrl new value(%d), hwnd=%04x\n", temp, hwnd);
885       if(!UPDOWN_InBounds(infoPtr, temp)){
886         if(temp < infoPtr->MinVal)  
887           temp = infoPtr->MinVal;
888         if(temp > infoPtr->MaxVal)
889           temp = infoPtr->MaxVal;
890       }
891       wParam = infoPtr->CurVal; /* save prev value   */
892       infoPtr->CurVal = temp;   /* set the new value */
893       if(dwStyle & UDS_SETBUDDYINT)
894         UPDOWN_SetBuddyInt (infoPtr);
895       return wParam;            /* return prev value */
896       
897     case UDM_GETRANGE:
898       if (wParam || lParam)
899         UNKNOWN_PARAM(UDM_GETRANGE, wParam, lParam);
900       return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
901
902     case UDM_SETRANGE:
903       if (wParam)
904         UNKNOWN_PARAM(UDM_SETRANGE, wParam, lParam); /* we must have:     */
905       infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
906       infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
907                                          /* |Max-Min| <= UD_MAXVAL        */
908       TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n", 
909             infoPtr->MinVal, infoPtr->MaxVal, hwnd);
910       break;                             
911
912     case UDM_GETRANGE32:
913       if (wParam)
914         *(LPINT)wParam = infoPtr->MinVal;
915       if (lParam)
916         *(LPINT)lParam = infoPtr->MaxVal;
917       break;
918
919     case UDM_SETRANGE32:
920       infoPtr->MinVal = (INT)wParam;
921       infoPtr->MaxVal = (INT)lParam;
922       if (infoPtr->MaxVal <= infoPtr->MinVal)
923         infoPtr->MaxVal = infoPtr->MinVal + 1;
924       TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n", 
925             infoPtr->MinVal, infoPtr->MaxVal, hwnd);
926       break;
927
928     case UDM_GETPOS32:
929       if ((LPBOOL)lParam != NULL)
930         *((LPBOOL)lParam) = TRUE;
931       return infoPtr->CurVal;
932
933     case UDM_SETPOS32:
934       if(!UPDOWN_InBounds(infoPtr, (int)lParam)){
935         if((int)lParam < infoPtr->MinVal)
936           lParam = infoPtr->MinVal;
937         if((int)lParam > infoPtr->MaxVal)
938           lParam = infoPtr->MaxVal;
939       }
940       temp = infoPtr->CurVal; /* save prev value   */
941       infoPtr->CurVal = (int)lParam;   /* set the new value */
942       if(dwStyle & UDS_SETBUDDYINT)
943         UPDOWN_SetBuddyInt (infoPtr);
944       return temp;            /* return prev value */
945
946     default: 
947       if (message >= WM_USER) 
948         ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
949       return DefWindowProcA (hwnd, message, wParam, lParam); 
950     } 
951
952     return 0;
953 }
954
955 /***********************************************************************
956  * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy 
957  *                           control.
958  */
959 LRESULT CALLBACK
960 UPDOWN_Buddy_SubclassProc (
961   HWND   hwnd, 
962   UINT   uMsg, 
963   WPARAM wParam, 
964   LPARAM lParam)
965 {
966   WNDPROC superClassWndProc = (WNDPROC)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
967   TRACE("hwnd=%04x, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n", 
968          hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
969
970   switch (uMsg) 
971   {
972     case WM_KEYDOWN:
973     {
974       if ( ((int)wParam == VK_UP ) || ((int)wParam == VK_DOWN ) )
975       {
976         HWND upDownHwnd      = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
977         UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(upDownHwnd);
978       
979         if (!lstrcmpA (infoPtr->szBuddyClass, "ListBox"))
980         {
981           /* if the buddy is a list window, we must update curr index */
982           INT oldVal = SendMessageA(hwnd, LB_GETCURSEL, 0, 0);
983           SendMessageA(hwnd, LB_SETCURSEL, oldVal+1, 0);
984         }
985         else
986         {
987           UPDOWN_GetBuddyInt(infoPtr);
988           UPDOWN_DoAction(infoPtr, 1, wParam==VK_UP);
989         }
990
991         break;
992       }
993       /* else Fall Through */
994     }
995   }
996   return CallWindowProcA( superClassWndProc, hwnd, uMsg, wParam, lParam);
997 }
998
999 /***********************************************************************
1000  *              UPDOWN_Register [Internal]
1001  *
1002  * Registers the updown window class.
1003  */
1004
1005 VOID
1006 UPDOWN_Register(void)
1007 {
1008     WNDCLASSA wndClass;
1009
1010     ZeroMemory( &wndClass, sizeof( WNDCLASSA ) );
1011     wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW;
1012     wndClass.lpfnWndProc   = (WNDPROC)UpDownWindowProc;
1013     wndClass.cbClsExtra    = 0;
1014     wndClass.cbWndExtra    = sizeof(UPDOWN_INFO*);
1015     wndClass.hCursor       = LoadCursorA( 0, IDC_ARROWA );
1016     wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
1017     wndClass.lpszClassName = UPDOWN_CLASSA;
1018  
1019     RegisterClassA( &wndClass );
1020 }
1021
1022
1023 /***********************************************************************
1024  *              UPDOWN_Unregister       [Internal]
1025  *
1026  * Unregisters the updown window class.
1027  */
1028
1029 VOID
1030 UPDOWN_Unregister (void)
1031 {
1032     UnregisterClassA (UPDOWN_CLASSA, (HINSTANCE)NULL);
1033 }
1034