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
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(updown
);
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 */
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 */
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
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 /***********************************************************************
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 /***********************************************************************
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
);
124 return (infoPtr
->MaxVal
<= val
) && (val
<= infoPtr
->MinVal
);
127 /***********************************************************************
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
;
146 delta
= infoPtr
->MinVal
- infoPtr
->CurVal
;
150 infoPtr
->CurVal
+= delta
;
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
;
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
188 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
189 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
)
190 rect
->left
+= border
;
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
;
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
;
240 /***********************************************************************
241 * UPDOWN_GetThousandSep
242 * Returns the thousand sep. If an error occurs, it returns ','.
244 static WCHAR
UPDOWN_GetThousandSep(void)
248 if(GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_STHOUSAND
, sep
, 2) != 1)
254 /***********************************************************************
256 * Tries to read the pos from the buddy window and if it succeeds,
257 * it stores it in the control's CurVal
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
;
267 if (!((infoPtr
->Flags
& FLAG_BUDDYINT
) && IsWindow(infoPtr
->Buddy
)))
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
;
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)
281 sep
= UPDOWN_GetThousandSep();
283 /* now get rid of the separators */
284 for(src
= dst
= txt
; *src
; src
++)
285 if(*src
!= sep
) *dst
++ = *src
;
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
;
299 /***********************************************************************
301 * Tries to set the pos to the buddy window based on current pos
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
)
309 WCHAR txt
[20], txt_old
[20] = { 0 };
312 if (!((infoPtr
->Flags
& FLAG_BUDDYINT
) && IsWindow(infoPtr
->Buddy
)))
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();
333 memcpy(tmp
, txt
, sizeof(txt
));
334 if (start
== 0) start
= 3;
337 for (len
=0; *src
; len
++) {
338 if (len
% 3 == 0) *dst
++ = sep
;
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
)
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
)
368 else if (infoPtr
->dwStyle
& UDS_ALIGNRIGHT
)
370 /* FIXME: take disabled etc. into account */
371 DrawThemeBackground (buddyTheme
, hdc
, 0, 0, &br
, NULL
);
375 /***********************************************************************
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
;
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
);
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
,
410 (infoPtr
->dwStyle
& UDS_ALIGNLEFT
? BF_LEFT
: BF_RIGHT
));
414 /* Draw the incr button */
415 UPDOWN_GetArrowRect (infoPtr
, &rect
, FLAG_INCR
);
417 DrawThemeBackground(theme
, hdc
, uPart
, uState
, &rect
, NULL
);
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
);
429 DrawThemeBackground(theme
, hdc
, dPart
, dState
, &rect
, NULL
);
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) );
441 /***********************************************************************
444 * Asynchronous drawing (must ONLY be used in WM_PAINT).
447 static LRESULT
UPDOWN_Paint (const UPDOWN_INFO
*infoPtr
, HDC hdc
)
450 if (hdc
) return UPDOWN_Draw (infoPtr
, hdc
);
451 hdc
= BeginPaint (infoPtr
->Self
, &ps
);
452 UPDOWN_Draw (infoPtr
, hdc
);
453 EndPaint (infoPtr
->Self
, &ps
);
457 /***********************************************************************
460 * Handle key presses (up & down) when we have to do so
462 static LRESULT
UPDOWN_KeyPressed(UPDOWN_INFO
*infoPtr
, int key
)
466 if (key
== VK_UP
) arrow
= FLAG_INCR
;
467 else if (key
== VK_DOWN
) arrow
= FLAG_DECR
;
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
);
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
;
490 val
= infoPtr
->MaxVal
;
495 if(err
) *err
= !succ
;
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
;
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
;
519 ret
= infoPtr
->MaxVal
;
525 /***********************************************************************
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
);
546 /***********************************************************************
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
))
558 if (iWheelDelta
!= 0)
560 UPDOWN_GetBuddyInt(infoPtr
);
561 UPDOWN_DoAction(infoPtr
, abs(iWheelDelta
), iWheelDelta
> 0 ? FLAG_INCR
: FLAG_DECR
);
568 /***********************************************************************
569 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
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
);
585 UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
586 if (wParam
== VK_UP
|| wParam
== VK_DOWN
)
593 UPDOWN_MouseWheel(infoPtr
, (int)wParam
);
597 RemoveWindowSubclass(hwnd
, UPDOWN_Buddy_SubclassProc
, BUDDY_SUBCLASSID
);
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 /***********************************************************************
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];
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
;
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
) {
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
;
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
;
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
) {
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
);
706 /***********************************************************************
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
)
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
;
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 /***********************************************************************
754 * Returns TRUE if it is enabled as well as its buddy (if any)
757 static BOOL
UPDOWN_IsEnabled (const UPDOWN_INFO
*infoPtr
)
759 if (!IsWindowEnabled(infoPtr
->Self
))
762 return IsWindowEnabled(infoPtr
->Buddy
);
766 /***********************************************************************
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
)
785 infoPtr
->Flags
&= ~FLAG_PRESSED
;
786 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
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
)
805 TRACE("msg %04x point %s\n", msg
, wine_dbgstr_point(&pt
));
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
);
820 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
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);
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
);
857 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
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
);
875 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
876 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
880 ERR("Impossible case (msg=%x)!\n", msg
);
885 /***********************************************************************
888 static LRESULT WINAPI
UpDownWindowProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
890 UPDOWN_INFO
*infoPtr
= UPDOWN_GetInfoPtr (hwnd
);
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
);
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;
915 infoPtr
->MinVal
= 100;
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
);
937 Free (infoPtr
->AccelVect
);
938 UPDOWN_ResetSubclass (infoPtr
);
940 SetWindowLongPtrW (hwnd
, 0, 0);
941 theme
= GetWindowTheme (hwnd
);
942 CloseThemeData (theme
);
943 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd
);
948 infoPtr
->dwStyle
&= ~WS_DISABLED
;
950 infoPtr
->dwStyle
|= WS_DISABLED
;
951 UPDOWN_CancelMode (infoPtr
);
953 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
956 case WM_STYLECHANGED
:
957 if (wParam
== GWL_STYLE
)
958 infoPtr
->dwStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
961 case WM_THEMECHANGED
:
962 theme
= GetWindowTheme (hwnd
);
963 CloseThemeData (theme
);
964 OpenThemeData (hwnd
, L
"Spin");
965 InvalidateRect (hwnd
, NULL
, TRUE
);
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
) {
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
;
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
) {
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);
1010 return UPDOWN_CancelMode (infoPtr
);
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
),
1022 if (UPDOWN_IsBuddyEdit(infoPtr
))
1023 SendMessageW(infoPtr
->Buddy
, EM_SETSEL
, 0, MAKELONG(0, -1));
1025 UPDOWN_CancelMode(infoPtr
);
1028 case WM_LBUTTONDOWN
:
1031 if(UPDOWN_IsEnabled(infoPtr
))
1032 UPDOWN_HandleMouseEvent (infoPtr
, message
, (SHORT
)LOWORD(lParam
), (SHORT
)HIWORD(lParam
));
1036 UPDOWN_MouseWheel(infoPtr
, wParam
);
1040 if((infoPtr
->dwStyle
& UDS_ARROWKEYS
) && UPDOWN_IsEnabled(infoPtr
))
1041 return UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
1044 case WM_PRINTCLIENT
:
1046 return UPDOWN_Paint (infoPtr
, (HDC
)wParam
);
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
));
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
))
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
);
1084 return infoPtr
->Base
;
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
);
1100 return (LRESULT
)infoPtr
->Buddy
;
1103 return (LRESULT
)UPDOWN_SetBuddy (infoPtr
, (HWND
)wParam
);
1110 pos
= UPDOWN_GetPos(infoPtr
, &err
);
1111 return MAKELONG(pos
, err
);
1115 return UPDOWN_SetPos(infoPtr
, (short)LOWORD(lParam
));
1118 return MAKELONG(infoPtr
->MaxVal
, infoPtr
->MinVal
);
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
));
1128 case UDM_GETRANGE32
:
1129 if (wParam
) *(LPINT
)wParam
= infoPtr
->MinVal
;
1130 if (lParam
) *(LPINT
)lParam
= infoPtr
->MaxVal
;
1133 case UDM_SETRANGE32
:
1134 UPDOWN_SetRange(infoPtr
, (INT
)lParam
, (INT
)wParam
);
1139 return UPDOWN_GetPos(infoPtr
, (BOOL
*)lParam
);
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
;
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
);
1165 /***********************************************************************
1166 * UPDOWN_Register [Internal]
1168 * Registers the updown window class.
1170 void UPDOWN_Register(void)
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
);