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 UINT AccelCount; /* Number of elements in AccelVect */
53 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
54 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
55 INT Base; /* Base to display nr in the buddy window */
56 INT CurVal; /* Current up-down value */
57 INT MinVal; /* Minimum up-down value */
58 INT MaxVal; /* Maximum up-down value */
59 HWND Buddy; /* Handle to the buddy window */
60 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
61 INT Flags; /* Internal Flags FLAG_* */
62 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
65 /* Control configuration constants */
67 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
68 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
69 #define REPEAT_DELAY 50 /* delay between auto-increments */
71 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
72 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
73 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
74 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
75 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
76 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
81 #define FLAG_INCR 0x01
82 #define FLAG_DECR 0x02
83 #define FLAG_MOUSEIN 0x04
84 #define FLAG_PRESSED 0x08
85 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
87 #define BUDDY_TYPE_UNKNOWN 0
88 #define BUDDY_TYPE_LISTBOX 1
89 #define BUDDY_TYPE_EDIT 2
91 #define TIMER_AUTOREPEAT 1
93 #define TIMER_AUTOPRESS 3
95 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
96 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
98 static const WCHAR BUDDY_UPDOWN_HWND[] = { 'b', 'u', 'd', 'd', 'y', 'U', 'p', 'D', 'o', 'w', 'n', 'H', 'W', 'N', 'D', 0 };
99 static const WCHAR BUDDY_SUPERCLASS_WNDPROC[] = { 'b', 'u', 'd', 'd', 'y', 'S', 'u', 'p', 'p', 'e', 'r',
100 'C', 'l', 'a', 's', 's', 'W', 'n', 'd', 'P', 'r', 'o', 'c', 0 };
101 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
103 /***********************************************************************
105 * Tests if our buddy is an edit control.
107 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
109 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
112 /***********************************************************************
113 * UPDOWN_IsBuddyListbox
114 * Tests if our buddy is a listbox control.
116 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
118 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
121 /***********************************************************************
123 * Tests if a given value 'val' is between the Min&Max limits
125 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
127 if(infoPtr->MaxVal > infoPtr->MinVal)
128 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
130 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
133 /***********************************************************************
135 * Change the current value by delta.
136 * It returns TRUE is the value was changed successfuly, or FALSE
137 * if the value was not changed, as it would go out of bounds.
139 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
141 /* check if we can do the modification first */
142 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
143 if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP) {
144 delta += (delta < 0 ? -1 : 1) *
145 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
146 (infoPtr->MinVal - infoPtr->MaxVal) +
147 (delta < 0 ? 1 : -1);
151 infoPtr->CurVal += delta;
155 /***********************************************************************
156 * UPDOWN_HasBuddyBorder
158 * When we have a buddy set and that we are aligned on our buddy, we
159 * want to draw a sunken edge to make like we are part of that control.
161 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
163 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
165 return ( ((dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
166 UPDOWN_IsBuddyEdit(infoPtr) );
169 /***********************************************************************
170 * UPDOWN_GetArrowRect
171 * wndPtr - pointer to the up-down wnd
172 * rect - will hold the rectangle
173 * arrow - FLAG_INCR to get the "increment" rect (up or right)
174 * FLAG_DECR to get the "decrement" rect (down or left)
175 * If both flags are pressent, the envelope is returned.
177 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
179 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
181 GetClientRect (infoPtr->Self, rect);
184 * Make sure we calculate the rectangle to fit even if we draw the
187 if (UPDOWN_HasBuddyBorder(infoPtr)) {
188 if (dwStyle & UDS_ALIGNLEFT)
189 rect->left += DEFAULT_BUDDYBORDER;
191 rect->right -= DEFAULT_BUDDYBORDER;
193 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
196 /* now figure out if we need a space away from the buddy */
197 if ( IsWindow(infoPtr->Buddy) ) {
198 if (dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
199 else rect->left += DEFAULT_BUDDYSPACER;
203 * We're calculating the midpoint to figure-out where the
204 * separation between the buttons will lay. We make sure that we
205 * round the uneven numbers by adding 1.
207 if (dwStyle & UDS_HORZ) {
208 int len = rect->right - rect->left + 1; /* compute the width */
209 if (arrow & FLAG_INCR)
210 rect->left = rect->left + len/2;
211 if (arrow & FLAG_DECR)
212 rect->right = rect->left + len/2 - 1;
214 int len = rect->bottom - rect->top + 1; /* compute the height */
215 if (arrow & FLAG_INCR)
216 rect->bottom = rect->top + len/2 - 1;
217 if (arrow & FLAG_DECR)
218 rect->top = rect->top + len/2;
222 /***********************************************************************
223 * UPDOWN_GetArrowFromPoint
224 * Returns the rectagle (for the up or down arrow) that contains pt.
225 * If it returns the up rect, it returns TRUE.
226 * If it returns the down rect, it returns FALSE.
228 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
230 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
231 if(PtInRect(rect, pt)) return FLAG_INCR;
233 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
234 if(PtInRect(rect, pt)) return FLAG_DECR;
240 /***********************************************************************
241 * UPDOWN_GetThousandSep
242 * Returns the thousand sep. If an error occurs, it returns ','.
244 static WCHAR UPDOWN_GetThousandSep()
248 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
254 /***********************************************************************
256 * Tries to read the pos from the buddy window and if it succeeds,
257 * it stores it in the control's CurVal
259 * TRUE - if it read the integer from the buddy successfully
260 * FALSE - if an error occurred
262 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
264 WCHAR txt[20], sep, *src, *dst;
267 if (!IsWindow(infoPtr->Buddy))
270 /*if the buddy is a list window, we must set curr index */
271 if (UPDOWN_IsBuddyListbox(infoPtr)) {
272 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
273 if(newVal < 0) return FALSE;
275 /* we have a regular window, so will get the text */
276 if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
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 (!IsWindow(infoPtr->Buddy)) return FALSE;
311 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
313 /*if the buddy is a list window, we must set curr index */
314 if (UPDOWN_IsBuddyListbox(infoPtr)) {
315 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
318 /* Regular window, so set caption to the number */
319 if (infoPtr->Base == 16) fmt[1] = 'X';
320 len = wsprintfW(txt, fmt, infoPtr->CurVal);
323 /* Do thousands separation if necessary */
324 if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
325 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
326 WCHAR sep = UPDOWN_GetThousandSep();
329 memcpy(tmp, txt, sizeof(txt));
330 if (start == 0) start = 3;
333 for (len=0; *src; len++) {
334 if (len % 3 == 0) *dst++ = sep;
340 return SetWindowTextW(infoPtr->Buddy, txt);
343 /***********************************************************************
346 * Draw the arrows. The background need not be erased.
348 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
350 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
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 (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 (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
368 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
369 (pressed ? DFCS_PUSHED : 0) |
370 (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 (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
378 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
379 (pressed ? DFCS_PUSHED : 0) |
380 (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);
431 TRACE("hwnd=%p, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
432 hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
434 if (uMsg == WM_KEYDOWN) {
435 HWND upDownHwnd = GetPropW(hwnd, BUDDY_UPDOWN_HWND);
437 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
440 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
443 /***********************************************************************
446 * Sets bud as a new Buddy.
447 * Then, it should subclass the buddy
448 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
449 * process the UP/DOWN arrow keys.
450 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
451 * the size/pos of the buddy and the control are adjusted accordingly.
453 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
455 static const WCHAR editW[] = { 'E', 'd', 'i', 't', 0 };
456 static const WCHAR listboxW[] = { 'L', 'i', 's', 't', 'b', 'o', 'x', 0 };
457 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
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(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 (dwStyle & UDS_ALIGNLEFT) {
508 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
509 } else if (dwStyle & UDS_ALIGNRIGHT) {
510 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
511 x = budRect.right+DEFAULT_XSEP;
513 x = budRect.right+DEFAULT_XSEP;
516 /* first adjust the buddy to accommodate the up/down */
517 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
518 budRect.right - budRect.left, budRect.bottom - budRect.top,
519 SWP_NOACTIVATE|SWP_NOZORDER);
521 /* now position the up/down */
522 /* Since the UDS_ALIGN* flags were used, */
523 /* we will pick the position and size of the window. */
524 width = DEFAULT_WIDTH;
527 * If the updown has a buddy border, it has to overlap with the buddy
528 * to look as if it is integrated with the buddy control.
529 * We nudge the control or change its size to overlap.
531 if (UPDOWN_HasBuddyBorder(infoPtr)) {
532 if(dwStyle & UDS_ALIGNLEFT)
533 width += DEFAULT_BUDDYBORDER;
535 x -= DEFAULT_BUDDYBORDER;
538 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
539 budRect.top - DEFAULT_ADDTOP, width,
540 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
541 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
544 GetWindowRect(infoPtr->Self, &rect);
545 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
546 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
547 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
552 /***********************************************************************
555 * This function increments/decrements the CurVal by the
556 * 'delta' amount according to the 'action' flag which can be a
557 * combination of FLAG_INCR and FLAG_DECR
558 * It notifies the parent as required.
559 * It handles wraping and non-wraping correctly.
560 * It is assumed that delta>0
562 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
564 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
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 /* We must notify parent now to obtain permission */
574 ni.iPos = infoPtr->CurVal;
576 ni.hdr.hwndFrom = infoPtr->Self;
577 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
578 ni.hdr.code = UDN_DELTAPOS;
579 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
580 /* Parent said: OK to adjust */
582 /* Now adjust value with (maybe new) delta */
583 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
584 /* Now take care about our buddy */
585 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
589 /* Also, notify it. This message is sent in any case. */
590 SendMessageW( infoPtr->Notify, dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
591 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
594 /***********************************************************************
597 * Returns TRUE if it is enabled as well as its buddy (if any)
600 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
602 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
605 return IsWindowEnabled(infoPtr->Buddy);
609 /***********************************************************************
612 * Deletes any timers, releases the mouse and does redraw if necessary.
613 * If the control is not in "capture" mode, it does nothing.
614 * If the control was not in cancel mode, it returns FALSE.
615 * If the control was in cancel mode, it returns TRUE.
617 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
619 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
621 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
622 KillTimer (infoPtr->Self, TIMER_ACCEL);
623 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
625 if (GetCapture() == infoPtr->Self) {
627 hdr.hwndFrom = infoPtr->Self;
628 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
629 hdr.code = NM_RELEASEDCAPTURE;
630 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
634 infoPtr->Flags &= ~FLAG_PRESSED;
635 InvalidateRect (infoPtr->Self, NULL, FALSE);
640 /***********************************************************************
641 * UPDOWN_HandleMouseEvent
643 * Handle a mouse event for the updown.
644 * 'pt' is the location of the mouse event in client or
645 * windows coordinates.
647 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINTS pts)
649 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
650 POINT pt = { pts.x, pts.y };
656 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
657 /* If we are inside an arrow, then nothing to do */
658 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
660 /* If the buddy is an edit, will set focus to it */
661 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
663 /* Now see which one is the 'active' arrow */
664 if (infoPtr->Flags & FLAG_ARROW) {
666 /* Update the CurVal if necessary */
667 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
669 /* Set up the correct flags */
670 infoPtr->Flags |= FLAG_PRESSED;
672 /* repaint the control */
673 InvalidateRect (infoPtr->Self, NULL, FALSE);
675 /* process the click */
676 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
678 /* now capture all mouse messages */
679 SetCapture (infoPtr->Self);
681 /* and startup the first timer */
682 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
687 /* save the flags to see if any got modified */
688 temp = infoPtr->Flags;
690 /* Now see which one is the 'active' arrow */
691 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
693 /* Update the flags if we are in/out */
694 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
696 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
698 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
701 /* If state changed, redraw the control */
702 if(temp != infoPtr->Flags)
703 InvalidateRect (infoPtr->Self, &rect, FALSE);
707 ERR("Impossible case (msg=%x)!\n", msg);
712 /***********************************************************************
715 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
717 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
718 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
721 if (!infoPtr && (message != WM_CREATE))
722 return DefWindowProcW (hwnd, message, wParam, lParam);
727 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
728 infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
729 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
731 /* initialize the info struct */
732 infoPtr->Self = hwnd;
733 infoPtr->Notify = ((LPCREATESTRUCTA)lParam)->hwndParent;
734 infoPtr->AccelCount = 0;
735 infoPtr->AccelVect = 0;
736 infoPtr->AccelIndex = -1;
738 infoPtr->MinVal = 100;
740 infoPtr->Base = 10; /* Default to base 10 */
741 infoPtr->Buddy = 0; /* No buddy window yet */
742 infoPtr->Flags = 0; /* And no flags */
744 /* Do we pick the buddy win ourselves? */
745 if (dwStyle & UDS_AUTOBUDDY)
746 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
748 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
752 if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
754 if(infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
757 SetWindowLongPtrW (hwnd, 0, 0);
758 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
762 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
763 InvalidateRect (infoPtr->Self, NULL, FALSE);
767 /* is this the auto-press timer? */
768 if(wParam == TIMER_AUTOPRESS) {
769 KillTimer(hwnd, TIMER_AUTOPRESS);
770 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
771 InvalidateRect(infoPtr->Self, NULL, FALSE);
774 /* if initial timer, kill it and start the repeat timer */
775 if(wParam == TIMER_AUTOREPEAT) {
776 KillTimer(hwnd, TIMER_AUTOREPEAT);
777 /* if no accel info given, used default timer */
778 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
779 infoPtr->AccelIndex = -1;
782 infoPtr->AccelIndex = 0; /* otherwise, use it */
783 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
785 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
788 /* now, if the mouse is above us, do the thing...*/
789 if(infoPtr->Flags & FLAG_MOUSEIN) {
790 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
791 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
793 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
794 KillTimer(hwnd, TIMER_ACCEL);
795 infoPtr->AccelIndex++; /* move to the next accel info */
796 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
797 /* make sure we have at least 1ms intervals */
798 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
804 return UPDOWN_CancelMode (infoPtr);
807 if (GetCapture() != infoPtr->Self) break;
809 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
810 (infoPtr->Flags & FLAG_ARROW) ) {
812 SendMessageW( infoPtr->Notify,
813 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
814 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
816 if (UPDOWN_IsBuddyEdit(infoPtr))
817 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
819 UPDOWN_CancelMode(infoPtr);
824 if(UPDOWN_IsEnabled(infoPtr))
825 UPDOWN_HandleMouseEvent (infoPtr, message, MAKEPOINTS(lParam));
829 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
830 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
834 return UPDOWN_Paint (infoPtr, (HDC)wParam);
837 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
838 if (wParam && lParam) {
839 temp = min(infoPtr->AccelCount, wParam);
840 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
846 TRACE("UpDown Ctrl new accel info, hwnd=%p\n", hwnd);
847 if(infoPtr->AccelVect) {
848 Free (infoPtr->AccelVect);
849 infoPtr->AccelCount = 0;
850 infoPtr->AccelVect = 0;
852 if(wParam==0) return TRUE;
853 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
854 if(infoPtr->AccelVect == 0) return FALSE;
855 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
859 return infoPtr->Base;
862 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
863 if (wParam==10 || wParam==16) {
864 temp = infoPtr->Base;
865 infoPtr->Base = wParam;
871 return (LRESULT)infoPtr->Buddy;
874 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
877 temp = UPDOWN_GetBuddyInt (infoPtr);
878 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
881 temp = (short)LOWORD(lParam);
882 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
883 if(!UPDOWN_InBounds(infoPtr, temp)) {
884 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
885 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
887 wParam = infoPtr->CurVal;
888 infoPtr->CurVal = temp;
889 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
890 return wParam; /* return prev value */
893 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
897 infoPtr->MaxVal = (short)(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
898 infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
899 /* |Max-Min| <= UD_MAXVAL */
900 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
901 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
905 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
906 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
910 infoPtr->MinVal = (INT)wParam;
911 infoPtr->MaxVal = (INT)lParam;
912 if (infoPtr->MaxVal <= infoPtr->MinVal)
913 infoPtr->MaxVal = infoPtr->MinVal + 1;
914 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
915 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
919 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
920 return infoPtr->CurVal;
923 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
924 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
925 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
927 temp = infoPtr->CurVal; /* save prev value */
928 infoPtr->CurVal = (int)lParam; /* set the new value */
929 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
930 return temp; /* return prev value */
932 case UDM_GETUNICODEFORMAT:
933 /* we lie a bit here, we're always using Unicode internally */
934 return infoPtr->UnicodeFormat;
936 case UDM_SETUNICODEFORMAT:
937 /* do we really need to honour this flag? */
938 temp = infoPtr->UnicodeFormat;
939 infoPtr->UnicodeFormat = (BOOL)wParam;
943 if ((message >= WM_USER) && (message < WM_APP))
944 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
945 return DefWindowProcW (hwnd, message, wParam, lParam);
951 /***********************************************************************
952 * UPDOWN_Register [Internal]
954 * Registers the updown window class.
956 void UPDOWN_Register(void)
960 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
961 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
962 wndClass.lpfnWndProc = UpDownWindowProc;
963 wndClass.cbClsExtra = 0;
964 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
965 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
966 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
967 wndClass.lpszClassName = UPDOWN_CLASSW;
969 RegisterClassW( &wndClass );
973 /***********************************************************************
974 * UPDOWN_Unregister [Internal]
976 * Unregisters the updown window class.
978 void UPDOWN_Unregister (void)
980 UnregisterClassW (UPDOWN_CLASSW, NULL);