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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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.
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(updown);
50 HWND Self; /* Handle to this up-down control */
51 HWND Notify; /* Handle to the parent window */
52 DWORD dwStyle; /* The GWL_STYLE for this window */
53 UINT AccelCount; /* Number of elements in AccelVect */
54 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
55 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
56 INT Base; /* Base to display nr in the buddy window */
57 INT CurVal; /* Current up-down value */
58 INT MinVal; /* Minimum up-down value */
59 INT MaxVal; /* Maximum up-down value */
60 HWND Buddy; /* Handle to the buddy window */
61 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
62 INT Flags; /* Internal Flags FLAG_* */
63 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
66 /* Control configuration constants */
68 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
69 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
70 #define REPEAT_DELAY 50 /* delay between auto-increments */
72 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
73 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
74 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
75 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
76 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
77 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
82 #define FLAG_INCR 0x01
83 #define FLAG_DECR 0x02
84 #define FLAG_MOUSEIN 0x04
85 #define FLAG_PRESSED 0x08
86 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
88 #define BUDDY_TYPE_UNKNOWN 0
89 #define BUDDY_TYPE_LISTBOX 1
90 #define BUDDY_TYPE_EDIT 2
92 #define TIMER_AUTOREPEAT 1
94 #define TIMER_AUTOPRESS 3
96 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
97 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
99 static const WCHAR BUDDY_UPDOWN_HWND[] = { 'b', 'u', 'd', 'd', 'y', 'U', 'p', 'D', 'o', 'w', 'n', 'H', 'W', 'N', 'D', 0 };
100 static const WCHAR BUDDY_SUPERCLASS_WNDPROC[] = { 'b', 'u', 'd', 'd', 'y', 'S', 'u', 'p', 'p', 'e', 'r',
101 'C', 'l', 'a', 's', 's', 'W', 'n', 'd', 'P', 'r', 'o', 'c', 0 };
102 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
104 /***********************************************************************
106 * Tests if our buddy is an edit control.
108 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
110 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
113 /***********************************************************************
114 * UPDOWN_IsBuddyListbox
115 * Tests if our buddy is a listbox control.
117 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
119 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
122 /***********************************************************************
124 * Tests if a given value 'val' is between the Min&Max limits
126 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
128 if(infoPtr->MaxVal > infoPtr->MinVal)
129 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
131 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
134 /***********************************************************************
136 * Change the current value by delta.
137 * It returns TRUE is the value was changed successfuly, or FALSE
138 * if the value was not changed, as it would go out of bounds.
140 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
142 /* check if we can do the modification first */
143 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
144 if (infoPtr->dwStyle & UDS_WRAP) {
145 delta += (delta < 0 ? -1 : 1) *
146 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
147 (infoPtr->MinVal - infoPtr->MaxVal) +
148 (delta < 0 ? 1 : -1);
152 infoPtr->CurVal += delta;
156 /***********************************************************************
157 * UPDOWN_HasBuddyBorder
159 * When we have a buddy set and that we are aligned on our buddy, we
160 * want to draw a sunken edge to make like we are part of that control.
162 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
164 return ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
165 UPDOWN_IsBuddyEdit(infoPtr) );
168 /***********************************************************************
169 * UPDOWN_GetArrowRect
170 * wndPtr - pointer to the up-down wnd
171 * rect - will hold the rectangle
172 * arrow - FLAG_INCR to get the "increment" rect (up or right)
173 * FLAG_DECR to get the "decrement" rect (down or left)
174 * If both flags are pressent, the envelope is returned.
176 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
178 GetClientRect (infoPtr->Self, rect);
181 * Make sure we calculate the rectangle to fit even if we draw the
184 if (UPDOWN_HasBuddyBorder(infoPtr)) {
185 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
186 rect->left += DEFAULT_BUDDYBORDER;
188 rect->right -= DEFAULT_BUDDYBORDER;
190 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
193 /* now figure out if we need a space away from the buddy */
194 if ( IsWindow(infoPtr->Buddy) ) {
195 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
196 else rect->left += DEFAULT_BUDDYSPACER;
200 * We're calculating the midpoint to figure-out where the
201 * separation between the buttons will lay. We make sure that we
202 * round the uneven numbers by adding 1.
204 if (infoPtr->dwStyle & UDS_HORZ) {
205 int len = rect->right - rect->left + 1; /* compute the width */
206 if (arrow & FLAG_INCR)
207 rect->left = rect->left + len/2;
208 if (arrow & FLAG_DECR)
209 rect->right = rect->left + len/2 - 1;
211 int len = rect->bottom - rect->top + 1; /* compute the height */
212 if (arrow & FLAG_INCR)
213 rect->bottom = rect->top + len/2 - 1;
214 if (arrow & FLAG_DECR)
215 rect->top = rect->top + len/2;
219 /***********************************************************************
220 * UPDOWN_GetArrowFromPoint
221 * Returns the rectagle (for the up or down arrow) that contains pt.
222 * If it returns the up rect, it returns TRUE.
223 * If it returns the down rect, it returns FALSE.
225 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
227 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
228 if(PtInRect(rect, pt)) return FLAG_INCR;
230 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
231 if(PtInRect(rect, pt)) return FLAG_DECR;
237 /***********************************************************************
238 * UPDOWN_GetThousandSep
239 * Returns the thousand sep. If an error occurs, it returns ','.
241 static WCHAR UPDOWN_GetThousandSep()
245 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
251 /***********************************************************************
253 * Tries to read the pos from the buddy window and if it succeeds,
254 * it stores it in the control's CurVal
256 * TRUE - if it read the integer from the buddy successfully
257 * FALSE - if an error occurred
259 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
261 WCHAR txt[20], sep, *src, *dst;
264 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
267 /*if the buddy is a list window, we must set curr index */
268 if (UPDOWN_IsBuddyListbox(infoPtr)) {
269 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
270 if(newVal < 0) return FALSE;
272 /* we have a regular window, so will get the text */
273 /* note that a zero-length string is a legitimate value for 'txt',
274 * and ought to result in a successful conversion to '0'. */
275 if (GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt)) < 0)
278 sep = UPDOWN_GetThousandSep();
280 /* now get rid of the separators */
281 for(src = dst = txt; *src; src++)
282 if(*src != sep) *dst++ = *src;
285 /* try to convert the number and validate it */
286 newVal = strtolW(txt, &src, infoPtr->Base);
287 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
290 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
291 infoPtr->CurVal = newVal;
296 /***********************************************************************
298 * Tries to set the pos to the buddy window based on current pos
300 * TRUE - if it set the caption of the buddy successfully
301 * FALSE - if an error occurred
303 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
305 WCHAR fmt[3] = { '%', 'd', '\0' };
309 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
312 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
314 /*if the buddy is a list window, we must set curr index */
315 if (UPDOWN_IsBuddyListbox(infoPtr)) {
316 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
319 /* Regular window, so set caption to the number */
320 if (infoPtr->Base == 16) fmt[1] = 'X';
321 len = wsprintfW(txt, fmt, infoPtr->CurVal);
324 /* Do thousands separation if necessary */
325 if (!(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
326 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
327 WCHAR sep = UPDOWN_GetThousandSep();
330 memcpy(tmp, txt, sizeof(txt));
331 if (start == 0) start = 3;
334 for (len=0; *src; len++) {
335 if (len % 3 == 0) *dst++ = sep;
341 return SetWindowTextW(infoPtr->Buddy, txt);
344 /***********************************************************************
347 * Draw the arrows. The background need not be erased.
349 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
354 /* Draw the common border between ourselves and our buddy */
355 if (UPDOWN_HasBuddyBorder(infoPtr)) {
356 GetClientRect(infoPtr->Self, &rect);
357 DrawEdge(hdc, &rect, EDGE_SUNKEN,
359 (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
362 /* Draw the incr button */
363 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
364 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
365 hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
366 DrawFrameControl(hdc, &rect, DFC_SCROLL,
367 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
368 ((infoPtr->dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
369 (pressed ? DFCS_PUSHED : 0) |
370 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
372 /* Draw the decr button */
373 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
374 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
375 hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
376 DrawFrameControl(hdc, &rect, DFC_SCROLL,
377 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
378 ((infoPtr->dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
379 (pressed ? DFCS_PUSHED : 0) |
380 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
385 /***********************************************************************
388 * Asynchronous drawing (must ONLY be used in WM_PAINT).
391 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
394 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
395 hdc = BeginPaint (infoPtr->Self, &ps);
396 UPDOWN_Draw (infoPtr, hdc);
397 EndPaint (infoPtr->Self, &ps);
401 /***********************************************************************
404 * Handle key presses (up & down) when we have to do so
406 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
410 if (key == VK_UP) arrow = FLAG_INCR;
411 else if (key == VK_DOWN) arrow = FLAG_DECR;
414 UPDOWN_GetBuddyInt (infoPtr);
415 infoPtr->Flags &= ~FLAG_ARROW;
416 infoPtr->Flags |= FLAG_PRESSED | arrow;
417 InvalidateRect (infoPtr->Self, NULL, FALSE);
418 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
419 UPDOWN_DoAction (infoPtr, 1, arrow);
423 /***********************************************************************
424 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
427 static LRESULT CALLBACK
428 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
430 WNDPROC superClassWndProc = (WNDPROC)GetPropW(hwnd, BUDDY_SUPERCLASS_WNDPROC);
432 TRACE("hwnd=%p, wndProc=%p, uMsg=%04x, wParam=%08x, lParam=%08lx\n",
433 hwnd, superClassWndProc, uMsg, wParam, lParam);
435 if (uMsg == WM_KEYDOWN) {
436 HWND upDownHwnd = GetPropW(hwnd, BUDDY_UPDOWN_HWND);
438 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
441 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
444 /***********************************************************************
447 * Sets bud as a new Buddy.
448 * Then, it should subclass the buddy
449 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
450 * process the UP/DOWN arrow keys.
451 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
452 * the size/pos of the buddy and the control are adjusted accordingly.
454 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
456 static const WCHAR editW[] = { 'E', 'd', 'i', 't', 0 };
457 static const WCHAR listboxW[] = { 'L', 'i', 's', 't', 'b', 'o', 'x', 0 };
458 RECT budRect; /* new coord for the buddy */
459 int x, width; /* new x position and width for the up-down */
461 WCHAR buddyClass[40];
464 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
466 ret = infoPtr->Buddy;
468 /* there is already a body assigned */
469 if (infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
474 /* Store buddy window handle */
475 infoPtr->Buddy = bud;
479 /* keep upDown ctrl hwnd in a buddy property */
480 SetPropW( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
482 /* Store buddy window class type */
483 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
484 if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
485 if (lstrcmpiW(buddyClass, editW) == 0)
486 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
487 else if (lstrcmpiW(buddyClass, listboxW) == 0)
488 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
491 if(infoPtr->dwStyle & UDS_ARROWKEYS){
492 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
493 when we reset the upDown ctrl buddy to another buddy because it is not
494 good to break the window proc chain. */
495 if (!GetPropW(bud, BUDDY_SUPERCLASS_WNDPROC)) {
496 baseWndProc = (WNDPROC)SetWindowLongPtrW(bud, GWLP_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
497 SetPropW(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
501 /* Get the rect of the buddy relative to its parent */
502 GetWindowRect(infoPtr->Buddy, &budRect);
503 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
505 /* now do the positioning */
506 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
508 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
509 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
510 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
511 x = budRect.right+DEFAULT_XSEP;
517 /* first adjust the buddy to accommodate the up/down */
518 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
519 budRect.right - budRect.left, budRect.bottom - budRect.top,
520 SWP_NOACTIVATE|SWP_NOZORDER);
522 /* now position the up/down */
523 /* Since the UDS_ALIGN* flags were used, */
524 /* we will pick the position and size of the window. */
525 width = DEFAULT_WIDTH;
528 * If the updown has a buddy border, it has to overlap with the buddy
529 * to look as if it is integrated with the buddy control.
530 * We nudge the control or change its size to overlap.
532 if (UPDOWN_HasBuddyBorder(infoPtr)) {
533 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
534 width += DEFAULT_BUDDYBORDER;
536 x -= DEFAULT_BUDDYBORDER;
539 SetWindowPos(infoPtr->Self, 0, x,
540 budRect.top - DEFAULT_ADDTOP, width,
541 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
542 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
545 GetWindowRect(infoPtr->Self, &rect);
546 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
547 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
548 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
553 /***********************************************************************
556 * This function increments/decrements the CurVal by the
557 * 'delta' amount according to the 'action' flag which can be a
558 * combination of FLAG_INCR and FLAG_DECR
559 * It notifies the parent as required.
560 * It handles wraping and non-wraping correctly.
561 * It is assumed that delta>0
563 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
567 TRACE("%d by %d\n", action, delta);
569 /* check if we can do the modification first */
570 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
571 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
573 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
575 /* We must notify parent now to obtain permission */
576 ni.iPos = infoPtr->CurVal;
578 ni.hdr.hwndFrom = infoPtr->Self;
579 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
580 ni.hdr.code = UDN_DELTAPOS;
581 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
582 /* Parent said: OK to adjust */
584 /* Now adjust value with (maybe new) delta */
585 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
586 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
588 /* Now take care about our buddy */
589 UPDOWN_SetBuddyInt (infoPtr);
593 /* Also, notify it. This message is sent in any case. */
594 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
595 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
598 /***********************************************************************
601 * Returns TRUE if it is enabled as well as its buddy (if any)
604 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
606 if (!IsWindowEnabled(infoPtr->Self))
609 return IsWindowEnabled(infoPtr->Buddy);
613 /***********************************************************************
616 * Deletes any timers, releases the mouse and does redraw if necessary.
617 * If the control is not in "capture" mode, it does nothing.
618 * If the control was not in cancel mode, it returns FALSE.
619 * If the control was in cancel mode, it returns TRUE.
621 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
623 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
625 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
626 KillTimer (infoPtr->Self, TIMER_ACCEL);
627 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
629 if (GetCapture() == infoPtr->Self) {
631 hdr.hwndFrom = infoPtr->Self;
632 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
633 hdr.code = NM_RELEASEDCAPTURE;
634 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
638 infoPtr->Flags &= ~FLAG_PRESSED;
639 InvalidateRect (infoPtr->Self, NULL, FALSE);
644 /***********************************************************************
645 * UPDOWN_HandleMouseEvent
647 * Handle a mouse event for the updown.
648 * 'pt' is the location of the mouse event in client or
649 * windows coordinates.
651 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
657 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
661 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
663 /* If the buddy is an edit, will set focus to it */
664 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
666 /* Now see which one is the 'active' arrow */
667 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
669 /* Update the flags if we are in/out */
670 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
672 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
674 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
676 if (infoPtr->Flags & FLAG_ARROW) {
678 /* Update the CurVal if necessary */
679 UPDOWN_GetBuddyInt (infoPtr);
681 /* Set up the correct flags */
682 infoPtr->Flags |= FLAG_PRESSED;
684 /* repaint the control */
685 InvalidateRect (infoPtr->Self, NULL, FALSE);
687 /* process the click */
688 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
689 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
691 /* now capture all mouse messages */
692 SetCapture (infoPtr->Self);
694 /* and startup the first timer */
695 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
700 /* save the flags to see if any got modified */
701 temp = infoPtr->Flags;
703 /* Now see which one is the 'active' arrow */
704 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
706 /* Update the flags if we are in/out */
707 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
709 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
711 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
714 /* If state changed, redraw the control */
715 if(temp != infoPtr->Flags)
716 InvalidateRect (infoPtr->Self, &rect, FALSE);
720 ERR("Impossible case (msg=%x)!\n", msg);
725 /***********************************************************************
728 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
730 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
733 TRACE("hwnd=%p msg=%04x wparam=%08x lparam=%08lx\n", hwnd, message, wParam, lParam);
735 if (!infoPtr && (message != WM_CREATE))
736 return DefWindowProcW (hwnd, message, wParam, lParam);
741 infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
742 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
744 /* initialize the info struct */
745 infoPtr->Self = hwnd;
746 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
747 infoPtr->dwStyle = ((LPCREATESTRUCTW)lParam)->style;
748 infoPtr->AccelCount = 0;
749 infoPtr->AccelVect = 0;
750 infoPtr->AccelIndex = -1;
752 infoPtr->MinVal = 100;
754 infoPtr->Base = 10; /* Default to base 10 */
755 infoPtr->Buddy = 0; /* No buddy window yet */
756 infoPtr->Flags = 0; /* And no flags */
758 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
760 /* Do we pick the buddy win ourselves? */
761 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
762 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
764 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
768 if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
770 if(infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
773 SetWindowLongPtrW (hwnd, 0, 0);
774 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
778 if (infoPtr->dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
779 InvalidateRect (infoPtr->Self, NULL, FALSE);
782 case WM_STYLECHANGED:
783 if (wParam == GWL_STYLE) {
784 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
785 InvalidateRect (infoPtr->Self, NULL, FALSE);
790 /* is this the auto-press timer? */
791 if(wParam == TIMER_AUTOPRESS) {
792 KillTimer(hwnd, TIMER_AUTOPRESS);
793 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
794 InvalidateRect(infoPtr->Self, NULL, FALSE);
797 /* if initial timer, kill it and start the repeat timer */
798 if(wParam == TIMER_AUTOREPEAT) {
799 KillTimer(hwnd, TIMER_AUTOREPEAT);
800 /* if no accel info given, used default timer */
801 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
802 infoPtr->AccelIndex = -1;
805 infoPtr->AccelIndex = 0; /* otherwise, use it */
806 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
808 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
811 /* now, if the mouse is above us, do the thing...*/
812 if(infoPtr->Flags & FLAG_MOUSEIN) {
813 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
814 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
816 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
817 KillTimer(hwnd, TIMER_ACCEL);
818 infoPtr->AccelIndex++; /* move to the next accel info */
819 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
820 /* make sure we have at least 1ms intervals */
821 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
827 return UPDOWN_CancelMode (infoPtr);
830 if (GetCapture() != infoPtr->Self) break;
832 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
833 (infoPtr->Flags & FLAG_ARROW) ) {
835 SendMessageW( infoPtr->Notify,
836 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
837 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
839 if (UPDOWN_IsBuddyEdit(infoPtr))
840 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
842 UPDOWN_CancelMode(infoPtr);
847 if(UPDOWN_IsEnabled(infoPtr))
848 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
852 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
853 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
857 return UPDOWN_Paint (infoPtr, (HDC)wParam);
860 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
861 if (wParam && lParam) {
862 temp = min(infoPtr->AccelCount, wParam);
863 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
869 TRACE("UDM_SETACCEL\n");
871 if(infoPtr->AccelVect) {
872 Free (infoPtr->AccelVect);
873 infoPtr->AccelCount = 0;
874 infoPtr->AccelVect = 0;
876 if(wParam==0) return TRUE;
877 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
878 if(infoPtr->AccelVect == 0) return FALSE;
879 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
880 infoPtr->AccelCount = wParam;
882 for (temp = 0; temp < wParam; temp++)
883 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
888 return infoPtr->Base;
891 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
892 if (wParam==10 || wParam==16) {
893 temp = infoPtr->Base;
894 infoPtr->Base = wParam;
900 return (LRESULT)infoPtr->Buddy;
903 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
906 temp = UPDOWN_GetBuddyInt (infoPtr);
907 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
910 temp = (short)LOWORD(lParam);
911 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
912 if(!UPDOWN_InBounds(infoPtr, temp)) {
913 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
914 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
916 wParam = infoPtr->CurVal;
917 infoPtr->CurVal = temp;
918 UPDOWN_SetBuddyInt (infoPtr);
919 return wParam; /* return prev value */
922 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
926 infoPtr->MaxVal = (short)(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
927 infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
928 /* |Max-Min| <= UD_MAXVAL */
929 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
930 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
934 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
935 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
939 infoPtr->MinVal = (INT)wParam;
940 infoPtr->MaxVal = (INT)lParam;
941 if (infoPtr->MaxVal <= infoPtr->MinVal)
942 infoPtr->MaxVal = infoPtr->MinVal + 1;
943 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
944 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
948 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
949 return infoPtr->CurVal;
952 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
953 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
954 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
956 temp = infoPtr->CurVal; /* save prev value */
957 infoPtr->CurVal = (int)lParam; /* set the new value */
958 UPDOWN_SetBuddyInt (infoPtr);
959 return temp; /* return prev value */
961 case UDM_GETUNICODEFORMAT:
962 /* we lie a bit here, we're always using Unicode internally */
963 return infoPtr->UnicodeFormat;
965 case UDM_SETUNICODEFORMAT:
966 /* do we really need to honour this flag? */
967 temp = infoPtr->UnicodeFormat;
968 infoPtr->UnicodeFormat = (BOOL)wParam;
972 if ((message >= WM_USER) && (message < WM_APP))
973 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
974 return DefWindowProcW (hwnd, message, wParam, lParam);
980 /***********************************************************************
981 * UPDOWN_Register [Internal]
983 * Registers the updown window class.
985 void UPDOWN_Register(void)
989 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
990 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
991 wndClass.lpfnWndProc = UpDownWindowProc;
992 wndClass.cbClsExtra = 0;
993 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
994 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
995 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
996 wndClass.lpszClassName = UPDOWN_CLASSW;
998 RegisterClassW( &wndClass );
1002 /***********************************************************************
1003 * UPDOWN_Unregister [Internal]
1005 * Unregisters the updown window class.
1007 void UPDOWN_Unregister (void)
1009 UnregisterClassW (UPDOWN_CLASSW, NULL);