4 * Copyright 1997, 2002 Dimitrie O. Paun
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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.
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.
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(updown
);
52 HWND Self
; /* Handle to this up-down control */
53 HWND Notify
; /* Handle to the parent window */
54 DWORD dwStyle
; /* The GWL_STYLE for this window */
55 UINT AccelCount
; /* Number of elements in AccelVect */
56 UDACCEL
* AccelVect
; /* Vector containing AccelCount elements */
57 INT AccelIndex
; /* Current accel index, -1 if not accel'ing */
58 INT Base
; /* Base to display nr in the buddy window */
59 INT CurVal
; /* Current up-down value */
60 INT MinVal
; /* Minimum up-down value */
61 INT MaxVal
; /* Maximum up-down value */
62 HWND Buddy
; /* Handle to the buddy window */
63 INT BuddyType
; /* Remembers the buddy type BUDDY_TYPE_* */
64 INT Flags
; /* Internal Flags FLAG_* */
65 BOOL UnicodeFormat
; /* Marks the use of Unicode internally */
68 /* Control configuration constants */
70 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
71 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
72 #define REPEAT_DELAY 50 /* delay between auto-increments */
74 #define DEFAULT_WIDTH 16 /* default width of the ctrl */
75 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
76 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
77 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
78 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
79 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
80 #define DEFAULT_BUDDYBORDER_THEMED 1 /* buddy border when theming is enabled */
81 #define DEFAULT_BUDDYSPACER_THEMED 0 /* buddy spacer when theming is enabled */
85 #define FLAG_INCR 0x01
86 #define FLAG_DECR 0x02
87 #define FLAG_MOUSEIN 0x04
88 #define FLAG_PRESSED 0x08
89 #define FLAG_BUDDYINT 0x10 /* UDS_SETBUDDYINT was set on creation */
90 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
92 #define BUDDY_TYPE_UNKNOWN 0
93 #define BUDDY_TYPE_LISTBOX 1
94 #define BUDDY_TYPE_EDIT 2
96 #define TIMER_AUTOREPEAT 1
98 #define TIMER_AUTOPRESS 3
100 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
101 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
103 /* id used for SetWindowSubclass */
104 #define BUDDY_SUBCLASSID 1
106 static void UPDOWN_DoAction (UPDOWN_INFO
*infoPtr
, int delta
, int action
);
108 /***********************************************************************
110 * Tests if our buddy is an edit control.
112 static inline BOOL
UPDOWN_IsBuddyEdit(const UPDOWN_INFO
*infoPtr
)
114 return infoPtr
->BuddyType
== BUDDY_TYPE_EDIT
;
117 /***********************************************************************
118 * UPDOWN_IsBuddyListbox
119 * Tests if our buddy is a listbox control.
121 static inline BOOL
UPDOWN_IsBuddyListbox(const UPDOWN_INFO
*infoPtr
)
123 return infoPtr
->BuddyType
== BUDDY_TYPE_LISTBOX
;
126 /***********************************************************************
128 * Tests if a given value 'val' is between the Min&Max limits
130 static BOOL
UPDOWN_InBounds(const UPDOWN_INFO
*infoPtr
, int val
)
132 if(infoPtr
->MaxVal
> infoPtr
->MinVal
)
133 return (infoPtr
->MinVal
<= val
) && (val
<= infoPtr
->MaxVal
);
135 return (infoPtr
->MaxVal
<= val
) && (val
<= infoPtr
->MinVal
);
138 /***********************************************************************
140 * Change the current value by delta.
141 * It returns TRUE is the value was changed successfully, or FALSE
142 * if the value was not changed, as it would go out of bounds.
144 static BOOL
UPDOWN_OffsetVal(UPDOWN_INFO
*infoPtr
, int delta
)
146 /* check if we can do the modification first */
147 if(!UPDOWN_InBounds (infoPtr
, infoPtr
->CurVal
+delta
)) {
148 if (infoPtr
->dwStyle
& UDS_WRAP
) {
149 delta
+= (delta
< 0 ? -1 : 1) *
150 (infoPtr
->MaxVal
< infoPtr
->MinVal
? -1 : 1) *
151 (infoPtr
->MinVal
- infoPtr
->MaxVal
) +
152 (delta
< 0 ? 1 : -1);
153 } else if ((infoPtr
->MaxVal
> infoPtr
->MinVal
&& infoPtr
->CurVal
+delta
> infoPtr
->MaxVal
)
154 || (infoPtr
->MaxVal
< infoPtr
->MinVal
&& infoPtr
->CurVal
+delta
< infoPtr
->MaxVal
)) {
155 delta
= infoPtr
->MaxVal
- infoPtr
->CurVal
;
157 delta
= infoPtr
->MinVal
- infoPtr
->CurVal
;
161 infoPtr
->CurVal
+= delta
;
165 /***********************************************************************
166 * UPDOWN_HasBuddyBorder
168 * When we have a buddy set and that we are aligned on our buddy, we
169 * want to draw a sunken edge to make like we are part of that control.
171 static BOOL
UPDOWN_HasBuddyBorder(const UPDOWN_INFO
*infoPtr
)
173 return ( ((infoPtr
->dwStyle
& (UDS_ALIGNLEFT
| UDS_ALIGNRIGHT
)) != 0) &&
174 UPDOWN_IsBuddyEdit(infoPtr
) );
177 /***********************************************************************
178 * UPDOWN_GetArrowRect
179 * wndPtr - pointer to the up-down wnd
180 * rect - will hold the rectangle
181 * arrow - FLAG_INCR to get the "increment" rect (up or right)
182 * FLAG_DECR to get the "decrement" rect (down or left)
183 * If both flags are present, the envelope is returned.
185 static void UPDOWN_GetArrowRect (const UPDOWN_INFO
* infoPtr
, RECT
*rect
, int arrow
)
187 HTHEME theme
= GetWindowTheme (infoPtr
->Self
);
188 const int border
= theme
? DEFAULT_BUDDYBORDER_THEMED
: DEFAULT_BUDDYBORDER
;
189 const int spacer
= theme
? DEFAULT_BUDDYSPACER_THEMED
: DEFAULT_BUDDYSPACER
;
190 GetClientRect (infoPtr
->Self
, rect
);
193 * Make sure we calculate the rectangle to fit even if we draw the
196 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
197 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
)
198 rect
->left
+= border
;
200 rect
->right
-= border
;
202 InflateRect(rect
, 0, -border
);
205 /* now figure out if we need a space away from the buddy */
206 if (IsWindow(infoPtr
->Buddy
) ) {
207 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
) rect
->right
-= spacer
;
208 else if (infoPtr
->dwStyle
& UDS_ALIGNRIGHT
) rect
->left
+= spacer
;
212 * We're calculating the midpoint to figure-out where the
213 * separation between the buttons will lay. We make sure that we
214 * round the uneven numbers by adding 1.
216 if (infoPtr
->dwStyle
& UDS_HORZ
) {
217 int len
= rect
->right
- rect
->left
+ 1; /* compute the width */
218 if (arrow
& FLAG_INCR
)
219 rect
->left
= rect
->left
+ len
/2;
220 if (arrow
& FLAG_DECR
)
221 rect
->right
= rect
->left
+ len
/2 - (theme
? 0 : 1);
223 int len
= rect
->bottom
- rect
->top
+ 1; /* compute the height */
224 if (arrow
& FLAG_INCR
)
225 rect
->bottom
= rect
->top
+ len
/2 - (theme
? 0 : 1);
226 if (arrow
& FLAG_DECR
)
227 rect
->top
= rect
->top
+ len
/2;
231 /***********************************************************************
232 * UPDOWN_GetArrowFromPoint
233 * Returns the rectagle (for the up or down arrow) that contains pt.
234 * If it returns the up rect, it returns FLAG_INCR.
235 * If it returns the down rect, it returns FLAG_DECR.
237 static INT
UPDOWN_GetArrowFromPoint (const UPDOWN_INFO
*infoPtr
, RECT
*rect
, POINT pt
)
239 UPDOWN_GetArrowRect (infoPtr
, rect
, FLAG_INCR
);
240 if(PtInRect(rect
, pt
)) return FLAG_INCR
;
242 UPDOWN_GetArrowRect (infoPtr
, rect
, FLAG_DECR
);
243 if(PtInRect(rect
, pt
)) return FLAG_DECR
;
249 /***********************************************************************
250 * UPDOWN_GetThousandSep
251 * Returns the thousand sep. If an error occurs, it returns ','.
253 static WCHAR
UPDOWN_GetThousandSep(void)
257 if(GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_STHOUSAND
, sep
, 2) != 1)
263 /***********************************************************************
265 * Tries to read the pos from the buddy window and if it succeeds,
266 * it stores it in the control's CurVal
268 * TRUE - if it read the integer from the buddy successfully
269 * FALSE - if an error occurred
271 static BOOL
UPDOWN_GetBuddyInt (UPDOWN_INFO
*infoPtr
)
273 WCHAR txt
[20], sep
, *src
, *dst
;
276 if (!((infoPtr
->Flags
& FLAG_BUDDYINT
) && IsWindow(infoPtr
->Buddy
)))
279 /*if the buddy is a list window, we must set curr index */
280 if (UPDOWN_IsBuddyListbox(infoPtr
)) {
281 newVal
= SendMessageW(infoPtr
->Buddy
, LB_GETCARETINDEX
, 0, 0);
282 if(newVal
< 0) return FALSE
;
284 /* we have a regular window, so will get the text */
285 /* note that a zero-length string is a legitimate value for 'txt',
286 * and ought to result in a successful conversion to '0'. */
287 if (GetWindowTextW(infoPtr
->Buddy
, txt
, COUNT_OF(txt
)) < 0)
290 sep
= UPDOWN_GetThousandSep();
292 /* now get rid of the separators */
293 for(src
= dst
= txt
; *src
; src
++)
294 if(*src
!= sep
) *dst
++ = *src
;
297 /* try to convert the number and validate it */
298 newVal
= strtolW(txt
, &src
, infoPtr
->Base
);
299 if(*src
|| !UPDOWN_InBounds (infoPtr
, newVal
)) return FALSE
;
302 TRACE("new value(%d) from buddy (old=%d)\n", newVal
, infoPtr
->CurVal
);
303 infoPtr
->CurVal
= newVal
;
308 /***********************************************************************
310 * Tries to set the pos to the buddy window based on current pos
312 * TRUE - if it set the caption of the buddy successfully
313 * FALSE - if an error occurred
315 static BOOL
UPDOWN_SetBuddyInt (const UPDOWN_INFO
*infoPtr
)
317 static const WCHAR fmt_hex
[] = { '0', 'x', '%', '0', '4', 'X', 0 };
318 static const WCHAR fmt_dec_oct
[] = { '%', 'd', '\0' };
320 WCHAR txt
[20], txt_old
[20] = { 0 };
323 if (!((infoPtr
->Flags
& FLAG_BUDDYINT
) && IsWindow(infoPtr
->Buddy
)))
326 TRACE("set new value(%d) to buddy.\n", infoPtr
->CurVal
);
328 /*if the buddy is a list window, we must set curr index */
329 if (UPDOWN_IsBuddyListbox(infoPtr
)) {
330 return SendMessageW(infoPtr
->Buddy
, LB_SETCURSEL
, infoPtr
->CurVal
, 0) != LB_ERR
;
333 /* Regular window, so set caption to the number */
334 fmt
= (infoPtr
->Base
== 16) ? fmt_hex
: fmt_dec_oct
;
335 len
= wsprintfW(txt
, fmt
, infoPtr
->CurVal
);
338 /* Do thousands separation if necessary */
339 if ((infoPtr
->Base
== 10) && !(infoPtr
->dwStyle
& UDS_NOTHOUSANDS
) && (len
> 3)) {
340 WCHAR tmp
[COUNT_OF(txt
)], *src
= tmp
, *dst
= txt
;
341 WCHAR sep
= UPDOWN_GetThousandSep();
344 memcpy(tmp
, txt
, sizeof(txt
));
345 if (start
== 0) start
= 3;
348 for (len
=0; *src
; len
++) {
349 if (len
% 3 == 0) *dst
++ = sep
;
355 /* if nothing changed exit earlier */
356 GetWindowTextW(infoPtr
->Buddy
, txt_old
, sizeof(txt_old
)/sizeof(WCHAR
));
357 if (lstrcmpiW(txt_old
, txt
) == 0) return FALSE
;
359 return SetWindowTextW(infoPtr
->Buddy
, txt
);
362 /***********************************************************************
363 * UPDOWN_DrawBuddyBackground
365 * Draw buddy background for visual integration.
367 static BOOL
UPDOWN_DrawBuddyBackground (const UPDOWN_INFO
*infoPtr
, HDC hdc
)
370 HTHEME buddyTheme
= GetWindowTheme (infoPtr
->Buddy
);
371 if (!buddyTheme
) return FALSE
;
373 GetWindowRect (infoPtr
->Buddy
, &br
);
374 MapWindowPoints (NULL
, infoPtr
->Self
, (POINT
*)&br
, 2);
375 GetClientRect (infoPtr
->Self
, &r
);
377 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
)
379 else if (infoPtr
->dwStyle
& UDS_ALIGNRIGHT
)
381 /* FIXME: take disabled etc. into account */
382 DrawThemeBackground (buddyTheme
, hdc
, 0, 0, &br
, NULL
);
386 /***********************************************************************
389 * Draw the arrows. The background need not be erased.
391 static LRESULT
UPDOWN_Draw (const UPDOWN_INFO
*infoPtr
, HDC hdc
)
393 BOOL uPressed
, uHot
, dPressed
, dHot
;
395 HTHEME theme
= GetWindowTheme (infoPtr
->Self
);
396 int uPart
= 0, uState
= 0, dPart
= 0, dState
= 0;
397 BOOL needBuddyBg
= FALSE
;
399 uPressed
= (infoPtr
->Flags
& FLAG_PRESSED
) && (infoPtr
->Flags
& FLAG_INCR
);
400 uHot
= (infoPtr
->Flags
& FLAG_INCR
) && (infoPtr
->Flags
& FLAG_MOUSEIN
);
401 dPressed
= (infoPtr
->Flags
& FLAG_PRESSED
) && (infoPtr
->Flags
& FLAG_DECR
);
402 dHot
= (infoPtr
->Flags
& FLAG_DECR
) && (infoPtr
->Flags
& FLAG_MOUSEIN
);
404 uPart
= (infoPtr
->dwStyle
& UDS_HORZ
) ? SPNP_UPHORZ
: SPNP_UP
;
405 uState
= (infoPtr
->dwStyle
& WS_DISABLED
) ? DNS_DISABLED
406 : (uPressed
? DNS_PRESSED
: (uHot
? DNS_HOT
: DNS_NORMAL
));
407 dPart
= (infoPtr
->dwStyle
& UDS_HORZ
) ? SPNP_DOWNHORZ
: SPNP_DOWN
;
408 dState
= (infoPtr
->dwStyle
& WS_DISABLED
) ? DNS_DISABLED
409 : (dPressed
? DNS_PRESSED
: (dHot
? DNS_HOT
: DNS_NORMAL
));
410 needBuddyBg
= IsWindow (infoPtr
->Buddy
)
411 && (IsThemeBackgroundPartiallyTransparent (theme
, uPart
, uState
)
412 || IsThemeBackgroundPartiallyTransparent (theme
, dPart
, dState
));
415 /* Draw the common border between ourselves and our buddy */
416 if (UPDOWN_HasBuddyBorder(infoPtr
) || needBuddyBg
) {
417 if (!theme
|| !UPDOWN_DrawBuddyBackground (infoPtr
, hdc
)) {
418 GetClientRect(infoPtr
->Self
, &rect
);
419 DrawEdge(hdc
, &rect
, EDGE_SUNKEN
,
421 (infoPtr
->dwStyle
& UDS_ALIGNLEFT
? BF_LEFT
: BF_RIGHT
));
425 /* Draw the incr button */
426 UPDOWN_GetArrowRect (infoPtr
, &rect
, FLAG_INCR
);
428 DrawThemeBackground(theme
, hdc
, uPart
, uState
, &rect
, NULL
);
430 DrawFrameControl(hdc
, &rect
, DFC_SCROLL
,
431 (infoPtr
->dwStyle
& UDS_HORZ
? DFCS_SCROLLRIGHT
: DFCS_SCROLLUP
) |
432 ((infoPtr
->dwStyle
& UDS_HOTTRACK
) && uHot
? DFCS_HOT
: 0) |
433 (uPressed
? DFCS_PUSHED
: 0) |
434 (infoPtr
->dwStyle
& WS_DISABLED
? DFCS_INACTIVE
: 0) );
437 /* Draw the decr button */
438 UPDOWN_GetArrowRect(infoPtr
, &rect
, FLAG_DECR
);
440 DrawThemeBackground(theme
, hdc
, dPart
, dState
, &rect
, NULL
);
442 DrawFrameControl(hdc
, &rect
, DFC_SCROLL
,
443 (infoPtr
->dwStyle
& UDS_HORZ
? DFCS_SCROLLLEFT
: DFCS_SCROLLDOWN
) |
444 ((infoPtr
->dwStyle
& UDS_HOTTRACK
) && dHot
? DFCS_HOT
: 0) |
445 (dPressed
? DFCS_PUSHED
: 0) |
446 (infoPtr
->dwStyle
& WS_DISABLED
? DFCS_INACTIVE
: 0) );
452 /***********************************************************************
455 * Asynchronous drawing (must ONLY be used in WM_PAINT).
458 static LRESULT
UPDOWN_Paint (const UPDOWN_INFO
*infoPtr
, HDC hdc
)
461 if (hdc
) return UPDOWN_Draw (infoPtr
, hdc
);
462 hdc
= BeginPaint (infoPtr
->Self
, &ps
);
463 UPDOWN_Draw (infoPtr
, hdc
);
464 EndPaint (infoPtr
->Self
, &ps
);
468 /***********************************************************************
471 * Handle key presses (up & down) when we have to do so
473 static LRESULT
UPDOWN_KeyPressed(UPDOWN_INFO
*infoPtr
, int key
)
477 if (key
== VK_UP
) arrow
= FLAG_INCR
;
478 else if (key
== VK_DOWN
) arrow
= FLAG_DECR
;
481 UPDOWN_GetBuddyInt (infoPtr
);
482 infoPtr
->Flags
&= ~FLAG_ARROW
;
483 infoPtr
->Flags
|= FLAG_PRESSED
| arrow
;
484 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
485 SetTimer(infoPtr
->Self
, TIMER_AUTOPRESS
, AUTOPRESS_DELAY
, 0);
486 accel
= (infoPtr
->AccelCount
&& infoPtr
->AccelVect
) ? infoPtr
->AccelVect
[0].nInc
: 1;
487 UPDOWN_DoAction (infoPtr
, accel
, arrow
);
491 static int UPDOWN_GetPos(UPDOWN_INFO
*infoPtr
, BOOL
*err
)
493 BOOL succ
= UPDOWN_GetBuddyInt(infoPtr
);
494 int val
= infoPtr
->CurVal
;
496 if(!UPDOWN_InBounds(infoPtr
, val
)) {
497 if((infoPtr
->MinVal
< infoPtr
->MaxVal
&& val
< infoPtr
->MinVal
)
498 || (infoPtr
->MinVal
> infoPtr
->MaxVal
&& val
> infoPtr
->MinVal
))
499 val
= infoPtr
->MinVal
;
501 val
= infoPtr
->MaxVal
;
506 if(err
) *err
= !succ
;
510 static int UPDOWN_SetPos(UPDOWN_INFO
*infoPtr
, int pos
)
512 int ret
= infoPtr
->CurVal
;
514 if(!UPDOWN_InBounds(infoPtr
, pos
)) {
515 if((infoPtr
->MinVal
< infoPtr
->MaxVal
&& pos
< infoPtr
->MinVal
)
516 || (infoPtr
->MinVal
> infoPtr
->MaxVal
&& pos
> infoPtr
->MinVal
))
517 pos
= infoPtr
->MinVal
;
519 pos
= infoPtr
->MaxVal
;
522 infoPtr
->CurVal
= pos
;
523 UPDOWN_SetBuddyInt(infoPtr
);
525 if(!UPDOWN_InBounds(infoPtr
, ret
)) {
526 if((infoPtr
->MinVal
< infoPtr
->MaxVal
&& ret
< infoPtr
->MinVal
)
527 || (infoPtr
->MinVal
> infoPtr
->MaxVal
&& ret
> infoPtr
->MinVal
))
528 ret
= infoPtr
->MinVal
;
530 ret
= infoPtr
->MaxVal
;
536 /***********************************************************************
539 * Handle UDM_SETRANGE, UDM_SETRANGE32
541 * FIXME: handle Max == Min properly:
542 * - arrows should be disabled (without WS_DISABLED set),
543 * visually they can't be pressed and don't respond;
544 * - all input messages should still pass in.
546 static LRESULT
UPDOWN_SetRange(UPDOWN_INFO
*infoPtr
, INT Max
, INT Min
)
548 infoPtr
->MaxVal
= Max
;
549 infoPtr
->MinVal
= Min
;
551 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
552 infoPtr
->MinVal
, infoPtr
->MaxVal
, infoPtr
->Self
);
557 /***********************************************************************
560 * Handle mouse wheel scrolling
562 static LRESULT
UPDOWN_MouseWheel(UPDOWN_INFO
*infoPtr
, WPARAM wParam
)
564 int iWheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
) / WHEEL_DELTA
;
566 if (wParam
& (MK_SHIFT
| MK_CONTROL
))
569 if (iWheelDelta
!= 0)
571 UPDOWN_GetBuddyInt(infoPtr
);
572 UPDOWN_DoAction(infoPtr
, abs(iWheelDelta
), iWheelDelta
> 0 ? FLAG_INCR
: FLAG_DECR
);
579 /***********************************************************************
580 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
583 static LRESULT CALLBACK
584 UPDOWN_Buddy_SubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
,
585 UINT_PTR uId
, DWORD_PTR ref_data
)
587 UPDOWN_INFO
*infoPtr
= UPDOWN_GetInfoPtr((HWND
)ref_data
);
589 TRACE("hwnd=%p, uMsg=%04x, wParam=%08lx, lParam=%08lx\n",
590 hwnd
, uMsg
, wParam
, lParam
);
595 UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
596 if ((wParam
== VK_UP
) || (wParam
== VK_DOWN
)) return 0;
600 UPDOWN_MouseWheel(infoPtr
, (int)wParam
);
607 return DefSubclassProc(hwnd
, uMsg
, wParam
, lParam
);
610 /***********************************************************************
613 * Sets bud as a new Buddy.
614 * Then, it should subclass the buddy
615 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
616 * process the UP/DOWN arrow keys.
617 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
618 * the size/pos of the buddy and the control are adjusted accordingly.
620 static HWND
UPDOWN_SetBuddy (UPDOWN_INFO
* infoPtr
, HWND bud
)
622 RECT budRect
; /* new coord for the buddy */
623 int x
, width
; /* new x position and width for the up-down */
624 WCHAR buddyClass
[40];
627 TRACE("(hwnd=%p, bud=%p)\n", infoPtr
->Self
, bud
);
629 ret
= infoPtr
->Buddy
;
631 /* there is already a buddy assigned */
632 if (infoPtr
->Buddy
) RemoveWindowSubclass(infoPtr
->Buddy
, UPDOWN_Buddy_SubclassProc
,
634 if (!IsWindow(bud
)) bud
= NULL
;
636 /* Store buddy window handle */
637 infoPtr
->Buddy
= bud
;
640 /* Store buddy window class type */
641 infoPtr
->BuddyType
= BUDDY_TYPE_UNKNOWN
;
642 if (GetClassNameW(bud
, buddyClass
, COUNT_OF(buddyClass
))) {
643 if (lstrcmpiW(buddyClass
, WC_EDITW
) == 0)
644 infoPtr
->BuddyType
= BUDDY_TYPE_EDIT
;
645 else if (lstrcmpiW(buddyClass
, WC_LISTBOXW
) == 0)
646 infoPtr
->BuddyType
= BUDDY_TYPE_LISTBOX
;
649 if (infoPtr
->dwStyle
& UDS_ARROWKEYS
)
650 SetWindowSubclass(bud
, UPDOWN_Buddy_SubclassProc
, BUDDY_SUBCLASSID
,
651 (DWORD_PTR
)infoPtr
->Self
);
653 /* Get the rect of the buddy relative to its parent */
654 GetWindowRect(infoPtr
->Buddy
, &budRect
);
655 MapWindowPoints(HWND_DESKTOP
, GetParent(infoPtr
->Buddy
), (POINT
*)(&budRect
.left
), 2);
657 /* now do the positioning */
658 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
) {
660 budRect
.left
+= DEFAULT_WIDTH
+ DEFAULT_XSEP
;
661 } else if (infoPtr
->dwStyle
& UDS_ALIGNRIGHT
) {
662 budRect
.right
-= DEFAULT_WIDTH
+ DEFAULT_XSEP
;
663 x
= budRect
.right
+DEFAULT_XSEP
;
669 /* first adjust the buddy to accommodate the up/down */
670 SetWindowPos(infoPtr
->Buddy
, 0, budRect
.left
, budRect
.top
,
671 budRect
.right
- budRect
.left
, budRect
.bottom
- budRect
.top
,
672 SWP_NOACTIVATE
|SWP_NOZORDER
);
674 /* now position the up/down */
675 /* Since the UDS_ALIGN* flags were used, */
676 /* we will pick the position and size of the window. */
677 width
= DEFAULT_WIDTH
;
680 * If the updown has a buddy border, it has to overlap with the buddy
681 * to look as if it is integrated with the buddy control.
682 * We nudge the control or change its size to overlap.
684 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
685 if(infoPtr
->dwStyle
& UDS_ALIGNLEFT
)
686 width
+= DEFAULT_BUDDYBORDER
;
688 x
-= DEFAULT_BUDDYBORDER
;
691 SetWindowPos(infoPtr
->Self
, 0, x
,
692 budRect
.top
- DEFAULT_ADDTOP
, width
,
693 budRect
.bottom
- budRect
.top
+ DEFAULT_ADDTOP
+ DEFAULT_ADDBOT
,
694 SWP_NOACTIVATE
|SWP_FRAMECHANGED
|SWP_NOZORDER
);
697 GetWindowRect(infoPtr
->Self
, &rect
);
698 MapWindowPoints(HWND_DESKTOP
, GetParent(infoPtr
->Self
), (POINT
*)&rect
, 2);
699 SetWindowPos(infoPtr
->Self
, 0, rect
.left
, rect
.top
, DEFAULT_WIDTH
, rect
.bottom
- rect
.top
,
700 SWP_NOACTIVATE
|SWP_FRAMECHANGED
|SWP_NOZORDER
);
705 /***********************************************************************
708 * This function increments/decrements the CurVal by the
709 * 'delta' amount according to the 'action' flag which can be a
710 * combination of FLAG_INCR and FLAG_DECR
711 * It notifies the parent as required.
712 * It handles wrapping and non-wrapping correctly.
713 * It is assumed that delta>0
715 static void UPDOWN_DoAction (UPDOWN_INFO
*infoPtr
, int delta
, int action
)
719 TRACE("%d by %d\n", action
, delta
);
721 /* check if we can do the modification first */
722 delta
*= (action
& FLAG_INCR
? 1 : -1) * (infoPtr
->MaxVal
< infoPtr
->MinVal
? -1 : 1);
723 if ( (action
& FLAG_INCR
) && (action
& FLAG_DECR
) ) delta
= 0;
725 TRACE("current %d, delta: %d\n", infoPtr
->CurVal
, delta
);
727 /* We must notify parent now to obtain permission */
728 ni
.iPos
= infoPtr
->CurVal
;
730 ni
.hdr
.hwndFrom
= infoPtr
->Self
;
731 ni
.hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
732 ni
.hdr
.code
= UDN_DELTAPOS
;
733 if (!SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, ni
.hdr
.idFrom
, (LPARAM
)&ni
)) {
734 /* Parent said: OK to adjust */
736 /* Now adjust value with (maybe new) delta */
737 if (UPDOWN_OffsetVal (infoPtr
, ni
.iDelta
)) {
738 TRACE("new %d, delta: %d\n", infoPtr
->CurVal
, ni
.iDelta
);
740 /* Now take care about our buddy */
741 UPDOWN_SetBuddyInt (infoPtr
);
745 /* Also, notify it. This message is sent in any case. */
746 SendMessageW( infoPtr
->Notify
, (infoPtr
->dwStyle
& UDS_HORZ
) ? WM_HSCROLL
: WM_VSCROLL
,
747 MAKELONG(SB_THUMBPOSITION
, infoPtr
->CurVal
), (LPARAM
)infoPtr
->Self
);
750 /***********************************************************************
753 * Returns TRUE if it is enabled as well as its buddy (if any)
756 static BOOL
UPDOWN_IsEnabled (const UPDOWN_INFO
*infoPtr
)
758 if (!IsWindowEnabled(infoPtr
->Self
))
761 return IsWindowEnabled(infoPtr
->Buddy
);
765 /***********************************************************************
768 * Deletes any timers, releases the mouse and does redraw if necessary.
769 * If the control is not in "capture" mode, it does nothing.
770 * If the control was not in cancel mode, it returns FALSE.
771 * If the control was in cancel mode, it returns TRUE.
773 static BOOL
UPDOWN_CancelMode (UPDOWN_INFO
*infoPtr
)
775 if (!(infoPtr
->Flags
& FLAG_PRESSED
)) return FALSE
;
777 KillTimer (infoPtr
->Self
, TIMER_AUTOREPEAT
);
778 KillTimer (infoPtr
->Self
, TIMER_ACCEL
);
779 KillTimer (infoPtr
->Self
, TIMER_AUTOPRESS
);
781 if (GetCapture() == infoPtr
->Self
) {
783 hdr
.hwndFrom
= infoPtr
->Self
;
784 hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
785 hdr
.code
= NM_RELEASEDCAPTURE
;
786 SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, hdr
.idFrom
, (LPARAM
)&hdr
);
790 infoPtr
->Flags
&= ~FLAG_PRESSED
;
791 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
796 /***********************************************************************
797 * UPDOWN_HandleMouseEvent
799 * Handle a mouse event for the updown.
800 * 'pt' is the location of the mouse event in client or
801 * windows coordinates.
803 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO
*infoPtr
, UINT msg
, INT x
, INT y
)
810 TRACE("msg %04x point %s\n", msg
, wine_dbgstr_point(&pt
));
814 case WM_LBUTTONDOWN
: /* Initialise mouse tracking */
816 /* If the buddy is an edit, will set focus to it */
817 if (UPDOWN_IsBuddyEdit(infoPtr
)) SetFocus(infoPtr
->Buddy
);
819 /* Now see which one is the 'active' arrow */
820 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
822 /* Update the flags if we are in/out */
823 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
825 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
827 if (infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
829 if (infoPtr
->Flags
& FLAG_ARROW
) {
831 /* Update the CurVal if necessary */
832 UPDOWN_GetBuddyInt (infoPtr
);
834 /* Set up the correct flags */
835 infoPtr
->Flags
|= FLAG_PRESSED
;
837 /* repaint the control */
838 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
840 /* process the click */
841 temp
= (infoPtr
->AccelCount
&& infoPtr
->AccelVect
) ? infoPtr
->AccelVect
[0].nInc
: 1;
842 UPDOWN_DoAction (infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
844 /* now capture all mouse messages */
845 SetCapture (infoPtr
->Self
);
847 /* and startup the first timer */
848 SetTimer(infoPtr
->Self
, TIMER_AUTOREPEAT
, INITIAL_DELAY
, 0);
853 /* save the flags to see if any got modified */
854 temp
= infoPtr
->Flags
;
856 /* Now see which one is the 'active' arrow */
857 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
859 /* Update the flags if we are in/out */
860 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
862 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
864 if(infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
867 /* If state changed, redraw the control */
868 if(temp
!= infoPtr
->Flags
)
869 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
871 /* Set up tracking so the mousein flags can be reset when the
872 * mouse leaves the control */
873 tme
.cbSize
= sizeof( tme
);
874 tme
.dwFlags
= TME_LEAVE
;
875 tme
.hwndTrack
= infoPtr
->Self
;
876 TrackMouseEvent (&tme
);
880 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
881 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
885 ERR("Impossible case (msg=%x)!\n", msg
);
890 /***********************************************************************
893 static LRESULT WINAPI
UpDownWindowProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
895 UPDOWN_INFO
*infoPtr
= UPDOWN_GetInfoPtr (hwnd
);
896 static const WCHAR themeClass
[] = {'S','p','i','n',0};
899 TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd
, message
, wParam
, lParam
);
901 if (!infoPtr
&& (message
!= WM_CREATE
))
902 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
908 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
910 infoPtr
= Alloc (sizeof(UPDOWN_INFO
));
911 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
913 /* initialize the info struct */
914 infoPtr
->Self
= hwnd
;
915 infoPtr
->Notify
= pcs
->hwndParent
;
916 infoPtr
->dwStyle
= pcs
->style
;
917 infoPtr
->AccelCount
= 0;
918 infoPtr
->AccelVect
= 0;
919 infoPtr
->AccelIndex
= -1;
921 infoPtr
->MinVal
= 100;
923 infoPtr
->Base
= 10; /* Default to base 10 */
924 infoPtr
->Buddy
= 0; /* No buddy window yet */
925 infoPtr
->Flags
= (infoPtr
->dwStyle
& UDS_SETBUDDYINT
) ? FLAG_BUDDYINT
: 0;
927 SetWindowLongW (hwnd
, GWL_STYLE
, infoPtr
->dwStyle
& ~WS_BORDER
);
928 if (!(infoPtr
->dwStyle
& UDS_HORZ
))
929 SetWindowPos (hwnd
, NULL
, 0, 0, DEFAULT_WIDTH
, pcs
->cy
,
930 SWP_NOOWNERZORDER
| SWP_NOZORDER
| SWP_NOMOVE
);
932 /* Do we pick the buddy win ourselves? */
933 if (infoPtr
->dwStyle
& UDS_AUTOBUDDY
)
934 UPDOWN_SetBuddy (infoPtr
, GetWindow (hwnd
, GW_HWNDPREV
));
936 OpenThemeData (hwnd
, themeClass
);
938 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd
);
943 Free (infoPtr
->AccelVect
);
946 RemoveWindowSubclass(infoPtr
->Buddy
, UPDOWN_Buddy_SubclassProc
,
949 SetWindowLongPtrW (hwnd
, 0, 0);
950 theme
= GetWindowTheme (hwnd
);
951 CloseThemeData (theme
);
952 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd
);
957 infoPtr
->dwStyle
&= ~WS_DISABLED
;
959 infoPtr
->dwStyle
|= WS_DISABLED
;
960 UPDOWN_CancelMode (infoPtr
);
962 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
965 case WM_STYLECHANGED
:
966 if (wParam
== GWL_STYLE
) {
967 infoPtr
->dwStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
968 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
972 case WM_THEMECHANGED
:
973 theme
= GetWindowTheme (hwnd
);
974 CloseThemeData (theme
);
975 OpenThemeData (hwnd
, themeClass
);
976 InvalidateRect (hwnd
, NULL
, FALSE
);
980 /* is this the auto-press timer? */
981 if(wParam
== TIMER_AUTOPRESS
) {
982 KillTimer(hwnd
, TIMER_AUTOPRESS
);
983 infoPtr
->Flags
&= ~(FLAG_PRESSED
| FLAG_ARROW
);
984 InvalidateRect(infoPtr
->Self
, NULL
, FALSE
);
987 /* if initial timer, kill it and start the repeat timer */
988 if(wParam
== TIMER_AUTOREPEAT
) {
991 KillTimer(hwnd
, TIMER_AUTOREPEAT
);
992 /* if no accel info given, used default timer */
993 if(infoPtr
->AccelCount
==0 || infoPtr
->AccelVect
==0) {
994 infoPtr
->AccelIndex
= -1;
995 delay
= REPEAT_DELAY
;
997 infoPtr
->AccelIndex
= 0; /* otherwise, use it */
998 delay
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
1000 SetTimer(hwnd
, TIMER_ACCEL
, delay
, 0);
1003 /* now, if the mouse is above us, do the thing...*/
1004 if(infoPtr
->Flags
& FLAG_MOUSEIN
) {
1007 temp
= infoPtr
->AccelIndex
== -1 ? 1 : infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nInc
;
1008 UPDOWN_DoAction(infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
1010 if(infoPtr
->AccelIndex
!= -1 && infoPtr
->AccelIndex
< infoPtr
->AccelCount
-1) {
1011 KillTimer(hwnd
, TIMER_ACCEL
);
1012 infoPtr
->AccelIndex
++; /* move to the next accel info */
1013 temp
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
1014 /* make sure we have at least 1ms intervals */
1015 SetTimer(hwnd
, TIMER_ACCEL
, temp
, 0);
1021 return UPDOWN_CancelMode (infoPtr
);
1024 if (GetCapture() != infoPtr
->Self
) break;
1026 if ( (infoPtr
->Flags
& FLAG_MOUSEIN
) &&
1027 (infoPtr
->Flags
& FLAG_ARROW
) ) {
1029 SendMessageW( infoPtr
->Notify
,
1030 (infoPtr
->dwStyle
& UDS_HORZ
) ? WM_HSCROLL
: WM_VSCROLL
,
1031 MAKELONG(SB_ENDSCROLL
, infoPtr
->CurVal
),
1033 if (UPDOWN_IsBuddyEdit(infoPtr
))
1034 SendMessageW(infoPtr
->Buddy
, EM_SETSEL
, 0, MAKELONG(0, -1));
1036 UPDOWN_CancelMode(infoPtr
);
1039 case WM_LBUTTONDOWN
:
1042 if(UPDOWN_IsEnabled(infoPtr
))
1043 UPDOWN_HandleMouseEvent (infoPtr
, message
, (SHORT
)LOWORD(lParam
), (SHORT
)HIWORD(lParam
));
1047 UPDOWN_MouseWheel(infoPtr
, wParam
);
1051 if((infoPtr
->dwStyle
& UDS_ARROWKEYS
) && UPDOWN_IsEnabled(infoPtr
))
1052 return UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
1055 case WM_PRINTCLIENT
:
1057 return UPDOWN_Paint (infoPtr
, (HDC
)wParam
);
1060 if (wParam
==0 && lParam
==0) return infoPtr
->AccelCount
;
1061 if (wParam
&& lParam
) {
1062 int temp
= min(infoPtr
->AccelCount
, wParam
);
1063 memcpy((void *)lParam
, infoPtr
->AccelVect
, temp
*sizeof(UDACCEL
));
1070 TRACE("UDM_SETACCEL\n");
1072 if(infoPtr
->AccelVect
) {
1073 Free (infoPtr
->AccelVect
);
1074 infoPtr
->AccelCount
= 0;
1075 infoPtr
->AccelVect
= 0;
1077 if(wParam
==0) return TRUE
;
1078 infoPtr
->AccelVect
= Alloc (wParam
*sizeof(UDACCEL
));
1079 if(infoPtr
->AccelVect
== 0) return FALSE
;
1080 memcpy(infoPtr
->AccelVect
, (void*)lParam
, wParam
*sizeof(UDACCEL
));
1081 infoPtr
->AccelCount
= wParam
;
1083 if (TRACE_ON(updown
))
1087 for (i
= 0; i
< wParam
; i
++)
1088 TRACE("%u: nSec %u nInc %u\n", i
,
1089 infoPtr
->AccelVect
[i
].nSec
, infoPtr
->AccelVect
[i
].nInc
);
1095 return infoPtr
->Base
;
1098 TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam
, hwnd
);
1099 if (wParam
==10 || wParam
==16) {
1100 WPARAM old_base
= infoPtr
->Base
;
1101 infoPtr
->Base
= wParam
;
1103 if (old_base
!= infoPtr
->Base
)
1104 UPDOWN_SetBuddyInt(infoPtr
);
1111 return (LRESULT
)infoPtr
->Buddy
;
1114 return (LRESULT
)UPDOWN_SetBuddy (infoPtr
, (HWND
)wParam
);
1121 pos
= UPDOWN_GetPos(infoPtr
, &err
);
1122 return MAKELONG(pos
, err
);
1126 return UPDOWN_SetPos(infoPtr
, (short)LOWORD(lParam
));
1129 return MAKELONG(infoPtr
->MaxVal
, infoPtr
->MinVal
);
1133 UD_MINVAL <= Max <= UD_MAXVAL
1134 UD_MINVAL <= Min <= UD_MAXVAL
1135 |Max-Min| <= UD_MAXVAL */
1136 UPDOWN_SetRange(infoPtr
, (short)lParam
, (short)HIWORD(lParam
));
1139 case UDM_GETRANGE32
:
1140 if (wParam
) *(LPINT
)wParam
= infoPtr
->MinVal
;
1141 if (lParam
) *(LPINT
)lParam
= infoPtr
->MaxVal
;
1144 case UDM_SETRANGE32
:
1145 UPDOWN_SetRange(infoPtr
, (INT
)lParam
, (INT
)wParam
);
1150 return UPDOWN_GetPos(infoPtr
, (BOOL
*)lParam
);
1154 return UPDOWN_SetPos(infoPtr
, (int)lParam
);
1156 case UDM_GETUNICODEFORMAT
:
1157 /* we lie a bit here, we're always using Unicode internally */
1158 return infoPtr
->UnicodeFormat
;
1160 case UDM_SETUNICODEFORMAT
:
1162 /* do we really need to honour this flag? */
1163 int temp
= infoPtr
->UnicodeFormat
;
1164 infoPtr
->UnicodeFormat
= (BOOL
)wParam
;
1168 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1169 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1170 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
1176 /***********************************************************************
1177 * UPDOWN_Register [Internal]
1179 * Registers the updown window class.
1181 void UPDOWN_Register(void)
1185 ZeroMemory( &wndClass
, sizeof( WNDCLASSW
) );
1186 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1187 wndClass
.lpfnWndProc
= UpDownWindowProc
;
1188 wndClass
.cbClsExtra
= 0;
1189 wndClass
.cbWndExtra
= sizeof(UPDOWN_INFO
*);
1190 wndClass
.hCursor
= LoadCursorW( 0, (LPWSTR
)IDC_ARROW
);
1191 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
1192 wndClass
.lpszClassName
= UPDOWN_CLASSW
;
1194 RegisterClassW( &wndClass
);
1198 /***********************************************************************
1199 * UPDOWN_Unregister [Internal]
1201 * Unregisters the updown window class.
1203 void UPDOWN_Unregister (void)
1205 UnregisterClassW (UPDOWN_CLASSW
, NULL
);