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.
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(updown);
49 HWND Self; /* Handle to this up-down control */
50 UINT AccelCount; /* Number of elements in AccelVect */
51 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
52 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
53 INT Base; /* Base to display nr in the buddy window */
54 INT CurVal; /* Current up-down value */
55 INT MinVal; /* Minimum up-down value */
56 INT MaxVal; /* Maximum up-down value */
57 HWND Buddy; /* Handle to the buddy window */
58 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
59 INT Flags; /* Internal Flags FLAG_* */
60 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
63 /* Control configuration constants */
65 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
66 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
67 #define REPEAT_DELAY 50 /* delay between auto-increments */
69 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
70 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
71 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
72 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
73 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
74 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
79 #define FLAG_INCR 0x01
80 #define FLAG_DECR 0x02
81 #define FLAG_MOUSEIN 0x04
82 #define FLAG_PRESSED 0x08
83 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
85 #define BUDDY_TYPE_UNKNOWN 0
86 #define BUDDY_TYPE_LISTBOX 1
87 #define BUDDY_TYPE_EDIT 2
89 #define TIMER_AUTOREPEAT 1
91 #define TIMER_AUTOPRESS 3
93 #define BUDDY_UPDOWN_HWND "buddyUpDownHWND"
94 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
96 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
97 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
99 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
101 /***********************************************************************
103 * Tests if our buddy is an edit control.
105 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
107 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
110 /***********************************************************************
111 * UPDOWN_IsBuddyListbox
112 * Tests if our buddy is a listbox control.
114 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
116 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
119 /***********************************************************************
121 * Tests if a given value 'val' is between the Min&Max limits
123 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
125 if(infoPtr->MaxVal > infoPtr->MinVal)
126 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
128 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
131 /***********************************************************************
133 * Change the current value by delta.
134 * It returns TRUE is the value was changed successfuly, or FALSE
135 * if the value was not changed, as it would go out of bounds.
137 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
139 /* check if we can do the modification first */
140 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
141 if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP) {
142 delta += (delta < 0 ? -1 : 1) *
143 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
144 (infoPtr->MinVal - infoPtr->MaxVal) +
145 (delta < 0 ? 1 : -1);
149 infoPtr->CurVal += delta;
153 /***********************************************************************
154 * UPDOWN_HasBuddyBorder
156 * When we have a buddy set and that we are aligned on our buddy, we
157 * want to draw a sunken edge to make like we are part of that control.
159 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
161 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
163 return ( ((dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
164 UPDOWN_IsBuddyEdit(infoPtr) );
167 /***********************************************************************
168 * UPDOWN_GetArrowRect
169 * wndPtr - pointer to the up-down wnd
170 * rect - will hold the rectangle
171 * arrow - FLAG_INCR to get the "increment" rect (up or right)
172 * FLAG_DECR to get the "decrement" rect (down or left)
173 * If both flags are pressent, the envelope is returned.
175 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
177 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
179 GetClientRect (infoPtr->Self, rect);
182 * Make sure we calculate the rectangle to fit even if we draw the
185 if (UPDOWN_HasBuddyBorder(infoPtr)) {
186 if (dwStyle & UDS_ALIGNLEFT)
187 rect->left += DEFAULT_BUDDYBORDER;
189 rect->right -= DEFAULT_BUDDYBORDER;
191 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
194 /* now figure out if we need a space away from the buddy */
195 if ( IsWindow(infoPtr->Buddy) ) {
196 if (dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
197 else rect->left += DEFAULT_BUDDYSPACER;
201 * We're calculating the midpoint to figure-out where the
202 * separation between the buttons will lay. We make sure that we
203 * round the uneven numbers by adding 1.
205 if (dwStyle & UDS_HORZ) {
206 int len = rect->right - rect->left + 1; /* compute the width */
207 if (arrow & FLAG_INCR)
208 rect->left = rect->left + len/2;
209 if (arrow & FLAG_DECR)
210 rect->right = rect->left + len/2 - 1;
212 int len = rect->bottom - rect->top + 1; /* compute the height */
213 if (arrow & FLAG_INCR)
214 rect->bottom = rect->top + len/2 - 1;
215 if (arrow & FLAG_DECR)
216 rect->top = rect->top + len/2;
220 /***********************************************************************
221 * UPDOWN_GetArrowFromPoint
222 * Returns the rectagle (for the up or down arrow) that contains pt.
223 * If it returns the up rect, it returns TRUE.
224 * If it returns the down rect, it returns FALSE.
226 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
228 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
229 if(PtInRect(rect, pt)) return FLAG_INCR;
231 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
232 if(PtInRect(rect, pt)) return FLAG_DECR;
238 /***********************************************************************
239 * UPDOWN_GetThousandSep
240 * Returns the thousand sep. If an error occurs, it returns ','.
242 static WCHAR UPDOWN_GetThousandSep()
246 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
252 /***********************************************************************
254 * Tries to read the pos from the buddy window and if it succeeds,
255 * it stores it in the control's CurVal
257 * TRUE - if it read the integer from the buddy successfully
258 * FALSE - if an error occurred
260 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
262 WCHAR txt[20], sep, *src, *dst;
265 if (!IsWindow(infoPtr->Buddy))
268 /*if the buddy is a list window, we must set curr index */
269 if (UPDOWN_IsBuddyListbox(infoPtr)) {
270 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
271 if(newVal < 0) return FALSE;
273 /* we have a regular window, so will get the text */
274 if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
276 sep = UPDOWN_GetThousandSep();
278 /* now get rid of the separators */
279 for(src = dst = txt; *src; src++)
280 if(*src != sep) *dst++ = *src;
283 /* try to convert the number and validate it */
284 newVal = strtolW(txt, &src, infoPtr->Base);
285 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
288 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
289 infoPtr->CurVal = newVal;
294 /***********************************************************************
296 * Tries to set the pos to the buddy window based on current pos
298 * TRUE - if it set the caption of the buddy successfully
299 * FALSE - if an error occurred
301 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
303 WCHAR fmt[3] = { '%', 'd', '\0' };
307 if (!IsWindow(infoPtr->Buddy)) return FALSE;
309 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
311 /*if the buddy is a list window, we must set curr index */
312 if (UPDOWN_IsBuddyListbox(infoPtr)) {
313 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
316 /* Regular window, so set caption to the number */
317 if (infoPtr->Base == 16) fmt[1] = 'X';
318 len = wsprintfW(txt, fmt, infoPtr->CurVal);
321 /* Do thousands separation if necessary */
322 if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
323 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
324 WCHAR sep = UPDOWN_GetThousandSep();
327 memcpy(tmp, txt, sizeof(txt));
328 if (start == 0) start = 3;
331 for (len=0; *src; len++) {
332 if (len % 3 == 0) *dst++ = sep;
338 return SetWindowTextW(infoPtr->Buddy, txt);
341 /***********************************************************************
344 * Draw the arrows. The background need not be erased.
346 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
348 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
352 /* Draw the common border between ourselves and our buddy */
353 if (UPDOWN_HasBuddyBorder(infoPtr)) {
354 GetClientRect(infoPtr->Self, &rect);
355 DrawEdge(hdc, &rect, EDGE_SUNKEN,
357 (dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
360 /* Draw the incr button */
361 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
362 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
363 hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
364 DrawFrameControl(hdc, &rect, DFC_SCROLL,
365 (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
366 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
367 (pressed ? DFCS_PUSHED : 0) |
368 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
370 /* Draw the decr button */
371 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
372 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
373 hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
374 DrawFrameControl(hdc, &rect, DFC_SCROLL,
375 (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
376 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
377 (pressed ? DFCS_PUSHED : 0) |
378 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
383 /***********************************************************************
386 * Asynchronous drawing (must ONLY be used in WM_PAINT).
389 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
392 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
393 hdc = BeginPaint (infoPtr->Self, &ps);
394 UPDOWN_Draw (infoPtr, hdc);
395 EndPaint (infoPtr->Self, &ps);
399 /***********************************************************************
402 * Handle key presses (up & down) when we have to do so
404 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
408 if (key == VK_UP) arrow = FLAG_INCR;
409 else if (key == VK_DOWN) arrow = FLAG_DECR;
412 UPDOWN_GetBuddyInt (infoPtr);
413 infoPtr->Flags &= ~FLAG_ARROW;
414 infoPtr->Flags |= FLAG_PRESSED | arrow;
415 InvalidateRect (infoPtr->Self, NULL, FALSE);
416 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
417 UPDOWN_DoAction (infoPtr, 1, arrow);
421 /***********************************************************************
422 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
425 static LRESULT CALLBACK
426 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
428 WNDPROC superClassWndProc = (WNDPROC)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
429 TRACE("hwnd=%p, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
430 hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
432 if (uMsg == WM_KEYDOWN) {
433 HWND upDownHwnd = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
435 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
438 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
441 /***********************************************************************
443 * Tests if 'bud' is a valid window handle. If not, returns FALSE.
444 * Else, sets it as a new Buddy.
445 * Then, it should subclass the buddy
446 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
447 * process the UP/DOWN arrow keys.
448 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
449 * the size/pos of the buddy and the control are adjusted accordingly.
451 static BOOL UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
453 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
454 RECT budRect; /* new coord for the buddy */
455 int x, width; /* new x position and width for the up-down */
459 /* Is it a valid bud? */
460 if(!IsWindow(bud)) return FALSE;
462 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
464 /* there is already a body assigned */
465 if (infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
467 /* Store buddy window handle */
468 infoPtr->Buddy = bud;
470 /* keep upDown ctrl hwnd in a buddy property */
471 SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
473 /* Store buddy window class type */
474 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
475 if (GetClassNameA(bud, buddyClass, COUNT_OF(buddyClass))) {
476 if (lstrcmpiA(buddyClass, "Edit") == 0)
477 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
478 else if (lstrcmpiA(buddyClass, "Listbox") == 0)
479 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
482 if(dwStyle & UDS_ARROWKEYS){
483 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
484 when we reset the upDown ctrl buddy to another buddy because it is not
485 good to break the window proc chain. */
486 if (!GetPropA(bud, BUDDY_SUPERCLASS_WNDPROC)) {
487 baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
488 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
492 /* Get the rect of the buddy relative to its parent */
493 GetWindowRect(infoPtr->Buddy, &budRect);
494 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
496 /* now do the positioning */
497 if (dwStyle & UDS_ALIGNLEFT) {
499 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
500 } else if (dwStyle & UDS_ALIGNRIGHT) {
501 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
502 x = budRect.right+DEFAULT_XSEP;
504 x = budRect.right+DEFAULT_XSEP;
507 /* first adjust the buddy to accomodate the up/down */
508 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
509 budRect.right - budRect.left, budRect.bottom - budRect.top,
510 SWP_NOACTIVATE|SWP_NOZORDER);
512 /* now position the up/down */
513 /* Since the UDS_ALIGN* flags were used, */
514 /* we will pick the position and size of the window. */
515 width = DEFAULT_WIDTH;
518 * If the updown has a buddy border, it has to overlap with the buddy
519 * to look as if it is integrated with the buddy control.
520 * We nudge the control or change it size to overlap.
522 if (UPDOWN_HasBuddyBorder(infoPtr)) {
523 if(dwStyle & UDS_ALIGNLEFT)
524 width += DEFAULT_BUDDYBORDER;
526 x -= DEFAULT_BUDDYBORDER;
529 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
530 budRect.top - DEFAULT_ADDTOP, width,
531 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
537 /***********************************************************************
540 * This function increments/decrements the CurVal by the
541 * 'delta' amount according to the 'action' flag which can be a
542 * combination of FLAG_INCR and FLAG_DECR
543 * It notifies the parent as required.
544 * It handles wraping and non-wraping correctly.
545 * It is assumed that delta>0
547 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
549 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
552 TRACE("%d by %d\n", action, delta);
554 /* check if we can do the modification first */
555 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
556 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
558 /* We must notify parent now to obtain permission */
559 ni.iPos = infoPtr->CurVal;
561 ni.hdr.hwndFrom = infoPtr->Self;
562 ni.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
563 ni.hdr.code = UDN_DELTAPOS;
564 if (!SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY,
565 (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
566 /* Parent said: OK to adjust */
568 /* Now adjust value with (maybe new) delta */
569 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
570 /* Now take care about our buddy */
571 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
575 /* Also, notify it. This message is sent in any case. */
576 SendMessageW( GetParent(infoPtr->Self),
577 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
578 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal),
579 (LPARAM)infoPtr->Self);
582 /***********************************************************************
585 * Returns TRUE if it is enabled as well as its buddy (if any)
588 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
590 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
593 return IsWindowEnabled(infoPtr->Buddy);
597 /***********************************************************************
600 * Deletes any timers, releases the mouse and does redraw if necessary.
601 * If the control is not in "capture" mode, it does nothing.
602 * If the control was not in cancel mode, it returns FALSE.
603 * If the control was in cancel mode, it returns TRUE.
605 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
607 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
609 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
610 KillTimer (infoPtr->Self, TIMER_ACCEL);
611 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
613 if (GetCapture() == infoPtr->Self) {
615 hdr.hwndFrom = infoPtr->Self;
616 hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
617 hdr.code = NM_RELEASEDCAPTURE;
618 SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
622 infoPtr->Flags &= ~FLAG_PRESSED;
623 InvalidateRect (infoPtr->Self, NULL, FALSE);
628 /***********************************************************************
629 * UPDOWN_HandleMouseEvent
631 * Handle a mouse event for the updown.
632 * 'pt' is the location of the mouse event in client or
633 * windows coordinates.
635 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINTS pts)
637 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
638 POINT pt = { pts.x, pts.y };
644 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
645 /* If we are inside an arrow, then nothing to do */
646 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
648 /* If the buddy is an edit, will set focus to it */
649 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
651 /* Now see which one is the 'active' arrow */
652 if (infoPtr->Flags & FLAG_ARROW) {
654 /* Update the CurVal if necessary */
655 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
657 /* Set up the correct flags */
658 infoPtr->Flags |= FLAG_PRESSED;
660 /* repaint the control */
661 InvalidateRect (infoPtr->Self, NULL, FALSE);
663 /* process the click */
664 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
666 /* now capture all mouse messages */
667 SetCapture (infoPtr->Self);
669 /* and startup the first timer */
670 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
675 /* save the flags to see if any got modified */
676 temp = infoPtr->Flags;
678 /* Now see which one is the 'active' arrow */
679 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
681 /* Update the flags if we are in/out */
682 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
684 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
686 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
689 /* If state changed, redraw the control */
690 if(temp != infoPtr->Flags)
691 InvalidateRect (infoPtr->Self, &rect, FALSE);
695 ERR("Impossible case (msg=%x)!\n", msg);
700 /***********************************************************************
703 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
706 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
707 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
710 if (!infoPtr && (message != WM_CREATE))
711 return DefWindowProcW (hwnd, message, wParam, lParam);
716 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
717 infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
718 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
720 /* initialize the info struct */
721 infoPtr->Self = hwnd;
722 infoPtr->AccelCount = 0;
723 infoPtr->AccelVect = 0;
724 infoPtr->AccelIndex = -1;
726 infoPtr->MinVal = 100;
728 infoPtr->Base = 10; /* Default to base 10 */
729 infoPtr->Buddy = 0; /* No buddy window yet */
730 infoPtr->Flags = 0; /* And no flags */
732 /* Do we pick the buddy win ourselves? */
733 if (dwStyle & UDS_AUTOBUDDY)
734 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
736 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
740 if(infoPtr->AccelVect) COMCTL32_Free (infoPtr->AccelVect);
742 if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
744 COMCTL32_Free (infoPtr);
745 SetWindowLongW (hwnd, 0, 0);
746 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
750 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
751 InvalidateRect (infoPtr->Self, NULL, FALSE);
755 /* is this the auto-press timer? */
756 if(wParam == TIMER_AUTOPRESS) {
757 KillTimer(hwnd, TIMER_AUTOPRESS);
758 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
759 InvalidateRect(infoPtr->Self, NULL, FALSE);
762 /* if initial timer, kill it and start the repeat timer */
763 if(wParam == TIMER_AUTOREPEAT) {
764 KillTimer(hwnd, TIMER_AUTOREPEAT);
765 /* if no accel info given, used default timer */
766 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
767 infoPtr->AccelIndex = -1;
770 infoPtr->AccelIndex = 0; /* otherwise, use it */
771 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
773 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
776 /* now, if the mouse is above us, do the thing...*/
777 if(infoPtr->Flags & FLAG_MOUSEIN) {
778 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
779 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
781 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
782 KillTimer(hwnd, TIMER_ACCEL);
783 infoPtr->AccelIndex++; /* move to the next accel info */
784 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
785 /* make sure we have at least 1ms intervals */
786 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
792 return UPDOWN_CancelMode (infoPtr);
795 if (GetCapture() != infoPtr->Self) break;
797 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
798 (infoPtr->Flags & FLAG_ARROW) ) {
800 SendMessageW( GetParent(hwnd),
801 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
802 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
804 if (UPDOWN_IsBuddyEdit(infoPtr))
805 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
807 UPDOWN_CancelMode(infoPtr);
812 if(UPDOWN_IsEnabled(infoPtr))
813 UPDOWN_HandleMouseEvent (infoPtr, message, MAKEPOINTS(lParam));
817 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
818 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
822 return UPDOWN_Paint (infoPtr, (HDC)wParam);
825 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
826 if (wParam && lParam) {
827 temp = min(infoPtr->AccelCount, wParam);
828 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
834 TRACE("UpDown Ctrl new accel info, hwnd=%p\n", hwnd);
835 if(infoPtr->AccelVect) {
836 COMCTL32_Free (infoPtr->AccelVect);
837 infoPtr->AccelCount = 0;
838 infoPtr->AccelVect = 0;
840 if(wParam==0) return TRUE;
841 infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
842 if(infoPtr->AccelVect == 0) return FALSE;
843 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
847 return infoPtr->Base;
850 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
851 if (wParam==10 || wParam==16) {
852 temp = infoPtr->Base;
853 infoPtr->Base = wParam;
859 return (LRESULT)infoPtr->Buddy;
862 temp = (int)infoPtr->Buddy;
863 UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
867 temp = UPDOWN_GetBuddyInt (infoPtr);
868 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
871 temp = SLOWORD(lParam);
872 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
873 if(!UPDOWN_InBounds(infoPtr, temp)) {
874 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
875 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
877 wParam = infoPtr->CurVal;
878 infoPtr->CurVal = temp;
879 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
880 return wParam; /* return prev value */
883 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
887 infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
888 infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
889 /* |Max-Min| <= UD_MAXVAL */
890 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
891 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
895 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
896 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
900 infoPtr->MinVal = (INT)wParam;
901 infoPtr->MaxVal = (INT)lParam;
902 if (infoPtr->MaxVal <= infoPtr->MinVal)
903 infoPtr->MaxVal = infoPtr->MinVal + 1;
904 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
905 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
909 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
910 return infoPtr->CurVal;
913 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
914 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
915 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
917 temp = infoPtr->CurVal; /* save prev value */
918 infoPtr->CurVal = (int)lParam; /* set the new value */
919 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
920 return temp; /* return prev value */
922 case UDM_GETUNICODEFORMAT:
923 /* we lie a bit here, we're always using Unicode internally */
924 return infoPtr->UnicodeFormat;
926 case UDM_SETUNICODEFORMAT:
927 /* do we really need to honour this flag? */
928 temp = infoPtr->UnicodeFormat;
929 infoPtr->UnicodeFormat = (BOOL)wParam;
933 if ((message >= WM_USER) && (message < WM_APP))
934 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
935 return DefWindowProcW (hwnd, message, wParam, lParam);
941 /***********************************************************************
942 * UPDOWN_Register [Internal]
944 * Registers the updown window class.
946 void UPDOWN_Register(void)
950 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
951 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW;
952 wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
953 wndClass.cbClsExtra = 0;
954 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
955 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
956 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
957 wndClass.lpszClassName = UPDOWN_CLASSW;
959 RegisterClassW( &wndClass );
963 /***********************************************************************
964 * UPDOWN_Unregister [Internal]
966 * Unregisters the updown window class.
968 void UPDOWN_Unregister (void)
970 UnregisterClassW (UPDOWN_CLASSW, NULL);