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