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 14 /* 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_ARROW (FLAG_INCR | FLAG_DECR)
91 #define BUDDY_TYPE_UNKNOWN 0
92 #define BUDDY_TYPE_LISTBOX 1
93 #define BUDDY_TYPE_EDIT 2
95 #define TIMER_AUTOREPEAT 1
97 #define TIMER_AUTOPRESS 3
99 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
100 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
102 static const WCHAR BUDDY_UPDOWN_HWND[] = { 'b', 'u', 'd', 'd', 'y', 'U', 'p', 'D', 'o', 'w', 'n', 'H', 'W', 'N', 'D', 0 };
103 static const WCHAR BUDDY_SUPERCLASS_WNDPROC[] = { 'b', 'u', 'd', 'd', 'y', 'S', 'u', 'p', 'p', 'e', 'r',
104 'C', 'l', 'a', 's', 's', 'W', 'n', 'd', 'P', 'r', 'o', 'c', 0 };
105 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
107 /***********************************************************************
109 * Tests if our buddy is an edit control.
111 static inline BOOL UPDOWN_IsBuddyEdit(const UPDOWN_INFO *infoPtr)
113 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
116 /***********************************************************************
117 * UPDOWN_IsBuddyListbox
118 * Tests if our buddy is a listbox control.
120 static inline BOOL UPDOWN_IsBuddyListbox(const UPDOWN_INFO *infoPtr)
122 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
125 /***********************************************************************
127 * Tests if a given value 'val' is between the Min&Max limits
129 static BOOL UPDOWN_InBounds(const UPDOWN_INFO *infoPtr, int val)
131 if(infoPtr->MaxVal > infoPtr->MinVal)
132 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
134 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
137 /***********************************************************************
139 * Change the current value by delta.
140 * It returns TRUE is the value was changed successfuly, or FALSE
141 * if the value was not changed, as it would go out of bounds.
143 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
145 /* check if we can do the modification first */
146 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
147 if (infoPtr->dwStyle & UDS_WRAP) {
148 delta += (delta < 0 ? -1 : 1) *
149 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
150 (infoPtr->MinVal - infoPtr->MaxVal) +
151 (delta < 0 ? 1 : -1);
155 infoPtr->CurVal += delta;
159 /***********************************************************************
160 * UPDOWN_HasBuddyBorder
162 * When we have a buddy set and that we are aligned on our buddy, we
163 * want to draw a sunken edge to make like we are part of that control.
165 static BOOL UPDOWN_HasBuddyBorder(const UPDOWN_INFO *infoPtr)
167 return ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
168 UPDOWN_IsBuddyEdit(infoPtr) );
171 /***********************************************************************
172 * UPDOWN_GetArrowRect
173 * wndPtr - pointer to the up-down wnd
174 * rect - will hold the rectangle
175 * arrow - FLAG_INCR to get the "increment" rect (up or right)
176 * FLAG_DECR to get the "decrement" rect (down or left)
177 * If both flags are pressent, the envelope is returned.
179 static void UPDOWN_GetArrowRect (const UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
181 HTHEME theme = GetWindowTheme (infoPtr->Self);
182 const int border = theme ? DEFAULT_BUDDYBORDER_THEMED : DEFAULT_BUDDYBORDER;
183 const int spacer = theme ? DEFAULT_BUDDYSPACER_THEMED : DEFAULT_BUDDYSPACER;
184 GetClientRect (infoPtr->Self, rect);
187 * Make sure we calculate the rectangle to fit even if we draw the
190 if (UPDOWN_HasBuddyBorder(infoPtr)) {
191 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
192 rect->left += border;
194 rect->right -= border;
196 InflateRect(rect, 0, -border);
199 /* now figure out if we need a space away from the buddy */
200 if (IsWindow(infoPtr->Buddy) ) {
201 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= spacer;
202 else rect->left += spacer;
206 * We're calculating the midpoint to figure-out where the
207 * separation between the buttons will lay. We make sure that we
208 * round the uneven numbers by adding 1.
210 if (infoPtr->dwStyle & UDS_HORZ) {
211 int len = rect->right - rect->left + 1; /* compute the width */
212 if (arrow & FLAG_INCR)
213 rect->left = rect->left + len/2;
214 if (arrow & FLAG_DECR)
215 rect->right = rect->left + len/2 - (theme ? 0 : 1);
217 int len = rect->bottom - rect->top + 1; /* compute the height */
218 if (arrow & FLAG_INCR)
219 rect->bottom = rect->top + len/2 - (theme ? 0 : 1);
220 if (arrow & FLAG_DECR)
221 rect->top = rect->top + len/2;
225 /***********************************************************************
226 * UPDOWN_GetArrowFromPoint
227 * Returns the rectagle (for the up or down arrow) that contains pt.
228 * If it returns the up rect, it returns FLAG_INCR.
229 * If it returns the down rect, it returns FLAG_DECR.
231 static INT UPDOWN_GetArrowFromPoint (const UPDOWN_INFO *infoPtr, RECT *rect, POINT pt)
233 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
234 if(PtInRect(rect, pt)) return FLAG_INCR;
236 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
237 if(PtInRect(rect, pt)) return FLAG_DECR;
243 /***********************************************************************
244 * UPDOWN_GetThousandSep
245 * Returns the thousand sep. If an error occurs, it returns ','.
247 static WCHAR UPDOWN_GetThousandSep(void)
251 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
257 /***********************************************************************
259 * Tries to read the pos from the buddy window and if it succeeds,
260 * it stores it in the control's CurVal
262 * TRUE - if it read the integer from the buddy successfully
263 * FALSE - if an error occurred
265 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
267 WCHAR txt[20], sep, *src, *dst;
270 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
273 /*if the buddy is a list window, we must set curr index */
274 if (UPDOWN_IsBuddyListbox(infoPtr)) {
275 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
276 if(newVal < 0) return FALSE;
278 /* we have a regular window, so will get the text */
279 /* note that a zero-length string is a legitimate value for 'txt',
280 * and ought to result in a successful conversion to '0'. */
281 if (GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt)) < 0)
284 sep = UPDOWN_GetThousandSep();
286 /* now get rid of the separators */
287 for(src = dst = txt; *src; src++)
288 if(*src != sep) *dst++ = *src;
291 /* try to convert the number and validate it */
292 newVal = strtolW(txt, &src, infoPtr->Base);
293 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
296 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
297 infoPtr->CurVal = newVal;
302 /***********************************************************************
304 * Tries to set the pos to the buddy window based on current pos
306 * TRUE - if it set the caption of the buddy successfully
307 * FALSE - if an error occurred
309 static BOOL UPDOWN_SetBuddyInt (const UPDOWN_INFO *infoPtr)
311 WCHAR fmt[3] = { '%', 'd', '\0' };
315 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
318 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
320 /*if the buddy is a list window, we must set curr index */
321 if (UPDOWN_IsBuddyListbox(infoPtr)) {
322 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
325 /* Regular window, so set caption to the number */
326 if (infoPtr->Base == 16) fmt[1] = 'X';
327 len = wsprintfW(txt, fmt, infoPtr->CurVal);
330 /* Do thousands separation if necessary */
331 if (!(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
332 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
333 WCHAR sep = UPDOWN_GetThousandSep();
336 memcpy(tmp, txt, sizeof(txt));
337 if (start == 0) start = 3;
340 for (len=0; *src; len++) {
341 if (len % 3 == 0) *dst++ = sep;
347 return SetWindowTextW(infoPtr->Buddy, txt);
350 /***********************************************************************
351 * UPDOWN_DrawBuddyBackground
353 * Draw buddy background for visual integration.
355 static BOOL UPDOWN_DrawBuddyBackground (const UPDOWN_INFO *infoPtr, HDC hdc)
358 HTHEME buddyTheme = GetWindowTheme (infoPtr->Buddy);
359 if (!buddyTheme) return FALSE;
361 GetClientRect (infoPtr->Buddy, &br);
362 MapWindowPoints (infoPtr->Buddy, infoPtr->Self, (POINT*)&br, 2);
363 /* FIXME: take disabled etc. into account */
364 DrawThemeBackground (buddyTheme, hdc, 0, 0, &br, NULL);
368 /***********************************************************************
371 * Draw the arrows. The background need not be erased.
373 static LRESULT UPDOWN_Draw (const UPDOWN_INFO *infoPtr, HDC hdc)
375 BOOL uPressed, uHot, dPressed, dHot;
377 HTHEME theme = GetWindowTheme (infoPtr->Self);
378 int uPart = 0, uState = 0, dPart = 0, dState = 0;
379 BOOL needBuddyBg = FALSE;
381 uPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
382 uHot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
383 dPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
384 dHot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
386 uPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_UPHORZ : SPNP_UP;
387 uState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
388 : (uPressed ? DNS_PRESSED : (uHot ? DNS_HOT : DNS_NORMAL));
389 dPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_DOWNHORZ : SPNP_DOWN;
390 dState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
391 : (dPressed ? DNS_PRESSED : (dHot ? DNS_HOT : DNS_NORMAL));
392 needBuddyBg = IsWindow (infoPtr->Buddy)
393 && (IsThemeBackgroundPartiallyTransparent (theme, uPart, uState)
394 || IsThemeBackgroundPartiallyTransparent (theme, dPart, dState));
397 /* Draw the common border between ourselves and our buddy */
398 if (UPDOWN_HasBuddyBorder(infoPtr) || needBuddyBg) {
399 if (!theme || !UPDOWN_DrawBuddyBackground (infoPtr, hdc)) {
400 GetClientRect(infoPtr->Self, &rect);
401 DrawEdge(hdc, &rect, EDGE_SUNKEN,
403 (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
407 /* Draw the incr button */
408 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
410 DrawThemeBackground(theme, hdc, uPart, uState, &rect, NULL);
412 DrawFrameControl(hdc, &rect, DFC_SCROLL,
413 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
414 ((infoPtr->dwStyle & UDS_HOTTRACK) && uHot ? DFCS_HOT : 0) |
415 (uPressed ? DFCS_PUSHED : 0) |
416 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
419 /* Draw the decr button */
420 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
422 DrawThemeBackground(theme, hdc, dPart, dState, &rect, NULL);
424 DrawFrameControl(hdc, &rect, DFC_SCROLL,
425 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
426 ((infoPtr->dwStyle & UDS_HOTTRACK) && dHot ? DFCS_HOT : 0) |
427 (dPressed ? DFCS_PUSHED : 0) |
428 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
434 /***********************************************************************
437 * Asynchronous drawing (must ONLY be used in WM_PAINT).
440 static LRESULT UPDOWN_Paint (const UPDOWN_INFO *infoPtr, HDC hdc)
443 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
444 hdc = BeginPaint (infoPtr->Self, &ps);
445 UPDOWN_Draw (infoPtr, hdc);
446 EndPaint (infoPtr->Self, &ps);
450 /***********************************************************************
453 * Handle key presses (up & down) when we have to do so
455 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
459 if (key == VK_UP) arrow = FLAG_INCR;
460 else if (key == VK_DOWN) arrow = FLAG_DECR;
463 UPDOWN_GetBuddyInt (infoPtr);
464 infoPtr->Flags &= ~FLAG_ARROW;
465 infoPtr->Flags |= FLAG_PRESSED | arrow;
466 InvalidateRect (infoPtr->Self, NULL, FALSE);
467 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
468 UPDOWN_DoAction (infoPtr, 1, arrow);
472 /***********************************************************************
473 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
476 static LRESULT CALLBACK
477 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
479 WNDPROC superClassWndProc = (WNDPROC)GetPropW(hwnd, BUDDY_SUPERCLASS_WNDPROC);
481 TRACE("hwnd=%p, wndProc=%p, uMsg=%04x, wParam=%08lx, lParam=%08lx\n",
482 hwnd, superClassWndProc, uMsg, wParam, lParam);
484 if (uMsg == WM_KEYDOWN) {
485 HWND upDownHwnd = GetPropW(hwnd, BUDDY_UPDOWN_HWND);
487 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
490 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
493 /***********************************************************************
496 * Sets bud as a new Buddy.
497 * Then, it should subclass the buddy
498 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
499 * process the UP/DOWN arrow keys.
500 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
501 * the size/pos of the buddy and the control are adjusted accordingly.
503 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
505 static const WCHAR editW[] = { 'E', 'd', 'i', 't', 0 };
506 static const WCHAR listboxW[] = { 'L', 'i', 's', 't', 'b', 'o', 'x', 0 };
507 RECT budRect; /* new coord for the buddy */
508 int x, width; /* new x position and width for the up-down */
510 WCHAR buddyClass[40];
513 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
515 ret = infoPtr->Buddy;
517 /* there is already a body assigned */
518 if (infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
523 /* Store buddy window handle */
524 infoPtr->Buddy = bud;
528 /* keep upDown ctrl hwnd in a buddy property */
529 SetPropW( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
531 /* Store buddy window class type */
532 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
533 if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
534 if (lstrcmpiW(buddyClass, editW) == 0)
535 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
536 else if (lstrcmpiW(buddyClass, listboxW) == 0)
537 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
540 if(infoPtr->dwStyle & UDS_ARROWKEYS){
541 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
542 when we reset the upDown ctrl buddy to another buddy because it is not
543 good to break the window proc chain. */
544 if (!GetPropW(bud, BUDDY_SUPERCLASS_WNDPROC)) {
545 baseWndProc = (WNDPROC)SetWindowLongPtrW(bud, GWLP_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
546 SetPropW(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
550 /* Get the rect of the buddy relative to its parent */
551 GetWindowRect(infoPtr->Buddy, &budRect);
552 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
554 /* now do the positioning */
555 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
557 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
558 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
559 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
560 x = budRect.right+DEFAULT_XSEP;
566 /* first adjust the buddy to accommodate the up/down */
567 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
568 budRect.right - budRect.left, budRect.bottom - budRect.top,
569 SWP_NOACTIVATE|SWP_NOZORDER);
571 /* now position the up/down */
572 /* Since the UDS_ALIGN* flags were used, */
573 /* we will pick the position and size of the window. */
574 width = DEFAULT_WIDTH;
577 * If the updown has a buddy border, it has to overlap with the buddy
578 * to look as if it is integrated with the buddy control.
579 * We nudge the control or change its size to overlap.
581 if (UPDOWN_HasBuddyBorder(infoPtr)) {
582 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
583 width += DEFAULT_BUDDYBORDER;
585 x -= DEFAULT_BUDDYBORDER;
588 SetWindowPos(infoPtr->Self, 0, x,
589 budRect.top - DEFAULT_ADDTOP, width,
590 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
591 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
594 GetWindowRect(infoPtr->Self, &rect);
595 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
596 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
597 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
602 /***********************************************************************
605 * This function increments/decrements the CurVal by the
606 * 'delta' amount according to the 'action' flag which can be a
607 * combination of FLAG_INCR and FLAG_DECR
608 * It notifies the parent as required.
609 * It handles wraping and non-wraping correctly.
610 * It is assumed that delta>0
612 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
616 TRACE("%d by %d\n", action, delta);
618 /* check if we can do the modification first */
619 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
620 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
622 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
624 /* We must notify parent now to obtain permission */
625 ni.iPos = infoPtr->CurVal;
627 ni.hdr.hwndFrom = infoPtr->Self;
628 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
629 ni.hdr.code = UDN_DELTAPOS;
630 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
631 /* Parent said: OK to adjust */
633 /* Now adjust value with (maybe new) delta */
634 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
635 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
637 /* Now take care about our buddy */
638 UPDOWN_SetBuddyInt (infoPtr);
642 /* Also, notify it. This message is sent in any case. */
643 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
644 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
647 /***********************************************************************
650 * Returns TRUE if it is enabled as well as its buddy (if any)
653 static BOOL UPDOWN_IsEnabled (const UPDOWN_INFO *infoPtr)
655 if (!IsWindowEnabled(infoPtr->Self))
658 return IsWindowEnabled(infoPtr->Buddy);
662 /***********************************************************************
665 * Deletes any timers, releases the mouse and does redraw if necessary.
666 * If the control is not in "capture" mode, it does nothing.
667 * If the control was not in cancel mode, it returns FALSE.
668 * If the control was in cancel mode, it returns TRUE.
670 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
672 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
674 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
675 KillTimer (infoPtr->Self, TIMER_ACCEL);
676 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
678 if (GetCapture() == infoPtr->Self) {
680 hdr.hwndFrom = infoPtr->Self;
681 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
682 hdr.code = NM_RELEASEDCAPTURE;
683 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
687 infoPtr->Flags &= ~FLAG_PRESSED;
688 InvalidateRect (infoPtr->Self, NULL, FALSE);
693 /***********************************************************************
694 * UPDOWN_HandleMouseEvent
696 * Handle a mouse event for the updown.
697 * 'pt' is the location of the mouse event in client or
698 * windows coordinates.
700 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
707 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
711 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
713 /* If the buddy is an edit, will set focus to it */
714 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
716 /* Now see which one is the 'active' arrow */
717 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
719 /* Update the flags if we are in/out */
720 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
722 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
724 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
726 if (infoPtr->Flags & FLAG_ARROW) {
728 /* Update the CurVal if necessary */
729 UPDOWN_GetBuddyInt (infoPtr);
731 /* Set up the correct flags */
732 infoPtr->Flags |= FLAG_PRESSED;
734 /* repaint the control */
735 InvalidateRect (infoPtr->Self, NULL, FALSE);
737 /* process the click */
738 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
739 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
741 /* now capture all mouse messages */
742 SetCapture (infoPtr->Self);
744 /* and startup the first timer */
745 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
750 /* save the flags to see if any got modified */
751 temp = infoPtr->Flags;
753 /* Now see which one is the 'active' arrow */
754 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
756 /* Update the flags if we are in/out */
757 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
759 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
761 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
764 /* If state changed, redraw the control */
765 if(temp != infoPtr->Flags)
766 InvalidateRect (infoPtr->Self, NULL, FALSE);
768 /* Set up tracking so the mousein flags can be reset when the
769 * mouse leaves the control */
770 tme.cbSize = sizeof( tme );
771 tme.dwFlags = TME_LEAVE;
772 tme.hwndTrack = infoPtr->Self;
773 TrackMouseEvent (&tme);
777 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
778 InvalidateRect (infoPtr->Self, NULL, FALSE);
782 ERR("Impossible case (msg=%x)!\n", msg);
787 /***********************************************************************
790 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
792 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
794 static const WCHAR themeClass[] = {'S','p','i','n',0};
797 TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd, message, wParam, lParam);
799 if (!infoPtr && (message != WM_CREATE))
800 return DefWindowProcW (hwnd, message, wParam, lParam);
805 infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
806 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
808 /* initialize the info struct */
809 infoPtr->Self = hwnd;
810 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
811 infoPtr->dwStyle = ((LPCREATESTRUCTW)lParam)->style;
812 infoPtr->AccelCount = 0;
813 infoPtr->AccelVect = 0;
814 infoPtr->AccelIndex = -1;
816 infoPtr->MinVal = 100;
818 infoPtr->Base = 10; /* Default to base 10 */
819 infoPtr->Buddy = 0; /* No buddy window yet */
820 infoPtr->Flags = 0; /* And no flags */
822 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
824 /* Do we pick the buddy win ourselves? */
825 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
826 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
828 OpenThemeData (hwnd, themeClass);
830 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
834 Free (infoPtr->AccelVect);
836 if(infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
839 SetWindowLongPtrW (hwnd, 0, 0);
840 theme = GetWindowTheme (hwnd);
841 CloseThemeData (theme);
842 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
847 infoPtr->dwStyle &= ~WS_DISABLED;
849 infoPtr->dwStyle |= WS_DISABLED;
850 UPDOWN_CancelMode (infoPtr);
852 InvalidateRect (infoPtr->Self, NULL, FALSE);
855 case WM_STYLECHANGED:
856 if (wParam == GWL_STYLE) {
857 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
858 InvalidateRect (infoPtr->Self, NULL, FALSE);
862 case WM_THEMECHANGED:
863 theme = GetWindowTheme (hwnd);
864 CloseThemeData (theme);
865 OpenThemeData (hwnd, themeClass);
866 InvalidateRect (hwnd, NULL, FALSE);
870 /* is this the auto-press timer? */
871 if(wParam == TIMER_AUTOPRESS) {
872 KillTimer(hwnd, TIMER_AUTOPRESS);
873 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
874 InvalidateRect(infoPtr->Self, NULL, FALSE);
877 /* if initial timer, kill it and start the repeat timer */
878 if(wParam == TIMER_AUTOREPEAT) {
879 KillTimer(hwnd, TIMER_AUTOREPEAT);
880 /* if no accel info given, used default timer */
881 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
882 infoPtr->AccelIndex = -1;
885 infoPtr->AccelIndex = 0; /* otherwise, use it */
886 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
888 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
891 /* now, if the mouse is above us, do the thing...*/
892 if(infoPtr->Flags & FLAG_MOUSEIN) {
893 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
894 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
896 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
897 KillTimer(hwnd, TIMER_ACCEL);
898 infoPtr->AccelIndex++; /* move to the next accel info */
899 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
900 /* make sure we have at least 1ms intervals */
901 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
907 return UPDOWN_CancelMode (infoPtr);
910 if (GetCapture() != infoPtr->Self) break;
912 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
913 (infoPtr->Flags & FLAG_ARROW) ) {
915 SendMessageW( infoPtr->Notify,
916 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
917 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
919 if (UPDOWN_IsBuddyEdit(infoPtr))
920 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
922 UPDOWN_CancelMode(infoPtr);
928 if(UPDOWN_IsEnabled(infoPtr))
929 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
933 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
934 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
939 return UPDOWN_Paint (infoPtr, (HDC)wParam);
942 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
943 if (wParam && lParam) {
944 temp = min(infoPtr->AccelCount, wParam);
945 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
951 TRACE("UDM_SETACCEL\n");
953 if(infoPtr->AccelVect) {
954 Free (infoPtr->AccelVect);
955 infoPtr->AccelCount = 0;
956 infoPtr->AccelVect = 0;
958 if(wParam==0) return TRUE;
959 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
960 if(infoPtr->AccelVect == 0) return FALSE;
961 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
962 infoPtr->AccelCount = wParam;
964 for (temp = 0; temp < wParam; temp++)
965 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
970 return infoPtr->Base;
973 TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam, hwnd);
974 if (wParam==10 || wParam==16) {
975 temp = infoPtr->Base;
976 infoPtr->Base = wParam;
982 return (LRESULT)infoPtr->Buddy;
985 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
988 temp = UPDOWN_GetBuddyInt (infoPtr);
989 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
992 temp = (short)LOWORD(lParam);
993 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
994 if(!UPDOWN_InBounds(infoPtr, temp)) {
995 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
996 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
998 wParam = infoPtr->CurVal;
999 infoPtr->CurVal = temp;
1000 UPDOWN_SetBuddyInt (infoPtr);
1001 return wParam; /* return prev value */
1004 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
1008 infoPtr->MaxVal = (short)(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
1009 infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
1010 /* |Max-Min| <= UD_MAXVAL */
1011 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
1012 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
1015 case UDM_GETRANGE32:
1016 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
1017 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
1020 case UDM_SETRANGE32:
1021 infoPtr->MinVal = (INT)wParam;
1022 infoPtr->MaxVal = (INT)lParam;
1023 if (infoPtr->MaxVal <= infoPtr->MinVal)
1024 infoPtr->MaxVal = infoPtr->MinVal + 1;
1025 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
1026 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
1030 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
1031 return infoPtr->CurVal;
1034 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
1035 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
1036 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
1038 temp = infoPtr->CurVal; /* save prev value */
1039 infoPtr->CurVal = (int)lParam; /* set the new value */
1040 UPDOWN_SetBuddyInt (infoPtr);
1041 return temp; /* return prev value */
1043 case UDM_GETUNICODEFORMAT:
1044 /* we lie a bit here, we're always using Unicode internally */
1045 return infoPtr->UnicodeFormat;
1047 case UDM_SETUNICODEFORMAT:
1048 /* do we really need to honour this flag? */
1049 temp = infoPtr->UnicodeFormat;
1050 infoPtr->UnicodeFormat = (BOOL)wParam;
1054 if ((message >= WM_USER) && (message < WM_APP))
1055 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam);
1056 return DefWindowProcW (hwnd, message, wParam, lParam);
1062 /***********************************************************************
1063 * UPDOWN_Register [Internal]
1065 * Registers the updown window class.
1067 void UPDOWN_Register(void)
1071 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
1072 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1073 wndClass.lpfnWndProc = UpDownWindowProc;
1074 wndClass.cbClsExtra = 0;
1075 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
1076 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1077 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1078 wndClass.lpszClassName = UPDOWN_CLASSW;
1080 RegisterClassW( &wndClass );
1084 /***********************************************************************
1085 * UPDOWN_Unregister [Internal]
1087 * Unregisters the updown window class.
1089 void UPDOWN_Unregister (void)
1091 UnregisterClassW (UPDOWN_CLASSW, NULL);