Add struct StorageBaseImpl at the start of derived structures instead
[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         if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
277
278         sep = UPDOWN_GetThousandSep();
279
280         /* now get rid of the separators */
281         for(src = dst = txt; *src; src++)
282             if(*src != sep) *dst++ = *src;
283         *dst = 0;
284
285         /* try to convert the number and validate it */
286         newVal = strtolW(txt, &src, infoPtr->Base);
287         if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
288     }
289
290     TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
291     infoPtr->CurVal = newVal;
292     return TRUE;
293 }
294
295
296 /***********************************************************************
297  *           UPDOWN_SetBuddyInt
298  * Tries to set the pos to the buddy window based on current pos
299  * returns:
300  *   TRUE  - if it set the caption of the  buddy successfully
301  *   FALSE - if an error occurred
302  */
303 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
304 {
305     WCHAR fmt[3] = { '%', 'd', '\0' };
306     WCHAR txt[20];
307     int len;
308
309     if (!IsWindow(infoPtr->Buddy)) return FALSE;
310
311     TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
312
313     /*if the buddy is a list window, we must set curr index */
314     if (UPDOWN_IsBuddyListbox(infoPtr)) {
315         return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
316     }
317
318     /* Regular window, so set caption to the number */
319     if (infoPtr->Base == 16) fmt[1] = 'X';
320     len = wsprintfW(txt, fmt, infoPtr->CurVal);
321
322
323     /* Do thousands separation if necessary */
324     if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
325         WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
326         WCHAR sep = UPDOWN_GetThousandSep();
327         int start = len % 3;
328
329         memcpy(tmp, txt, sizeof(txt));
330         if (start == 0) start = 3;
331         dst += start;
332         src += start;
333         for (len=0; *src; len++) {
334             if (len % 3 == 0) *dst++ = sep;
335             *dst++ = *src++;
336         }
337         *dst = 0;
338     }
339
340     return SetWindowTextW(infoPtr->Buddy, txt);
341 }
342
343 /***********************************************************************
344  * UPDOWN_Draw
345  *
346  * Draw the arrows. The background need not be erased.
347  */
348 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
349 {
350     DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
351     BOOL pressed, hot;
352     RECT rect;
353
354     /* Draw the common border between ourselves and our buddy */
355     if (UPDOWN_HasBuddyBorder(infoPtr)) {
356         GetClientRect(infoPtr->Self, &rect);
357         DrawEdge(hdc, &rect, EDGE_SUNKEN,
358                  BF_BOTTOM | BF_TOP |
359                  (dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
360     }
361
362     /* Draw the incr button */
363     UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
364     pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
365     hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
366     DrawFrameControl(hdc, &rect, DFC_SCROLL,
367         (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
368         ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
369         (pressed ? DFCS_PUSHED : 0) |
370         (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
371
372     /* Draw the decr button */
373     UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
374     pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
375     hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
376     DrawFrameControl(hdc, &rect, DFC_SCROLL,
377         (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
378         ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
379         (pressed ? DFCS_PUSHED : 0) |
380         (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
381
382     return 0;
383 }
384
385 /***********************************************************************
386  * UPDOWN_Paint
387  *
388  * Asynchronous drawing (must ONLY be used in WM_PAINT).
389  * Calls UPDOWN_Draw.
390  */
391 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
392 {
393     PAINTSTRUCT ps;
394     if (hdc) return UPDOWN_Draw (infoPtr, hdc);
395     hdc = BeginPaint (infoPtr->Self, &ps);
396     UPDOWN_Draw (infoPtr, hdc);
397     EndPaint (infoPtr->Self, &ps);
398     return 0;
399 }
400
401 /***********************************************************************
402  * UPDOWN_KeyPressed
403  *
404  * Handle key presses (up & down) when we have to do so
405  */
406 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
407 {
408     int arrow;
409
410     if (key == VK_UP) arrow = FLAG_INCR;
411     else if (key == VK_DOWN) arrow = FLAG_DECR;
412     else return 1;
413
414     UPDOWN_GetBuddyInt (infoPtr);
415     infoPtr->Flags &= ~FLAG_ARROW;
416     infoPtr->Flags |= FLAG_PRESSED | arrow;
417     InvalidateRect (infoPtr->Self, NULL, FALSE);
418     SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
419     UPDOWN_DoAction (infoPtr, 1, arrow);
420     return 0;
421 }
422
423 /***********************************************************************
424  * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
425  *                           control.
426  */
427 static LRESULT CALLBACK
428 UPDOWN_Buddy_SubclassProc(HWND  hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
429 {
430     WNDPROC superClassWndProc = (WNDPROC)GetPropW(hwnd, BUDDY_SUPERCLASS_WNDPROC);
431
432     TRACE("hwnd=%p, wndProc=%p, uMsg=%04x, wParam=%08x, lParam=%08lx\n",
433           hwnd, superClassWndProc, uMsg, wParam, lParam);
434
435     if (uMsg == WM_KEYDOWN) {
436         HWND upDownHwnd = GetPropW(hwnd, BUDDY_UPDOWN_HWND);
437
438         UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
439     }
440
441     return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
442 }
443
444 /***********************************************************************
445  *           UPDOWN_SetBuddy
446  *
447  * Sets bud as a new Buddy.
448  * Then, it should subclass the buddy
449  * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
450  * process the UP/DOWN arrow keys.
451  * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
452  * the size/pos of the buddy and the control are adjusted accordingly.
453  */
454 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
455 {
456     static const WCHAR editW[] = { 'E', 'd', 'i', 't', 0 };
457     static const WCHAR listboxW[] = { 'L', 'i', 's', 't', 'b', 'o', 'x', 0 };
458     DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
459     RECT  budRect;  /* new coord for the buddy */
460     int   x, width;  /* new x position and width for the up-down */
461     WNDPROC baseWndProc;
462     WCHAR buddyClass[40];
463     HWND ret;
464
465     TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
466
467     ret = infoPtr->Buddy;
468
469     /* there is already a body assigned */
470     if (infoPtr->Buddy)  RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
471
472     if(!IsWindow(bud))
473         bud = 0;
474
475     /* Store buddy window handle */
476     infoPtr->Buddy = bud;
477
478     if(bud) {
479
480         /* keep upDown ctrl hwnd in a buddy property */
481         SetPropW( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
482
483         /* Store buddy window class type */
484         infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
485         if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
486             if (lstrcmpiW(buddyClass, editW) == 0)
487                 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
488             else if (lstrcmpiW(buddyClass, listboxW) == 0)
489                 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
490         }
491
492         if(dwStyle & UDS_ARROWKEYS){
493             /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
494                when we reset the upDown ctrl buddy to another buddy because it is not
495                good to break the window proc chain. */
496             if (!GetPropW(bud, BUDDY_SUPERCLASS_WNDPROC)) {
497                 baseWndProc = (WNDPROC)SetWindowLongPtrW(bud, GWLP_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
498                 SetPropW(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
499             }
500         }
501
502         /* Get the rect of the buddy relative to its parent */
503         GetWindowRect(infoPtr->Buddy, &budRect);
504         MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
505
506         /* now do the positioning */
507         if  (dwStyle & UDS_ALIGNLEFT) {
508             x  = budRect.left;
509             budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
510         } else if (dwStyle & UDS_ALIGNRIGHT) {
511             budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
512             x  = budRect.right+DEFAULT_XSEP;
513         } else {
514             /* nothing to do */
515             return ret;
516         }
517
518         /* first adjust the buddy to accommodate the up/down */
519         SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
520                      budRect.right  - budRect.left, budRect.bottom - budRect.top,
521                      SWP_NOACTIVATE|SWP_NOZORDER);
522
523         /* now position the up/down */
524         /* Since the UDS_ALIGN* flags were used, */
525         /* we will pick the position and size of the window. */
526         width = DEFAULT_WIDTH;
527
528         /*
529          * If the updown has a buddy border, it has to overlap with the buddy
530          * to look as if it is integrated with the buddy control.
531          * We nudge the control or change its size to overlap.
532          */
533         if (UPDOWN_HasBuddyBorder(infoPtr)) {
534             if(dwStyle & UDS_ALIGNLEFT)
535                 width += DEFAULT_BUDDYBORDER;
536             else
537                 x -= DEFAULT_BUDDYBORDER;
538         }
539
540         SetWindowPos(infoPtr->Self, 0, x,
541                      budRect.top - DEFAULT_ADDTOP, width,
542                      budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
543                      SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
544     } else {
545         RECT rect;
546         GetWindowRect(infoPtr->Self, &rect);
547         MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
548         SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
549                      SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
550     }
551     return ret;
552 }
553
554 /***********************************************************************
555  *           UPDOWN_DoAction
556  *
557  * This function increments/decrements the CurVal by the
558  * 'delta' amount according to the 'action' flag which can be a
559  * combination of FLAG_INCR and FLAG_DECR
560  * It notifies the parent as required.
561  * It handles wraping and non-wraping correctly.
562  * It is assumed that delta>0
563  */
564 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
565 {
566     DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
567     NM_UPDOWN ni;
568
569     TRACE("%d by %d\n", action, delta);
570
571     /* check if we can do the modification first */
572     delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
573     if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
574
575     TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
576
577     /* We must notify parent now to obtain permission */
578     ni.iPos = infoPtr->CurVal;
579     ni.iDelta = delta;
580     ni.hdr.hwndFrom = infoPtr->Self;
581     ni.hdr.idFrom   = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
582     ni.hdr.code = UDN_DELTAPOS;
583     if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
584         /* Parent said: OK to adjust */
585
586         /* Now adjust value with (maybe new) delta */
587         if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
588             TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
589
590             /* Now take care about our buddy */
591             if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
592         }
593     }
594
595     /* Also, notify it. This message is sent in any case. */
596     SendMessageW( infoPtr->Notify, dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
597                   MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
598 }
599
600 /***********************************************************************
601  *           UPDOWN_IsEnabled
602  *
603  * Returns TRUE if it is enabled as well as its buddy (if any)
604  *         FALSE otherwise
605  */
606 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
607 {
608     if (!IsWindowEnabled(infoPtr->Self))
609         return FALSE;
610     if(infoPtr->Buddy)
611         return IsWindowEnabled(infoPtr->Buddy);
612     return TRUE;
613 }
614
615 /***********************************************************************
616  *           UPDOWN_CancelMode
617  *
618  * Deletes any timers, releases the mouse and does  redraw if necessary.
619  * If the control is not in "capture" mode, it does nothing.
620  * If the control was not in cancel mode, it returns FALSE.
621  * If the control was in cancel mode, it returns TRUE.
622  */
623 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
624 {
625     if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
626
627     KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
628     KillTimer (infoPtr->Self, TIMER_ACCEL);
629     KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
630
631     if (GetCapture() == infoPtr->Self) {
632         NMHDR hdr;
633         hdr.hwndFrom = infoPtr->Self;
634         hdr.idFrom   = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
635         hdr.code = NM_RELEASEDCAPTURE;
636         SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
637         ReleaseCapture();
638     }
639
640     infoPtr->Flags &= ~FLAG_PRESSED;
641     InvalidateRect (infoPtr->Self, NULL, FALSE);
642
643     return TRUE;
644 }
645
646 /***********************************************************************
647  *           UPDOWN_HandleMouseEvent
648  *
649  * Handle a mouse event for the updown.
650  * 'pt' is the location of the mouse event in client or
651  * windows coordinates.
652  */
653 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
654 {
655     DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
656     POINT pt = { x, y };
657     RECT rect;
658     int temp, arrow;
659
660     TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
661
662     switch(msg)
663     {
664         case WM_LBUTTONDOWN:  /* Initialise mouse tracking */
665
666             /* If the buddy is an edit, will set focus to it */
667             if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
668
669             /* Now see which one is the 'active' arrow */
670             arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
671
672             /* Update the flags if we are in/out */
673             infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
674             if (arrow)
675                 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
676             else
677                 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
678
679             if (infoPtr->Flags & FLAG_ARROW) {
680
681                 /* Update the CurVal if necessary */
682                 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
683
684                 /* Set up the correct flags */
685                 infoPtr->Flags |= FLAG_PRESSED;
686
687                 /* repaint the control */
688                 InvalidateRect (infoPtr->Self, NULL, FALSE);
689
690                 /* process the click */
691                 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
692                 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
693
694                 /* now capture all mouse messages */
695                 SetCapture (infoPtr->Self);
696
697                 /* and startup the first timer */
698                 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
699             }
700             break;
701
702         case WM_MOUSEMOVE:
703             /* save the flags to see if any got modified */
704             temp = infoPtr->Flags;
705
706             /* Now see which one is the 'active' arrow */
707             arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
708
709             /* Update the flags if we are in/out */
710             infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
711             if(arrow) {
712                 infoPtr->Flags |=  FLAG_MOUSEIN | arrow;
713             } else {
714                 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
715             }
716
717             /* If state changed, redraw the control */
718             if(temp != infoPtr->Flags)
719                  InvalidateRect (infoPtr->Self, &rect, FALSE);
720             break;
721
722         default:
723             ERR("Impossible case (msg=%x)!\n", msg);
724     }
725
726 }
727
728 /***********************************************************************
729  *           UpDownWndProc
730  */
731 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
732 {
733     UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
734     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
735     int temp;
736
737     TRACE("hwnd=%p msg=%04x wparam=%08x lparam=%08lx\n", hwnd, message, wParam, lParam);
738
739     if (!infoPtr && (message != WM_CREATE))
740         return DefWindowProcW (hwnd, message, wParam, lParam);
741
742     switch(message)
743     {
744         case WM_CREATE:
745             SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
746             infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
747             SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
748
749             /* initialize the info struct */
750             infoPtr->Self = hwnd;
751             infoPtr->Notify = ((LPCREATESTRUCTA)lParam)->hwndParent;
752             infoPtr->AccelCount = 0;
753             infoPtr->AccelVect = 0;
754             infoPtr->AccelIndex = -1;
755             infoPtr->CurVal = 0;
756             infoPtr->MinVal = 100;
757             infoPtr->MaxVal = 0;
758             infoPtr->Base  = 10; /* Default to base 10  */
759             infoPtr->Buddy = 0;  /* No buddy window yet */
760             infoPtr->Flags = 0;  /* And no flags        */
761
762             /* Do we pick the buddy win ourselves? */
763             if (dwStyle & UDS_AUTOBUDDY)
764                 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
765
766             TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
767             break;
768
769         case WM_DESTROY:
770             if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
771
772             if(infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
773
774             Free (infoPtr);
775             SetWindowLongPtrW (hwnd, 0, 0);
776             TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
777             break;
778
779         case WM_ENABLE:
780             if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
781             InvalidateRect (infoPtr->Self, NULL, FALSE);
782             break;
783
784         case WM_TIMER:
785            /* is this the auto-press timer? */
786            if(wParam == TIMER_AUTOPRESS) {
787                 KillTimer(hwnd, TIMER_AUTOPRESS);
788                 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
789                 InvalidateRect(infoPtr->Self, NULL, FALSE);
790            }
791
792            /* if initial timer, kill it and start the repeat timer */
793            if(wParam == TIMER_AUTOREPEAT) {
794                 KillTimer(hwnd, TIMER_AUTOREPEAT);
795                 /* if no accel info given, used default timer */
796                 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
797                     infoPtr->AccelIndex = -1;
798                     temp = REPEAT_DELAY;
799                 } else {
800                     infoPtr->AccelIndex = 0; /* otherwise, use it */
801                     temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
802                 }
803                 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
804             }
805
806             /* now, if the mouse is above us, do the thing...*/
807             if(infoPtr->Flags & FLAG_MOUSEIN) {
808                 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
809                 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
810
811                 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
812                     KillTimer(hwnd, TIMER_ACCEL);
813                     infoPtr->AccelIndex++; /* move to the next accel info */
814                     temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
815                     /* make sure we have at least 1ms intervals */
816                     SetTimer(hwnd, TIMER_ACCEL, temp, 0);
817                 }
818             }
819             break;
820
821         case WM_CANCELMODE:
822           return UPDOWN_CancelMode (infoPtr);
823
824         case WM_LBUTTONUP:
825             if (GetCapture() != infoPtr->Self) break;
826
827             if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
828                  (infoPtr->Flags & FLAG_ARROW) ) {
829
830                 SendMessageW( infoPtr->Notify,
831                               dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
832                               MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
833                               (LPARAM)hwnd);
834                 if (UPDOWN_IsBuddyEdit(infoPtr))
835                     SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
836             }
837             UPDOWN_CancelMode(infoPtr);
838             break;
839
840         case WM_LBUTTONDOWN:
841         case WM_MOUSEMOVE:
842             if(UPDOWN_IsEnabled(infoPtr))
843                 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
844             break;
845
846         case WM_KEYDOWN:
847             if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
848                 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
849             break;
850
851         case WM_PAINT:
852             return UPDOWN_Paint (infoPtr, (HDC)wParam);
853
854         case UDM_GETACCEL:
855             if (wParam==0 && lParam==0) return infoPtr->AccelCount;
856             if (wParam && lParam) {
857                 temp = min(infoPtr->AccelCount, wParam);
858                 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
859                 return temp;
860             }
861             return 0;
862
863         case UDM_SETACCEL:
864             TRACE("UDM_SETACCEL\n");
865
866             if(infoPtr->AccelVect) {
867                 Free (infoPtr->AccelVect);
868                 infoPtr->AccelCount = 0;
869                 infoPtr->AccelVect  = 0;
870             }
871             if(wParam==0) return TRUE;
872             infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
873             if(infoPtr->AccelVect == 0) return FALSE;
874             memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
875             infoPtr->AccelCount = wParam;
876
877             for (temp = 0; temp < wParam; temp++)
878                 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
879
880             return TRUE;
881
882         case UDM_GETBASE:
883             return infoPtr->Base;
884
885         case UDM_SETBASE:
886             TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
887             if (wParam==10 || wParam==16) {
888                 temp = infoPtr->Base;
889                 infoPtr->Base = wParam;
890                 return temp;
891             }
892             break;
893
894         case UDM_GETBUDDY:
895             return (LRESULT)infoPtr->Buddy;
896
897         case UDM_SETBUDDY:
898             return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
899
900         case UDM_GETPOS:
901             temp = UPDOWN_GetBuddyInt (infoPtr);
902             return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
903
904         case UDM_SETPOS:
905             temp = (short)LOWORD(lParam);
906             TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
907             if(!UPDOWN_InBounds(infoPtr, temp)) {
908                 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
909                 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
910             }
911             wParam = infoPtr->CurVal;
912             infoPtr->CurVal = temp;
913             if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
914             return wParam;            /* return prev value */
915
916         case UDM_GETRANGE:
917             return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
918
919         case UDM_SETRANGE:
920                                                      /* we must have:     */
921             infoPtr->MaxVal = (short)(lParam);       /* UD_MINVAL <= Max <= UD_MAXVAL */
922             infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
923                                                      /* |Max-Min| <= UD_MAXVAL        */
924             TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
925                   infoPtr->MinVal, infoPtr->MaxVal, hwnd);
926             break;
927
928         case UDM_GETRANGE32:
929             if (wParam) *(LPINT)wParam = infoPtr->MinVal;
930             if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
931             break;
932
933         case UDM_SETRANGE32:
934             infoPtr->MinVal = (INT)wParam;
935             infoPtr->MaxVal = (INT)lParam;
936             if (infoPtr->MaxVal <= infoPtr->MinVal)
937                 infoPtr->MaxVal = infoPtr->MinVal + 1;
938             TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
939                   infoPtr->MinVal, infoPtr->MaxVal, hwnd);
940             break;
941
942         case UDM_GETPOS32:
943             if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
944             return infoPtr->CurVal;
945
946         case UDM_SETPOS32:
947             if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
948                 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
949                 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
950             }
951             temp = infoPtr->CurVal;         /* save prev value   */
952             infoPtr->CurVal = (int)lParam;  /* set the new value */
953             if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
954             return temp;                    /* return prev value */
955
956         case UDM_GETUNICODEFORMAT:
957             /* we lie a bit here, we're always using Unicode internally */
958             return infoPtr->UnicodeFormat;
959
960         case UDM_SETUNICODEFORMAT:
961             /* do we really need to honour this flag? */
962             temp = infoPtr->UnicodeFormat;
963             infoPtr->UnicodeFormat = (BOOL)wParam;
964             return temp;
965
966         default:
967             if ((message >= WM_USER) && (message < WM_APP))
968                 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
969             return DefWindowProcW (hwnd, message, wParam, lParam);
970     }
971
972     return 0;
973 }
974
975 /***********************************************************************
976  *              UPDOWN_Register [Internal]
977  *
978  * Registers the updown window class.
979  */
980 void UPDOWN_Register(void)
981 {
982     WNDCLASSW wndClass;
983
984     ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
985     wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
986     wndClass.lpfnWndProc   = UpDownWindowProc;
987     wndClass.cbClsExtra    = 0;
988     wndClass.cbWndExtra    = sizeof(UPDOWN_INFO*);
989     wndClass.hCursor       = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
990     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
991     wndClass.lpszClassName = UPDOWN_CLASSW;
992
993     RegisterClassW( &wndClass );
994 }
995
996
997 /***********************************************************************
998  *              UPDOWN_Unregister       [Internal]
999  *
1000  * Unregisters the updown window class.
1001  */
1002 void UPDOWN_Unregister (void)
1003 {
1004     UnregisterClassW (UPDOWN_CLASSW, NULL);
1005 }