Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / comctl32 / updown.c
blob31a80e204a18e06bc323bb1af66b12c260958af3
1 /*
2 * Updown control
4 * Copyright 1997, 2002 Dimitrie O. Paun
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * NOTE
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.
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <stdio.h>
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40 #include "winnls.h"
41 #include "commctrl.h"
42 #include "comctl32.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(updown);
48 typedef struct
50 HWND Self; /* Handle to this up-down control */
51 HWND Notify; /* Handle to the parent window */
52 DWORD dwStyle; /* The GWL_STYLE for this window */
53 UINT AccelCount; /* Number of elements in AccelVect */
54 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
55 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
56 INT Base; /* Base to display nr in the buddy window */
57 INT CurVal; /* Current up-down value */
58 INT MinVal; /* Minimum up-down value */
59 INT MaxVal; /* Maximum up-down value */
60 HWND Buddy; /* Handle to the buddy window */
61 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
62 INT Flags; /* Internal Flags FLAG_* */
63 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
64 } UPDOWN_INFO;
66 /* Control configuration constants */
68 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
69 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
70 #define REPEAT_DELAY 50 /* delay between auto-increments */
72 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
73 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
74 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
75 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
76 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
77 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
80 /* Work constants */
82 #define FLAG_INCR 0x01
83 #define FLAG_DECR 0x02
84 #define FLAG_MOUSEIN 0x04
85 #define FLAG_PRESSED 0x08
86 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
88 #define BUDDY_TYPE_UNKNOWN 0
89 #define BUDDY_TYPE_LISTBOX 1
90 #define BUDDY_TYPE_EDIT 2
92 #define TIMER_AUTOREPEAT 1
93 #define TIMER_ACCEL 2
94 #define TIMER_AUTOPRESS 3
96 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongPtrW (hwnd,0))
97 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
99 static const WCHAR BUDDY_UPDOWN_HWND[] = { 'b', 'u', 'd', 'd', 'y', 'U', 'p', 'D', 'o', 'w', 'n', 'H', 'W', 'N', 'D', 0 };
100 static const WCHAR BUDDY_SUPERCLASS_WNDPROC[] = { 'b', 'u', 'd', 'd', 'y', 'S', 'u', 'p', 'p', 'e', 'r',
101 'C', 'l', 'a', 's', 's', 'W', 'n', 'd', 'P', 'r', 'o', 'c', 0 };
102 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
104 /***********************************************************************
105 * UPDOWN_IsBuddyEdit
106 * Tests if our buddy is an edit control.
108 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
110 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
113 /***********************************************************************
114 * UPDOWN_IsBuddyListbox
115 * Tests if our buddy is a listbox control.
117 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
119 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
122 /***********************************************************************
123 * UPDOWN_InBounds
124 * Tests if a given value 'val' is between the Min&Max limits
126 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
128 if(infoPtr->MaxVal > infoPtr->MinVal)
129 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
130 else
131 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
134 /***********************************************************************
135 * UPDOWN_OffsetVal
136 * Change the current value by delta.
137 * It returns TRUE is the value was changed successfuly, or FALSE
138 * if the value was not changed, as it would go out of bounds.
140 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
142 /* check if we can do the modification first */
143 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
144 if (infoPtr->dwStyle & UDS_WRAP) {
145 delta += (delta < 0 ? -1 : 1) *
146 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
147 (infoPtr->MinVal - infoPtr->MaxVal) +
148 (delta < 0 ? 1 : -1);
149 } else return FALSE;
152 infoPtr->CurVal += delta;
153 return TRUE;
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(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)
174 * If both flags are pressent, the envelope is returned.
176 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
178 GetClientRect (infoPtr->Self, rect);
181 * Make sure we calculate the rectangle to fit even if we draw the
182 * border.
184 if (UPDOWN_HasBuddyBorder(infoPtr)) {
185 if (infoPtr->dwStyle & UDS_ALIGNLEFT)
186 rect->left += DEFAULT_BUDDYBORDER;
187 else
188 rect->right -= DEFAULT_BUDDYBORDER;
190 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
193 /* now figure out if we need a space away from the buddy */
194 if ( IsWindow(infoPtr->Buddy) ) {
195 if (infoPtr->dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
196 else rect->left += DEFAULT_BUDDYSPACER;
200 * We're calculating the midpoint to figure-out where the
201 * separation between the buttons will lay. We make sure that we
202 * round the uneven numbers by adding 1.
204 if (infoPtr->dwStyle & UDS_HORZ) {
205 int len = rect->right - rect->left + 1; /* compute the width */
206 if (arrow & FLAG_INCR)
207 rect->left = rect->left + len/2;
208 if (arrow & FLAG_DECR)
209 rect->right = rect->left + len/2 - 1;
210 } else {
211 int len = rect->bottom - rect->top + 1; /* compute the height */
212 if (arrow & FLAG_INCR)
213 rect->bottom = rect->top + len/2 - 1;
214 if (arrow & FLAG_DECR)
215 rect->top = rect->top + len/2;
219 /***********************************************************************
220 * UPDOWN_GetArrowFromPoint
221 * Returns the rectagle (for the up or down arrow) that contains pt.
222 * If it returns the up rect, it returns TRUE.
223 * If it returns the down rect, it returns FALSE.
225 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
227 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
228 if(PtInRect(rect, pt)) return FLAG_INCR;
230 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
231 if(PtInRect(rect, pt)) return FLAG_DECR;
233 return 0;
237 /***********************************************************************
238 * UPDOWN_GetThousandSep
239 * Returns the thousand sep. If an error occurs, it returns ','.
241 static WCHAR UPDOWN_GetThousandSep()
243 WCHAR sep[2];
245 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
246 sep[0] = ',';
248 return sep[0];
251 /***********************************************************************
252 * UPDOWN_GetBuddyInt
253 * Tries to read the pos from the buddy window and if it succeeds,
254 * it stores it in the control's CurVal
255 * returns:
256 * TRUE - if it read the integer from the buddy successfully
257 * FALSE - if an error occurred
259 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
261 WCHAR txt[20], sep, *src, *dst;
262 int newVal;
264 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
265 return FALSE;
267 /*if the buddy is a list window, we must set curr index */
268 if (UPDOWN_IsBuddyListbox(infoPtr)) {
269 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
270 if(newVal < 0) return FALSE;
271 } else {
272 /* we have a regular window, so will get the text */
273 /* note that a zero-length string is a legitimate value for 'txt',
274 * and ought to result in a successful conversion to '0'. */
275 if (GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt)) < 0)
276 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;
283 *dst = 0;
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;
292 return TRUE;
296 /***********************************************************************
297 * UPDOWN_SetBuddyInt
298 * Tries to set the pos to the buddy window based on current pos
299 * returns:
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' };
306 WCHAR txt[20];
307 int len;
309 if (!((infoPtr->dwStyle & UDS_SETBUDDYINT) && IsWindow(infoPtr->Buddy)))
310 return FALSE;
312 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
314 /*if the buddy is a list window, we must set curr index */
315 if (UPDOWN_IsBuddyListbox(infoPtr)) {
316 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
319 /* Regular window, so set caption to the number */
320 if (infoPtr->Base == 16) fmt[1] = 'X';
321 len = wsprintfW(txt, fmt, infoPtr->CurVal);
324 /* Do thousands separation if necessary */
325 if (!(infoPtr->dwStyle & UDS_NOTHOUSANDS) && (len > 3)) {
326 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
327 WCHAR sep = UPDOWN_GetThousandSep();
328 int start = len % 3;
330 memcpy(tmp, txt, sizeof(txt));
331 if (start == 0) start = 3;
332 dst += start;
333 src += start;
334 for (len=0; *src; len++) {
335 if (len % 3 == 0) *dst++ = sep;
336 *dst++ = *src++;
338 *dst = 0;
341 return SetWindowTextW(infoPtr->Buddy, txt);
344 /***********************************************************************
345 * UPDOWN_Draw
347 * Draw the arrows. The background need not be erased.
349 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
351 BOOL pressed, hot;
352 RECT rect;
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,
358 BF_BOTTOM | BF_TOP |
359 (infoPtr->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 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
368 ((infoPtr->dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
369 (pressed ? DFCS_PUSHED : 0) |
370 (infoPtr->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 (infoPtr->dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
378 ((infoPtr->dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
379 (pressed ? DFCS_PUSHED : 0) |
380 (infoPtr->dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
382 return 0;
385 /***********************************************************************
386 * UPDOWN_Paint
388 * Asynchronous drawing (must ONLY be used in WM_PAINT).
389 * Calls UPDOWN_Draw.
391 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
393 PAINTSTRUCT ps;
394 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
395 hdc = BeginPaint (infoPtr->Self, &ps);
396 UPDOWN_Draw (infoPtr, hdc);
397 EndPaint (infoPtr->Self, &ps);
398 return 0;
401 /***********************************************************************
402 * UPDOWN_KeyPressed
404 * Handle key presses (up & down) when we have to do so
406 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
408 int arrow;
410 if (key == VK_UP) arrow = FLAG_INCR;
411 else if (key == VK_DOWN) arrow = FLAG_DECR;
412 else return 1;
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);
420 return 0;
423 /***********************************************************************
424 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
425 * control.
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 /***********************************************************************
445 * UPDOWN_SetBuddy
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 RECT budRect; /* new coord for the buddy */
459 int x, width; /* new x position and width for the up-down */
460 WNDPROC baseWndProc;
461 WCHAR buddyClass[40];
462 HWND ret;
464 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
466 ret = infoPtr->Buddy;
468 /* there is already a body assigned */
469 if (infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
471 if(!IsWindow(bud))
472 bud = 0;
474 /* Store buddy window handle */
475 infoPtr->Buddy = bud;
477 if(bud) {
479 /* keep upDown ctrl hwnd in a buddy property */
480 SetPropW( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
482 /* Store buddy window class type */
483 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
484 if (GetClassNameW(bud, buddyClass, COUNT_OF(buddyClass))) {
485 if (lstrcmpiW(buddyClass, editW) == 0)
486 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
487 else if (lstrcmpiW(buddyClass, listboxW) == 0)
488 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
491 if(infoPtr->dwStyle & UDS_ARROWKEYS){
492 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
493 when we reset the upDown ctrl buddy to another buddy because it is not
494 good to break the window proc chain. */
495 if (!GetPropW(bud, BUDDY_SUPERCLASS_WNDPROC)) {
496 baseWndProc = (WNDPROC)SetWindowLongPtrW(bud, GWLP_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
497 SetPropW(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
501 /* Get the rect of the buddy relative to its parent */
502 GetWindowRect(infoPtr->Buddy, &budRect);
503 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
505 /* now do the positioning */
506 if (infoPtr->dwStyle & UDS_ALIGNLEFT) {
507 x = budRect.left;
508 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
509 } else if (infoPtr->dwStyle & UDS_ALIGNRIGHT) {
510 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
511 x = budRect.right+DEFAULT_XSEP;
512 } else {
513 /* nothing to do */
514 return ret;
517 /* first adjust the buddy to accommodate the up/down */
518 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
519 budRect.right - budRect.left, budRect.bottom - budRect.top,
520 SWP_NOACTIVATE|SWP_NOZORDER);
522 /* now position the up/down */
523 /* Since the UDS_ALIGN* flags were used, */
524 /* we will pick the position and size of the window. */
525 width = DEFAULT_WIDTH;
528 * If the updown has a buddy border, it has to overlap with the buddy
529 * to look as if it is integrated with the buddy control.
530 * We nudge the control or change its size to overlap.
532 if (UPDOWN_HasBuddyBorder(infoPtr)) {
533 if(infoPtr->dwStyle & UDS_ALIGNLEFT)
534 width += DEFAULT_BUDDYBORDER;
535 else
536 x -= DEFAULT_BUDDYBORDER;
539 SetWindowPos(infoPtr->Self, 0, x,
540 budRect.top - DEFAULT_ADDTOP, width,
541 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
542 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
543 } else {
544 RECT rect;
545 GetWindowRect(infoPtr->Self, &rect);
546 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Self), (POINT *)&rect, 2);
547 SetWindowPos(infoPtr->Self, 0, rect.left, rect.top, DEFAULT_WIDTH, rect.bottom - rect.top,
548 SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOZORDER);
550 return ret;
553 /***********************************************************************
554 * UPDOWN_DoAction
556 * This function increments/decrements the CurVal by the
557 * 'delta' amount according to the 'action' flag which can be a
558 * combination of FLAG_INCR and FLAG_DECR
559 * It notifies the parent as required.
560 * It handles wraping and non-wraping correctly.
561 * It is assumed that delta>0
563 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
565 NM_UPDOWN ni;
567 TRACE("%d by %d\n", action, delta);
569 /* check if we can do the modification first */
570 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
571 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
573 TRACE("current %d, delta: %d\n", infoPtr->CurVal, delta);
575 /* We must notify parent now to obtain permission */
576 ni.iPos = infoPtr->CurVal;
577 ni.iDelta = delta;
578 ni.hdr.hwndFrom = infoPtr->Self;
579 ni.hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
580 ni.hdr.code = UDN_DELTAPOS;
581 if (!SendMessageW(infoPtr->Notify, WM_NOTIFY, (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
582 /* Parent said: OK to adjust */
584 /* Now adjust value with (maybe new) delta */
585 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
586 TRACE("new %d, delta: %d\n", infoPtr->CurVal, ni.iDelta);
588 /* Now take care about our buddy */
589 UPDOWN_SetBuddyInt (infoPtr);
593 /* Also, notify it. This message is sent in any case. */
594 SendMessageW( infoPtr->Notify, (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
595 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal), (LPARAM)infoPtr->Self);
598 /***********************************************************************
599 * UPDOWN_IsEnabled
601 * Returns TRUE if it is enabled as well as its buddy (if any)
602 * FALSE otherwise
604 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
606 if (!IsWindowEnabled(infoPtr->Self))
607 return FALSE;
608 if(infoPtr->Buddy)
609 return IsWindowEnabled(infoPtr->Buddy);
610 return TRUE;
613 /***********************************************************************
614 * UPDOWN_CancelMode
616 * Deletes any timers, releases the mouse and does redraw if necessary.
617 * If the control is not in "capture" mode, it does nothing.
618 * If the control was not in cancel mode, it returns FALSE.
619 * If the control was in cancel mode, it returns TRUE.
621 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
623 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
625 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
626 KillTimer (infoPtr->Self, TIMER_ACCEL);
627 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
629 if (GetCapture() == infoPtr->Self) {
630 NMHDR hdr;
631 hdr.hwndFrom = infoPtr->Self;
632 hdr.idFrom = GetWindowLongPtrW (infoPtr->Self, GWLP_ID);
633 hdr.code = NM_RELEASEDCAPTURE;
634 SendMessageW(infoPtr->Notify, WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
635 ReleaseCapture();
638 infoPtr->Flags &= ~FLAG_PRESSED;
639 InvalidateRect (infoPtr->Self, NULL, FALSE);
641 return TRUE;
644 /***********************************************************************
645 * UPDOWN_HandleMouseEvent
647 * Handle a mouse event for the updown.
648 * 'pt' is the location of the mouse event in client or
649 * windows coordinates.
651 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, INT x, INT y)
653 POINT pt = { x, y };
654 RECT rect;
655 int temp, arrow;
657 TRACE("msg %04x point %s\n", msg, wine_dbgstr_point(&pt));
659 switch(msg)
661 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
663 /* If the buddy is an edit, will set focus to it */
664 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
666 /* Now see which one is the 'active' arrow */
667 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
669 /* Update the flags if we are in/out */
670 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
671 if (arrow)
672 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
673 else
674 if (infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
676 if (infoPtr->Flags & FLAG_ARROW) {
678 /* Update the CurVal if necessary */
679 UPDOWN_GetBuddyInt (infoPtr);
681 /* Set up the correct flags */
682 infoPtr->Flags |= FLAG_PRESSED;
684 /* repaint the control */
685 InvalidateRect (infoPtr->Self, NULL, FALSE);
687 /* process the click */
688 temp = (infoPtr->AccelCount && infoPtr->AccelVect) ? infoPtr->AccelVect[0].nInc : 1;
689 UPDOWN_DoAction (infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
691 /* now capture all mouse messages */
692 SetCapture (infoPtr->Self);
694 /* and startup the first timer */
695 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
697 break;
699 case WM_MOUSEMOVE:
700 /* save the flags to see if any got modified */
701 temp = infoPtr->Flags;
703 /* Now see which one is the 'active' arrow */
704 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
706 /* Update the flags if we are in/out */
707 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
708 if(arrow) {
709 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
710 } else {
711 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
714 /* If state changed, redraw the control */
715 if(temp != infoPtr->Flags)
716 InvalidateRect (infoPtr->Self, &rect, FALSE);
717 break;
719 default:
720 ERR("Impossible case (msg=%x)!\n", msg);
725 /***********************************************************************
726 * UpDownWndProc
728 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
730 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
731 int temp;
733 TRACE("hwnd=%p msg=%04x wparam=%08x lparam=%08lx\n", hwnd, message, wParam, lParam);
735 if (!infoPtr && (message != WM_CREATE))
736 return DefWindowProcW (hwnd, message, wParam, lParam);
738 switch(message)
740 case WM_CREATE:
741 infoPtr = (UPDOWN_INFO*)Alloc (sizeof(UPDOWN_INFO));
742 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
744 /* initialize the info struct */
745 infoPtr->Self = hwnd;
746 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
747 infoPtr->dwStyle = ((LPCREATESTRUCTW)lParam)->style;
748 infoPtr->AccelCount = 0;
749 infoPtr->AccelVect = 0;
750 infoPtr->AccelIndex = -1;
751 infoPtr->CurVal = 0;
752 infoPtr->MinVal = 100;
753 infoPtr->MaxVal = 0;
754 infoPtr->Base = 10; /* Default to base 10 */
755 infoPtr->Buddy = 0; /* No buddy window yet */
756 infoPtr->Flags = 0; /* And no flags */
758 SetWindowLongW (hwnd, GWL_STYLE, infoPtr->dwStyle & ~WS_BORDER);
760 /* Do we pick the buddy win ourselves? */
761 if (infoPtr->dwStyle & UDS_AUTOBUDDY)
762 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
764 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
765 break;
767 case WM_DESTROY:
768 if(infoPtr->AccelVect) Free (infoPtr->AccelVect);
770 if(infoPtr->Buddy) RemovePropW(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
772 Free (infoPtr);
773 SetWindowLongPtrW (hwnd, 0, 0);
774 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
775 break;
777 case WM_ENABLE:
778 if (wParam) {
779 infoPtr->dwStyle &= ~WS_DISABLED;
780 } else {
781 infoPtr->dwStyle |= WS_DISABLED;
782 UPDOWN_CancelMode (infoPtr);
784 InvalidateRect (infoPtr->Self, NULL, FALSE);
785 break;
787 case WM_STYLECHANGED:
788 if (wParam == GWL_STYLE) {
789 infoPtr->dwStyle = ((LPSTYLESTRUCT)lParam)->styleNew;
790 InvalidateRect (infoPtr->Self, NULL, FALSE);
792 break;
794 case WM_TIMER:
795 /* is this the auto-press timer? */
796 if(wParam == TIMER_AUTOPRESS) {
797 KillTimer(hwnd, TIMER_AUTOPRESS);
798 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
799 InvalidateRect(infoPtr->Self, NULL, FALSE);
802 /* if initial timer, kill it and start the repeat timer */
803 if(wParam == TIMER_AUTOREPEAT) {
804 KillTimer(hwnd, TIMER_AUTOREPEAT);
805 /* if no accel info given, used default timer */
806 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
807 infoPtr->AccelIndex = -1;
808 temp = REPEAT_DELAY;
809 } else {
810 infoPtr->AccelIndex = 0; /* otherwise, use it */
811 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
813 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
816 /* now, if the mouse is above us, do the thing...*/
817 if(infoPtr->Flags & FLAG_MOUSEIN) {
818 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
819 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
821 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
822 KillTimer(hwnd, TIMER_ACCEL);
823 infoPtr->AccelIndex++; /* move to the next accel info */
824 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
825 /* make sure we have at least 1ms intervals */
826 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
829 break;
831 case WM_CANCELMODE:
832 return UPDOWN_CancelMode (infoPtr);
834 case WM_LBUTTONUP:
835 if (GetCapture() != infoPtr->Self) break;
837 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
838 (infoPtr->Flags & FLAG_ARROW) ) {
840 SendMessageW( infoPtr->Notify,
841 (infoPtr->dwStyle & UDS_HORZ) ? WM_HSCROLL : WM_VSCROLL,
842 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
843 (LPARAM)hwnd);
844 if (UPDOWN_IsBuddyEdit(infoPtr))
845 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
847 UPDOWN_CancelMode(infoPtr);
848 break;
850 case WM_LBUTTONDOWN:
851 case WM_MOUSEMOVE:
852 if(UPDOWN_IsEnabled(infoPtr))
853 UPDOWN_HandleMouseEvent (infoPtr, message, (SHORT)LOWORD(lParam), (SHORT)HIWORD(lParam));
854 break;
856 case WM_KEYDOWN:
857 if((infoPtr->dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
858 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
859 break;
861 case WM_PAINT:
862 return UPDOWN_Paint (infoPtr, (HDC)wParam);
864 case UDM_GETACCEL:
865 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
866 if (wParam && lParam) {
867 temp = min(infoPtr->AccelCount, wParam);
868 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
869 return temp;
871 return 0;
873 case UDM_SETACCEL:
874 TRACE("UDM_SETACCEL\n");
876 if(infoPtr->AccelVect) {
877 Free (infoPtr->AccelVect);
878 infoPtr->AccelCount = 0;
879 infoPtr->AccelVect = 0;
881 if(wParam==0) return TRUE;
882 infoPtr->AccelVect = Alloc (wParam*sizeof(UDACCEL));
883 if(infoPtr->AccelVect == 0) return FALSE;
884 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
885 infoPtr->AccelCount = wParam;
887 for (temp = 0; temp < wParam; temp++)
888 TRACE("%d: nSec %u nInc %u\n", temp, infoPtr->AccelVect[temp].nSec, infoPtr->AccelVect[temp].nInc);
890 return TRUE;
892 case UDM_GETBASE:
893 return infoPtr->Base;
895 case UDM_SETBASE:
896 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
897 if (wParam==10 || wParam==16) {
898 temp = infoPtr->Base;
899 infoPtr->Base = wParam;
900 return temp;
902 break;
904 case UDM_GETBUDDY:
905 return (LRESULT)infoPtr->Buddy;
907 case UDM_SETBUDDY:
908 return (LRESULT)UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
910 case UDM_GETPOS:
911 temp = UPDOWN_GetBuddyInt (infoPtr);
912 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
914 case UDM_SETPOS:
915 temp = (short)LOWORD(lParam);
916 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
917 if(!UPDOWN_InBounds(infoPtr, temp)) {
918 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
919 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
921 wParam = infoPtr->CurVal;
922 infoPtr->CurVal = temp;
923 UPDOWN_SetBuddyInt (infoPtr);
924 return wParam; /* return prev value */
926 case UDM_GETRANGE:
927 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
929 case UDM_SETRANGE:
930 /* we must have: */
931 infoPtr->MaxVal = (short)(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
932 infoPtr->MinVal = (short)HIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
933 /* |Max-Min| <= UD_MAXVAL */
934 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
935 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
936 break;
938 case UDM_GETRANGE32:
939 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
940 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
941 break;
943 case UDM_SETRANGE32:
944 infoPtr->MinVal = (INT)wParam;
945 infoPtr->MaxVal = (INT)lParam;
946 if (infoPtr->MaxVal <= infoPtr->MinVal)
947 infoPtr->MaxVal = infoPtr->MinVal + 1;
948 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
949 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
950 break;
952 case UDM_GETPOS32:
953 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
954 return infoPtr->CurVal;
956 case UDM_SETPOS32:
957 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
958 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
959 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
961 temp = infoPtr->CurVal; /* save prev value */
962 infoPtr->CurVal = (int)lParam; /* set the new value */
963 UPDOWN_SetBuddyInt (infoPtr);
964 return temp; /* return prev value */
966 case UDM_GETUNICODEFORMAT:
967 /* we lie a bit here, we're always using Unicode internally */
968 return infoPtr->UnicodeFormat;
970 case UDM_SETUNICODEFORMAT:
971 /* do we really need to honour this flag? */
972 temp = infoPtr->UnicodeFormat;
973 infoPtr->UnicodeFormat = (BOOL)wParam;
974 return temp;
976 default:
977 if ((message >= WM_USER) && (message < WM_APP))
978 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
979 return DefWindowProcW (hwnd, message, wParam, lParam);
982 return 0;
985 /***********************************************************************
986 * UPDOWN_Register [Internal]
988 * Registers the updown window class.
990 void UPDOWN_Register(void)
992 WNDCLASSW wndClass;
994 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
995 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
996 wndClass.lpfnWndProc = UpDownWindowProc;
997 wndClass.cbClsExtra = 0;
998 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
999 wndClass.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
1000 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
1001 wndClass.lpszClassName = UPDOWN_CLASSW;
1003 RegisterClassW( &wndClass );
1007 /***********************************************************************
1008 * UPDOWN_Unregister [Internal]
1010 * Unregisters the updown window class.
1012 void UPDOWN_Unregister (void)
1014 UnregisterClassW (UPDOWN_CLASSW, NULL);