dbghelp: Remove DMT_ entries for .DBG and .PDB files.
[wine.git] / dlls / comctl32 / updown.c
blob9f46bfaffa7c823b14ba19253da31f3b51df650f
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/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(updown);
40 typedef struct
42 HWND Self; /* Handle to this up-down control */
43 HWND Notify; /* Handle to the parent window */
44 DWORD dwStyle; /* The GWL_STYLE for this window */
45 UINT AccelCount; /* Number of elements in AccelVect */
46 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
47 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
48 INT Base; /* Base to display nr in the buddy window */
49 INT CurVal; /* Current up-down value */
50 INT MinVal; /* Minimum up-down value */
51 INT MaxVal; /* Maximum up-down value */
52 HWND Buddy; /* Handle to the buddy window */
53 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
54 INT Flags; /* Internal Flags FLAG_* */
55 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
56 } UPDOWN_INFO;
58 /* Control configuration constants */
60 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
61 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
62 #define REPEAT_DELAY 50 /* delay between auto-increments */
64 #define DEFAULT_WIDTH 16 /* default width of the ctrl */
65 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
66 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
67 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
68 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
69 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
70 #define DEFAULT_BUDDYBORDER_THEMED 1 /* buddy border when theming is enabled */
71 #define DEFAULT_BUDDYSPACER_THEMED 0 /* buddy spacer when theming is enabled */
73 /* Work constants */
75 #define FLAG_INCR 0x01
76 #define FLAG_DECR 0x02
77 #define FLAG_MOUSEIN 0x04
78 #define FLAG_PRESSED 0x08
79 #define FLAG_BUDDYINT 0x10 /* UDS_SETBUDDYINT was set on creation */
80 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
82 #define BUDDY_TYPE_UNKNOWN 0
83 #define BUDDY_TYPE_LISTBOX 1
84 #define BUDDY_TYPE_EDIT 2
86 #define TIMER_AUTOREPEAT 1
87 #define TIMER_ACCEL 2
88 #define TIMER_AUTOPRESS 3
90 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
92 /* id used for SetWindowSubclass */
93 #define BUDDY_SUBCLASSID 1
95 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
97 /***********************************************************************
98 * UPDOWN_IsBuddyEdit
99 * Tests if our buddy is an edit control.
101 static inline BOOL UPDOWN_IsBuddyEdit(const UPDOWN_INFO *infoPtr)
103 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
106 /***********************************************************************
107 * UPDOWN_IsBuddyListbox
108 * Tests if our buddy is a listbox control.
110 static inline BOOL UPDOWN_IsBuddyListbox(const UPDOWN_INFO *infoPtr)
112 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
115 /***********************************************************************
116 * UPDOWN_InBounds
117 * Tests if a given value 'val' is between the Min&Max limits
119 static BOOL UPDOWN_InBounds(const UPDOWN_INFO *infoPtr, int val)
121 if(infoPtr->MaxVal > infoPtr->MinVal)
122 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
123 else
124 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
127 /***********************************************************************
128 * UPDOWN_OffsetVal
129 * Change the current value by delta.
130 * It returns TRUE is the value was changed successfully, or FALSE
131 * if the value was not changed, as it would go out of bounds.
133 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
135 /* check if we can do the modification first */
136 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
137 if (infoPtr->dwStyle & UDS_WRAP) {
138 delta += (delta < 0 ? -1 : 1) *
139 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
140 (infoPtr->MinVal - infoPtr->MaxVal) +
141 (delta < 0 ? 1 : -1);
142 } else if ((infoPtr->MaxVal > infoPtr->MinVal && infoPtr->CurVal+delta > infoPtr->MaxVal)
143 || (infoPtr->MaxVal < infoPtr->MinVal && infoPtr->CurVal+delta < infoPtr->MaxVal)) {
144 delta = infoPtr->MaxVal - infoPtr->CurVal;
145 } else {
146 delta = infoPtr->MinVal - infoPtr->CurVal;
150 infoPtr->CurVal += delta;
151 return delta != 0;
154 /***********************************************************************
155 * UPDOWN_HasBuddyBorder
157 * When we have a buddy set and that we are aligned on our buddy, we
158 * want to draw a sunken edge to make like we are part of that control.
160 static BOOL UPDOWN_HasBuddyBorder(const UPDOWN_INFO *infoPtr)
162 return ( ((infoPtr->dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
163 UPDOWN_IsBuddyEdit(infoPtr) );
166 /***********************************************************************
167 * UPDOWN_GetArrowRect
168 * wndPtr - pointer to the up-down wnd
169 * rect - will hold the rectangle
170 * arrow - FLAG_INCR to get the "increment" rect (up or right)
171 * FLAG_DECR to get the "decrement" rect (down or left)
173 static void UPDOWN_GetArrowRect (const UPDOWN_INFO* infoPtr, RECT *rect, unsigned int arrow)
175 HTHEME theme = GetWindowTheme (infoPtr->Self);
176 const int border = theme ? DEFAULT_BUDDYBORDER_THEMED : DEFAULT_BUDDYBORDER;
177 const int spacer = theme ? DEFAULT_BUDDYSPACER_THEMED : DEFAULT_BUDDYSPACER;
178 int size;
180 assert(arrow && (arrow & (FLAG_INCR | FLAG_DECR)) != (FLAG_INCR | FLAG_DECR));
182 GetClientRect (infoPtr->Self, rect);
185 * Make sure we calculate the rectangle to fit even if we draw the
186 * border.
188 if (UPDOWN_HasBuddyBorder(infoPtr)) {
189 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
190 rect->left += border;
191 else
192 rect->right -= border;
194 InflateRect(rect, 0, -border);
197 /* now figure out if we need a space away from the buddy */
198 if (IsWindow(infoPtr->Buddy) ) {
199 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= spacer;
200 else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) rect->left += spacer;
204 * We're calculating the midpoint to figure-out where the
205 * separation between the buttons will lay.
207 if (infoPtr->dwStyle & UDS_HORZ) {
208 size = (rect->right - rect->left) / 2;
209 if (arrow & FLAG_INCR)
210 rect->left = rect->right - size;
211 else if (arrow & FLAG_DECR)
212 rect->right = rect->left + size;
213 } else {
214 size = (rect->bottom - rect->top) / 2;
215 if (arrow & FLAG_INCR)
216 rect->bottom = rect->top + size;
217 else if (arrow & FLAG_DECR)
218 rect->top = rect->bottom - size;
222 /***********************************************************************
223 * UPDOWN_GetArrowFromPoint
224 * Returns the rectagle (for the up or down arrow) that contains pt.
225 * If it returns the up rect, it returns FLAG_INCR.
226 * If it returns the down rect, it returns FLAG_DECR.
228 static INT UPDOWN_GetArrowFromPoint (const UPDOWN_INFO *infoPtr, RECT *rect, POINT pt)
230 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
231 if(PtInRect(rect, pt)) return FLAG_INCR;
233 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
234 if(PtInRect(rect, pt)) return FLAG_DECR;
236 return 0;
240 /***********************************************************************
241 * UPDOWN_GetThousandSep
242 * Returns the thousand sep. If an error occurs, it returns ','.
244 static WCHAR UPDOWN_GetThousandSep(void)
246 WCHAR sep[2];
248 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
249 sep[0] = ',';
251 return sep[0];
254 /***********************************************************************
255 * UPDOWN_GetBuddyInt
256 * Tries to read the pos from the buddy window and if it succeeds,
257 * it stores it in the control's CurVal
258 * returns:
259 * TRUE - if it read the integer from the buddy successfully
260 * FALSE - if an error occurred
262 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
264 WCHAR txt[20], sep, *src, *dst;
265 int newVal;
267 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
268 return FALSE;
270 /*if the buddy is a list window, we must set curr index */
271 if (UPDOWN_IsBuddyListbox(infoPtr)) {
272 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
273 if(newVal < 0) return FALSE;
274 } else {
275 /* we have a regular window, so will get the text */
276 /* note that a zero-length string is a legitimate value for 'txt',
277 * and ought to result in a successful conversion to '0'. */
278 if (GetWindowTextW(infoPtr->Buddy, txt, ARRAY_SIZE(txt)) < 0)
279 return FALSE;
281 sep = UPDOWN_GetThousandSep();
283 /* now get rid of the separators */
284 for(src = dst = txt; *src; src++)
285 if(*src != sep) *dst++ = *src;
286 *dst = 0;
288 /* try to convert the number and validate it */
289 newVal = wcstol(txt, &src, infoPtr->Base);
290 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
293 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
294 infoPtr->CurVal = newVal;
295 return TRUE;
299 /***********************************************************************
300 * UPDOWN_SetBuddyInt
301 * Tries to set the pos to the buddy window based on current pos
302 * returns:
303 * TRUE - if it set the caption of the buddy successfully
304 * FALSE - if an error occurred
306 static BOOL UPDOWN_SetBuddyInt (const UPDOWN_INFO *infoPtr)
308 const WCHAR *fmt;
309 WCHAR txt[20], txt_old[20] = { 0 };
310 int len;
312 if (!((infoPtr->Flags & FLAG_BUDDYINT) && IsWindow(infoPtr->Buddy)))
313 return FALSE;
315 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
317 /*if the buddy is a list window, we must set curr index */
318 if (UPDOWN_IsBuddyListbox(infoPtr)) {
319 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
322 /* Regular window, so set caption to the number */
323 fmt = (infoPtr->Base == 16) ? L"0x%04X" : L"%d";
324 len = wsprintfW(txt, fmt, infoPtr->CurVal);
327 /* Do thousands separation if necessary */
328 if ((infoPtr->Base == 10) && !(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
329 WCHAR tmp[ARRAY_SIZE(txt)], *src = tmp, *dst = txt;
330 WCHAR sep = UPDOWN_GetThousandSep();
331 int start = len % 3;
333 memcpy(tmp, txt, sizeof(txt));
334 if (start == 0) start = 3;
335 dst += start;
336 src += start;
337 for (len=0; *src; len++) {
338 if (len % 3 == 0) *dst++ = sep;
339 *dst++ = *src++;
341 *dst = 0;
344 /* if nothing changed exit earlier */
345 GetWindowTextW(infoPtr->Buddy, txt_old, ARRAY_SIZE(txt_old));
346 if (lstrcmpiW(txt_old, txt) == 0) return FALSE;
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, r;
359 HTHEME buddyTheme = GetWindowTheme (infoPtr->Buddy);
360 if (!buddyTheme) return FALSE;
362 GetWindowRect (infoPtr->Buddy, &br);
363 MapWindowPoints (NULL, infoPtr->Self, (POINT*)&br, 2);
364 GetClientRect (infoPtr->Self, &r);
366 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
367 br.left = r.left;
368 else if (infoPtr->dwStyle & UDS_ALIGNRIGHT)
369 br.right = r.right;
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, accel;
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 accel = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
476 UPDOWN_DoAction (infoPtr, accel, arrow);
477 return 0;
480 static int UPDOWN_GetPos(UPDOWN_INFO *infoPtr, BOOL *err)
482 BOOL succ = UPDOWN_GetBuddyInt(infoPtr);
483 int val = infoPtr->CurVal;
485 if(!UPDOWN_InBounds(infoPtr, val)) {
486 if((infoPtr->MinVal < infoPtr->MaxVal && val < infoPtr->MinVal)
487 || (infoPtr->MinVal > infoPtr->MaxVal && val > infoPtr->MinVal))
488 val = infoPtr->MinVal;
489 else
490 val = infoPtr->MaxVal;
492 succ = FALSE;
495 if(err) *err = !succ;
496 return val;
499 static int UPDOWN_SetPos(UPDOWN_INFO *infoPtr, int pos)
501 int ret = infoPtr->CurVal;
503 if(!UPDOWN_InBounds(infoPtr, pos)) {
504 if((infoPtr->MinVal < infoPtr->MaxVal && pos < infoPtr->MinVal)
505 || (infoPtr->MinVal > infoPtr->MaxVal && pos > infoPtr->MinVal))
506 pos = infoPtr->MinVal;
507 else
508 pos = infoPtr->MaxVal;
511 infoPtr->CurVal = pos;
512 UPDOWN_SetBuddyInt(infoPtr);
514 if(!UPDOWN_InBounds(infoPtr, ret)) {
515 if((infoPtr->MinVal < infoPtr->MaxVal && ret < infoPtr->MinVal)
516 || (infoPtr->MinVal > infoPtr->MaxVal && ret > infoPtr->MinVal))
517 ret = infoPtr->MinVal;
518 else
519 ret = infoPtr->MaxVal;
521 return ret;
525 /***********************************************************************
526 * UPDOWN_SetRange
528 * Handle UDM_SETRANGE, UDM_SETRANGE32
530 * FIXME: handle Max == Min properly:
531 * - arrows should be disabled (without WS_DISABLED set),
532 * visually they can't be pressed and don't respond;
533 * - all input messages should still pass in.
535 static LRESULT UPDOWN_SetRange(UPDOWN_INFO *infoPtr, INT Max, INT Min)
537 infoPtr->MaxVal = Max;
538 infoPtr->MinVal = Min;
540 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
541 infoPtr->MinVal, infoPtr->MaxVal, infoPtr->Self);
543 return 0;
546 /***********************************************************************
547 * UPDOWN_MouseWheel
549 * Handle mouse wheel scrolling
551 static LRESULT UPDOWN_MouseWheel(UPDOWN_INFO *infoPtr, WPARAM wParam)
553 int iWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
555 if (wParam & (MK_SHIFT | MK_CONTROL))
556 return 0;
558 if (iWheelDelta != 0)
560 UPDOWN_GetBuddyInt(infoPtr);
561 UPDOWN_DoAction(infoPtr, abs(iWheelDelta), iWheelDelta > 0 ? FLAG_INCR : FLAG_DECR);
564 return 1;
568 /***********************************************************************
569 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
570 * control.
572 static LRESULT CALLBACK
573 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
574 UINT_PTR uId, DWORD_PTR ref_data)
576 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr((HWND)ref_data);
578 TRACE("hwnd %p, uMsg %04x, wParam %Ix, lParam %Ix\n", hwnd, uMsg, wParam, lParam);
580 switch(uMsg)
582 case WM_KEYDOWN:
583 if (infoPtr)
585 UPDOWN_KeyPressed(infoPtr, (int)wParam);
586 if (wParam == VK_UP || wParam == VK_DOWN)
587 return 0;
589 break;
591 case WM_MOUSEWHEEL:
592 if (infoPtr)
593 UPDOWN_MouseWheel(infoPtr, (int)wParam);
594 break;
596 case WM_NCDESTROY:
597 RemoveWindowSubclass(hwnd, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID);
598 break;
599 default:
600 break;
603 return DefSubclassProc(hwnd, uMsg, wParam, lParam);
606 static void UPDOWN_ResetSubclass (UPDOWN_INFO *infoPtr)
608 SetWindowSubclass(infoPtr->Buddy, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID, 0);
611 /***********************************************************************
612 * UPDOWN_SetBuddy
614 * Sets bud as a new Buddy.
615 * Then, it should subclass the buddy
616 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
617 * process the UP/DOWN arrow keys.
618 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
619 * the size/pos of the buddy and the control are adjusted accordingly.
621 static HWND UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
623 RECT budRect; /* new coord for the buddy */
624 int x, width; /* new x position and width for the up-down */
625 WCHAR buddyClass[40];
626 HWND old_buddy;
628 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
630 old_buddy = infoPtr->Buddy;
632 UPDOWN_ResetSubclass (infoPtr);
634 if (!IsWindow(bud)) bud = NULL;
636 /* Store buddy window handle */
637 infoPtr->Buddy = bud;
639 if(bud) {
640 /* Store buddy window class type */
641 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
642 if (GetClassNameW(bud, buddyClass, ARRAY_SIZE(buddyClass))) {
643 if (lstrcmpiW(buddyClass, WC_EDITW) == 0)
644 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
645 else if (lstrcmpiW(buddyClass, WC_LISTBOXW) == 0)
646 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
649 if (infoPtr->dwStyle & UDS_ARROWKEYS)
650 SetWindowSubclass(bud, UPDOWN_Buddy_SubclassProc, BUDDY_SUBCLASSID,
651 (DWORD_PTR)infoPtr->Self);
653 /* Get the rect of the buddy relative to its parent */
654 GetWindowRect(infoPtr->Buddy, &budRect);
655 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
657 /* now do the positioning */
658 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
659 x = budRect.left;
660 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
661 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
662 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
663 x = budRect.right+DEFAULT_XSEP;
664 } else {
665 /* nothing to do */
666 return old_buddy;
669 /* first adjust the buddy to accommodate the up/down */
670 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
671 budRect.right - budRect.left, budRect.bottom - budRect.top,
672 SWP_NOACTIVATE|SWP_NOZORDER);
674 /* now position the up/down */
675 /* Since the UDS_ALIGN* flags were used, */
676 /* we will pick the position and size of the window. */
677 width = DEFAULT_WIDTH;
680 * If the updown has a buddy border, it has to overlap with the buddy
681 * to look as if it is integrated with the buddy control.
682 * We nudge the control or change its size to overlap.
684 if (UPDOWN_HasBuddyBorder(infoPtr)) {
685 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
686 width += DEFAULT_BUDDYBORDER;
687 else
688 x -= DEFAULT_BUDDYBORDER;
691 SetWindowPos(infoPtr->Self, 0, x,
692 budRect.top - DEFAULT_ADDTOP, width,
693 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
694 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
695 } else if (!(infoPtr->dwStyle & UDS_HORZ) && old_buddy != NULL) {
696 RECT rect;
697 GetWindowRect(infoPtr->Self, &rect);
698 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
699 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
700 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
703 return old_buddy;
706 /***********************************************************************
707 * UPDOWN_DoAction
709 * This function increments/decrements the CurVal by the
710 * 'delta' amount according to the 'action' flag which can be a
711 * combination of FLAG_INCR and FLAG_DECR
712 * It notifies the parent as required.
713 * It handles wrapping and non-wrapping correctly.
714 * It is assumed that delta>0
716 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
718 NM_UPDOWN ni;
720 TRACE("%d by %d\n", action, delta);
722 /* check if we can do the modification first */
723 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
724 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
726 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
728 /* We must notify parent now to obtain permission */
729 ni.iPos = infoPtr->CurVal;
730 ni.iDelta = delta;
731 ni.hdr.hwndFrom = infoPtr->Self;
732 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
733 ni.hdr.code = UDN_DELTAPOS;
734 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, ni.hdr.idFrom, (LPARAM)&ni)) {
735 /* Parent said: OK to adjust */
737 /* Now adjust value with (maybe new) delta */
738 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
739 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
741 /* Now take care about our buddy */
742 UPDOWN_SetBuddyInt (infoPtr);
746 /* Also, notify it. This message is sent in any case. */
747 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
748 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
751 /***********************************************************************
752 * UPDOWN_IsEnabled
754 * Returns TRUE if it is enabled as well as its buddy (if any)
755 * FALSE otherwise
757 static BOOL UPDOWN_IsEnabled (const UPDOWN_INFO *infoPtr)
759 if (!IsWindowEnabled(infoPtr->Self))
760 return FALSE;
761 if(infoPtr->Buddy)
762 return IsWindowEnabled(infoPtr->Buddy);
763 return TRUE;
766 /***********************************************************************
767 * UPDOWN_CancelMode
769 * Deletes any timers, releases the mouse and does redraw if necessary.
770 * If the control is not in "capture" mode, it does nothing.
771 * If the control was not in cancel mode, it returns FALSE.
772 * If the control was in cancel mode, it returns TRUE.
774 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
776 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
778 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
779 KillTimer (infoPtr->Self, TIMER_ACCEL);
780 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
782 if (GetCapture() == infoPtr->Self)
783 ReleaseCapture();
785 infoPtr->Flags &= ~FLAG_PRESSED;
786 InvalidateRect (infoPtr->Self, NULL, FALSE);
788 return TRUE;
791 /***********************************************************************
792 * UPDOWN_HandleMouseEvent
794 * Handle a mouse event for the updown.
795 * 'pt' is the location of the mouse event in client or
796 * windows coordinates.
798 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
800 POINT pt = { x, y };
801 RECT rect;
802 int temp, arrow;
803 TRACKMOUSEEVENT tme;
805 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
807 switch(msg)
809 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
811 /* If the buddy is an edit, will set focus to it */
812 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
814 /* Now see which one is the 'active' arrow */
815 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
817 /* Update the flags if we are in/out */
818 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
819 if (arrow)
820 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
821 else
822 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
824 if (infoPtr->Flags & FLAG_ARROW) {
826 /* Update the CurVal if necessary */
827 UPDOWN_GetBuddyInt (infoPtr);
829 /* Set up the correct flags */
830 infoPtr->Flags |= FLAG_PRESSED;
832 /* repaint the control */
833 InvalidateRect (infoPtr->Self, NULL, FALSE);
835 /* process the click */
836 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
837 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
839 /* now capture all mouse messages */
840 SetCapture (infoPtr->Self);
842 /* and startup the first timer */
843 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
845 break;
847 case WM_MOUSEMOVE:
848 /* save the flags to see if any got modified */
849 temp = infoPtr->Flags;
851 /* Now see which one is the 'active' arrow */
852 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
854 /* Update the flags if we are in/out */
855 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
856 if(arrow) {
857 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
858 } else {
859 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
862 /* If state changed, redraw the control */
863 if(temp != infoPtr->Flags)
864 InvalidateRect (infoPtr->Self, NULL, FALSE);
866 /* Set up tracking so the mousein flags can be reset when the
867 * mouse leaves the control */
868 tme.cbSize = sizeof( tme );
869 tme.dwFlags = TME_LEAVE;
870 tme.hwndTrack = infoPtr->Self;
871 TrackMouseEvent (&tme);
873 break;
874 case WM_MOUSELEAVE:
875 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
876 InvalidateRect (infoPtr->Self, NULL, FALSE);
877 break;
879 default:
880 ERR("Impossible case (msg=%x)!\n", msg);
885 /***********************************************************************
886 * UpDownWndProc
888 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
890 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
891 HTHEME theme;
893 TRACE("hwnd %p, msg %04x, wparam %Id, lparam %Ix\n", hwnd, message, wParam, lParam);
895 if (!infoPtr && (message != WM_CREATE))
896 return DefWindowProcW (hwnd, message, wParam, lParam);
898 switch(message)
900 case WM_CREATE:
902 CREATESTRUCTW *pcs = (CREATESTRUCTW*)lParam;
904 infoPtr = Alloc(sizeof(*infoPtr));
905 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
907 /* initialize the info struct */
908 infoPtr->Self = hwnd;
909 infoPtr->Notify = pcs->hwndParent;
910 infoPtr->dwStyle = pcs->style;
911 infoPtr->AccelCount = 0;
912 infoPtr->AccelVect = 0;
913 infoPtr->AccelIndex = -1;
914 infoPtr->CurVal = 0;
915 infoPtr->MinVal = 100;
916 infoPtr->MaxVal = 0;
917 infoPtr->Base = 10; /* Default to base 10 */
918 infoPtr->Buddy = 0; /* No buddy window yet */
919 infoPtr->Flags = (infoPtr->dwStyle & UDS_SETBUDDYINT) ? FLAG_BUDDYINT : 0;
921 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
922 if (!(infoPtr->dwStyle & UDS_HORZ))
923 SetWindowPos (hwnd, NULL, 0, 0, DEFAULT_WIDTH, pcs->cy,
924 SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
926 /* Do we pick the buddy win ourselves? */
927 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
928 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
930 OpenThemeData (hwnd, L"Spin");
932 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
934 break;
936 case WM_DESTROY:
937 Free (infoPtr->AccelVect);
938 UPDOWN_ResetSubclass (infoPtr);
939 Free (infoPtr);
940 SetWindowLongPtrW (hwnd, 0, 0);
941 theme = GetWindowTheme (hwnd);
942 CloseThemeData (theme);
943 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
944 break;
946 case WM_ENABLE:
947 if (wParam) {
948 infoPtr->dwStyle &= ~WS_DISABLED;
949 } else {
950 infoPtr->dwStyle |= WS_DISABLED;
951 UPDOWN_CancelMode (infoPtr);
953 InvalidateRect (infoPtr->Self, NULL, FALSE);
954 break;
956 case WM_STYLECHANGED:
957 if (wParam == GWL_STYLE)
958 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
959 break;
961 case WM_THEMECHANGED:
962 theme = GetWindowTheme (hwnd);
963 CloseThemeData (theme);
964 OpenThemeData (hwnd, L"Spin");
965 InvalidateRect (hwnd, NULL, TRUE);
966 break;
968 case WM_TIMER:
969 /* is this the auto-press timer? */
970 if(wParam == TIMER_AUTOPRESS) {
971 KillTimer(hwnd, TIMER_AUTOPRESS);
972 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
973 InvalidateRect(infoPtr->Self, NULL, FALSE);
976 /* if initial timer, kill it and start the repeat timer */
977 if(wParam == TIMER_AUTOREPEAT) {
978 INT delay;
980 KillTimer(hwnd, TIMER_AUTOREPEAT);
981 /* if no accel info given, used default timer */
982 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
983 infoPtr->AccelIndex = -1;
984 delay = REPEAT_DELAY;
985 } else {
986 infoPtr->AccelIndex = 0; /* otherwise, use it */
987 delay = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
989 SetTimer(hwnd, TIMER_ACCEL, delay, 0);
992 /* now, if the mouse is above us, do the thing...*/
993 if(infoPtr->Flags & FLAG_MOUSEIN) {
994 int temp;
996 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
997 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
999 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
1000 KillTimer(hwnd, TIMER_ACCEL);
1001 infoPtr->AccelIndex++; /* move to the next accel info */
1002 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
1003 /* make sure we have at least 1ms intervals */
1004 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
1007 break;
1009 case WM_CANCELMODE:
1010 return UPDOWN_CancelMode (infoPtr);
1012 case WM_LBUTTONUP:
1013 if (GetCapture() != infoPtr->Self) break;
1015 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
1016 (infoPtr->Flags & FLAG_ARROW) ) {
1018 SendMessageW( infoPtr->Notify,
1019 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
1020 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
1021 (LPARAM)hwnd);
1022 if (UPDOWN_IsBuddyEdit(infoPtr))
1023 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
1025 UPDOWN_CancelMode(infoPtr);
1026 break;
1028 case WM_LBUTTONDOWN:
1029 case WM_MOUSEMOVE:
1030 case WM_MOUSELEAVE:
1031 if(UPDOWN_IsEnabled(infoPtr))
1032 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
1033 break;
1035 case WM_MOUSEWHEEL:
1036 UPDOWN_MouseWheel(infoPtr, wParam);
1037 break;
1039 case WM_KEYDOWN:
1040 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
1041 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
1042 break;
1044 case WM_PRINTCLIENT:
1045 case WM_PAINT:
1046 return UPDOWN_Paint (infoPtr, (HDC)wParam);
1048 case UDM_GETACCEL:
1049 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
1050 if (wParam && lParam) {
1051 int temp = min(infoPtr->AccelCount, wParam);
1052 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
1053 return temp;
1055 return 0;
1057 case UDM_SETACCEL:
1059 TRACE("UDM_SETACCEL\n");
1061 if(infoPtr->AccelVect) {
1062 Free (infoPtr->AccelVect);
1063 infoPtr->AccelCount = 0;
1064 infoPtr->AccelVect = 0;
1066 if(wParam==0) return TRUE;
1067 infoPtr->AccelVect = Alloc(wParam*sizeof(UDACCEL));
1068 if(!infoPtr->AccelVect) return FALSE;
1069 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
1070 infoPtr->AccelCount = wParam;
1072 if (TRACE_ON(updown))
1074 UINT i;
1076 for (i = 0; i < wParam; i++)
1077 TRACE("%u: nSec %u nInc %u\n", i,
1078 infoPtr->AccelVect[i].nSec, infoPtr->AccelVect[i].nInc);
1081 return TRUE;
1083 case UDM_GETBASE:
1084 return infoPtr->Base;
1086 case UDM_SETBASE:
1087 TRACE("UpDown Ctrl new base(%Id), hwnd=%p\n", wParam, hwnd);
1088 if (wParam==10 || wParam==16) {
1089 WPARAM old_base = infoPtr->Base;
1090 infoPtr->Base = wParam;
1092 if (old_base != infoPtr->Base)
1093 UPDOWN_SetBuddyInt(infoPtr);
1095 return old_base;
1097 break;
1099 case UDM_GETBUDDY:
1100 return (LRESULT)infoPtr->Buddy;
1102 case UDM_SETBUDDY:
1103 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
1105 case UDM_GETPOS:
1107 BOOL err;
1108 int pos;
1110 pos = UPDOWN_GetPos(infoPtr, &err);
1111 return MAKELONG(pos, err);
1113 case UDM_SETPOS:
1115 return UPDOWN_SetPos(infoPtr, (short)LOWORD(lParam));
1117 case UDM_GETRANGE:
1118 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
1120 case UDM_SETRANGE:
1121 /* we must have:
1122 UD_MINVAL <= Max <= UD_MAXVAL
1123 UD_MINVAL <= Min <= UD_MAXVAL
1124 |Max-Min| <= UD_MAXVAL */
1125 UPDOWN_SetRange(infoPtr, (short)lParam, (short)HIWORD(lParam));
1126 break;
1128 case UDM_GETRANGE32:
1129 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
1130 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
1131 break;
1133 case UDM_SETRANGE32:
1134 UPDOWN_SetRange(infoPtr, (INT)lParam, (INT)wParam);
1135 break;
1137 case UDM_GETPOS32:
1139 return UPDOWN_GetPos(infoPtr, (BOOL*)lParam);
1141 case UDM_SETPOS32:
1143 return UPDOWN_SetPos(infoPtr, (int)lParam);
1145 case UDM_GETUNICODEFORMAT:
1146 /* we lie a bit here, we're always using Unicode internally */
1147 return infoPtr->UnicodeFormat;
1149 case UDM_SETUNICODEFORMAT:
1151 /* do we really need to honour this flag? */
1152 int temp = infoPtr->UnicodeFormat;
1153 infoPtr->UnicodeFormat = (BOOL)wParam;
1154 return temp;
1156 default:
1157 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1158 ERR("unknown msg %04x wp=%Ix lp=%Ix\n", message, wParam, lParam);
1159 return DefWindowProcW (hwnd, message, wParam, lParam);
1162 return 0;
1165 /***********************************************************************
1166 * UPDOWN_Register [Internal]
1168 * Registers the updown window class.
1170 void UPDOWN_Register(void)
1172 WNDCLASSW wndClass;
1174 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
1175 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1176 wndClass.lpfnWndProc = UpDownWindowProc;
1177 wndClass.cbClsExtra = 0;
1178 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
1179 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1180 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1181 wndClass.lpszClassName = UPDOWN_CLASSW;
1183 RegisterClassW( &wndClass );
1187 /***********************************************************************
1188 * UPDOWN_Unregister [Internal]
1190 * Unregisters the updown window class.
1192 void UPDOWN_Unregister (void)
1194 UnregisterClassW (UPDOWN_CLASSW, NULL);