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 old_buddy
= 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
);
695 } else if (!(infoPtr
->dwStyle
& UDS_HORZ
) && old_buddy
!= NULL
) {
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
);
706 /***********************************************************************
709 * This function increments/decrements the CurVal by the
710 * 'delta' amount according to the 'action' flag which can be a
711 * combination of FLAG_INCR and FLAG_DECR
712 * It notifies the parent as required.
713 * It handles wrapping and non-wrapping correctly.
714 * It is assumed that delta>0
716 static void UPDOWN_DoAction (UPDOWN_INFO
*infoPtr
, int delta
, int action
)
720 TRACE("%d by %d\n", action
, delta
);
722 /* check if we can do the modification first */
723 delta
*= (action
& FLAG_INCR
? 1 : -1) * (infoPtr
->MaxVal
< infoPtr
->MinVal
? -1 : 1);
724 if ( (action
& FLAG_INCR
) && (action
& FLAG_DECR
) ) delta
= 0;
726 TRACE("current %d, delta: %d\n", infoPtr
->CurVal
, delta
);
728 /* We must notify parent now to obtain permission */
729 ni
.iPos
= infoPtr
->CurVal
;
731 ni
.hdr
.hwndFrom
= infoPtr
->Self
;
732 ni
.hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
733 ni
.hdr
.code
= UDN_DELTAPOS
;
734 if (!SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, ni
.hdr
.idFrom
, (LPARAM
)&ni
)) {
735 /* Parent said: OK to adjust */
737 /* Now adjust value with (maybe new) delta */
738 if (UPDOWN_OffsetVal (infoPtr
, ni
.iDelta
)) {
739 TRACE("new %d, delta: %d\n", infoPtr
->CurVal
, ni
.iDelta
);
741 /* Now take care about our buddy */
742 UPDOWN_SetBuddyInt (infoPtr
);
746 /* Also, notify it. This message is sent in any case. */
747 SendMessageW( infoPtr
->Notify
, (infoPtr
->dwStyle
& UDS_HORZ
) ? WM_HSCROLL
: WM_VSCROLL
,
748 MAKELONG(SB_THUMBPOSITION
, infoPtr
->CurVal
), (LPARAM
)infoPtr
->Self
);
751 /***********************************************************************
754 * Returns TRUE if it is enabled as well as its buddy (if any)
757 static BOOL
UPDOWN_IsEnabled (const UPDOWN_INFO
*infoPtr
)
759 if (!IsWindowEnabled(infoPtr
->Self
))
762 return IsWindowEnabled(infoPtr
->Buddy
);
766 /***********************************************************************
769 * Deletes any timers, releases the mouse and does redraw if necessary.
770 * If the control is not in "capture" mode, it does nothing.
771 * If the control was not in cancel mode, it returns FALSE.
772 * If the control was in cancel mode, it returns TRUE.
774 static BOOL
UPDOWN_CancelMode (UPDOWN_INFO
*infoPtr
)
776 if (!(infoPtr
->Flags
& FLAG_PRESSED
)) return FALSE
;
778 KillTimer (infoPtr
->Self
, TIMER_AUTOREPEAT
);
779 KillTimer (infoPtr
->Self
, TIMER_ACCEL
);
780 KillTimer (infoPtr
->Self
, TIMER_AUTOPRESS
);
782 if (GetCapture() == infoPtr
->Self
) {
784 hdr
.hwndFrom
= infoPtr
->Self
;
785 hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
786 hdr
.code
= NM_RELEASEDCAPTURE
;
787 SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, hdr
.idFrom
, (LPARAM
)&hdr
);
791 infoPtr
->Flags
&= ~FLAG_PRESSED
;
792 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
797 /***********************************************************************
798 * UPDOWN_HandleMouseEvent
800 * Handle a mouse event for the updown.
801 * 'pt' is the location of the mouse event in client or
802 * windows coordinates.
804 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO
*infoPtr
, UINT msg
, INT x
, INT y
)
811 TRACE("msg %04x point %s\n", msg
, wine_dbgstr_point(&pt
));
815 case WM_LBUTTONDOWN
: /* Initialise mouse tracking */
817 /* If the buddy is an edit, will set focus to it */
818 if (UPDOWN_IsBuddyEdit(infoPtr
)) SetFocus(infoPtr
->Buddy
);
820 /* Now see which one is the 'active' arrow */
821 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
823 /* Update the flags if we are in/out */
824 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
826 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
828 if (infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
830 if (infoPtr
->Flags
& FLAG_ARROW
) {
832 /* Update the CurVal if necessary */
833 UPDOWN_GetBuddyInt (infoPtr
);
835 /* Set up the correct flags */
836 infoPtr
->Flags
|= FLAG_PRESSED
;
838 /* repaint the control */
839 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
841 /* process the click */
842 temp
= (infoPtr
->AccelCount
&& infoPtr
->AccelVect
) ? infoPtr
->AccelVect
[0].nInc
: 1;
843 UPDOWN_DoAction (infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
845 /* now capture all mouse messages */
846 SetCapture (infoPtr
->Self
);
848 /* and startup the first timer */
849 SetTimer(infoPtr
->Self
, TIMER_AUTOREPEAT
, INITIAL_DELAY
, 0);
854 /* save the flags to see if any got modified */
855 temp
= infoPtr
->Flags
;
857 /* Now see which one is the 'active' arrow */
858 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
860 /* Update the flags if we are in/out */
861 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
863 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
865 if(infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
868 /* If state changed, redraw the control */
869 if(temp
!= infoPtr
->Flags
)
870 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
872 /* Set up tracking so the mousein flags can be reset when the
873 * mouse leaves the control */
874 tme
.cbSize
= sizeof( tme
);
875 tme
.dwFlags
= TME_LEAVE
;
876 tme
.hwndTrack
= infoPtr
->Self
;
877 TrackMouseEvent (&tme
);
881 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
882 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
886 ERR("Impossible case (msg=%x)!\n", msg
);
891 /***********************************************************************
894 static LRESULT WINAPI
UpDownWindowProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
896 UPDOWN_INFO
*infoPtr
= UPDOWN_GetInfoPtr (hwnd
);
897 static const WCHAR themeClass
[] = {'S','p','i','n',0};
900 TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd
, message
, wParam
, lParam
);
902 if (!infoPtr
&& (message
!= WM_CREATE
))
903 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
909 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
911 infoPtr
= Alloc (sizeof(UPDOWN_INFO
));
912 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
914 /* initialize the info struct */
915 infoPtr
->Self
= hwnd
;
916 infoPtr
->Notify
= pcs
->hwndParent
;
917 infoPtr
->dwStyle
= pcs
->style
;
918 infoPtr
->AccelCount
= 0;
919 infoPtr
->AccelVect
= 0;
920 infoPtr
->AccelIndex
= -1;
922 infoPtr
->MinVal
= 100;
924 infoPtr
->Base
= 10; /* Default to base 10 */
925 infoPtr
->Buddy
= 0; /* No buddy window yet */
926 infoPtr
->Flags
= (infoPtr
->dwStyle
& UDS_SETBUDDYINT
) ? FLAG_BUDDYINT
: 0;
928 SetWindowLongW (hwnd
, GWL_STYLE
, infoPtr
->dwStyle
& ~WS_BORDER
);
929 if (!(infoPtr
->dwStyle
& UDS_HORZ
))
930 SetWindowPos (hwnd
, NULL
, 0, 0, DEFAULT_WIDTH
, pcs
->cy
,
931 SWP_NOOWNERZORDER
| SWP_NOZORDER
| SWP_NOMOVE
);
933 /* Do we pick the buddy win ourselves? */
934 if (infoPtr
->dwStyle
& UDS_AUTOBUDDY
)
935 UPDOWN_SetBuddy (infoPtr
, GetWindow (hwnd
, GW_HWNDPREV
));
937 OpenThemeData (hwnd
, themeClass
);
939 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd
);
944 Free (infoPtr
->AccelVect
);
947 RemoveWindowSubclass(infoPtr
->Buddy
, UPDOWN_Buddy_SubclassProc
,
950 SetWindowLongPtrW (hwnd
, 0, 0);
951 theme
= GetWindowTheme (hwnd
);
952 CloseThemeData (theme
);
953 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd
);
958 infoPtr
->dwStyle
&= ~WS_DISABLED
;
960 infoPtr
->dwStyle
|= WS_DISABLED
;
961 UPDOWN_CancelMode (infoPtr
);
963 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
966 case WM_STYLECHANGED
:
967 if (wParam
== GWL_STYLE
) {
968 infoPtr
->dwStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
969 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
973 case WM_THEMECHANGED
:
974 theme
= GetWindowTheme (hwnd
);
975 CloseThemeData (theme
);
976 OpenThemeData (hwnd
, themeClass
);
977 InvalidateRect (hwnd
, NULL
, FALSE
);
981 /* is this the auto-press timer? */
982 if(wParam
== TIMER_AUTOPRESS
) {
983 KillTimer(hwnd
, TIMER_AUTOPRESS
);
984 infoPtr
->Flags
&= ~(FLAG_PRESSED
| FLAG_ARROW
);
985 InvalidateRect(infoPtr
->Self
, NULL
, FALSE
);
988 /* if initial timer, kill it and start the repeat timer */
989 if(wParam
== TIMER_AUTOREPEAT
) {
992 KillTimer(hwnd
, TIMER_AUTOREPEAT
);
993 /* if no accel info given, used default timer */
994 if(infoPtr
->AccelCount
==0 || infoPtr
->AccelVect
==0) {
995 infoPtr
->AccelIndex
= -1;
996 delay
= REPEAT_DELAY
;
998 infoPtr
->AccelIndex
= 0; /* otherwise, use it */
999 delay
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
1001 SetTimer(hwnd
, TIMER_ACCEL
, delay
, 0);
1004 /* now, if the mouse is above us, do the thing...*/
1005 if(infoPtr
->Flags
& FLAG_MOUSEIN
) {
1008 temp
= infoPtr
->AccelIndex
== -1 ? 1 : infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nInc
;
1009 UPDOWN_DoAction(infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
1011 if(infoPtr
->AccelIndex
!= -1 && infoPtr
->AccelIndex
< infoPtr
->AccelCount
-1) {
1012 KillTimer(hwnd
, TIMER_ACCEL
);
1013 infoPtr
->AccelIndex
++; /* move to the next accel info */
1014 temp
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
1015 /* make sure we have at least 1ms intervals */
1016 SetTimer(hwnd
, TIMER_ACCEL
, temp
, 0);
1022 return UPDOWN_CancelMode (infoPtr
);
1025 if (GetCapture() != infoPtr
->Self
) break;
1027 if ( (infoPtr
->Flags
& FLAG_MOUSEIN
) &&
1028 (infoPtr
->Flags
& FLAG_ARROW
) ) {
1030 SendMessageW( infoPtr
->Notify
,
1031 (infoPtr
->dwStyle
& UDS_HORZ
) ? WM_HSCROLL
: WM_VSCROLL
,
1032 MAKELONG(SB_ENDSCROLL
, infoPtr
->CurVal
),
1034 if (UPDOWN_IsBuddyEdit(infoPtr
))
1035 SendMessageW(infoPtr
->Buddy
, EM_SETSEL
, 0, MAKELONG(0, -1));
1037 UPDOWN_CancelMode(infoPtr
);
1040 case WM_LBUTTONDOWN
:
1043 if(UPDOWN_IsEnabled(infoPtr
))
1044 UPDOWN_HandleMouseEvent (infoPtr
, message
, (SHORT
)LOWORD(lParam
), (SHORT
)HIWORD(lParam
));
1048 UPDOWN_MouseWheel(infoPtr
, wParam
);
1052 if((infoPtr
->dwStyle
& UDS_ARROWKEYS
) && UPDOWN_IsEnabled(infoPtr
))
1053 return UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
1056 case WM_PRINTCLIENT
:
1058 return UPDOWN_Paint (infoPtr
, (HDC
)wParam
);
1061 if (wParam
==0 && lParam
==0) return infoPtr
->AccelCount
;
1062 if (wParam
&& lParam
) {
1063 int temp
= min(infoPtr
->AccelCount
, wParam
);
1064 memcpy((void *)lParam
, infoPtr
->AccelVect
, temp
*sizeof(UDACCEL
));
1071 TRACE("UDM_SETACCEL\n");
1073 if(infoPtr
->AccelVect
) {
1074 Free (infoPtr
->AccelVect
);
1075 infoPtr
->AccelCount
= 0;
1076 infoPtr
->AccelVect
= 0;
1078 if(wParam
==0) return TRUE
;
1079 infoPtr
->AccelVect
= Alloc (wParam
*sizeof(UDACCEL
));
1080 if(infoPtr
->AccelVect
== 0) return FALSE
;
1081 memcpy(infoPtr
->AccelVect
, (void*)lParam
, wParam
*sizeof(UDACCEL
));
1082 infoPtr
->AccelCount
= wParam
;
1084 if (TRACE_ON(updown
))
1088 for (i
= 0; i
< wParam
; i
++)
1089 TRACE("%u: nSec %u nInc %u\n", i
,
1090 infoPtr
->AccelVect
[i
].nSec
, infoPtr
->AccelVect
[i
].nInc
);
1096 return infoPtr
->Base
;
1099 TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam
, hwnd
);
1100 if (wParam
==10 || wParam
==16) {
1101 WPARAM old_base
= infoPtr
->Base
;
1102 infoPtr
->Base
= wParam
;
1104 if (old_base
!= infoPtr
->Base
)
1105 UPDOWN_SetBuddyInt(infoPtr
);
1112 return (LRESULT
)infoPtr
->Buddy
;
1115 return (LRESULT
)UPDOWN_SetBuddy (infoPtr
, (HWND
)wParam
);
1122 pos
= UPDOWN_GetPos(infoPtr
, &err
);
1123 return MAKELONG(pos
, err
);
1127 return UPDOWN_SetPos(infoPtr
, (short)LOWORD(lParam
));
1130 return MAKELONG(infoPtr
->MaxVal
, infoPtr
->MinVal
);
1134 UD_MINVAL <= Max <= UD_MAXVAL
1135 UD_MINVAL <= Min <= UD_MAXVAL
1136 |Max-Min| <= UD_MAXVAL */
1137 UPDOWN_SetRange(infoPtr
, (short)lParam
, (short)HIWORD(lParam
));
1140 case UDM_GETRANGE32
:
1141 if (wParam
) *(LPINT
)wParam
= infoPtr
->MinVal
;
1142 if (lParam
) *(LPINT
)lParam
= infoPtr
->MaxVal
;
1145 case UDM_SETRANGE32
:
1146 UPDOWN_SetRange(infoPtr
, (INT
)lParam
, (INT
)wParam
);
1151 return UPDOWN_GetPos(infoPtr
, (BOOL
*)lParam
);
1155 return UPDOWN_SetPos(infoPtr
, (int)lParam
);
1157 case UDM_GETUNICODEFORMAT
:
1158 /* we lie a bit here, we're always using Unicode internally */
1159 return infoPtr
->UnicodeFormat
;
1161 case UDM_SETUNICODEFORMAT
:
1163 /* do we really need to honour this flag? */
1164 int temp
= infoPtr
->UnicodeFormat
;
1165 infoPtr
->UnicodeFormat
= (BOOL
)wParam
;
1169 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1170 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1171 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
1177 /***********************************************************************
1178 * UPDOWN_Register [Internal]
1180 * Registers the updown window class.
1182 void UPDOWN_Register(void)
1186 ZeroMemory( &wndClass
, sizeof( WNDCLASSW
) );
1187 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1188 wndClass
.lpfnWndProc
= UpDownWindowProc
;
1189 wndClass
.cbClsExtra
= 0;
1190 wndClass
.cbWndExtra
= sizeof(UPDOWN_INFO
*);
1191 wndClass
.hCursor
= LoadCursorW( 0, (LPWSTR
)IDC_ARROW
);
1192 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
1193 wndClass
.lpszClassName
= UPDOWN_CLASSW
;
1195 RegisterClassW( &wndClass
);
1199 /***********************************************************************
1200 * UPDOWN_Unregister [Internal]
1202 * Unregisters the updown window class.
1204 void UPDOWN_Unregister (void)
1206 UnregisterClassW (UPDOWN_CLASSW
, NULL
);