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);
156 infoPtr->CurVal += delta;
160 /***********************************************************************
161 * UPDOWN_HasBuddyBorder
163 * When we have a buddy set and that we are aligned on our buddy, we
164 * want to draw a sunken edge to make like we are part of that control.
166 static BOOL UPDOWN_HasBuddyBorder(const UPDOWN_INFO *infoPtr)
168 return ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
169 UPDOWN_IsBuddyEdit(infoPtr) );
172 /***********************************************************************
173 * UPDOWN_GetArrowRect
174 * wndPtr - pointer to the up-down wnd
175 * rect - will hold the rectangle
176 * arrow - FLAG_INCR to get the "increment" rect (up or right)
177 * FLAG_DECR to get the "decrement" rect (down or left)
178 * If both flags are present, the envelope is returned.
180 static void UPDOWN_GetArrowRect (const UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
182 HTHEME theme = GetWindowTheme (infoPtr->Self);
183 const int border = theme ? DEFAULT_BUDDYBORDER_THEMED : DEFAULT_BUDDYBORDER;
184 const int spacer = theme ? DEFAULT_BUDDYSPACER_THEMED : DEFAULT_BUDDYSPACER;
185 GetClientRect (infoPtr->Self, rect);
188 * Make sure we calculate the rectangle to fit even if we draw the
191 if (UPDOWN_HasBuddyBorder(infoPtr)) {
192 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
193 rect->left += border;
195 rect->right -= border;
197 InflateRect(rect, 0, -border);
200 /* now figure out if we need a space away from the buddy */
201 if (IsWindow(infoPtr->Buddy) ) {
202 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= spacer;
203 else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) rect->left += spacer;
207 * We're calculating the midpoint to figure-out where the
208 * separation between the buttons will lay. We make sure that we
209 * round the uneven numbers by adding 1.
211 if (infoPtr->dwStyle & UDS_HORZ) {
212 int len = rect->right - rect->left + 1; /* compute the width */
213 if (arrow & FLAG_INCR)
214 rect->left = rect->left + len/2;
215 if (arrow & FLAG_DECR)
216 rect->right = rect->left + len/2 - (theme ? 0 : 1);
218 int len = rect->bottom - rect->top + 1; /* compute the height */
219 if (arrow & FLAG_INCR)
220 rect->bottom = rect->top + len/2 - (theme ? 0 : 1);
221 if (arrow & FLAG_DECR)
222 rect->top = rect->top + len/2;
226 /***********************************************************************
227 * UPDOWN_GetArrowFromPoint
228 * Returns the rectagle (for the up or down arrow) that contains pt.
229 * If it returns the up rect, it returns FLAG_INCR.
230 * If it returns the down rect, it returns FLAG_DECR.
232 static INT UPDOWN_GetArrowFromPoint (const UPDOWN_INFO *infoPtr, RECT *rect, POINT pt)
234 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
235 if(PtInRect(rect, pt)) return FLAG_INCR;
237 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
238 if(PtInRect(rect, pt)) return FLAG_DECR;
244 /***********************************************************************
245 * UPDOWN_GetThousandSep
246 * Returns the thousand sep. If an error occurs, it returns ','.
248 static WCHAR UPDOWN_GetThousandSep(void)
252 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
258 /***********************************************************************
260 * Tries to read the pos from the buddy window and if it succeeds,
261 * it stores it in the control's CurVal
263 * TRUE - if it read the integer from the buddy successfully
264 * FALSE - if an error occurred
266 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
268 WCHAR txt[20], sep, *src, *dst;
271 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
274 /*if the buddy is a list window, we must set curr index */
275 if (UPDOWN_IsBuddyListbox(infoPtr)) {
276 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
277 if(newVal < 0) return FALSE;
279 /* we have a regular window, so will get the text */
280 /* note that a zero-length string is a legitimate value for 'txt',
281 * and ought to result in a successful conversion to '0'. */
282 if (GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt)) < 0)
285 sep = UPDOWN_GetThousandSep();
287 /* now get rid of the separators */
288 for(src = dst = txt; *src; src++)
289 if(*src != sep) *dst++ = *src;
292 /* try to convert the number and validate it */
293 newVal = strtolW(txt, &src, infoPtr->Base);
294 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
297 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
298 infoPtr->CurVal = newVal;
303 /***********************************************************************
305 * Tries to set the pos to the buddy window based on current pos
307 * TRUE - if it set the caption of the buddy successfully
308 * FALSE - if an error occurred
310 static BOOL UPDOWN_SetBuddyInt (const UPDOWN_INFO *infoPtr)
312 WCHAR fmt[3] = { '%', 'd', '\0' };
313 WCHAR txt[20], txt_old[20] = { 0 };
316 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
319 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
321 /*if the buddy is a list window, we must set curr index */
322 if (UPDOWN_IsBuddyListbox(infoPtr)) {
323 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
326 /* Regular window, so set caption to the number */
327 if (infoPtr->Base == 16) fmt[1] = 'X';
328 len = wsprintfW(txt, fmt, infoPtr->CurVal);
331 /* Do thousands separation if necessary */
332 if ((infoPtr->Base == 10) && !(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
333 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
334 WCHAR sep = UPDOWN_GetThousandSep();
337 memcpy(tmp, txt, sizeof(txt));
338 if (start == 0) start = 3;
341 for (len=0; *src; len++) {
342 if (len % 3 == 0) *dst++ = sep;
348 /* if nothing changed exit earlier */
349 GetWindowTextW(infoPtr->Buddy, txt_old, sizeof(txt_old)/sizeof(WCHAR));
350 if (lstrcmpiW(txt_old, txt) == 0) return 0;
352 return SetWindowTextW(infoPtr->Buddy, txt);
355 /***********************************************************************
356 * UPDOWN_DrawBuddyBackground
358 * Draw buddy background for visual integration.
360 static BOOL UPDOWN_DrawBuddyBackground (const UPDOWN_INFO *infoPtr, HDC hdc)
363 HTHEME buddyTheme = GetWindowTheme (infoPtr->Buddy);
364 if (!buddyTheme) return FALSE;
366 GetClientRect (infoPtr->Buddy, &br);
367 MapWindowPoints (infoPtr->Buddy, infoPtr->Self, (POINT*)&br, 2);
368 /* FIXME: take disabled etc. into account */
369 DrawThemeBackground (buddyTheme, hdc, 0, 0, &br, NULL);
373 /***********************************************************************
376 * Draw the arrows. The background need not be erased.
378 static LRESULT UPDOWN_Draw (const UPDOWN_INFO *infoPtr, HDC hdc)
380 BOOL uPressed, uHot, dPressed, dHot;
382 HTHEME theme = GetWindowTheme (infoPtr->Self);
383 int uPart = 0, uState = 0, dPart = 0, dState = 0;
384 BOOL needBuddyBg = FALSE;
386 uPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
387 uHot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
388 dPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
389 dHot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
391 uPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_UPHORZ : SPNP_UP;
392 uState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
393 : (uPressed ? DNS_PRESSED : (uHot ? DNS_HOT : DNS_NORMAL));
394 dPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_DOWNHORZ : SPNP_DOWN;
395 dState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
396 : (dPressed ? DNS_PRESSED : (dHot ? DNS_HOT : DNS_NORMAL));
397 needBuddyBg = IsWindow (infoPtr->Buddy)
398 && (IsThemeBackgroundPartiallyTransparent (theme, uPart, uState)
399 || IsThemeBackgroundPartiallyTransparent (theme, dPart, dState));
402 /* Draw the common border between ourselves and our buddy */
403 if (UPDOWN_HasBuddyBorder(infoPtr) || needBuddyBg) {
404 if (!theme || !UPDOWN_DrawBuddyBackground (infoPtr, hdc)) {
405 GetClientRect(infoPtr->Self, &rect);
406 DrawEdge(hdc, &rect, EDGE_SUNKEN,
408 (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
412 /* Draw the incr button */
413 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
415 DrawThemeBackground(theme, hdc, uPart, uState, &rect, NULL);
417 DrawFrameControl(hdc, &rect, DFC_SCROLL,
418 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
419 ((infoPtr->dwStyle & UDS_HOTTRACK) && uHot ? DFCS_HOT : 0) |
420 (uPressed ? DFCS_PUSHED : 0) |
421 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
424 /* Draw the decr button */
425 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
427 DrawThemeBackground(theme, hdc, dPart, dState, &rect, NULL);
429 DrawFrameControl(hdc, &rect, DFC_SCROLL,
430 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
431 ((infoPtr->dwStyle & UDS_HOTTRACK) && dHot ? DFCS_HOT : 0) |
432 (dPressed ? DFCS_PUSHED : 0) |
433 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
439 /***********************************************************************
442 * Asynchronous drawing (must ONLY be used in WM_PAINT).
445 static LRESULT UPDOWN_Paint (const UPDOWN_INFO *infoPtr, HDC hdc)
448 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
449 hdc = BeginPaint (infoPtr->Self, &ps);
450 UPDOWN_Draw (infoPtr, hdc);
451 EndPaint (infoPtr->Self, &ps);
455 /***********************************************************************
458 * Handle key presses (up & down) when we have to do so
460 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
464 if (key == VK_UP) arrow = FLAG_INCR;
465 else if (key == VK_DOWN) arrow = FLAG_DECR;
468 UPDOWN_GetBuddyInt (infoPtr);
469 infoPtr->Flags &= ~FLAG_ARROW;
470 infoPtr->Flags |= FLAG_PRESSED | arrow;
471 InvalidateRect (infoPtr->Self, NULL, FALSE);
472 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
473 UPDOWN_DoAction (infoPtr, 1, arrow);
477 /***********************************************************************
480 * Handle UDM_SETRANGE, UDM_SETRANGE32
482 * FIXME: handle Max == Min properly:
483 * - arrows should be disabled (without WS_DISABLED set),
484 * visually they can't be pressed and don't respond;
485 * - all input messages should still pass in.
487 static LRESULT UPDOWN_SetRange(UPDOWN_INFO *infoPtr, INT Max, INT Min)
489 infoPtr->MaxVal = Max;
490 infoPtr->MinVal = Min;
492 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
493 infoPtr->MinVal, infoPtr->MaxVal, infoPtr->Self);
498 /***********************************************************************
501 * Handle mouse wheel scrolling
503 static LRESULT UPDOWN_MouseWheel(UPDOWN_INFO *infoPtr, WPARAM wParam)
505 int iWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
507 if (wParam & (MK_SHIFT | MK_CONTROL))
510 if (iWheelDelta != 0)
512 UPDOWN_GetBuddyInt(infoPtr);
513 UPDOWN_DoAction(infoPtr, abs(iWheelDelta), iWheelDelta > 0 ? FLAG_INCR : FLAG_DECR);
520 /***********************************************************************
521 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
524 static LRESULT CALLBACK
525 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
526 UINT_PTR uId, DWORD_PTR ref_data)
528 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr((HWND)ref_data);
530 TRACE("hwnd=%p, uMsg=%04x, wParam=%08lx, lParam=%08lx\n",
531 hwnd, uMsg, wParam, lParam);
536 UPDOWN_KeyPressed(infoPtr, (int)wParam);
537 if ((wParam == VK_UP) || (wParam == VK_DOWN)) return 0;
541 UPDOWN_MouseWheel(infoPtr, (int)wParam);
548 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
551 /***********************************************************************
554 * Sets bud as a new Buddy.
555 * Then, it should subclass the buddy
556 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
557 * process the UP/DOWN arrow keys.
558 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
559 * the size/pos of the buddy and the control are adjusted accordingly.
561 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
563 RECT budRect; /* new coord for the buddy */
564 int x, width; /* new x position and width for the up-down */
565 WCHAR buddyClass[40];
568 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
570 ret = infoPtr->Buddy;
572 /* there is already a buddy assigned */
573 if (infoPtr->Buddy) RemoveWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc,
575 if (!IsWindow(bud)) bud = NULL;
577 /* Store buddy window handle */
578 infoPtr->Buddy = bud;
581 /* Store buddy window class type */
582 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
583 if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
584 if (lstrcmpiW(buddyClass, WC_EDITW) == 0)
585 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
586 else if (lstrcmpiW(buddyClass, WC_LISTBOXW) == 0)
587 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
590 if (infoPtr->dwStyle & UDS_ARROWKEYS)
591 SetWindowSubclass(bud, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID,
592 (DWORD_PTR)infoPtr->Self);
594 /* Get the rect of the buddy relative to its parent */
595 GetWindowRect(infoPtr->Buddy, &budRect);
596 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
598 /* now do the positioning */
599 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
601 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
602 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
603 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
604 x = budRect.right+DEFAULT_XSEP;
610 /* first adjust the buddy to accommodate the up/down */
611 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
612 budRect.right - budRect.left, budRect.bottom - budRect.top,
613 SWP_NOACTIVATE|SWP_NOZORDER);
615 /* now position the up/down */
616 /* Since the UDS_ALIGN* flags were used, */
617 /* we will pick the position and size of the window. */
618 width = DEFAULT_WIDTH;
621 * If the updown has a buddy border, it has to overlap with the buddy
622 * to look as if it is integrated with the buddy control.
623 * We nudge the control or change its size to overlap.
625 if (UPDOWN_HasBuddyBorder(infoPtr)) {
626 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
627 width += DEFAULT_BUDDYBORDER;
629 x -= DEFAULT_BUDDYBORDER;
632 SetWindowPos(infoPtr->Self, 0, x,
633 budRect.top - DEFAULT_ADDTOP, width,
634 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
635 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
638 GetWindowRect(infoPtr->Self, &rect);
639 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
640 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
641 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
646 /***********************************************************************
649 * This function increments/decrements the CurVal by the
650 * 'delta' amount according to the 'action' flag which can be a
651 * combination of FLAG_INCR and FLAG_DECR
652 * It notifies the parent as required.
653 * It handles wrapping and non-wrapping correctly.
654 * It is assumed that delta>0
656 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
660 TRACE("%d by %d\n", action, delta);
662 /* check if we can do the modification first */
663 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
664 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
666 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
668 /* We must notify parent now to obtain permission */
669 ni.iPos = infoPtr->CurVal;
671 ni.hdr.hwndFrom = infoPtr->Self;
672 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
673 ni.hdr.code = UDN_DELTAPOS;
674 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, ni.hdr.idFrom, (LPARAM)&ni)) {
675 /* Parent said: OK to adjust */
677 /* Now adjust value with (maybe new) delta */
678 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
679 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
681 /* Now take care about our buddy */
682 UPDOWN_SetBuddyInt (infoPtr);
686 /* Also, notify it. This message is sent in any case. */
687 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
688 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
691 /***********************************************************************
694 * Returns TRUE if it is enabled as well as its buddy (if any)
697 static BOOL UPDOWN_IsEnabled (const UPDOWN_INFO *infoPtr)
699 if (!IsWindowEnabled(infoPtr->Self))
702 return IsWindowEnabled(infoPtr->Buddy);
706 /***********************************************************************
709 * Deletes any timers, releases the mouse and does redraw if necessary.
710 * If the control is not in "capture" mode, it does nothing.
711 * If the control was not in cancel mode, it returns FALSE.
712 * If the control was in cancel mode, it returns TRUE.
714 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
716 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
718 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
719 KillTimer (infoPtr->Self, TIMER_ACCEL);
720 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
722 if (GetCapture() == infoPtr->Self) {
724 hdr.hwndFrom = infoPtr->Self;
725 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
726 hdr.code = NM_RELEASEDCAPTURE;
727 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
731 infoPtr->Flags &= ~FLAG_PRESSED;
732 InvalidateRect (infoPtr->Self, NULL, FALSE);
737 /***********************************************************************
738 * UPDOWN_HandleMouseEvent
740 * Handle a mouse event for the updown.
741 * 'pt' is the location of the mouse event in client or
742 * windows coordinates.
744 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
751 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
755 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
757 /* If the buddy is an edit, will set focus to it */
758 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
760 /* Now see which one is the 'active' arrow */
761 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
763 /* Update the flags if we are in/out */
764 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
766 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
768 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
770 if (infoPtr->Flags & FLAG_ARROW) {
772 /* Update the CurVal if necessary */
773 UPDOWN_GetBuddyInt (infoPtr);
775 /* Set up the correct flags */
776 infoPtr->Flags |= FLAG_PRESSED;
778 /* repaint the control */
779 InvalidateRect (infoPtr->Self, NULL, FALSE);
781 /* process the click */
782 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
783 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
785 /* now capture all mouse messages */
786 SetCapture (infoPtr->Self);
788 /* and startup the first timer */
789 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
794 /* save the flags to see if any got modified */
795 temp = infoPtr->Flags;
797 /* Now see which one is the 'active' arrow */
798 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
800 /* Update the flags if we are in/out */
801 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
803 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
805 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
808 /* If state changed, redraw the control */
809 if(temp != infoPtr->Flags)
810 InvalidateRect (infoPtr->Self, NULL, FALSE);
812 /* Set up tracking so the mousein flags can be reset when the
813 * mouse leaves the control */
814 tme.cbSize = sizeof( tme );
815 tme.dwFlags = TME_LEAVE;
816 tme.hwndTrack = infoPtr->Self;
817 TrackMouseEvent (&tme);
821 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
822 InvalidateRect (infoPtr->Self, NULL, FALSE);
826 ERR("Impossible case (msg=%x)!\n", msg);
831 /***********************************************************************
834 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
836 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
837 static const WCHAR themeClass[] = {'S','p','i','n',0};
840 TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd, message, wParam, lParam);
842 if (!infoPtr && (message != WM_CREATE))
843 return DefWindowProcW (hwnd, message, wParam, lParam);
849 CREATESTRUCTW *pcs = (CREATESTRUCTW*)lParam;
851 infoPtr = Alloc (sizeof(UPDOWN_INFO));
852 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
854 /* initialize the info struct */
855 infoPtr->Self = hwnd;
856 infoPtr->Notify = pcs->hwndParent;
857 infoPtr->dwStyle = pcs->style;
858 infoPtr->AccelCount = 0;
859 infoPtr->AccelVect = 0;
860 infoPtr->AccelIndex = -1;
862 infoPtr->MinVal = 100;
864 infoPtr->Base = 10; /* Default to base 10 */
865 infoPtr->Buddy = 0; /* No buddy window yet */
866 infoPtr->Flags = (infoPtr->dwStyle & UDS_SETBUDDYINT) ? FLAG_BUDDYINT : 0;
868 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
869 if (!(infoPtr->dwStyle & UDS_HORZ))
870 SetWindowPos (hwnd, NULL, 0, 0, DEFAULT_WIDTH, pcs->cy,
871 SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
873 /* Do we pick the buddy win ourselves? */
874 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
875 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
877 OpenThemeData (hwnd, themeClass);
879 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
884 Free (infoPtr->AccelVect);
887 RemoveWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc,
890 SetWindowLongPtrW (hwnd, 0, 0);
891 theme = GetWindowTheme (hwnd);
892 CloseThemeData (theme);
893 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
898 infoPtr->dwStyle &= ~WS_DISABLED;
900 infoPtr->dwStyle |= WS_DISABLED;
901 UPDOWN_CancelMode (infoPtr);
903 InvalidateRect (infoPtr->Self, NULL, FALSE);
906 case WM_STYLECHANGED:
907 if (wParam == GWL_STYLE) {
908 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
909 InvalidateRect (infoPtr->Self, NULL, FALSE);
913 case WM_THEMECHANGED:
914 theme = GetWindowTheme (hwnd);
915 CloseThemeData (theme);
916 OpenThemeData (hwnd, themeClass);
917 InvalidateRect (hwnd, NULL, FALSE);
921 /* is this the auto-press timer? */
922 if(wParam == TIMER_AUTOPRESS) {
923 KillTimer(hwnd, TIMER_AUTOPRESS);
924 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
925 InvalidateRect(infoPtr->Self, NULL, FALSE);
928 /* if initial timer, kill it and start the repeat timer */
929 if(wParam == TIMER_AUTOREPEAT) {
932 KillTimer(hwnd, TIMER_AUTOREPEAT);
933 /* if no accel info given, used default timer */
934 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
935 infoPtr->AccelIndex = -1;
938 infoPtr->AccelIndex = 0; /* otherwise, use it */
939 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
941 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
944 /* now, if the mouse is above us, do the thing...*/
945 if(infoPtr->Flags & FLAG_MOUSEIN) {
948 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
949 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
951 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
952 KillTimer(hwnd, TIMER_ACCEL);
953 infoPtr->AccelIndex++; /* move to the next accel info */
954 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
955 /* make sure we have at least 1ms intervals */
956 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
962 return UPDOWN_CancelMode (infoPtr);
965 if (GetCapture() != infoPtr->Self) break;
967 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
968 (infoPtr->Flags & FLAG_ARROW) ) {
970 SendMessageW( infoPtr->Notify,
971 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
972 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
974 if (UPDOWN_IsBuddyEdit(infoPtr))
975 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
977 UPDOWN_CancelMode(infoPtr);
983 if(UPDOWN_IsEnabled(infoPtr))
984 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
988 UPDOWN_MouseWheel(infoPtr, wParam);
992 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
993 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
998 return UPDOWN_Paint (infoPtr, (HDC)wParam);
1001 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
1002 if (wParam && lParam) {
1003 int temp = min(infoPtr->AccelCount, wParam);
1004 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
1013 TRACE("UDM_SETACCEL\n");
1015 if(infoPtr->AccelVect) {
1016 Free (infoPtr->AccelVect);
1017 infoPtr->AccelCount = 0;
1018 infoPtr->AccelVect = 0;
1020 if(wParam==0) return TRUE;
1021 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
1022 if(infoPtr->AccelVect == 0) return FALSE;
1023 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
1024 infoPtr->AccelCount = wParam;
1026 for (temp = 0; temp < wParam; temp++)
1027 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
1032 return infoPtr->Base;
1035 TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam, hwnd);
1036 if (wParam==10 || wParam==16) {
1037 WPARAM old_base = infoPtr->Base;
1038 infoPtr->Base = wParam;
1040 if (old_base != infoPtr->Base)
1041 UPDOWN_SetBuddyInt(infoPtr);
1048 return (LRESULT)infoPtr->Buddy;
1051 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
1055 BOOL ret = UPDOWN_GetBuddyInt (infoPtr);
1056 return MAKELONG(infoPtr->CurVal, ret ? 0 : 1);
1060 int temp = (short)LOWORD(lParam);
1062 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
1063 if(!UPDOWN_InBounds(infoPtr, temp)) {
1064 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
1065 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
1067 wParam = infoPtr->CurVal;
1068 infoPtr->CurVal = temp;
1069 UPDOWN_SetBuddyInt (infoPtr);
1070 return wParam; /* return prev value */
1073 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
1077 UD_MINVAL <= Max <= UD_MAXVAL
1078 UD_MINVAL <= Min <= UD_MAXVAL
1079 |Max-Min| <= UD_MAXVAL */
1080 UPDOWN_SetRange(infoPtr, (short)lParam, (short)HIWORD(lParam));
1083 case UDM_GETRANGE32:
1084 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
1085 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
1088 case UDM_SETRANGE32:
1089 UPDOWN_SetRange(infoPtr, (INT)lParam, (INT)wParam);
1094 BOOL ret = UPDOWN_GetBuddyInt (infoPtr);
1095 if ((LPBOOL)lParam) *((LPBOOL)lParam) = !ret;
1096 return infoPtr->CurVal;
1102 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
1103 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
1104 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
1106 temp = infoPtr->CurVal; /* save prev value */
1107 infoPtr->CurVal = (int)lParam; /* set the new value */
1108 UPDOWN_SetBuddyInt (infoPtr);
1109 return temp; /* return prev value */
1111 case UDM_GETUNICODEFORMAT:
1112 /* we lie a bit here, we're always using Unicode internally */
1113 return infoPtr->UnicodeFormat;
1115 case UDM_SETUNICODEFORMAT:
1117 /* do we really need to honour this flag? */
1118 int temp = infoPtr->UnicodeFormat;
1119 infoPtr->UnicodeFormat = (BOOL)wParam;
1123 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1124 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam);
1125 return DefWindowProcW (hwnd, message, wParam, lParam);
1131 /***********************************************************************
1132 * UPDOWN_Register [Internal]
1134 * Registers the updown window class.
1136 void UPDOWN_Register(void)
1140 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
1141 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1142 wndClass.lpfnWndProc = UpDownWindowProc;
1143 wndClass.cbClsExtra = 0;
1144 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
1145 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1146 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1147 wndClass.lpszClassName = UPDOWN_CLASSW;
1149 RegisterClassW( &wndClass );
1153 /***********************************************************************
1154 * UPDOWN_Unregister [Internal]
1156 * Unregisters the updown window class.
1158 void UPDOWN_Unregister (void)
1160 UnregisterClassW (UPDOWN_CLASSW, NULL);