Moved mode setting out of .spec file into Makefile.
[wine/gsoc_dplay.git] / dlls / comctl32 / updown.c
blob260c525ca805b40cfa38b032db5a60a9e63f3c7e
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
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "commctrl.h"
31 #include "winnls.h"
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(updown);
37 typedef struct
39 HWND Self; /* Handle to this up-down control */
40 UINT AccelCount; /* Number of elements in AccelVect */
41 UDACCEL* AccelVect; /* Vector containing AccelCount elements */
42 INT AccelIndex; /* Current accel index, -1 if not accel'ing */
43 INT Base; /* Base to display nr in the buddy window */
44 INT CurVal; /* Current up-down value */
45 INT MinVal; /* Minimum up-down value */
46 INT MaxVal; /* Maximum up-down value */
47 HWND Buddy; /* Handle to the buddy window */
48 INT BuddyType; /* Remembers the buddy type BUDDY_TYPE_* */
49 INT Flags; /* Internal Flags FLAG_* */
50 BOOL UnicodeFormat; /* Marks the use of Unicode internally */
51 } UPDOWN_INFO;
53 /* Control configuration constants */
55 #define INITIAL_DELAY 500 /* initial timer until auto-inc kicks in */
56 #define AUTOPRESS_DELAY 250 /* time to keep arrow pressed on KEY_DOWN */
57 #define REPEAT_DELAY 50 /* delay between auto-increments */
59 #define DEFAULT_WIDTH 14 /* default width of the ctrl */
60 #define DEFAULT_XSEP 0 /* default separation between buddy and ctrl */
61 #define DEFAULT_ADDTOP 0 /* amount to extend above the buddy window */
62 #define DEFAULT_ADDBOT 0 /* amount to extend below the buddy window */
63 #define DEFAULT_BUDDYBORDER 2 /* Width/height of the buddy border */
64 #define DEFAULT_BUDDYSPACER 2 /* Spacer between the buddy and the ctrl */
67 /* Work constants */
69 #define FLAG_INCR 0x01
70 #define FLAG_DECR 0x02
71 #define FLAG_MOUSEIN 0x04
72 #define FLAG_PRESSED 0x08
73 #define FLAG_ARROW (FLAG_INCR | FLAG_DECR)
75 #define BUDDY_TYPE_UNKNOWN 0
76 #define BUDDY_TYPE_LISTBOX 1
77 #define BUDDY_TYPE_EDIT 2
79 #define TIMER_AUTOREPEAT 1
80 #define TIMER_ACCEL 2
81 #define TIMER_AUTOPRESS 3
83 #define BUDDY_UPDOWN_HWND "buddyUpDownHWND"
84 #define BUDDY_SUPERCLASS_WNDPROC "buddySupperClassWndProc"
86 #define UPDOWN_GetInfoPtr(hwnd) ((UPDOWN_INFO *)GetWindowLongA (hwnd,0))
87 #define COUNT_OF(a) (sizeof(a)/sizeof(a[0]))
89 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action);
91 /***********************************************************************
92 * UPDOWN_IsBuddyEdit
93 * Tests if our buddy is an edit control.
95 static inline BOOL UPDOWN_IsBuddyEdit(UPDOWN_INFO *infoPtr)
97 return infoPtr->BuddyType == BUDDY_TYPE_EDIT;
100 /***********************************************************************
101 * UPDOWN_IsBuddyListbox
102 * Tests if our buddy is a listbox control.
104 static inline BOOL UPDOWN_IsBuddyListbox(UPDOWN_INFO *infoPtr)
106 return infoPtr->BuddyType == BUDDY_TYPE_LISTBOX;
109 /***********************************************************************
110 * UPDOWN_InBounds
111 * Tests if a given value 'val' is between the Min&Max limits
113 static BOOL UPDOWN_InBounds(UPDOWN_INFO *infoPtr, int val)
115 if(infoPtr->MaxVal > infoPtr->MinVal)
116 return (infoPtr->MinVal <= val) && (val <= infoPtr->MaxVal);
117 else
118 return (infoPtr->MaxVal <= val) && (val <= infoPtr->MinVal);
121 /***********************************************************************
122 * UPDOWN_OffsetVal
123 * Change the current value by delta.
124 * It returns TRUE is the value was changed successfuly, or FALSE
125 * if the value was not changed, as it would go out of bounds.
127 static BOOL UPDOWN_OffsetVal(UPDOWN_INFO *infoPtr, int delta)
129 /* check if we can do the modification first */
130 if(!UPDOWN_InBounds (infoPtr, infoPtr->CurVal+delta)) {
131 if (GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_WRAP) {
132 delta += (delta < 0 ? -1 : 1) *
133 (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1) *
134 (infoPtr->MinVal - infoPtr->MaxVal) +
135 (delta < 0 ? 1 : -1);
136 } else return FALSE;
139 infoPtr->CurVal += delta;
140 return TRUE;
143 /***********************************************************************
144 * UPDOWN_HasBuddyBorder
146 * When we have a buddy set and that we are aligned on our buddy, we
147 * want to draw a sunken edge to make like we are part of that control.
149 static BOOL UPDOWN_HasBuddyBorder(UPDOWN_INFO* infoPtr)
151 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
153 return ( ((dwStyle & (UDS_ALIGNLEFT | UDS_ALIGNRIGHT)) != 0) &&
154 UPDOWN_IsBuddyEdit(infoPtr) );
157 /***********************************************************************
158 * UPDOWN_GetArrowRect
159 * wndPtr - pointer to the up-down wnd
160 * rect - will hold the rectangle
161 * arrow - FLAG_INCR to get the "increment" rect (up or right)
162 * FLAG_DECR to get the "decrement" rect (down or left)
163 * If both flags are pressent, the envelope is returned.
165 static void UPDOWN_GetArrowRect (UPDOWN_INFO* infoPtr, RECT *rect, int arrow)
167 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
169 GetClientRect (infoPtr->Self, rect);
172 * Make sure we calculate the rectangle to fit even if we draw the
173 * border.
175 if (UPDOWN_HasBuddyBorder(infoPtr)) {
176 if (dwStyle & UDS_ALIGNLEFT)
177 rect->left += DEFAULT_BUDDYBORDER;
178 else
179 rect->right -= DEFAULT_BUDDYBORDER;
181 InflateRect(rect, 0, -DEFAULT_BUDDYBORDER);
184 /* now figure out if we need a space away from the buddy */
185 if ( IsWindow(infoPtr->Buddy) ) {
186 if (dwStyle & UDS_ALIGNLEFT) rect->right -= DEFAULT_BUDDYSPACER;
187 else rect->left += DEFAULT_BUDDYSPACER;
191 * We're calculating the midpoint to figure-out where the
192 * separation between the buttons will lay. We make sure that we
193 * round the uneven numbers by adding 1.
195 if (dwStyle & UDS_HORZ) {
196 int len = rect->right - rect->left + 1; /* compute the width */
197 if (arrow & FLAG_INCR)
198 rect->left = rect->left + len/2;
199 if (arrow & FLAG_DECR)
200 rect->right = rect->left + len/2 - 1;
201 } else {
202 int len = rect->bottom - rect->top + 1; /* compute the height */
203 if (arrow & FLAG_INCR)
204 rect->bottom = rect->top + len/2 - 1;
205 if (arrow & FLAG_DECR)
206 rect->top = rect->top + len/2;
210 /***********************************************************************
211 * UPDOWN_GetArrowFromPoint
212 * Returns the rectagle (for the up or down arrow) that contains pt.
213 * If it returns the up rect, it returns TRUE.
214 * If it returns the down rect, it returns FALSE.
216 static BOOL UPDOWN_GetArrowFromPoint (UPDOWN_INFO* infoPtr, RECT *rect, POINT pt)
218 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_INCR);
219 if(PtInRect(rect, pt)) return FLAG_INCR;
221 UPDOWN_GetArrowRect (infoPtr, rect, FLAG_DECR);
222 if(PtInRect(rect, pt)) return FLAG_DECR;
224 return 0;
228 /***********************************************************************
229 * UPDOWN_GetThousandSep
230 * Returns the thousand sep. If an error occurs, it returns ','.
232 static WCHAR UPDOWN_GetThousandSep()
234 WCHAR sep[2];
236 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, sep, 2) != 1)
237 sep[0] = ',';
239 return sep[0];
242 /***********************************************************************
243 * UPDOWN_GetBuddyInt
244 * Tries to read the pos from the buddy window and if it succeeds,
245 * it stores it in the control's CurVal
246 * returns:
247 * TRUE - if it read the integer from the buddy successfully
248 * FALSE - if an error occurred
250 static BOOL UPDOWN_GetBuddyInt (UPDOWN_INFO *infoPtr)
252 WCHAR txt[20], sep, *src, *dst;
253 int newVal;
255 if (!IsWindow(infoPtr->Buddy))
256 return FALSE;
258 /*if the buddy is a list window, we must set curr index */
259 if (UPDOWN_IsBuddyListbox(infoPtr)) {
260 newVal = SendMessageW(infoPtr->Buddy, LB_GETCARETINDEX, 0, 0);
261 if(newVal < 0) return FALSE;
262 } else {
263 /* we have a regular window, so will get the text */
264 if (!GetWindowTextW(infoPtr->Buddy, txt, COUNT_OF(txt))) return FALSE;
266 sep = UPDOWN_GetThousandSep();
268 /* now get rid of the separators */
269 for(src = dst = txt; *src; src++)
270 if(*src != sep) *dst++ = *src;
271 *dst = 0;
273 /* try to convert the number and validate it */
274 newVal = strtolW(txt, &src, infoPtr->Base);
275 if(*src || !UPDOWN_InBounds (infoPtr, newVal)) return FALSE;
278 TRACE("new value(%d) from buddy (old=%d)\n", newVal, infoPtr->CurVal);
279 infoPtr->CurVal = newVal;
280 return TRUE;
284 /***********************************************************************
285 * UPDOWN_SetBuddyInt
286 * Tries to set the pos to the buddy window based on current pos
287 * returns:
288 * TRUE - if it set the caption of the buddy successfully
289 * FALSE - if an error occurred
291 static BOOL UPDOWN_SetBuddyInt (UPDOWN_INFO *infoPtr)
293 WCHAR fmt[3] = { '%', 'd', '\0' };
294 WCHAR txt[20];
295 int len;
297 if (!IsWindow(infoPtr->Buddy)) return FALSE;
299 TRACE("set new value(%d) to buddy.\n", infoPtr->CurVal);
301 /*if the buddy is a list window, we must set curr index */
302 if (UPDOWN_IsBuddyListbox(infoPtr)) {
303 return SendMessageW(infoPtr->Buddy, LB_SETCURSEL, infoPtr->CurVal, 0) != LB_ERR;
306 /* Regular window, so set caption to the number */
307 if (infoPtr->Base == 16) fmt[1] = 'X';
308 len = wsprintfW(txt, fmt, infoPtr->CurVal);
311 /* Do thousands seperation if necessary */
312 if (!(GetWindowLongW (infoPtr->Self, GWL_STYLE) & UDS_NOTHOUSANDS) && (len > 3)) {
313 WCHAR tmp[COUNT_OF(txt)], *src = tmp, *dst = txt;
314 WCHAR sep = UPDOWN_GetThousandSep();
315 int start = len % 3;
317 memcpy(tmp, txt, sizeof(txt));
318 if (start == 0) start = 3;
319 dst += start;
320 src += start;
321 for (len=0; *src; len++) {
322 if (len % 3 == 0) *dst++ = sep;
323 *dst++ = *src++;
325 *dst = 0;
328 return SetWindowTextW(infoPtr->Buddy, txt);
331 /***********************************************************************
332 * UPDOWN_Draw
334 * Draw the arrows. The background need not be erased.
336 static LRESULT UPDOWN_Draw (UPDOWN_INFO *infoPtr, HDC hdc)
338 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
339 BOOL pressed, hot;
340 RECT rect;
342 /* Draw the common border between ourselves and our buddy */
343 if (UPDOWN_HasBuddyBorder(infoPtr)) {
344 GetClientRect(infoPtr->Self, &rect);
345 DrawEdge(hdc, &rect, EDGE_SUNKEN,
346 BF_BOTTOM | BF_TOP |
347 (dwStyle & UDS_ALIGNLEFT ? BF_LEFT : BF_RIGHT));
350 /* Draw the incr button */
351 UPDOWN_GetArrowRect (infoPtr, &rect, FLAG_INCR);
352 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_INCR);
353 hot = (infoPtr->Flags & FLAG_INCR) && (infoPtr->Flags & FLAG_MOUSEIN);
354 DrawFrameControl(hdc, &rect, DFC_SCROLL,
355 (dwStyle & UDS_HORZ ? DFCS_SCROLLRIGHT : DFCS_SCROLLUP) |
356 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
357 (pressed ? DFCS_PUSHED : 0) |
358 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
360 /* Draw the decr button */
361 UPDOWN_GetArrowRect(infoPtr, &rect, FLAG_DECR);
362 pressed = (infoPtr->Flags & FLAG_PRESSED) && (infoPtr->Flags & FLAG_DECR);
363 hot = (infoPtr->Flags & FLAG_DECR) && (infoPtr->Flags & FLAG_MOUSEIN);
364 DrawFrameControl(hdc, &rect, DFC_SCROLL,
365 (dwStyle & UDS_HORZ ? DFCS_SCROLLLEFT : DFCS_SCROLLDOWN) |
366 ((dwStyle & UDS_HOTTRACK) && hot ? DFCS_HOT : 0) |
367 (pressed ? DFCS_PUSHED : 0) |
368 (dwStyle & WS_DISABLED ? DFCS_INACTIVE : 0) );
370 return 0;
373 /***********************************************************************
374 * UPDOWN_Paint
376 * Asynchronous drawing (must ONLY be used in WM_PAINT).
377 * Calls UPDOWN_Draw.
379 static LRESULT UPDOWN_Paint (UPDOWN_INFO *infoPtr, HDC hdc)
381 PAINTSTRUCT ps;
382 if (hdc) return UPDOWN_Draw (infoPtr, hdc);
383 hdc = BeginPaint (infoPtr->Self, &ps);
384 UPDOWN_Draw (infoPtr, hdc);
385 EndPaint (infoPtr->Self, &ps);
386 return 0;
389 /***********************************************************************
390 * UPDOWN_KeyPressed
392 * Handle key presses (up & down) when we have to do so
394 static LRESULT UPDOWN_KeyPressed(UPDOWN_INFO *infoPtr, int key)
396 int arrow;
398 if (key == VK_UP) arrow = FLAG_INCR;
399 else if (key == VK_DOWN) arrow = FLAG_DECR;
400 else return 1;
402 UPDOWN_GetBuddyInt (infoPtr);
403 infoPtr->Flags &= ~FLAG_ARROW;
404 infoPtr->Flags |= FLAG_PRESSED | arrow;
405 InvalidateRect (infoPtr->Self, NULL, FALSE);
406 SetTimer(infoPtr->Self, TIMER_AUTOPRESS, AUTOPRESS_DELAY, 0);
407 UPDOWN_DoAction (infoPtr, 1, arrow);
408 return 0;
411 /***********************************************************************
412 * UPDOWN_Buddy_SubclassProc used to handle messages sent to the buddy
413 * control.
415 static LRESULT CALLBACK
416 UPDOWN_Buddy_SubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
418 WNDPROC superClassWndProc = (WNDPROC)GetPropA(hwnd, BUDDY_SUPERCLASS_WNDPROC);
419 TRACE("hwnd=%04x, wndProc=%d, uMsg=%04x, wParam=%d, lParam=%d\n",
420 hwnd, (INT)superClassWndProc, uMsg, wParam, (UINT)lParam);
422 if (uMsg == WM_KEYDOWN) {
423 HWND upDownHwnd = GetPropA(hwnd, BUDDY_UPDOWN_HWND);
425 UPDOWN_KeyPressed(UPDOWN_GetInfoPtr(upDownHwnd), (int)wParam);
428 return CallWindowProcW( superClassWndProc, hwnd, uMsg, wParam, lParam);
431 /***********************************************************************
432 * UPDOWN_SetBuddy
433 * Tests if 'bud' is a valid window handle. If not, returns FALSE.
434 * Else, sets it as a new Buddy.
435 * Then, it should subclass the buddy
436 * If window has the UDS_ARROWKEYS, it subcalsses the buddy window to
437 * process the UP/DOWN arrow keys.
438 * If window has the UDS_ALIGNLEFT or UDS_ALIGNRIGHT style
439 * the size/pos of the buddy and the control are adjusted accordingly.
441 static BOOL UPDOWN_SetBuddy (UPDOWN_INFO* infoPtr, HWND bud)
443 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
444 RECT budRect; /* new coord for the buddy */
445 int x, width; /* new x position and width for the up-down */
446 WNDPROC baseWndProc;
447 CHAR buddyClass[40];
449 /* Is it a valid bud? */
450 if(!IsWindow(bud)) return FALSE;
452 TRACE("(hwnd=%04x, bud=%04x)\n", infoPtr->Self, bud);
454 /* there is already a body assigned */
455 if (infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
457 /* Store buddy window handle */
458 infoPtr->Buddy = bud;
460 /* keep upDown ctrl hwnd in a buddy property */
461 SetPropA( bud, BUDDY_UPDOWN_HWND, infoPtr->Self);
463 /* Store buddy window class type */
464 infoPtr->BuddyType = BUDDY_TYPE_UNKNOWN;
465 if (GetClassNameA(bud, buddyClass, COUNT_OF(buddyClass))) {
466 if (lstrcmpiA(buddyClass, "Edit") == 0)
467 infoPtr->BuddyType = BUDDY_TYPE_EDIT;
468 else if (lstrcmpiA(buddyClass, "Listbox") == 0)
469 infoPtr->BuddyType = BUDDY_TYPE_LISTBOX;
472 if(dwStyle & UDS_ARROWKEYS){
473 /* Note that I don't clear the BUDDY_SUPERCLASS_WNDPROC property
474 when we reset the upDown ctrl buddy to another buddy because it is not
475 good to break the window proc chain. */
476 if (!GetPropA(bud, BUDDY_SUPERCLASS_WNDPROC)) {
477 baseWndProc = (WNDPROC)SetWindowLongW(bud, GWL_WNDPROC, (LPARAM)UPDOWN_Buddy_SubclassProc);
478 SetPropA(bud, BUDDY_SUPERCLASS_WNDPROC, (HANDLE)baseWndProc);
482 /* Get the rect of the buddy relative to its parent */
483 GetWindowRect(infoPtr->Buddy, &budRect);
484 MapWindowPoints(HWND_DESKTOP, GetParent(infoPtr->Buddy), (POINT *)(&budRect.left), 2);
486 /* now do the positioning */
487 if (dwStyle & UDS_ALIGNLEFT) {
488 x = budRect.left;
489 budRect.left += DEFAULT_WIDTH + DEFAULT_XSEP;
490 } else if (dwStyle & UDS_ALIGNRIGHT) {
491 budRect.right -= DEFAULT_WIDTH + DEFAULT_XSEP;
492 x = budRect.right+DEFAULT_XSEP;
493 } else {
494 x = budRect.right+DEFAULT_XSEP;
497 /* first adjust the buddy to accomodate the up/down */
498 SetWindowPos(infoPtr->Buddy, 0, budRect.left, budRect.top,
499 budRect.right - budRect.left, budRect.bottom - budRect.top,
500 SWP_NOACTIVATE|SWP_NOZORDER);
502 /* now position the up/down */
503 /* Since the UDS_ALIGN* flags were used, */
504 /* we will pick the position and size of the window. */
505 width = DEFAULT_WIDTH;
508 * If the updown has a buddy border, it has to overlap with the buddy
509 * to look as if it is integrated with the buddy control.
510 * We nudge the control or change it size to overlap.
512 if (UPDOWN_HasBuddyBorder(infoPtr)) {
513 if(dwStyle & UDS_ALIGNLEFT)
514 width += DEFAULT_BUDDYBORDER;
515 else
516 x -= DEFAULT_BUDDYBORDER;
519 SetWindowPos(infoPtr->Self, infoPtr->Buddy, x,
520 budRect.top - DEFAULT_ADDTOP, width,
521 budRect.bottom - budRect.top + DEFAULT_ADDTOP + DEFAULT_ADDBOT,
522 SWP_NOACTIVATE);
524 return TRUE;
527 /***********************************************************************
528 * UPDOWN_DoAction
530 * This function increments/decrements the CurVal by the
531 * 'delta' amount according to the 'action' flag which can be a
532 * combination of FLAG_INCR and FLAG_DECR
533 * It notifies the parent as required.
534 * It handles wraping and non-wraping correctly.
535 * It is assumed that delta>0
537 static void UPDOWN_DoAction (UPDOWN_INFO *infoPtr, int delta, int action)
539 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
540 NM_UPDOWN ni;
542 TRACE("%d by %d\n", action, delta);
544 /* check if we can do the modification first */
545 delta *= (action & FLAG_INCR ? 1 : -1) * (infoPtr->MaxVal < infoPtr->MinVal ? -1 : 1);
546 if ( (action & FLAG_INCR) && (action & FLAG_DECR) ) delta = 0;
548 /* We must notify parent now to obtain permission */
549 ni.iPos = infoPtr->CurVal;
550 ni.iDelta = delta;
551 ni.hdr.hwndFrom = infoPtr->Self;
552 ni.hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
553 ni.hdr.code = UDN_DELTAPOS;
554 if (!SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY,
555 (WPARAM)ni.hdr.idFrom, (LPARAM)&ni)) {
556 /* Parent said: OK to adjust */
558 /* Now adjust value with (maybe new) delta */
559 if (UPDOWN_OffsetVal (infoPtr, ni.iDelta)) {
560 /* Now take care about our buddy */
561 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
565 /* Also, notify it. This message is sent in any case. */
566 SendMessageW( GetParent(infoPtr->Self),
567 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
568 MAKELONG(SB_THUMBPOSITION, infoPtr->CurVal),
569 (LPARAM)infoPtr->Self);
572 /***********************************************************************
573 * UPDOWN_IsEnabled
575 * Returns TRUE if it is enabled as well as its buddy (if any)
576 * FALSE otherwise
578 static BOOL UPDOWN_IsEnabled (UPDOWN_INFO *infoPtr)
580 if(GetWindowLongW (infoPtr->Self, GWL_STYLE) & WS_DISABLED)
581 return FALSE;
582 if(infoPtr->Buddy)
583 return IsWindowEnabled(infoPtr->Buddy);
584 return TRUE;
587 /***********************************************************************
588 * UPDOWN_CancelMode
590 * Deletes any timers, releases the mouse and does redraw if necessary.
591 * If the control is not in "capture" mode, it does nothing.
592 * If the control was not in cancel mode, it returns FALSE.
593 * If the control was in cancel mode, it returns TRUE.
595 static BOOL UPDOWN_CancelMode (UPDOWN_INFO *infoPtr)
597 if (!(infoPtr->Flags & FLAG_PRESSED)) return FALSE;
599 KillTimer (infoPtr->Self, TIMER_AUTOREPEAT);
600 KillTimer (infoPtr->Self, TIMER_ACCEL);
601 KillTimer (infoPtr->Self, TIMER_AUTOPRESS);
603 if (GetCapture() == infoPtr->Self) {
604 NMHDR hdr;
605 hdr.hwndFrom = infoPtr->Self;
606 hdr.idFrom = GetWindowLongW (infoPtr->Self, GWL_ID);
607 hdr.code = NM_RELEASEDCAPTURE;
608 SendMessageW(GetParent (infoPtr->Self), WM_NOTIFY, hdr.idFrom, (LPARAM)&hdr);
609 ReleaseCapture();
612 infoPtr->Flags &= ~FLAG_PRESSED;
613 InvalidateRect (infoPtr->Self, NULL, FALSE);
615 return TRUE;
618 /***********************************************************************
619 * UPDOWN_HandleMouseEvent
621 * Handle a mouse event for the updown.
622 * 'pt' is the location of the mouse event in client or
623 * windows coordinates.
625 static void UPDOWN_HandleMouseEvent (UPDOWN_INFO *infoPtr, UINT msg, POINTS pts)
627 DWORD dwStyle = GetWindowLongW (infoPtr->Self, GWL_STYLE);
628 POINT pt = { pts.x, pts.y };
629 RECT rect;
630 int temp, arrow;
632 switch(msg)
634 case WM_LBUTTONDOWN: /* Initialise mouse tracking */
635 /* If we are inside an arrow, then nothing to do */
636 if(!(infoPtr->Flags & FLAG_MOUSEIN)) return;
638 /* If the buddy is an edit, will set focus to it */
639 if (UPDOWN_IsBuddyEdit(infoPtr)) SetFocus(infoPtr->Buddy);
641 /* Now see which one is the 'active' arrow */
642 if (infoPtr->Flags & FLAG_ARROW) {
644 /* Update the CurVal if necessary */
645 if (dwStyle & UDS_SETBUDDYINT) UPDOWN_GetBuddyInt (infoPtr);
647 /* Set up the correct flags */
648 infoPtr->Flags |= FLAG_PRESSED;
650 /* repaint the control */
651 InvalidateRect (infoPtr->Self, NULL, FALSE);
653 /* process the click */
654 UPDOWN_DoAction (infoPtr, 1, infoPtr->Flags & FLAG_ARROW);
656 /* now capture all mouse messages */
657 SetCapture (infoPtr->Self);
659 /* and startup the first timer */
660 SetTimer(infoPtr->Self, TIMER_AUTOREPEAT, INITIAL_DELAY, 0);
662 break;
664 case WM_MOUSEMOVE:
665 /* save the flags to see if any got modified */
666 temp = infoPtr->Flags;
668 /* Now see which one is the 'active' arrow */
669 arrow = UPDOWN_GetArrowFromPoint (infoPtr, &rect, pt);
671 /* Update the flags if we are in/out */
672 infoPtr->Flags &= ~(FLAG_MOUSEIN | FLAG_ARROW);
673 if(arrow) {
674 infoPtr->Flags |= FLAG_MOUSEIN | arrow;
675 } else {
676 if(infoPtr->AccelIndex != -1) infoPtr->AccelIndex = 0;
679 /* If state changed, redraw the control */
680 if(temp != infoPtr->Flags)
681 InvalidateRect (infoPtr->Self, &rect, FALSE);
682 break;
684 default:
685 ERR("Impossible case (msg=%x)!\n", msg);
690 /***********************************************************************
691 * UpDownWndProc
693 static LRESULT WINAPI UpDownWindowProc(HWND hwnd, UINT message, WPARAM wParam,
694 LPARAM lParam)
696 UPDOWN_INFO *infoPtr = UPDOWN_GetInfoPtr (hwnd);
697 DWORD dwStyle = GetWindowLongW (hwnd, GWL_STYLE);
698 int temp;
700 if (!infoPtr && (message != WM_CREATE))
701 return DefWindowProcW (hwnd, message, wParam, lParam);
703 switch(message)
705 case WM_CREATE:
706 SetWindowLongW (hwnd, GWL_STYLE, dwStyle & ~WS_BORDER);
707 infoPtr = (UPDOWN_INFO*)COMCTL32_Alloc (sizeof(UPDOWN_INFO));
708 SetWindowLongW (hwnd, 0, (DWORD)infoPtr);
710 /* initialize the info struct */
711 infoPtr->Self = hwnd;
712 infoPtr->AccelCount = 0;
713 infoPtr->AccelVect = 0;
714 infoPtr->AccelIndex = -1;
715 infoPtr->CurVal = 0;
716 infoPtr->MinVal = 0;
717 infoPtr->MaxVal = 9999;
718 infoPtr->Base = 10; /* Default to base 10 */
719 infoPtr->Buddy = 0; /* No buddy window yet */
720 infoPtr->Flags = 0; /* And no flags */
722 /* Do we pick the buddy win ourselves? */
723 if (dwStyle & UDS_AUTOBUDDY)
724 UPDOWN_SetBuddy (infoPtr, GetWindow (hwnd, GW_HWNDPREV));
726 TRACE("UpDown Ctrl creation, hwnd=%04x\n", hwnd);
727 break;
729 case WM_DESTROY:
730 if(infoPtr->AccelVect) COMCTL32_Free (infoPtr->AccelVect);
732 if(infoPtr->Buddy) RemovePropA(infoPtr->Buddy, BUDDY_UPDOWN_HWND);
734 COMCTL32_Free (infoPtr);
735 SetWindowLongW (hwnd, 0, 0);
736 TRACE("UpDown Ctrl destruction, hwnd=%04x\n", hwnd);
737 break;
739 case WM_ENABLE:
740 if (dwStyle & WS_DISABLED) UPDOWN_CancelMode (infoPtr);
741 InvalidateRect (infoPtr->Self, NULL, FALSE);
742 break;
744 case WM_TIMER:
745 /* is this the auto-press timer? */
746 if(wParam == TIMER_AUTOPRESS) {
747 KillTimer(hwnd, TIMER_AUTOPRESS);
748 infoPtr->Flags &= ~(FLAG_PRESSED | FLAG_ARROW);
749 InvalidateRect(infoPtr->Self, NULL, FALSE);
752 /* if initial timer, kill it and start the repeat timer */
753 if(wParam == TIMER_AUTOREPEAT) {
754 KillTimer(hwnd, TIMER_AUTOREPEAT);
755 /* if no accel info given, used default timer */
756 if(infoPtr->AccelCount==0 || infoPtr->AccelVect==0) {
757 infoPtr->AccelIndex = -1;
758 temp = REPEAT_DELAY;
759 } else {
760 infoPtr->AccelIndex = 0; /* otherwise, use it */
761 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
763 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
766 /* now, if the mouse is above us, do the thing...*/
767 if(infoPtr->Flags & FLAG_MOUSEIN) {
768 temp = infoPtr->AccelIndex == -1 ? 1 : infoPtr->AccelVect[infoPtr->AccelIndex].nInc;
769 UPDOWN_DoAction(infoPtr, temp, infoPtr->Flags & FLAG_ARROW);
771 if(infoPtr->AccelIndex != -1 && infoPtr->AccelIndex < infoPtr->AccelCount-1) {
772 KillTimer(hwnd, TIMER_ACCEL);
773 infoPtr->AccelIndex++; /* move to the next accel info */
774 temp = infoPtr->AccelVect[infoPtr->AccelIndex].nSec * 1000 + 1;
775 /* make sure we have at least 1ms intervals */
776 SetTimer(hwnd, TIMER_ACCEL, temp, 0);
779 break;
781 case WM_CANCELMODE:
782 return UPDOWN_CancelMode (infoPtr);
784 case WM_LBUTTONUP:
785 if (GetCapture() != infoPtr->Self) break;
787 if ( (infoPtr->Flags & FLAG_MOUSEIN) &&
788 (infoPtr->Flags & FLAG_ARROW) ) {
790 SendMessageW( GetParent(hwnd),
791 dwStyle & UDS_HORZ ? WM_HSCROLL : WM_VSCROLL,
792 MAKELONG(SB_ENDSCROLL, infoPtr->CurVal),
793 (LPARAM)hwnd);
794 if (UPDOWN_IsBuddyEdit(infoPtr))
795 SendMessageW(infoPtr->Buddy, EM_SETSEL, 0, MAKELONG(0, -1));
797 UPDOWN_CancelMode(infoPtr);
798 break;
800 case WM_LBUTTONDOWN:
801 case WM_MOUSEMOVE:
802 if(UPDOWN_IsEnabled(infoPtr))
803 UPDOWN_HandleMouseEvent (infoPtr, message, MAKEPOINTS(lParam));
804 break;
806 case WM_KEYDOWN:
807 if((dwStyle & UDS_ARROWKEYS) && UPDOWN_IsEnabled(infoPtr))
808 return UPDOWN_KeyPressed(infoPtr, (int)wParam);
809 break;
811 case WM_PAINT:
812 return UPDOWN_Paint (infoPtr, (HDC)wParam);
814 case UDM_GETACCEL:
815 if (wParam==0 && lParam==0) return infoPtr->AccelCount;
816 if (wParam && lParam) {
817 temp = min(infoPtr->AccelCount, wParam);
818 memcpy((void *)lParam, infoPtr->AccelVect, temp*sizeof(UDACCEL));
819 return temp;
821 return 0;
823 case UDM_SETACCEL:
824 TRACE("UpDown Ctrl new accel info, hwnd=%04x\n", hwnd);
825 if(infoPtr->AccelVect) {
826 COMCTL32_Free (infoPtr->AccelVect);
827 infoPtr->AccelCount = 0;
828 infoPtr->AccelVect = 0;
830 if(wParam==0) return TRUE;
831 infoPtr->AccelVect = COMCTL32_Alloc (wParam*sizeof(UDACCEL));
832 if(infoPtr->AccelVect == 0) return FALSE;
833 memcpy(infoPtr->AccelVect, (void*)lParam, wParam*sizeof(UDACCEL));
834 return TRUE;
836 case UDM_GETBASE:
837 return infoPtr->Base;
839 case UDM_SETBASE:
840 TRACE("UpDown Ctrl new base(%d), hwnd=%04x\n", wParam, hwnd);
841 if (wParam==10 || wParam==16) {
842 temp = infoPtr->Base;
843 infoPtr->Base = wParam;
844 return temp;
846 break;
848 case UDM_GETBUDDY:
849 return (LRESULT)infoPtr->Buddy;
851 case UDM_SETBUDDY:
852 temp = (int)infoPtr->Buddy;
853 UPDOWN_SetBuddy (infoPtr, (HWND)wParam);
854 return temp;
856 case UDM_GETPOS:
857 temp = UPDOWN_GetBuddyInt (infoPtr);
858 return MAKELONG(infoPtr->CurVal, temp ? 0 : 1);
860 case UDM_SETPOS:
861 temp = SLOWORD(lParam);
862 TRACE("UpDown Ctrl new value(%d), hwnd=%04x\n", temp, hwnd);
863 if(!UPDOWN_InBounds(infoPtr, temp)) {
864 if(temp < infoPtr->MinVal) temp = infoPtr->MinVal;
865 if(temp > infoPtr->MaxVal) temp = infoPtr->MaxVal;
867 wParam = infoPtr->CurVal;
868 infoPtr->CurVal = temp;
869 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
870 return wParam; /* return prev value */
872 case UDM_GETRANGE:
873 return MAKELONG(infoPtr->MaxVal, infoPtr->MinVal);
875 case UDM_SETRANGE:
876 /* we must have: */
877 infoPtr->MaxVal = SLOWORD(lParam); /* UD_MINVAL <= Max <= UD_MAXVAL */
878 infoPtr->MinVal = SHIWORD(lParam); /* UD_MINVAL <= Min <= UD_MAXVAL */
879 /* |Max-Min| <= UD_MAXVAL */
880 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
881 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
882 break;
884 case UDM_GETRANGE32:
885 if (wParam) *(LPINT)wParam = infoPtr->MinVal;
886 if (lParam) *(LPINT)lParam = infoPtr->MaxVal;
887 break;
889 case UDM_SETRANGE32:
890 infoPtr->MinVal = (INT)wParam;
891 infoPtr->MaxVal = (INT)lParam;
892 if (infoPtr->MaxVal <= infoPtr->MinVal)
893 infoPtr->MaxVal = infoPtr->MinVal + 1;
894 TRACE("UpDown Ctrl new range(%d to %d), hwnd=%04x\n",
895 infoPtr->MinVal, infoPtr->MaxVal, hwnd);
896 break;
898 case UDM_GETPOS32:
899 if ((LPBOOL)lParam != NULL) *((LPBOOL)lParam) = TRUE;
900 return infoPtr->CurVal;
902 case UDM_SETPOS32:
903 if(!UPDOWN_InBounds(infoPtr, (int)lParam)) {
904 if((int)lParam < infoPtr->MinVal) lParam = infoPtr->MinVal;
905 if((int)lParam > infoPtr->MaxVal) lParam = infoPtr->MaxVal;
907 temp = infoPtr->CurVal; /* save prev value */
908 infoPtr->CurVal = (int)lParam; /* set the new value */
909 if(dwStyle & UDS_SETBUDDYINT) UPDOWN_SetBuddyInt (infoPtr);
910 return temp; /* return prev value */
912 case UDM_GETUNICODEFORMAT:
913 /* we lie a bit here, we're always using Unicode internally */
914 return infoPtr->UnicodeFormat;
916 case UDM_SETUNICODEFORMAT:
917 /* do we really need to honour this flag? */
918 temp = infoPtr->UnicodeFormat;
919 infoPtr->UnicodeFormat = (BOOL)wParam;
920 return temp;
922 default:
923 if ((message >= WM_USER) && (message < WM_APP))
924 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam);
925 return DefWindowProcW (hwnd, message, wParam, lParam);
928 return 0;
931 /***********************************************************************
932 * UPDOWN_Register [Internal]
934 * Registers the updown window class.
936 void UPDOWN_Register(void)
938 WNDCLASSW wndClass;
940 ZeroMemory( &wndClass, sizeof( WNDCLASSW ) );
941 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW;
942 wndClass.lpfnWndProc = (WNDPROC)UpDownWindowProc;
943 wndClass.cbClsExtra = 0;
944 wndClass.cbWndExtra = sizeof(UPDOWN_INFO*);
945 wndClass.hCursor = LoadCursorW( 0, IDC_ARROWW );
946 wndClass.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
947 wndClass.lpszClassName = UPDOWN_CLASSW;
949 RegisterClassW( &wndClass );
953 /***********************************************************************
954 * UPDOWN_Unregister [Internal]
956 * Unregisters the updown window class.
958 void UPDOWN_Unregister (void)
960 UnregisterClassW (UPDOWN_CLASSW, (HINSTANCE)NULL);