Don't calculate the redundant and unused REBAR_ROW structures.
[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 BUDDY_UPDOWN_HWND        "buddyUpDownHWND"
96 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
97
98 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
99 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
100
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)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
431     TRACE("hwnd=%p, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
432           hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
433
434     if (uMsg == WM_KEYDOWN) {
435         HWND upDownHwnd = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
436
437         UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
438     }
439
440     return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
441 }
442
443 /***********************************************************************
444  *           UPDOWN_SetBuddy
445  * Tests if 'bud' is a valid window handle. If not, returns FALSE.
446  * Else, sets it as a new Buddy.
447  * Then, it should subclass the buddy
448  * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
449  * process the UP/DOWN arrow keys.
450  * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
451  * the size/pos of the buddy and the control are adjusted accordingly.
452  */
453 static BOOL UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
454 {
455     DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
456     RECT  budRect;  /* new coord for the buddy */
457     int   x, width;  /* new x position and width for the up-down */
458     WNDPROC baseWndProc;
459     CHAR buddyClass[40];
460
461     /* Is it a valid bud? */
462     if(!IsWindow(bud)) return FALSE;
463
464     TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
465
466     /* there is already a body assigned */
467     if (infoPtr->Buddy)  RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
468
469     /* Store buddy window handle */
470     infoPtr->Buddy = bud;
471
472     /* keep upDown ctrl hwnd in a buddy property */
473     SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
474
475     /* Store buddy window class type */
476     infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
477     if (GetClassNameA(bud, buddyClass, COUNT_OF(buddyClass))) {
478         if (lstrcmpiA(buddyClass, "Edit") == 0)
479             infoPtr->BuddyType = BUDDY_TYPE_EDIT;
480         else if (lstrcmpiA(buddyClass, "Listbox") == 0)
481             infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
482     }
483
484     if(dwStyle & UDS_ARROWKEYS){
485         /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
486            when we reset the upDown ctrl buddy to another buddy because it is not
487            good to break the window proc chain. */
488         if (!GetPropA(bud, BUDDY_SUPERCLASS_WNDPROC)) {
489             baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
490             SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
491         }
492     }
493
494     /* Get the rect of the buddy relative to its parent */
495     GetWindowRect(infoPtr->Buddy, &budRect);
496     MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
497
498     /* now do the positioning */
499     if  (dwStyle & UDS_ALIGNLEFT) {
500         x  = budRect.left;
501         budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
502     } else if (dwStyle & UDS_ALIGNRIGHT) {
503         budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
504         x  = budRect.right+DEFAULT_XSEP;
505     } else {
506         x  = budRect.right+DEFAULT_XSEP;
507     }
508
509     /* first adjust the buddy to accomodate the up/down */
510     SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
511                  budRect.right  - budRect.left, budRect.bottom - budRect.top,
512                  SWP_NOACTIVATE|SWP_NOZORDER);
513
514     /* now position the up/down */
515     /* Since the UDS_ALIGN* flags were used, */
516     /* we will pick the position and size of the window. */
517     width = DEFAULT_WIDTH;
518
519     /*
520      * If the updown has a buddy border, it has to overlap with the buddy
521      * to look as if it is integrated with the buddy control.
522      * We nudge the control or change it size to overlap.
523      */
524     if (UPDOWN_HasBuddyBorder(infoPtr)) {
525         if(dwStyle & UDS_ALIGNLEFT)
526             width += DEFAULT_BUDDYBORDER;
527         else
528             x -= DEFAULT_BUDDYBORDER;
529     }
530
531     SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
532                  budRect.top - DEFAULT_ADDTOP, width,
533                  budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
534                  SWP_NOACTIVATE);
535
536     return TRUE;
537 }
538
539 /***********************************************************************
540  *           UPDOWN_DoAction
541  *
542  * This function increments/decrements the CurVal by the
543  * 'delta' amount according to the 'action' flag which can be a
544  * combination of FLAG_INCR and FLAG_DECR
545  * It notifies the parent as required.
546  * It handles wraping and non-wraping correctly.
547  * It is assumed that delta>0
548  */
549 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
550 {
551     DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
552     NM_UPDOWN ni;
553
554     TRACE("%d by %d\n", action, delta);
555
556     /* check if we can do the modification first */
557     delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
558     if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
559
560     /* We must notify parent now to obtain permission */
561     ni.iPos = infoPtr->CurVal;
562     ni.iDelta = delta;
563     ni.hdr.hwndFrom = infoPtr->Self;
564     ni.hdr.idFrom   = GetWindowLongW (infoPtr->Self, GWL_ID);
565     ni.hdr.code = UDN_DELTAPOS;
566     if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
567         /* Parent said: OK to adjust */
568
569         /* Now adjust value with (maybe new) delta */
570         if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
571             /* Now take care about our buddy */
572             if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
573         }
574     }
575
576     /* Also, notify it. This message is sent in any case. */
577     SendMessageW( infoPtr->Notify, dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
578                   MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
579 }
580
581 /***********************************************************************
582  *           UPDOWN_IsEnabled
583  *
584  * Returns TRUE if it is enabled as well as its buddy (if any)
585  *         FALSE otherwise
586  */
587 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
588 {
589     if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
590         return FALSE;
591     if(infoPtr->Buddy)
592         return IsWindowEnabled(infoPtr->Buddy);
593     return TRUE;
594 }
595
596 /***********************************************************************
597  *           UPDOWN_CancelMode
598  *
599  * Deletes any timers, releases the mouse and does  redraw if necessary.
600  * If the control is not in "capture" mode, it does nothing.
601  * If the control was not in cancel mode, it returns FALSE.
602  * If the control was in cancel mode, it returns TRUE.
603  */
604 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
605 {
606     if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
607
608     KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
609     KillTimer (infoPtr->Self, TIMER_ACCEL);
610     KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
611
612     if (GetCapture() == infoPtr->Self) {
613         NMHDR hdr;
614         hdr.hwndFrom = infoPtr->Self;
615         hdr.idFrom   = GetWindowLongW (infoPtr->Self, GWL_ID);
616         hdr.code = NM_RELEASEDCAPTURE;
617         SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
618         ReleaseCapture();
619     }
620
621     infoPtr->Flags &= ~FLAG_PRESSED;
622     InvalidateRect (infoPtr->Self, NULL, FALSE);
623
624     return TRUE;
625 }
626
627 /***********************************************************************
628  *           UPDOWN_HandleMouseEvent
629  *
630  * Handle a mouse event for the updown.
631  * 'pt' is the location of the mouse event in client or
632  * windows coordinates.
633  */
634 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINTS pts)
635 {
636     DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
637     POINT pt = { pts.x, pts.y };
638     RECT rect;
639     int temp, arrow;
640
641     switch(msg)
642     {
643         case WM_LBUTTONDOWN:  /* Initialise mouse tracking */
644             /* If we are inside an arrow, then nothing to do */
645             if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
646
647             /* If the buddy is an edit, will set focus to it */
648             if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
649
650             /* Now see which one is the 'active' arrow */
651             if (infoPtr->Flags & FLAG_ARROW) {
652
653                 /* Update the CurVal if necessary */
654                 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
655
656                 /* Set up the correct flags */
657                 infoPtr->Flags |= FLAG_PRESSED;
658
659                 /* repaint the control */
660                 InvalidateRect (infoPtr->Self, NULL, FALSE);
661
662                 /* process the click */
663                 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
664
665                 /* now capture all mouse messages */
666                 SetCapture (infoPtr->Self);
667
668                 /* and startup the first timer */
669                 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
670             }
671             break;
672
673         case WM_MOUSEMOVE:
674             /* save the flags to see if any got modified */
675             temp = infoPtr->Flags;
676
677             /* Now see which one is the 'active' arrow */
678             arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
679
680             /* Update the flags if we are in/out */
681             infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
682             if(arrow) {
683                 infoPtr->Flags |=  FLAG_MOUSEIN | arrow;
684             } else {
685                 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
686             }
687
688             /* If state changed, redraw the control */
689             if(temp != infoPtr->Flags)
690                  InvalidateRect (infoPtr->Self, &rect, FALSE);
691             break;
692
693         default:
694             ERR("Impossible case (msg=%x)!\n", msg);
695     }
696
697 }
698
699 /***********************************************************************
700  *           UpDownWndProc
701  */
702 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
703                                 LPARAM lParam)
704 {
705     UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
706     DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
707     int temp;
708
709     if (!infoPtr && (message != WM_CREATE))
710         return DefWindowProcW (hwnd, message, wParam, lParam);
711
712     switch(message)
713     {
714         case WM_CREATE:
715             SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
716             infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
717             SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
718
719             /* initialize the info struct */
720             infoPtr->Self = hwnd;
721             infoPtr->Notify = ((LPCREATESTRUCTA)lParam)->hwndParent;
722             infoPtr->AccelCount = 0;
723             infoPtr->AccelVect = 0;
724             infoPtr->AccelIndex = -1;
725             infoPtr->CurVal = 0;
726             infoPtr->MinVal = 100;
727             infoPtr->MaxVal = 0;
728             infoPtr->Base  = 10; /* Default to base 10  */
729             infoPtr->Buddy = 0;  /* No buddy window yet */
730             infoPtr->Flags = 0;  /* And no flags        */
731
732             /* Do we pick the buddy win ourselves? */
733             if (dwStyle & UDS_AUTOBUDDY)
734                 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
735
736             TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
737             break;
738
739         case WM_DESTROY:
740             if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
741
742             if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
743
744             Free (infoPtr);
745             SetWindowLongW (hwnd, 0, 0);
746             TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
747             break;
748
749         case WM_ENABLE:
750             if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
751             InvalidateRect (infoPtr->Self, NULL, FALSE);
752             break;
753
754         case WM_TIMER:
755            /* is this the auto-press timer? */
756            if(wParam == TIMER_AUTOPRESS) {
757                 KillTimer(hwnd, TIMER_AUTOPRESS);
758                 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
759                 InvalidateRect(infoPtr->Self, NULL, FALSE);
760            }
761
762            /* if initial timer, kill it and start the repeat timer */
763            if(wParam == TIMER_AUTOREPEAT) {
764                 KillTimer(hwnd, TIMER_AUTOREPEAT);
765                 /* if no accel info given, used default timer */
766                 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
767                     infoPtr->AccelIndex = -1;
768                     temp = REPEAT_DELAY;
769                 } else {
770                     infoPtr->AccelIndex = 0; /* otherwise, use it */
771                     temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
772                 }
773                 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
774             }
775
776             /* now, if the mouse is above us, do the thing...*/
777             if(infoPtr->Flags & FLAG_MOUSEIN) {
778                 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
779                 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
780
781                 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
782                     KillTimer(hwnd, TIMER_ACCEL);
783                     infoPtr->AccelIndex++; /* move to the next accel info */
784                     temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
785                     /* make sure we have at least 1ms intervals */
786                     SetTimer(hwnd, TIMER_ACCEL, temp, 0);
787                 }
788             }
789             break;
790
791         case WM_CANCELMODE:
792           return UPDOWN_CancelMode (infoPtr);
793
794         case WM_LBUTTONUP:
795             if (GetCapture() != infoPtr->Self) break;
796
797             if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
798                  (infoPtr->Flags & FLAG_ARROW) ) {
799
800                 SendMessageW( infoPtr->Notify,
801                               dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
802                               MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
803                               (LPARAM)hwnd);
804                 if (UPDOWN_IsBuddyEdit(infoPtr))
805                     SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
806             }
807             UPDOWN_CancelMode(infoPtr);
808             break;
809
810         case WM_LBUTTONDOWN:
811         case WM_MOUSEMOVE:
812             if(UPDOWN_IsEnabled(infoPtr))
813                 UPDOWN_HandleMouseEvent (infoPtr, message, MAKEPOINTS(lParam));
814             break;
815
816         case WM_KEYDOWN:
817             if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
818                 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
819             break;
820
821         case WM_PAINT:
822             return UPDOWN_Paint (infoPtr, (HDC)wParam);
823
824         case UDM_GETACCEL:
825             if (wParam==0 && lParam==0) return infoPtr->AccelCount;
826             if (wParam && lParam) {
827                 temp = min(infoPtr->AccelCount, wParam);
828                 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
829                 return temp;
830             }
831             return 0;
832
833         case UDM_SETACCEL:
834             TRACE("UpDown Ctrl new accel info, hwnd=%p\n", hwnd);
835             if(infoPtr->AccelVect) {
836                 Free (infoPtr->AccelVect);
837                 infoPtr->AccelCount = 0;
838                 infoPtr->AccelVect  = 0;
839             }
840             if(wParam==0) return TRUE;
841             infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
842             if(infoPtr->AccelVect == 0) return FALSE;
843             memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
844             return TRUE;
845
846         case UDM_GETBASE:
847             return infoPtr->Base;
848
849         case UDM_SETBASE:
850             TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
851             if (wParam==10 || wParam==16) {
852                 temp = infoPtr->Base;
853                 infoPtr->Base = wParam;
854                 return temp;
855             }
856             break;
857
858         case UDM_GETBUDDY:
859             return (LRESULT)infoPtr->Buddy;
860
861         case UDM_SETBUDDY:
862             temp = (int)infoPtr->Buddy;
863             UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
864             return temp;
865
866         case UDM_GETPOS:
867             temp = UPDOWN_GetBuddyInt (infoPtr);
868             return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
869
870         case UDM_SETPOS:
871             temp = (short)LOWORD(lParam);
872             TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
873             if(!UPDOWN_InBounds(infoPtr, temp)) {
874                 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
875                 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
876             }
877             wParam = infoPtr->CurVal;
878             infoPtr->CurVal = temp;
879             if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
880             return wParam;            /* return prev value */
881
882         case UDM_GETRANGE:
883             return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
884
885         case UDM_SETRANGE:
886                                                      /* we must have:     */
887             infoPtr->MaxVal = (short)(lParam);       /* UD_MINVAL <= Max <= UD_MAXVAL */
888             infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
889                                                      /* |Max-Min| <= UD_MAXVAL        */
890             TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
891                   infoPtr->MinVal, infoPtr->MaxVal, hwnd);
892             break;
893
894         case UDM_GETRANGE32:
895             if (wParam) *(LPINT)wParam = infoPtr->MinVal;
896             if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
897             break;
898
899         case UDM_SETRANGE32:
900             infoPtr->MinVal = (INT)wParam;
901             infoPtr->MaxVal = (INT)lParam;
902             if (infoPtr->MaxVal <= infoPtr->MinVal)
903                 infoPtr->MaxVal = infoPtr->MinVal + 1;
904             TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
905                   infoPtr->MinVal, infoPtr->MaxVal, hwnd);
906             break;
907
908         case UDM_GETPOS32:
909             if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
910             return infoPtr->CurVal;
911
912         case UDM_SETPOS32:
913             if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
914                 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
915                 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
916             }
917             temp = infoPtr->CurVal;         /* save prev value   */
918             infoPtr->CurVal = (int)lParam;  /* set the new value */
919             if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
920             return temp;                    /* return prev value */
921
922         case UDM_GETUNICODEFORMAT:
923             /* we lie a bit here, we're always using Unicode internally */
924             return infoPtr->UnicodeFormat;
925
926         case UDM_SETUNICODEFORMAT:
927             /* do we really need to honour this flag? */
928             temp = infoPtr->UnicodeFormat;
929             infoPtr->UnicodeFormat = (BOOL)wParam;
930             return temp;
931
932         default:
933             if ((message >= WM_USER) && (message < WM_APP))
934                 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
935             return DefWindowProcW (hwnd, message, wParam, lParam);
936     }
937
938     return 0;
939 }
940
941 /***********************************************************************
942  *              UPDOWN_Register [Internal]
943  *
944  * Registers the updown window class.
945  */
946 void UPDOWN_Register(void)
947 {
948     WNDCLASSW wndClass;
949
950     ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
951     wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW;
952     wndClass.lpfnWndProc   = (WNDPROC)UpDownWindowProc;
953     wndClass.cbClsExtra    = 0;
954     wndClass.cbWndExtra    = sizeof(UPDOWN_INFO*);
955     wndClass.hCursor       = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
956     wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
957     wndClass.lpszClassName = UPDOWN_CLASSW;
958
959     RegisterClassW( &wndClass );
960 }
961
962
963 /***********************************************************************
964  *              UPDOWN_Unregister       [Internal]
965  *
966  * Unregisters the updown window class.
967  */
968 void UPDOWN_Unregister (void)
969 {
970     UnregisterClassW (UPDOWN_CLASSW, NULL);
971 }