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/heap.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(updown
);
44 HWND Self
; /* Handle to this up-down control */
45 HWND Notify
; /* Handle to the parent window */
46 DWORD dwStyle
; /* The GWL_STYLE for this window */
47 UINT AccelCount
; /* Number of elements in AccelVect */
48 UDACCEL
* AccelVect
; /* Vector containing AccelCount elements */
49 INT AccelIndex
; /* Current accel index, -1 if not accel'ing */
50 INT Base
; /* Base to display nr in the buddy window */
51 INT CurVal
; /* Current up-down value */
52 INT MinVal
; /* Minimum up-down value */
53 INT MaxVal
; /* Maximum up-down value */
54 HWND Buddy
; /* Handle to the buddy window */
55 INT BuddyType
; /* Remembers the buddy type BUDDY_TYPE_* */
56 INT Flags
; /* Internal Flags FLAG_* */
57 BOOL UnicodeFormat
; /* Marks the use of Unicode internally */
60 /* Control configuration constants */
62 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
63 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
64 #define REPEAT_DELAY 50 /* delay between auto-increments */
66 #define DEFAULT_WIDTH 16 /* default width of the ctrl */
67 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
68 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
69 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
70 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
71 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
72 #define DEFAULT_BUDDYBORDER_THEMED 1 /* buddy border when theming is enabled */
73 #define DEFAULT_BUDDYSPACER_THEMED 0 /* buddy spacer when theming is enabled */
77 #define FLAG_INCR 0x01
78 #define FLAG_DECR 0x02
79 #define FLAG_MOUSEIN 0x04
80 #define FLAG_PRESSED 0x08
81 #define FLAG_BUDDYINT 0x10 /* UDS_SETBUDDYINT was set on creation */
82 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
84 #define BUDDY_TYPE_UNKNOWN 0
85 #define BUDDY_TYPE_LISTBOX 1
86 #define BUDDY_TYPE_EDIT 2
88 #define TIMER_AUTOREPEAT 1
90 #define TIMER_AUTOPRESS 3
92 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
94 /* id used for SetWindowSubclass */
95 #define BUDDY_SUBCLASSID 1
97 static void UPDOWN_DoAction (UPDOWN_INFO
*infoPtr
, int delta
, int action
);
99 /***********************************************************************
101 * Tests if our buddy is an edit control.
103 static inline BOOL
UPDOWN_IsBuddyEdit(const UPDOWN_INFO
*infoPtr
)
105 return infoPtr
->BuddyType
== BUDDY_TYPE_EDIT
;
108 /***********************************************************************
109 * UPDOWN_IsBuddyListbox
110 * Tests if our buddy is a listbox control.
112 static inline BOOL
UPDOWN_IsBuddyListbox(const UPDOWN_INFO
*infoPtr
)
114 return infoPtr
->BuddyType
== BUDDY_TYPE_LISTBOX
;
117 /***********************************************************************
119 * Tests if a given value 'val' is between the Min&Max limits
121 static BOOL
UPDOWN_InBounds(const UPDOWN_INFO
*infoPtr
, int val
)
123 if(infoPtr
->MaxVal
> infoPtr
->MinVal
)
124 return (infoPtr
->MinVal
<= val
) && (val
<= infoPtr
->MaxVal
);
126 return (infoPtr
->MaxVal
<= val
) && (val
<= infoPtr
->MinVal
);
129 /***********************************************************************
131 * Change the current value by delta.
132 * It returns TRUE is the value was changed successfully, or FALSE
133 * if the value was not changed, as it would go out of bounds.
135 static BOOL
UPDOWN_OffsetVal(UPDOWN_INFO
*infoPtr
, int delta
)
137 /* check if we can do the modification first */
138 if(!UPDOWN_InBounds (infoPtr
, infoPtr
->CurVal
+delta
)) {
139 if (infoPtr
->dwStyle
& UDS_WRAP
) {
140 delta
+= (delta
< 0 ? -1 : 1) *
141 (infoPtr
->MaxVal
< infoPtr
->MinVal
? -1 : 1) *
142 (infoPtr
->MinVal
- infoPtr
->MaxVal
) +
143 (delta
< 0 ? 1 : -1);
144 } else if ((infoPtr
->MaxVal
> infoPtr
->MinVal
&& infoPtr
->CurVal
+delta
> infoPtr
->MaxVal
)
145 || (infoPtr
->MaxVal
< infoPtr
->MinVal
&& infoPtr
->CurVal
+delta
< infoPtr
->MaxVal
)) {
146 delta
= infoPtr
->MaxVal
- infoPtr
->CurVal
;
148 delta
= infoPtr
->MinVal
- infoPtr
->CurVal
;
152 infoPtr
->CurVal
+= delta
;
156 /***********************************************************************
157 * UPDOWN_HasBuddyBorder
159 * When we have a buddy set and that we are aligned on our buddy, we
160 * want to draw a sunken edge to make like we are part of that control.
162 static BOOL
UPDOWN_HasBuddyBorder(const UPDOWN_INFO
*infoPtr
)
164 return ( ((infoPtr
->dwStyle
& (UDS_ALIGNLEFT
| UDS_ALIGNRIGHT
)) != 0) &&
165 UPDOWN_IsBuddyEdit(infoPtr
) );
168 /***********************************************************************
169 * UPDOWN_GetArrowRect
170 * wndPtr - pointer to the up-down wnd
171 * rect - will hold the rectangle
172 * arrow - FLAG_INCR to get the "increment" rect (up or right)
173 * FLAG_DECR to get the "decrement" rect (down or left)
175 static void UPDOWN_GetArrowRect (const UPDOWN_INFO
* infoPtr
, RECT
*rect
, unsigned int arrow
)
177 HTHEME theme
= GetWindowTheme (infoPtr
->Self
);
178 const int border
= theme
? DEFAULT_BUDDYBORDER_THEMED
: DEFAULT_BUDDYBORDER
;
179 const int spacer
= theme
? DEFAULT_BUDDYSPACER_THEMED
: DEFAULT_BUDDYSPACER
;
182 assert(arrow
&& (arrow
& (FLAG_INCR
| FLAG_DECR
)) != (FLAG_INCR
| FLAG_DECR
));
184 GetClientRect (infoPtr
->Self
, rect
);
187 * Make sure we calculate the rectangle to fit even if we draw the
190 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
191 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
)
192 rect
->left
+= border
;
194 rect
->right
-= border
;
196 InflateRect(rect
, 0, -border
);
199 /* now figure out if we need a space away from the buddy */
200 if (IsWindow(infoPtr
->Buddy
) ) {
201 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
) rect
->right
-= spacer
;
202 else if (infoPtr
->dwStyle
& UDS_ALIGNRIGHT
) rect
->left
+= spacer
;
206 * We're calculating the midpoint to figure-out where the
207 * separation between the buttons will lay.
209 if (infoPtr
->dwStyle
& UDS_HORZ
) {
210 size
= (rect
->right
- rect
->left
) / 2;
211 if (arrow
& FLAG_INCR
)
212 rect
->left
= rect
->right
- size
;
213 else if (arrow
& FLAG_DECR
)
214 rect
->right
= rect
->left
+ size
;
216 size
= (rect
->bottom
- rect
->top
) / 2;
217 if (arrow
& FLAG_INCR
)
218 rect
->bottom
= rect
->top
+ size
;
219 else if (arrow
& FLAG_DECR
)
220 rect
->top
= rect
->bottom
- size
;
224 /***********************************************************************
225 * UPDOWN_GetArrowFromPoint
226 * Returns the rectagle (for the up or down arrow) that contains pt.
227 * If it returns the up rect, it returns FLAG_INCR.
228 * If it returns the down rect, it returns FLAG_DECR.
230 static INT
UPDOWN_GetArrowFromPoint (const UPDOWN_INFO
*infoPtr
, RECT
*rect
, POINT pt
)
232 UPDOWN_GetArrowRect (infoPtr
, rect
, FLAG_INCR
);
233 if(PtInRect(rect
, pt
)) return FLAG_INCR
;
235 UPDOWN_GetArrowRect (infoPtr
, rect
, FLAG_DECR
);
236 if(PtInRect(rect
, pt
)) return FLAG_DECR
;
242 /***********************************************************************
243 * UPDOWN_GetThousandSep
244 * Returns the thousand sep. If an error occurs, it returns ','.
246 static WCHAR
UPDOWN_GetThousandSep(void)
250 if(GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_STHOUSAND
, sep
, 2) != 1)
256 /***********************************************************************
258 * Tries to read the pos from the buddy window and if it succeeds,
259 * it stores it in the control's CurVal
261 * TRUE - if it read the integer from the buddy successfully
262 * FALSE - if an error occurred
264 static BOOL
UPDOWN_GetBuddyInt (UPDOWN_INFO
*infoPtr
)
266 WCHAR txt
[20], sep
, *src
, *dst
;
269 if (!((infoPtr
->Flags
& FLAG_BUDDYINT
) && IsWindow(infoPtr
->Buddy
)))
272 /*if the buddy is a list window, we must set curr index */
273 if (UPDOWN_IsBuddyListbox(infoPtr
)) {
274 newVal
= SendMessageW(infoPtr
->Buddy
, LB_GETCARETINDEX
, 0, 0);
275 if(newVal
< 0) return FALSE
;
277 /* we have a regular window, so will get the text */
278 /* note that a zero-length string is a legitimate value for 'txt',
279 * and ought to result in a successful conversion to '0'. */
280 if (GetWindowTextW(infoPtr
->Buddy
, txt
, ARRAY_SIZE(txt
)) < 0)
283 sep
= UPDOWN_GetThousandSep();
285 /* now get rid of the separators */
286 for(src
= dst
= txt
; *src
; src
++)
287 if(*src
!= sep
) *dst
++ = *src
;
290 /* try to convert the number and validate it */
291 newVal
= strtolW(txt
, &src
, infoPtr
->Base
);
292 if(*src
|| !UPDOWN_InBounds (infoPtr
, newVal
)) return FALSE
;
295 TRACE("new value(%d) from buddy (old=%d)\n", newVal
, infoPtr
->CurVal
);
296 infoPtr
->CurVal
= newVal
;
301 /***********************************************************************
303 * Tries to set the pos to the buddy window based on current pos
305 * TRUE - if it set the caption of the buddy successfully
306 * FALSE - if an error occurred
308 static BOOL
UPDOWN_SetBuddyInt (const UPDOWN_INFO
*infoPtr
)
310 static const WCHAR fmt_hex
[] = { '0', 'x', '%', '0', '4', 'X', 0 };
311 static const WCHAR fmt_dec_oct
[] = { '%', 'd', '\0' };
313 WCHAR txt
[20], txt_old
[20] = { 0 };
316 if (!((infoPtr
->Flags
& FLAG_BUDDYINT
) && IsWindow(infoPtr
->Buddy
)))
319 TRACE("set new value(%d) to buddy.\n", infoPtr
->CurVal
);
321 /*if the buddy is a list window, we must set curr index */
322 if (UPDOWN_IsBuddyListbox(infoPtr
)) {
323 return SendMessageW(infoPtr
->Buddy
, LB_SETCURSEL
, infoPtr
->CurVal
, 0) != LB_ERR
;
326 /* Regular window, so set caption to the number */
327 fmt
= (infoPtr
->Base
== 16) ? fmt_hex
: fmt_dec_oct
;
328 len
= wsprintfW(txt
, fmt
, infoPtr
->CurVal
);
331 /* Do thousands separation if necessary */
332 if ((infoPtr
->Base
== 10) && !(infoPtr
->dwStyle
& UDS_NOTHOUSANDS
) && (len
> 3)) {
333 WCHAR tmp
[ARRAY_SIZE(txt
)], *src
= tmp
, *dst
= txt
;
334 WCHAR sep
= UPDOWN_GetThousandSep();
337 memcpy(tmp
, txt
, sizeof(txt
));
338 if (start
== 0) start
= 3;
341 for (len
=0; *src
; len
++) {
342 if (len
% 3 == 0) *dst
++ = sep
;
348 /* if nothing changed exit earlier */
349 GetWindowTextW(infoPtr
->Buddy
, txt_old
, ARRAY_SIZE(txt_old
));
350 if (lstrcmpiW(txt_old
, txt
) == 0) return FALSE
;
352 return SetWindowTextW(infoPtr
->Buddy
, txt
);
355 /***********************************************************************
356 * UPDOWN_DrawBuddyBackground
358 * Draw buddy background for visual integration.
360 static BOOL
UPDOWN_DrawBuddyBackground (const UPDOWN_INFO
*infoPtr
, HDC hdc
)
363 HTHEME buddyTheme
= GetWindowTheme (infoPtr
->Buddy
);
364 if (!buddyTheme
) return FALSE
;
366 GetWindowRect (infoPtr
->Buddy
, &br
);
367 MapWindowPoints (NULL
, infoPtr
->Self
, (POINT
*)&br
, 2);
368 GetClientRect (infoPtr
->Self
, &r
);
370 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
)
372 else if (infoPtr
->dwStyle
& UDS_ALIGNRIGHT
)
374 /* FIXME: take disabled etc. into account */
375 DrawThemeBackground (buddyTheme
, hdc
, 0, 0, &br
, NULL
);
379 /***********************************************************************
382 * Draw the arrows. The background need not be erased.
384 static LRESULT
UPDOWN_Draw (const UPDOWN_INFO
*infoPtr
, HDC hdc
)
386 BOOL uPressed
, uHot
, dPressed
, dHot
;
388 HTHEME theme
= GetWindowTheme (infoPtr
->Self
);
389 int uPart
= 0, uState
= 0, dPart
= 0, dState
= 0;
390 BOOL needBuddyBg
= FALSE
;
392 uPressed
= (infoPtr
->Flags
& FLAG_PRESSED
) && (infoPtr
->Flags
& FLAG_INCR
);
393 uHot
= (infoPtr
->Flags
& FLAG_INCR
) && (infoPtr
->Flags
& FLAG_MOUSEIN
);
394 dPressed
= (infoPtr
->Flags
& FLAG_PRESSED
) && (infoPtr
->Flags
& FLAG_DECR
);
395 dHot
= (infoPtr
->Flags
& FLAG_DECR
) && (infoPtr
->Flags
& FLAG_MOUSEIN
);
397 uPart
= (infoPtr
->dwStyle
& UDS_HORZ
) ? SPNP_UPHORZ
: SPNP_UP
;
398 uState
= (infoPtr
->dwStyle
& WS_DISABLED
) ? DNS_DISABLED
399 : (uPressed
? DNS_PRESSED
: (uHot
? DNS_HOT
: DNS_NORMAL
));
400 dPart
= (infoPtr
->dwStyle
& UDS_HORZ
) ? SPNP_DOWNHORZ
: SPNP_DOWN
;
401 dState
= (infoPtr
->dwStyle
& WS_DISABLED
) ? DNS_DISABLED
402 : (dPressed
? DNS_PRESSED
: (dHot
? DNS_HOT
: DNS_NORMAL
));
403 needBuddyBg
= IsWindow (infoPtr
->Buddy
)
404 && (IsThemeBackgroundPartiallyTransparent (theme
, uPart
, uState
)
405 || IsThemeBackgroundPartiallyTransparent (theme
, dPart
, dState
));
408 /* Draw the common border between ourselves and our buddy */
409 if (UPDOWN_HasBuddyBorder(infoPtr
) || needBuddyBg
) {
410 if (!theme
|| !UPDOWN_DrawBuddyBackground (infoPtr
, hdc
)) {
411 GetClientRect(infoPtr
->Self
, &rect
);
412 DrawEdge(hdc
, &rect
, EDGE_SUNKEN
,
414 (infoPtr
->dwStyle
& UDS_ALIGNLEFT
? BF_LEFT
: BF_RIGHT
));
418 /* Draw the incr button */
419 UPDOWN_GetArrowRect (infoPtr
, &rect
, FLAG_INCR
);
421 DrawThemeBackground(theme
, hdc
, uPart
, uState
, &rect
, NULL
);
423 DrawFrameControl(hdc
, &rect
, DFC_SCROLL
,
424 (infoPtr
->dwStyle
& UDS_HORZ
? DFCS_SCROLLRIGHT
: DFCS_SCROLLUP
) |
425 ((infoPtr
->dwStyle
& UDS_HOTTRACK
) && uHot
? DFCS_HOT
: 0) |
426 (uPressed
? DFCS_PUSHED
: 0) |
427 (infoPtr
->dwStyle
& WS_DISABLED
? DFCS_INACTIVE
: 0) );
430 /* Draw the decr button */
431 UPDOWN_GetArrowRect(infoPtr
, &rect
, FLAG_DECR
);
433 DrawThemeBackground(theme
, hdc
, dPart
, dState
, &rect
, NULL
);
435 DrawFrameControl(hdc
, &rect
, DFC_SCROLL
,
436 (infoPtr
->dwStyle
& UDS_HORZ
? DFCS_SCROLLLEFT
: DFCS_SCROLLDOWN
) |
437 ((infoPtr
->dwStyle
& UDS_HOTTRACK
) && dHot
? DFCS_HOT
: 0) |
438 (dPressed
? DFCS_PUSHED
: 0) |
439 (infoPtr
->dwStyle
& WS_DISABLED
? DFCS_INACTIVE
: 0) );
445 /***********************************************************************
448 * Asynchronous drawing (must ONLY be used in WM_PAINT).
451 static LRESULT
UPDOWN_Paint (const UPDOWN_INFO
*infoPtr
, HDC hdc
)
454 if (hdc
) return UPDOWN_Draw (infoPtr
, hdc
);
455 hdc
= BeginPaint (infoPtr
->Self
, &ps
);
456 UPDOWN_Draw (infoPtr
, hdc
);
457 EndPaint (infoPtr
->Self
, &ps
);
461 /***********************************************************************
464 * Handle key presses (up & down) when we have to do so
466 static LRESULT
UPDOWN_KeyPressed(UPDOWN_INFO
*infoPtr
, int key
)
470 if (key
== VK_UP
) arrow
= FLAG_INCR
;
471 else if (key
== VK_DOWN
) arrow
= FLAG_DECR
;
474 UPDOWN_GetBuddyInt (infoPtr
);
475 infoPtr
->Flags
&= ~FLAG_ARROW
;
476 infoPtr
->Flags
|= FLAG_PRESSED
| arrow
;
477 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
478 SetTimer(infoPtr
->Self
, TIMER_AUTOPRESS
, AUTOPRESS_DELAY
, 0);
479 accel
= (infoPtr
->AccelCount
&& infoPtr
->AccelVect
) ? infoPtr
->AccelVect
[0].nInc
: 1;
480 UPDOWN_DoAction (infoPtr
, accel
, arrow
);
484 static int UPDOWN_GetPos(UPDOWN_INFO
*infoPtr
, BOOL
*err
)
486 BOOL succ
= UPDOWN_GetBuddyInt(infoPtr
);
487 int val
= infoPtr
->CurVal
;
489 if(!UPDOWN_InBounds(infoPtr
, val
)) {
490 if((infoPtr
->MinVal
< infoPtr
->MaxVal
&& val
< infoPtr
->MinVal
)
491 || (infoPtr
->MinVal
> infoPtr
->MaxVal
&& val
> infoPtr
->MinVal
))
492 val
= infoPtr
->MinVal
;
494 val
= infoPtr
->MaxVal
;
499 if(err
) *err
= !succ
;
503 static int UPDOWN_SetPos(UPDOWN_INFO
*infoPtr
, int pos
)
505 int ret
= infoPtr
->CurVal
;
507 if(!UPDOWN_InBounds(infoPtr
, pos
)) {
508 if((infoPtr
->MinVal
< infoPtr
->MaxVal
&& pos
< infoPtr
->MinVal
)
509 || (infoPtr
->MinVal
> infoPtr
->MaxVal
&& pos
> infoPtr
->MinVal
))
510 pos
= infoPtr
->MinVal
;
512 pos
= infoPtr
->MaxVal
;
515 infoPtr
->CurVal
= pos
;
516 UPDOWN_SetBuddyInt(infoPtr
);
518 if(!UPDOWN_InBounds(infoPtr
, ret
)) {
519 if((infoPtr
->MinVal
< infoPtr
->MaxVal
&& ret
< infoPtr
->MinVal
)
520 || (infoPtr
->MinVal
> infoPtr
->MaxVal
&& ret
> infoPtr
->MinVal
))
521 ret
= infoPtr
->MinVal
;
523 ret
= infoPtr
->MaxVal
;
529 /***********************************************************************
532 * Handle UDM_SETRANGE, UDM_SETRANGE32
534 * FIXME: handle Max == Min properly:
535 * - arrows should be disabled (without WS_DISABLED set),
536 * visually they can't be pressed and don't respond;
537 * - all input messages should still pass in.
539 static LRESULT
UPDOWN_SetRange(UPDOWN_INFO
*infoPtr
, INT Max
, INT Min
)
541 infoPtr
->MaxVal
= Max
;
542 infoPtr
->MinVal
= Min
;
544 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
545 infoPtr
->MinVal
, infoPtr
->MaxVal
, infoPtr
->Self
);
550 /***********************************************************************
553 * Handle mouse wheel scrolling
555 static LRESULT
UPDOWN_MouseWheel(UPDOWN_INFO
*infoPtr
, WPARAM wParam
)
557 int iWheelDelta
= GET_WHEEL_DELTA_WPARAM(wParam
) / WHEEL_DELTA
;
559 if (wParam
& (MK_SHIFT
| MK_CONTROL
))
562 if (iWheelDelta
!= 0)
564 UPDOWN_GetBuddyInt(infoPtr
);
565 UPDOWN_DoAction(infoPtr
, abs(iWheelDelta
), iWheelDelta
> 0 ? FLAG_INCR
: FLAG_DECR
);
572 /***********************************************************************
573 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
576 static LRESULT CALLBACK
577 UPDOWN_Buddy_SubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
,
578 UINT_PTR uId
, DWORD_PTR ref_data
)
580 UPDOWN_INFO
*infoPtr
= UPDOWN_GetInfoPtr((HWND
)ref_data
);
582 TRACE("hwnd=%p, uMsg=%04x, wParam=%08lx, lParam=%08lx\n",
583 hwnd
, uMsg
, wParam
, lParam
);
590 UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
591 if (wParam
== VK_UP
|| wParam
== VK_DOWN
)
598 UPDOWN_MouseWheel(infoPtr
, (int)wParam
);
602 RemoveWindowSubclass(hwnd
, UPDOWN_Buddy_SubclassProc
, BUDDY_SUBCLASSID
);
608 return DefSubclassProc(hwnd
, uMsg
, wParam
, lParam
);
611 static void UPDOWN_ResetSubclass (UPDOWN_INFO
*infoPtr
)
613 SetWindowSubclass(infoPtr
->Buddy
, UPDOWN_Buddy_SubclassProc
, BUDDY_SUBCLASSID
, 0);
616 /***********************************************************************
619 * Sets bud as a new Buddy.
620 * Then, it should subclass the buddy
621 * If window has the UDS_ARROWKEYS, it subclasses the buddy window to
622 * process the UP/DOWN arrow keys.
623 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
624 * the size/pos of the buddy and the control are adjusted accordingly.
626 static HWND
UPDOWN_SetBuddy (UPDOWN_INFO
* infoPtr
, HWND bud
)
628 RECT budRect
; /* new coord for the buddy */
629 int x
, width
; /* new x position and width for the up-down */
630 WCHAR buddyClass
[40];
633 TRACE("(hwnd=%p, bud=%p)\n", infoPtr
->Self
, bud
);
635 old_buddy
= infoPtr
->Buddy
;
637 UPDOWN_ResetSubclass (infoPtr
);
639 if (!IsWindow(bud
)) bud
= NULL
;
641 /* Store buddy window handle */
642 infoPtr
->Buddy
= bud
;
645 /* Store buddy window class type */
646 infoPtr
->BuddyType
= BUDDY_TYPE_UNKNOWN
;
647 if (GetClassNameW(bud
, buddyClass
, ARRAY_SIZE(buddyClass
))) {
648 if (lstrcmpiW(buddyClass
, WC_EDITW
) == 0)
649 infoPtr
->BuddyType
= BUDDY_TYPE_EDIT
;
650 else if (lstrcmpiW(buddyClass
, WC_LISTBOXW
) == 0)
651 infoPtr
->BuddyType
= BUDDY_TYPE_LISTBOX
;
654 if (infoPtr
->dwStyle
& UDS_ARROWKEYS
)
655 SetWindowSubclass(bud
, UPDOWN_Buddy_SubclassProc
, BUDDY_SUBCLASSID
,
656 (DWORD_PTR
)infoPtr
->Self
);
658 /* Get the rect of the buddy relative to its parent */
659 GetWindowRect(infoPtr
->Buddy
, &budRect
);
660 MapWindowPoints(HWND_DESKTOP
, GetParent(infoPtr
->Buddy
), (POINT
*)(&budRect
.left
), 2);
662 /* now do the positioning */
663 if (infoPtr
->dwStyle
& UDS_ALIGNLEFT
) {
665 budRect
.left
+= DEFAULT_WIDTH
+ DEFAULT_XSEP
;
666 } else if (infoPtr
->dwStyle
& UDS_ALIGNRIGHT
) {
667 budRect
.right
-= DEFAULT_WIDTH
+ DEFAULT_XSEP
;
668 x
= budRect
.right
+DEFAULT_XSEP
;
674 /* first adjust the buddy to accommodate the up/down */
675 SetWindowPos(infoPtr
->Buddy
, 0, budRect
.left
, budRect
.top
,
676 budRect
.right
- budRect
.left
, budRect
.bottom
- budRect
.top
,
677 SWP_NOACTIVATE
|SWP_NOZORDER
);
679 /* now position the up/down */
680 /* Since the UDS_ALIGN* flags were used, */
681 /* we will pick the position and size of the window. */
682 width
= DEFAULT_WIDTH
;
685 * If the updown has a buddy border, it has to overlap with the buddy
686 * to look as if it is integrated with the buddy control.
687 * We nudge the control or change its size to overlap.
689 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
690 if(infoPtr
->dwStyle
& UDS_ALIGNLEFT
)
691 width
+= DEFAULT_BUDDYBORDER
;
693 x
-= DEFAULT_BUDDYBORDER
;
696 SetWindowPos(infoPtr
->Self
, 0, x
,
697 budRect
.top
- DEFAULT_ADDTOP
, width
,
698 budRect
.bottom
- budRect
.top
+ DEFAULT_ADDTOP
+ DEFAULT_ADDBOT
,
699 SWP_NOACTIVATE
|SWP_FRAMECHANGED
|SWP_NOZORDER
);
700 } else if (!(infoPtr
->dwStyle
& UDS_HORZ
) && old_buddy
!= NULL
) {
702 GetWindowRect(infoPtr
->Self
, &rect
);
703 MapWindowPoints(HWND_DESKTOP
, GetParent(infoPtr
->Self
), (POINT
*)&rect
, 2);
704 SetWindowPos(infoPtr
->Self
, 0, rect
.left
, rect
.top
, DEFAULT_WIDTH
, rect
.bottom
- rect
.top
,
705 SWP_NOACTIVATE
|SWP_FRAMECHANGED
|SWP_NOZORDER
);
711 /***********************************************************************
714 * This function increments/decrements the CurVal by the
715 * 'delta' amount according to the 'action' flag which can be a
716 * combination of FLAG_INCR and FLAG_DECR
717 * It notifies the parent as required.
718 * It handles wrapping and non-wrapping correctly.
719 * It is assumed that delta>0
721 static void UPDOWN_DoAction (UPDOWN_INFO
*infoPtr
, int delta
, int action
)
725 TRACE("%d by %d\n", action
, delta
);
727 /* check if we can do the modification first */
728 delta
*= (action
& FLAG_INCR
? 1 : -1) * (infoPtr
->MaxVal
< infoPtr
->MinVal
? -1 : 1);
729 if ( (action
& FLAG_INCR
) && (action
& FLAG_DECR
) ) delta
= 0;
731 TRACE("current %d, delta: %d\n", infoPtr
->CurVal
, delta
);
733 /* We must notify parent now to obtain permission */
734 ni
.iPos
= infoPtr
->CurVal
;
736 ni
.hdr
.hwndFrom
= infoPtr
->Self
;
737 ni
.hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
738 ni
.hdr
.code
= UDN_DELTAPOS
;
739 if (!SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, ni
.hdr
.idFrom
, (LPARAM
)&ni
)) {
740 /* Parent said: OK to adjust */
742 /* Now adjust value with (maybe new) delta */
743 if (UPDOWN_OffsetVal (infoPtr
, ni
.iDelta
)) {
744 TRACE("new %d, delta: %d\n", infoPtr
->CurVal
, ni
.iDelta
);
746 /* Now take care about our buddy */
747 UPDOWN_SetBuddyInt (infoPtr
);
751 /* Also, notify it. This message is sent in any case. */
752 SendMessageW( infoPtr
->Notify
, (infoPtr
->dwStyle
& UDS_HORZ
) ? WM_HSCROLL
: WM_VSCROLL
,
753 MAKELONG(SB_THUMBPOSITION
, infoPtr
->CurVal
), (LPARAM
)infoPtr
->Self
);
756 /***********************************************************************
759 * Returns TRUE if it is enabled as well as its buddy (if any)
762 static BOOL
UPDOWN_IsEnabled (const UPDOWN_INFO
*infoPtr
)
764 if (!IsWindowEnabled(infoPtr
->Self
))
767 return IsWindowEnabled(infoPtr
->Buddy
);
771 /***********************************************************************
774 * Deletes any timers, releases the mouse and does redraw if necessary.
775 * If the control is not in "capture" mode, it does nothing.
776 * If the control was not in cancel mode, it returns FALSE.
777 * If the control was in cancel mode, it returns TRUE.
779 static BOOL
UPDOWN_CancelMode (UPDOWN_INFO
*infoPtr
)
781 if (!(infoPtr
->Flags
& FLAG_PRESSED
)) return FALSE
;
783 KillTimer (infoPtr
->Self
, TIMER_AUTOREPEAT
);
784 KillTimer (infoPtr
->Self
, TIMER_ACCEL
);
785 KillTimer (infoPtr
->Self
, TIMER_AUTOPRESS
);
787 if (GetCapture() == infoPtr
->Self
) {
789 hdr
.hwndFrom
= infoPtr
->Self
;
790 hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
791 hdr
.code
= NM_RELEASEDCAPTURE
;
792 SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, hdr
.idFrom
, (LPARAM
)&hdr
);
796 infoPtr
->Flags
&= ~FLAG_PRESSED
;
797 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
802 /***********************************************************************
803 * UPDOWN_HandleMouseEvent
805 * Handle a mouse event for the updown.
806 * 'pt' is the location of the mouse event in client or
807 * windows coordinates.
809 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO
*infoPtr
, UINT msg
, INT x
, INT y
)
816 TRACE("msg %04x point %s\n", msg
, wine_dbgstr_point(&pt
));
820 case WM_LBUTTONDOWN
: /* Initialise mouse tracking */
822 /* If the buddy is an edit, will set focus to it */
823 if (UPDOWN_IsBuddyEdit(infoPtr
)) SetFocus(infoPtr
->Buddy
);
825 /* Now see which one is the 'active' arrow */
826 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
828 /* Update the flags if we are in/out */
829 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
831 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
833 if (infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
835 if (infoPtr
->Flags
& FLAG_ARROW
) {
837 /* Update the CurVal if necessary */
838 UPDOWN_GetBuddyInt (infoPtr
);
840 /* Set up the correct flags */
841 infoPtr
->Flags
|= FLAG_PRESSED
;
843 /* repaint the control */
844 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
846 /* process the click */
847 temp
= (infoPtr
->AccelCount
&& infoPtr
->AccelVect
) ? infoPtr
->AccelVect
[0].nInc
: 1;
848 UPDOWN_DoAction (infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
850 /* now capture all mouse messages */
851 SetCapture (infoPtr
->Self
);
853 /* and startup the first timer */
854 SetTimer(infoPtr
->Self
, TIMER_AUTOREPEAT
, INITIAL_DELAY
, 0);
859 /* save the flags to see if any got modified */
860 temp
= infoPtr
->Flags
;
862 /* Now see which one is the 'active' arrow */
863 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
865 /* Update the flags if we are in/out */
866 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
868 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
870 if(infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
873 /* If state changed, redraw the control */
874 if(temp
!= infoPtr
->Flags
)
875 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
877 /* Set up tracking so the mousein flags can be reset when the
878 * mouse leaves the control */
879 tme
.cbSize
= sizeof( tme
);
880 tme
.dwFlags
= TME_LEAVE
;
881 tme
.hwndTrack
= infoPtr
->Self
;
882 TrackMouseEvent (&tme
);
886 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
887 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
891 ERR("Impossible case (msg=%x)!\n", msg
);
896 /***********************************************************************
899 static LRESULT WINAPI
UpDownWindowProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
901 UPDOWN_INFO
*infoPtr
= UPDOWN_GetInfoPtr (hwnd
);
902 static const WCHAR themeClass
[] = {'S','p','i','n',0};
905 TRACE("hwnd=%p msg=%04x wparam=%08lx lparam=%08lx\n", hwnd
, message
, wParam
, lParam
);
907 if (!infoPtr
&& (message
!= WM_CREATE
))
908 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
914 CREATESTRUCTW
*pcs
= (CREATESTRUCTW
*)lParam
;
916 infoPtr
= heap_alloc_zero(sizeof(*infoPtr
));
917 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
919 /* initialize the info struct */
920 infoPtr
->Self
= hwnd
;
921 infoPtr
->Notify
= pcs
->hwndParent
;
922 infoPtr
->dwStyle
= pcs
->style
;
923 infoPtr
->AccelCount
= 0;
924 infoPtr
->AccelVect
= 0;
925 infoPtr
->AccelIndex
= -1;
927 infoPtr
->MinVal
= 100;
929 infoPtr
->Base
= 10; /* Default to base 10 */
930 infoPtr
->Buddy
= 0; /* No buddy window yet */
931 infoPtr
->Flags
= (infoPtr
->dwStyle
& UDS_SETBUDDYINT
) ? FLAG_BUDDYINT
: 0;
933 SetWindowLongW (hwnd
, GWL_STYLE
, infoPtr
->dwStyle
& ~WS_BORDER
);
934 if (!(infoPtr
->dwStyle
& UDS_HORZ
))
935 SetWindowPos (hwnd
, NULL
, 0, 0, DEFAULT_WIDTH
, pcs
->cy
,
936 SWP_NOOWNERZORDER
| SWP_NOZORDER
| SWP_NOMOVE
);
938 /* Do we pick the buddy win ourselves? */
939 if (infoPtr
->dwStyle
& UDS_AUTOBUDDY
)
940 UPDOWN_SetBuddy (infoPtr
, GetWindow (hwnd
, GW_HWNDPREV
));
942 OpenThemeData (hwnd
, themeClass
);
944 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd
);
949 heap_free (infoPtr
->AccelVect
);
950 UPDOWN_ResetSubclass (infoPtr
);
952 SetWindowLongPtrW (hwnd
, 0, 0);
953 theme
= GetWindowTheme (hwnd
);
954 CloseThemeData (theme
);
955 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd
);
960 infoPtr
->dwStyle
&= ~WS_DISABLED
;
962 infoPtr
->dwStyle
|= WS_DISABLED
;
963 UPDOWN_CancelMode (infoPtr
);
965 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
968 case WM_STYLECHANGED
:
969 if (wParam
== GWL_STYLE
) {
970 infoPtr
->dwStyle
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
971 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
975 case WM_THEMECHANGED
:
976 theme
= GetWindowTheme (hwnd
);
977 CloseThemeData (theme
);
978 OpenThemeData (hwnd
, themeClass
);
979 InvalidateRect (hwnd
, NULL
, FALSE
);
983 /* is this the auto-press timer? */
984 if(wParam
== TIMER_AUTOPRESS
) {
985 KillTimer(hwnd
, TIMER_AUTOPRESS
);
986 infoPtr
->Flags
&= ~(FLAG_PRESSED
| FLAG_ARROW
);
987 InvalidateRect(infoPtr
->Self
, NULL
, FALSE
);
990 /* if initial timer, kill it and start the repeat timer */
991 if(wParam
== TIMER_AUTOREPEAT
) {
994 KillTimer(hwnd
, TIMER_AUTOREPEAT
);
995 /* if no accel info given, used default timer */
996 if(infoPtr
->AccelCount
==0 || infoPtr
->AccelVect
==0) {
997 infoPtr
->AccelIndex
= -1;
998 delay
= REPEAT_DELAY
;
1000 infoPtr
->AccelIndex
= 0; /* otherwise, use it */
1001 delay
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
1003 SetTimer(hwnd
, TIMER_ACCEL
, delay
, 0);
1006 /* now, if the mouse is above us, do the thing...*/
1007 if(infoPtr
->Flags
& FLAG_MOUSEIN
) {
1010 temp
= infoPtr
->AccelIndex
== -1 ? 1 : infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nInc
;
1011 UPDOWN_DoAction(infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
1013 if(infoPtr
->AccelIndex
!= -1 && infoPtr
->AccelIndex
< infoPtr
->AccelCount
-1) {
1014 KillTimer(hwnd
, TIMER_ACCEL
);
1015 infoPtr
->AccelIndex
++; /* move to the next accel info */
1016 temp
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
1017 /* make sure we have at least 1ms intervals */
1018 SetTimer(hwnd
, TIMER_ACCEL
, temp
, 0);
1024 return UPDOWN_CancelMode (infoPtr
);
1027 if (GetCapture() != infoPtr
->Self
) break;
1029 if ( (infoPtr
->Flags
& FLAG_MOUSEIN
) &&
1030 (infoPtr
->Flags
& FLAG_ARROW
) ) {
1032 SendMessageW( infoPtr
->Notify
,
1033 (infoPtr
->dwStyle
& UDS_HORZ
) ? WM_HSCROLL
: WM_VSCROLL
,
1034 MAKELONG(SB_ENDSCROLL
, infoPtr
->CurVal
),
1036 if (UPDOWN_IsBuddyEdit(infoPtr
))
1037 SendMessageW(infoPtr
->Buddy
, EM_SETSEL
, 0, MAKELONG(0, -1));
1039 UPDOWN_CancelMode(infoPtr
);
1042 case WM_LBUTTONDOWN
:
1045 if(UPDOWN_IsEnabled(infoPtr
))
1046 UPDOWN_HandleMouseEvent (infoPtr
, message
, (SHORT
)LOWORD(lParam
), (SHORT
)HIWORD(lParam
));
1050 UPDOWN_MouseWheel(infoPtr
, wParam
);
1054 if((infoPtr
->dwStyle
& UDS_ARROWKEYS
) && UPDOWN_IsEnabled(infoPtr
))
1055 return UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
1058 case WM_PRINTCLIENT
:
1060 return UPDOWN_Paint (infoPtr
, (HDC
)wParam
);
1063 if (wParam
==0 && lParam
==0) return infoPtr
->AccelCount
;
1064 if (wParam
&& lParam
) {
1065 int temp
= min(infoPtr
->AccelCount
, wParam
);
1066 memcpy((void *)lParam
, infoPtr
->AccelVect
, temp
*sizeof(UDACCEL
));
1073 TRACE("UDM_SETACCEL\n");
1075 if(infoPtr
->AccelVect
) {
1076 heap_free (infoPtr
->AccelVect
);
1077 infoPtr
->AccelCount
= 0;
1078 infoPtr
->AccelVect
= 0;
1080 if(wParam
==0) return TRUE
;
1081 infoPtr
->AccelVect
= heap_alloc(wParam
*sizeof(UDACCEL
));
1082 if(!infoPtr
->AccelVect
) return FALSE
;
1083 memcpy(infoPtr
->AccelVect
, (void*)lParam
, wParam
*sizeof(UDACCEL
));
1084 infoPtr
->AccelCount
= wParam
;
1086 if (TRACE_ON(updown
))
1090 for (i
= 0; i
< wParam
; i
++)
1091 TRACE("%u: nSec %u nInc %u\n", i
,
1092 infoPtr
->AccelVect
[i
].nSec
, infoPtr
->AccelVect
[i
].nInc
);
1098 return infoPtr
->Base
;
1101 TRACE("UpDown Ctrl new base(%ld), hwnd=%p\n", wParam
, hwnd
);
1102 if (wParam
==10 || wParam
==16) {
1103 WPARAM old_base
= infoPtr
->Base
;
1104 infoPtr
->Base
= wParam
;
1106 if (old_base
!= infoPtr
->Base
)
1107 UPDOWN_SetBuddyInt(infoPtr
);
1114 return (LRESULT
)infoPtr
->Buddy
;
1117 return (LRESULT
)UPDOWN_SetBuddy (infoPtr
, (HWND
)wParam
);
1124 pos
= UPDOWN_GetPos(infoPtr
, &err
);
1125 return MAKELONG(pos
, err
);
1129 return UPDOWN_SetPos(infoPtr
, (short)LOWORD(lParam
));
1132 return MAKELONG(infoPtr
->MaxVal
, infoPtr
->MinVal
);
1136 UD_MINVAL <= Max <= UD_MAXVAL
1137 UD_MINVAL <= Min <= UD_MAXVAL
1138 |Max-Min| <= UD_MAXVAL */
1139 UPDOWN_SetRange(infoPtr
, (short)lParam
, (short)HIWORD(lParam
));
1142 case UDM_GETRANGE32
:
1143 if (wParam
) *(LPINT
)wParam
= infoPtr
->MinVal
;
1144 if (lParam
) *(LPINT
)lParam
= infoPtr
->MaxVal
;
1147 case UDM_SETRANGE32
:
1148 UPDOWN_SetRange(infoPtr
, (INT
)lParam
, (INT
)wParam
);
1153 return UPDOWN_GetPos(infoPtr
, (BOOL
*)lParam
);
1157 return UPDOWN_SetPos(infoPtr
, (int)lParam
);
1159 case UDM_GETUNICODEFORMAT
:
1160 /* we lie a bit here, we're always using Unicode internally */
1161 return infoPtr
->UnicodeFormat
;
1163 case UDM_SETUNICODEFORMAT
:
1165 /* do we really need to honour this flag? */
1166 int temp
= infoPtr
->UnicodeFormat
;
1167 infoPtr
->UnicodeFormat
= (BOOL
)wParam
;
1171 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1172 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1173 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
1179 /***********************************************************************
1180 * UPDOWN_Register [Internal]
1182 * Registers the updown window class.
1184 void UPDOWN_Register(void)
1188 ZeroMemory( &wndClass
, sizeof( WNDCLASSW
) );
1189 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1190 wndClass
.lpfnWndProc
= UpDownWindowProc
;
1191 wndClass
.cbClsExtra
= 0;
1192 wndClass
.cbWndExtra
= sizeof(UPDOWN_INFO
*);
1193 wndClass
.hCursor
= LoadCursorW( 0, (LPWSTR
)IDC_ARROW
);
1194 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
1195 wndClass
.lpszClassName
= UPDOWN_CLASSW
;
1197 RegisterClassW( &wndClass
);
1201 /***********************************************************************
1202 * UPDOWN_Unregister [Internal]
1204 * Unregisters the updown window class.
1206 void UPDOWN_Unregister (void)
1208 UnregisterClassW (UPDOWN_CLASSW
, NULL
);