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