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 */
450 WNDPROC baseWndProc, currWndProc;
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 currWndProc = (WNDPROC) GetWindowLongW(bud, GWL_WNDPROC);
481 if (currWndProc != UPDOWN_Buddy_SubclassProc) {
482 baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
483 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
487 /* Get the rect of the buddy relative to its parent */
488 GetWindowRect(infoPtr->Buddy, &budRect);
489 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
491 /* now do the positioning */
492 if (dwStyle & UDS_ALIGNLEFT) {
494 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
495 } else if (dwStyle & UDS_ALIGNRIGHT) {
496 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
497 x = budRect.right+DEFAULT_XSEP;
499 x = budRect.right+DEFAULT_XSEP;
502 /* first adjust the buddy to accomodate the up/down */
503 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
504 budRect.right - budRect.left, budRect.bottom - budRect.top,
505 SWP_NOACTIVATE|SWP_NOZORDER);
507 /* now position the up/down */
508 /* Since the UDS_ALIGN* flags were used, */
509 /* we will pick the position and size of the window. */
510 width = DEFAULT_WIDTH;
513 * If the updown has a buddy border, it has to overlap with the buddy
514 * to look as if it is integrated with the buddy control.
515 * We nudge the control or change it size to overlap.
517 if (UPDOWN_HasBuddyBorder(infoPtr)) {
518 if(dwStyle & UDS_ALIGNLEFT)
519 width += DEFAULT_BUDDYBORDER;
521 x -= DEFAULT_BUDDYBORDER;
524 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
525 budRect.top - DEFAULT_ADDTOP, width,
526 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
532 /***********************************************************************
535 * This function increments/decrements the CurVal by the
536 * 'delta' amount according to the 'action' flag which can be a
537 * combination of FLAG_INCR and FLAG_DECR
538 * It notifies the parent as required.
539 * It handles wraping and non-wraping correctly.
540 * It is assumed that delta>0
542 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
544 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
547 TRACE("%d by %d\n", action, delta);
549 /* check if we can do the modification first */
550 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
551 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
553 /* We must notify parent now to obtain permission */
554 ni.iPos = infoPtr->CurVal;
556 ni.hdr.hwndFrom = infoPtr->Self;
557 ni.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
558 ni.hdr.code = UDN_DELTAPOS;
559 if (!SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY,
560 (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
561 /* Parent said: OK to adjust */
563 /* Now adjust value with (maybe new) delta */
564 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
565 /* Now take care about our buddy */
566 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
570 /* Also, notify it. This message is sent in any case. */
571 SendMessageW( GetParent(infoPtr->Self),
572 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
573 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), infoPtr->Self);
576 /***********************************************************************
579 * Returns TRUE if it is enabled as well as its buddy (if any)
582 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
584 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
587 return IsWindowEnabled(infoPtr->Buddy);
591 /***********************************************************************
594 * Deletes any timers, releases the mouse and does redraw if necessary.
595 * If the control is not in "capture" mode, it does nothing.
596 * If the control was not in cancel mode, it returns FALSE.
597 * If the control was in cancel mode, it returns TRUE.
599 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
601 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
603 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
604 KillTimer (infoPtr->Self, TIMER_ACCEL);
605 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
607 if (GetCapture() == infoPtr->Self) {
609 hdr.hwndFrom = infoPtr->Self;
610 hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
611 hdr.code = NM_RELEASEDCAPTURE;
612 SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
616 infoPtr->Flags &= ~FLAG_PRESSED;
617 InvalidateRect (infoPtr->Self, NULL, FALSE);
622 /***********************************************************************
623 * UPDOWN_HandleMouseEvent
625 * Handle a mouse event for the updown.
626 * 'pt' is the location of the mouse event in client or
627 * windows coordinates.
629 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINT pt)
631 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
637 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
638 /* If we are inside an arrow, then nothing to do */
639 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
641 /* If the buddy is an edit, will set focus to it */
642 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
644 /* Now see which one is the 'active' arrow */
645 if (infoPtr->Flags & FLAG_ARROW) {
647 /* Update the CurVal if necessary */
648 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
650 /* Set up the correct flags */
651 infoPtr->Flags |= FLAG_PRESSED;
653 /* repaint the control */
654 InvalidateRect (infoPtr->Self, NULL, FALSE);
656 /* process the click */
657 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
659 /* now capture all mouse messages */
660 SetCapture (infoPtr->Self);
662 /* and startup the first timer */
663 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
668 /* save the flags to see if any got modified */
669 temp = infoPtr->Flags;
671 /* Now see which one is the 'active' arrow */
672 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
674 /* Update the flags if we are in/out */
675 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
677 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
679 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
682 /* If state changed, redraw the control */
683 if(temp != infoPtr->Flags)
684 InvalidateRect (infoPtr->Self, &rect, FALSE);
688 ERR("Impossible case (msg=%x)!\n", msg);
693 /***********************************************************************
696 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
699 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
700 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
703 if (!infoPtr && (message != WM_CREATE))
704 return DefWindowProcW (hwnd, message, wParam, lParam);
709 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
710 infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
711 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
713 /* initialize the info struct */
714 infoPtr->Self = hwnd;
715 infoPtr->AccelCount = 0;
716 infoPtr->AccelVect = 0;
717 infoPtr->AccelIndex = -1;
720 infoPtr->MaxVal = 9999;
721 infoPtr->Base = 10; /* Default to base 10 */
722 infoPtr->Buddy = 0; /* No buddy window yet */
723 infoPtr->Flags = 0; /* And no flags */
725 /* Do we pick the buddy win ourselves? */
726 if (dwStyle & UDS_AUTOBUDDY)
727 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
729 TRACE("UpDown Ctrl creation, hwnd=%04x\n", hwnd);
733 if(infoPtr->AccelVect) COMCTL32_Free (infoPtr->AccelVect);
735 if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
737 COMCTL32_Free (infoPtr);
738 SetWindowLongW (hwnd, 0, 0);
739 TRACE("UpDown Ctrl destruction, hwnd=%04x\n", hwnd);
743 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
744 InvalidateRect (infoPtr->Self, NULL, FALSE);
748 /* is this the auto-press timer? */
749 if(wParam == TIMER_AUTOPRESS) {
750 KillTimer(hwnd, TIMER_AUTOPRESS);
751 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
752 InvalidateRect(infoPtr->Self, NULL, FALSE);
755 /* if initial timer, kill it and start the repeat timer */
756 if(wParam == TIMER_AUTOREPEAT) {
757 KillTimer(hwnd, TIMER_AUTOREPEAT);
758 /* if no accel info given, used default timer */
759 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
760 infoPtr->AccelIndex = -1;
763 infoPtr->AccelIndex = 0; /* otherwise, use it */
764 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
766 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
769 /* now, if the mouse is above us, do the thing...*/
770 if(infoPtr->Flags & FLAG_MOUSEIN) {
771 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
772 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
774 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
775 KillTimer(hwnd, TIMER_ACCEL);
776 infoPtr->AccelIndex++; /* move to the next accel info */
777 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
778 /* make sure we have at least 1ms intervals */
779 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
785 return UPDOWN_CancelMode (infoPtr);
788 if (GetCapture() != infoPtr->Self) break;
790 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
791 (infoPtr->Flags & FLAG_ARROW) ) {
793 SendMessageW( GetParent(hwnd),
794 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
795 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal), hwnd);
796 if (UPDOWN_IsBuddyEdit(infoPtr))
797 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
799 UPDOWN_CancelMode(infoPtr);
804 if(UPDOWN_IsEnabled(infoPtr)){
806 pt.x = SLOWORD(lParam);
807 pt.y = SHIWORD(lParam);
808 UPDOWN_HandleMouseEvent (infoPtr, message, pt );
813 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr)) {
814 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
819 return UPDOWN_Paint (infoPtr, (HDC)wParam);
822 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
823 if (wParam && lParam) {
824 temp = min(infoPtr->AccelCount, wParam);
825 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
828 UNKNOWN_PARAM(UDM_GETACCEL, wParam, lParam);
832 TRACE("UpDown Ctrl new accel info, hwnd=%04x\n", hwnd);
833 if(infoPtr->AccelVect) {
834 COMCTL32_Free (infoPtr->AccelVect);
835 infoPtr->AccelCount = 0;
836 infoPtr->AccelVect = 0;
838 if(wParam==0) return TRUE;
839 infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
840 if(infoPtr->AccelVect == 0) return FALSE;
841 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
845 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETBASE, wParam, lParam);
846 return infoPtr->Base;
849 TRACE("UpDown Ctrl new base(%d), hwnd=%04x\n", wParam, hwnd);
850 if ( !(wParam==10 || wParam==16) || lParam)
851 UNKNOWN_PARAM(UDM_SETBASE, wParam, lParam);
852 if (wParam==10 || wParam==16) {
853 temp = infoPtr->Base;
854 infoPtr->Base = wParam;
860 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETBUDDY, wParam, lParam);
861 return infoPtr->Buddy;
864 if (lParam) UNKNOWN_PARAM(UDM_SETBUDDY, wParam, lParam);
865 temp = infoPtr->Buddy;
866 UPDOWN_SetBuddy (infoPtr, wParam);
870 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
871 temp = UPDOWN_GetBuddyInt (infoPtr);
872 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
875 if (wParam || HIWORD(lParam)) UNKNOWN_PARAM(UDM_GETPOS, wParam, lParam);
876 temp = SLOWORD(lParam);
877 TRACE("UpDown Ctrl new value(%d), hwnd=%04x\n", temp, hwnd);
878 if(!UPDOWN_InBounds(infoPtr, temp)) {
879 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
880 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
882 wParam = infoPtr->CurVal;
883 infoPtr->CurVal = temp;
884 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
885 return wParam; /* return prev value */
888 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETRANGE, wParam, lParam);
889 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
892 if (wParam) UNKNOWN_PARAM(UDM_SETRANGE, wParam, lParam);
894 infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
895 infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
896 /* |Max-Min| <= UD_MAXVAL */
897 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
898 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
902 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
903 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
907 infoPtr->MinVal = (INT)wParam;
908 infoPtr->MaxVal = (INT)lParam;
909 if (infoPtr->MaxVal <= infoPtr->MinVal)
910 infoPtr->MaxVal = infoPtr->MinVal + 1;
911 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
912 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
916 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
917 return infoPtr->CurVal;
920 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
921 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
922 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
924 temp = infoPtr->CurVal; /* save prev value */
925 infoPtr->CurVal = (int)lParam; /* set the new value */
926 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
927 return temp; /* return prev value */
929 case UDM_GETUNICODEFORMAT:
930 if (wParam || lParam) UNKNOWN_PARAM(UDM_GETUNICODEFORMAT, wParam, lParam);
931 /* we lie a bit here, we're always using Unicode internally */
932 return infoPtr->UnicodeFormat;
934 case UDM_SETUNICODEFORMAT:
935 if (lParam) UNKNOWN_PARAM(UDM_SETUNICODEFORMAT, wParam, lParam);
936 /* do we really need to honour this flag? */
937 temp = infoPtr->UnicodeFormat;
938 infoPtr->UnicodeFormat = (BOOL)wParam;
942 if (message >= WM_USER)
943 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
944 return DefWindowProcW (hwnd, message, wParam, lParam);
950 /***********************************************************************
951 * UPDOWN_Register [Internal]
953 * Registers the updown window class.
957 UPDOWN_Register(void)
961 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
962 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW;
963 wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
964 wndClass.cbClsExtra = 0;
965 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
966 wndClass.hCursor = LoadCursorW( 0, IDC_ARROWW );
967 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
968 wndClass.lpszClassName = UPDOWN_CLASSW;
970 RegisterClassW( &wndClass );
974 /***********************************************************************
975 * UPDOWN_Unregister [Internal]
977 * Unregisters the updown window class.
981 UPDOWN_Unregister (void)
983 UnregisterClassW (UPDOWN_CLASSW, (HINSTANCE)NULL);