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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This code was audited for completeness against the documented features
23 * of Comctl32.dll version 6.0 on Sep. 9, 2002, by Dimitrie O. Paun.
25 * Unless otherwise noted, we believe this code to be complete, as per
26 * the specification mentioned above.
27 * If you discover missing features, or bugs, please note them below.
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(updown
);
50 HWND Self
; /* Handle to this up-down control */
51 HWND Notify
; /* Handle to the parent window */
52 UINT AccelCount
; /* Number of elements in AccelVect */
53 UDACCEL
* AccelVect
; /* Vector containing AccelCount elements */
54 INT AccelIndex
; /* Current accel index, -1 if not accel'ing */
55 INT Base
; /* Base to display nr in the buddy window */
56 INT CurVal
; /* Current up-down value */
57 INT MinVal
; /* Minimum up-down value */
58 INT MaxVal
; /* Maximum up-down value */
59 HWND Buddy
; /* Handle to the buddy window */
60 INT BuddyType
; /* Remembers the buddy type BUDDY_TYPE_* */
61 INT Flags
; /* Internal Flags FLAG_* */
62 BOOL UnicodeFormat
; /* Marks the use of Unicode internally */
65 /* Control configuration constants */
67 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
68 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
69 #define REPEAT_DELAY 50 /* delay between auto-increments */
71 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
72 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
73 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
74 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
75 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
76 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
81 #define FLAG_INCR 0x01
82 #define FLAG_DECR 0x02
83 #define FLAG_MOUSEIN 0x04
84 #define FLAG_PRESSED 0x08
85 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
87 #define BUDDY_TYPE_UNKNOWN 0
88 #define BUDDY_TYPE_LISTBOX 1
89 #define BUDDY_TYPE_EDIT 2
91 #define TIMER_AUTOREPEAT 1
93 #define TIMER_AUTOPRESS 3
95 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
96 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
98 static const WCHAR BUDDY_UPDOWN_HWND
[] = { 'b', 'u', 'd', 'd', 'y', 'U', 'p', 'D', 'o', 'w', 'n', 'H', 'W', 'N', 'D', 0 };
99 static const WCHAR BUDDY_SUPERCLASS_WNDPROC
[] = { 'b', 'u', 'd', 'd', 'y', 'S', 'u', 'p', 'p', 'e', 'r',
100 'C', 'l', 'a', 's', 's', 'W', 'n', 'd', 'P', 'r', 'o', 'c', 0 };
101 static void UPDOWN_DoAction (UPDOWN_INFO
*infoPtr
, int delta
, int action
);
103 /***********************************************************************
105 * Tests if our buddy is an edit control.
107 static inline BOOL
UPDOWN_IsBuddyEdit(UPDOWN_INFO
*infoPtr
)
109 return infoPtr
->BuddyType
== BUDDY_TYPE_EDIT
;
112 /***********************************************************************
113 * UPDOWN_IsBuddyListbox
114 * Tests if our buddy is a listbox control.
116 static inline BOOL
UPDOWN_IsBuddyListbox(UPDOWN_INFO
*infoPtr
)
118 return infoPtr
->BuddyType
== BUDDY_TYPE_LISTBOX
;
121 /***********************************************************************
123 * Tests if a given value 'val' is between the Min&Max limits
125 static BOOL
UPDOWN_InBounds(UPDOWN_INFO
*infoPtr
, int val
)
127 if(infoPtr
->MaxVal
> infoPtr
->MinVal
)
128 return (infoPtr
->MinVal
<= val
) && (val
<= infoPtr
->MaxVal
);
130 return (infoPtr
->MaxVal
<= val
) && (val
<= infoPtr
->MinVal
);
133 /***********************************************************************
135 * Change the current value by delta.
136 * It returns TRUE is the value was changed successfuly, or FALSE
137 * if the value was not changed, as it would go out of bounds.
139 static BOOL
UPDOWN_OffsetVal(UPDOWN_INFO
*infoPtr
, int delta
)
141 /* check if we can do the modification first */
142 if(!UPDOWN_InBounds (infoPtr
, infoPtr
->CurVal
+delta
)) {
143 if (GetWindowLongW (infoPtr
->Self
, GWL_STYLE
) & UDS_WRAP
) {
144 delta
+= (delta
< 0 ? -1 : 1) *
145 (infoPtr
->MaxVal
< infoPtr
->MinVal
? -1 : 1) *
146 (infoPtr
->MinVal
- infoPtr
->MaxVal
) +
147 (delta
< 0 ? 1 : -1);
151 infoPtr
->CurVal
+= delta
;
155 /***********************************************************************
156 * UPDOWN_HasBuddyBorder
158 * When we have a buddy set and that we are aligned on our buddy, we
159 * want to draw a sunken edge to make like we are part of that control.
161 static BOOL
UPDOWN_HasBuddyBorder(UPDOWN_INFO
* infoPtr
)
163 DWORD dwStyle
= GetWindowLongW (infoPtr
->Self
, GWL_STYLE
);
165 return ( ((dwStyle
& (UDS_ALIGNLEFT
| UDS_ALIGNRIGHT
)) != 0) &&
166 UPDOWN_IsBuddyEdit(infoPtr
) );
169 /***********************************************************************
170 * UPDOWN_GetArrowRect
171 * wndPtr - pointer to the up-down wnd
172 * rect - will hold the rectangle
173 * arrow - FLAG_INCR to get the "increment" rect (up or right)
174 * FLAG_DECR to get the "decrement" rect (down or left)
175 * If both flags are pressent, the envelope is returned.
177 static void UPDOWN_GetArrowRect (UPDOWN_INFO
* infoPtr
, RECT
*rect
, int arrow
)
179 DWORD dwStyle
= GetWindowLongW (infoPtr
->Self
, GWL_STYLE
);
181 GetClientRect (infoPtr
->Self
, rect
);
184 * Make sure we calculate the rectangle to fit even if we draw the
187 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
188 if (dwStyle
& UDS_ALIGNLEFT
)
189 rect
->left
+= DEFAULT_BUDDYBORDER
;
191 rect
->right
-= DEFAULT_BUDDYBORDER
;
193 InflateRect(rect
, 0, -DEFAULT_BUDDYBORDER
);
196 /* now figure out if we need a space away from the buddy */
197 if ( IsWindow(infoPtr
->Buddy
) ) {
198 if (dwStyle
& UDS_ALIGNLEFT
) rect
->right
-= DEFAULT_BUDDYSPACER
;
199 else rect
->left
+= DEFAULT_BUDDYSPACER
;
203 * We're calculating the midpoint to figure-out where the
204 * separation between the buttons will lay. We make sure that we
205 * round the uneven numbers by adding 1.
207 if (dwStyle
& UDS_HORZ
) {
208 int len
= rect
->right
- rect
->left
+ 1; /* compute the width */
209 if (arrow
& FLAG_INCR
)
210 rect
->left
= rect
->left
+ len
/2;
211 if (arrow
& FLAG_DECR
)
212 rect
->right
= rect
->left
+ len
/2 - 1;
214 int len
= rect
->bottom
- rect
->top
+ 1; /* compute the height */
215 if (arrow
& FLAG_INCR
)
216 rect
->bottom
= rect
->top
+ len
/2 - 1;
217 if (arrow
& FLAG_DECR
)
218 rect
->top
= rect
->top
+ len
/2;
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 TRUE.
226 * If it returns the down rect, it returns FALSE.
228 static BOOL
UPDOWN_GetArrowFromPoint (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()
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 (!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 if (!GetWindowTextW(infoPtr
->Buddy
, txt
, COUNT_OF(txt
))) return FALSE
;
278 sep
= UPDOWN_GetThousandSep();
280 /* now get rid of the separators */
281 for(src
= dst
= txt
; *src
; src
++)
282 if(*src
!= sep
) *dst
++ = *src
;
285 /* try to convert the number and validate it */
286 newVal
= strtolW(txt
, &src
, infoPtr
->Base
);
287 if(*src
|| !UPDOWN_InBounds (infoPtr
, newVal
)) return FALSE
;
290 TRACE("new value(%d) from buddy (old=%d)\n", newVal
, infoPtr
->CurVal
);
291 infoPtr
->CurVal
= newVal
;
296 /***********************************************************************
298 * Tries to set the pos to the buddy window based on current pos
300 * TRUE - if it set the caption of the buddy successfully
301 * FALSE - if an error occurred
303 static BOOL
UPDOWN_SetBuddyInt (UPDOWN_INFO
*infoPtr
)
305 WCHAR fmt
[3] = { '%', 'd', '\0' };
309 if (!IsWindow(infoPtr
->Buddy
)) return FALSE
;
311 TRACE("set new value(%d) to buddy.\n", infoPtr
->CurVal
);
313 /*if the buddy is a list window, we must set curr index */
314 if (UPDOWN_IsBuddyListbox(infoPtr
)) {
315 return SendMessageW(infoPtr
->Buddy
, LB_SETCURSEL
, infoPtr
->CurVal
, 0) != LB_ERR
;
318 /* Regular window, so set caption to the number */
319 if (infoPtr
->Base
== 16) fmt
[1] = 'X';
320 len
= wsprintfW(txt
, fmt
, infoPtr
->CurVal
);
323 /* Do thousands separation if necessary */
324 if (!(GetWindowLongW (infoPtr
->Self
, GWL_STYLE
) & UDS_NOTHOUSANDS
) && (len
> 3)) {
325 WCHAR tmp
[COUNT_OF(txt
)], *src
= tmp
, *dst
= txt
;
326 WCHAR sep
= UPDOWN_GetThousandSep();
329 memcpy(tmp
, txt
, sizeof(txt
));
330 if (start
== 0) start
= 3;
333 for (len
=0; *src
; len
++) {
334 if (len
% 3 == 0) *dst
++ = sep
;
340 return SetWindowTextW(infoPtr
->Buddy
, txt
);
343 /***********************************************************************
346 * Draw the arrows. The background need not be erased.
348 static LRESULT
UPDOWN_Draw (UPDOWN_INFO
*infoPtr
, HDC hdc
)
350 DWORD dwStyle
= GetWindowLongW (infoPtr
->Self
, GWL_STYLE
);
354 /* Draw the common border between ourselves and our buddy */
355 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
356 GetClientRect(infoPtr
->Self
, &rect
);
357 DrawEdge(hdc
, &rect
, EDGE_SUNKEN
,
359 (dwStyle
& UDS_ALIGNLEFT
? BF_LEFT
: BF_RIGHT
));
362 /* Draw the incr button */
363 UPDOWN_GetArrowRect (infoPtr
, &rect
, FLAG_INCR
);
364 pressed
= (infoPtr
->Flags
& FLAG_PRESSED
) && (infoPtr
->Flags
& FLAG_INCR
);
365 hot
= (infoPtr
->Flags
& FLAG_INCR
) && (infoPtr
->Flags
& FLAG_MOUSEIN
);
366 DrawFrameControl(hdc
, &rect
, DFC_SCROLL
,
367 (dwStyle
& UDS_HORZ
? DFCS_SCROLLRIGHT
: DFCS_SCROLLUP
) |
368 ((dwStyle
& UDS_HOTTRACK
) && hot
? DFCS_HOT
: 0) |
369 (pressed
? DFCS_PUSHED
: 0) |
370 (dwStyle
& WS_DISABLED
? DFCS_INACTIVE
: 0) );
372 /* Draw the decr button */
373 UPDOWN_GetArrowRect(infoPtr
, &rect
, FLAG_DECR
);
374 pressed
= (infoPtr
->Flags
& FLAG_PRESSED
) && (infoPtr
->Flags
& FLAG_DECR
);
375 hot
= (infoPtr
->Flags
& FLAG_DECR
) && (infoPtr
->Flags
& FLAG_MOUSEIN
);
376 DrawFrameControl(hdc
, &rect
, DFC_SCROLL
,
377 (dwStyle
& UDS_HORZ
? DFCS_SCROLLLEFT
: DFCS_SCROLLDOWN
) |
378 ((dwStyle
& UDS_HOTTRACK
) && hot
? DFCS_HOT
: 0) |
379 (pressed
? DFCS_PUSHED
: 0) |
380 (dwStyle
& WS_DISABLED
? DFCS_INACTIVE
: 0) );
385 /***********************************************************************
388 * Asynchronous drawing (must ONLY be used in WM_PAINT).
391 static LRESULT
UPDOWN_Paint (UPDOWN_INFO
*infoPtr
, HDC hdc
)
394 if (hdc
) return UPDOWN_Draw (infoPtr
, hdc
);
395 hdc
= BeginPaint (infoPtr
->Self
, &ps
);
396 UPDOWN_Draw (infoPtr
, hdc
);
397 EndPaint (infoPtr
->Self
, &ps
);
401 /***********************************************************************
404 * Handle key presses (up & down) when we have to do so
406 static LRESULT
UPDOWN_KeyPressed(UPDOWN_INFO
*infoPtr
, int key
)
410 if (key
== VK_UP
) arrow
= FLAG_INCR
;
411 else if (key
== VK_DOWN
) arrow
= FLAG_DECR
;
414 UPDOWN_GetBuddyInt (infoPtr
);
415 infoPtr
->Flags
&= ~FLAG_ARROW
;
416 infoPtr
->Flags
|= FLAG_PRESSED
| arrow
;
417 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
418 SetTimer(infoPtr
->Self
, TIMER_AUTOPRESS
, AUTOPRESS_DELAY
, 0);
419 UPDOWN_DoAction (infoPtr
, 1, arrow
);
423 /***********************************************************************
424 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
427 static LRESULT CALLBACK
428 UPDOWN_Buddy_SubclassProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
430 WNDPROC superClassWndProc
= (WNDPROC
)GetPropW(hwnd
, BUDDY_SUPERCLASS_WNDPROC
);
432 TRACE("hwnd=%p, wndProc=%p, uMsg=%04x, wParam=%08x, lParam=%08lx\n",
433 hwnd
, superClassWndProc
, uMsg
, wParam
, lParam
);
435 if (uMsg
== WM_KEYDOWN
) {
436 HWND upDownHwnd
= GetPropW(hwnd
, BUDDY_UPDOWN_HWND
);
438 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd
), (int)wParam
);
441 return CallWindowProcW( superClassWndProc
, hwnd
, uMsg
, wParam
, lParam
);
444 /***********************************************************************
447 * Sets bud as a new Buddy.
448 * Then, it should subclass the buddy
449 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
450 * process the UP/DOWN arrow keys.
451 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
452 * the size/pos of the buddy and the control are adjusted accordingly.
454 static HWND
UPDOWN_SetBuddy (UPDOWN_INFO
* infoPtr
, HWND bud
)
456 static const WCHAR editW
[] = { 'E', 'd', 'i', 't', 0 };
457 static const WCHAR listboxW
[] = { 'L', 'i', 's', 't', 'b', 'o', 'x', 0 };
458 DWORD dwStyle
= GetWindowLongW (infoPtr
->Self
, GWL_STYLE
);
459 RECT budRect
; /* new coord for the buddy */
460 int x
, width
; /* new x position and width for the up-down */
462 WCHAR buddyClass
[40];
465 TRACE("(hwnd=%p, bud=%p)\n", infoPtr
->Self
, bud
);
467 ret
= infoPtr
->Buddy
;
469 /* there is already a body assigned */
470 if (infoPtr
->Buddy
) RemovePropW(infoPtr
->Buddy
, BUDDY_UPDOWN_HWND
);
475 /* Store buddy window handle */
476 infoPtr
->Buddy
= bud
;
480 /* keep upDown ctrl hwnd in a buddy property */
481 SetPropW( bud
, BUDDY_UPDOWN_HWND
, infoPtr
->Self
);
483 /* Store buddy window class type */
484 infoPtr
->BuddyType
= BUDDY_TYPE_UNKNOWN
;
485 if (GetClassNameW(bud
, buddyClass
, COUNT_OF(buddyClass
))) {
486 if (lstrcmpiW(buddyClass
, editW
) == 0)
487 infoPtr
->BuddyType
= BUDDY_TYPE_EDIT
;
488 else if (lstrcmpiW(buddyClass
, listboxW
) == 0)
489 infoPtr
->BuddyType
= BUDDY_TYPE_LISTBOX
;
492 if(dwStyle
& UDS_ARROWKEYS
){
493 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
494 when we reset the upDown ctrl buddy to another buddy because it is not
495 good to break the window proc chain. */
496 if (!GetPropW(bud
, BUDDY_SUPERCLASS_WNDPROC
)) {
497 baseWndProc
= (WNDPROC
)SetWindowLongPtrW(bud
, GWLP_WNDPROC
, (LPARAM
)UPDOWN_Buddy_SubclassProc
);
498 SetPropW(bud
, BUDDY_SUPERCLASS_WNDPROC
, (HANDLE
)baseWndProc
);
502 /* Get the rect of the buddy relative to its parent */
503 GetWindowRect(infoPtr
->Buddy
, &budRect
);
504 MapWindowPoints(HWND_DESKTOP
, GetParent(infoPtr
->Buddy
), (POINT
*)(&budRect
.left
), 2);
506 /* now do the positioning */
507 if (dwStyle
& UDS_ALIGNLEFT
) {
509 budRect
.left
+= DEFAULT_WIDTH
+ DEFAULT_XSEP
;
510 } else if (dwStyle
& UDS_ALIGNRIGHT
) {
511 budRect
.right
-= DEFAULT_WIDTH
+ DEFAULT_XSEP
;
512 x
= budRect
.right
+DEFAULT_XSEP
;
518 /* first adjust the buddy to accommodate the up/down */
519 SetWindowPos(infoPtr
->Buddy
, 0, budRect
.left
, budRect
.top
,
520 budRect
.right
- budRect
.left
, budRect
.bottom
- budRect
.top
,
521 SWP_NOACTIVATE
|SWP_NOZORDER
);
523 /* now position the up/down */
524 /* Since the UDS_ALIGN* flags were used, */
525 /* we will pick the position and size of the window. */
526 width
= DEFAULT_WIDTH
;
529 * If the updown has a buddy border, it has to overlap with the buddy
530 * to look as if it is integrated with the buddy control.
531 * We nudge the control or change its size to overlap.
533 if (UPDOWN_HasBuddyBorder(infoPtr
)) {
534 if(dwStyle
& UDS_ALIGNLEFT
)
535 width
+= DEFAULT_BUDDYBORDER
;
537 x
-= DEFAULT_BUDDYBORDER
;
540 SetWindowPos(infoPtr
->Self
, 0, x
,
541 budRect
.top
- DEFAULT_ADDTOP
, width
,
542 budRect
.bottom
- budRect
.top
+ DEFAULT_ADDTOP
+ DEFAULT_ADDBOT
,
543 SWP_NOACTIVATE
|SWP_FRAMECHANGED
|SWP_NOZORDER
);
546 GetWindowRect(infoPtr
->Self
, &rect
);
547 MapWindowPoints(HWND_DESKTOP
, GetParent(infoPtr
->Self
), (POINT
*)&rect
, 2);
548 SetWindowPos(infoPtr
->Self
, 0, rect
.left
, rect
.top
, DEFAULT_WIDTH
, rect
.bottom
- rect
.top
,
549 SWP_NOACTIVATE
|SWP_FRAMECHANGED
|SWP_NOZORDER
);
554 /***********************************************************************
557 * This function increments/decrements the CurVal by the
558 * 'delta' amount according to the 'action' flag which can be a
559 * combination of FLAG_INCR and FLAG_DECR
560 * It notifies the parent as required.
561 * It handles wraping and non-wraping correctly.
562 * It is assumed that delta>0
564 static void UPDOWN_DoAction (UPDOWN_INFO
*infoPtr
, int delta
, int action
)
566 DWORD dwStyle
= GetWindowLongW (infoPtr
->Self
, GWL_STYLE
);
569 TRACE("%d by %d\n", action
, delta
);
571 /* check if we can do the modification first */
572 delta
*= (action
& FLAG_INCR
? 1 : -1) * (infoPtr
->MaxVal
< infoPtr
->MinVal
? -1 : 1);
573 if ( (action
& FLAG_INCR
) && (action
& FLAG_DECR
) ) delta
= 0;
575 TRACE("current %d, delta: %d\n", infoPtr
->CurVal
, delta
);
577 /* We must notify parent now to obtain permission */
578 ni
.iPos
= infoPtr
->CurVal
;
580 ni
.hdr
.hwndFrom
= infoPtr
->Self
;
581 ni
.hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
582 ni
.hdr
.code
= UDN_DELTAPOS
;
583 if (!SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, (WPARAM
)ni
.hdr
.idFrom
, (LPARAM
)&ni
)) {
584 /* Parent said: OK to adjust */
586 /* Now adjust value with (maybe new) delta */
587 if (UPDOWN_OffsetVal (infoPtr
, ni
.iDelta
)) {
588 TRACE("new %d, delta: %d\n", infoPtr
->CurVal
, ni
.iDelta
);
590 /* Now take care about our buddy */
591 if (dwStyle
& UDS_SETBUDDYINT
) UPDOWN_SetBuddyInt (infoPtr
);
595 /* Also, notify it. This message is sent in any case. */
596 SendMessageW( infoPtr
->Notify
, dwStyle
& UDS_HORZ
? WM_HSCROLL
: WM_VSCROLL
,
597 MAKELONG(SB_THUMBPOSITION
, infoPtr
->CurVal
), (LPARAM
)infoPtr
->Self
);
600 /***********************************************************************
603 * Returns TRUE if it is enabled as well as its buddy (if any)
606 static BOOL
UPDOWN_IsEnabled (UPDOWN_INFO
*infoPtr
)
608 if (!IsWindowEnabled(infoPtr
->Self
))
611 return IsWindowEnabled(infoPtr
->Buddy
);
615 /***********************************************************************
618 * Deletes any timers, releases the mouse and does redraw if necessary.
619 * If the control is not in "capture" mode, it does nothing.
620 * If the control was not in cancel mode, it returns FALSE.
621 * If the control was in cancel mode, it returns TRUE.
623 static BOOL
UPDOWN_CancelMode (UPDOWN_INFO
*infoPtr
)
625 if (!(infoPtr
->Flags
& FLAG_PRESSED
)) return FALSE
;
627 KillTimer (infoPtr
->Self
, TIMER_AUTOREPEAT
);
628 KillTimer (infoPtr
->Self
, TIMER_ACCEL
);
629 KillTimer (infoPtr
->Self
, TIMER_AUTOPRESS
);
631 if (GetCapture() == infoPtr
->Self
) {
633 hdr
.hwndFrom
= infoPtr
->Self
;
634 hdr
.idFrom
= GetWindowLongPtrW (infoPtr
->Self
, GWLP_ID
);
635 hdr
.code
= NM_RELEASEDCAPTURE
;
636 SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, hdr
.idFrom
, (LPARAM
)&hdr
);
640 infoPtr
->Flags
&= ~FLAG_PRESSED
;
641 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
646 /***********************************************************************
647 * UPDOWN_HandleMouseEvent
649 * Handle a mouse event for the updown.
650 * 'pt' is the location of the mouse event in client or
651 * windows coordinates.
653 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO
*infoPtr
, UINT msg
, INT x
, INT y
)
655 DWORD dwStyle
= GetWindowLongW (infoPtr
->Self
, GWL_STYLE
);
660 TRACE("msg %04x point %s\n", msg
, wine_dbgstr_point(&pt
));
664 case WM_LBUTTONDOWN
: /* Initialise mouse tracking */
666 /* If the buddy is an edit, will set focus to it */
667 if (UPDOWN_IsBuddyEdit(infoPtr
)) SetFocus(infoPtr
->Buddy
);
669 /* Now see which one is the 'active' arrow */
670 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
672 /* Update the flags if we are in/out */
673 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
675 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
677 if (infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
679 if (infoPtr
->Flags
& FLAG_ARROW
) {
681 /* Update the CurVal if necessary */
682 if (dwStyle
& UDS_SETBUDDYINT
) UPDOWN_GetBuddyInt (infoPtr
);
684 /* Set up the correct flags */
685 infoPtr
->Flags
|= FLAG_PRESSED
;
687 /* repaint the control */
688 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
690 /* process the click */
691 temp
= (infoPtr
->AccelCount
&& infoPtr
->AccelVect
) ? infoPtr
->AccelVect
[0].nInc
: 1;
692 UPDOWN_DoAction (infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
694 /* now capture all mouse messages */
695 SetCapture (infoPtr
->Self
);
697 /* and startup the first timer */
698 SetTimer(infoPtr
->Self
, TIMER_AUTOREPEAT
, INITIAL_DELAY
, 0);
703 /* save the flags to see if any got modified */
704 temp
= infoPtr
->Flags
;
706 /* Now see which one is the 'active' arrow */
707 arrow
= UPDOWN_GetArrowFromPoint (infoPtr
, &rect
, pt
);
709 /* Update the flags if we are in/out */
710 infoPtr
->Flags
&= ~(FLAG_MOUSEIN
| FLAG_ARROW
);
712 infoPtr
->Flags
|= FLAG_MOUSEIN
| arrow
;
714 if(infoPtr
->AccelIndex
!= -1) infoPtr
->AccelIndex
= 0;
717 /* If state changed, redraw the control */
718 if(temp
!= infoPtr
->Flags
)
719 InvalidateRect (infoPtr
->Self
, &rect
, FALSE
);
723 ERR("Impossible case (msg=%x)!\n", msg
);
728 /***********************************************************************
731 static LRESULT WINAPI
UpDownWindowProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
733 UPDOWN_INFO
*infoPtr
= UPDOWN_GetInfoPtr (hwnd
);
734 DWORD dwStyle
= GetWindowLongW (hwnd
, GWL_STYLE
);
737 TRACE("hwnd=%p msg=%04x wparam=%08x lparam=%08lx\n", hwnd
, message
, wParam
, lParam
);
739 if (!infoPtr
&& (message
!= WM_CREATE
))
740 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
745 SetWindowLongW (hwnd
, GWL_STYLE
, dwStyle
& ~WS_BORDER
);
746 infoPtr
= (UPDOWN_INFO
*)Alloc (sizeof(UPDOWN_INFO
));
747 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
749 /* initialize the info struct */
750 infoPtr
->Self
= hwnd
;
751 infoPtr
->Notify
= ((LPCREATESTRUCTA
)lParam
)->hwndParent
;
752 infoPtr
->AccelCount
= 0;
753 infoPtr
->AccelVect
= 0;
754 infoPtr
->AccelIndex
= -1;
756 infoPtr
->MinVal
= 100;
758 infoPtr
->Base
= 10; /* Default to base 10 */
759 infoPtr
->Buddy
= 0; /* No buddy window yet */
760 infoPtr
->Flags
= 0; /* And no flags */
762 /* Do we pick the buddy win ourselves? */
763 if (dwStyle
& UDS_AUTOBUDDY
)
764 UPDOWN_SetBuddy (infoPtr
, GetWindow (hwnd
, GW_HWNDPREV
));
766 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd
);
770 if(infoPtr
->AccelVect
) Free (infoPtr
->AccelVect
);
772 if(infoPtr
->Buddy
) RemovePropW(infoPtr
->Buddy
, BUDDY_UPDOWN_HWND
);
775 SetWindowLongPtrW (hwnd
, 0, 0);
776 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd
);
780 if (dwStyle
& WS_DISABLED
) UPDOWN_CancelMode (infoPtr
);
781 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
785 /* is this the auto-press timer? */
786 if(wParam
== TIMER_AUTOPRESS
) {
787 KillTimer(hwnd
, TIMER_AUTOPRESS
);
788 infoPtr
->Flags
&= ~(FLAG_PRESSED
| FLAG_ARROW
);
789 InvalidateRect(infoPtr
->Self
, NULL
, FALSE
);
792 /* if initial timer, kill it and start the repeat timer */
793 if(wParam
== TIMER_AUTOREPEAT
) {
794 KillTimer(hwnd
, TIMER_AUTOREPEAT
);
795 /* if no accel info given, used default timer */
796 if(infoPtr
->AccelCount
==0 || infoPtr
->AccelVect
==0) {
797 infoPtr
->AccelIndex
= -1;
800 infoPtr
->AccelIndex
= 0; /* otherwise, use it */
801 temp
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
803 SetTimer(hwnd
, TIMER_ACCEL
, temp
, 0);
806 /* now, if the mouse is above us, do the thing...*/
807 if(infoPtr
->Flags
& FLAG_MOUSEIN
) {
808 temp
= infoPtr
->AccelIndex
== -1 ? 1 : infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nInc
;
809 UPDOWN_DoAction(infoPtr
, temp
, infoPtr
->Flags
& FLAG_ARROW
);
811 if(infoPtr
->AccelIndex
!= -1 && infoPtr
->AccelIndex
< infoPtr
->AccelCount
-1) {
812 KillTimer(hwnd
, TIMER_ACCEL
);
813 infoPtr
->AccelIndex
++; /* move to the next accel info */
814 temp
= infoPtr
->AccelVect
[infoPtr
->AccelIndex
].nSec
* 1000 + 1;
815 /* make sure we have at least 1ms intervals */
816 SetTimer(hwnd
, TIMER_ACCEL
, temp
, 0);
822 return UPDOWN_CancelMode (infoPtr
);
825 if (GetCapture() != infoPtr
->Self
) break;
827 if ( (infoPtr
->Flags
& FLAG_MOUSEIN
) &&
828 (infoPtr
->Flags
& FLAG_ARROW
) ) {
830 SendMessageW( infoPtr
->Notify
,
831 dwStyle
& UDS_HORZ
? WM_HSCROLL
: WM_VSCROLL
,
832 MAKELONG(SB_ENDSCROLL
, infoPtr
->CurVal
),
834 if (UPDOWN_IsBuddyEdit(infoPtr
))
835 SendMessageW(infoPtr
->Buddy
, EM_SETSEL
, 0, MAKELONG(0, -1));
837 UPDOWN_CancelMode(infoPtr
);
842 if(UPDOWN_IsEnabled(infoPtr
))
843 UPDOWN_HandleMouseEvent (infoPtr
, message
, (SHORT
)LOWORD(lParam
), (SHORT
)HIWORD(lParam
));
847 if((dwStyle
& UDS_ARROWKEYS
) && UPDOWN_IsEnabled(infoPtr
))
848 return UPDOWN_KeyPressed(infoPtr
, (int)wParam
);
852 return UPDOWN_Paint (infoPtr
, (HDC
)wParam
);
855 if (wParam
==0 && lParam
==0) return infoPtr
->AccelCount
;
856 if (wParam
&& lParam
) {
857 temp
= min(infoPtr
->AccelCount
, wParam
);
858 memcpy((void *)lParam
, infoPtr
->AccelVect
, temp
*sizeof(UDACCEL
));
864 TRACE("UDM_SETACCEL\n");
866 if(infoPtr
->AccelVect
) {
867 Free (infoPtr
->AccelVect
);
868 infoPtr
->AccelCount
= 0;
869 infoPtr
->AccelVect
= 0;
871 if(wParam
==0) return TRUE
;
872 infoPtr
->AccelVect
= Alloc (wParam
*sizeof(UDACCEL
));
873 if(infoPtr
->AccelVect
== 0) return FALSE
;
874 memcpy(infoPtr
->AccelVect
, (void*)lParam
, wParam
*sizeof(UDACCEL
));
875 infoPtr
->AccelCount
= wParam
;
877 for (temp
= 0; temp
< wParam
; temp
++)
878 TRACE("%d: nSec %u nInc %u\n", temp
, infoPtr
->AccelVect
[temp
].nSec
, infoPtr
->AccelVect
[temp
].nInc
);
883 return infoPtr
->Base
;
886 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam
, hwnd
);
887 if (wParam
==10 || wParam
==16) {
888 temp
= infoPtr
->Base
;
889 infoPtr
->Base
= wParam
;
895 return (LRESULT
)infoPtr
->Buddy
;
898 return (LRESULT
)UPDOWN_SetBuddy (infoPtr
, (HWND
)wParam
);
901 temp
= UPDOWN_GetBuddyInt (infoPtr
);
902 return MAKELONG(infoPtr
->CurVal
, temp
? 0 : 1);
905 temp
= (short)LOWORD(lParam
);
906 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp
, hwnd
);
907 if(!UPDOWN_InBounds(infoPtr
, temp
)) {
908 if(temp
< infoPtr
->MinVal
) temp
= infoPtr
->MinVal
;
909 if(temp
> infoPtr
->MaxVal
) temp
= infoPtr
->MaxVal
;
911 wParam
= infoPtr
->CurVal
;
912 infoPtr
->CurVal
= temp
;
913 if(dwStyle
& UDS_SETBUDDYINT
) UPDOWN_SetBuddyInt (infoPtr
);
914 return wParam
; /* return prev value */
917 return MAKELONG(infoPtr
->MaxVal
, infoPtr
->MinVal
);
921 infoPtr
->MaxVal
= (short)(lParam
); /* UD_MINVAL <= Max <= UD_MAXVAL */
922 infoPtr
->MinVal
= (short)HIWORD(lParam
); /* UD_MINVAL <= Min <= UD_MAXVAL */
923 /* |Max-Min| <= UD_MAXVAL */
924 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
925 infoPtr
->MinVal
, infoPtr
->MaxVal
, hwnd
);
929 if (wParam
) *(LPINT
)wParam
= infoPtr
->MinVal
;
930 if (lParam
) *(LPINT
)lParam
= infoPtr
->MaxVal
;
934 infoPtr
->MinVal
= (INT
)wParam
;
935 infoPtr
->MaxVal
= (INT
)lParam
;
936 if (infoPtr
->MaxVal
<= infoPtr
->MinVal
)
937 infoPtr
->MaxVal
= infoPtr
->MinVal
+ 1;
938 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
939 infoPtr
->MinVal
, infoPtr
->MaxVal
, hwnd
);
943 if ((LPBOOL
)lParam
!= NULL
) *((LPBOOL
)lParam
) = TRUE
;
944 return infoPtr
->CurVal
;
947 if(!UPDOWN_InBounds(infoPtr
, (int)lParam
)) {
948 if((int)lParam
< infoPtr
->MinVal
) lParam
= infoPtr
->MinVal
;
949 if((int)lParam
> infoPtr
->MaxVal
) lParam
= infoPtr
->MaxVal
;
951 temp
= infoPtr
->CurVal
; /* save prev value */
952 infoPtr
->CurVal
= (int)lParam
; /* set the new value */
953 if(dwStyle
& UDS_SETBUDDYINT
) UPDOWN_SetBuddyInt (infoPtr
);
954 return temp
; /* return prev value */
956 case UDM_GETUNICODEFORMAT
:
957 /* we lie a bit here, we're always using Unicode internally */
958 return infoPtr
->UnicodeFormat
;
960 case UDM_SETUNICODEFORMAT
:
961 /* do we really need to honour this flag? */
962 temp
= infoPtr
->UnicodeFormat
;
963 infoPtr
->UnicodeFormat
= (BOOL
)wParam
;
967 if ((message
>= WM_USER
) && (message
< WM_APP
))
968 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message
, wParam
, lParam
);
969 return DefWindowProcW (hwnd
, message
, wParam
, lParam
);
975 /***********************************************************************
976 * UPDOWN_Register [Internal]
978 * Registers the updown window class.
980 void UPDOWN_Register(void)
984 ZeroMemory( &wndClass
, sizeof( WNDCLASSW
) );
985 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
986 wndClass
.lpfnWndProc
= UpDownWindowProc
;
987 wndClass
.cbClsExtra
= 0;
988 wndClass
.cbWndExtra
= sizeof(UPDOWN_INFO
*);
989 wndClass
.hCursor
= LoadCursorW( 0, (LPWSTR
)IDC_ARROW
);
990 wndClass
.hbrBackground
= (HBRUSH
)(COLOR_BTNFACE
+ 1);
991 wndClass
.lpszClassName
= UPDOWN_CLASSW
;
993 RegisterClassW( &wndClass
);
997 /***********************************************************************
998 * UPDOWN_Unregister [Internal]
1000 * Unregisters the updown window class.
1002 void UPDOWN_Unregister (void)
1004 UnregisterClassW (UPDOWN_CLASSW
, NULL
);