regedit: Mark WCHAR szFrameClass static.
[wine.git] / dlls / comctl32 / updown.c
blobe07f9e7a9f27c4ad1b463654ca0a47282568a22a
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
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winnls.h"
32 #include "commctrl.h"
33 #include "comctl32.h"
34 #include "uxtheme.h"
35 #include "vssym32.h"
36 #include "wine/heap.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(updown);
41 typedef struct
43 HWND Self; /* Handle to this up-down control */
44 HWND Notify; /* Handle to the parent window */
45 DWORD dwStyle; /* The GWL_STYLE for this window */
46 UINT AccelCount; /* Number of elements in AccelVect */
47 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
48 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
49 INT Base; /* Base to display nr in the buddy window */
50 INT CurVal; /* Current up-down value */
51 INT MinVal; /* Minimum up-down value */
52 INT MaxVal; /* Maximum up-down value */
53 HWND Buddy; /* Handle to the buddy window */
54 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
55 INT Flags; /* Internal Flags FLAG_* */
56 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
57 } UPDOWN_INFO;
59 /* Control configuration constants */
61 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
62 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
63 #define REPEAT_DELAY 50 /* delay between auto-increments */
65 #define DEFAULT_WIDTH 16 /* default width of the ctrl */
66 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
67 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
68 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
69 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
70 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
71 #define DEFAULT_BUDDYBORDER_THEMED 1 /* buddy border when theming is enabled */
72 #define DEFAULT_BUDDYSPACER_THEMED 0 /* buddy spacer when theming is enabled */
74 /* Work constants */
76 #define FLAG_INCR 0x01
77 #define FLAG_DECR 0x02
78 #define FLAG_MOUSEIN 0x04
79 #define FLAG_PRESSED 0x08
80 #define FLAG_BUDDYINT 0x10 /* UDS_SETBUDDYINT was set on creation */
81 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
83 #define BUDDY_TYPE_UNKNOWN 0
84 #define BUDDY_TYPE_LISTBOX 1
85 #define BUDDY_TYPE_EDIT 2
87 #define TIMER_AUTOREPEAT 1
88 #define TIMER_ACCEL 2
89 #define TIMER_AUTOPRESS 3
91 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
93 /* id used for SetWindowSubclass */
94 #define BUDDY_SUBCLASSID 1
96 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
98 /***********************************************************************
99 * UPDOWN_IsBuddyEdit
100 * Tests if our buddy is an edit control.
102 static inline BOOL UPDOWN_IsBuddyEdit(const UPDOWN_INFO *infoPtr)
104 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
107 /***********************************************************************
108 * UPDOWN_IsBuddyListbox
109 * Tests if our buddy is a listbox control.
111 static inline BOOL UPDOWN_IsBuddyListbox(const UPDOWN_INFO *infoPtr)
113 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
116 /***********************************************************************
117 * UPDOWN_InBounds
118 * Tests if a given value 'val' is between the Min&Max limits
120 static BOOL UPDOWN_InBounds(const UPDOWN_INFO *infoPtr, int val)
122 if(infoPtr->MaxVal > infoPtr->MinVal)
123 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
124 else
125 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
128 /***********************************************************************
129 * UPDOWN_OffsetVal
130 * Change the current value by delta.
131 * It returns TRUE is the value was changed successfully, or FALSE
132 * if the value was not changed, as it would go out of bounds.
134 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
136 /* check if we can do the modification first */
137 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
138 if (infoPtr->dwStyle & UDS_WRAP) {
139 delta += (delta < 0 ? -1 : 1) *
140 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
141 (infoPtr->MinVal - infoPtr->MaxVal) +
142 (delta < 0 ? 1 : -1);
143 } else if ((infoPtr->MaxVal > infoPtr->MinVal && infoPtr->CurVal+delta > infoPtr->MaxVal)
144 || (infoPtr->MaxVal < infoPtr->MinVal && infoPtr->CurVal+delta < infoPtr->MaxVal)) {
145 delta = infoPtr->MaxVal - infoPtr->CurVal;
146 } else {
147 delta = infoPtr->MinVal - infoPtr->CurVal;
151 infoPtr->CurVal += delta;
152 return delta != 0;
155 /***********************************************************************
156 * UPDOWN_HasBuddyBorder
158 * When we have a buddy set and that we are aligned on our buddy, we
159 * want to draw a sunken edge to make like we are part of that control.
161 static BOOL UPDOWN_HasBuddyBorder(const UPDOWN_INFO *infoPtr)
163 return ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
164 UPDOWN_IsBuddyEdit(infoPtr) );
167 /***********************************************************************
168 * UPDOWN_GetArrowRect
169 * wndPtr - pointer to the up-down wnd
170 * rect - will hold the rectangle
171 * arrow - FLAG_INCR to get the "increment" rect (up or right)
172 * FLAG_DECR to get the "decrement" rect (down or left)
174 static void UPDOWN_GetArrowRect (const UPDOWN_INFO* infoPtr, RECT *rect, unsigned int arrow)
176 HTHEME theme = GetWindowTheme (infoPtr->Self);
177 const int border = theme ? DEFAULT_BUDDYBORDER_THEMED : DEFAULT_BUDDYBORDER;
178 const int spacer = theme ? DEFAULT_BUDDYSPACER_THEMED : DEFAULT_BUDDYSPACER;
179 int size;
181 assert(arrow && (arrow & (FLAG_INCR | FLAG_DECR)) != (FLAG_INCR | FLAG_DECR));
183 GetClientRect (infoPtr->Self, rect);
186 * Make sure we calculate the rectangle to fit even if we draw the
187 * border.
189 if (UPDOWN_HasBuddyBorder(infoPtr)) {
190 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
191 rect->left += border;
192 else
193 rect->right -= border;
195 InflateRect(rect, 0, -border);
198 /* now figure out if we need a space away from the buddy */
199 if (IsWindow(infoPtr->Buddy) ) {
200 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= spacer;
201 else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) rect->left += spacer;
205 * We're calculating the midpoint to figure-out where the
206 * separation between the buttons will lay.
208 if (infoPtr->dwStyle & UDS_HORZ) {
209 size = (rect->right - rect->left) / 2;
210 if (arrow & FLAG_INCR)
211 rect->left = rect->right - size;
212 else if (arrow & FLAG_DECR)
213 rect->right = rect->left + size;
214 } else {
215 size = (rect->bottom - rect->top) / 2;
216 if (arrow & FLAG_INCR)
217 rect->bottom = rect->top + size;
218 else if (arrow & FLAG_DECR)
219 rect->top = rect->bottom - size;
223 /***********************************************************************
224 * UPDOWN_GetArrowFromPoint
225 * Returns the rectagle (for the up or down arrow) that contains pt.
226 * If it returns the up rect, it returns FLAG_INCR.
227 * If it returns the down rect, it returns FLAG_DECR.
229 static INT UPDOWN_GetArrowFromPoint (const UPDOWN_INFO *infoPtr, RECT *rect, POINT pt)
231 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
232 if(PtInRect(rect, pt)) return FLAG_INCR;
234 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
235 if(PtInRect(rect, pt)) return FLAG_DECR;
237 return 0;
241 /***********************************************************************
242 * UPDOWN_GetThousandSep
243 * Returns the thousand sep. If an error occurs, it returns ','.
245 static WCHAR UPDOWN_GetThousandSep(void)
247 WCHAR sep[2];
249 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
250 sep[0] = ',';
252 return sep[0];
255 /***********************************************************************
256 * UPDOWN_GetBuddyInt
257 * Tries to read the pos from the buddy window and if it succeeds,
258 * it stores it in the control's CurVal
259 * returns:
260 * TRUE - if it read the integer from the buddy successfully
261 * FALSE - if an error occurred
263 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
265 WCHAR txt[20], sep, *src, *dst;
266 int newVal;
268 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
269 return FALSE;
271 /*if the buddy is a list window, we must set curr index */
272 if (UPDOWN_IsBuddyListbox(infoPtr)) {
273 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
274 if(newVal < 0) return FALSE;
275 } else {
276 /* we have a regular window, so will get the text */
277 /* note that a zero-length string is a legitimate value for 'txt',
278 * and ought to result in a successful conversion to '0'. */
279 if (GetWindowTextW(infoPtr->Buddy, txt, ARRAY_SIZE(txt)) < 0)
280 return FALSE;
282 sep = UPDOWN_GetThousandSep();
284 /* now get rid of the separators */
285 for(src = dst = txt; *src; src++)
286 if(*src != sep) *dst++ = *src;
287 *dst = 0;
289 /* try to convert the number and validate it */
290 newVal = wcstol(txt, &src, infoPtr->Base);
291 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
294 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
295 infoPtr->CurVal = newVal;
296 return TRUE;
300 /***********************************************************************
301 * UPDOWN_SetBuddyInt
302 * Tries to set the pos to the buddy window based on current pos
303 * returns:
304 * TRUE - if it set the caption of the buddy successfully
305 * FALSE - if an error occurred
307 static BOOL UPDOWN_SetBuddyInt (const UPDOWN_INFO *infoPtr)
309 const WCHAR *fmt;
310 WCHAR txt[20], txt_old[20] = { 0 };
311 int len;
313 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
314 return FALSE;
316 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
318 /*if the buddy is a list window, we must set curr index */
319 if (UPDOWN_IsBuddyListbox(infoPtr)) {
320 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
323 /* Regular window, so set caption to the number */
324 fmt = (infoPtr->Base == 16) ? L"0x%04X" : L"%d";
325 len = wsprintfW(txt, fmt, infoPtr->CurVal);
328 /* Do thousands separation if necessary */
329 if ((infoPtr->Base == 10) && !(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
330 WCHAR tmp[ARRAY_SIZE(txt)], *src = tmp, *dst = txt;
331 WCHAR sep = UPDOWN_GetThousandSep();
332 int start = len % 3;
334 memcpy(tmp, txt, sizeof(txt));
335 if (start == 0) start = 3;
336 dst += start;
337 src += start;
338 for (len=0; *src; len++) {
339 if (len % 3 == 0) *dst++ = sep;
340 *dst++ = *src++;
342 *dst = 0;
345 /* if nothing changed exit earlier */
346 GetWindowTextW(infoPtr->Buddy, txt_old, ARRAY_SIZE(txt_old));
347 if (lstrcmpiW(txt_old, txt) == 0) return FALSE;
349 return SetWindowTextW(infoPtr->Buddy, txt);
352 /***********************************************************************
353 * UPDOWN_DrawBuddyBackground
355 * Draw buddy background for visual integration.
357 static BOOL UPDOWN_DrawBuddyBackground (const UPDOWN_INFO *infoPtr, HDC hdc)
359 RECT br, r;
360 HTHEME buddyTheme = GetWindowTheme (infoPtr->Buddy);
361 if (!buddyTheme) return FALSE;
363 GetWindowRect (infoPtr->Buddy, &br);
364 MapWindowPoints (NULL, infoPtr->Self, (POINT*)&br, 2);
365 GetClientRect (infoPtr->Self, &r);
367 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
368 br.left = r.left;
369 else if (infoPtr->dwStyle & UDS_ALIGNRIGHT)
370 br.right = r.right;
371 /* FIXME: take disabled etc. into account */
372 DrawThemeBackground (buddyTheme, hdc, 0, 0, &br, NULL);
373 return TRUE;
376 /***********************************************************************
377 * UPDOWN_Draw
379 * Draw the arrows. The background need not be erased.
381 static LRESULT UPDOWN_Draw (const UPDOWN_INFO *infoPtr, HDC hdc)
383 BOOL uPressed, uHot, dPressed, dHot;
384 RECT rect;
385 HTHEME theme = GetWindowTheme (infoPtr->Self);
386 int uPart = 0, uState = 0, dPart = 0, dState = 0;
387 BOOL needBuddyBg = FALSE;
389 uPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
390 uHot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
391 dPressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
392 dHot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
393 if (theme) {
394 uPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_UPHORZ : SPNP_UP;
395 uState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
396 : (uPressed ? DNS_PRESSED : (uHot ? DNS_HOT : DNS_NORMAL));
397 dPart = (infoPtr->dwStyle & UDS_HORZ) ? SPNP_DOWNHORZ : SPNP_DOWN;
398 dState = (infoPtr->dwStyle & WS_DISABLED) ? DNS_DISABLED
399 : (dPressed ? DNS_PRESSED : (dHot ? DNS_HOT : DNS_NORMAL));
400 needBuddyBg = IsWindow (infoPtr->Buddy)
401 && (IsThemeBackgroundPartiallyTransparent (theme, uPart, uState)
402 || IsThemeBackgroundPartiallyTransparent (theme, dPart, dState));
405 /* Draw the common border between ourselves and our buddy */
406 if (UPDOWN_HasBuddyBorder(infoPtr) || needBuddyBg) {
407 if (!theme || !UPDOWN_DrawBuddyBackground (infoPtr, hdc)) {
408 GetClientRect(infoPtr->Self, &rect);
409 DrawEdge(hdc, &rect, EDGE_SUNKEN,
410 BF_BOTTOM | BF_TOP |
411 (infoPtr->dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
415 /* Draw the incr button */
416 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
417 if (theme) {
418 DrawThemeBackground(theme, hdc, uPart, uState, &rect, NULL);
419 } else {
420 DrawFrameControl(hdc, &rect, DFC_SCROLL,
421 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
422 ((infoPtr->dwStyle & UDS_HOTTRACK) && uHot ? DFCS_HOT : 0) |
423 (uPressed ? DFCS_PUSHED : 0) |
424 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
427 /* Draw the decr button */
428 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
429 if (theme) {
430 DrawThemeBackground(theme, hdc, dPart, dState, &rect, NULL);
431 } else {
432 DrawFrameControl(hdc, &rect, DFC_SCROLL,
433 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
434 ((infoPtr->dwStyle & UDS_HOTTRACK) && dHot ? DFCS_HOT : 0) |
435 (dPressed ? DFCS_PUSHED : 0) |
436 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
439 return 0;
442 /***********************************************************************
443 * UPDOWN_Paint
445 * Asynchronous drawing (must ONLY be used in WM_PAINT).
446 * Calls UPDOWN_Draw.
448 static LRESULT UPDOWN_Paint (const UPDOWN_INFO *infoPtr, HDC hdc)
450 PAINTSTRUCT ps;
451 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
452 hdc = BeginPaint (infoPtr->Self, &ps);
453 UPDOWN_Draw (infoPtr, hdc);
454 EndPaint (infoPtr->Self, &ps);
455 return 0;
458 /***********************************************************************
459 * UPDOWN_KeyPressed
461 * Handle key presses (up & down) when we have to do so
463 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
465 int arrow, accel;
467 if (key == VK_UP) arrow = FLAG_INCR;
468 else if (key == VK_DOWN) arrow = FLAG_DECR;
469 else return 1;
471 UPDOWN_GetBuddyInt (infoPtr);
472 infoPtr->Flags &= ~FLAG_ARROW;
473 infoPtr->Flags |= FLAG_PRESSED | arrow;
474 InvalidateRect (infoPtr->Self, NULL, FALSE);
475 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
476 accel = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
477 UPDOWN_DoAction (infoPtr, accel, arrow);
478 return 0;
481 static int UPDOWN_GetPos(UPDOWN_INFO *infoPtr, BOOL *err)
483 BOOL succ = UPDOWN_GetBuddyInt(infoPtr);
484 int val = infoPtr->CurVal;
486 if(!UPDOWN_InBounds(infoPtr, val)) {
487 if((infoPtr->MinVal < infoPtr->MaxVal && val < infoPtr->MinVal)
488 || (infoPtr->MinVal > infoPtr->MaxVal && val > infoPtr->MinVal))
489 val = infoPtr->MinVal;
490 else
491 val = infoPtr->MaxVal;
493 succ = FALSE;
496 if(err) *err = !succ;
497 return val;
500 static int UPDOWN_SetPos(UPDOWN_INFO *infoPtr, int pos)
502 int ret = infoPtr->CurVal;
504 if(!UPDOWN_InBounds(infoPtr, pos)) {
505 if((infoPtr->MinVal < infoPtr->MaxVal && pos < infoPtr->MinVal)
506 || (infoPtr->MinVal > infoPtr->MaxVal && pos > infoPtr->MinVal))
507 pos = infoPtr->MinVal;
508 else
509 pos = infoPtr->MaxVal;
512 infoPtr->CurVal = pos;
513 UPDOWN_SetBuddyInt(infoPtr);
515 if(!UPDOWN_InBounds(infoPtr, ret)) {
516 if((infoPtr->MinVal < infoPtr->MaxVal && ret < infoPtr->MinVal)
517 || (infoPtr->MinVal > infoPtr->MaxVal && ret > infoPtr->MinVal))
518 ret = infoPtr->MinVal;
519 else
520 ret = infoPtr->MaxVal;
522 return ret;
526 /***********************************************************************
527 * UPDOWN_SetRange
529 * Handle UDM_SETRANGE, UDM_SETRANGE32
531 * FIXME: handle Max == Min properly:
532 * - arrows should be disabled (without WS_DISABLED set),
533 * visually they can't be pressed and don't respond;
534 * - all input messages should still pass in.
536 static LRESULT UPDOWN_SetRange(UPDOWN_INFO *infoPtr, INT Max, INT Min)
538 infoPtr->MaxVal = Max;
539 infoPtr->MinVal = Min;
541 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
542 infoPtr->MinVal, infoPtr->MaxVal, infoPtr->Self);
544 return 0;
547 /***********************************************************************
548 * UPDOWN_MouseWheel
550 * Handle mouse wheel scrolling
552 static LRESULT UPDOWN_MouseWheel(UPDOWN_INFO *infoPtr, WPARAM wParam)
554 int iWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
556 if (wParam & (MK_SHIFT | MK_CONTROL))
557 return 0;
559 if (iWheelDelta != 0)
561 UPDOWN_GetBuddyInt(infoPtr);
562 UPDOWN_DoAction(infoPtr, abs(iWheelDelta), iWheelDelta > 0 ? FLAG_INCR : FLAG_DECR);
565 return 1;
569 /***********************************************************************
570 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
571 * control.
573 static LRESULT CALLBACK
574 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
575 UINT_PTR uId, DWORD_PTR ref_data)
577 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr((HWND)ref_data);
579 TRACE("hwnd %p, uMsg %04x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
581 switch(uMsg)
583 case WM_KEYDOWN:
584 if (infoPtr)
586 UPDOWN_KeyPressed(infoPtr, (int)wParam);
587 if (wParam == VK_UP || wParam == VK_DOWN)
588 return 0;
590 break;
592 case WM_MOUSEWHEEL:
593 if (infoPtr)
594 UPDOWN_MouseWheel(infoPtr, (int)wParam);
595 break;
597 case WM_NCDESTROY:
598 RemoveWindowSubclass(hwnd, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID);
599 break;
600 default:
601 break;
604 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
607 static void UPDOWN_ResetSubclass (UPDOWN_INFO *infoPtr)
609 SetWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID, 0);
612 /***********************************************************************
613 * UPDOWN_SetBuddy
615 * Sets bud as a new Buddy.
616 * Then, it should subclass the buddy
617 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
618 * process the UP/DOWN arrow keys.
619 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
620 * the size/pos of the buddy and the control are adjusted accordingly.
622 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
624 RECT budRect; /* new coord for the buddy */
625 int x, width; /* new x position and width for the up-down */
626 WCHAR buddyClass[40];
627 HWND old_buddy;
629 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
631 old_buddy = infoPtr->Buddy;
633 UPDOWN_ResetSubclass (infoPtr);
635 if (!IsWindow(bud)) bud = NULL;
637 /* Store buddy window handle */
638 infoPtr->Buddy = bud;
640 if(bud) {
641 /* Store buddy window class type */
642 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
643 if (GetClassNameW(bud, buddyClass, ARRAY_SIZE(buddyClass))) {
644 if (lstrcmpiW(buddyClass, WC_EDITW) == 0)
645 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
646 else if (lstrcmpiW(buddyClass, WC_LISTBOXW) == 0)
647 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
650 if (infoPtr->dwStyle & UDS_ARROWKEYS)
651 SetWindowSubclass(bud, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID,
652 (DWORD_PTR)infoPtr->Self);
654 /* Get the rect of the buddy relative to its parent */
655 GetWindowRect(infoPtr->Buddy, &budRect);
656 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
658 /* now do the positioning */
659 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
660 x = budRect.left;
661 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
662 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
663 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
664 x = budRect.right+DEFAULT_XSEP;
665 } else {
666 /* nothing to do */
667 return old_buddy;
670 /* first adjust the buddy to accommodate the up/down */
671 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
672 budRect.right - budRect.left, budRect.bottom - budRect.top,
673 SWP_NOACTIVATE|SWP_NOZORDER);
675 /* now position the up/down */
676 /* Since the UDS_ALIGN* flags were used, */
677 /* we will pick the position and size of the window. */
678 width = DEFAULT_WIDTH;
681 * If the updown has a buddy border, it has to overlap with the buddy
682 * to look as if it is integrated with the buddy control.
683 * We nudge the control or change its size to overlap.
685 if (UPDOWN_HasBuddyBorder(infoPtr)) {
686 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
687 width += DEFAULT_BUDDYBORDER;
688 else
689 x -= DEFAULT_BUDDYBORDER;
692 SetWindowPos(infoPtr->Self, 0, x,
693 budRect.top - DEFAULT_ADDTOP, width,
694 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
695 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
696 } else if (!(infoPtr->dwStyle & UDS_HORZ) && old_buddy != NULL) {
697 RECT rect;
698 GetWindowRect(infoPtr->Self, &rect);
699 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
700 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
701 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
704 return old_buddy;
707 /***********************************************************************
708 * UPDOWN_DoAction
710 * This function increments/decrements the CurVal by the
711 * 'delta' amount according to the 'action' flag which can be a
712 * combination of FLAG_INCR and FLAG_DECR
713 * It notifies the parent as required.
714 * It handles wrapping and non-wrapping correctly.
715 * It is assumed that delta>0
717 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
719 NM_UPDOWN ni;
721 TRACE("%d by %d\n", action, delta);
723 /* check if we can do the modification first */
724 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
725 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
727 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
729 /* We must notify parent now to obtain permission */
730 ni.iPos = infoPtr->CurVal;
731 ni.iDelta = delta;
732 ni.hdr.hwndFrom = infoPtr->Self;
733 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
734 ni.hdr.code = UDN_DELTAPOS;
735 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, ni.hdr.idFrom, (LPARAM)&ni)) {
736 /* Parent said: OK to adjust */
738 /* Now adjust value with (maybe new) delta */
739 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
740 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
742 /* Now take care about our buddy */
743 UPDOWN_SetBuddyInt (infoPtr);
747 /* Also, notify it. This message is sent in any case. */
748 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
749 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
752 /***********************************************************************
753 * UPDOWN_IsEnabled
755 * Returns TRUE if it is enabled as well as its buddy (if any)
756 * FALSE otherwise
758 static BOOL UPDOWN_IsEnabled (const UPDOWN_INFO *infoPtr)
760 if (!IsWindowEnabled(infoPtr->Self))
761 return FALSE;
762 if(infoPtr->Buddy)
763 return IsWindowEnabled(infoPtr->Buddy);
764 return TRUE;
767 /***********************************************************************
768 * UPDOWN_CancelMode
770 * Deletes any timers, releases the mouse and does redraw if necessary.
771 * If the control is not in "capture" mode, it does nothing.
772 * If the control was not in cancel mode, it returns FALSE.
773 * If the control was in cancel mode, it returns TRUE.
775 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
777 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
779 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
780 KillTimer (infoPtr->Self, TIMER_ACCEL);
781 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
783 if (GetCapture() == infoPtr->Self)
784 ReleaseCapture();
786 infoPtr->Flags &= ~FLAG_PRESSED;
787 InvalidateRect (infoPtr->Self, NULL, FALSE);
789 return TRUE;
792 /***********************************************************************
793 * UPDOWN_HandleMouseEvent
795 * Handle a mouse event for the updown.
796 * 'pt' is the location of the mouse event in client or
797 * windows coordinates.
799 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
801 POINT pt = { x, y };
802 RECT rect;
803 int temp, arrow;
804 TRACKMOUSEEVENT tme;
806 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
808 switch(msg)
810 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
812 /* If the buddy is an edit, will set focus to it */
813 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
815 /* Now see which one is the 'active' arrow */
816 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
818 /* Update the flags if we are in/out */
819 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
820 if (arrow)
821 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
822 else
823 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
825 if (infoPtr->Flags & FLAG_ARROW) {
827 /* Update the CurVal if necessary */
828 UPDOWN_GetBuddyInt (infoPtr);
830 /* Set up the correct flags */
831 infoPtr->Flags |= FLAG_PRESSED;
833 /* repaint the control */
834 InvalidateRect (infoPtr->Self, NULL, FALSE);
836 /* process the click */
837 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
838 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
840 /* now capture all mouse messages */
841 SetCapture (infoPtr->Self);
843 /* and startup the first timer */
844 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
846 break;
848 case WM_MOUSEMOVE:
849 /* save the flags to see if any got modified */
850 temp = infoPtr->Flags;
852 /* Now see which one is the 'active' arrow */
853 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
855 /* Update the flags if we are in/out */
856 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
857 if(arrow) {
858 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
859 } else {
860 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
863 /* If state changed, redraw the control */
864 if(temp != infoPtr->Flags)
865 InvalidateRect (infoPtr->Self, NULL, FALSE);
867 /* Set up tracking so the mousein flags can be reset when the
868 * mouse leaves the control */
869 tme.cbSize = sizeof( tme );
870 tme.dwFlags = TME_LEAVE;
871 tme.hwndTrack = infoPtr->Self;
872 TrackMouseEvent (&tme);
874 break;
875 case WM_MOUSELEAVE:
876 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
877 InvalidateRect (infoPtr->Self, NULL, FALSE);
878 break;
880 default:
881 ERR("Impossible case (msg=%x)!\n", msg);
886 /***********************************************************************
887 * UpDownWndProc
889 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
891 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
892 HTHEME theme;
894 TRACE("hwnd %p, msg %04x, wparam %Id, lparam %Ix\n", hwnd, message, wParam, lParam);
896 if (!infoPtr && (message != WM_CREATE))
897 return DefWindowProcW (hwnd, message, wParam, lParam);
899 switch(message)
901 case WM_CREATE:
903 CREATESTRUCTW *pcs = (CREATESTRUCTW*)lParam;
905 infoPtr = heap_alloc_zero(sizeof(*infoPtr));
906 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
908 /* initialize the info struct */
909 infoPtr->Self = hwnd;
910 infoPtr->Notify = pcs->hwndParent;
911 infoPtr->dwStyle = pcs->style;
912 infoPtr->AccelCount = 0;
913 infoPtr->AccelVect = 0;
914 infoPtr->AccelIndex = -1;
915 infoPtr->CurVal = 0;
916 infoPtr->MinVal = 100;
917 infoPtr->MaxVal = 0;
918 infoPtr->Base = 10; /* Default to base 10 */
919 infoPtr->Buddy = 0; /* No buddy window yet */
920 infoPtr->Flags = (infoPtr->dwStyle & UDS_SETBUDDYINT) ? FLAG_BUDDYINT : 0;
922 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
923 if (!(infoPtr->dwStyle & UDS_HORZ))
924 SetWindowPos (hwnd, NULL, 0, 0, DEFAULT_WIDTH, pcs->cy,
925 SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
927 /* Do we pick the buddy win ourselves? */
928 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
929 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
931 OpenThemeData (hwnd, L"Spin");
933 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
935 break;
937 case WM_DESTROY:
938 heap_free (infoPtr->AccelVect);
939 UPDOWN_ResetSubclass (infoPtr);
940 heap_free (infoPtr);
941 SetWindowLongPtrW (hwnd, 0, 0);
942 theme = GetWindowTheme (hwnd);
943 CloseThemeData (theme);
944 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
945 break;
947 case WM_ENABLE:
948 if (wParam) {
949 infoPtr->dwStyle &= ~WS_DISABLED;
950 } else {
951 infoPtr->dwStyle |= WS_DISABLED;
952 UPDOWN_CancelMode (infoPtr);
954 InvalidateRect (infoPtr->Self, NULL, FALSE);
955 break;
957 case WM_STYLECHANGED:
958 if (wParam == GWL_STYLE) {
959 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
960 InvalidateRect (infoPtr->Self, NULL, FALSE);
962 break;
964 case WM_THEMECHANGED:
965 theme = GetWindowTheme (hwnd);
966 CloseThemeData (theme);
967 OpenThemeData (hwnd, L"Spin");
968 InvalidateRect (hwnd, NULL, TRUE);
969 break;
971 case WM_TIMER:
972 /* is this the auto-press timer? */
973 if(wParam == TIMER_AUTOPRESS) {
974 KillTimer(hwnd, TIMER_AUTOPRESS);
975 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
976 InvalidateRect(infoPtr->Self, NULL, FALSE);
979 /* if initial timer, kill it and start the repeat timer */
980 if(wParam == TIMER_AUTOREPEAT) {
981 INT delay;
983 KillTimer(hwnd, TIMER_AUTOREPEAT);
984 /* if no accel info given, used default timer */
985 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
986 infoPtr->AccelIndex = -1;
987 delay = REPEAT_DELAY;
988 } else {
989 infoPtr->AccelIndex = 0; /* otherwise, use it */
990 delay = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
992 SetTimer(hwnd, TIMER_ACCEL, delay, 0);
995 /* now, if the mouse is above us, do the thing...*/
996 if(infoPtr->Flags & FLAG_MOUSEIN) {
997 int temp;
999 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
1000 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
1002 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
1003 KillTimer(hwnd, TIMER_ACCEL);
1004 infoPtr->AccelIndex++; /* move to the next accel info */
1005 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
1006 /* make sure we have at least 1ms intervals */
1007 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
1010 break;
1012 case WM_CANCELMODE:
1013 return UPDOWN_CancelMode (infoPtr);
1015 case WM_LBUTTONUP:
1016 if (GetCapture() != infoPtr->Self) break;
1018 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
1019 (infoPtr->Flags & FLAG_ARROW) ) {
1021 SendMessageW( infoPtr->Notify,
1022 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
1023 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
1024 (LPARAM)hwnd);
1025 if (UPDOWN_IsBuddyEdit(infoPtr))
1026 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
1028 UPDOWN_CancelMode(infoPtr);
1029 break;
1031 case WM_LBUTTONDOWN:
1032 case WM_MOUSEMOVE:
1033 case WM_MOUSELEAVE:
1034 if(UPDOWN_IsEnabled(infoPtr))
1035 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1036 break;
1038 case WM_MOUSEWHEEL:
1039 UPDOWN_MouseWheel(infoPtr, wParam);
1040 break;
1042 case WM_KEYDOWN:
1043 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
1044 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
1045 break;
1047 case WM_PRINTCLIENT:
1048 case WM_PAINT:
1049 return UPDOWN_Paint (infoPtr, (HDC)wParam);
1051 case UDM_GETACCEL:
1052 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
1053 if (wParam && lParam) {
1054 int temp = min(infoPtr->AccelCount, wParam);
1055 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
1056 return temp;
1058 return 0;
1060 case UDM_SETACCEL:
1062 TRACE("UDM_SETACCEL\n");
1064 if(infoPtr->AccelVect) {
1065 heap_free (infoPtr->AccelVect);
1066 infoPtr->AccelCount = 0;
1067 infoPtr->AccelVect = 0;
1069 if(wParam==0) return TRUE;
1070 infoPtr->AccelVect = heap_alloc(wParam*sizeof(UDACCEL));
1071 if(!infoPtr->AccelVect) return FALSE;
1072 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
1073 infoPtr->AccelCount = wParam;
1075 if (TRACE_ON(updown))
1077 UINT i;
1079 for (i = 0; i < wParam; i++)
1080 TRACE("%u: nSec %u nInc %u\n", i,
1081 infoPtr->AccelVect[i].nSec, infoPtr->AccelVect[i].nInc);
1084 return TRUE;
1086 case UDM_GETBASE:
1087 return infoPtr->Base;
1089 case UDM_SETBASE:
1090 TRACE("UpDown Ctrl new base(%Id), hwnd=%p\n", wParam, hwnd);
1091 if (wParam==10 || wParam==16) {
1092 WPARAM old_base = infoPtr->Base;
1093 infoPtr->Base = wParam;
1095 if (old_base != infoPtr->Base)
1096 UPDOWN_SetBuddyInt(infoPtr);
1098 return old_base;
1100 break;
1102 case UDM_GETBUDDY:
1103 return (LRESULT)infoPtr->Buddy;
1105 case UDM_SETBUDDY:
1106 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
1108 case UDM_GETPOS:
1110 BOOL err;
1111 int pos;
1113 pos = UPDOWN_GetPos(infoPtr, &err);
1114 return MAKELONG(pos, err);
1116 case UDM_SETPOS:
1118 return UPDOWN_SetPos(infoPtr, (short)LOWORD(lParam));
1120 case UDM_GETRANGE:
1121 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
1123 case UDM_SETRANGE:
1124 /* we must have:
1125 UD_MINVAL <= Max <= UD_MAXVAL
1126 UD_MINVAL <= Min <= UD_MAXVAL
1127 |Max-Min| <= UD_MAXVAL */
1128 UPDOWN_SetRange(infoPtr, (short)lParam, (short)HIWORD(lParam));
1129 break;
1131 case UDM_GETRANGE32:
1132 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
1133 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
1134 break;
1136 case UDM_SETRANGE32:
1137 UPDOWN_SetRange(infoPtr, (INT)lParam, (INT)wParam);
1138 break;
1140 case UDM_GETPOS32:
1142 return UPDOWN_GetPos(infoPtr, (BOOL*)lParam);
1144 case UDM_SETPOS32:
1146 return UPDOWN_SetPos(infoPtr, (int)lParam);
1148 case UDM_GETUNICODEFORMAT:
1149 /* we lie a bit here, we're always using Unicode internally */
1150 return infoPtr->UnicodeFormat;
1152 case UDM_SETUNICODEFORMAT:
1154 /* do we really need to honour this flag? */
1155 int temp = infoPtr->UnicodeFormat;
1156 infoPtr->UnicodeFormat = (BOOL)wParam;
1157 return temp;
1159 default:
1160 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1161 ERR("unknown msg %04x wp=%Ix lp=%Ix\n", message, wParam, lParam);
1162 return DefWindowProcW (hwnd, message, wParam, lParam);
1165 return 0;
1168 /***********************************************************************
1169 * UPDOWN_Register [Internal]
1171 * Registers the updown window class.
1173 void UPDOWN_Register(void)
1175 WNDCLASSW wndClass;
1177 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
1178 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1179 wndClass.lpfnWndProc = UpDownWindowProc;
1180 wndClass.cbClsExtra = 0;
1181 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
1182 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1183 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1184 wndClass.lpszClassName = UPDOWN_CLASSW;
1186 RegisterClassW( &wndClass );
1190 /***********************************************************************
1191 * UPDOWN_Unregister [Internal]
1193 * Unregisters the updown window class.
1195 void UPDOWN_Unregister (void)
1197 UnregisterClassW (UPDOWN_CLASSW, NULL);