Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / comctl32 / updown.c
blob076e315894aa67ae97d458fe92b51226d71f85cb
1 /*
2 * Updown control
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTE
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.
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <stdio.h>
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "commctrl.h"
42 #include "comctl32.h"
43 #include "uxtheme.h"
44 #include "tmschema.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(updown);
50 typedef struct
52 HWND Self; /* Handle to this up-down control */
53 HWND Notify; /* Handle to the parent window */
54 DWORD dwStyle; /* The GWL_STYLE for this window */
55 UINT AccelCount; /* Number of elements in AccelVect */
56 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
57 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
58 INT Base; /* Base to display nr in the buddy window */
59 INT CurVal; /* Current up-down value */
60 INT MinVal; /* Minimum up-down value */
61 INT MaxVal; /* Maximum up-down value */
62 HWND Buddy; /* Handle to the buddy window */
63 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
64 INT Flags; /* Internal Flags FLAG_* */
65 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
66 } UPDOWN_INFO;
68 /* Control configuration constants */
70 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
71 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
72 #define REPEAT_DELAY 50 /* delay between auto-increments */
74 #define DEFAULT_WIDTH 16 /* default width of the ctrl */
75 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
76 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
77 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
78 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
79 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
80 #define DEFAULT_BUDDYBORDER_THEMED 1 /* buddy border when theming is enabled */
81 #define DEFAULT_BUDDYSPACER_THEMED 0 /* buddy spacer when theming is enabled */
83 /* Work constants */
85 #define FLAG_INCR 0x01
86 #define FLAG_DECR 0x02
87 #define FLAG_MOUSEIN 0x04
88 #define FLAG_PRESSED 0x08
89 #define FLAG_BUDDYINT 0x10 /* UDS_SETBUDDYINT was set on creation */
90 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
92 #define BUDDY_TYPE_UNKNOWN 0
93 #define BUDDY_TYPE_LISTBOX 1
94 #define BUDDY_TYPE_EDIT 2
96 #define TIMER_AUTOREPEAT 1
97 #define TIMER_ACCEL 2
98 #define TIMER_AUTOPRESS 3
100 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
101 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
103 /* id used for SetWindowSubclass */
104 #define BUDDY_SUBCLASSID 1
106 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
108 /***********************************************************************
109 * UPDOWN_IsBuddyEdit
110 * Tests if our buddy is an edit control.
112 static inline BOOL UPDOWN_IsBuddyEdit(const UPDOWN_INFO *infoPtr)
114 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
117 /***********************************************************************
118 * UPDOWN_IsBuddyListbox
119 * Tests if our buddy is a listbox control.
121 static inline BOOL UPDOWN_IsBuddyListbox(const UPDOWN_INFO *infoPtr)
123 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
126 /***********************************************************************
127 * UPDOWN_InBounds
128 * Tests if a given value 'val' is between the Min&Max limits
130 static BOOL UPDOWN_InBounds(const UPDOWN_INFO *infoPtr, int val)
132 if(infoPtr->MaxVal > infoPtr->MinVal)
133 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
134 else
135 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
138 /***********************************************************************
139 * UPDOWN_OffsetVal
140 * Change the current value by delta.
141 * It returns TRUE is the value was changed successfully, or FALSE
142 * if the value was not changed, as it would go out of bounds.
144 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
146 /* check if we can do the modification first */
147 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
148 if (infoPtr->dwStyle & UDS_WRAP) {
149 delta += (delta < 0 ? -1 : 1) *
150 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
151 (infoPtr->MinVal - infoPtr->MaxVal) +
152 (delta < 0 ? 1 : -1);
153 } else return FALSE;
156 infoPtr->CurVal += delta;
157 return TRUE;
160 /***********************************************************************
161 * UPDOWN_HasBuddyBorder
163 * When we have a buddy set and that we are aligned on our buddy, we
164 * want to draw a sunken edge to make like we are part of that control.
166 static BOOL UPDOWN_HasBuddyBorder(const UPDOWN_INFO *infoPtr)
168 return ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
169 UPDOWN_IsBuddyEdit(infoPtr) );
172 /***********************************************************************
173 * UPDOWN_GetArrowRect
174 * wndPtr - pointer to the up-down wnd
175 * rect - will hold the rectangle
176 * arrow - FLAG_INCR to get the "increment" rect (up or right)
177 * FLAG_DECR to get the "decrement" rect (down or left)
178 * If both flags are present, the envelope is returned.
180 static void UPDOWN_GetArrowRect (const UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
182 HTHEME theme = GetWindowTheme (infoPtr->Self);
183 const int border = theme ? DEFAULT_BUDDYBORDER_THEMED : DEFAULT_BUDDYBORDER;
184 const int spacer = theme ? DEFAULT_BUDDYSPACER_THEMED : DEFAULT_BUDDYSPACER;
185 GetClientRect (infoPtr->Self, rect);
188 * Make sure we calculate the rectangle to fit even if we draw the
189 * border.
191 if (UPDOWN_HasBuddyBorder(infoPtr)) {
192 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
193 rect->left += border;
194 else
195 rect->right -= border;
197 InflateRect(rect, 0, -border);
200 /* now figure out if we need a space away from the buddy */
201 if (IsWindow(infoPtr->Buddy) ) {
202 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= spacer;
203 else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) rect->left += spacer;
207 * We're calculating the midpoint to figure-out where the
208 * separation between the buttons will lay. We make sure that we
209 * round the uneven numbers by adding 1.
211 if (infoPtr->dwStyle & UDS_HORZ) {
212 int len = rect->right - rect->left + 1; /* compute the width */
213 if (arrow & FLAG_INCR)
214 rect->left = rect->left + len/2;
215 if (arrow & FLAG_DECR)
216 rect->right = rect->left + len/2 - (theme ? 0 : 1);
217 } else {
218 int len = rect->bottom - rect->top + 1; /* compute the height */
219 if (arrow & FLAG_INCR)
220 rect->bottom = rect->top + len/2 - (theme ? 0 : 1);
221 if (arrow & FLAG_DECR)
222 rect->top = rect->top + len/2;
226 /***********************************************************************
227 * UPDOWN_GetArrowFromPoint
228 * Returns the rectagle (for the up or down arrow) that contains pt.
229 * If it returns the up rect, it returns FLAG_INCR.
230 * If it returns the down rect, it returns FLAG_DECR.
232 static INT UPDOWN_GetArrowFromPoint (const UPDOWN_INFO *infoPtr, RECT *rect, POINT pt)
234 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
235 if(PtInRect(rect, pt)) return FLAG_INCR;
237 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
238 if(PtInRect(rect, pt)) return FLAG_DECR;
240 return 0;
244 /***********************************************************************
245 * UPDOWN_GetThousandSep
246 * Returns the thousand sep. If an error occurs, it returns ','.
248 static WCHAR UPDOWN_GetThousandSep(void)
250 WCHAR sep[2];
252 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
253 sep[0] = ',';
255 return sep[0];
258 /***********************************************************************
259 * UPDOWN_GetBuddyInt
260 * Tries to read the pos from the buddy window and if it succeeds,
261 * it stores it in the control's CurVal
262 * returns:
263 * TRUE - if it read the integer from the buddy successfully
264 * FALSE - if an error occurred
266 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
268 WCHAR txt[20], sep, *src, *dst;
269 int newVal;
271 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
272 return FALSE;
274 /*if the buddy is a list window, we must set curr index */
275 if (UPDOWN_IsBuddyListbox(infoPtr)) {
276 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
277 if(newVal < 0) return FALSE;
278 } else {
279 /* we have a regular window, so will get the text */
280 /* note that a zero-length string is a legitimate value for 'txt',
281 * and ought to result in a successful conversion to '0'. */
282 if (GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt)) < 0)
283 return FALSE;
285 sep = UPDOWN_GetThousandSep();
287 /* now get rid of the separators */
288 for(src = dst = txt; *src; src++)
289 if(*src != sep) *dst++ = *src;
290 *dst = 0;
292 /* try to convert the number and validate it */
293 newVal = strtolW(txt, &src, infoPtr->Base);
294 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
297 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
298 infoPtr->CurVal = newVal;
299 return TRUE;
303 /***********************************************************************
304 * UPDOWN_SetBuddyInt
305 * Tries to set the pos to the buddy window based on current pos
306 * returns:
307 * TRUE - if it set the caption of the buddy successfully
308 * FALSE - if an error occurred
310 static BOOL UPDOWN_SetBuddyInt (const UPDOWN_INFO *infoPtr)
312 static const WCHAR fmt_hex[] = { '0', 'x', '%', '0', '4', 'X', 0 };
313 static const WCHAR fmt_dec_oct[] = { '%', 'd', '\0' };
314 const WCHAR *fmt;
315 WCHAR txt[20], txt_old[20] = { 0 };
316 int len;
318 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
319 return FALSE;
321 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
323 /*if the buddy is a list window, we must set curr index */
324 if (UPDOWN_IsBuddyListbox(infoPtr)) {
325 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
328 /* Regular window, so set caption to the number */
329 fmt = (infoPtr->Base == 16) ? fmt_hex : fmt_dec_oct;
330 len = wsprintfW(txt, fmt, infoPtr->CurVal);
333 /* Do thousands separation if necessary */
334 if ((infoPtr->Base == 10) && !(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
335 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
336 WCHAR sep = UPDOWN_GetThousandSep();
337 int start = len % 3;
339 memcpy(tmp, txt, sizeof(txt));
340 if (start == 0) start = 3;
341 dst += start;
342 src += start;
343 for (len=0; *src; len++) {
344 if (len % 3 == 0) *dst++ = sep;
345 *dst++ = *src++;
347 *dst = 0;
350 /* if nothing changed exit earlier */
351 GetWindowTextW(infoPtr->Buddy, txt_old, sizeof(txt_old)/sizeof(WCHAR));
352 if (lstrcmpiW(txt_old, txt) == 0) return 0;
354 return SetWindowTextW(infoPtr->Buddy, txt);
357 /***********************************************************************
358 * UPDOWN_DrawBuddyBackground
360 * Draw buddy background for visual integration.
362 static BOOL UPDOWN_DrawBuddyBackground (const UPDOWN_INFO *infoPtr, HDC hdc)
364 RECT br;
365 HTHEME buddyTheme = GetWindowTheme (infoPtr->Buddy);
366 if (!buddyTheme) return FALSE;
368 GetClientRect (infoPtr->Buddy, &br);
369 MapWindowPoints (infoPtr->Buddy, infoPtr->Self, (POINT*)&br, 2);
370 /* FIXME: take disabled etc. into account */
371 DrawThemeBackground (buddyTheme, hdc, 0, 0, &br, NULL);
372 return TRUE;
375 /***********************************************************************
376 * UPDOWN_Draw
378 * Draw the arrows. The background need not be erased.
380 static LRESULT UPDOWN_Draw (const UPDOWN_INFO *infoPtr, HDC hdc)
382 BOOL uPressed, uHot, dPressed, dHot;
383 RECT rect;
384 HTHEME theme = GetWindowTheme (infoPtr->Self);
385 int uPart = 0, uState = 0, dPart = 0, dState = 0;
386 BOOL needBuddyBg = FALSE;
388 uPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
389 uHot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
390 dPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
391 dHot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
392 if (theme) {
393 uPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_UPHORZ : SPNP_UP;
394 uState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
395 : (uPressed ? DNS_PRESSED : (uHot ? DNS_HOT : DNS_NORMAL));
396 dPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_DOWNHORZ : SPNP_DOWN;
397 dState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
398 : (dPressed ? DNS_PRESSED : (dHot ? DNS_HOT : DNS_NORMAL));
399 needBuddyBg = IsWindow (infoPtr->Buddy)
400 && (IsThemeBackgroundPartiallyTransparent (theme, uPart, uState)
401 || IsThemeBackgroundPartiallyTransparent (theme, dPart, dState));
404 /* Draw the common border between ourselves and our buddy */
405 if (UPDOWN_HasBuddyBorder(infoPtr) || needBuddyBg) {
406 if (!theme || !UPDOWN_DrawBuddyBackground (infoPtr, hdc)) {
407 GetClientRect(infoPtr->Self, &rect);
408 DrawEdge(hdc, &rect, EDGE_SUNKEN,
409 BF_BOTTOM | BF_TOP |
410 (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
414 /* Draw the incr button */
415 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
416 if (theme) {
417 DrawThemeBackground(theme, hdc, uPart, uState, &rect, NULL);
418 } else {
419 DrawFrameControl(hdc, &rect, DFC_SCROLL,
420 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
421 ((infoPtr->dwStyle & UDS_HOTTRACK) && uHot ? DFCS_HOT : 0) |
422 (uPressed ? DFCS_PUSHED : 0) |
423 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
426 /* Draw the decr button */
427 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
428 if (theme) {
429 DrawThemeBackground(theme, hdc, dPart, dState, &rect, NULL);
430 } else {
431 DrawFrameControl(hdc, &rect, DFC_SCROLL,
432 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
433 ((infoPtr->dwStyle & UDS_HOTTRACK) && dHot ? DFCS_HOT : 0) |
434 (dPressed ? DFCS_PUSHED : 0) |
435 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
438 return 0;
441 /***********************************************************************
442 * UPDOWN_Paint
444 * Asynchronous drawing (must ONLY be used in WM_PAINT).
445 * Calls UPDOWN_Draw.
447 static LRESULT UPDOWN_Paint (const UPDOWN_INFO *infoPtr, HDC hdc)
449 PAINTSTRUCT ps;
450 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
451 hdc = BeginPaint (infoPtr->Self, &ps);
452 UPDOWN_Draw (infoPtr, hdc);
453 EndPaint (infoPtr->Self, &ps);
454 return 0;
457 /***********************************************************************
458 * UPDOWN_KeyPressed
460 * Handle key presses (up & down) when we have to do so
462 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
464 int arrow;
466 if (key == VK_UP) arrow = FLAG_INCR;
467 else if (key == VK_DOWN) arrow = FLAG_DECR;
468 else return 1;
470 UPDOWN_GetBuddyInt (infoPtr);
471 infoPtr->Flags &= ~FLAG_ARROW;
472 infoPtr->Flags |= FLAG_PRESSED | arrow;
473 InvalidateRect (infoPtr->Self, NULL, FALSE);
474 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
475 UPDOWN_DoAction (infoPtr, 1, arrow);
476 return 0;
479 /***********************************************************************
480 * UPDOWN_SetRange
482 * Handle UDM_SETRANGE, UDM_SETRANGE32
484 * FIXME: handle Max == Min properly:
485 * - arrows should be disabled (without WS_DISABLED set),
486 * visually they can't be pressed and don't respond;
487 * - all input messages should still pass in.
489 static LRESULT UPDOWN_SetRange(UPDOWN_INFO *infoPtr, INT Max, INT Min)
491 infoPtr->MaxVal = Max;
492 infoPtr->MinVal = Min;
494 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
495 infoPtr->MinVal, infoPtr->MaxVal, infoPtr->Self);
497 return 0;
500 /***********************************************************************
501 * UPDOWN_MouseWheel
503 * Handle mouse wheel scrolling
505 static LRESULT UPDOWN_MouseWheel(UPDOWN_INFO *infoPtr, WPARAM wParam)
507 int iWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
509 if (wParam & (MK_SHIFT | MK_CONTROL))
510 return 0;
512 if (iWheelDelta != 0)
514 UPDOWN_GetBuddyInt(infoPtr);
515 UPDOWN_DoAction(infoPtr, abs(iWheelDelta), iWheelDelta > 0 ? FLAG_INCR : FLAG_DECR);
518 return 1;
522 /***********************************************************************
523 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
524 * control.
526 static LRESULT CALLBACK
527 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
528 UINT_PTR uId, DWORD_PTR ref_data)
530 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr((HWND)ref_data);
532 TRACE("hwnd=%p, uMsg=%04x, wParam=%08lx, lParam=%08lx\n",
533 hwnd, uMsg, wParam, lParam);
535 switch(uMsg)
537 case WM_KEYDOWN:
538 UPDOWN_KeyPressed(infoPtr, (int)wParam);
539 if ((wParam == VK_UP) || (wParam == VK_DOWN)) return 0;
540 break;
542 case WM_MOUSEWHEEL:
543 UPDOWN_MouseWheel(infoPtr, (int)wParam);
544 break;
546 default:
547 break;
550 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
553 /***********************************************************************
554 * UPDOWN_SetBuddy
556 * Sets bud as a new Buddy.
557 * Then, it should subclass the buddy
558 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
559 * process the UP/DOWN arrow keys.
560 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
561 * the size/pos of the buddy and the control are adjusted accordingly.
563 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
565 RECT budRect; /* new coord for the buddy */
566 int x, width; /* new x position and width for the up-down */
567 WCHAR buddyClass[40];
568 HWND ret;
570 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
572 ret = infoPtr->Buddy;
574 /* there is already a buddy assigned */
575 if (infoPtr->Buddy) RemoveWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc,
576 BUDDY_SUBCLASSID);
577 if (!IsWindow(bud)) bud = NULL;
579 /* Store buddy window handle */
580 infoPtr->Buddy = bud;
582 if(bud) {
583 /* Store buddy window class type */
584 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
585 if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
586 if (lstrcmpiW(buddyClass, WC_EDITW) == 0)
587 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
588 else if (lstrcmpiW(buddyClass, WC_LISTBOXW) == 0)
589 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
592 if (infoPtr->dwStyle & UDS_ARROWKEYS)
593 SetWindowSubclass(bud, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID,
594 (DWORD_PTR)infoPtr->Self);
596 /* Get the rect of the buddy relative to its parent */
597 GetWindowRect(infoPtr->Buddy, &budRect);
598 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
600 /* now do the positioning */
601 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
602 x = budRect.left;
603 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
604 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
605 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
606 x = budRect.right+DEFAULT_XSEP;
607 } else {
608 /* nothing to do */
609 return ret;
612 /* first adjust the buddy to accommodate the up/down */
613 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
614 budRect.right - budRect.left, budRect.bottom - budRect.top,
615 SWP_NOACTIVATE|SWP_NOZORDER);
617 /* now position the up/down */
618 /* Since the UDS_ALIGN* flags were used, */
619 /* we will pick the position and size of the window. */
620 width = DEFAULT_WIDTH;
623 * If the updown has a buddy border, it has to overlap with the buddy
624 * to look as if it is integrated with the buddy control.
625 * We nudge the control or change its size to overlap.
627 if (UPDOWN_HasBuddyBorder(infoPtr)) {
628 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
629 width += DEFAULT_BUDDYBORDER;
630 else
631 x -= DEFAULT_BUDDYBORDER;
634 SetWindowPos(infoPtr->Self, 0, x,
635 budRect.top - DEFAULT_ADDTOP, width,
636 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
637 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
638 } else {
639 RECT rect;
640 GetWindowRect(infoPtr->Self, &rect);
641 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
642 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
643 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
645 return ret;
648 /***********************************************************************
649 * UPDOWN_DoAction
651 * This function increments/decrements the CurVal by the
652 * 'delta' amount according to the 'action' flag which can be a
653 * combination of FLAG_INCR and FLAG_DECR
654 * It notifies the parent as required.
655 * It handles wrapping and non-wrapping correctly.
656 * It is assumed that delta>0
658 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
660 NM_UPDOWN ni;
662 TRACE("%d by %d\n", action, delta);
664 /* check if we can do the modification first */
665 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
666 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
668 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
670 /* We must notify parent now to obtain permission */
671 ni.iPos = infoPtr->CurVal;
672 ni.iDelta = delta;
673 ni.hdr.hwndFrom = infoPtr->Self;
674 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
675 ni.hdr.code = UDN_DELTAPOS;
676 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, ni.hdr.idFrom, (LPARAM)&ni)) {
677 /* Parent said: OK to adjust */
679 /* Now adjust value with (maybe new) delta */
680 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
681 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
683 /* Now take care about our buddy */
684 UPDOWN_SetBuddyInt (infoPtr);
688 /* Also, notify it. This message is sent in any case. */
689 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
690 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
693 /***********************************************************************
694 * UPDOWN_IsEnabled
696 * Returns TRUE if it is enabled as well as its buddy (if any)
697 * FALSE otherwise
699 static BOOL UPDOWN_IsEnabled (const UPDOWN_INFO *infoPtr)
701 if (!IsWindowEnabled(infoPtr->Self))
702 return FALSE;
703 if(infoPtr->Buddy)
704 return IsWindowEnabled(infoPtr->Buddy);
705 return TRUE;
708 /***********************************************************************
709 * UPDOWN_CancelMode
711 * Deletes any timers, releases the mouse and does redraw if necessary.
712 * If the control is not in "capture" mode, it does nothing.
713 * If the control was not in cancel mode, it returns FALSE.
714 * If the control was in cancel mode, it returns TRUE.
716 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
718 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
720 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
721 KillTimer (infoPtr->Self, TIMER_ACCEL);
722 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
724 if (GetCapture() == infoPtr->Self) {
725 NMHDR hdr;
726 hdr.hwndFrom = infoPtr->Self;
727 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
728 hdr.code = NM_RELEASEDCAPTURE;
729 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
730 ReleaseCapture();
733 infoPtr->Flags &= ~FLAG_PRESSED;
734 InvalidateRect (infoPtr->Self, NULL, FALSE);
736 return TRUE;
739 /***********************************************************************
740 * UPDOWN_HandleMouseEvent
742 * Handle a mouse event for the updown.
743 * 'pt' is the location of the mouse event in client or
744 * windows coordinates.
746 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
748 POINT pt = { x, y };
749 RECT rect;
750 int temp, arrow;
751 TRACKMOUSEEVENT tme;
753 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
755 switch(msg)
757 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
759 /* If the buddy is an edit, will set focus to it */
760 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
762 /* Now see which one is the 'active' arrow */
763 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
765 /* Update the flags if we are in/out */
766 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
767 if (arrow)
768 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
769 else
770 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
772 if (infoPtr->Flags & FLAG_ARROW) {
774 /* Update the CurVal if necessary */
775 UPDOWN_GetBuddyInt (infoPtr);
777 /* Set up the correct flags */
778 infoPtr->Flags |= FLAG_PRESSED;
780 /* repaint the control */
781 InvalidateRect (infoPtr->Self, NULL, FALSE);
783 /* process the click */
784 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
785 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
787 /* now capture all mouse messages */
788 SetCapture (infoPtr->Self);
790 /* and startup the first timer */
791 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
793 break;
795 case WM_MOUSEMOVE:
796 /* save the flags to see if any got modified */
797 temp = infoPtr->Flags;
799 /* Now see which one is the 'active' arrow */
800 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
802 /* Update the flags if we are in/out */
803 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
804 if(arrow) {
805 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
806 } else {
807 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
810 /* If state changed, redraw the control */
811 if(temp != infoPtr->Flags)
812 InvalidateRect (infoPtr->Self, NULL, FALSE);
814 /* Set up tracking so the mousein flags can be reset when the
815 * mouse leaves the control */
816 tme.cbSize = sizeof( tme );
817 tme.dwFlags = TME_LEAVE;
818 tme.hwndTrack = infoPtr->Self;
819 TrackMouseEvent (&tme);
821 break;
822 case WM_MOUSELEAVE:
823 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
824 InvalidateRect (infoPtr->Self, NULL, FALSE);
825 break;
827 default:
828 ERR("Impossible case (msg=%x)!\n", msg);
833 /***********************************************************************
834 * UpDownWndProc
836 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
838 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
839 static const WCHAR themeClass[] = {'S','p','i','n',0};
840 HTHEME theme;
842 TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd, message, wParam, lParam);
844 if (!infoPtr && (message != WM_CREATE))
845 return DefWindowProcW (hwnd, message, wParam, lParam);
847 switch(message)
849 case WM_CREATE:
851 CREATESTRUCTW *pcs = (CREATESTRUCTW*)lParam;
853 infoPtr = Alloc (sizeof(UPDOWN_INFO));
854 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
856 /* initialize the info struct */
857 infoPtr->Self = hwnd;
858 infoPtr->Notify = pcs->hwndParent;
859 infoPtr->dwStyle = pcs->style;
860 infoPtr->AccelCount = 0;
861 infoPtr->AccelVect = 0;
862 infoPtr->AccelIndex = -1;
863 infoPtr->CurVal = 0;
864 infoPtr->MinVal = 100;
865 infoPtr->MaxVal = 0;
866 infoPtr->Base = 10; /* Default to base 10 */
867 infoPtr->Buddy = 0; /* No buddy window yet */
868 infoPtr->Flags = (infoPtr->dwStyle & UDS_SETBUDDYINT) ? FLAG_BUDDYINT : 0;
870 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
871 if (!(infoPtr->dwStyle & UDS_HORZ))
872 SetWindowPos (hwnd, NULL, 0, 0, DEFAULT_WIDTH, pcs->cy,
873 SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
875 /* Do we pick the buddy win ourselves? */
876 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
877 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
879 OpenThemeData (hwnd, themeClass);
881 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
883 break;
885 case WM_DESTROY:
886 Free (infoPtr->AccelVect);
888 if (infoPtr->Buddy)
889 RemoveWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc,
890 BUDDY_SUBCLASSID);
891 Free (infoPtr);
892 SetWindowLongPtrW (hwnd, 0, 0);
893 theme = GetWindowTheme (hwnd);
894 CloseThemeData (theme);
895 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
896 break;
898 case WM_ENABLE:
899 if (wParam) {
900 infoPtr->dwStyle &= ~WS_DISABLED;
901 } else {
902 infoPtr->dwStyle |= WS_DISABLED;
903 UPDOWN_CancelMode (infoPtr);
905 InvalidateRect (infoPtr->Self, NULL, FALSE);
906 break;
908 case WM_STYLECHANGED:
909 if (wParam == GWL_STYLE) {
910 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
911 InvalidateRect (infoPtr->Self, NULL, FALSE);
913 break;
915 case WM_THEMECHANGED:
916 theme = GetWindowTheme (hwnd);
917 CloseThemeData (theme);
918 OpenThemeData (hwnd, themeClass);
919 InvalidateRect (hwnd, NULL, FALSE);
920 break;
922 case WM_TIMER:
923 /* is this the auto-press timer? */
924 if(wParam == TIMER_AUTOPRESS) {
925 KillTimer(hwnd, TIMER_AUTOPRESS);
926 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
927 InvalidateRect(infoPtr->Self, NULL, FALSE);
930 /* if initial timer, kill it and start the repeat timer */
931 if(wParam == TIMER_AUTOREPEAT) {
932 int temp;
934 KillTimer(hwnd, TIMER_AUTOREPEAT);
935 /* if no accel info given, used default timer */
936 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
937 infoPtr->AccelIndex = -1;
938 temp = REPEAT_DELAY;
939 } else {
940 infoPtr->AccelIndex = 0; /* otherwise, use it */
941 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
943 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
946 /* now, if the mouse is above us, do the thing...*/
947 if(infoPtr->Flags & FLAG_MOUSEIN) {
948 int temp;
950 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
951 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
953 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
954 KillTimer(hwnd, TIMER_ACCEL);
955 infoPtr->AccelIndex++; /* move to the next accel info */
956 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
957 /* make sure we have at least 1ms intervals */
958 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
961 break;
963 case WM_CANCELMODE:
964 return UPDOWN_CancelMode (infoPtr);
966 case WM_LBUTTONUP:
967 if (GetCapture() != infoPtr->Self) break;
969 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
970 (infoPtr->Flags & FLAG_ARROW) ) {
972 SendMessageW( infoPtr->Notify,
973 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
974 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
975 (LPARAM)hwnd);
976 if (UPDOWN_IsBuddyEdit(infoPtr))
977 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
979 UPDOWN_CancelMode(infoPtr);
980 break;
982 case WM_LBUTTONDOWN:
983 case WM_MOUSEMOVE:
984 case WM_MOUSELEAVE:
985 if(UPDOWN_IsEnabled(infoPtr))
986 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
987 break;
989 case WM_MOUSEWHEEL:
990 UPDOWN_MouseWheel(infoPtr, wParam);
991 break;
993 case WM_KEYDOWN:
994 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
995 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
996 break;
998 case WM_PRINTCLIENT:
999 case WM_PAINT:
1000 return UPDOWN_Paint (infoPtr, (HDC)wParam);
1002 case UDM_GETACCEL:
1003 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
1004 if (wParam && lParam) {
1005 int temp = min(infoPtr->AccelCount, wParam);
1006 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
1007 return temp;
1009 return 0;
1011 case UDM_SETACCEL:
1013 unsigned temp;
1015 TRACE("UDM_SETACCEL\n");
1017 if(infoPtr->AccelVect) {
1018 Free (infoPtr->AccelVect);
1019 infoPtr->AccelCount = 0;
1020 infoPtr->AccelVect = 0;
1022 if(wParam==0) return TRUE;
1023 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
1024 if(infoPtr->AccelVect == 0) return FALSE;
1025 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
1026 infoPtr->AccelCount = wParam;
1028 for (temp = 0; temp < wParam; temp++)
1029 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
1031 return TRUE;
1033 case UDM_GETBASE:
1034 return infoPtr->Base;
1036 case UDM_SETBASE:
1037 TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam, hwnd);
1038 if (wParam==10 || wParam==16) {
1039 WPARAM old_base = infoPtr->Base;
1040 infoPtr->Base = wParam;
1042 if (old_base != infoPtr->Base)
1043 UPDOWN_SetBuddyInt(infoPtr);
1045 return old_base;
1047 break;
1049 case UDM_GETBUDDY:
1050 return (LRESULT)infoPtr->Buddy;
1052 case UDM_SETBUDDY:
1053 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
1055 case UDM_GETPOS:
1057 BOOL ret = UPDOWN_GetBuddyInt (infoPtr);
1058 return MAKELONG(infoPtr->CurVal, ret ? 0 : 1);
1060 case UDM_SETPOS:
1062 int temp = (short)LOWORD(lParam);
1064 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
1065 if(!UPDOWN_InBounds(infoPtr, temp)) {
1066 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
1067 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
1069 wParam = infoPtr->CurVal;
1070 infoPtr->CurVal = temp;
1071 UPDOWN_SetBuddyInt (infoPtr);
1072 return wParam; /* return prev value */
1074 case UDM_GETRANGE:
1075 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
1077 case UDM_SETRANGE:
1078 /* we must have:
1079 UD_MINVAL <= Max <= UD_MAXVAL
1080 UD_MINVAL <= Min <= UD_MAXVAL
1081 |Max-Min| <= UD_MAXVAL */
1082 UPDOWN_SetRange(infoPtr, (short)lParam, (short)HIWORD(lParam));
1083 break;
1085 case UDM_GETRANGE32:
1086 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
1087 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
1088 break;
1090 case UDM_SETRANGE32:
1091 UPDOWN_SetRange(infoPtr, (INT)lParam, (INT)wParam);
1092 break;
1094 case UDM_GETPOS32:
1096 BOOL ret = UPDOWN_GetBuddyInt (infoPtr);
1097 if ((LPBOOL)lParam) *((LPBOOL)lParam) = !ret;
1098 return infoPtr->CurVal;
1100 case UDM_SETPOS32:
1102 int temp;
1104 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
1105 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
1106 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
1108 temp = infoPtr->CurVal; /* save prev value */
1109 infoPtr->CurVal = (int)lParam; /* set the new value */
1110 UPDOWN_SetBuddyInt (infoPtr);
1111 return temp; /* return prev value */
1113 case UDM_GETUNICODEFORMAT:
1114 /* we lie a bit here, we're always using Unicode internally */
1115 return infoPtr->UnicodeFormat;
1117 case UDM_SETUNICODEFORMAT:
1119 /* do we really need to honour this flag? */
1120 int temp = infoPtr->UnicodeFormat;
1121 infoPtr->UnicodeFormat = (BOOL)wParam;
1122 return temp;
1124 default:
1125 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1126 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam);
1127 return DefWindowProcW (hwnd, message, wParam, lParam);
1130 return 0;
1133 /***********************************************************************
1134 * UPDOWN_Register [Internal]
1136 * Registers the updown window class.
1138 void UPDOWN_Register(void)
1140 WNDCLASSW wndClass;
1142 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
1143 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1144 wndClass.lpfnWndProc = UpDownWindowProc;
1145 wndClass.cbClsExtra = 0;
1146 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
1147 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1148 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1149 wndClass.lpszClassName = UPDOWN_CLASSW;
1151 RegisterClassW( &wndClass );
1155 /***********************************************************************
1156 * UPDOWN_Unregister [Internal]
1158 * Unregisters the updown window class.
1160 void UPDOWN_Unregister (void)
1162 UnregisterClassW (UPDOWN_CLASSW, NULL);