- fix the "int format, HANDLE arg" type of warnings for comctl32
[wine/multimedia.git] / dlls / comctl32 / updown.c
blob3cff9ac57a1c34ff176395682d50d2d9788562fc
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 <stdio.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "winuser.h"
39 #include "commctrl.h"
40 #include "winnls.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(updown);
46 typedef struct
48 HWND Self; /* Handle to this up-down control */
49 UINT AccelCount; /* Number of elements in AccelVect */
50 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
51 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
52 INT Base; /* Base to display nr in the buddy window */
53 INT CurVal; /* Current up-down value */
54 INT MinVal; /* Minimum up-down value */
55 INT MaxVal; /* Maximum up-down value */
56 HWND Buddy; /* Handle to the buddy window */
57 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
58 INT Flags; /* Internal Flags FLAG_* */
59 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
60 } UPDOWN_INFO;
62 /* Control configuration constants */
64 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
65 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
66 #define REPEAT_DELAY 50 /* delay between auto-increments */
68 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
69 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
70 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
71 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
72 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
73 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
76 /* Work constants */
78 #define FLAG_INCR 0x01
79 #define FLAG_DECR 0x02
80 #define FLAG_MOUSEIN 0x04
81 #define FLAG_PRESSED 0x08
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
89 #define TIMER_ACCEL 2
90 #define TIMER_AUTOPRESS 3
92 #define BUDDY_UPDOWN_HWND "buddyUpDownHWND"
93 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
95 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
96 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
98 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
100 /***********************************************************************
101 * UPDOWN_IsBuddyEdit
102 * Tests if our buddy is an edit control.
104 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
106 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
109 /***********************************************************************
110 * UPDOWN_IsBuddyListbox
111 * Tests if our buddy is a listbox control.
113 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
115 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
118 /***********************************************************************
119 * UPDOWN_InBounds
120 * Tests if a given value 'val' is between the Min&Max limits
122 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
124 if(infoPtr->MaxVal > infoPtr->MinVal)
125 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
126 else
127 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
130 /***********************************************************************
131 * UPDOWN_OffsetVal
132 * Change the current value by delta.
133 * It returns TRUE is the value was changed successfuly, or FALSE
134 * if the value was not changed, as it would go out of bounds.
136 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
138 /* check if we can do the modification first */
139 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
140 if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP) {
141 delta += (delta < 0 ? -1 : 1) *
142 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
143 (infoPtr->MinVal - infoPtr->MaxVal) +
144 (delta < 0 ? 1 : -1);
145 } else return FALSE;
148 infoPtr->CurVal += delta;
149 return TRUE;
152 /***********************************************************************
153 * UPDOWN_HasBuddyBorder
155 * When we have a buddy set and that we are aligned on our buddy, we
156 * want to draw a sunken edge to make like we are part of that control.
158 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
160 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
162 return ( ((dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
163 UPDOWN_IsBuddyEdit(infoPtr) );
166 /***********************************************************************
167 * UPDOWN_GetArrowRect
168 * wndPtr - pointer to the up-down wnd
169 * rect - will hold the rectangle
170 * arrow - FLAG_INCR to get the "increment" rect (up or right)
171 * FLAG_DECR to get the "decrement" rect (down or left)
172 * If both flags are pressent, the envelope is returned.
174 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
176 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
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 (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 (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 (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 (!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 if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
275 sep = UPDOWN_GetThousandSep();
277 /* now get rid of the separators */
278 for(src = dst = txt; *src; src++)
279 if(*src != sep) *dst++ = *src;
280 *dst = 0;
282 /* try to convert the number and validate it */
283 newVal = strtolW(txt, &src, infoPtr->Base);
284 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
287 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
288 infoPtr->CurVal = newVal;
289 return TRUE;
293 /***********************************************************************
294 * UPDOWN_SetBuddyInt
295 * Tries to set the pos to the buddy window based on current pos
296 * returns:
297 * TRUE - if it set the caption of the buddy successfully
298 * FALSE - if an error occurred
300 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
302 WCHAR fmt[3] = { '%', 'd', '\0' };
303 WCHAR txt[20];
304 int len;
306 if (!IsWindow(infoPtr->Buddy)) return FALSE;
308 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
310 /*if the buddy is a list window, we must set curr index */
311 if (UPDOWN_IsBuddyListbox(infoPtr)) {
312 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
315 /* Regular window, so set caption to the number */
316 if (infoPtr->Base == 16) fmt[1] = 'X';
317 len = wsprintfW(txt, fmt, infoPtr->CurVal);
320 /* Do thousands seperation if necessary */
321 if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
322 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
323 WCHAR sep = UPDOWN_GetThousandSep();
324 int start = len % 3;
326 memcpy(tmp, txt, sizeof(txt));
327 if (start == 0) start = 3;
328 dst += start;
329 src += start;
330 for (len=0; *src; len++) {
331 if (len % 3 == 0) *dst++ = sep;
332 *dst++ = *src++;
334 *dst = 0;
337 return SetWindowTextW(infoPtr->Buddy, txt);
340 /***********************************************************************
341 * UPDOWN_Draw
343 * Draw the arrows. The background need not be erased.
345 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
347 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
348 BOOL pressed, hot;
349 RECT rect;
351 /* Draw the common border between ourselves and our buddy */
352 if (UPDOWN_HasBuddyBorder(infoPtr)) {
353 GetClientRect(infoPtr->Self, &rect);
354 DrawEdge(hdc, &rect, EDGE_SUNKEN,
355 BF_BOTTOM | BF_TOP |
356 (dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
359 /* Draw the incr button */
360 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
361 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
362 hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
363 DrawFrameControl(hdc, &rect, DFC_SCROLL,
364 (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
365 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
366 (pressed ? DFCS_PUSHED : 0) |
367 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
369 /* Draw the decr button */
370 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
371 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
372 hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
373 DrawFrameControl(hdc, &rect, DFC_SCROLL,
374 (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
375 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
376 (pressed ? DFCS_PUSHED : 0) |
377 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
379 return 0;
382 /***********************************************************************
383 * UPDOWN_Paint
385 * Asynchronous drawing (must ONLY be used in WM_PAINT).
386 * Calls UPDOWN_Draw.
388 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
390 PAINTSTRUCT ps;
391 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
392 hdc = BeginPaint (infoPtr->Self, &ps);
393 UPDOWN_Draw (infoPtr, hdc);
394 EndPaint (infoPtr->Self, &ps);
395 return 0;
398 /***********************************************************************
399 * UPDOWN_KeyPressed
401 * Handle key presses (up & down) when we have to do so
403 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
405 int arrow;
407 if (key == VK_UP) arrow = FLAG_INCR;
408 else if (key == VK_DOWN) arrow = FLAG_DECR;
409 else return 1;
411 UPDOWN_GetBuddyInt (infoPtr);
412 infoPtr->Flags &= ~FLAG_ARROW;
413 infoPtr->Flags |= FLAG_PRESSED | arrow;
414 InvalidateRect (infoPtr->Self, NULL, FALSE);
415 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
416 UPDOWN_DoAction (infoPtr, 1, arrow);
417 return 0;
420 /***********************************************************************
421 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
422 * control.
424 static LRESULT CALLBACK
425 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
427 WNDPROC superClassWndProc = (WNDPROC)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
428 TRACE("hwnd=%p, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
429 hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
431 if (uMsg == WM_KEYDOWN) {
432 HWND upDownHwnd = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
434 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
437 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
440 /***********************************************************************
441 * UPDOWN_SetBuddy
442 * Tests if 'bud' is a valid window handle. If not, returns FALSE.
443 * Else, sets it as a new Buddy.
444 * Then, it should subclass the buddy
445 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
446 * process the UP/DOWN arrow keys.
447 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
448 * the size/pos of the buddy and the control are adjusted accordingly.
450 static BOOL UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
452 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
453 RECT budRect; /* new coord for the buddy */
454 int x, width; /* new x position and width for the up-down */
455 WNDPROC baseWndProc;
456 CHAR buddyClass[40];
458 /* Is it a valid bud? */
459 if(!IsWindow(bud)) return FALSE;
461 TRACE("(hwnd=%p, bud=%p)\n", infoPtr->Self, bud);
463 /* there is already a body assigned */
464 if (infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
466 /* Store buddy window handle */
467 infoPtr->Buddy = bud;
469 /* keep upDown ctrl hwnd in a buddy property */
470 SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
472 /* Store buddy window class type */
473 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
474 if (GetClassNameA(bud, buddyClass, COUNT_OF(buddyClass))) {
475 if (lstrcmpiA(buddyClass, "Edit") == 0)
476 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
477 else if (lstrcmpiA(buddyClass, "Listbox") == 0)
478 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
481 if(dwStyle & UDS_ARROWKEYS){
482 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
483 when we reset the upDown ctrl buddy to another buddy because it is not
484 good to break the window proc chain. */
485 if (!GetPropA(bud, BUDDY_SUPERCLASS_WNDPROC)) {
486 baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
487 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
491 /* Get the rect of the buddy relative to its parent */
492 GetWindowRect(infoPtr->Buddy, &budRect);
493 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
495 /* now do the positioning */
496 if (dwStyle & UDS_ALIGNLEFT) {
497 x = budRect.left;
498 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
499 } else if (dwStyle & UDS_ALIGNRIGHT) {
500 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
501 x = budRect.right+DEFAULT_XSEP;
502 } else {
503 x = budRect.right+DEFAULT_XSEP;
506 /* first adjust the buddy to accomodate the up/down */
507 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
508 budRect.right - budRect.left, budRect.bottom - budRect.top,
509 SWP_NOACTIVATE|SWP_NOZORDER);
511 /* now position the up/down */
512 /* Since the UDS_ALIGN* flags were used, */
513 /* we will pick the position and size of the window. */
514 width = DEFAULT_WIDTH;
517 * If the updown has a buddy border, it has to overlap with the buddy
518 * to look as if it is integrated with the buddy control.
519 * We nudge the control or change it size to overlap.
521 if (UPDOWN_HasBuddyBorder(infoPtr)) {
522 if(dwStyle & UDS_ALIGNLEFT)
523 width += DEFAULT_BUDDYBORDER;
524 else
525 x -= DEFAULT_BUDDYBORDER;
528 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
529 budRect.top - DEFAULT_ADDTOP, width,
530 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
531 SWP_NOACTIVATE);
533 return TRUE;
536 /***********************************************************************
537 * UPDOWN_DoAction
539 * This function increments/decrements the CurVal by the
540 * 'delta' amount according to the 'action' flag which can be a
541 * combination of FLAG_INCR and FLAG_DECR
542 * It notifies the parent as required.
543 * It handles wraping and non-wraping correctly.
544 * It is assumed that delta>0
546 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
548 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
549 NM_UPDOWN ni;
551 TRACE("%d by %d\n", action, delta);
553 /* check if we can do the modification first */
554 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
555 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
557 /* We must notify parent now to obtain permission */
558 ni.iPos = infoPtr->CurVal;
559 ni.iDelta = delta;
560 ni.hdr.hwndFrom = infoPtr->Self;
561 ni.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
562 ni.hdr.code = UDN_DELTAPOS;
563 if (!SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY,
564 (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
565 /* Parent said: OK to adjust */
567 /* Now adjust value with (maybe new) delta */
568 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
569 /* Now take care about our buddy */
570 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
574 /* Also, notify it. This message is sent in any case. */
575 SendMessageW( GetParent(infoPtr->Self),
576 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
577 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal),
578 (LPARAM)infoPtr->Self);
581 /***********************************************************************
582 * UPDOWN_IsEnabled
584 * Returns TRUE if it is enabled as well as its buddy (if any)
585 * FALSE otherwise
587 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
589 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
590 return FALSE;
591 if(infoPtr->Buddy)
592 return IsWindowEnabled(infoPtr->Buddy);
593 return TRUE;
596 /***********************************************************************
597 * UPDOWN_CancelMode
599 * Deletes any timers, releases the mouse and does redraw if necessary.
600 * If the control is not in "capture" mode, it does nothing.
601 * If the control was not in cancel mode, it returns FALSE.
602 * If the control was in cancel mode, it returns TRUE.
604 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
606 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
608 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
609 KillTimer (infoPtr->Self, TIMER_ACCEL);
610 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
612 if (GetCapture() == infoPtr->Self) {
613 NMHDR hdr;
614 hdr.hwndFrom = infoPtr->Self;
615 hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
616 hdr.code = NM_RELEASEDCAPTURE;
617 SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
618 ReleaseCapture();
621 infoPtr->Flags &= ~FLAG_PRESSED;
622 InvalidateRect (infoPtr->Self, NULL, FALSE);
624 return TRUE;
627 /***********************************************************************
628 * UPDOWN_HandleMouseEvent
630 * Handle a mouse event for the updown.
631 * 'pt' is the location of the mouse event in client or
632 * windows coordinates.
634 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINTS pts)
636 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
637 POINT pt = { pts.x, pts.y };
638 RECT rect;
639 int temp, arrow;
641 switch(msg)
643 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
644 /* If we are inside an arrow, then nothing to do */
645 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
647 /* If the buddy is an edit, will set focus to it */
648 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
650 /* Now see which one is the 'active' arrow */
651 if (infoPtr->Flags & FLAG_ARROW) {
653 /* Update the CurVal if necessary */
654 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
656 /* Set up the correct flags */
657 infoPtr->Flags |= FLAG_PRESSED;
659 /* repaint the control */
660 InvalidateRect (infoPtr->Self, NULL, FALSE);
662 /* process the click */
663 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
665 /* now capture all mouse messages */
666 SetCapture (infoPtr->Self);
668 /* and startup the first timer */
669 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
671 break;
673 case WM_MOUSEMOVE:
674 /* save the flags to see if any got modified */
675 temp = infoPtr->Flags;
677 /* Now see which one is the 'active' arrow */
678 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
680 /* Update the flags if we are in/out */
681 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
682 if(arrow) {
683 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
684 } else {
685 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
688 /* If state changed, redraw the control */
689 if(temp != infoPtr->Flags)
690 InvalidateRect (infoPtr->Self, &rect, FALSE);
691 break;
693 default:
694 ERR("Impossible case (msg=%x)!\n", msg);
699 /***********************************************************************
700 * UpDownWndProc
702 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
703 LPARAM lParam)
705 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
706 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
707 int temp;
709 if (!infoPtr && (message != WM_CREATE))
710 return DefWindowProcW (hwnd, message, wParam, lParam);
712 switch(message)
714 case WM_CREATE:
715 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
716 infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
717 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
719 /* initialize the info struct */
720 infoPtr->Self = hwnd;
721 infoPtr->AccelCount = 0;
722 infoPtr->AccelVect = 0;
723 infoPtr->AccelIndex = -1;
724 infoPtr->CurVal = 0;
725 infoPtr->MinVal = 0;
726 infoPtr->MaxVal = 9999;
727 infoPtr->Base = 10; /* Default to base 10 */
728 infoPtr->Buddy = 0; /* No buddy window yet */
729 infoPtr->Flags = 0; /* And no flags */
731 /* Do we pick the buddy win ourselves? */
732 if (dwStyle & UDS_AUTOBUDDY)
733 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
735 TRACE("UpDown Ctrl creation, hwnd=%p\n", hwnd);
736 break;
738 case WM_DESTROY:
739 if(infoPtr->AccelVect) COMCTL32_Free (infoPtr->AccelVect);
741 if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
743 COMCTL32_Free (infoPtr);
744 SetWindowLongW (hwnd, 0, 0);
745 TRACE("UpDown Ctrl destruction, hwnd=%p\n", hwnd);
746 break;
748 case WM_ENABLE:
749 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
750 InvalidateRect (infoPtr->Self, NULL, FALSE);
751 break;
753 case WM_TIMER:
754 /* is this the auto-press timer? */
755 if(wParam == TIMER_AUTOPRESS) {
756 KillTimer(hwnd, TIMER_AUTOPRESS);
757 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
758 InvalidateRect(infoPtr->Self, NULL, FALSE);
761 /* if initial timer, kill it and start the repeat timer */
762 if(wParam == TIMER_AUTOREPEAT) {
763 KillTimer(hwnd, TIMER_AUTOREPEAT);
764 /* if no accel info given, used default timer */
765 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
766 infoPtr->AccelIndex = -1;
767 temp = REPEAT_DELAY;
768 } else {
769 infoPtr->AccelIndex = 0; /* otherwise, use it */
770 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
772 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
775 /* now, if the mouse is above us, do the thing...*/
776 if(infoPtr->Flags & FLAG_MOUSEIN) {
777 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
778 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
780 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
781 KillTimer(hwnd, TIMER_ACCEL);
782 infoPtr->AccelIndex++; /* move to the next accel info */
783 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
784 /* make sure we have at least 1ms intervals */
785 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
788 break;
790 case WM_CANCELMODE:
791 return UPDOWN_CancelMode (infoPtr);
793 case WM_LBUTTONUP:
794 if (GetCapture() != infoPtr->Self) break;
796 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
797 (infoPtr->Flags & FLAG_ARROW) ) {
799 SendMessageW( GetParent(hwnd),
800 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
801 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
802 (LPARAM)hwnd);
803 if (UPDOWN_IsBuddyEdit(infoPtr))
804 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
806 UPDOWN_CancelMode(infoPtr);
807 break;
809 case WM_LBUTTONDOWN:
810 case WM_MOUSEMOVE:
811 if(UPDOWN_IsEnabled(infoPtr))
812 UPDOWN_HandleMouseEvent (infoPtr, message, MAKEPOINTS(lParam));
813 break;
815 case WM_KEYDOWN:
816 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
817 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
818 break;
820 case WM_PAINT:
821 return UPDOWN_Paint (infoPtr, (HDC)wParam);
823 case UDM_GETACCEL:
824 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
825 if (wParam && lParam) {
826 temp = min(infoPtr->AccelCount, wParam);
827 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
828 return temp;
830 return 0;
832 case UDM_SETACCEL:
833 TRACE("UpDown Ctrl new accel info, hwnd=%p\n", hwnd);
834 if(infoPtr->AccelVect) {
835 COMCTL32_Free (infoPtr->AccelVect);
836 infoPtr->AccelCount = 0;
837 infoPtr->AccelVect = 0;
839 if(wParam==0) return TRUE;
840 infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
841 if(infoPtr->AccelVect == 0) return FALSE;
842 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
843 return TRUE;
845 case UDM_GETBASE:
846 return infoPtr->Base;
848 case UDM_SETBASE:
849 TRACE("UpDown Ctrl new base(%d), hwnd=%p\n", wParam, hwnd);
850 if (wParam==10 || wParam==16) {
851 temp = infoPtr->Base;
852 infoPtr->Base = wParam;
853 return temp;
855 break;
857 case UDM_GETBUDDY:
858 return (LRESULT)infoPtr->Buddy;
860 case UDM_SETBUDDY:
861 temp = (int)infoPtr->Buddy;
862 UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
863 return temp;
865 case UDM_GETPOS:
866 temp = UPDOWN_GetBuddyInt (infoPtr);
867 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
869 case UDM_SETPOS:
870 temp = SLOWORD(lParam);
871 TRACE("UpDown Ctrl new value(%d), hwnd=%p\n", temp, hwnd);
872 if(!UPDOWN_InBounds(infoPtr, temp)) {
873 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
874 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
876 wParam = infoPtr->CurVal;
877 infoPtr->CurVal = temp;
878 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
879 return wParam; /* return prev value */
881 case UDM_GETRANGE:
882 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
884 case UDM_SETRANGE:
885 /* we must have: */
886 infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
887 infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
888 /* |Max-Min| <= UD_MAXVAL */
889 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
890 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
891 break;
893 case UDM_GETRANGE32:
894 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
895 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
896 break;
898 case UDM_SETRANGE32:
899 infoPtr->MinVal = (INT)wParam;
900 infoPtr->MaxVal = (INT)lParam;
901 if (infoPtr->MaxVal <= infoPtr->MinVal)
902 infoPtr->MaxVal = infoPtr->MinVal + 1;
903 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%p\n",
904 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
905 break;
907 case UDM_GETPOS32:
908 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
909 return infoPtr->CurVal;
911 case UDM_SETPOS32:
912 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
913 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
914 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
916 temp = infoPtr->CurVal; /* save prev value */
917 infoPtr->CurVal = (int)lParam; /* set the new value */
918 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
919 return temp; /* return prev value */
921 case UDM_GETUNICODEFORMAT:
922 /* we lie a bit here, we're always using Unicode internally */
923 return infoPtr->UnicodeFormat;
925 case UDM_SETUNICODEFORMAT:
926 /* do we really need to honour this flag? */
927 temp = infoPtr->UnicodeFormat;
928 infoPtr->UnicodeFormat = (BOOL)wParam;
929 return temp;
931 default:
932 if ((message >= WM_USER) && (message < WM_APP))
933 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
934 return DefWindowProcW (hwnd, message, wParam, lParam);
937 return 0;
940 /***********************************************************************
941 * UPDOWN_Register [Internal]
943 * Registers the updown window class.
945 void UPDOWN_Register(void)
947 WNDCLASSW wndClass;
949 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
950 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW;
951 wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
952 wndClass.cbClsExtra = 0;
953 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
954 wndClass.hCursor = LoadCursorW( 0, IDC_ARROWW );
955 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
956 wndClass.lpszClassName = UPDOWN_CLASSW;
958 RegisterClassW( &wndClass );
962 /***********************************************************************
963 * UPDOWN_Unregister [Internal]
965 * Unregisters the updown window class.
967 void UPDOWN_Unregister (void)
969 UnregisterClassW (UPDOWN_CLASSW, (HINSTANCE)NULL);