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
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(updown);
39 HWND Self; /* Handle to this up-down control */
40 UINT AccelCount; /* Number of elements in AccelVect */
41 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
42 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
43 INT Base; /* Base to display nr in the buddy window */
44 INT CurVal; /* Current up-down value */
45 INT MinVal; /* Minimum up-down value */
46 INT MaxVal; /* Maximum up-down value */
47 HWND Buddy; /* Handle to the buddy window */
48 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
49 INT Flags; /* Internal Flags FLAG_* */
50 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
53 /* Control configuration constants */
55 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
56 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
57 #define REPEAT_DELAY 50 /* delay between auto-increments */
59 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
60 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
61 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
62 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
63 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
64 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
69 #define FLAG_INCR 0x01
70 #define FLAG_DECR 0x02
71 #define FLAG_MOUSEIN 0x04
72 #define FLAG_PRESSED 0x08
73 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
75 #define BUDDY_TYPE_UNKNOWN 0
76 #define BUDDY_TYPE_LISTBOX 1
77 #define BUDDY_TYPE_EDIT 2
79 #define TIMER_AUTOREPEAT 1
81 #define TIMER_AUTOPRESS 3
83 #define BUDDY_UPDOWN_HWND "buddyUpDownHWND"
84 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
86 #define UNKNOWN_PARAM(msg, wParam, lParam) WARN(\
87 "Unknown parameter(s) for message " #msg \
88 "(%04x): wp=%04x lp=%08lx\n", msg, wParam, lParam);
90 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
91 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
93 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
95 /***********************************************************************
97 * Tests if our buddy is an edit control.
99 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
101 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
104 /***********************************************************************
105 * UPDOWN_IsBuddyListbox
106 * Tests if our buddy is a listbox control.
108 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
110 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
113 /***********************************************************************
115 * Tests if a given value 'val' is between the Min&Max limits
117 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
119 if(infoPtr->MaxVal > infoPtr->MinVal)
120 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
122 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
125 /***********************************************************************
127 * Change the current value by delta.
128 * It returns TRUE is the value was changed successfuly, or FALSE
129 * if the value was not changed, as it would go out of bounds.
131 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
133 /* check if we can do the modification first */
134 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
135 if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP) {
136 delta += (delta < 0 ? -1 : 1) *
137 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
138 (infoPtr->MinVal - infoPtr->MaxVal) +
139 (delta < 0 ? 1 : -1);
143 infoPtr->CurVal += delta;
147 /***********************************************************************
148 * UPDOWN_HasBuddyBorder
150 * When we have a buddy set and that we are aligned on our buddy, we
151 * want to draw a sunken edge to make like we are part of that control.
153 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
155 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
157 return ( ((dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
158 UPDOWN_IsBuddyEdit(infoPtr) );
161 /***********************************************************************
162 * UPDOWN_GetArrowRect
163 * wndPtr - pointer to the up-down wnd
164 * rect - will hold the rectangle
165 * arrow - FLAG_INCR to get the "increment" rect (up or right)
166 * FLAG_DECR to get the "decrement" rect (down or left)
167 * If both flags are pressent, the envelope is returned.
169 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
171 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
173 GetClientRect (infoPtr->Self, rect);
176 * Make sure we calculate the rectangle to fit even if we draw the
179 if (UPDOWN_HasBuddyBorder(infoPtr)) {
180 if (dwStyle & UDS_ALIGNLEFT)
181 rect->left += DEFAULT_BUDDYBORDER;
183 rect->right -= DEFAULT_BUDDYBORDER;
185 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
188 /* now figure out if we need a space away from the buddy */
189 if ( IsWindow(infoPtr->Buddy) ) {
190 if (dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
191 else rect->left += DEFAULT_BUDDYSPACER;
195 * We're calculating the midpoint to figure-out where the
196 * separation between the buttons will lay. We make sure that we
197 * round the uneven numbers by adding 1.
199 if (dwStyle & UDS_HORZ) {
200 int len = rect->right - rect->left + 1; /* compute the width */
201 if (arrow & FLAG_INCR)
202 rect->left = rect->left + len/2;
203 if (arrow & FLAG_DECR)
204 rect->right = rect->left + len/2 - 1;
206 int len = rect->bottom - rect->top + 1; /* compute the height */
207 if (arrow & FLAG_INCR)
208 rect->bottom = rect->top + len/2 - 1;
209 if (arrow & FLAG_DECR)
210 rect->top = rect->top + len/2;
214 /***********************************************************************
215 * UPDOWN_GetArrowFromPoint
216 * Returns the rectagle (for the up or down arrow) that contains pt.
217 * If it returns the up rect, it returns TRUE.
218 * If it returns the down rect, it returns FALSE.
220 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
222 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
223 if(PtInRect(rect, pt)) return FLAG_INCR;
225 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
226 if(PtInRect(rect, pt)) return FLAG_DECR;
232 /***********************************************************************
233 * UPDOWN_GetThousandSep
234 * Returns the thousand sep. If an error occurs, it returns ','.
236 static WCHAR UPDOWN_GetThousandSep()
240 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
246 /***********************************************************************
248 * Tries to read the pos from the buddy window and if it succeeds,
249 * it stores it in the control's CurVal
251 * TRUE - if it read the integer from the buddy successfully
252 * FALSE - if an error occurred
254 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
256 WCHAR txt[20], sep, *src, *dst;
259 if (!IsWindow(infoPtr->Buddy))
262 /*if the buddy is a list window, we must set curr index */
263 if (UPDOWN_IsBuddyListbox(infoPtr)) {
264 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
265 if(newVal < 0) return FALSE;
267 /* we have a regular window, so will get the text */
268 if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
270 sep = UPDOWN_GetThousandSep();
272 /* now get rid of the separators */
273 for(src = dst = txt; *src; src++)
274 if(*src != sep) *dst++ = *src;
277 /* try to convert the number and validate it */
278 newVal = strtolW(txt, &src, infoPtr->Base);
279 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
282 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
283 infoPtr->CurVal = newVal;
288 /***********************************************************************
290 * Tries to set the pos to the buddy window based on current pos
292 * TRUE - if it set the caption of the buddy successfully
293 * FALSE - if an error occurred
295 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
297 WCHAR fmt[3] = { '%', 'd', '\0' };
301 if (!IsWindow(infoPtr->Buddy)) return FALSE;
303 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
305 /*if the buddy is a list window, we must set curr index */
306 if (UPDOWN_IsBuddyListbox(infoPtr)) {
307 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
310 /* Regular window, so set caption to the number */
311 if (infoPtr->Base == 16) fmt[1] = 'X';
312 len = wsprintfW(txt, fmt, infoPtr->CurVal);
315 /* Do thousands seperation if necessary */
316 if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
317 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
318 WCHAR sep = UPDOWN_GetThousandSep();
321 memcpy(tmp, txt, sizeof(txt));
322 if (start == 0) start = 3;
325 for (len=0; *src; len++) {
326 if (len % 3 == 0) *dst++ = sep;
332 return SetWindowTextW(infoPtr->Buddy, txt);
335 /***********************************************************************
338 * Draw the arrows. The background need not be erased.
340 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
342 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
346 /* Draw the common border between ourselves and our buddy */
347 if (UPDOWN_HasBuddyBorder(infoPtr)) {
348 GetClientRect(infoPtr->Self, &rect);
349 DrawEdge(hdc, &rect, EDGE_SUNKEN,
351 (dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
354 /* Draw the incr button */
355 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
356 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
357 hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
358 DrawFrameControl(hdc, &rect, DFC_SCROLL,
359 (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
360 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
361 (pressed ? DFCS_PUSHED : 0) |
362 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
364 /* Draw the decr button */
365 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
366 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
367 hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
368 DrawFrameControl(hdc, &rect, DFC_SCROLL,
369 (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
370 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
371 (pressed ? DFCS_PUSHED : 0) |
372 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
377 /***********************************************************************
380 * Asynchronous drawing (must ONLY be used in WM_PAINT).
383 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
386 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
387 hdc = BeginPaint (infoPtr->Self, &ps);
388 UPDOWN_Draw (infoPtr, hdc);
389 EndPaint (infoPtr->Self, &ps);
393 /***********************************************************************
396 * Handle key presses (up & down) when we have to do so
398 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
402 if (key == VK_UP) arrow = FLAG_INCR;
403 else if (key == VK_DOWN) arrow = FLAG_DECR;
406 UPDOWN_GetBuddyInt (infoPtr);
407 infoPtr->Flags &= ~FLAG_ARROW;
408 infoPtr->Flags |= FLAG_PRESSED | arrow;
409 InvalidateRect (infoPtr->Self, NULL, FALSE);
410 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
411 UPDOWN_DoAction (infoPtr, 1, arrow);
415 /***********************************************************************
416 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
419 static LRESULT CALLBACK
420 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
422 WNDPROC superClassWndProc = (WNDPROC)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
423 TRACE("hwnd=%04x, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
424 hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
426 if (uMsg == WM_KEYDOWN) {
427 HWND upDownHwnd = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
429 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
432 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
435 /***********************************************************************
437 * Tests if 'bud' is a valid window handle. If not, returns FALSE.
438 * Else, sets it as a new Buddy.
439 * Then, it should subclass the buddy
440 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
441 * process the UP/DOWN arrow keys.
442 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
443 * the size/pos of the buddy and the control are adjusted accordingly.
445 static BOOL UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
447 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
448 RECT budRect; /* new coord for the buddy */
449 int x, width; /* new x position and width for the up-down */
453 /* Is it a valid bud? */
454 if(!IsWindow(bud)) return FALSE;
456 TRACE("(hwnd=%04x, bud=%04x)\n", infoPtr->Self, bud);
458 /* there is already a body assigned */
459 if (infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
461 /* Store buddy window handle */
462 infoPtr->Buddy = bud;
464 /* keep upDown ctrl hwnd in a buddy property */
465 SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
467 /* Store buddy window class type */
468 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
469 if (GetClassNameA(bud, buddyClass, COUNT_OF(buddyClass))) {
470 if (lstrcmpiA(buddyClass, "Edit") == 0)
471 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
472 else if (lstrcmpiA(buddyClass, "Listbox") == 0)
473 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
476 if(dwStyle & UDS_ARROWKEYS){
477 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
478 when we reset the upDown ctrl buddy to another buddy because it is not
479 good to break the window proc chain. */
480 if (!GetPropA(bud, BUDDY_SUPERCLASS_WNDPROC)) {
481 baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
482 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
486 /* Get the rect of the buddy relative to its parent */
487 GetWindowRect(infoPtr->Buddy, &budRect);
488 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
490 /* now do the positioning */
491 if (dwStyle & UDS_ALIGNLEFT) {
493 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
494 } else if (dwStyle & UDS_ALIGNRIGHT) {
495 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
496 x = budRect.right+DEFAULT_XSEP;
498 x = budRect.right+DEFAULT_XSEP;
501 /* first adjust the buddy to accomodate the up/down */
502 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
503 budRect.right - budRect.left, budRect.bottom - budRect.top,
504 SWP_NOACTIVATE|SWP_NOZORDER);
506 /* now position the up/down */
507 /* Since the UDS_ALIGN* flags were used, */
508 /* we will pick the position and size of the window. */
509 width = DEFAULT_WIDTH;
512 * If the updown has a buddy border, it has to overlap with the buddy
513 * to look as if it is integrated with the buddy control.
514 * We nudge the control or change it size to overlap.
516 if (UPDOWN_HasBuddyBorder(infoPtr)) {
517 if(dwStyle & UDS_ALIGNLEFT)
518 width += DEFAULT_BUDDYBORDER;
520 x -= DEFAULT_BUDDYBORDER;
523 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
524 budRect.top - DEFAULT_ADDTOP, width,
525 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
531 /***********************************************************************
534 * This function increments/decrements the CurVal by the
535 * 'delta' amount according to the 'action' flag which can be a
536 * combination of FLAG_INCR and FLAG_DECR
537 * It notifies the parent as required.
538 * It handles wraping and non-wraping correctly.
539 * It is assumed that delta>0
541 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
543 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
546 TRACE("%d by %d\n", action, delta);
548 /* check if we can do the modification first */
549 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
550 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
552 /* We must notify parent now to obtain permission */
553 ni.iPos = infoPtr->CurVal;
555 ni.hdr.hwndFrom = infoPtr->Self;
556 ni.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
557 ni.hdr.code = UDN_DELTAPOS;
558 if (!SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY,
559 (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
560 /* Parent said: OK to adjust */
562 /* Now adjust value with (maybe new) delta */
563 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
564 /* Now take care about our buddy */
565 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
569 /* Also, notify it. This message is sent in any case. */
570 SendMessageW( GetParent(infoPtr->Self),
571 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
572 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), infoPtr->Self);
575 /***********************************************************************
578 * Returns TRUE if it is enabled as well as its buddy (if any)
581 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
583 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
586 return IsWindowEnabled(infoPtr->Buddy);
590 /***********************************************************************
593 * Deletes any timers, releases the mouse and does redraw if necessary.
594 * If the control is not in "capture" mode, it does nothing.
595 * If the control was not in cancel mode, it returns FALSE.
596 * If the control was in cancel mode, it returns TRUE.
598 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
600 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
602 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
603 KillTimer (infoPtr->Self, TIMER_ACCEL);
604 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
606 if (GetCapture() == infoPtr->Self) {
608 hdr.hwndFrom = infoPtr->Self;
609 hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
610 hdr.code = NM_RELEASEDCAPTURE;
611 SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
615 infoPtr->Flags &= ~FLAG_PRESSED;
616 InvalidateRect (infoPtr->Self, NULL, FALSE);
621 /***********************************************************************
622 * UPDOWN_HandleMouseEvent
624 * Handle a mouse event for the updown.
625 * 'pt' is the location of the mouse event in client or
626 * windows coordinates.
628 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINT pt)
630 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
636 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
637 /* If we are inside an arrow, then nothing to do */
638 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
640 /* If the buddy is an edit, will set focus to it */
641 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
643 /* Now see which one is the 'active' arrow */
644 if (infoPtr->Flags & FLAG_ARROW) {
646 /* Update the CurVal if necessary */
647 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
649 /* Set up the correct flags */
650 infoPtr->Flags |= FLAG_PRESSED;
652 /* repaint the control */
653 InvalidateRect (infoPtr->Self, NULL, FALSE);
655 /* process the click */
656 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
658 /* now capture all mouse messages */
659 SetCapture (infoPtr->Self);
661 /* and startup the first timer */
662 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
667 /* save the flags to see if any got modified */
668 temp = infoPtr->Flags;
670 /* Now see which one is the 'active' arrow */
671 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
673 /* Update the flags if we are in/out */
674 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
676 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
678 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
681 /* If state changed, redraw the control */
682 if(temp != infoPtr->Flags)
683 InvalidateRect (infoPtr->Self, &rect, FALSE);
687 ERR("Impossible case (msg=%x)!\n", msg);
692 /***********************************************************************
695 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
698 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
699 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
702 if (!infoPtr && (message != WM_CREATE))
703 return DefWindowProcW (hwnd, message, wParam, lParam);
708 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
709 infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
710 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
712 /* initialize the info struct */
713 infoPtr->Self = hwnd;
714 infoPtr->AccelCount = 0;
715 infoPtr->AccelVect = 0;
716 infoPtr->AccelIndex = -1;
719 infoPtr->MaxVal = 9999;
720 infoPtr->Base = 10; /* Default to base 10 */
721 infoPtr->Buddy = 0; /* No buddy window yet */
722 infoPtr->Flags = 0; /* And no flags */
724 /* Do we pick the buddy win ourselves? */
725 if (dwStyle & UDS_AUTOBUDDY)
726 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
728 TRACE("UpDown Ctrl creation, hwnd=%04x\n", hwnd);
732 if(infoPtr->AccelVect) COMCTL32_Free (infoPtr->AccelVect);
734 if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
736 COMCTL32_Free (infoPtr);
737 SetWindowLongW (hwnd, 0, 0);
738 TRACE("UpDown Ctrl destruction, hwnd=%04x\n", hwnd);
742 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
743 InvalidateRect (infoPtr->Self, NULL, FALSE);
747 /* is this the auto-press timer? */
748 if(wParam == TIMER_AUTOPRESS) {
749 KillTimer(hwnd, TIMER_AUTOPRESS);
750 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
751 InvalidateRect(infoPtr->Self, NULL, FALSE);
754 /* if initial timer, kill it and start the repeat timer */
755 if(wParam == TIMER_AUTOREPEAT) {
756 KillTimer(hwnd, TIMER_AUTOREPEAT);
757 /* if no accel info given, used default timer */
758 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
759 infoPtr->AccelIndex = -1;
762 infoPtr->AccelIndex = 0; /* otherwise, use it */
763 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
765 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
768 /* now, if the mouse is above us, do the thing...*/
769 if(infoPtr->Flags & FLAG_MOUSEIN) {
770 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
771 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
773 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
774 KillTimer(hwnd, TIMER_ACCEL);
775 infoPtr->AccelIndex++; /* move to the next accel info */
776 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
777 /* make sure we have at least 1ms intervals */
778 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
784 return UPDOWN_CancelMode (infoPtr);
787 if (GetCapture() != infoPtr->Self) break;
789 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
790 (infoPtr->Flags & FLAG_ARROW) ) {
792 SendMessageW( GetParent(hwnd),
793 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
794 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal), hwnd);
795 if (UPDOWN_IsBuddyEdit(infoPtr))
796 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
798 UPDOWN_CancelMode(infoPtr);
803 if(UPDOWN_IsEnabled(infoPtr)){
805 pt.x = SLOWORD(lParam);
806 pt.y = SHIWORD(lParam);
807 UPDOWN_HandleMouseEvent (infoPtr, message, pt );
812 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr)) {
813 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
818 return UPDOWN_Paint (infoPtr, (HDC)wParam);
821 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
822 if (wParam && lParam) {
823 temp = min(infoPtr->AccelCount, wParam);
824 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
827 UNKNOWN_PARAM(UDM_GETACCEL, wParam, lParam);
831 TRACE("UpDown Ctrl new accel info, hwnd=%04x\n", hwnd);
832 if(infoPtr->AccelVect) {
833 COMCTL32_Free (infoPtr->AccelVect);
834 infoPtr->AccelCount = 0;
835 infoPtr->AccelVect = 0;
837 if(wParam==0) return TRUE;
838 infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
839 if(infoPtr->AccelVect == 0) return FALSE;
840 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
844 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETBASE, wParam, lParam);
845 return infoPtr->Base;
848 TRACE("UpDown Ctrl new base(%d), hwnd=%04x\n", wParam, hwnd);
849 if ( !(wParam==10 || wParam==16) || lParam)
850 UNKNOWN_PARAM(UDM_SETBASE, wParam, lParam);
851 if (wParam==10 || wParam==16) {
852 temp = infoPtr->Base;
853 infoPtr->Base = wParam;
859 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETBUDDY, wParam, lParam);
860 return infoPtr->Buddy;
863 if (lParam) UNKNOWN_PARAM(UDM_SETBUDDY, wParam, lParam);
864 temp = infoPtr->Buddy;
865 UPDOWN_SetBuddy (infoPtr, wParam);
869 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
870 temp = UPDOWN_GetBuddyInt (infoPtr);
871 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
874 if (wParam || HIWORD(lParam)) UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
875 temp = SLOWORD(lParam);
876 TRACE("UpDown Ctrl new value(%d), hwnd=%04x\n", temp, hwnd);
877 if(!UPDOWN_InBounds(infoPtr, temp)) {
878 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
879 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
881 wParam = infoPtr->CurVal;
882 infoPtr->CurVal = temp;
883 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
884 return wParam; /* return prev value */
887 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETRANGE, wParam, lParam);
888 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
891 if (wParam) UNKNOWN_PARAM(UDM_SETRANGE, wParam, lParam);
893 infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
894 infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
895 /* |Max-Min| <= UD_MAXVAL */
896 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
897 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
901 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
902 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
906 infoPtr->MinVal = (INT)wParam;
907 infoPtr->MaxVal = (INT)lParam;
908 if (infoPtr->MaxVal <= infoPtr->MinVal)
909 infoPtr->MaxVal = infoPtr->MinVal + 1;
910 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
911 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
915 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
916 return infoPtr->CurVal;
919 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
920 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
921 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
923 temp = infoPtr->CurVal; /* save prev value */
924 infoPtr->CurVal = (int)lParam; /* set the new value */
925 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
926 return temp; /* return prev value */
928 case UDM_GETUNICODEFORMAT:
929 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETUNICODEFORMAT, wParam, lParam);
930 /* we lie a bit here, we're always using Unicode internally */
931 return infoPtr->UnicodeFormat;
933 case UDM_SETUNICODEFORMAT:
934 if (lParam) UNKNOWN_PARAM(UDM_SETUNICODEFORMAT, wParam, lParam);
935 /* do we really need to honour this flag? */
936 temp = infoPtr->UnicodeFormat;
937 infoPtr->UnicodeFormat = (BOOL)wParam;
941 if ((message >= WM_USER) && (message < WM_APP))
942 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
943 return DefWindowProcW (hwnd, message, wParam, lParam);
949 /***********************************************************************
950 * UPDOWN_Register [Internal]
952 * Registers the updown window class.
956 UPDOWN_Register(void)
960 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
961 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW;
962 wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
963 wndClass.cbClsExtra = 0;
964 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
965 wndClass.hCursor = LoadCursorW( 0, IDC_ARROWW );
966 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
967 wndClass.lpszClassName = UPDOWN_CLASSW;
969 RegisterClassW( &wndClass );
973 /***********************************************************************
974 * UPDOWN_Unregister [Internal]
976 * Unregisters the updown window class.
980 UPDOWN_Unregister (void)
982 UnregisterClassW (UPDOWN_CLASSW, (HINSTANCE)NULL);