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