push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / dlls / comctl32 / updown.c
blobf6b240a7296227288d88ce08758bcb8430a48260
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 static const WCHAR BUDDY_UPDOWN_HWND[] = { 'b', 'u', 'd', 'd', 'y', 'U', 'p', 'D', 'o', 'w', 'n', 'H', 'W', 'N', 'D', 0 };
104 static const WCHAR BUDDY_SUPERCLASS_WNDPROC[] = { 'b', 'u', 'd', 'd', 'y', 'S', 'u', 'p', 'p', 'e', 'r',
105 'C', 'l', 'a', 's', 's', 'W', 'n', 'd', 'P', 'r', 'o', 'c', 0 };
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 WCHAR fmt[3] = { '%', 'd', '\0' };
313 WCHAR txt[20];
314 int len;
316 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
317 return FALSE;
319 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
321 /*if the buddy is a list window, we must set curr index */
322 if (UPDOWN_IsBuddyListbox(infoPtr)) {
323 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
326 /* Regular window, so set caption to the number */
327 if (infoPtr->Base == 16) fmt[1] = 'X';
328 len = wsprintfW(txt, fmt, infoPtr->CurVal);
331 /* Do thousands separation if necessary */
332 if ((infoPtr->Base == 10) && !(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
333 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
334 WCHAR sep = UPDOWN_GetThousandSep();
335 int start = len % 3;
337 memcpy(tmp, txt, sizeof(txt));
338 if (start == 0) start = 3;
339 dst += start;
340 src += start;
341 for (len=0; *src; len++) {
342 if (len % 3 == 0) *dst++ = sep;
343 *dst++ = *src++;
345 *dst = 0;
348 return SetWindowTextW(infoPtr->Buddy, txt);
351 /***********************************************************************
352 * UPDOWN_DrawBuddyBackground
354 * Draw buddy background for visual integration.
356 static BOOL UPDOWN_DrawBuddyBackground (const UPDOWN_INFO *infoPtr, HDC hdc)
358 RECT br;
359 HTHEME buddyTheme = GetWindowTheme (infoPtr->Buddy);
360 if (!buddyTheme) return FALSE;
362 GetClientRect (infoPtr->Buddy, &br);
363 MapWindowPoints (infoPtr->Buddy, infoPtr->Self, (POINT*)&br, 2);
364 /* FIXME: take disabled etc. into account */
365 DrawThemeBackground (buddyTheme, hdc, 0, 0, &br, NULL);
366 return TRUE;
369 /***********************************************************************
370 * UPDOWN_Draw
372 * Draw the arrows. The background need not be erased.
374 static LRESULT UPDOWN_Draw (const UPDOWN_INFO *infoPtr, HDC hdc)
376 BOOL uPressed, uHot, dPressed, dHot;
377 RECT rect;
378 HTHEME theme = GetWindowTheme (infoPtr->Self);
379 int uPart = 0, uState = 0, dPart = 0, dState = 0;
380 BOOL needBuddyBg = FALSE;
382 uPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
383 uHot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
384 dPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
385 dHot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
386 if (theme) {
387 uPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_UPHORZ : SPNP_UP;
388 uState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
389 : (uPressed ? DNS_PRESSED : (uHot ? DNS_HOT : DNS_NORMAL));
390 dPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_DOWNHORZ : SPNP_DOWN;
391 dState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
392 : (dPressed ? DNS_PRESSED : (dHot ? DNS_HOT : DNS_NORMAL));
393 needBuddyBg = IsWindow (infoPtr->Buddy)
394 && (IsThemeBackgroundPartiallyTransparent (theme, uPart, uState)
395 || IsThemeBackgroundPartiallyTransparent (theme, dPart, dState));
398 /* Draw the common border between ourselves and our buddy */
399 if (UPDOWN_HasBuddyBorder(infoPtr) || needBuddyBg) {
400 if (!theme || !UPDOWN_DrawBuddyBackground (infoPtr, hdc)) {
401 GetClientRect(infoPtr->Self, &rect);
402 DrawEdge(hdc, &rect, EDGE_SUNKEN,
403 BF_BOTTOM | BF_TOP |
404 (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
408 /* Draw the incr button */
409 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
410 if (theme) {
411 DrawThemeBackground(theme, hdc, uPart, uState, &rect, NULL);
412 } else {
413 DrawFrameControl(hdc, &rect, DFC_SCROLL,
414 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
415 ((infoPtr->dwStyle & UDS_HOTTRACK) && uHot ? DFCS_HOT : 0) |
416 (uPressed ? DFCS_PUSHED : 0) |
417 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
420 /* Draw the decr button */
421 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
422 if (theme) {
423 DrawThemeBackground(theme, hdc, dPart, dState, &rect, NULL);
424 } else {
425 DrawFrameControl(hdc, &rect, DFC_SCROLL,
426 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
427 ((infoPtr->dwStyle & UDS_HOTTRACK) && dHot ? DFCS_HOT : 0) |
428 (dPressed ? DFCS_PUSHED : 0) |
429 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
432 return 0;
435 /***********************************************************************
436 * UPDOWN_Paint
438 * Asynchronous drawing (must ONLY be used in WM_PAINT).
439 * Calls UPDOWN_Draw.
441 static LRESULT UPDOWN_Paint (const UPDOWN_INFO *infoPtr, HDC hdc)
443 PAINTSTRUCT ps;
444 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
445 hdc = BeginPaint (infoPtr->Self, &ps);
446 UPDOWN_Draw (infoPtr, hdc);
447 EndPaint (infoPtr->Self, &ps);
448 return 0;
451 /***********************************************************************
452 * UPDOWN_KeyPressed
454 * Handle key presses (up & down) when we have to do so
456 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
458 int arrow;
460 if (key == VK_UP) arrow = FLAG_INCR;
461 else if (key == VK_DOWN) arrow = FLAG_DECR;
462 else return 1;
464 UPDOWN_GetBuddyInt (infoPtr);
465 infoPtr->Flags &= ~FLAG_ARROW;
466 infoPtr->Flags |= FLAG_PRESSED | arrow;
467 InvalidateRect (infoPtr->Self, NULL, FALSE);
468 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
469 UPDOWN_DoAction (infoPtr, 1, arrow);
470 return 0;
473 /***********************************************************************
474 * UPDOWN_SetRange
476 * Handle UDM_SETRANGE, UDM_SETRANGE32
478 * FIXME: handle Max == Min properly:
479 * - arrows should be disabled (without WS_DISABLED set),
480 * visually they can't be pressed and don't respond;
481 * - all input messages should still pass in.
483 static LRESULT UPDOWN_SetRange(UPDOWN_INFO *infoPtr, INT Max, INT Min)
485 infoPtr->MaxVal = Max;
486 infoPtr->MinVal = Min;
488 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
489 infoPtr->MinVal, infoPtr->MaxVal, infoPtr->Self);
491 return 0;
494 /***********************************************************************
495 * UPDOWN_MouseWheel
497 * Handle mouse wheel scrolling
499 static LRESULT UPDOWN_MouseWheel(UPDOWN_INFO *infoPtr, WPARAM wParam)
501 int iWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
503 if (wParam & (MK_SHIFT | MK_CONTROL))
504 return 0;
506 if (iWheelDelta != 0)
508 UPDOWN_GetBuddyInt(infoPtr);
509 UPDOWN_DoAction(infoPtr, abs(iWheelDelta), iWheelDelta > 0 ? FLAG_INCR : FLAG_DECR);
512 return 1;
516 /***********************************************************************
517 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
518 * control.
520 static LRESULT CALLBACK
521 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
523 WNDPROC superClassWndProc = (WNDPROC)GetPropW(hwnd, BUDDY_SUPERCLASS_WNDPROC);
524 HWND upDownHwnd = GetPropW(hwnd, BUDDY_UPDOWN_HWND);
525 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr(upDownHwnd);
527 TRACE("hwnd=%p, wndProc=%p, uMsg=%04x, wParam=%08lx, lParam=%08lx\n",
528 hwnd, superClassWndProc, uMsg, wParam, lParam);
530 switch(uMsg)
532 case WM_KEYDOWN:
533 UPDOWN_KeyPressed(infoPtr, (int)wParam);
534 if ((wParam == VK_UP) || (wParam == VK_DOWN)) return 0;
535 break;
537 case WM_MOUSEWHEEL:
538 UPDOWN_MouseWheel(infoPtr, (int)wParam);
539 break;
541 default:
542 break;
545 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
548 /***********************************************************************
549 * UPDOWN_SetBuddy
551 * Sets bud as a new Buddy.
552 * Then, it should subclass the buddy
553 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
554 * process the UP/DOWN arrow keys.
555 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
556 * the size/pos of the buddy and the control are adjusted accordingly.
558 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
560 RECT budRect; /* new coord for the buddy */
561 int x, width; /* new x position and width for the up-down */
562 WNDPROC baseWndProc;
563 WCHAR buddyClass[40];
564 HWND ret;
566 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
568 ret = infoPtr->Buddy;
570 /* there is already a body assigned */
571 if (infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
573 if(!IsWindow(bud))
574 bud = 0;
576 /* Store buddy window handle */
577 infoPtr->Buddy = bud;
579 if(bud) {
581 /* keep upDown ctrl hwnd in a buddy property */
582 SetPropW( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
584 /* Store buddy window class type */
585 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
586 if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
587 if (lstrcmpiW(buddyClass, WC_EDITW) == 0)
588 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
589 else if (lstrcmpiW(buddyClass, WC_LISTBOXW) == 0)
590 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
593 if (infoPtr->dwStyle & UDS_ARROWKEYS) {
594 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
595 when we reset the upDown ctrl buddy to another buddy because it is not
596 good to break the window proc chain. */
597 if (!GetPropW(bud, BUDDY_SUPERCLASS_WNDPROC)) {
598 baseWndProc = (WNDPROC)SetWindowLongPtrW(bud, GWLP_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
599 SetPropW(bud, BUDDY_SUPERCLASS_WNDPROC, baseWndProc);
603 /* Get the rect of the buddy relative to its parent */
604 GetWindowRect(infoPtr->Buddy, &budRect);
605 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
607 /* now do the positioning */
608 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
609 x = budRect.left;
610 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
611 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
612 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
613 x = budRect.right+DEFAULT_XSEP;
614 } else {
615 /* nothing to do */
616 return ret;
619 /* first adjust the buddy to accommodate the up/down */
620 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
621 budRect.right - budRect.left, budRect.bottom - budRect.top,
622 SWP_NOACTIVATE|SWP_NOZORDER);
624 /* now position the up/down */
625 /* Since the UDS_ALIGN* flags were used, */
626 /* we will pick the position and size of the window. */
627 width = DEFAULT_WIDTH;
630 * If the updown has a buddy border, it has to overlap with the buddy
631 * to look as if it is integrated with the buddy control.
632 * We nudge the control or change its size to overlap.
634 if (UPDOWN_HasBuddyBorder(infoPtr)) {
635 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
636 width += DEFAULT_BUDDYBORDER;
637 else
638 x -= DEFAULT_BUDDYBORDER;
641 SetWindowPos(infoPtr->Self, 0, x,
642 budRect.top - DEFAULT_ADDTOP, width,
643 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
644 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
645 } else {
646 RECT rect;
647 GetWindowRect(infoPtr->Self, &rect);
648 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
649 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
650 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
652 return ret;
655 /***********************************************************************
656 * UPDOWN_DoAction
658 * This function increments/decrements the CurVal by the
659 * 'delta' amount according to the 'action' flag which can be a
660 * combination of FLAG_INCR and FLAG_DECR
661 * It notifies the parent as required.
662 * It handles wrapping and non-wrapping correctly.
663 * It is assumed that delta>0
665 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
667 NM_UPDOWN ni;
669 TRACE("%d by %d\n", action, delta);
671 /* check if we can do the modification first */
672 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
673 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
675 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
677 /* We must notify parent now to obtain permission */
678 ni.iPos = infoPtr->CurVal;
679 ni.iDelta = delta;
680 ni.hdr.hwndFrom = infoPtr->Self;
681 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
682 ni.hdr.code = UDN_DELTAPOS;
683 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, ni.hdr.idFrom, (LPARAM)&ni)) {
684 /* Parent said: OK to adjust */
686 /* Now adjust value with (maybe new) delta */
687 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
688 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
690 /* Now take care about our buddy */
691 UPDOWN_SetBuddyInt (infoPtr);
695 /* Also, notify it. This message is sent in any case. */
696 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
697 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
700 /***********************************************************************
701 * UPDOWN_IsEnabled
703 * Returns TRUE if it is enabled as well as its buddy (if any)
704 * FALSE otherwise
706 static BOOL UPDOWN_IsEnabled (const UPDOWN_INFO *infoPtr)
708 if (!IsWindowEnabled(infoPtr->Self))
709 return FALSE;
710 if(infoPtr->Buddy)
711 return IsWindowEnabled(infoPtr->Buddy);
712 return TRUE;
715 /***********************************************************************
716 * UPDOWN_CancelMode
718 * Deletes any timers, releases the mouse and does redraw if necessary.
719 * If the control is not in "capture" mode, it does nothing.
720 * If the control was not in cancel mode, it returns FALSE.
721 * If the control was in cancel mode, it returns TRUE.
723 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
725 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
727 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
728 KillTimer (infoPtr->Self, TIMER_ACCEL);
729 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
731 if (GetCapture() == infoPtr->Self) {
732 NMHDR hdr;
733 hdr.hwndFrom = infoPtr->Self;
734 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
735 hdr.code = NM_RELEASEDCAPTURE;
736 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
737 ReleaseCapture();
740 infoPtr->Flags &= ~FLAG_PRESSED;
741 InvalidateRect (infoPtr->Self, NULL, FALSE);
743 return TRUE;
746 /***********************************************************************
747 * UPDOWN_HandleMouseEvent
749 * Handle a mouse event for the updown.
750 * 'pt' is the location of the mouse event in client or
751 * windows coordinates.
753 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
755 POINT pt = { x, y };
756 RECT rect;
757 int temp, arrow;
758 TRACKMOUSEEVENT tme;
760 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
762 switch(msg)
764 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
766 /* If the buddy is an edit, will set focus to it */
767 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
769 /* Now see which one is the 'active' arrow */
770 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
772 /* Update the flags if we are in/out */
773 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
774 if (arrow)
775 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
776 else
777 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
779 if (infoPtr->Flags & FLAG_ARROW) {
781 /* Update the CurVal if necessary */
782 UPDOWN_GetBuddyInt (infoPtr);
784 /* Set up the correct flags */
785 infoPtr->Flags |= FLAG_PRESSED;
787 /* repaint the control */
788 InvalidateRect (infoPtr->Self, NULL, FALSE);
790 /* process the click */
791 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
792 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
794 /* now capture all mouse messages */
795 SetCapture (infoPtr->Self);
797 /* and startup the first timer */
798 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
800 break;
802 case WM_MOUSEMOVE:
803 /* save the flags to see if any got modified */
804 temp = infoPtr->Flags;
806 /* Now see which one is the 'active' arrow */
807 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
809 /* Update the flags if we are in/out */
810 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
811 if(arrow) {
812 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
813 } else {
814 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
817 /* If state changed, redraw the control */
818 if(temp != infoPtr->Flags)
819 InvalidateRect (infoPtr->Self, NULL, FALSE);
821 /* Set up tracking so the mousein flags can be reset when the
822 * mouse leaves the control */
823 tme.cbSize = sizeof( tme );
824 tme.dwFlags = TME_LEAVE;
825 tme.hwndTrack = infoPtr->Self;
826 TrackMouseEvent (&tme);
828 break;
829 case WM_MOUSELEAVE:
830 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
831 InvalidateRect (infoPtr->Self, NULL, FALSE);
832 break;
834 default:
835 ERR("Impossible case (msg=%x)!\n", msg);
840 /***********************************************************************
841 * UpDownWndProc
843 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
845 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
846 static const WCHAR themeClass[] = {'S','p','i','n',0};
847 HTHEME theme;
849 TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd, message, wParam, lParam);
851 if (!infoPtr && (message != WM_CREATE))
852 return DefWindowProcW (hwnd, message, wParam, lParam);
854 switch(message)
856 case WM_CREATE:
858 CREATESTRUCTW *pcs = (CREATESTRUCTW*)lParam;
860 infoPtr = Alloc (sizeof(UPDOWN_INFO));
861 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
863 /* initialize the info struct */
864 infoPtr->Self = hwnd;
865 infoPtr->Notify = pcs->hwndParent;
866 infoPtr->dwStyle = pcs->style;
867 infoPtr->AccelCount = 0;
868 infoPtr->AccelVect = 0;
869 infoPtr->AccelIndex = -1;
870 infoPtr->CurVal = 0;
871 infoPtr->MinVal = 100;
872 infoPtr->MaxVal = 0;
873 infoPtr->Base = 10; /* Default to base 10 */
874 infoPtr->Buddy = 0; /* No buddy window yet */
875 infoPtr->Flags = (infoPtr->dwStyle & UDS_SETBUDDYINT) ? FLAG_BUDDYINT : 0;
877 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
878 if (!(infoPtr->dwStyle & UDS_HORZ))
879 SetWindowPos (hwnd, NULL, 0, 0, DEFAULT_WIDTH, pcs->cy,
880 SWP_NOOWNERZORDER | SWP_NOMOVE);
882 /* Do we pick the buddy win ourselves? */
883 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
884 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
886 OpenThemeData (hwnd, themeClass);
888 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
890 break;
892 case WM_DESTROY:
893 Free (infoPtr->AccelVect);
895 if(infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
897 Free (infoPtr);
898 SetWindowLongPtrW (hwnd, 0, 0);
899 theme = GetWindowTheme (hwnd);
900 CloseThemeData (theme);
901 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
902 break;
904 case WM_ENABLE:
905 if (wParam) {
906 infoPtr->dwStyle &= ~WS_DISABLED;
907 } else {
908 infoPtr->dwStyle |= WS_DISABLED;
909 UPDOWN_CancelMode (infoPtr);
911 InvalidateRect (infoPtr->Self, NULL, FALSE);
912 break;
914 case WM_STYLECHANGED:
915 if (wParam == GWL_STYLE) {
916 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
917 InvalidateRect (infoPtr->Self, NULL, FALSE);
919 break;
921 case WM_THEMECHANGED:
922 theme = GetWindowTheme (hwnd);
923 CloseThemeData (theme);
924 OpenThemeData (hwnd, themeClass);
925 InvalidateRect (hwnd, NULL, FALSE);
926 break;
928 case WM_TIMER:
929 /* is this the auto-press timer? */
930 if(wParam == TIMER_AUTOPRESS) {
931 KillTimer(hwnd, TIMER_AUTOPRESS);
932 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
933 InvalidateRect(infoPtr->Self, NULL, FALSE);
936 /* if initial timer, kill it and start the repeat timer */
937 if(wParam == TIMER_AUTOREPEAT) {
938 int temp;
940 KillTimer(hwnd, TIMER_AUTOREPEAT);
941 /* if no accel info given, used default timer */
942 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
943 infoPtr->AccelIndex = -1;
944 temp = REPEAT_DELAY;
945 } else {
946 infoPtr->AccelIndex = 0; /* otherwise, use it */
947 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
949 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
952 /* now, if the mouse is above us, do the thing...*/
953 if(infoPtr->Flags & FLAG_MOUSEIN) {
954 int temp;
956 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
957 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
959 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
960 KillTimer(hwnd, TIMER_ACCEL);
961 infoPtr->AccelIndex++; /* move to the next accel info */
962 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
963 /* make sure we have at least 1ms intervals */
964 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
967 break;
969 case WM_CANCELMODE:
970 return UPDOWN_CancelMode (infoPtr);
972 case WM_LBUTTONUP:
973 if (GetCapture() != infoPtr->Self) break;
975 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
976 (infoPtr->Flags & FLAG_ARROW) ) {
978 SendMessageW( infoPtr->Notify,
979 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
980 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
981 (LPARAM)hwnd);
982 if (UPDOWN_IsBuddyEdit(infoPtr))
983 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
985 UPDOWN_CancelMode(infoPtr);
986 break;
988 case WM_LBUTTONDOWN:
989 case WM_MOUSEMOVE:
990 case WM_MOUSELEAVE:
991 if(UPDOWN_IsEnabled(infoPtr))
992 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
993 break;
995 case WM_MOUSEWHEEL:
996 UPDOWN_MouseWheel(infoPtr, wParam);
997 break;
999 case WM_KEYDOWN:
1000 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
1001 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
1002 break;
1004 case WM_PRINTCLIENT:
1005 case WM_PAINT:
1006 return UPDOWN_Paint (infoPtr, (HDC)wParam);
1008 case UDM_GETACCEL:
1009 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
1010 if (wParam && lParam) {
1011 int temp = min(infoPtr->AccelCount, wParam);
1012 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
1013 return temp;
1015 return 0;
1017 case UDM_SETACCEL:
1019 unsigned temp;
1021 TRACE("UDM_SETACCEL\n");
1023 if(infoPtr->AccelVect) {
1024 Free (infoPtr->AccelVect);
1025 infoPtr->AccelCount = 0;
1026 infoPtr->AccelVect = 0;
1028 if(wParam==0) return TRUE;
1029 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
1030 if(infoPtr->AccelVect == 0) return FALSE;
1031 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
1032 infoPtr->AccelCount = wParam;
1034 for (temp = 0; temp < wParam; temp++)
1035 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
1037 return TRUE;
1039 case UDM_GETBASE:
1040 return infoPtr->Base;
1042 case UDM_SETBASE:
1043 TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam, hwnd);
1044 if (wParam==10 || wParam==16) {
1045 WPARAM old_base = infoPtr->Base;
1046 infoPtr->Base = wParam;
1048 if (old_base != infoPtr->Base)
1049 UPDOWN_SetBuddyInt(infoPtr);
1051 return old_base;
1053 break;
1055 case UDM_GETBUDDY:
1056 return (LRESULT)infoPtr->Buddy;
1058 case UDM_SETBUDDY:
1059 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
1061 case UDM_GETPOS:
1063 BOOL ret = UPDOWN_GetBuddyInt (infoPtr);
1064 return MAKELONG(infoPtr->CurVal, ret ? 0 : 1);
1066 case UDM_SETPOS:
1068 int temp = (short)LOWORD(lParam);
1070 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
1071 if(!UPDOWN_InBounds(infoPtr, temp)) {
1072 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
1073 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
1075 wParam = infoPtr->CurVal;
1076 infoPtr->CurVal = temp;
1077 UPDOWN_SetBuddyInt (infoPtr);
1078 return wParam; /* return prev value */
1080 case UDM_GETRANGE:
1081 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
1083 case UDM_SETRANGE:
1084 /* we must have:
1085 UD_MINVAL <= Max <= UD_MAXVAL
1086 UD_MINVAL <= Min <= UD_MAXVAL
1087 |Max-Min| <= UD_MAXVAL */
1088 UPDOWN_SetRange(infoPtr, (short)lParam, (short)HIWORD(lParam));
1089 break;
1091 case UDM_GETRANGE32:
1092 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
1093 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
1094 break;
1096 case UDM_SETRANGE32:
1097 UPDOWN_SetRange(infoPtr, (INT)lParam, (INT)wParam);
1098 break;
1100 case UDM_GETPOS32:
1102 BOOL ret = UPDOWN_GetBuddyInt (infoPtr);
1103 if ((LPBOOL)lParam) *((LPBOOL)lParam) = !ret;
1104 return infoPtr->CurVal;
1106 case UDM_SETPOS32:
1108 int temp;
1110 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
1111 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
1112 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
1114 temp = infoPtr->CurVal; /* save prev value */
1115 infoPtr->CurVal = (int)lParam; /* set the new value */
1116 UPDOWN_SetBuddyInt (infoPtr);
1117 return temp; /* return prev value */
1119 case UDM_GETUNICODEFORMAT:
1120 /* we lie a bit here, we're always using Unicode internally */
1121 return infoPtr->UnicodeFormat;
1123 case UDM_SETUNICODEFORMAT:
1125 /* do we really need to honour this flag? */
1126 int temp = infoPtr->UnicodeFormat;
1127 infoPtr->UnicodeFormat = (BOOL)wParam;
1128 return temp;
1130 default:
1131 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1132 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam);
1133 return DefWindowProcW (hwnd, message, wParam, lParam);
1136 return 0;
1139 /***********************************************************************
1140 * UPDOWN_Register [Internal]
1142 * Registers the updown window class.
1144 void UPDOWN_Register(void)
1146 WNDCLASSW wndClass;
1148 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
1149 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1150 wndClass.lpfnWndProc = UpDownWindowProc;
1151 wndClass.cbClsExtra = 0;
1152 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
1153 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1154 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1155 wndClass.lpszClassName = UPDOWN_CLASSW;
1157 RegisterClassW( &wndClass );
1161 /***********************************************************************
1162 * UPDOWN_Unregister [Internal]
1164 * Unregisters the updown window class.
1166 void UPDOWN_Unregister (void)
1168 UnregisterClassW (UPDOWN_CLASSW, NULL);