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 UINT AccelCount; /* Number of elements in AccelVect */
52 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
53 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
54 INT Base; /* Base to display nr in the buddy window */
55 INT CurVal; /* Current up-down value */
56 INT MinVal; /* Minimum up-down value */
57 INT MaxVal; /* Maximum up-down value */
58 HWND Buddy; /* Handle to the buddy window */
59 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
60 INT Flags; /* Internal Flags FLAG_* */
61 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
64 /* Control configuration constants */
66 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
67 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
68 #define REPEAT_DELAY 50 /* delay between auto-increments */
70 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
71 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
72 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
73 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
74 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
75 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
80 #define FLAG_INCR 0x01
81 #define FLAG_DECR 0x02
82 #define FLAG_MOUSEIN 0x04
83 #define FLAG_PRESSED 0x08
84 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
86 #define BUDDY_TYPE_UNKNOWN 0
87 #define BUDDY_TYPE_LISTBOX 1
88 #define BUDDY_TYPE_EDIT 2
90 #define TIMER_AUTOREPEAT 1
92 #define TIMER_AUTOPRESS 3
94 #define BUDDY_UPDOWN_HWND "buddyUpDownHWND"
95 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
97 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
98 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
100 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
102 /***********************************************************************
104 * Tests if our buddy is an edit control.
106 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
108 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
111 /***********************************************************************
112 * UPDOWN_IsBuddyListbox
113 * Tests if our buddy is a listbox control.
115 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
117 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
120 /***********************************************************************
122 * Tests if a given value 'val' is between the Min&Max limits
124 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
126 if(infoPtr->MaxVal > infoPtr->MinVal)
127 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
129 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
132 /***********************************************************************
134 * Change the current value by delta.
135 * It returns TRUE is the value was changed successfuly, or FALSE
136 * if the value was not changed, as it would go out of bounds.
138 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
140 /* check if we can do the modification first */
141 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
142 if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP) {
143 delta += (delta < 0 ? -1 : 1) *
144 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
145 (infoPtr->MinVal - infoPtr->MaxVal) +
146 (delta < 0 ? 1 : -1);
150 infoPtr->CurVal += delta;
154 /***********************************************************************
155 * UPDOWN_HasBuddyBorder
157 * When we have a buddy set and that we are aligned on our buddy, we
158 * want to draw a sunken edge to make like we are part of that control.
160 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
162 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
164 return ( ((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 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
180 GetClientRect (infoPtr->Self, rect);
183 * Make sure we calculate the rectangle to fit even if we draw the
186 if (UPDOWN_HasBuddyBorder(infoPtr)) {
187 if (dwStyle & UDS_ALIGNLEFT)
188 rect->left += DEFAULT_BUDDYBORDER;
190 rect->right -= DEFAULT_BUDDYBORDER;
192 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
195 /* now figure out if we need a space away from the buddy */
196 if ( IsWindow(infoPtr->Buddy) ) {
197 if (dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
198 else rect->left += DEFAULT_BUDDYSPACER;
202 * We're calculating the midpoint to figure-out where the
203 * separation between the buttons will lay. We make sure that we
204 * round the uneven numbers by adding 1.
206 if (dwStyle & UDS_HORZ) {
207 int len = rect->right - rect->left + 1; /* compute the width */
208 if (arrow & FLAG_INCR)
209 rect->left = rect->left + len/2;
210 if (arrow & FLAG_DECR)
211 rect->right = rect->left + len/2 - 1;
213 int len = rect->bottom - rect->top + 1; /* compute the height */
214 if (arrow & FLAG_INCR)
215 rect->bottom = rect->top + len/2 - 1;
216 if (arrow & FLAG_DECR)
217 rect->top = rect->top + len/2;
221 /***********************************************************************
222 * UPDOWN_GetArrowFromPoint
223 * Returns the rectagle (for the up or down arrow) that contains pt.
224 * If it returns the up rect, it returns TRUE.
225 * If it returns the down rect, it returns FALSE.
227 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
229 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
230 if(PtInRect(rect, pt)) return FLAG_INCR;
232 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
233 if(PtInRect(rect, pt)) return FLAG_DECR;
239 /***********************************************************************
240 * UPDOWN_GetThousandSep
241 * Returns the thousand sep. If an error occurs, it returns ','.
243 static WCHAR UPDOWN_GetThousandSep()
247 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
253 /***********************************************************************
255 * Tries to read the pos from the buddy window and if it succeeds,
256 * it stores it in the control's CurVal
258 * TRUE - if it read the integer from the buddy successfully
259 * FALSE - if an error occurred
261 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
263 WCHAR txt[20], sep, *src, *dst;
266 if (!IsWindow(infoPtr->Buddy))
269 /*if the buddy is a list window, we must set curr index */
270 if (UPDOWN_IsBuddyListbox(infoPtr)) {
271 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
272 if(newVal < 0) return FALSE;
274 /* we have a regular window, so will get the text */
275 if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
277 sep = UPDOWN_GetThousandSep();
279 /* now get rid of the separators */
280 for(src = dst = txt; *src; src++)
281 if(*src != sep) *dst++ = *src;
284 /* try to convert the number and validate it */
285 newVal = strtolW(txt, &src, infoPtr->Base);
286 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
289 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
290 infoPtr->CurVal = newVal;
295 /***********************************************************************
297 * Tries to set the pos to the buddy window based on current pos
299 * TRUE - if it set the caption of the buddy successfully
300 * FALSE - if an error occurred
302 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
304 WCHAR fmt[3] = { '%', 'd', '\0' };
308 if (!IsWindow(infoPtr->Buddy)) return FALSE;
310 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
312 /*if the buddy is a list window, we must set curr index */
313 if (UPDOWN_IsBuddyListbox(infoPtr)) {
314 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
317 /* Regular window, so set caption to the number */
318 if (infoPtr->Base == 16) fmt[1] = 'X';
319 len = wsprintfW(txt, fmt, infoPtr->CurVal);
322 /* Do thousands separation if necessary */
323 if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
324 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
325 WCHAR sep = UPDOWN_GetThousandSep();
328 memcpy(tmp, txt, sizeof(txt));
329 if (start == 0) start = 3;
332 for (len=0; *src; len++) {
333 if (len % 3 == 0) *dst++ = sep;
339 return SetWindowTextW(infoPtr->Buddy, txt);
342 /***********************************************************************
345 * Draw the arrows. The background need not be erased.
347 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
349 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
353 /* Draw the common border between ourselves and our buddy */
354 if (UPDOWN_HasBuddyBorder(infoPtr)) {
355 GetClientRect(infoPtr->Self, &rect);
356 DrawEdge(hdc, &rect, EDGE_SUNKEN,
358 (dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
361 /* Draw the incr button */
362 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
363 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
364 hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
365 DrawFrameControl(hdc, &rect, DFC_SCROLL,
366 (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
367 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
368 (pressed ? DFCS_PUSHED : 0) |
369 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
371 /* Draw the decr button */
372 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
373 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
374 hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
375 DrawFrameControl(hdc, &rect, DFC_SCROLL,
376 (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
377 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
378 (pressed ? DFCS_PUSHED : 0) |
379 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
384 /***********************************************************************
387 * Asynchronous drawing (must ONLY be used in WM_PAINT).
390 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
393 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
394 hdc = BeginPaint (infoPtr->Self, &ps);
395 UPDOWN_Draw (infoPtr, hdc);
396 EndPaint (infoPtr->Self, &ps);
400 /***********************************************************************
403 * Handle key presses (up & down) when we have to do so
405 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
409 if (key == VK_UP) arrow = FLAG_INCR;
410 else if (key == VK_DOWN) arrow = FLAG_DECR;
413 UPDOWN_GetBuddyInt (infoPtr);
414 infoPtr->Flags &= ~FLAG_ARROW;
415 infoPtr->Flags |= FLAG_PRESSED | arrow;
416 InvalidateRect (infoPtr->Self, NULL, FALSE);
417 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
418 UPDOWN_DoAction (infoPtr, 1, arrow);
422 /***********************************************************************
423 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
426 static LRESULT CALLBACK
427 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
429 WNDPROC superClassWndProc = (WNDPROC)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
430 TRACE("hwnd=%p, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
431 hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
433 if (uMsg == WM_KEYDOWN) {
434 HWND upDownHwnd = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
436 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
439 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
442 /***********************************************************************
444 * Tests if 'bud' is a valid window handle. If not, returns FALSE.
445 * Else, sets it as a new Buddy.
446 * Then, it should subclass the buddy
447 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
448 * process the UP/DOWN arrow keys.
449 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
450 * the size/pos of the buddy and the control are adjusted accordingly.
452 static BOOL UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
454 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
455 RECT budRect; /* new coord for the buddy */
456 int x, width; /* new x position and width for the up-down */
460 /* Is it a valid bud? */
461 if(!IsWindow(bud)) return FALSE;
463 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
465 /* there is already a body assigned */
466 if (infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
468 /* Store buddy window handle */
469 infoPtr->Buddy = bud;
471 /* keep upDown ctrl hwnd in a buddy property */
472 SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
474 /* Store buddy window class type */
475 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
476 if (GetClassNameA(bud, buddyClass, COUNT_OF(buddyClass))) {
477 if (lstrcmpiA(buddyClass, "Edit") == 0)
478 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
479 else if (lstrcmpiA(buddyClass, "Listbox") == 0)
480 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
483 if(dwStyle & UDS_ARROWKEYS){
484 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
485 when we reset the upDown ctrl buddy to another buddy because it is not
486 good to break the window proc chain. */
487 if (!GetPropA(bud, BUDDY_SUPERCLASS_WNDPROC)) {
488 baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
489 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
493 /* Get the rect of the buddy relative to its parent */
494 GetWindowRect(infoPtr->Buddy, &budRect);
495 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
497 /* now do the positioning */
498 if (dwStyle & UDS_ALIGNLEFT) {
500 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
501 } else if (dwStyle & UDS_ALIGNRIGHT) {
502 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
503 x = budRect.right+DEFAULT_XSEP;
505 x = budRect.right+DEFAULT_XSEP;
508 /* first adjust the buddy to accomodate the up/down */
509 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
510 budRect.right - budRect.left, budRect.bottom - budRect.top,
511 SWP_NOACTIVATE|SWP_NOZORDER);
513 /* now position the up/down */
514 /* Since the UDS_ALIGN* flags were used, */
515 /* we will pick the position and size of the window. */
516 width = DEFAULT_WIDTH;
519 * If the updown has a buddy border, it has to overlap with the buddy
520 * to look as if it is integrated with the buddy control.
521 * We nudge the control or change it size to overlap.
523 if (UPDOWN_HasBuddyBorder(infoPtr)) {
524 if(dwStyle & UDS_ALIGNLEFT)
525 width += DEFAULT_BUDDYBORDER;
527 x -= DEFAULT_BUDDYBORDER;
530 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
531 budRect.top - DEFAULT_ADDTOP, width,
532 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
538 /***********************************************************************
541 * This function increments/decrements the CurVal by the
542 * 'delta' amount according to the 'action' flag which can be a
543 * combination of FLAG_INCR and FLAG_DECR
544 * It notifies the parent as required.
545 * It handles wraping and non-wraping correctly.
546 * It is assumed that delta>0
548 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
550 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
553 TRACE("%d by %d\n", action, delta);
555 /* check if we can do the modification first */
556 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
557 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
559 /* We must notify parent now to obtain permission */
560 ni.iPos = infoPtr->CurVal;
562 ni.hdr.hwndFrom = infoPtr->Self;
563 ni.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
564 ni.hdr.code = UDN_DELTAPOS;
565 if (!SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY,
566 (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
567 /* Parent said: OK to adjust */
569 /* Now adjust value with (maybe new) delta */
570 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
571 /* Now take care about our buddy */
572 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
576 /* Also, notify it. This message is sent in any case. */
577 SendMessageW( GetParent(infoPtr->Self),
578 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
579 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal),
580 (LPARAM)infoPtr->Self);
583 /***********************************************************************
586 * Returns TRUE if it is enabled as well as its buddy (if any)
589 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
591 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
594 return IsWindowEnabled(infoPtr->Buddy);
598 /***********************************************************************
601 * Deletes any timers, releases the mouse and does redraw if necessary.
602 * If the control is not in "capture" mode, it does nothing.
603 * If the control was not in cancel mode, it returns FALSE.
604 * If the control was in cancel mode, it returns TRUE.
606 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
608 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
610 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
611 KillTimer (infoPtr->Self, TIMER_ACCEL);
612 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
614 if (GetCapture() == infoPtr->Self) {
616 hdr.hwndFrom = infoPtr->Self;
617 hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
618 hdr.code = NM_RELEASEDCAPTURE;
619 SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
623 infoPtr->Flags &= ~FLAG_PRESSED;
624 InvalidateRect (infoPtr->Self, NULL, FALSE);
629 /***********************************************************************
630 * UPDOWN_HandleMouseEvent
632 * Handle a mouse event for the updown.
633 * 'pt' is the location of the mouse event in client or
634 * windows coordinates.
636 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINTS pts)
638 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
639 POINT pt = { pts.x, pts.y };
645 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
646 /* If we are inside an arrow, then nothing to do */
647 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
649 /* If the buddy is an edit, will set focus to it */
650 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
652 /* Now see which one is the 'active' arrow */
653 if (infoPtr->Flags & FLAG_ARROW) {
655 /* Update the CurVal if necessary */
656 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
658 /* Set up the correct flags */
659 infoPtr->Flags |= FLAG_PRESSED;
661 /* repaint the control */
662 InvalidateRect (infoPtr->Self, NULL, FALSE);
664 /* process the click */
665 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
667 /* now capture all mouse messages */
668 SetCapture (infoPtr->Self);
670 /* and startup the first timer */
671 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
676 /* save the flags to see if any got modified */
677 temp = infoPtr->Flags;
679 /* Now see which one is the 'active' arrow */
680 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
682 /* Update the flags if we are in/out */
683 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
685 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
687 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
690 /* If state changed, redraw the control */
691 if(temp != infoPtr->Flags)
692 InvalidateRect (infoPtr->Self, &rect, FALSE);
696 ERR("Impossible case (msg=%x)!\n", msg);
701 /***********************************************************************
704 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
707 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
708 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
711 if (!infoPtr && (message != WM_CREATE))
712 return DefWindowProcW (hwnd, message, wParam, lParam);
717 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
718 infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
719 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
721 /* initialize the info struct */
722 infoPtr->Self = hwnd;
723 infoPtr->AccelCount = 0;
724 infoPtr->AccelVect = 0;
725 infoPtr->AccelIndex = -1;
727 infoPtr->MinVal = 100;
729 infoPtr->Base = 10; /* Default to base 10 */
730 infoPtr->Buddy = 0; /* No buddy window yet */
731 infoPtr->Flags = 0; /* And no flags */
733 /* Do we pick the buddy win ourselves? */
734 if (dwStyle & UDS_AUTOBUDDY)
735 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
737 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
741 if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
743 if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
746 SetWindowLongW (hwnd, 0, 0);
747 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
751 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
752 InvalidateRect (infoPtr->Self, NULL, FALSE);
756 /* is this the auto-press timer? */
757 if(wParam == TIMER_AUTOPRESS) {
758 KillTimer(hwnd, TIMER_AUTOPRESS);
759 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
760 InvalidateRect(infoPtr->Self, NULL, FALSE);
763 /* if initial timer, kill it and start the repeat timer */
764 if(wParam == TIMER_AUTOREPEAT) {
765 KillTimer(hwnd, TIMER_AUTOREPEAT);
766 /* if no accel info given, used default timer */
767 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
768 infoPtr->AccelIndex = -1;
771 infoPtr->AccelIndex = 0; /* otherwise, use it */
772 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
774 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
777 /* now, if the mouse is above us, do the thing...*/
778 if(infoPtr->Flags & FLAG_MOUSEIN) {
779 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
780 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
782 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
783 KillTimer(hwnd, TIMER_ACCEL);
784 infoPtr->AccelIndex++; /* move to the next accel info */
785 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
786 /* make sure we have at least 1ms intervals */
787 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
793 return UPDOWN_CancelMode (infoPtr);
796 if (GetCapture() != infoPtr->Self) break;
798 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
799 (infoPtr->Flags & FLAG_ARROW) ) {
801 SendMessageW( GetParent(hwnd),
802 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
803 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
805 if (UPDOWN_IsBuddyEdit(infoPtr))
806 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
808 UPDOWN_CancelMode(infoPtr);
813 if(UPDOWN_IsEnabled(infoPtr))
814 UPDOWN_HandleMouseEvent (infoPtr, message, MAKEPOINTS(lParam));
818 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
819 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
823 return UPDOWN_Paint (infoPtr, (HDC)wParam);
826 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
827 if (wParam && lParam) {
828 temp = min(infoPtr->AccelCount, wParam);
829 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
835 TRACE("UpDown Ctrl new accel info, hwnd=%p\n", hwnd);
836 if(infoPtr->AccelVect) {
837 Free (infoPtr->AccelVect);
838 infoPtr->AccelCount = 0;
839 infoPtr->AccelVect = 0;
841 if(wParam==0) return TRUE;
842 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
843 if(infoPtr->AccelVect == 0) return FALSE;
844 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
848 return infoPtr->Base;
851 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
852 if (wParam==10 || wParam==16) {
853 temp = infoPtr->Base;
854 infoPtr->Base = wParam;
860 return (LRESULT)infoPtr->Buddy;
863 temp = (int)infoPtr->Buddy;
864 UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
868 temp = UPDOWN_GetBuddyInt (infoPtr);
869 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
872 temp = (short)LOWORD(lParam);
873 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
874 if(!UPDOWN_InBounds(infoPtr, temp)) {
875 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
876 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
878 wParam = infoPtr->CurVal;
879 infoPtr->CurVal = temp;
880 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
881 return wParam; /* return prev value */
884 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
888 infoPtr->MaxVal = (short)(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
889 infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
890 /* |Max-Min| <= UD_MAXVAL */
891 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
892 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
896 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
897 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
901 infoPtr->MinVal = (INT)wParam;
902 infoPtr->MaxVal = (INT)lParam;
903 if (infoPtr->MaxVal <= infoPtr->MinVal)
904 infoPtr->MaxVal = infoPtr->MinVal + 1;
905 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
906 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
910 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
911 return infoPtr->CurVal;
914 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
915 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
916 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
918 temp = infoPtr->CurVal; /* save prev value */
919 infoPtr->CurVal = (int)lParam; /* set the new value */
920 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
921 return temp; /* return prev value */
923 case UDM_GETUNICODEFORMAT:
924 /* we lie a bit here, we're always using Unicode internally */
925 return infoPtr->UnicodeFormat;
927 case UDM_SETUNICODEFORMAT:
928 /* do we really need to honour this flag? */
929 temp = infoPtr->UnicodeFormat;
930 infoPtr->UnicodeFormat = (BOOL)wParam;
934 if ((message >= WM_USER) && (message < WM_APP))
935 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
936 return DefWindowProcW (hwnd, message, wParam, lParam);
942 /***********************************************************************
943 * UPDOWN_Register [Internal]
945 * Registers the updown window class.
947 void UPDOWN_Register(void)
951 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
952 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW;
953 wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
954 wndClass.cbClsExtra = 0;
955 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
956 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
957 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
958 wndClass.lpszClassName = UPDOWN_CLASSW;
960 RegisterClassW( &wndClass );
964 /***********************************************************************
965 * UPDOWN_Unregister [Internal]
967 * Unregisters the updown window class.
969 void UPDOWN_Unregister (void)
971 UnregisterClassW (UPDOWN_CLASSW, NULL);