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